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:

PdfType3Font t3 = new PdfType3Font("DemoT3");
t3.Size = 24;
t3.FirstChar = (byte)' ';
t3.LastChar = (byte)'9';
t3.FontMatrix = new PdfMatrix(0.01, 0, 0, 0.01, 0, 0);
double[] 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 };
t3.Widths = widths;
Dim t3 As New PdfType3Font("DemoT3")
t3.Size = 24
t3.FirstChar = CByte(" "C)
t3.LastChar = CByte("9"C)
t3.FontMatrix = New PdfMatrix(0.01, 0, 0, 0.01, 0, 0)
Dim widths As Double() = 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}
t3.Widths = widths

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.

PdfPen hollowPen = new PdfPen(null, 8);
PdfBrush hollowBrush = new PdfBrush(null);
// space
PdfType3Glyph t3s = new PdfType3Glyph(0x20, new PdfSize(100, 100));
t3.Glyphs.Add(t3s);
// 1
PdfType3Glyph t31 = new PdfType3Glyph(0x31, new PdfSize(100, 100));
t31.Graphics.DrawRectangle(hollowPen, 5, 5, 90, 90);
t31.Graphics.DrawEllipse(hollowBrush, 40, 40, 20, 20);
t31.Graphics.CompressAndClose();
t3.Glyphs.Add(t31);
Dim hollowPen As New PdfPen(Nothing, 8)
Dim hollowBrush As New PdfBrush(Nothing)
' space
Dim t3s As New PdfType3Glyph(&H20, New PdfSize(100, 100))
t3.Glyphs.Add(t3s)
' 1
Dim t31 As New PdfType3Glyph(&H31, New PdfSize(100, 100))
t31.Graphics.DrawRectangle(hollowPen, 5, 5, 90, 90)
t31.Graphics.DrawEllipse(hollowBrush, 40, 40, 20, 20)
t31.Graphics.CompressAndClose()
t3.Glyphs.Add(t31)

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:

PdfBrush paleVioletRedbrush = new PdfBrush(PdfRgbColor.PaleVioletRed);
page.Graphics.DrawString("0 1 2 3 4 5 6 7 8 9", t3, paleVioletRedbrush, 50, 150);
PdfBrush midnightBluebrush = new PdfBrush(PdfRgbColor.MidnightBlue);
page.Graphics.DrawString("0 1 2 3 4 5 6 7 8 9", t3, midnightBluebrush, 50, 200);
Dim paleVioletRedbrush As New PdfBrush(PdfRgbColor.PaleVioletRed)
page.Graphics.DrawString("0 1 2 3 4 5 6 7 8 9", t3, paleVioletRedbrush, 50, 150)
Dim midnightBluebrush As New PdfBrush(PdfRgbColor.MidnightBlue)
page.Graphics.DrawString("0 1 2 3 4 5 6 7 8 9", t3, midnightBluebrush, 50, 200)

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.

%d bloggers like this: