예제 #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);
        }
 private void ApplyUserSpaceScaling(SvgDrawContext context)
 {
     if (!this.attributesAndStyles.ContainsKey(SvgConstants.Attributes.MARKER_UNITS) || SvgConstants.Values.STROKEWIDTH
         .Equals(this.attributesAndStyles.Get(SvgConstants.Attributes.MARKER_UNITS)))
     {
         String parentValue = this.GetParent().GetAttribute(SvgConstants.Attributes.STROKE_WIDTH);
         if (parentValue != null)
         {
             float strokeWidthScale;
             if (CssUtils.IsPercentageValue(parentValue))
             {
                 // If stroke width is a percentage value is always computed as a percentage of the normalized viewBox diagonal length.
                 double rootViewPortHeight    = context.GetRootViewPort().GetHeight();
                 double rootViewPortWidth     = context.GetRootViewPort().GetWidth();
                 double viewBoxDiagonalLength = Math.Sqrt(rootViewPortHeight * rootViewPortHeight + rootViewPortWidth * rootViewPortWidth
                                                          );
                 strokeWidthScale = CssUtils.ParseRelativeValue(parentValue, (float)viewBoxDiagonalLength);
             }
             else
             {
                 strokeWidthScale = SvgCssUtils.ConvertPtsToPx(ParseFontRelativeOrAbsoluteLengthOnMarker(parentValue));
             }
             context.GetCurrentCanvas().ConcatMatrix(AffineTransform.GetScaleInstance(strokeWidthScale, strokeWidthScale
                                                                                      ));
         }
     }
 }
예제 #3
0
        virtual public void ParseXfaOnlyXML()
        {
            TextReader bis    = File.OpenText(RESOURCE_TEST_PATH + SNIPPETS + TEST + "snippet.html");
            Document   doc    = new Document(PageSize.A4);
            float      margin = utils.ParseRelativeValue("10%", PageSize.A4.Width);

            doc.SetMargins(margin, margin, margin, margin);
            PdfWriter writer = null;

            try {
                writer = PdfWriter.GetInstance(doc, new FileStream(
                                                   TARGET + TEST + "Test.pdf", FileMode.Create));
            }
            catch (DocumentException e) {
                Console.WriteLine(e);
            }
            CssFilesImpl cssFiles = new CssFilesImpl();

            cssFiles.Add(XMLWorkerHelper.GetInstance().GetDefaultCSS());
            StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles);
            HtmlPipelineContext  hpc         = new HtmlPipelineContext(null);

            hpc.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(Tags.GetHtmlTagProcessorFactory());
            IPipeline pipeline = new CssResolverPipeline(cssResolver,
                                                         new HtmlPipeline(hpc, new PdfWriterPipeline(doc, writer)));
            XMLWorker worker = new XMLWorker(pipeline, true);

            doc.Open();
            XMLParser p = new XMLParser(true, worker);

            p.Parse(bis);
            doc.Close();
        }
예제 #4
0
        /// <summary>Calculates the text rise value for &lt;sup&gt; and &lt;sub&gt; tags.</summary>
        /// <param name="stylesContainer">the styles container</param>
        /// <param name="vAlignVal">the vertical alignment value</param>
        /// <returns>the calculated text rise</returns>
        private static float CalcTextRiseForSupSub(IStylesContainer stylesContainer, String vAlignVal)
        {
            float  parentFontSize      = GetParentFontSize(stylesContainer);
            String superscriptPosition = "33%";
            String subscriptPosition   = "-20%";
            String relativeValue       = CssConstants.SUPER.Equals(vAlignVal) ? superscriptPosition : subscriptPosition;

            return(CssUtils.ParseRelativeValue(relativeValue, parentFontSize));
        }
예제 #5
0
        /// <summary>Calculates text rise for percentage value text rise.</summary>
        /// <param name="stylesContainer">the styles container</param>
        /// <param name="rootFontSize">the root font size</param>
        /// <param name="vAlignVal">the vertical alignment value</param>
        /// <returns>the calculated text rise</returns>
        private static float CalcTextRiseForPercentageValue(IStylesContainer stylesContainer, float rootFontSize,
                                                            String vAlignVal)
        {
            String ownFontSizeStr        = stylesContainer.GetStyles().Get(CssConstants.FONT_SIZE);
            float  fontSize              = CssUtils.ParseAbsoluteLength(ownFontSizeStr);
            String lineHeightStr         = stylesContainer.GetStyles().Get(CssConstants.LINE_HEIGHT);
            float  lineHeightActualValue = GetLineHeightActualValue(fontSize, rootFontSize, lineHeightStr);

            return(CssUtils.ParseRelativeValue(vAlignVal, lineHeightActualValue));
        }
예제 #6
0
        /// <summary>Creates a scale transformation.</summary>
        /// <param name="values">values of the transformation</param>
        /// <returns>AffineTransform for the scale operation</returns>
        private static AffineTransform CreateScaleTransformation(IList <String> values)
        {
            if (values.Count == 0 || values.Count > 2)
            {
                throw new SvgProcessingException(SvgLogMessageConstant.TRANSFORM_INCORRECT_NUMBER_OF_VALUES);
            }
            float scaleX = CssUtils.ParseRelativeValue(values[0], 1);
            float scaleY = values.Count == 2 ? CssUtils.ParseRelativeValue(values[1], 1) : scaleX;

            return(AffineTransform.GetScaleInstance(scaleX, scaleY));
        }
예제 #7
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));
     }
 }
        // TODO (DEVSIX-3596) Add support of 'lh' 'ch' units and viewport-relative units
        private float ParseFontRelativeOrAbsoluteLengthOnMarker(String length)
        {
            float value = 0f;

            if (CssUtils.IsMetricValue(length) || CssUtils.IsNumericValue(length))
            {
                value = CssUtils.ParseAbsoluteLength(length);
            }
            else
            {
                if (CssUtils.IsFontRelativeValue(length))
                {
                    // Defaut font-size is medium
                    value = CssUtils.ParseRelativeValue(length, CssUtils.ParseAbsoluteFontSize(CommonCssConstants.MEDIUM));
                    // Different browsers process font-relative units for markers differently.
                    // We do it according to the css specification.
                    if (CssUtils.IsRemValue(length))
                    {
                        ISvgNodeRenderer rootElement = GetSvgRootElement(GetParent());
                        if (rootElement != null && rootElement.GetAttribute(CommonCssConstants.FONT_SIZE) != null)
                        {
                            value = CssUtils.ParseRelativeValue(length, CssUtils.ParseAbsoluteFontSize(rootElement.GetAttribute(CommonCssConstants
                                                                                                                                .FONT_SIZE)));
                        }
                    }
                    else
                    {
                        if (CssUtils.IsEmValue(length))
                        {
                            ISvgNodeRenderer parentElement = this.GetParent();
                            if (parentElement != null && parentElement.GetAttribute(CommonCssConstants.FONT_SIZE) != null)
                            {
                                value = CssUtils.ParseRelativeValue(length, CssUtils.ParseAbsoluteFontSize(parentElement.GetAttribute(CommonCssConstants
                                                                                                                                      .FONT_SIZE)));
                            }
                        }
                        else
                        {
                            if (CssUtils.IsExValue(length))
                            {
                                if (this.GetAttribute(CommonCssConstants.FONT_SIZE) != null)
                                {
                                    value = CssUtils.ParseRelativeValue(length, CssUtils.ParseAbsoluteFontSize(this.GetAttribute(CommonCssConstants
                                                                                                                                 .FONT_SIZE)));
                                }
                            }
                        }
                    }
                }
            }
            return(value);
        }
        private void ApplyCoordinatesTranslation(SvgDrawContext context)
        {
            float xScale = 1;
            float yScale = 1;

            if (this.attributesAndStyles.ContainsKey(SvgConstants.Attributes.VIEWBOX))
            {
                //Parse viewbox parameters stuff
                String         viewBoxValues = attributesAndStyles.Get(SvgConstants.Attributes.VIEWBOX);
                IList <String> valueStrings  = SvgCssUtils.SplitValueList(viewBoxValues);
                float[]        viewBox       = GetViewBoxValues();
                xScale = context.GetCurrentViewPort().GetWidth() / viewBox[2];
                yScale = context.GetCurrentViewPort().GetHeight() / viewBox[3];
            }
            float moveX = DEFAULT_REF_X;

            if (this.attributesAndStyles.ContainsKey(SvgConstants.Attributes.REFX))
            {
                String refX = this.attributesAndStyles.Get(SvgConstants.Attributes.REFX);
                if (CssUtils.IsPercentageValue(refX))
                {
                    moveX = CssUtils.ParseRelativeValue(refX, context.GetRootViewPort().GetWidth());
                }
                else
                {
                    moveX = ParseFontRelativeOrAbsoluteLengthOnMarker(refX);
                }
                //Apply scale
                moveX *= -1 * xScale;
            }
            float moveY = DEFAULT_REF_Y;

            if (this.attributesAndStyles.ContainsKey(SvgConstants.Attributes.REFY))
            {
                String refY = this.attributesAndStyles.Get(SvgConstants.Attributes.REFY);
                if (CssUtils.IsPercentageValue(refY))
                {
                    moveY = CssUtils.ParseRelativeValue(refY, context.GetRootViewPort().GetHeight());
                }
                else
                {
                    moveY = ParseFontRelativeOrAbsoluteLengthOnMarker(refY);
                }
                moveY *= -1 * yScale;
            }
            AffineTransform translation = AffineTransform.GetTranslateInstance(moveX, moveY);

            if (!translation.IsIdentity())
            {
                context.GetCurrentCanvas().ConcatMatrix(translation);
            }
        }
예제 #10
0
        public void ResolveIndentations()
        {
            CssUtils cssUtils = CssUtils.GetInstance();

            Assert.AreEqual(cssUtils.ParseRelativeValue("1em", 12), ((Paragraph)elementList[0]).SpacingBefore, 0);
            Assert.AreEqual(cssUtils.ParsePxInCmMmPcToPt("1.5in"), ((Paragraph)elementList[1]).IndentationLeft, 0);
            Assert.AreEqual(cssUtils.ParsePxInCmMmPcToPt("3cm"), ((Paragraph)elementList[2]).IndentationRight, 0);
            Assert.AreEqual(cssUtils.ParsePxInCmMmPcToPt("5pc") - 12, ((Paragraph)elementList[3]).SpacingBefore, 0);
            Assert.AreEqual(cssUtils.ParsePxInCmMmPcToPt("4pc"), ((Paragraph)elementList[3]).SpacingAfter, 0);
            Assert.AreEqual(cssUtils.ParsePxInCmMmPcToPt("80px") - cssUtils.ParsePxInCmMmPcToPt("4pc"),
                            ((Paragraph)elementList[4]).SpacingBefore, 0);
            Assert.AreEqual(cssUtils.ParsePxInCmMmPcToPt("80px"), ((Paragraph)elementList[4]).SpacingAfter, 0);
        }
예제 #11
0
 /// <summary>Parses the relative font size.</summary>
 /// <param name="relativeFontSizeValue">
 /// the relative font size value as a
 /// <see cref="System.String"/>
 /// </param>
 /// <param name="baseValue">the base value</param>
 /// <returns>
 /// the relative font size value as a
 /// <c>float</c>
 /// </returns>
 public static float ParseRelativeFontSize(String relativeFontSizeValue, float baseValue)
 {
     if (CssConstants.SMALLER.Equals(relativeFontSizeValue))
     {
         return((float)(baseValue / 1.2));
     }
     else
     {
         if (CssConstants.LARGER.Equals(relativeFontSizeValue))
         {
             return((float)(baseValue * 1.2));
         }
     }
     return(CssUtils.ParseRelativeValue(relativeFontSizeValue, baseValue));
 }
예제 #12
0
        /// <summary>Evaluates the stop color offset value</summary>
        /// <returns>the stop color offset value in [0, 1] range</returns>
        public virtual double GetOffset()
        {
            double?offset          = null;
            String offsetAttribute = GetAttribute(SvgConstants.Attributes.OFFSET);

            if (CssUtils.IsPercentageValue(offsetAttribute))
            {
                offset = (double)CssUtils.ParseRelativeValue(offsetAttribute, 1);
            }
            else
            {
                if (CssUtils.IsNumericValue(offsetAttribute))
                {
                    offset = CssUtils.ParseDouble(offsetAttribute);
                }
            }
            double result = offset != null ? offset.Value : 0d;

            return(result > 1d ? 1d : result > 0d ? result : 0d);
        }
예제 #13
0
        /// <summary>Merge parent style declarations for passed styleProperty into passed style map</summary>
        /// <param name="styles">the styles map</param>
        /// <param name="styleProperty">the style property</param>
        /// <param name="parentPropValue">the parent properties value</param>
        /// <param name="parentFontSizeString">the parent font-size for resolving relative, font-dependent attributes</param>
        public virtual void MergeParentStyleDeclaration(IDictionary <String, String> styles, String styleProperty,
                                                        String parentPropValue, String parentFontSizeString)
        {
            String childPropValue = styles.Get(styleProperty);

            if ((childPropValue == null && CheckInheritance(styleProperty)) || CommonCssConstants.INHERIT.Equals(childPropValue
                                                                                                                 ))
            {
                if (ValueIsOfMeasurement(parentPropValue, CommonCssConstants.EM) || ValueIsOfMeasurement(parentPropValue,
                                                                                                         CommonCssConstants.EX) || (ValueIsOfMeasurement(parentPropValue, CommonCssConstants.PERCENTAGE) && fontSizeDependentPercentage
                                                                                                                                    .Contains(styleProperty)))
                {
                    float absoluteParentFontSize = CssUtils.ParseAbsoluteLength(parentFontSizeString);
                    // Format to 4 decimal places to prevent differences between Java and C#
                    styles.Put(styleProperty, DecimalFormatUtil.FormatNumber(CssUtils.ParseRelativeValue(parentPropValue, absoluteParentFontSize
                                                                                                         ), "0.####") + CommonCssConstants.PT);
                }
                else
                {
                    //Property is inherited, add to element style declarations
                    styles.Put(styleProperty, parentPropValue);
                }
            }
            else
            {
                if (CommonCssConstants.TEXT_DECORATION.Equals(styleProperty) && !CommonCssConstants.INLINE_BLOCK.Equals(styles
                                                                                                                        .Get(CommonCssConstants.DISPLAY)))
                {
                    // TODO Note! This property is formally not inherited, but the browsers behave very similar to inheritance here.

                    /* Text decorations on inline boxes are drawn across the entire element,
                     * going across any descendant elements without paying any attention to their presence. */
                    // Also, when, for example, parent element has text-decoration:underline, and the child text-decoration:overline,
                    // then the text in the child will be both overline and underline. This is why the declarations are merged
                    // See TextDecorationTest#textDecoration01Test
                    styles.Put(styleProperty, CssPropertyMerger.MergeTextDecoration(childPropValue, parentPropValue));
                }
            }
        }
 /// <summary>Parse absolute length.</summary>
 /// <param name="length">
 ///
 /// <see cref="System.String"/>
 /// for parsing
 /// </param>
 /// <param name="percentRelativeValue">the value on which percent length is based on</param>
 /// <param name="defaultValue">default value if length is not recognized</param>
 /// <param name="context">
 /// current
 /// <see cref="iText.Svg.Renderers.SvgDrawContext"/>
 /// </param>
 /// <returns>absolute value in points</returns>
 protected internal virtual float ParseAbsoluteLength(String length, float percentRelativeValue, float defaultValue
                                                      , SvgDrawContext context)
 {
     if (CssUtils.IsPercentageValue(length))
     {
         return(CssUtils.ParseRelativeValue(length, percentRelativeValue));
     }
     else
     {
         float     em        = GetCurrentFontSize();
         float     rem       = context.GetRemValue();
         UnitValue unitValue = CssUtils.ParseLengthValueToPt(length, em, rem);
         if (unitValue != null && unitValue.IsPointValue())
         {
             return(unitValue.GetValue());
         }
         else
         {
             return(defaultValue);
         }
     }
 }
예제 #15
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);
        }
예제 #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);
        }
예제 #17
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);
        }
예제 #18
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);
        }
예제 #19
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);
        }
예제 #20
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);
        }
예제 #21
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);
        }
예제 #22
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;
        }