summaryrefslogtreecommitdiff
path: root/src/luaotfload-colors.lua
diff options
context:
space:
mode:
Diffstat (limited to 'src/luaotfload-colors.lua')
-rw-r--r--src/luaotfload-colors.lua130
1 files changed, 74 insertions, 56 deletions
diff --git a/src/luaotfload-colors.lua b/src/luaotfload-colors.lua
index 89884b6..98549c8 100644
--- a/src/luaotfload-colors.lua
+++ b/src/luaotfload-colors.lua
@@ -19,8 +19,7 @@ explanation: http://tug.org/pipermail/luatex/2013-May/004305.html
--doc]]--
-local log = luaotfload.log
-local logreport = log.report
+local logreport = luaotfload and luaotfload.log.report or print
local nodedirect = node.direct
local newnode = nodedirect.new
@@ -44,10 +43,7 @@ local texsettoks = tex.settoks
local texgettoks = tex.gettoks
local stringformat = string.format
-
-local otffeatures = fonts.constructors.newfeatures("otf")
local identifiers = fonts.hashes.identifiers
-local registerotffeature = otffeatures.register
local add_color_callback --[[ this used to be a global‽ ]]
@@ -101,36 +97,6 @@ local sanitize_color_expression = function (digits)
return sanitized
end
---[[doc--
-``setcolor`` modifies tfmdata.properties.color in place
---doc]]--
-
---- fontobj -> string -> unit
----
---- (where “string” is a rgb value as three octet
---- hexadecimal, with an optional fourth transparency
---- value)
----
-local setcolor = function (tfmdata, value)
- local sanitized = sanitize_color_expression(value)
- local properties = tfmdata.properties
-
- if sanitized then
- properties.color = sanitized
- add_color_callback()
- end
-end
-
-registerotffeature {
- name = "color",
- description = "color",
- initializers = {
- base = setcolor,
- node = setcolor,
- }
-}
-
-
--- something is carried around in ``res``
--- for later use by color_handler() --- but what?
@@ -192,6 +158,7 @@ local whatsit_t = nodetype("whatsit")
local disc_t = nodetype("disc")
local pdfliteral_t = node.subtype("pdf_literal")
local colorstack_t = node.subtype("pdf_colorstack")
+local mlist_to_hlist = node.mlist_to_hlist
local color_callback
local color_attr = luatexbase.new_attribute("luaotfload_color_attribute")
@@ -230,8 +197,6 @@ local get_font_color = function (font_id)
return font_color
end
-local cnt = 0
-
--[[doc--
While the second argument and second returned value are apparently
always nil when the function is called, they temporarily take string
@@ -240,26 +205,24 @@ values during the node list traversal.
--- (node * (string | nil)) -> (node * (string | nil))
local node_colorize
-node_colorize = function (head, current_color)
+node_colorize = function (head, toplevel, current_color)
local n = head
while n do
local n_id = getid(n)
if n_id == hlist_t or n_id == vlist_t then
- cnt = cnt + 1
local n_list = getlist(n)
if getattribute(n_list, color_attr) then
if current_color then
head, n, current_color = color_whatsit(head, n, current_color, false)
end
else
- n_list, current_color = node_colorize(n_list, current_color)
+ n_list, current_color = node_colorize(n_list, false, current_color)
if current_color and getsubtype(n) == 1 then -- created by linebreak
n_list, _, current_color = color_whatsit(n_list, nodetail(n_list), current_color, false, true)
end
setfield(n, "head", n_list)
end
- cnt = cnt - 1
elseif n_id == glyph_t then
--- colorization is restricted to those fonts
@@ -303,7 +266,7 @@ node_colorize = function (head, current_color)
n = getnext(n)
end
- if cnt == 0 and current_color then
+ if toplevel and current_color then
head, _, current_color = color_whatsit(head, nodetail(head), current_color, false, true)
end
@@ -314,7 +277,7 @@ end
--- node -> node
local color_handler = function (head)
head = todirect(head)
- head = node_colorize(head)
+ head = node_colorize(head, true)
head = tonode(head)
-- now append our page resources
@@ -351,6 +314,8 @@ local color_handler = function (head)
end
local color_callback_activated = 0
+local add_to_callback = luatexbase.add_to_callback
+local priority_in_callback = luatexbase.priority_in_callback
--- unit -> unit
add_color_callback = function ( )
@@ -360,22 +325,75 @@ add_color_callback = function ( )
end
if color_callback_activated == 0 then
- luatexbase.add_to_callback(color_callback,
- color_handler,
- "luaotfload.color_handler")
- luatexbase.add_to_callback("hpack_filter",
- function (head, groupcode)
- if groupcode == "hbox" or
- groupcode == "adjusted_hbox" or
- groupcode == "align_set" then
- head = color_handler(head)
- end
- return head
- end,
- "luaotfload.color_handler")
+ add_to_callback(color_callback,
+ color_handler,
+ "luaotfload.color_handler")
+ add_to_callback("hpack_filter",
+ function (head, groupcode)
+ if groupcode == "hbox" or
+ groupcode == "adjusted_hbox" or
+ groupcode == "align_set" then
+ head = color_handler(head)
+ end
+ return head
+ end,
+ "luaotfload.color_handler")
+ add_to_callback("mlist_to_hlist",
+ function (head, display_type, need_penalties)
+ if priority_in_callback("mlist_to_hlist","luaotfload.color_handler") == 1 then
+ head = mlist_to_hlist(head, display_type, need_penalties)
+ end
+ if display_type == "text" then
+ return head
+ end
+ return color_handler(head)
+ end,
+ "luaotfload.color_handler")
color_callback_activated = 1
end
end
+--[[doc--
+``setcolor`` modifies tfmdata.properties.color in place
+--doc]]--
+
+--- fontobj -> string -> unit
+---
+--- (where “string” is a rgb value as three octet
+--- hexadecimal, with an optional fourth transparency
+--- value)
+---
+local setcolor = function (tfmdata, value)
+ local sanitized = sanitize_color_expression(value)
+ local properties = tfmdata.properties
+
+ if sanitized then
+ properties.color = sanitized
+ add_color_callback()
+ end
+end
+
+return {
+ init = function ()
+ logreport = luaotfload.log.report
+ if not fonts then
+ logreport ("log", 0, "color",
+ "OTF mechanisms missing -- did you forget to \z
+ load a font loader?")
+ return false
+ end
+ fonts.handlers.otf.features.register {
+ name = "color",
+ description = "color",
+ initializers = {
+ base = setcolor,
+ node = setcolor,
+ }
+ }
+ return true
+ end
+}
+
+
-- vim:tw=71:sw=4:ts=4:expandtab