ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Metafun: Finding intersection between characters
@ 2018-09-22  8:35 Henri Menke
  2018-09-22  9:27 ` Hans Hagen
  0 siblings, 1 reply; 17+ messages in thread
From: Henri Menke @ 2018-09-22  8:35 UTC (permalink / raw)
  To: ntg-context

Dear list,

Challanged by a very old TeX.SX question
https://tex.stackexchange.com/questions/180510
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

---

\startluacode
-- 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 {}
        end
        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
        end
    end
end
\stopluacode

\def\mpdefineoutlines#1#2{\ctxlua{
    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 .. ";")
    end
  }}

\starttext

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

\mpdefineoutlines{B}{B}
\mpdefineoutlines{T}{T}

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];
        forever:
            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 ;
endfor ;
\stopMPpage

\stoptext

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2018-09-28 10:09 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <mailman.3457.1537623691.2042.ntg-context@ntg.nl>
2018-09-25 23:48 ` Metafun: Finding intersection between characters Jeong Dal
2018-09-26  7:59   ` Hans Hagen
2018-09-26 11:44     ` Jeong Dal
2018-09-26 10:10   ` Hans Hagen
2018-09-26 12:14     ` Jeong Dal
2018-09-26 12:24       ` Hans Hagen
2018-09-26 21:38         ` Jeong Dal
2018-09-27  8:49           ` Hans Hagen
2018-09-27 15:06             ` Jeong Dal
2018-09-27 15:43               ` Hans Hagen
2018-09-28 10:09                 ` Jeong Dal
2018-09-22  8:35 Henri Menke
2018-09-22  9:27 ` Hans Hagen
2018-09-22 10:08   ` Floris van Manen
2018-09-22 11:45     ` Hans Hagen
2018-09-22 13:41       ` Alan Braslau
2018-09-22 17:06         ` Hans Hagen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).