From af9ddc141efa0d7cd645f23a875a45d800c120fb Mon Sep 17 00:00:00 2001 From: Khaled Hosny Date: Fri, 12 Feb 2010 15:08:12 +0200 Subject: Rename |otfl-font-msc.lua| to |otfl-font-clr.lua| It deals with colors only now. --- README | 6 +- luaotfload.dtx | 2 +- otfl-font-clr.lua | 168 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ otfl-font-msc.lua | 166 ----------------------------------------------------- 4 files changed, 172 insertions(+), 170 deletions(-) create mode 100644 otfl-font-clr.lua delete mode 100644 otfl-font-msc.lua diff --git a/README b/README index 1b4dd8d..e04b106 100644 --- a/README +++ b/README @@ -57,7 +57,7 @@ Source files: otfl-node-ini.lua . otfl-node-inj.lua . otfl-node-res.lua OTF support files from the ConTeXt distribution - otfl-node-msc.lua luaotfload module for additional features + otfl-node-clr.lua luaotfload module for font color support Derived files: luaotfload.pdf documentation @@ -77,13 +77,13 @@ starts by: Copyright 2008 PRAGMA ADE / ConTeXt Development Team " -for otf-*.lua files except otfl-font-msc.lua, and +for otf-*.lua files except otfl-font-clr.lua, and " Copyright 2010 Khaled Hosny " -for otfl-font-msc.lua, and continues by: +for otfl-font-clr.lua, and continues by: " This program is free software; you can redistribute it and/or diff --git a/luaotfload.dtx b/luaotfload.dtx index a155ffa..52320be 100644 --- a/luaotfload.dtx +++ b/luaotfload.dtx @@ -505,7 +505,7 @@ end luaotfload.loadmodule('font-xtx.lua') luaotfload.loadmodule('font-map.lua') luaotfload.loadmodule('font-dum.lua') -luaotfload.loadmodule('font-msc.lua') +luaotfload.loadmodule('font-clr.lua') % \end{macrocode} % diff --git a/otfl-font-clr.lua b/otfl-font-clr.lua new file mode 100644 index 0000000..622c9cf --- /dev/null +++ b/otfl-font-clr.lua @@ -0,0 +1,168 @@ +if not modules then modules = { } end modules ['font-clr'] = { + version = 1.001, + comment = "companion to font-otf.lua (font color)", + author = "Khaled Hosny", + copyright = "Khaled Hosny", + license = "GPL" +} + +fonts.triggers = fonts.triggers or { } +fonts.initializers = fonts.initializers or { } +fonts.initializers.common = fonts.initializers.common or { } + +local initializers, format = fonts.initializers, string.format + +table.insert(fonts.triggers,"color") + +function initializers.common.color(tfmdata,value) + if value then + tfmdata.color = value + luaotfload.add_color_callback() + end +end + +initializers.base.otf.color = initializers.common.color +initializers.node.otf.color = initializers.common.color + +local function hex2dec(hex,one) + if one then + return format("%.1g", tonumber(hex, 16)/255) + else + return format("%.3g", tonumber(hex, 16)/255) + end +end + +local res + +local function pageresources(a) + local res2 + 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) +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('(..)(..)(..)') + elseif #hex == 8 then + _, _, r, g, b, a = hex:find('(..)(..)(..)(..)') + a = hex2dec(a,true) + pageresources(a) + end + else + return nil + end + r = hex2dec(r) + g = hex2dec(g) + b = hex2dec(b) + if a then + push = format('/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) + 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 function lookup_next_color(head) + for n in node.traverse(head) do + if n.id == glyph then + if fonts.ids[n.font] and fonts.ids[n.font].color then + return fonts.ids[n.font].color + else + return -1 + end + elseif n.id == vlist or n.id == hlist or n.id == sbox 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 + return -1 + end + end + return nil +end + +function luaotfload.node_colorize(head, current_color, next_color) + for n in node.traverse(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 = luaotfload.node_colorize(n.list, current_color, next_color_in) + elseif n.id == glyph then + local tfmdata = fonts.ids[n.font] + if tfmdata and tfmdata.color then + if tfmdata.color ~= current_color then + local pushcolor = hex_to_rgba(tfmdata.color) + local push = node.new(whatsit, 8) + push.mode = 1 + push.data = pushcolor + head = node.insert_before(head, n, push) + current_color = tfmdata.color + end + local next_color_in = lookup_next_color (n.next) or next_color + if next_color_in ~= tfmdata.color then + local _, popcolor = hex_to_rgba(tfmdata.color) + local pop = node.new(whatsit, 8) + pop.mode = 1 + pop.data = popcolor + head = node.insert_after(head, n, pop) + current_color = nil + end + end + end + end + return head, current_color +end + +function luaotfload.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 = tex.pdfpageresources:gsub(r, "") + end + local h = luaotfload.node_colorize(head, nil, nil) + -- now append our page resources + if res and res:find("%S") then -- test for non-empty string + local r = "/ExtGState<<"..res..">>" + tex.pdfpageresources = tex.pdfpageresources..r + end + return h +end + +luaotfload.color_callback_activated = 0 +local message_displayed = 0 + +function luaotfload.add_color_callback() + if tex.luatexversion < 44 then + if message_displayed == 0 then + luatextra.module_warning("luaotfload","You must have a LuaTeX with version >= 0.44 in order to get colors working, colors won't work.") + message_displayed = 1 + end + elseif luaotfload.color_callback_activated == 0 then + callback.add("pre_output_filter", luaotfload.colorize, "loaotfload.colorize") + luaotfload.color_callback_activated = 1 + end +end + +function luaotfload.remove_color_callback() + if luaotfload.color_callback_activated == 1 then + callback.remove("pre_output_filter", "loaotfload.colorize") + luaotfload.color_callback_activated = 0 + end +end diff --git a/otfl-font-msc.lua b/otfl-font-msc.lua deleted file mode 100644 index d7cfe20..0000000 --- a/otfl-font-msc.lua +++ /dev/null @@ -1,166 +0,0 @@ -if not modules then modules = { } end modules ['font-msc'] = { - version = 1.001, - comment = "companion to font-otf.lua (slanting, extending, colors)", - author = "Khaled Hosny", - copyright = "Khaled Hosny", - license = "GPL" -} - ---[[ - Support for font color. ---]] - -table.insert(fonts.triggers,"color") - -function initializers.common.color(tfmdata,value) - if value then - tfmdata.color = value - luaotfload.add_color_callback() - end -end - -initializers.base.otf.color = initializers.common.color -initializers.node.otf.color = initializers.common.color - -local function hex2dec(hex,one) - if one then - return format("%.1g", tonumber(hex, 16)/255) - else - return format("%.3g", tonumber(hex, 16)/255) - end -end - -local res - -local function pageresources(a) - local res2 - 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) -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('(..)(..)(..)') - elseif #hex == 8 then - _, _, r, g, b, a = hex:find('(..)(..)(..)(..)') - a = hex2dec(a,true) - pageresources(a) - end - else - return nil - end - r = hex2dec(r) - g = hex2dec(g) - b = hex2dec(b) - if a then - push = format('/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) - 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 function lookup_next_color(head) - for n in node.traverse(head) do - if n.id == glyph then - if fonts.ids[n.font] and fonts.ids[n.font].color then - return fonts.ids[n.font].color - else - return -1 - end - elseif n.id == vlist or n.id == hlist or n.id == sbox 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 - return -1 - end - end - return nil -end - -function luaotfload.node_colorize(head, current_color, next_color) - for n in node.traverse(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 = luaotfload.node_colorize(n.list, current_color, next_color_in) - elseif n.id == glyph then - local tfmdata = fonts.ids[n.font] - if tfmdata and tfmdata.color then - if tfmdata.color ~= current_color then - local pushcolor = hex_to_rgba(tfmdata.color) - local push = node.new(whatsit, 8) - push.mode = 1 - push.data = pushcolor - head = node.insert_before(head, n, push) - current_color = tfmdata.color - end - local next_color_in = lookup_next_color (n.next) or next_color - if next_color_in ~= tfmdata.color then - local _, popcolor = hex_to_rgba(tfmdata.color) - local pop = node.new(whatsit, 8) - pop.mode = 1 - pop.data = popcolor - head = node.insert_after(head, n, pop) - current_color = nil - end - end - end - end - return head, current_color -end - -function luaotfload.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 = tex.pdfpageresources:gsub(r, "") - end - local h = luaotfload.node_colorize(head, nil, nil) - -- now append our page resources - if res and res:find("%S") then -- test for non-empty string - local r = "/ExtGState<<"..res..">>" - tex.pdfpageresources = tex.pdfpageresources..r - end - return h -end - -luaotfload.color_callback_activated = 0 -local message_displayed = 0 - -function luaotfload.add_color_callback() - if tex.luatexversion < 44 then - if message_displayed == 0 then - luatextra.module_warning("luaotfload","You must have a LuaTeX with version >= 0.44 in order to get colors working, colors won't work.") - message_displayed = 1 - end - elseif luaotfload.color_callback_activated == 0 then - callback.add("pre_output_filter", luaotfload.colorize, "loaotfload.colorize") - luaotfload.color_callback_activated = 1 - end -end - -function luaotfload.remove_color_callback() - if luaotfload.color_callback_activated == 1 then - callback.remove("pre_output_filter", "loaotfload.colorize") - luaotfload.color_callback_activated = 0 - end -end -- cgit v1.2.3