| 1 |
-- Copyright 2012-2025 Patrick Gundlach, [email protected] |
| 2 |
-- Copyright 2025 Udi Fogiel, [email protected] |
| 3 |
-- Public repository: |
| 4 |
-- https://github.com/Udi-Fogiel/lvdebug (issues/pull requests,...) Version: V1.1, 2026-04-24 |
| 5 |
-- |
| 6 |
-- Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
-- of this software and associated documentation files (the "Software"), to deal |
| 8 |
-- in the Software without restriction, including without limitation the rights |
| 9 |
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
-- copies of the Software, and to permit persons to whom the Software is |
| 11 |
-- furnished to do so, subject to the following conditions: |
| 12 |
-- |
| 13 |
-- The above copyright notice and this permission notice shall be included in |
| 14 |
-- all copies or substantial portions of the Software. |
| 15 |
-- |
| 16 |
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 |
-- SOFTWARE. |
| 23 |
|
| 24 |
|
| 25 |
-- There are 65782 scaled points in a PDF point |
| 26 |
-- Therefore we need to divide all TeX lengths by |
| 27 |
-- this amount to get the PDF points. |
| 28 |
local number_sp_in_a_pdf_point = tex.sp('1bp') |
| 29 |
|
| 30 |
|
| 31 |
-- The idea is the following: at page shipout, all elements on a page are fixed. |
| 32 |
-- TeX creates an intermediate data structure before putting that into the PDF |
| 33 |
-- We can "intercept" that data structure and add pdf_literal (whatist) nodes, |
| 34 |
-- that makes glues, kerns and other items visible by drawing a rule, rectangle |
| 35 |
-- or other visual aids. This has no influence on typeset material, because |
| 36 |
-- these pdf_literal instructions are only visible to the PDF file (PDF |
| 37 |
-- renderer) and have no size themselves. |
| 38 |
|
| 39 |
-- We recursively loop through the contents of boxes and look at the (linear) |
| 40 |
-- list of items in that box. We start at the "shipout box". |
| 41 |
|
| 42 |
-- The "algorithm" goes like this: |
| 43 |
-- |
| 44 |
-- head = pointer_to_beginning_of_box_material |
| 45 |
-- while head is not nil |
| 46 |
-- if this_item_is_a_box |
| 47 |
-- recurse_into_contents |
| 48 |
-- draw a rectangle around the contents |
| 49 |
-- elseif this_item_is_a_glue |
| 50 |
-- draw a rule that has the length of that glue |
| 51 |
-- elseif this_item_is_a_kern |
| 52 |
-- draw a rectangle with width of that kern |
| 53 |
-- ... |
| 54 |
-- end |
| 55 |
-- move pointer to the next item in the list |
| 56 |
-- -- the pointer is "nil" if there is no next item |
| 57 |
-- end |
| 58 |
|
| 59 |
local HLIST = node.id("hlist") |
| 60 |
local VLIST = node.id("vlist") |
| 61 |
local RULE = node.id("rule") |
| 62 |
local DIR = node.id("dir") |
| 63 |
local DISC = node.id("disc") |
| 64 |
local GLUE = node.id("glue") |
| 65 |
local KERN = node.id("kern") |
| 66 |
local PENALTY = node.id("penalty") |
| 67 |
local GLYPH = node.id("glyph") |
| 68 |
|
| 69 |
local fmt = string.format |
| 70 |
local floor = math.floor |
| 71 |
local insert = table.insert |
| 72 |
local table_remove = table.remove |
| 73 |
local insert_after = node.insert_after |
| 74 |
local insert_before = node.insert_before |
| 75 |
|
| 76 |
local running_glue_dimen = -2^30 |
| 77 |
|
| 78 |
local params = { |
| 79 |
hlist = {show = true, color = "0.5 G", width = 0.1}, |
| 80 |
vlist = {show = true, color = "0.1 G", width = 0.1}, |
| 81 |
rule = {show = true, color = "1 0 0 RG", width = 0.4}, |
| 82 |
disc = {show = true, color = "0 0 1 RG", width = 0.3}, |
| 83 |
glue = {show = true}, |
| 84 |
kern = {show = true, negativecolor = "1 0 0 rg", color = "1 1 0 rg", width = 1}, |
| 85 |
penalty = {show = true, colorfunc = function(p) local color = "1 g" if p < 10000 then |
| 86 |
color = fmt("%d g", 1 - floor(p / 10000)) end return color end}, |
| 87 |
glyph = {show = false, color = "1 0 0 RG", width = 0.1, baseline = true}, |
| 88 |
opacity = "" |
| 89 |
} |
| 90 |
|
| 91 |
-- Helpers |
| 92 |
|
| 93 |
local function math_round(num, idp) |
| 94 |
if idp and idp>0 then |
| 95 |
local mult = 10^idp |
| 96 |
return floor(num * mult + 0.5) / mult |
| 97 |
end |
| 98 |
return floor(num + 0.5) |
| 99 |
end |
| 100 |
|
| 101 |
local function new_literal(data) |
| 102 |
local n = node.new("whatsit", "pdf_literal") |
| 103 |
n.data = data |
| 104 |
return n |
| 105 |
end |
| 106 |
|
| 107 |
local function insert_literal_before(list, target, data) |
| 108 |
return insert_before(list, target, new_literal(data)) |
| 109 |
end |
| 110 |
|
| 111 |
local function to_bp(sp) |
| 112 |
return math_round(sp / number_sp_in_a_pdf_point, 2) |
| 113 |
end |
| 114 |
|
| 115 |
local function pdf_rect(opacity, color, lw, x, y, w, h, mode) |
| 116 |
-- mode: "s" = stroke, "B" = fill+stroke |
| 117 |
return fmt("q %s %s %g w %g %g %g %g re %s Q", |
| 118 |
opacity, color, lw, x, y, w, h, mode or "s") |
| 119 |
end |
| 120 |
|
| 121 |
local show_page_elements |
| 122 |
|
| 123 |
local function show_page_elements(parent) |
| 124 |
|
| 125 |
local dirstack = {} |
| 126 |
local currdir = parent.direction |
| 127 |
local head = parent.list |
| 128 |
|
| 129 |
while head do |
| 130 |
|
| 131 |
if head.id == HLIST or head.id == VLIST then |
| 132 |
local boxtype = node.type(head.id) |
| 133 |
local p = params[boxtype] |
| 134 |
show_page_elements(head) |
| 135 |
if p.show then |
| 136 |
local wd = to_bp(head.width) |
| 137 |
local dp = to_bp(head.depth) |
| 138 |
local ht = to_bp(head.height + head.depth) |
| 139 |
local f = 1 - 2 * head.direction |
| 140 |
local x, y, w, h |
| 141 |
if head.id == HLIST then |
| 142 |
x, y, w, h = 0, -dp, f * wd, ht |
| 143 |
else |
| 144 |
x, y, w, h = 0, 0, f * wd, -ht |
| 145 |
end |
| 146 |
head.list = insert_before(head.list, head.list, |
| 147 |
new_literal(fmt("q %s %s %g w %g %g %g %g re s Q", |
| 148 |
params.opacity, p.color, p.width, x, y, w, h))) |
| 149 |
end |
| 150 |
|
| 151 |
elseif head.id == RULE and params.rule.show then |
| 152 |
if head.width ~= running_glue_dimen |
| 153 |
and head.height ~= running_glue_dimen |
| 154 |
and head.depth ~= running_glue_dimen then |
| 155 |
local dp = to_bp(head.depth) |
| 156 |
local ht = to_bp(head.height) |
| 157 |
parent.list = insert_literal_before(parent.list, head, |
| 158 |
fmt("q %s %s %g w 0 %g m 0 %g l S Q", params.opacity, |
| 159 |
params.rule.color, params.rule.width, -dp, ht)) |
| 160 |
end |
| 161 |
|
| 162 |
elseif head.id == DISC and params.disc.show then |
| 163 |
parent.list = insert_literal_before(parent.list, head, |
| 164 |
fmt("q %s %s %g w 0 -1 m 0 0 l S Q", |
| 165 |
params.opacity, params.disc.color, params.disc.width)) |
| 166 |
|
| 167 |
elseif head.id == DIR then |
| 168 |
if head.subtype == 0 then |
| 169 |
insert(dirstack,currdir) |
| 170 |
currdir = head.direction |
| 171 |
elseif #dirstack > 0 then |
| 172 |
currdir = table_remove(dirstack) |
| 173 |
end |
| 174 |
|
| 175 |
elseif head.id == GLUE and params.glue.show then |
| 176 |
local spec = head.spec or head |
| 177 |
local wd = spec.width |
| 178 |
local color = "0.5 G" |
| 179 |
if parent.glue_sign == 1 and parent.glue_order == spec.stretch_order then |
| 180 |
wd = wd + parent.glue_set * spec.stretch |
| 181 |
color = "0 0 1 RG" |
| 182 |
elseif parent.glue_sign == 2 and parent.glue_order == spec.shrink_order then |
| 183 |
wd = wd - parent.glue_set * spec.shrink |
| 184 |
color = "1 0 1 RG" |
| 185 |
end |
| 186 |
local wd_bp = to_bp(wd) |
| 187 |
local f = 1 - 2 * currdir |
| 188 |
local data |
| 189 |
if parent.id == HLIST then |
| 190 |
data = fmt("q %s [0.2] 0 d 0.5 w 0 0 m %g 0 l S Q", color, wd_bp * f) |
| 191 |
else |
| 192 |
local dash = 0.25 * f |
| 193 |
data = fmt("q 0.1 G 0.1 w -0.5 0 m 0.5 0 l -0.5 %g m 0.5 %g l S [0.2] 0 d 0.5 w %g 0 m %g %g l S Q", |
| 194 |
-wd_bp, -wd_bp, dash, dash, -wd_bp) |
| 195 |
end |
| 196 |
parent.list = insert_literal_before(parent.list, head, data) |
| 197 |
|
| 198 |
elseif head.id == KERN and params.kern.show then |
| 199 |
local color = head.kern < 0 and params.kern.negativecolor or params.kern.color |
| 200 |
local k = to_bp(head.kern) |
| 201 |
local f = 1 - 2 * currdir |
| 202 |
local w, h |
| 203 |
if parent.id == HLIST then |
| 204 |
w, h = f * k, params.kern.width |
| 205 |
else |
| 206 |
w, h = f * params.kern.width, -k |
| 207 |
end |
| 208 |
parent.list = insert_literal_before(parent.list, head, |
| 209 |
fmt("q %s %s 0 w 0 0 %g %g re B Q", params.opacity, color, w, h)) |
| 210 |
|
| 211 |
elseif head.id == PENALTY and params.penalty.show then |
| 212 |
parent.list = insert_literal_before(parent.list, head, |
| 213 |
fmt("q %s 0 w 0 0 %g 1 re B Q", |
| 214 |
params.penalty.colorfunc(head.penalty), 1 - 2 * currdir)) |
| 215 |
|
| 216 |
elseif head.id == GLYPH and params.glyph.show then |
| 217 |
local p = params.glyph |
| 218 |
local f = 1 - 2 * currdir |
| 219 |
local wd = -to_bp(head.width) |
| 220 |
local ht = to_bp(head.height + head.depth) |
| 221 |
local dp = to_bp(head.depth) |
| 222 |
local baseline = (head.depth ~= 0 and p.baseline) |
| 223 |
and fmt("%g %g m %g %g l", 0, 0, f * wd, 0) or "" |
| 224 |
parent.list, head = insert_after(parent.list, head, |
| 225 |
new_literal(fmt("q %s %s %g w %s %g %g %g %g re s Q", |
| 226 |
params.opacity, p.color, p.width, baseline, 0, -dp, f * wd, ht))) |
| 227 |
end |
| 228 |
|
| 229 |
head = head.next |
| 230 |
end |
| 231 |
return true |
| 232 |
end |
| 233 |
|
| 234 |
return { |
| 235 |
show_page_elements = show_page_elements, |
| 236 |
params = params |
| 237 |
} |