Dot-Net

從字型中提取幾何圖形

  • February 20, 2022

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

替代文字 替代文字

對於 Windows,您可以使用 Gdiplus。創建一個GraphicsPath並在其上呼叫 AddString()。

然後檢查 PathData 或 PathPoints。

替代文字

在 Adob​​e Illustrator 中

Object Menu > Expand...

這會將文本轉換為由錨點和貝塞爾曲線組成的路徑。

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

引用自:https://stackoverflow.com/questions/3999975