示例#1
0
        public virtual PRTokeniser GetLocation(String location)
        {
            String fullName = BaseFont.RESOURCE_PATH + "cmaps." + location;
            Stream inp      = StreamUtil.GetResourceStream(fullName);

            if (inp == null)
            {
                throw new IOException(MessageLocalization.GetComposedMessage("the.cmap.1.was.not.found", fullName));
            }
            return(new PRTokeniser(new RandomAccessFileOrArray(new RandomAccessSourceFactory().CreateSource(inp))));
        }
        private static Dictionary <string, string> GetLanguageMessages(String language, String country)
        {
            if (language == null)
            {
                throw new ArgumentException("The language cannot be null.");
            }
            Stream isp = null;

            try {
                String file;
                if (country != null)
                {
                    file = language + "_" + country + ".lng";
                }
                else
                {
                    file = language + ".lng";
                }
                isp = StreamUtil.GetResourceStream(BASE_PATH + file);
                if (isp != null)
                {
                    return(ReadLanguageStream(isp));
                }
                if (country == null)
                {
                    return(null);
                }
                file = language + ".lng";
                isp  = StreamUtil.GetResourceStream(BASE_PATH + file);
                if (isp != null)
                {
                    return(ReadLanguageStream(isp));
                }
                else
                {
                    return(null);
                }
            }
            finally {
                try {
                    isp.Close();
                } catch {
                }
                // do nothing
            }
        }
示例#3
0
        private static void LoadRegistry()
        {
            Stream     isp = StreamUtil.GetResourceStream(RESOURCE_PATH_CMAP + "cjk_registry.properties");
            Properties p   = new Properties();

            p.Load(isp);
            isp.Close();
            foreach (string key in p.Keys)
            {
                String   value = p[key];
                String[] sp    = value.Split(cspace, StringSplitOptions.RemoveEmptyEntries);
                Dictionary <String, object> hs = new Dictionary <string, object>();
                foreach (String s in sp)
                {
                    hs[s] = null;
                }
                registryNames[key] = hs;
            }
        }
示例#4
0
 /**
  * @param key
  * @return a hyphenation tree
  */
 public static HyphenationTree GetResourceHyphenationTree(String key)
 {
     try {
         Stream stream = StreamUtil.GetResourceStream(defaultHyphLocation + key + ".xml");
         if (stream == null && key.Length > 2)
         {
             stream = StreamUtil.GetResourceStream(defaultHyphLocation + key.Substring(0, 2) + ".xml");
         }
         if (stream == null)
         {
             return(null);
         }
         HyphenationTree hTree = new HyphenationTree();
         hTree.LoadSimplePatterns(stream);
         return(hTree);
     }
     catch {
         return(null);
     }
 }
示例#5
0
        internal static Dictionary <String, Object> ReadFontProperties(String name)
        {
            name += ".properties";
            Stream     isp = StreamUtil.GetResourceStream(RESOURCE_PATH_CMAP + name);
            Properties p   = new Properties();

            p.Load(isp);
            isp.Close();
            IntHashtable W = CreateMetric(p["W"]);

            p.Remove("W");
            IntHashtable W2 = CreateMetric(p["W2"]);

            p.Remove("W2");
            Dictionary <String, Object> map = new Dictionary <string, object>();

            foreach (string key in p.Keys)
            {
                map[key] = p[key];
            }
            map["W"]  = W;
            map["W2"] = W2;
            return(map);
        }
示例#6
0
        /**
         * Creates a file specification with the file embedded. The file may
         * come from the file system or from a byte array.
         * @param writer the <CODE>PdfWriter</CODE>
         * @param filePath the file path
         * @param fileDisplay the file information that is presented to the user
         * @param fileStore the byte array with the file. If it is not <CODE>null</CODE>
         * it takes precedence over <CODE>filePath</CODE>
         * @param mimeType the optional mimeType
         * @param fileParameter the optional extra file parameters such as the creation or modification date
         * @param compressionLevel the level of compression
         * @throws IOException on error
         * @return the file specification
         * @since   2.1.3
         */
        public static PdfFileSpecification FileEmbedded(PdfWriter writer, String filePath, String fileDisplay, byte[] fileStore, String mimeType, PdfDictionary fileParameter, int compressionLevel)
        {
            PdfFileSpecification fs = new PdfFileSpecification();

            fs.writer = writer;
            fs.Put(PdfName.F, new PdfString(fileDisplay));
            fs.SetUnicodeFileName(fileDisplay, false);
            PdfEFStream          stream;
            Stream               inp = null;
            PdfIndirectReference refi;
            PdfIndirectReference refFileLength = null;

            try {
                if (fileStore == null)
                {
                    refFileLength = writer.PdfIndirectReference;
                    if (File.Exists(filePath))
                    {
                        inp = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                    }
                    else
                    {
                        if (filePath.StartsWith("file:/") || filePath.StartsWith("http://") || filePath.StartsWith("https://"))
                        {
                            WebRequest wr = WebRequest.Create(filePath);
                            wr.Credentials = CredentialCache.DefaultCredentials;
                            inp            = wr.GetResponse().GetResponseStream();
                        }
                        else
                        {
                            inp = StreamUtil.GetResourceStream(filePath);
                            if (inp == null)
                            {
                                throw new IOException(MessageLocalization.GetComposedMessage("1.not.found.as.file.or.resource", filePath));
                            }
                        }
                    }
                    stream = new PdfEFStream(inp, writer);
                }
                else
                {
                    stream = new PdfEFStream(fileStore);
                }
                stream.Put(PdfName.TYPE, PdfName.EMBEDDEDFILE);
                stream.FlateCompress(compressionLevel);

                PdfDictionary param = new PdfDictionary();
                if (fileParameter != null)
                {
                    param.Merge(fileParameter);
                }
                if (!param.Contains(PdfName.MODDATE))
                {
                    param.Put(PdfName.MODDATE, new PdfDate());
                }
                if (fileStore == null)
                {
                    stream.Put(PdfName.PARAMS, refFileLength);
                }
                else
                {
                    param.Put(PdfName.SIZE, new PdfNumber(stream.RawLength));
                    stream.Put(PdfName.PARAMS, param);
                }

                if (mimeType != null)
                {
                    stream.Put(PdfName.SUBTYPE, new PdfName(mimeType));
                }

                refi = writer.AddToBody(stream).IndirectReference;
                if (fileStore == null)
                {
                    stream.WriteLength();
                    param.Put(PdfName.SIZE, new PdfNumber(stream.RawLength));
                    writer.AddToBody(param, refFileLength);
                }
            }
            finally {
                if (inp != null)
                {
                    try{ inp.Close(); }catch {}
                }
            }
            PdfDictionary f = new PdfDictionary();

            f.Put(PdfName.F, refi);
            f.Put(PdfName.UF, refi);
            fs.Put(PdfName.EF, f);
            return(fs);
        }
示例#7
0
        /** Creates a new Type1 font.
         * @param ttfAfm the AFM file if the input is made with a <CODE>byte</CODE> array
         * @param pfb the PFB file if the input is made with a <CODE>byte</CODE> array
         * @param afmFile the name of one of the 14 built-in fonts or the location of an AFM file. The file must end in '.afm'
         * @param enc the encoding to be applied to this font
         * @param emb true if the font is to be embedded in the PDF
         * @throws DocumentException the AFM file is invalid
         * @throws IOException the AFM file could not be read
         */
        internal Type1Font(string afmFile, string enc, bool emb, byte[] ttfAfm, byte[] pfb, bool forceRead)
        {
            if (emb && ttfAfm != null && pfb == null)
            {
                throw new DocumentException(MessageLocalization.GetComposedMessage("two.byte.arrays.are.needed.if.the.type1.font.is.embedded"));
            }
            if (emb && ttfAfm != null)
            {
                this.pfb = pfb;
            }
            encoding = enc;
            embedded = emb;
            fileName = afmFile;
            FontType = FONT_TYPE_T1;
            RandomAccessFileOrArray rf = null;
            Stream istr = null;

            if (BuiltinFonts14.ContainsKey(afmFile))
            {
                embedded    = false;
                builtinFont = true;
                byte[] buf = new byte[1024];
                try {
                    istr = StreamUtil.GetResourceStream(RESOURCE_PATH + afmFile + ".afm");
                    if (istr == null)
                    {
                        string msg = MessageLocalization.GetComposedMessage("1.not.found.as.resource", afmFile);
                        Console.Error.WriteLine(msg);
                        throw new DocumentException(msg);
                    }
                    MemoryStream ostr = new MemoryStream();
                    while (true)
                    {
                        int size = istr.Read(buf, 0, buf.Length);
                        if (size == 0)
                        {
                            break;
                        }
                        ostr.Write(buf, 0, size);
                    }
                    buf = ostr.ToArray();
                }
                finally {
                    if (istr != null)
                    {
                        try {
                            istr.Close();
                        }
                        catch {
                            // empty on purpose
                        }
                    }
                }
                try {
                    rf = new RandomAccessFileOrArray(buf);
                    Process(rf);
                }
                finally {
                    if (rf != null)
                    {
                        try {
                            rf.Close();
                        }
                        catch {
                            // empty on purpose
                        }
                    }
                }
            }
            else if (afmFile.ToLower(System.Globalization.CultureInfo.InvariantCulture).EndsWith(".afm"))
            {
                try {
                    if (ttfAfm == null)
                    {
                        rf = new RandomAccessFileOrArray(afmFile, forceRead);
                    }
                    else
                    {
                        rf = new RandomAccessFileOrArray(ttfAfm);
                    }
                    Process(rf);
                }
                finally {
                    if (rf != null)
                    {
                        try {
                            rf.Close();
                        }
                        catch {
                            // empty on purpose
                        }
                    }
                }
            }
            else if (afmFile.ToLower(System.Globalization.CultureInfo.InvariantCulture).EndsWith(".pfm"))
            {
                try {
                    MemoryStream ba = new MemoryStream();
                    if (ttfAfm == null)
                    {
                        rf = new RandomAccessFileOrArray(afmFile, forceRead);
                    }
                    else
                    {
                        rf = new RandomAccessFileOrArray(ttfAfm);
                    }
                    Pfm2afm.Convert(rf, ba);
                    rf.Close();
                    rf = new RandomAccessFileOrArray(ba.ToArray());
                    Process(rf);
                }
                finally {
                    if (rf != null)
                    {
                        try {
                            rf.Close();
                        }
                        catch  {
                            // empty on purpose
                        }
                    }
                }
            }
            else
            {
                throw new DocumentException(MessageLocalization.GetComposedMessage("1.is.not.an.afm.or.pfm.font.file", afmFile));
            }
            EncodingScheme = EncodingScheme.Trim();
            if (EncodingScheme.Equals("AdobeStandardEncoding") || EncodingScheme.Equals("StandardEncoding"))
            {
                fontSpecific = false;
            }
            if (!encoding.StartsWith("#"))
            {
                PdfEncodings.ConvertToBytes(" ", enc); // check if the encoding exists
            }
            CreateEncoding();
        }
示例#8
0
        static GlyphList()
        {
            Stream istr = null;

            try {
                istr = StreamUtil.GetResourceStream(BaseFont.RESOURCE_PATH + "glyphlist.txt");
                if (istr == null)
                {
                    String msg = "glyphlist.txt not found as resource.";
                    throw new Exception(msg);
                }
                byte[]       buf  = new byte[1024];
                MemoryStream outp = new MemoryStream();
                while (true)
                {
                    int size = istr.Read(buf, 0, buf.Length);
                    if (size == 0)
                    {
                        break;
                    }
                    outp.Write(buf, 0, size);
                }
                istr.Close();
                istr = null;
                String          s  = PdfEncodings.ConvertToString(outp.ToArray(), null);
                StringTokenizer tk = new StringTokenizer(s, "\r\n");
                while (tk.HasMoreTokens())
                {
                    String line = tk.NextToken();
                    if (line.StartsWith("#"))
                    {
                        continue;
                    }
                    StringTokenizer t2   = new StringTokenizer(line, " ;\r\n\t\f");
                    String          name = null;
                    String          hex  = null;
                    if (!t2.HasMoreTokens())
                    {
                        continue;
                    }
                    name = t2.NextToken();
                    if (!t2.HasMoreTokens())
                    {
                        continue;
                    }
                    hex = t2.NextToken();
                    int num = int.Parse(hex, NumberStyles.HexNumber);
                    unicode2names[num]  = name;
                    names2unicode[name] = new int[] { num };
                }
            }
            catch (Exception e) {
                Console.Error.WriteLine("glyphlist.txt loading error: " + e.Message);
            }
            finally {
                if (istr != null)
                {
                    try {
                        istr.Close();
                    }
                    catch {
                        // empty on purpose
                    }
                }
            }
        }