Dear Hans, dear list,

With a Type 1 font whose accented letters are seac composites, LMTX embeds
those letters as empty glyphs. Minimal example, with Bitstream Charter from
TeX Live (bchr8a.pfb and bchr8a.afm next to the file):

\definefont[CH][file:bchr8a.afm at 24pt]
\starttext \CH Hamburg Äé \stoptext

"Hamburg" prints, "Äé" does not; in the embedded CFF the two glyphs are a
width and one hmoveto. The cause seems to be seac() in font-cff.lmt, which
only resets the stack. A CID-keyed CFF cannot keep a seac, so the composite
has to be flattened: the base at the origin, the accent at (adx + sbx -
asb, ady), as the Type 1 spec says. We do this in our setup, and the result
matches FreeType's rendering of the Type 1 original.

Seen with 2026.02.19 / LuaMetaTeX 2.11.08; the code is the same in
2026.10.01. Our Lua function that does the flattening is attached
(seac-flatten-excerpt.lua), with the small script that reads the seac
parameters from the PFB (seac_table.py), in case they are useful.

Best regards,
Dax Kelson
Guru Labs
#!/usr/bin/env python3
"""Generate tex/gl-lmtx-legacy-seac.lua: the seac composites of the legacy Type 1 fonts.

The Zurich PFBs build their accented letters with the Type 1 `seac` operator (base and accent
glyph by StandardEncoding code, accent offset).  pdfTeX embedded those charstrings unchanged and
every PDF viewer draws them.  LMTX converts Type 1 to CFF and writes a seac glyph as an empty
outline (SEAM.md 7.1; measured again in NOTES.md "seac").  gl-lmtx-fonts.lua rebuilds those
glyphs at embedding time from this table, which is read from the PFBs themselves:

  [face] = { [composite] = { sbx, asb, adx, ady, base, accent }, ... }

sbx is the composite's own left side bearing (hsbw).  The accent's origin goes to
(adx + sbx - asb, ady), the Type 1 rule as fontTools' T1 outline extractor implements it.

Usage: seac_table.py PFBDIR... > tex/gl-lmtx-legacy-seac.lua
"""
import glob, os, sys
from fontTools.t1Lib import T1Font
from fontTools.encodings.StandardEncoding import StandardEncoding


def seacs(path):
    f = T1Font(path)
    f.parse()
    cs = f['CharStrings']
    out = {}
    for name in sorted(cs.keys()):
        c = cs[name]
        c.decompile()
        prog = c.program
        if 'seac' not in prog:
            continue
        i = prog.index('seac')
        asb, adx, ady, bchar, achar = prog[i - 5:i]
        j = prog.index('hsbw')
        sbx, wx = prog[j - 2:j]
        out[name] = (sbx, asb, adx, ady, StandardEncoding[bchar], StandardEncoding[achar])
    return out


def main():
    paths = []
    for d in sys.argv[1:]:
        paths.extend(sorted(glob.glob(os.path.join(d, '**', '*.pfb'), recursive=True)))
    print('-- generated by tools/seac_table.py from the legacy PFBs; do not edit')
    print('return {')
    total = 0
    for p in paths:
        face = os.path.splitext(os.path.basename(p))[0].lower()
        s = seacs(p)
        if not s:
            continue
        total += len(s)
        print('  ["%s"] = {' % face)
        for name, (sbx, asb, adx, ady, b, a) in sorted(s.items()):
            print('    ["%s"] = { %s, %s, %s, %s, "%s", "%s" },' % (name, sbx, asb, adx, ady, b, a))
        print('  },')
    print('}')
    print('-- %d composites in %d files' % (total, len(paths)), file=sys.stderr)


if __name__ == '__main__':
    main()
-- Flattening Type 1 seac composites for LMTX's CID-keyed CFF (Guru Labs, 2026).
-- Excerpt from our environment, shared for reference: the base glyph is drawn at the origin and the
-- accent at (adx + sbx - asb, ady), per the Type 1 spec (seac). The (sbx, asb, adx, ady, base, accent)
-- parameters per composite come from the PFB (see seac_table.py); 'streams' is the table that
-- otf.loadstreamdata returns and the backend embeds from.

-- LMTX converts a Type 1 seac glyph to a CFF charstring holding only the width and side bearing,
-- so the accented letter is embedded empty. This rebuilds each composite's Type 2 charstring from the
-- converted base and accent charstrings: the base at the origin, the accent's origin at
-- (adx + sbx - asb, ady), as the Type 1 spec and fontTools define seac. It works in memory on the
-- stream table the backend embeds from (font-shp.lmt loadstreamdata); no font file is changed.
-- legacyseac[face][glyph] = { sbx, asb, adx, ady, basename, accentname }, read from the PFB.
-- "report" and "gl.fonts" are names from our environment; adapt as needed.

local function t2decode(s)
    local t, i, n = { }, 1, #s
    while i <= n do
        local b0 = byte(s,i)
        if b0 == 28 then
            local b1, b2 = byte(s,i+1,i+2)
            local v = b1*256 + b2
            if v >= 32768 then v = v - 65536 end
            t[#t+1] = v
            i = i + 3
        elseif b0 >= 32 and b0 <= 246 then
            t[#t+1] = b0 - 139
            i = i + 1
        elseif b0 >= 247 and b0 <= 250 then
            t[#t+1] = (b0-247)*256 + byte(s,i+1) + 108
            i = i + 2
        elseif b0 >= 251 and b0 <= 254 then
            t[#t+1] = -(b0-251)*256 - byte(s,i+1) - 108
            i = i + 2
        elseif b0 == 255 then
            local b1, b2, b3, b4 = byte(s,i+1,i+4)
            local v = ((b1*256 + b2)*256 + b3)*256 + b4
            if v >= 2147483648 then v = v - 4294967296 end
            t[#t+1] = v / 65536
            i = i + 5
        elseif b0 == 12 then
            t[#t+1] = { 1200 + byte(s,i+1), i, i+1 }
            i = i + 2
        elseif b0 == 10 or b0 == 29 or b0 == 11 or b0 == 19 or b0 == 20 then
            return nil -- subroutines, return, hint masks: not in the converted Type 1 streams
        else
            t[#t+1] = { b0, i, i }
            i = i + 1
        end
    end
    return t
end

local function t2num(v)
    if v ~= floor(v) then
        local f = floor(v * 65536 + 0.5)
        if f < 0 then f = f + 4294967296 end
        return char(255, floor(f/16777216) % 256, floor(f/65536) % 256, floor(f/256) % 256, f % 256)
    elseif v >= -107 and v <= 107 then
        return char(v + 139)
    elseif v >= 108 and v <= 1131 then
        v = v - 108
        return char(floor(v/256) + 247, v % 256)
    elseif v >= -1131 and v <= -108 then
        v = -v - 108
        return char(floor(v/256) + 251, v % 256)
    else
        if v < 0 then v = v + 65536 end
        return char(28, floor(v/256), v % 256)
    end
end

-- first operator: its index, its arguments without the width, the width (or nil)
local function t2head(t)
    local args = { }
    for k = 1, #t do
        local v = t[k]
        if type(v) == "number" then
            args[#args+1] = v
        else
            local op, need = v[1], nil
            if op == 22 or op == 4 then need = 1 elseif op == 21 then need = 2 else return nil end
            local width = nil
            if #args == need + 1 then
                width = table.remove(args,1)
            elseif #args ~= need then
                return nil
            end
            return k, op, args, width
        end
    end
end

gl.fonts.t2decode = t2decode   -- section 2e (gl-lmtx-seac.lua) translates an accent's charstring

-- the width operand of a converted stream (section 2e reads a composite's width from 2b's stream)
function gl.fonts.t2width(s)
    local t = s and t2decode(s)
    if t then
        local k, op, args, width = t2head(t)
        return width
    end
end

-- current point after the whole charstring (Type 2 semantics; closepath 9 does not move it,
-- as in Type 1)
local function t2endpoint(t)
    local x, y, s = 0, 0, { }
    for k = 1, #t do
        local v = t[k]
        if type(v) == "number" then
            s[#s+1] = v
        else
            local op, n = v[1], #s
            if op == 21 then
                x = x + s[n-1] ; y = y + s[n]
            elseif op == 22 then
                x = x + s[n]
            elseif op == 4 then
                y = y + s[n]
            elseif op == 5 or op == 8 or op == 24 or op == 25 then
                for j = 1, n - 1, 2 do x = x + s[j] ; y = y + s[j+1] end
            elseif op == 6 or op == 7 then
                local h = op == 6
                for j = 1, n do
                    if h then x = x + s[j] else y = y + s[j] end
                    h = not h
                end
            elseif op == 27 then -- hhcurveto: dy1? {dxa dxb dyb dxc}+
                local j = 1
                if n % 4 == 1 then y = y + s[1] ; j = 2 end
                while j + 3 <= n do
                    x = x + s[j] + s[j+1] + s[j+3] ; y = y + s[j+2] ; j = j + 4
                end
            elseif op == 26 then -- vvcurveto: dx1? {dya dxb dyb dyc}+
                local j = 1
                if n % 4 == 1 then x = x + s[1] ; j = 2 end
                while j + 3 <= n do
                    y = y + s[j] + s[j+2] + s[j+3] ; x = x + s[j+1] ; j = j + 4
                end
            elseif op == 30 or op == 31 then -- vhcurveto / hvcurveto, alternating, optional last df
                local h, j = op == 31, 1
                while j + 3 <= n do
                    local e = (j + 4 == n) and s[n] or 0
                    if h then
                        x = x + s[j] + s[j+1] + e ; y = y + s[j+2] + s[j+3]
                    else
                        y = y + s[j] + s[j+2] + e ; x = x + s[j+1] + s[j+3]
                    end
                    h = not h
                    j = j + 4
                end
            elseif op == 1235 then -- flex
                for j = 1, 11, 2 do x = x + s[j] ; y = y + s[j+1] end
            elseif op == 1234 then -- hflex
                x = x + s[1] + s[2] + s[4] + s[5] + s[6] + s[7]
            elseif op == 1236 then -- hflex1
                x = x + s[1] + s[3] + s[5] + s[6] + s[7] + s[9]
            elseif op == 1237 then -- flex1
                local dx, dy = 0, 0
                for j = 1, 9, 2 do dx = dx + s[j] ; dy = dy + s[j+1] end
                if math.abs(dx) > math.abs(dy) then x = x + dx + s[11] else y = y + dy + s[11] end
            end
            s = { }
        end
    end
    return x, y
end

local seacdone   = { }   -- stream table -> true
local seacreport = { }   -- face -> number of rebuilt composites
gl.fonts.seacreport = seacreport

function gl.fonts.repairseac(tfmdata,base)
    local composites = legacyseac[base]
    if not composites then
        return
    end
    local data = otf.loadstreamdata and otf.loadstreamdata(tfmdata)
    local streams = data and data.streams
    if not streams then
        report("fatal: no glyph streams for %a, seac composites not repaired",base)
        os.exit(1)
    end
    if seacdone[streams] then
        return
    end
    seacdone[streams] = true
    local pfb   = handlers.pfb or fonts.constructors.handlers.pfb
    local names = pfb.loadvector(tfmdata.properties.filename)
    local index = { }
    for i, n in next, names do
        index[n] = i
    end
    local done, failed = 0, { }
    for name, s in next, composites do
        local sbx, asb, adx, ady, bname, aname = s[1], s[2], s[3], s[4], s[5], s[6]
        local si, bi, ai = index[name], index[bname], index[aname]
        local S, B, A = si and streams[si], bi and streams[bi], ai and streams[ai]
        local tS, tB, tA = S and t2decode(S), B and t2decode(B), A and t2decode(A)
        local kS, _, aS, wS, kB, opB, aB, wB, kA, opA, aA, wA
        if tS and tB and tA then
            -- (separate calls: "x and f()" would keep only the first return value)
            kS, _,   aS, wS = t2head(tS)
            kB, opB, aB, wB = t2head(tB)
            kA, opA, aA, wA = t2head(tA)
        end
        if not (kS and kB and kA and wS and byte(B,#B) == 14 and byte(A,#A) == 14) then
            failed[#failed+1] = name
        else
            -- base: as drawn alone, under the composite's width
            local head = t2num(wS)
            for j = 1, #aB do head = head .. t2num(aB[j]) end
            head = head .. char(opB)
            local bodyB = sub(B, tB[kB][3] + 1, #B - 1)              -- after the first moveto, without endchar
            -- accent: move from the base's end point to the accent's start point
            local ex, ey = t2endpoint(tB)
            local ax0, ay0 = 0, 0
            if opA == 22 then ax0 = aA[1] elseif opA == 4 then ay0 = aA[1] else ax0, ay0 = aA[1], aA[2] end
            local ox, oy = adx + sbx - asb, ady
            local move = t2num(ox + ax0 - ex) .. t2num(oy + ay0 - ey) .. char(21)
            local bodyA = sub(A, tA[kA][3] + 1)                     -- after the first moveto, with endchar
            streams[si] = head .. bodyB .. move .. bodyA
            done = done + 1
        end
    end
    seacreport[base] = done
    if #failed > 0 then
        report("fatal: %s seac composites of %a not repaired: %s",#failed,base,table.concat(failed," "))
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : [email protected] / 
https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

Reply via email to