Thanks Taco,

I made some progress :
https://wiki.contextgarden.net/Talk:Cover_Pages

I now have two additional questions ... ;-)

- How can I deal with "space" characters (because current solution
  requires to explicitly add the ~ character)

- What would be the work to do in order to make this mechanism directly
  usable in metapost / metafun, within \startMPpage \stopMPpage,
  - to avoid switching between \start\stopMPdrawing and TeX
  - to have something like "draw followtext" in mp-blob.mpiv ?



%------------------------------------------------------------------------------

\setuppapersize  [A4]

%------------------------------------------------------------------------------

\def\MyFont{\definedfont[name:%
texgyrepagellabold%
%exo2black%
%agaramondprobold%
*default at 55pt]}

%------------------------------------------------------------------------------

\startluacode

function document.outlinepaths(character)
    local chardata  = fonts.hashes.characters[true] -- by unicode
    local shapedata = fonts.hashes.shapes[true] -- by index
    local c         = chardata[character]
    if c and c.index and shapedata then
        local shapeglyphs = shapedata.glyphs or { }
        local glyph       = shapeglyphs[c.index]
        if glyph and (glyph.segments or glyph.sequence) then
            local units  = shapedata.units or 1000
            local factor = 100/units
            return fonts.metapost.paths(glyph,factor)
        end
    end
    return { }
end

function document.defineoutline(char,target)
    local outlines = document.outlinepaths(char)
    local nofpaths = #outlines
context("\\startMPdrawing") -- ADDED by garulfo
    context("path %s[] ;",target)
    context("numeric %sn ; %sn := %s ;",target,target,nofpaths)
    for i=1,nofpaths do
        context("%s[%i] := %s ; ",target,i,outlines[i])
    end
context("\\stopMPdrawing") -- ADDED by garulfo
end

\stopluacode

%------------------------------------------------------------------------------

\def\mpdefineoutlines#1#2{\ctxlua{document.defineoutline(\number`#1,"#2")}}

%------------------------------------------------------------------------------

\def\EmptyLetters#1%
{%
\resetMPdrawing
\MPtoks={}
%
\startMPdrawing
  picture pic[], pictA ;
  numeric wid[], len[], pos[], n ;
  wid[0] := len[0] := pos[0] := n := 0 ;
  numeric scaling;
  path pictB , frame ;
  pair shiftB ;
  picture monImageA; monImageA := nullpicture;
  picture monImageB; monImageB := nullpicture;
  color colorframe;  colorframe := 0.6white;
%
\stopMPdrawing
%
\MyFont\handletokens#1\with\whatever
%
\startMPdrawing
  frame :=  unitsquare
xscaled ( (xpart urcorner monImageB - xpart llcorner monImageB) + 5mm ) yscaled ( (ypart urcorner monImageB - ypart llcorner monImageB) + 5mm );
  frame := frame shifted (center monImageB - center frame);
  addto monImageB contour
        frame
        withpostscript "evenodd"
        withcolor transparent (1,1.,colorframe);
  draw monImageB ;
  draw monImageA ;
\stopMPdrawing
%
\MPdrawingdonetrue
\getMPdrawing}

%------------------------------------------------------------------------------

\def\whatever#1%
  {\appendtoks#1\to\MPtoks
   \setbox\MPbox=\hbox{\MyFont\the\MPtoks}%
   %
   \startMPdrawing
     n := n + 1 ; len[n] := \the\wd\MPbox ;
   \stopMPdrawing
   %
   \mpdefineoutlines{M}{MaLettre}
   %
   \startMPdrawing
   pictA := textext.drt("\MyFont M") ;
   pictB := MaLettre[1];
   scaling := bbheight pictA / bbheight pictB;
   \stopMPdrawing
   %
\doifinstringelse{~}{#1}{}{% HOW TO PROPERLY COPE WITH SPACES ?
   \mpdefineoutlines{#1}{MyGlyph}
   %
   \startMPdrawing
     string sb; sb := "i";
     pic[n] := textext.drt("\MyFont\setstrut\strut#1") ;
     %
     shiftB := - llcorner pic[n];
     pic[n] := pic[n] shifted shiftB ;
     %
     wid[n] := abs(xpart urcorner pic[n] - xpart llcorner pic[n]) ;
     pos[n] := len[n]-wid[n] ;
     %
     addto monImageB contour
           ((MyGlyph[1] scaled scaling)
           shifted ( (pos[n],0) +  shiftB))
           withpostscript "collect";
     %
     if MyGlyphn > 1 :
        for ind=2 upto MyGlyphn :
if (((xpart llcorner MyGlyph[ind]) > (xpart llcorner MyGlyph[1])) and ((ypart llcorner MyGlyph[ind]) > (ypart llcorner MyGlyph[1])) and ((xpart urcorner MyGlyph[ind]) < (xpart urcorner MyGlyph[1])) and ((ypart urcorner MyGlyph[ind]) < (ypart urcorner MyGlyph[1]))) :
                %
                addto monImageA contour
                      (MyGlyph[ind] scaled scaling)
                      shifted ( (pos[n],0) +  shiftB)
                      withcolor transparent (1,1.,colorframe);
            else :
                addto monImageB contour
                      (MyGlyph[ind] scaled scaling)
                      shifted ( (pos[n],0) +  shiftB)
                      withpostscript "collect";
            fi;
         endfor;
      fi;
   \stopMPdrawing}}


%==============================================================================
%==============================================================================

\starttext

%------------------------------------------------------------------------------

\startuseMPgraphic{MyBackgroundGraphic}
  path pathbackground ;
  numeric dimA ; dimA := \the\paperwidth;
  numeric dimB ; dimB := \the\paperheight;
  pathbackground :=  unitsquare xscaled dimA yscaled dimB ;
  fill pathbackground
       withshademethod "linear"
       withshadedirection(0,1)
       withshadecolors (red,blue);
\stopuseMPgraphic

%------------------------------------------------------------------------------

\definelayer[MyBackgroundLayer]
\setlayer[MyBackgroundLayer]{\useMPgraphic{MyBackgroundGraphic}}
\setupbackgrounds[page][background={MyBackgroundLayer}]

%------------------------------------------------------------------------------

\EmptyLetters{MetaPost~is~fun!}
\EmptyLetters{ABCDEFGHIJ}
\EmptyLetters{KLMNOPQRST}
\EmptyLetters{UVWXYZ!?\&*}
\EmptyLetters{abcdefghij}
\EmptyLetters{klmnopqrst}
\EmptyLetters{uvwxyz<>;:@}
\EmptyLetters{0123456789}

%------------------------------------------------------------------------------

\stoptext



Le 21/12/2020 à 16:29, Taco Hoekwater a écrit :


On 21 Dec 2020, at 16:09, Garulfo <garu...@azules.eu> wrote:

Hi,


* I try to "remove the paths of text" from a metapost pictures to let
  the background visible.

* Using
  https://mailman.ntg.nl/pipermail/ntg-context/2018/thread.html#92725

* I was able to get the following with one glyph (MVE code below)

  https://wiki.contextgarden.net/images/d/d2/test_extract_glyphs.pdf


2 questions:

1/ for some fonts (e.g. dejavu vs latin modern) the "encompassing" path
   of the B glyph can switch from B[1] to B[Bn]. Is there a trick to
   identify which id match with "the encompassing" path ?

What you get is the order of the paths as defined in the font, which is 
arbitrary, as you discovered.

A quick trick (that will not always work, but should work quite well for font 
variations of alphabetics) is to check the actual arclength of the paths. The 
longest one will typically be the outer one.

But much depends on the font, so a generic solution is likely quite 
complicated. Consider an outline font’s colon glyph ;)



2/ now, I would like to do the same, but for text... a kind of
   derivative of "textext" or "outlinetext".
   Would you please give me some guidelines ?
        does it already exist ?
        should I use something like \handletokens, \scantokens ?
        do you have examples where I can get inspiration ?


Thanks again for your help,
Garulfo


%==============================================================================




\starttext

%------------------------------------------------------------------------------

\startluacode

function document.outlinepaths(character)
    local chardata  = fonts.hashes.characters[true] -- by unicode
    local shapedata = fonts.hashes.shapes[true] -- by index
    local c         = chardata[character]
    if c and c.index and shapedata then
        local shapeglyphs = shapedata.glyphs or { }
        local glyph       = shapeglyphs[c.index]
        if glyph and (glyph.segments or glyph.sequence) then
            local units  = shapedata.units or 1000
            local factor = 100/units
            return fonts.metapost.paths(glyph,factor)
        end
    end
    return { }
end

function document.defineoutline(char,target)
    local outlines = document.outlinepaths(char)
    local nofpaths = #outlines
    context("path %s[] ;",target)
    context("numeric %sn ; %sn := %s ;",target,target,nofpaths)
    for i=1,nofpaths do
        context("%s[%i] := %s ; ",target,i,outlines[i])
    end
end

\stopluacode

%------------------------------------------------------------------------------

\def\mpdefineoutlines#1#2{\ctxlua{document.defineoutline(\number`#1,"#2")}}

%------------------------------------------------------------------------------

\startMPpage

numeric dimA ; dimA := 20cm;

path pathbackground ;
pathbackground :=  unitsquare scaled dimA ;
fill pathbackground withshademethod "linear" withshadedirection(0,1) 
withshadecolors (red,blue);

%--------------------------------------

\mpdefineoutlines{B}{B}

%--------------------------------------

numeric dimB  ; dimB := 2cm;
numeric vratio ; vratio := 2;

path frame ;
frame :=  unitsquare
          xscaled (dimA - 2 * dimB)
          yscaled (dimA / vratio)
          shifted (dimB , (dimA - (dimA / vratio))/2 );

%--------------------------------------

pair shift ; shift := center frame;
numeric scal ; scal := (0.9) * (10cm / (bbheight B[1])) ;

picture monImageB; monImageB := nullpicture;
addto monImageB contour
      (B[1] shifted (- center B[1]) scaled scal)
      shifted shift
      withpostscript "collect";

addto monImageB contour
      frame
      withpostscript "evenodd";

for i=2 upto Bn :
  addto monImageB contour
        (B[i] shifted (- center B[1]) scaled scal)
        shifted shift;
endfor ;

%--------------------------------------

draw monImageB withcolor white;

\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
___________________________________________________________________________________

Taco Hoekwater
Elvenkind BV




___________________________________________________________________________________
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
___________________________________________________________________________________

___________________________________________________________________________________
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
___________________________________________________________________________________

Reply via email to