Difference between revisions of "Scaling Rotating Mirroring Clipping"

From Wiki
Jump to navigation Jump to search
(→‎\clip =: clipping sampl)
m (→‎Rotating: add mirror)
(27 intermediate revisions by 9 users not shown)
Line 1: Line 1:
In [http://source.contextgarden.net/tex/context/base/core-mis.mkii core-mis.mkii] ([http://source.contextgarden.net/tex/context/base/core-mis.mkiv core-mis.mkiv] for MkIV) there are some handy command for graphical text manipulation defined.
+
You can use the commands for text, but also for graphics or other “building blocks”.
  
== \scale ==
+
=Scaling=
  
You can insert symbols at a reduced size in the text flow. This example creates a filled square scaled to .5 ex height
+
The {{cmd|scale}} command is all-purpose, you can scale by factors or to a defined size.
  
<texcode>
+
As a special case, you can insert symbols at a reduced size in the text flow using {{cmd|getscaledglyph}}.
\definefontsynonym[Dingbats][uzdr]
 
\definesymbol[filledSq][\getglyph{Dingbats}{\char110}]
 
\def\FilledSquare%
 
    {\dontleavehmode\scale[height=0.5ex]{\symbol[filledSq]}}
 
</texcode>
 
  
In TeX terms, \scale creates a \hbox. If TeX is in vertical
+
<context source=yes>
mode (as it is when \startlines is in effect, because every line
+
\getscaledglyph{.5}{Serif}{a} a \getscaledglyph{2}{Serif}{a}
is a paragraph by itself in the mode), then a simple \hbox is
+
</context>
not a good enough hint to make it switch to horizontal (paragraph)
 
mode.
 
  
Hans then posted a simpler way.
 
<texcode>
 
\getscaledglyph{.5}{Serif}{a}
 
</texcode>
 
  
{{todo|An explanation and example of the <cmd>\scale</cmd> command.}}
+
= Mirroring =
 
+
<context source=yes>
== \mirror ==
+
There is a \mirror{\sl mirorred} {\sl word} in this sentence.
 
 
<texcode>
 
There is a \mirror{\sl mirorred} word in this sentence.
 
</texcode>
 
 
 
<context>
 
There is a \mirror{\sl mirorred} word in this sentence.
 
 
</context>
 
</context>
  
<code>\mirror</code> makes a <code>\hbox</code> and mirrors the contents. If you want to have a longer paragraph mirrored, you have to make a <code>\vbox</code> manually.
+
{{cmd|mirror}} makes a <code>\hbox</code> and mirrors the contents. If you want to have a longer paragraph mirrored, you have to make a <code>\vbox</code> manually.
  
<texcode>
+
<context source=yes>
 
\mirror{\vbox{Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do:  
 
\mirror{\vbox{Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do:  
once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, `and  
+
once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, \quote{and  
what is the use of a book,' thought Alice `without pictures or conversation?'}}
+
what is the use of a book,} thought Alice \quote{without pictures or conversation?}}}
</texcode>
+
</context>
 
 
  
<context>
+
= Rotating =
\mirror{\vbox{Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do:
 
once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, `and
 
what is the use of a book,' thought Alice `without pictures or conversation?'}}
 
</context>
 
  
== \rotate ==
+
You can {{cmd|rotate}} something by an arbitrary angle like
  
You can rotate something by an arbitrary angle by saying something like
+
<context source=yes>
<texcode>
 
 
\rotate[rotation=42]{the ultimate answer}
 
\rotate[rotation=42]{the ultimate answer}
</texcode>.
+
</context>
<context>
 
\rotate[rotation=42]{the ultimate answer}
 
</context>.
 
  
 
The rotation angle and the location (which determines around which point the content is rotated) are optional and can also be set by
 
The rotation angle and the location (which determines around which point the content is rotated) are optional and can also be set by
<texcode>
+
 
 +
<context source=yes>
 
\setuprotate[rotation=42,location=normal] % normal is the default
 
\setuprotate[rotation=42,location=normal] % normal is the default
 
\rotate{the ultimate answer}
 
\rotate{the ultimate answer}
</texcode>
+
</context>
 +
 
 +
<context source=yes>
 +
\ss Next generation L\rotate{m}\rotate{m}TSP\mirror{3}Al\rotate[rotation=270,location=high]{v}?
 +
</context>
 +
 
 +
You can rotate a long paragraph as well.
 +
 
 +
==Location parameter==
  
An example by Hans Hagen showing the influence of <code>location=</code> argument at different angles:
+
The influence of <code>location=</code> at different angles:
  
<texcode>
+
<context source=yes>
 
\dostepwiserecurse{0}{359}{45}
 
\dostepwiserecurse{0}{359}{45}
 
   {\startlinecorrection[blank]
 
   {\startlinecorrection[blank]
Line 80: Line 62:
 
       \hbox to .2\hsize{\hss\ruledhbox{\rotate[location=high]  {\ruledhbox{\bfb  (high)}}}}}
 
       \hbox to .2\hsize{\hss\ruledhbox{\rotate[location=high]  {\ruledhbox{\bfb  (high)}}}}}
 
     \stoplinecorrection}
 
     \stoplinecorrection}
 +
</context>
 +
 +
=== Rotating Figures ===
 +
 +
The following code shows how the location parameter influences rotation with a frame:
 +
 +
<texcode>
 +
\setupbodyfont[sans, 30pt]
 +
\setuppagenumbering[location=,]
 +
\showframe
 +
 +
\starttext
 +
    \topskip4cm
 +
        \dostepwiserecurse{0}{360}{10}{
 +
\doloopoverlist{normal,default,depth,fit,broad,high,middle}{
 +
\ruledhbox{\rotate[rotation=\recurselevel, location=\recursestring]{%
 +
\framed
 +
  [width=\textheight, height=\textwidth, rulethickness=5pt,
 +
    depth=0cm, foregroundstyle=\bfd]
 +
  {\recursestring, \recurselevel°}
 +
            }
 +
        }
 +
    }
 +
}
 +
\stoptext
 
</texcode>
 
</texcode>
  
<context>
+
or, even nicer:
\setuplayout[location=middle,scale=.5]
+
 
\dostepwiserecurse{0}{359}{45}
+
<context source="yes">
  {\startlinecorrection[blank]
+
\useMPlibrary[dum]
    \hbox
+
\setupexternalfigures[width=4em,height=3em,frame=on,]
      {\expanded{\setuprotate[rotation=\recurselevel]}%
+
\dostepwiserecurse{0}{359}{45}{{\bf\recurselevel˚}\quad
      \traceboxplacementtrue
+
  \doloopoverlist{normal,depth,fit,broad,high,middle}{
      \hbox to .2\hsize{\hss\ruledhbox{\rotate[location=depth] {\ruledhbox{\bfb  (depth)}}}}%
+
    \dontleavehmode\ruledhbox{%
      \hbox to .2\hsize{\hss\ruledhbox{\rotate[location=fit]   {\ruledhbox{\bfb    (fit)}}}}%
+
      \rotate[rotation=\recurselevel, location=\recursestring]{%
      \hbox to .2\hsize{\hss\ruledhbox{\rotate[location=broad] {\ruledhbox{\bfb  (broad)}}}}%
+
        \externalfigure[\recursestring]
      \hbox to .2\hsize{\hss\ruledhbox{\rotate[location=normal]{\ruledhbox{\bfb (normal)}}}}%
+
      }%
      \hbox to .2\hsize{\hss\ruledhbox{\rotate[location=high]  {\ruledhbox{\bfb   (high)}}}}}
+
    }%
    \stoplinecorrection}
+
   }\par
 +
}
 
</context>
 
</context>
  
You can rotate a long paragraph as well.
+
==In a box==
 
 
{{todo|Some more comments, and an example.}}
 
  
 
Depending on the usage you will sometimes probably want to include the content of what you wish to rotate into a box. Compare the following two examples:
 
Depending on the usage you will sometimes probably want to include the content of what you wish to rotate into a box. Compare the following two examples:
  
<texcode>
+
<context source=yes>
 
\setuplayout[width=3cm]
 
\setuplayout[width=3cm]
 
\setuprotate[rotation=180,location=broad]
 
\setuprotate[rotation=180,location=broad]
Line 109: Line 115:
 
a\rotate{e}i\par
 
a\rotate{e}i\par
 
a\rotate{\hbox{e}}i
 
a\rotate{\hbox{e}}i
 +
</context>
 +
 +
==Page dependent landscape figures==
 +
 +
Sometimes, in a double-sided portrait document, you may wish to rotate a landscape figure so that the top is towards the spine of the book – no matter whether it’s on an odd or even page. In this case the {{cmd|doifoddpageelse}} command can help:
 +
 +
<texcode>
 +
\rotate[rotation=\doifoddpageelse{90}{270}]{\externalfigure[cow]}
 
</texcode>
 
</texcode>
  
<context>
+
This example is by Wolfgang, from [http://www.ntg.nl/pipermail/ntg-context/2011/059834.html the thread starting here]. The thread contains other solutions to the problem.
\setuplayout[width=3cm]
 
\setuprotate[rotation=180,location=broad]
 
  
a\rotate{e}i\par
+
=Clipping=
a\rotate{\hbox{e}}i
 
</context>
 
  
== \clip ==
+
You can adjust the size of an element by cropping/clipping its borders:
(This is not just about text manipulation...)
 
  
 
<context source="yes">
 
<context source="yes">
Line 126: Line 135:
 
</context>
 
</context>
  
Not really perfect:
+
<context source="yes">
 +
\definedfont[SansBold at 20mm]
 +
\baselineskip 0pt % switch off the space between the stripes
 +
\lineskip 0pt
 +
\def\MyLogo{\vbox{\hbox{\CONTEXT}\null}} % null is necessary for the whole bounding box
 +
\clip[ny=3,y=1]{\color[yellow]{\MyLogo}}
 +
\clip[ny=3,y=2]{\color[orange]{\MyLogo}}
 +
\clip[ny=3,y=3]{\color[red]{\MyLogo}}
 +
</context>
 +
 
 +
And here’s how to cut out a piece by measures (see also {{cmd|setupclipping}} and {{cmd|clip}}):
  
 
<context source="yes">
 
<context source="yes">
\setupcolors[state=start]
+
\clip[width=3cm, height=1cm, hoffset=1cm, voffset=5mm]{\externalfigure[cow][width=5cm]}
\clip[ny=3,y=1]{\definedfont[SansBold at 36pt]\color[red]\CONTEXT}
 
\clip[ny=3,y=2]{\definedfont[SansBold at 36pt]\color[yellow]\CONTEXT}
 
\clip[ny=3,y=3]{\definedfont[SansBold at 36pt]\color[blue]\CONTEXT}
 
 
</context>
 
</context>
  
see <cmd>setupclipping</cmd> and <cmd>clip</cmd>
+
 
 +
=Further reading=
 +
 
 +
Graphic transformations are implemented in [https://source.contextgarden.net/tex/context/base/mkiv/grph-trf.mkiv grph-trf.mkiv] and [https://source.contextgarden.net/tex/context/base/mkiv/grph-trf.lua grph-trf.lua].
 +
 
 +
[[Category:Basics]]
 +
[[Category:Graphics]]

Revision as of 19:35, 1 May 2022

You can use the commands for text, but also for graphics or other “building blocks”.

Scaling

The \scale command is all-purpose, you can scale by factors or to a defined size.

As a special case, you can insert symbols at a reduced size in the text flow using \getscaledglyph.

\getscaledglyph{.5}{Serif}{a} a \getscaledglyph{2}{Serif}{a}


Mirroring

There is a \mirror{\sl mirorred} {\sl word} in this sentence.

\mirror makes a \hbox and mirrors the contents. If you want to have a longer paragraph mirrored, you have to make a \vbox manually.

\mirror{\vbox{Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: 
once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, \quote{and 
what is the use of a book,} thought Alice \quote{without pictures or conversation?}}}

Rotating

You can \rotate something by an arbitrary angle like

\rotate[rotation=42]{the ultimate answer}

The rotation angle and the location (which determines around which point the content is rotated) are optional and can also be set by

\setuprotate[rotation=42,location=normal] % normal is the default
\rotate{the ultimate answer}

\ss Next generation L\rotate{m}\rotate{m}TSP\mirror{3}Al\rotate[rotation=270,location=high]{v}?

You can rotate a long paragraph as well.

Location parameter

The influence of location= at different angles:

\dostepwiserecurse{0}{359}{45}
   {\startlinecorrection[blank]
    \hbox
      {\expanded{\setuprotate[rotation=\recurselevel]}%
       \traceboxplacementtrue
       \hbox to .2\hsize{\hss\ruledhbox{\rotate[location=depth] {\ruledhbox{\bfb  (depth)}}}}%
       \hbox to .2\hsize{\hss\ruledhbox{\rotate[location=fit]   {\ruledhbox{\bfb    (fit)}}}}%
       \hbox to .2\hsize{\hss\ruledhbox{\rotate[location=broad] {\ruledhbox{\bfb  (broad)}}}}%
       \hbox to .2\hsize{\hss\ruledhbox{\rotate[location=normal]{\ruledhbox{\bfb (normal)}}}}%
       \hbox to .2\hsize{\hss\ruledhbox{\rotate[location=high]  {\ruledhbox{\bfb   (high)}}}}}
    \stoplinecorrection}

Rotating Figures

The following code shows how the location parameter influences rotation with a frame:

\setupbodyfont[sans, 30pt]
\setuppagenumbering[location=,]
\showframe

\starttext
     \topskip4cm
        \dostepwiserecurse{0}{360}{10}{
\doloopoverlist{normal,default,depth,fit,broad,high,middle}{
\ruledhbox{\rotate[rotation=\recurselevel, location=\recursestring]{%
\framed
   [width=\textheight, height=\textwidth, rulethickness=5pt,
    depth=0cm, foregroundstyle=\bfd]
  {\recursestring, \recurselevel°}
            }
        }
    }
}
\stoptext

or, even nicer:

\useMPlibrary[dum]
\setupexternalfigures[width=4em,height=3em,frame=on,]
\dostepwiserecurse{0}{359}{45}{{\bf\recurselevel˚}\quad
  \doloopoverlist{normal,depth,fit,broad,high,middle}{
    \dontleavehmode\ruledhbox{%
      \rotate[rotation=\recurselevel, location=\recursestring]{%
        \externalfigure[\recursestring]
      }%
    }%
  }\par
}

In a box

Depending on the usage you will sometimes probably want to include the content of what you wish to rotate into a box. Compare the following two examples:

\setuplayout[width=3cm]
\setuprotate[rotation=180,location=broad]

a\rotate{e}i\par
a\rotate{\hbox{e}}i

Page dependent landscape figures

Sometimes, in a double-sided portrait document, you may wish to rotate a landscape figure so that the top is towards the spine of the book – no matter whether it’s on an odd or even page. In this case the \doifoddpageelse command can help:

\rotate[rotation=\doifoddpageelse{90}{270}]{\externalfigure[cow]}

This example is by Wolfgang, from the thread starting here. The thread contains other solutions to the problem.

Clipping

You can adjust the size of an element by cropping/clipping its borders:

\clip[nx=3,ny=3,x=1,y=1]{\externalfigure[cow]}

\definedfont[SansBold at 20mm]
\baselineskip 0pt % switch off the space between the stripes
\lineskip 0pt
\def\MyLogo{\vbox{\hbox{\CONTEXT}\null}} % null is necessary for the whole bounding box
\clip[ny=3,y=1]{\color[yellow]{\MyLogo}}
\clip[ny=3,y=2]{\color[orange]{\MyLogo}}
\clip[ny=3,y=3]{\color[red]{\MyLogo}}

And here’s how to cut out a piece by measures (see also \setupclipping and \clip):

\clip[width=3cm, height=1cm, hoffset=1cm, voffset=5mm]{\externalfigure[cow][width=5cm]}


Further reading

Graphic transformations are implemented in grph-trf.mkiv and grph-trf.lua.