meta-imp-demo.mkxl /size: 7898 b    last modification: 2023-12-21 09:44
1%D \module
2%D   [       file=meta-imp-demo,
3%D        version=2021.01.21,
4%D          title=\METAPOST\ Graphics,
5%D       subtitle=Demo Font,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14%D This is just a demo of defining a font in \METAPOST. The rendering as well as definitions
15%D are delayed till we actually need the glyph.
16
17\startMPcalculation{simplefun}
18
19    picture DemoSnippets[];
20
21    vardef DemoCircle = image(fill fullcircle scaled 10 ;) enddef ;
22    vardef DemoCenter = image(fill fullcircle scaled  3 ;) enddef ;
23
24    vardef DemoLeft       = image(fill fullcircle scaled 3 shifted (-5, 0) ;) enddef ;
25    vardef DemoRight      = image(fill fullcircle scaled 3 shifted ( 5, 0) ;) enddef ;
26    vardef DemoUp         = image(fill fullcircle scaled 3 shifted ( 0, 5) ;) enddef ;
27    vardef DemoDown       = image(fill fullcircle scaled 3 shifted ( 0,-5) ;) enddef ;
28    vardef DemoLowerLeft  = image(fill fullcircle scaled 3 shifted (-5,-5) ;) enddef ;
29    vardef DemoLowerRight = image(fill fullcircle scaled 3 shifted ( 5,-5) ;) enddef ;
30    vardef DemoUpperRight = image(fill fullcircle scaled 3 shifted ( 5, 5) ;) enddef ;
31    vardef DemoUpperLeft  = image(fill fullcircle scaled 3 shifted (-5, 5) ;) enddef ;
32
33    vardef DemoGlyph =
34        composeglyph(DemoSnippets) ;
35    enddef ;
36
37    lmt_registerglyphs [
38        name     = "demo",
39        units    = 10,
40        width    = 10,
41        height   = 10,
42        depth    = 0,
43    ] ;
44
45    % we need to set up some basics (signals to the backend and such) at definition
46    % time so we just force a bogus shape (zero)
47
48    lmt_registerglyph [
49        category = "demo",
50    ] ;
51
52\stopMPcalculation
53
54\startluacode
55
56metapost.metafonts.demo = {
57    [utf.byte("g")] = {
58        code   = "DemoGlyph",
59        shapes = {
60            { shape = "DemoCircle", color = "gray"    },
61            { shape = "DemoLeft",   color = "cyan"    },
62            { shape = "DemoUp",     color = "magenta" },
63            { shape = "DemoCenter", color = "yellow"  },
64        }
65    },
66    [utf.byte("h")] = {
67        code   = "DemoGlyph",
68        shapes = {
69            { shape = "DemoCircle", color = "gray"  },
70            { shape = "DemoRight",  color = "red"   },
71            { shape = "DemoDown",   color = "green" },
72            { shape = "DemoCenter", color = "blue"  },
73        }
74    },
75    [19] = {
76        code   = "DemoGlyph",
77        shapes = {
78            { shape = "DemoCircle", color = "gray"     },
79            { shape = "DemoUp",     color = "darkgray" },
80            { shape = "DemoDown",   color = "darkgray" },
81            { shape = "DemoLeft",   color = "darkgray" },
82            { shape = "DemoRight",  color = "darkgray" },
83            { shape = "DemoCenter", color = "darkgray" },
84        }
85    },
86    ["weirdsymbol"] = {
87        code   = "DemoGlyph",
88        shapes = {
89            { shape = "DemoCircle", color = "middlegray" },
90            { shape = "DemoUp",     color = "red"        },
91            { shape = "DemoDown",   color = "green"      },
92            { shape = "DemoLeft",   color = "blue"       },
93            { shape = "DemoRight",  color = "magenta"    },
94            { shape = "DemoCenter", color = "yellow"     },
95        }
96    },
97    ["weirdersymbol"] = {
98        code   = "DemoGlyph",
99        shapes = {
100         -- { shape = "DemoCircle",     color = "gray"        },
101            { shape = "DemoCenter",     color = "darkgray"    },
102            { shape = "DemoLowerLeft",  color = "red"         },
103            { shape = "DemoLowerRight", color = "green"       },
104            { shape = "DemoUpperRight", color = "blue"        },
105            { shape = "DemoUpperLeft",  color = "yellow"      },
106            { shape = "DemoUp",         color = "darkred"     },
107            { shape = "DemoDown",       color = "darkgreen"   },
108            { shape = "DemoLeft",       color = "darkblue"    },
109            { shape = "DemoRight",      color = "darkmagenta" },
110        }
111    }
112}
113
114\stopluacode
115
116\continueifinputfile{meta-imp-demo.mkxl}
117
118\setuplayout[tight]
119
120\setupbodyfont[dejavu]
121
122\definefontfeature[demo][metapost=demo,metafont=demo]
123
124\definefont[DemoA][Serif*demo]
125\definefont[DemoB][Serif*demo sa .5]
126\definefont[DemoC][Serif*demo sa .25]
127
128\starttext
129
130    \startbuffer
131        x\ruledhbox{gh}%
132        x\ruledhbox{\char19}%
133        x\privatecharacter{weirdsymbol}%
134        x\privatecharacter{weirdersymbol}%
135        x%
136    \stopbuffer
137
138    \startTEXpage[offset=2pt]
139        \DemoA\getbuffer\par
140        \DemoB\getbuffer\quad\DemoC\getbuffer\par
141    \stopTEXpage
142
143    {\DemoA\ctxlua{fonts.helpers.setmetaglyph {
144        category = "demo",
145        name     = "whatever",
146        code     = "DemoGlyph",
147        shapes   = {
148            { shape = "DemoCenter",     color = "darkgray"   },
149            { shape = "DemoLowerLeft",  color = "darkred"    },
150            { shape = "DemoLowerRight", color = "darkgreen"  },
151            { shape = "DemoUpperRight", color = "darkblue"   },
152            { shape = "DemoUpperLeft",  color = "darkyellow" },
153            { shape = "DemoUp",         color = "red"        },
154            { shape = "DemoDown",       color = "green"      },
155            { shape = "DemoLeft",       color = "blue"       },
156            { shape = "DemoRight",      color = "magenta"    },
157        }
158    }}}
159
160    {\DemoA\setmetaglyph {
161        category {demo}
162        name     {forever}
163        code     {DemoGlyph}
164        shapes   {
165            { shape {DemoCenter}     color {middlegray} }
166            { shape {DemoLowerLeft}  color {darkgray}   }
167            { shape {DemoLowerRight} color {darkgray}   }
168            { shape {DemoUpperRight} color {darkgray}   }
169            { shape {DemoUpperLeft}  color {darkgray}   }
170            { shape {DemoUp}         color {lightgray}  }
171            { shape {DemoDown}       color {lightgray}  }
172            { shape {DemoLeft}       color {lightgray}  }
173            { shape {DemoRight}      color {lightgray}  }
174        }
175    }}
176
177    \startTEXpage[offset=2pt]
178        \DemoA
179        \privatecharacter{whatever}
180        \privatecharacter{forever}
181    \stopTEXpage
182
183    \DemoA\setupinterlinespace \dorecurse{10}{\dorecurse{1000}{g h }\par}\page
184    \DemoB\setupinterlinespace \dorecurse{20}{\dorecurse{1000}{g h }\par}\page
185    \DemoC\setupinterlinespace \dorecurse{30}{\dorecurse{1000}{g h }\par}\page
186
187    \protected\def\MyChar#1%
188      {\begingroup
189       \DemoA
190       \iffontchar\font\privatecharactercode{forever-#1}\else
191         \setmetaglyph {
192           category {demo}
193           name     {forever-#1}
194           code     {DemoGlyph}
195           shapes   {
196               { shape {DemoCenter}     color {middlegray} }
197               { shape {DemoLowerLeft}  color {dark#1}     }
198               { shape {DemoLowerRight} color {dark#1}     }
199               { shape {DemoUpperRight} color {dark#1}     }
200               { shape {DemoUpperLeft}  color {dark#1}     }
201               { shape {DemoUp}         color {light#1}    }
202               { shape {DemoDown}       color {light#1}    }
203               { shape {DemoLeft}       color {light#1}    }
204               { shape {DemoRight}      color {light#1}    }
205           }
206         }%
207       \fi
208       \privatecharacter{forever-#1}%
209       \endgroup}
210
211    \dontcomplain
212
213    \dorecurse{1000}{%
214        \MyChar{red}\space
215        \MyChar{green}\space
216        \MyChar{blue}\space
217        \MyChar{yellow}\space
218    }
219
220    \page
221
222    \start
223        \glyphxscale 1200
224        \glyphyscale  800
225        \dorecurse{1000}{%
226            \MyChar{red}\allowbreak
227            \MyChar{green}\allowbreak
228            \MyChar{blue}\allowbreak
229            \MyChar{yellow}\allowbreak
230        }
231    \stop
232
233\stoptext
234