-
Notifications
You must be signed in to change notification settings - Fork 1
/
lua-widow-control.lua
1506 lines (1279 loc) · 44.2 KB
/
lua-widow-control.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
lua-widow-control
https://github.com/gucci-on-fleek/lua-widow-control
SPDX-License-Identifier: MPL-2.0+
SPDX-FileCopyrightText: 2022 Max Chernoff
]]
--- Tell the linter about node attributes
--- @class node
--- @field depth integer
--- @field height integer
--- @field id integer
--- @field list node
--- @field next node
--- @field penalty integer
--- @field prev node
--- @field subtype integer
-- Initial setup
lwc = lwc or {}
lwc.name = "lua-widow-control"
-- Locals for `debug_print`
local debug_lib = debug
local string_rep = string.rep
local write_nl = texio.write_nl
local write_log
if status.luatex_engine == "luametatex" then
write_log = "logfile"
else
write_log = "log"
end
--- Prints debugging messages to the log, only if `debug` is set to `true`.
---
--- @param title string The "title" to use
--- @param text string? The "content" to print
--- @return nil
local function debug(title, text)
if not lwc.debug then return end
-- The number of spaces we need
local filler = 15 - #title
if text then
write_nl(write_log, "LWC (" .. title .. string_rep(" ", filler) .. "): " .. text)
else
write_nl(write_log, "LWC: " .. string_rep(" ", 18) .. title)
end
end
--[[
\lwc/ is intended to be format-agonistic. It only runs on Lua\TeX{},
but there are still some slight differences between formats. Here, we
detect the format name then set some flags for later processing.
]]
local format = tex.formatname
local context, latex, plain, optex, lmtx
if format:find("cont") then -- cont-en, cont-fr, cont-nl, ...
context = true
if status.luatex_engine == "luametatex" then
lmtx = true
end
elseif format:find("latex") then -- lualatex, lualatex-dev, ...
latex = true
elseif format == "luatex" or format == "luahbtex" then -- Plain
plain = true
elseif format:find("optex") then -- OpTeX
optex = _G.optex
end
--[[
Save some local copies of the node library to reduce table lookups.
This is probably a useless micro-optimization, but it is done in all of the
ConTeXt and expl3 Lua code, so I should probably do it here too.
]]
-- Node ID's
-- (We need to hardcode the subid's sadly)
local id_from_name = node.id
local baselineskip_subid = 2
local glue_id = id_from_name("glue")
local glyph_id = id_from_name("glyph")
local hlist_id = id_from_name("hlist")
local insert_id = id_from_name("insert") or id_from_name("ins")
local line_subid = 1
local linebreakpenalty_subid = 1
local par_id = id_from_name("par") or id_from_name("local_par")
local penalty_id = id_from_name("penalty")
-- Local versions of globals
local abs = math.abs
local copy = node.copy
local copy_list = node.copy_list or node.copylist
local find_attribute = node.find_attribute or node.findattribute
local free = node.free
local free_list = node.flush_list or node.flushlist
local get_attribute = node.get_attribute or node.getattribute
local insert_token = token.put_next or token.putnext
local last = node.slide
local linebreak = tex.linebreak
local new_node = node.new
local remove = node.remove
local set_attribute = node.set_attribute or node.setattribute
local string_char = string.char
local tex_box = tex.box
local tex_count = tex.count
local tex_dimen = tex.dimen
local tex_lists = tex.lists
local traverse = node.traverse
local traverse_id = node.traverse_id or node.traverseid
local vpack = node.vpack
-- Misc. Constants
local iffalse = token.create("iffalse")
local iftrue = token.create("iftrue")
local INFINITY = 10000
local INSERT_CLASS_MULTIPLE = 1000 * 1000
local INSERT_FIRST_MULTIPLE = 1000
local min_col_width = tex.sp("250pt")
local PAGE_MULTIPLE = 100
local SINGLE_LINE = 50
lwc.colours = {
expanded = {0.00, 0.70, 0.25},
failure = {0.90, 0.00, 0.25},
moved = {0.25, 0.25, 1.00},
}
--[[ Package/module initialization.
Here, we replace any format/engine-specific variables/functions with some
generic equivalents. This way, we can write the rest of the module without
worrying about any format/engine differences.
]]
local contrib_head,
draft_offset,
emergencystretch,
hold_head,
info,
insert_attribute,
max_cost,
pagenum,
paragraph_attribute,
shrink_order,
stretch_order,
warning
if lmtx then
-- LMTX has removed underscores from most of the Lua parts
debug("LMTX")
contrib_head = "contributehead"
shrink_order = "shrinkorder"
stretch_order = "stretchorder"
hold_head = "holdhead"
else
contrib_head = "contrib_head"
shrink_order = "shrink_order"
stretch_order = "stretch_order"
hold_head = "hold_head"
end
if context then
debug("ConTeXt")
warning = logs.reporter(lwc.name, "warning")
local _info = logs.reporter(lwc.name, "info")
--[[ We don't want the info messages on the terminal, but ConTeXt doesn't
provide any logfile-only reporters, so we need this hack.
]]
info = function (text)
logs.pushtarget("logfile")
_info(text)
logs.poptarget()
end
paragraph_attribute = attributes.public(lwc.name .. "_paragraph")
insert_attribute = attributes.public(lwc.name .. "_insert")
pagenum = function() return tex_count["realpageno"] end
-- Dimen names
emergencystretch = "lwc_emergency_stretch"
draft_offset = "lwc_draft_offset"
max_cost = "lwc_max_cost"
elseif plain or latex or optex then
pagenum = function() return tex_count[0] end
-- Dimen names
if tex.isdimen("g__lwc_emergencystretch_dim") then
emergencystretch = "g__lwc_emergencystretch_dim"
draft_offset = "g__lwc_draftoffset_dim"
max_cost = "g__lwc_maxcost_int"
else
emergencystretch = "lwcemergencystretch"
draft_offset = "lwcdraftoffset"
max_cost = "lwcmaxcost"
end
if plain or latex then
debug("Plain/LaTeX")
luatexbase.provides_module {
name = lwc.name,
date = "2022/08/23", --%%slashdate
version = "2.2.2", --%%version
description = [[
This module provides a LuaTeX-based solution to prevent
widows and orphans from appearing in a document. It does
so by increasing or decreasing the lengths of previous
paragraphs.]],
}
warning = function(str) luatexbase.module_warning(lwc.name, str) end
info = function(str) luatexbase.module_info(lwc.name, str) end
paragraph_attribute = luatexbase.new_attribute(lwc.name .. "_paragraph")
insert_attribute = luatexbase.new_attribute(lwc.name .. "_insert")
elseif optex then
debug("OpTeX")
warning = function(str) write_nl(lwc.name .. " Warning: " .. str) end
info = function(str) write_nl("log", lwc.name .. " Info: " .. str) end
paragraph_attribute = alloc.new_attribute(lwc.name .. "_paragraph")
insert_attribute = alloc.new_attribute(lwc.name .. "_insert")
end
else -- This shouldn't ever happen
error [[Unsupported format.
Please use LaTeX, Plain TeX, ConTeXt or OpTeX.]]
end
--[[ Select the fonts
We want to use cmr7 for the draft mode cost displays, and the easiest
way to do so is to just hardcode the font id's. This relies on some
implementation details; however, it is very unlikely to ever be an issue
]]
local SMALL_FONT
if plain then
SMALL_FONT = 4
elseif latex then
SMALL_FONT = 7
elseif optex then
SMALL_FONT = 7
elseif context then
SMALL_FONT = fonts.definers.define({
name = "LMRoman7-Regular",
size = tex.sp("7pt"),
})
end
--[[ Table to hold the alternate paragraph versions.
This is global(ish) mutable state, which isn't ideal, but any other way of
passing this data around would be even worse.
]]
local paragraphs = {}
local inserts = {}
--[[ Function definitions
]]
--- Gets the current paragraph and page locations
--- @return string
local function get_location()
return "At " .. pagenum() .. "/" .. #paragraphs
end
--- Prints the starting glyphs and glue of an `hlist`
---
--- @param head node
--- @return nil
local function get_chars(head)
if not lwc.debug then return end
local chars = ""
for n in traverse(head) do
if n.id == glyph_id then
if n.char < 127 then -- Only ASCII
chars = chars .. string_char(n.char)
else
chars = chars .. "#" -- Replacement for an unknown glyph
end
elseif n.id == glue_id then
chars = chars .. " " -- Any glue goes to a space
end
if #chars > 25 then
break
end
end
debug(get_location(), chars)
end
--- The "cost function" to use. Users can redefine this if they wish.
---
--- @param demerits number The demerits of the broken paragraph
--- @param lines number The number of lines in the broken paragraph
--- @return number The cost of the broken paragraph
function lwc.paragraph_cost(demerits, lines)
return demerits / math.sqrt(lines)
end
--- Checks if the ConTeXt "grid snapping" is active
---
--- @return boolean
local function grid_mode_enabled()
-- Compare the token "mode" to see if `\\ifgridsnapping` is `\\iftrue`
return token.create("ifgridsnapping").mode == iftrue.mode
end
--- Gets the next node of a specified type/subtype in a node list
---
--- @param head node The head of the node list
--- @param id number The node type
--- @param args table?
--- subtype: number = The node subtype
--- reverse: bool = Whether we should iterate backwards
--- @return node?
local function next_of_type(head, id, args)
args = args or {}
if lmtx or not args.reverse then
for n, subtype in traverse_id(id, head, args.reverse) do
if (subtype == args.subtype) or (args.subtype == nil) then
return n
end
end
else
--[[ Only LMTX has the built-in backwards traverser, so we need to do it
ourselves here.
]]
while head do
if head.id == id and
(head.subtype == args.subtype or args.subtype == nil)
then
return head
end
head = head.prev
end
end
-- Needed for the special `tex.lists` nodes
if head and head.id == id and
(head.subtype == args.subtype or args.subtype == nil)
then
return head
end
end
--- Breaks a paragraph one line longer than natural
---
--- @param head node The unbroken paragraph
--- @return node long_node The broken paragraph
--- @return table long_info An info table about the broken paragraph
local function long_paragraph(head)
-- We can't modify the original paragraph
head = copy_list(head)
if lmtx then
tex.preparelinebreak(head)
end
-- Prevent ultra-short last lines (\TeX{}Book p. 104), except with narrow columns
-- Equivalent to \\parfillskip=0pt plus 0.8\\hsize
local parfillskip = last(head)
if tex.hsize > min_col_width then
parfillskip[stretch_order] = 0
parfillskip.stretch = 0.8 * tex.hsize -- Last line must be at least 20% long
end
-- Break the paragraph 1 line longer than natural
return linebreak(head, {
looseness = 1,
emergencystretch = tex_dimen[emergencystretch],
})
end
--- Breaks a paragraph at its natural length
---
--- @param head node The unbroken paragraph
--- @return table natural_info An info table about the broken paragraph
local function natural_paragraph(head)
-- We can't modify the original paragraph
head = copy_list(head)
if lmtx then
tex.preparelinebreak(head)
end
-- Break the paragraph naturally to get \\prevgraf
local natural_node, natural_info = linebreak(head)
free_list(natural_node)
return natural_info
end
lwc.draft_mode = false
--- Changes the text colour in a node list if draft mode is active
---
--- @param head node The first node to colour
--- @param colour string The name of a colour in `lwc.colours`
--- @return node head The coloured node
local function colour_list(head, colour)
if not lwc.draft_mode then
return head
end
local pdf_colour = string.format(
"%.2f %.2f %.2f rg",
table.unpack(lwc.colours[colour])
)
if optex and optex.set_node_color then
for n in node.traverse(head) do
optex.set_node_color(n, pdf_colour)
end
return head
end
if context then
nodes.tracers.colors.setlist(head, "lwc_" .. colour)
return head
end
-- Adapted from https://tex.stackexchange.com/a/372437
-- \\pdfextension colorstack is ignored in LMTX
local start_colour = new_node("whatsit", "pdf_colorstack")
start_colour.stack = 0
start_colour.command = 1
start_colour.data = pdf_colour
local end_colour = new_node("whatsit", "pdf_colorstack")
end_colour.stack = 0
end_colour.command = 2
start_colour.next = head
last(head).next = end_colour
return start_colour
end
--- Typesets the cost of a paragraph beside it in draft mode
---
--- @param paragraph node
--- @param cost number
--- @return nil
local function show_cost(paragraph, cost)
if not lwc.draft_mode then
return
end
local last_hlist = next_of_type(
last(paragraph),
hlist_id,
{ subtype = line_subid, reverse = true }
)
local cost_str
if cost < math.maxinteger then
cost_str = string.format("%.0f", cost)
else
cost_str = "infinite"
end
local m, first
for letter in cost_str:gmatch(".") do
local n = new_node("glyph")
n.font = SMALL_FONT
n.char = string.byte(letter)
if not first then
first = n
else
m.next = n
end
m = n
end
local hss = new_node("glue")
hss.stretch = 1
hss[stretch_order] = 1
hss.shrink = 1
hss[shrink_order] = 1
local hbox
local offset = new_node("glue")
local offset_width = tex_dimen[draft_offset]
if offset_width >= 0 then
offset.width = offset_width
m.next = hss
hbox = node.hpack(first, 0, "exactly")
else
offset.width = offset_width - last_hlist.width
hss.next = first
hbox = node.hpack(hss, 0, "exactly")
end
last(last_hlist.list).next = offset
offset.next = hbox
end
--- Saves each paragraph, but lengthened by 1 line
---
--- Called by the `pre_linebreak_filter` callback
---
--- @param head node
--- @return node
function lwc.save_paragraphs(head)
if (head.id ~= par_id and context) or -- Ensure that we were actually given a par
status.output_active or -- Don't run during the output routine
tex.nest.ptr > 1 -- Don't run inside boxes
then
return head
end
-- Prevent the "underfull hbox" warnings when we store a potential paragraph
local renable_box_warnings
if (context or optex) or
#luatexbase.callback_descriptions("hpack_quality") == 0
then -- See #18 and michal-h21/linebreaker#3
renable_box_warnings = true
lwc.callbacks.disable_box_warnings.enable()
end
long_node, long_info = long_paragraph(head)
natural_info = natural_paragraph(head)
if renable_box_warnings then
lwc.callbacks.disable_box_warnings.disable()
end
if not grid_mode_enabled() then
-- Offset the \\prevdepth differences between natural and long
local prevdepth = new_node("glue")
prevdepth.width = natural_info.prevdepth - long_info.prevdepth
last(long_node).next = prevdepth
end
local long_cost = lwc.paragraph_cost(long_info.demerits, long_info.prevgraf)
if long_info.prevgraf ~= natural_info.prevgraf + 1 or
long_cost < 10 -- Any paragraph that is "free" to expand is suspicious
then
-- This paragraph is infinitely bad
long_cost = math.maxinteger
end
local saved_node = next_of_type(long_node, hlist_id, { subtype = line_subid })
show_cost(saved_node, long_cost)
for n in traverse_id(hlist_id, saved_node) do
n.list = colour_list(n.list, "expanded")
end
table.insert(paragraphs, {
cost = long_cost,
node = copy_list(saved_node)
})
free_list(long_node)
-- Print some debugging information
get_chars(head)
debug(get_location(), "nat lines " .. natural_info.prevgraf)
debug(
get_location(),
"nat cost " ..
lwc.paragraph_cost(natural_info.demerits, natural_info.prevgraf)
)
debug(get_location(), "long lines " .. long_info.prevgraf)
debug(
get_location(),
"long cost " ..
lwc.paragraph_cost(long_info.demerits, long_info.prevgraf)
)
-- \ConTeXt{} crashes if we return `true`
return head
end
--- Tags the beginning and the end of each paragraph as it is added to the page.
---
--- We add an attribute to the first and last node of each paragraph. The ID is
--- some arbitrary number for \lwc/, and the value corresponds to the
--- paragraphs index, which is negated for the end of the paragraph.
---
--- @param head node
--- @return nil
local function mark_paragraphs(head)
-- Tag the paragraphs
if not status.output_active then -- Don't run during the output routine
-- Get the start and end of the paragraph
local top = next_of_type(head, hlist_id, { subtype = line_subid })
local bottom = last(head)
while bottom.id == insert_id do
bottom = bottom.prev
end
if top ~= bottom then
set_attribute(
top,
paragraph_attribute,
#paragraphs + (PAGE_MULTIPLE * pagenum())
)
set_attribute(
bottom,
paragraph_attribute,
-1 * (#paragraphs + (PAGE_MULTIPLE * pagenum()))
)
else
-- We need a special tag for a 1-line paragraph since the node can only
-- have a single attribute value
set_attribute(
top,
paragraph_attribute,
#paragraphs + (PAGE_MULTIPLE * pagenum()) + SINGLE_LINE
)
end
if #paragraphs > 0 then
show_cost(head, paragraphs[#paragraphs].cost)
end
end
end
--- Tags the each line with the indices of any corresponding inserts.
---
--- We need to tag the first element of the hlist before the any insert nodes
--- since the insert nodes are removed before `pre_output_filter` gets called.
---
--- @param head node
--- @return nil
local function mark_inserts(head)
local insert_indices = {}
for insert in traverse_id(insert_id, head) do
-- Save the found insert nodes for later
inserts[#inserts+1] = copy(insert)
-- Tag the insert's content so that we can find it later
set_attribute(insert.list, insert_attribute, #inserts)
for n in traverse(insert.list.next) do
set_attribute(n, insert_attribute, -1 * #inserts)
end
--[[ Each hlist/line can have multiple inserts, but so we can't just tag
the hlist as we go. Instead, we need save up all of their indices,
then tag the hlist with the first and last indices.
]]
insert_indices[#insert_indices+1] = #inserts
if not insert.next or
insert.next.id ~= insert_id
then
local hlist_before = next_of_type(insert, hlist_id, { reverse = true} )
local insert_class
if lmtx then
insert_class = insert.index
else
insert_class = insert.subtype
end
--[[ We tag the first element of the hlist/line with an integer
that holds the insert class and the first and last indices
of the inserts contained in the line. This won't work if
the line has multiple classes of inserts, but I don't think
that happens in real-world documents.
]]
set_attribute(
hlist_before.list,
insert_attribute,
insert_class * INSERT_CLASS_MULTIPLE +
insert_indices[1] * INSERT_FIRST_MULTIPLE +
insert_indices[#insert_indices]
)
-- Clear the indices to prepare for the next line
insert_indices = {}
end
end
end
--- Saves the inserts and tags a typeset paragraph. Called by the
--- `post_linebreak_filter` callback.
---
--- @param head node
--- @return node
function lwc.mark_paragraphs(head)
mark_paragraphs(head)
mark_inserts(head)
return head
end
--- Checks to see if a penalty matches the widow/orphan/broken penalties
---
--- @param penalty number
--- @return boolean
function is_matching_penalty(penalty)
local widowpenalty = tex.widowpenalty
local clubpenalty = tex.clubpenalty
local displaywidowpenalty = tex.displaywidowpenalty
local brokenpenalty = tex.brokenpenalty
penalty = penalty - tex.interlinepenalty
-- https://tug.org/TUGboat/tb39-3/tb123mitt-widows-code.pdf#subsection.0.2.1
return penalty ~= 0 and
penalty < INFINITY and (
penalty == widowpenalty or
penalty == displaywidowpenalty or
penalty == clubpenalty or
penalty == clubpenalty + widowpenalty or
penalty == clubpenalty + displaywidowpenalty or
penalty == brokenpenalty or
penalty == brokenpenalty + widowpenalty or
penalty == brokenpenalty + displaywidowpenalty or
penalty == brokenpenalty + clubpenalty or
penalty == brokenpenalty + clubpenalty + widowpenalty or
penalty == brokenpenalty + clubpenalty + displaywidowpenalty
)
end
--- Reset any state saved between pages
---
--- @return nil
local function reset_state()
for _, paragraph in ipairs(paragraphs) do
free_list(paragraph.node)
end
paragraphs = {}
for _, insert in ipairs(inserts) do
free(insert)
end
inserts = {}
end
--- When we are unable to remove a widow/orphan, print a warning
---
--- @return nil
local function remove_widows_fail()
warning("Widow/Orphan/broken hyphen NOT removed on page " .. pagenum())
local last_line = next_of_type(
last(tex_lists.page_head),
hlist_id,
{ subtype = line_subid, reverse = true }
)
if last_line then
last_line.list = colour_list(last_line.list, "failure")
end
local next_first_line = next_of_type(
tex_lists[contrib_head],
hlist_id,
{ subtype = line_subid }
)
if next_first_line then
next_first_line.list = colour_list(next_first_line.list, "failure")
end
reset_state()
end
--- Finds the first and last paragraphs present on a page
---
--- @param head node The node representing the start of the page
--- @return number first_index The index of the first paragraph on the page in
--- the `paragraphs` table
--- @return number last_index The index of the last paragraph on the page in the
--- `paragraphs` table
local function first_last_paragraphs(head)
local first_index, last_index
-- Find the last paragraph on the page, starting at the end, heading in reverse
local n = last(head)
while n do
local value = get_attribute(n, paragraph_attribute)
if value then
last_index = value % PAGE_MULTIPLE
break
end
n = n.prev
end
-- Find the first paragraph on the page, from the top
local first_val, first_head = find_attribute(head, paragraph_attribute)
while abs(first_val) // PAGE_MULTIPLE == pagenum() - 1 do
--[[ If the first complete paragraph on the page was initially broken on the
previous page, then we can't expand it here so we need to skip it.
]]
first_val, first_head = find_attribute(
first_head.next,
paragraph_attribute
)
end
first_index = first_val % PAGE_MULTIPLE
if first_index >= SINGLE_LINE then
first_index = first_index - SINGLE_LINE
end
debug("first/last", first_index .. "/" .. last_index)
return first_index, last_index
end
--- Selects the "best" paragraph on the page to expand
---
--- @param head node The node representing the start of the page
--- @return number? best_index The index of the paragraph to expand in the
--- `paragraphs` table
local function best_paragraph(head)
local first_paragraph_index, last_paragraph_index = first_last_paragraphs(head)
-- Find the paragraph on the page with the least cost.
local best_index = 1
local best_cost = paragraphs[best_index].cost
-- We find the current "best" replacement
for index, paragraph in pairs(paragraphs) do
if paragraph.cost < best_cost and
index < last_paragraph_index and
index >= first_paragraph_index
then
best_index, best_cost = index, paragraph.cost
end
end
debug(
"selected para",
pagenum() .. "/" .. best_index .. " (" .. best_cost .. ")"
)
if best_cost > tex_count[max_cost] or
best_index == last_paragraph_index or
best_cost == math.maxinteger
then
return nil
else
return best_index
end
end
--- Gets any inserts present in the moved line
---
--- @param last_line node The moved last line
--- @return table<node> inserts A list of the present inserts
local function get_inserts(last_line)
local selected_inserts = {}
local n = last_line.list
while n do -- Iterate through the last line
local line_value
line_value, n = find_attribute(n, insert_attribute)
if not n then
break
end
-- Demux the insert values
local class = line_value // INSERT_CLASS_MULTIPLE
local first_index = (line_value % INSERT_CLASS_MULTIPLE) // INSERT_FIRST_MULTIPLE
local last_index = line_value % INSERT_FIRST_MULTIPLE
-- Get the output box containing the insert boxes
local insert_box
if lmtx then
insert_box = tex.getinsertcontent(class)
else
insert_box = tex_box[class]
end
-- Get any portions of the insert held over until the next page
local split_insert = next_of_type(
tex_lists[hold_head],
insert_id,
{ subtype = class }
)
for i, insert in ipairs { insert_box, split_insert } do
local m = insert and insert.list
while m do -- Iterate through the insert box
local box_value
box_value, m = find_attribute(m, insert_attribute)
if not m then
break
end
if abs(box_value) >= first_index and
abs(box_value) <= last_index
then
-- Remove the respective contents from the insert box
insert.list = remove(insert.list, m)
if box_value > 0 and i == 1 then
table.insert(selected_inserts, copy(inserts[box_value]))
end
m = free(m)
else
m = m.next
end
end
end
if not insert_box.list then
tex_box[class] = nil
end
if split_insert and not split_insert.list then
remove(tex_lists[hold_head], split_insert)
end
n = n.next
end
if #selected_inserts ~= 0 then
info("Moving footnotes on page " .. pagenum())
end
return selected_inserts
end
lwc.nobreak_behaviour = "keep"
--- Moves the last line of the page onto the following page.
---
--- This is the most complicated function of the module since it needs to
--- look back to see if there is a heading preceding the last line, then it does
--- some low-level node shuffling.
---
--- @param head node The node representing the start of the page
--- @return boolean success
local function move_last_line(head)
-- Start of final paragraph
debug("remove_widows", "moving last line")
-- Here we check to see if the widow/orphan was preceded by a large penalty
local big_penalty_found, last_line, hlist_head
local n = last(head).prev
while n do
if n.id == glue_id then
-- Ignore any glue nodes
elseif n.id == penalty_id and n.penalty >= INFINITY then
-- Infinite break penalty
big_penalty_found = true
elseif big_penalty_found and n.id == hlist_id then
-- Line before the penalty
if lwc.nobreak_behaviour == "keep" then
hlist_head = n
big_penalty_found = false
elseif lwc.nobreak_behaviour == "split" then
n = last(head)
break
elseif lwc.nobreak_behaviour == "warn" then
debug("last line", "heading found")
return false
end
else
-- Not found
if hlist_head then
n = hlist_head
else
n = last(head)
end
break
end
n = n.prev
end