private XmlDocument GetXmlDocument(XmlSavingOptions options)
        {
            XmlDocument doc = new XmlDocument();

            doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", null));
            XmlNode root = doc.AppendChild(doc.CreateElement("data"));

            (root as XmlElement).SetAttribute("type", "image");
            (root as XmlElement).SetAttribute("filename", this.mFileName);
            (root as XmlElement).SetAttribute("name", Path.GetFileNameWithoutExtension(this.mFileName));

            XmlNode nodeDefinitions = root.AppendChild(doc.CreateElement("definitions"));

            for (int i = 0; i < 256; i++)
            {
                XmlNode nodeValue = nodeDefinitions.AppendChild(doc.CreateElement("value"));
                (nodeValue as XmlElement).SetAttribute("text", Convert.ToString(i, 2).PadLeft(8, '0'));
                (nodeValue as XmlElement).SetAttribute("byte", String.Format("{0:X2}", i));
            }

            //XmlNode nodeImage = root.AppendChild(doc.CreateElement("item"));
            XmlNode nodeBitmap = root.AppendChild(doc.CreateElement("bitmap"));

            BitmapHelper.SaveToXml(this.mEditor.BmpEditor.Bmp, nodeBitmap, options);
            return(doc);
        }
 public Options()
 {
     this.mImageStyleFilename = String.Empty;
     this.mFontStyleFilename  = String.Empty;
     this.mXmlSavingOptions   = new XmlSavingOptions();
     this.mSetBitsByDefault   = false;
 }
        private XmlDocument GetXmlDocument(XmlSavingOptions options)
        {
            XmlDocument doc = new XmlDocument();

            doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", null));
            XmlNode root = doc.AppendChild(doc.CreateElement("data"));

            (root as XmlElement).SetAttribute("type", "font");
            (root as XmlElement).SetAttribute("filename", this.mFileName);
            (root as XmlElement).SetAttribute("name", Path.GetFileNameWithoutExtension(this.mFileName));

            //(root as XmlElement).SetAttribute("family", this.mFontEdCtrl.FontContainer.FontFamily);
            //(root as XmlElement).SetAttribute("size", Convert.ToString(this.mFontEdCtrl.FontContainer.Size, CultureInfo.InvariantCulture));
            //string stylestr = Enum.Format(typeof(FontStyle), this.mFontEdCtrl.FontContainer.Style, "g");
            //(root as XmlElement).SetAttribute("style", stylestr);
            //FontStyle fs = (FontStyle)Enum.Parse(typeof(FontStyle), stylestr);
            root.AppendChild(doc.CreateElement("family")).InnerText = this.mFontEdCtrl.FontContainer.FontFamily.GetName(CultureInfo.InvariantCulture.LCID);
            root.AppendChild(doc.CreateElement("size")).InnerText   = Convert.ToString(this.mFontEdCtrl.FontContainer.Size, CultureInfo.InvariantCulture);
            root.AppendChild(doc.CreateElement("style")).InnerText  = Enum.Format(typeof(FontStyle), this.mFontEdCtrl.FontContainer.Style, "g");;
            XmlNode nodeCharset = root.AppendChild(doc.CreateElement("string"));

            XmlNode nodeDefinitions = root.AppendChild(doc.CreateElement("definitions"));

            for (int i = 0; i < 256; i++)
            {
                XmlNode nodeValue = nodeDefinitions.AppendChild(doc.CreateElement("value"));
                (nodeValue as XmlElement).SetAttribute("text", Convert.ToString(i, 2).PadLeft(8, '0'));
                (nodeValue as XmlElement).SetAttribute("byte", String.Format("{0:X2}", i));
            }
            StringBuilder allChars  = new StringBuilder();
            XmlNode       nodeChars = root.AppendChild(doc.CreateElement("chars"));

            foreach (char c in this.mFontEdCtrl.FontContainer.CharBitmaps.Keys)
            {
                allChars.Append(c);
                XmlNode nodeChar = nodeChars.AppendChild(doc.CreateElement("char"));
                (nodeChar as XmlElement).SetAttribute("character", Convert.ToString(c));
                //(nodeChar as XmlElement).SetAttribute("unicode", this.Bytes2String(Encoding.Unicode.GetBytes(new char[] { c })));
                //(nodeChar as XmlElement).SetAttribute("utf7", this.Bytes2String(Encoding.UTF7.GetBytes(new char[] { c })));
                //(nodeChar as XmlElement).SetAttribute("utf8", this.Bytes2String(Encoding.UTF8.GetBytes(new char[] { c })));
                //(nodeChar as XmlElement).SetAttribute("utf32", this.Bytes2String(Encoding.UTF32.GetBytes(new char[] { c })));
                //(nodeChar as XmlElement).SetAttribute("ascii", this.Bytes2String(Encoding.ASCII.GetBytes(new char[] { c })));
                this.AddCharAtEncoding(Encoding.ASCII, c, nodeChar);
                this.AddCharAtEncoding(Encoding.Unicode, c, nodeChar);
                this.AddCharAtEncoding(Encoding.UTF32, c, nodeChar);
                this.AddCharAtEncoding(Encoding.UTF7, c, nodeChar);
                this.AddCharAtEncoding(Encoding.UTF8, c, nodeChar);
                XmlNode nodeBitmap = nodeChar.AppendChild(doc.CreateElement("bitmap"));
                BitmapHelper.SaveToXml(this.mFontEdCtrl.FontContainer.CharBitmaps[c], nodeBitmap, options);
            }
            nodeCharset.InnerText = allChars.ToString();
            return(doc);
        }
        public static void SaveToXml(Bitmap sourceBitmap, XmlNode node, XmlSavingOptions options)
        {
            Bitmap bmp = BitmapHelper.RotateFlip(sourceBitmap, options.FlipHorizontal, options.FlipVertical, options.Angle);

            if (options.Inverse)
            {
                bmp = BitmapHelper.Inverse(bmp);
            }
            if ((bmp.Width % 8) != 0)
            {
                if (options.AlignRight)
                {
                    bmp = BitmapHelper.Resize(bmp, 8 - bmp.Width % 8, 0, 0, 0);
                }
            }
            //separate node for bitmap's data
            //XmlNode nodeBitmap = node.AppendChild(node.OwnerDocument.CreateElement("bitmap"));
            XmlNode nodeBitmap = node;

            //bitmap info
            (nodeBitmap as XmlElement).SetAttribute("width", Convert.ToString(sourceBitmap.Width, CultureInfo.InvariantCulture));
            (nodeBitmap as XmlElement).SetAttribute("height", Convert.ToString(sourceBitmap.Height, CultureInfo.InvariantCulture));
            //preview node, all bits at one line
            XmlNode nodePreview = nodeBitmap.AppendChild(node.OwnerDocument.CreateElement("preview"));

            BitmapData bmd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format1bppIndexed);

            for (int y = 0; y < bmp.Height; y++)
            {
                //bitmap's line
                XmlNode nodeLine = nodeBitmap.AppendChild(node.OwnerDocument.CreateElement("line"));
                (nodeLine as XmlElement).SetAttribute("index", Convert.ToString(y, CultureInfo.InvariantCulture));

                StringBuilder byteData = new StringBuilder();
                StringBuilder lineData = new StringBuilder();
                for (int x = 0; x < bmp.Width; x++)
                {
                    //if (bmp.GetPixel(x, y).GetBrightness() > this.mBrightnessEdge)
                    if (BitmapHelper.GetPixel(bmd, x, y))
                    {
                        byteData.Append("1");
                    }
                    else
                    {
                        byteData.Append("0");
                    }
                    //if byte filled or end of line
                    if ((x % 8) == 7 || x == (bmp.Width - 1))
                    {
                        XmlNode nodeColumn = nodeLine.AppendChild(node.OwnerDocument.CreateElement("column"));
                        (nodeColumn as XmlElement).SetAttribute("index", Convert.ToString((int)(x / 8), CultureInfo.InvariantCulture));

                        //ensure what 8 bits (1 byte)
                        while (byteData.Length < 8)
                        {
                            byteData.Append("0");
                        }

                        nodeColumn.InnerText = byteData.ToString();
                        lineData.Append(byteData);
                        if (options.MirrorEachByte)
                        {
                            string str = byteData.ToString();
                            byteData.Length = 0;
                            foreach (char c in str)
                            {
                                byteData.Insert(0, c);
                            }
                            nodeColumn.InnerText = byteData.ToString();
                        }
                        byteData.Length = 0;
                    }
                }
                lineData = lineData.Replace('0', '_').Replace('1', '#');
                XmlNode nodePreviewLine = nodePreview.AppendChild(node.OwnerDocument.CreateElement("line"));
                nodePreviewLine.InnerText = lineData.ToString();
            }
            bmp.UnlockBits(bmd);
        }