Skip to content

Relfos/LunarFonts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lunar Fonts

Font loader / rasterizer for TTF format in pure C#.

This code is a direct port from the Delphi version which I ported some years ago from C.

The original C version was written by Sean Barrett

Special thanks to Ben Baker for his bug fixes.

Installation

Available as a Nuget package.

    PM> Install-Package LunarLabs.Fonts

Sample code

At the core, this library allows loading a font from a file and from there you can rasterize a single character at a specified scale (and also query info about characters). Here's a simple example of how to do it:

	using LunarLabs.Fonts;
	//...
	var bytes = File.ReadAllBytes(fontFileName);

	var font = new Font(bytes);

	// convert font size to pixels
	var scale = font.ScaleInPixels(64);	

	var result = font.RenderGlyph('A', scale);
	// result.Image now contains the pixels, width and height of the rasterized glyph
	// it also contains other fields with useful info like xAdvance which lets you know how many pixels to move forward if rendering multiple characters sequentially

Note that the pixels array stores the pixels as single-channel greyscale data. What you do with this is up to you, you can blit it to the screen, to a bitmap or to an OpenGL texture etc.

You can check the included sample for a more advanced program that shows how to output a full string and save the result as a image file.

Kerning is also supported via method GetKerning(charA, charB, scale), which returns a int containing amount of pixels to subtract from xAdvance when calculating position of next character based on previous.

Font Viewer

Include is also the source code for a font viewer that uses this library.

It allows browsing of any folder that contains .ttf files and quickly preview custom text using each font.

Contact

Let me know if you find bugs or if you have suggestions to improve the code.

And maybe follow me @onihunters :)

About

Font loader / renderer for TFF format in pure C#

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages