4

I have a string e.g. "Hello world" and a font name e.g. "Courier". I need to get an array of polylines that represent this string. These polylines will be rendered in OpenGL scene.

I mean I need to convert each letter into one or more 2D polylines.

How do I do this in iOS (Objective-C or C++)

Saad
  • 8,857
  • 2
  • 41
  • 51
Dmitriy
  • 5,357
  • 8
  • 45
  • 57

2 Answers2

6

You can get the path from a text and a font using Core Text. There is a Core Text method called CTFontCreatePathForGlyph that will give you the path for a given glyph of a given symbol. Using that you can get the path for all your glyphs.

You should take a look at this article about "low-level text rendering". You can also look at the sample code for this article to see how to get the path for a text (and stroking it using Core Animation).

You will still have to convert the path to a polyline (since a path may contain arcs and curves while a polyline is just a list of points)

David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
  • There is no ready solution for this currently, isn't it? CTFontCreatePathForGlyph requires CGGlyph and CTFontRef, how do I get them from `strin "Hello world" and a font name e.g. "Courier"` – Dmitriy May 29 '12 at 09:32
  • If you look at the second video in the article by Ole Begemann, he strokes the text "hello world". He also posted the code that he uses to be able to do this on [GitHub](https://github.com/ole/Animated-Paths) (as the post says). Have a look at how he does it there. – David Rönnqvist May 29 '12 at 09:37
1

The FreeType library supports the loading of a wide range of font files (TrueType, PostScript and more). The FreeType engine can load fonts and render to bitmap, but it can also return detailed glyph information.

You can use the FT_Outline_Decompose function to take an FT_Outline and decompose it into its constituent lines, arcs and curves. You simply provide a series of function pointers in an FT_Outline_Funcs struct which will be invoked during the outline processing. These functions are defined by you, so you can simply convert them to whatever format suits your app best (eg. polylines).

The API reference:

Note that if you are rendering large blocks of text which don't change frequently, rendering and filling a series of curves at each frame is quite expensive. The usual solution is to choose a point size, render all the required glyphs into a texture and use a texture atlas to render strings into a quad.

Some useful information is posted here in one of the OpenGL FAQs on fonts, although they also talk about using FreeType. The texture atlas technique is worth reading up:

Community
  • 1
  • 1
gavinb
  • 19,278
  • 3
  • 45
  • 60
  • @David Rönnqvist's answer, to use `CTFontCreatePathForGlyph` is the better answer, so +1 to that. I should have remembered about CoreText! FreeType is often used for font support with OpenGL, and can be useful in multi-platform development. – gavinb May 28 '12 at 13:19
  • FWIW, iOS uses FreeType behind the scenes, unlike Mac OS X which uses Apple’s own code. – al45tair May 29 '12 at 16:58
  • @alastair Very interesting - I assumed it was the same on both platforms. In that case, perhaps directly linking to the underlying FreeType support is a viable alternative. – gavinb May 30 '12 at 11:19