代码之家  ›  专栏  ›  技术社区  ›  geographika

从字体提取几何图形

  •  7
  • geographika  · 技术社区  · 14 年前

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

    如图所示,我想得到与下图相似的字母的顶点。 http://polymaps.org/ )

    alt text

    更新

    多亏了使用gdi的提示,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

    5 回复  |  直到 8 年前
        1
  •  3
  •   Tom Sirgedas    14 年前

    对于Windows,您可以使用gdiplus。创建 GraphicsPath 并对其调用addString()。

    然后检查路径数据或路径点。

        2
  •  2
  •   maček    14 年前

    alt text

    在Adobe Illustrator中

    Object Menu > Expand...
    

    这将把文本转换成由锚定和贝塞尔曲线组成的路径。

    除了使用应用程序之外,我不知道如何通过编程实现这一点。

        3
  •  0
  •   activout.se    14 年前

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

        4
  •  0
  •   brainjam    14 年前

    Inkscape 免费,包括 text to path functionality . 一些inkscape功能是命令驱动的,但我不知道这是否能准确地处理您的问题。Inkscape的本地格式是SVG。

        5
  •  0
  •   Marky0    8 年前

    我需要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)