【问题标题】:Extract Geometry from Font从字体中提取几何图形
【发布时间】:2010-10-22 18:51:21
【问题描述】:

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

正如一张图片告诉一千个单词 - 我想获得类似于下图的字母的顶点(http://polymaps.org/ 提供)

更新

感谢使用 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());

【问题讨论】:

  • 我猜在 Adob​​e Illustrator 中弹出一些文本并将文本转换为路径会很容易。不过,这对于 superuser.com 来说更像是一个问题。
  • 我希望在没有昂贵的软件包的情况下做到这一点,并围绕可重用​​的脚本构建
  • 关于您的“A”看起来不正确:问题是有两条路径。除了 PathData,您还需要查看并行数组 PathTypes msdn.microsoft.com/en-us/library/… 。当一个点的类型为 0 时,您需要关闭最后一个图并开始一个新的。
  • 哦,你也可以在 GraphicsPath 上调用“Flatten()”来将贝塞尔曲线转换成直线段。

标签: .net vector fonts geometry extract


【解决方案1】:

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

然后检查 PathData 或 PathPoints。

【讨论】:

    【解决方案2】:

    在 Adob​​e Illustrator 中

    Object Menu > Expand...
    

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

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

    【讨论】:

    • 非常好。从未使用过 Illustrator,但我看到有可供下载的试用版。它是否包含将坐标提取为文本的脚本语言?
    • 不幸的是,我不知道答案:(
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 2013-02-11
        • 1970-01-01
        • 1970-01-01
        • 2011-05-14
        • 2013-04-16
        • 2023-03-03
        • 2019-10-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多