在一个 Windows 应用程序(.Net 4.0)上工作,它将接受字体文件名作为输入,并将返回字体中每个字形的图像。尝试了所有可能的 Google 东西,但没有得到所有 glyph 的图像。如果得到的图像也有一些错误的输出。请建议我这样做的方法...目前有以下工作...
private void generateBitmaps(string strFontpath)
{
////Generate font object
GlyphTypeface font = new GlyphTypeface(new Uri(strFontpath));
int fontSize = 22;
int intGlyphcount = 0;
//Collect geometry of all glyphs
var Glyphs = from glyphIndex in font.CharacterToGlyphMap.Values
select font.GetGlyphOutline(glyphIndex, fontSize, 1d);
// now create the visual we'll draw them to
DrawingVisual viz = new DrawingVisual();
System.Drawing.Size cellSize = new System.Drawing.Size(fontSize, Convert.ToInt32(fontSize * font.Height));
int bitWidth = (int)Math.Ceiling(Convert.ToDecimal(cellSize.Width*10));
int bitHeight = (int)Math.Ceiling(Convert.ToDecimal(cellSize.Height * 10));
//using (DrawingContext dc = viz.RenderOpen())
{
foreach (var g in Glyphs)
{
if (intGlyphcount > font.GlyphCount)
break;
//if (g.IsEmpty())
// continue; // don't draw the blank ones
DrawingContext dc = viz.RenderOpen();
dc.PushTransform(new TranslateTransform());
System.Windows.Media.Pen glyphPen = new System.Windows.Media.Pen(System.Windows.Media.Brushes.Black, 1);
dc.DrawGeometry(System.Windows.Media.Brushes.Red, glyphPen, g);
//GeometryDrawing glyphDrawing = new GeometryDrawing(System.Windows.Media.Brushes.White, glyphPen, g);
//DrawingImage geometryImage = new DrawingImage(glyphDrawing);
//dc.DrawImage(geometryImage, new Rect(0, 0, 150, 150));
dc.Close();
//geometryImage.Freeze();
//dc.Pop(); // get rid of the transform
RenderTargetBitmap bmp = new RenderTargetBitmap(
200, 200,
96, 96,
PixelFormats.Pbgra32);
bmp.Render(viz);
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmp));
using (FileStream file = new FileStream(@"GlyphList\Glyph" + intGlyphcount++ + ".png", FileMode.Create))
encoder.Save(file);
//dc.Pop();
}
}
MessageBox.Show("Done");
}