Example #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));
        }
Example #2
0
        internal PdfType1Font(PdfDirectObject baseObject) : base(baseObject)
        {
            codeToBytesMap = new Dictionary <int, byte[]>();

            var       fd            = FontDescriptor;
            Type1Font t1            = null;
            bool      fontIsDamaged = false;

            if (fd != null)
            {
                // a Type1 font may contain a Type1C font
                var fontFile3 = fd.FontFile3;
                if (fontFile3 != null)
                {
                    throw new ArgumentException("Use PDType1CFont for FontFile3");
                }

                // or it may contain a PFB
                var fontFile = fd.FontFile;
                if (fontFile != null)
                {
                    try
                    {
                        t1 = 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;
                    }
                }
            }
            isEmbedded = t1 != null;
            isDamaged  = fontIsDamaged;
            type1font  = t1;

            // find a generic font to use for rendering, could be a .pfb, but might be a .ttf
            if (type1font != null)
            {
                genericFont = type1font;
            }
            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}");
                }
            }
            ReadEncoding();
            fontMatrixTransform = FontMatrix;
            fontMatrixTransform = fontMatrixTransform.PreConcat(SKMatrix.CreateScale(1000, 1000));
        }
Example #3
0
        public PdfType1Font(Document context, string baseFont) : base(context, baseFont)
        {
            Dictionary[PdfName.Subtype]  = PdfName.Type1;
            Dictionary[PdfName.BaseFont] = PdfName.Get(baseFont);
            switch (baseFont)
            {
            case "ZapfDingbats":
                encoding = ZapfDingbatsEncoding.Instance;
                break;

            case "Symbol":
                encoding = SymbolEncoding.Instance;
                break;

            default:
                encoding = WinAnsiEncoding.Instance;
                Dictionary[PdfName.Encoding] = PdfName.WinAnsiEncoding;
                break;
            }

            // standard 14 fonts may be accessed concurrently, as they are singletons
            codeToBytesMap = new Dictionary <int, byte[]>();

            // todo: could load the PFB font here if we wanted to support Standard 14 embedding
            type1font = null;
            FontMapping <BaseFont> mapping = FontMappers.Instance.GetBaseFont(BaseFont, FontDescriptor);

            genericFont = mapping.Font;

            if (mapping.IsFallback)
            {
                string fontName;
                try
                {
                    fontName = genericFont.Name;
                }
                catch (IOException e)
                {
                    Debug.WriteLine($"debug: Couldn't get font name - setting to '?' {e}");
                    fontName = "?";
                }
                Debug.WriteLine($"warn: Using fallback font {fontName} for base font {BaseFont}");
            }
            isEmbedded          = false;
            isDamaged           = false;
            fontMatrixTransform = SKMatrix.Identity;
        }
        internal PdfTrueTypeFont(PdfDirectObject baseObject)
            : base(baseObject)
        {
            TrueTypeFont ttfFont       = null;
            bool         fontIsDamaged = false;

            if (FontDescriptor != null)
            {
                var fd        = base.FontDescriptor;
                var ff2Stream = fd.FontFile2;
                if (ff2Stream != null)
                {
                    try
                    {
                        // embedded
                        TTFParser ttfParser = new TTFParser(true);
                        ttfFont = ttfParser.Parse(ff2Stream.BaseDataObject.ExtractBody(true));
                    }
                    catch (IOException e)
                    {
                        Debug.WriteLine($"warn: Could not read embedded TTF for font {BaseFont} {e}");
                        fontIsDamaged = true;
                    }
                }
            }
            isEmbedded = ttfFont != null;
            isDamaged  = fontIsDamaged;

            // substitute
            if (ttfFont == null)
            {
                FontMapping <TrueTypeFont> mapping = FontMappers.Instance.GetTrueTypeFont(BaseFont, FontDescriptor);
                ttfFont = mapping.Font;

                if (mapping.IsFallback)
                {
                    Debug.WriteLine($"warn: Using fallback font '{ttfFont}' for '{BaseFont}'");
                }
            }
            ttf = ttfFont;
            ReadEncoding();
        }