public static FontProgramDescriptor FetchDescriptor(byte[] fontProgram)
        {
            if (fontProgram == null || fontProgram.Length == 0)
            {
                return(null);
            }
            FontProgramDescriptor fontDescriptor = null;

            if (FETCH_CACHED_FIRST)
            {
                fontDescriptor = FetchCachedDescriptor(null, fontProgram);
                if (fontDescriptor != null)
                {
                    return(fontDescriptor);
                }
            }
            try {
                fontDescriptor = FetchTrueTypeFontDescriptor(fontProgram);
            }
            catch (Exception) {
            }
            if (fontDescriptor == null)
            {
                try {
                    fontDescriptor = FetchType1FontDescriptor(null, fontProgram);
                }
                catch (Exception) {
                }
            }
            return(fontDescriptor);
        }
        /// <exception cref="System.IO.IOException"/>
        private static FontProgramDescriptor FetchTTCDescriptor(String baseName)
        {
            int ttcSplit = baseName.ToLowerInvariant().IndexOf(".ttc,", StringComparison.Ordinal);

            if (ttcSplit > 0)
            {
                String ttcName;
                int    ttcIndex;
                try {
                    ttcName = baseName.JSubstring(0, ttcSplit + 4);
                    // count(.ttc) = 4
                    ttcIndex = Convert.ToInt32(baseName.Substring(ttcSplit + 5));
                }
                catch (FormatException nfe) {
                    // count(.ttc,) = 5)
                    throw new iText.IO.IOException(nfe.Message, nfe);
                }
                OpenTypeParser        parser     = new OpenTypeParser(ttcName, ttcIndex);
                FontProgramDescriptor descriptor = FetchOpenTypeFontDescriptor(parser);
                parser.Close();
                return(descriptor);
            }
            else
            {
                return(null);
            }
        }
        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);
        }
Example #4
0
 /// <summary>Register a font file and use an alias for the font contained in it.</summary>
 /// <param name="path">the path to a font file</param>
 /// <param name="alias">the alias you want to use for the font</param>
 internal virtual void RegisterFont(String path, String alias)
 {
     try {
         if (path.ToLowerInvariant().EndsWith(".ttf") || path.ToLowerInvariant().EndsWith(".otf") || path.ToLowerInvariant
                 ().IndexOf(".ttc,", StringComparison.Ordinal) > 0)
         {
             FontProgramDescriptor descriptor = FontProgramDescriptorFactory.FetchDescriptor(path);
             fontNames.Put(descriptor.GetFontNameLowerCase(), path);
             if (alias != null)
             {
                 String lcAlias = alias.ToLowerInvariant();
                 fontNames.Put(lcAlias, path);
                 if (lcAlias.EndsWith("regular"))
                 {
                     //do this job to give higher priority to regular fonts in comparison with light, narrow, etc
                     SaveCopyOfRegularFont(lcAlias, path);
                 }
             }
             // register all the font names with all the locales
             foreach (String name in descriptor.GetFullNameAllLangs())
             {
                 fontNames.Put(name, path);
                 if (name.EndsWith("regular"))
                 {
                     //do this job to give higher priority to regular fonts in comparison with light, narrow, etc
                     SaveCopyOfRegularFont(name, path);
                 }
             }
             if (descriptor.GetFamilyNameEnglishOpenType() != null)
             {
                 foreach (String fullName in descriptor.GetFullNamesEnglishOpenType())
                 {
                     RegisterFontFamily(descriptor.GetFamilyNameEnglishOpenType(), fullName, null);
                 }
             }
         }
         else
         {
             if (path.ToLowerInvariant().EndsWith(".ttc"))
             {
                 TrueTypeCollection ttc = new TrueTypeCollection(path);
                 for (int i = 0; i < ttc.GetTTCSize(); i++)
                 {
                     String fullPath = path + "," + i;
                     if (alias != null)
                     {
                         RegisterFont(fullPath, alias + "," + i);
                     }
                     else
                     {
                         RegisterFont(fullPath);
                     }
                 }
             }
             else
             {
                 if (path.ToLowerInvariant().EndsWith(".afm") || path.ToLowerInvariant().EndsWith(".pfm"))
                 {
                     FontProgramDescriptor descriptor = FontProgramDescriptorFactory.FetchDescriptor(path);
                     RegisterFontFamily(descriptor.GetFamilyNameLowerCase(), descriptor.GetFullNameLowerCase(), null);
                     fontNames.Put(descriptor.GetFontNameLowerCase(), path);
                     fontNames.Put(descriptor.GetFullNameLowerCase(), path);
                 }
             }
         }
         LOGGER.Trace(MessageFormatUtil.Format("Registered {0}", path));
     }
     catch (System.IO.IOException e) {
         throw new iText.IO.IOException(e);
     }
 }