private static void ApplyBackgroundColor(String backgroundColorStr, IPropertyContainer element, BackgroundBox clip) { if (backgroundColorStr != null && !CssConstants.TRANSPARENT.Equals(backgroundColorStr)) { float[] rgbaColor = CssDimensionParsingUtils.ParseRgbaColor(backgroundColorStr); Color color = new DeviceRgb(rgbaColor[0], rgbaColor[1], rgbaColor[2]); float opacity = rgbaColor[3]; Background backgroundColor = new Background(color, opacity, clip); element.SetProperty(Property.BACKGROUND, backgroundColor); } }
/// <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 = CssDimensionParsingUtils.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 = CssDimensionParsingUtils.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); bool textWrappingDisabled = CssConstants.NOWRAP.Equals(whiteSpace) || CssConstants.PRE.Equals(whiteSpace); element.SetProperty(Property.NO_SOFT_WRAP_INLINE, textWrappingDisabled); if (!textWrappingDisabled) { String overflowWrap = cssProps.Get(CssConstants.OVERFLOW_WRAP); if (CssConstants.ANYWHERE.Equals(overflowWrap)) { element.SetProperty(Property.OVERFLOW_WRAP, OverflowWrapPropertyValue.ANYWHERE); } else { if (CssConstants.BREAK_WORD.Equals(overflowWrap)) { element.SetProperty(Property.OVERFLOW_WRAP, OverflowWrapPropertyValue.BREAK_WORD); } else { element.SetProperty(Property.OVERFLOW_WRAP, OverflowWrapPropertyValue.NORMAL); } } String wordBreak = cssProps.Get(CssConstants.WORD_BREAK); if (CssConstants.BREAK_ALL.Equals(wordBreak)) { element.SetProperty(Property.SPLIT_CHARACTERS, new BreakAllSplitCharacters()); } else { if (CssConstants.KEEP_ALL.Equals(wordBreak)) { element.SetProperty(Property.SPLIT_CHARACTERS, new KeepAllSplitCharacters()); } else { if (CssConstants.BREAK_WORD.Equals(wordBreak)) { // CSS specification cite that describes the reason for overflow-wrap overriding: // "For compatibility with legacy content, the word-break property also supports // a deprecated break-word keyword. When specified, this has the same effect // as word-break: normal and overflow-wrap: anywhere, regardless of the actual value // of the overflow-wrap property." element.SetProperty(Property.OVERFLOW_WRAP, OverflowWrapPropertyValue.BREAK_WORD); element.SetProperty(Property.SPLIT_CHARACTERS, new DefaultSplitCharacters()); } else { element.SetProperty(Property.SPLIT_CHARACTERS, new DefaultSplitCharacters()); } } } } float[] colors = new float[4]; Color textDecorationColor; float opacity_1 = 1f; String textDecorationColorProp = cssProps.Get(CssConstants.TEXT_DECORATION_COLOR); if (textDecorationColorProp == null || CssConstants.CURRENTCOLOR.Equals(textDecorationColorProp)) { if (element.GetProperty <TransparentColor>(Property.FONT_COLOR) != null) { TransparentColor transparentColor = element.GetProperty <TransparentColor>(Property.FONT_COLOR); textDecorationColor = transparentColor.GetColor(); opacity_1 = transparentColor.GetOpacity(); } else { textDecorationColor = ColorConstants.BLACK; } } else { if (textDecorationColorProp.StartsWith("hsl")) { logger.Error(iText.Html2pdf.LogMessageConstant.HSL_COLOR_NOT_SUPPORTED); textDecorationColor = ColorConstants.BLACK; } else { colors = CssDimensionParsingUtils.ParseRgbaColor(textDecorationColorProp); textDecorationColor = new DeviceRgb(colors[0], colors[1], colors[2]); opacity_1 = colors[3]; } } String textDecorationLineProp = cssProps.Get(CssConstants.TEXT_DECORATION_LINE); if (textDecorationLineProp != null) { String[] textDecorationLines = iText.IO.Util.StringUtil.Split(textDecorationLineProp, "\\s+"); IList <Underline> underlineList = new List <Underline>(); foreach (String textDecorationLine in textDecorationLines) { if (CssConstants.BLINK.Equals(textDecorationLine)) { logger.Error(iText.Html2pdf.LogMessageConstant.TEXT_DECORATION_BLINK_NOT_SUPPORTED); } else { if (CssConstants.LINE_THROUGH.Equals(textDecorationLine)) { underlineList.Add(new Underline(textDecorationColor, opacity_1, .75f, 0, 0, 1 / 4f, PdfCanvasConstants.LineCapStyle .BUTT)); } else { if (CssConstants.OVERLINE.Equals(textDecorationLine)) { underlineList.Add(new Underline(textDecorationColor, opacity_1, .75f, 0, 0, 9 / 10f, PdfCanvasConstants.LineCapStyle .BUTT)); } else { if (CssConstants.UNDERLINE.Equals(textDecorationLine)) { underlineList.Add(new Underline(textDecorationColor, opacity_1, .75f, 0, 0, -1 / 10f, PdfCanvasConstants.LineCapStyle .BUTT)); } else { if (CssConstants.NONE.Equals(textDecorationLine)) { 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 = CssDimensionParsingUtils.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 && !CssConstants.NORMAL.Equals(letterSpacing)) { UnitValue letterSpacingValue = CssDimensionParsingUtils.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 = CssDimensionParsingUtils.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); SetLineHeight(element, lineHeight, em, rem); SetLineHeightByLeading(element, lineHeight, em, rem); }
/// <summary> /// Creates a /// <see cref="iText.Layout.Borders.Border"/> /// instance based on specific properties. /// </summary> /// <param name="outlineWidth">the outline width</param> /// <param name="outlineStyle">the outline style</param> /// <param name="outlineColor">the outline color</param> /// <param name="em">the em value</param> /// <param name="rem">the root em value</param> /// <returns>the border</returns> public static Border GetCertainBorder(String outlineWidth, String outlineStyle, String outlineColor, float em, float rem) { if (outlineStyle == null || CssConstants.NONE.Equals(outlineStyle)) { return(null); } if (outlineWidth == null) { outlineWidth = CssDefaults.GetDefaultValue(CssConstants.OUTLINE_WIDTH); } float outlineWidthValue; if (CssConstants.BORDER_WIDTH_VALUES.Contains(outlineWidth)) { if (CssConstants.THIN.Equals(outlineWidth)) { outlineWidth = "1px"; } else { if (CssConstants.MEDIUM.Equals(outlineWidth)) { outlineWidth = "2px"; } else { if (CssConstants.THICK.Equals(outlineWidth)) { outlineWidth = "3px"; } } } } UnitValue unitValue = CssDimensionParsingUtils.ParseLengthValueToPt(outlineWidth, em, rem); if (unitValue == null) { return(null); } if (unitValue.IsPercentValue()) { LOGGER.Error("outline-width in percents is not supported"); return(null); } outlineWidthValue = unitValue.GetValue(); Border outline = null; if (outlineWidthValue > 0) { DeviceRgb color = (DeviceRgb)ColorConstants.BLACK; float opacity = 1f; if (outlineColor != null) { if (!CssConstants.TRANSPARENT.Equals(outlineColor)) { float[] rgbaColor = CssDimensionParsingUtils.ParseRgbaColor(outlineColor); color = new DeviceRgb(rgbaColor[0], rgbaColor[1], rgbaColor[2]); opacity = rgbaColor[3]; } else { opacity = 0f; } } else { if (CssConstants.GROOVE.Equals(outlineStyle) || CssConstants.RIDGE.Equals(outlineStyle) || CssConstants.INSET .Equals(outlineStyle) || CssConstants.OUTSET.Equals(outlineStyle)) { color = new DeviceRgb(212, 208, 200); } } switch (outlineStyle) { case CssConstants.SOLID: case CssConstants.AUTO: { outline = new SolidBorder(color, outlineWidthValue, opacity); break; } case CssConstants.DASHED: { outline = new DashedBorder(color, outlineWidthValue, opacity); break; } case CssConstants.DOTTED: { outline = new DottedBorder(color, outlineWidthValue, opacity); break; } case CssConstants.DOUBLE: { outline = new DoubleBorder(color, outlineWidthValue, opacity); break; } case CssConstants.GROOVE: { outline = new GrooveBorder(color, outlineWidthValue, opacity); break; } case CssConstants.RIDGE: { outline = new RidgeBorder(color, outlineWidthValue, opacity); break; } case CssConstants.INSET: { outline = new InsetBorder(color, outlineWidthValue, opacity); break; } case CssConstants.OUTSET: { outline = new OutsetBorder(color, outlineWidthValue, opacity); break; } default: { outline = null; break; } } } return(outline); }