Debian Codegraph @

texmf-dist/tex/lualatex/showhyphenation/showhyphenation.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 KERN = ID ( "kern" )
20 local WI = ID ( "whatsit" )
21 local HLIST = ID ( "hlist" )
22 local VLIST = ID ( "vlist" )
23 local INS = ID ( "ins" )
24
25 local SWAPPED = table.swapped
26 local SUBTYPES = node.subtypes
27 local WIS = node.whatsits
28 local PDF_LITERAL = SWAPPED ( WIS () )["pdf_literal"]
29 local LEADERS = SWAPPED ( SUBTYPES ("glue") )["leaders"]
30 local RIGHTSKIP = SWAPPED ( SUBTYPES ("glue") )["rightskip"]
31
32 local NEW = node.new
33 local COPY = node.copy
34 local REM = node.remove
35 local PREV = node.prev
36 local NEXT = node.next
37 local INS_B = node.insert_before
38 local INS_A = node.insert_after
39 local HAS_GLYPH = node.has_glyph
40 local T = node.traverse
41 local T_ID = node.traverse_id
42 local T_GLYPH = node.traverse_glyph
43
44 local FLOOR = math.floor
45
46 local GET_FONT = font.getfont
47
48 local ATC = luatexbase.add_to_callback
49
50 -----
51
52 local make = false
53 local on_top = false
54 local DIR = PREV
55 local AB = INS_B
56 local f = 1
57 local color = "1 .25 .45"
58
59 function make_not_on_top ()
60 on_top = false
61 DIR = PREV
62 AB = INS_B
63 f = 1
64 end
65
66 function make_on_top ()
67 on_top = true
68 DIR = NEXT
69 AB = INS_A
70 f = - 1
71 end
72
73 local in_use = make_not_on_top
74
75 function showhyphenation_make ()
76 make = true
77 end
78
79 function showhyphenation_on_top ()
80 in_use = make_on_top
81 in_use()
82 end
83
84 function showhyphenation_lime ()
85 color = ".75 1 .25"
86 end
87
88 local function round ( num, dec )
89 return FLOOR ( num * 10^dec + 0.5 ) / 10^dec
90 end
91
92 local function calc_value ( value )
93 value = round ( value / 65781, 3 )
94 return value
95 end
96
97 local function get_x_value ( n, x_value, minus, half )
98 local exp_fac = 1
99 local f = 1
100 if minus then
101 f = - 1
102 end
103 local ff = 1
104 if half then
105 ff = 0.5
106 end
107 if n.expansion_factor then
108 exp_fac = n.expansion_factor / 1000000 + 1
109 end
110 if n.id == GLYPH then
111 x_value = x_value + calc_value ( n.width ) * exp_fac * f
112 elseif n.id == KERN then
113 x_value = x_value + calc_value ( n.kern ) * exp_fac * f * ff
114 end
115 return x_value
116 end
117
118 local line_end_count = 0
119
120 local function find_glyph ( n, d, kern_value, linebreak, lig )
121 local line_end = nil
122 local ligtype_mark = nil
123 local kern_at_disc = 0
124 if d ( n ) then
125 n = d ( n )
126 while not ( n.id == GLYPH and n.char ~= 45 or n.replace and HAS_GLYPH ( n.replace ) ) do
127 if n.id == GLUE or n.subtype == LEADERS or n.id == INS then
128 return false
129 end
130 if n.width and not ( n.char and n.char == 45 ) then
131 kern_value = get_x_value ( n, kern_value ) -- rules, etc.
132 elseif n.kern then
133 local factor = 1
134 if linebreak or not lig then
135 factor = 0.5 -- hyphen kern
136 kern_at_disc = get_x_value ( n, kern_at_disc ) * factor
137 end
138 kern_value = get_x_value ( n, kern_value ) * factor
139 elseif n.replace then
140 for kern_node in T_ID ( KERN, n.replace ) do
141 kern_value = get_x_value ( kern_node, kern_value )
142 end
143 end
144 if n.char == 45 then
145 line_end = true
146 line_end_count = line_end_count + 1
147 end
148 if line_end and n.user_id == 848485 then
149 ligtype_mark = true
150 end
151 if d ( n ) then
152 n = d ( n )
153 else
154 return false
155 end
156 end
157 if n.replace then
158 local REPLACE = n.replace
159 for some_node in T ( REPLACE ) do
160 if some_node.width then
161 kern_value = get_x_value ( some_node, kern_value )
162 elseif some_node.kern then
163 kern_value = get_x_value ( some_node, kern_value )
164 end
165 end
166 else
167 if n.width then
168 kern_value = get_x_value ( n, kern_value )
169 end
170 end
171 end
172 return n, kern_value, ligtype_mark, kern_at_disc
173 end
174
175 local function check_linebreak ( n, d )
176 while n.id ~= GLYPH do
177 if d ( n ) then
178 n = d ( n )
179 else break end
180 end
181 if n.char == 45 and d == PREV or n.id == GLUE and n.subtype == RIGHTSKIP and d == NEXT then
182 make_not_on_top()
183 return true
184 end
185 end
186
187 local function hyphenation_points ( head )
188 for n in T ( head ) do
189 if n.id == HLIST or n.id == VLIST then
190 n.head = hyphenation_points ( n.head )
191 elseif n.id == DISC then
192 check_linebreak ( n, PREV )
193 check_linebreak ( n, NEXT )
194 local lig_add = 0
195 local prev_next_kern = 0
196 local prev_next_kern_lig = 0
197 local pre_kerns = 0
198 local second_pre_glyph = 0
199 local lig = false
200 if n.replace then
201 if not HAS_GLYPH ( n.replace ) then
202 local REPLACE = n.replace
203 for some_node in T ( REPLACE ) do
204 if some_node.kern and not check_linebreak ( n, DIR ) then
205 prev_next_kern_lig = get_x_value ( some_node, prev_next_kern_lig ) * 0.5
206 end
207 end
208 else
209 local gn_counter = 0
210 for glyph_node in T_GLYPH ( n.replace ) do
211 if glyph_node.components then
212 lig = true
213 end
214 end
215 if n.post then
216 for _ in T_ID ( KERN, n.pre ) do
217 pre_kerns = pre_kerns + 1
218 end
219 for glyph_node in T_GLYPH ( n.pre ) do
220 if glyph_node.width then
221 lig_add = get_x_value ( glyph_node, lig_add )
222 break -- hyphen!
223 end
224 end
225 for glyph_node in T_GLYPH ( n.replace ) do
226 if glyph_node.width then
227 gn_counter = gn_counter + 1
228 if gn_counter == 2 then
229 second_pre_glyph = get_x_value ( glyph_node, second_pre_glyph, true )
230 end
231 end
232 end
233 if not on_top then
234 if n.replace.kern then
235 prev_next_kern_lig = get_x_value ( n.replace, prev_next_kern_lig )
236 end
237 end
238 else
239 if n.replace.kern then
240 prev_next_kern_lig = get_x_value ( n.replace, prev_next_kern_lig ) * 0.5
241 end
242 for glyph_node in T_GLYPH ( n.replace ) do -- short-armed f
243 if glyph_node.char ~= 45 then
244 lig_add = get_x_value ( glyph_node, lig_add )
245 end
246 end
247 end
248 if on_top then
249 for some_node in T ( n.replace ) do
250 if some_node.width then
251 lig_add = get_x_value ( some_node, lig_add, true )
252 elseif some_node.kern and some_node ~= n.replace then
253 lig_add = get_x_value ( some_node, lig_add, true )
254 end
255 end
256 end
257 if gn_counter > 1 then
258 local counter = 0
259 if not on_top then
260 for kern_node in T_ID ( KERN, n.replace ) do
261 counter = counter + 1
262 if counter > 1 then
263 if pre_kerns > 1 then
264 if pre_kerns > 2 then
265 prev_next_kern_lig = get_x_value ( kern_node, prev_next_kern_lig ) + lig_add
266 else
267 prev_next_kern_lig = get_x_value ( kern_node, prev_next_kern_lig, false, true )
268 end
269 pre_kerns = pre_kerns - 1
270 end
271 end
272 end
273 else
274 for kern_node in T_ID ( KERN, n.replace ) do
275 counter = counter + 1
276 if counter > 1 then
277 if pre_kerns > 1 then
278 if pre_kerns > 2 then
279 prev_next_kern_lig = get_x_value ( kern_node, prev_next_kern_lig, true ) + second_pre_glyph
280 else
281 prev_next_kern_lig = get_x_value ( kern_node, prev_next_kern_lig, true, true )
282 end
283 pre_kerns = pre_kerns - 1
284 end
285 end
286 end
287 end
288 end
289 end
290 end
291 local prev_next_glyph = n
292 local ligtype_mark = nil
293 if find_glyph ( prev_next_glyph, DIR, 0 ) then
294 prev_next_glyph, prev_next_kern, ligtype_mark = find_glyph ( prev_next_glyph, DIR, 0, check_linebreak ( n, PREV ) or check_linebreak ( n, NEXT ), lig )
295 end
296 local reverse = PREV
297 if DIR == PREV then
298 reverse = NEXT
299 end
300 if find_glyph ( n, reverse, 0 ) then
301 local _, __, ___, kern_at_disc = find_glyph ( n, reverse, 0, check_linebreak ( n, PREV ) or check_linebreak ( n, NEXT ), lig )
302 if kern_at_disc then
303 prev_next_kern = prev_next_kern + kern_at_disc
304 end
305 end
306 head = AB ( head, prev_next_glyph, NEW ( WI, PDF_LITERAL ) )
307 lig_add = lig_add + ( prev_next_kern + prev_next_kern_lig ) * f
308 DIR ( prev_next_glyph ).mode = 0
309 local size_factor = 1
310 if font.current() then
311 size_factor = calc_value ( GET_FONT ( font.current() ).size / 10 )
312 end
313 local DATA
314 if PREV ( n ) and PREV ( n ).user_id and PREV ( n ).user_id == 848485 or ligtype_mark then
315 DATA = "q " .. lig_add - 2 * size_factor .. " " .. -3 * size_factor .. " m " .. lig_add + 2 * size_factor .. " " .. -3 * size_factor .. " l " .. lig_add + 2 * size_factor .. " " .. -4 * size_factor .. " l " .. lig_add - 2 * size_factor .. " " .. -4 * size_factor .. " l " .. color .. " rg f Q"
316 else
317 DATA = "q " .. lig_add .. " 0 m " .. lig_add + 2 * size_factor .. " " .. -3 * size_factor .. " l " .. lig_add - 2 * size_factor .. " " .. -3 * size_factor .. " l " .. color .. " rg f Q"
318 end
319 DIR ( prev_next_glyph ).data = DATA
320 in_use()
321 end
322 end
323 return head
324 end
325
326 local function make_hyphens ( head )
327 for n in T_ID ( DISC, head ) do
328 local REPLACE = n.replace
329 for some_node in T ( REPLACE ) do
330 REM ( REPLACE, some_node )
331 end
332 local PRE = n.pre
333 for some_node in T_GLYPH ( PRE ) do
334 REPLACE = INS_A ( REPLACE, REPLACE, COPY ( some_node ) )
335 end
336 local POST = n.post
337 for some_node in T_GLYPH ( POST ) do
338 REPLACE = INS_A ( REPLACE, REPLACE, COPY ( some_node ) )
339 end
340 n.replace = REPLACE
341 end
342 end
343
344 function showhyphenation_start ()
345 if make then
346 ATC ( "ligaturing", make_hyphens, "make hyphens" )
347 else
348 ATC ( "post_linebreak_filter", hyphenation_points, "show hyphenation points in postline", 1 )
349 ATC ( "hpack_filter", hyphenation_points, "show hyphenation points in hpack" )
350 end
351 end