예제 #1
0
        /**
         * Creates a new TrueType font for embedding.
         */
        public TrueTypeEmbedder(Document document, PdfDictionary dict, TrueTypeFont ttf, bool embedSubset)
        {
            this.document    = document;
            this.embedSubset = embedSubset;
            this.ttf         = ttf;
            fontDescriptor   = CreateFontDescriptor(ttf);

            if (!IsEmbeddingPermitted(ttf))
            {
                throw new IOException("This font does not permit embedding");
            }

            if (!embedSubset)
            {
                // full embedding
                PdfStream stream = new PdfStream(ttf.OriginalData);
                stream.Header[PdfName.Length]      =
                    stream.Header[PdfName.Length1] = PdfInteger.Get(ttf.OriginalDataSize);
                fontDescriptor.FontFile2           = new FontFile(document, stream);
            }
            dict[PdfName.Type]     = PdfName.Font;
            dict[PdfName.BaseFont] = new PdfName(ttf.Name);

            // choose a Unicode "cmap"
            cmapLookup = ttf.GetUnicodeCmapLookup();
        }
예제 #2
0
        internal PdfType0Font(Document document, TrueTypeFont ttf, bool embedSubset, bool closeTTF, bool vertical)
            : base(document, new PdfDictionary())
        {
            if (vertical)
            {
                ttf.EnableVerticalSubstitutions();
            }

            gsubData   = ttf.GsubData;
            cmapLookup = ttf.GetUnicodeCmapLookup();

            embedder = new PdfCIDFontType2Embedder(document, Dictionary, ttf, embedSubset, this, vertical);
            CIDFont  = embedder.GetCIDFont();
            ReadEncoding();
            if (closeTTF)
            {
                if (embedSubset)
                {
                    this.ttf = ttf;
                    //TODO document.registerTrueTypeFontForClosing(ttf);
                }
                else
                {
                    // the TTF is fully loaded and it is safe to close the underlying data source
                    ttf.Dispose();
                }
            }
        }
예제 #3
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();
        }