User Tools

Site Tools


Importing Fonts

How can you use your own font?

The PDFtalk library keeps a list of loaded fonts. These are Graphics.Fonts.Font objects readily read from its source file and interpreted as object(s). Since the process takes time, the loaded fonts are cached in the Graphics.Fonts.Font.LoadedFonts shared (class) variable.

Useful methods are:

Graphics.Fonts.Font loadedFonts.             "returns a <Dictionary key: Symbol value: Font>"
Graphics.Fonts.Font fontAt: #Helvetica.      "returns a Graphics.Fonts.StandardFont"
Graphics.Fonts.Font fontAt: #'Times-Roman'   "returns another StandardFont"
Graphics.Fonts.Font resetLoadedFonts.        "clears the shared variable #LoadedFonts"

The method #loadedFonts loads the fonts on demand. When the shared variable LoadedFonts is nil, all class methods are evaluated which are tagged by the #font: pragma.

Package [Fonts Development] adds some import helpers for OpenTalk and Type-1 (CustomFont) Fonts:

Graphics.Fonts.OpenTalk.Font importFile: aFilename in: aPackagenameSymbol.
Graphics.Fonts.OpenTalk.Font importAllIn: aDirectoryName into: aPackagenameSymbol.
Graphics.Fonts.OpenTalk.Font importInstalledWindowsFont: aFontBaseFilename in: aPackagenameSymbol.
Graphics.Fonts.CustomFont import: filenameTrunk as: selectorSymbol.

For example when you add the 'MICRDemoCheckPrixa.otf' OpenType(PS) font file (a demo font for MICR) with:

Graphics.Fonts.OpenType.Font importFile: 'MICRDemoCheckPrixa.otf' asFilename in: #'Fonts Development'.

This creates a class method for Graphics.Fonts.OpenType.Font:

	<font: #MICRDemoCheckPrixa>
	^self file: 'MICRDemoCheckPrixa.otf' contents: (ByteArray fromASCII85String: 

The newly imported Font can be accessed by

Graphics.Fonts.Font fontAt: #MICRDemoCheckPrixa.

and used in PDF for text as usual by its name:

| page |
page := Page newInBounds: (0 @ 0 corner: 165 @ 30) colorspace: DeviceCMYK new render: [:renderer |
	renderer textObjectDo: [
		renderer setFont: #MICRDemoCheckPrixa size: 14.
		renderer add: (NextLineRelative operands: #(10 10)).
		renderer showString: '01234567890 / ; : -']].
page saveAndShowAs: 'demo04b_MICRDemo.pdf'
fontsimport.txt · Last modified: 2023/01/05 20:29 by christian

Except where otherwise noted, content on this wiki is licensed under the following license: Public Domain
Public Domain Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki