从字体中提取几何图形

发布于 2024-09-28 08:55:57 字数 1944 浏览 4 评论 0 原文

我希望能够提取 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());

替代文本 替代文字

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.

        // 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁月染过的梦 2024-10-05 08:55:57

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

然后检查 PathData 或 PathPoints。

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

Then examine the PathData or PathPoints.

暖阳 2024-10-05 08:55:57

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

I needed the output in MATLAB so took the correctly marked answer using the MATLAB.NET interface. Source code posted below

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)
挖个坑埋了你 2024-10-05 08:55:57

alt text

在 Adob​​e Illustrator 中

Object Menu > Expand...

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

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

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.

旧城空念 2024-10-05 08:55:57

Inkscape 是免费的,包含 文本到路径功能。一些 Inkscape 功能是命令驱动的,但我不知道这是否能准确解决您的问题。 Inkscape 的原生格式是 SVG。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文