s-fonts-kerns.mkiv /size: 5333 b    last modification: 2021-10-28 13:51
1%D \module
2%D   [      file=s-fonts-kerns,
3%D        version=2018.02.20, % was s-fnt-40 (given the kin dof code < 2010)
4%D          title=\CONTEXT\ Style File,
5%D       subtitle=Show Fonts Kerns,
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 real old code but we keep it around for educational purposes. it's
15%D not that efficient and we have better ways.
16%D
17%D \starttyping
18%D \showfontkerns
19%D \stoptyping
20%D
21%D This one shows all kerns so it's s bit different and as we use it in the
22%D manual \unknown
23
24\startluacode
25
26    local nodecodes  = nodes.nodecodes
27
28    local disc_code  = nodecodes.disc
29    local kern_code  = nodecodes.kern
30    local glyph_code = nodecodes.glyph
31    local hlist_code = nodecodes.hlist
32
33    local function mark(l,c)
34        while l do
35            local id = l.id
36            if id == kern_code then
37                context.showkern(l.kern,c)
38            elseif id == disc_code then
39                mark(l.pre,2)
40                mark(l.post,3)
41                mark(l.replace,4)
42            elseif id == hlist_code then
43                mark(l.list,c)
44            end
45            l = l.next
46        end
47    end
48
49    local function collect(l)
50        local m = { }
51        while l do
52            local id = l.id
53            if id == hlist_code then
54                m[#m+1] = l
55            end
56            l = l.next
57        end
58        return m
59    end
60
61    local function inject(l,m,i)
62        local x = l
63        i = i or 0
64        while l do
65            local id = l.id
66            if id == kern_code then
67                i = i + 1
68                nodes.insertbefore(x,l,node.copy(m[i]))
69            elseif id == disc_code then
70                i = inject(l.pre,m,i)
71                i = inject(l.post,m,i)
72                i = inject(l.replace,m,i)
73            elseif id == hlist_code then
74                i = inject(l.list,m,i)
75            end
76            l = l.next
77        end
78        return i
79    end
80
81    moduledata.fonts       = moduledata.fonts       or { }
82    moduledata.fonts.kerns = moduledata.fonts.kerns or { }
83
84    function moduledata.fonts.kerns._show_(n)
85        inject(tex.box[n].list,collect(tex.box[6].list))
86    end
87
88    function moduledata.fonts.kerns.show(n)
89        context("\\bgroup")
90        context("\\setbox6\\vbox\\bgroup\\forgetall")
91     -- flatten(tex.box[n].list)
92        mark(tex.box[n].list,1)
93        context("\\egroup")
94        context("\\ctxlua{moduledata.fonts.kerns._show_('%s')}",n)
95        context("\\egroup")
96    end
97
98\stopluacode
99
100\setupcolors[state=start]
101
102\definefont[VerySmall][Mono at 4pt]
103
104\definecolor[kern:0][s=.5]
105\definecolor[kern:1][b=.5]
106\definecolor[kern:2][r=.5]
107\definecolor[kern:3][g=.5]
108\definecolor[kern:4][r=.5,g=.5]
109
110\def\showkernwidthfactor{1}
111
112\newconditional\showkernstate
113\settrue       \showexplicitkerns
114
115\unexpanded\def\showkern#1#2%
116  {\begingroup
117   \dontcomplain
118   \setbox\scratchbox\hbox to \zeropoint \bgroup
119      \ifcase#2\or
120        \ifconditional\showkernstate
121          \color[kern:0]{\dovlwdhtdp\showkernwidthfactor{0.75}{0.5}}%
122          \global\setfalse\showkernstate
123          \raise.8\strutht
124        \else
125          \color[kern:0]{\dovlwdhtdp\showkernwidthfactor{1.05}{0.5}}%
126          \global\settrue\showkernstate
127          \raise1.1\strutht
128        \fi
129      \else\ifconditional\showexplicitkerns % if other kerns
130        \color[kern:0]{\dovlwdhtdp\showkernwidthfactor{0.75}{0.5}}%
131        \lower1.2\strutdp
132      \fi\fi
133      \hbox to 0pt{\hss\VerySmall\color[kern:#2]{\expandafter\withoutpt\the\dimexpr#1sp}\hss}%
134   \egroup
135   \smashbox\scratchbox
136   \box\scratchbox
137   \endgroup
138   \par}
139
140\newbox\KernBox
141
142\unexpanded\def\ShowKerns#1%
143  {\global\setfalse
144   \showkernstate\ctxlua{moduledata.fonts.kerns.show("#1")}}
145
146\unexpanded\def\ShowKernedVBox#1%
147  {\global\setfalse\showkernstate
148   \setbox\KernBox\vbox{#1}%
149   \ShowKerns{KernBox}%
150   \unvbox\KernBox}
151
152\unexpanded\def\ShowKernedHBox#1%
153  {\global\setfalse\showkernstate
154   \setbox\KernBox\hbox{#1}%
155   \ShowKerns{KernBox}%
156   \unhbox\KernBox}
157
158\continueifinputfile{s-fonts-kerns.mkiv}
159
160\setupinterlinespace[1.5] % \setfalse\showexplicitkerns
161
162\definefontfeature[default][default][mode=node] % ,script=latn]
163
164\starttext
165
166% \enabletrackers[otf.kerns]
167
168\setuplayout[width=middle,height=middle]
169
170\usetypescript[modern] \setupbodyfont[modern]
171\setbox\KernBox\vbox{\input knuth }  \ShowKerns{KernBox} \copy\KernBox \blank
172
173\ShowKernedHBox{Very often glyphs get very small spaces inserted horizontally.}
174
175\usetypescript[cambria] \setupbodyfont[cambria]
176\setbox\KernBox\vbox{\input knuth }  \ShowKerns{KernBox} \copy\KernBox \blank
177
178\ShowKernedHBox{Very often glyphs get very small spaces inserted horizontally.}
179
180\usetypescript[palatino] \setupbodyfont[palatino]
181\setbox\KernBox\vbox{\input knuth }  \ShowKerns{KernBox} \copy\KernBox \blank
182
183\ShowKernedHBox{Very often glyphs get very small spaces inserted horizontally.}
184
185\usetypescript[dejavu] \setupbodyfont[dejavu]
186\setbox\KernBox\vbox{\input knuth }  \ShowKerns{KernBox} \copy\KernBox \blank
187
188\ShowKernedHBox{Very often glyphs get very small spaces inserted horizontally.}
189
190\stoptext
191