Fonts are electronic data files containing a set of glyphs, characters, and symbols. A typical font image contains the characters (and sizes) used in a project. Fonts are imported into the project as source code and compiled during the project build. Imported fonts can be stored in program memory or placed into external nonvolatile memory.
Fonts are created with font editors and may be considered works of art. Pre-created fonts are available from many sources, but may be licensed. It is not uncommon for fonts to be copyrighted.
This tutorial shows how to import and work with fonts located in program memory. Most of the MLA graphics projects are built with an imported default font so there is no need to import a font to begin displaying messages. The code for the default font is located in the files:
The Graphics Resource Converter utility (GRC) is used to import fonts. The GRC can import a font already existing on the developer's computer or can import a font from one of several font format files.
The GRC is located in the MLA Installation directory:
Linux and Apple OSX users launch the resource converter by executing grc.jar, and Microsoft Windows users invoke it by executing launch_grc.bat
Once the GRC has been used to import a font, the associated assembly (.s), C (.c), and header files (.h) are added to the open MLA project. Including the font files in the project will allow user's program in the project to access the font.
Inside the header (.h) and the C (.c) file are the definitions of the stucture containing the font. The assembly file (.s) contains the bitmaps of the characters. The name of the structure in the C (.c) file is taken from the font being imported. If you want to change the name of the font, you will need to edit both the header (.h) and C (.c) files. For this example we are using the font from the step-by-step instruction on importing fonts. The font name is Miriam_Fixed_Bold_18. The font has been saved to MyNewFont.s, MyNewFont_reference.c and MyNewFont.h.
Example of MPLAB®X IDE's project tree with the assembly (s.), header (.h), and C (.c) files added.
Basic Functions to use fonts and output text
GFX_FontSet(pFont) - Sets the font to be used in the application. The pointer must point to the font image in the imported font file.
GFX_TextStringDraw( x, y, pFont, length) Outputs the text string pointed to by pFont starting at the location specified by x and y. Outputs the number of characters specified by length. If length = 0 the string will be rendered until the null character is reached.
Example of GFX_SetFont and GFX_TextStringDraw used to draw a horizontal "Microchip"
Functions used to manipulate text
GFX_TextSringHeightGet( pFont) - Returns the height, in pixels, of the string pointed to by pFont. Requires the font to have been set with GFX_FontSet.
GFX_TextStrightWidthGet( textstring, pFont) - Returns the width, in pixels, of the string pointed to by "textstring" if drawn with the font designated by pFont.
GFX_TextCursoPositionSet(x, y) - Sets the drawing cursor to the location specified by X and Y.
GFX_TextCharDraw( ch) - Outputs the one character designated by ch at the current cursor position.
Example of using GFx_TextStringHeightGet, GFX_TextCursorPositionSet, and GFX_TextCharDraw to vertically output "Graphics"
Note that the two example programs demonstrating fonts can be reproduced by modifying the main() function in the MLA project "primitive_layer"