2010-10-22 57 views
5

我希望能够为TrueType字体文件中的每个字母提取几何图形。每个字母都有一组坐标,假设每个字母都在自己的网格中。从字体中提取几何图形

为图片讲述了千言万语 - 我想获得类似下面(的http://polymaps.org/提供)

alt text

更新

多亏了像信件顶点使用GDI的提示,现在已经合并到.NET System.Drawing.Drawing2D中,我得到了以下代码来创建WKT多边形。没有贝塞尔曲线可能。即使在翻转和旋转字母后,仍有一些路径无法正确连接。

 // C# Visual Studio 

     GraphicsPath gp = new GraphicsPath(); 

     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

+0

我猜这将是很容易在Adobe Illustrator中弹出一些文本并将文本转换为路径。不过,这对于superuser.com来说更是一个问题。 – 2010-10-22 19:02:26

+0

我希望能够在没有昂贵的软件包的情况下做到这一点,并围绕一个可重复使用的脚本 – geographika 2010-10-22 19:05:34

+2

围绕你的'A'看起来不正确:问题是有两条路径。除了PathData之外,您还需要查看并行数组PathTypes http://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.graphicspath.pathtypes.aspx。当一个点的类型为0时,您需要关闭最后一个数字并开始一个新数字。 – 2010-10-23 18:26:39

回答

2

对于Windows,您可以使用Gdiplus。创建一个GraphicsPath并在其上调用AddString()。

然后检查PathData或PathPoints。

2

alt text

在Adobe Illustrator

Object Menu > Expand... 

这将文本转换到由锚和贝塞尔曲线的路径。

除了使用应用程序,我不知道如何以编程方式执行此操作。

+0

非常好。从来没有使用Illustrator,但我看到有一个试用版可供下载。它是否包含脚本语言以将坐标提取到文本? – geographika 2010-10-22 19:18:28

+0

不幸的是,我不知道答案:( – 2010-10-22 19:21:40

0

也许你可以使用字体库,如FreeType 2来解码字体?

0

我需要MATLAB中的输出,因此使用MATLAB.NET接口正确标记了答案。下面贴出的源代码

clear all 
% Import .NET Framework System.Drawing 
NET.addAssembly('System.Drawing');  

% Display all available System Fonts (optional) 
    AvailableFonts = System.Drawing.Text.InstalledFontCollection(); 
    for i=1:AvailableFonts.Families.Length 
     disp(AvailableFonts.Families(i).Name); 
    end 

% Get GraphicsPath of chosen Font and text string 
% https://msdn.microsoft.com/en-us/library/ms142533(v=vs.110).aspx 

FontData= System.Drawing.Drawing2D.GraphicsPath(); 

text='Hello World'; 
font=System.Drawing.FontFamily('Arial'); 
style=cast(System.Drawing.FontStyle.Regular,'Int32'); 
emSize=48; 
origin=System.Drawing.Point(0,0); 
format=System.Drawing.StringFormat(); 

FontData.AddString(text,font,style,emSize,origin,format); 

%Extract X,Y data from FontData 

for i=1:FontData.PathPoints.Length 
    x(i)=FontData.PathPoints(i).X; 
    y(i)=-FontData.PathPoints(i).Y; 
end 

plot(x,y)