summaryrefslogtreecommitdiff
path: root/doc/context/sources/general/manuals/luametafun/luametafun-fonts.tex
blob: 27ea2fd0014c263a3a95de0f49b998e5900107e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
% language=us

\environment luametafun-style

\startcomponent luametafun-fonts

\startchapter[title={Fonts}]

Fonts are interesting phenomena but can also be quite hairy. Shapes can be
missing or not to your liking. There can be bugs too. Control over fonts has
always been on the agenda of \TEX\ macro packages, and \CONTEXT\ provides a lot
of control, especially in \MKIV. In \LMTX\ we add some more to that: we bring
back \METAFONT's but now in the \METAPOST\ way. A simple example shows how this
is (maybe I should say: will be) done.

We define three simple shapes and do that (for now) in the \type {simplefun}
instance because that's what is used when generating the glyphs.

\startbuffer
\startMPcalculation{simplefun}
    vardef TestGlyphLB =
        image (
            fill (unitsquare xscaled 10 yscaled 16 shifted (0,-3))
                withcolor "darkred" withtransparency (1,.5)
            ;
        )
    enddef ;

    vardef TestGlyphRB =
        image (
            fill (unitcircle xscaled 15 yscaled 12 shifted (0,-2))
                withcolor "darkblue" withtransparency (1,.5)
            ;
        )
    enddef ;

    vardef TestGlyphFS =
        image (
            fill (unittriangle xscaled 15 yscaled 12 shifted (0,-2))
                withcolor "darkgreen" withtransparency (1,.5)
            ;
        )
    enddef ;
\stopMPcalculation
\stopbuffer

\typebuffer[option=TEX] \getbuffer

This is not that spectacular, not is the following:

\startbuffer
\startMPcalculation{simplefun}
    lmt_registerglyphs [
        name  = "test",
        units = 10, % 1000
    ] ;

    lmt_registerglyph [
        category = "test",
        unicode  = 123,
        code     = "draw TestGlyphLB ;",
        width    = 10, % 1000
        height   = 13, % 1300
        depth    = 3   %  300
    ] ;

    lmt_registerglyph [
        category = "test",
        unicode  = 125,
        code     = "draw TestGlyphRB ;",
        width    = 15,
        height   = 10,
        depth    = 2
    ] ;

    lmt_registerglyph [
        category = "test",
        unicode  = "/",
        code     = "draw TestGlyphFS ;",
        width    = 15,
        height   = 10,
        depth    = 2
    ] ;

\stopMPcalculation
\stopbuffer

\typebuffer[option=TEX] \getbuffer

We now define a font. We always use a font as starting point which has the
advantage that we always get something reasonable when we test. Of course you can
use this \type {mps} font feature with other fonts too.

\startbuffer
\definefontfeature[metapost][metapost=test] % or: mps={category=test}

\definefont[MyFontA][Serif*metapost @ 10bp]
\definefont[MyFontB][Serif*metapost @ 12bp]
\stopbuffer

\typebuffer[option=TEX] \getbuffer

These fonts can now be used:

\startbuffer
\MyFontA \dorecurse{20}{\{ /#1/ \} }\par
\MyFontB \dorecurse{20}{\{ /#1/ \} }\par
\stopbuffer

\typebuffer[option=TEX]

We get some useless text but it demonstrates the idea:

{\getbuffer}

If you know a bit more about \CONTEXT\ you could think: so what, wasn't this
already possible? Sure, there are various ways to achieve similar effects, but
the method described here has a few advantages: it's relatively easy and we're
talking about real fonts here. This means that using the shapes for characters is
pretty efficient.

A more realistic example is given next. It is a subset of what is available in
the \CONTEXT\ core.

\startbuffer
\startMPcalculation{simplefun}

    pen SymbolPen ; SymbolPen := pencircle scaled 1/4 ;

    vardef SymbolBullet =
        fill unitcircle scaled  3 shifted (1.5,1.5) withpen SymbolPen
    enddef ;
    vardef SymbolSquare =
        draw unitsquare scaled (3-1/16) shifted (1.5,1.5) withpen SymbolPen
    enddef ;
    vardef SymbolBlackDiamond =
        fillup unitdiamond scaled (3-1/16) shifted (1.5,1.5) withpen SymbolPen
    enddef ;
    vardef SymbolNotDef =
        draw center unitcircle
            scaled 3
            shifted (1.5,1.5)
            withpen SymbolPen scaled 4
    enddef ;

    lmt_registerglyphs [
        name     = "symbols",
        units    = 10,
        usecolor = true,
        width    = 6,
        height   = 6,
        depth    = 0,
        code     = "SymbolNotDef ;",
    ] ;

    lmt_registerglyph [ category = "symbols", unicode = "0x2022",
        code  = "SymbolBullet ;"
    ] ;
    lmt_registerglyph [ category = "symbols", unicode = "0x25A1",
        code  = "SymbolSquare ;"
    ] ;
    lmt_registerglyph [ category = "symbols", unicode = "0x25C6",
        code  = "SymbolBlackDiamond ;"
    ] ;
\stopMPcalculation
\stopbuffer

\typebuffer[option=TEX] \getbuffer

We could use these symbols in for instance itemize symbols. You might notice the
potential difference in bullets:

\startbuffer
\definefontfeature[metapost][metapost=symbols]

\definefont[MyFont] [Serif*metapost sa 1]

\startitemize[packed]
    \startitem {\MyFont                 • □ ◆}\quad Regular rendering. \stopitem
    \startitem {\MyFont\red             • □ ◆}\quad Rendering with color. \stopitem
    \startitem {\MyFont\blue\showglyphs • □ ◆}\quad Idem but with boundingboxes shown. \stopitem
\stopitemize
\stopbuffer

\typebuffer[option=TEX] \getbuffer

When blown up, these symbols look as follows:

\startlinecorrection
\scale[width=\textwidth]{%
    {\MyFont                 •□◆}%
    {\MyFont\red             •□◆}%
    {\MyFont\blue\showglyphs •□◆}%
}
\stoplinecorrection

You can use these tricks with basically any font, so also with math fonts.
However, at least for now, you need to define these before the font gets loaded.

\startbuffer
\startMPcalculation{simplefun}

    pen KindergartenPen ; KindergartenPen := pencircle scaled 1 ;

    % 10 x 10 grid

    vardef KindergartenEqual =
        draw image
            (
                draw (2,6) -- (9,5) ;
                draw (2,4) -- (8,3) ;
            )
            shifted (0,-2)
            withpen KindergartenPen
            withcolor "KindergartenEqual"
    enddef ;
    vardef KindergartenPlus =
        draw image
            (
                draw (1,4) -- (9,5) ;
                draw (4,1) -- (5,8) ;
            )
            shifted (0,-2)
            withpen KindergartenPen
            withcolor "KindergartenPlus"
    enddef ;
    vardef KindergartenMinus =
        draw image
            (
                draw (1,5) -- (9,4) ;
            )
            shifted (0,-2)
            withpen KindergartenPen
            withcolor "KindergartenMinus"
    enddef ;
    vardef KindergartenTimes =
        draw image
            (
                draw (2,1) -- (9,8) ;
                draw (8,1) -- (2,8) ;
            )
            shifted (0,-2)
            withpen KindergartenPen
            withcolor "KindergartenTimes"
    enddef ;
    vardef KindergartenDivided =
        draw image
            (
                draw (2,1) -- (8,9) ;
            )
            shifted (0,-2)
            withpen KindergartenPen
            withcolor "KindergartenDivided"
    enddef ;

    lmt_registerglyphs [
        name     = "kindergarten",
        units    = 10,
      % usecolor = true,
        width    = 10,
        height   = 8,
        depth    = 2,
    ] ;

    lmt_registerglyph [ category = "kindergarten", unicode = "0x003D",
        code = "KindergartenEqual"
    ] ;
    lmt_registerglyph [ category = "kindergarten", unicode = "0x002B",
        code = "KindergartenPlus"
    ] ;
    lmt_registerglyph [ category = "kindergarten", unicode = "0x2212",
        code = "KindergartenMinus"
    ] ;
    lmt_registerglyph [ category = "kindergarten", unicode = "0x00D7",
        code = "KindergartenTimes"
    ] ;
    lmt_registerglyph [ category = "kindergarten", unicode = "0x002F",
        code = "KindergartenDivided"
    ] ;

\stopMPcalculation
\stopbuffer

\typebuffer[option=TEX] \getbuffer

We also define the colors. If we leave \type {usecolor} to true, the text colors
will be taken.

\startbuffer[definitions]
\definecolor[KindergartenEqual]  [darkgreen]
\definecolor[KindergartenPlus]   [darkred]
\definecolor[KindergartenMinus]  [darkred]
\definecolor[KindergartenTimes]  [darkblue]
\definecolor[KindergartenDivided][darkblue]

\definefontfeature[mathextra][metapost=kindergarten]
\stopbuffer

\typebuffer[definitions][option=TEX] \getbuffer[definitions]

Here is an example:

\starttyping
\switchtobodyfont[cambria]

$ y = 2 \times x + a - b / 3 $
\stoptyping

Scaled up:

\startlinecorrection
    \switchtobodyfont[cambria]
    \scale[width=\textwidth]{$ y = 2 \times x + a - b / 3 $}
\stoplinecorrection

Of course this won't work out well (yet) with extensible yet, due to related
definitions for which we don't have an interface yet. There is one thing that you
need to keep in mind: the fonts are flushed when the document gets finalized so
you have to make sure that colors are defined at the level that they are still
valid at that time. So best put color definitions like the above in the document
style.

This is an experimental interface anyway so we don't explain the parameters yet
as there might be more of them.

\stopchapter

\stopcomponent