From 05d201f0870567a0b4f86cf80e0445be2f987dc3 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Mon, 15 Apr 2013 14:26:15 +0200 Subject: prefer locals in font-clr --- otfl-font-clr.lua | 64 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 26 deletions(-) diff --git a/otfl-font-clr.lua b/otfl-font-clr.lua index 9fe2916..439fd7c 100644 --- a/otfl-font-clr.lua +++ b/otfl-font-clr.lua @@ -6,7 +6,15 @@ if not modules then modules = { } end modules ['font-clr'] = { license = "GPL" } -local format = string.format +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 otffeatures = fonts.constructors.newfeatures("otf") local ids = fonts.hashes.identifiers @@ -21,9 +29,9 @@ local function setcolor(tfmdata,value) if #value == 6 or #value == 8 then sanitized = value elseif #value == 7 then - _, _, sanitized = value:find("(......)") + _, _, sanitized = stringfind(value, "(......)") elseif #value > 8 then - _, _, sanitized = value:find("(........)") + _, _, sanitized = stringfind(value, "(........)") else -- broken color code ignored, issue a warning? end @@ -46,9 +54,9 @@ registerotffeature { local function hex2dec(hex,one) if one then - return format("%.1g", tonumber(hex, 16)/255) + return stringformat("%.1g", tonumber(hex, 16)/255) else - return format("%.3g", tonumber(hex, 16)/255) + return stringformat("%.3g", tonumber(hex, 16)/255) end end @@ -59,17 +67,17 @@ local function pageresources(a) if not res then res = "/TransGs1<>" end - res2 = format("/TransGs%s<>", a, a, a) - res = format("%s%s", res, res:find(res2) and "" or res2) + res2 = stringformat("/TransGs%s<>", a, a, a) + res = stringformat("%s%s", res, stringfind(res, res2) and "" or res2) end local function hex_to_rgba(hex) local r, g, b, a, push, pop, res3 if hex then if #hex == 6 then - _, _, r, g, b = hex:find('(..)(..)(..)') + _, _, r, g, b = stringfind(hex, '(..)(..)(..)') elseif #hex == 8 then - _, _, r, g, b, a = hex:find('(..)(..)(..)(..)') + _, _, r, g, b, a = stringfind(hex, '(..)(..)(..)(..)') a = hex2dec(a,true) pageresources(a) end @@ -80,24 +88,24 @@ local function hex_to_rgba(hex) g = hex2dec(g) b = hex2dec(b) if a then - push = format('/TransGs%g gs %s %s %s rg', a, r, g, b) + push = stringformat('/TransGs%g gs %s %s %s rg', a, r, g, b) pop = '0 g /TransGs1 gs' else - push = format('%s %s %s rg', r, g, b) + push = stringformat('%s %s %s rg', r, g, b) pop = '0 g' end return push, pop end -local glyph = node.id('glyph') -local hlist = node.id('hlist') -local vlist = node.id('vlist') -local whatsit = node.id('whatsit') -local pgi = node.id('page_insert') -local sbox = node.id('sub_box') +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') local function lookup_next_color(head) - for n in node.traverse(head) do + 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 @@ -119,7 +127,7 @@ local function lookup_next_color(head) end local function node_colorize(head, current_color, next_color) - for n in node.traverse(head) do + for n in traverse_nodes(head) do if n.id == hlist or n.id == vlist or n.id == sbox 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) @@ -128,19 +136,19 @@ local function node_colorize(head, current_color, next_color) 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 = node.new(whatsit, 8) + local push = newnode(whatsit, 8) push.mode = 1 push.data = pushcolor - head = node.insert_before(head, n, push) + head = insert_node_before(head, n, push) current_color = tfmdata.properties.color 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 = node.new(whatsit, 8) + local pop = newnode(whatsit, 8) pop.mode = 1 pop.data = popcolor - head = node.insert_after(head, n, pop) + head = insert_node_after(head, n, pop) current_color = nil end end @@ -154,11 +162,11 @@ local function font_colorize(head) -- and remove it to avoid duplicating it later if res then local r = "/ExtGState<<"..res..">>" - tex.pdfpageresources = tex.pdfpageresources:gsub(r, "") + tex.pdfpageresources = stringgsub(tex.pdfpageresources, r, "") end local h = node_colorize(head, nil, nil) -- now append our page resources - if res and res:find("%S") then -- test for non-empty string + if res and stringfind(res, "%S") then -- test for non-empty string local r = "/ExtGState<<"..res..">>" tex.pdfpageresources = tex.pdfpageresources..r end @@ -169,7 +177,11 @@ local color_callback_activated = 0 function add_color_callback() if color_callback_activated == 0 then - luatexbase.add_to_callback("pre_output_filter", font_colorize, "loaotfload.colorize") + luatexbase.add_to_callback( + "pre_output_filter", font_colorize, "luaotfload.colorize") color_callback_activated = 1 end end + +-- vim:tw=71:sw=4:ts=4:expandtab + -- cgit v1.2.3