summaryrefslogtreecommitdiff
path: root/macros/luatex/latex
diff options
context:
space:
mode:
Diffstat (limited to 'macros/luatex/latex')
-rw-r--r--macros/luatex/latex/linebreaker/README.md4
-rw-r--r--macros/luatex/latex/linebreaker/linebreaker-doc.pdfbin51966 -> 49887 bytes
-rw-r--r--macros/luatex/latex/linebreaker/linebreaker-doc.tex11
-rw-r--r--macros/luatex/latex/linebreaker/linebreaker.lua72
-rw-r--r--macros/luatex/latex/linebreaker/linebreaker.sty9
5 files changed, 88 insertions, 8 deletions
diff --git a/macros/luatex/latex/linebreaker/README.md b/macros/luatex/latex/linebreaker/README.md
index 26b4cd59e5..a418e231b0 100644
--- a/macros/luatex/latex/linebreaker/README.md
+++ b/macros/luatex/latex/linebreaker/README.md
@@ -1,4 +1,4 @@
-#The Linebreaker package, version v0.1a (2022-03-12)
+#The Linebreaker package, version v0.1b (2023-03-06)
This package tries to prevent overflow lines in paragraphs or boxes.
It changes the LuaTeX's `linebreak` callback, and it re-typesets the paragraph
@@ -15,6 +15,8 @@ with the lowest badness.
## License
+Author: Michal Hoftich <michal.h21@gmail.com>
+
Permission is granted to copy, distribute and/or modify this software
under the terms of the LaTeX Project Public License, version 1.3.
diff --git a/macros/luatex/latex/linebreaker/linebreaker-doc.pdf b/macros/luatex/latex/linebreaker/linebreaker-doc.pdf
index ac6659002f..b47fc0e3e3 100644
--- a/macros/luatex/latex/linebreaker/linebreaker-doc.pdf
+++ b/macros/luatex/latex/linebreaker/linebreaker-doc.pdf
Binary files differ
diff --git a/macros/luatex/latex/linebreaker/linebreaker-doc.tex b/macros/luatex/latex/linebreaker/linebreaker-doc.tex
index 8da047ceea..f94aa33ef0 100644
--- a/macros/luatex/latex/linebreaker/linebreaker-doc.tex
+++ b/macros/luatex/latex/linebreaker/linebreaker-doc.tex
@@ -140,6 +140,11 @@ Maximal allowed value of \verb|\emergencystretch|.
Maximal allowed value of \verb|tolerance|.
\end{function}
+\begin{function}{cubic}
+Use cubic method for the \verb|tolerance| calculation. By default, \verb|tolerance| is calculated
+using a linear method, with constant steps. With the cubic method, each step is larger than the previous.
+\end{function}
+
\begin{function}{debug}
Print debugging info to the terminal output.
\end{function}
@@ -186,6 +191,12 @@ under the terms of the LaTeX Project Public License, version 1.3.
\section{Changes}
\begin{description}
+ \item[v0.1b, 2023-03-06]\hfill
+ \begin{itemize}
+ \item Set maximal value of tolerance to 8189, bigger value doesn't have any effect.
+ \item Added \verb|cubic| method for calculating tolerance.
+ \item Explicitly set the \verb|tex.linebreak| parameters, to support paragraph direction and other paragraph variables.
+ \end{itemize}
\item[v0.1a, 2022-03-12]\hfill
\begin{itemize}
diff --git a/macros/luatex/latex/linebreaker/linebreaker.lua b/macros/luatex/latex/linebreaker/linebreaker.lua
index 6a2d20e074..99966ad302 100644
--- a/macros/luatex/latex/linebreaker/linebreaker.lua
+++ b/macros/luatex/latex/linebreaker/linebreaker.lua
@@ -1,3 +1,11 @@
+-- linebreaker.lua
+--
+-- (c) Michal Hoftich <michal.h21@gmail.com>
+--
+-- This program can be redistributed and/or modified under the terms
+-- of the LaTeX Project Public License Distributed from CTAN archives
+-- in directory macros/latex/base/lppl.txt.
+
local linebreaker = {}
local hlist_id = node.id "hlist"
@@ -22,7 +30,7 @@ end
linebreaker.debug = false
-- max allowed value of tolerance
-linebreaker.max_tolerance = 9999
+linebreaker.max_tolerance = 8189 -- maximal possible value of tolerance (thanks to Jan Šustek for pointing that out)
-- maximal allowed emergencystretch
linebreaker.max_emergencystretch = tex.sp("3em")
-- line breaking function is customizable
@@ -43,9 +51,38 @@ linebreaker.previous_points = linebreaker.vertical_point / linebreaker.boxsize
linebreaker.width_factor = 1.5
-- will be linebreaker active?
linebreaker.active = true
+
+-- use cubic method for tolerance calculation
+linebreaker.use_cubic = false
-- return array with default parameters
function linebreaker.parameters()
- return {}
+ return {
+ pardir = tex.pardir
+ ,pretolerance = tex.pretolerance
+ ,tracingparagraphs=tex.tracingparagraphs
+ ,tolerance=tex.tolerance
+ ,looseness=tex.looseness
+ ,hyphenpenalty=tex.hyphenpenalty
+ ,exhyphenpenalty=tex.exhyphenpenalty
+ ,pdfadjustspacing=tex.pdfadjustspacing
+ ,adjdemerits=tex.adjdemerits
+ ,pdfprotrudechars=tex.pdfprotrudechars
+ ,linepenalty=tex.linepenalty
+ ,lastlinefit=tex.lastlinefit
+ ,doublehyphendemerits=tex.doublehyphendemerits
+ ,finalhyphendemerits=tex.finalhyphendemerits
+ ,hangafter=tex.hangafter
+ ,interlinepenalty=tex.interlinepenalty
+ ,clubpenalty=tex.clubpenalty
+ ,widowpenalty=tex.widowpenalty
+ ,brokenpenalty=tex.brokenpenalty
+ ,emergencystretch=tex.emergencystretch
+ ,hangindent=tex.hangindent
+ ,hsize=tex.hsize
+ ,leftskip=tex.leftskip
+ ,rightskip=tex.rightskip
+ ,parshape=tex.parshape
+ }
end
-- diagnostic function for traversing nodes returned by linebreaking
@@ -144,9 +181,23 @@ local function glue_calc(n, sign,set)
end
--- calculate new tolerance
+local function cube_root(num)
+ return num ^(1/3)
+end
+
+
+-- calculate new tolerance using method suggested by Jan Šustek
+local function calc_tolerance_cubic(previous, step)
+ local previous = previous or tex.tolerance
+ local max_cycles = linebreaker.max_cycles
+ local max_tolerance = linebreaker.max_tolerance
+ local new = 100 * (cube_root(previous / 100) + (cube_root(max_tolerance/100) - cube_root(previous/100)) * (step/max_cycles))^3
+ -- local new = previous + (max_tolerance / max_cycles)-- + math.sqrt(previous * 4)
+ return (new < max_tolerance) and new or max_tolerance
+end
+
-- max_tolerance / max_cycles is added to the current tolerance value
-local function calc_tolerance(previous)
+local function calc_tolerance(previous, step)
local previous = previous or tex.tolerance
local max_cycles = linebreaker.max_cycles
local max_tolerance = linebreaker.max_tolerance
@@ -340,7 +391,9 @@ end
-- try to linebreak current paragraph with increasing tolerance and
-- emergencystretch
-function linebreaker.best_solution(par, parameters)
+function linebreaker.best_solution(par, parameters, step)
+ -- step is used in the tolerance calculation
+ local step = (step or 0) + 1
-- save current node list, because call to the linebreaker modifies it
-- and we wouldn't be able to run it multiple times
local head = node.copy_list(par)
@@ -373,9 +426,14 @@ function linebreaker.best_solution(par, parameters)
params.badness = badness
if badness > 0 then
-- calc new value of tolerance
- local tolerance = calc_tolerance(params.tolerance) -- or 10000
+ local tolerance
+ if linebreaker.use_cubic then
+ tolerance = calc_tolerance_cubic(params.tolerance, step) -- or 10000
+ else
+ tolerance = calc_tolerance(params.tolerance, step) -- or 10000
+ end
-- save tolerance to newparams so this value will be used in next run
- newparams.tolerance = tolerance
+ newparams.tolerance = math.floor(tolerance)
newparams.emergencystretch = (params.emergencystretch or 0) + linebreaker.max_emergencystretch / linebreaker.max_cycles
table.insert(parameters, newparams)
-- run linebreaker again
diff --git a/macros/luatex/latex/linebreaker/linebreaker.sty b/macros/luatex/latex/linebreaker/linebreaker.sty
index 2804f4bbe6..3442adad8c 100644
--- a/macros/luatex/latex/linebreaker/linebreaker.sty
+++ b/macros/luatex/latex/linebreaker/linebreaker.sty
@@ -1,3 +1,11 @@
+% linebreaker.sty
+%
+% (c) Michal Hoftich <michal.h21@gmail.com>
+%
+%% This program can be redistributed and/or modified under the terms
+%% of the LaTeX Project Public License Distributed from CTAN archives
+%% in directory macros/latex/base/lppl.txt.
+
\ProvidesPackage{linebreaker}
\RequirePackage{luatexbase}
@@ -16,6 +24,7 @@ luatexbase.add_to_callback("hpack_quality", linebreaker.hpack_quality, "Our over
maxtolerance .code:n = \directlua{linebreaker.max_tolerance = tonumber("#1")},
maxemergencystretch .code:n = \directlua{linebreaker.max_emergencystretch = tex.sp("#1")},
maxcycles .code:n = \directlua{linebreaker.max_cycles = tonumber("#1")},
+ cubic .code:n = \directlua{linebreaker.use_cubic = true}
}
\NewDocumentCommand{\linebreakersetup}{m}{
\keys_set:nn {linebreaker} {#1}