Using Fonts in a Graphics Application

This page contains information for the Graphics Library found in Microchip Libraries for Applications (MLA). It is not relevant for the MPLAB® Harmony Graphics Library.


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 non-volatile memory.

Fonts are created with font editors and may be considered works of art. Pre-created fonts are available from many sources but may have a license; 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:

  • internal_resoure.s
  • internal_resource_reference.c
  • internal_resource_reference.h

Importing Fonts

Step-by-Step Instructions:
Importing fonts to an MLA Graphics Project

The Graphics Resource Converter (GRC) utility 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 OS X® users launch the resource converter by executing grc.jar, and Microsoft Windows users invoke it by executing launch_grc.bat.

Using Fonts

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 the 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 front 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: To get a complete list of the functions available for working with fonts and text open GFX_help.png help_mlagfx.jar located in … microchip/mla/vYYYY-MM-DD/doc …
The two example programs demonstrating fonts can be reproduced by modifying the main() function in the MLA project "primitive_layer."
© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.