Exemplo n.º 1
0
        public override String Decode(PdfString content)
        {
            byte[]        contentBytes = content.GetValueBytes();
            StringBuilder builder      = new StringBuilder(contentBytes.Length);

            foreach (byte b in contentBytes)
            {
                int uni = fontEncoding.GetUnicode(b & 0xff);
                if (uni > -1)
                {
                    builder.Append((char)(int)uni);
                }
                else
                {
                    if (fontEncoding.GetBaseEncoding() == null)
                    {
                        Glyph glyph = fontProgram.GetGlyphByCode(b & 0xff);
                        if (glyph != null && glyph.GetChars() != null)
                        {
                            builder.Append(glyph.GetChars());
                        }
                    }
                }
            }
            return(builder.ToString());
        }
Exemplo n.º 2
0
        /// <summary><inheritDoc/></summary>
        public override GlyphLine DecodeIntoGlyphLine(PdfString content)
        {
            byte[]        contentBytes = content.GetValueBytes();
            IList <Glyph> glyphs       = new List <Glyph>(contentBytes.Length);

            foreach (byte b in contentBytes)
            {
                int   code  = b & 0xff;
                Glyph glyph = null;
                if (toUnicode != null && toUnicode.Lookup(code) != null && (glyph = fontProgram.GetGlyphByCode(code)) != null
                    )
                {
                    if (!JavaUtil.ArraysEquals(toUnicode.Lookup(code), glyph.GetChars()))
                    {
                        // Copy the glyph because the original one may be reused (e.g. standard Helvetica font program)
                        glyph = new Glyph(glyph);
                        glyph.SetChars(toUnicode.Lookup(code));
                    }
                }
                else
                {
                    int uni = fontEncoding.GetUnicode(code);
                    if (uni > -1)
                    {
                        glyph = GetGlyph(uni);
                    }
                    else
                    {
                        if (fontEncoding.GetBaseEncoding() == null)
                        {
                            glyph = fontProgram.GetGlyphByCode(code);
                        }
                    }
                }
                if (glyph != null)
                {
                    glyphs.Add(glyph);
                }
            }
            return(new GlyphLine(glyphs));
        }
Exemplo n.º 3
0
        protected internal virtual void FlushFontData(String fontName, PdfName subtype)
        {
            GetPdfObject().Put(PdfName.Subtype, subtype);
            if (fontName != null)
            {
                GetPdfObject().Put(PdfName.BaseFont, new PdfName(fontName));
            }
            int firstChar;
            int lastChar;

            for (firstChar = 0; firstChar < 256; ++firstChar)
            {
                if (shortTag[firstChar] != 0)
                {
                    break;
                }
            }
            for (lastChar = 255; lastChar >= firstChar; --lastChar)
            {
                if (shortTag[lastChar] != 0)
                {
                    break;
                }
            }
            if (firstChar > 255)
            {
                firstChar = 255;
                lastChar  = 255;
            }
            if (!IsSubset() || !IsEmbedded())
            {
                firstChar = 0;
                lastChar  = shortTag.Length - 1;
                for (int k = 0; k < shortTag.Length; ++k)
                {
                    // remove unsupported by encoding values in case custom encoding.
                    // save widths information in case standard pdf encodings (winansi or macroman)
                    if (fontEncoding.CanDecode(k))
                    {
                        shortTag[k] = 1;
                    }
                    else
                    {
                        if (!fontEncoding.HasDifferences() && fontProgram.GetGlyphByCode(k) != null)
                        {
                            shortTag[k] = 1;
                        }
                        else
                        {
                            shortTag[k] = 0;
                        }
                    }
                }
            }
            if (fontEncoding.HasDifferences())
            {
                // trim range of symbols
                for (int k = firstChar; k <= lastChar; ++k)
                {
                    if (!FontConstants.notdef.Equals(fontEncoding.GetDifference(k)))
                    {
                        firstChar = k;
                        break;
                    }
                }
                for (int k_1 = lastChar; k_1 >= firstChar; --k_1)
                {
                    if (!FontConstants.notdef.Equals(fontEncoding.GetDifference(k_1)))
                    {
                        lastChar = k_1;
                        break;
                    }
                }
                PdfDictionary enc = new PdfDictionary();
                enc.Put(PdfName.Type, PdfName.Encoding);
                PdfArray diff = new PdfArray();
                bool     gap  = true;
                for (int k_2 = firstChar; k_2 <= lastChar; ++k_2)
                {
                    if (shortTag[k_2] != 0)
                    {
                        if (gap)
                        {
                            diff.Add(new PdfNumber(k_2));
                            gap = false;
                        }
                        diff.Add(new PdfName(fontEncoding.GetDifference(k_2)));
                    }
                    else
                    {
                        gap = true;
                    }
                }
                enc.Put(PdfName.Differences, diff);
                GetPdfObject().Put(PdfName.Encoding, enc);
            }
            else
            {
                if (!fontEncoding.IsFontSpecific())
                {
                    GetPdfObject().Put(PdfName.Encoding, PdfEncodings.CP1252.Equals(fontEncoding.GetBaseEncoding()) ? PdfName.
                                       WinAnsiEncoding : PdfName.MacRomanEncoding);
                }
            }
            if (IsForceWidthsOutput() || !IsBuiltInFont() || fontEncoding.HasDifferences())
            {
                GetPdfObject().Put(PdfName.FirstChar, new PdfNumber(firstChar));
                GetPdfObject().Put(PdfName.LastChar, new PdfNumber(lastChar));
                PdfArray wd = new PdfArray();
                for (int k = firstChar; k <= lastChar; ++k)
                {
                    if (shortTag[k] == 0)
                    {
                        wd.Add(new PdfNumber(0));
                    }
                    else
                    {
                        //prevent lost of widths info
                        int   uni   = fontEncoding.GetUnicode(k);
                        Glyph glyph = uni > -1 ? GetGlyph(uni) : fontProgram.GetGlyphByCode(k);
                        wd.Add(new PdfNumber(glyph != null ? glyph.GetWidth() : 0));
                    }
                }
                GetPdfObject().Put(PdfName.Widths, wd);
            }
            PdfDictionary fontDescriptor = !IsBuiltInFont() ? GetFontDescriptor(fontName) : null;

            if (fontDescriptor != null)
            {
                GetPdfObject().Put(PdfName.FontDescriptor, fontDescriptor);
                fontDescriptor.Flush();
            }
        }