Type3 fonts

XFINIUM.PDF 5.2 brings support for creating Type3 fonts and drawing text with them. The PRO version bring support for rendering Type3 fonts in PDF files.

Type3 fonts are fully defined as PDF objects, their glyph description is not defined in an external file. This allows to use any PDF drawing primitives to create the glyph appearance.
We start by creating a PdfType3Font object:

The FirstChar and LastChar properties define the range of characters in the font. They represent character codes.
The Widths array specifies the widths of the font glyphs, first element in the array corresponds to FirstChar, last element in the array corresponds to LastChar. The length of the Widths array is LastChar - FirstChar + 1.
These widths are interpreted in glyph space as specified by FontMatrix. For the ease of creating the glyph descriptions, we use a 100*100 grid and we scale it to glyph space using the FontMatrix property.
This definition:
FontMatrix = new PdfMatrix(0.01, 0, 0, 0.01, 0, 0);
widths = new double[] { 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 };

is equivalent to:
FontMatrix = new PdfMatrix(1, 0, 0, 1, 0, 0);
widths = new double[] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };

After creating the font we start creating each glyph description (for simplicity we show the creation of glyphs for ‘space’ and ‘1’ but the full code is available for download at the end of the article).
The font creates glyph descriptions for characters ‘0’ to ‘9’ that look like dice faces.

Each glyph is represented by a PdfType3Glyph object. When we create a PdfType3Glyph object we have to specify the character code the glyph is associated to and the size of the glyph. The size must use the same scale like the Widths array and the width value provided in the glyph size must match the corresponding value in the Widths array.
The visual appearance of a glyph is created using the Graphics property which lets us draw almost anything on a glyph surface. The glyph surface acts as a mask when the glyph is painted, this is why we use a brush and a pen without color when creating the glyph visual.
After the glyph is created it is added to font’s Glyphs collection.

Text is drawn on the page as usual:

The conversion of characters in the drawn strings to character codes is done by simply casting each character to byte. For characters with ASCII code less than 128 it simply works because the conversion results in character’s ASCII code. If you are using characters above ASCII code 128 it is recommended to embed them in the string as hex code character (‘u0080’ for 128).

The font above generates the output below:

XFINIUM.PDF Type3 fonts sample
XFINIUM.PDF Type3 fonts sample

Sample source code can be downloaded here (the archive contains only the .cs and .vb files), for a full working application please download the SamplesExplorer application (Samples for Standard Edition) for the platform you are interested in.

6 thoughts on “Type3 fonts”

    1. You can either draw text on separate lines or you can draw text in a box a let the library perform the word wrapping for you. The ‘Text’ sample shows this functionality.

  1. my font is “MusiQwik” and I used PdfType3Font musicFont = new PdfType3Font(“MusiQwik”);
    \ it’s working but the character overlaps

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.