For an application I need to draw text/glyphs as a vector based path. Using GDI+ GraphicsPath and Graphics.DrawPath or WPF FormattedText and Geometry works fine and I get the output as shown in the first picture. But is it somehow possible to get letters as a single line path as shown in the second picture (shows an L).
I'm glad if anyone has an idea.
See this example. Using built-in function will always give you the path of a letter containing it's inner and outer borderline. Only the first one is made of single strokes which results in a much shorter path.
Single stroke fonts define themselves by the filled area of the font. TTF (outline) fonts define each character by the stroke surrounding the filled area. So, they necessarily form a closed shape. In other words, it's not possible to have a "true single stroke" outline font, because single stroke fonts only have a filled area. The .NET Framework only supports TTF fonts.
Luckily, there are some fonts that emulate single-stroke behavior by closing the outline strokes in on themselves. Mostly, they are used by CNC plotting software. Here is a link to the zip file containing the same fonts that @Simon Mourier suggested using.
I experimented with the first one and indeed I could not see a separate path for the enclosed areas. I did write some code that makes the strokes of a regular font close in on themselves, but the curved areas disappear in some spots. Whatever internal algorithm .NET uses to try to create a 1px path from a squashed outline just doesn't work as well as using a well designed font. So, this is as good as it's going to get using .NET.
You can use this code to see what each font produces after you install them. Or, I guess you could just try them in your software :) Either way, I hope this is useful for you.
This is the output of Graphics.DrawPath
, NOT Graphics.FillPath
.
private void button1_Click(object sender, EventArgs e) {
DrawText("single stroke ttf engraving fonts");
}
private void DrawText(string text) {
using (Graphics g = panel.CreateGraphics())
using (Font font = new System.Drawing.Font("1CamBam_Stick_1", 50, FontStyle.Regular))
using (GraphicsPath gp = new GraphicsPath())
using (StringFormat sf = new StringFormat()) {
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
gp.AddString(text, font.FontFamily, (int)font.Style, font.Size, panel.ClientRectangle, sf);
g.Clear(Color.Black);
g.DrawPath(Pens.Red, gp);
}
}
Also, Here is a very related article to read if you plan on doing alot of this. http://tipsandtricks.rolanddga.com/software/how-to-generate-single-line-fonts-for-use-with-dr-engrave/
来源:https://stackoverflow.com/questions/13797787/outline-of-a-text-as-single-line-vector-path