Beispiel #1
0
        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);
        }
 /// <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)
         {
             FontProgram fontProgram = FontProgramFactory.CreateFont(path);
             Object[]    allNames    = new Object[] { fontProgram.GetFontNames().GetFontName(), fontProgram.GetFontNames().GetFamilyName
                                                          (), fontProgram.GetFontNames().GetFullName() };
             fontNames.Put(((String)allNames[0]).ToLowerInvariant(), 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
             String[][] names = (String[][])allNames[2];
             //full name
             foreach (String[] name in names)
             {
                 String lcName = name[3].ToLowerInvariant();
                 fontNames.Put(lcName, path);
                 if (lcName.EndsWith("regular"))
                 {
                     //do this job to give higher priority to regular fonts in comparison with light, narrow, etc
                     SaveCopyOfRegularFont(lcName, path);
                 }
             }
             String fullName;
             String familyName = null;
             names = (String[][])allNames[1];
             //family name
             for (int k = 0; k < TTFamilyOrder.Length; k += 3)
             {
                 foreach (String[] name in names)
                 {
                     if (TTFamilyOrder[k].Equals(name[0]) && TTFamilyOrder[k + 1].Equals(name[1]) && TTFamilyOrder[k + 2].Equals
                             (name[2]))
                     {
                         familyName = name[3].ToLowerInvariant();
                         k          = TTFamilyOrder.Length;
                         break;
                     }
                 }
             }
             if (familyName != null)
             {
                 String lastName = "";
                 names = (String[][])allNames[2];
                 //full name
                 foreach (String[] name in names)
                 {
                     for (int k = 0; k < TTFamilyOrder.Length; k += 3)
                     {
                         if (TTFamilyOrder[k].Equals(name[0]) && TTFamilyOrder[k + 1].Equals(name[1]) && TTFamilyOrder[k + 2].Equals
                                 (name[2]))
                         {
                             fullName = name[3];
                             if (fullName.Equals(lastName))
                             {
                                 continue;
                             }
                             lastName = fullName;
                             RegisterFontFamily(familyName, fullName, null);
                             break;
                         }
                     }
                 }
             }
         }
         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"))
                 {
                     FontProgram fontProgram = FontProgramFactory.CreateFont(path);
                     String      fullName    = fontProgram.GetFontNames().GetFullName()[0][3].ToLowerInvariant();
                     String      familyName  = fontProgram.GetFontNames().GetFamilyName()[0][3].ToLowerInvariant();
                     String      psName      = fontProgram.GetFontNames().GetFontName().ToLowerInvariant();
                     RegisterFontFamily(familyName, fullName, null);
                     fontNames.Put(psName, path);
                     fontNames.Put(fullName, path);
                 }
             }
         }
         LOGGER.Trace(MessageFormatUtil.Format("Registered {0}", path));
     }
     catch (System.IO.IOException e) {
         throw new iText.IO.IOException(e);
     }
 }
 /// <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>
 public virtual void RegisterFont(String path, String alias)
 {
     try {
         if (path.ToLower(System.Globalization.CultureInfo.InvariantCulture).EndsWith(".ttf") || path.ToLower(System.Globalization.CultureInfo.InvariantCulture
                                                                                                              ).EndsWith(".otf") || path.ToLower(System.Globalization.CultureInfo.InvariantCulture).IndexOf(".ttc,")
             > 0)
         {
             FontProgram fontProgram = FontProgramFactory.CreateFont(path);
             Object[]    allNames    = new Object[] { fontProgram.GetFontNames().GetFontName(), fontProgram.GetFontNames().GetFamilyName
                                                          (), fontProgram.GetFontNames().GetFullName() };
             fontNames[((String)allNames[0]).ToLower(System.Globalization.CultureInfo.InvariantCulture)] = path;
             if (alias != null)
             {
                 String lcAlias = alias.ToLower(System.Globalization.CultureInfo.InvariantCulture);
                 fontNames[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
             String[][] names = (String[][])allNames[2];
             //full name
             foreach (String[] name in names)
             {
                 String lcName = name[3].ToLower(System.Globalization.CultureInfo.InvariantCulture);
                 fontNames[lcName] = path;
                 if (lcName.EndsWith("regular"))
                 {
                     //do this job to give higher priority to regular fonts in comparison with light, narrow, etc
                     SaveCopyOfRegularFont(lcName, path);
                 }
             }
             String fullName;
             String familyName = null;
             names = (String[][])allNames[1];
             //family name
             for (int k = 0; k < TTFamilyOrder.Length; k += 3)
             {
                 foreach (String[] name_1 in names)
                 {
                     if (TTFamilyOrder[k].Equals(name_1[0]) && TTFamilyOrder[k + 1].Equals(name_1[1]) && TTFamilyOrder[k + 2].Equals
                             (name_1[2]))
                     {
                         familyName = name_1[3].ToLower(System.Globalization.CultureInfo.InvariantCulture);
                         k          = TTFamilyOrder.Length;
                         break;
                     }
                 }
             }
             if (familyName != null)
             {
                 String lastName = "";
                 names = (String[][])allNames[2];
                 //full name
                 foreach (String[] name_1 in names)
                 {
                     for (int k_1 = 0; k_1 < TTFamilyOrder.Length; k_1 += 3)
                     {
                         if (TTFamilyOrder[k_1].Equals(name_1[0]) && TTFamilyOrder[k_1 + 1].Equals(name_1[1]) && TTFamilyOrder[k_1
                                                                                                                               + 2].Equals(name_1[2]))
                         {
                             fullName = name_1[3];
                             if (fullName.Equals(lastName))
                             {
                                 continue;
                             }
                             lastName = fullName;
                             RegisterFontFamily(familyName, fullName, null);
                             break;
                         }
                     }
                 }
             }
         }
         else
         {
             if (path.ToLower(System.Globalization.CultureInfo.InvariantCulture).EndsWith(".ttc"))
             {
                 if (alias != null)
                 {
                     LOGGER.Error("You can't define an alias for a true type collection.");
                 }
                 TrueTypeCollection ttc = new TrueTypeCollection(path, PdfEncodings.WINANSI);
                 for (int i = 0; i < ttc.GetTTCSize(); i++)
                 {
                     RegisterFont(path + "," + i);
                 }
             }
             else
             {
                 if (path.ToLower(System.Globalization.CultureInfo.InvariantCulture).EndsWith(".afm") || path.ToLower(System.Globalization.CultureInfo.InvariantCulture
                                                                                                                      ).EndsWith(".pfm"))
                 {
                     FontProgram fontProgram = FontProgramFactory.CreateFont(path, false);
                     String      fullName    = fontProgram.GetFontNames().GetFullName()[0][3].ToLower(System.Globalization.CultureInfo.InvariantCulture
                                                                                                      );
                     String familyName = fontProgram.GetFontNames().GetFamilyName()[0][3].ToLower(System.Globalization.CultureInfo.InvariantCulture
                                                                                                  );
                     String psName = fontProgram.GetFontNames().GetFontName().ToLower(System.Globalization.CultureInfo.InvariantCulture
                                                                                      );
                     RegisterFontFamily(familyName, fullName, null);
                     fontNames[psName]   = path;
                     fontNames[fullName] = path;
                 }
             }
         }
         LOGGER.Trace(String.Format("Registered {0}", path));
     }
     catch (System.IO.IOException e) {
         throw new iText.IO.IOException(e);
     }
 }