From 54c2374cfd769284cc48ac59ddb071f04ee40bd5 Mon Sep 17 00:00:00 2001 From: Karl Berry Date: Tue, 21 May 2013 23:05:34 +0000 Subject: luaotfload (21may13) git-svn-id: svn://tug.org/texlive/trunk@30619 c570f23f-e606-0410-a88d-b1316a301751 --- .../tex/luatex/luaotfload/luaotfload-extralibs.lua | 166 +++++++++---- .../tex/luatex/luaotfload/luaotfload-features.lua | 105 ++++++-- .../luatex/luaotfload/luaotfload-letterspace.lua | 270 +++++++++++++++++++++ .../tex/luatex/luaotfload/luaotfload.lua | 12 +- .../tex/luatex/luaotfload/luaotfload.sty | 2 +- 5 files changed, 492 insertions(+), 63 deletions(-) create mode 100644 Master/texmf-dist/tex/luatex/luaotfload/luaotfload-letterspace.lua (limited to 'Master/texmf-dist/tex/luatex') diff --git a/Master/texmf-dist/tex/luatex/luaotfload/luaotfload-extralibs.lua b/Master/texmf-dist/tex/luatex/luaotfload/luaotfload-extralibs.lua index 618808fdb85..ddf36940a8d 100644 --- a/Master/texmf-dist/tex/luatex/luaotfload/luaotfload-extralibs.lua +++ b/Master/texmf-dist/tex/luatex/luaotfload/luaotfload-extralibs.lua @@ -1,5 +1,5 @@ if not modules then modules = { } end modules ["extralibs"] = { - version = 2.200, + version = 2.201, comment = "companion to luaotfload.lua", author = "Hans Hagen, Philipp Gesang", copyright = "PRAGMA ADE / ConTeXt Development Team", @@ -23,6 +23,7 @@ local texattribute = tex.attribute local new_node = node.new local copy_node = node.copy +local otffeatures = fonts.constructors.newfeatures "otf" ----------------------------------------------------------------------- --- namespace @@ -38,8 +39,10 @@ local kerns = typesetters.kerns kerns.mapping = kerns.mapping or { } kerns.factors = kerns.factors or { } -local namespace = "kern" --- <= attribute <= plugin.name -local callback_name = "typesetters.kerncharacters" +local kern_callback = "typesetters.kerncharacters" + +typesetters.kernfont = typesetters.kernfont or { } +local kernfont = typesetters.kernfont ----------------------------------------------------------------------- --- node-ini @@ -207,6 +210,7 @@ if not markdata then fonthashes.marks = markdata end +--- next stems from the multilingual interface interfaces = interfaces or { } interfaces.variables = interfaces.variables or { } interfaces.variables.max = "max" @@ -281,9 +285,10 @@ commands = commands or { } --- LOAD --===================================================================-- ---- we should be ready at this moment to insert the library +--- we should be ready at this moment to insert the libraries -require "luaotfload-typo-krn" +require "luaotfload-typo-krn" --- typesetters.kerns +require "luaotfload-letterspace" --- typesetters.kernfont --===================================================================-- --- CLEAN @@ -296,67 +301,146 @@ local mapping = kerns.mapping local unsetvalue = attributes.unset_value local process_kerns = plugin_store.kern -local kerncharacters = function (head) - return process_kerns("kerns", hidden.a_kerns, head) -end +--- kern_callback : normal +--- · callback: process_kerns +--- · enabler: enablecharacterkerning +--- · disabler: disablecharacterkerning +--- · interface: kerns.set + +--- kernfont_callback : fontwise +--- · callback: kernfont.handler +--- · enabler: enablefontkerning +--- · disabler: disablefontkerning --- callback wrappers -local add_kern_processor = function (...) - for i=1, select("#", ...) do - luatexbase.add_to_callback( - select(i, ...), kerncharacters, callback_name - ) + +--- (node_t -> node_t) -> string -> string list -> bool +local registered_as = { } --- procname -> callbacks +local add_processor = function (processor, name, ...) + local callbacks = { ... } + for i=1, #callbacks do + luatexbase.add_to_callback(callbacks[i], processor, name) end + registered_as[name] = callbacks --- for removal + return true end -local remove_kern_processor = function (...) - for i=1, select("#", ...) do - luatexbase.remove_from_callback( - select(i, ...), kerncharacters, callback_name - ) + +--- string -> bool +local remove_processor = function (name) + local callbacks = registered_as[name] + if callbacks then + for i=1, #callbacks do + luatexbase.remove_from_callback(callbacks[i], name) + end + return true end + return false --> unregistered end --- we use the same callbacks as a node processor in Context -kerns.enablecharacterkerning = function ( ) - add_kern_processor("pre_linebreak_filter", "hpack_filter") +--- unit -> bool +local enablecharacterkerning = function ( ) + return add_processor(function (head) + return process_kerns("kerns", hidden.a_kerns, head) + end, + "typesetters.kerncharacters", + "pre_linebreak_filter", "hpack_filter" + ) end -kerns.disablecharacterkerning = function ( ) - remove_kern_processor("pre_linebreak_filter", "hpack_filter") +--- unit -> bool +local disablecharacterkerning = function ( ) + return remove_processor "typesetters.kerncharacters" end -local enabled = false --- callback state +kerns.enablecharacterkerning = enablecharacterkerning +kerns.disablecharacterkerning = disablecharacterkerning + +--- now for the simplistic variant +--- unit -> bool +local enablefontkerning = function ( ) + return add_processor( + kernfont.handler, + "typesetters.kernfont", + "pre_linebreak_filter", "hpack_filter" + ) +end ---- we just replace the kern enabler with our modified version -kerns.set = function (factor) - if factor ~= v_max then +--- unit -> bool +local disablefontkerning = function ( ) + return remove_processor "typesetters.kernfont" +end + +--- fontwise kerning uses a font property for passing along the +--- letterspacing factor + +local fontkerning_enabled = false --- callback state + +--- fontobj -> float -> unit +local initializefontkerning = function (tfmdata, factor) + if factor ~= "max" then factor = tonumber(factor) or 0 end - if factor == v_max or factor ~= 0 then - if not enabled then - kerns.enablecharacterkerning() - enabled = true + if factor == "max" or factor ~= 0 then + local fontproperties = tfmdata.properties + if fontproperties then + --- hopefully this field stays unused otherwise + fontproperties.kerncharacters = factor end - local a = factors[factor] - if not a then - a = #mapping + 1 - factors[factors], mapping[a] = a, factor + if not fontkerning_enabled then + fontkerning_enabled = enablefontkerning() end - factor = a - else - factor = unsetvalue end - texattribute[hidden.a_kerns] = factor - return factor end +--- like the font colorization, fontwise kerning is hooked into the +--- feature mechanism + +otffeatures.register { + name = "letterspace", --"kerncharacters", + description = "letterspace", --"kerncharacters", + initializers = { + base = initializefontkerning, + node = initializefontkerning, + } +} + +kerns.set = nil + +local characterkerning_enabled = false + +kerns.set = function (factor) + if factor ~= "max" then + factor = tonumber(factor) or 0 + end + if factor == "max" or factor ~= 0 then + if not characterkerning_enabled then + enablecharacterkerning() + characterkerning_enabled = true + end + local a = factors[factor] + if not a then + a = #mapping + 1 + factors[factors], mapping[a] = a, factor + end + factor = a + else + factor = unsetvalue + end + texattribute[hidden.a_kerns] = factor + return factor +end + + ----------------------------------------------------------------------- --- options ----------------------------------------------------------------------- -kerns.keepligature = false --- supposed to be of type function -kerns.keeptogether = false --- supposed to be of type function +kerns .keepligature = false --- supposed to be of type function +kerns .keeptogether = false --- supposed to be of type function +kernfont.keepligature = false --- supposed to be of type function +kernfont.keeptogether = false --- supposed to be of type function ----------------------------------------------------------------------- --- erase fake Context layer diff --git a/Master/texmf-dist/tex/luatex/luaotfload/luaotfload-features.lua b/Master/texmf-dist/tex/luatex/luaotfload/luaotfload-features.lua index f5419640f86..9713f95b3fd 100644 --- a/Master/texmf-dist/tex/luatex/luaotfload/luaotfload-features.lua +++ b/Master/texmf-dist/tex/luatex/luaotfload/luaotfload-features.lua @@ -1,13 +1,14 @@ if not modules then modules = { } end modules ["features"] = { - version = 2.200, + version = 2.201, comment = "companion to luaotfload.lua", author = "Hans Hagen, Khaled Hosny, Elie Roux, Philipp Gesang", copyright = "PRAGMA ADE / ConTeXt Development Team", license = "see context related readme files" } -local format, insert = string.format, table.insert local type, next = type, next +local tonumber = tonumber +local tostring = tostring local lpegmatch = lpeg.match ---[[ begin included font-ltx.lua ]] @@ -28,14 +29,15 @@ function fonts.definers.getspecification(str) return "", str, "", ":", str end -local old_feature_list = { } - local report = logs.names_report local stringfind = string.find local stringlower = string.lower local stringgsub = string.gsub +local stringsub = string.sub +local stringformat = string.format local stringis_empty = string.is_empty +local mathceil = math.ceil --- TODO an option to dump the default features for a script would make --- a nice addition to luaotfload-tool @@ -752,6 +754,7 @@ local support_incomplete = table.tohash({ --- (string, string) dict -> (string, string) dict local set_default_features = function (speclist) speclist = speclist or { } + speclist[""] = nil --- invalid options stub --- handle language tag local language = speclist.language @@ -864,13 +867,65 @@ end --- ----------------------------------------------------------------------- +--[[doc-- + + One further incompatibility between Xetex and Luatex-Fonts consists + in their option list syntax: apparently, Xetex requires key-value + options to be prefixed by a "+" (ascii “plus”) character. We + silently accept this as well, dropping the first byte if it is a + plus or minus character. + + Reference: https://github.com/lualatex/luaotfload/issues/79#issuecomment-18104483 + +--doc]]-- + +local handle_normal_option = function (key, val) + val = stringlower(val) + --- the former “toboolean()” handler + if val == "true" then + val = true + elseif val == "false" then + val = false + end + return key, val +end + +--[[doc-- + + Xetex style indexing begins at zero which we just increment before + passing it along to the font loader. Ymmv. + +--doc]]-- + +local handle_xetex_option = function (key, val) + val = stringlower(val) + local numeric = tonumber(val) --- decimal only; keeps colors intact + if numeric then --- ugh + if mathceil(numeric) == numeric then -- integer, possible index + val = tostring(numeric + 1) + end + elseif val == "true" then + val = true + elseif val == "false" then + val = false + end + return key, val +end + +--[[doc-- + + Instead of silently ignoring invalid options we emit a warning to + the log. + + Note that we have to return a pair to please rawset(). This creates + an entry on the resulting features hash which will later be removed + during set_default_features(). + +--doc]]-- -local toboolean = function (s) - if s == "true" then return true end - if s == "false" then return false end ---if s == "yes" then return true end --- Context style ---if s == "no" then return false end - return stringlower(s) +local handle_invalid_option = function (opt) + report("log", 0, "load", "font option “%s” unknown.", opt) + return "", false end --[[doc-- @@ -953,17 +1008,28 @@ local unprefixed = Cg(fontname, "anon") local path_lookup = lbrk * Cg(C((1-rbrk)^1), "path") * rbrk --- features ---------------------------------------------------------- -local field = (anum + S"+-.")^1 --- sic! +local field_char = anum + S"+-." --- sic! +local field = field_char^1 --- assignments are “lhs=rhs” +--- or “+lhs=rhs” (Xetex-style) --- switches are “+key” | “-key” -local assignment = C(field) * ws * equals * ws * (field / toboolean) +local normal_option = C(field) * ws * equals * ws * C(field) * ws +local xetex_option = P"+" * ws * normal_option +local assignment = xetex_option / handle_xetex_option + + normal_option / handle_normal_option +----- assignment = (field / strip_leading_sign) * ws +----- * equals * ws +----- * (field / toboolean) local switch = P"+" * ws * C(field) * Cc(true) + P"-" * ws * C(field) * Cc(false) - + C(field) * Cc(true) -- catch crap +-- + C(field) * Cc(true) -- catch crap +local ignore = (1 - featuresep)^1 --- ignores one option + / handle_invalid_option local feature_expr = ws * Cg(assignment + switch) * ws +local option = feature_expr + ignore local feature_list = Cf(Ct"" - * feature_expr - * (featuresep * feature_expr)^0 + * option + * (featuresep * option)^0 , rawset) * featuresep^-1 @@ -978,7 +1044,6 @@ local feature_list = Cf(Ct"" --- string; I won’t mess with it though until someone reports a --- problem.) --- local subvalue = P("(") * (C(P(1-S("()"))^1)/issub) * P(")") -- for Kim ---- Who’s Kim? --- Note to self: subfonts apparently start at index 0. Tested with --- Cambria.ttc that includes “Cambria Math” at 0 and “Cambria” at 1. --- Other values cause luatex to segfault. @@ -1089,8 +1154,8 @@ local handle_request = function (specification) specification.lookup = "path" return specification end - local lookup, name = select_lookup(request) - request.features = set_default_features(request.features) + local lookup, name = select_lookup(request) + request.features = set_default_features(request.features) if name then specification.name = name @@ -1207,11 +1272,11 @@ local function addfeature(data,feature,specifications) local featuretype = types[specification.type or "substitution"] local featureflags = specification.flags or noflags local added = false - local featurename = format("ctx_%s_%s",feature,s) + local featurename = stringformat("ctx_%s_%s",feature,s) local st = { } for t=1,#subtables do local list = subtables[t] - local full = format("%s_%s",featurename,t) + local full = stringformat("%s_%s",featurename,t) st[t] = full if featuretype == "gsub_ligature" then lookuptypes[full] = "ligature" diff --git a/Master/texmf-dist/tex/luatex/luaotfload/luaotfload-letterspace.lua b/Master/texmf-dist/tex/luatex/luaotfload/luaotfload-letterspace.lua new file mode 100644 index 00000000000..8566c06c963 --- /dev/null +++ b/Master/texmf-dist/tex/luatex/luaotfload/luaotfload-letterspace.lua @@ -0,0 +1,270 @@ +if not modules then modules = { } end modules ['letterspace'] = { + version = 2.201, + comment = "companion to luaotfload.lua", + author = "Hans Hagen, PRAGMA-ADE, Hasselt NL; adapted by Philipp Gesang", + copyright = "PRAGMA ADE / ConTeXt Development Team", + license = "see context related readme files" +} + +local next = next +local nodes, node, fonts = nodes, node, fonts + +local find_node_tail = node.tail or node.slide +local free_node = node.free +local free_nodelist = node.flush_list +local copy_node = node.copy +local copy_nodelist = node.copy_list +local insert_node_before = node.insert_before +local insert_node_after = node.insert_after + +local nodepool = nodes.pool +local tasks = nodes.tasks + +local new_kern = nodepool.kern +local new_glue = nodepool.glue + +local nodecodes = nodes.nodecodes +local kerncodes = nodes.kerncodes +local skipcodes = nodes.skipcodes + +local glyph_code = nodecodes.glyph +local kern_code = nodecodes.kern +local disc_code = nodecodes.disc +local glue_code = nodecodes.glue +local hlist_code = nodecodes.hlist +local vlist_code = nodecodes.vlist +local math_code = nodecodes.math + +local kerning_code = kerncodes.kerning +local userkern_code = kerncodes.userkern + +local fonthashes = fonts.hashes +local chardata = fonthashes.characters +local quaddata = fonthashes.quads + +typesetters = typesetters or { } +local typesetters = typesetters + +typesetters.kernfont = typesetters.kernfont or { } +local kernfont = typesetters.kernfont + +kernfont.keepligature = false +kernfont.keeptogether = false + +local kern_injector = function (fillup,kern) + if fillup then + local g = new_glue(kern) + local s = g.spec + s.stretch = kern + s.stretch_order = 1 + return g + else + return new_kern(kern) + end +end + +--[[doc-- + + Caveat lector. + This is a preliminary, makeshift adaptation of the Context + character kerning mechanism that emulates XeTeX-style fontwise + letterspacing. Note that in its present state it is far inferior to + the original, which is attribute-based and ignores font-boundaries. + Nevertheless, due to popular demand the following callback has been + added. It should not be relied upon to be present in future + versions. + +--doc]]-- + +local kernfactors = { } --- fontid -> factor + +local kerncharacters +kerncharacters = function (head) + local start, done = head, false + local lastfont = nil + local keepligature = kernfont.keepligature --- function + local keeptogether = kernfont.keeptogether --- function + local fillup = false + + local identifiers = fonthashes.identifiers + local kernfactors = kernfactors + + while start do + local attr = start[attribute] + local id = start.id + if id == glyph_code then + + --- 1) look up kern factor (slow, but cached rudimentarily) + local krn + local fontid = start.font + do + krn = kernfactors[fontid] + if not krn then + local tfmdata = identifiers[fontid] + if not tfmdata then -- unsafe + tfmdata = font.fonts[fontid] + end + if tfmdata then + fontproperties = tfmdata.properties + if fontproperties then + krn = fontproperties.kerncharacters + end + end + kernfactors[fontid] = krn + end + if not krn or krn == 0 then + goto nextnode + end + end + + if krn == "max" then + krn = .25 + fillup = true + else + fillup = false + end + + lastfont = fontid + + --- 2) resolve ligatures + local c = start.components + if c then + if keepligature and keepligature(start) then + -- keep 'm + else + c = kerncharacters (c) + local s = start + local p, n = s.prev, s.next + local tail = find_node_tail(c) + if p then + p.next = c + c.prev = p + else + head = c + end + if n then + n.prev = tail + end + tail.next = n + start = c + s.components = nil + -- we now leak nodes ! + -- free_node(s) + done = true + end + end -- kern ligature + + --- 3) apply the extra kerning + local prev = start.prev + if prev then + local pid = prev.id + + if not pid then + -- nothing + + elseif pid == kern_code then + if prev.subtype == kerning_code or prev[a_fontkern] then + if keeptogether and prev.prev.id == glyph_code and keeptogether(prev.prev,start) then + -- keep + else + prev.subtype = userkern_code + prev.kern = prev.kern + quaddata[lastfont]*krn -- here + done = true + end + end + + elseif pid == glyph_code then + if prev.font == lastfont then + local prevchar, lastchar = prev.char, start.char + if keeptogether and keeptogether(prev,start) then + -- keep 'm + elseif identifiers[lastfont] then + local kerns = chardata[lastfont][prevchar].kerns + local kern = kerns and kerns[lastchar] or 0 + krn = kern + quaddata[lastfont]*krn -- here + insert_node_before(head,start,kern_injector(fillup,krn)) + done = true + end + else + krn = quaddata[lastfont]*krn -- here + insert_node_before(head,start,kern_injector(fillup,krn)) + done = true + end + + elseif pid == disc_code then + -- a bit too complicated, we can best not copy and just calculate + -- but we could have multiple glyphs involved so ... + local disc = prev -- disc + local pre, post, replace = disc.pre, disc.post, disc.replace + local prv, nxt = disc.prev, disc.next + + if pre and prv then -- must pair with start.prev + -- this one happens in most cases + local before = copy_node(prv) + pre.prev = before + before.next = pre + before.prev = nil + pre = kerncharacters (before) + pre = pre.next + pre.prev = nil + disc.pre = pre + free_node(before) + end + + if post and nxt then -- must pair with start + local after = copy_node(nxt) + local tail = find_node_tail(post) + tail.next = after + after.prev = tail + after.next = nil + post = kerncharacters (post) + tail.next = nil + disc.post = post + free_node(after) + end + + if replace and prv and nxt then -- must pair with start and start.prev + local before = copy_node(prv) + local after = copy_node(nxt) + local tail = find_node_tail(replace) + replace.prev = before + before.next = replace + before.prev = nil + tail.next = after + after.prev = tail + after.next = nil + replace = kerncharacters (before) + replace = replace.next + replace.prev = nil + after.prev.next = nil + disc.replace = replace + free_node(after) + free_node(before) + elseif identifiers[lastfont] then + if prv and prv.id == glyph_code and prv.font == lastfont then + local prevchar, lastchar = prv.char, start.char + local kerns = chardata[lastfont][prevchar].kerns + local kern = kerns and kerns[lastchar] or 0 + krn = kern + quaddata[lastfont]*krn -- here + else + krn = quaddata[lastfont]*krn -- here + end + disc.replace = kern_injector(false,krn) -- only kerns permitted, no glue + end + + end + end + end + + ::nextnode:: + if start then + start = start.next + end + end + return head, done +end + +kernfont.handler = kerncharacters + +--- vim:sw=2:ts=2:expandtab:tw=71 + diff --git a/Master/texmf-dist/tex/luatex/luaotfload/luaotfload.lua b/Master/texmf-dist/tex/luatex/luaotfload/luaotfload.lua index dbecad210e0..f7f016d4755 100644 --- a/Master/texmf-dist/tex/luatex/luaotfload/luaotfload.lua +++ b/Master/texmf-dist/tex/luatex/luaotfload/luaotfload.lua @@ -37,7 +37,7 @@ config.luaotfload.color_callback = config.luaotfload.color_callback or "pre_l luaotfload.module = { name = "luaotfload", version = 2.2, - date = "2013/05/18", + date = "2013/05/20", description = "OpenType layout system.", author = "Elie Roux & Hans Hagen", copyright = "Elie Roux", @@ -77,6 +77,16 @@ if tex.luatexversion < luatex_version then warning("LuaTeX v%.2f is old, v%.2f is recommended.", tex.luatexversion/100, luatex_version /100) + --- we install a fallback for older versions as a safety + if not node.end_of_math then + local math_t = node.id"math" + local traverse_nodes = node.traverse_id + node.end_of_math = function (n) + for n in traverse_nodes(math_t, n.next) do + return n + end + end + end end diff --git a/Master/texmf-dist/tex/luatex/luaotfload/luaotfload.sty b/Master/texmf-dist/tex/luatex/luaotfload/luaotfload.sty index 637e91b97fe..df7a747db22 100644 --- a/Master/texmf-dist/tex/luatex/luaotfload/luaotfload.sty +++ b/Master/texmf-dist/tex/luatex/luaotfload/luaotfload.sty @@ -29,7 +29,7 @@ \else \NeedsTeXFormat{LaTeX2e} \ProvidesPackage{luaotfload}% - [2013/05/18 v2.2 OpenType layout system] + [2013/05/20 v2.2c OpenType layout system] \RequirePackage{luatexbase} \fi \ifnum\luatexversion<76 -- cgit v1.2.3