public void DrawChar(Graphics g, int x, int y, FontChar ch) { if (ch == null || g == null) { return; } x += ch.xoffset; y += ch.yoffset; g.DrawImage(ch.image, new Point(x, y)); }
public AngelFont(string path) { string xml = File.ReadAllText(path); var root = XMLReader.ReadFromString(xml); path = Path.GetDirectoryName(path); if (!"font".Equals(root.Name)) { root = root.GetNode("font"); } var info = root.GetNode("info"); //name = info.ReadString("face"); name = Path.GetFileNameWithoutExtension(path); info = root.GetNode("common"); this.height = info.GetInt32("lineHeight"); this.baseY = info.GetInt32("base"); int kerning = info.GetInt32("kerning"); var pages = root.GetNode("pages"); foreach (var child in pages.Children) { int id = child.GetInt32("id"); string imageFilename = child.GetString("file"); imageFilename = Path.Combine(path, imageFilename); var bitmap = new Bitmap(Image.FromFile(imageFilename)); this.pages.Add(id, bitmap); } var chars = root.GetNode("chars"); foreach (var child in chars.Children) { if (!child.Name.ToLower().Equals("char")) { continue; } var c = new FontChar(); int id = child.GetInt32("id"); c.x = child.GetInt32("x"); c.y = child.GetInt32("y"); c.width = child.GetInt32("width"); c.height = child.GetInt32("height"); c.xoffset = child.GetInt32("xoffset"); c.yoffset = child.GetInt32("yoffset"); c.xadvance = child.GetInt32("xadvance") + (id != 32 ? kerning : 0); c.page = child.GetInt32("page"); // An empty bitmap which will hold the cropped image c.image = new Bitmap(c.width, c.height); var g = Graphics.FromImage(c.image); var page = this.pages[c.page]; var section = new Rectangle(c.x, c.y, c.width, c.height); // Draw the given area (section) of the source image // at location 0,0 on the empty bitmap (bmp) g.DrawImage(page, 0, 0, section, GraphicsUnit.Pixel); this.chars.Add(id, c); } lineAdvance = this.chars[(int)'H'].height; }