Debian Codegraph @

texmf-dist/tex/lualatex/spacekern/spacekern.lua

raw
1 -- Copyright (c) 2022-2023 Thomas Kelkel [email protected]
2
3 -- This file may be distributed and/or modified under the
4 -- conditions of the LaTeX Project Public License, either
5 -- version 1.3c of this license or (at your option) any later
6 -- version. The latest version of this license is in
7
8 -- http://www.latex-project.org/lppl.txt
9
10 -- and version 1.3c or later is part of all distributions of
11 -- LaTeX version 2009/09/24 or later.
12
13 -- Version: 0.3
14
15 local ID = node.id
16 local GLYPH = ID ( "glyph" )
17 local DISC = ID ( "disc" )
18 local GLUE = ID ( "glue" )
19 local PENALTY = ID ( "penalty" )
20 local HLIST = ID ( "hlist" )
21 local INS = ID ( "ins" )
22
23 local SWAPPED = table.swapped
24 local SUBTYPES = node.subtypes
25 local SPACESKIP = SWAPPED ( SUBTYPES ("glue") )["spaceskip"]
26 local LEADERS = SWAPPED ( SUBTYPES ("glue") )["leaders"]
27 local LBPENALTY = SWAPPED ( SUBTYPES ("penalty") )["linebreakpenalty"]
28
29 local NEW = node.new
30 local REM = node.remove
31 local PREV = node.prev
32 local NEXT = node.next
33 local TAIL = node.tail
34 local INS_B = node.insert_before
35 local HAS_GLYPH = node.has_glyph
36 local T = node.traverse
37 local T_GLYPH = node.traverse_glyph
38
39 local pairs = pairs
40 local type = type
41
42 local U = unicode.utf8
43 local SUB = U.sub
44 local GSUB = U.gsub
45 local FIND = U.find
46
47 local FLOOR = math.floor
48
49 local GET_FONT = font.getfont
50
51 local ATC = luatexbase.add_to_callback
52
53 -----
54
55 local no_iw_kern = false
56
57 function spacekern_no_iw_kern ()
58 no_iw_kern = true
59 end
60
61 local function round ( num, dec )
62 return FLOOR ( num * 10^dec + 0.5 ) / 10^dec
63 end
64
65 local function find_first_last ( n, node_type, last )
66 local d = NEXT
67 if last then
68 d = PREV
69 n = TAIL ( n )
70 end
71 while true do
72 if n and n.id == node_type then
73 return n
74 end
75 if d ( n ) then
76 n = d ( n )
77 else
78 return false
79 end
80 end
81 end
82
83 local function check_node ( n, space_check, disc_check, last )
84 if n.replace then
85 return find_first_last ( n.replace, GLYPH, last ), space_check, disc_check
86 end
87 return n, space_check, disc_check
88 end
89
90 local function find_glyph ( n, d, hlist_check, macro )
91 if n then
92 local space_check = false
93 local disc_check = false
94 if hlist_check and ( n.id == GLYPH or n.replace and HAS_GLYPH ( n.replace ) ) then
95 return check_node ( n, space_check, disc_check, d == PREV )
96 elseif not hlist_check then
97 if d ( n ) then
98 n = d ( n )
99 else
100 return false
101 end
102 end
103 while not ( n.id == GLYPH or n.replace and HAS_GLYPH ( n.replace ) ) do
104 if n.id == HLIST and n.head then
105 local point = n.head
106 if d == PREV then
107 point = TAIL ( point )
108 end
109 return find_glyph ( point, d, true, macro )
110 end
111 if not d ( n ) or n.id == INS or macro and n.id == GLUE and n.width == 0 then
112 return false
113 else
114 if n.id == GLUE and n.subtype == SPACESKIP then
115 space_check = n
116 elseif n.subtype == LEADERS or n.id == GLUE and n.stretch > 0 then
117 return false
118 end
119 end
120 if n.id == DISC then
121 disc_check = n
122 end
123 n = d ( n )
124 end
125 return check_node ( n, space_check, disc_check, d == PREV )
126 end
127 return false
128 end
129
130 local function make_kern ( head, font, first_glyph, second_glyph, insert_point )
131 local tfmdata = GET_FONT ( font )
132 if tfmdata and tfmdata.resources then
133 local resources = tfmdata.resources
134 if resources.sequences then
135 local seq = resources.sequences
136 for _, t in pairs ( seq ) do
137 if t.steps and ( table.swapped ( t.order )["kern"] or tfmdata.specification.features.raw[t.name] ) then
138 local steps = t.steps
139 for _, k in pairs ( steps ) do
140 if k.coverage and k.coverage[first_glyph] then
141 local glyph_table = k.coverage[first_glyph]
142 if type ( glyph_table ) == "table" then
143 for key, value in pairs ( glyph_table ) do
144 if key == second_glyph and type ( value ) == "number" then
145 if not ( ( first_glyph == 32 or second_glyph == 32 ) and table.swapped ( t.order )["kern"] ) then
146 insert_point.width = insert_point.width + value / tfmdata.units_per_em * tfmdata.size
147 end
148 if first_glyph == 32 or second_glyph == 32 then
149 insert_point.stretch = insert_point.stretch + value / tfmdata.units_per_em * tfmdata.size * 0.5
150 insert_point.shrink = insert_point.shrink + value / tfmdata.units_per_em * tfmdata.size * 0.33333
151 end
152 end
153 end
154 end
155 end
156 end
157 end
158 end
159 end
160 end
161 return head, disc_node
162 end
163
164 local function check_glyph ( n, d, has_prev_next_glyph, prev_next_glyph )
165 if find_glyph ( n, d ) then
166 prev_next_glyph = find_glyph ( n, d )
167 has_prev_next_glyph = true
168 end
169 return has_prev_next_glyph, prev_next_glyph
170 end
171
172 local function make_kerns ( head, n )
173 local has_prev_glyph = false
174 local has_next_glyph = false
175 local prev_glyph = n
176 local next_glyph = n
177 has_prev_glyph, prev_glyph = check_glyph ( n, PREV, has_prev_glyph, prev_glyph )
178 has_next_glyph, next_glyph = check_glyph ( n, NEXT, has_next_glyph, next_glyph )
179 if has_prev_glyph and prev_glyph.char and prev_glyph.font then
180 head = make_kern ( head, prev_glyph.font, prev_glyph.char, 32, n )
181 end
182 if has_next_glyph and next_glyph.char and next_glyph.font then
183 head = make_kern ( head, next_glyph.font, 32, next_glyph.char, n )
184 end
185 if not no_iw_kern and has_prev_glyph and has_next_glyph and prev_glyph.char and next_glyph.char and prev_glyph.font and next_glyph.font then
186 local tfmdata = GET_FONT ( prev_glyph.font )
187 if tfmdata and tfmdata.name then
188 local font_id = GSUB ( SUB ( tfmdata.name, 1, FIND ( tfmdata.name, ":" ) - 1 ), "\"", "" )
189 local second_tfmdata = GET_FONT ( next_glyph.font )
190 if second_tfmdata and second_tfmdata.name then
191 local second_font_id = GSUB ( SUB ( second_tfmdata.name, 1, FIND ( second_tfmdata.name, ":" ) - 1 ), "\"", "" )
192 if font_id == second_font_id then
193 head = make_kern ( head, prev_glyph.font, prev_glyph.char, next_glyph.char, n )
194 end
195 end
196 end
197 end
198 return head
199 end
200
201 local function make_short_spaces ( head )
202 for n in T_GLYPH ( head ) do
203 if n.char and n.char == 59 and ( not find_glyph ( n, PREV ) or find_glyph ( n, PREV ).char ~= 59 ) then
204 if find_glyph ( n, NEXT, false, true ) then
205 local next_glyph = n
206 next_glyph = find_glyph ( n, NEXT, false, true )
207 if next_glyph.char and next_glyph.char == 59 then
208 REM ( head, next_glyph )
209 local SIZE = 0
210 if n.font then
211 local tfmdata = GET_FONT ( n.font )
212 if tfmdata.size then
213 SIZE = tfmdata.size
214 end
215 end
216 INS_B ( head, n, NEW ( GLUE ) )
217 local glue_node = PREV ( n )
218 glue_node.subtype = SPACESKIP
219 glue_node.width = round ( SIZE * .16667, 0 )
220 glue_node.stretch = glue_node.width * 0.5
221 glue_node.shrink = glue_node.width * 0.33333
222 local has_next_glyph = false
223 if find_glyph ( n, NEXT, false, true ) then
224 next_glyph = find_glyph ( n, NEXT, false, true )
225 has_next_glyph = true
226 end
227 if has_next_glyph and next_glyph.char and next_glyph.char == 59 then
228 INS_B ( head, glue_node, NEW ( PENALTY ) )
229 PREV ( glue_node ).subtype = LBPENALTY
230 PREV ( glue_node ).penalty = 10000
231 REM ( head, next_glyph )
232 end
233 REM ( head, n )
234 end
235 end
236 end
237 end
238 end
239
240 local function make_glues_and_kerns ( head )
241 for n in T ( head ) do
242 if n.id == GLUE and n.subtype == SPACESKIP then
243 head = make_kerns ( head, n )
244 end
245 end
246 return head
247 end
248
249 ATC ( "ligaturing", make_short_spaces, "make short spaces", 1 )
250 ATC ( "pre_linebreak_filter", make_glues_and_kerns, "kern between words and against space preline", 1 )
251 ATC ( "hpack_filter", make_glues_and_kerns, "kern between words and against space hpack" )