summaryrefslogtreecommitdiff
path: root/tex/context/base/s-fnt-23.tex
blob: dedcf06e42a7de721c6921e49a82a0588f267f13 (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
%D \module
%D   [      file=s-fnt-23,
%D        version=2009.03.04,
%D          title=\CONTEXT\ Style File,
%D       subtitle=Tracing Feature Application (3),
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

% last_data was written wrong so it needs checking

\startluacode
    local last_data = nil
    local format = string.format
    local function tpf(...)
--     print("!!!!",...)
        tex.print(tex.ctxcatcodes,format(...))
    end
    function fonts.otf.show_shape(n)
        local tfmdata = fonts.ids[font.current()]
        last_data = tfmdata
        local charnum = tonumber(n)
        if not charnum then
            charnum = tfmdata.unicodes[n]
        end
        local c = tfmdata.characters[charnum]
        local d = tfmdata.descriptions[charnum]
        if d then
            local factor = (tfmdata.size/tfmdata.units)*((7200/7227)/65536)
            local llx, lly, urx, ury = unpack(d.boundingbox)
            llx, lly, urx, ury = llx*factor, lly*factor, urx*factor, ury*factor
            local width, italic = (d.width or 0)*factor, (d.italic or 0)*factor
            local top_accent, bot_accent = (d.top_accent or 0)*factor, (d.bot_accent or 0)*factor
            local anchors, math = d.anchors, d.math
            tpf("\\startMPcode")
            tpf("pickup pencircle scaled .25bp ; ")
            tpf('picture p ; p := image(draw textext.drt("\\gray\\char%s");); draw p ;',charnum)
            tpf('draw (%s,%s)--(%s,%s)--(%s,%s)--(%s,%s)--cycle withcolor green ;',llx,lly,urx,lly,urx,ury,llx,ury)
            tpf('draw (%s,%s)--(%s,%s) withcolor green ;',llx,0,urx,0)
            tpf('draw boundingbox p withcolor .2white withpen pencircle scaled .065bp ;')
            tpf("defaultscale := 0.05 ; ")
            -- inefficient but non critical
            local function slant_1(v,dx,dy,txt,xsign,ysign,loc,labloc)
                if #v > 0 then
                    local l = { }
                    for kk, vv in ipairs(v) do
                        local h, k = vv.height, vv.kern
                        if h and k then
                            l[#l+1] = format("((%s,%s) shifted (%s,%s))",xsign*k*factor,ysign*h*factor,dx,dy)
                        end
                    end
                    tpf("draw ((%s,%s) shifted (%s,%s))--%s dashed (evenly scaled .25) withcolor .5white;", xsign*v[1].kern*factor,lly,dx,dy,l[1])
                    tpf("draw laddered (%s) withcolor .5white ;",table.concat(l,".."))
                    tpf("draw ((%s,%s) shifted (%s,%s))--%s dashed (evenly scaled .25) withcolor .5white;", xsign*v[#v].kern*factor,ury,dx,dy,l[#l])
                    for k, v in ipairs(l) do
                        tpf("draw %s withcolor blue  withpen pencircle scaled 1bp;",v)
                    end
                end
            end
            local function slant_2(v,dx,dy,txt,xsign,ysign,loc,labloc)
                if #v > 0 then
                    local l = { }
                    for kk, vv in ipairs(v) do
                        local h, k = vv.height, vv.kern
                        if h and k then
                            l[#l+1] = format("((%s,%s) shifted (%s,%s))",xsign*k*factor,ysign*h*factor,dx,dy)
                        end
                    end
                    if loc == "top" then
                        tpf('label.%s("\\type{%s}",%s shifted (0,-1bp)) ;',loc,txt,l[#l])
                    else
                        tpf('label.%s("\\type{%s}",%s shifted (0,2bp)) ;',loc,txt,l[1])
                    end
                    for kk, vv in ipairs(v) do
                        local h, k = vv.height, vv.kern
                        if h and k then
                            tpf('label.top("(%s,%s)",%s shifted (0,-2bp));',k,h,l[kk])
                        end
                    end
                end
            end
            if math then
                local kerns = math.kerns
                if kerns then
                    for _, slant in ipairs { slant_1, slant_2 } do
                        for k,v in pairs(kerns) do
                            if k == "top_right" then
                                slant(v,width+italic,0,k,1,1,"top","ulft")
                            elseif k == "bottom_right" then
                                slant(v,width,0,k,1,1,"bot","lrt")
                            elseif k == "top_left" then
                                slant(v,0,0,k,-1,1,"top","ulft")
                            elseif k == "bottom_left" then
                                slant(v,0,0,k,-1,1,"bot","lrt")
                            end
                        end
                    end
                end
            end
            local function show(x,y,txt)
                local xx, yy = x*factor, y*factor
                tpf("draw (%s,%s) withcolor blue withpen pencircle scaled 1bp;",xx,yy)
                tpf('label.top("\\type{%s}",(%s,%s-2bp)) ;',txt,xx,yy)
                tpf('label.bot("(%s,%s)",(%s,%s+2bp)) ;',x,y,xx,yy)
            end
            if anchors then
                local a = anchors.baselig
                if a then
                    for k, v in pairs(a) do
                        for kk, vv in ipairs(v) do
                            show(vv[1],vv[2],k .. ":" .. kk)
                        end
                    end
                end
                local a = anchors.mark
                if a then
                    for k, v in pairs(a) do
                        show(v[1],v[2],k)
                    end
                end
                local a = anchors.basechar
                if a then
                    for k, v in pairs(a) do
                        show(v[1],v[2],k)
                    end
                end
                local ba = anchors.centry
                if a then
                    for k, v in pairs(a) do
                        show(v[1],v[2],k)
                    end
                end
                local a = anchors.cexit
                if a then
                    for k, v in pairs(a) do
                        show(v[1],v[2],k)
                    end
                end
            end
            if italic ~= 0 then
                tpf('draw (%s,%s-1bp)--(%s,%s-0.5bp) withcolor blue;',width,ury,width,ury)
                tpf('draw (%s,%s-1bp)--(%s,%s-0.5bp) withcolor blue;',width+italic,ury,width+italic,ury)
                tpf('draw (%s,%s-1bp)--(%s,%s-1bp) withcolor blue;',width,ury,width+italic,ury)
                tpf('label.lft("\\type{%s}",(%s+2bp,%s-1bp));',"italic",width,ury)
                tpf('label.rt("%s",(%s-2bp,%s-1bp));',d.italic,width+italic,ury)
            end
            if top_accent ~= 0 then
                tpf('draw (%s,%s+1bp)--(%s,%s-1bp) withcolor blue;',top_accent,ury,top_accent,ury)
                tpf('label.bot("\\type{%s}",(%s,%s+1bp));',"top_accent",top_accent,ury)
                tpf('label.top("%s",(%s,%s-1bp));',d.top_accent,top_accent,ury)
            end
            if bot_accent ~= 0 then
                tpf('draw (%s,%s+1bp)--(%s,%s-1bp) withcolor blue;',bot_accent,lly,bot_accent,lly)
                tpf('label.top("\\type{%s}",(%s,%s-1bp));',"bot_accent",top_accent,ury)
                tpf('label.bot("%s",(%s,%s+1bp));',d.bot_accent,bot_accent,lly)
            end
            tpf('draw origin withcolor red withpen pencircle scaled 1bp;')
            tpf("setbounds currentpicture to boundingbox currentpicture enlarged 1bp ;")
            tpf("currentpicture := currentpicture scaled 8 ;")
            tpf("\\stopMPcode")
        elseif c then
            local factor = (7200/7227)/65536
            tpf("\\startMPcode")
            tpf("pickup pencircle scaled .25bp ; ")
            tpf('picture p ; p := image(draw textext.drt("\\gray\\char%s");); draw p ;',charnum)
            tpf('draw boundingbox p withcolor .2white withpen pencircle scaled .065bp ;')
            tpf("defaultscale := 0.05 ; ")
            local italic, top_accent, bot_accent = (c.italic or 0)*factor, (c.top_accent or 0)*factor, (c.bot_accent or 0)*factor
            local width, height, depth = (c.width or 0)*factor, (c.height or 0)*factor, (c.depth or 0)*factor
            local ury = height
            if italic ~= 0 then
                tpf('draw (%s,%s-1bp)--(%s,%s-0.5bp) withcolor blue;',width,ury,width,ury)
                tpf('draw (%s,%s-1bp)--(%s,%s-0.5bp) withcolor blue;',width+italic,ury,width+italic,ury)
                tpf('draw (%s,%s-1bp)--(%s,%s-1bp) withcolor blue;',width,ury,width+italic,height)
                tpf('label.lft("\\type{%s}",(%s+2bp,%s-1bp));',"italic",width,height)
                tpf('label.rt("%6.3f bp",(%s-2bp,%s-1bp));',italic,width+italic,height)
            end
            if top_accent ~= 0 then
                tpf('draw (%s,%s+1bp)--(%s,%s-1bp) withcolor blue;',top_accent,ury,top_accent,height)
                tpf('label.bot("\\type{%s}",(%s,%s+1bp));',"top_accent",top_accent,height)
                tpf('label.top("%6.3f bp",(%s,%s-1bp));',top_accent,top_accent,height)
            end
            if bot_accent ~= 0 then
                tpf('draw (%s,%s+1bp)--(%s,%s-1bp) withcolor blue;',bot_accent,lly,bot_accent,height)
                tpf('label.top("\\type{%s}",(%s,%s-1bp));',"bot_accent",top_accent,height)
                tpf('label.bot("%6.3f bp",(%s,%s+1bp));',bot_accent,bot_accent,height)
            end
            tpf('draw origin withcolor red withpen pencircle scaled 1bp;')
            tpf("setbounds currentpicture to boundingbox currentpicture enlarged 1bp ;")
            tpf("currentpicture := currentpicture scaled 8 ;")
            tpf("\\stopMPcode")
        else
            tpf("no such shape: %s",n)
        end
    end
    function fonts.otf.show_all_shapes(start,stop)
        local tfmdata = fonts.ids[font.current()]
        last_data = tfmdata
        start, stop = start or "\\startTEXpage\\gobbleoneargument", stop or "\\stopTEXpage"
        local unicodes, indices, descriptions = tfmdata.unicodes, tfmdata.indices, tfmdata.descriptions
        for _, unicode in next, table.sortedkeys(descriptions) do
            local d = descriptions[unicode]
            local name = d.name
            tpf("%s{%s}%%",start,unicode)
            tpf("\\writestatus{glyph}{U+%04X -> %s}%%",unicode,name)
            fonts.otf.show_shape(unicode)
            tpf(stop)
        end
    end
    function fonts.otf.show_shape_field(unicode,name)
        local tfmdata = last_data or fonts.ids[font.current()]
        local d = tfmdata.descriptions[unicode]
        if d then
            if name == "unicode" then
                tpf("U+%04X",unicode)
            else
                d = d[name]
                if d then
                    tpf(d)
                end
            end
        end
    end
\stopluacode

\setupcolors
  [state=start]

\def\GetGlyphField#1#2%
  {\ctxlua{fonts.otf.show_shape_field(#1,"#2")}}

\def\StartShowGlyphShape#1%
  {\startTEXpage
   \nonknuthmode
   \def\GlyphUnicode{#1}}

\def\StopShowGlyphShape
  {\par
   \midaligned{\tttf\setstrut\strut\GetGlyphField\GlyphUnicode{unicode}: \GetGlyphField\GlyphUnicode{name}}%
   \stopTEXpage}

\def\ShowGlyphShape#1#2#3% name size glyph
  {\begingroup
   \definedfont[#1 at #2]%
   \obeyMPboxdepth
   \ctxlua{fonts.otf.show_shape("#3")}%
   \endgroup}

\def\ShowAllGlyphShapes#1#2% name size
  {\begingroup
   \nonknuthmode
   \definedfont[#1 at #2]%
   \ctxlua{fonts.otf.show_all_shapes("\\StartShowGlyphShape","\\StopShowGlyphShape")}%
   \endgroup}

\setupcolors
  [state=start]

\doifnotmode{demo}{\endinput}

\starttext

\startTEXpage \ShowGlyphShape{simplenaskhi}{100bp}{0x62A}       \stopTEXpage
\startTEXpage \ShowGlyphShape{simplenaskhi}{100bp}{0x2004}      \stopTEXpage
\startTEXpage \ShowGlyphShape{simplenaskhi}{100bp}{0xF0299}     \stopTEXpage
\startTEXpage \ShowGlyphShape{simplenaskhi}{100bp}{NameMe.1190} \stopTEXpage

\ShowAllGlyphShapes{simplenaskhi}{100bp}
% \ShowAllGlyphShapes{xits}{100bp}

\stoptext