6

Stuck in a meeting this morning, I began to doodle on my meeting agenda. Doodlers everywhere will recognize the filled-in letters in the example below:

example 1

I know I've seen font types like this before, but that's not what this question is about. What I want to know is if there's a way to program TeX to do this to any arbitrary font.

Bonus points if you're able to make the color of the bubbles changeable (example below in red).

example 2

g.kov
  • 21,864
  • 1
  • 58
  • 95
grfrazee
  • 969
  • 1
    I don't think this is possible. TeX doesn't know anything about the glyphs a font has. It just knows the size of the box around a glyph. I don't know whether it's possible with LuaTeX though. – Skillmon Dec 08 '17 at 20:25

1 Answers1

12

Perhaps Asymptote, as a TeX friend, can be helpful.

This is a brief MWE:

// fillGlyphHoles.asy
//
// run 
//      asy fillGlyphHoles.asy
//
// to get fillGlyphHoles.pdf

settings.tex="pdflatex";
size(9cm);
import fontsize;defaultpen(fontsize(9pt));
texpreamble("\usepackage{lmodern}"
+"\usepackage{amsmath}"
+"\usepackage{amsfonts}"
+"\usepackage{amssymb}"
);

void fillGlyphsWithHoles(Label L, pen holePen=currentpen, pen glyphPen=currentpen){
    path[] tp=texpath(L); 
    path g; pair v; int nw;
    for(int i=0;i<tp.length;++i){
        g=tp[i];
        if(cyclic(g)){
            v=inside(g,glyphPen);
            nw=windingnumber(tp,v);
            if(!interior(nw,glyphPen)) fill(g,holePen);
        }
    }
    fill(tp,glyphPen);
}

Label[] L={
    Label("\texttt{Demo.}"),
    Label("As any dedicated reader"), 
    Label("can \emph{clearly} see,"),
    Label("$\mathbb{P}_{\alpha}=\frac{\sqrt{q}}{a^8+b^9}.$"),
};
fillGlyphsWithHoles(shift(0,100)*L[0],holePen=orange);
fillGlyphsWithHoles(shift(20,90)*rotate(12)*L[1],holePen=red+opacity(0.5));
fillGlyphsWithHoles(shift(30,80)*rotate(7)*L[2],holePen=green+opacity(0.9));
fillGlyphsWithHoles(shift(0,60)*rotate(7)*L[3],holePen=blue+opacity(0.5),glyphPen=olive);
shipout(bbox(Fill(paleyellow)));

enter image description here

Edit:

With radial shading:

// shadeGlyphHoles.asy
//
// run 
//      asy shadeGlyphHoles.asy
//
// to get shadeGlyphHoles.pdf

settings.tex="pdflatex";
size(9cm);
import fontsize;defaultpen(fontsize(9pt));
texpreamble("\usepackage{lmodern}"
+"\usepackage{amsmath}"
+"\usepackage{amsfonts}"
+"\usepackage{amssymb}"
);

void shadeGlyphsWithHoles(Label L, 
pen holePenA=currentpen, 
pen holePenB=currentpen, 
pen glyphPenA=currentpen,
pen glyphPenB=currentpen){

    path[] tp=texpath(L); 
    path g; pair v; int nw;
    pair a,b;

    for(int i=0;i<tp.length;++i){
        g=tp[i];
        a=min(g); b=max(g);
        b=(a.x,b.y);
        if(cyclic(g)){
            v=inside(g,glyphPenA);
            nw=windingnumber(tp,v);
            if(!interior(nw,glyphPenA)) axialshade(g,holePenA,a,holePenB,b);;
        }
    }
    axialshade(tp,glyphPenA,a,glyphPenB,b);
}

Label L=Label("can \emph{clearly} see,");
shadeGlyphsWithHoles(L,blue+opacity(0.3),white,blue,red+opacity(0.4));
shipout(bbox(Fill(lightgreen)));

enter image description here

Konrad Rudolph
  • 39,394
  • 22
  • 107
  • 160
g.kov
  • 21,864
  • 1
  • 58
  • 95