Beispiel #1
0
        /// <summary>Reads a dictionary.</summary>
        /// <remarks>
        /// Reads a dictionary. The tokeniser must be positioned past the
        /// <c>"&lt;&lt;"</c>
        /// token.
        /// </remarks>
        /// <returns>the dictionary</returns>
        /// <exception cref="System.IO.IOException">on error</exception>
        public virtual CMapObject ReadDictionary()
        {
            IDictionary <String, CMapObject> dic = new Dictionary <String, CMapObject>();

            while (true)
            {
                if (!NextValidToken())
                {
                    throw new iText.IO.IOException("Unexpected end of file.");
                }
                if (tokeniser.GetTokenType() == PdfTokenizer.TokenType.EndDic)
                {
                    break;
                }
                if (tokeniser.GetTokenType() == PdfTokenizer.TokenType.Other && "def".Equals(tokeniser.GetStringValue()))
                {
                    continue;
                }
                if (tokeniser.GetTokenType() != PdfTokenizer.TokenType.Name)
                {
                    throw new iText.IO.IOException("Dictionary key {0} is not a name.").SetMessageParams(tokeniser.GetStringValue
                                                                                                             ());
                }
                String     name = tokeniser.GetStringValue();
                CMapObject obj  = ReadObject();
                if (obj.IsToken())
                {
                    if (obj.ToString().Equals(">>"))
                    {
                        tokeniser.ThrowError(iText.IO.IOException.UnexpectedGtGt);
                    }
                    if (obj.ToString().Equals("]"))
                    {
                        tokeniser.ThrowError(iText.IO.IOException.UnexpectedCloseBracket);
                    }
                }
                dic.Put(name, obj);
            }
            return(new CMapObject(CMapObject.DICTIONARY, dic));
        }
Beispiel #2
0
        /// <summary>Reads an array.</summary>
        /// <remarks>Reads an array. The tokeniser must be positioned past the "[" token.</remarks>
        /// <returns>an array</returns>
        /// <exception cref="System.IO.IOException">on error</exception>
        public virtual CMapObject ReadArray()
        {
            IList <CMapObject> array = new List <CMapObject>();

            while (true)
            {
                CMapObject obj = ReadObject();
                if (obj.IsToken())
                {
                    if (obj.ToString().Equals("]"))
                    {
                        break;
                    }
                    if (obj.ToString().Equals(">>"))
                    {
                        tokeniser.ThrowError(iText.IO.IOException.UnexpectedGtGt);
                    }
                }
                array.Add(obj);
            }
            return(new CMapObject(CMapObject.ARRAY, array));
        }
Beispiel #3
0
        internal virtual void AddRange(String from, String to, CMapObject code)
        {
            byte[] a1 = DecodeStringToByte(from);
            byte[] a2 = DecodeStringToByte(to);
            if (a1.Length != a2.Length || a1.Length == 0)
            {
                throw new ArgumentException("Invalid map.");
            }
            byte[] sout = null;
            if (code.IsString())
            {
                sout = DecodeStringToByte(code.ToString());
            }
            int start = ByteArrayToInt(a1);
            int end   = ByteArrayToInt(a2);

            for (int k = start; k <= end; ++k)
            {
                IntToByteArray(k, a1);
                String mark = PdfEncodings.ConvertToString(a1, null);
                if (code.IsArray())
                {
                    IList <CMapObject> codes = (List <CMapObject>)code.GetValue();
                    AddChar(mark, codes[k - start]);
                }
                else
                {
                    if (code.IsNumber())
                    {
                        int nn = (int)code.GetValue() + k - start;
                        AddChar(mark, new CMapObject(CMapObject.NUMBER, nn));
                    }
                    else
                    {
                        if (code.IsString())
                        {
                            CMapObject s1 = new CMapObject(CMapObject.HEX_STRING, sout);
                            AddChar(mark, s1);
                            System.Diagnostics.Debug.Assert(sout != null);
                            IntToByteArray(ByteArrayToInt(sout) + 1, sout);
                        }
                    }
                }
            }
        }
Beispiel #4
0
        private static void ParseCid(String cmapName, AbstractCMap cmap, ICMapLocation location, int level)
        {
            if (level >= MAX_LEVEL)
            {
                return;
            }
            PdfTokenizer inp = location.GetLocation(cmapName);

            try {
                IList <CMapObject> list = new List <CMapObject>();
                CMapContentParser  cp   = new CMapContentParser(inp);
                int maxExc = 50;
                while (true)
                {
                    try {
                        cp.Parse(list);
                    }
                    catch (Exception) {
                        if (--maxExc < 0)
                        {
                            break;
                        }
                        continue;
                    }
                    if (list.Count == 0)
                    {
                        break;
                    }
                    String last = list[list.Count - 1].ToString();
                    if (level == 0 && list.Count == 3 && last.Equals(def))
                    {
                        CMapObject cmapObject = list[0];
                        if (Registry.Equals(cmapObject.ToString()))
                        {
                            cmap.SetRegistry(list[1].ToString());
                        }
                        else
                        {
                            if (Ordering.Equals(cmapObject.ToString()))
                            {
                                cmap.SetOrdering(list[1].ToString());
                            }
                            else
                            {
                                if (CMapName.Equals(cmapObject.ToString()))
                                {
                                    cmap.SetName(list[1].ToString());
                                }
                                else
                                {
                                    if (Supplement.Equals(cmapObject.ToString()))
                                    {
                                        try {
                                            cmap.SetSupplement((int)list[1].GetValue());
                                        }
                                        catch (Exception) {
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        if ((last.Equals(endcidchar) || last.Equals(endbfchar)) && list.Count >= 3)
                        {
                            int lMax = list.Count - 2;
                            for (int k = 0; k < lMax; k += 2)
                            {
                                if (list[k].IsString())
                                {
                                    cmap.AddChar(list[k].ToString(), list[k + 1]);
                                }
                            }
                        }
                        else
                        {
                            if ((last.Equals(endcidrange) || last.Equals(endbfrange)) && list.Count >= 4)
                            {
                                int lMax = list.Count - 3;
                                for (int k = 0; k < lMax; k += 3)
                                {
                                    if (list[k].IsString() && list[k + 1].IsString())
                                    {
                                        cmap.AddRange(list[k].ToString(), list[k + 1].ToString(), list[k + 2]);
                                    }
                                }
                            }
                            else
                            {
                                if (last.Equals(usecmap) && list.Count == 2 && list[0].IsName())
                                {
                                    ParseCid(list[0].ToString(), cmap, location, level + 1);
                                }
                                else
                                {
                                    if (last.Equals(endcodespacerange))
                                    {
                                        for (int i = 0; i < list.Count + 1; i += 2)
                                        {
                                            if (list[i].IsHexString() && list[i + 1].IsHexString())
                                            {
                                                byte[] low  = list[i].ToHexByteArray();
                                                byte[] high = list[i + 1].ToHexByteArray();
                                                cmap.AddCodeSpaceRange(low, high);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception) {
                ILog logger = LogManager.GetLogger(typeof(CMapParser));
                logger.Error(iText.IO.LogMessageConstant.UNKNOWN_ERROR_WHILE_PROCESSING_CMAP);
            }
            finally {
                inp.Close();
            }
        }