A free font collection for Pharo

The transition to Athens, the new vectorial canvas in Pharo, requires also a transition from bitmap fonts to free type fonts. There are quite a number of free fonts available these days, but they are distributed as files and this is less convenient for a Pharo image.

To fix the problem, I created a little project:

Gofer new
smalltalkhubUser: 'girba' project: 'FreeFonts';
package: 'FontInstaller';
load.

The project contains the FontInstaller class (did I mention it was small?) that imports TTF fonts from the file system and installs them in dedicated classes in the image.

Let’s take a look at an example. To use the FontInstaller all you need are the font files. In our example, I downloaded the Source Code Pro font and unzip the contents in the Fonts folder next to the Pharo image:

| fontDirectory |
fontDirectory := FileSystem workingDirectory / 'Fonts' /
'SourceCodePro_FontsOnly-1.017'.
FontInstaller new
installFromDirectory: fontDirectory / 'TTF'
url: 'http://sourceforge.net/projects/sourcecodepro.adobe/'
license: ((fontDirectory / 'LICENSE.txt') readStreamDo: #contents).

Executing this code results in the creation of several packages, each of them containing exactly one class. For example, the regular font ends up in the SourceCodeProRegular class that is placed in a package with the same name. Fonts can add a significant size to the image, and users might want to load only a selected set from a font family. Having a one font per package mapping makes it easier to customize loading.

The font is stored as a byte array in the fontContents, and it also has an install convenience method that stores it in the font provider. The license and url are placed in the class comment, while the name of the original file (used for traceability purposes) is stored in another method.

Following this scheme, I created a repository (http://www.smalltalkhub.com/#!/~girba/FreeFonts) with fonts from five font families:

For example, suppose you want to install the Source Code Pro Regular as a code font in your Pharo image. In this case, you need to load only the SourceCodeProRegular package and install it. The code might look like this:

Gofer new
smalltalkhubUser: 'girba' project: 'FreeFonts';
package: 'SourceCodeProRegular';
load.
(Smalltalk at: #SourceCodeProRegular) new install.
FreeTypeSystemSettings loadFt2Library: true.
StandardFonts codeFont: (LogicalFont
familyName: 'Source Code Pro'
pointSize: 10).

All in all, we got one step closer to adopting Athens.

Posted by Tudor Girba at 11 May 2013, 12:12 am link