示例#1
0
        internal static FontProgram SaveFont(FontProgram font, FontCacheKey key)
        {
            FontProgram fontFound = fontCache.Get(key);

            if (fontFound != null)
            {
                return(fontFound);
            }
            fontCache.Put(key, font);
            return(font);
        }
        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));
        }
        private static FontCacheKey CreateFontCacheKey(String name, byte[] fontProgram)
        {
            FontCacheKey key;

            if (name != null)
            {
                key = FontCacheKey.Create(name);
            }
            else
            {
                key = FontCacheKey.Create(fontProgram);
            }
            return(key);
        }
        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);
        }
        /// <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);
        }
        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));
        }
示例#8
0
 public static FontProgram SaveFont(FontProgram font, String fontName)
 {
     return(SaveFont(font, FontCacheKey.Create(fontName)));
 }
示例#9
0
 internal static FontProgram GetFont(FontCacheKey key)
 {
     return(fontCache.Get(key));
 }
示例#10
0
 public static FontProgram GetFont(String fontName)
 {
     return(fontCache.Get(FontCacheKey.Create(fontName)));
 }
        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);
        }
示例#12
0
 internal FontCacheTtcKey(byte[] fontBytes, int ttcIndex)
 {
     this.ttcKey   = new FontCacheKey.FontCacheBytesKey(fontBytes);
     this.ttcIndex = ttcIndex;
 }
示例#13
0
 internal FontCacheTtcKey(String fontName, int ttcIndex)
 {
     this.ttcKey   = new FontCacheKey.FontCacheStringKey(fontName);
     this.ttcIndex = ttcIndex;
 }