meta-imp-outlines.mkiv /size: 7123 b    last modification: 2020-07-01 14:35
1%D \module
2%D   [       file=meta-imp-outlines,
3%D        version=2015.06.02,
4%D          title=\METAPOST\ Graphics,
5%D       subtitle=Outlines,
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\startluacode
15
16local concat      = table.concat
17local formatters  = string.formatters
18local validstring = string.valid
19
20local f_setbounds = formatters["setbounds currentpicture to (%s) enlarged %.4G;"]
21local f_index     = formatters['draw anchored.bot(textext("\\tttf\\setstrut\\strut index %i") ysized 10bp ,.5[llcorner currentpicture,lrcorner currentpicture] shifted (0,%.4G));']
22local f_unicode   = formatters['draw anchored.bot(textext("\\tttf\\setstrut\\strut unicode %05X") ysized 10bp ,.5[llcorner currentpicture,lrcorner currentpicture] shifted (0,%.4G));']
23
24local f_in_red    = formatters["draw %s withpen pencircle scaled .15 withcolor .5red;"]
25local f_in_green  = formatters["draw %s withpen pencircle scaled .15 withcolor .5green;"]
26local f_in_blue   = formatters["draw %s withpen pencircle scaled .15 withcolor .5blue;"]
27local f_in_gray   = formatters["draw image(%s) withcolor .75yellow;"]
28
29local f_glyph = formatters [ [[
30pickup pencircle scaled .15;
31pointlabelfont := "Mono sa .125";
32pointlabelscale := 1bp ;
33drawoptionsfactor := .2bp ;
34originlength := 2bp ;
35%s;
36]] ]
37
38local metapost  = fonts.metapost
39
40local variables = interfaces.variables
41
42local v_all     = variables.all
43local v_page    = variables.page
44local v_text    = variables.text
45local v_command = variables.command
46local v_box     = variables.box
47local v_width   = variables.width
48local v_min     = variables.min
49local v_max     = variables.max
50local v_comment = variables.comment
51local v_simple  = variables.simple
52
53function metapost.showglyph(specification)
54    local fontid      = font.current()
55    local shapedata   = fonts.hashes.shapes[fontid] -- by index
56    local chardata    = fonts.hashes.characters[fontid] -- by unicode
57    local shapeglyphs = shapedata.glyphs or { }
58    local character   = validstring(specification.character)
59    local index       = validstring(specification.index)
60    local alternative = validstring(specification.alternative)
61    local command     = validstring(specification.command)
62    local options     = utilities.parsers.settings_to_set(specification.option)
63    local all         = not next(options) and not options[v_simple] or options[v_all]
64    local function shape(index,what,f_comment)
65        if not index then
66            return
67        end
68        local glyph = shapeglyphs[index]
69        if glyph and (glyph.segments or glyph.sequence) then
70            local units  = shapedata.units or 1000
71            local factor = 100/units
72            local paths  = metapost.paths(glyph,factor)
73            if #paths > 0 and glyph.boundingbox and glyph.width then
74                local graphic = f_glyph(concat{
75                                                    f_in_gray  (metapost.fill(paths)),
76                                                                metapost.draw(paths,true), -- true triggers trace
77                    (all or options[v_box])     and f_in_red   (metapost.boundingbox(glyph,factor)) or "",
78                    (all or options[v_width])   and f_in_green (metapost.widthline(glyph,factor)) or "",
79                    (all or options[v_min])     and f_in_blue  (metapost.zeroline(glyph,factor)) or "",
80                    (all or options[v_max])     and f_setbounds(metapost.maxbounds(data,index,factor),offset or 1) or "",
81                    (all or options[v_comment]) and f_comment  (what,1) or "",
82                })
83                if alternative == v_page then
84                    context.startMPpage()
85                        context(graphic)
86                    context.stopMPpage()
87                elseif alternative == v_command then
88                    context[command](graphic)
89                else -- v_text
90                    context.startMPcode()
91                        context(graphic)
92                    context.stopMPcode()
93                end
94            end
95        end
96    end
97
98    if character == v_all then
99        for u, c in table.sortedhash(chardata) do
100            shape(c.index,u,f_unicode)
101        end
102        return
103    end
104    if type(character) == "string" then
105        character = utf.byte(character)
106    end
107    if type(character) == "number" then
108        local c = chardata[character]
109        if c then
110            shape(c.index,c.index,f_index)
111        end
112        return
113    end
114    if type(index) == "number" then
115        shape(index,index,f_index)
116        return
117    end
118    local first, last
119    if type(index) == "string" then
120        first, last = string.split(index,":")
121        if first and last then
122            first = tonumber(first)
123            last  = tonumber(last)
124        else
125            first = tonumber(index)
126            last  = first
127        end
128        if not first then
129            first = 1
130            last  = #shapeglyphs
131        elseif not last then
132            last = first
133        end
134    else
135        first = 1
136        last  = #shapeglyphs
137    end
138    for index=first,last do
139        shape(index,index,f_index)
140    end
141end
142
143\stopluacode
144
145\unprotect
146
147% option: box|width|min|max|comment
148
149\unexpanded\def\showshape
150  {\dosingleargument\meta_shapes_show}
151
152\def\meta_shapes_show[#1]%
153  {\begingroup
154   \letdummyparameter\c!index\empty
155   \letdummyparameter\c!character\empty
156   \letdummyparameter\c!alternative\v!text
157   \letdummyparameter\c!command\empty
158   \letdummyparameter\c!option\v!all
159   \getdummyparameters[#1]%
160   \ctxlua{fonts.metapost.showglyph{
161        character   = "\dummyparameter\c!character",
162        index       = "\dummyparameter\c!index",
163        alternative = "\dummyparameter\c!alternative",
164        command     = "\dummyparameter\c!command",
165        option      = "\dummyparameter\c!option",
166   }}%
167   \endgroup}
168
169\protect
170
171\continueifinputfile{meta-imp-outlines.mkiv}
172
173\starttext
174
175% \setupbodyfont[pagella]
176
177% \definedfont[latinmodern-math]\showshape[index=3078,alternative=page]
178
179% \setupbodyfont[pagella]
180% \showshape[character=all,alternative=page]
181
182\usemodule[art-01]
183
184% \definedfont[lt55476.afm]
185
186\startcombination[3*1]
187    {\ruledhbox{\startMPcode draw textext("\showshape[character=a]") ; \stopMPcode}} {}
188    {\ruledhbox{\startMPcode draw textext("\showshape[character=x]") ; \stopMPcode}} {}
189    {\ruledhbox{\showshape[character=P,alternative=text]}} {}
190\stopcombination
191
192\startcombination[3*1]
193    {\ruledhbox{\startMPcode draw textext("\showshape[character=a,option={simple}]") ; \stopMPcode}} {}
194    {\ruledhbox{\startMPcode draw textext("\showshape[character=x,option={simple}]") ; \stopMPcode}} {}
195    {\ruledhbox{\showshape[character=P,alternative=text,option=simple]}} {}
196\stopcombination
197
198% \definedfont[almfixed]
199% \showshape[character=all,alternative=page]
200
201% \definedfont[file:sourcehansans-bold.otf]
202% \showshape[index=40000:41000,alternative=page]
203
204\stoptext
205