Skip to content

C# Font Reader (TrueType / OpenType / OpenFont) , Glyphs Layout and Rendering

License

Notifications You must be signed in to change notification settings

Happypig375/Typography

 
 

Repository files navigation

Typography, this image was rendered with this library, in subpixel rendering mode

Pure C# Font Reader, Glyph Layout and Rendering.

While developing the PixelFarm Rendering library, I figured that the way to render a glyph from a font may be useful for other libraries.

So, I spinned off the way to render a glyph from a font to here, the Typography library.

The Typography library does NOT need the PixelFarm Rendering library.

gdiplus_sample1

Typography project's Solution Explorer View

As shown in the above screenshot, an example (marked with (1)) that uses Typography with WinGdiPlus is provided,

and an example (marked with (2)) the uses Typography with a 'mini' snapshot of PixelFarm Rendering library (marked with (3)).

Concept

  • 1.Load .ttf, .otf, .ttc, .otc, .woff, .woff2 files, with OpenFontReader.
System.IO.Stream stream = ...;
var reader = new Typography.OpenFont.OpenFontReader();
var typeface = reader.Read(stream); // stream is disposed inside Read
if (typeface is null &&
   reader.ReadPreview(stream) is var preview &&
   preview.IsFontCollection) {
 // .ttc or .otc
 for (int i = 0; i < preview.MemberCount; i++) {
   typeface = reader.Read(stream, preview.GetMember(i).ActualStreamOffset); // choose a font...
 }
}
// use typeface
  • 2.Rasterize a character to a bitmap with a pure software renderer which has Agg(anti grain geometry) Quality! with our PixelFarm's MiniAgg :) (https://github.com/PaintLab/PixelFarm)

  • Supported platforms: .Net Framework >= 2.0 or .Net Standard >= 1.3

What are each project used for?

See => LayoutFarm#99

Screenshots

Some screenshots of the current master.

Enable TrueType Hinting, Tahoma, 8 pts

enable_truetype_hinting


Tahoma, 72 pts (TrueType Hinting: disabled)

compare1_tahoma_72pts


Tahoma, 8 pts, (TrueType Hinting: disabled)

compare2_tahoma_8pts


Tahoma, 11 pts (TrueType Hinting: disabled)

compare3_tahoma_11pts


Kerning: Enabled

enable_kerning1


Subpixel Rendering

lcd_09

Tahoma, grey-scale vs lcd-effect subpixel rendering

typography_thanamas

lcd-effect subpixel rendering, Sov_Thanamas font from https://www.f0nt.com/release/sov_thanamas/


A Multi-channel signed distance field (Msdf) Texture (https://github.com/Chlumsky/msdfgen)

msdfgen


Another Msdf Texture

msdfgen2


A Msdf Texture Sample msdf_test_03


An Android GLES2-based demo

gles_android_emu

pic 1: GLES2-based android demo, DroidSans.ttf. Each glyph is tesselated to GlyphRun mesh (with C# Tesselator), and is rendered directly to GLES2 shader.

gles_android_emu

pic 2: The same technique as pic1, msjh.ttf, '啊rAbc' ,

Please note that baseline of 啊 is not correct


Advanced OpenFont Text Shaping

1. GSUB : Ligature features

ligature

pic 1: Showing GSUB's glyph ligature, see f-i

(see more : LayoutFarm#80 (comment))


2. GPOS

gpos

pic 2: Testing with Thai (complex script) glyph that require gpos table


3. GSUB : ccmp

gsub

pic 3: Testing with Thai glyphs (complex script), showing glyph substitution


4. GSUB and GPOS

th_glyph

th_glyph2

pic 4: testing with Thai glyph (complex script)

th_sarabun

pic 5: complex GSUB-GPOS, Sarabun font from https://github.com/cadsondemak/Sarabun

(see more: LayoutFarm#82 (comment))


Fun with Emojis!

Emojis and related features were contributed by @samhocevar.

typo_symbol

pic 1: Segoe UI Symbol Normal, on Windows 7

typo_symbol2

pic 2: FirefoxEmoji.ttf from https://github.com/mozilla/fxemoji

typo_symbol3

pic 3: Segoe UI Emoji Normal, on Windows 10

Advanced Emoji Ligatures

This is 👩🏾‍👨🏾‍👧🏾‍👶🏾 “Family - Woman: Medium-Dark Skin Tone, Man: Medium-Dark Skin Tone, Girl: Medium-Dark Skin Tone, Baby: Medium-Dark Skin Tone” without ccmp ligatures:

image

And here it is with ccmp:

image

pic 4: Say with Emoji?

(see: LayoutFarm#18)

(see more sam's work at https://github.com/samhocevar/emoji.wpf)


CFF Font and MathTable

Typography can read CFF font(.otf)

In this version, the results are not hinted by CFF instructions, so they are not pixel-perfect.

latin_long_compare

pic 1: compare with Win7's NotePad, latin-modern-math-regular.otf, 18 pts

Further more=> Typography can read all advanced MathTable.

See its action here => CSharpMath


Web Open Font Format (WOFF1 and WOFF2)

woff2

Roboto-Regular.woff2, Typography reads and restores woff2

see LayoutFarm#27


The HtmlRenderer example!

This is a snapshot of Html drawboard from (https://github.com/LayoutFarm/HtmlRenderer). The glyphs are generated/layouted with Typography, and rendered with PixelFarm (https://github.com/PaintLab/PixelFarm).

html_renderer_with_selection2

pic 1: HtmlRenderer on GLES2 surface, text are renderered with the Typography

Also, please note the text selection on the Html Surface.


License

The project is based on multiple open-sourced projects (listed below) all using permissive licenses.

A license for a whole project is MIT.

But if you use some part of the codebase, please check each source file's header for the licensing info if available.

Font

Apache2, 2014-2016, Samuel Carlsson, Big thanks for https://github.com/vidstige/NRasterizer

MIT, 2015, Michael Popoloski, https://github.com/MikePopoloski/SharpFont

The FreeType Project LICENSE (3-clauses BSD style),2003-2016, David Turner, Robert Wilhelm, and Werner Lemberg and others, https://www.freetype.org/

MIT, 2016, Viktor Chlumsky, https://github.com/Chlumsky/msdfgen

Apache2, 2018, Apache/PDFBox Authors, https://github.com/apache/pdfbox

Geometry

BSD, 2002-2005, Maxim Shemanarev, Anti-Grain Geometry - Version 2.4 http://www.antigrain.com

BSD, 2007-2014, Lars Brubaker, agg-sharp, https://github.com/MatterHackers/agg-sharp

MIT, 2016, Viktor Chlumsky, https://github.com/Chlumsky/msdfgen

BSD, 2009-2010, Poly2Tri Contributors, https://github.com/PaintLab/poly2tri-cs

Apache2, 2016-2017, WinterDev, https://github.com/PaintLab/PixelFarm

Platforms

MIT, 2015-2015, Xamarin, Inc., https://github.com/mono/SkiaSharp

MIT, 2006-2009, Stefanos Apostolopoulos and other Open Tool Kit Contributors, https://github.com/opentk/opentk

MIT, 2013, Antonie Blom, https://github.com/andykorth/Pencil.Gaming

MIT, 2004, 2007, Novell Inc., for System.Drawing

Unpack, Zlib,Brotli

MIT, 2018, SharpZipLib, https://github.com/icsharpcode/SharpZipLib

MIT, 2009, 2010, 2013-2016 by the Brotli Authors., https://github.com/google/brotli

MIT, 2017, brezza92 (C# port from original code, by hand), https://github.com/brezza92/brotli

MIT, 2019, master131, https://github.com/master131/BrotliSharpLib

Demo

MIT, 2017, Zou Wei, https://github.com/zwcloud, see more Zou Wei's GUI works at here and here

About

C# Font Reader (TrueType / OpenType / OpenFont) , Glyphs Layout and Rendering

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 99.9%
  • Other 0.1%