views:

39

answers:

4

I'd like to be able to extract the geometry for each letter in a TrueType font file. Each letter would have a set of coordinates, assuming each letter is in its own grid.

As a picture tells a thousand words - I'd like to get the vertices for letters similar to the image below (courtesy of http://polymaps.org/)

alt text

Update

Thanks to the hint to use GDI, which is now incorporated into .NET System.Drawing.Drawing2D I got the following code to create WKT polygons. No bezier curves possible. And even after the letters were flipped and rotated some paths still would not join correctly.

        GraphicsPath gp = new GraphicsPath();

        Font fnt = new Font("Arial", 12);
        Point origin = new Point(0, 0);
        StringFormat format = new StringFormat();
        FontFamily ff = new FontFamily("Arial");
        //enter letter here
        gp.AddString("T", ff, 0, 12, origin, format); //ABCDEFGHIJKLMNOPQRSTUVWXYZ

        StringBuilder sb = new StringBuilder();
        sb.AppendLine("DECLARE @g geometry;");
        sb.Append("SET @g = geometry::STGeomFromText('POLYGON ((");


        Matrix flipmatrix = new Matrix(-1, 0, 0, 1, 0, 0);
        gp.Transform(flipmatrix);
        Matrix rotationtransform = new Matrix();

        RectangleF r = gp.GetBounds();

        // Get center point
        PointF rotationPoint = new PointF(r.Left + (r.Width / 2), r.Top + (r.Height / 2));
        rotationtransform.RotateAt(180, rotationPoint);
        gp.Transform(rotationtransform);
        //gp.CloseAllFigures(); //make sure the polygon is closed - does not work

        foreach (PointF pt in gp.PathData.Points)
        {
            sb.AppendFormat("{0} {1},", pt.X, pt.Y);

        }
        PointF firstpoint = gp.PathData.Points[0];

        sb.AppendFormat("{0} {1}", firstpoint.X, firstpoint.Y); //make last point same as first
        sb.Append("))',0);");
        sb.AppendLine("");
        sb.AppendLine("SELECT @g");
        System.Diagnostics.Debug.WriteLine(sb.ToString());

alt text alt text

+1  A: 

alt text

In Adobe Illustrator

Object Menu > Expand...

This will convert the text to paths made of anchors and Bezier curves.

Aside from using an application, I don't know how to do this programmatically.

macek
Very nice. Never used Illustrator, but I see there is a trial available for download. Does it include a scripting language to extract the coordinates to text?
geographika
Unfortunately, I don't know the answer to that :(
macek
+1  A: 

For Windows, you can use Gdiplus. Create a GraphicsPath and call AddString() on it.

Then examine the PathData or PathPoints.

Tom Sirgedas
A: 

Maybe you can use a font library such as FreeType 2 to decode the font?

divideandconquer.se
A: 

Inkscape is free and includes text to path functionality. Some Inkscape functionality is command driven, but I don't know whether that will handle your problem exactly. Inkscape's native format is SVG.

brainjam