Debian Codegraph @

texmf-dist/tex/lualatex/lua-typo/lua-typo.sty

raw
1 %%
2 %% This is file `lua-typo.sty',
3 %% generated with the docstrip utility.
4 %%
5 %% The original source files were:
6 %%
7 %% lua-typo.dtx (with options: `sty')
8 %%
9 %% IMPORTANT NOTICE:
10 %% For the copyright see the source file `lua-typo.dtx’.
11 %%
12 \NeedsTeXFormat{LaTeX2e}[2021/06/01]
13 \ProvidesPackage{lua-typo}
14 [2026-01-07 v.0.88 Daniel Flipo]
15 \DeclareRelease{v0.4}{2021-01-01}{lua-typo-2021-04-18.sty}
16 \DeclareRelease{v0.65}{2023-03-08}{lua-typo-2023-03-08.sty}
17 \DeclareCurrentRelease{}{2023-09-13}
18 \ifdefined\directlua
19 \RequirePackage{luatexbase,luacode,luacolor,atveryend}
20 \else
21 \PackageError{This package is meant for LuaTeX only! Aborting}
22 {No more information available, sorry!}
23 \fi
24 \newdimen\luatypoLLminWD
25 \newdimen\luatypoBackPI
26 \newdimen\luatypoBackFuzz
27 \newdimen\luatypoMarginparTol
28 \newcount\luatypoStretchMax
29 \newcount\luatypoHyphMax
30 \newcount\luatypoPageMin
31 \newcount\luatypoMinFull
32 \newcount\luatypoMinPart
33 \newcount\luatypoMinLen
34 \newcount\luatypo@LANGno
35 \newcount\luatypo@options
36 \newtoks\luatypo@single
37 \newtoks\luatypo@double
38 \begin{luacode}
39 luatypo = { }
40 \end{luacode}
41 \DeclareKeys[luatypo]
42 {
43 ShowOptions.if = LT@ShowOptions ,
44 None.if = LT@None ,
45 BackParindent.if = LT@BackParindent ,
46 ShortLines.if = LT@ShortLines ,
47 ShortPages.if = LT@ShortPages ,
48 OverfullLines.if = LT@OverfullLines ,
49 UnderfullLines.if = LT@UnderfullLines ,
50 Widows.if = LT@Widows ,
51 Orphans.if = LT@Orphans ,
52 EOPHyphens.if = LT@EOPHyphens ,
53 RepeatedHyphens.if = LT@RepeatedHyphens ,
54 ParLastHyphen.if = LT@ParLastHyphen ,
55 EOLShortWords.if = LT@EOLShortWords ,
56 FirstWordMatch.if = LT@FirstWordMatch ,
57 LastWordMatch.if = LT@LastWordMatch ,
58 FootnoteSplit.if = LT@FootnoteSplit ,
59 ShortFinalWord.if = LT@ShortFinalWord ,
60 MarginparPos.if = LT@MarginparPos ,
61 PageFirstLine.if = LT@PageFirstLine ,
62 All.if = LT@All ,
63 All.code = \LT@ShortLinestrue \LT@ShortPagestrue
64 \LT@OverfullLinestrue \LT@UnderfullLinestrue
65 \LT@Widowstrue \LT@Orphanstrue
66 \LT@EOPHyphenstrue \LT@RepeatedHyphenstrue
67 \LT@ParLastHyphentrue \LT@EOLShortWordstrue
68 \LT@FirstWordMatchtrue \LT@LastWordMatchtrue
69 \LT@BackParindenttrue \LT@FootnoteSplittrue
70 \LT@ShortFinalWordtrue \LT@MarginparPostrue
71 }
72 \ProcessKeyOptions[luatypo]
73 \AtEndOfPackage{%
74 \ifLT@None
75 \directlua{ luatypo.None = true }%
76 \else
77 \directlua{ luatypo.None = false }%
78 \fi
79 \ifLT@BackParindent
80 \advance\luatypo@options by 1
81 \directlua{ luatypo.BackParindent = true }%
82 \else
83 \directlua{ luatypo.BackParindent = false }%
84 \fi
85 \ifLT@ShortLines
86 \advance\luatypo@options by 1
87 \directlua{ luatypo.ShortLines = true }%
88 \else
89 \directlua{ luatypo.ShortLines = false }%
90 \fi
91 \ifLT@ShortPages
92 \advance\luatypo@options by 1
93 \directlua{ luatypo.ShortPages = true }%
94 \else
95 \directlua{ luatypo.ShortPages = false }%
96 \fi
97 \ifLT@OverfullLines
98 \advance\luatypo@options by 1
99 \directlua{ luatypo.OverfullLines = true }%
100 \else
101 \directlua{ luatypo.OverfullLines = false }%
102 \fi
103 \ifLT@UnderfullLines
104 \advance\luatypo@options by 1
105 \directlua{ luatypo.UnderfullLines = true }%
106 \else
107 \directlua{ luatypo.UnderfullLines = false }%
108 \fi
109 \ifLT@Widows
110 \advance\luatypo@options by 1
111 \directlua{ luatypo.Widows = true }%
112 \else
113 \directlua{ luatypo.Widows = false }%
114 \fi
115 \ifLT@Orphans
116 \advance\luatypo@options by 1
117 \directlua{ luatypo.Orphans = true }%
118 \else
119 \directlua{ luatypo.Orphans = false }%
120 \fi
121 \ifLT@EOPHyphens
122 \advance\luatypo@options by 1
123 \directlua{ luatypo.EOPHyphens = true }%
124 \else
125 \directlua{ luatypo.EOPHyphens = false }%
126 \fi
127 \ifLT@RepeatedHyphens
128 \advance\luatypo@options by 1
129 \directlua{ luatypo.RepeatedHyphens = true }%
130 \else
131 \directlua{ luatypo.RepeatedHyphens = false }%
132 \fi
133 \ifLT@ParLastHyphen
134 \advance\luatypo@options by 1
135 \directlua{ luatypo.ParLastHyphen = true }%
136 \else
137 \directlua{ luatypo.ParLastHyphen = false }%
138 \fi
139 \ifLT@EOLShortWords
140 \advance\luatypo@options by 1
141 \directlua{ luatypo.EOLShortWords = true }%
142 \else
143 \directlua{ luatypo.EOLShortWords = false }%
144 \fi
145 \ifLT@FirstWordMatch
146 \advance\luatypo@options by 1
147 \directlua{ luatypo.FirstWordMatch = true }%
148 \else
149 \directlua{ luatypo.FirstWordMatch = false }%
150 \fi
151 \ifLT@LastWordMatch
152 \advance\luatypo@options by 1
153 \directlua{ luatypo.LastWordMatch = true }%
154 \else
155 \directlua{ luatypo.LastWordMatch = false }%
156 \fi
157 \ifLT@FootnoteSplit
158 \advance\luatypo@options by 1
159 \directlua{ luatypo.FootnoteSplit = true }%
160 \else
161 \directlua{ luatypo.FootnoteSplit = false }%
162 \fi
163 \ifLT@ShortFinalWord
164 \advance\luatypo@options by 1
165 \directlua{ luatypo.ShortFinalWord = true }%
166 \else
167 \directlua{ luatypo.ShortFinalWord = false }%
168 \fi
169 \ifLT@MarginparPos
170 \advance\luatypo@options by 1
171 \directlua{ luatypo.MarginparPos = true }%
172 \else
173 \directlua{ luatypo.MarginparPos = false }%
174 \fi
175 \ifLT@PageFirstLine
176 \advance\luatypo@options by 1
177 \directlua{ luatypo.PageFirstLine = true }%
178 \else
179 \directlua{ luatypo.PageFirstLine = false }%
180 \fi
181 }
182 \ifLT@ShowOptions
183 \GenericWarning{* }{%
184 *** List of possible options for lua-typo ***\MessageBreak
185 [Default values between brackets]%
186 \MessageBreak
187 ShowOptions [false]\MessageBreak
188 None [false]\MessageBreak
189 All [false]\MessageBreak
190 BackParindent [false]\MessageBreak
191 ShortLines [false]\MessageBreak
192 ShortPages [false]\MessageBreak
193 OverfullLines [false]\MessageBreak
194 UnderfullLines [false]\MessageBreak
195 Widows [false]\MessageBreak
196 Orphans [false]\MessageBreak
197 EOPHyphens [false]\MessageBreak
198 RepeatedHyphens [false]\MessageBreak
199 ParLastHyphen [false]\MessageBreak
200 EOLShortWords [false]\MessageBreak
201 FirstWordMatch [false]\MessageBreak
202 LastWordMatch [false]\MessageBreak
203 FootnoteSplit [false]\MessageBreak
204 ShortFinalWord [false]\MessageBreak
205 MarginparPos [false]\MessageBreak
206 \MessageBreak
207 *********************************************%
208 \MessageBreak Lua-typo [ShowOptions]
209 }%
210 \fi
211 \AtBeginDocument{%
212 \@ifpackageloaded{reledmac}%
213 {\PackageWarning{lua-typo}{%
214 'lua-typo' is incompatible with\MessageBreak
215 the 'reledmac' package.\MessageBreak
216 'lua-typo' checking disabled.\MessageBreak
217 Reported}%
218 \LT@Nonetrue
219 \directlua{ luatypo.None = true }%
220 }{}%
221 \directlua{
222 luatypo.HYPHmax = tex.count.luatypoHyphMax
223 luatypo.PAGEmin = tex.count.luatypoPageMin
224 luatypo.Stretch = tex.count.luatypoStretchMax
225 luatypo.MinFull = tex.count.luatypoMinFull
226 luatypo.MinPart = tex.count.luatypoMinPart
227 luatypo.MinFull = math.min(luatypo.MinPart,luatypo.MinFull)
228 luatypo.MinLen = tex.count.luatypoMinLen
229 luatypo.LLminWD = tex.dimen.luatypoLLminWD
230 luatypo.BackPI = tex.dimen.luatypoBackPI
231 luatypo.BackFuzz = tex.dimen.luatypoBackFuzz
232 luatypo.MParTol = tex.dimen.luatypoMarginparTol
233 local tbl = luatypo.colortbl
234 local map = { }
235 for i,v in ipairs (luatypo.colortbl) do
236 if i == 1 or v > tbl[i-1] then
237 table.insert(map, v)
238 end
239 end
240 luatypo.map = map
241 }%
242 }
243 \AtVeryEndDocument{%
244 \ifnum\luatypo@options = 0 \LT@Nonetrue \fi
245 \ifLT@None
246 \directlua{
247 texio.write_nl(' ')
248 texio.write_nl('************************************')
249 texio.write_nl('*** lua-typo running with NO option:')
250 texio.write_nl('*** NO CHECK PERFORMED! ***')
251 texio.write_nl('************************************')
252 texio.write_nl(' ')
253 }%
254 \else
255 \directlua{
256 texio.write_nl(' ')
257 texio.write_nl('*************************************')
258 if luatypo.pagelist == " " then
259 if luatypo.failedlist == " " then
260 texio.write_nl('*** lua-typo: No Typo Flaws found.')
261 else
262 texio.write_nl('*** WARNING: ')
263 texio.write('lua-typo failed to scan these pages:')
264 texio.write_nl('***' .. luatypo.failedlist)
265 texio.write_nl('*** Please report to the maintainer.')
266 end
267 else
268 texio.write_nl('*** lua-typo: WARNING *************')
269 texio.write_nl('The following pages need attention:')
270 texio.write(luatypo.pagelist)
271 end
272 texio.write_nl('*************************************')
273 texio.write_nl(' ')
274 if luatypo.failedlist == " " then
275 else
276 local prt = "WARNING: lua-typo failed to scan pages " ..
277 luatypo.failedlist .. "\string\n\string\n"
278 luatypo.buffer = prt .. luatypo.buffer
279 end
280 local fileout= tex.jobname .. ".typo"
281 local out=io.open(fileout,"w+")
282 out:write(luatypo.buffer)
283 io.close(out)
284 }%
285 \fi}
286 \newcommand*{\luatypoOneChar}[2]{%
287 \def\luatypo@LANG{#1}\luatypo@single={#2}%
288 \ifcsname l@\luatypo@LANG\endcsname
289 \luatypo@LANGno=\the\csname l@\luatypo@LANG\endcsname \relax
290 \directlua{
291 local langno = \the\luatypo@LANGno
292 local string = \the\luatypo@single
293 luatypo.single[langno] = " "
294 for p, c in utf8.codes(string) do
295 local s = utf8.char(c)
296 luatypo.single[langno] = luatypo.single[langno] .. s
297 end
298 }%
299 \else
300 \PackageWarning{luatypo}{Unknown language "\luatypo@LANG",
301 \MessageBreak \protect\luatypoOneChar\space command ignored}%
302 \fi}
303 \newcommand*{\luatypoTwoChars}[2]{%
304 \def\luatypo@LANG{#1}\luatypo@double={#2}%
305 \ifcsname l@\luatypo@LANG\endcsname
306 \luatypo@LANGno=\the\csname l@\luatypo@LANG\endcsname \relax
307 \directlua{
308 local langno = \the\luatypo@LANGno
309 local string = \the\luatypo@double
310 luatypo.double[langno] = " "
311 for p, c in utf8.codes(string) do
312 local s = utf8.char(c)
313 luatypo.double[langno] = luatypo.double[langno] .. s
314 end
315 }%
316 \else
317 \PackageWarning{luatypo}{Unknown language "\luatypo@LANG",
318 \MessageBreak \protect\luatypoTwoChars\space command ignored}%
319 \fi}
320 \newcommand*{\luatypoSetColor}[2]{%
321 \begingroup
322 \color{#2}%
323 \directlua{luatypo.colortbl[#1]=\the\LuaCol@Attribute}%
324 \endgroup
325 }
326 \begin{luacode}
327 luatypo.colortbl = { }
328 luatypo.map = { }
329 luatypo.single = { }
330 luatypo.double = { }
331 luatypo.pagelist = " "
332 luatypo.failedlist = " "
333 luatypo.buffer = "List of typographic flaws found for "
334 .. tex.jobname .. ".pdf:\string\n\string\n"
335
336 local char_to_discard = { }
337 char_to_discard[string.byte(",")] = true
338 char_to_discard[string.byte(".")] = true
339 char_to_discard[string.byte("!")] = true
340 char_to_discard[string.byte("?")] = true
341 char_to_discard[string.byte(":")] = true
342 char_to_discard[string.byte(";")] = true
343 char_to_discard[string.byte("-")] = true
344
345 local eow_char = { }
346 eow_char[string.byte(".")] = true
347 eow_char[string.byte("!")] = true
348 eow_char[string.byte("?")] = true
349 eow_char[utf8.codepoint("…")] = true
350
351 local DISC = node.id("disc")
352 local GLYPH = node.id("glyph")
353 local GLUE = node.id("glue")
354 local KERN = node.id("kern")
355 local RULE = node.id("rule")
356 local HLIST = node.id("hlist")
357 local VLIST = node.id("vlist")
358 local LPAR = node.id("local_par")
359 local MKERN = node.id("margin_kern")
360 local PENALTY = node.id("penalty")
361 local WHATSIT = node.id("whatsit")
362 local USRSKIP = 0
363 local PARSKIP = 3
364 local LFTSKIP = 8
365 local RGTSKIP = 9
366 local TOPSKIP = 10
367 local PARFILL = 15
368 local LINE = 1
369 local BOX = 2
370 local INDENT = 3
371 local ALIGN = 4
372 local EQN = 6
373 local USER = 0
374 local HYPH = 0x2D
375 local LIGA = 0x102
376 local parline = 0
377 local dimensions = node.dimensions
378 local rangedimensions = node.rangedimensions
379 local effective_glue = node.effective_glue
380 local set_attribute = node.set_attribute
381 local get_attribute = node.get_attribute
382 local slide = node.slide
383 local traverse = node.traverse
384 local traverse_id = node.traverse_id
385 local has_field = node.has_field
386 local uses_font = node.uses_font
387 local is_glyph = node.is_glyph
388 local utf8_len = utf8.len
389 local utf8_find = unicode.utf8.find
390 local utf8_gsub = unicode.utf8.gsub
391 local utf8_reverse = function (s)
392 if utf8_len(s) > 1 then
393 local so = ""
394 for p, c in utf8.codes(s) do
395 so = utf8.char(c) .. so
396 end
397 s = so
398 end
399 return s
400 end
401 local utf8_sub = function (s,i,j)
402 i=utf8.offset(s,i)
403 j=utf8.offset(s,j+1)-1
404 return string.sub(s,i,j)
405 end
406 local color_node = function (node, color)
407 local attr = oberdiek.luacolor.getattribute()
408 if node and node.id == DISC then
409 local pre = node.pre
410 local post = node.post
411 local repl = node.replace
412 if pre then
413 set_attribute(pre,attr,color)
414 end
415 if post then
416 set_attribute(post,attr,color)
417 end
418 if repl then
419 set_attribute(repl,attr,color)
420 end
421 elseif node then
422 set_attribute(node,attr,color)
423 end
424 end
425 local color_line = function (head, color)
426 local first = head.head
427 local map = luatypo.map
428 local color_node_if = function (node, color)
429 local c = oberdiek.luacolor.getattribute()
430 local att = get_attribute(node,c)
431 local uncolored = true
432 for i,v in ipairs (map) do
433 if att == v then
434 uncolored = false
435 break
436 end
437 end
438 if uncolored then
439 color_node (node, color)
440 end
441 end
442 for n in traverse(first) do
443 if n.id == HLIST or n.id == VLIST then
444 local ff = n.head
445 for nn in traverse(ff) do
446 if nn.id == HLIST or nn.id == VLIST then
447 local f3 = nn.head
448 for n3 in traverse(f3) do
449 if n3.id == HLIST or n3.id == VLIST then
450 local f4 = n3.head
451 for n4 in traverse(f4) do
452 if n4.id == HLIST or n4.id == VLIST then
453 local f5 = n4.head
454 for n5 in traverse(f5) do
455 if n5.id == HLIST or n5.id == VLIST then
456 local f6 = n5.head
457 for n6 in traverse(f6) do
458 color_node_if(n6, color)
459 end
460 else
461 color_node_if(n5, color)
462 end
463 end
464 else
465 color_node_if(n4, color)
466 end
467 end
468 else
469 color_node_if(n3, color)
470 end
471 end
472 else
473 color_node_if(nn, color)
474 end
475 end
476 else
477 color_node_if(n, color)
478 end
479 end
480 end
481 log_flaw= function (msg, line, colno, footnote)
482 local pageno = tex.getcount("c@page")
483 local prt ="p. " .. pageno
484 if colno then
485 prt = prt .. ", col." .. colno
486 end
487 if line then
488 local l = string.format("%2d, ", line)
489 if footnote then
490 prt = prt .. ", (ftn.) line " .. l
491 else
492 prt = prt .. ", line " .. l
493 end
494 end
495 prt = prt .. msg
496 luatypo.buffer = luatypo.buffer .. prt .. "\string\n"
497 end
498 local signature = function (node, string, swap)
499 local n = node
500 local str = string
501 if n and n.id == GLYPH then
502 local b = n.char
503 if b and not char_to_discard[b] then
504 if n.components then
505 local c = ""
506 for nn in traverse_id(GLYPH, n.components) do
507 c = c .. utf8.char(nn.char)
508 end
509 if swap then
510 str = str .. utf8_reverse(c)
511 else
512 str = str .. c
513 end
514 else
515 str = str .. utf8.char(b)
516 end
517 end
518 elseif n and n.id == DISC then
519 local pre = n.pre
520 local post = n.post
521 local c1 = ""
522 local c2 = ""
523 if pre and pre.char then
524 if pre.components then
525 for nn in traverse_id(GLYPH, pre.components) do
526 c1 = c1 .. utf8.char(nn.char)
527 end
528 else
529 c1 = utf8.char(pre.char)
530 end
531 c1 = utf8_gsub(c1, "-", "")
532 end
533 if post and post.char then
534 if post.components then
535 for nn in traverse_id(GLYPH, post.components) do
536 c2 = c2 .. utf8.char(nn.char)
537 end
538 else
539 c2 = utf8.char(post.char)
540 end
541 end
542 if swap then
543 str = str .. utf8_reverse(c2) .. c1
544 else
545 str = str .. c1 .. c2
546 end
547 elseif n and n.id == GLUE then
548 str = str .. "_"
549 end
550 local s = utf8_gsub(str, "_", "")
551 local len = utf8_len(s)
552 return len, str
553 end
554 local check_line_last_word =
555 function (old, node, line, colno, flag, footnote)
556 local COLOR = luatypo.colortbl[12]
557 local match = false
558 local new = ""
559 local maxlen = 0
560 local MinFull = luatypo.MinFull
561 local MinPart = luatypo.MinPart
562 if node then
563 local swap = true
564 local box, go
565 local lastn = node
566 while lastn and lastn.id ~= GLYPH and lastn.id ~= DISC and
567 lastn.id ~= HLIST do
568 lastn = lastn.prev
569 end
570 local n = lastn
571 local words = 0
572 while n and (words <= 2 or maxlen < MinPart) do
573 if n and n.id == HLIST then
574 box = n
575 local first = n.head
576 local lastn = slide(first)
577 n = lastn
578 while n do
579 maxlen, new = signature (n, new, swap)
580 n = n.prev
581 end
582 n = box.prev
583 local w = utf8_gsub(new, "_", "")
584 words = words + utf8_len(new) - utf8_len(w) + 1
585 else
586 repeat
587 maxlen, new = signature (n, new, swap)
588 n = n.prev
589 until not n or n.id == GLUE or n.id == HLIST
590 if n and n.id == GLUE then
591 maxlen, new = signature (n, new, swap)
592 words = words + 1
593 n = n.prev
594 end
595 end
596 end
597 new = utf8_reverse(new)
598 new = utf8_gsub(new, "_+$", "") -- $
599 new = utf8_gsub(new, "^_+", "")
600 maxlen = math.min(utf8_len(old), utf8_len(new))
601 if flag and old ~= "" then
602 local oldlast = utf8_gsub (old, ".*_", "")
603 local newlast = utf8_gsub (new, ".*_", "")
604 local oldsub = ""
605 local newsub = ""
606 local dlo = utf8_reverse(old)
607 local wen = utf8_reverse(new)
608 for p, c in utf8.codes(dlo) do
609 local s = utf8_gsub(oldsub, "_", "")
610 if utf8_len(s) < MinPart then
611 oldsub = utf8.char(c) .. oldsub
612 end
613 end
614 for p, c in utf8.codes(wen) do
615 local s = utf8_gsub(newsub, "_", "")
616 if utf8_len(s) < MinPart then
617 newsub = utf8.char(c) .. newsub
618 end
619 end
620 if oldsub == newsub then
621 match = true
622 end
623 if oldlast == newlast and utf8_len(newlast) >= MinFull then
624 if utf8_len(newlast) > MinPart or not match then
625 oldsub = oldlast
626 newsub = newlast
627 end
628 match = true
629 end
630 if match then
631 local k = utf8_len(newsub)
632 local osub = utf8_reverse(oldsub)
633 local nsub = utf8_reverse(newsub)
634 while osub == nsub and k < maxlen do
635 k = k + 1
636 osub = utf8_sub(dlo,1,k)
637 nsub = utf8_sub(wen,1,k)
638 if osub == nsub then
639 newsub = utf8_reverse(nsub)
640 end
641 end
642 newsub = utf8_gsub(newsub, "^_+", "")
643 local msg = "E.O.L. MATCH=" .. newsub
644 log_flaw(msg, line, colno, footnote)
645 local ns = utf8_gsub(newsub, "_", "")
646 k = utf8_len(ns)
647 oldsub = utf8_reverse(newsub)
648 local newsub = ""
649 local n = lastn
650 local l = 0
651 local lo = 0
652 local li = 0
653 while n and newsub ~= oldsub and l < k do
654 if n and n.id == HLIST then
655 local first = n.head
656 for nn in traverse_id(GLYPH, first) do
657 color_node(nn, COLOR)
658 local c = nn.char
659 if not char_to_discard[c] then l = l + 1 end
660 end
661 elseif n then
662 color_node(n, COLOR)
663 li, newsub = signature(n, newsub, swap)
664 l = l + li - lo
665 lo = li
666 end
667 n = n.prev
668 end
669 end
670 end
671 end
672 return new, match
673 end
674 local check_line_first_word =
675 function (old, node, line, colno, flag, footnote)
676 local COLOR = luatypo.colortbl[11]
677 local match = false
678 local swap = false
679 local new = ""
680 local maxlen = 0
681 local MinFull = luatypo.MinFull
682 local MinPart = luatypo.MinPart
683 local n = node
684 local box, go
685 while n and n.id ~= GLYPH and n.id ~= DISC and
686 (n.id ~= HLIST or n.subtype == INDENT) do
687 n = n.next
688 end
689 start = n
690 local words = 0
691 while n and (words <= 2 or maxlen < MinPart) do
692 if n and n.id == HLIST then
693 box = n
694 n = n.head
695 while n do
696 maxlen, new = signature (n, new, swap)
697 n = n.next
698 end
699 n = box.next
700 local w = utf8_gsub(new, "_", "")
701 words = words + utf8_len(new) - utf8_len(w) + 1
702 else
703 repeat
704 maxlen, new = signature (n, new, swap)
705 n = n.next
706 until not n or n.id == GLUE or n.id == HLIST
707 if n and n.id == GLUE then
708 maxlen, new = signature (n, new, swap)
709 words = words + 1
710 n = n.next
711 end
712 end
713 end
714 new = utf8_gsub(new, "_+$", "") -- $
715 new = utf8_gsub(new, "^_+", "")
716 maxlen = math.min(utf8_len(old), utf8_len(new))
717 if flag and old ~= "" then
718 local oldfirst = utf8_gsub (old, "_.*", "")
719 local newfirst = utf8_gsub (new, "_.*", "")
720 local oldsub = ""
721 local newsub = ""
722 for p, c in utf8.codes(old) do
723 local s = utf8_gsub(oldsub, "_", "")
724 if utf8_len(s) < MinPart then
725 oldsub = oldsub .. utf8.char(c)
726 end
727 end
728 for p, c in utf8.codes(new) do
729 local s = utf8_gsub(newsub, "_", "")
730 if utf8_len(s) < MinPart then
731 newsub = newsub .. utf8.char(c)
732 end
733 end
734 if oldsub == newsub then
735 match = true
736 end
737 if oldfirst == newfirst and utf8_len(newfirst) >= MinFull then
738 if utf8_len(newfirst) > MinPart or not match then
739 oldsub = oldfirst
740 newsub = newfirst
741 end
742 match = true
743 end
744 if match then
745 local k = utf8_len(newsub)
746 local osub = oldsub
747 local nsub = newsub
748 while osub == nsub and k < maxlen do
749 k = k + 1
750 osub = utf8_sub(old,1,k)
751 nsub = utf8_sub(new,1,k)
752 if osub == nsub then
753 newsub = nsub
754 end
755 end
756 newsub = utf8_gsub(newsub, "_+$", "") --$
757 local msg = "B.O.L. MATCH=" .. newsub
758 log_flaw(msg, line, colno, footnote)
759 local ns = utf8_gsub(newsub, "_", "")
760 k = utf8_len(ns)
761 oldsub = newsub
762 local newsub = ""
763 local n = start
764 local l = 0
765 local lo = 0
766 local li = 0
767 while n and newsub ~= oldsub and l < k do
768 if n and n.id == HLIST then
769 local nn = n.head
770 for nnn in traverse(nn) do
771 color_node(nnn, COLOR)
772 local c = nn.char
773 if not char_to_discard[c] then l = l + 1 end
774 end
775 elseif n then
776 color_node(n, COLOR)
777 li, newsub = signature(n, newsub, swap)
778 l = l + li - lo
779 lo = li
780 end
781 n = n.next
782 end
783 end
784 end
785 return new, match
786 end
787 local check_page_first_word = function (node, colno, footnote)
788 local COLOR = luatypo.colortbl[15]
789 local match = false
790 local swap = false
791 local new = ""
792 local minlen = luatypo.MinLen
793 local len = 0
794 local n = node
795 local pn
796 while n and n.id ~= GLYPH and n.id ~= DISC and
797 (n.id ~= HLIST or n.subtype == INDENT) do
798 n = n.next
799 end
800 local start = n
801 if n and n.id == HLIST then
802 start = n.head
803 n = n.head
804 end
805 repeat
806 len, new = signature (n, new, swap)
807 n = n.next
808 until len > minlen or (n and n.id == GLYPH and eow_char[n.char]) or
809 (n and n.id == GLUE) or
810 (n and n.id == KERN and n.subtype == 1)
811 if n and (n.id == GLUE or n.id == KERN) then
812 pn = n
813 n = n.next
814 end
815 if len <= minlen and n and n.id == GLYPH and eow_char[n.char] then
816 repeat
817 n = n.next
818 until not n or n.id == GLYPH or
819 (n.id == GLUE and n.subtype == PARFILL)
820 if n and n.id == GLYPH then
821 match = true
822 end
823 end
824 if match then
825 local msg = "ShortFinalWord=" .. new
826 log_flaw(msg, 1, colno, footnote)
827 local n = start
828 repeat
829 color_node(n, COLOR)
830 n = n.next
831 until eow_char[n.char]
832 color_node(n, COLOR)
833 end
834 return match
835 end
836 local check_regexpr = function (glyph, line, colno, footnote)
837 local COLOR = luatypo.colortbl[4]
838 local lang = glyph.lang
839 local match = false
840 local retflag = false
841 local lchar, id = is_glyph(glyph)
842 local previous = glyph.prev
843 if lang and luatypo.single[lang] then
844 if lchar and previous and previous.id == GLUE then
845 match = utf8_find(luatypo.single[lang], utf8.char(lchar))
846 if match then
847 retflag = true
848 local msg = "RGX MATCH=" .. utf8.char(lchar)
849 log_flaw(msg, line, colno, footnote)
850 color_node(glyph,COLOR)
851 end
852 end
853 end
854 if lang and luatypo.double[lang] then
855 if lchar and previous and previous.id == GLYPH then
856 local pchar, id = is_glyph(previous)
857 local pprev = previous.prev
858 if pchar and pprev and pprev.id == GLUE then
859 local pattern = utf8.char(pchar) .. utf8.char(lchar)
860 match = utf8_find(luatypo.double[lang], pattern)
861 if match then
862 retflag = true
863 local msg = "RGX MATCH=" .. pattern
864 log_flaw(msg, line, colno, footnote)
865 color_node(previous,COLOR)
866 color_node(glyph,COLOR)
867 end
868 end
869 elseif lchar and previous and previous.id == KERN then
870 local pprev = previous.prev
871 if pprev and pprev.id == GLYPH then
872 local pchar, id = is_glyph(pprev)
873 local ppprev = pprev.prev
874 if pchar and ppprev and ppprev.id == GLUE then
875 local pattern = utf8.char(pchar) .. utf8.char(lchar)
876 match = utf8_find(luatypo.double[lang], pattern)
877 if match then
878 retflag = true
879 local msg = "REGEXP MATCH=" .. pattern
880 log_flaw(msg, line, colno, footnote)
881 color_node(pprev,COLOR)
882 color_node(glyph,COLOR)
883 end
884 end
885 end
886 end
887 end
888 return retflag
889 end
890 local show_pre_disc = function (disc, color)
891 local n = disc
892 while n and n.id ~= GLUE do
893 color_node(n, color)
894 n = n.prev
895 end
896 return n
897 end
898 local footnoterule_ahead = function (head)
899 local n = head
900 local flag = false
901 local totalht, ruleht, ht1, ht2, ht3
902 if n and n.id == KERN and n.subtype == 1 then
903 totalht = n.kern
904 n = n.next
905 while n and n.id == GLUE do n = n.next end
906 if n and n.id == RULE and n.subtype == 0 then
907 ruleht = n.height
908 totalht = totalht + ruleht
909 n = n.next
910 if n and n.id == KERN and n.subtype == 1 then
911 totalht = totalht + n.kern
912 if totalht == 0 or totalht == ruleht then
913 flag = true
914 else
915 end
916 end
917 end
918 end
919 return flag
920 end
921 local check_EOP = function (node)
922 local n = node
923 local page_bot = false
924 local body_bot = false
925 while n and (n.id == GLUE or n.id == PENALTY or
926 n.id == WHATSIT ) do
927 n = n.next
928 end
929 if not n then
930 page_bot = true
931 body_bot = true
932 elseif footnoterule_ahead(n) then
933 body_bot = true
934 end
935 return page_bot, body_bot
936 end
937 local check_marginnote = function (head, line, colno, vpos, bpmn)
938 local OverfullLines = luatypo.OverfullLines
939 local UnderfullLines = luatypo.UnderfullLines
940 local MarginparPos = luatypo.MarginparPos
941 local margintol = luatypo.MParTol
942 local marginpp = tex.getdimen("marginparpush")
943 local textht = tex.getdimen("textheight")
944 local mnflag = false
945 local ofirst = true
946 local ufirst = true
947 local n = head.head
948 local bottom = vpos
949 if vpos <= bpmn then
950 bottom = bpmn + marginpp
951 end
952 repeat
953 if n and (n.id == GLUE or n.id == PENALTY) then
954 n = n.next
955 elseif n and n.id == VLIST then
956 n = n.head
957 end
958 until not n or (n.id == HLIST and n.subtype == LINE)
959 local head = n
960 if head then
961 else
962 end
963 local last = head
964 while head do
965 local next = head.next
966 if head.id == HLIST and head.subtype == LINE then
967 bottom = bottom + head.height + head.depth
968 local first = head.head
969 local linewd = head.width
970 local hmax = linewd + tex.hfuzz
971 local w,h,d = dimensions(1,2,0, first)
972 local Stretch = math.max(luatypo.Stretch/100,1)
973 if w > hmax and OverfullLines then
974 mnflag = true
975 local COLOR = luatypo.colortbl[8]
976 color_line (head, COLOR)
977 if ofirst then
978 local msg = "OVERFULL line(s) in margin note"
979 log_flaw(msg, line, colno, false)
980 ofirst = false
981 end
982 elseif head.glue_set > Stretch and head.glue_sign == 1 and
983 head.glue_order == 0 and UnderfullLines then
984 mnflag = true
985 local COLOR = luatypo.colortbl[9]
986 color_line (head, COLOR)
987 if ufirst then
988 local msg = "UNDERFULL line(s) in margin note"
989 log_flaw(msg, line, colno, false)
990 ufirst = false
991 end
992 end
993 end
994 last = head
995 head = next
996 end
997 if bottom > textht + margintol and MarginparPos then
998 mnflag = true
999 local COLOR = luatypo.colortbl[17]
1000 color_line (last, COLOR)
1001 local msg = "Margin note too low"
1002 log_flaw(msg, line, colno, false)
1003 end
1004 return bottom, mnflag
1005 end
1006 local check_vbox = function (head, line, colno, vpos, lmax)
1007 local OverfullLines = luatypo.OverfullLines
1008 local UnderfullLines = luatypo.UnderfullLines
1009 local vbflag = false
1010 local l = 0
1011 local ll = line
1012 local n = head.head
1013 while n do
1014 if n.id == HLIST and n.subtype == LINE then
1015 l = l + 1
1016 if l > 1 then ll = ll + 1 end
1017 local first = n.head
1018 local linewd = n.width
1019 local hmax = linewd + tex.hfuzz
1020 local w,h,d = dimensions(1,2,0, first)
1021 local Stretch = math.max(luatypo.Stretch/100,1)
1022 if w > hmax and OverfullLines then
1023 vbflag = true
1024 local COLOR = luatypo.colortbl[8]
1025 color_line (n, COLOR)
1026 local wpt = string.format("%.2fpt", (w-n.width)/65536)
1027 local msg = "OVERFULL line in vbox, " .. wpt
1028 log_flaw(msg, ll, colno, false)
1029 elseif n.glue_set > Stretch and n.glue_sign == 1 and
1030 n.glue_order == 0 and UnderfullLines then
1031 vbflag = true
1032 local COLOR = luatypo.colortbl[9]
1033 color_line (n, COLOR)
1034 local s = string.format("%.0f%s", 100*n.glue_set, "%")
1035 local msg = "UNDERFULL line in vbox, stretch=" .. s
1036 log_flaw(msg, ll, colno, false)
1037 end
1038 end
1039 n = n.next
1040 end
1041 lmax = math.max(l, lmax)
1042 return lmax, vbflag
1043 end
1044 local get_pagebody = function (head)
1045 local textht = tex.getdimen("textheight")
1046 local fn = head.list
1047 local body
1048 repeat
1049 fn = fn.next
1050 until fn.id == VLIST and fn.height > 0
1051 first = fn.list
1052 repeat
1053 for n in traverse_id(VLIST,first) do
1054 if n.subtype == 0 and n.height >= textht-1 then
1055 if n.height <= textht+8 then
1056 body = n
1057 break
1058 else
1059 first = n.list
1060 end
1061 else
1062 end
1063 end
1064 until body or not first
1065 if not body then
1066 texio.write_nl('***lua-typo ERROR: PAGE BODY *NOT* FOUND!***')
1067 end
1068 return body
1069 end
1070 check_vtop = function (top, colno, vpos)
1071 local head = top.list
1072 local PAGEmin = luatypo.PAGEmin
1073 local HYPHmax = luatypo.HYPHmax
1074 local LLminWD = luatypo.LLminWD
1075 local BackPI = luatypo.BackPI
1076 local BackFuzz = luatypo.BackFuzz
1077 local BackParindent = luatypo.BackParindent
1078 local ShortLines = luatypo.ShortLines
1079 local ShortPages = luatypo.ShortPages
1080 local OverfullLines = luatypo.OverfullLines
1081 local UnderfullLines = luatypo.UnderfullLines
1082 local Widows = luatypo.Widows
1083 local Orphans = luatypo.Orphans
1084 local EOPHyphens = luatypo.EOPHyphens
1085 local RepeatedHyphens = luatypo.RepeatedHyphens
1086 local FirstWordMatch = luatypo.FirstWordMatch
1087 local ParLastHyphen = luatypo.ParLastHyphen
1088 local EOLShortWords = luatypo.EOLShortWords
1089 local LastWordMatch = luatypo.LastWordMatch
1090 local FootnoteSplit = luatypo.FootnoteSplit
1091 local ShortFinalWord = luatypo.ShortFinalWord
1092 local PageFirstLine = luatypo.PageFirstLine
1093 local Stretch = math.max(luatypo.Stretch/100,1)
1094 local blskip = tex.getglue("baselineskip")
1095 local vpos_min = PAGEmin * blskip
1096 vpos_min = vpos_min * 1.5
1097 local linewd = tex.getdimen("textwidth")
1098 local first_bot = true
1099 local done = false
1100 local footnote = false
1101 local ftnsplit = false
1102 local orphanflag = false
1103 local widowflag = false
1104 local pageshort = false
1105 local overfull = false
1106 local underfull = false
1107 local shortline = false
1108 local backpar = false
1109 local pflflag = false
1110 local firstwd = ""
1111 local lastwd = ""
1112 local hyphcount = 0
1113 local pageline = 0
1114 local ftnline = 0
1115 local line = 0
1116 local bpmn = 0
1117 local body_bottom = false
1118 local page_bottom = false
1119 local pageflag = false
1120 local pageno = tex.getcount("c@page")
1121 while head do
1122 local nextnode = head.next
1123 if head.id == HLIST and head.subtype == LINE and
1124 (head.height > 0 or head.depth > 0) then
1125 vpos = vpos + head.height + head.depth
1126 done = true
1127 local linewd = head.width
1128 local first = head.head
1129 local ListItem = false
1130 if footnote then
1131 ftnline = ftnline + 1
1132 line = ftnline
1133 else
1134 pageline = pageline + 1
1135 line = pageline
1136 pflflag = false
1137 end
1138 page_bottom, body_bottom = check_EOP(nextnode)
1139 local hmax = linewd + tex.hfuzz
1140 local w,h,d = dimensions(1,2,0, first)
1141 if w > hmax and OverfullLines then
1142 pageflag = true
1143 overfull = true
1144 local wpt = string.format("%.2fpt", (w-head.width)/65536)
1145 local msg = "OVERFULL line " .. wpt
1146 log_flaw(msg, line, colno, footnote)
1147 elseif head.glue_set > Stretch and head.glue_sign == 1 and
1148 head.glue_order == 0 and UnderfullLines then
1149 pageflag = true
1150 underfull = true
1151 local s = string.format("%.0f%s", 100*head.glue_set, "%")
1152 local msg = "UNDERFULL line stretch=" .. s
1153 log_flaw(msg, line, colno, footnote)
1154 end
1155 if footnote and page_bottom then
1156 ftnsplit = true
1157 end
1158 while first.id == MKERN or
1159 (first.id == GLUE and first.subtype == LFTSKIP) do
1160 first = first.next
1161 end
1162 if first.id == LPAR then
1163 hyphcount = 0
1164 firstwd = ""
1165 lastwd = ""
1166 if not footnote then
1167 parline = 1
1168 if pageline == 1 and PageFirstLine then
1169 local nn = first.next
1170 if nn.id == HLIST and nn.subtype == INDENT and
1171 nn.width > 0 then
1172 pageflag = true
1173 pflflag = true
1174 end
1175 end
1176 if body_bottom then
1177 orphanflag = true
1178 end
1179 end
1180 local nn = first.next
1181 if nn and nn.id == HLIST and nn.subtype == BOX then
1182 ListItem = true
1183 end
1184 elseif not footnote then
1185 parline = parline + 1
1186 end
1187 if FirstWordMatch then
1188 local flag = not ListItem and (line > 1)
1189 firstwd, flag =
1190 check_line_first_word(firstwd, first, line, colno,
1191 flag, footnote)
1192 if flag then
1193 pageflag = true
1194 end
1195 end
1196 if ShortFinalWord and pageline == 1 and parline > 1 and
1197 check_page_first_word(first, colno, footnote) then
1198 pageflag = true
1199 end
1200 local cn = first
1201 local lmax = 1
1202 repeat
1203 if cn.id == VLIST and cn.subtype == 0 then
1204 lmax, vbflag = check_vbox (cn, line, colno, vpos, lmax)
1205 end
1206 cn = cn.next
1207 until not cn
1208 if not footnote then
1209 line = line + lmax - 1
1210 parline = parline + lmax - 1
1211 pageline = pageline + lmax - 1
1212 end
1213 local ln = slide(first)
1214 if ln.id == RULE and ln.subtype == 0 then
1215 ln = ln.prev
1216 end
1217 local pn = ln.prev
1218 if pn and pn.id == GLUE and pn.subtype == PARFILL then
1219 hyphcount = 0
1220 ftnsplit = false
1221 orphanflag = false
1222 if pageline == 1 and parline > 1 then
1223 widowflag = true
1224 end
1225 local PFskip = effective_glue(pn,head)
1226 if ShortLines then
1227 local llwd = linewd - PFskip
1228 if llwd < LLminWD then
1229 pageflag = true
1230 shortline = true
1231 local msg = "SHORT LINE: length=" ..
1232 string.format("%.0fpt", llwd/65536)
1233 log_flaw(msg, line, colno, footnote)
1234 end
1235 end
1236 if BackParindent and PFskip < BackPI and
1237 PFskip >= BackFuzz and parline > 1 then
1238 pageflag = true
1239 backpar = true
1240 local msg = "NEARLY FULL line: backskip=" ..
1241 string.format("%.1fpt", PFskip/65536)
1242 log_flaw(msg, line, colno, footnote)
1243 end
1244 if LastWordMatch then
1245 local flag = true
1246 if PFskip > BackPI or line == 1 then
1247 flag = false
1248 end
1249 local pnp = pn.prev
1250 lastwd, flag =
1251 check_line_last_word(lastwd, pnp, line, colno,
1252 flag, footnote)
1253 if flag then
1254 pageflag = true
1255 end
1256 end
1257 elseif pn and pn.id == DISC then
1258 hyphcount = hyphcount + 1
1259 if hyphcount > HYPHmax and RepeatedHyphens then
1260 local COLOR = luatypo.colortbl[3]
1261 local pg = show_pre_disc (pn,COLOR)
1262 pageflag = true
1263 local msg = "REPEATED HYPHENS: more than " .. HYPHmax
1264 log_flaw(msg, line, colno, footnote)
1265 end
1266 if (page_bottom or body_bottom) and EOPHyphens then
1267 pageflag = true
1268 local msg = "LAST WORD SPLIT"
1269 log_flaw(msg, line, colno, footnote)
1270 local COLOR = luatypo.colortbl[2]
1271 local pg = show_pre_disc (pn,COLOR)
1272 end
1273 if LastWordMatch then
1274 local flag = true
1275 lastwd, flag =
1276 check_line_last_word(lastwd, pn, line, colno,
1277 flag, footnote)
1278 if flag then
1279 pageflag = true
1280 end
1281 end
1282 if nextnode and ParLastHyphen then
1283 local nn = nextnode.next
1284 local nnn = nil
1285 if nn and nn.next then
1286 nnn = nn.next
1287 if nnn.id == HLIST and nnn.subtype == LINE and
1288 nnn.glue_order == 2 then
1289 pageflag = true
1290 local msg = "HYPHEN on next to last line"
1291 log_flaw(msg, line, colno, footnote)
1292 local COLOR = luatypo.colortbl[1]
1293 local pg = show_pre_disc (pn,COLOR)
1294 end
1295 end
1296 end
1297 else
1298 hyphcount = 0
1299 if LastWordMatch and pn then
1300 local flag = true
1301 lastwd, flag =
1302 check_line_last_word(lastwd, pn, line, colno,
1303 flag, footnote)
1304 if flag then
1305 pageflag = true
1306 end
1307 end
1308 if EOLShortWords then
1309 while pn and pn.id ~= GLYPH and pn.id ~= HLIST do
1310 pn = pn.prev
1311 end
1312 if pn and pn.id == GLYPH then
1313 if check_regexpr(pn, line, colno, footnote) then
1314 pageflag = true
1315 end
1316 end
1317 end
1318 end
1319 if pflflag then
1320 local COLOR = luatypo.colortbl[5]
1321 color_line (head, COLOR)
1322 local msg = "INDENTED first line (paragraph start)."
1323 log_flaw(msg, line, colno, false)
1324 end
1325 if widowflag and Widows then
1326 pageflag = true
1327 local msg = "WIDOW"
1328 log_flaw(msg, line, colno, footnote)
1329 local COLOR = luatypo.colortbl[5]
1330 if backpar or shortline or overfull or underfull then
1331 COLOR = luatypo.colortbl[16]
1332 if backpar then backpar = false end
1333 if shortline then shortline = false end
1334 if overfull then overfull = false end
1335 if underfull then underfull = false end
1336 end
1337 color_line (head, COLOR)
1338 widowflag = false
1339 elseif orphanflag and Orphans then
1340 pageflag = true
1341 local msg = "ORPHAN"
1342 log_flaw(msg, line, colno, footnote)
1343 local COLOR = luatypo.colortbl[6]
1344 if overfull or underfull then
1345 COLOR = luatypo.colortbl[16]
1346 end
1347 color_line (head, COLOR)
1348 elseif ftnsplit and FootnoteSplit then
1349 pageflag = true
1350 local msg = "FOOTNOTE SPLIT"
1351 log_flaw(msg, line, colno, footnote)
1352 local COLOR = luatypo.colortbl[14]
1353 if overfull or underfull then
1354 COLOR = luatypo.colortbl[16]
1355 end
1356 color_line (head, COLOR)
1357 elseif shortline then
1358 local COLOR = luatypo.colortbl[7]
1359 color_line (head, COLOR)
1360 shortline = false
1361 elseif overfull then
1362 local COLOR = luatypo.colortbl[8]
1363 color_line (head, COLOR)
1364 overfull = false
1365 elseif underfull then
1366 local COLOR = luatypo.colortbl[9]
1367 color_line (head, COLOR)
1368 underfull = false
1369 elseif backpar then
1370 local COLOR = luatypo.colortbl[13]
1371 color_line (head, COLOR)
1372 backpar = false
1373 end
1374 elseif head and head.id == HLIST and head.subtype == BOX then
1375 if head.width > 0 then
1376 if head.height == 0 then
1377 bpmn, mnflag = check_marginnote(head, line, colno, vpos, bpmn)
1378 if mnflag then pageflag = true end
1379 page_bottom, body_bottom = check_EOP(nextnode)
1380 else
1381 local hf = head.list
1382 if hf and hf.id == VLIST and hf.subtype == 0 then
1383 break
1384 else
1385 line = line + 1
1386 pageline = pageline + 1
1387 end
1388 end
1389 end
1390 vpos = vpos + head.height + head.depth
1391 page_bottom, body_bottom = check_EOP (nextnode)
1392 elseif head.id == HLIST and
1393 (head.subtype == EQN or head.subtype == ALIGN) and
1394 (head.height > 0 or head.depth > 0) then
1395 vpos = vpos + head.height + head.depth
1396 if footnote then
1397 ftnline = ftnline + 1
1398 line = ftnline
1399 else
1400 pageline = pageline + 1
1401 line = pageline
1402 end
1403 page_bottom, body_bottom = check_EOP (nextnode)
1404 local fl = true
1405 local wd = 0
1406 local hmax = 0
1407 if head.subtype == EQN then
1408 local f = head.list
1409 wd = rangedimensions(head,f)
1410 hmax = head.width + tex.hfuzz
1411 else
1412 wd = head.width
1413 hmax = tex.getdimen("linewidth") + tex.hfuzz
1414 end
1415 if wd > hmax and OverfullLines then
1416 if head.subtype == ALIGN then
1417 local first = head.list
1418 for n in traverse_id(HLIST, first) do
1419 local last = slide(n.list)
1420 if last.id == GLUE and last.subtype == USER then
1421 wd = wd - effective_glue(last,n)
1422 if wd <= hmax then fl = false end
1423 end
1424 end
1425 end
1426 if fl then
1427 pageflag = true
1428 local w = wd - hmax + tex.hfuzz
1429 local wpt = string.format("%.2fpt", w/65536)
1430 local msg = "OVERFULL equation " .. wpt
1431 log_flaw(msg, line, colno, footnote)
1432 local COLOR = luatypo.colortbl[8]
1433 color_line (head, COLOR)
1434 end
1435 end
1436 elseif head and head.id == RULE and head.subtype == 0 then
1437 vpos = vpos + head.height + head.depth
1438 local prev = head.prev
1439 if body_bottom or footnoterule_ahead (prev) then
1440 footnote = true
1441 ftnline = 0
1442 body_bottom = false
1443 orphanflag = false
1444 hyphcount = 0
1445 firstwd = ""
1446 lastwd = ""
1447 end
1448 elseif body_bottom and head.id == GLUE and head.subtype == 0 then
1449 if first_bot then
1450 if pageline > 1 and pageline < PAGEmin
1451 and vpos < vpos_min and ShortPages then
1452 pageshort = true
1453 pageflag = true
1454 local msg = "SHORT PAGE: only " .. pageline .. " lines"
1455 log_flaw(msg, line, colno, footnote)
1456 local COLOR = luatypo.colortbl[10]
1457 local n = head
1458 repeat
1459 n = n.prev
1460 until n.id == HLIST and n.subtype == LINE
1461 color_line (n, COLOR)
1462 end
1463 first_bot = false
1464 end
1465 elseif head.id == GLUE then
1466 vpos = vpos + effective_glue(head,top)
1467 elseif head.id == KERN and head.subtype == 1 then
1468 vpos = vpos + head.kern
1469 elseif head.id == VLIST then
1470 vpos = vpos + head.height + head.depth
1471 end
1472 head = nextnode
1473 end
1474 if pageflag then
1475 local plist = luatypo.pagelist
1476 local lastp = tonumber(string.match(plist, "%s(%d+),%s$"))
1477 if not lastp or pageno > lastp then
1478 luatypo.pagelist = luatypo.pagelist .. tostring(pageno) .. ", "
1479 end
1480 end
1481 return head, done
1482 end
1483 luatypo.check_page = function (head)
1484 local pageno = tex.getcount("c@page")
1485 local body = get_pagebody(head)
1486 local textwd, textht, checked, boxed
1487 local top, first, next
1488 local n2, n3, col, colno
1489 local vpos = 0
1490 local footnote = false
1491 local count = 0
1492 if body then
1493 top = body
1494 first = body.list
1495 textwd = tex.getdimen("textwidth")
1496 textht = tex.getdimen("textheight")
1497 end
1498 if ((first and first.id == HLIST and first.subtype == BOX) or
1499 (first and first.id == VLIST and first.subtype == 0)) and
1500 (first.width == textwd and first.height > 0 and not boxed) then
1501 top = body.list
1502 if first.id == VLIST then
1503 boxed = body
1504 end
1505 end
1506 while top do
1507 first = top.list
1508 next = top.next
1509 if top and top.id == VLIST and top.subtype == 0 and
1510 top.width > textwd/2 then
1511 local n, ok = check_vtop(top,colno,vpos)
1512 if ok then checked = true end
1513 if n then
1514 next = n
1515 end
1516 elseif (top and top.id == HLIST and top.subtype == BOX) and
1517 (first and first.id == VLIST and first.subtype == 0) and
1518 (first.height > 0 and first.width > 0) then
1519 colno = 0
1520 for col in traverse_id(VLIST, first) do
1521 colno = colno + 1
1522 local n, ok = check_vtop(col,colno,vpos)
1523 if ok then checked = true end
1524 end
1525 colno = nil
1526 top = top.next
1527 elseif (top and top.id == HLIST and top.subtype == BOX) and
1528 (first and first.id == HLIST and first.subtype == BOX) and
1529 (first.height > 0 and first.width > 0) then
1530 colno = 0
1531 for n in traverse_id(HLIST, first) do
1532 colno = colno + 1
1533 local col = n.list
1534 if col and col.list then
1535 local n, ok = check_vtop(col,colno,vpos)
1536 if ok then checked = true end
1537 end
1538 end
1539 colno = nil
1540 end
1541 if boxed and not next then
1542 next = boxed
1543 boxed = nil
1544 end
1545 top = next
1546 end
1547 if not checked then
1548 luatypo.failedlist = luatypo.failedlist .. tostring(pageno) .. ", "
1549 end
1550 return true
1551 end
1552 return luatypo.check_page
1553 \end{luacode}
1554 \AtBeginDocument{%
1555 \directlua{
1556 if not luatypo.None then
1557 luatexbase.add_to_callback
1558 ("pre_shipout_filter",luatypo.check_page,"check_page",1)
1559 end
1560 }%
1561 }
1562 \InputIfFileExists{lua-typo.cfg}%
1563 {\PackageInfo{lua-typo.sty}{"lua-typo.cfg" file loaded}}%
1564 {\PackageInfo{lua-typo.sty}{"lua-typo.cfg" file not found.
1565 \MessageBreak Providing default values.}%
1566 \definecolor{LTgrey}{gray}{0.6}%
1567 \definecolor{LTred}{rgb}{1,0.55,0}
1568 \definecolor{LTline}{rgb}{0.7,0,0.3}
1569 \luatypoSetColor1{red}% Paragraph last full line hyphenated
1570 \luatypoSetColor2{red}% Page last word hyphenated
1571 \luatypoSetColor3{red}% Hyphens on to many consecutive lines
1572 \luatypoSetColor4{red}% Short word at end of line
1573 \luatypoSetColor5{cyan}% Widow
1574 \luatypoSetColor6{cyan}% Orphan
1575 \luatypoSetColor7{cyan}% Paragraph ending on a short line
1576 \luatypoSetColor8{blue}% Overfull lines
1577 \luatypoSetColor9{blue}% Underfull lines
1578 \luatypoSetColor{10}{red}% Nearly empty page
1579 \luatypoSetColor{11}{LTred}% First word matches
1580 \luatypoSetColor{12}{LTred}% Last word matches
1581 \luatypoSetColor{13}{LTgrey}% Paragraph ending on a nearly full line
1582 \luatypoSetColor{14}{cyan}% Footnote split
1583 \luatypoSetColor{15}{red}% Too short first (final) word on the page
1584 \luatypoSetColor{16}{LTline}% Line color for multiple flaws
1585 \luatypoSetColor{17}{red}% Margin note ending too low
1586 \luatypoBackPI=1em\relax
1587 \luatypoBackFuzz=2pt\relax
1588 \ifdim\parindent=0pt \luatypoLLminWD=20pt\relax
1589 \else\luatypoLLminWD=2\parindent\relax\fi
1590 \luatypoStretchMax=200\relax
1591 \luatypoHyphMax=2\relax
1592 \luatypoPageMin=5\relax
1593 \luatypoMinFull=3\relax
1594 \luatypoMinPart=4\relax
1595 \luatypoMinLen=4\relax
1596 \luatypoMarginparTol=\baselineskip
1597 }%
1598 %%
1599 %%
1600 %% End of file `lua-typo.sty'.