Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TrueType Font Parsing in C

I want to read a ttf and draw a text with that font on a buffer. Even though there are libraries such as freetype, gd to do this task, I want to write my own code. Can you advice me on how to accomplish this task?

like image 754
elasolova Avatar asked Jul 05 '10 13:07

elasolova


People also ask

How do I read TTF files?

How to open a TTF file. You can open a TTF file in Microsoft Windows Font Viewer (Windows), Apple Font Book (Mac), or iFont (iOS, Android). Before opening a TTF file in Windows Font Viewer, you must place the file in the C:/​Windows/​Fonts directory.

Is TTF better or OTF?

The Differences Between OTF and TTF For most of us non-designers, the additional options will likely go unused. In other words, OTF is indeed the "better" of the two due to the additional features and options, but for the average computer user, those differences don't really matter.

What is a TrueType font used for?

TrueType is an outline font standard developed by Apple in the late 1980s as a competitor to Adobe's Type 1 fonts used in PostScript. It has become the most common format for fonts on the classic Mac OS, macOS, and Microsoft Windows operating systems.


3 Answers

I would suggest you

  1. Read all the TTF docs you can find

  2. Find all the open source TTF parsers + renderers you can find, in many different languages, such as Freetype (c/c++), Batik (java), and anything else you can google for. Also George Williams' fontforge will likely be very helpful to you on your journey.

  3. Rip apart all the programs you collected in 1. and see how they work. See if you can make a tiny small example program to do something simple, like dump the list of points for the outline of the letter "I".

  4. Work on your rasterization. Start with something very simple, like rasterizing the letter "l".

The problem with TTF is that there is not a simple file format, and freetype handles a lot of crazy details for you. However if you don't care about portability, and you already have a specific TTF file you want to render, and you only care about a small simple alphabet, like Latin or Cyrillic, you might be OK.

Also you might want to check out a list of TTF documentation I linked to from my little project https://github.com/donbright/font_to_svg/

like image 96
don bright Avatar answered Oct 06 '22 03:10

don bright


Unless you're one of the world's top experts on fonts, typography, and writing systems, the answer is simple: DON'T. TrueType/OpenType has a lot of tables you need to support for correct rendering, and even when using FreeType (which is an extremely low-level library), most people get it wrong.

If you need to do low-level, deterministic-across-platforms font handling, then at the very least, you should be using FreeType and libotf. This will provide you with access to the glyphs and outlines which you can then render however you like. In most cases though using your GUI system's text rendering routines will be a lot easier and less error-prone.

Finally, if you insist on ignoring my advice, a good RTFS on FreeType and Microsoft's online resources explaining the tables in TrueType/OpenType fonts are probably the best place to get started.

like image 40
R.. GitHub STOP HELPING ICE Avatar answered Oct 06 '22 05:10

R.. GitHub STOP HELPING ICE


Not impossible, for anyone else tempted to try. I was curious about doing it because I like the DIY graphics approach where I allocate some memory and write into it, then save as a jpg or png. I pirated a bitmap font from giflib but that's strictly 8x8 pixels.

A few links:

`http://stevehanov.ca/blog/index.php?id=143`
`https://www.google.com/search?q=ttf+parser+c&ie=utf-8&oe=utf-8`
like image 27
Alan Corey Avatar answered Oct 06 '22 04:10

Alan Corey