예제 #1
0
        public virtual void SeekTest()
        {
            RandomAccessFileOrArray raf = new RandomAccessFileOrArray(new RandomAccessSourceFactory().CreateBestSource
                                                                          (SOURCE_FOLDER + "NotoSansCJKjp-Bold.otf"));

            byte[] cff = new byte[16014190];
            try {
                raf.Seek(283192);
                raf.ReadFully(cff);
            }
            finally {
                raf.Close();
            }
            CFFFont cffFont = new CFFFont(cff);

            cffFont.Seek(0);
            // Get int (bin 0000 0001 0000 0000  0000 0100 0000 0011)
            NUnit.Framework.Assert.AreEqual(16778243, cffFont.GetInt());
            cffFont.Seek(0);
            // Gets the first short (bin 0000 0001 0000 0000)
            NUnit.Framework.Assert.AreEqual(256, cffFont.GetShort());
            cffFont.Seek(2);
            // Gets the second short (bin 0000 0100 0000 0011)
            NUnit.Framework.Assert.AreEqual(1027, cffFont.GetShort());
        }
예제 #2
0
        /**
         * This will read the required data from the stream.
         *
         * @param ttf The font that is being read.
         * @param data The stream to read the data from.
         * @throws java.io.IOException If there is an error reading the data.
         */
        public override void Read(TrueTypeFont ttf, TTFDataStream data)
        {
            byte[] bytes = data.Read((int)Length);

            CFFParser parser = new CFFParser();

            cffFont = parser.Parse(bytes, new CFFBytesource(font))[0];

            initialized = true;
        }
예제 #3
0
        public virtual void GetPositionTest()
        {
            RandomAccessFileOrArray raf = new RandomAccessFileOrArray(new RandomAccessSourceFactory().CreateBestSource
                                                                          (SOURCE_FOLDER + "NotoSansCJKjp-Bold.otf"));

            byte[] cff = new byte[16014190];
            try {
                raf.Seek(283192);
                raf.ReadFully(cff);
            }
            finally {
                raf.Close();
            }
            CFFFont cffFont = new CFFFont(cff);

            cffFont.Seek(0);
            NUnit.Framework.Assert.AreEqual(0, cffFont.GetPosition());
            cffFont.Seek(16);
            NUnit.Framework.Assert.AreEqual(16, cffFont.GetPosition());
        }
예제 #4
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));
        }