2010-10-22 54 views
5

J'aimerais pouvoir extraire la géométrie pour chaque lettre d'un fichier de police TrueType. Chaque lettre aurait un ensemble de coordonnées, en supposant que chaque lettre est dans sa propre grille.Extraire la géométrie à partir de la police

Comme une image vaut mille mots - Je voudrais obtenir les sommets pour des lettres semblables à l'image ci-dessous (avec la permission de http://polymaps.org/)

alt text

Mise à jour

Merci à l'indice pour utiliser GDI, qui est maintenant incorporé dans .NET System.Drawing.Drawing2D J'ai obtenu le code suivant pour créer des polygones WKT. Aucune courbe de Bézier possible. Et même après que les lettres aient été retournées et tournées, certains chemins ne se rejoignaient pas correctement.

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

+0

Je suppose qu'il serait assez facile d'afficher du texte dans Adobe Illustrator et de convertir le texte en chemin. C'est plus une question pour superuser.com, cependant. –

+0

J'espérais le faire sans un logiciel coûteux, et construit autour d'un script réutilisable – geographika

+2

à propos de votre 'A' semblant incorrect: Le problème est qu'il y a deux chemins. En plus de PathData, vous devrez regarder le tableau parallèle PathTypes http://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.graphicspath.pathtypes.aspx. Quand le type d'un point est 0, vous devez fermer le dernier chiffre et en commencer un nouveau. –

Répondre

2

Pour Windows, vous pouvez utiliser Gdiplus. Créez un GraphicsPath et appelez AddString() dessus.

Ensuite, examinez PathData ou PathPoints.

2

alt text

Dans Adobe Illustrator

Object Menu > Expand... 

Cela va convertir le texte à des chemins en ancres et des courbes de Bézier. En dehors de l'utilisation d'une application, je ne sais pas comment le faire par programmation.

+0

Très bien. Jamais utilisé Illustrator, mais je vois qu'il y a un procès disponible en téléchargement. Inclut-il un langage de script pour extraire les coordonnées au texte? – geographika

+0

Malheureusement, je ne connais pas la réponse à cette question :( –

0

Peut-être que vous pouvez utiliser une bibliothèque de polices telle que FreeType 2 pour décoder la police?

0

est gratuit et inclut text to path functionality. Certaines fonctionnalités d'Inkscape sont commandées par des commandes, mais je ne sais pas si cela permettra de gérer votre problème exactement. Le format natif d'Inkscape est SVG.

0

J'avais besoin de la sortie dans MATLAB donc j'ai pris la bonne réponse en utilisant l'interface MATLAB.NET. Le code source affiché ci-dessous

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)