Using Fonts in a Graphics Application

Fonts

Fonts

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:

  • 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 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:
…microchip/mla/vYYYY_MM_DD/framework/gfx/utilities/grc

Linux and Apple OSX 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 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.

font_def.png
font_files_include.png

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.

TextStringDraw_Code.png
TextStringDraw_Picture.png

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.

VertStringDraw_Code.png
VertStringDraw_Picture.png

Example of using GFx_TextStringHeightGet, GFX_TextCursorPositionSet, and GFX_TextCharDraw to vertically output "Graphics"

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…
Note that the two example programs demonstrating fonts can be reproduced by modifying the main() function in the MLA project "primitive_layer"

20th Annual
Microchip MASTERs Conference 2016
Register now - Deadline: July 29

JW Marriott Desert Ridge Resort-Phoenix, AZ

© 2016 Microchip Technology, Inc.
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.