using System; using System.Collections.Generic; using System.Drawing; using System.Xml; namespace VPScriptEditor { public class FontFile { private Dictionary data; private Symbol[] glyphs; private Bitmap[] bitmaps; private class Symbol { private bool[,] data; private int width; private char text; public char getText() { return text; } public bool getXY(int x, int y) { return data[x, y]; } public int getWidth() { return width; } public Symbol(XmlAttributeCollection attrs) { data = new bool[12, 12]; for (int i = 0; i < attrs.Count; i++) { switch (attrs[i].Name) { case "Width": width = int.Parse(attrs[i].Value); break; case "Data": byte[] bdata = Convert.FromBase64String(attrs[i].Value); for (int y = 0; y < 12; y++) { int curr = bdata[y * 2 + 1] * 256 + bdata[y * 2]; for (int x = 0; x < 12; x++) if ((curr & (0x8000 >> x)) > 0) data[x, y] = true; else data[x, y] = false; } break; case "Text": if (attrs[i].Value.Length != 1) throw new Exception("Invalid database, character " + attrs[i].Value + " not supported"); text = attrs[i].Value[0]; break; default: throw new Exception(("Wrong Font XML Format - Unknown attribute: " + attrs[i].Name)); } } } } public FontFile(System.IO.StreamReader file, Color color) { XmlDocument fontXml = new XmlDocument(); fontXml.Load(file); XmlNodeList fontNode = fontXml.GetElementsByTagName("Symbol"); glyphs = new Symbol[fontNode.Count]; bitmaps = new Bitmap[fontNode.Count]; data = new Dictionary(); for (int i = 0; i < fontNode.Count; i++) { glyphs[i] = new Symbol(fontNode[i].Attributes); data.Add(glyphs[i].getText(), i); bitmaps[i] = new Bitmap(glyphs[i].getWidth(), 12); for (int y = 0; y < 12; y++) for (int x = 0; x < glyphs[i].getWidth(); x++) if (glyphs[i].getXY(x, y)) bitmaps[i].SetPixel(x, y, color); } } public bool containsSymbol(char symbol) { return data.ContainsKey(symbol); } public int getSymbolWidth(char symbol) { return glyphs[data[symbol]].getWidth(); } public bool getSymbolXY(char symbol, int x, int y) { return glyphs[data[symbol]].getXY(x, y); } public Bitmap getBitmap(char symbol) { return bitmaps[data[symbol]]; } } }