Converting TrueType glyphs to PostScript

I've adjusted the simple TrueType glyph rendering program I wrote a few weeks ago to output PostScript, using the appropriate drawing commands to give a perfectly smooth outline of the glyph.

Here's the new program: eps-unicode-character.c

I'm hoping to extend this enough to do some very simple typesetting. Basically, I just want to be able to generate nice tables of Japanese kana glyphs to hang on my wall, and I haven't been able to find anything that handles Japanese characters well enough when generating PostScript.

One nice thing about this program is that it was fairly easy to add some ‘debugging’ code to show where the start and end points of each line or curve are, and also the control points. I've added an option -d to do that. Here's what you get if you look closely at a bit of a Times New Roman capital A:

Close-up of part of glyph, showing control points for curves.

Tracing glyph outlines

My program uses the FreeType library again. This time, instead of getting FreeType to render a bitmap, we retrieve the glyph outline (see the draw_outline() function in my program).

The outline of a glyph is made up of straight lines and curves, which can be fairly simply turned into PostScript commands to create a path of the appropriate shape (which can then be filled in). The FreeType documentation has more details about how glyph outlines work.

The easiest way of extracting the outline information is to call the FT_Outline_Decompose() function, passing it a structure containing pointers to some callback functions. You need to supply four callback functions:

  • move_to — start new path segment, directly translates to PostScript moveto operator
  • line_to — straight line from current point, directly translates to PostScript lineto operator
  • cubic_to — cubic Bézier curve from current point, using two control points, exactly the same as the PostScript curveto operator
  • conic_to — simpler Bézier curve using only one control point

The first three are trivial when outputting PostScript, but the last doesn't map directly to any PostScript command (that I know of). The solution is to work out which two control points to use with the curveto operator to give the same curve. I wouldn't have the faintest idea how to do this, but I found some code in Yudit that does it, and the results look right to me.

Here's how I think it should work. You have the current point (place where the last drawing operation left off) which I'll call (x1,y1), and the place you're drawing to, (x2,y2). The conic curve has a single control point (cx,cy). The two control points to use for a cubic Bézier curve are:

((x1 + 2cx) / 3, (y1 + 2cy) / 3)
((2cx + x2) / 3, (2cy + y2) / 3)

Like I said, I don't know the maths for this stuff, so don't take my word for it.

< Hikaru's Go | Text::VimColor version 0.08 >

Miniblog

(nuggets of inanity)

Tuesday Apr 24th 2007, 16:54 »
Just took the annual web design survey that AListApart do. I don't realy consider myself to be a web designer, but I have been doing a lot of HTML and CSS lately.
Monday Apr 23rd 2007, 18:23 »
Strange, there appears to be a bare-knuckle boxing match going on in the field outside my flat. Wish they wouldn't make so much noise about it.
Thursday Mar 1st 2007, 18:47 »
“In its written form, Hebrew has no vowels, making it the ideal language for texting.”
—Said in jest on some Radio 4 programme just now.

Archive: 2007 · 2006 · 2005 · 2004
Feed