예제 #1
0
 public RandomAccessFileOrArray(RandomAccessFileOrArray file)
 {
     filename    = file.filename;
     arrayIn     = file.arrayIn;
     startOffset = file.startOffset;
 }
예제 #2
0
 internal EnumerateTTC(String ttcFile)
 {
     fileName = ttcFile;
     rf       = new RandomAccessFileOrArray(ttcFile);
     FindNames();
 }
예제 #3
0
 internal EnumerateTTC(byte[] ttcArray)
 {
     fileName = "Byte array TTC";
     rf       = new RandomAccessFileOrArray(ttcArray);
     FindNames();
 }
예제 #4
0
        /** If the embedded flag is <CODE>false</CODE> or if the font is
         *  one of the 14 built in types, it returns <CODE>null</CODE>,
         * otherwise the font is read and output in a PdfStream object.
         * @return the PdfStream containing the font or <CODE>null</CODE>
         * @throws DocumentException if there is an error reading the font
         */
        public override PdfStream GetFullFontStream()
        {
            if (builtinFont || !embedded)
            {
                return(null);
            }
            RandomAccessFileOrArray rf = null;

            try {
                string filePfb = fileName.Substring(0, fileName.Length - 3) + "pfb";
                if (pfb == null)
                {
                    rf = new RandomAccessFileOrArray(filePfb, true);
                }
                else
                {
                    rf = new RandomAccessFileOrArray(pfb);
                }
                int    fileLength = rf.Length;
                byte[] st         = new byte[fileLength - 18];
                int[]  lengths    = new int[3];
                int    bytePtr    = 0;
                for (int k = 0; k < 3; ++k)
                {
                    if (rf.Read() != 0x80)
                    {
                        throw new DocumentException("Start marker missing in " + filePfb);
                    }
                    if (rf.Read() != PFB_TYPES[k])
                    {
                        throw new DocumentException("Incorrect segment type in " + filePfb);
                    }
                    int size = rf.Read();
                    size      += rf.Read() << 8;
                    size      += rf.Read() << 16;
                    size      += rf.Read() << 24;
                    lengths[k] = size;
                    while (size != 0)
                    {
                        int got = rf.Read(st, bytePtr, size);
                        if (got < 0)
                        {
                            throw new DocumentException("Premature end in " + filePfb);
                        }
                        bytePtr += got;
                        size    -= got;
                    }
                }
                return(new StreamFont(st, lengths, compressionLevel));
            }
            finally {
                if (rf != null)
                {
                    try {
                        rf.Close();
                    }
                    catch  {
                        // empty on purpose
                    }
                }
            }
        }
예제 #5
0
 /** Creates a new instance of Pfm2afm */
 private Pfm2afm(RandomAccessFileOrArray inp, Stream outp)
 {
     this.inp  = inp;
     encoding  = Encoding.GetEncoding(1252);
     this.outp = new StreamWriter(outp, encoding);
 }
예제 #6
0
        /** Reads the font metrics
         * @param rf the AFM file
         * @throws DocumentException the AFM file is invalid
         * @throws IOException the AFM file could not be read
         */
        public void Process(RandomAccessFileOrArray rf)
        {
            string line;
            bool   isMetrics = false;

            while ((line = rf.ReadLine()) != null)
            {
                StringTokenizer tok = new StringTokenizer(line, " ,\n\r\t\f");
                if (!tok.HasMoreTokens())
                {
                    continue;
                }
                string ident = tok.NextToken();
                if (ident.Equals("FontName"))
                {
                    FontName = tok.NextToken("\u00ff").Substring(1);
                }
                else if (ident.Equals("FullName"))
                {
                    FullName = tok.NextToken("\u00ff").Substring(1);
                }
                else if (ident.Equals("FamilyName"))
                {
                    FamilyName = tok.NextToken("\u00ff").Substring(1);
                }
                else if (ident.Equals("Weight"))
                {
                    Weight = tok.NextToken("\u00ff").Substring(1);
                }
                else if (ident.Equals("ItalicAngle"))
                {
                    ItalicAngle = float.Parse(tok.NextToken(), System.Globalization.NumberFormatInfo.InvariantInfo);
                }
                else if (ident.Equals("IsFixedPitch"))
                {
                    IsFixedPitch = tok.NextToken().Equals("true");
                }
                else if (ident.Equals("CharacterSet"))
                {
                    CharacterSet = tok.NextToken("\u00ff").Substring(1);
                }
                else if (ident.Equals("FontBBox"))
                {
                    llx = (int)float.Parse(tok.NextToken(), System.Globalization.NumberFormatInfo.InvariantInfo);
                    lly = (int)float.Parse(tok.NextToken(), System.Globalization.NumberFormatInfo.InvariantInfo);
                    urx = (int)float.Parse(tok.NextToken(), System.Globalization.NumberFormatInfo.InvariantInfo);
                    ury = (int)float.Parse(tok.NextToken(), System.Globalization.NumberFormatInfo.InvariantInfo);
                }
                else if (ident.Equals("UnderlinePosition"))
                {
                    UnderlinePosition = (int)float.Parse(tok.NextToken(), System.Globalization.NumberFormatInfo.InvariantInfo);
                }
                else if (ident.Equals("UnderlineThickness"))
                {
                    UnderlineThickness = (int)float.Parse(tok.NextToken(), System.Globalization.NumberFormatInfo.InvariantInfo);
                }
                else if (ident.Equals("EncodingScheme"))
                {
                    EncodingScheme = tok.NextToken("\u00ff").Substring(1);
                }
                else if (ident.Equals("CapHeight"))
                {
                    CapHeight = (int)float.Parse(tok.NextToken(), System.Globalization.NumberFormatInfo.InvariantInfo);
                }
                else if (ident.Equals("XHeight"))
                {
                    XHeight = (int)float.Parse(tok.NextToken(), System.Globalization.NumberFormatInfo.InvariantInfo);
                }
                else if (ident.Equals("Ascender"))
                {
                    Ascender = (int)float.Parse(tok.NextToken(), System.Globalization.NumberFormatInfo.InvariantInfo);
                }
                else if (ident.Equals("Descender"))
                {
                    Descender = (int)float.Parse(tok.NextToken(), System.Globalization.NumberFormatInfo.InvariantInfo);
                }
                else if (ident.Equals("StdHW"))
                {
                    StdHW = (int)float.Parse(tok.NextToken(), System.Globalization.NumberFormatInfo.InvariantInfo);
                }
                else if (ident.Equals("StdVW"))
                {
                    StdVW = (int)float.Parse(tok.NextToken(), System.Globalization.NumberFormatInfo.InvariantInfo);
                }
                else if (ident.Equals("StartCharMetrics"))
                {
                    isMetrics = true;
                    break;
                }
            }
            if (!isMetrics)
            {
                throw new DocumentException("Missing StartCharMetrics in " + fileName);
            }
            while ((line = rf.ReadLine()) != null)
            {
                StringTokenizer tok = new StringTokenizer(line);
                if (!tok.HasMoreTokens())
                {
                    continue;
                }
                string ident = tok.NextToken();
                if (ident.Equals("EndCharMetrics"))
                {
                    isMetrics = false;
                    break;
                }
                int    C  = -1;
                int    WX = 250;
                string N  = "";
                int[]  B  = null;

                tok = new StringTokenizer(line, ";");
                while (tok.HasMoreTokens())
                {
                    StringTokenizer tokc = new StringTokenizer(tok.NextToken());
                    if (!tokc.HasMoreTokens())
                    {
                        continue;
                    }
                    ident = tokc.NextToken();
                    if (ident.Equals("C"))
                    {
                        C = int.Parse(tokc.NextToken());
                    }
                    else if (ident.Equals("WX"))
                    {
                        WX = (int)float.Parse(tokc.NextToken(), System.Globalization.NumberFormatInfo.InvariantInfo);
                    }
                    else if (ident.Equals("N"))
                    {
                        N = tokc.NextToken();
                    }
                    else if (ident.Equals("B"))
                    {
                        B = new int[] { int.Parse(tokc.NextToken()),
                                        int.Parse(tokc.NextToken()),
                                        int.Parse(tokc.NextToken()),
                                        int.Parse(tokc.NextToken()) };
                    }
                }
                Object[] metrics = new Object[] { C, WX, N, B };
                if (C >= 0)
                {
                    CharMetrics[C] = metrics;
                }
                CharMetrics[N] = metrics;
            }
            if (isMetrics)
            {
                throw new DocumentException("Missing EndCharMetrics in " + fileName);
            }
            if (!CharMetrics.ContainsKey("nonbreakingspace"))
            {
                Object[] space = (Object[])CharMetrics["space"];
                if (space != null)
                {
                    CharMetrics["nonbreakingspace"] = space;
                }
            }
            while ((line = rf.ReadLine()) != null)
            {
                StringTokenizer tok = new StringTokenizer(line);
                if (!tok.HasMoreTokens())
                {
                    continue;
                }
                string ident = tok.NextToken();
                if (ident.Equals("EndFontMetrics"))
                {
                    return;
                }
                if (ident.Equals("StartKernPairs"))
                {
                    isMetrics = true;
                    break;
                }
            }
            if (!isMetrics)
            {
                throw new DocumentException("Missing EndFontMetrics in " + fileName);
            }
            while ((line = rf.ReadLine()) != null)
            {
                StringTokenizer tok = new StringTokenizer(line);
                if (!tok.HasMoreTokens())
                {
                    continue;
                }
                string ident = tok.NextToken();
                if (ident.Equals("KPX"))
                {
                    string   first   = tok.NextToken();
                    string   second  = tok.NextToken();
                    int      width   = (int)float.Parse(tok.NextToken(), System.Globalization.NumberFormatInfo.InvariantInfo);
                    Object[] relates = (Object[])KernPairs[first];
                    if (relates == null)
                    {
                        KernPairs[first] = new Object[] { second, width }
                    }
                    ;
                    else
                    {
                        int      n        = relates.Length;
                        Object[] relates2 = new Object[n + 2];
                        Array.Copy(relates, 0, relates2, 0, n);
                        relates2[n]      = second;
                        relates2[n + 1]  = width;
                        KernPairs[first] = relates2;
                    }
                }
                else if (ident.Equals("EndKernPairs"))
                {
                    isMetrics = false;
                    break;
                }
            }
            if (isMetrics)
            {
                throw new DocumentException("Missing EndKernPairs in " + fileName);
            }
            rf.Close();
        }
예제 #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("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 = GetResourceStream(RESOURCE_PATH + afmFile + ".afm");
                    if (istr == null)
                    {
                        Console.Error.WriteLine(afmFile + " not found as resource.");
                        throw new DocumentException(afmFile + " not found as resource.");
                    }
                    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(afmFile + " is not an AFM or PFM font file.");
            }
            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
 public PRTokeniser(RandomAccessFileOrArray file)
 {
     this.file = file;
 }
예제 #9
0
 public PRTokeniser(byte[] pdfIn)
 {
     file = new RandomAccessFileOrArray(pdfIn);
 }
예제 #10
0
 public PRTokeniser(string filename)
 {
     file = new RandomAccessFileOrArray(filename);
 }