From 16e7aa4c96dfcc82913d7335e7bbd73e31037c95 Mon Sep 17 00:00:00 2001 From: Max Chernoff <49086429+gucci-on-fleek@users.noreply.github.com> Date: Fri, 21 Oct 2022 22:58:35 -0600 Subject: [PATCH] Make the cost offset configurable --- docs/manual/lwc-manual.tex | 29 ++++++++++++- source/lua-widow-control.lua | 75 ++++++++++++++++++--------------- source/lua-widow-control.opm | 3 ++ source/lua-widow-control.sty | 4 ++ source/lua-widow-control.tex | 3 ++ source/t-lua-widow-control.mkxl | 3 ++ tests/context/draft.mkiv.tltext | 28 ++++++------ tests/context/draft.tltext | 28 ++++++------ tests/context/presets.tltext | 3 +- tests/latex/draft-offset.lvtext | 24 +++++++++++ tests/latex/draft-offset.tltext | 23 ++++++++++ 11 files changed, 160 insertions(+), 63 deletions(-) create mode 100644 tests/latex/draft-offset.lvtext create mode 100644 tests/latex/draft-offset.tltext diff --git a/docs/manual/lwc-manual.tex b/docs/manual/lwc-manual.tex index 0e77510..8358cea 100644 --- a/docs/manual/lwc-manual.tex +++ b/docs/manual/lwc-manual.tex @@ -525,8 +525,13 @@ \subsection{Maximum Cost} \subsection{Draft Mode} -You can use the draft mode to visualize how \lwc/ processes pages. Any \color[failure]{remaining widows and orphans will be coloured red}, any \color[expanded]{expanded paragraphs will be coloured green}, and any \color[moved]{lines moved to the next page will be coloured blue}. In addition, the cost of each paragraph is shown in the right margin. +\setuplwc[draft=start] +\definecolor[lwc_expanded][s=0] +\definecolor[lwc_failure][s=0] +\definecolor[lwc_moved][s=0] +You can use the draft mode to visualize how \lwc/ processes pages. Any \color[failure]{remaining widows and orphans will be coloured red}, any \color[expanded]{expanded paragraphs will be coloured green}, and any \color[moved]{lines moved to the next page will be coloured blue}. In addition, the cost of each paragraph is shown in the right margin as it is here. +\setuplwc[draft=stop] \startTABLE[setups=commandtable] \NC \plainop/ \NC\inlineTEX{\lwcdraft 1} @@ -539,8 +544,30 @@ \subsection{Draft Mode} \NC\NR \stopTABLE +\setuplwc[draft=start] Advanced users may also customize the colours used by modifying the \inlineLUA{lwc.colours} table. The table keys are currently \type{expanded}, \type{failure}, and \type{moved}. The table values are \acronym{RGB} 3-tuples, where each element is a float between zero and one. +\subsection{Draft Offset} + +In draft mode, the paragraph costs are printed in the right margin, offset a certain distance from the right edge of the paragraph. By default, this offset is the same length as the paragraph indent; however, you can change this to any value that you want: + +\setuplwc[draft=stop] +\startTABLE[setups=commandtable] + \NC \plainop/ + \NC\inlineTEX{\lwcdraftoffset = $\meta{dimension}$} + \NC\NR + \NC \LaTeX{} + \NC\inlineTEX{\lwcsetup{draftoffset = $\meta{dimension}$}} + \NC\NR + \NC \ConTeXt{} + \NC\inlineTEX{\setuplwc[draftoffset = $\meta{dimension}$]} + \NC\NR +\stopTABLE + +\setuplwc[draft=start, draftoffset=-1em] +If you set the \q{draft offset} to a negative value, the paragraph costs will instead be printed to the left of the paragraph, as it is here. + +\setuplwc[draft=stop] \subsection{Debug Mode} \Lwc/ offers a \q{debug} mode that prints extra information in the log files. This may be helpful to understand how \lwc/ is processing paragraphs and pages, although the information is likely inscrutable unless you are the package's author. If you are reporting an issue with \lwc/ make sure to compile your document with debug mode enabled! diff --git a/source/lua-widow-control.lua b/source/lua-widow-control.lua index 0014023..0b905ff 100644 --- a/source/lua-widow-control.lua +++ b/source/lua-widow-control.lua @@ -119,7 +119,6 @@ local iftrue = token.create("iftrue") local INFINITY = 10000 local INSERT_CLASS_MULTIPLE = 1000 * 1000 local INSERT_FIRST_MULTIPLE = 1000 -local llap_offset = math.max(tex.dimen.parindent, tex.sp("12pt")) local min_col_width = tex.sp("250pt") local PAGE_MULTIPLE = 100 local SINGLE_LINE = 50 @@ -137,6 +136,7 @@ lwc.colours = { worrying about any format/engine differences. ]] local contrib_head, + draft_offset, emergencystretch, hold_head, info, @@ -181,6 +181,7 @@ if context then -- 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 @@ -188,9 +189,11 @@ elseif plain or latex or optex then -- 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 @@ -446,35 +449,6 @@ local function colour_list(head, colour) end ---- Generate an \\llap'ed box containing the provided string ---- ---- @param str string The string to typeset ---- @return node head The box node -local function llap_string(str) - local first = new_node("glue") - first.width = llap_offset - - local m = first - for letter in str:gmatch(".") do - local n = new_node("glyph") - n.font = SMALL_FONT - n.char = string.byte(letter) - - m.next = n - m = n - end - - local hss = new_node("glue") - hss.stretch = 1 - hss[stretch_order] = 1 - hss.shrink = 1 - hss[shrink_order] = 1 - m.next = hss - - return node.hpack(first, 0, "exactly") -end - - --- Typesets the cost of a paragraph beside it in draft mode --- --- @param paragraph node @@ -485,11 +459,11 @@ local function show_cost(paragraph, cost) return end - local last_hlist_end = last(next_of_type( + local last_hlist = next_of_type( last(paragraph), hlist_id, { subtype = line_subid, reverse = true } - ).list) + ) local cost_str if cost < math.maxinteger then @@ -498,7 +472,42 @@ local function show_cost(paragraph, cost) cost_str = "infinite" end - last_hlist_end.next = llap_string(cost_str) + 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 diff --git a/source/lua-widow-control.opm b/source/lua-widow-control.opm index 6d3ce3e..6d00a21 100644 --- a/source/lua-widow-control.opm +++ b/source/lua-widow-control.opm @@ -14,6 +14,9 @@ \_newdimen\lwcemergencystretch \lwcemergencystretch=3em +\_newdimen\lwcdraftoffset +\lwcdraftoffset=\parindent + \_newcount\lwcmaxcost \lwcmaxcost=2147483647 diff --git a/source/lua-widow-control.sty b/source/lua-widow-control.sty index f5db525..6d11907 100644 --- a/source/lua-widow-control.sty +++ b/source/lua-widow-control.sty @@ -76,6 +76,10 @@ emergencystretch .value_required:n = true, emergencystretch .initial:x = \dim_max:nn { 3em } { 30pt }, + draftoffset .dim_gset:N = \g__lwc_draftoffset_dim, + draftoffset .value_required:n = true, + draftoffset .initial:x = \dim_max:nn { \parindent } { 12pt }, + max-cost .int_gset:N = \g__lwc_maxcost_int, max-cost .value_required:n = true, max-cost .initial:x = \c_max_int, diff --git a/source/lua-widow-control.tex b/source/lua-widow-control.tex index 9c26719..e6e4daf 100644 --- a/source/lua-widow-control.tex +++ b/source/lua-widow-control.tex @@ -24,6 +24,9 @@ \newdimen\lwcemergencystretch \lwcemergencystretch=3em +\newdimen\lwcdraftoffset +\lwcdraftoffset=\parindent + \newcount\lwcmaxcost \lwcmaxcost=2147483647 diff --git a/source/t-lua-widow-control.mkxl b/source/t-lua-widow-control.mkxl index 5b9dd6f..8b3b657 100644 --- a/source/t-lua-widow-control.mkxl +++ b/source/t-lua-widow-control.mkxl @@ -18,10 +18,12 @@ % Set up the options \newdimen\lwc_emergency_stretch +\newdimen\lwc_draft_offset \newcount\lwc_max_cost \starttexdefinition lwc_set_parameters \lwc_emergency_stretch=\lwcparameter{emergencystretch} + \lwc_draft_offset=\lwcparameter{draftoffset} \doifelse{\lwcparameter{\c!state}}\v!start{ \lwc_enable @@ -96,6 +98,7 @@ \c!state=\v!start, debug=\v!stop, draft=\v!stop, + draftoffset=12pt, ] \setuplwc[default] diff --git a/tests/context/draft.mkiv.tltext b/tests/context/draft.mkiv.tltext index eb350e2..aa84236 100644 --- a/tests/context/draft.mkiv.tltext +++ b/tests/context/draft.mkiv.tltext @@ -143,17 +143,17 @@ 134.5 478.2 645.0 659.1 of that 134.5 478.2 657.0 671.1 are page 134.5 267.8 669.0 683.1 unless worse. -498.1 522.8 143.7 153.5 infinite infinite -498.1 514.0 311.9 321.8 6439 6439 -498.1 514.0 456.1 466.0 2690 2690 -498.1 522.8 480.1 490.0 infinite infinite -498.1 518.0 552.2 562.1 13132 13132 -498.1 514.0 672.4 682.2 6391 6391 +490.2 514.9 143.7 153.5 infinite infinite +490.2 506.0 311.9 321.8 6439 6439 +490.2 506.0 456.1 466.0 2690 2690 +490.2 514.9 480.1 490.0 infinite infinite +490.2 510.0 552.2 562.1 13132 13132 +490.2 506.0 672.4 682.2 6391 6391 154.4 478.2 128.2 144.5 This a 134.5 478.2 140.3 154.4 filler last 134.5 219.2 152.3 166.4 sentence here. -498.1 522.8 155.7 165.5 infinite infinite +490.2 514.9 155.7 165.5 infinite infinite 134.5 404.8 128.2 144.5 A TEXbook: 154.4 478.2 140.3 156.5 You algorithm @@ -201,14 +201,14 @@ 134.5 478.2 645.0 659.1 are page 134.5 267.8 657.0 671.1 unless worse. 154.4 478.2 669.0 685.2 This a -498.1 522.8 131.6 141.5 infinite infinite -498.1 514.0 299.9 309.7 6439 6439 -498.1 514.0 444.1 453.9 2690 2690 -498.1 522.8 468.1 478.0 infinite infinite -498.1 518.0 540.2 550.1 13132 13132 -498.1 514.0 660.4 670.2 6391 6391 +490.2 514.9 131.6 141.5 infinite infinite +490.2 506.0 299.9 309.7 6439 6439 +490.2 506.0 444.1 453.9 2690 2690 +490.2 514.9 468.1 478.0 infinite infinite +490.2 510.0 540.2 550.1 13132 13132 +490.2 506.0 660.4 670.2 6391 6391 134.5 478.2 128.2 142.4 filler last 134.5 219.2 140.3 154.4 sentence here. -498.1 522.8 143.7 153.5 infinite infinite +490.2 514.9 143.7 153.5 infinite infinite diff --git a/tests/context/draft.tltext b/tests/context/draft.tltext index 60120ed..5586c59 100644 --- a/tests/context/draft.tltext +++ b/tests/context/draft.tltext @@ -143,17 +143,17 @@ 134.5 478.4 648.1 658.1 of that 134.5 478.4 660.2 670.1 are page 134.5 267.9 672.2 682.1 unless worse. -498.1 522.8 145.9 152.8 infinite infinite -498.1 514.0 314.1 321.1 6387 6387 -498.1 514.0 458.3 465.3 2690 2690 -498.1 522.8 482.3 489.3 infinite infinite -498.1 518.0 554.4 561.4 13132 13132 -498.1 514.0 674.6 681.6 6351 6351 +490.2 514.8 145.9 152.8 infinite infinite +490.2 506.0 314.1 321.1 6387 6387 +490.2 506.0 458.3 465.3 2690 2690 +490.2 514.8 482.3 489.3 infinite infinite +490.2 510.0 554.4 561.4 13132 13132 +490.2 506.0 674.6 681.6 6351 6351 154.4 478.3 131.4 143.6 This a 134.5 478.4 143.5 153.4 filler last 134.5 219.3 155.5 165.4 sentence here. -498.1 522.8 157.9 164.9 infinite infinite +490.2 514.8 157.9 164.9 infinite infinite 134.5 404.8 131.4 143.6 A TEXbook: 154.4 478.3 143.5 155.6 You algorithm @@ -201,14 +201,14 @@ 134.5 478.4 648.1 658.1 are page 134.5 267.9 660.2 670.1 unless worse. 154.4 478.3 672.2 684.3 This a -498.1 522.8 133.9 140.8 infinite infinite -498.1 514.0 302.1 309.1 6387 6387 -498.1 514.0 446.3 453.3 2690 2690 -498.1 522.8 470.3 477.3 infinite infinite -498.1 518.0 542.4 549.4 13132 13132 -498.1 514.0 662.6 669.5 6351 6351 +490.2 514.8 133.9 140.8 infinite infinite +490.2 506.0 302.1 309.1 6387 6387 +490.2 506.0 446.3 453.3 2690 2690 +490.2 514.8 470.3 477.3 infinite infinite +490.2 510.0 542.4 549.4 13132 13132 +490.2 506.0 662.6 669.5 6351 6351 134.5 478.4 131.4 141.4 filler last 134.5 219.3 143.5 153.4 sentence here. -498.1 522.8 145.9 152.8 infinite infinite +490.2 514.8 145.9 152.8 infinite infinite diff --git a/tests/context/presets.tltext b/tests/context/presets.tltext index 769a551..d62538e 100644 --- a/tests/context/presets.tltext +++ b/tests/context/presets.tltext @@ -50,5 +50,6 @@ 260.0 284.7 069.9 076.9 infinite infinite 138.8 144.7 036.1 048.0 4 4 -035.4 272.7 065.9 077.8 Hello infinite +035.4 243.2 065.9 077.8 Hello Hello +260.0 284.7 069.9 076.9 infinite infinite diff --git a/tests/latex/draft-offset.lvtext b/tests/latex/draft-offset.lvtext new file mode 100644 index 0000000..89c4ca3 --- /dev/null +++ b/tests/latex/draft-offset.lvtext @@ -0,0 +1,24 @@ +\documentclass{article} + +\usepackage[draft]{lua-widow-control} + +\parindent=0pt +\parskip=\baselineskip + +\begin{document} + +One \hfill one. + +\lwcsetup{draftoffset=0pt} Two \hfill two. + +\lwcsetup{draftoffset=1in} Three \hfill three. + +\lwcsetup{draftoffset=-1sp} Four \hfill four. + +\lwcsetup{draftoffset=-1in} Five \hfill five. + +\lwcsetup{draftoffset=1em} Six \hfill six \hfill six. + +Seven \hfill seven \hfill seven. + +\end{document} diff --git a/tests/latex/draft-offset.tltext b/tests/latex/draft-offset.tltext new file mode 100644 index 0000000..515ceaa --- /dev/null +++ b/tests/latex/draft-offset.tltext @@ -0,0 +1,23 @@ +037.1 061.8 225.6 231.8 infinite infinite +133.8 151.5 123.5 137.7 One One +461.7 479.4 123.5 137.7 one. one. +133.4 151.7 147.4 161.6 Two Two +461.2 502.2 147.4 161.6 two. infinite +133.4 158.9 171.4 185.5 Three Three +454.5 479.4 171.4 185.5 three. three. +109.1 153.9 195.3 209.4 infinite Four +459.2 479.4 195.3 209.4 four. four. +133.8 152.4 219.2 233.3 Five Five +461.7 479.4 219.2 233.3 five. five. +492.4 517.1 129.9 136.1 infinite infinite +549.5 574.2 177.7 183.9 infinite infinite +133.8 147.3 243.1 257.2 Six Six +300.0 312.0 243.1 257.2 six six +464.7 479.4 243.1 257.2 six. six. +487.4 507.3 249.5 255.7 30307 30307 +133.8 158.7 267.0 281.1 Seven Seven +294.4 317.7 267.0 281.1 seven seven +453.4 479.4 267.0 281.1 seven. seven. +487.4 507.3 273.4 279.6 25929 25929 +303.1 308.1 691.4 705.5 1 1 +