ElectricFox.BdfSharp 1.0.3

dotnet add package ElectricFox.BdfSharp --version 1.0.3
                    
NuGet\Install-Package ElectricFox.BdfSharp -Version 1.0.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ElectricFox.BdfSharp" Version="1.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ElectricFox.BdfSharp" Version="1.0.3" />
                    
Directory.Packages.props
<PackageReference Include="ElectricFox.BdfSharp" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ElectricFox.BdfSharp --version 1.0.3
                    
#r "nuget: ElectricFox.BdfSharp, 1.0.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ElectricFox.BdfSharp@1.0.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ElectricFox.BdfSharp&version=1.0.3
                    
Install as a Cake Addin
#tool nuget:?package=ElectricFox.BdfSharp&version=1.0.3
                    
Install as a Cake Tool

ElectricFox.BdfSharp - C# BDF Font Loader and Renderer

Introduction

This library provides tools for loading and rendering BDF font files in C#.

I originally built it because I had a collection of BDF fonts and wanted to use them in projects such as:

  • Adding text to pixel art
  • Rendering text to small displays (e.g. ePaper screens)

Many of the fonts I tested weren't strictly compliant with the BDF spec, so this library is designed to be forgiving when parsing. It also provides flexible glyph lookup options to handle unusual encodings, missing mappings, and even icon-style glyphs with no standard encoding.

Useage

Loading a font

var font = BdfFont.Load("some-bdf-font.bdf");

Measuring text

This library includes the ability to measure the size of rendered text in pixels.

var rect = font.MeasureString("Hello, World!");

The MeasureString method returns a rectangle that describes both the size and the position of the text relative to its baseline. Unlike simple width/height measurements, this rectangle also includes an X and Y offset that indicate where the text sits in relation to the origin point (typically the baseline at X = 0, Y = 0).

This extra positioning information is important for precise alignment: it allows you to line up text on a shared baseline rather than using the arbitrary "top edge" of the bounding box, which can vary depending on the font or glyphs used.

For example, a call to MeasureString("Qu") might return a rectangle with values:

X = 0
Y = -15
Width = 29
Height = 19

Here, the red cross marks the text’s origin (0,0). The negative Y value indicates that part of the text extends above the baseline, while the height ensures that descenders (like in g or y) would also be captured correctly.

Example showing the position of two letters with the origin highlighted

Rendering Text

You can render text into a two-dimensional pixel array using RenderBitmap. The array dimensions will match the rectangle returned by MeasureString.

var data = font.RenderBitmap("Hello, World!");

for (int x = 0; x < data.GetLength(0); x++)
{
    for (int y = 0; y < data.GetLength(1); y++)
    {
        g.FillRectangle(data[x, y] ? Brushes.Black : Brushes.White, x, y, 1, 1 );
    }
}

Looking up glyphs

BDF fonts often contain unusual or inconsistent encodings. To make lookups more reliable, this library uses a multi-stage fallback strategy when resolving glyphs for characters:

  1. A glyph with an ENCODING value matching the requested code point
  2. A glyph whose STARTCHAR name is a single character matching the requested code point
  3. A glyph with a name in the form uniABCD or U+ABCD that matches the Unicode value

You can control this behaviour with the GlyphLookupOption enum:

  • EncodingStrict – Only use the ENCODING field. No fallbacks.
  • BestGuess (default) – Try ENCODING first, then fall back to names when possible.
  • UseIndex – When all else fails, fallback to the glyph’s index position in the font.
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.3 131 9/8/2025
1.0.0 128 9/7/2025

Initial release