private void Import(string xmlInfo, string glyphs) { Bitmap image = (Bitmap)Image.FromFile(glyphs); XDocument doc = XDocument.Load(xmlInfo); XElement root = doc.Element("NFTR"); this.MagicStamp = "NFTR"; this.VersionS = root.Element("Version").Value; this.errorChar = ushort.Parse(root.Element("ErrorChar").Value); this.lineGap = byte.Parse(root.Element("LineGap").Value); this.boxWidth = byte.Parse(root.Element("BoxWidth").Value); this.boxHeight = byte.Parse(root.Element("BoxHeight").Value); this.glyphWidth = byte.Parse(root.Element("GlyphWidth").Value); this.glyphHeight = byte.Parse(root.Element("GlyphHeight").Value); this.depth = byte.Parse(root.Element("Depth").Value); this.defaultWidth = GWidth.FromXml(root.Element("DefaultWidth")); this.rotation = (RotationMode)Enum.Parse(typeof(RotationMode), root.Element("Rotation").Value); this.encoding = (EncodingMode)Enum.Parse(typeof(EncodingMode), root.Element("Encoding").Value); // Gets Width regions XElement xwidths = root.Element("Widths"); Cwdh.WidthRegion[] widthRegs = new Cwdh.WidthRegion[xwidths.Elements("Region").Count()]; // ... gets the data from the xml foreach (XElement xreg in xwidths.Elements("Region")) { int id = int.Parse(xreg.Element("Id").Value); widthRegs[id] = new Cwdh.WidthRegion(this.defaultWidth); widthRegs[id].Id = id; widthRegs[id].FirstChar = Convert.ToUInt16(xreg.Element("FirstChar").Value, 16); widthRegs[id].LastChar = Convert.ToUInt16(xreg.Element("LastChar").Value, 16); } // ... assign the next region for (int i = 0; i < widthRegs.Length; i++) { if (i + 1 == widthRegs.Length) widthRegs[i].NextRegion = null; else widthRegs[i].NextRegion = widthRegs[i + 1]; } // Gets Cmap regions XElement xcmaps = root.Element("Maps"); this.cmaps = new Cmap[xcmaps.Elements("Map").Count()]; foreach (XElement xmap in xcmaps.Elements("Map")) { int id = int.Parse(xmap.Element("Id").Value); this.cmaps[id] = new Cmap(this); this.cmaps[id].Id = id; this.cmaps[id].Type = Convert.ToUInt32(xmap.Element("Type").Value); this.cmaps[id].FirstChar = Convert.ToUInt16(xmap.Element("FirstChar").Value, 16); this.cmaps[id].LastChar = Convert.ToUInt16(xmap.Element("LastChar").Value, 16); } // Gets Glyphs XElement xglyphs = root.Element("Glyphs"); this.glyphs = new List<Glyph>(xglyphs.Elements("Glyph").Count()); for (int i = 0; i < xglyphs.Elements("Glyph").Count(); i++) this.glyphs.Add(new Glyph()); // Reversing the glyphs, there are more probability to have high char codes first // ... so there will be less array resizing in InsertWidth and InsertChar foreach (XElement xglyph in xglyphs.Elements("Glyph").Reverse()) { int id = int.Parse(xglyph.Element("Id").Value); Glyph g = new Glyph(); g.Id = id; g.Width = GWidth.FromXml(xglyph.Element("Width")); g.Image = this.CharFromMap(image, id); g.IdMap = int.Parse(xglyph.Element("IdMap").Value); g.CharCode = Convert.ToUInt16(xglyph.Element("Code").Value, 16); this.glyphs[id] = g; if (g.Width.IdRegion >= 0) widthRegs[g.Width.IdRegion].InsertWidth(g.CharCode, g.Width, g.Id); this.cmaps[g.IdMap].InsertCharCode(g.Id, g.CharCode); } this.CreateStructure(widthRegs[0]); }
protected override void Read(Stream strIn, int size) { base.Read(strIn, size); this.finf = this.Blocks.GetByType<Finf>(0); this.cglp = this.Blocks.GetByType<Cglp>(0); this.cwdh = this.Blocks.GetByType<Cwdh>(0); this.cmaps = this.Blocks.GetByType<Cmap>().ToArray(); this.errorChar = this.finf.ErrorCharIndex; this.encoding = this.finf.Encoding; this.lineGap = this.finf.LineGap; this.defaultWidth = this.finf.DefaultWidth; this.glyphHeight = this.cglp.GlyphHeight; this.glyphWidth = this.cglp.GlyphWidth; this.boxWidth = this.cglp.BoxWidth; this.boxHeight = this.cglp.BoxHeight; this.depth = this.cglp.Depth; this.rotation = (RotationMode)this.cglp.Rotation; // Get glyphs info this.glyphs = new List<Glyph>(); for (int i = 0, idx = 0; i < this.cglp.NumGlyphs; i++) { int idMap; ushort charCode = this.SearchCharByImage(i, out idMap); if (idMap == -1) { this.cglp.GetGlyphImage(i).Save("Unvalid char " + i.ToString() + ".png"); continue; } Glyph g = new Glyph(); g.Id = idx++; g.Image = this.cglp.GetGlyph(i); g.Width = this.cwdh.GetWidth(charCode, g.Id); g.CharCode = charCode; g.IdMap = idMap; this.glyphs.Add(g); } #if DEBUG this.PrintInfo(); #endif }