onefontCookbook
onefont cookbook
Short recipes for common onefont tasks, each with complete code you can copy.
Recipes
Read a TTF or OTF font fileRead a font’s names, style, metrics and tables in C# from a TTF, OTF or TTC file, on any platform, and tell TrueType outlines from CFF.Check if a font supports a characterFind out in C# whether a font has a glyph for a character, list the characters it lacks, and fall back to other fonts cluster by cluster.Measure and wrap textMeasure the width of a string in C# from the font itself, with kerning and ligatures, and wrap a paragraph at Unicode line break opportunities, without System.Drawing.Convert text to SVG pathsTurn a string into SVG path data in C#: shape it with the font’s ligatures and kerning, then write each glyph’s outline through a sink that emits SVG commands.Right-to-left and mixed-direction textLay out Arabic, Hebrew and mixed-direction text in C#: resolve the Unicode bidi levels, cut the paragraph into runs, put the runs in display order, and shape each one in its direction.Turn on OpenType featuresSwitch OpenType features such as small caps, figure styles, fractions and ligatures on or off in C#, and choose a language’s local forms, with ShapingOptions.Shape complex scriptsShape Hindi, other Indic scripts and Thai in C# with onefont: syllables, conjuncts, reordered vowel signs and stacked marks, and which character each glyph came from.Find caret positions and hit-test textCompute caret positions for shaped text in C#, including inside ligatures, and map a click’s x coordinate back to an index in the string.Shape many runs without allocatingShape thousands of strings in C# with no garbage per call: reuse a ShapedRun and a ShapingOptions, and read clusters and glyphs from spans.