Beispiel #1
0
        /**
         * Constructor.
         *
         * @param fontDictionary the corresponding dictionary
         * @throws IOException it something went wrong
         */
        public PdfType1CFont(PdfDirectObject fontDictionary)
            : base(fontDictionary)
        {
            FontDescriptor fd = FontDescriptor;

            byte[] bytes = null;
            if (fd != null)
            {
                var ff3Stream = fd.FontFile3;
                if (ff3Stream != null)
                {
                    bytes = ff3Stream.BaseDataObject.ExtractBody(true).GetBuffer();
                    if (bytes.Length == 0)
                    {
                        Debug.WriteLine($"error: Invalid data for embedded Type1C font {Name}");
                        bytes = null;
                    }
                }
            }

            bool         fontIsDamaged = false;
            CFFType1Font cffEmbedded   = null;

            try
            {
                if (bytes != null)
                {
                    // note: this could be an OpenType file, fortunately CFFParser can handle that
                    CFFParser cffParser = new CFFParser();
                    cffEmbedded = (CFFType1Font)cffParser.Parse(bytes, new FF3ByteSource(fd, bytes))[0];
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine($"error: Can't read the embedded Type1C font {Name} {e}");
                fontIsDamaged = true;
            }
            isDamaged = fontIsDamaged;
            cffFont   = cffEmbedded;

            if (cffFont != null)
            {
                genericFont = cffFont;
                isEmbedded  = true;
            }
            else
            {
                FontMapping <BaseFont> mapping = FontMappers.Instance.GetBaseFont(BaseFont, fd);
                genericFont = mapping.Font;

                if (mapping.IsFallback)
                {
                    Debug.WriteLine($"warn: Using fallback font {genericFont.Name} for {BaseFont}");
                }
                isEmbedded = false;
            }
            ReadEncoding();
            fontMatrixTransform = FontMatrix;
            fontMatrixTransform = fontMatrixTransform.PreConcat(SKMatrix.CreateScale(1000, 1000));
        }
Beispiel #2
0
        /**
         * Constructor.
         *
         * @param fontDictionary The font dictionary according to the PDF specification.
         * @param parent The parent font.
         */
        public CIDFontType0(PdfDirectObject fontDictionary, PdfType0Font parent)
            : base(fontDictionary, parent)
        {
            FontDescriptor fd       = FontDescriptor;
            FontFile       fontFile = null;

            byte[] bytes = null;
            if (fd != null)
            {
                fontFile = fd.FontFile3 ?? fd.FontFile;
                if (fontFile != null)
                {
                    bytes = fontFile.BaseDataObject.ExtractBody(true).GetBuffer();
                }
            }

            bool    fontIsDamaged = false;
            CFFFont cffFont       = null;

            if (bytes != null && bytes.Length > 0 && (bytes[0] & 0xff) == '%')
            {
                // PDFBOX-2642 contains a corrupt PFB font instead of a CFF
                Debug.WriteLine("warn: Found PFB but expected embedded CFF font " + fd.FontName);

                try
                {
                    t1Font = PdfType1Font.LoadType1Font(fontFile);
                }
                catch (DamagedFontException e)
                {
                    Debug.WriteLine($"warn: Can't read damaged embedded Type1 font {fd.FontName} {e}");
                    fontIsDamaged = true;
                }
                catch (IOException e)
                {
                    Debug.WriteLine($"error: Can't read the embedded Type1 font {fd.FontName} {e}");
                    fontIsDamaged = true;
                }
            }
            else if (bytes != null)
            {
                CFFParser cffParser = new CFFParser();
                try
                {
                    cffFont = cffParser.Parse(bytes, new FF3ByteSource(fd, bytes))[0];
                }
                catch (IOException e)
                {
                    Debug.WriteLine("error: Can't read the embedded CFF font " + fd.FontName, e);
                    fontIsDamaged = true;
                }
            }

            if (cffFont != null)
            {
                // embedded
                if (cffFont is CFFCIDFont)
                {
                    cidFont = (CFFCIDFont)cffFont;
                    t1Font  = null;
                }
                else
                {
                    cidFont = null;
                    t1Font  = cffFont;
                }
                cid2gid    = ReadCIDToGIDMap();
                isEmbedded = true;
                isDamaged  = false;
            }
            else if (t1Font != null)
            {
                if (t1Font is Type1Font type1Font)
                {
                    cidFont = null;
                }
                cid2gid    = ReadCIDToGIDMap();
                isEmbedded = true;
                isDamaged  = false;
            }
            else
            {
                // find font or substitute
                CIDFontMapping mapping = FontMappers.Instance.GetCIDFont(BaseFont, FontDescriptor, CIDSystemInfo);
                BaseFont       font;
                if (mapping.IsCIDFont)
                {
                    cffFont = mapping.Font.CFF.Font;
                    if (cffFont is CFFCIDFont)
                    {
                        cidFont = (CFFCIDFont)cffFont;
                        t1Font  = null;
                        font    = cidFont;
                    }
                    else
                    {
                        // PDFBOX-3515: OpenType fonts are loaded as CFFType1Font
                        CFFType1Font f = (CFFType1Font)cffFont;
                        cidFont = null;
                        t1Font  = f;
                        font    = f;
                    }
                }
                else
                {
                    cidFont = null;
                    t1Font  = mapping.TrueTypeFont;
                    font    = t1Font;
                }

                if (mapping.IsFallback)
                {
                    Debug.WriteLine($"warning: Using fallback {font.Name} for CID-keyed font {BaseFont}");
                }
                isEmbedded = false;
                isDamaged  = fontIsDamaged;
            }
            fontMatrixTransform = FontMatrix;
            fontMatrixTransform = fontMatrixTransform.PostConcat(SKMatrix.CreateScale(1000, 1000));
        }