Convert font letters to vector format and fill with a pattern

Run it with the sequence latex->dvips->ps2pdf. It does not work with xelatex

\documentclass{article}
\usepackage{pst-text,pst-grad}
\usepackage[tiling]{pst-fill}
\pagestyle{empty}
\begin{document}
\DeclareFixedFont{\pi}{U}{psy}{m}{n}{4mm}% the symbol font
\DeclareFixedFont{\ps}{U}{psy}{m}{n}{12cm}% the symbol font
\DeclareFixedFont{\PS}{T1}{ptm}{m}{n}{5cm}% the times font

\psboxfill{\black\Large\pi p}

\begin{pspicture}(10,6)
\rput(3,3){%
  \pscharpath[fillstyle=solid,fillcolor=blue!20,
    addfillstyle=boxfill,fillangle=20,fillsep=1mm,fillloopaddx=10]{\ps p}}
\rput(8.5,3){%
  \pscharpath[fillstyle=solid,fillcolor=red!40]{\PS Pi}}
\end{pspicture}

\end{document}

enter image description here


TikZ/PGF has no direct access to the font shapes, but once it has been told the paths then it can work with them in exactly the same way as any other path. So whilst PSTricks is probably the better choice if you have a completely free choice, then if you're already using TikZ/PGF in your document you may prefer to use this method even though it is slightly more complicated.

The general idea is to convert a font to PGF paths. This is done via a combination of fontforge and some scripts. It has to be done once for each font, but obviously once it's been done then the results can be cached and reused without that overhead. I've done the conversion for the STIX fonts and the scripts I used are available on the TeX-SX Launchpad Site. Also there are some ideas for a TikZ library to make use of the paths in a "natural" way. If you download pgflibraryletter.shapes.dtx and run tex on it then the following code will work:

\documentclass{article}
%\url{https://tex.stackexchange.com/q/58851/86}
\usepackage{tikz}
\usetikzlibrary{shapes.letters}
\pgfkeys{
  /pgf/letter/.cd,
  load font={stikz}{normal},
  size=4,
  load encoding=char,
}

\begin{document}
\begin{tikzpicture}
\node[draw,fill=blue!40,letter=π] (pi) {};
\node[draw,fill=red!40,letter=P,right of previous letter,] (P) at (pi.east) {};
\node[draw,fill=red!40,letter=i,right of previous letter,] (i) at (P.east) {};
\end{tikzpicture}
\end{document}

and produce the following picture:

TikZ letter shapes

This is by no means stable code - and I've found that doing complicated stuff (such as complicated decorations) on the paths quickly exhausts TeX's limits. It can do kerning, but not ligatures (well, you can always insert them manually but it won't automatically convert them).

This started out life in response to the question that Leo Liu linked to: Outlining (filling) glyph outline with text in TikZ and was also used in https://tex.stackexchange.com/a/51462/86. Contrary to the impression given in some of the comments on the first of these, the paths are faithful representations of the font glyphs in that cubic beziers are used at every stage of the process.


While it is true that tikz has no access to glyph paths, the PDF format allows stroking and clipping (besides filling) with text paths; cf. the use of \pdfliteral in the example below. 2 Tr means "fill, then stroke", and 5 Tr means "stroke text and add to clipping path". The most usual text rendering mode, which is simply filling glyphs, is activated with 0 Tr.

An interface to this functionality would make a nice tikz library...

\documentclass{article}
\usepackage{tikz,txfonts}
\usetikzlibrary{shadings}

\begin{document}    
\begin{tikzpicture}
  \path (0,0) node {
         \Huge
         \pgfsetstrokecolor{red}
         \pgfsetfillcolor{blue}
         \pdfliteral{2 Tr} %Needs pdfTeX
         Hello};

  \path (0,-1) node {
         \Huge
         \pgfsetstrokecolor{red}
         \pdfliteral{5 Tr}
         Hello%
         \tikz[trim left,baseline=1mm]
         \shade[upper left=red,upper right=green,lower left=blue,lower right=yellow]
         (0,0) rectangle (-20mm, 7mm);};
\end{tikzpicture}
\end{document}

Hello