| 1 |
-- Copyright (c) 2022-2023 Thomas Kelkel [email protected] |
| 2 |
|
| 3 |
-- This file may be distributed and/or modified under the |
| 4 |
-- conditions of the LaTeX Project Public License, either |
| 5 |
-- version 1.3c of this license or (at your option) any later |
| 6 |
-- version. The latest version of this license is in |
| 7 |
|
| 8 |
-- http://www.latex-project.org/lppl.txt |
| 9 |
|
| 10 |
-- and version 1.3c or later is part of all distributions of |
| 11 |
-- LaTeX version 2009/09/24 or later. |
| 12 |
|
| 13 |
-- Version: 0.3 |
| 14 |
|
| 15 |
local ID = node.id |
| 16 |
local GLYPH = ID ( "glyph" ) |
| 17 |
local DISC = ID ( "disc" ) |
| 18 |
local GLUE = ID ( "glue" ) |
| 19 |
local KERN = ID ( "kern" ) |
| 20 |
local WI = ID ( "whatsit" ) |
| 21 |
local HLIST = ID ( "hlist" ) |
| 22 |
local VLIST = ID ( "vlist" ) |
| 23 |
|
| 24 |
local PDF_LITERAL = table.swapped ( node.whatsits () )["pdf_literal"] |
| 25 |
|
| 26 |
local NEW = node.new |
| 27 |
local PREV = node.prev |
| 28 |
local NEXT = node.next |
| 29 |
local TAIL = node.tail |
| 30 |
local INS_B = node.insert_before |
| 31 |
local INS_A = node.insert_after |
| 32 |
local HAS_GLYPH = node.has_glyph |
| 33 |
local T = node.traverse |
| 34 |
local T_ID = node.traverse_id |
| 35 |
|
| 36 |
local FLOOR = math.floor |
| 37 |
local ABS = math.abs |
| 38 |
|
| 39 |
local ATC = luatexbase.add_to_callback |
| 40 |
|
| 41 |
----- |
| 42 |
|
| 43 |
local on_top = false |
| 44 |
local DIR = PREV |
| 45 |
local AB = INS_B |
| 46 |
local f = 1 |
| 47 |
|
| 48 |
function showkerning_on_top () |
| 49 |
on_top = true |
| 50 |
DIR = NEXT |
| 51 |
AB = INS_A |
| 52 |
f = - 1 |
| 53 |
end |
| 54 |
|
| 55 |
local function round ( num, dec ) |
| 56 |
return FLOOR ( num * 10^dec + 0.5 ) / 10^dec |
| 57 |
end |
| 58 |
|
| 59 |
local function calc_value ( value ) |
| 60 |
value = round ( value / 65781, 3 ) |
| 61 |
return value |
| 62 |
end |
| 63 |
|
| 64 |
local function find_first_last ( n, node_type, last ) |
| 65 |
local d = NEXT |
| 66 |
if last then |
| 67 |
d = PREV |
| 68 |
n = TAIL ( n ) |
| 69 |
end |
| 70 |
while true do |
| 71 |
if n and n.id == node_type then |
| 72 |
return n |
| 73 |
end |
| 74 |
if d ( n ) then |
| 75 |
n = d ( n ) |
| 76 |
else |
| 77 |
return false |
| 78 |
end |
| 79 |
end |
| 80 |
end |
| 81 |
|
| 82 |
local function find_glyph ( n, d ) |
| 83 |
local kern_value = 0 |
| 84 |
if d ( n ) then |
| 85 |
n = d ( n ) |
| 86 |
while not ( n.id == GLYPH or n.replace and HAS_GLYPH ( n.replace ) ) do |
| 87 |
if not d ( n ) then return false end |
| 88 |
if n.id == GLUE then |
| 89 |
kern_value = kern_value + n.width |
| 90 |
end |
| 91 |
n = d ( n ) |
| 92 |
end |
| 93 |
else |
| 94 |
return false |
| 95 |
end |
| 96 |
local return_value = n |
| 97 |
if n.id == DISC then |
| 98 |
return_value = find_first_last ( n.replace, GLYPH, d == PREV ) |
| 99 |
for kern_node in T_ID ( KERN, n.replace ) do |
| 100 |
kern_value = kern_value + kern_node.kern |
| 101 |
end |
| 102 |
end |
| 103 |
return return_value, n, kern_value |
| 104 |
end |
| 105 |
|
| 106 |
local function check_glyph ( n, d, has_prev_next_glyph, prev_next_glyph, insert_prev_next ) |
| 107 |
local kern_value |
| 108 |
if find_glyph ( n, d ) then |
| 109 |
prev_next_glyph, insert_prev_next, kern_value = find_glyph ( n, d ) |
| 110 |
has_prev_next_glyph = true |
| 111 |
end |
| 112 |
return has_prev_next_glyph, prev_next_glyph, insert_prev_next, kern_value |
| 113 |
end |
| 114 |
|
| 115 |
local function get_x_value ( n, x_value ) |
| 116 |
local exp_fac = 1 |
| 117 |
if n.expansion_factor then |
| 118 |
exp_fac = n.expansion_factor / 1000000 + 1 |
| 119 |
end |
| 120 |
if n.width then |
| 121 |
x_value = x_value + calc_value ( n.width ) * exp_fac |
| 122 |
elseif n.kern then |
| 123 |
x_value = x_value + calc_value ( n.kern ) * exp_fac |
| 124 |
end |
| 125 |
return x_value |
| 126 |
end |
| 127 |
|
| 128 |
local function find_kern ( n, d ) |
| 129 |
local kern_value = 0 |
| 130 |
local disc = false |
| 131 |
if d ( n ) then |
| 132 |
n = d ( n ) |
| 133 |
while n and not ( n.id == GLYPH or disc ) do |
| 134 |
if n.id == KERN then |
| 135 |
kern_value = get_x_value ( n, kern_value ) |
| 136 |
elseif n.replace then |
| 137 |
local start_point = n.replace |
| 138 |
if d == PREV then |
| 139 |
start_point = TAIL ( n.replace ) |
| 140 |
end |
| 141 |
if start_point.id == KERN then |
| 142 |
kern_value = get_x_value ( start_point, kern_value ) |
| 143 |
end |
| 144 |
disc = true |
| 145 |
end |
| 146 |
if d ( n ) then |
| 147 |
n = d ( n ) |
| 148 |
else |
| 149 |
break |
| 150 |
end |
| 151 |
end |
| 152 |
if not ( kern_value == 0 ) then |
| 153 |
return kern_value |
| 154 |
end |
| 155 |
end |
| 156 |
return false |
| 157 |
end |
| 158 |
|
| 159 |
local function make_rule ( head, n, kern_node, current_head, disc_list ) |
| 160 |
local color = "0 0 0" |
| 161 |
local has_prev_glyph = false |
| 162 |
local has_next_glyph = false |
| 163 |
local prev_glyph = n |
| 164 |
local next_glyph = n |
| 165 |
local insert_prev = n |
| 166 |
local insert_next = n |
| 167 |
local insert_point = n |
| 168 |
local x_value = 0 |
| 169 |
local height = 0 |
| 170 |
local depth = 0 |
| 171 |
local prev_kern_value = 0 |
| 172 |
local next_kern_value = 0 |
| 173 |
local thickness = calc_value ( kern_node.kern ) |
| 174 |
has_prev_glyph, prev_glyph, insert_prev, prev_kern_value = check_glyph ( n, PREV, has_prev_glyph, prev_glyph, insert_prev ) |
| 175 |
has_next_glyph, next_glyph, insert_next, next_kern_value = check_glyph ( n, NEXT, has_next_glyph, next_glyph, insert_next ) |
| 176 |
if disc_list and not ( TAIL ( current_head ) == kern_node ) and not ( current_head == kern_node ) then |
| 177 |
has_prev_glyph, prev_glyph, insert_prev, prev_kern_value = check_glyph ( kern_node, PREV, has_prev_glyph, kern_node, kern_node ) |
| 178 |
has_next_glyph, next_glyph, insert_next, next_kern_value = check_glyph ( kern_node, NEXT, has_next_glyph, kern_node, kern_node ) |
| 179 |
end |
| 180 |
if not on_top and has_prev_glyph then |
| 181 |
insert_point = insert_prev |
| 182 |
x_value = get_x_value ( prev_glyph, x_value ) |
| 183 |
x_value = x_value + calc_value ( prev_kern_value ) |
| 184 |
if disc_list and HAS_GLYPH ( current_head ) and TAIL ( current_head ) == kern_node then |
| 185 |
for some_node in T ( current_head ) do |
| 186 |
if some_node == TAIL ( current_head ) then break end |
| 187 |
x_value = get_x_value ( some_node, x_value ) |
| 188 |
end |
| 189 |
end |
| 190 |
elseif on_top then |
| 191 |
insert_point = insert_next |
| 192 |
if has_next_glyph then |
| 193 |
x_value = get_x_value ( next_glyph, x_value ) |
| 194 |
x_value = x_value + calc_value ( next_kern_value ) |
| 195 |
end |
| 196 |
if disc_list and HAS_GLYPH ( current_head ) and current_head == kern_node then |
| 197 |
local kern_counter = 0 |
| 198 |
for some_node in T ( current_head ) do |
| 199 |
if some_node.id == KERN then |
| 200 |
kern_counter = kern_counter + 1 |
| 201 |
end |
| 202 |
if not ( some_node == current_head ) then |
| 203 |
x_value = get_x_value ( some_node, x_value ) |
| 204 |
end |
| 205 |
end |
| 206 |
if find_kern ( n, NEXT ) then |
| 207 |
x_value = x_value + find_kern ( n, NEXT ) |
| 208 |
end |
| 209 |
end |
| 210 |
end |
| 211 |
if disc_list and HAS_GLYPH ( current_head ) then |
| 212 |
if TAIL ( current_head ) == kern_node then |
| 213 |
has_prev_glyph = false |
| 214 |
has_prev_glyph, prev_glyph = check_glyph ( kern_node, PREV, has_prev_glyph, prev_glyph, insert_prev ) |
| 215 |
end |
| 216 |
if current_head == kern_node then |
| 217 |
has_next_glyph = false |
| 218 |
has_next_glyph, next_glyph = check_glyph ( kern_node, NEXT, has_next_glyph, next_glyph, insert_next ) |
| 219 |
end |
| 220 |
end |
| 221 |
if has_prev_glyph then |
| 222 |
height = calc_value ( prev_glyph.height ) |
| 223 |
depth = calc_value ( prev_glyph.depth ) |
| 224 |
if has_next_glyph then |
| 225 |
if next_glyph.height > prev_glyph.height then height = calc_value ( next_glyph.height ) end |
| 226 |
if next_glyph.depth > prev_glyph.depth then depth = calc_value ( next_glyph.depth ) end |
| 227 |
end |
| 228 |
elseif has_next_glyph then |
| 229 |
height = calc_value ( next_glyph.height ) |
| 230 |
depth = calc_value ( next_glyph.depth ) |
| 231 |
end |
| 232 |
if find_kern ( n, PREV ) then |
| 233 |
thickness = thickness + find_kern ( n, PREV ) |
| 234 |
end |
| 235 |
if thickness ~= 0 and thickness > calc_value ( tex.hsize ) * - 1 then |
| 236 |
if thickness > 0 then |
| 237 |
color = "0 1 0" |
| 238 |
else |
| 239 |
color = "1 0 0" |
| 240 |
end |
| 241 |
if disc_list and not ( TAIL ( current_head ) == kern_node ) and not ( current_head == kern_node ) then |
| 242 |
current_head = AB ( current_head, insert_point, NEW ( WI, PDF_LITERAL ) ) |
| 243 |
else |
| 244 |
head = AB ( head, insert_point, NEW ( WI, PDF_LITERAL ) ) |
| 245 |
end |
| 246 |
x_value = ( x_value + calc_value ( kern_node.expansion_factor ) + thickness * 0.5 ) * f |
| 247 |
DIR ( insert_point ).mode = 0 |
| 248 |
DIR ( insert_point ).data = "q " .. ABS ( thickness ) .. " w " .. x_value .. " " .. - depth .. " m " .. x_value .. " " .. height .. " l " .. color .. " RG S Q" |
| 249 |
end |
| 250 |
return head, current_head |
| 251 |
end |
| 252 |
|
| 253 |
local function show_kerns ( head ) |
| 254 |
for n in T ( head ) do |
| 255 |
if n.id == HLIST or n.id == VLIST then |
| 256 |
n.head = show_kerns ( n.head ) |
| 257 |
elseif n.id == KERN and not find_kern ( n, NEXT ) then |
| 258 |
head = make_rule ( head, n, n, head, false ) |
| 259 |
elseif n.replace then |
| 260 |
local REPLACE = n.replace |
| 261 |
for kern_node in T_ID ( KERN, REPLACE ) do |
| 262 |
if not find_kern ( kern_node, NEXT ) then |
| 263 |
head, REPLACE = make_rule ( head , n, kern_node, REPLACE, true ) |
| 264 |
end |
| 265 |
end |
| 266 |
n.replace = REPLACE |
| 267 |
end |
| 268 |
end |
| 269 |
return head |
| 270 |
end |
| 271 |
|
| 272 |
ATC ( "post_linebreak_filter", show_kerns, "show kerns in postline" ) |
| 273 |
ATC ( "hpack_filter", show_kerns, "show kerns in hpack" ) |