Пример #1
0
        /// <summary>
        /// Save font. It will create this
        ///  <font Name="something" StemV="20" Width="[234 34]"><EmbeddedFont>dasjdlkajsdljxlkjsaldjasldjxflj</embeddedFont></font>
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="element"></param>
        public void Save(System.Xml.XmlDocument doc, System.Xml.XmlElement element)
        {
            // save font
            XmlElement   el   = doc.CreateElement("Font");
            XmlAttribute attr = doc.CreateAttribute("Name");

            attr.Value = this.Font.Name;
            el.SetAttributeNode(attr);

            attr       = doc.CreateAttribute("SaveID");
            attr.Value = this.SaveID.ToString();
            el.SetAttributeNode(attr);

            // save metrics
            attr       = doc.CreateAttribute("EmHeight");
            attr.Value = this.Font.FontFamily.GetEmHeight(this.Font.Style).ToString();
            el.SetAttributeNode(attr);

            attr       = doc.CreateAttribute("Ascent");
            attr.Value = this.Font.FontFamily.GetCellAscent(this.font.Style).ToString();
            el.SetAttributeNode(attr);

            attr       = doc.CreateAttribute("Descent");
            attr.Value = this.Font.FontFamily.GetCellDescent(this.font.Style).ToString();
            el.SetAttributeNode(attr);

            attr       = doc.CreateAttribute("Bold");
            attr.Value = this.Font.Bold ? "1" : "0";
            el.SetAttributeNode(attr);

            attr       = doc.CreateAttribute("Italic");
            attr.Value = this.Font.Italic ? "1" : "0";
            el.SetAttributeNode(attr);

            //  Get font metrics to save some properties of font
            FontMetrics metrics = new FontMetrics(this.Font);

            attr       = doc.CreateAttribute("EmbeddingLicense");
            attr.Value = ((int)metrics.EmbeddingLicense).ToString();
            el.SetAttributeNode(attr);

            attr       = doc.CreateAttribute("ItalicAngle");
            attr.Value = metrics.ItalicAngle.ToString();
            el.SetAttributeNode(attr);

            attr = doc.CreateAttribute("BBoxLeft"); attr.Value = metrics.FontBBox.Left.ToString(); el.SetAttributeNode(attr);
            attr = doc.CreateAttribute("BBoxRight"); attr.Value = metrics.FontBBox.Right.ToString(); el.SetAttributeNode(attr);
            attr = doc.CreateAttribute("BBoxTop"); attr.Value = metrics.FontBBox.Top.ToString(); el.SetAttributeNode(attr);
            attr = doc.CreateAttribute("BBoxBottom"); attr.Value = metrics.FontBBox.Bottom.ToString(); el.SetAttributeNode(attr);

            attr       = doc.CreateAttribute("StemV");
            attr.Value = metrics.StemV.ToString();
            el.SetAttributeNode(attr);

            attr       = doc.CreateAttribute("Flags");
            attr.Value = metrics.Flags.ToString();
            el.SetAttributeNode(attr);

            attr = doc.CreateAttribute("FirstChar"); attr.Value = metrics.FirstChar.ToString(); el.SetAttributeNode(attr);
            attr = doc.CreateAttribute("LastChar"); attr.Value = metrics.LastChar.ToString(); el.SetAttributeNode(attr);

            // make glyph widths
            string glyphwidth = "[ ";

            for (int i = metrics.FirstChar; i <= metrics.LastChar; i++)
            {
                int gw = metrics.GetGlyphWidth(this.font, i);
                glyphwidth += gw.ToString() + " ";
            }
            glyphwidth += " ]";

            attr = doc.CreateAttribute("Widths"); attr.Value = glyphwidth; el.SetAttributeNode(attr);

            // embedd font if not std font
            if (!this.IsStandardFont)
            {
                byte[] fontBuffer = FontMetrics.GetFontData(this.Font);
                attr = doc.CreateAttribute("EmbeddedDecodedFontLength"); attr.Value = fontBuffer.Length.ToString(); el.SetAttributeNode(attr);

                XmlText textValue = doc.CreateTextNode("EmdeddedFont");
                textValue.Value = Convert.ToBase64String(fontBuffer);
                attr            = doc.CreateAttribute("EmbeddedFontLength"); attr.Value = textValue.Value.Length.ToString(); el.SetAttributeNode(attr);
                el.AppendChild(textValue);
            }
            element.AppendChild(el);
        }