簡體   English   中英

從字體中提取幾何圖形

[英]Extract Geometry from Font

我希望能夠提取 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...

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

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

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

也許您可以使用諸如FreeType 2 之類的字體庫來解碼字體?

Inkscape是免費的並且包括文本到路徑功能 某些 Inkscape 功能是命令驅動的,但我不知道這是否能准確地處理您的問題。 Inkscape 的原生格式是 SVG。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM