public virtual void GetCid2ByteCMapNoFontAsian()
 {
     NUnit.Framework.Assert.That(() => {
         // Without font-asian module in the class path
         // no CMap can be found.
         FontCache.GetCid2Byte("78ms-RKSJ-H");
     }
                                 , NUnit.Framework.Throws.InstanceOf <iText.IO.IOException>())
     ;
 }
 public virtual void GetUni2CidCMapNoFontAsian()
 {
     NUnit.Framework.Assert.That(() => {
         // Without font-asian module in the class path
         // no CMap can be found.
         FontCache.GetUni2CidCmap("UniJIS-UTF16-H");
     }
                                 , NUnit.Framework.Throws.InstanceOf <iText.IO.IOException>())
     ;
 }
        public virtual void ClearFontCacheTest()
        {
            String fontName = "FreeSans.ttf";

            NUnit.Framework.Assert.IsNull(FontCache.GetFont(fontName));
            FontProgram fontProgram = new FontCacheNoFontAsianTest.FontProgramMock();

            FontCache.SaveFont(fontProgram, fontName);
            NUnit.Framework.Assert.AreEqual(fontProgram, FontCache.GetFont(fontName));
            FontCache.ClearSavedFonts();
            NUnit.Framework.Assert.IsNull(FontCache.GetFont(fontName));
        }
예제 #4
0
        /// <summary>
        /// Finds all CMap names that belong to the same registry to which a given
        /// font belongs.
        /// </summary>
        /// <param name="fontName">a name of the font for which CMap's are searched.</param>
        /// <returns>a set of CMap names corresponding to the given font.</returns>
        public static ICollection <String> GetCompatibleCmaps(String fontName)
        {
            IDictionary <String, Object> cidFonts = FontCache.GetAllPredefinedCidFonts().Get(fontName);

            if (cidFonts == null)
            {
                return(null);
            }
            String registry = (String)cidFonts.Get(REGISTRY_PROP);

            return(registryNames.Get(registry));
        }
예제 #5
0
        /// <summary>
        /// Creates a new True Type font from ttc file,
        /// <p/>
        /// The fonts may or may not be cached depending on the flag <CODE>cached</CODE>.
        /// </summary>
        /// <remarks>
        /// Creates a new True Type font from ttc file,
        /// <p/>
        /// The fonts may or may not be cached depending on the flag <CODE>cached</CODE>.
        /// If the <CODE>byte</CODE> arrays are present the font will be
        /// read from them instead of the name. A name is still required to identify
        /// the font type.
        /// <p/>
        /// Besides the common encodings described by name, custom encodings
        /// can also be made. These encodings will only work for the single byte fonts
        /// Type1 and TrueType. The encoding string starts with a '#'
        /// followed by "simple" or "full". If "simple" there is a decimal for the first character position and then a list
        /// of hex values representing the Unicode codes that compose that encoding.<br />
        /// The "simple" encoding is recommended for TrueType fonts
        /// as the "full" encoding risks not matching the character with the right glyph
        /// if not done with care.<br />
        /// The "full" encoding is specially aimed at Type1 fonts where the glyphs have to be
        /// described by non standard names like the Tex math fonts. Each group of three elements
        /// compose a code position: the one byte code order in decimal or as 'x' (x cannot be the space), the name and the Unicode character
        /// used to access the glyph. The space must be assigned to character position 32 otherwise
        /// text justification will not work.
        /// <p/>
        /// Example for a "simple" encoding that includes the Unicode
        /// character space, A, B and ecyrillic:
        /// <PRE>
        /// "# simple 32 0020 0041 0042 0454"
        /// </PRE>
        /// <p/>
        /// Example for a "full" encoding for a Type1 Tex font:
        /// <PRE>
        /// "# full 'A' nottriangeqlleft 0041 'B' dividemultiply 0042 32 space 0020"
        /// </PRE>
        /// </remarks>
        /// <param name="ttcPath">location  of true type collection file (*.ttc)</param>
        /// <param name="ttcIndex">the encoding to be applied to this font</param>
        /// <param name="cached">
        /// true if the font comes from the cache or is added to
        /// the cache if new, false if the font is always created new
        /// </param>
        /// <returns>
        /// returns a new font. This font may come from the cache but only if cached
        /// is true, otherwise it will always be created new
        /// </returns>
        /// <exception cref="System.IO.IOException"/>
        public static FontProgram CreateFont(String ttcPath, int ttcIndex, bool cached)
        {
            if (cached)
            {
                FontProgram fontFound = FontCache.GetFont(ttcPath + ttcIndex);
                if (fontFound != null)
                {
                    return(fontFound);
                }
            }
            FontProgram fontBuilt = new TrueTypeFont(ttcPath, ttcIndex);

            return(cached ? FontCache.SaveFont(fontBuilt, ttcPath + ttcIndex) : fontBuilt);
        }
        public virtual void FontStringTtcCacheKeyTest()
        {
            String       fontName = "Font.ttc";
            FontCacheKey ttc0     = FontCacheKey.Create(fontName, 0);
            FontCacheKey ttc1     = FontCacheKey.Create(fontName, 1);

            NUnit.Framework.Assert.IsNull(FontCache.GetFont(ttc0));
            NUnit.Framework.Assert.IsNull(FontCache.GetFont(ttc1));
            FontProgram fontProgram = new FontCacheNoFontAsianTest.FontProgramMock();

            FontCache.SaveFont(fontProgram, ttc1);
            NUnit.Framework.Assert.IsNull(FontCache.GetFont(ttc0));
            NUnit.Framework.Assert.AreEqual(fontProgram, FontCache.GetFont(ttc1));
        }
예제 #7
0
        /// <exception cref="System.IO.IOException"/>
        public static FontProgram CreateType1Font(String metricsPath, String binaryPath, bool cached)
        {
            FontProgram fontProgram;

            if (cached && metricsPath != null)
            {
                fontProgram = FontCache.GetFont(metricsPath);
                if (fontProgram != null)
                {
                    return(fontProgram);
                }
            }
            fontProgram = new Type1Font(metricsPath, binaryPath, null, null);
            return(cached && metricsPath != null?FontCache.SaveFont(fontProgram, metricsPath) : fontProgram);
        }
예제 #8
0
        // todo make comment relevant to type 1 font creation
        /// <summary>Creates a new font.</summary>
        /// <remarks>
        /// Creates a new font. This font can be one of the 14 built in types,
        /// a Type1 font referred to by an AFM or PFM file, a TrueType font (simple or collection) or a CJK font from the
        /// Adobe Asian Font Pack. TrueType fonts and CJK fonts can have an optional style modifier
        /// appended to the name. These modifiers are: Bold, Italic and BoldItalic. An
        /// example would be "STSong-Light,Bold". Note that this modifiers do not work if
        /// the font is embedded. Fonts in TrueType collections are addressed by index such as "msgothic.ttc,1".
        /// This would get the second font (indexes start at 0), in this case "MS PGothic".
        /// <p/>
        /// The fonts may or may not be cached depending on the flag <CODE>cached</CODE>.
        /// If the <CODE>byte</CODE> arrays are present the font will be
        /// read from them instead of the name. A name is still required to identify
        /// the font type.
        /// <p/>
        /// Besides the common encodings described by name, custom encodings
        /// can also be made. These encodings will only work for the single byte fonts
        /// Type1 and TrueType. The encoding string starts with a '#'
        /// followed by "simple" or "full". If "simple" there is a decimal for the first character position and then a list
        /// of hex values representing the Unicode codes that compose that encoding.<br />
        /// The "simple" encoding is recommended for TrueType fonts
        /// as the "full" encoding risks not matching the character with the right glyph
        /// if not done with care.<br />
        /// The "full" encoding is specially aimed at Type1 fonts where the glyphs have to be
        /// described by non standard names like the Tex math fonts. Each group of three elements
        /// compose a code position: the one byte code order in decimal or as 'x' (x cannot be the space), the name and the Unicode character
        /// used to access the glyph. The space must be assigned to character position 32 otherwise
        /// text justification will not work.
        /// <p/>
        /// Example for a "simple" encoding that includes the Unicode
        /// character space, A, B and ecyrillic:
        /// <PRE>
        /// "# simple 32 0020 0041 0042 0454"
        /// </PRE>
        /// <p/>
        /// Example for a "full" encoding for a Type1 Tex font:
        /// <PRE>
        /// "# full 'A' nottriangeqlleft 0041 'B' dividemultiply 0042 32 space 0020"
        /// </PRE>
        /// </remarks>
        /// <param name="name">the name of the font or its location on file</param>
        /// <param name="cached">
        /// true if the font comes from the cache or is added to
        /// the cache if new, false if the font is always created new
        /// </param>
        /// <param name="afm">the afm or pfm metrics file in a byte array</param>
        /// <param name="pfb">the pfb in a byte array</param>
        /// <returns>
        /// returns a new font. This font may come from the cache but only if cached
        /// is true, otherwise it will always be created new
        /// </returns>
        /// <exception cref="System.IO.IOException"/>
        public static FontProgram CreateType1Font(String name, byte[] afm, byte[] pfb, bool cached)
        {
            FontProgram fontProgram;

            if (cached && name != null)
            {
                fontProgram = FontCache.GetFont(name);
                if (fontProgram != null)
                {
                    return(fontProgram);
                }
            }
            fontProgram = new Type1Font(name, null, afm, pfb);
            return(cached && name != null?FontCache.SaveFont(fontProgram, name) : fontProgram);
        }
예제 #9
0
 /// <param name="cmap">CMap name.</param>
 /// <param name="uniMap">CMap to convert Unicode value to CID.</param>
 public CMapEncoding(String cmap, String uniMap)
 {
     this.cmap   = cmap;
     this.uniMap = uniMap;
     if (cmap.Equals(PdfEncodings.IDENTITY_H) || cmap.Equals(PdfEncodings.IDENTITY_V))
     {
         cid2Uni  = FontCache.GetCid2UniCmap(uniMap);
         isDirect = true;
     }
     else
     {
         cid2Code = FontCache.GetCid2Byte(cmap);
         code2Cid = cid2Code.GetReversMap();
     }
 }
예제 #10
0
        /// <summary>Creates a new TrueType font program from ttc (TrueType Collection) file bytes.</summary>
        /// <param name="ttc">the content of a TrueType Collection file (*.ttc)</param>
        /// <param name="ttcIndex">the index of the font file from the collection to be read</param>
        /// <param name="cached">
        /// true if the font comes from the cache or is added to
        /// the cache if new, false if the font is always created new
        /// </param>
        /// <returns>
        /// returns a new
        /// <see cref="FontProgram"/>
        /// instance. This font may come from the cache but only if cached
        /// is true, otherwise it will always be created new
        /// </returns>
        public static FontProgram CreateFont(byte[] ttc, int ttcIndex, bool cached)
        {
            FontCacheKey fontKey = FontCacheKey.Create(ttc, ttcIndex);

            if (cached)
            {
                FontProgram fontFound = FontCache.GetFont(fontKey);
                if (fontFound != null)
                {
                    return(fontFound);
                }
            }
            FontProgram fontBuilt = new TrueTypeFont(ttc, ttcIndex);

            return(cached ? FontCache.SaveFont(fontBuilt, fontKey) : fontBuilt);
        }
예제 #11
0
        /// <exception cref="System.IO.IOException"/>
        public static FontProgram CreateFont(byte[] ttc, int ttcIndex, bool cached)
        {
            if (cached)
            {
                String      ttcNameKey = String.Format("{0}{1}", ArrayUtil.HashCode(ttc), ttcIndex);
                FontProgram fontFound  = FontCache.GetFont(ttcNameKey);
                if (fontFound != null)
                {
                    return(fontFound);
                }
            }
            FontProgram fontBuilt    = new TrueTypeFont(ttc, ttcIndex);
            String      ttcNameKey_1 = String.Format("{0}{1}", ArrayUtil.HashCode(ttc), ttcIndex);

            return(cached ? FontCache.SaveFont(fontBuilt, ttcNameKey_1) : fontBuilt);
        }
        private static FontProgramDescriptor FetchCachedDescriptor(String fontName, byte[] fontProgram)
        {
            FontProgram  fontFound;
            FontCacheKey key;

            if (fontName != null)
            {
                key = FontCacheKey.Create(fontName);
            }
            else
            {
                key = FontCacheKey.Create(fontProgram);
            }
            fontFound = FontCache.GetFont(key);
            return(fontFound != null?FetchDescriptorFromFontProgram(fontFound) : null);
        }
예제 #13
0
        private void InitializeCidFontProperties(IDictionary <String, Object> fontDesc)
        {
            fontIdentification.SetPanose((String)fontDesc.Get("Panose"));
            fontMetrics.SetItalicAngle(Convert.ToInt32((String)fontDesc.Get("ItalicAngle"), System.Globalization.CultureInfo.InvariantCulture
                                                       ));
            fontMetrics.SetCapHeight(Convert.ToInt32((String)fontDesc.Get("CapHeight"), System.Globalization.CultureInfo.InvariantCulture
                                                     ));
            fontMetrics.SetTypoAscender(Convert.ToInt32((String)fontDesc.Get("Ascent"), System.Globalization.CultureInfo.InvariantCulture
                                                        ));
            fontMetrics.SetTypoDescender(Convert.ToInt32((String)fontDesc.Get("Descent"), System.Globalization.CultureInfo.InvariantCulture
                                                         ));
            fontMetrics.SetStemV(Convert.ToInt32((String)fontDesc.Get("StemV"), System.Globalization.CultureInfo.InvariantCulture
                                                 ));
            pdfFontFlags = Convert.ToInt32((String)fontDesc.Get("Flags"), System.Globalization.CultureInfo.InvariantCulture
                                           );
            String          fontBBox = (String)fontDesc.Get("FontBBox");
            StringTokenizer tk       = new StringTokenizer(fontBBox, " []\r\n\t\f");
            int             llx      = Convert.ToInt32(tk.NextToken(), System.Globalization.CultureInfo.InvariantCulture);
            int             lly      = Convert.ToInt32(tk.NextToken(), System.Globalization.CultureInfo.InvariantCulture);
            int             urx      = Convert.ToInt32(tk.NextToken(), System.Globalization.CultureInfo.InvariantCulture);
            int             ury      = Convert.ToInt32(tk.NextToken(), System.Globalization.CultureInfo.InvariantCulture);

            fontMetrics.UpdateBbox(llx, lly, urx, ury);
            registry = (String)fontDesc.Get("Registry");
            String uniMap = GetCompatibleUniMap(registry);

            if (uniMap != null)
            {
                IntHashtable metrics = (IntHashtable)fontDesc.Get("W");
                CMapCidUni   cid2Uni = FontCache.GetCid2UniCmap(uniMap);
                avgWidth = 0;
                foreach (int cid in cid2Uni.GetCids())
                {
                    int   uni   = cid2Uni.Lookup(cid);
                    int   width = metrics.ContainsKey(cid) ? metrics.Get(cid) : DEFAULT_WIDTH;
                    Glyph glyph = new Glyph(cid, width, uni);
                    avgWidth += glyph.GetWidth();
                    codeToGlyph.Put(cid, glyph);
                    unicodeToGlyph.Put(uni, glyph);
                }
                FixSpaceIssue();
                if (codeToGlyph.Count != 0)
                {
                    avgWidth /= codeToGlyph.Count;
                }
            }
        }
예제 #14
0
        /// <exception cref="System.IO.IOException"/>
        public static FontProgram CreateFont(byte[] ttc, int ttcIndex, bool cached)
        {
            String fontKey = null;

            if (cached)
            {
                fontKey = iText.IO.Util.JavaUtil.IntegerToString(ArrayUtil.HashCode(ttc)) + iText.IO.Util.JavaUtil.IntegerToString
                              (ttcIndex);
                FontProgram fontFound = FontCache.GetFont(fontKey);
                if (fontFound != null)
                {
                    return(fontFound);
                }
            }
            FontProgram fontBuilt = new TrueTypeFont(ttc, ttcIndex);

            return(cached ? FontCache.SaveFont(fontBuilt, fontKey) : fontBuilt);
        }
예제 #15
0
        private static FontProgram CreateType1Font(String metricsPath, String binaryPath, byte[] afm, byte[] pfb,
                                                   bool cached)
        {
            FontProgram  fontProgram;
            FontCacheKey fontKey = null;

            if (cached)
            {
                fontKey     = CreateFontCacheKey(metricsPath, afm);
                fontProgram = FontCache.GetFont(fontKey);
                if (fontProgram != null)
                {
                    return(fontProgram);
                }
            }
            fontProgram = new Type1Font(metricsPath, binaryPath, afm, pfb);
            return(cached ? FontCache.SaveFont(fontProgram, fontKey) : fontProgram);
        }
        public virtual void FontBytesTtcCacheKeyTest()
        {
            byte[]       fontBytes       = "SupposedTtcFontData".GetBytes(System.Text.Encoding.UTF8);
            byte[]       otherFontBytes  = "DifferentTtcFontBytes".GetBytes(System.Text.Encoding.UTF8);
            byte[]       normalFontBytes = "NormalFontBytes".GetBytes(System.Text.Encoding.UTF8);
            FontCacheKey ttc0            = FontCacheKey.Create(fontBytes, 1);
            FontCacheKey otherTtc0       = FontCacheKey.Create(otherFontBytes, 1);
            FontCacheKey normal          = FontCacheKey.Create(normalFontBytes);

            NUnit.Framework.Assert.IsNull(FontCache.GetFont(ttc0));
            NUnit.Framework.Assert.IsNull(FontCache.GetFont(otherTtc0));
            NUnit.Framework.Assert.IsNull(FontCache.GetFont(normal));
            FontProgram otherTtc0MockFontProgram = new FontCacheNoFontAsianTest.FontProgramMock();
            FontProgram normalMockFontProgram    = new FontCacheNoFontAsianTest.FontProgramMock();

            FontCache.SaveFont(otherTtc0MockFontProgram, otherTtc0);
            FontCache.SaveFont(normalMockFontProgram, normal);
            NUnit.Framework.Assert.IsNull(FontCache.GetFont(ttc0));
            NUnit.Framework.Assert.AreEqual(otherTtc0MockFontProgram, FontCache.GetFont(otherTtc0));
            NUnit.Framework.Assert.AreEqual(normalMockFontProgram, FontCache.GetFont(normal));
        }
예제 #17
0
        // todo make comment relevant to type 1 font creation
        /// <summary>Creates a new font.</summary>
        /// <remarks>
        /// Creates a new font. This font can be one of the 14 built in types,
        /// a Type1 font referred to by an AFM or PFM file, a TrueType font (simple or collection) or a CJK font from the
        /// Adobe Asian Font Pack. TrueType fonts and CJK fonts can have an optional style modifier
        /// appended to the name. These modifiers are: Bold, Italic and BoldItalic. An
        /// example would be "STSong-Light,Bold". Note that this modifiers do not work if
        /// the font is embedded. Fonts in TrueType collections are addressed by index such as "msgothic.ttc,1".
        /// This would get the second font (indexes start at 0), in this case "MS PGothic".
        /// <p/>
        /// The fonts may or may not be cached depending on the flag <CODE>cached</CODE>.
        /// If the <CODE>byte</CODE> arrays are present the font will be
        /// read from them instead of the name. A name is still required to identify
        /// the font type.
        /// <p/>
        /// Besides the common encodings described by name, custom encodings
        /// can also be made. These encodings will only work for the single byte fonts
        /// Type1 and TrueType. The encoding string starts with a '#'
        /// followed by "simple" or "full". If "simple" there is a decimal for the first character position and then a list
        /// of hex values representing the Unicode codes that compose that encoding.<br />
        /// The "simple" encoding is recommended for TrueType fonts
        /// as the "full" encoding risks not matching the character with the right glyph
        /// if not done with care.<br />
        /// The "full" encoding is specially aimed at Type1 fonts where the glyphs have to be
        /// described by non standard names like the Tex math fonts. Each group of three elements
        /// compose a code position: the one byte code order in decimal or as 'x' (x cannot be the space), the name and the Unicode character
        /// used to access the glyph. The space must be assigned to character position 32 otherwise
        /// text justification will not work.
        /// <p/>
        /// Example for a "simple" encoding that includes the Unicode
        /// character space, A, B and ecyrillic:
        /// <PRE>
        /// "# simple 32 0020 0041 0042 0454"
        /// </PRE>
        /// <p/>
        /// Example for a "full" encoding for a Type1 Tex font:
        /// <PRE>
        /// "# full 'A' nottriangeqlleft 0041 'B' dividemultiply 0042 32 space 0020"
        /// </PRE>
        /// </remarks>
        /// <param name="name">the name of the font or its location on file</param>
        /// <param name="cached">
        /// true if the font comes from the cache or is added to
        /// the cache if new, false if the font is always created new
        /// </param>
        /// <param name="afm">the afm or pfm metrics file in a byte array</param>
        /// <param name="pfb">the pfb in a byte array</param>
        /// <returns>
        /// returns a new font. This font may come from the cache but only if cached
        /// is true, otherwise it will always be created new
        /// </returns>
        /// <exception cref="System.IO.IOException"/>
        public static FontProgram CreateType1Font(String name, byte[] afm, byte[] pfb, bool cached)
        {
            FontProgram fontProgram;
            String      fontKey = null;

            if (cached)
            {
                if (name != null)
                {
                    fontKey = name;
                }
                else
                {
                    fontKey = iText.IO.Util.JavaUtil.IntegerToString(ArrayUtil.HashCode(afm));
                }
                fontProgram = FontCache.GetFont(fontKey);
                if (fontProgram != null)
                {
                    return(fontProgram);
                }
            }
            fontProgram = new Type1Font(name, null, afm, pfb);
            return(cached ? FontCache.SaveFont(fontProgram, fontKey) : fontProgram);
        }
예제 #18
0
        /// <summary>Creates a new font.</summary>
        /// <remarks>
        /// Creates a new font. This font can be one of the 14 built in types,
        /// a Type1 font referred to by an AFM or PFM file, a TrueType font (simple or collection) or a CJK font from the
        /// Adobe Asian Font Pack. TrueType fonts and CJK fonts can have an optional style modifier
        /// appended to the name. These modifiers are: Bold, Italic and BoldItalic. An
        /// example would be "STSong-Light,Bold". Note that this modifiers do not work if
        /// the font is embedded. Fonts in TrueType collections are addressed by index such as "msgothic.ttc,1".
        /// This would get the second font (indexes start at 0), in this case "MS PGothic".
        /// <p/>
        /// The fonts may or may not be cached depending on the flag <CODE>cached</CODE>.
        /// If the <CODE>byte</CODE> arrays are present the font will be
        /// read from them instead of the name. A name is still required to identify
        /// the font type.
        /// <p/>
        /// Besides the common encodings described by name, custom encodings
        /// can also be made. These encodings will only work for the single byte fonts
        /// Type1 and TrueType. The encoding string starts with a '#'
        /// followed by "simple" or "full". If "simple" there is a decimal for the first character position and then a list
        /// of hex values representing the Unicode codes that compose that encoding.<br />
        /// The "simple" encoding is recommended for TrueType fonts
        /// as the "full" encoding risks not matching the character with the right glyph
        /// if not done with care.<br />
        /// The "full" encoding is specially aimed at Type1 fonts where the glyphs have to be
        /// described by non standard names like the Tex math fonts. Each group of three elements
        /// compose a code position: the one byte code order in decimal or as 'x' (x cannot be the space), the name and the Unicode character
        /// used to access the glyph. The space must be assigned to character position 32 otherwise
        /// text justification will not work.
        /// <p/>
        /// Example for a "simple" encoding that includes the Unicode
        /// character space, A, B and ecyrillic:
        /// <PRE>
        /// "# simple 32 0020 0041 0042 0454"
        /// </PRE>
        /// <p/>
        /// Example for a "full" encoding for a Type1 Tex font:
        /// <PRE>
        /// "# full 'A' nottriangeqlleft 0041 'B' dividemultiply 0042 32 space 0020"
        /// </PRE>
        /// </remarks>
        /// <param name="name">the name of the font or its location on file</param>
        /// <param name="font">the true type font or the afm in a byte array</param>
        /// <param name="cached">
        /// true if the font comes from the cache or is added to
        /// the cache if new, false if the font is always created new
        /// </param>
        /// <returns>
        /// returns a new font. This font may come from the cache but only if cached
        /// is true, otherwise it will always be created new
        /// </returns>
        /// <exception cref="System.IO.IOException"/>
        public static FontProgram CreateFont(String name, byte[] font, bool cached)
        {
            String baseName = FontProgram.GetBaseName(name);
            //yes, we trying to find built-in standard font with original name, not baseName.
            bool        isBuiltinFonts14 = FontConstants.BUILTIN_FONTS_14.Contains(name);
            bool        isCidFont        = !isBuiltinFonts14 && FontCache.IsPredefinedCidFont(baseName);
            FontProgram fontFound;

            if (cached && name != null)
            {
                fontFound = FontCache.GetFont(name);
                if (fontFound != null)
                {
                    return(fontFound);
                }
            }
            if (name == null)
            {
                if (font != null)
                {
                    try {
                        return(new TrueTypeFont(font));
                    }
                    catch (Exception) {
                    }
                    try {
                        return(new Type1Font(null, null, font, null));
                    }
                    catch (Exception) {
                    }
                }
                throw new iText.IO.IOException(iText.IO.IOException.FontIsNotRecognized);
            }
            FontProgram fontBuilt;

            if (isBuiltinFonts14 || name.ToLower(System.Globalization.CultureInfo.InvariantCulture).EndsWith(".afm") ||
                name.ToLower(System.Globalization.CultureInfo.InvariantCulture).EndsWith(".pfm"))
            {
                fontBuilt = new Type1Font(name, null, font, null);
            }
            else
            {
                if (baseName.ToLower(System.Globalization.CultureInfo.InvariantCulture).EndsWith(".ttf") || baseName.ToLower
                        (System.Globalization.CultureInfo.InvariantCulture).EndsWith(".otf") || baseName.ToLower(System.Globalization.CultureInfo.InvariantCulture
                                                                                                                 ).IndexOf(".ttc,") > 0)
                {
                    if (font != null)
                    {
                        fontBuilt = new TrueTypeFont(font);
                    }
                    else
                    {
                        fontBuilt = new TrueTypeFont(name);
                    }
                }
                else
                {
                    if (isCidFont)
                    {
                        fontBuilt = new CidFont(name, FontCache.GetCompatibleCmaps(baseName));
                    }
                    else
                    {
                        throw new iText.IO.IOException(iText.IO.IOException.Font1IsNotRecognized).SetMessageParams(name);
                    }
                }
            }
            return(cached ? FontCache.SaveFont(fontBuilt, name) : fontBuilt);
        }
예제 #19
0
        public static ICollection <String> GetCompatibleCmaps(String fontName)
        {
            String registry = (String)FontCache.GetAllFonts().Get(fontName).Get(REGISTRY_PROP);

            return(registryNames.Get(registry));
        }
예제 #20
0
        private static FontProgram CreateFont(String name, byte[] fontProgram, bool cached)
        {
            String baseName = FontProgram.TrimFontStyle(name);
            //yes, we trying to find built-in standard font with original name, not baseName.
            bool         isBuiltinFonts14 = StandardFonts.IsStandardFont(name);
            bool         isCidFont        = !isBuiltinFonts14 && FontCache.IsPredefinedCidFont(baseName);
            FontProgram  fontFound;
            FontCacheKey fontKey = null;

            if (cached)
            {
                fontKey   = CreateFontCacheKey(name, fontProgram);
                fontFound = FontCache.GetFont(fontKey);
                if (fontFound != null)
                {
                    return(fontFound);
                }
            }
            FontProgram fontBuilt = null;

            if (name == null)
            {
                if (fontProgram != null)
                {
                    try {
                        if (WoffConverter.IsWoffFont(fontProgram))
                        {
                            fontProgram = WoffConverter.Convert(fontProgram);
                        }
                        else
                        {
                            if (Woff2Converter.IsWoff2Font(fontProgram))
                            {
                                fontProgram = Woff2Converter.Convert(fontProgram);
                            }
                        }
                        fontBuilt = new TrueTypeFont(fontProgram);
                    }
                    catch (Exception) {
                    }
                    if (fontBuilt == null)
                    {
                        try {
                            fontBuilt = new Type1Font(null, null, fontProgram, null);
                        }
                        catch (Exception) {
                        }
                    }
                }
            }
            else
            {
                String fontFileExtension   = null;
                int    extensionBeginIndex = baseName.LastIndexOf('.');
                if (extensionBeginIndex > 0)
                {
                    fontFileExtension = baseName.Substring(extensionBeginIndex).ToLowerInvariant();
                }
                if (isBuiltinFonts14 || ".afm".Equals(fontFileExtension) || ".pfm".Equals(fontFileExtension))
                {
                    fontBuilt = new Type1Font(name, null, null, null);
                }
                else
                {
                    if (isCidFont)
                    {
                        fontBuilt = new CidFont(name, FontCache.GetCompatibleCmaps(baseName));
                    }
                    else
                    {
                        if (".ttf".Equals(fontFileExtension) || ".otf".Equals(fontFileExtension))
                        {
                            if (fontProgram != null)
                            {
                                fontBuilt = new TrueTypeFont(fontProgram);
                            }
                            else
                            {
                                fontBuilt = new TrueTypeFont(name);
                            }
                        }
                        else
                        {
                            if (".woff".Equals(fontFileExtension) || ".woff2".Equals(fontFileExtension))
                            {
                                if (fontProgram == null)
                                {
                                    fontProgram = ReadFontBytesFromPath(baseName);
                                }
                                if (".woff".Equals(fontFileExtension))
                                {
                                    try {
                                        fontProgram = WoffConverter.Convert(fontProgram);
                                    }
                                    catch (ArgumentException woffException) {
                                        throw new iText.IO.IOException(iText.IO.IOException.InvalidWoffFile, woffException);
                                    }
                                }
                                else
                                {
                                    // ".woff2".equals(fontFileExtension)
                                    try {
                                        fontProgram = Woff2Converter.Convert(fontProgram);
                                    }
                                    catch (FontCompressionException woff2Exception) {
                                        throw new iText.IO.IOException(iText.IO.IOException.InvalidWoff2File, woff2Exception);
                                    }
                                }
                                fontBuilt = new TrueTypeFont(fontProgram);
                            }
                            else
                            {
                                int ttcSplit = baseName.ToLowerInvariant().IndexOf(".ttc,", StringComparison.Ordinal);
                                if (ttcSplit > 0)
                                {
                                    try {
                                        // count(.ttc) = 4
                                        String ttcName = baseName.JSubstring(0, ttcSplit + 4);
                                        // count(.ttc,) = 5)
                                        int ttcIndex = Convert.ToInt32(baseName.Substring(ttcSplit + 5));
                                        fontBuilt = new TrueTypeFont(ttcName, ttcIndex);
                                    }
                                    catch (FormatException nfe) {
                                        throw new iText.IO.IOException(nfe.Message, nfe);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (fontBuilt == null)
            {
                if (name != null)
                {
                    throw new iText.IO.IOException(iText.IO.IOException.TypeOfFont1IsNotRecognized).SetMessageParams(name);
                }
                else
                {
                    throw new iText.IO.IOException(iText.IO.IOException.TypeOfFontIsNotRecognized);
                }
            }
            return(cached ? FontCache.SaveFont(fontBuilt, fontKey) : fontBuilt);
        }
 public virtual void Before()
 {
     FontCache.ClearSavedFonts();
 }
        public static FontProgramDescriptor FetchDescriptor(String fontName)
        {
            if (fontName == null || fontName.Length == 0)
            {
                return(null);
            }
            String baseName = FontProgram.TrimFontStyle(fontName);
            //yes, we trying to find built-in standard font with original name, not baseName.
            bool isBuiltinFonts14 = StandardFonts.IsStandardFont(fontName);
            bool isCidFont        = !isBuiltinFonts14 && FontCache.IsPredefinedCidFont(baseName);
            FontProgramDescriptor fontDescriptor = null;

            if (FETCH_CACHED_FIRST)
            {
                fontDescriptor = FetchCachedDescriptor(fontName, null);
                if (fontDescriptor != null)
                {
                    return(fontDescriptor);
                }
            }
            try {
                String fontNameLowerCase = baseName.ToLowerInvariant();
                if (isBuiltinFonts14 || fontNameLowerCase.EndsWith(".afm") || fontNameLowerCase.EndsWith(".pfm"))
                {
                    fontDescriptor = FetchType1FontDescriptor(fontName, null);
                }
                else
                {
                    if (isCidFont)
                    {
                        fontDescriptor = FetchCidFontDescriptor(fontName);
                    }
                    else
                    {
                        if (fontNameLowerCase.EndsWith(".ttf") || fontNameLowerCase.EndsWith(".otf"))
                        {
                            fontDescriptor = FetchTrueTypeFontDescriptor(fontName);
                        }
                        else
                        {
                            if (fontNameLowerCase.EndsWith(".woff") || fontNameLowerCase.EndsWith(".woff2"))
                            {
                                byte[] fontProgram;
                                if (fontNameLowerCase.EndsWith(".woff"))
                                {
                                    fontProgram = WoffConverter.Convert(FontProgramFactory.ReadFontBytesFromPath(baseName));
                                }
                                else
                                {
                                    fontProgram = Woff2Converter.Convert(FontProgramFactory.ReadFontBytesFromPath(baseName));
                                }
                                fontDescriptor = FetchTrueTypeFontDescriptor(fontProgram);
                            }
                            else
                            {
                                fontDescriptor = FetchTTCDescriptor(baseName);
                            }
                        }
                    }
                }
            }
            catch (Exception) {
                fontDescriptor = null;
            }
            return(fontDescriptor);
        }
 public virtual void GetRegistryNamesNoFontAsian()
 {
     // Without font-asian module in the class path
     // registry names collection is expected to be empty.
     NUnit.Framework.Assert.IsTrue(FontCache.GetRegistryNames().IsEmpty());
 }
 public virtual void GetCompatibleCmapsNoFontAsian()
 {
     // Without font-asian module in the class path
     // any value passed into a method is expected to return null.
     NUnit.Framework.Assert.IsNull(FontCache.GetCompatibleCmaps("HeiseiKakuGo-W5"));
 }
 public virtual void IsPredefinedCidFontNoFontAsian()
 {
     // Without font-asian module in the class path
     // any value passed into a method is expected to return false.
     NUnit.Framework.Assert.IsFalse(FontCache.IsPredefinedCidFont("78-RKSJ-V"));
 }
 public virtual void GetCompatibleCidFontNoFontAsian()
 {
     // Without font-asian module in the class path
     // any value passed into a method is expected to return null.
     NUnit.Framework.Assert.IsNull(FontCache.GetCompatibleCidFont("78-RKSJ-V"));
 }