Ejemplo n.º 1
0
            public virtual byte[] CharToByte(String text, String encoding)
            {
                char[] cc  = text.ToCharArray();
                byte[] b   = new byte[cc.Length];
                int    ptr = 0;
                int    len = cc.Length;

                for (int k = 0; k < len; ++k)
                {
                    char c = cc[k];
                    if (c < 128)
                    {
                        b[ptr++] = (byte)c;
                    }
                    else
                    {
                        byte v = (byte)c2b.Get(c);
                        if (v != 0)
                        {
                            b[ptr++] = v;
                        }
                    }
                }
                if (ptr == len)
                {
                    return(b);
                }
                byte[] b2 = new byte[ptr];
                System.Array.Copy(b, 0, b2, 0, ptr);
                return(b2);
            }
Ejemplo n.º 2
0
        internal static TrueTypeFont CreateFontProgram(PdfDictionary fontDictionary, CMapToUnicode toUnicode)
        {
            iText.Kernel.Font.DocTrueTypeFont fontProgram = new iText.Kernel.Font.DocTrueTypeFont(fontDictionary);
            PdfDictionary fontDescriptor = fontDictionary.GetAsDictionary(PdfName.FontDescriptor);

            FillFontDescriptor(fontProgram, fontDescriptor);
            int dw = (fontDescriptor != null && fontDescriptor.ContainsKey(PdfName.DW)) ? (int)fontDescriptor.GetAsInt
                         (PdfName.DW) : 1000;

            if (toUnicode != null)
            {
                IntHashtable widths = FontUtil.ConvertCompositeWidthsArray(fontDictionary.GetAsArray(PdfName.W));
                fontProgram.avgWidth = 0;
                foreach (int cid in toUnicode.GetCodes())
                {
                    int   width = widths.ContainsKey(cid) ? widths.Get(cid) : dw;
                    Glyph glyph = new Glyph(cid, width, toUnicode.Lookup(cid));
                    if (glyph.HasValidUnicode())
                    {
                        fontProgram.unicodeToGlyph[glyph.GetUnicode()] = glyph;
                    }
                    fontProgram.codeToGlyph[cid] = glyph;
                    fontProgram.avgWidth        += width;
                }
                if (fontProgram.codeToGlyph.Count != 0)
                {
                    fontProgram.avgWidth /= fontProgram.codeToGlyph.Count;
                }
            }
            if (fontProgram.codeToGlyph.Get(0) == null)
            {
                fontProgram.codeToGlyph[0] = new Glyph(0, dw, -1);
            }
            return(fontProgram);
        }
Ejemplo n.º 3
0
 /// <summary>Gets the kerning between two glyphs.</summary>
 /// <param name="first">the first glyph</param>
 /// <param name="second">the second glyph</param>
 /// <returns>the kerning to be applied</returns>
 public override int GetKerning(Glyph first, Glyph second)
 {
     if (first == null || second == null)
     {
         return(0);
     }
     return(kerning.Get((first.GetCode() << 16) + second.GetCode()));
 }
Ejemplo n.º 4
0
        private static void FillDifferences(iText.Kernel.Font.DocFontEncoding fontEncoding, PdfArray diffs, CMapToUnicode
                                            toUnicode)
        {
            IntHashtable byte2uni = toUnicode != null?toUnicode.CreateDirectMapping() : new IntHashtable();

            if (diffs != null)
            {
                int currentNumber = 0;
                for (int k = 0; k < diffs.Size(); ++k)
                {
                    PdfObject obj = diffs.Get(k);
                    if (obj.IsNumber())
                    {
                        currentNumber = ((PdfNumber)obj).IntValue();
                    }
                    else
                    {
                        if (currentNumber > 255)
                        {
                            ILog LOGGER = LogManager.GetLogger(typeof(iText.Kernel.Font.DocFontEncoding));
                            LOGGER.Warn(MessageFormatUtil.Format(iText.IO.LogMessageConstant.DOCFONT_HAS_ILLEGAL_DIFFERENCES, ((PdfName
                                                                                                                                )obj).GetValue()));
                        }
                        else
                        {
                            /* don't return or break, because differences subarrays may
                             * be in any order:
                             * e.g. [255 /space /one 250 /two /three]
                             * /one is invalid but all others should be parsed
                             */
                            String glyphName = ((PdfName)obj).GetValue();
                            int    unicode   = AdobeGlyphList.NameToUnicode(glyphName);
                            if (unicode != -1)
                            {
                                fontEncoding.codeToUnicode[currentNumber] = (int)unicode;
                                fontEncoding.unicodeToCode.Put((int)unicode, currentNumber);
                                fontEncoding.differences[currentNumber] = glyphName;
                                fontEncoding.unicodeDifferences.Put((int)unicode, (int)unicode);
                            }
                            else
                            {
                                if (byte2uni.ContainsKey(currentNumber))
                                {
                                    unicode = byte2uni.Get(currentNumber);
                                    fontEncoding.codeToUnicode[currentNumber] = (int)unicode;
                                    fontEncoding.unicodeToCode.Put((int)unicode, currentNumber);
                                    fontEncoding.differences[currentNumber] = glyphName;
                                    fontEncoding.unicodeDifferences.Put((int)unicode, (int)unicode);
                                }
                            }
                            currentNumber++;
                        }
                    }
                }
            }
        }
Ejemplo n.º 5
0
 public virtual int GetCidCode(int cmapCode)
 {
     if (isDirect)
     {
         return(cmapCode);
     }
     else
     {
         return(code2Cid.Get(cmapCode));
     }
 }
Ejemplo n.º 6
0
        private static void FillDifferences(iText.Kernel.Font.DocFontEncoding fontEncoding, CMapToUnicode toUnicode
                                            )
        {
            IntHashtable byte2uni = toUnicode.CreateDirectMapping();

            foreach (int?code in byte2uni.GetKeys())
            {
                int    unicode   = byte2uni.Get((int)code);
                String glyphName = AdobeGlyphList.UnicodeToName(unicode);
                fontEncoding.codeToUnicode[(int)code] = unicode;
                fontEncoding.unicodeToCode.Put(unicode, (int)code);
                fontEncoding.differences[(int)code] = glyphName;
                fontEncoding.unicodeDifferences.Put(unicode, unicode);
            }
        }
Ejemplo n.º 7
0
        private void InitializeCidFontProperties(IDictionary <String, Object> fontDesc)
        {
            fontIdentification.SetPanose((String)fontDesc.Get("Panose"));
            fontMetrics.SetItalicAngle(Convert.ToInt32((String)fontDesc.Get("ItalicAngle"), System.Globalization.CultureInfo.InvariantCulture
                                                       ));
            fontMetrics.SetCapHeight(Convert.ToInt32((String)fontDesc.Get("CapHeight"), System.Globalization.CultureInfo.InvariantCulture
                                                     ));
            fontMetrics.SetTypoAscender(Convert.ToInt32((String)fontDesc.Get("Ascent"), System.Globalization.CultureInfo.InvariantCulture
                                                        ));
            fontMetrics.SetTypoDescender(Convert.ToInt32((String)fontDesc.Get("Descent"), System.Globalization.CultureInfo.InvariantCulture
                                                         ));
            fontMetrics.SetStemV(Convert.ToInt32((String)fontDesc.Get("StemV"), System.Globalization.CultureInfo.InvariantCulture
                                                 ));
            pdfFontFlags = Convert.ToInt32((String)fontDesc.Get("Flags"), System.Globalization.CultureInfo.InvariantCulture
                                           );
            String          fontBBox = (String)fontDesc.Get("FontBBox");
            StringTokenizer tk       = new StringTokenizer(fontBBox, " []\r\n\t\f");
            int             llx      = Convert.ToInt32(tk.NextToken(), System.Globalization.CultureInfo.InvariantCulture);
            int             lly      = Convert.ToInt32(tk.NextToken(), System.Globalization.CultureInfo.InvariantCulture);
            int             urx      = Convert.ToInt32(tk.NextToken(), System.Globalization.CultureInfo.InvariantCulture);
            int             ury      = Convert.ToInt32(tk.NextToken(), System.Globalization.CultureInfo.InvariantCulture);

            fontMetrics.UpdateBbox(llx, lly, urx, ury);
            registry = (String)fontDesc.Get("Registry");
            String uniMap = GetCompatibleUniMap(registry);

            if (uniMap != null)
            {
                IntHashtable metrics = (IntHashtable)fontDesc.Get("W");
                CMapCidUni   cid2Uni = FontCache.GetCid2UniCmap(uniMap);
                avgWidth = 0;
                foreach (int cid in cid2Uni.GetCids())
                {
                    int   uni   = cid2Uni.Lookup(cid);
                    int   width = metrics.ContainsKey(cid) ? metrics.Get(cid) : DEFAULT_WIDTH;
                    Glyph glyph = new Glyph(cid, width, uni);
                    avgWidth += glyph.GetWidth();
                    codeToGlyph.Put(cid, glyph);
                    unicodeToGlyph.Put(uni, glyph);
                }
                FixSpaceIssue();
                if (codeToGlyph.Count != 0)
                {
                    avgWidth /= codeToGlyph.Count;
                }
            }
        }
Ejemplo n.º 8
0
 public override bool TransformOne(GlyphLine line) {
     if (line.idx >= line.end) {
         return false;
     }
     Glyph g = line.Get(line.idx);
     bool changed = false;
     if (!openReader.IsSkip(g.GetCode(), lookupFlag)) {
         int substCode = substMap.Get(g.GetCode());
         // there is no need to substitute a symbol with itself
         if (substCode != 0 && substCode != g.GetCode()) {
             line.SubstituteOneToOne(openReader, substCode);
             changed = true;
         }
     }
     line.idx++;
     return changed;
 }
Ejemplo n.º 9
0
        /// <summary>
        /// Converts a
        /// <c>String</c>
        /// to a
        /// <c>byte</c>
        /// array according
        /// to the font's encoding.
        /// </summary>
        /// <param name="encoding">the encoding</param>
        /// <param name="ch">
        /// the
        /// <c>char</c>
        /// to be converted
        /// </param>
        /// <returns>
        /// an array of
        /// <c>byte</c>
        /// representing the conversion according to the font's encoding
        /// </returns>
        public static byte[] ConvertToBytes(char ch, String encoding)
        {
            if (encoding == null || encoding.Length == 0)
            {
                return(new byte[] { (byte)ch });
            }
            IntHashtable hash = null;

            if (encoding.Equals(WINANSI))
            {
                hash = winansi;
            }
            else
            {
                if (encoding.Equals(PDF_DOC_ENCODING))
                {
                    hash = pdfEncoding;
                }
            }
            if (hash != null)
            {
                int c;
                if (ch < 128 || ch > 160 && ch <= 255)
                {
                    c = ch;
                }
                else
                {
                    c = hash.Get((int)ch);
                }
                if (c != 0)
                {
                    return(new byte[] { (byte)c });
                }
                else
                {
                    return(new byte[0]);
                }
            }
            try {
                return(EncodingUtil.ConvertToBytes(new char[] { ch }, encoding));
            }
            catch (System.IO.IOException e) {
                throw new iText.IO.IOException(iText.IO.IOException.PdfEncodings, e);
            }
        }
Ejemplo n.º 10
0
        private static void FillDifferences(iText.Kernel.Font.DocFontEncoding fontEncoding, PdfArray diffs, CMapToUnicode
                                            toUnicode)
        {
            IntHashtable byte2uni = toUnicode != null?toUnicode.CreateDirectMapping() : new IntHashtable();

            if (diffs != null)
            {
                int currentNumber = 0;
                for (int k = 0; k < diffs.Size(); ++k)
                {
                    PdfObject obj = diffs.Get(k);
                    if (obj.IsNumber())
                    {
                        currentNumber = ((PdfNumber)obj).IntValue();
                    }
                    else
                    {
                        String glyphName = ((PdfName)obj).GetValue();
                        int    unicode   = (int)AdobeGlyphList.NameToUnicode(glyphName);
                        if (unicode != -1)
                        {
                            fontEncoding.codeToUnicode[currentNumber] = (int)unicode;
                            fontEncoding.unicodeToCode.Put((int)unicode, currentNumber);
                            fontEncoding.differences[currentNumber] = glyphName;
                            fontEncoding.unicodeDifferences.Put((int)unicode, (int)unicode);
                        }
                        else
                        {
                            if (byte2uni.Contains(currentNumber))
                            {
                                unicode = byte2uni.Get(currentNumber);
                                fontEncoding.codeToUnicode[currentNumber] = (int)unicode;
                                fontEncoding.unicodeToCode.Put((int)unicode, currentNumber);
                                fontEncoding.differences[currentNumber] = glyphName;
                                fontEncoding.unicodeDifferences.Put((int)unicode, (int)unicode);
                            }
                        }
                        currentNumber++;
                    }
                }
            }
        }
Ejemplo n.º 11
0
        public override bool TransformOne(GlyphLine line)
        {
            if (line.idx >= line.end)
            {
                return(false);
            }
            Glyph g       = line.Get(line.idx);
            bool  changed = false;

            if (!openReader.IsSkip(g.GetCode(), lookupFlag))
            {
                int substCode = substMap.Get(g.GetCode());
                if (substCode != 0)
                {
                    line.SubstituteOneToOne(openReader, substCode);
                    changed = true;
                }
            }
            line.idx++;
            return(changed);
        }
Ejemplo n.º 12
0
 public virtual int GetOtfClass(int glyph)
 {
     return(mapClass.Get(glyph));
 }
Ejemplo n.º 13
0
 /// <summary>
 /// Converts a unicode symbol or font specific code
 /// to
 /// <c>byte</c>
 /// according to the encoding.
 /// </summary>
 /// <param name="unicode">a unicode symbol or FontSpecif code to be converted.</param>
 /// <returns>
 /// a
 /// <c>byte</c>
 /// representing the conversion according to the encoding
 /// </returns>
 public virtual int ConvertToByte(int unicode)
 {
     return(unicodeToCode.Get(unicode));
 }
Ejemplo n.º 14
0
        /// <summary>
        /// Converts a
        /// <c>String</c>
        /// to a
        /// <c>byte</c>
        /// array according
        /// to the font's encoding.
        /// </summary>
        /// <param name="encoding">the encoding</param>
        /// <param name="text">
        /// the
        /// <c>String</c>
        /// to be converted
        /// </param>
        /// <returns>
        /// an array of
        /// <c>byte</c>
        /// representing the conversion according to the font's encoding
        /// </returns>
        public static byte[] ConvertToBytes(String text, String encoding)
        {
            if (text == null)
            {
                return(new byte[0]);
            }
            if (encoding == null || encoding.Length == 0)
            {
                int    len = text.Length;
                byte[] b   = new byte[len];
                for (int k = 0; k < len; ++k)
                {
                    b[k] = (byte)text[k];
                }
                return(b);
            }
            IExtraEncoding extra = extraEncodings.Get(encoding.ToLower(System.Globalization.CultureInfo.InvariantCulture
                                                                       ));

            if (extra != null)
            {
                byte[] b = extra.CharToByte(text, encoding);
                if (b != null)
                {
                    return(b);
                }
            }
            IntHashtable hash = null;

            if (encoding.Equals(WINANSI))
            {
                hash = winansi;
            }
            else
            {
                if (encoding.Equals(PDF_DOC_ENCODING))
                {
                    hash = pdfEncoding;
                }
            }
            if (hash != null)
            {
                char[] cc  = text.ToCharArray();
                int    len = cc.Length;
                int    ptr = 0;
                byte[] b   = new byte[len];
                int    c;
                for (int k = 0; k < len; ++k)
                {
                    char ch = cc[k];
                    if (ch < 128 || ch > 160 && ch <= 255)
                    {
                        c = ch;
                    }
                    else
                    {
                        c = hash.Get((int)ch);
                    }
                    if (c != 0)
                    {
                        b[ptr++] = (byte)c;
                    }
                }
                if (ptr == len)
                {
                    return(b);
                }
                byte[] b2 = new byte[ptr];
                System.Array.Copy(b, 0, b2, 0, ptr);
                return(b2);
            }
            try {
                return(EncodingUtil.ConvertToBytes(text.ToCharArray(), encoding));
            }
            catch (System.IO.IOException e) {
                throw new iText.IO.IOException(iText.IO.IOException.PdfEncodings, e);
            }
        }
Ejemplo n.º 15
0
 public virtual int Lookup(int character)
 {
     return(map.Get(character));
 }
Ejemplo n.º 16
0
 public virtual int GetUnicodeDifference(int index)
 {
     return(unicodeDifferences.Get(index));
 }
Ejemplo n.º 17
0
            private void SerObject(PdfObject obj, int level, ByteBufferOutputStream bb, IntHashtable serialized)
            {
                if (level <= 0)
                {
                    return;
                }
                if (obj == null)
                {
                    bb.Append("$Lnull");
                    return;
                }
                PdfIndirectReference   reference = null;
                ByteBufferOutputStream savedBb   = null;

                if (obj.IsIndirectReference())
                {
                    reference = (PdfIndirectReference)obj;
                    int key = GetCopyObjectKey(obj);
                    if (serialized.ContainsKey(key))
                    {
                        bb.Append((int)serialized.Get(key));
                        return;
                    }
                    else
                    {
                        savedBb = bb;
                        bb      = new ByteBufferOutputStream();
                    }
                }
                if (obj.IsStream())
                {
                    bb.Append("$B");
                    SerDic((PdfDictionary)obj, level - 1, bb, serialized);
                    if (level > 0)
                    {
                        md5.Reset();
                        bb.Append(md5.Digest(((PdfStream)obj).GetBytes(false)));
                    }
                }
                else
                {
                    if (obj.IsDictionary())
                    {
                        SerDic((PdfDictionary)obj, level - 1, bb, serialized);
                    }
                    else
                    {
                        if (obj.IsArray())
                        {
                            SerArray((PdfArray)obj, level - 1, bb, serialized);
                        }
                        else
                        {
                            if (obj.IsString())
                            {
                                bb.Append("$S").Append(obj.ToString());
                            }
                            else
                            {
                                if (obj.IsName())
                                {
                                    bb.Append("$N").Append(obj.ToString());
                                }
                                else
                                {
                                    bb.Append("$L").Append(obj.ToString());
                                }
                            }
                        }
                    }
                }
                if (savedBb != null)
                {
                    int key = GetCopyObjectKey(reference);
                    if (!serialized.ContainsKey(key))
                    {
                        serialized.Put(key, CalculateHash(bb.GetBuffer()));
                    }
                    savedBb.Append(bb);
                }
            }