From 21b1fb201a2fd9f058c1fee24dbd29df1debe6f7 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Sat, 4 May 2013 11:49:12 +0200 Subject: bump version; add/update attributions --- luaotfload-colors.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'luaotfload-colors.lua') diff --git a/luaotfload-colors.lua b/luaotfload-colors.lua index 3d8bfab..1525214 100644 --- a/luaotfload-colors.lua +++ b/luaotfload-colors.lua @@ -1,9 +1,9 @@ if not modules then modules = { } end modules ['luaotfload-colors'] = { - version = 1.001, + version = 2.200, comment = "companion to luaotfload.lua (font color)", - author = "Khaled Hosny and Elie Roux", + author = "Khaled Hosny, Elie Roux, Philipp Gesang", copyright = "Luaotfload Development Team", - license = "GPL" + license = "GNU GPL v2" } local newnode = node.new -- cgit v1.2.3 From 162e15dcee3f77cb2cda7d01de157e02507b497e Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Sat, 4 May 2013 12:59:43 +0200 Subject: rename glyph type locals and other identifiers --- luaotfload-colors.lua | 70 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 41 insertions(+), 29 deletions(-) (limited to 'luaotfload-colors.lua') diff --git a/luaotfload-colors.lua b/luaotfload-colors.lua index 1525214..d7b84fc 100644 --- a/luaotfload-colors.lua +++ b/luaotfload-colors.lua @@ -6,19 +6,19 @@ if not modules then modules = { } end modules ['luaotfload-colors'] = { license = "GNU GPL v2" } -local newnode = node.new -local nodetype = node.id -local traverse_nodes = node.traverse -local insert_node_before = node.insert_before -local insert_node_after = node.insert_after +local newnode = node.new +local nodetype = node.id +local traverse_nodes = node.traverse +local insert_node_before = node.insert_before +local insert_node_after = node.insert_after -local stringformat = string.format -local stringgsub = string.gsub -local stringfind = string.find +local stringformat = string.format +local stringgsub = string.gsub +local stringfind = string.find -local otffeatures = fonts.constructors.newfeatures("otf") -local ids = fonts.hashes.identifiers -local registerotffeature = otffeatures.register +local otffeatures = fonts.constructors.newfeatures("otf") +local identifiers = fonts.hashes.identifiers +local registerotffeature = otffeatures.register local function setcolor(tfmdata,value) local sanitized @@ -97,29 +97,38 @@ local function hex_to_rgba(hex) return push, pop end -local glyph = nodetype('glyph') -local hlist = nodetype('hlist') -local vlist = nodetype('vlist') -local whatsit = nodetype('whatsit') -local pgi = nodetype('page_insert') -local sbox = nodetype('sub_box') +--- Luatex internal types + +local glyph_t = nodetype("glyph") +local hlist_t = nodetype("hlist") +local vlist_t = nodetype("vlist") +local whatsit_t = nodetype("whatsit") +local page_insert_t = nodetype("page_insert") +local sub_box_t = nodetype("sub_box") local function lookup_next_color(head) for n in traverse_nodes(head) do - if n.id == glyph then - if ids[n.font] and ids[n.font].properties and ids[n.font].properties.color then - return ids[n.font].properties.color + local n_id = n.id + if n_id == glyph_t then + local n_font + if identifiers[n_font] + and identifiers[n_font].properties + and identifiers[n_font].properties.color + then + return identifiers[n.font].properties.color else return -1 end - elseif n.id == vlist or n.id == hlist or n.id == sbox then + + elseif n_id == vlist_t or n_id == hlist_t or n_id == sub_box_t then local r = lookup_next_color(n.list) if r == -1 then return -1 elseif r then return r end - elseif n.id == whatsit or n.id == pgi then + + elseif n_id == whatsit_t or n_id == page_insert_t then return -1 end end @@ -128,15 +137,18 @@ end local function node_colorize(head, current_color, next_color) for n in traverse_nodes(head) do - if n.id == hlist or n.id == vlist or n.id == sbox then + local n_id = n.id + + if n_id == hlist_t or n_id == vlist_t or n_id == sub_box_t then local next_color_in = lookup_next_color(n.next) or next_color n.list, current_color = node_colorize(n.list, current_color, next_color_in) - elseif n.id == glyph then - local tfmdata = ids[n.font] + + elseif n_id == glyph_t then + local tfmdata = identifiers[n.font] if tfmdata and tfmdata.properties and tfmdata.properties.color then if tfmdata.properties.color ~= current_color then local pushcolor = hex_to_rgba(tfmdata.properties.color) - local push = newnode(whatsit, 8) + local push = newnode(whatsit_t, 8) push.mode = 1 push.data = pushcolor head = insert_node_before(head, n, push) @@ -145,7 +157,7 @@ local function node_colorize(head, current_color, next_color) local next_color_in = lookup_next_color (n.next) or next_color if next_color_in ~= tfmdata.properties.color then local _, popcolor = hex_to_rgba(tfmdata.properties.color) - local pop = newnode(whatsit, 8) + local pop = newnode(whatsit_t, 8) pop.mode = 1 pop.data = popcolor head = insert_node_after(head, n, pop) @@ -164,13 +176,13 @@ local function font_colorize(head) local r = "/ExtGState<<"..res..">>" tex.pdfpageresources = stringgsub(tex.pdfpageresources, r, "") end - local h = node_colorize(head, nil, nil) + local new_head = node_colorize(head, nil, nil) -- now append our page resources if res and stringfind(res, "%S") then -- test for non-empty string local r = "/ExtGState<<"..res..">>" tex.pdfpageresources = tex.pdfpageresources..r end - return h + return new_head end local color_callback_activated = 0 -- cgit v1.2.3 From ed0b0f5b4f9cb26c12069146d2c9c3ed990e8478 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Sat, 4 May 2013 13:54:13 +0200 Subject: type annotations for luaotfload-colors --- luaotfload-colors.lua | 91 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 64 insertions(+), 27 deletions(-) (limited to 'luaotfload-colors.lua') diff --git a/luaotfload-colors.lua b/luaotfload-colors.lua index d7b84fc..0bc3f9c 100644 --- a/luaotfload-colors.lua +++ b/luaotfload-colors.lua @@ -20,6 +20,18 @@ 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‽ ]] + +--[[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 function setcolor(tfmdata,value) local sanitized local properties = tfmdata.properties @@ -52,7 +64,15 @@ registerotffeature { } } -local function hex2dec(hex,one) + +--[[doc-- +This converts a single octet into a decimal with three digits of +precision. The optional second argument limits precision to a single +digit. +--doc]]-- + +--- string -> bool? -> float +local function hex_to_dec(hex,one) if one then return stringformat("%.1g", tonumber(hex, 16)/255) else @@ -60,33 +80,41 @@ local function hex2dec(hex,one) end end -local res +--- something is carried around in ``res`` +--- for later use by color_handler() --- but what? +local res --- <- state of what? + +--- float -> unit local function pageresources(a) local res2 if not res then res = "/TransGs1<>" end res2 = stringformat("/TransGs%s<>", a, a, a) - res = stringformat("%s%s", res, stringfind(res, res2) and "" or res2) + res = stringformat("%s%s", + res, + stringfind(res, res2) and "" or res2) end +--- string -> (string * string) local function hex_to_rgba(hex) local r, g, b, a, push, pop, res3 if hex then + --- TODO lpeg this mess if #hex == 6 then _, _, r, g, b = stringfind(hex, '(..)(..)(..)') elseif #hex == 8 then _, _, r, g, b, a = stringfind(hex, '(..)(..)(..)(..)') - a = hex2dec(a,true) + a = hex_to_dec(a,true) pageresources(a) end else return nil end - r = hex2dec(r) - g = hex2dec(g) - b = hex2dec(b) + r = hex_to_dec(r) + g = hex_to_dec(g) + b = hex_to_dec(b) if a then push = stringformat('/TransGs%g gs %s %s %s rg', a, r, g, b) pop = '0 g /TransGs1 gs' @@ -106,6 +134,7 @@ local whatsit_t = nodetype("whatsit") local page_insert_t = nodetype("page_insert") local sub_box_t = nodetype("sub_box") +--- node -> nil | -1 | color‽ local function lookup_next_color(head) for n in traverse_nodes(head) do local n_id = n.id @@ -122,9 +151,7 @@ local function lookup_next_color(head) elseif n_id == vlist_t or n_id == hlist_t or n_id == sub_box_t then local r = lookup_next_color(n.list) - if r == -1 then - return -1 - elseif r then + if r then return r end @@ -135,6 +162,13 @@ local function lookup_next_color(head) return nil end +--[[doc-- +While the second argument and second returned value are apparently +always nil when the function is called, they temporarily take string +values during the node list traversal. +--doc]]-- + +--- node -> string -> int -> (node * string) local function node_colorize(head, current_color, next_color) for n in traverse_nodes(head) do local n_id = n.id @@ -169,28 +203,31 @@ local function node_colorize(head, current_color, next_color) return head, current_color end -local function font_colorize(head) - -- check if our page resources existed in the previous run - -- and remove it to avoid duplicating it later - if res then - local r = "/ExtGState<<"..res..">>" - tex.pdfpageresources = stringgsub(tex.pdfpageresources, r, "") - end - local new_head = node_colorize(head, nil, nil) - -- now append our page resources - if res and stringfind(res, "%S") then -- test for non-empty string - local r = "/ExtGState<<"..res..">>" - tex.pdfpageresources = tex.pdfpageresources..r - end - return new_head +--- node -> node +local function color_handler (head) + -- check if our page resources existed in the previous run + -- and remove it to avoid duplicating it later + if res then + local r = "/ExtGState<<"..res..">>" + tex.pdfpageresources = stringgsub(tex.pdfpageresources, r, "") + end + local new_head = node_colorize(head, nil, nil) + -- now append our page resources + if res and stringfind(res, "%S") then -- test for non-empty string + local r = "/ExtGState<<"..res..">>" + tex.pdfpageresources = tex.pdfpageresources..r + end + return new_head end local color_callback_activated = 0 -function add_color_callback() +--- unit -> unit +function add_color_callback () if color_callback_activated == 0 then - luatexbase.add_to_callback( - "pre_output_filter", font_colorize, "luaotfload.colorize") + luatexbase.add_to_callback("pre_output_filter", + color_handler, + "luaotfload.color_handler") color_callback_activated = 1 end end -- cgit v1.2.3 From 2b455d3b8d0d08374f21c99fb1f7d6bad75df4ca Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Sat, 4 May 2013 14:32:09 +0200 Subject: refactor color expression validation --- luaotfload-colors.lua | 57 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 15 deletions(-) (limited to 'luaotfload-colors.lua') diff --git a/luaotfload-colors.lua b/luaotfload-colors.lua index 0bc3f9c..58089dc 100644 --- a/luaotfload-colors.lua +++ b/luaotfload-colors.lua @@ -15,6 +15,7 @@ local insert_node_after = node.insert_after local stringformat = string.format local stringgsub = string.gsub local stringfind = string.find +local stringsub = string.sub local otffeatures = fonts.constructors.newfeatures("otf") local identifiers = fonts.hashes.identifiers @@ -22,6 +23,43 @@ local registerotffeature = otffeatures.register local add_color_callback --[[ this used to be a global‽ ]] +local lpeg = require"lpeg" +local lpegmatch = lpeg.match +local C, Cg, Ct, P, R, S = lpeg.C, lpeg.Cg, lpeg.Ct, lpeg.P, lpeg.R, lpeg.S + +local digit16 = R("09", "af", "AF") +local octet = C(digit16 * digit16) +local p_rgb = Cg(octet, "red") + * Cg(octet, "green") + * Cg(octet, "blue") +local p_rgba = p_rgb * Cg(octet, "alpha") +local p_color = Ct(p_rgba + p_rgb) + +local old_sanitize_color_expression = function (value) + local sanitized + if value then + value = tostring(value) + local n_value = #value + if n_value == 6 or n_value == 8 then + sanitized = value + elseif n_value == 7 then --> take first six bytes + _, _, sanitized = stringsub(value, 1, 6) + elseif n_value > 8 then --> take first eight bytes + _, _, sanitized = stringsub(value, 1, 8) + else + -- broken color code ignored, issue a warning? + luaotfload.warning( + "“%s” is not a valid rgb[a] color expression", value) + end + end + return sanitized +end + +local sanitize_color_expression = function (digits) + local old = old_sanitize_color_expression(digits) + return old +end + --[[doc-- ``setcolor`` modifies tfmdata.properties.color in place --doc]]-- @@ -32,23 +70,12 @@ local add_color_callback --[[ this used to be a global‽ ]] --- hexadecimal, with an optional fourth transparency --- value) --- -local function setcolor(tfmdata,value) - local sanitized +local function setcolor (tfmdata, value) + print("~~~~~~~~~~~~~") + print(value) + local sanitized = sanitize_color_expression(value) local properties = tfmdata.properties - if value then - value = tostring(value) - if #value == 6 or #value == 8 then - sanitized = value - elseif #value == 7 then - _, _, sanitized = stringfind(value, "(......)") - elseif #value > 8 then - _, _, sanitized = stringfind(value, "(........)") - else - -- broken color code ignored, issue a warning? - end - end - if sanitized then tfmdata.properties.color = sanitized add_color_callback() -- cgit v1.2.3 From 7182e2279ca9acc02195074849eeb9d1e8943fcc Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Sat, 4 May 2013 14:51:26 +0200 Subject: switch to lpeg based color expression matching --- luaotfload-colors.lua | 60 +++++++++++++++++++++++---------------------------- 1 file changed, 27 insertions(+), 33 deletions(-) (limited to 'luaotfload-colors.lua') diff --git a/luaotfload-colors.lua b/luaotfload-colors.lua index 58089dc..5be0a1f 100644 --- a/luaotfload-colors.lua +++ b/luaotfload-colors.lua @@ -23,41 +23,37 @@ local registerotffeature = otffeatures.register local add_color_callback --[[ this used to be a global‽ ]] -local lpeg = require"lpeg" -local lpegmatch = lpeg.match +--[[doc-- +Color string validator / parser. +--doc]]-- + +local lpeg = require"lpeg" +local lpegmatch = lpeg.match local C, Cg, Ct, P, R, S = lpeg.C, lpeg.Cg, lpeg.Ct, lpeg.P, lpeg.R, lpeg.S -local digit16 = R("09", "af", "AF") -local octet = C(digit16 * digit16) -local p_rgb = Cg(octet, "red") - * Cg(octet, "green") - * Cg(octet, "blue") -local p_rgba = p_rgb * Cg(octet, "alpha") -local p_color = Ct(p_rgba + p_rgb) - -local old_sanitize_color_expression = function (value) - local sanitized - if value then - value = tostring(value) - local n_value = #value - if n_value == 6 or n_value == 8 then - sanitized = value - elseif n_value == 7 then --> take first six bytes - _, _, sanitized = stringsub(value, 1, 6) - elseif n_value > 8 then --> take first eight bytes - _, _, sanitized = stringsub(value, 1, 8) - else - -- broken color code ignored, issue a warning? - luaotfload.warning( - "“%s” is not a valid rgb[a] color expression", value) - end - end - return sanitized -end +local digit16 = R("09", "af", "AF") +local octet = C(digit16 * digit16) + +local p_rgb = octet * octet * octet +local p_rgba = p_rgb * octet +local valid_digits = C(p_rgba + p_rgb) -- matches eight or six hex digits + +local p_Crgb = Cg(octet, "red") --- for captures + * Cg(octet, "green") + * Cg(octet, "blue") +local p_Crgba = p_Crgb * Cg(octet, "alpha") +local extract_color = Ct(p_Crgba + p_Crgb) +--- string -> (string | nil) local sanitize_color_expression = function (digits) - local old = old_sanitize_color_expression(digits) - return old + digits = tostring(digits) + local sanitized = lpegmatch(valid_digits, digits) + if not sanitized then + luaotfload.warning( + "“%s” is not a valid rgb[a] color expression", digits) + return nil + end + return sanitized end --[[doc-- @@ -71,8 +67,6 @@ end --- value) --- local function setcolor (tfmdata, value) - print("~~~~~~~~~~~~~") - print(value) local sanitized = sanitize_color_expression(value) local properties = tfmdata.properties -- cgit v1.2.3 From cb31932c3cdc8f5caa42bb179e1b7444508513a4 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Sat, 4 May 2013 15:56:16 +0200 Subject: move rgba converter to lpeg; also some caching --- luaotfload-colors.lua | 105 ++++++++++++++++++++++++++++---------------------- 1 file changed, 59 insertions(+), 46 deletions(-) (limited to 'luaotfload-colors.lua') diff --git a/luaotfload-colors.lua b/luaotfload-colors.lua index 5be0a1f..4121441 100644 --- a/luaotfload-colors.lua +++ b/luaotfload-colors.lua @@ -23,6 +23,21 @@ local registerotffeature = otffeatures.register local add_color_callback --[[ this used to be a global‽ ]] +--[[doc-- +This converts a single octet into a decimal with three digits of +precision. The optional second argument limits precision to a single +digit. +--doc]]-- + +--- string -> bool? -> string +local hex_to_dec = function (hex,one) --- one isn’t actually used anywhere ... + if one then + return stringformat("%.1g", tonumber(hex, 16)/255) + else + return stringformat("%.3g", tonumber(hex, 16)/255) + end +end + --[[doc-- Color string validator / parser. --doc]]-- @@ -38,10 +53,10 @@ local p_rgb = octet * octet * octet local p_rgba = p_rgb * octet local valid_digits = C(p_rgba + p_rgb) -- matches eight or six hex digits -local p_Crgb = Cg(octet, "red") --- for captures - * Cg(octet, "green") - * Cg(octet, "blue") -local p_Crgba = p_Crgb * Cg(octet, "alpha") +local p_Crgb = Cg(octet/hex_to_dec, "red") --- for captures + * Cg(octet/hex_to_dec, "green") + * Cg(octet/hex_to_dec, "blue") +local p_Crgba = p_Crgb * Cg(octet/hex_to_dec, "alpha") local extract_color = Ct(p_Crgba + p_Crgb) --- string -> (string | nil) @@ -66,12 +81,12 @@ end --- hexadecimal, with an optional fourth transparency --- value) --- -local function setcolor (tfmdata, value) +local setcolor = function (tfmdata, value) local sanitized = sanitize_color_expression(value) local properties = tfmdata.properties if sanitized then - tfmdata.properties.color = sanitized + properties.color = sanitized add_color_callback() end end @@ -86,64 +101,62 @@ registerotffeature { } ---[[doc-- -This converts a single octet into a decimal with three digits of -precision. The optional second argument limits precision to a single -digit. ---doc]]-- - ---- string -> bool? -> float -local function hex_to_dec(hex,one) - if one then - return stringformat("%.1g", tonumber(hex, 16)/255) - else - return stringformat("%.3g", tonumber(hex, 16)/255) - end -end - --- something is carried around in ``res`` --- for later use by color_handler() --- but what? local res --- <- state of what? --- float -> unit -local function pageresources(a) +local function pageresources(alpha) local res2 if not res then res = "/TransGs1<>" end - res2 = stringformat("/TransGs%s<>", a, a, a) + res2 = stringformat("/TransGs%s<>", + alpha, alpha, alpha) res = stringformat("%s%s", res, stringfind(res, res2) and "" or res2) end +--- we store results of below color handler as tuples of +--- push/pop strings +local color_cache = { } --- (string, (string * string)) hash_t + --- string -> (string * string) -local function hex_to_rgba(hex) - local r, g, b, a, push, pop, res3 - if hex then - --- TODO lpeg this mess - if #hex == 6 then - _, _, r, g, b = stringfind(hex, '(..)(..)(..)') - elseif #hex == 8 then - _, _, r, g, b, a = stringfind(hex, '(..)(..)(..)(..)') - a = hex_to_dec(a,true) - pageresources(a) - end - else - return nil +local hex_to_rgba = function (digits) + if not digits then + return end - r = hex_to_dec(r) - g = hex_to_dec(g) - b = hex_to_dec(b) - if a then - push = stringformat('/TransGs%g gs %s %s %s rg', a, r, g, b) - pop = '0 g /TransGs1 gs' - else - push = stringformat('%s %s %s rg', r, g, b) - pop = '0 g' + + --- this is called like a thousand times, so some + --- memoizing is in order. + local cached = color_cache[digits] + if not cached then + local push, pop + local rgb = lpegmatch(extract_color, digits) + if rgb.alpha then + pageresources(rgb.alpha) + push = stringformat( + "/TransGs%g gs %s %s %s rg", + rgb.alpha, + rgb.red, + rgb.green, + rgb.blue) + pop = "0 g /TransGs1 gs" + else + push = stringformat( + "%s %s %s rg", + rgb.red, + rgb.green, + rgb.blue) + pop = "0 g" + end + color_cache[digits] = { push, pop } + return push, pop end - return push, pop + + return cached[1], cached[2] end --- Luatex internal types -- cgit v1.2.3 From e40dec48ecdf2fc6a7bbee2078e422f3dbb95f69 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Sat, 4 May 2013 16:33:15 +0200 Subject: improve legibility of node_colorize --- luaotfload-colors.lua | 60 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 23 deletions(-) (limited to 'luaotfload-colors.lua') diff --git a/luaotfload-colors.lua b/luaotfload-colors.lua index 4121441..cd9341e 100644 --- a/luaotfload-colors.lua +++ b/luaotfload-colors.lua @@ -169,9 +169,11 @@ local page_insert_t = nodetype("page_insert") local sub_box_t = nodetype("sub_box") --- node -> nil | -1 | color‽ -local function lookup_next_color(head) +local lookup_next_color +lookup_next_color = function (head) --- paragraph material for n in traverse_nodes(head) do local n_id = n.id + if n_id == glyph_t then local n_font if identifiers[n_font] @@ -202,35 +204,47 @@ always nil when the function is called, they temporarily take string values during the node list traversal. --doc]]-- +local cnt = 0 --- node -> string -> int -> (node * string) -local function node_colorize(head, current_color, next_color) +local node_colorize +node_colorize = function (head, current_color, next_color) for n in traverse_nodes(head) do - local n_id = n.id + local n_id = n.id + local nextnode = n.next if n_id == hlist_t or n_id == vlist_t or n_id == sub_box_t then - local next_color_in = lookup_next_color(n.next) or next_color + local next_color_in = lookup_next_color(nextnode) or next_color n.list, current_color = node_colorize(n.list, current_color, next_color_in) elseif n_id == glyph_t then + cnt = cnt + 1 local tfmdata = identifiers[n.font] + + --- colorization is restricted to those fonts + --- that received the “color” property upon + --- loading (see ``setcolor()`` above) if tfmdata and tfmdata.properties and tfmdata.properties.color then - if tfmdata.properties.color ~= current_color then - local pushcolor = hex_to_rgba(tfmdata.properties.color) - local push = newnode(whatsit_t, 8) - push.mode = 1 - push.data = pushcolor - head = insert_node_before(head, n, push) - current_color = tfmdata.properties.color + local font_color = tfmdata.properties.color +-- luaotfload.info(cnt, utf.char(n.char), n.font, "", font_color) + if fontcolor ~= current_color then + local pushcolor = hex_to_rgba(fontcolor) + local push = newnode(whatsit_t, 8) + push.mode = 1 + push.data = pushcolor + head = insert_node_before(head, n, push) + current_color = fontcolor end - local next_color_in = lookup_next_color (n.next) or next_color - if next_color_in ~= tfmdata.properties.color then - local _, popcolor = hex_to_rgba(tfmdata.properties.color) - local pop = newnode(whatsit_t, 8) - pop.mode = 1 - pop.data = popcolor - head = insert_node_after(head, n, pop) - current_color = nil + local next_color_in = lookup_next_color (nextnode) or next_color + if next_color_in ~= font_color then + local _, popcolor = hex_to_rgba(font_color) + local pop = newnode(whatsit_t, 8) + pop.mode = 1 + pop.data = popcolor + head = insert_node_after(head, n, pop) + current_color = nil end +-- else +-- luaotfload.info(cnt, utf.char(n.char), n.font, "") end end end @@ -238,17 +252,17 @@ local function node_colorize(head, current_color, next_color) end --- node -> node -local function color_handler (head) +local color_handler = function (head) -- check if our page resources existed in the previous run -- and remove it to avoid duplicating it later if res then - local r = "/ExtGState<<"..res..">>" + local r = "/ExtGState<<" .. res .. ">>" tex.pdfpageresources = stringgsub(tex.pdfpageresources, r, "") end local new_head = node_colorize(head, nil, nil) -- now append our page resources if res and stringfind(res, "%S") then -- test for non-empty string - local r = "/ExtGState<<"..res..">>" + local r = "/ExtGState<<" .. res .. ">>" tex.pdfpageresources = tex.pdfpageresources..r end return new_head @@ -257,7 +271,7 @@ end local color_callback_activated = 0 --- unit -> unit -function add_color_callback () +add_color_callback = function ( ) if color_callback_activated == 0 then luatexbase.add_to_callback("pre_output_filter", color_handler, -- cgit v1.2.3 From c50ea5d545136b0140532d552bf0a59ba0398987 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Sat, 4 May 2013 16:59:23 +0200 Subject: fix invalid identifiers --- luaotfload-colors.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'luaotfload-colors.lua') diff --git a/luaotfload-colors.lua b/luaotfload-colors.lua index cd9341e..bc3d46f 100644 --- a/luaotfload-colors.lua +++ b/luaotfload-colors.lua @@ -226,13 +226,13 @@ node_colorize = function (head, current_color, next_color) if tfmdata and tfmdata.properties and tfmdata.properties.color then local font_color = tfmdata.properties.color -- luaotfload.info(cnt, utf.char(n.char), n.font, "", font_color) - if fontcolor ~= current_color then - local pushcolor = hex_to_rgba(fontcolor) + if font_color ~= current_color then + local pushcolor = hex_to_rgba(font_color) local push = newnode(whatsit_t, 8) push.mode = 1 push.data = pushcolor head = insert_node_before(head, n, push) - current_color = fontcolor + current_color = font_color end local next_color_in = lookup_next_color (nextnode) or next_color if next_color_in ~= font_color then -- cgit v1.2.3 From b6df45277d50880e6a66dc93bc19615a7d3310c4 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Sat, 4 May 2013 19:52:03 +0200 Subject: move colorization to ``pre_linebreak_filter`` also introduces a config option: *color_callback* that controls which callback will be activated. set it to pre_output_filter to restore the previous behavior. --- luaotfload-colors.lua | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) (limited to 'luaotfload-colors.lua') diff --git a/luaotfload-colors.lua b/luaotfload-colors.lua index bc3d46f..ec076c2 100644 --- a/luaotfload-colors.lua +++ b/luaotfload-colors.lua @@ -6,6 +6,24 @@ if not modules then modules = { } end modules ['luaotfload-colors'] = { license = "GNU GPL v2" } +--[[doc-- +buggy coloring with the pre_output_filter when expansion is enabled + · tfmdata for different expansion values is split over different objects + · in ``initializeexpansion()``, chr.expansion_factor is set, and only + those characters that have it are affected + · in constructors.scale: chr.expansion_factor = ve*1000 if commented out + makes the bug vanish +--doc]]-- + + +local color_callback = config.luaotfload.color_callback +if not color_callback then + --- maybe this would be better as a method: "early" | "late" + color_callback = "pre_linebreak_filter" +-- color_callback = "pre_output_filter" --- old behavior, breaks expansion +end + + local newnode = node.new local nodetype = node.id local traverse_nodes = node.traverse @@ -225,7 +243,9 @@ node_colorize = function (head, current_color, next_color) --- loading (see ``setcolor()`` above) if tfmdata and tfmdata.properties and tfmdata.properties.color then local font_color = tfmdata.properties.color --- luaotfload.info(cnt, utf.char(n.char), n.font, "", font_color) +-- luaotfload.info( +-- "n: %d; %s; %d %s, %s", +-- cnt, utf.char(n.char), n.font, "", font_color) if font_color ~= current_color then local pushcolor = hex_to_rgba(font_color) local push = newnode(whatsit_t, 8) @@ -243,8 +263,11 @@ node_colorize = function (head, current_color, next_color) head = insert_node_after(head, n, pop) current_color = nil end + -- else --- luaotfload.info(cnt, utf.char(n.char), n.font, "") +-- luaotfload.info( +-- "n: %d; %s; %d %s", +-- cnt, utf.char(n.char), n.font, "") end end end @@ -273,7 +296,7 @@ local color_callback_activated = 0 --- unit -> unit add_color_callback = function ( ) if color_callback_activated == 0 then - luatexbase.add_to_callback("pre_output_filter", + luatexbase.add_to_callback(color_callback, color_handler, "luaotfload.color_handler") color_callback_activated = 1 -- cgit v1.2.3