[latex3-commits] [git/LaTeX3-latex3-luaotfload] dev: Allow GID based coloring with custom tables (4dfabed)
Marcel Fabian Krüger
tex at 2krueger.de
Thu Jan 9 15:34:06 CET 2020
Repository : https://github.com/latex3/luaotfload
On branch : dev
Link : https://github.com/latex3/luaotfload/commit/4dfabed839d04dfc03169d6ba8536842395cf168
>---------------------------------------------------------------
commit 4dfabed839d04dfc03169d6ba8536842395cf168
Author: Marcel Fabian Krüger <tex at 2krueger.de>
Date: Thu Jan 9 15:34:06 2020 +0100
Allow GID based coloring with custom tables
>---------------------------------------------------------------
4dfabed839d04dfc03169d6ba8536842395cf168
src/luaotfload-colors.lua | 66 ++++++++++++++++++++++++++++++++++++------
src/luaotfload-harf-define.lua | 14 +++++++++
2 files changed, 71 insertions(+), 9 deletions(-)
diff --git a/src/luaotfload-colors.lua b/src/luaotfload-colors.lua
index 2538a45..4700e07 100644
--- a/src/luaotfload-colors.lua
+++ b/src/luaotfload-colors.lua
@@ -42,6 +42,7 @@ local tonode = nodedirect.tonode
local setfield = nodedirect.setfield
local getid = nodedirect.getid
local getfont = nodedirect.getfont
+local getchar = nodedirect.getchar
local getlist = nodedirect.getlist
local getsubtype = nodedirect.getsubtype
local getnext = nodedirect.getnext
@@ -198,9 +199,14 @@ color_whatsit = function (head, curr, color, push, tail)
end
-- number -> string | nil
-local get_font_color = function (font_id)
+local get_glyph_color = function (font_id, char)
local tfmdata = identifiers[font_id]
local font_color = tfmdata and tfmdata.properties and tfmdata.properties.color
+ if type(font_color) == "table" then
+ local char_tbl = tfmdata.characters[char]
+ char = char_tbl and (char_tbl.index or char)
+ return char and font_color[char] or font_color.default
+ end
return font_color
end
@@ -235,21 +241,21 @@ node_colorize = function (head, toplevel, current_color)
--- colorization is restricted to those fonts
--- that received the “color” property upon
--- loading (see ``setcolor()`` above)
- local font_color = get_font_color(getfont(n))
- if font_color ~= current_color then
+ local glyph_color = get_glyph_color(getfont(n), getchar(n))
+ if glyph_color ~= current_color then
if current_color then
head, n, current_color = color_whatsit(head, n, current_color, false)
end
- if font_color then
- head, n, current_color = color_whatsit(head, n, font_color, true)
+ if glyph_color then
+ head, n, current_color = color_whatsit(head, n, glyph_color, true)
end
end
if current_color and color_callback == "pre_linebreak_filter" then
local nn = getnext(n)
while nn and getid(nn) == glyph_t do
- local font_color = get_font_color(getfont(nn))
- if font_color == current_color then
+ local glyph_color = get_glyph_color(getfont(nn), getchar(nn))
+ if glyph_color == current_color then
n = nn
else
break
@@ -381,8 +387,41 @@ end
--- hexadecimal, with an optional fourth transparency
--- value)
---
-local setcolor = function (tfmdata, value)
- local sanitized = sanitize_color_expression(value)
+local glyph_color_tables = { }
+-- Currently this either sets a common color for the whole font or
+-- builds a GID lookup table. This might change later to replace the
+-- lookup table with color information in the character hash. The
+-- problem with that approach right now are differences between harf
+-- and node and difficulties with getting the mapped unicode value for
+-- a GID.
+local function setcolor (tfmdata, value)
+ local sanitized
+ local color_table = glyph_color_tables[tonumber(value) or value]
+ if color_table then
+ sanitized = {}
+ local unicodes = tfmdata.resources.unicodes
+ local gid_mapping = {}
+ local descriptions = tfmdata.descriptions or tfmdata.characters
+ for color, glyphs in next, color_table do
+ for _, glyph in ipairs(glyphs) do
+ local gid = glyph == "default" and "default" or tonumber(glyph)
+ if not gid then
+ local unicode = unicodes[glyph]
+ local desc = unicode and descriptions[unicode]
+ gid = desc and (desc.index or unicode)
+ end
+ if gid then
+ sanitized[gid] = sanitize_color_expression(color)
+ else
+ -- TODO: ??? Error out, warn or just ignore? Ignore
+ -- makes sense because we have to ignore for GIDs
+ -- anyway.
+ end
+ end
+ end
+ else
+ sanitized = sanitize_color_expression(value)
+ end
local properties = tfmdata.properties
if sanitized then
@@ -391,6 +430,15 @@ local setcolor = function (tfmdata, value)
end
end
+function luaotfload.add_colorscheme(name, colortable)
+ if fonts == nil then
+ fonts = name
+ name = #glyph_color_tables + 1
+ end
+ glyph_color_tables[name] = colortable
+ return name
+end
+
return function ()
logreport = luaotfload.log.report
if not fonts then
diff --git a/src/luaotfload-harf-define.lua b/src/luaotfload-harf-define.lua
index 67f67ba..3c588c1 100644
--- a/src/luaotfload-harf-define.lua
+++ b/src/luaotfload-harf-define.lua
@@ -193,6 +193,17 @@ local function loadfont(spec)
end
cached.face = hbface
cached.font = hbfont
+ do
+ local nominals = cached.nominals
+ local gid_offset = cached.gid_offset
+ cached.name_to_char = setmetatable({}, {__index = function(t, name)
+ local gid = hbfont:get_glyph_from_name(name)
+ local char = gid and (nominals[gid] or gid_offset + gid)
+ t[name] = char -- ? Do we want this
+ return char
+ end})
+ end
+
return cached
end
@@ -356,6 +367,9 @@ local function scalefont(data, spec)
specification = spec,
shared = {},
properties = {},
+ resources = {
+ unicodes = data.name_to_char,
+ },
}
tfmdata.shared.processes = fonts.handlers.otf.setfeatures(tfmdata, features)
fonts.constructors.applymanipulators("otf", tfmdata, features, false)
More information about the latex3-commits
mailing list