Skip to main content

Font Support

Fonts on TinyCLR are stored in a custom .tcfnt binary format. Any TrueType font can be converted to .tcfnt with the FontConverter tool, embedded as a project resource, and used by the graphics engine for drawing text.

note

Old NETMF font files don't work on TinyCLR — re-convert with the current FontConverter even if you have older .tinyfnt files lying around.

NuGet package: GHIElectronics.TinyCLR.UI (the graphics engine that draws text). The FontConverter itself is a separate command-line tool available on the Downloads page.

Using a font

Load the converted font as a resource and pass it to the graphics engine:

using GHIElectronics.TinyCLR.UI;

var screen = Graphics.FromHdc(displayController.Hdc);
var font = Resources.GetFont(Resources.FontResources.NinaB);
var greenPen = new Pen(Color.Green);

screen.DrawString("Hello World!", font, greenPen.Brush, 10, 100);
screen.Flush();

See Graphics for the rest of the drawing API.

Converting a font

Converting a TrueType font to .tcfnt is a two-step process:

  1. Write a .fntdef file — a plain-text descriptor for the font: which face, which sizes, which character ranges, etc.
  2. Run the FontConverter — produces a .tcfnt file you embed as a resource.

A minimal .fntdef for the standard ASCII range:

SelectFont "FN:Arial,WE:400,HE:12,IT:0"
ImportRange 32 126

Run the converter:

GHIElectronics.TinyCLR.FontConverter.exe Arial.fntdef Arial.tcfnt
tip

The TinyFont Tool is a third-party GUI that can also generate compatible font files if you'd rather not write .fntdef files by hand.

.fntdef reference

The order of statements matters — for example, AntiAlias must come before the ImportRange it should apply to.

Font selection

AddFontToProcess path

Include a TrueType font file in the search list, in addition to the system-installed fonts. If the path contains spaces, quote it and escape backslashes.

AddFontToProcess C:\Windows\Fonts\Arial.ttf

SelectFont "selection string"

Search the available fonts for the first one matching the comma-delimited fields in the selection string. No spaces around commas or colons — they cause parse errors.

FieldMeaning
HEHeight in logical units. 0 for default; positive = cell height; negative = character height.
WIAverage character width in logical units. 0 for best aspect-ratio match.
ESEscapement angle in tenths of a degree. Negative = clockwise rotation.
WEWeight (0–1000). 400 = normal, 700 = bold. 0 for default.
ITItalic — IT:1 enables italic.
FNFace name (typeface name).
FullNameFull font name, e.g. "Monotype:Arial Regular (Microsoft)".

Example — regular-weight Arial at height 12:

SelectFont "HE:12,WE:400,FN:Arial"

Character ranges

ImportRange start end

Unicode code points to include, inclusive. Both arguments required. Use multiple ImportRange statements to convert non-contiguous ranges. Must come after SelectFont.

ImportRangeAndMap start end offset

Same as ImportRange, but maps the imported characters into a different range — output characters land at start + offset through end + offset. Must come after SelectFont.

SetAsDefaultCharacter charcode

Sets the Unicode code point used as a substitute for any character not in an imported range. Must come after SelectFont.

SetDefaultCharacter

Use the currently-selected TrueType font's own default character as the substitute. Must come after SelectFont.

NoDefaultCharacter

Don't substitute anything for unconverted characters. Must come after SelectFont.

Anti-aliasing

AntiAlias level

Levels 1, 2, 4, or 8 produce 2, 5, 17, or 65 levels of gray in the output bitmap respectively. Must come after SelectFont and before any ImportRange it should apply to.

Position adjustments

All of these take an integer (positive or negative). The Adjust* family applies to EM units unless noted; OffsetX / OffsetY apply to subsequent character ranges.

StatementEffect
AdjustAscentEM units added to ascent. Once per file.
AdjustDescentEM units added to descent. Once per file.
AdjustExternalLeadingEM units added to external leading. Once per file.
AdjustInternalLeadingEM units added to internal leading. Once per file.
AdjustLeftMarginDevice units added to left margin. Applies to the next ImportRange.
AdjustRightMarginEM units added to right margin. Applies to the next ImportRange.
OffsetXEM units to shift left (positive) or right (negative). Applies to subsequent ranges.
OffsetYEM units to shift down (positive) or up (negative). Applies to subsequent ranges.

Other

Verbosity level

Not currently supported — set to 0.

# comments

Lines starting with # are comments.

# This font is used by the main menu.
SelectFont "FN:Arial,WE:400,HE:14"
ImportRange 32 126

API reference

NamespaceDescription
GHIElectronics.TinyCLR.UITinyCLR UI framework (fonts, controls, layout)