Difference between revisions of "Overlays"
(17 intermediate revisions by 8 users not shown) | |||
Line 1: | Line 1: | ||
− | + | __TOC__ | |
− | + | Overlays are predefined snippets that you can stack on each other. In contrast to [[Layers|layers]], which regard whole pages, overlays mostly are only single pictures, words or the like. | |
− | + | = Defining = | |
− | |||
− | |||
− | |||
<texcode> | <texcode> | ||
Line 14: | Line 11: | ||
Better put your commands in braces. | Better put your commands in braces. | ||
− | + | = Using = | |
+ | |||
+ | You can use defined overlays anywhere there is a <tt>background</tt> option. | ||
+ | |||
+ | <context source=yes> | ||
+ | \setupcolors[state=start] | ||
+ | |||
+ | \defineoverlay[tea][{\color[green]{\ss\bf GREEN TEA}}] | ||
+ | |||
+ | \framed[ | ||
+ | width=8em, | ||
+ | background=tea, | ||
+ | align={middle,hilo}, | ||
+ | style=italicface, | ||
+ | ]{ | ||
+ | today | ||
+ | \blank[small] | ||
+ | for sale | ||
+ | } | ||
+ | </context> | ||
+ | |||
+ | You can use this also with pictures. See also [[Logos|logos]]. | ||
+ | |||
+ | Another use for overlays is for adding a graphic to every page to indicate chapters (sometimes known as a [[Thumb index|thumb index]]). | ||
+ | |||
+ | = Overlays vs. Layers = | ||
+ | |||
+ | Overlays and Layers are different mechanisms, which are powerful when combined. | ||
+ | |||
+ | == Overlays == | ||
+ | |||
+ | ConTeXt has an overlay environment that can be used in the text to stack the arguments on the same spot. In the example below, each new element is placed on top of the previous element. | ||
+ | |||
+ | <context source=yes> | ||
+ | \setupbodyfont[40pt] | ||
+ | |||
+ | \startoverlay | ||
+ | {\strut\red m} | ||
+ | {\strut\green a} | ||
+ | {\strut\blue l} | ||
+ | \stopoverlay | ||
+ | </context> | ||
+ | |||
+ | Without the \strut argument each argument is horizontally and vertically centered: | ||
+ | |||
+ | <context source=yes> | ||
+ | \setupbodyfont[40pt] | ||
+ | |||
+ | \startoverlay | ||
+ | {\red m} | ||
+ | {\green a} | ||
+ | {\blue l} | ||
+ | \stopoverlay | ||
+ | </context> | ||
+ | |||
+ | |||
+ | Overlays are created with the \defineoverlay command, which takes an overlay name and content. The overlays are then applied to a \framed command or the background to the page the textblock, header, and so forth. | ||
− | + | The next example shows two overlays (“behind” and “before”), which are activated in the frame with a background key. | |
− | < | + | <context source=yes> |
− | \defineoverlay[ | + | \defineoverlay[behind][{\blackrule[height=2cm,width=8cm,color=yellow]}] |
+ | \defineoverlay[before][{\blackrule[height=8cm,width=2cm,color=orange]}] | ||
+ | |||
+ | \framed | ||
+ | [width=10cm, | ||
+ | height=10cm, | ||
+ | background={color,behind,foreground,before}, | ||
+ | backgroundcolor=red] | ||
+ | {\blackrule[width=6cm,height=6cm,color=blue]} | ||
+ | </context> | ||
+ | |||
+ | The arguments of the background key used two names in addition to the “behind” and “before” overlays. The first name is “color”, which is needed when using the backgroundcolor key to put a color in the entire frame's area. The second is “foreground”, which can place overlays *before* the content of the frame because “foreground” is the frame text itself. | ||
+ | |||
+ | The overlay environment and overlay mechanism for \framed are similar. | ||
+ | |||
+ | The methods to set them because in the first case you set the content for each level which the argument in braces ({…}{…}) and in the second case you define them first with \defineoverlay and set the order in the background key. | ||
+ | |||
+ | == Layers == | ||
+ | |||
+ | With layers the content from each command can be positioned at a certain horizontal and vertical position. The following example creates a new layer and set three texts. The texts are placed in the page using the \flushlayer command. | ||
+ | |||
+ | <context source=yes> | ||
+ | \starttext | ||
+ | |||
+ | \definelayer[test] | ||
+ | |||
+ | \setlayer[test][x=3cm,y=6cm]{ONE} | ||
+ | \setlayer[test][x=8cm,y=2cm]{TWO} | ||
+ | \setlayer[test][x=2cm,y=9cm]{THREE} | ||
+ | |||
+ | \flushlayer[test] | ||
+ | |||
+ | \stoptext | ||
+ | </context> | ||
+ | |||
+ | When you put new some text after \flushlayer would be surprised because it would end put on the following page but you shouldn’t wonder about this because layers have nothing to do with the page background or foreground. | ||
+ | |||
+ | What makes the layer and overlay mechanism is interesting is when you combine them and put the \flushlayer command in a overlay which does now place the texts before or after the content of a frame. With my next example we’re finally in the process to combine them. | ||
+ | |||
+ | <context source=yes> | ||
+ | \setupbodyfont[30pt] | ||
+ | |||
+ | \starttext | ||
+ | |||
+ | \definelayer[behindtext] | ||
+ | \definelayer[beforetext] | ||
+ | |||
+ | \setupbackgrounds[text][background={behindtext,foreground,beforetext}] | ||
+ | |||
+ | \setlayer[behindtext][x=-2cm,y=.75ex]{\red Behind} | ||
+ | \setlayer[beforetext][x=+2cm,y=.75ex]{\yellow Before} | ||
+ | |||
+ | The Text! | ||
+ | |||
+ | \setlayer[behindtext][x=5cm,y=4cm]{\red More background text!} | ||
+ | \setlayer[beforetext][x=3cm,y=8cm]{\yellow More foreground text!} | ||
+ | |||
+ | \stoptext | ||
+ | </context> | ||
+ | |||
+ | When you look at the example you can see that haven't created a overlay for layer even though I said you need it to put the layer after the text. This isn’t necessary because when you create a new layer context does also create a overlay with the same name in which the layer is placed. The other question is how you can place overlays on the text/page when I told before overalls can only be placed on frame, this is possible because all page elements (textblock, header, footer and even the page and paper itself) are frames. Even though these elements are frames they don’t provide all the options from \framed because they are a special version with less functionality. | ||
+ | |||
+ | = Example of circled numbers = | ||
+ | |||
+ | <context source="yes"> | ||
+ | % Solution A - a framed that uses an overlay as background ========== | ||
+ | |||
+ | \startuseMPgraphic{MPtextcircle} | ||
+ | path p; | ||
+ | p := fullcircle xscaled OverlayWidth | ||
+ | yscaled OverlayWidth; | ||
+ | setbounds currentpicture to p ; | ||
+ | % p:= p shifted (0,StrutDepth/2.7); | ||
+ | draw p withpen pencircle scaled 0.25pt | ||
+ | withcolor OverlayLineColor; | ||
+ | \stopuseMPgraphic | ||
+ | |||
+ | \defineoverlay[OLtextcircle][\useMPgraphic{MPtextcircle}] | ||
+ | |||
+ | \defineframed | ||
+ | [textcircledA] | ||
+ | [ | ||
+ | frame=off, | ||
+ | strut=yes, | ||
+ | rulethickness=0.0pt, | ||
+ | background=OLtextcircle, | ||
+ | framecolor=darkblue, | ||
+ | location=top,] | ||
+ | |||
+ | % Solution B - same with fixed dimensions =========================== | ||
+ | |||
+ | \defineframed | ||
+ | [textcircledB] | ||
+ | [textcircledA] | ||
+ | [width=1.75em, | ||
+ | framecolor=darkred,] | ||
+ | |||
+ | % Solution C - a framed with radius ================================= | ||
+ | |||
+ | \defineframed | ||
+ | [textcircledC] | ||
+ | [width=1.5em, | ||
+ | frame=on, | ||
+ | strut=yes, | ||
+ | rulethickness=0.25pt, | ||
+ | framecolor=darkgreen, | ||
+ | offset=1pt, | ||
+ | location=top, | ||
+ | corner=round, | ||
+ | radius=.5em,] | ||
− | + | % =================================================================== | |
− | + | \setupbodyfont[pagella, 14pt] | |
− | |||
− | + | \dostepwiserecurse{0}{150}{20}{% | |
− | \ | + | #1 = \textcircledA{#1} = \textcircledB{#1} = \textcircledC{#1}\\} |
− | \ | ||
− | |||
− | |||
</context> | </context> | ||
− | + | = See Also = | |
− | + | * [[:Category:Command/Overlays]] | |
+ | * [[Layers]] – [[:Category:Command/Layers]] | ||
+ | * [[Collectors]] | ||
[[Category:Graphics]] | [[Category:Graphics]] | ||
− |
Latest revision as of 16:37, 2 June 2024
Contents
Overlays are predefined snippets that you can stack on each other. In contrast to layers, which regard whole pages, overlays mostly are only single pictures, words or the like.
Defining
\defineoverlay[reference name][commands]
Better put your commands in braces.
Using
You can use defined overlays anywhere there is a background option.
\setupcolors[state=start] \defineoverlay[tea][{\color[green]{\ss\bf GREEN TEA}}] \framed[ width=8em, background=tea, align={middle,hilo}, style=italicface, ]{ today \blank[small] for sale }
You can use this also with pictures. See also logos.
Another use for overlays is for adding a graphic to every page to indicate chapters (sometimes known as a thumb index).
Overlays vs. Layers
Overlays and Layers are different mechanisms, which are powerful when combined.
Overlays
ConTeXt has an overlay environment that can be used in the text to stack the arguments on the same spot. In the example below, each new element is placed on top of the previous element.
\setupbodyfont[40pt] \startoverlay {\strut\red m} {\strut\green a} {\strut\blue l} \stopoverlay
Without the \strut argument each argument is horizontally and vertically centered:
\setupbodyfont[40pt] \startoverlay {\red m} {\green a} {\blue l} \stopoverlay
Overlays are created with the \defineoverlay command, which takes an overlay name and content. The overlays are then applied to a \framed command or the background to the page the textblock, header, and so forth.
The next example shows two overlays (“behind” and “before”), which are activated in the frame with a background key.
\defineoverlay[behind][{\blackrule[height=2cm,width=8cm,color=yellow]}] \defineoverlay[before][{\blackrule[height=8cm,width=2cm,color=orange]}] \framed [width=10cm, height=10cm, background={color,behind,foreground,before}, backgroundcolor=red] {\blackrule[width=6cm,height=6cm,color=blue]}
The arguments of the background key used two names in addition to the “behind” and “before” overlays. The first name is “color”, which is needed when using the backgroundcolor key to put a color in the entire frame's area. The second is “foreground”, which can place overlays *before* the content of the frame because “foreground” is the frame text itself.
The overlay environment and overlay mechanism for \framed are similar.
The methods to set them because in the first case you set the content for each level which the argument in braces ({…}{…}) and in the second case you define them first with \defineoverlay and set the order in the background key.
Layers
With layers the content from each command can be positioned at a certain horizontal and vertical position. The following example creates a new layer and set three texts. The texts are placed in the page using the \flushlayer command.
\starttext \definelayer[test] \setlayer[test][x=3cm,y=6cm]{ONE} \setlayer[test][x=8cm,y=2cm]{TWO} \setlayer[test][x=2cm,y=9cm]{THREE} \flushlayer[test] \stoptext
When you put new some text after \flushlayer would be surprised because it would end put on the following page but you shouldn’t wonder about this because layers have nothing to do with the page background or foreground.
What makes the layer and overlay mechanism is interesting is when you combine them and put the \flushlayer command in a overlay which does now place the texts before or after the content of a frame. With my next example we’re finally in the process to combine them.
\setupbodyfont[30pt] \starttext \definelayer[behindtext] \definelayer[beforetext] \setupbackgrounds[text][background={behindtext,foreground,beforetext}] \setlayer[behindtext][x=-2cm,y=.75ex]{\red Behind} \setlayer[beforetext][x=+2cm,y=.75ex]{\yellow Before} The Text! \setlayer[behindtext][x=5cm,y=4cm]{\red More background text!} \setlayer[beforetext][x=3cm,y=8cm]{\yellow More foreground text!} \stoptext
When you look at the example you can see that haven't created a overlay for layer even though I said you need it to put the layer after the text. This isn’t necessary because when you create a new layer context does also create a overlay with the same name in which the layer is placed. The other question is how you can place overlays on the text/page when I told before overalls can only be placed on frame, this is possible because all page elements (textblock, header, footer and even the page and paper itself) are frames. Even though these elements are frames they don’t provide all the options from \framed because they are a special version with less functionality.
Example of circled numbers
% Solution A - a framed that uses an overlay as background ========== \startuseMPgraphic{MPtextcircle} path p; p := fullcircle xscaled OverlayWidth yscaled OverlayWidth; setbounds currentpicture to p ; % p:= p shifted (0,StrutDepth/2.7); draw p withpen pencircle scaled 0.25pt withcolor OverlayLineColor; \stopuseMPgraphic \defineoverlay[OLtextcircle][\useMPgraphic{MPtextcircle}] \defineframed [textcircledA] [ frame=off, strut=yes, rulethickness=0.0pt, background=OLtextcircle, framecolor=darkblue, location=top,] % Solution B - same with fixed dimensions =========================== \defineframed [textcircledB] [textcircledA] [width=1.75em, framecolor=darkred,] % Solution C - a framed with radius ================================= \defineframed [textcircledC] [width=1.5em, frame=on, strut=yes, rulethickness=0.25pt, framecolor=darkgreen, offset=1pt, location=top, corner=round, radius=.5em,] % =================================================================== \setupbodyfont[pagella, 14pt] \dostepwiserecurse{0}{150}{20}{% #1 = \textcircledA{#1} = \textcircledB{#1} = \textcircledC{#1}\\}