public static bool TryGetActualFontFamily(string fontfamily, out PDFFontSelector found) { found = null; if (string.IsNullOrEmpty(fontfamily)) { return(false); } fontfamily = fontfamily.Trim(); //bool result = false; if (fontfamily.EndsWith(",")) { fontfamily = fontfamily.Substring(0, fontfamily.Length - 1); } if (fontfamily.StartsWith("'") || fontfamily.StartsWith("\"")) { fontfamily = fontfamily.Substring(1, fontfamily.Length - 2); } if (string.IsNullOrEmpty(fontfamily)) { return(false); } found = new PDFFontSelector(fontfamily); return(true); }
public void ParsingQuoted() { var str = "'Gill Sans MT', Helvetica, Arial, sans-serif"; var sel = PDFFontSelector.Parse(str); var curr = sel; Assert.IsNotNull(curr); Assert.AreEqual("Gill Sans MT", curr.FamilyName); curr = curr.Next; Assert.IsNotNull(curr); Assert.AreEqual("Helvetica", curr.FamilyName); curr = curr.Next; Assert.IsNotNull(curr); Assert.AreEqual("Arial", curr.FamilyName); curr = curr.Next; Assert.IsNotNull(curr); Assert.AreEqual("sans-serif", curr.FamilyName); curr = curr.Next; Assert.IsNull(curr); }
protected override bool DoSetStyleValue(Style onStyle, CSSStyleItemReader reader) { PDFFontSelector root = null; PDFFontSelector curr = null; bool hasExpr = false; while (reader.ReadNextValue()) { string fontfamily = reader.CurrentTextValue.Trim(); PDFFontSelector found; if (IsExpression(fontfamily)) { if (null != root) { throw new InvalidOperationException("Expressions - calc() and var() cannot be used within part of a font selector, us calc(concat(...)) instead."); } hasExpr = AttachExpressionBindingHandler(onStyle, StyleKeys.FontFamilyKey, fontfamily, DoConvertFontSelector); } if (TryGetActualFontFamily(fontfamily, out found)) { if (hasExpr) { throw new InvalidOperationException("Expressions - calc() and var() cannot be used within part of a font selector, us calc(concat(...)) instead."); } if (null == root) { root = found; } if (null == curr) { curr = found; } else { curr.Next = found; curr = found; } } } if (null != root) { onStyle.SetValue(StyleKeys.FontFamilyKey, root); return(true); } else { return(hasExpr); } }
public void ToStringOverrides() { var str = "'Gill Sans MT', Gill Sans, Helvetica, Arial, sans-serif"; var sel = PDFFontSelector.Parse(str); var result = sel.ToString(); Assert.AreEqual("'Gill Sans MT', 'Gill Sans', Helvetica, Arial, sans-serif", result); str = "\"Gill Sans MT\", Helvetica,, sans-serif "; sel = PDFFontSelector.Parse(str); result = sel.ToString(); Assert.AreEqual("'Gill Sans MT', Helvetica, sans-serif", result); }
public void ParsingDoubleQuotedAndEmpty() { var str = "\"Gill Sans MT\", Helvetica,, sans-serif "; var sel = PDFFontSelector.Parse(str); var curr = sel; Assert.IsNotNull(curr); Assert.AreEqual("Gill Sans MT", curr.FamilyName); curr = curr.Next; Assert.IsNotNull(curr); Assert.AreEqual("Helvetica", curr.FamilyName); curr = curr.Next; Assert.IsNotNull(curr); Assert.AreEqual("sans-serif", curr.FamilyName); curr = curr.Next; Assert.IsNull(curr); }
public void ParsingSimple() { var str = "Gill Sans, Helvetica, Arial"; var sel = PDFFontSelector.Parse(str); var curr = sel; Assert.IsNotNull(curr); Assert.AreEqual("Gill Sans", curr.FamilyName); curr = curr.Next; Assert.IsNotNull(curr); Assert.AreEqual("Helvetica", curr.FamilyName); curr = curr.Next; Assert.IsNotNull(curr); Assert.AreEqual("Arial", curr.FamilyName); curr = curr.Next; Assert.IsNull(curr); }
protected bool DoConvertFontSelector(StyleBase onStyle, object value, out PDFFontSelector selector) { if (null == value) { selector = null; return(false); } else if (value is PDFFontSelector f) { selector = f; return(true); } else if (TryGetActualFontFamily(value.ToString(), out selector)) { return(true); } else { selector = null; return(false); } }
protected override bool DoSetStyleValue(Style onStyle, CSSStyleItemReader reader) { bool result = false; if (!reader.ReadNextValue()) { return(result); } if (IsExpression(reader.CurrentTextValue)) { throw new InvalidOperationException("The compound 'font' css selector does not currently support expressions"); } //compound values - caption|icon|menu|message-box|small-caption|status-bar // invalid values - initial|inherit switch (reader.CurrentTextValue.ToLower()) { case ("caption"): ApplyFont(onStyle, CaptionFont); return(true); case ("icon"): ApplyFont(onStyle, IconFont); return(true); case ("menu"): ApplyFont(onStyle, MenuFont); return(true); case ("message-box"): ApplyFont(onStyle, MessageBoxFont); return(true); case ("small-caption"): ApplyFont(onStyle, SmallCaptionFont); return(true); case ("status-bar"): ApplyFont(onStyle, StatusBarFont); return(true); default: //Just fall back to parsing each individual item break; } result = true; //discreet values - font-style font-variant font-weight font-size/line-height font-family bool italic; bool bold; PDFUnit fsize, lineheight; double relativeLeading; if (CSSFontStyleParser.TryGetFontStyle(reader.CurrentTextValue, out italic)) { onStyle.Font.FontItalic = italic; if (!reader.MoveToNextValue()) { return(result); } } if (IsDefinedFontVariant(reader.CurrentTextValue)) { //We dont support variant but we should honour it being there. if (!reader.MoveToNextValue()) { return(result); } } if (CSSFontWeightParser.TryGetFontWeight(reader.CurrentTextValue, out bold)) { onStyle.Font.FontBold = bold; if (!reader.MoveToNextValue()) { return(result); } } //font-size/line height are special and will return the both values from the reader if both are present //other wise it is just the font-size if (reader.CurrentTextValue.IndexOf('/') > 0) { // we have both string part = reader.CurrentTextValue.Substring(0, reader.CurrentTextValue.IndexOf('/')); if (ParseCSSUnit(part, out fsize)) { onStyle.Font.FontSize = fsize; } else { result = false; } part = reader.CurrentTextValue.Substring(reader.CurrentTextValue.IndexOf('/') + 1); //if we have a simple double value then it is relative leading based on font size. if (double.TryParse(part, out relativeLeading)) { onStyle.Text.Leading = onStyle.Font.FontSize * relativeLeading; } else if (ParseCSSUnit(part, out lineheight)) { onStyle.Text.Leading = lineheight; } else { result = false; } if (!reader.MoveToNextValue()) { return(result); } } else if (ParseCSSUnit(reader.CurrentTextValue, out fsize)) { onStyle.Font.FontSize = fsize; if (!reader.MoveToNextValue()) { return(result); } } //last one is multiple font families bool foundFamily = false; PDFFontSelector root = null; do { if (IsExpression(reader.CurrentTextValue)) { throw new InvalidOperationException("The compound 'font' css selector does not currently support expressions"); } PDFFontSelector selector; PDFFontSelector curr = null; if (CSSFontFamilyParser.TryGetActualFontFamily(reader.CurrentTextValue, out selector)) { if (null == root) { root = selector; } if (null == curr) { curr = selector; } else { curr.Next = selector; curr = selector; } foundFamily = true; } }while (reader.MoveToNextValue()); if (foundFamily) { onStyle.SetValue(StyleKeys.FontFamilyKey, root); } else { result = false; } return(result); }