ElectricFox.BdfSharp
1.0.3
dotnet add package ElectricFox.BdfSharp --version 1.0.3
NuGet\Install-Package ElectricFox.BdfSharp -Version 1.0.3
<PackageReference Include="ElectricFox.BdfSharp" Version="1.0.3" />
<PackageVersion Include="ElectricFox.BdfSharp" Version="1.0.3" />
<PackageReference Include="ElectricFox.BdfSharp" />
paket add ElectricFox.BdfSharp --version 1.0.3
#r "nuget: ElectricFox.BdfSharp, 1.0.3"
#:package ElectricFox.BdfSharp@1.0.3
#addin nuget:?package=ElectricFox.BdfSharp&version=1.0.3
#tool nuget:?package=ElectricFox.BdfSharp&version=1.0.3
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.
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:
- A glyph with an
ENCODING
value matching the requested code point - A glyph whose
STARTCHAR
name is a single character matching the requested code point - A glyph with a name in the form
uniABCD
orU+ABCD
that matches the Unicode value
You can control this behaviour with the GlyphLookupOption
enum:
EncodingStrict
– Only use theENCODING
field. No fallbacks.BestGuess
(default) – TryENCODING
first, then fall back to names when possible.UseIndex
– When all else fails, fallback to the glyph’s index position in the font.
Product | Versions 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. |
-
net8.0
- System.IO.Pipelines (>= 9.0.8)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Initial release