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);
        }
        private static FontProgramDescriptor FetchCidFontDescriptor(String fontName)
        {
            CidFont font = new CidFont(fontName, null);

            return(new FontProgramDescriptor(font.GetFontNames(), font.GetFontMetrics()));
        }
Exemple #3
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);
        }