コード例 #1
0
 virtual public void ValidateMetricValue()
 {
     Assert.AreEqual(true, css.IsMetricValue("px"));
     Assert.AreEqual(true, css.IsMetricValue("in"));
     Assert.AreEqual(true, css.IsMetricValue("cm"));
     Assert.AreEqual(true, css.IsMetricValue("mm"));
     Assert.AreEqual(true, css.IsMetricValue("pc"));
     Assert.AreEqual(false, css.IsMetricValue("em"));
     Assert.AreEqual(false, css.IsMetricValue("ex"));
     Assert.AreEqual(true, css.IsMetricValue("pt"));
     Assert.AreEqual(true, css.IsMetricValue("inch"));
     Assert.AreEqual(false, css.IsMetricValue("m"));
 }
コード例 #2
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);
        }
コード例 #3
0
 /// <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);
 }
コード例 #4
0
            /* (non-Javadoc)
             * @see com.itextpdf.html2pdf.css.resolve.HtmlStylesToCssConverter.IAttributeConverter#convert(com.itextpdf.styledxmlparser.html.node.IElementNode, java.lang.String)
             */
            public virtual IList <CssDeclaration> Convert(IElementNode element, String value)
            {
                // Trim semicolons at the end because they seem to not affect the value in browsers
                String cssEquivalent = iText.IO.Util.StringUtil.ReplaceAll(value, ";+$", "");

                if (!CssUtils.IsMetricValue(cssEquivalent) && !cssEquivalent.EndsWith(CssConstants.PERCENTAGE))
                {
                    cssEquivalent += CssConstants.PX;
                }
                return(JavaUtil.ArraysAsList(new CssDeclaration(CssConstants.HEIGHT, cssEquivalent)));
            }
コード例 #5
0
        // 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);
        }
コード例 #6
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);
        }
コード例 #7
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));
 }
コード例 #8
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);
        }
コード例 #9
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);
        }
コード例 #10
0
        /* (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);
        }
コード例 #11
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);
        }
コード例 #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
        /// <summary>Apply vertical alignment to inline elements.</summary>
        /// <param name="cssProps">the CSS properties</param>
        /// <param name="context">the processor context</param>
        /// <param name="stylesContainer">the styles container</param>
        /// <param name="childElements">the child elements</param>
        public static void ApplyVerticalAlignmentForInlines(IDictionary <String, String> cssProps, ProcessorContext
                                                            context, IStylesContainer stylesContainer, IList <IPropertyContainer> childElements)
        {
            String vAlignVal = cssProps.Get(CssConstants.VERTICAL_ALIGN);

            if (vAlignVal != null)
            {
                // TODO DEVSIX-1750 for inline images and tables (inline-blocks) v-align is not supported
                float textRise = 0;
                // TODO DEVSIX-3757 'top' and 'bottom' values are not supported;
                // 'top' and 'bottom' require information of actual line height, therefore should be applied at layout level;
                // 'sub', 'super' calculations are based on the behaviour of the common browsers (+33% and -20% shift accordingly from the parent's font size);
                // 'middle', 'text-top', 'text-bottom' calculations are based on the approximate assumptions that x-height is 0.5 of the font size
                // and descender and ascender heights are 0.2 and 0.8 of the font size accordingly.
                if (CssConstants.SUB.Equals(vAlignVal) || CssConstants.SUPER.Equals(vAlignVal))
                {
                    textRise = CalcTextRiseForSupSub(stylesContainer, vAlignVal);
                }
                else
                {
                    if (CssConstants.MIDDLE.Equals(vAlignVal))
                    {
                        textRise = CalcTextRiseForMiddle(stylesContainer);
                    }
                    else
                    {
                        if (CssConstants.TEXT_TOP.Equals(vAlignVal))
                        {
                            textRise = CalcTextRiseForTextTop(stylesContainer, context.GetCssContext().GetRootFontSize());
                        }
                        else
                        {
                            if (CssConstants.TEXT_BOTTOM.Equals(vAlignVal))
                            {
                                textRise = CalcTextRiseForTextBottom(stylesContainer, context.GetCssContext().GetRootFontSize());
                            }
                            else
                            {
                                if (CssUtils.IsMetricValue(vAlignVal))
                                {
                                    textRise = CssUtils.ParseAbsoluteLength(vAlignVal);
                                }
                                else
                                {
                                    if (vAlignVal.EndsWith(CssConstants.PERCENTAGE))
                                    {
                                        textRise = CalcTextRiseForPercentageValue(stylesContainer, context.GetCssContext().GetRootFontSize(), vAlignVal
                                                                                  );
                                    }
                                }
                            }
                        }
                    }
                }
                if (textRise != 0)
                {
                    foreach (IPropertyContainer element in childElements)
                    {
                        if (element is Text)
                        {
                            float?effectiveTr = element.GetProperty <float?>(Property.TEXT_RISE);
                            if (effectiveTr != null)
                            {
                                effectiveTr += textRise;
                            }
                            else
                            {
                                effectiveTr = textRise;
                            }
                            element.SetProperty(Property.TEXT_RISE, effectiveTr);
                        }
                        else
                        {
                            if (element is IBlockElement)
                            {
                                break;
                            }
                        }
                    }
                }
            }
        }
コード例 #15
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);
        }
コード例 #16
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);
        }
コード例 #17
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);
        }