luametafun-fonts.tex /size: 9122 b    last modification: 2021-10-28 13:50
1% language=us runpath=texruns:manuals/luametafun
2
3\environment luametafun-style
4
5\startcomponent luametafun-fonts
6
7\startchapter[title={Fonts}]
8
9Fonts are interesting phenomena but can also be quite hairy. Shapes can be
10missing or not to your liking. There can be bugs too. Control over fonts has
11always been on the agenda of \TEX\ macro packages, and \CONTEXT\ provides a lot
12of control, especially in \MKIV. In \LMTX\ we add some more to that: we bring
13back \METAFONT's but now in the \METAPOST\ way. A simple example shows how this
14is (maybe I should say: will be) done.
15
16We define three simple shapes and do that (for now) in the \type {simplefun}
17instance because that's what is used when generating the glyphs.
18
19\startbuffer
20\startMPcalculation{simplefun}
21    vardef TestGlyphLB =
22        image (
23            fill (unitsquare xscaled 10 yscaled 16 shifted (0,-3))
24                withcolor "darkred" withtransparency (1,.5)
25            ;
26        )
27    enddef ;
28
29    vardef TestGlyphRB =
30        image (
31            fill (unitcircle xscaled 15 yscaled 12 shifted (0,-2))
32                withcolor "darkblue" withtransparency (1,.5)
33            ;
34        )
35    enddef ;
36
37    vardef TestGlyphFS =
38        image (
39            fill (unittriangle xscaled 15 yscaled 12 shifted (0,-2))
40                withcolor "darkgreen" withtransparency (1,.5)
41            ;
42        )
43    enddef ;
44\stopMPcalculation
45\stopbuffer
46
47\typebuffer[option=TEX] \getbuffer
48
49This is not that spectacular, not is the following:
50
51\startbuffer
52\startMPcalculation{simplefun}
53    lmt_registerglyphs [
54        name  = "test",
55        units = 10, % 1000
56    ] ;
57
58    lmt_registerglyph [
59        category = "test",
60        unicode  = 123,
61        code     = "draw TestGlyphLB ;",
62        width    = 10, % 1000
63        height   = 13, % 1300
64        depth    = 3   %  300
65    ] ;
66
67    lmt_registerglyph [
68        category = "test",
69        unicode  = 125,
70        code     = "draw TestGlyphRB ;",
71        width    = 15,
72        height   = 10,
73        depth    = 2
74    ] ;
75
76    lmt_registerglyph [
77        category = "test",
78        unicode  = "/",
79        code     = "draw TestGlyphFS ;",
80        width    = 15,
81        height   = 10,
82        depth    = 2
83    ] ;
84
85\stopMPcalculation
86\stopbuffer
87
88\typebuffer[option=TEX] \getbuffer
89
90We now define a font. We always use a font as starting point which has the
91advantage that we always get something reasonable when we test. Of course you can
92use this \type {mps} font feature with other fonts too.
93
94\startbuffer
95\definefontfeature[metapost][metapost=test] % or: mps={category=test}
96
97\definefont[MyFontA][Serif*metapost @ 10bp]
98\definefont[MyFontB][Serif*metapost @ 12bp]
99\stopbuffer
100
101\typebuffer[option=TEX] \getbuffer
102
103These fonts can now be used:
104
105\startbuffer
106\MyFontA \dorecurse{20}{\{ /#1/ \} }\par
107\MyFontB \dorecurse{20}{\{ /#1/ \} }\par
108\stopbuffer
109
110\typebuffer[option=TEX]
111
112We get some useless text but it demonstrates the idea:
113
114{\getbuffer}
115
116If you know a bit more about \CONTEXT\ you could think: so what, wasn't this
117already possible? Sure, there are various ways to achieve similar effects, but
118the method described here has a few advantages: it's relatively easy and we're
119talking about real fonts here. This means that using the shapes for characters is
120pretty efficient.
121
122A more realistic example is given next. It is a subset of what is available in
123the \CONTEXT\ core.
124
125\startbuffer
126\startMPcalculation{simplefun}
127
128    pen SymbolPen ; SymbolPen := pencircle scaled 1/4 ;
129
130    vardef SymbolBullet =
131        fill unitcircle scaled  3 shifted (1.5,1.5) withpen SymbolPen
132    enddef ;
133    vardef SymbolSquare =
134        draw unitsquare scaled (3-1/16) shifted (1.5,1.5) withpen SymbolPen
135    enddef ;
136    vardef SymbolBlackDiamond =
137        fillup unitdiamond scaled (3-1/16) shifted (1.5,1.5) withpen SymbolPen
138    enddef ;
139    vardef SymbolNotDef =
140        draw center unitcircle
141            scaled 3
142            shifted (1.5,1.5)
143            withpen SymbolPen scaled 4
144    enddef ;
145
146    lmt_registerglyphs [
147        name     = "symbols",
148        units    = 10,
149        usecolor = true,
150        width    = 6,
151        height   = 6,
152        depth    = 0,
153        code     = "SymbolNotDef ;",
154    ] ;
155
156    lmt_registerglyph [ category = "symbols", unicode = "0x2022",
157        code  = "SymbolBullet ;"
158    ] ;
159    lmt_registerglyph [ category = "symbols", unicode = "0x25A1",
160        code  = "SymbolSquare ;"
161    ] ;
162    lmt_registerglyph [ category = "symbols", unicode = "0x25C6",
163        code  = "SymbolBlackDiamond ;"
164    ] ;
165\stopMPcalculation
166\stopbuffer
167
168\typebuffer[option=TEX] \getbuffer
169
170We could use these symbols in for instance itemize symbols. You might notice the
171potential difference in bullets:
172
173\startbuffer
174\definefontfeature[metapost][metapost=symbols]
175
176\definefont[MyFont] [Serif*metapost sa 1]
177
178\startitemize[packed]
179    \startitem {\MyFont                   }\quad Regular rendering. \stopitem
180    \startitem {\MyFont\red               }\quad Rendering with color. \stopitem
181    \startitem {\MyFont\blue\showglyphs   }\quad Idem but with boundingboxes shown. \stopitem
182\stopitemize
183\stopbuffer
184
185\typebuffer[option=TEX] \getbuffer
186
187When blown up, these symbols look as follows:
188
189\startlinecorrection
190\scale[width=\textwidth]{%
191    {\MyFont                 •□◆}%
192    {\MyFont\red             •□◆}%
193    {\MyFont\blue\showglyphs •□◆}%
194}
195\stoplinecorrection
196
197You can use these tricks with basically any font, so also with math fonts.
198However, at least for now, you need to define these before the font gets loaded.
199
200\startbuffer
201\startMPcalculation{simplefun}
202
203    pen KindergartenPen ; KindergartenPen := pencircle scaled 1 ;
204
205    % 10 x 10 grid
206
207    vardef KindergartenEqual =
208        draw image
209            (
210                draw (2,6) -- (9,5) ;
211                draw (2,4) -- (8,3) ;
212            )
213            shifted (0,-2)
214            withpen KindergartenPen
215            withcolor "KindergartenEqual"
216    enddef ;
217    vardef KindergartenPlus =
218        draw image
219            (
220                draw (1,4) -- (9,5) ;
221                draw (4,1) -- (5,8) ;
222            )
223            shifted (0,-2)
224            withpen KindergartenPen
225            withcolor "KindergartenPlus"
226    enddef ;
227    vardef KindergartenMinus =
228        draw image
229            (
230                draw (1,5) -- (9,4) ;
231            )
232            shifted (0,-2)
233            withpen KindergartenPen
234            withcolor "KindergartenMinus"
235    enddef ;
236    vardef KindergartenTimes =
237        draw image
238            (
239                draw (2,1) -- (9,8) ;
240                draw (8,1) -- (2,8) ;
241            )
242            shifted (0,-2)
243            withpen KindergartenPen
244            withcolor "KindergartenTimes"
245    enddef ;
246    vardef KindergartenDivided =
247        draw image
248            (
249                draw (2,1) -- (8,9) ;
250            )
251            shifted (0,-2)
252            withpen KindergartenPen
253            withcolor "KindergartenDivided"
254    enddef ;
255
256    lmt_registerglyphs [
257        name     = "kindergarten",
258        units    = 10,
259      % usecolor = true,
260        width    = 10,
261        height   = 8,
262        depth    = 2,
263    ] ;
264
265    lmt_registerglyph [ category = "kindergarten", unicode = "0x003D",
266        code = "KindergartenEqual"
267    ] ;
268    lmt_registerglyph [ category = "kindergarten", unicode = "0x002B",
269        code = "KindergartenPlus"
270    ] ;
271    lmt_registerglyph [ category = "kindergarten", unicode = "0x2212",
272        code = "KindergartenMinus"
273    ] ;
274    lmt_registerglyph [ category = "kindergarten", unicode = "0x00D7",
275        code = "KindergartenTimes"
276    ] ;
277    lmt_registerglyph [ category = "kindergarten", unicode = "0x002F",
278        code = "KindergartenDivided"
279    ] ;
280
281\stopMPcalculation
282\stopbuffer
283
284\typebuffer[option=TEX] \getbuffer
285
286We also define the colors. If we leave \type {usecolor} to true, the text colors
287will be taken.
288
289\startbuffer[definitions]
290\definecolor[KindergartenEqual]  [darkgreen]
291\definecolor[KindergartenPlus]   [darkred]
292\definecolor[KindergartenMinus]  [darkred]
293\definecolor[KindergartenTimes]  [darkblue]
294\definecolor[KindergartenDivided][darkblue]
295
296\definefontfeature[mathextra][metapost=kindergarten]
297\stopbuffer
298
299\typebuffer[definitions][option=TEX] \getbuffer[definitions]
300
301Here is an example:
302
303\starttyping
304\switchtobodyfont[cambria]
305
306$ y = 2 \times x + a - b / 3 $
307\stoptyping
308
309Scaled up:
310
311\startlinecorrection
312    \switchtobodyfont[cambria]
313    \scale[width=\textwidth]{$ y = 2 \times x + a - b / 3 $}
314\stoplinecorrection
315
316Of course this won't work out well (yet) with extensible yet, due to related
317definitions for which we don't have an interface yet. There is one thing that you
318need to keep in mind: the fonts are flushed when the document gets finalized so
319you have to make sure that colors are defined at the level that they are still
320valid at that time. So best put color definitions like the above in the document
321style.
322
323This is an experimental interface anyway so we don't explain the parameters yet
324as there might be more of them.
325
326\stopchapter
327
328\stopcomponent
329