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);
        }
Exemplo n.º 2
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);
        }