private void ApplyRotation(SvgDrawContext context) { if (this.attributesAndStyles.ContainsKey(SvgConstants.Attributes.ORIENT)) { String orient = this.attributesAndStyles.Get(SvgConstants.Attributes.ORIENT); double rotAngle = double.NaN; // If placed by marker-start, the marker is oriented 180° different from // the orientation that would be used if auto was specified. // For all other markers, auto-start-reverse means the same as auto. if (SvgConstants.Values.AUTO.Equals(orient) || (SvgConstants.Values.AUTO_START_REVERSE.Equals(orient) && ! SvgConstants.Attributes.MARKER_START.Equals(this.attributesAndStyles.Get(SvgConstants.Tags.MARKER)))) { rotAngle = ((IMarkerCapable)GetParent()).GetAutoOrientAngle(this, false); } else { if (SvgConstants.Values.AUTO_START_REVERSE.Equals(orient) && SvgConstants.Attributes.MARKER_START.Equals(this .attributesAndStyles.Get(SvgConstants.Tags.MARKER))) { rotAngle = ((IMarkerCapable)GetParent()).GetAutoOrientAngle(this, true); } else { if (CssUtils.IsAngleValue(orient) || CssUtils.IsNumericValue(orient)) { rotAngle = CssUtils.ParseAngle(this.attributesAndStyles.Get(SvgConstants.Attributes.ORIENT)); } } } if (!double.IsNaN(rotAngle)) { context.GetCurrentCanvas().ConcatMatrix(AffineTransform.GetRotateInstance(rotAngle)); } } }
/** * Calculates the leading of the given tag. * <br /> * First checks which line-height string is present in the css of the tag, if any. Following strings are allowed; * <ul> * <li>a constant (containing px, in, cm, mm, pc, em, ex or pt),</li> * <li>percentage (containing %),</li> * <li>multiplier (only digits),</li> * </ul> * Then this methods calculates the leading based on the font-size and the line-height.<br /><br /> * If no line-height was given or if the line-height:normal was given, leading = font-size * 1.5f. * @param t tag of which the leading has to be calculated. * @return float containing the leading of the tag. */ public float CalculateLeading(Tag t) { float leading = 0; IDictionary <String, String> css = t.CSS; float fontSize = fontSizeTranslator.GetFontSize(t); if (css.ContainsKey(CSS.Property.LINE_HEIGHT)) { String value = css[CSS.Property.LINE_HEIGHT]; if (utils.IsNumericValue(value)) { leading = float.Parse(value, CultureInfo.InvariantCulture) * fontSize; } else if (utils.IsRelativeValue(value)) { leading = utils.ParseRelativeValue(value, fontSize); } else if (utils.IsMetricValue(value)) { leading = utils.ParsePxInCmMmPcToPt(value); } // a faulty value was entered for line-height. if (leading == 0) { leading = fontSize * 1.5f; } } else { leading = fontSize * 1.5f; } return(leading); }
/// <summary>Resolves the property type.</summary> /// <param name="value">the value</param> /// <returns>the property type value</returns> private int ResolvePropertyType(String value) { if (value.Contains("url(") || CssGradientUtil.IsCssLinearGradientValue(value) || CommonCssConstants.NONE.Equals (value)) { return(BACKGROUND_IMAGE_TYPE); } else { if (CommonCssConstants.BACKGROUND_REPEAT_VALUES.Contains(value)) { return(BACKGROUND_REPEAT_TYPE); } else { if (CommonCssConstants.BACKGROUND_ATTACHMENT_VALUES.Contains(value)) { return(BACKGROUND_ATTACHMENT_TYPE); } else { if (CommonCssConstants.BACKGROUND_POSITION_VALUES.Contains(value)) { return(BACKGROUND_POSITION_TYPE); } else { if (CssUtils.IsNumericValue(value) || CssUtils.IsMetricValue(value) || CssUtils.IsRelativeValue(value)) { return(BACKGROUND_POSITION_OR_SIZE_TYPE); } else { if (CommonCssConstants.BACKGROUND_SIZE_VALUES.Contains(value)) { return(BACKGROUND_POSITION_OR_SIZE_TYPE); } else { if (CssUtils.IsColorProperty(value)) { return(BACKGROUND_COLOR_TYPE); } else { if (CommonCssConstants.BACKGROUND_ORIGIN_OR_CLIP_VALUES.Contains(value)) { return(BACKGROUND_ORIGIN_OR_CLIP_TYPE); } } } } } } } } return(UNDEFINED_TYPE); }
/// <summary>Check to see if the passed value is a measurement of the type based on the passed measurement symbol string /// </summary> /// <param name="value">string containing value to check</param> /// <param name="measurement">measurement symbol (e.g. % for relative, px for pixels)</param> /// <returns>True if the value is numerical and ends with the measurement symbol, false otherwise</returns> private static bool ValueIsOfMeasurement(String value, String measurement) { if (value == null) { return(false); } return(value.EndsWith(measurement) && CssUtils.IsNumericValue(value.JSubstring(0, value.Length - measurement .Length).Trim())); }
/* (non-Javadoc) * @see com.itextpdf.styledxmlparser.css.resolve.shorthand.IShorthandResolver#resolveShorthand(java.lang.String) */ public virtual IList <CssDeclaration> ResolveShorthand(String shorthandExpression) { String widthPropName = MessageFormatUtil.Format(_0_WIDTH, GetPrefix()); String stylePropName = MessageFormatUtil.Format(_0_STYLE, GetPrefix()); String colorPropName = MessageFormatUtil.Format(_0_COLOR, GetPrefix()); if (CommonCssConstants.INITIAL.Equals(shorthandExpression) || CommonCssConstants.INHERIT.Equals(shorthandExpression )) { return(JavaUtil.ArraysAsList(new CssDeclaration(widthPropName, shorthandExpression), new CssDeclaration(stylePropName , shorthandExpression), new CssDeclaration(colorPropName, shorthandExpression))); } String[] props = iText.IO.Util.StringUtil.Split(shorthandExpression, "\\s+"); String borderColorValue = null; String borderStyleValue = null; String borderWidthValue = null; foreach (String value in props) { if (CommonCssConstants.INITIAL.Equals(value) || CommonCssConstants.INHERIT.Equals(value)) { ILog logger = LogManager.GetLogger(typeof(AbstractBorderShorthandResolver)); logger.Warn(MessageFormatUtil.Format(iText.StyledXmlParser.LogMessageConstant.INVALID_CSS_PROPERTY_DECLARATION , shorthandExpression)); return(JavaCollectionsUtil.EmptyList <CssDeclaration>()); } if (CommonCssConstants.BORDER_WIDTH_VALUES.Contains(value) || CssUtils.IsNumericValue(value) || CssUtils.IsMetricValue (value) || CssUtils.IsRelativeValue(value)) { borderWidthValue = value; } else { if (CommonCssConstants.BORDER_STYLE_VALUES.Contains(value) || value.Equals(CommonCssConstants.AUTO)) { // AUTO property value is needed for outline property only borderStyleValue = value; } else { if (CssUtils.IsColorProperty(value)) { borderColorValue = value; } } } } IList <CssDeclaration> resolvedDecl = new List <CssDeclaration>(); resolvedDecl.Add(new CssDeclaration(widthPropName, borderWidthValue == null ? CommonCssConstants.INITIAL : borderWidthValue)); resolvedDecl.Add(new CssDeclaration(stylePropName, borderStyleValue == null ? CommonCssConstants.INITIAL : borderStyleValue)); resolvedDecl.Add(new CssDeclaration(colorPropName, borderColorValue == null ? CommonCssConstants.INITIAL : borderColorValue)); return(resolvedDecl); }
virtual public void ValidateNumericValue() { Assert.AreEqual(true, css.IsNumericValue("1")); Assert.AreEqual(true, css.IsNumericValue("12")); Assert.AreEqual(true, css.IsNumericValue("1.2")); Assert.AreEqual(true, css.IsNumericValue(".12")); Assert.AreEqual(false, css.IsNumericValue("12f")); Assert.AreEqual(false, css.IsNumericValue("f1.2")); Assert.AreEqual(false, css.IsNumericValue(".12f")); }
// TODO (DEVSIX-3596) Add support of 'lh' 'ch' units and viewport-relative units private float ParseFontRelativeOrAbsoluteLengthOnMarker(String length) { float value = 0f; if (CssUtils.IsMetricValue(length) || CssUtils.IsNumericValue(length)) { value = CssUtils.ParseAbsoluteLength(length); } else { if (CssUtils.IsFontRelativeValue(length)) { // Defaut font-size is medium value = CssUtils.ParseRelativeValue(length, CssUtils.ParseAbsoluteFontSize(CommonCssConstants.MEDIUM)); // Different browsers process font-relative units for markers differently. // We do it according to the css specification. if (CssUtils.IsRemValue(length)) { ISvgNodeRenderer rootElement = GetSvgRootElement(GetParent()); if (rootElement != null && rootElement.GetAttribute(CommonCssConstants.FONT_SIZE) != null) { value = CssUtils.ParseRelativeValue(length, CssUtils.ParseAbsoluteFontSize(rootElement.GetAttribute(CommonCssConstants .FONT_SIZE))); } } else { if (CssUtils.IsEmValue(length)) { ISvgNodeRenderer parentElement = this.GetParent(); if (parentElement != null && parentElement.GetAttribute(CommonCssConstants.FONT_SIZE) != null) { value = CssUtils.ParseRelativeValue(length, CssUtils.ParseAbsoluteFontSize(parentElement.GetAttribute(CommonCssConstants .FONT_SIZE))); } } else { if (CssUtils.IsExValue(length)) { if (this.GetAttribute(CommonCssConstants.FONT_SIZE) != null) { value = CssUtils.ParseRelativeValue(length, CssUtils.ParseAbsoluteFontSize(this.GetAttribute(CommonCssConstants .FONT_SIZE))); } } } } } } return(value); }
private static void SetLineHeight(IPropertyContainer elementToSet, String lineHeight, float em, float rem) { if (lineHeight != null && !CssConstants.NORMAL.Equals(lineHeight) && !CssConstants.AUTO.Equals(lineHeight) ) { if (CssUtils.IsNumericValue(lineHeight)) { float?number = CssUtils.ParseFloat(lineHeight); if (number != null) { elementToSet.SetProperty(Property.LINE_HEIGHT, LineHeight.CreateMultipliedValue((float)number)); } else { elementToSet.SetProperty(Property.LINE_HEIGHT, LineHeight.CreateNormalValue()); } } else { UnitValue lineHeightValue = CssUtils.ParseLengthValueToPt(lineHeight, em, rem); if (lineHeightValue != null && lineHeightValue.IsPointValue()) { elementToSet.SetProperty(Property.LINE_HEIGHT, LineHeight.CreateFixedValue(lineHeightValue.GetValue())); } else { if (lineHeightValue != null) { elementToSet.SetProperty(Property.LINE_HEIGHT, LineHeight.CreateMultipliedValue(lineHeightValue.GetValue() / 100f)); } else { elementToSet.SetProperty(Property.LINE_HEIGHT, LineHeight.CreateNormalValue()); } } } } else { elementToSet.SetProperty(Property.LINE_HEIGHT, LineHeight.CreateNormalValue()); } }
/// <summary>Evaluates the stop color offset value</summary> /// <returns>the stop color offset value in [0, 1] range</returns> public virtual double GetOffset() { double?offset = null; String offsetAttribute = GetAttribute(SvgConstants.Attributes.OFFSET); if (CssUtils.IsPercentageValue(offsetAttribute)) { offset = (double)CssUtils.ParseRelativeValue(offsetAttribute, 1); } else { if (CssUtils.IsNumericValue(offsetAttribute)) { offset = CssUtils.ParseDouble(offsetAttribute); } } double result = offset != null ? offset.Value : 0d; return(result > 1d ? 1d : result > 0d ? result : 0d); }
private static void SetLineHeightByLeading(IPropertyContainer element, String lineHeight, float em, float rem) { // specification does not give auto as a possible lineHeight value // nevertheless some browsers compute it as normal so we apply the same behaviour. // What's more, it's basically the same thing as if lineHeight is not set in the first place if (lineHeight != null && !CssConstants.NORMAL.Equals(lineHeight) && !CssConstants.AUTO.Equals(lineHeight) ) { if (CssUtils.IsNumericValue(lineHeight)) { float?mult = CssUtils.ParseFloat(lineHeight); if (mult != null) { element.SetProperty(Property.LEADING, new Leading(Leading.MULTIPLIED, (float)mult)); } } else { UnitValue lineHeightValue = CssUtils.ParseLengthValueToPt(lineHeight, em, rem); if (lineHeightValue != null && lineHeightValue.IsPointValue()) { element.SetProperty(Property.LEADING, new Leading(Leading.FIXED, lineHeightValue.GetValue())); } else { if (lineHeightValue != null) { element.SetProperty(Property.LEADING, new Leading(Leading.MULTIPLIED, lineHeightValue.GetValue() / 100)); } } } } else { element.SetProperty(Property.LEADING, new Leading(Leading.MULTIPLIED, DEFAULT_LINE_HEIGHT)); } }
private double GetCoordinateForObjectBoundingBox(String attributeName, double defaultValue) { String attributeValue = GetAttribute(attributeName); double absoluteValue = defaultValue; if (CssUtils.IsPercentageValue(attributeValue)) { absoluteValue = CssUtils.ParseRelativeValue(attributeValue, 1); } else { if (CssUtils.IsNumericValue(attributeValue) || CssUtils.IsMetricValue(attributeValue) || CssUtils.IsRelativeValue (attributeValue)) { // if there is incorrect value metric, then we do not need to parse the value int unitsPosition = CssUtils.DeterminePositionBetweenValueAndUnit(attributeValue); if (unitsPosition > 0) { // We want to ignore the unit type. From the svg specification: // "the normal of the linear gradient is perpendicular to the gradient vector in // object bounding box space (i.e., the abstract coordinate system where (0,0) // is at the top/left of the object bounding box and (1,1) is at the bottom/right // of the object bounding box)". // Different browsers treats this differently. We chose the "Google Chrome" approach // which treats the "abstract coordinate system" in the coordinate metric measure, // i.e. for value '0.5cm' the top/left of the object bounding box would be (1cm, 1cm), // for value '0.5em' the top/left of the object bounding box would be (1em, 1em) and etc. // no null pointer should be thrown as determine absoluteValue = CssUtils.ParseDouble(attributeValue.JSubstring(0, unitsPosition)).Value; } } } // need to multiply by 0.75 as further the (top, right) coordinates of the object bbox // would be transformed into (0.75, 0.75) point instead of (1, 1). The reason described // as a comment inside the method constructing the gradient transformation return(absoluteValue * 0.75); }
/// <summary>Gets the actual value of the line height.</summary> /// <param name="fontSize">the font size</param> /// <param name="rootFontSize">the root font size</param> /// <param name="lineHeightStr"> /// the line height as a /// <see cref="System.String"/> /// </param> /// <returns> /// the actual line height as a /// <c>float</c> /// </returns> private static float GetLineHeightActualValue(float fontSize, float rootFontSize, String lineHeightStr) { float lineHeightActualValue; if (lineHeightStr != null) { if (CssConstants.NORMAL.Equals(lineHeightStr) || CssConstants.AUTO.Equals(lineHeightStr)) { lineHeightActualValue = (float)(fontSize * 1.2); } else { UnitValue lineHeightValue = CssUtils.ParseLengthValueToPt(lineHeightStr, fontSize, rootFontSize); if (CssUtils.IsNumericValue(lineHeightStr)) { lineHeightActualValue = fontSize * lineHeightValue.GetValue(); } else { if (lineHeightValue.IsPointValue()) { lineHeightActualValue = lineHeightValue.GetValue(); } else { lineHeightActualValue = fontSize * lineHeightValue.GetValue() / 100; } } } } else { lineHeightActualValue = (float)(fontSize * 1.2); } return(lineHeightActualValue); }
virtual public PdfDiv apply(PdfDiv div, Tag t, IMarginMemory memory, IPageSizeContainable psc) { IDictionary <String, String> css = t.CSS; float fontSize = FontSizeTranslator.GetInstance().TranslateFontSize(t); if (fontSize == Font.UNDEFINED) { fontSize = FontSizeTranslator.DEFAULT_FONT_SIZE; } String align = null; if (t.Attributes.ContainsKey(HTML.Attribute.ALIGN)) { align = t.Attributes[HTML.Attribute.ALIGN]; } else if (css.ContainsKey(CSS.Property.TEXT_ALIGN)) { align = css[CSS.Property.TEXT_ALIGN]; } if (align != null) { if (Util.EqualsIgnoreCase(align, CSS.Value.CENTER)) { div.TextAlignment = Element.ALIGN_CENTER; } else if (Util.EqualsIgnoreCase(align, CSS.Value.LEFT)) { div.TextAlignment = Element.ALIGN_LEFT; } else if (Util.EqualsIgnoreCase(align, CSS.Value.RIGHT)) { div.TextAlignment = Element.ALIGN_RIGHT; } else if (Util.EqualsIgnoreCase(align, CSS.Value.JUSTIFY)) { div.TextAlignment = Element.ALIGN_JUSTIFIED; } } String widthValue; if (!t.CSS.TryGetValue(HTML.Attribute.WIDTH, out widthValue)) { t.Attributes.TryGetValue(HTML.Attribute.WIDTH, out widthValue); } if (widthValue != null) { float pageWidth = psc.PageSize.Width; if (utils.IsNumericValue(widthValue) || utils.IsMetricValue(widthValue)) { div.Width = Math.Min(pageWidth, utils.ParsePxInCmMmPcToPt(widthValue)); } else if (utils.IsRelativeValue(widthValue)) { if (widthValue.Contains(CSS.Value.PERCENTAGE)) { div.PercentageWidth = utils.ParseRelativeValue(widthValue, 1f); } else { div.Width = Math.Min(pageWidth, utils.ParseRelativeValue(widthValue, fontSize)); } } } String heightValue; if (!t.CSS.TryGetValue(HTML.Attribute.HEIGHT, out heightValue)) { t.Attributes.TryGetValue(HTML.Attribute.HEIGHT, out heightValue); } if (heightValue != null) { if (utils.IsNumericValue(heightValue) || utils.IsMetricValue(heightValue)) { div.Height = utils.ParsePxInCmMmPcToPt(heightValue); } else if (utils.IsRelativeValue(heightValue)) { if (heightValue.Contains(CSS.Value.PERCENTAGE)) { div.PercentageHeight = utils.ParseRelativeValue(heightValue, 1f); } else { div.Height = utils.ParseRelativeValue(heightValue, fontSize); } } } float?marginTop = null; float?marginBottom = null; foreach (KeyValuePair <String, String> entry in css) { String key = entry.Key; String value = entry.Value; if (Util.EqualsIgnoreCase(key, CSS.Property.LEFT)) { div.Left = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.RIGHT)) { if (div.Width == null || div.Left == null) { div.Right = utils.ParseValueToPt(value, fontSize); } } else if (Util.EqualsIgnoreCase(key, CSS.Property.TOP)) { div.Top = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.BOTTOM)) { if (div.Height == null || div.Top == null) { div.Bottom = utils.ParseValueToPt(value, fontSize); } } else if (Util.EqualsIgnoreCase(key, CSS.Property.BACKGROUND_COLOR)) { div.BackgroundColor = HtmlUtilities.DecodeColor(value); } else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_LEFT)) { div.PaddingLeft = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_RIGHT)) { div.PaddingRight = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_TOP)) { div.PaddingTop = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_BOTTOM)) { div.PaddingBottom = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.MARGIN_TOP)) { marginTop = utils.CalculateMarginTop(value, fontSize, memory); } else if (Util.EqualsIgnoreCase(key, CSS.Property.MARGIN_BOTTOM)) { marginBottom = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.FLOAT)) { if (Util.EqualsIgnoreCase(value, CSS.Value.LEFT)) { div.Float = PdfDiv.FloatType.LEFT; } else if (Util.EqualsIgnoreCase(value, CSS.Value.RIGHT)) { div.Float = PdfDiv.FloatType.RIGHT; } } else if (Util.EqualsIgnoreCase(key, CSS.Property.POSITION)) { if (Util.EqualsIgnoreCase(value, CSS.Value.ABSOLUTE)) { div.Position = PdfDiv.PositionType.ABSOLUTE; } else if (Util.EqualsIgnoreCase(value, CSS.Value.FIXED)) { div.Position = PdfDiv.PositionType.FIXED; } else if (Util.EqualsIgnoreCase(value, CSS.Value.RELATIVE)) { div.Position = PdfDiv.PositionType.RELATIVE; } } else if (Util.EqualsIgnoreCase(key, CSS.Property.DISPLAY)) { if (Util.EqualsIgnoreCase(value, CSS.Value.BLOCK)) { div.Display = PdfDiv.DisplayType.BLOCK; } else if (Util.EqualsIgnoreCase(value, CSS.Value.INLINE)) { div.Display = PdfDiv.DisplayType.INLINE; } else if (Util.EqualsIgnoreCase(value, CSS.Value.INLINE_BLOCK)) { div.Display = PdfDiv.DisplayType.INLINE_BLOCK; } else if (Util.EqualsIgnoreCase(value, CSS.Value.INLINE_TABLE)) { div.Display = PdfDiv.DisplayType.INLINE_TABLE; } else if (Util.EqualsIgnoreCase(value, CSS.Value.LIST_ITEM)) { div.Display = PdfDiv.DisplayType.LIST_ITEM; } else if (Util.EqualsIgnoreCase(value, CSS.Value.NONE)) { div.Display = PdfDiv.DisplayType.NONE; } else if (Util.EqualsIgnoreCase(value, CSS.Value.RUN_IN)) { div.Display = PdfDiv.DisplayType.RUN_IN; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE)) { div.Display = PdfDiv.DisplayType.TABLE; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_CAPTION)) { div.Display = PdfDiv.DisplayType.TABLE_CAPTION; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_CELL)) { div.Display = PdfDiv.DisplayType.TABLE_CELL; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_COLUMN_GROUP)) { div.Display = PdfDiv.DisplayType.TABLE_COLUMN_GROUP; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_COLUMN)) { div.Display = PdfDiv.DisplayType.TABLE_COLUMN; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_FOOTER_GROUP)) { div.Display = PdfDiv.DisplayType.TABLE_FOOTER_GROUP; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_HEADER_GROUP)) { div.Display = PdfDiv.DisplayType.TABLE_HEADER_GROUP; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_ROW)) { div.Display = PdfDiv.DisplayType.TABLE_ROW; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_ROW_GROUP)) { div.Display = PdfDiv.DisplayType.TABLE_ROW_GROUP; } } else if (Util.EqualsIgnoreCase(CSS.Property.BORDER_TOP_STYLE, key)) { if (Util.EqualsIgnoreCase(CSS.Value.DOTTED, value)) { div.BorderStyle = PdfDiv.BorderTopStyle.DOTTED; } else if (Util.EqualsIgnoreCase(CSS.Value.DASHED, value)) { div.BorderStyle = PdfDiv.BorderTopStyle.DASHED; } else if (Util.EqualsIgnoreCase(CSS.Value.SOLID, value)) { div.BorderStyle = PdfDiv.BorderTopStyle.SOLID; } else if (Util.EqualsIgnoreCase(CSS.Value.DOUBLE, value)) { div.BorderStyle = PdfDiv.BorderTopStyle.DOUBLE; } else if (Util.EqualsIgnoreCase(CSS.Value.GROOVE, value)) { div.BorderStyle = PdfDiv.BorderTopStyle.GROOVE; } else if (Util.EqualsIgnoreCase(CSS.Value.RIDGE, value)) { div.BorderStyle = PdfDiv.BorderTopStyle.RIDGE; } else if (Util.EqualsIgnoreCase(value, CSS.Value.INSET)) { div.BorderStyle = PdfDiv.BorderTopStyle.INSET; } else if (Util.EqualsIgnoreCase(value, CSS.Value.OUTSET)) { div.BorderStyle = PdfDiv.BorderTopStyle.OUTSET; } } //TODO: border, background properties. } return(div); }
/** * * @param c the Chunk to apply CSS to. * @param t the tag containing the chunk data * @return the styled chunk */ virtual public Chunk Apply(Chunk c, Tag t) { Font f = ApplyFontStyles(t); float size = f.Size; String value = null; IDictionary <String, String> rules = t.CSS; foreach (KeyValuePair <String, String> entry in rules) { String key = entry.Key; value = entry.Value; if (Util.EqualsIgnoreCase(CSS.Property.FONT_STYLE, key)) { if (Util.EqualsIgnoreCase(CSS.Value.OBLIQUE, value)) { c.SetSkew(0, 12); } } else if (Util.EqualsIgnoreCase(CSS.Property.LETTER_SPACING, key)) { c.SetCharacterSpacing(utils.ParsePxInCmMmPcToPt(value)); } else if (Util.EqualsIgnoreCase(CSS.Property.XFA_FONT_HORIZONTAL_SCALE, key)) { // only % allowed; need a catch block NumberFormatExc? c.SetHorizontalScaling( float.Parse(value.Replace("%", "")) / 100); } } // following styles are separate from the for each loop, because they are based on font settings like size. if (rules.TryGetValue(CSS.Property.VERTICAL_ALIGN, out value)) { if (Util.EqualsIgnoreCase(CSS.Value.SUPER, value) || Util.EqualsIgnoreCase(CSS.Value.TOP, value) || Util.EqualsIgnoreCase(CSS.Value.TEXT_TOP, value)) { c.SetTextRise((float)(size / 2 + 0.5)); } else if (Util.EqualsIgnoreCase(CSS.Value.SUB, value) || Util.EqualsIgnoreCase(CSS.Value.BOTTOM, value) || Util.EqualsIgnoreCase(CSS.Value.TEXT_BOTTOM, value)) { c.SetTextRise(-size / 2); } else { c.SetTextRise(utils.ParsePxInCmMmPcToPt(value)); } } String xfaVertScale; if (rules.TryGetValue(CSS.Property.XFA_FONT_VERTICAL_SCALE, out xfaVertScale)) { if (xfaVertScale.Contains("%")) { size *= float.Parse(xfaVertScale.Replace("%", "")) / 100; c.SetHorizontalScaling(100 / float.Parse(xfaVertScale.Replace("%", ""))); } } if (rules.TryGetValue(CSS.Property.TEXT_DECORATION, out value)) { // Restriction? In html a underline and a line-through is possible on one piece of text. A Chunk can set an underline only once. if (Util.EqualsIgnoreCase(CSS.Value.UNDERLINE, value)) { c.SetUnderline(0.75f, -size / 8f); } if (Util.EqualsIgnoreCase(CSS.Value.LINE_THROUGH, value)) { c.SetUnderline(0.75f, size / 4f); } } if (rules.TryGetValue(CSS.Property.BACKGROUND_COLOR, out value)) { c.SetBackground(HtmlUtilities.DecodeColor(value)); } f.Size = size; c.Font = f; float?leading = null; value = null; if (rules.TryGetValue(CSS.Property.LINE_HEIGHT, out value)) { if (utils.IsNumericValue(value)) { leading = float.Parse(value) * c.Font.Size; } else if (utils.IsRelativeValue(value)) { leading = utils.ParseRelativeValue(value, c.Font.Size); } else if (utils.IsMetricValue(value)) { leading = utils.ParsePxInCmMmPcToPt(value); } } if (leading != null) { c.setLineHeight((float)leading); } return(c); }
/** * Styles a paragraph * * @param p the paragraph * @param t the tag * @param configuration the MarginMemory * @return a styled {@link Paragraph} */ virtual public Paragraph Apply(Paragraph p, Tag t, IMarginMemory configuration) { /*MaxLeadingAndSize m = new MaxLeadingAndSize(); * if (configuration.GetRootTags().Contains(t.GetName())) { * m.SetLeading(t); * } else { * m.SetVariablesBasedOnChildren(t); * }*/ CssUtils utils = CssUtils.GetInstance(); float fontSize = FontSizeTranslator.GetInstance().GetFontSize(t); if (fontSize < 0) { fontSize = 0; } float lmb = 0; bool hasLMB = false; IDictionary <String, String> css = t.CSS; foreach (KeyValuePair <String, String> entry in css) { String key = entry.Key; String value = entry.Value; if (Util.EqualsIgnoreCase(CSS.Property.MARGIN_TOP, key)) { p.SpacingBefore = p.SpacingBefore + utils.CalculateMarginTop(value, fontSize, configuration); } else if (Util.EqualsIgnoreCase(CSS.Property.PADDING_TOP, key)) { p.SpacingBefore = p.SpacingBefore + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.MARGIN_BOTTOM, key)) { float after = utils.ParseValueToPt(value, fontSize); p.SpacingAfter = p.SpacingAfter + after; lmb = after; hasLMB = true; } else if (Util.EqualsIgnoreCase(CSS.Property.PADDING_BOTTOM, key)) { p.SpacingAfter = p.SpacingAfter + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.MARGIN_LEFT, key)) { p.IndentationLeft = p.IndentationLeft + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.MARGIN_RIGHT, key)) { p.IndentationRight = p.IndentationRight + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.PADDING_LEFT, key)) { p.IndentationLeft = p.IndentationLeft + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.PADDING_RIGHT, key)) { p.IndentationRight = p.IndentationRight + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.TEXT_ALIGN, key)) { if (Util.EqualsIgnoreCase(CSS.Value.RIGHT, value)) { p.Alignment = Element.ALIGN_RIGHT; } else if (Util.EqualsIgnoreCase(CSS.Value.CENTER, value)) { p.Alignment = Element.ALIGN_CENTER; } else if (Util.EqualsIgnoreCase(CSS.Value.LEFT, value)) { p.Alignment = Element.ALIGN_LEFT; } else if (Util.EqualsIgnoreCase(CSS.Value.JUSTIFY, value)) { p.Alignment = Element.ALIGN_JUSTIFIED; } } else if (Util.EqualsIgnoreCase(CSS.Property.TEXT_INDENT, key)) { p.FirstLineIndent = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.LINE_HEIGHT, key)) { if (utils.IsNumericValue(value)) { p.Leading = float.Parse(value) * fontSize; } else if (utils.IsRelativeValue(value)) { p.Leading = utils.ParseRelativeValue(value, fontSize); } else if (utils.IsMetricValue(value)) { p.Leading = utils.ParsePxInCmMmPcToPt(value); } } } // setDefaultMargin to largestFont if no margin-bottom is set and p-tag is child of the root tag. /*if (null != t.GetParent()) { * String parent = t.GetParent().GetName(); * if (css[CSS.Property.MARGIN_TOP] == null && configuration.GetRootTags().Contains(parent)) { * p.SetSpacingBefore(p.GetSpacingBefore() + utils.CalculateMarginTop(fontSize + "pt", 0, configuration)); * } * if (css[CSS.Property.MARGIN_BOTTOM] == null && configuration.GetRootTags().Contains(parent)) { * p.SetSpacingAfter(p.GetSpacingAfter() + fontSize); * css.Put(CSS.Property.MARGIN_BOTTOM, fontSize + "pt"); * lmb = fontSize; * hasLMB = true; * } * //p.SetLeading(m.GetLargestLeading()); We need possibility to detect that line-height undefined; * if (p.GetAlignment() == -1) { * p.SetAlignment(Element.ALIGN_LEFT); * } * }*/ if (hasLMB) { configuration.LastMarginBottom = lmb; } Font font = appliers.ChunkCssAplier.ApplyFontStyles(t); p.Font = font; // TODO reactive for positioning and implement more return(p); }
/** * * @param c the Chunk to apply CSS to. * @param t the tag containing the chunk data * @return the styled chunk */ public override Chunk Apply(Chunk c, Tag t, IMarginMemory mm, IPageSizeContainable psc, HtmlPipelineContext ctx) { Font f = ApplyFontStyles(t); float size = f.Size; String value = null; IDictionary <String, String> rules = t.CSS; foreach (KeyValuePair <String, String> entry in rules) { String key = entry.Key; value = entry.Value; if (Util.EqualsIgnoreCase(CSS.Property.FONT_STYLE, key)) { if (Util.EqualsIgnoreCase(CSS.Value.OBLIQUE, value)) { c.SetSkew(0, 12); } } else if (Util.EqualsIgnoreCase(CSS.Property.LETTER_SPACING, key)) { String letterSpacing = entry.Value; float letterSpacingValue = 0f; if (utils.IsRelativeValue(value)) { letterSpacingValue = utils.ParseRelativeValue(letterSpacing, f.Size); } else if (utils.IsMetricValue(value)) { letterSpacingValue = utils.ParsePxInCmMmPcToPt(letterSpacing); } c.SetCharacterSpacing(letterSpacingValue); } else if (Util.EqualsIgnoreCase(CSS.Property.XFA_FONT_HORIZONTAL_SCALE, key)) { // only % allowed; need a catch block NumberFormatExc? c.SetHorizontalScaling( float.Parse(value.Replace("%", ""), CultureInfo.InvariantCulture) / 100); } } // following styles are separate from the for each loop, because they are based on font settings like size. if (rules.TryGetValue(CSS.Property.VERTICAL_ALIGN, out value)) { if (Util.EqualsIgnoreCase(CSS.Value.SUPER, value) || Util.EqualsIgnoreCase(CSS.Value.TOP, value) || Util.EqualsIgnoreCase(CSS.Value.TEXT_TOP, value)) { c.SetTextRise((float)(size / 2 + 0.5)); } else if (Util.EqualsIgnoreCase(CSS.Value.SUB, value) || Util.EqualsIgnoreCase(CSS.Value.BOTTOM, value) || Util.EqualsIgnoreCase(CSS.Value.TEXT_BOTTOM, value)) { c.SetTextRise(-size / 2); } else { c.SetTextRise(utils.ParsePxInCmMmPcToPt(value)); } } String xfaVertScale; if (rules.TryGetValue(CSS.Property.XFA_FONT_VERTICAL_SCALE, out xfaVertScale)) { if (xfaVertScale.Contains("%")) { size *= float.Parse(xfaVertScale.Replace("%", ""), CultureInfo.InvariantCulture) / 100; c.SetHorizontalScaling(100 / float.Parse(xfaVertScale.Replace("%", ""), CultureInfo.InvariantCulture)); } } if (rules.TryGetValue(CSS.Property.TEXT_DECORATION, out value)) { String[] splitValues = new Regex(@"\s+").Split(value); foreach (String curValue in splitValues) { if (Util.EqualsIgnoreCase(CSS.Value.UNDERLINE, curValue)) { c.SetUnderline(null, 0.75f, 0, 0, -0.125f, PdfContentByte.LINE_CAP_BUTT); } if (Util.EqualsIgnoreCase(CSS.Value.LINE_THROUGH, curValue)) { c.SetUnderline(null, 0.75f, 0, 0, 0.25f, PdfContentByte.LINE_CAP_BUTT); } } } if (rules.TryGetValue(CSS.Property.BACKGROUND_COLOR, out value)) { c.SetBackground(HtmlUtilities.DecodeColor(value)); } f.Size = size; c.Font = f; float?leading = null; value = null; if (rules.TryGetValue(CSS.Property.LINE_HEIGHT, out value)) { if (utils.IsNumericValue(value)) { leading = float.Parse(value, CultureInfo.InvariantCulture) * c.Font.Size; } else if (utils.IsRelativeValue(value)) { leading = utils.ParseRelativeValue(value, c.Font.Size); } else if (utils.IsMetricValue(value)) { leading = utils.ParsePxInCmMmPcToPt(value); } } if (leading != null) { c.setLineHeight((float)leading); } return(c); }
/// <summary>Applies font styles to an element.</summary> /// <param name="cssProps">the CSS props</param> /// <param name="context">the processor context</param> /// <param name="stylesContainer">the styles container</param> /// <param name="element">the element</param> public static void ApplyFontStyles(IDictionary <String, String> cssProps, ProcessorContext context, IStylesContainer stylesContainer, IPropertyContainer element) { float em = CssUtils.ParseAbsoluteLength(cssProps.Get(CssConstants.FONT_SIZE)); float rem = context.GetCssContext().GetRootFontSize(); if (em != 0) { element.SetProperty(Property.FONT_SIZE, UnitValue.CreatePointValue(em)); } if (cssProps.Get(CssConstants.FONT_FAMILY) != null) { // TODO DEVSIX-2534 IList <String> fontFamilies = FontFamilySplitter.SplitFontFamily(cssProps.Get(CssConstants.FONT_FAMILY)); element.SetProperty(Property.FONT, fontFamilies.ToArray(new String[fontFamilies.Count])); } if (cssProps.Get(CssConstants.FONT_WEIGHT) != null) { element.SetProperty(Property.FONT_WEIGHT, cssProps.Get(CssConstants.FONT_WEIGHT)); } if (cssProps.Get(CssConstants.FONT_STYLE) != null) { element.SetProperty(Property.FONT_STYLE, cssProps.Get(CssConstants.FONT_STYLE)); } String cssColorPropValue = cssProps.Get(CssConstants.COLOR); if (cssColorPropValue != null) { TransparentColor transparentColor; if (!CssConstants.TRANSPARENT.Equals(cssColorPropValue)) { float[] rgbaColor = CssUtils.ParseRgbaColor(cssColorPropValue); Color color = new DeviceRgb(rgbaColor[0], rgbaColor[1], rgbaColor[2]); float opacity = rgbaColor[3]; transparentColor = new TransparentColor(color, opacity); } else { transparentColor = new TransparentColor(ColorConstants.BLACK, 0f); } element.SetProperty(Property.FONT_COLOR, transparentColor); } // Make sure to place that before text-align applier String direction = cssProps.Get(CssConstants.DIRECTION); if (CssConstants.RTL.Equals(direction)) { element.SetProperty(Property.BASE_DIRECTION, BaseDirection.RIGHT_TO_LEFT); element.SetProperty(Property.TEXT_ALIGNMENT, TextAlignment.RIGHT); } else { if (CssConstants.LTR.Equals(direction)) { element.SetProperty(Property.BASE_DIRECTION, BaseDirection.LEFT_TO_RIGHT); element.SetProperty(Property.TEXT_ALIGNMENT, TextAlignment.LEFT); } } if (stylesContainer is IElementNode && ((IElementNode)stylesContainer).ParentNode() is IElementNode && CssConstants .RTL.Equals(((IElementNode)((IElementNode)stylesContainer).ParentNode()).GetStyles().Get(CssConstants. DIRECTION)) && !element.HasProperty(Property.HORIZONTAL_ALIGNMENT)) { // We should only apply horizontal alignment if parent has dir attribute or direction property element.SetProperty(Property.HORIZONTAL_ALIGNMENT, HorizontalAlignment.RIGHT); } // Make sure to place that after direction applier String align = cssProps.Get(CssConstants.TEXT_ALIGN); if (CssConstants.LEFT.Equals(align)) { element.SetProperty(Property.TEXT_ALIGNMENT, TextAlignment.LEFT); } else { if (CssConstants.RIGHT.Equals(align)) { element.SetProperty(Property.TEXT_ALIGNMENT, TextAlignment.RIGHT); } else { if (CssConstants.CENTER.Equals(align)) { element.SetProperty(Property.TEXT_ALIGNMENT, TextAlignment.CENTER); } else { if (CssConstants.JUSTIFY.Equals(align)) { element.SetProperty(Property.TEXT_ALIGNMENT, TextAlignment.JUSTIFIED); element.SetProperty(Property.SPACING_RATIO, 1f); } } } } String whiteSpace = cssProps.Get(CssConstants.WHITE_SPACE); element.SetProperty(Property.NO_SOFT_WRAP_INLINE, CssConstants.NOWRAP.Equals(whiteSpace) || CssConstants.PRE .Equals(whiteSpace)); String textDecorationProp = cssProps.Get(CssConstants.TEXT_DECORATION); if (textDecorationProp != null) { String[] textDecorations = iText.IO.Util.StringUtil.Split(textDecorationProp, "\\s+"); IList <Underline> underlineList = new List <Underline>(); foreach (String textDecoration in textDecorations) { if (CssConstants.BLINK.Equals(textDecoration)) { logger.Error(iText.Html2pdf.LogMessageConstant.TEXT_DECORATION_BLINK_NOT_SUPPORTED); } else { if (CssConstants.LINE_THROUGH.Equals(textDecoration)) { underlineList.Add(new Underline(null, .75f, 0, 0, 1 / 4f, PdfCanvasConstants.LineCapStyle.BUTT)); } else { if (CssConstants.OVERLINE.Equals(textDecoration)) { underlineList.Add(new Underline(null, .75f, 0, 0, 9 / 10f, PdfCanvasConstants.LineCapStyle.BUTT)); } else { if (CssConstants.UNDERLINE.Equals(textDecoration)) { underlineList.Add(new Underline(null, .75f, 0, 0, -1 / 10f, PdfCanvasConstants.LineCapStyle.BUTT)); } else { if (CssConstants.NONE.Equals(textDecoration)) { underlineList = null; // if none and any other decoration are used together, none is displayed break; } } } } } } element.SetProperty(Property.UNDERLINE, underlineList); } String textIndent = cssProps.Get(CssConstants.TEXT_INDENT); if (textIndent != null) { UnitValue textIndentValue = CssUtils.ParseLengthValueToPt(textIndent, em, rem); if (textIndentValue != null) { if (textIndentValue.IsPointValue()) { element.SetProperty(Property.FIRST_LINE_INDENT, textIndentValue.GetValue()); } else { logger.Error(MessageFormatUtil.Format(iText.Html2pdf.LogMessageConstant.CSS_PROPERTY_IN_PERCENTS_NOT_SUPPORTED , CssConstants.TEXT_INDENT)); } } } String letterSpacing = cssProps.Get(CssConstants.LETTER_SPACING); if (letterSpacing != null && !letterSpacing.Equals(CssConstants.NORMAL)) { UnitValue letterSpacingValue = CssUtils.ParseLengthValueToPt(letterSpacing, em, rem); if (letterSpacingValue.IsPointValue()) { element.SetProperty(Property.CHARACTER_SPACING, letterSpacingValue.GetValue()); } } // browsers ignore values in percents String wordSpacing = cssProps.Get(CssConstants.WORD_SPACING); if (wordSpacing != null) { UnitValue wordSpacingValue = CssUtils.ParseLengthValueToPt(wordSpacing, em, rem); if (wordSpacingValue != null) { if (wordSpacingValue.IsPointValue()) { element.SetProperty(Property.WORD_SPACING, wordSpacingValue.GetValue()); } } } // browsers ignore values in percents String lineHeight = cssProps.Get(CssConstants.LINE_HEIGHT); // specification does not give auto as a possible lineHeight value // nevertheless some browsers compute it as normal so we apply the same behaviour. // What's more, it's basically the same thing as if lineHeight is not set in the first place if (lineHeight != null && !CssConstants.NORMAL.Equals(lineHeight) && !CssConstants.AUTO.Equals(lineHeight) ) { if (CssUtils.IsNumericValue(lineHeight)) { float?mult = CssUtils.ParseFloat(lineHeight); if (mult != null) { element.SetProperty(Property.LEADING, new Leading(Leading.MULTIPLIED, (float)mult)); } } else { UnitValue lineHeightValue = CssUtils.ParseLengthValueToPt(lineHeight, em, rem); if (lineHeightValue != null && lineHeightValue.IsPointValue()) { element.SetProperty(Property.LEADING, new Leading(Leading.FIXED, lineHeightValue.GetValue())); } else { if (lineHeightValue != null) { element.SetProperty(Property.LEADING, new Leading(Leading.MULTIPLIED, lineHeightValue.GetValue() / 100)); } } } } else { element.SetProperty(Property.LEADING, new Leading(Leading.MULTIPLIED, 1.2f)); } }
/* (non-Javadoc) * @see com.itextpdf.styledxmlparser.css.resolve.shorthand.IShorthandResolver#resolveShorthand(java.lang.String) */ public virtual IList <CssDeclaration> ResolveShorthand(String shorthandExpression) { if (UNSUPPORTED_VALUES_OF_FONT_SHORTHAND.Contains(shorthandExpression)) { ILog logger = LogManager.GetLogger(typeof(FontShorthandResolver)); logger.Error(MessageFormatUtil.Format("The \"{0}\" value of CSS shorthand property \"font\" is not supported" , shorthandExpression)); } if (CommonCssConstants.INITIAL.Equals(shorthandExpression) || CommonCssConstants.INHERIT.Equals(shorthandExpression )) { return(JavaUtil.ArraysAsList(new CssDeclaration(CommonCssConstants.FONT_STYLE, shorthandExpression), new CssDeclaration (CommonCssConstants.FONT_VARIANT, shorthandExpression), new CssDeclaration(CommonCssConstants.FONT_WEIGHT , shorthandExpression), new CssDeclaration(CommonCssConstants.FONT_SIZE, shorthandExpression), new CssDeclaration (CommonCssConstants.LINE_HEIGHT, shorthandExpression), new CssDeclaration(CommonCssConstants.FONT_FAMILY , shorthandExpression))); } String fontStyleValue = null; String fontVariantValue = null; String fontWeightValue = null; String fontSizeValue = null; String lineHeightValue = null; String fontFamilyValue = null; IList <String> properties = GetFontProperties(iText.IO.Util.StringUtil.ReplaceAll(shorthandExpression, "\\s*,\\s*" , ",")); foreach (String value in properties) { int slashSymbolIndex = value.IndexOf('/'); if (CommonCssConstants.ITALIC.Equals(value) || CommonCssConstants.OBLIQUE.Equals(value)) { fontStyleValue = value; } else { if (CommonCssConstants.SMALL_CAPS.Equals(value)) { fontVariantValue = value; } else { if (FONT_WEIGHT_NOT_DEFAULT_VALUES.Contains(value)) { fontWeightValue = value; } else { if (slashSymbolIndex > 0) { fontSizeValue = value.JSubstring(0, slashSymbolIndex); lineHeightValue = value.JSubstring(slashSymbolIndex + 1, value.Length); } else { if (FONT_SIZE_VALUES.Contains(value) || CssUtils.IsMetricValue(value) || CssUtils.IsNumericValue(value) || CssUtils.IsRelativeValue(value)) { fontSizeValue = value; } else { fontFamilyValue = value; } } } } } } IList <CssDeclaration> cssDeclarations = JavaUtil.ArraysAsList(new CssDeclaration(CommonCssConstants.FONT_STYLE , fontStyleValue == null ? CommonCssConstants.INITIAL : fontStyleValue), new CssDeclaration(CommonCssConstants .FONT_VARIANT, fontVariantValue == null ? CommonCssConstants.INITIAL : fontVariantValue), new CssDeclaration (CommonCssConstants.FONT_WEIGHT, fontWeightValue == null ? CommonCssConstants.INITIAL : fontWeightValue ), new CssDeclaration(CommonCssConstants.FONT_SIZE, fontSizeValue == null ? CommonCssConstants.INITIAL : fontSizeValue), new CssDeclaration(CommonCssConstants.LINE_HEIGHT, lineHeightValue == null ? CommonCssConstants .INITIAL : lineHeightValue), new CssDeclaration(CommonCssConstants.FONT_FAMILY, fontFamilyValue == null ? CommonCssConstants.INITIAL : fontFamilyValue)); return(cssDeclarations); }
public PdfDiv apply(PdfDiv div, Tag t, IMarginMemory memory, IPageSizeContainable psc) { IDictionary <String, String> css = t.CSS; float fontSize = FontSizeTranslator.GetInstance().TranslateFontSize(t); if (fontSize == Font.UNDEFINED) { fontSize = FontSizeTranslator.DEFAULT_FONT_SIZE; } String align = null; if (t.Attributes.ContainsKey(HTML.Attribute.ALIGN)) { align = t.Attributes[HTML.Attribute.ALIGN]; } else if (css.ContainsKey(CSS.Property.TEXT_ALIGN)) { align = css[CSS.Property.TEXT_ALIGN]; } if (align != null) { if (Util.EqualsIgnoreCase(align, CSS.Value.CENTER)) { div.TextAlignment = Element.ALIGN_CENTER; } else if (Util.EqualsIgnoreCase(align, CSS.Value.RIGHT)) { div.TextAlignment = Element.ALIGN_RIGHT; } else if (Util.EqualsIgnoreCase(align, CSS.Value.JUSTIFY)) { div.TextAlignment = Element.ALIGN_JUSTIFIED; } } String widthValue; if (!t.CSS.TryGetValue(HTML.Attribute.WIDTH, out widthValue)) { t.Attributes.TryGetValue(HTML.Attribute.WIDTH, out widthValue); } if (widthValue != null) { if (utils.IsNumericValue(widthValue) || utils.IsMetricValue(widthValue)) { div.Width = utils.ParsePxInCmMmPcToPt(widthValue); } else if (utils.IsRelativeValue(widthValue)) { if (widthValue.Contains(CSS.Value.PERCENTAGE)) { div.PercentageWidth = utils.ParseRelativeValue(widthValue, 1f); } else { div.Width = utils.ParseRelativeValue(widthValue, fontSize); } } } String heightValue; if (!t.CSS.TryGetValue(HTML.Attribute.HEIGHT, out heightValue)) { t.Attributes.TryGetValue(HTML.Attribute.HEIGHT, out heightValue); } if (heightValue != null) { if (utils.IsNumericValue(heightValue) || utils.IsMetricValue(heightValue)) { div.Height = utils.ParsePxInCmMmPcToPt(heightValue); } else if (utils.IsRelativeValue(heightValue)) { if (heightValue.Contains(CSS.Value.PERCENTAGE)) { div.PercentageHeight = utils.ParseRelativeValue(heightValue, 1f); } else { div.Height = utils.ParseRelativeValue(heightValue, fontSize); } } } float?marginTop = null; float?marginBottom = null; foreach (KeyValuePair <String, String> entry in css) { String key = entry.Key; String value = entry.Value; if (Util.EqualsIgnoreCase(key, CSS.Property.LEFT)) { div.Left = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.RIGHT)) { if (div.Width == null || div.Left == null) { div.Right = utils.ParseValueToPt(value, fontSize); } } else if (Util.EqualsIgnoreCase(key, CSS.Property.TOP)) { div.Top = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.BOTTOM)) { if (div.Height == null || div.Top == null) { div.Bottom = utils.ParseValueToPt(value, fontSize); } } else if (Util.EqualsIgnoreCase(key, CSS.Property.BACKGROUND_COLOR)) { div.BackgroundColor = HtmlUtilities.DecodeColor(value); } else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_LEFT)) { div.PaddingLeft = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_RIGHT)) { div.PaddingRight = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_TOP)) { div.PaddingTop = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_BOTTOM)) { div.PaddingBottom = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.MARGIN_TOP)) { marginTop = utils.CalculateMarginTop(value, fontSize, memory); } else if (Util.EqualsIgnoreCase(key, CSS.Property.MARGIN_BOTTOM)) { marginBottom = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.FLOAT)) { if (Util.EqualsIgnoreCase(value, CSS.Value.LEFT)) { div.Float = PdfDiv.FloatType.LEFT; } else if (Util.EqualsIgnoreCase(value, CSS.Value.RIGHT)) { div.Float = PdfDiv.FloatType.RIGHT; } } else if (Util.EqualsIgnoreCase(key, CSS.Property.POSITION)) { if (Util.EqualsIgnoreCase(value, CSS.Value.ABSOLUTE)) { div.Position = PdfDiv.PositionType.ABSOLUTE; } else if (Util.EqualsIgnoreCase(value, CSS.Value.FIXED)) { div.Position = PdfDiv.PositionType.FIXED; } else if (Util.EqualsIgnoreCase(value, CSS.Value.RELATIVE)) { div.Position = PdfDiv.PositionType.RELATIVE; } } //TODO: border, background properties. } return(div); }
public override PdfDiv Apply(PdfDiv div, Tag t, IMarginMemory memory, IPageSizeContainable psc, HtmlPipelineContext context) { IDictionary<String, String> css = t.CSS; float fontSize = FontSizeTranslator.GetInstance().TranslateFontSize(t); if (fontSize == Font.UNDEFINED) { fontSize = FontSizeTranslator.DEFAULT_FONT_SIZE; } String align = null; if (t.Attributes.ContainsKey(HTML.Attribute.ALIGN)) { align = t.Attributes[HTML.Attribute.ALIGN]; } else if (css.ContainsKey(CSS.Property.TEXT_ALIGN)) { align = css[CSS.Property.TEXT_ALIGN]; } if (align != null) { div.TextAlignment = CSS.GetElementAlignment(align); } String widthValue; if (!css.TryGetValue(HTML.Attribute.WIDTH, out widthValue)) { t.Attributes.TryGetValue(HTML.Attribute.WIDTH, out widthValue); } if (widthValue != null) { float pageWidth = psc.PageSize.Width; if (utils.IsNumericValue(widthValue) || utils.IsMetricValue(widthValue)) { div.Width = Math.Min(pageWidth, utils.ParsePxInCmMmPcToPt(widthValue)); } else if (utils.IsRelativeValue(widthValue)) { if (widthValue.Contains(CSS.Value.PERCENTAGE)) { div.PercentageWidth = utils.ParseRelativeValue(widthValue, 1f); } else { div.Width = Math.Min(pageWidth, utils.ParseRelativeValue(widthValue, fontSize)); } } } String heightValue; if (!css.TryGetValue(HTML.Attribute.HEIGHT, out heightValue)) { t.Attributes.TryGetValue(HTML.Attribute.HEIGHT, out heightValue); } if (heightValue != null) { if (utils.IsNumericValue(heightValue) || utils.IsMetricValue(heightValue)) { div.Height = utils.ParsePxInCmMmPcToPt(heightValue); } else if (utils.IsRelativeValue(heightValue)) { if (heightValue.Contains(CSS.Value.PERCENTAGE)) { div.PercentageHeight = utils.ParseRelativeValue(heightValue, 1f); } else { div.Height = utils.ParseRelativeValue(heightValue, fontSize); } } } float? marginTop = null; float? marginBottom = null; foreach (KeyValuePair<String, String> entry in css) { String key = entry.Key; String value = entry.Value; if (Util.EqualsIgnoreCase(key, CSS.Property.LEFT)) { div.Left = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.RIGHT)) { if (div.Width == null || div.Left == null) { div.Right = utils.ParseValueToPt(value, fontSize); } } else if (Util.EqualsIgnoreCase(key, CSS.Property.TOP)) { div.Top = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.BOTTOM)) { if (div.Height == null || div.Top == null) { div.Bottom = utils.ParseValueToPt(value, fontSize); } } else if (Util.EqualsIgnoreCase(key, CSS.Property.BACKGROUND_COLOR)) { div.BackgroundColor = HtmlUtilities.DecodeColor(value); } else if (Util.EqualsIgnoreCase(key, CSS.Property.BACKGROUND_IMAGE)) { string url = utils.ExtractUrl(value); try { Image img = new ImageRetrieve(context.ResourcePath, context.GetImageProvider()).RetrieveImage(url); div.BackgroundImage = img; } catch (NoImageException e) { if (LOG.IsLogging(Level.ERROR)) { LOG.Error(string.Format(LocaleMessages.GetInstance().GetMessage("html.tag.img.failed"), url), e); } } } else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_LEFT)) { div.PaddingLeft = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_RIGHT)) { div.PaddingRight = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_TOP)) { div.PaddingTop = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_BOTTOM)) { div.PaddingBottom = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.MARGIN_TOP)) { marginTop = utils.CalculateMarginTop(value, fontSize, memory); } else if (Util.EqualsIgnoreCase(key, CSS.Property.MARGIN_BOTTOM)) { marginBottom = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.FLOAT)) { if (Util.EqualsIgnoreCase(value, CSS.Value.LEFT)) { div.Float = PdfDiv.FloatType.LEFT; } else if (Util.EqualsIgnoreCase(value, CSS.Value.RIGHT)) { div.Float = PdfDiv.FloatType.RIGHT; } } else if (Util.EqualsIgnoreCase(key, CSS.Property.POSITION)) { if (Util.EqualsIgnoreCase(value, CSS.Value.ABSOLUTE)) { div.Position = PdfDiv.PositionType.ABSOLUTE; } else if (Util.EqualsIgnoreCase(value, CSS.Value.FIXED)) { div.Position = PdfDiv.PositionType.FIXED; } else if (Util.EqualsIgnoreCase(value, CSS.Value.RELATIVE)) { div.Position = PdfDiv.PositionType.RELATIVE; } } else if (Util.EqualsIgnoreCase(key, CSS.Property.DISPLAY)) { if (Util.EqualsIgnoreCase(value, CSS.Value.BLOCK)) { div.Display = PdfDiv.DisplayType.BLOCK; } else if (Util.EqualsIgnoreCase(value, CSS.Value.INLINE)) { div.Display = PdfDiv.DisplayType.INLINE; } else if (Util.EqualsIgnoreCase(value, CSS.Value.INLINE_BLOCK)) { div.Display = PdfDiv.DisplayType.INLINE_BLOCK; } else if (Util.EqualsIgnoreCase(value, CSS.Value.INLINE_TABLE)) { div.Display = PdfDiv.DisplayType.INLINE_TABLE; } else if (Util.EqualsIgnoreCase(value, CSS.Value.LIST_ITEM)) { div.Display = PdfDiv.DisplayType.LIST_ITEM; } else if (Util.EqualsIgnoreCase(value, CSS.Value.NONE)) { div.Display = PdfDiv.DisplayType.NONE; } else if (Util.EqualsIgnoreCase(value, CSS.Value.RUN_IN)) { div.Display = PdfDiv.DisplayType.RUN_IN; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE)) { div.Display = PdfDiv.DisplayType.TABLE; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_CAPTION)) { div.Display = PdfDiv.DisplayType.TABLE_CAPTION; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_CELL)) { div.Display = PdfDiv.DisplayType.TABLE_CELL; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_COLUMN_GROUP)) { div.Display = PdfDiv.DisplayType.TABLE_COLUMN_GROUP; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_COLUMN)) { div.Display = PdfDiv.DisplayType.TABLE_COLUMN; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_FOOTER_GROUP)) { div.Display = PdfDiv.DisplayType.TABLE_FOOTER_GROUP; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_HEADER_GROUP)) { div.Display = PdfDiv.DisplayType.TABLE_HEADER_GROUP; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_ROW)) { div.Display = PdfDiv.DisplayType.TABLE_ROW; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_ROW_GROUP)) { div.Display = PdfDiv.DisplayType.TABLE_ROW_GROUP; } } else if (Util.EqualsIgnoreCase(CSS.Property.BORDER_TOP_STYLE, key)) { if (Util.EqualsIgnoreCase(CSS.Value.DOTTED, value)) { div.BorderStyle = PdfDiv.BorderTopStyle.DOTTED; } else if (Util.EqualsIgnoreCase(CSS.Value.DASHED, value)) { div.BorderStyle = PdfDiv.BorderTopStyle.DASHED; } else if (Util.EqualsIgnoreCase(CSS.Value.SOLID, value)) { div.BorderStyle = PdfDiv.BorderTopStyle.SOLID; } else if (Util.EqualsIgnoreCase(CSS.Value.DOUBLE, value)) { div.BorderStyle = PdfDiv.BorderTopStyle.DOUBLE; } else if (Util.EqualsIgnoreCase(CSS.Value.GROOVE, value)) { div.BorderStyle = PdfDiv.BorderTopStyle.GROOVE; } else if (Util.EqualsIgnoreCase(CSS.Value.RIDGE, value)) { div.BorderStyle = PdfDiv.BorderTopStyle.RIDGE; } else if (Util.EqualsIgnoreCase(value, CSS.Value.INSET)) { div.BorderStyle = PdfDiv.BorderTopStyle.INSET; } else if (Util.EqualsIgnoreCase(value, CSS.Value.OUTSET)) { div.BorderStyle = PdfDiv.BorderTopStyle.OUTSET; } } else if (Util.EqualsIgnoreCase(key, CSS.Property.PAGE_BREAK_INSIDE)) { if (Util.EqualsIgnoreCase(value, CSS.Value.AVOID)) { div.KeepTogether = true; } } //TODO: border, background properties. } return div; }
/* (non-Javadoc) * @see com.itextpdf.tool.xml.css.CssApplier#apply(com.itextpdf.text.Element, com.itextpdf.tool.xml.Tag) */ public Paragraph Apply(Paragraph p, Tag t, IMarginMemory configuration) { /*if (this.configuration.GetRootTags().Contains(t.Name)) { * m.SetLeading(t); * } else { * m.SetVariablesBasedOnChildren(t); * }*/ float fontSize = FontSizeTranslator.GetInstance().GetFontSize(t); float lmb = 0; bool hasLMB = false; IDictionary <String, String> css = t.CSS; foreach (KeyValuePair <String, String> entry in css) { String key = entry.Key; String value = entry.Value; if (Util.EqualsIgnoreCase(CSS.Property.MARGIN_TOP, key)) { p.SpacingBefore = p.SpacingBefore + utils.CalculateMarginTop(value, fontSize, configuration); } else if (Util.EqualsIgnoreCase(CSS.Property.PADDING_TOP, key)) { p.SpacingBefore = p.SpacingBefore + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.MARGIN_BOTTOM, key)) { float after = utils.ParseValueToPt(value, fontSize); p.SpacingAfter = p.SpacingAfter + after; lmb = after; hasLMB = true; } else if (Util.EqualsIgnoreCase(CSS.Property.PADDING_BOTTOM, key)) { p.SpacingAfter = p.SpacingAfter + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.MARGIN_LEFT, key)) { p.IndentationLeft = p.IndentationLeft + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.MARGIN_RIGHT, key)) { p.IndentationRight = p.IndentationRight + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.PADDING_LEFT, key)) { p.IndentationLeft = p.IndentationLeft + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.PADDING_RIGHT, key)) { p.IndentationRight = p.IndentationRight + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.TEXT_ALIGN, key)) { if (Util.EqualsIgnoreCase(CSS.Value.RIGHT, value)) { p.Alignment = Element.ALIGN_RIGHT; } else if (Util.EqualsIgnoreCase(CSS.Value.CENTER, value)) { p.Alignment = Element.ALIGN_CENTER; } else if (Util.EqualsIgnoreCase(CSS.Value.LEFT, value)) { p.Alignment = Element.ALIGN_LEFT; } else if (Util.EqualsIgnoreCase(CSS.Value.JUSTIFY, value)) { p.Alignment = Element.ALIGN_JUSTIFIED; } } else if (Util.EqualsIgnoreCase(CSS.Property.TEXT_INDENT, key)) { p.FirstLineIndent = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.LINE_HEIGHT, key)) { if (utils.IsNumericValue(value)) { p.Leading = float.Parse(value, CultureInfo.InvariantCulture) * fontSize; } else if (utils.IsRelativeValue(value)) { p.Leading = utils.ParseRelativeValue(value, fontSize); } else if (utils.IsMetricValue(value)) { p.Leading = utils.ParsePxInCmMmPcToPt(value); } } } // setDefaultMargin to largestFont if no margin-bottom is set and p-tag is child of the root tag. /*if (null != t.Parent) { * String parent = t.Parent.Name; * if (!css.ContainsKey(CSS.Property.MARGIN_TOP) && configuration.GetRootTags().Contains(parent)) { * p.SpacingBefore = p.SpacingBefore+utils.CalculateMarginTop(fontSize.ToString(CultureInfo.InvariantCulture)+"pt", 0, configuration); * } * if (!css.ContainsKey(CSS.Property.MARGIN_BOTTOM) && configuration.GetRootTags().Contains(parent)) { * p.SpacingAfter = p.SpacingAfter+fontSize; * css[CSS.Property.MARGIN_BOTTOM] = fontSize.ToString(CultureInfo.InvariantCulture)+"pt"; * lmb = fontSize; * hasLMB = true; * } * p.Leading = m.GetLargestLeading(); * if (p.Alignment == -1) { * p.Alignment = Element.ALIGN_LEFT; * } * }*/ if (hasLMB) { configuration.LastMarginBottom = lmb; } //TODO this only work around for applaying of font properties to paragraph Chunk dummy = new ChunkCssApplier().Apply(new Chunk("dummy"), t); p.Font = dummy.Font; return(p); }