protected override bool DoSetStyleValue(Style style, CSSStyleItemReader reader) { PDFFontSource root = null; PDFFontSource curr = null; bool hasExpr = false; while (reader.ReadNextValue(',', ';', true)) { string src = reader.CurrentTextValue.Trim(); PDFFontSource found; if (IsExpression(src)) { if (hasExpr) { throw new InvalidOperationException("Connot bind mupltiple values onto a Font src property"); } if (this.AttachExpressionBindingHandler(style, StyleKeys.FontFaceSrcKey, src, DoConvertFontSource)) { hasExpr = true; } } if (TryGetFontSource(src, out found)) { if (hasExpr) { throw new InvalidOperationException("Connot bind mupltiple values onto a Font src property"); } if (null == root) { root = found; } if (null == curr) { curr = found; } else { curr.Next = found; curr = found; } } //We need this to go past the original comma on the following ones, //becase we ignore white space in values, bit of a hack if (!reader.InnerEnumerator.EOS && reader.InnerEnumerator.Current == ',') { reader.InnerEnumerator.MoveNext(); } } if (null != root) { style.SetValue(StyleKeys.FontFaceSrcKey, root); return(true); } else { return(false); } }
private bool TryGetFontSource(string value, out PDFFontSource found) { if (PDFFontSource.TryParseOneValue(value, out found)) { return(true); } else { return(false); } }
protected bool DoConvertFontSource(StyleBase onstyle, object value, out PDFFontSource source) { if (null == value) { source = null; return(false); } else if (value is PDFFontSource src) { source = src; return(true); } else if (TryGetFontSource(value.ToString(), out source)) { return(true); } else { source = null; return(false); } }
public void ParsePDFFontSource() { string sample = "url(https://somewebsite.com/path/to/font.woff)"; PDFFontSource parsed; Assert.IsTrue(PDFFontSource.TryParseOneValue(sample, out parsed)); Assert.AreEqual(FontSourceType.Url, parsed.Type); Assert.AreEqual("https://somewebsite.com/path/to/font.woff", parsed.Source); Assert.AreEqual(FontSourceFormat.Default, parsed.Format); sample = "url(path/to/font.woff)"; Assert.IsTrue(PDFFontSource.TryParseOneValue(sample, out parsed)); Assert.AreEqual(FontSourceType.Url, parsed.Type); Assert.AreEqual("path/to/font.woff", parsed.Source); Assert.AreEqual(FontSourceFormat.Default, parsed.Format); sample = "url(path/to/font.woff) format(\"woff\")"; Assert.IsTrue(PDFFontSource.TryParseOneValue(sample, out parsed)); Assert.AreEqual(FontSourceType.Url, parsed.Type); Assert.AreEqual("path/to/font.woff", parsed.Source); Assert.AreEqual(FontSourceFormat.WOFF, parsed.Format); sample = "url('path/to/font.woff')"; Assert.IsTrue(PDFFontSource.TryParseOneValue(sample, out parsed)); Assert.AreEqual(FontSourceType.Url, parsed.Type); Assert.AreEqual("path/to/font.woff", parsed.Source); Assert.AreEqual(FontSourceFormat.Default, parsed.Format); sample = "url(\"path/to/svgfont.svg#example\")"; Assert.IsTrue(PDFFontSource.TryParseOneValue(sample, out parsed)); Assert.AreEqual(FontSourceType.Url, parsed.Type); Assert.AreEqual("path/to/svgfont.svg#example", parsed.Source); Assert.AreEqual(FontSourceFormat.Default, parsed.Format); sample = "url(\"path/to/svgfont.svg#example\") format(\"svg\")"; Assert.IsTrue(PDFFontSource.TryParseOneValue(sample, out parsed)); Assert.AreEqual(FontSourceType.Url, parsed.Type); Assert.AreEqual("path/to/svgfont.svg#example", parsed.Source); Assert.AreEqual(FontSourceFormat.SVG, parsed.Format); //Some locals sample = "local(font)"; Assert.IsTrue(PDFFontSource.TryParseOneValue(sample, out parsed)); Assert.AreEqual(FontSourceType.Local, parsed.Type); Assert.AreEqual("font", parsed.Source); Assert.AreEqual(FontSourceFormat.Default, parsed.Format); sample = "local(some font)"; Assert.IsTrue(PDFFontSource.TryParseOneValue(sample, out parsed)); Assert.AreEqual(FontSourceType.Local, parsed.Type); Assert.AreEqual("some font", parsed.Source); Assert.AreEqual(FontSourceFormat.Default, parsed.Format); sample = "local('some font') format(truetype)"; Assert.IsTrue(PDFFontSource.TryParseOneValue(sample, out parsed)); Assert.AreEqual(FontSourceType.Local, parsed.Type); Assert.AreEqual("some font", parsed.Source); Assert.AreEqual(FontSourceFormat.TrueType, parsed.Format); sample = "local(\"some other font\") format(\"opentype\")"; Assert.IsTrue(PDFFontSource.TryParseOneValue(sample, out parsed)); Assert.AreEqual(FontSourceType.Local, parsed.Type); Assert.AreEqual("some other font", parsed.Source); Assert.AreEqual(FontSourceFormat.OpenType, parsed.Format); //empty is false Assert.IsFalse(PDFFontSource.TryParseOneValue("", out parsed)); //unbalanced quotes is false Assert.IsFalse(PDFFontSource.TryParseOneValue("local(\"some other font) format(\"opentype\")", out parsed)); //Unknown source type is false Assert.IsFalse(PDFFontSource.TryParseOneValue("remote(\"path/to/svgfont.svg#example\") format(\"svg\")", out parsed)); //Other marker e.g. other is ignored so true Assert.IsTrue(PDFFontSource.TryParseOneValue("url(\"path/to/svgfont.svg#example\") other(\"svg\")", out parsed)); Assert.AreEqual(FontSourceType.Url, parsed.Type); Assert.AreEqual("path/to/svgfont.svg#example", parsed.Source); Assert.AreEqual(FontSourceFormat.Default, parsed.Format); //Parse Multiple var full = @"local(font), url(path/to/font.svg) format('svg'), url(path/to/font.woff) format('woff'), url(path/to/font.ttf) format(truetype), url('path/to/font.otf') format(embedded-opentype)"; Assert.IsTrue(PDFFontSource.TryParse(full, out parsed)); Assert.AreEqual("font", parsed.Source); Assert.AreEqual(FontSourceType.Local, parsed.Type); Assert.AreEqual(FontSourceFormat.Default, parsed.Format); parsed = parsed.Next; Assert.IsNotNull(parsed); Assert.AreEqual("path/to/font.svg", parsed.Source); Assert.AreEqual(FontSourceType.Url, parsed.Type); Assert.AreEqual(FontSourceFormat.SVG, parsed.Format); parsed = parsed.Next; Assert.IsNotNull(parsed); Assert.AreEqual("path/to/font.woff", parsed.Source); Assert.AreEqual(FontSourceType.Url, parsed.Type); Assert.AreEqual(FontSourceFormat.WOFF, parsed.Format); parsed = parsed.Next; Assert.IsNotNull(parsed); Assert.AreEqual("path/to/font.ttf", parsed.Source); Assert.AreEqual(FontSourceType.Url, parsed.Type); Assert.AreEqual(FontSourceFormat.TrueType, parsed.Format); parsed = parsed.Next; Assert.IsNotNull(parsed); Assert.AreEqual("path/to/font.otf", parsed.Source); Assert.AreEqual(FontSourceType.Url, parsed.Type); Assert.AreEqual(FontSourceFormat.EmbeddedOpenType, parsed.Format); }