Example #1
0
        public void SaveAsSimpleFont(Stream stream)
        {
            if (stream == null)
            {
                throw new PdfArgumentNullException("stream");
            }
            if (!stream.CanWrite)
            {
                throw new PdfArgumentException("stream");
            }
            CT_SimpleFont font = new CT_SimpleFont {
                Name = this.GetPostScriptName()
            };
            PdfRectangle fontBoundingBox = this.GetFontBoundingBox();

            font.BBox0       = fontBoundingBox.LowerLeftX;
            font.BBox1       = fontBoundingBox.LowerLeftY;
            font.BBox2       = fontBoundingBox.UpperRightX;
            font.BBox3       = fontBoundingBox.UpperRightY;
            font.Flags       = (int)this.GetFlag();
            font.ItalicAngle = this.GetItalicAngle();
            font.Ascent      = this.GetAscent();
            font.Descent     = this.GetDescent();
            font.CapHeight   = this.GetCapitalLetterHeight();
            font.StemV       = this.GetVerticalStem();
            List <CT_Width> list = new List <CT_Width>();

            foreach (KeyValuePair <int, int> pair in this.CMap.CurrentMap)
            {
                CT_Width width;
                width = new CT_Width {
                    Index = pair.Key,
                    Value = this.GetWidth(pair.Value)
                };
                list.Add(width);
            }
            font.Widths = list.ToArray();
            IStreamSerializer serializerByType = SerializerBuilder.GetSerializerByType(typeof(CT_SimpleFont));
            MemoryStream      stream2          = new MemoryStream();

            serializerByType.Serialize(stream2, font);
            stream2.Seek(0L, SeekOrigin.Begin);
            PdfFilter.FlateFilter.Encode(stream2, stream, null);
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:SimpleTrueTypeFont" /> class.
        /// </summary>
        /// <param name="fontStream">The font stream.</param>
        public SimpleTrueTypeFont(Stream fontStream)
        {
            this.widths = new Dictionary <int, float>();
            if ((fontStream == null) || !fontStream.CanRead)
            {
                throw new PdfArgumentNullException("fontStream");
            }
            byte[] buffer = new byte[(int)fontStream.Length];
            fontStream.Read(buffer, 0, buffer.Length);

            // hdt 替换原有的XmlSerializer,Release版不支持
            CT_SimpleFont font;

            using (MemoryStream stream = new MemoryStream(FlateUtility.FlateDecode(buffer, true)))
                using (XmlReader reader = XmlReader.Create(stream, _settings))
                {
                    font = new CT_SimpleFont();
                    reader.ReadToFollowing("SimpleFont");
                    font.Ascent      = Convert.ToSingle(reader.GetAttribute("Ascent"));
                    font.BBox0       = Convert.ToSingle(reader.GetAttribute("BBox0"));
                    font.BBox1       = Convert.ToSingle(reader.GetAttribute("BBox1"));
                    font.BBox2       = Convert.ToSingle(reader.GetAttribute("BBox2"));
                    font.BBox3       = Convert.ToSingle(reader.GetAttribute("BBox3"));
                    font.CapHeight   = Convert.ToSingle(reader.GetAttribute("CapHeight"));
                    font.Descent     = Convert.ToSingle(reader.GetAttribute("Descent"));
                    font.Flags       = Convert.ToInt32(reader.GetAttribute("Flags"));
                    font.ItalicAngle = Convert.ToSingle(reader.GetAttribute("ItalicAngle"));
                    font.Name        = reader.GetAttribute("Name");
                    font.StemV       = Convert.ToSingle(reader.GetAttribute("StemV"));

                    List <CT_Width> widths = new List <CT_Width>();
                    while (reader.Read() && reader.Name == "Widths" && reader.NodeType != XmlNodeType.EndElement)
                    {
                        CT_Width w = new CT_Width();
                        w.Index = Convert.ToInt32(reader.GetAttribute("Index"));
                        w.Value = Convert.ToSingle(reader.GetAttribute("Value"));
                        widths.Add(w);
                    }
                    font.Widths = widths.ToArray();
                }
            this.simpleFont = font;
            this.InitWidths();
        }