Coder Social home page Coder Social logo

layoutfarm / typography Goto Github PK

View Code? Open in Web Editor NEW
404.0 23.0 83.0 137.74 MB

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

License: Other

C# 99.90% Smalltalk 0.01% Makefile 0.10%
truetype opentype font typography msdf ttf otf rendering layout contour

typography's Introduction

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.

typography_thanamas

Sov_Thanamas font from https://www.f0nt.com/release/sov_thanamas/


Cross Platform

The Typography library is a cross-platform library and does NOT need the PixelFarm Rendering library.

You can use the library to read font files (.ttf, .otf, .ttc, .otc, .woff, .woff2) and

  1. Access all information inside the font.
  2. Layout the font glyphs according to the OpenFont specification.

The core modules (Typography.OpenFont, Typography.GlyphLayout) do NOT provide a glyph rendering implementation. But as you are able to access and read all glyphs, it is easy to render them provided the exact position of each glyph.

Below are some screenshots of projects that use Typography to read each glyph from font files and render using their rendering engine.

sum2

1. MatterHackers/agg-sharp, 2. CSharpMath/SkiaSharp, Xamarin.Forms, 3. emoji.wpf/wpf, 4. zwcloud's ImGui/GL,GLES


Project arrangement: The purpose of each project

The core modules are Typography.OpenFont and Typography.GlyphLayout.

Typography.OpenFont

  • This project is the core and does not depend on other projects.
  • This project contains a font reader that can read files implementing Open Font Format (ISO/IEC 14496-22:2015 and Microsoft OpenType Specification) or Web Open Font Format (either WOFF 1.0 or 2.0)
  • The OpenType GSUB, GPOS layout mechanism is in here but a more easy-to-use interface is provided in Typography.GlyphLayout below.
  • No Visual/Graphics Rendering Here

Typography.GlyphLayout

Since the GlyphLayout engine is not stable and quite complex, I separated this from the OpenFont core project.

  • This project invokes OpenType Layout Engine/Mechanism (esp. GSUB, GPOS) inside Typography.OpenFont
  • The engine converts a string to a list of glyph indexes, then substitutes glyphs and places them into proper positions with respect to the provided settings, eg Script/Languague Setup, or Advanced GSUB/GPOS on Emoji
  • No Visual/Graphics Rendering Here

sum4

1) CoreModules, 2) Typography.One: a more easy-to-use than core module

See more detail about the 2 modules and others here : #99


PixelFarm's Typography

Since the core library does not provide a glyph rendering implementation, You can learn how to do it from the example repositories above, or you may learn it from my implementation => PixelFarm.Typography.

PixelFarm.Typography links the core Typography library to the PixelFarm Rendering library. You can learn how to implement string drawing, how to implement text-layout services, and how to cache glyph shapes, so you can apply this to your own library.

sum3

HtmlRenderer on GLES2 surface, text are rendered with the PixelFarm.Typography


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 copy source code directly, 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/

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

Apache2, 2020, Adobe Font Development Kit for OpenType (AFDKO), https://github.com/adobe-type-tools/afdko

Text Processing

Unicode (BSD style), 2020, UNICODE, INC, https://www.unicode.org/license.html

Apache2, 2014 , Muhammad Tayyab Akram, https://sheenbidi.codeplex.com/ , https://github.com/Tehreer

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

MIT, 2020, brezza92 (https://github.com/brezza92), MathML layout engine

typography's People

Contributors

brezza92 avatar happypig375 avatar maheshpanthi11 avatar prepare avatar rachamimyaakobov avatar samhocevar avatar taleinat avatar vidstige avatar virzak avatar zwcloud avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

typography's Issues

implement Typography.TextService

we can cache this

key value
font+ small_word output glyph-plan

for faster glyph-sub/pos,

Implement as optional service (not in a core)
user can use it or not.

How to get a glyph from character directly?

For example, for character 'D', the glyph index for msjh.ttf and Helvetica.ttf are both 39. But for unifont-9.0.06.ttf, the glyph index is 71.

The only way to get a Glyph I found is Typeface.GetGlyphByIndex(). I want to know whether we can get the glyph from the character D directly?

TypeReader parts as NuGet package

I think it would be best to try to move out the ttf loader parts into a library, then you can just add a dependency from PixelFarm on that.

Implement Bi-directional text

Possible Bug of Typography.OpenFont.IO.ByteOrderSwappingBinaryReader

When I'm testing a ttf file with Typography, Typography.OpenFont.IO.ByteOrderSwappingBinaryReader.ReadInt16 throw an System.IO.EndOfStreamException: 'Unable to read beyond the end of the stream.'

Stack:

Typography.OpenFont.IO.ByteOrderSwappingBinaryReader.ReadInt16() Line 59	C#
Typography.OpenFont.Tables.GPOS.ValueRecord.ReadFrom(System.IO.BinaryReader reader, ushort valueFormat) Line 107	C#
Typography.OpenFont.Tables.GPOS.ValueRecord.CreateFrom(System.IO.BinaryReader reader, ushort valueFormat) Line 158	C#
Typography.OpenFont.Tables.GPOS.PairSetTable.ReadFrom(System.IO.BinaryReader reader, ushort v1format, ushort v2format) Line 49	C#
Typography.OpenFont.Tables.GPOS.LookupTable.ReadLookupType2(System.IO.BinaryReader reader) Line 468	C#
Typography.OpenFont.Tables.GPOS.LookupTable.ReadRecordContent(System.IO.BinaryReader reader) Line 245	C#
Typography.OpenFont.Tables.GPOS.ReadLookupListTable(System.IO.BinaryReader reader, long lookupListBeginAt) Line 170	C#
Typography.OpenFont.Tables.GPOS.ReadContentFrom(System.IO.BinaryReader reader) Line 69	C#
Typography.OpenFont.Tables.TableEntry.LoadDataFrom(System.IO.BinaryReader reader) Line 22	C#
Typography.OpenFont.OpenFontReader.ReadTableIfExists<Typography.OpenFont.Tables.GPOS>(Typography.OpenFont.Tables.TableEntryCollection tables, System.IO.BinaryReader reader, Typography.OpenFont.Tables.GPOS resultTable) Line 162	C#
Typography.OpenFont.OpenFontReader.Read(System.IO.Stream stream, Typography.OpenFont.ReadFlags readFlags) Line 91	C#

The ttf font file I'm using:
DroidSans.zip. I forgot where it is from. But it can be opened by the Windows Font Viewer so it should be valid.

Discussing the use of NotImplementedException

For the moment, NotImplementedException is thrown about whenever an unknown table format is encountered. For instance GPOS.cs has 12 such occurrences, GSUB.cs has 7, and GDEF.cs has 1. But all these three tables are optional in the standard. My understanding is that a client should just ignore the features it does not know about instead of throwing.

How would you feel about replacing those throws with safe fallbacks instead? I can try to do the work but I wanted your thoughts about it first.

(Context: I’m currently writing my own parser for a subset of OpenType for Emoji.Wpf and I thought it might be interesting to use Typography.OpenFont instead, and contribute some of my code.)

Using Typography in my GUI lib.

Hi, @prepare

I don't know how to get in touch with you so I created this issue. It seems you are not on gitter yet. Would you mind joining this gitter group? We can discuss about further development of Typography together.

I have published my GUI lib. A former version of Typography is included.

You can see how Typography is used in TypographyTextContext.

Currently the text size and postion got from TypographyTextContext is not accurate. I will look into this issue later.

PS: Don't worry about the LGPL license. I'll grant you a permissive license for codes that directly related to Typography.

Documentation Project

First I'm not a native English speaking person.
so you can correct it where it is wrong grammar/ wrong intention or not proper.

You CAN HELP ME create the topics that you want to know.
so I will create a description for it

:)

Will this become the C# version DirectWrite or pango?

I desperately need a text-layout engine written in raw C#. DirectWrite is windows-only. And pango is almost impossible to be compiled on mobile platforms. ( I'm exhausted with cross-compiling, makefiles and defines. It has taken me almost two month to learn and try to achieve that but completely get nothing. )

Will Typography become the C# version DirectWrite or pango? I was searching for papers and algorithms for creating a text-layout engine myself before I ran into this project. It really amazed me when I saw Glyph Layout in ReadMe.

I'm really looking forward to your reply! @prepare

Add essential functions to GlyphLayout.

The current one is quite limited. I think as a text layout engine, at least the following functions should be added:

  1. layout text characters as one or multiple lines inside a rectangle, including alignment (nine position)
  2. given a point, get a index of the nearest character (hit-test)
  3. given an index of character, get the position of the caret/cursor
  4. reading direction-some languages reads from right to left
  5. calculate the exact size of specified text without rendering them-in this case, the user only cares about how much space the layouted glyphs will occupy, so no contours need to be exacted and only the size of each glyph is required. In the following picture, the size of dashed rectangle printed glyphs is the exact size of 啊rAbc.
  6. font family
  7. font style/stretch/weight

Also many basic conceptions need to be documented, including

  • coordinate system
  • text position definition
  • the scaling matters-I think it is related to dpi and the internal unit used in TrueType or OpenType.

textlayout

How others design the text-layout API:


@prepare's note
I make the tasks here ...

  • 1) layout text characters as one or multiple lines inside a rectangle, including alignment (nine position)
  • 2) given a point, get a index of the nearest character (hit-test) (see below)
  • 3) given an index of character, get the position of the caret/cursor (see below)
  • 4) reading direction-some languages reads from right to left
  • 5) calculate the exact size of specified text without rendering them (see #23)
  • 6) font family below...
  • 7) font style, font weight below...

Demo not working for GdiPlus

Hi,

I have an issue on the demo. On my environment the GdiPlus demo is drawing characters at wrong positions. Though not familiar with the library, I tried to dig a little bit, and it seems that the DoGlyphPosition methods do nothing. The positions are not updated at all and remain at (0, 0).

I am using VS2017 and upgraded solution, but the build is successful (with a lot of warnings). Not sure whether this is the reason.

Thanks in advance.

How to get the line height of a layouted text?

For example,

class MyTextPrinter : DevTextPrinterBase { ... }
//...
MyTextPrinter printer = new MyTextPrinter();
printer.FontFilename= "D:\msjh.ttf";
printer.FontSizeInPoints = 12;
printer.DrawString(new []{'A', 'B', 'C', '啊'}, 0, 0);

How to get the actual line height of the layouted and printed text? Is it just printer.FontSizeInPoints?
(I know the function of GlyphLayout is quite limited and can only layout text characters in one line.)

implement other essentail tables

@vidstige , please comment about this how-to-build-each-table

OpenType Layout
(https://www.microsoft.com/typography/otspec/ttochap1.htm)

  1. Name: created (https://github.com/prepare/FontRasterizer/blob/master/OpenTypeReader/Tables/NameEntry.cs)
  2. Kern: created (https://github.com/prepare/FontRasterizer/blob/master/OpenTypeReader/Tables/Kern.cs)
  3. Glyph Substitution table (GSUB) :developing ...
  4. the Glyph Positioning table (GPOS): developing ...
  5. the Baseline table (BASE): developing ...
  6. the Justification table (JSTF), and
  7. the Glyph Definition table (GDEF): developing ...

I've changed how to create each table.
see
https://github.com/prepare/FontRasterizer/blob/master/OpenTypeReader/OpenTypeReader.cs#L46

  1. All tables are subclass of TableEntry (https://github.com/prepare/FontRasterizer/blob/master/OpenTypeReader/Tables/TableEntry.cs)
  2. First, Load all as UnreadTableEntry
    ((https://github.com/prepare/FontRasterizer/blob/master/OpenTypeReader/OpenTypeReader.cs#L58)
  3. and substitute some of them with actual table
    (https://github.com/prepare/FontRasterizer/blob/master/OpenTypeReader/OpenTypeReader.cs#L63)
  4. I have created a Kern table and Name table

Add ScriptLang ComboBox

ScriptLang plays an important role on Glyph Shaping

for example, Thai Script is a 'complex' script (need GSUB, GPOS)

script_01

pic 1: Thai glyphs with incorrect ScriptLang (in red box, latin), shows abnormal result

script_02
pic 2: Thai glyphs with correct ScriptLang (in red box, Thai), shows NORMAL result


Now, How about your 'Script' ?, Dose Typography present the text correct or not?

:)

Bad rendering method in the OpenGLES based demo.

From this, we can see each GlyphRun uses a drawcall and it harms performance badly when rendering a string containing many characters.

I think we should apply the offset when we fill the vertex buffer. I mean, bake the offset into vertex instead of offset every glyph in vertex shader, just like what I have done before in my original OpenGLES demo.

Update:
No, my soultion is incorrect partially. The mesh should only be moved after it is built. Otherwise we can not re-use the old mesh when the position of text is changed but its content and style remains the same. But I think the offset of each glyph should be applied to built vertices.


@prepare's note:

  • add VBO version for GLES glyph rendering

Differences in the MSDF generation results...

I've encountered some differences in the generation of MSDF bitmaps between this Typography library, and the original C++ MSDFGen implementation from which it is adapted.

With certain fonts (hand made ones, using FontForge) - certain glyphs are producing different MSDF maps - but it is only certain glyphs... the majority appear to be OK.

For example...

LayoutFarm - layoutfarm_b MSDFGen - msdfgen_b

Look pretty much equivalent (apart from the page size, but that's fine)

However, the same code generating glyph:

LayoutFarm - layoutfarm_a MSDFGen - msdfgen_a

Any idea what could be going on?

Font --- shardailes.zip

MSDF Glyph Generation doesn't match Chlumsky's MSDFGen output

How up to date is your C# implementation to Chlumsky's MSDFGen tool (on which it is based)?
Glyph generation from your implementation does not match, and results in very blocky curves.

I modified the edgeColoringSimple function to match the MSDFGen implementation.
I also modified the ConvertToIntBmp in MsdfGen.cs to write out the color channels in the correct order (argb) (Although BMP stores the bytes in ABGR order, Intel ordering when the uint32_t is written out, changes that order - so we write out a uint32_t of argb, so that when BMP reads it in byte-by-byte, it comes in a ABGR)

I also wrote a little function that converts a contour list to a MSDFGen shape definition, to test the possibility that your contour list was somehow causing the problem. So I use Typography.OpenFont to generate the shape definition, and then cut/paste the output into the commandline of MSDFGen.
Unfortunately, MSDFGen works just fine with that shape, so that confirms that the problem is with your C# implementation of MSDFGen.

I've compared the .cpp and .cs files, but I can't see where the divergence might be (admittedly, it was very very late at night)

(I've attached the DumpContours.cs for anyone that might find it useful)

I've also attached a couple of screenshots of the resultant MSDF glyph generated from both MSDFGen and Typeography.MSDFGen when run through the MSDF pixel shader.

contours.txt

msdfgen msdf screenshot
msdfgen output
typeography msdf screenshot
typeography output

Problem in GlyphIndexList.Replace()

At line 114 in GlyphIndexList.cs, _tmpGlypIndexBackup may be empty if the whole string was replaced with a ligature, so the code crashes there when accessing _tmpGlypIndexBackup[0].

I might be able to fix this later, but I do not really understand GlyphIndexToUserChar yet. Maybe you know what’s going on and can fix it more quickly than me :-)

Suggest renaming the .sln file

I know it’s not a big deal, but when I Alt-Tab through my windows, the VS instance I use for Typography appears as “MainDev”, which is not really descriptive. Any problem with renaming it to Typography.sln?

image

Upgrade to VS2017

I think the project should be upgraded to VS2017 since it has been released.

The most important thing is that the support for .NET Standard(including .NET core) is greatly improved in VS2017. Also some project structures are changed. For example, project.json and *.xproj are no longer used. Instead, *.csproj is used for all C# projects in VS2017.

VS2017 can easily upgrade VS2015 solution and projects automatically. And I have done that on my machine.

I have been using VS2017 RC and RTM for several months. It provides more flexible installation and is smaller and quicker than the big VS2015. BTW, the android projects using Typography is written with VS2017--I can't install VS2015 on my machine because the remaining disk space is not enough.

Vertical Metrics?

Any idea when you'll be implementing Vertical Metrics support in OpenFont?

GPOS's PairPosFormat2 is not implemented

My artists have given me a new font that kills the importer - it appears to hit an NotImplementedException in GPOS.cs - at line 581.

The font has been authored in FontForge -- I'm not sure what to look for, or ask them to change to prevent the unsupported table format from being included.

Any suggestions?

Incorrect RGB ordering

If I set canvasPainter.FillColor = new Color(255, 0, 0); in VcxTextPrinter.cs I would expect that the text is rendered in red, but instead it is rendered in blue (using the PixelFarmSample.WinForms sample).

Swapping R and B in ColorOrder.cs results in the correct color being used, but I don’t know where the actual bug is.

As a result, the code in CPAL.cs is incorrect with regards to RGB ordering. The buffer is in BGRA format and the GetColor code should be as follows:

b = colorBGRABuffer[startAt];
g = colorBGRABuffer[startAt + 1];
r = colorBGRABuffer[startAt + 2];
a = colorBGRABuffer[startAt + 3];

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.