ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Hans Hagen <j.hagen@xs4all.nl>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>,
	Garulfo <garulfo@azules.eu>
Subject: Re: MetaFun - "remove the paths of text" from a rectangle to let the background visible
Date: Thu, 24 Dec 2020 00:58:02 +0100	[thread overview]
Message-ID: <0c97b00d-4db1-2af1-be40-438a75a49dab@xs4all.nl> (raw)
In-Reply-To: <0f2c9f15-00c8-07d3-9273-60d26fe28cfc@azules.eu>

On 12/24/2020 12:14 AM, Garulfo wrote:
> 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 ?


Maybe pickup some tricks from this:

\starttext

\startMPpage
     StartPage;

         fill Page
            withshademethod "linear"
            withshadedirection(0,1)
            withshadecolors (red, blue) ;

         picture p ; p := lmt_outline [
             text          = "\strut foo f o o",
             drawcolor     = "white",
             rulethickness = .2
         ] ysized 5cm ;

         draw (p shifted - center p) shifted center Page;

     StopPage;
\stopMPpage

\startuseMPgraphic{MyShade}
     fill OverlayBox
        withshademethod "linear"
        withshadedirection(0,1)
        withshadecolors (red, blue) ;
\stopuseMPgraphic

\defineoverlay[MyShade][\useMPgraphic{MyShade}]

\setupbackgrounds[page][background=MyShade]

\startmakeup

     \definedfont[SerifBold*default @ 50pt] \setupinterlinespace

     \defineeffect[MyOutline][alternative=outer,rulethickness=1pt]

     \startcolor[white]
         \starteffect[MyOutline]
             \input jojomayer
         \stopeffect
     \stopcolor

\stopmakeup

\stoptext

> %------------------------------------------------------------------------------ 
> 
> 
> \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 <garulfo@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
> ___________________________________________________________________________________ 
> 


-- 

-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
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
___________________________________________________________________________________

  parent reply	other threads:[~2020-12-23 23:58 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-21 15:09 Garulfo
2020-12-21 15:29 ` Taco Hoekwater
2020-12-23 23:14   ` Garulfo
2020-12-23 23:46     ` Otared Kavian
2020-12-24  0:42       ` Hans Hagen
2020-12-23 23:58     ` Hans Hagen [this message]
2020-12-24  9:02     ` Taco Hoekwater
2020-12-24 17:04       ` Garulfo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0c97b00d-4db1-2af1-be40-438a75a49dab@xs4all.nl \
    --to=j.hagen@xs4all.nl \
    --cc=garulfo@azules.eu \
    --cc=ntg-context@ntg.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).