Beispiel #1
0
 /**
  * Constructor.
  *
  * @param fontDictionary The font dictionary according to the PDF specification.
  */
 public CIDFont(PdfDirectObject fontObject, PdfType0Font parent)
     : base(fontObject)
 {
     this.parent = parent;
     ReadWidths();
     ReadVerticalDisplacements();
 }
Beispiel #2
0
        public static CIDFont WrapFont(PdfDirectObject pdfObject, PdfType0Font PdfType0Font)
        {
            if (pdfObject.Wrapper is CIDFont cidFont)
            {
                return(cidFont);
            }
            else if (pdfObject.Reference?.Wrapper is CIDFont cidFontRef)
            {
                return(cidFontRef);
            }

            var pdfDictionary = (PdfDictionary)pdfObject.Resolve();
            var subType       = (PdfName)pdfDictionary[PdfName.Subtype];

            if (subType.Equals(PdfName.CIDFontType0))
            {
                cidFont = new CIDFontType0(pdfObject, PdfType0Font);
            }
            else if (subType.Equals(PdfName.CIDFontType2))
            {
                cidFont = new CIDFontType2(pdfObject, PdfType0Font);
            }
            else
            {
                throw new NotSupportedException();
            }

            return(cidFont);
        }
        /**
         * Creates a new TrueType font embedder for the given TTF as a PDCIDFontType2.
         *
         * @param document parent document
         * @param dict font dictionary
         * @param ttf True Type Font
         * @param parent parent Type 0 font
         * @ if the TTF could not be read
         */
        public PdfCIDFontType2Embedder(Document document, PdfDictionary dict, TrueTypeFont ttf, bool embedSubset, PdfType0Font parent, bool vertical)
            : base(document, dict, ttf, embedSubset)
        {
            this.document = document;
            this.dict     = dict;
            this.parent   = parent;
            this.vertical = vertical;

            // parent Type 0 font
            dict[PdfName.Subtype]  = PdfName.Type0;
            dict[PdfName.BaseFont] = PdfName.Get(FontDescriptor.FontName);
            dict[PdfName.Encoding] = vertical ? PdfName.IdentityV : PdfName.IdentityH; // CID = GID

            // descendant CIDFont
            cidFont = CreateCIDFont();
            PdfArray descendantFonts = new PdfArray();

            descendantFonts.Add(document.File.Register(cidFont));
            dict[PdfName.DescendantFonts] = descendantFonts;

            if (!embedSubset)
            {
                // build GID -> Unicode map
                BuildToUnicodeCMap(null);
            }
        }
Beispiel #4
0
        /**
         * Constructor.
         *
         * @param fontDictionary The font dictionary according to the PDF specification.
         * @param parent The parent font.
         * @param trueTypeFont The true type font used to create the parent font
         * @throws IOException
         */
        public CIDFontType2(PdfDirectObject fontDictionary, PdfType0Font parent, TrueTypeFont trueTypeFont)
            : base(fontDictionary, parent)
        {
            FontDescriptor fd = FontDescriptor;

            if (trueTypeFont != null)
            {
                ttf        = trueTypeFont;
                isEmbedded = true;
                isDamaged  = false;
            }
            else
            {
                bool         fontIsDamaged = false;
                TrueTypeFont ttfFont       = null;

                FontFile stream = null;
                if (fd != null)
                {
                    // Acrobat looks in FontFile too, even though it is not in the spec, see PDFBOX-2599
                    stream = fd.FontFile2 ?? fd.FontFile3 ?? fd.FontFile;
                }
                if (stream != null)
                {
                    try
                    {
                        // embedded OTF or TTF
                        OTFParser otfParser = new OTFParser(true);
                        ttfFont = otfParser.Parse((Bytes.Buffer)stream.BaseDataObject.ExtractBody(true), fd.FontName);

                        if (ttfFont is OpenTypeFont otf && otf.IsPostScript)
                        {
                            // PDFBOX-3344 contains PostScript outlines instead of TrueType
                            fontIsDamaged = true;
                            Debug.WriteLine($"warning: Found CFF/OTF but expected embedded TTF font {fd.FontName}");
                        }
                    }
                    catch (IOException e)
                    {
                        fontIsDamaged = true;
                        Debug.WriteLine($"warning: Could not read embedded OTF for font {BaseFont} {e}");
                    }
                }
                isEmbedded = ttfFont != null;
                isDamaged  = fontIsDamaged;

                if (ttfFont == null)
                {
                    ttfFont = FindFontOrSubstitute();
                }
                ttf = ttfFont;
            }
            cmapLookup = ttf.GetUnicodeCmapLookup(false);
            cid2gid    = ReadCIDToGIDMap();
        }
Beispiel #5
0
 /**
  * <summary>Creates the representation of a font.</summary>
  */
 public static Font Get(Document context, string path)
 {
     return(PdfType0Font.Load(context, path));
 }
Beispiel #6
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));
        }
Beispiel #7
0
 /**
  * Constructor.
  *
  * @param fontDictionary The font dictionary according to the PDF specification.
  * @param parent The parent font.
  * @throws IOException
  */
 public CIDFontType2(PdfDirectObject fontDictionary, PdfType0Font parent)
     : this(fontDictionary, parent, null)
 {
 }