| 1 |
-- |
| 2 |
-- This is file `luacolor.lua', |
| 3 |
-- generated with the docstrip utility. |
| 4 |
-- |
| 5 |
-- The original source files were: |
| 6 |
-- |
| 7 |
-- luacolor.dtx (with options: `lua') |
| 8 |
-- |
| 9 |
-- This is a generated file. |
| 10 |
-- |
| 11 |
-- Project: luacolor |
| 12 |
-- Version: 2023-08-18 v1.18 |
| 13 |
-- |
| 14 |
-- Copyright (C) |
| 15 |
-- 2007, 2009-2011 Heiko Oberdiek |
| 16 |
-- 2016-2023 Oberdiek Package Support Group |
| 17 |
-- |
| 18 |
-- This work may be distributed and/or modified under the |
| 19 |
-- conditions of the LaTeX Project Public License, either |
| 20 |
-- version 1.3c of this license or (at your option) any later |
| 21 |
-- version. This version of this license is in |
| 22 |
-- https://www.latex-project.org/lppl/lppl-1-3c.txt |
| 23 |
-- and the latest version of this license is in |
| 24 |
-- https://www.latex-project.org/lppl.txt |
| 25 |
-- and version 1.3 or later is part of all distributions of |
| 26 |
-- LaTeX version 2005/12/01 or later. |
| 27 |
-- |
| 28 |
-- This work has the LPPL maintenance status "maintained". |
| 29 |
-- |
| 30 |
-- The Current Maintainers of this work are |
| 31 |
-- Heiko Oberdiek and the Oberdiek Package Support Group |
| 32 |
-- https://github.com/ho-tex/luacolor/issues |
| 33 |
-- |
| 34 |
-- This work consists of the main source file luacolor.dtx |
| 35 |
-- and the derived files |
| 36 |
-- luacolor.sty, luacolor.pdf, luacolor.ins, luacolor.drv, |
| 37 |
-- luacolor.lua, |
| 38 |
-- |
| 39 |
oberdiek = oberdiek or {} |
| 40 |
local luacolor = oberdiek.luacolor or {} |
| 41 |
oberdiek.luacolor = luacolor |
| 42 |
function luacolor.getversion() |
| 43 |
tex.write("2023-08-18 v1.18") |
| 44 |
end |
| 45 |
local ifpdf = tonumber(tex.outputmode or tex.pdfoutput) > 0 |
| 46 |
local prefix |
| 47 |
local prefixes = { |
| 48 |
dvips = "color ", |
| 49 |
dvipdfm = "pdf:sc ", |
| 50 |
truetex = "textcolor:", |
| 51 |
pctexps = "ps::", |
| 52 |
} |
| 53 |
local patterns = { |
| 54 |
["^color "] = "dvips", |
| 55 |
["^pdf: *begincolor "] = "dvipdfm", |
| 56 |
["^pdf: *bcolor "] = "dvipdfm", |
| 57 |
["^pdf: *bc "] = "dvipdfm", |
| 58 |
["^pdf: *setcolor "] = "dvipdfm", |
| 59 |
["^pdf: *scolor "] = "dvipdfm", |
| 60 |
["^pdf: *sc "] = "dvipdfm", |
| 61 |
["^textcolor:"] = "truetex", |
| 62 |
["^ps::"] = "pctexps", |
| 63 |
} |
| 64 |
local function info(msg, term) |
| 65 |
local target = "log" |
| 66 |
if term then |
| 67 |
target = "term and log" |
| 68 |
end |
| 69 |
texio.write_nl(target, "Package luacolor info: " .. msg .. ".") |
| 70 |
texio.write_nl(target, "") |
| 71 |
end |
| 72 |
function luacolor.dvidetect() |
| 73 |
local v = tex.box[0] |
| 74 |
assert(v.id == node.id("hlist")) |
| 75 |
for v in node.traverse_id(node.id("whatsit"), v.head) do |
| 76 |
if v and v.subtype == node.subtype("special") then |
| 77 |
local data = v.data |
| 78 |
for pattern, driver in pairs(patterns) do |
| 79 |
if string.find(data, pattern) then |
| 80 |
prefix = prefixes[driver] |
| 81 |
tex.write(driver) |
| 82 |
return |
| 83 |
end |
| 84 |
end |
| 85 |
info("\\special{" .. data .. "}", true) |
| 86 |
return |
| 87 |
end |
| 88 |
end |
| 89 |
info("Missing \\special", true) |
| 90 |
end |
| 91 |
local map = { |
| 92 |
n = 0, |
| 93 |
} |
| 94 |
function luacolor.get(color) |
| 95 |
tex.write("" .. luacolor.getvalue(color)) |
| 96 |
end |
| 97 |
function luacolor.getvalue(color) |
| 98 |
local n = map[color] |
| 99 |
if not n then |
| 100 |
n = map.n + 1 |
| 101 |
map.n = n |
| 102 |
map[n] = color |
| 103 |
map[color] = n |
| 104 |
end |
| 105 |
return n |
| 106 |
end |
| 107 |
local attribute |
| 108 |
function luacolor.setattribute(attr) |
| 109 |
attribute = attr |
| 110 |
end |
| 111 |
function luacolor.getattribute() |
| 112 |
return attribute |
| 113 |
end |
| 114 |
local LIST = 1 |
| 115 |
local LIST_LEADERS = 2 |
| 116 |
local LIST_DISC = 3 |
| 117 |
local COLOR = 4 |
| 118 |
local NOCOLOR = 5 |
| 119 |
local RULE = node.id("rule") |
| 120 |
local node_types = { |
| 121 |
[node.id("hlist")] = LIST, |
| 122 |
[node.id("vlist")] = LIST, |
| 123 |
[node.id("rule")] = COLOR, |
| 124 |
[node.id("glyph")] = COLOR, |
| 125 |
[node.id("disc")] = LIST_DISC, |
| 126 |
[node.id("whatsit")] = { |
| 127 |
[node.subtype("pdf_colorstack")] = |
| 128 |
function(n) |
| 129 |
return n.stack == 0 and NOCOLOR or nil |
| 130 |
end, |
| 131 |
[node.subtype("special")] = COLOR, |
| 132 |
[node.subtype("pdf_literal")] = COLOR, |
| 133 |
[node.subtype("pdf_save")] = COLOR, |
| 134 |
[node.subtype("pdf_restore")] = COLOR, -- probably not needed |
| 135 |
-- TODO (DPC) [node.subtype("pdf_refximage")] = COLOR, |
| 136 |
}, |
| 137 |
[node.id("glue")] = |
| 138 |
function(n) |
| 139 |
if n.subtype >= 100 then -- leaders |
| 140 |
if n.leader.id == RULE then |
| 141 |
return COLOR |
| 142 |
else |
| 143 |
return LIST_LEADERS |
| 144 |
end |
| 145 |
end |
| 146 |
end, |
| 147 |
} |
| 148 |
local function get_type(n) |
| 149 |
local ret = node_types[n.id] |
| 150 |
if type(ret) == 'table' then |
| 151 |
ret = ret[n.subtype] |
| 152 |
end |
| 153 |
if type(ret) == 'function' then |
| 154 |
ret = ret(n) |
| 155 |
end |
| 156 |
return ret |
| 157 |
end |
| 158 |
local mode = 2 -- luatex.pdfliteral.direct |
| 159 |
local WHATSIT = node.id("whatsit") |
| 160 |
local SPECIAL = node.subtype("special") |
| 161 |
local PDFLITERAL = node.subtype("pdf_literal") |
| 162 |
local DRY_FALSE = false |
| 163 |
local DRY_TRUE = true |
| 164 |
local function traverse(list, color, dry) |
| 165 |
if not list then |
| 166 |
return color |
| 167 |
end |
| 168 |
local head |
| 169 |
if get_type(list) == LIST then |
| 170 |
head = list.head |
| 171 |
elseif get_type(list) == LIST_DISC then |
| 172 |
head = list.replace |
| 173 |
else |
| 174 |
texio.write_nl("!!! Error: Wrong list type: " .. node.type(list.id)) |
| 175 |
return color |
| 176 |
end |
| 177 |
for n in node.traverse(head) do |
| 178 |
local t = get_type(n) |
| 179 |
if t == LIST or t == LIST_DISC then |
| 180 |
color = traverse(n, color, dry) |
| 181 |
elseif t == LIST_LEADERS then |
| 182 |
local color_after = traverse(n.leader, color, DRY_TRUE) |
| 183 |
if color == color_after then |
| 184 |
traverse(n.leader, color, DRY_FALSE or dry) |
| 185 |
else |
| 186 |
traverse(n.leader, '', DRY_FALSE or dry) |
| 187 |
color = '' |
| 188 |
end |
| 189 |
elseif t == COLOR then |
| 190 |
local v = node.has_attribute(n, attribute) |
| 191 |
if v then |
| 192 |
local newColor = map[v] |
| 193 |
if newColor ~= color then |
| 194 |
color = newColor |
| 195 |
if dry == DRY_FALSE then |
| 196 |
local newNode |
| 197 |
if ifpdf then |
| 198 |
newNode = node.new(WHATSIT, PDFLITERAL) |
| 199 |
newNode.mode = mode |
| 200 |
newNode.data = color |
| 201 |
else |
| 202 |
newNode = node.new(WHATSIT, SPECIAL) |
| 203 |
newNode.data = prefix .. color |
| 204 |
end |
| 205 |
head = node.insert_before(head, n, newNode) |
| 206 |
end |
| 207 |
end |
| 208 |
end |
| 209 |
elseif t == NOCOLOR then |
| 210 |
color = '' |
| 211 |
end |
| 212 |
end |
| 213 |
if get_type(list) == LIST then |
| 214 |
list.head = head |
| 215 |
else |
| 216 |
list.replace = head |
| 217 |
end |
| 218 |
return color |
| 219 |
end |
| 220 |
function luacolor.process(box) |
| 221 |
local color = "" |
| 222 |
local list = tex.getbox(box) |
| 223 |
traverse(list, color, DRY_FALSE) |
| 224 |
end |
| 225 |
|
| 226 |
if luatexbase.callbacktypes.pre_shipout_filter then |
| 227 |
luatexbase.add_to_callback("pre_shipout_filter", function(list) |
| 228 |
traverse(list, "", DRY_FALSE) |
| 229 |
return true |
| 230 |
end, "luacolor.process") |
| 231 |
end |
| 232 |
if luaotfload.set_colorhandler then |
| 233 |
local set_attribute = node.direct.set_attribute |
| 234 |
luaotfload.set_colorhandler(function(head, n, color) |
| 235 |
set_attribute(n, attribute, luacolor.getvalue(color)) |
| 236 |
return head, n |
| 237 |
end) |
| 238 |
end |
| 239 |
-- |
| 240 |
-- End of File `luacolor.lua'. |