[NTG-context] Metafun: Finding intersection between characters

Henri Menke henrimenke at gmail.com
Sat Sep 22 10:35:13 CEST 2018

Dear list,

Challanged by a very old TeX.SX question
I wanted to calculate all the intersection points between two
characters.  Therefore I ripped off the \showshape macro to load the
outlines from the font and convert them to MetaPost paths.  Then I try
to find all intersections by cutting the path.

It somewhat works but for some reason, in the MWE below two intersection
points are missing.  I also have the feeling that my implementation is
extremely inefficient.  I would very much appreciate some hints by the
MetaPost experts!

Cheers, Henri


-- That's a simple reimplemetation of the showshape macro
function outlinepaths(character)
    local fontid      = font.current()
    local shapedata   = fonts.hashes.shapes[fontid] -- by index
    local chardata    = fonts.hashes.characters[fontid] -- by unicode
    local shapeglyphs = shapedata.glyphs or { }

    character = utf.byte(character)
    local c = chardata[character]
    if c then
        if not c.index then
            return {}
        local glyph = shapeglyphs[c.index]
        if glyph and (glyph.segments or glyph.sequence) then
            local units  = shapedata.units or 1000
            local factor = 100/units
            local paths  = fonts.metapost.paths(glyph,factor)
            return paths

    local char = "\luaescapestring{#1}"
    local outlines = outlinepaths("#2")
    local len = \letterhash outlines
    tex.print("path " .. char .. "[];")
    tex.print(char .. "n := " .. len .. ";")
    for i, path in ipairs(outlines) do
        tex.print(char .. "[" .. i .. "] := " .. path .. ";")


pair shift; shift := (1cm,-1cm);
numeric angle; angle := 5;


nofill B2;
nofill B3;
eofill B1 withcolor .5[blue,white];

fill T1 shifted (shift) rotated (angle) withcolor .5[red,white];

path r;
numeric n; n := 0;
for i = 1 upto Bn:
    for j = 1 upto Tn:
        r := B[i];
            pair q;
            r := r cutbefore (T[j] shifted (shift) rotated (angle));
            exitif length cuttings = 0;
            r := subpath(epsilon, length r) of r;
            q = point 0 of r;
            n := n + 1;
            dotlabel.urt(textext("\tfx" & decimal n), q);
    endfor ;
endfor ;


More information about the ntg-context mailing list