Debian Codegraph @

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