Why .NET FontManager
Once working with DTP area, you need to make certain charts, vector images and logos with a real vector format and proper pantones. Or, perhaps you simply like good charts on your mobile device without raster. In all these cases you need to make images in specific and sometimes ancient formats.
So, you need proper and high quality fonts as well! It is not enough to make a few polygons to be sure it works and looks excellent.
But .NET does not even have support for OpenType fonts. Thereby, we decided to create our own library .NET FontManager with a number of helpful features. Once we have done the same trick dozens of times, we decided to share our code with others.
Moreover, we have API for Java as well.
When you need .NET FontManager
Whenever you have non-standard or commercial font, you need to think how to work with it, how to distribute it and how to keep windows font substitution hands out of your font.
Also, you don’t need to purchase TrueType fonts. OpenType, CID fonts, Type1 hold glyphs in vector and are handled by freetype2.
Here is a simple decision: bring everything with your application, use it from your private folder and it will work in an excellent manner: the proper look, the proper feel and no interference with other fonts and programs. Easy distribution is important sometimes.
When you work with font files you know the proper font all the time; and errors in font metadata will not bring you any trouble. Just use contents and add any “font name” to it according to your needs.
How to integrate
Technically, it works through freetype2 version 2.5 and supports all fonts from this library. Instead of playing with low C code you can work with .NET and operate with pure .NET abstractions.
For example, with OxyPlot library the typical appliance will look like:
All fonts are handled by .Net FontManager and stored in SVG font format:
This is the typical result:
.NET FontManager is an intellectual wrapper over the FreeType2 open source library and it uses GPLv2 as the original library.
When creating FreetypeFontSupplier, you provide the byte array with the font contents. It might be a file from a disk or contents downloaded from the Internet, or a resource from your assembly. During the creation time you provide the byte array with one of the known font formats.
During the creation process FreetypeFontSupplier is called FreeType to parse the byte array, extract the contours and fill it as FreetypeFontSupplier.GlyphInfo hashmap. At the same time, the basic font metrics are discovered as well and become available once the FreetypeFontSupplier is created.
After creation of FreetypeFontSupplier, FreeType is no longer used and everything is held in the memory. All computations and drawings are performed by our code, not by FreeType2. So, you don’t need to think about Cairo and other Linux-world libraries with wrappers. Once the font is parsed, all further operations are pure .NET.
If you have only CIDType2 from PDF, just extract the bytes of the font into array and let the library work. The in-existence of the certain characters might limit paints, but the known glyphs will work well. Please do not forget to extract PDF Encoding and/or CMap conversions additionally in your code, especially for the case Type0 descendants; some “optimizers” might keep only codes of the glyphs in fonts instead of normal tables. Over-optimized PDFs might be a bunch of surprises.
To start using the library, just specify the font file: private Matrix defaultMatrix = new Matrix(0.01f, 0, 0, 0.01f, 0, 0); FreetypeFontSupplier fsup = new FreetypeFontSupplier( File.ReadAllBytes(“arial.ttf”), defaultMatrix);
The default matrix is necessary to scale glyphs in a normal canvas for measurements and drawings. For example, for SVG library the “common size” is 100. Thus, we use 1/100 for scaling for this case. Defaults are useable for most cases, so you can leave the 2nd parameter as null.
Once a file is loaded you can compute the sizes of the text for 12pt font size:
In the same way we can compute ascent, descent and even “units per em”.
Now we can either discover such details from the font, for example, to produce a new format (like SVG) or imply draw the text as follows:
where (0, 0) are left/top corner of the text (ascending is added automatically so you don’t need to care about it), 12.0f is font size in points and brush is the color, hatch, gradient or texture.
In the same way you can make an outlined text:
Sometimes you need to fill the text in the specific rectangle:
The text will be auto-wrapped against right boundary.
And the last trick is to make SVG custom font definition for specific sub-string:
This code seems tricky, but it indicates the basic principles of using the library. Every number and every curve are available through .NET FontManager; and it is easy to use it in the same way for other libraries. Just use a new API instead of the trick with sophisticated “measurers” and “renderers”. Your fonts will be always available and predictable.
.NET FontManager appliances: OxyPlot charts library
For the sake of usability we have added bindings to popular .NET libraries. One of them is OxyPlot. It works fast and has very good architecture allowing to add a few additional options.
Certainly, we have added not only fonts support but SVG output as well; which means we have added SVG Colors (with icc-color(#CMYK,) profile) and fonts.
.NET FontManager appliances: SvgNet
Unlike the charts library, SvgNet is an open source code supporting drawings to SVG. Logos, simple boxes and other vectorized stuff are there.
For OxyPlot we have added certain code to handle custom fonts and improve the colors. Unfortunately, due to GDI API limits we cannot inherit System.Drawing.Font and supply our derived implementation instead of the standard one (Microsoft like sealed classes even in cases it should not be sealed). So, we have added a few more DrawString() calls for SVGGraphics implementation as well as in the interface. We have no possibility to follow Microsoft contract for Graphics class.
.NET FontManager library source (dotNET-FontManager-sources.zip).
This library is provided “AS IS” under terms of the GPLv3. In case you need it under another (for example, commercial) license, please contact us using the contact form below. Commercial support is available as well.