Пример #1
0
 /** Adds an extra encoding.
  * @param name the name of the encoding. The encoding recognition is case insensitive
  * @param enc the conversion class
  */
 public static void AddExtraEncoding(String name, IExtraEncoding enc)
 {
     lock (extraEncodings) { // This serializes concurrent updates
         Dictionary <String, IExtraEncoding> newEncodings = new Dictionary <string, IExtraEncoding>(extraEncodings);
         newEncodings[name.ToLower(System.Globalization.CultureInfo.InvariantCulture)] = enc;
         extraEncodings = newEncodings; // This swap does not require synchronization with reader
     }
 }
Пример #2
0
 /** Adds an extra encoding.
  * @param name the name of the encoding. The encoding recognition is case insensitive
  * @param enc the conversion class
  */
 public static void AddExtraEncoding(String name, IExtraEncoding enc)
 {
     lock (extraEncodings) { // This serializes concurrent updates
         Hashtable newEncodings = (Hashtable)extraEncodings.Clone();
         newEncodings[name.ToLower(CultureInfo.InvariantCulture)] = enc;
         extraEncodings = newEncodings; // This swap does not require synchronization with reader
     }
 }
 /// <summary>
 /// Adds an extra encoding.
 /// </summary>
 /// <param name="name">the name of the encoding. The encoding recognition is case insensitive</param>
 /// <param name="enc">the conversion class</param>
 public static void AddExtraEncoding(string name, IExtraEncoding enc)
 {
     lock (ExtraEncodings)
     { // This serializes concurrent updates
         var newEncodings = (Hashtable)ExtraEncodings.Clone();
         newEncodings[name.ToLowerInvariant()] = enc;
         ExtraEncodings = newEncodings;  // This swap does not require synchronization with reader
     }
 }
Пример #4
0
        /// <summary>
        /// Converts a
        /// <c>byte</c>
        /// array to a
        /// <c>String</c>
        /// according
        /// to the some encoding.
        /// </summary>
        /// <param name="bytes">the bytes to convert</param>
        /// <param name="encoding">the encoding</param>
        /// <returns>
        /// the converted
        /// <c>String</c>
        /// </returns>
        public static String ConvertToString(byte[] bytes, String encoding)
        {
            if (bytes == null)
            {
                return(EMPTY_STRING);
            }
            if (encoding == null || encoding.Length == 0)
            {
                char[] c = new char[bytes.Length];
                for (int k = 0; k < bytes.Length; ++k)
                {
                    c[k] = (char)(bytes[k] & 0xff);
                }
                return(new String(c));
            }
            IExtraEncoding extra = extraEncodings.Get(encoding.ToLower(System.Globalization.CultureInfo.InvariantCulture
                                                                       ));

            if (extra != null)
            {
                String text = extra.ByteToChar(bytes, encoding);
                if (text != null)
                {
                    return(text);
                }
            }
            char[] ch = null;
            if (encoding.Equals(WINANSI))
            {
                ch = winansiByteToChar;
            }
            else
            {
                if (encoding.Equals(PDF_DOC_ENCODING))
                {
                    ch = pdfEncodingByteToChar;
                }
            }
            if (ch != null)
            {
                int    len = bytes.Length;
                char[] c   = new char[len];
                for (int k = 0; k < len; ++k)
                {
                    c[k] = ch[bytes[k] & 0xff];
                }
                return(new String(c));
            }
            try {
                return(EncodingUtil.ConvertToString(bytes, encoding));
            }
            catch (ArgumentException e) {
                throw new iText.IO.IOException(iText.IO.IOException.PdfEncodings, e);
            }
        }
Пример #5
0
        public static string ConvertToString(byte[] bytes, string encoding)
        {
            if (bytes == null)
            {
                return(PdfObject.NOTHING);
            }
            if (encoding == null || encoding.Length == 0)
            {
                char[] c = new char[bytes.Length];
                for (int k = 0; k < bytes.Length; ++k)
                {
                    c[k] = (char)(bytes[k] & 0xff);
                }
                return(new String(c));
            }
            IExtraEncoding extra = (IExtraEncoding)extraEncodings[encoding.ToLower(System.Globalization.CultureInfo.InvariantCulture)];

            if (extra != null)
            {
                String text = extra.ByteToChar(bytes, encoding);
                if (text != null)
                {
                    return(text);
                }
            }
            char[] ch = null;
            if (encoding.Equals(BaseFont.WINANSI))
            {
                ch = winansiByteToChar;
            }
            else if (encoding.Equals(PdfObject.TEXT_PDFDOCENCODING))
            {
                ch = pdfEncodingByteToChar;
            }
            if (ch != null)
            {
                int    len = bytes.Length;
                char[] c   = new char[len];
                for (int k = 0; k < len; ++k)
                {
                    c[k] = ch[bytes[k] & 0xff];
                }
                return(new String(c));
            }
            String nameU  = encoding.ToUpper(System.Globalization.CultureInfo.InvariantCulture);
            bool   marker = false;
            bool   big    = false;
            int    offset = 0;

            if (bytes.Length >= 2)
            {
                if (bytes[0] == (byte)254 && bytes[1] == (byte)255)
                {
                    marker = true;
                    big    = true;
                    offset = 2;
                }
                else if (bytes[0] == (byte)255 && bytes[1] == (byte)254)
                {
                    marker = true;
                    big    = false;
                    offset = 2;
                }
            }
            Encoding enc = null;

            if (nameU.Equals("UNICODEBIGUNMARKED") || nameU.Equals("UNICODEBIG"))
            {
                enc = new UnicodeEncoding(marker ? big : true, false);
            }
            if (nameU.Equals("UNICODELITTLEUNMARKED") || nameU.Equals("UNICODELITTLE"))
            {
                enc = new UnicodeEncoding(marker ? big : false, false);
            }
            if (enc != null)
            {
                return(enc.GetString(bytes, offset, bytes.Length - offset));
            }
            return(IanaEncodings.GetEncodingEncoding(encoding).GetString(bytes));
        }
Пример #6
0
        /** Converts a <CODE>String</CODE> to a </CODE>byte</CODE> array according
         * to the font's encoding.
         * @return an array of <CODE>byte</CODE> representing the conversion according to the font's encoding
         * @param encoding the encoding
         * @param char1 the <CODE>char</CODE> to be converted
         */
        public static byte[] ConvertToBytes(char char1, String encoding)
        {
            if (encoding == null || encoding.Length == 0)
            {
                return new byte[] { (byte)char1 }
            }
            ;
            IExtraEncoding extra = (IExtraEncoding)extraEncodings[encoding.ToLower(System.Globalization.CultureInfo.InvariantCulture)];

            if (extra != null)
            {
                byte[] b = extra.CharToByte(char1, encoding);

                if (b != null)
                {
                    return(b);
                }
            }
            IntHashtable hash = null;

            if (encoding.Equals(BaseFont.WINANSI))
            {
                hash = winansi;
            }
            else if (encoding.Equals(PdfObject.TEXT_PDFDOCENCODING))
            {
                hash = pdfEncoding;
            }
            if (hash != null)
            {
                int c = 0;
                if (char1 < 128 || (char1 > 160 && char1 <= 255))
                {
                    c = char1;
                }
                else
                {
                    c = hash[char1];
                }
                if (c != 0)
                {
                    return new byte[] { (byte)c }
                }
                ;
                else
                {
                    return(new byte[0]);
                }
            }
            Encoding encw = IanaEncodings.GetEncodingEncoding(encoding);

            byte[] preamble = encw.GetPreamble();
            char[] text     = new char[] { char1 };
            if (preamble.Length == 0)
            {
                return(encw.GetBytes(text));
            }
            byte[] encoded = encw.GetBytes(text);
            byte[] total   = new byte[encoded.Length + preamble.Length];
            Array.Copy(preamble, 0, total, 0, preamble.Length);
            Array.Copy(encoded, 0, total, preamble.Length, encoded.Length);
            return(total);
        }
Пример #7
0
        /**
         * Converts a <CODE>string</CODE> to a </CODE>byte</CODE> array according
         * to the font's encoding.
         * @param text the <CODE>string</CODE> to be converted
         * @return an array of <CODE>byte</CODE> representing the conversion according to the font's encoding
         */
        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 = (IExtraEncoding)extraEncodings[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(BaseFont.CP1252))
            {
                hash = winansi;
            }
            else if (encoding.Equals(PdfObject.TEXT_PDFDOCENCODING))
            {
                hash = pdfEncoding;
            }
            if (hash != null)
            {
                char[] cc  = text.ToCharArray();
                int    len = cc.Length;
                int    ptr = 0;
                byte[] b   = new byte[len];
                int    c   = 0;
                for (int k = 0; k < len; ++k)
                {
                    char char1 = cc[k];
                    if (char1 < 128 || (char1 > 160 && char1 <= 255))
                    {
                        c = char1;
                    }
                    else
                    {
                        c = hash[char1];
                    }
                    if (c != 0)
                    {
                        b[ptr++] = (byte)c;
                    }
                }
                if (ptr == len)
                {
                    return(b);
                }
                byte[] b2 = new byte[ptr];
                Array.Copy(b, 0, b2, 0, ptr);
                return(b2);
            }
            Encoding encw = IanaEncodings.GetEncodingEncoding(encoding);

            byte[] preamble = encw.GetPreamble();
            if (preamble.Length == 0)
            {
                return(encw.GetBytes(text));
            }
            byte[] encoded = encw.GetBytes(text);
            byte[] total   = new byte[encoded.Length + preamble.Length];
            Array.Copy(preamble, 0, total, 0, preamble.Length);
            Array.Copy(encoded, 0, total, preamble.Length, encoded.Length);
            return(total);
        }
Пример #8
0
 /** Adds an extra encoding.
  * @param name the name of the encoding. The encoding recognition is case insensitive
  * @param enc the conversion class
  */    
 public static void AddExtraEncoding(String name, IExtraEncoding enc) {
     lock (extraEncodings) { // This serializes concurrent updates
         Dictionary<String, IExtraEncoding> newEncodings = new Dictionary<string,IExtraEncoding>(extraEncodings);
         newEncodings[name.ToLower(System.Globalization.CultureInfo.InvariantCulture)] = enc;
         extraEncodings = newEncodings;  // This swap does not require synchronization with reader
     }
 }
Пример #9
0
 /// <summary>Adds an extra encoding.</summary>
 /// <param name="name">the name of the encoding. The encoding recognition is case insensitive</param>
 /// <param name="enc">the conversion class</param>
 public static void AddExtraEncoding(String name, IExtraEncoding enc)
 {
     lock (extraEncodings) {
         extraEncodings[name.ToLower(System.Globalization.CultureInfo.InvariantCulture)] = enc;
     }
 }
Пример #10
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);
            }
        }
Пример #11
0
 /// <summary>Adds an extra encoding.</summary>
 /// <param name="name">the name of the encoding. The encoding recognition is case insensitive</param>
 /// <param name="enc">the conversion class</param>
 public static void AddExtraEncoding(String name, IExtraEncoding enc)
 {
     lock (extraEncodings) {
         extraEncodings.Put(name.ToLowerInvariant(), enc);
     }
 }
Пример #12
0
 /** Adds an extra encoding.
  * @param name the name of the encoding. The encoding recognition is case insensitive
  * @param enc the conversion class
  */
 public static void AddExtraEncoding(String name, IExtraEncoding enc)
 {
     lock (extraEncodings) {
         extraEncodings[name.ToLower()] = enc;
     }
 }