Skip to content

Commit

Permalink
Make the cost offset configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
gucci-on-fleek committed Oct 22, 2022
1 parent 7d66935 commit 16e7aa4
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 63 deletions.
29 changes: 28 additions & 1 deletion docs/manual/lwc-manual.tex
Expand Up @@ -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}
Expand All @@ -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!
Expand Down
75 changes: 42 additions & 33 deletions source/lua-widow-control.lua
Expand Up @@ -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
Expand All @@ -137,6 +136,7 @@ lwc.colours = {
worrying about any format/engine differences.
]]
local contrib_head,
draft_offset,
emergencystretch,
hold_head,
info,
Expand Down Expand Up @@ -181,16 +181,19 @@ 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

-- 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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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


Expand Down
3 changes: 3 additions & 0 deletions source/lua-widow-control.opm
Expand Up @@ -14,6 +14,9 @@
\_newdimen\lwcemergencystretch
\lwcemergencystretch=3em

\_newdimen\lwcdraftoffset
\lwcdraftoffset=\parindent

\_newcount\lwcmaxcost
\lwcmaxcost=2147483647

Expand Down
4 changes: 4 additions & 0 deletions source/lua-widow-control.sty
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions source/lua-widow-control.tex
Expand Up @@ -24,6 +24,9 @@
\newdimen\lwcemergencystretch
\lwcemergencystretch=3em

\newdimen\lwcdraftoffset
\lwcdraftoffset=\parindent

\newcount\lwcmaxcost
\lwcmaxcost=2147483647

Expand Down
3 changes: 3 additions & 0 deletions source/t-lua-widow-control.mkxl
Expand Up @@ -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
Expand Down Expand Up @@ -96,6 +98,7 @@
\c!state=\v!start,
debug=\v!stop,
draft=\v!stop,
draftoffset=12pt,
]

\setuplwc[default]
Expand Down
28 changes: 14 additions & 14 deletions tests/context/draft.mkiv.tltext

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 14 additions & 14 deletions tests/context/draft.tltext

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion tests/context/presets.tltext

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions 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}
23 changes: 23 additions & 0 deletions tests/latex/draft-offset.tltext

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 16e7aa4

Please sign in to comment.