Пример #1
0
        /**
         * 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);
        }
Пример #2
0
        /// <summary>Resolve the font size stored inside the passed renderer</summary>
        /// <param name="renderer">renderer containing the font size declaration</param>
        /// <param name="parentFontSize">parent font size to fall back on if the renderer does not contain a font size declarations or if the stored declaration is invalid
        ///     </param>
        /// <returns>float containing the font-size, or the parent font size if the renderer's declaration cannot be resolved
        ///     </returns>
        public static float ResolveFontSize(ISvgTextNodeRenderer renderer, float parentFontSize)
        {
            //Use own font-size declaration if it is present, parent's otherwise
            float  fontSize        = float.NaN;
            String elementFontSize = renderer.GetAttribute(SvgConstants.Attributes.FONT_SIZE);

            if (null != elementFontSize && !String.IsNullOrEmpty(elementFontSize))
            {
                if (CssUtils.IsRelativeValue(elementFontSize) || CommonCssConstants.LARGER.Equals(elementFontSize) || CommonCssConstants
                    .SMALLER.Equals(elementFontSize))
                {
                    // TODO DEVSIX-2866 Support rem value for svgs
                    fontSize = CssUtils.ParseRelativeFontSize(elementFontSize, parentFontSize);
                }
                else
                {
                    fontSize = CssUtils.ParseAbsoluteFontSize(elementFontSize, CommonCssConstants.PX);
                }
            }
            if ((float.IsNaN(fontSize)) || fontSize < 0f)
            {
                fontSize = parentFontSize;
            }
            return(fontSize);
        }
Пример #3
0
        /* (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);
        }
Пример #4
0
 /// <summary>Parses an absolute length.</summary>
 /// <param name="value">
 /// the absolute length as a
 /// <see cref="System.String"/>
 /// value
 /// </param>
 /// <returns>
 /// the absolute length as a
 /// <c>float</c>
 /// value
 /// </returns>
 private static float ParseAbsoluteLength(String value)
 {
     if (CssUtils.IsRelativeValue(value))
     {
         // TODO here should be used default font size of the browser, it probably should be fetched from the more generic place than private class constant
         return(CssUtils.ParseRelativeValue(value, DEFAULT_FONT_SIZE));
     }
     else
     {
         return(CssUtils.ParseAbsoluteLength(value));
     }
 }
Пример #5
0
        /**
         *
         * @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);
        }
Пример #6
0
 /// <summary>Checks if a string represents length value.</summary>
 /// <param name="pageSizeChunk">the string that possibly represents a length value</param>
 /// <returns>true, if the string represents a length value</returns>
 private static bool IsLengthValue(String pageSizeChunk)
 {
     return(CssUtils.IsMetricValue(pageSizeChunk) || CssUtils.IsRelativeValue(pageSizeChunk));
 }
Пример #7
0
        /**
         * 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);
        }
Пример #8
0
        /**
         *
         * @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>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(") || 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);
 }
Пример #10
0
        /* (non-Javadoc)
         * @see com.itextpdf.html2pdf.css.resolve.ICssResolver#resolveStyles(com.itextpdf.html2pdf.html.node.INode, com.itextpdf.html2pdf.css.resolve.CssContext)
         */
        private IDictionary <String, String> ResolveStyles(INode element, CssContext context)
        {
            IList <CssRuleSet> ruleSets = new List <CssRuleSet>();

            ruleSets.Add(new CssRuleSet(null, UserAgentCss.GetStyles(element)));
            if (element is IElementNode)
            {
                ruleSets.Add(new CssRuleSet(null, HtmlStylesToCssConverter.Convert((IElementNode)element)));
            }
            ruleSets.AddAll(cssStyleSheet.GetCssRuleSets(element, deviceDescription));
            if (element is IElementNode)
            {
                String styleAttribute = ((IElementNode)element).GetAttribute(AttributeConstants.STYLE);
                if (styleAttribute != null)
                {
                    ruleSets.Add(new CssRuleSet(null, CssRuleSetParser.ParsePropertyDeclarations(styleAttribute)));
                }
            }
            IDictionary <String, String> elementStyles = CssStyleSheet.ExtractStylesFromRuleSets(ruleSets);

            if (CssConstants.CURRENTCOLOR.Equals(elementStyles.Get(CssConstants.COLOR)))
            {
                // css-color-3/#currentcolor:
                // If the ‘currentColor’ keyword is set on the ‘color’ property itself, it is treated as ‘color: inherit’.
                elementStyles.Put(CssConstants.COLOR, CssConstants.INHERIT);
            }
            String parentFontSizeStr = null;

            if (element.ParentNode() is IStylesContainer)
            {
                IStylesContainer             parentNode   = (IStylesContainer)element.ParentNode();
                IDictionary <String, String> parentStyles = parentNode.GetStyles();
                if (parentStyles == null && !(element.ParentNode() is IDocumentNode))
                {
                    ILog logger = LogManager.GetLogger(typeof(iText.Html2pdf.Css.Resolve.DefaultCssResolver));
                    logger.Error(iText.Html2pdf.LogMessageConstant.ERROR_RESOLVING_PARENT_STYLES);
                }
                if (parentStyles != null)
                {
                    ICollection <IStyleInheritance> inheritanceRules = new HashSet <IStyleInheritance>();
                    inheritanceRules.Add(cssInheritance);
                    foreach (KeyValuePair <String, String> entry in parentStyles)
                    {
                        elementStyles = StyleUtil.MergeParentStyleDeclaration(elementStyles, entry.Key, entry.Value, parentStyles.
                                                                              Get(CommonCssConstants.FONT_SIZE), inheritanceRules);
                    }
                    parentFontSizeStr = parentStyles.Get(CssConstants.FONT_SIZE);
                }
            }
            String elementFontSize = elementStyles.Get(CssConstants.FONT_SIZE);

            if (CssUtils.IsRelativeValue(elementFontSize) || CssConstants.LARGER.Equals(elementFontSize) || CssConstants
                .SMALLER.Equals(elementFontSize))
            {
                float baseFontSize;
                if (CssUtils.IsRemValue(elementFontSize))
                {
                    baseFontSize = context.GetRootFontSize();
                }
                else
                {
                    if (parentFontSizeStr == null)
                    {
                        baseFontSize = CssUtils.ParseAbsoluteFontSize(CssDefaults.GetDefaultValue(CssConstants.FONT_SIZE));
                    }
                    else
                    {
                        baseFontSize = CssUtils.ParseAbsoluteLength(parentFontSizeStr);
                    }
                }
                float absoluteFontSize = CssUtils.ParseRelativeFontSize(elementFontSize, baseFontSize);
                // Format to 4 decimal places to prevent differences between Java and C#
                elementStyles.Put(CssConstants.FONT_SIZE, DecimalFormatUtil.FormatNumber(absoluteFontSize, "0.####") + CssConstants
                                  .PT);
            }
            else
            {
                elementStyles.Put(CssConstants.FONT_SIZE, Convert.ToString(CssUtils.ParseAbsoluteFontSize(elementFontSize)
                                                                           , System.Globalization.CultureInfo.InvariantCulture) + CssConstants.PT);
            }
            // Update root font size
            if (element is IElementNode && TagConstants.HTML.Equals(((IElementNode)element).Name()))
            {
                context.SetRootFontSize(elementStyles.Get(CssConstants.FONT_SIZE));
            }
            ICollection <String> keys = new HashSet <String>();

            foreach (KeyValuePair <String, String> entry in elementStyles)
            {
                if (CssConstants.INITIAL.Equals(entry.Value) || CssConstants.INHERIT.Equals(entry.Value))
                {
                    // if "inherit" is not resolved till now, parents don't have it
                    keys.Add(entry.Key);
                }
            }
            foreach (String key in keys)
            {
                elementStyles.Put(key, CssDefaults.GetDefaultValue(key));
            }
            // This is needed for correct resolving of content property, so doing it right here
            CounterProcessorUtil.ProcessCounters(elementStyles, context, element);
            ResolveContentProperty(elementStyles, element, context);
            return(elementStyles);
        }
        /* (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);
        }
Пример #12
0
        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;
        }
Пример #13
0
        /* (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);
        }
Пример #14
0
        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);
        }
Пример #15
0
        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);
        }
Пример #16
0
        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);
        }