Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions lua-visual-debug-keys.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ local inner_keys = {
},
kern = {
show = {scanner = scan_bool},
negative_color = {scanner = scan_string},
negativecolor = {scanner = scan_string},
color = {scanner = scan_string},
width = {scanner = scan_float}
},
penalty = {
show = {scanner = scan_bool},
colorfunc = {scanner = scan_string},
},
glyph = {
show = {scanner = scan_bool},
Expand Down Expand Up @@ -69,14 +70,27 @@ local function onlyglyphs()
params.glyph.show = true
end

local function set_penalty()
local vals = process_keys(inner_keys.penalty,messages)
params.penalty.show = vals.show ~= nil and vals.show or params.penalty.show
if vals.colorfunc then
local func, err = load("return " .. vals.colorfunc)
if func then
params.penalty.colorfunc = func()
else
texio.write_nl('log', "lua-visual-debug: error in colorfunc: " .. err)
end
end
end

local outer_keys = {
hlist = {scanner = function() return true end, func = set_params},
vlist = {scanner = function() return true end, func = set_params},
rule = {scanner = function() return true end, func = set_params},
disc = {scanner = function() return true end, func = set_params},
glue = {scanner = function() return true end, func = set_params},
kern = {scanner = function() return true end, func = set_params},
penalty = {scanner = function() return true end, func = set_params},
penalty = {scanner = function() return true end, func = set_penalty},
glyph = {scanner = function() return true end, func = set_params},
opacity = {scanner = scan_string},
onlyglyphs = {default = true, func = onlyglyphs}
Expand Down
13 changes: 6 additions & 7 deletions lua-visual-debug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ local params = {
rule = {show = true, color = "1 0 0 RG", width = 0.4},
disc = {show = true, color = "0 0 1 RG", width = 0.3},
glue = {show = true},
kern = {show = true, negative_color = "1 0 0 rg", color = "1 1 0 rg", width = 1},
penalty = {show = true},
kern = {show = true, negativecolor = "1 0 0 rg", color = "1 1 0 rg", width = 1},
penalty = {show = true, colorfunc = function(p) local color = "1 g" if p < 10000 then
color = fmt("%d g", 1 - floor(p / 10000)) end return color end},
glyph = {show = false, color = "1 0 0 RG", width = 0.1, baseline = true},
opacity = ""
}
Expand Down Expand Up @@ -190,7 +191,7 @@ local function show_page_elements(parent)

elseif head.id == KERN and params.kern.show then
local rectangle = node.new("whatsit","pdf_literal")
local color = head.kern < 0 and params.kern.negative_color
local color = head.kern < 0 and params.kern.negativecolor
or params.kern.color
local k = math_round(head.kern / number_sp_in_a_pdf_point,2)
if parent.id == HLIST then
Expand All @@ -204,11 +205,9 @@ local function show_page_elements(parent)


elseif head.id == PENALTY and params.penalty.show then
local color = "1 g"
-- maybe add a default vlaue in case the function returns nil?
local color = params.penalty.colorfunc(head.penalty)
local rectangle = node.new("whatsit","pdf_literal")
if head.penalty < 10000 then
color = fmt("%d g", 1 - floor(head.penalty / 10000))
end
rectangle.data = fmt("q %s 0 w 0 0 1 1 re B Q",color)
parent.list = insert_before(parent.list,head,rectangle)

Expand Down
14 changes: 14 additions & 0 deletions lvdebug-doc.tex
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ \section{Configuration}
& \texttt{width} & \texttt{1} & Line width in bp units \\
\midrule
\texttt{penalty} & \texttt{show} & \texttt{true} & Whether to mark penalties \\
& \texttt{colorfunc} & (see below) & Lua function that accepts the penalty value and returns a PDF color string \\
\midrule
\texttt{glyph} & \texttt{show} & \texttt{false} & Whether to mark glyphs \\
& \texttt{color} & \texttt{1 0 0 RG} & PDF stroking color operator \\
Expand All @@ -120,6 +121,19 @@ \section{Configuration}
\item The \texttt{opacity} key applies to all node types. For fine-tuned opacity control per node type, the \texttt{color} keys can be (ab)used to include graphics state operators.
\end{itemize}

\medskip
\noindent
The default \texttt{colorfunc} for penalties is:
\begin{verbatim}
function(p)
local color = "1 g"
if p < 10000 then
color = string.format("%g g", 1 - math.floor(p / 10000))
end
return color
end
\end{verbatim}

\section{Copying}

Copyright 2012–2023 Patrick Gundlach (patrick@gundla.ch) and other authors (see Git for information), licensed under the MIT license. See the Lua file for details.
Expand Down