| 1 |
----------------------------------------------------------------------- |
| 2 |
-- FILE: luaotfload-colors.lua |
| 3 |
-- DESCRIPTION: part of luaotfload / font colors |
| 4 |
----------------------------------------------------------------------- |
| 5 |
|
| 6 |
assert(luaotfload_module, "This is a part of luaotfload and should not be loaded independently") { |
| 7 |
name = "luaotfload-colors", |
| 8 |
version = "3.29", --TAGVERSION |
| 9 |
date = "2024-12-03", --TAGDATE |
| 10 |
description = "luaotfload submodule / color", |
| 11 |
license = "GPL v2.0", |
| 12 |
author = "Khaled Hosny, Elie Roux, Philipp Gesang, Dohyun Kim, David Carlisle", |
| 13 |
copyright = "Luaotfload Development Team" |
| 14 |
} |
| 15 |
|
| 16 |
--[[doc-- |
| 17 |
|
| 18 |
buggy coloring with the pre_output_filter when expansion is enabled |
| 19 |
· tfmdata for different expansion values is split over different objects |
| 20 |
· in ``initializeexpansion()``, chr.expansion_factor is set, and only |
| 21 |
those characters that have it are affected |
| 22 |
· in constructors.scale: chr.expansion_factor = ve*1000 if commented out |
| 23 |
makes the bug vanish |
| 24 |
|
| 25 |
explanation: http://tug.org/pipermail/luatex/2013-May/004305.html |
| 26 |
|
| 27 |
--doc]]-- |
| 28 |
|
| 29 |
local logreport = luaotfload and luaotfload.log.report or print |
| 30 |
|
| 31 |
local nodedirect = node.direct |
| 32 |
local newnode = nodedirect.new |
| 33 |
local insert_node_before = nodedirect.insert_before |
| 34 |
local insert_node_after = nodedirect.insert_after |
| 35 |
local todirect = nodedirect.todirect |
| 36 |
local tonode = nodedirect.tonode |
| 37 |
local setfield = nodedirect.setfield |
| 38 |
local setdisc = nodedirect.setdisc |
| 39 |
local setreplace = nodedirect.setreplace |
| 40 |
local getid = nodedirect.getid |
| 41 |
local getfont = nodedirect.getfont |
| 42 |
local getchar = nodedirect.getchar |
| 43 |
local getlist = nodedirect.getlist |
| 44 |
local getdisc = nodedirect.getdisc |
| 45 |
local getsubtype = nodedirect.getsubtype |
| 46 |
local getnext = nodedirect.getnext |
| 47 |
local nodetail = nodedirect.tail |
| 48 |
local getattribute = nodedirect.has_attribute |
| 49 |
local setattribute = nodedirect.set_attribute |
| 50 |
|
| 51 |
local call_callback = luatexbase.call_callback |
| 52 |
|
| 53 |
local stringformat = string.format |
| 54 |
local identifiers = fonts.hashes.identifiers |
| 55 |
|
| 56 |
local add_color_callback --[[ this used to be a global‽ ]] |
| 57 |
|
| 58 |
local custom_setcolor, custom_settransparent |
| 59 |
|
| 60 |
--[[doc-- |
| 61 |
Color string parser. |
| 62 |
--doc]]-- |
| 63 |
|
| 64 |
local lpeg = require"lpeg" |
| 65 |
local lpegmatch = lpeg.match |
| 66 |
local C, Cc, P, R, S = lpeg.C, lpeg.Cc, lpeg.P, lpeg.R, lpeg.S |
| 67 |
|
| 68 |
local spaces = S"\t "^0 |
| 69 |
local digit16 = R("09", "af", "AF") |
| 70 |
local opaque = S("fF") * S("fF") |
| 71 |
local octet = digit16 * digit16 / function(s) |
| 72 |
return tonumber(s, 16) / 255 |
| 73 |
end |
| 74 |
|
| 75 |
local function lpeg_repeat(patt, count) |
| 76 |
patt = P(patt) |
| 77 |
local result = patt |
| 78 |
for i = 2, count do |
| 79 |
result = result * patt |
| 80 |
end |
| 81 |
return result |
| 82 |
end |
| 83 |
|
| 84 |
local split_color = spaces * C(lpeg_repeat(digit16, 6)) * (opaque + C(lpeg_repeat(digit16, 2)))^-1 * spaces * -1 |
| 85 |
+ spaces * (C((spaces * (1 - S', ')^1)^1) + Cc(nil)) * spaces * (',' * spaces * C((spaces * (1 - S' ,')^1)^1)^-1 * spaces)^-1 * -1 |
| 86 |
|
| 87 |
luatexbase.create_callback('luaotfload.split_color', 'exclusive', function(value) |
| 88 |
local rgb, a = lpegmatch(split_color, value) |
| 89 |
if not rgb and not a then |
| 90 |
logreport("both", 0, "color", |
| 91 |
"%q is not a valid rgb[a] color expression", |
| 92 |
digits) |
| 93 |
end |
| 94 |
return rgb, a |
| 95 |
end) |
| 96 |
|
| 97 |
local extract_color = octet * octet * octet / function(r,g,b) |
| 98 |
return stringformat("%.3g %.3g %.3g rg", r, g, b) |
| 99 |
end * -1 |
| 100 |
luatexbase.create_callback('luaotfload.parse_color', 'exclusive', function(value) |
| 101 |
local rgb = lpegmatch(extract_color, value) |
| 102 |
if not rgb then |
| 103 |
logreport("both", 0, "color", |
| 104 |
"Invalid color part in color expression %q", |
| 105 |
value) |
| 106 |
end |
| 107 |
return rgb |
| 108 |
end) |
| 109 |
|
| 110 |
-- Keep the currently collected page resources needed for the current |
| 111 |
-- colors in `res`. |
| 112 |
|
| 113 |
local res = nil |
| 114 |
|
| 115 |
--- float -> unit |
| 116 |
local function pageresources(alpha) |
| 117 |
res = res or {true} -- Initialize with /TransGs1 |
| 118 |
local f = res[alpha] |
| 119 |
or stringformat("/TransGs%.3g gs", alpha, alpha) |
| 120 |
res[alpha] = f |
| 121 |
return f |
| 122 |
end |
| 123 |
|
| 124 |
local extract_transparent = octet * -1 |
| 125 |
luatexbase.create_callback('luaotfload.parse_transparent', 'exclusive', function(value) |
| 126 |
local a |
| 127 |
if type(value) == 'string' then |
| 128 |
a = lpegmatch(extract_transparent, value) |
| 129 |
if not a then |
| 130 |
logreport("both", 0, "color", |
| 131 |
"Invalid transparency part in color expression %q", |
| 132 |
value) |
| 133 |
end |
| 134 |
else |
| 135 |
a = value |
| 136 |
end |
| 137 |
if a then |
| 138 |
a = pageresources(a) |
| 139 |
end |
| 140 |
return a |
| 141 |
end) |
| 142 |
|
| 143 |
|
| 144 |
--- string -> (string | nil) |
| 145 |
local function sanitize_color_expression (digits) |
| 146 |
digits = tostring(digits) |
| 147 |
local rgb, a = call_callback('luaotfload.split_color', digits) |
| 148 |
if rgb then |
| 149 |
rgb = call_callback('luaotfload.parse_color', rgb) |
| 150 |
end |
| 151 |
if a then |
| 152 |
a = call_callback('luaotfload.parse_transparent', a) |
| 153 |
end |
| 154 |
return rgb, a |
| 155 |
end |
| 156 |
|
| 157 |
local color_stack = 0 |
| 158 |
-- Beside maybe allowing {transparency} package compatibility at some |
| 159 |
-- point, this ensures that the stack is only created if it is actually |
| 160 |
-- needed. Especially important because it adds /TransGs1 gs to every page |
| 161 |
local function transparent_stack() |
| 162 |
-- if token.is_defined'TRP@colorstack' then -- transparency |
| 163 |
-- transparent_stack = tonumber(token.get_macro'TRP@colorstack') |
| 164 |
-- else |
| 165 |
transparent_stack = pdf.newcolorstack("/TransGs1 gs","direct",true) |
| 166 |
-- end |
| 167 |
return transparent_stack |
| 168 |
end |
| 169 |
|
| 170 |
--- Luatex internal types |
| 171 |
|
| 172 |
local nodetype = node.id |
| 173 |
local glyph_t = nodetype("glyph") |
| 174 |
local hlist_t = nodetype("hlist") |
| 175 |
local vlist_t = nodetype("vlist") |
| 176 |
local whatsit_t = nodetype("whatsit") |
| 177 |
local disc_t = nodetype("disc") |
| 178 |
local colorstack_t = node.subtype("pdf_colorstack") |
| 179 |
|
| 180 |
local color_callback |
| 181 |
local color_attr = luatexbase.new_attribute("luaotfload_color_attribute") |
| 182 |
|
| 183 |
-- Pass nil for new_color or old_color to indicate no color |
| 184 |
-- If color is nil, pass tail to decide where to add whatsit |
| 185 |
local function color_whatsit (head, curr, stack, old_color, new_color, tail) |
| 186 |
if new_color == old_color then |
| 187 |
return head, curr, old_color |
| 188 |
end |
| 189 |
local colornode = newnode(whatsit_t, colorstack_t) |
| 190 |
setfield(colornode, "stack", tonumber(stack) or stack()) |
| 191 |
setfield(colornode, "command", new_color and (old_color and 0 or 1) or 2) -- 1: push, 2: pop |
| 192 |
setfield(colornode, "data", new_color) -- Is nil for pop |
| 193 |
if tail then |
| 194 |
head, curr = insert_node_after (head, curr, colornode) |
| 195 |
else |
| 196 |
head = insert_node_before(head, curr, colornode) |
| 197 |
end |
| 198 |
return head, curr, new_color |
| 199 |
end |
| 200 |
|
| 201 |
-- number -> string | nil |
| 202 |
local function get_glyph_color (font_id, char) |
| 203 |
local tfmdata = identifiers[font_id] |
| 204 |
local properties = tfmdata and tfmdata.properties |
| 205 |
local font_color = properties and properties.color_rgb |
| 206 |
local font_transparent = properties and properties.color_a |
| 207 |
if type(font_color) == "table" then |
| 208 |
local char_tbl = tfmdata.characters[char] |
| 209 |
char = char_tbl and (char_tbl.index or char) |
| 210 |
font_color = char and font_color[char] or font_color.default |
| 211 |
font_transparent = font_transparent and (char and font_transparent[char] or font_transparent.default) |
| 212 |
end |
| 213 |
return font_color, font_transparent |
| 214 |
end |
| 215 |
|
| 216 |
--[[doc-- |
| 217 |
While the second argument and second returned value are apparently |
| 218 |
always nil when the function is called, they temporarily take string |
| 219 |
values during the node list traversal. |
| 220 |
--doc]]-- |
| 221 |
|
| 222 |
--- (node * (string | nil)) -> (node * (string | nil)) |
| 223 |
local function node_colorize (head, toplevel, current_color, current_transparent) |
| 224 |
local n = head |
| 225 |
while n do |
| 226 |
local n_id = getid(n) |
| 227 |
|
| 228 |
if n_id == hlist_t or n_id == vlist_t then |
| 229 |
local n_list = getlist(n) |
| 230 |
if getattribute(n_list, color_attr) then |
| 231 |
head, n, current_color = color_whatsit(head, n, color_stack, current_color, nil) |
| 232 |
head, n, current_transparent = color_whatsit(head, n, transparent_stack, current_transparent, nil) |
| 233 |
else |
| 234 |
n_list, current_color, current_transparent = node_colorize(n_list, false, current_color, current_transparent) |
| 235 |
if getsubtype(n) == 1 then -- created by linebreak |
| 236 |
local nn = nodetail(n_list) |
| 237 |
n_list, nn, current_color = color_whatsit(n_list, nn, color_stack, current_color, nil, true) |
| 238 |
n_list, nn, current_transparent = color_whatsit(n_list, nn, transparent_stack, current_transparent, nil, true) |
| 239 |
end |
| 240 |
setfield(n, "head", n_list) |
| 241 |
end |
| 242 |
|
| 243 |
elseif n_id == disc_t then |
| 244 |
local n_pre, n_post, n_replace = getdisc(n) |
| 245 |
n_replace, current_color, current_transparent = node_colorize(n_replace, false, current_color, current_transparent) |
| 246 |
setdisc(n, n_pre, n_post, n_replace) |
| 247 |
|
| 248 |
elseif n_id == glyph_t then |
| 249 |
--- colorization is restricted to those fonts |
| 250 |
--- that received the “color” property upon |
| 251 |
--- loading (see ``setcolor()`` above) |
| 252 |
local glyph_color, glyph_transparent = get_glyph_color(getfont(n), getchar(n)) |
| 253 |
if custom_setcolor then |
| 254 |
if glyph_color then |
| 255 |
head, n = custom_setcolor(head, n, glyph_color) -- Don't change current_color to transform all other color_whatsit calls into noops |
| 256 |
end |
| 257 |
else |
| 258 |
head, n, current_color = color_whatsit(head, n, color_stack, current_color, glyph_color) |
| 259 |
end |
| 260 |
if custom_settransparent then |
| 261 |
if glyph_transparent then |
| 262 |
head, n = custom_settransparent(head, n, glyph_transparent) -- Don't change current_transparent to transform all other color_whatsit calls into noops |
| 263 |
end |
| 264 |
else |
| 265 |
head, n, current_transparent = color_whatsit(head, n, transparent_stack, current_transparent, glyph_transparent) |
| 266 |
end |
| 267 |
|
| 268 |
elseif n_id == whatsit_t then |
| 269 |
head, n, current_color = color_whatsit(head, n, color_stack, current_color, nil) |
| 270 |
head, n, current_transparent = color_whatsit(head, n, transparent_stack, current_transparent, nil) |
| 271 |
|
| 272 |
end |
| 273 |
|
| 274 |
n = getnext(n) |
| 275 |
end |
| 276 |
|
| 277 |
if toplevel then |
| 278 |
local nn = nodetail(head) |
| 279 |
head, nn, current_color = color_whatsit(head, nn, color_stack, current_color, nil, true) |
| 280 |
head, nn, current_transparent = color_whatsit(head, nn, transparent_stack, current_transparent, nil, true) |
| 281 |
end |
| 282 |
|
| 283 |
setattribute(head, color_attr, 1) |
| 284 |
return head, current_color, current_transparent |
| 285 |
end |
| 286 |
|
| 287 |
local getpageres = pdf.getpageresources or function() return pdf.pageresources end |
| 288 |
local setpageres = pdf.setpageresources or function(s) pdf.pageresources = s end |
| 289 |
local catat11 = luatexbase.registernumber("catcodetable@atletter") |
| 290 |
local gettoks, scantoks = tex.gettoks, tex.scantoks |
| 291 |
local pgf = { bye = "pgfutil@everybye", extgs = "\\pgf@sys@addpdfresource@extgs@plain" } |
| 292 |
|
| 293 |
--- node -> node |
| 294 |
local function color_handler (head) |
| 295 |
head = todirect(head) |
| 296 |
head = node_colorize(head, true) |
| 297 |
head = tonode(head) |
| 298 |
|
| 299 |
-- now append our page resources |
| 300 |
if res and tonumber(transparent_stack) then |
| 301 |
if scantoks and nil == pgf.loaded then |
| 302 |
pgf.loaded = token.create(pgf.bye).cmdname == "assign_toks" |
| 303 |
end |
| 304 |
local tpr = pgf.loaded and gettoks(pgf.bye) or -- PGF |
| 305 |
-- token.is_defined'TRP@list' and token.get_macro'TRP@list' or -- transparency |
| 306 |
getpageres() or "" |
| 307 |
|
| 308 |
local t = "" |
| 309 |
for k in pairs(res) do |
| 310 |
local str = stringformat("/TransGs%.3g<</ca %.3g>>", k, k) -- don't touch stroking elements |
| 311 |
if not tpr:find(str) then |
| 312 |
t = t .. str |
| 313 |
end |
| 314 |
end |
| 315 |
if t ~= "" then |
| 316 |
if pgf.loaded then |
| 317 |
scantoks("global", pgf.bye, catat11, stringformat("%s{%s}%s", pgf.extgs, t, tpr)) |
| 318 |
-- elseif token.is_defined'TRP@list' then |
| 319 |
-- token.set_macro('TRP@list', t .. tpr, 'global') |
| 320 |
else |
| 321 |
local tpr, n = tpr:gsub("/ExtGState<<", "%1"..t) |
| 322 |
if n == 0 then |
| 323 |
tpr = stringformat("%s/ExtGState<<%s>>", tpr, t) |
| 324 |
end |
| 325 |
setpageres(tpr) |
| 326 |
end |
| 327 |
end |
| 328 |
res = nil -- reset res |
| 329 |
end |
| 330 |
return head |
| 331 |
end |
| 332 |
|
| 333 |
local color_callback_name = "luaotfload.color_handler" |
| 334 |
local color_callback_activated = 0 |
| 335 |
local add_to_callback = luatexbase.add_to_callback |
| 336 |
|
| 337 |
--- unit -> unit |
| 338 |
add_color_callback = function ( ) |
| 339 |
color_callback = config.luaotfload.run.color_callback |
| 340 |
if not color_callback then |
| 341 |
color_callback = "post_linebreak_filter" |
| 342 |
end |
| 343 |
|
| 344 |
if color_callback_activated == 0 then |
| 345 |
add_to_callback(color_callback, |
| 346 |
color_handler, |
| 347 |
color_callback_name) |
| 348 |
add_to_callback("hpack_filter", |
| 349 |
function (head, groupcode) |
| 350 |
if groupcode == "hbox" or |
| 351 |
groupcode == "adjusted_hbox" or |
| 352 |
groupcode == "align_set" then |
| 353 |
head = color_handler(head) |
| 354 |
end |
| 355 |
return head |
| 356 |
end, |
| 357 |
color_callback_name) |
| 358 |
add_to_callback("post_mlist_to_hlist_filter", |
| 359 |
function (head, display_type) |
| 360 |
if display_type == "text" then |
| 361 |
return head |
| 362 |
end |
| 363 |
return color_handler(head) |
| 364 |
end, |
| 365 |
color_callback_name) |
| 366 |
color_callback_activated = 1 |
| 367 |
end |
| 368 |
end |
| 369 |
|
| 370 |
--[[doc-- |
| 371 |
``setcolor`` modifies tfmdata.properties.color in place |
| 372 |
--doc]]-- |
| 373 |
|
| 374 |
--- fontobj -> string -> unit |
| 375 |
--- |
| 376 |
--- (where “string” is a rgb value as three octet |
| 377 |
--- hexadecimal, with an optional fourth transparency |
| 378 |
--- value) |
| 379 |
--- |
| 380 |
local glyph_color_tables = { } |
| 381 |
-- Currently this either sets a common color for the whole font or |
| 382 |
-- builds a GID lookup table. This might change later to replace the |
| 383 |
-- lookup table with color information in the character hash. The |
| 384 |
-- problem with that approach right now are differences between harf |
| 385 |
-- and node and difficulties with getting the mapped unicode value for |
| 386 |
-- a GID. |
| 387 |
local function setcolor (tfmdata, value) |
| 388 |
local sanitized_rgb, sanitized_a |
| 389 |
local color_table = glyph_color_tables[tonumber(value) or value] |
| 390 |
if color_table then |
| 391 |
sanitized_rgb = {} |
| 392 |
local unicodes = tfmdata.resources.unicodes |
| 393 |
local gid_mapping = {} |
| 394 |
local descriptions = tfmdata.descriptions or tfmdata.characters |
| 395 |
for color, glyphs in next, color_table do |
| 396 |
for _, glyph in ipairs(glyphs) do |
| 397 |
local gid = glyph == "default" and "default" or tonumber(glyph) |
| 398 |
if not gid then |
| 399 |
local unicode = unicodes[glyph] |
| 400 |
local desc = unicode and descriptions[unicode] |
| 401 |
gid = desc and (desc.index or unicode) |
| 402 |
end |
| 403 |
if gid then |
| 404 |
local a |
| 405 |
sanitized_rgb[gid], a |
| 406 |
= sanitize_color_expression(color) |
| 407 |
if a then |
| 408 |
sanitized_a = sanitized_a or {} |
| 409 |
sanitized_a[gid] = a |
| 410 |
end |
| 411 |
else |
| 412 |
-- TODO: ??? Error out, warn or just ignore? Ignore |
| 413 |
-- makes sense because we have to ignore for GIDs |
| 414 |
-- anyway. |
| 415 |
end |
| 416 |
end |
| 417 |
end |
| 418 |
else |
| 419 |
sanitized_rgb, sanitized_a = sanitize_color_expression(value) |
| 420 |
end |
| 421 |
local properties = tfmdata.properties |
| 422 |
|
| 423 |
if sanitized_rgb then |
| 424 |
properties.color_rgb, properties.color_a = sanitized_rgb, sanitized_a |
| 425 |
add_color_callback() |
| 426 |
end |
| 427 |
end |
| 428 |
|
| 429 |
function luaotfload.add_colorscheme(name, colortable) |
| 430 |
if fonts == nil then |
| 431 |
fonts = name |
| 432 |
name = #glyph_color_tables + 1 |
| 433 |
else |
| 434 |
name = name:lower() |
| 435 |
end |
| 436 |
glyph_color_tables[name] = colortable |
| 437 |
return name |
| 438 |
end |
| 439 |
|
| 440 |
-- cb must have the signature |
| 441 |
-- head, n = cb(head, n, color) |
| 442 |
-- and apply the PDF color operators in color to the node n. |
| 443 |
-- Call with nil to disable. |
| 444 |
function luaotfload.set_colorhandler(cb) |
| 445 |
custom_setcolor = cb |
| 446 |
end |
| 447 |
function luaotfload.set_transparenthandler(cb) |
| 448 |
custom_settransparent = cb |
| 449 |
end |
| 450 |
function luaotfload.set_transparent_colorstack(stack) |
| 451 |
if type(transparent_stack) == 'number' then |
| 452 |
tex.error"luaotfload's transparency stack can't be changed after it has been used" |
| 453 |
else |
| 454 |
local t = type(stack) |
| 455 |
if t == 'function' or t == 'number' then |
| 456 |
function transparent_stack() |
| 457 |
if t == 'function' then |
| 458 |
transparent_stack = stack() |
| 459 |
else |
| 460 |
transparent_stack = stack |
| 461 |
end |
| 462 |
return transparent_stack |
| 463 |
end |
| 464 |
else |
| 465 |
tex.error("Invalid argument in luaotfload.set_transparent_colorstack") |
| 466 |
end |
| 467 |
end |
| 468 |
end |
| 469 |
|
| 470 |
setmetatable(fonts.handlers.otf.statistics.usedfeatures.color, { |
| 471 |
__index = function(t, k) |
| 472 |
t[k] = k |
| 473 |
return k |
| 474 |
end, |
| 475 |
}) |
| 476 |
|
| 477 |
return function () |
| 478 |
assert(logreport == luaotfload.log.report) |
| 479 |
logreport = luaotfload.log.report |
| 480 |
if not fonts then |
| 481 |
logreport ("log", 0, "color", |
| 482 |
"OTF mechanisms missing -- did you forget to \z |
| 483 |
load a font loader?") |
| 484 |
return false |
| 485 |
end |
| 486 |
fonts.handlers.otf.features.register { |
| 487 |
name = "color", |
| 488 |
description = "color", |
| 489 |
initializers = { |
| 490 |
base = setcolor, |
| 491 |
node = setcolor, |
| 492 |
plug = setcolor, |
| 493 |
} |
| 494 |
} |
| 495 |
return true |
| 496 |
end |
| 497 |
|
| 498 |
-- vim:tw=71:sw=4:ts=4:expandtab |
| 499 |
|