public virtual void ParseBase64SrcTest() { CssStyleSheet styleSheet = CssStyleSheetParser.Parse(new FileStream(sourceFolder + "srcs2.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 = CssFontFace.SplitSourcesSequence(src.GetExpression()); NUnit.Framework.Assert.AreEqual(8, sources.Length, "8 sources expected"); for (int i = 0; i < 6; i++) { Match m = iText.IO.Util.StringUtil.Match(CssFontFace.CssFontFaceSrc.UrlPattern, sources[i]); NUnit.Framework.Assert.IsTrue(m.Success, "Expression doesn't match pattern: " + sources[i]); } for (int i = 6; i < sources.Length; i++) { Match m = iText.IO.Util.StringUtil.Match(CssFontFace.CssFontFaceSrc.UrlPattern, sources[i]); NUnit.Framework.Assert.IsFalse(m.Success, "Expression matches pattern (though it shouldn't!): " + sources[ i]); } }
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(CssFontFace.CssFontFaceSrc.UrlPattern, sources[i]); NUnit.Framework.Assert.IsTrue(m.Success, "Expression doesn't match pattern: " + sources[i]); String format = iText.IO.Util.StringUtil.Group(m, CssFontFace.CssFontFaceSrc.FormatGroup); String source2 = MessageFormatUtil.Format("{0}({1}){2}", iText.IO.Util.StringUtil.Group(m, CssFontFace.CssFontFaceSrc .TypeGroup), iText.IO.Util.StringUtil.Group(m, CssFontFace.CssFontFaceSrc.UrlGroup), format != null ? MessageFormatUtil.Format(" format({0})", format) : ""); String url = CssFontFace.CssFontFaceSrc.Unquote(iText.IO.Util.StringUtil.Group(m, CssFontFace.CssFontFaceSrc .UrlGroup)); NUnit.Framework.Assert.IsTrue(url.StartsWith(fontSrc), "Invalid url: " + url); NUnit.Framework.Assert.IsTrue(format == null || CssFontFace.CssFontFaceSrc.ParseFormat(format) != CssFontFace.FontFormat .None, "Invalid format: " + format); NUnit.Framework.Assert.AreEqual(sources[i], source2, "Group check fails: "); CssFontFace.CssFontFaceSrc fontFaceSrc = CssFontFace.CssFontFaceSrc.Create(sources[i]); NUnit.Framework.Assert.IsTrue(fontFaceSrc.GetSrc().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.GetFormat()) { case CssFontFace.FontFormat.OpenType: { NUnit.Framework.Assert.IsTrue(sources[i].Contains("opentype"), "Format " + fontFaceSrc.GetFormat() + " expected: " + sources[i]); break; } case CssFontFace.FontFormat.TrueType: { NUnit.Framework.Assert.IsTrue(sources[i].Contains("truetype"), "Format " + fontFaceSrc.GetFormat() + " expected: " + sources[i]); break; } case CssFontFace.FontFormat.SVG: { NUnit.Framework.Assert.IsTrue(sources[i].Contains("svg"), "Format " + fontFaceSrc.GetFormat() + " expected: " + sources[i]); break; } case CssFontFace.FontFormat.None: { NUnit.Framework.Assert.IsFalse(sources[i].Contains("format("), "Format " + fontFaceSrc.GetFormat() + " expected: " + sources[i]); break; } } } }