Ejemplo n.º 1
0
        public virtual void TrueTypeFontAndCmapConstructorTest()
        {
            TrueTypeFont ttf       = new TrueTypeFont(sourceFolder + "NotoSerif-Regular_v1.7.ttf");
            PdfType0Font type0Font = new PdfType0Font(ttf, PdfEncodings.IDENTITY_H);
            CMapEncoding cmap      = type0Font.GetCmap();

            NUnit.Framework.Assert.IsNotNull(cmap);
            NUnit.Framework.Assert.IsTrue(cmap.IsDirect());
            NUnit.Framework.Assert.IsFalse(cmap.HasUniMap());
            NUnit.Framework.Assert.IsNull(cmap.GetUniMapName());
            NUnit.Framework.Assert.AreEqual("Adobe", cmap.GetRegistry());
            NUnit.Framework.Assert.AreEqual("Identity", cmap.GetOrdering());
            NUnit.Framework.Assert.AreEqual(0, cmap.GetSupplement());
            NUnit.Framework.Assert.AreEqual(PdfEncodings.IDENTITY_H, cmap.GetCmapName());
        }
Ejemplo n.º 2
0
        public virtual void DictionaryConstructorTest()
        {
            String        filePath    = sourceFolder + "documentWithType0Noto.pdf";
            PdfDocument   pdfDocument = new PdfDocument(new PdfReader(filePath));
            PdfDictionary fontDict    = pdfDocument.GetPage(1).GetResources().GetResource(PdfName.Font).GetAsDictionary(new
                                                                                                                        PdfName("F1"));
            PdfType0Font type0Font = new PdfType0Font(fontDict);
            CMapEncoding cmap      = type0Font.GetCmap();

            NUnit.Framework.Assert.IsNotNull(cmap);
            NUnit.Framework.Assert.IsTrue(cmap.IsDirect());
            NUnit.Framework.Assert.IsFalse(cmap.HasUniMap());
            NUnit.Framework.Assert.IsNull(cmap.GetUniMapName());
            NUnit.Framework.Assert.AreEqual("Adobe", cmap.GetRegistry());
            NUnit.Framework.Assert.AreEqual("Identity", cmap.GetOrdering());
            NUnit.Framework.Assert.AreEqual(0, cmap.GetSupplement());
            NUnit.Framework.Assert.AreEqual(PdfEncodings.IDENTITY_H, cmap.GetCmapName());
        }
Ejemplo n.º 3
0
 private void FlushFontData()
 {
     if (cidFontType == CID_FONT_TYPE_0)
     {
         GetPdfObject().Put(PdfName.Type, PdfName.Font);
         GetPdfObject().Put(PdfName.Subtype, PdfName.Type0);
         String name  = fontProgram.GetFontNames().GetFontName();
         String style = fontProgram.GetFontNames().GetStyle();
         if (style.Length > 0)
         {
             name += "-" + style;
         }
         GetPdfObject().Put(PdfName.BaseFont, new PdfName(MessageFormatUtil.Format("{0}-{1}", name, cmapEncoding.GetCmapName
                                                                                       ())));
         GetPdfObject().Put(PdfName.Encoding, new PdfName(cmapEncoding.GetCmapName()));
         PdfDictionary fontDescriptor = GetFontDescriptor(name);
         PdfDictionary cidFont        = GetCidFont(fontDescriptor, fontProgram.GetFontNames().GetFontName(), false);
         GetPdfObject().Put(PdfName.DescendantFonts, new PdfArray(cidFont));
         fontDescriptor.Flush();
         cidFont.Flush();
     }
     else
     {
         if (cidFontType == CID_FONT_TYPE_2)
         {
             TrueTypeFont  ttf            = (TrueTypeFont)GetFontProgram();
             String        fontName       = UpdateSubsetPrefix(ttf.GetFontNames().GetFontName(), subset, embedded);
             PdfDictionary fontDescriptor = GetFontDescriptor(fontName);
             PdfStream     fontStream;
             ttf.UpdateUsedGlyphs((SortedSet <int>)longTag, subset, subsetRanges);
             if (ttf.IsCff())
             {
                 byte[] cffBytes;
                 if (subset)
                 {
                     cffBytes = new CFFFontSubset(ttf.GetFontStreamBytes(), longTag).Process();
                 }
                 else
                 {
                     cffBytes = ttf.GetFontStreamBytes();
                 }
                 fontStream = GetPdfFontStream(cffBytes, new int[] { cffBytes.Length });
                 fontStream.Put(PdfName.Subtype, new PdfName("CIDFontType0C"));
                 // The PDF Reference manual advises to add -cmap in case CIDFontType0
                 GetPdfObject().Put(PdfName.BaseFont, new PdfName(MessageFormatUtil.Format("{0}-{1}", fontName, cmapEncoding
                                                                                           .GetCmapName())));
                 fontDescriptor.Put(PdfName.FontFile3, fontStream);
             }
             else
             {
                 byte[] ttfBytes = null;
                 //getDirectoryOffset() > 0 means ttc, which shall be subsetted anyway.
                 if (subset || ttf.GetDirectoryOffset() > 0)
                 {
                     try {
                         ttfBytes = ttf.GetSubset(longTag, subset);
                     }
                     catch (iText.IO.IOException) {
                         ILog logger = LogManager.GetLogger(typeof(iText.Kernel.Font.PdfType0Font));
                         logger.Warn(iText.IO.LogMessageConstant.FONT_SUBSET_ISSUE);
                         ttfBytes = null;
                     }
                 }
                 if (ttfBytes == null)
                 {
                     ttfBytes = ttf.GetFontStreamBytes();
                 }
                 fontStream = GetPdfFontStream(ttfBytes, new int[] { ttfBytes.Length });
                 GetPdfObject().Put(PdfName.BaseFont, new PdfName(fontName));
                 fontDescriptor.Put(PdfName.FontFile2, fontStream);
             }
             // CIDSet shall be based on font.numberOfGlyphs property of the font, it is maxp.numGlyphs for ttf,
             // because technically we convert all unused glyphs to space, e.g. just remove outlines.
             int    numOfGlyphs = ttf.GetFontMetrics().GetNumberOfGlyphs();
             byte[] cidSetBytes = new byte[ttf.GetFontMetrics().GetNumberOfGlyphs() / 8 + 1];
             for (int i = 0; i < numOfGlyphs / 8; i++)
             {
                 cidSetBytes[i] |= 0xff;
             }
             for (int i = 0; i < numOfGlyphs % 8; i++)
             {
                 cidSetBytes[cidSetBytes.Length - 1] |= rotbits[i];
             }
             fontDescriptor.Put(PdfName.CIDSet, new PdfStream(cidSetBytes));
             PdfDictionary cidFont = GetCidFont(fontDescriptor, fontName, !ttf.IsCff());
             GetPdfObject().Put(PdfName.Type, PdfName.Font);
             GetPdfObject().Put(PdfName.Subtype, PdfName.Type0);
             GetPdfObject().Put(PdfName.Encoding, new PdfName(cmapEncoding.GetCmapName()));
             GetPdfObject().Put(PdfName.DescendantFonts, new PdfArray(cidFont));
             PdfStream toUnicode = GetToUnicode();
             if (toUnicode != null)
             {
                 GetPdfObject().Put(PdfName.ToUnicode, toUnicode);
                 if (toUnicode.GetIndirectReference() != null)
                 {
                     toUnicode.Flush();
                 }
             }
             // getPdfObject().getIndirectReference() != null by assertion of PdfType0Font#flush()
             // This means, that fontDescriptor, cidFont and fontStream already are indirects
             if (GetPdfObject().GetIndirectReference().GetDocument().GetPdfVersion().CompareTo(PdfVersion.PDF_2_0) >= 0
                 )
             {
                 // CIDSet is deprecated in PDF 2.0
                 fontDescriptor.Remove(PdfName.CIDSet);
             }
             fontDescriptor.Flush();
             cidFont.Flush();
             fontStream.Flush();
         }
         else
         {
             throw new InvalidOperationException("Unsupported CID Font");
         }
     }
 }