コード例 #1
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.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"))
             {
                 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.ToLower(System.Globalization.CultureInfo.InvariantCulture).EndsWith(".afm") || path.ToLower(System.Globalization.CultureInfo.InvariantCulture
                                                                                                                      ).EndsWith(".pfm"))
                 {
                     FontProgram fontProgram = FontProgramFactory.CreateFont(path);
                     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);
     }
 }
コード例 #2
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);
     }
 }