Text Drawing & Font Handling
December 17, 2006

Introduction

SuperGraphics strives to be a generalized, platform independent, graphics API — rendering text is a natural part of that functionality. With simpler graphics systems, such as QuickDraw, drawing text was just a matter of providing the name of the font you wanted to draw with, setting some parameters for bold, italic, etc., then drawing your string at an X,Y coordinate. Abstracting text rendering for multiple, modern graphics systems is a complicated problem because these systems support many different features in different ways.

SuperGraphics takes a high-level approach and still tries to keep things simple. However, because of such complexities, SuperGraphics currently only supports the drawing of text in a single font or style. This will change in future releases of SuperGraphics as functionality is expanded to support styled or RTF text.

The actual text drawing functions include the drawing of: text lines, text lines with optional wrapping, and text boxes (which confine the visible lines of text to inside a box). Text may be rotated in 90 increments, have custom line spacing (leading), and be aligned left, right, or center. SuperGraphics even supports hyphenation through its own implementation of the TeX hyphenation algorithm, allowing identical behavior across platforms.

Style information is supported by drawing with the appropriate font in a font family, i.e., The Helvetica family includes: Helvetica Regular, Helvetica Bold, etc.. Future versions of SuperGraphics may include generic boolean properties for “Bold” and “Italic”, but the current system is the most flexible because it is unambiguous and allows for styles that may not have been accounted for, such as: “Medium,” “Heavy,” and “Black.”

Text drawing is supported by the following set of classes: SuperGraphicsFontManager, SuperGraphicsFontFamily, SuperGraphicsFont, and SuperGraphicsFontGlyph.

Fonts

Before text is ever rendered, SuperGraphics needs to know which fonts are available. We’ve tried to give you as much flexibility as possible in accessing them: First, SuperGraphics can load font definitions at runtime from the system the SuperGraphics application is running on.

The SuperGraphicsFontManager class can build a font table that stores and organizes fonts by their font family. This organization scheme is the same one used in advance graphics applications such as Adobe Illustrator; it is quite different from the ‘flat’ list of fonts that is returned by REALbasic’s built in Font() function.

Once these font definitions have been loaded, you specify the font your applications wants to draw with by passing in the Full Font Name, i.e., “Helvetica Bold.” In the future, SuperGraphicsFontManager could easily support the retrieval of a font by its alternate name, i.e., its PostScript Name or its QuickDraw Name. Currently, SuperGraphicsFontManager only supports the gathering of fonts and font families while running on OS X.

The second option for gathering fonts allows you to create and load special font definition files that SuperGraphics supports. To create these files, SuperGraphics includes a small utility window that runs on OS X which can load fonts and font families from the system its running on:




With this utility, you can choose the fonts you want your SuperGraphics application to have access to and export data about them to an XML-based Font Definition File. More than just the font name and family is exported: The XML definition file includes metrics information that is needed by SuperGraphics to calculate typographic and optical text sizes for all glyphs, word widths and to perform line wraps.

The XML definition file stores all the information the base SuperGraphics class needs to perform text drawing without actually storing the fonts themselves, which make them completely legal to distribute because they do not actually contain the copyrighted fonts themselves. These definition files can travel with your application and provide font information to your SuperGraphics application at runtime, on any system, whether or not it has the fonts installed, and no matter the operating system.

For example, it is this system and platform independent functionality that allows the SuperGraphicsSVG implementation to ‘draw’ text and calculate word wraps and line breaks in exactly the same way no matter which platform its running on! The actually font file is only needed when the generated SVG file is actual rasterized, which, in an automation or production environment, might occur at a later time on a machine running a different operating system.

In the event that the actual font files are needed to rasterize the text in the SVG document, SVG supports the embedding of fonts. SuperGraphicsSVG can easily be extended to embed these fonts if an SVG version of the font is available. Please note that many TrueType and PostScript fonts are copyrighted and that converting them to SVG format and distributing them may be illegal depending on the license accompanying each particular font.

Drawing

When it comes time to handling drawing commands, the core SuperGraphics class handles all the calculations for word wrap, hyphenation, line spacing, and placement, ensuring that text is drawn equally across any implementation.

The base SuperGraphics class also lets the SuperGraphics implementor (i.e., SuperGraphicsSVG or SuperGraphicsCoreGraphics) receive notification at the start and end of text block drawing so it can handle any extra work required if text block entities are supported by the implementation.

Otherwise, the only work that needs to be done by a SuperGraphics implementation, be it SVG or PDF or any future implementation created by you or other developers, is to draw a single line of text at an X,Y location with the appropriate font at the appropriate size; this simple function is called repeatedly by the base SuperGraphics class in the event of multi-line drawing.