Esempio n. 1
0
 /// <summary>
 /// Create a
 /// <see cref="FontFace"/>
 /// instance from a list of
 /// CSS font attributes ("font-family" or "src").
 /// </summary>
 /// <param name="properties">the font properties</param>
 /// <returns>
 /// the
 /// <see cref="FontFace"/>
 /// instance
 /// </returns>
 public static iText.Svg.Processors.Impl.Font.FontFace Create(IList<CssDeclaration> properties) {
     String fontFamily = null;
     String srcs = null;
     foreach (CssDeclaration descriptor in properties) {
         if ("font-family".Equals(descriptor.GetProperty())) {
             fontFamily = FontFamilySplitter.RemoveQuotes(descriptor.GetExpression());
         }
         else {
             if ("src".Equals(descriptor.GetProperty())) {
                 srcs = descriptor.GetExpression();
             }
         }
     }
     if (fontFamily == null || srcs == null) {
         // 'font-family' and 'src' is required according to spec:
         // https://www.w3.org/TR/2013/CR-css-fonts-3-20131003/#descdef-font-family\
         // https://www.w3.org/TR/2013/CR-css-fonts-3-20131003/#descdef-src
         return null;
     }
     IList<FontFace.FontFaceSrc> sources = new List<FontFace.FontFaceSrc>();
     // ttc collection are supported via url(Arial.ttc#1), url(Arial.ttc#2), etc.
     foreach (String src in SplitSourcesSequence(srcs)) {
         //local|url("ideal-sans-serif.woff")( format("woff"))?
         FontFace.FontFaceSrc source = FontFace.FontFaceSrc.Create(src.Trim());
         if (source != null) {
             sources.Add(source);
         }
     }
     if (sources.Count > 0) {
         return new iText.Svg.Processors.Impl.Font.FontFace(fontFamily, sources);
     }
     else {
         return null;
     }
 }
Esempio n. 2
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>
 /// <returns>true, if successful</returns>
 private bool CreateFont(String fontFamily, FontFace.FontFaceSrc src)
 {
     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);
                     return(true);
                 }
             }
             catch (Exception) {
             }
             return(false);
         }
     }
 }
Esempio n. 3
0
        public virtual void SrcPropertyTest()
        {
            String        fontSrc    = "web-fonts/droid-serif-invalid.";
            CssStyleSheet styleSheet = CssStyleSheetParser.Parse(new FileStream(sourceFolder + "srcs.css", FileMode.Open
                                                                                , FileAccess.Read));
            CssFontFaceRule fontFaceRule = (CssFontFaceRule)styleSheet.GetStatements()[0];
            CssDeclaration  src          = fontFaceRule.GetProperties()[0];

            NUnit.Framework.Assert.AreEqual("src", src.GetProperty(), "src expected");
            String[] sources = iText.IO.Util.StringUtil.Split(src.GetExpression(), ",");
            NUnit.Framework.Assert.AreEqual(27, sources.Length, "27 sources expected");
            for (int i = 0; i < sources.Length; i++)
            {
                Match m = iText.IO.Util.StringUtil.Match(FontFace.FontFaceSrc.UrlPattern, sources[i]);
                NUnit.Framework.Assert.IsTrue(m.Success, "Expression doesn't match pattern: " + sources[i]);
                String format  = iText.IO.Util.StringUtil.Group(m, FontFace.FontFaceSrc.FormatGroup);
                String source2 = MessageFormatUtil.Format("{0}({1}){2}", iText.IO.Util.StringUtil.Group(m, FontFace.FontFaceSrc
                                                                                                        .TypeGroup), iText.IO.Util.StringUtil.Group(m, FontFace.FontFaceSrc.UrlGroup), format != null ? MessageFormatUtil
                                                          .Format(" format({0})", format) : "");
                String url = FontFace.FontFaceSrc.Unquote(iText.IO.Util.StringUtil.Group(m, FontFace.FontFaceSrc.UrlGroup)
                                                          );
                NUnit.Framework.Assert.IsTrue(url.StartsWith(fontSrc), "Invalid url: " + url);
                NUnit.Framework.Assert.IsTrue(format == null || FontFace.FontFaceSrc.ParseFormat(format) != FontFace.FontFormat
                                              .None, "Invalid format: " + format);
                NUnit.Framework.Assert.AreEqual(sources[i], source2, "Group check fails: ");
                FontFace.FontFaceSrc fontFaceSrc = FontFace.FontFaceSrc.Create(sources[i]);
                NUnit.Framework.Assert.IsTrue(fontFaceSrc.src.StartsWith(fontSrc), "Invalid url: " + fontSrc);
                String type = "url";
                if (fontFaceSrc.isLocal)
                {
                    type = "local";
                }
                NUnit.Framework.Assert.IsTrue(sources[i].StartsWith(type), "Type '" + type + "' expected: " + sources[i]);
                switch (fontFaceSrc.format)
                {
                case FontFace.FontFormat.OpenType: {
                    NUnit.Framework.Assert.IsTrue(sources[i].Contains("opentype"), "Format " + fontFaceSrc.format + " expected: "
                                                  + sources[i]);
                    break;
                }

                case FontFace.FontFormat.TrueType: {
                    NUnit.Framework.Assert.IsTrue(sources[i].Contains("truetype"), "Format " + fontFaceSrc.format + " expected: "
                                                  + sources[i]);
                    break;
                }

                case FontFace.FontFormat.SVG: {
                    NUnit.Framework.Assert.IsTrue(sources[i].Contains("svg"), "Format " + fontFaceSrc.format + " expected: " +
                                                  sources[i]);
                    break;
                }

                case FontFace.FontFormat.None: {
                    NUnit.Framework.Assert.IsFalse(sources[i].Contains("format("), "Format " + fontFaceSrc.format + " expected: "
                                                   + sources[i]);
                    break;
                }
                }
            }
        }