[NTG-context] MetaFun - "remove the paths of text" from a rectangle to let the background visible
Taco Hoekwater
taco at elvenkind.com
Mon Dec 21 16:29:28 CET 2020
> On 21 Dec 2020, at 16:09, Garulfo <garulfo at 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 at 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
More information about the ntg-context
mailing list