Example #1
0
 /// <summary>Creates a font and adds it to the context.</summary>
 /// <param name="fontFamily">the font family</param>
 /// <param name="src">the source of the font</param>
 /// <param name="src">the unicode range</param>
 /// <returns>true, if successful</returns>
 private bool CreateFont(String fontFamily, FontFace.FontFaceSrc src, Range uniRange) {
     if (!SupportedFontFormat(src.format)) {
         return false;
     }
     else {
         if (src.isLocal) {
             // to method with lazy initialization
             ICollection<FontInfo> fonts = context.GetFontProvider().GetFontSet().Get(src.src);
             if (fonts.Count > 0) {
                 foreach (FontInfo fi in fonts) {
                     context.AddTemporaryFont(fi, fontFamily);
                 }
                 return true;
             }
             else {
                 return false;
             }
         }
         else {
             try {
                 // Cache at resource resolver level only, at font level we will create font in any case.
                 // The instance of fontProgram will be collected by GC if the is no need in it.
                 byte[] bytes = context.GetResourceResolver().RetrieveBytesFromResource(src.src);
                 if (bytes != null) {
                     FontProgram fp = FontProgramFactory.CreateFont(bytes, false);
                     context.AddTemporaryFont(fp, PdfEncodings.IDENTITY_H, fontFamily, uniRange);
                     return true;
                 }
             }
             catch (Exception) {
             }
             return false;
         }
     }
 }
Example #2
0
 private Range ResolveUnicodeRange(IList<CssDeclaration> declarations) {
     Range range = null;
     foreach (CssDeclaration descriptor in declarations) {
         if ("unicode-range".Equals(descriptor.GetProperty())) {
             range = CssUtils.ParseUnicodeRange(descriptor.GetExpression());
         }
     }
     return range;
 }
 private void AddAllAvailableFonts(Range rangeToLoad)
 {
     AddShippedFreeFonts(rangeToLoad);
     foreach (byte[] fontData in calligraphyFontsTempList)
     {
         AddFont(fontData, null);
     }
     calligraphyFontsTempList = null;
 }
 /// <summary>Adds the shipped free fonts.</summary>
 private void AddShippedFreeFonts(Range rangeToLoad)
 {
     foreach (String fontName in SHIPPED_FONT_NAMES)
     {
         try {
             using (Stream stream = ResourceUtil.GetResourceStream(SHIPPED_FONT_RESOURCE_PATH + fontName)) {
                 byte[] fontProgramBytes = StreamUtil.InputStreamToArray(stream);
                 AddFont(fontProgramBytes, null, rangeToLoad);
             }
         }
         catch (Exception) {
             LOGGER.Error(iText.Html2pdf.LogMessageConstant.ERROR_LOADING_FONT);
         }
     }
 }
Example #5
0
 /// <summary>Add temporary font from @font-face.</summary>
 /// <param name="fontProgram">the font program</param>
 /// <param name="encoding">the encoding</param>
 /// <param name="alias">the alias</param>
 /// <param name="unicodeRange">the unicode range</param>
 public virtual void AddTemporaryFont(FontProgram fontProgram, String encoding, String alias, Range unicodeRange
                                      )
 {
     if (tempFonts == null)
     {
         tempFonts = new FontSet();
     }
     tempFonts.AddFont(fontProgram, encoding, alias, unicodeRange);
 }