/**
  * Construct a new {@link StyleAttrCSSResolver} with default settings.
  */
 public StyleAttrSvgCSSResolver()
     : this(new CssFilesImpl(), CssUtils.GetInstance())
 {
 }
 /**
  * Construct a new StyleAttrCSSResolver with the given {@link CssFiles} and the {@link DefaultCssInheritanceRules}.
  *
  * @param cssFiles a {@link CssFiles} implementation.
  */
 public StyleAttrSvgCSSResolver(ICssFiles cssFiles)
     : this(cssFiles, CssUtils.GetInstance())
 {
 }
Пример #3
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);
        }
 /**
  * @param cssFiles the {@link CssFile} implementation
  * @param r the {@link FileRetrieve} implementation
  */
 public StyleAttrSvgCSSResolver(ICssFiles cssFiles, IFileRetrieve r)
     : this(new DefaultCssInheritanceRules(), cssFiles, CssUtils.GetInstance(), r)
 {
 }
Пример #5
0
        /* (non-Javadoc)
         * @see com.itextpdf.tool.xml.css.CssApplier#apply(com.itextpdf.text.Element, com.itextpdf.tool.xml.Tag)
         */
        virtual public LineSeparator Apply(LineSeparator ls, Tag t, IPageSizeContainable psc)
        {
            float lineWidth = 1;
            IDictionary <String, String> css = t.CSS;

            if (t.Attributes.ContainsKey(HTML.Attribute.SIZE))
            {
                lineWidth = CssUtils.GetInstance().ParsePxInCmMmPcToPt(t.Attributes[HTML.Attribute.SIZE]);
            }
            else if (css.ContainsKey(CSS.Property.HEIGHT))
            {
                lineWidth = CssUtils.GetInstance().ParsePxInCmMmPcToPt(css[CSS.Property.HEIGHT]);
            }
            ls.LineWidth = lineWidth;
            BaseColor lineColor = BaseColor.BLACK;

            if (t.Attributes.ContainsKey(CSS.Property.COLOR))
            {
                lineColor = HtmlUtilities.DecodeColor(t.Attributes[CSS.Property.COLOR]);
            }
            else if (css.ContainsKey(CSS.Property.COLOR))
            {
                lineColor = HtmlUtilities.DecodeColor(css[CSS.Property.COLOR]);
            }
            else if (css.ContainsKey(CSS.Property.BACKGROUND_COLOR))
            {
                lineColor = HtmlUtilities.DecodeColor(css[CSS.Property.BACKGROUND_COLOR]);
            }
            ls.LineColor = lineColor;
            float  percentage = 100;
            String widthStr;

            css.TryGetValue(CSS.Property.WIDTH, out widthStr);
            if (widthStr == null)
            {
                t.Attributes.TryGetValue(CSS.Property.WIDTH, out widthStr);
            }
            if (widthStr != null)
            {
                if (widthStr.Contains("%"))
                {
                    percentage = float.Parse(widthStr.Replace("%", ""), CultureInfo.InvariantCulture);
                }
                else
                {
                    percentage = (CssUtils.GetInstance().ParsePxInCmMmPcToPt(widthStr) / psc.PageSize.Width) * 100;
                }
            }
            ls.Percentage = percentage;
            String align;

            t.Attributes.TryGetValue(HTML.Attribute.ALIGN, out align);
            if (CSS.Value.RIGHT.Equals(align))
            {
                ls.Alignment = Element.ALIGN_RIGHT;
            }
            else if (CSS.Value.LEFT.Equals(align))
            {
                ls.Alignment = Element.ALIGN_LEFT;
            }
            else if (CSS.Value.CENTER.Equals(align))
            {
                ls.Alignment = Element.ALIGN_CENTER;
            }
            return(ls);
        }
Пример #6
0
 virtual public void SetUp()
 {
     LoggerFactory.GetInstance().SetLogger(new SysoLogger(3));
     css = CssUtils.GetInstance();
     str = "  een  twee   drie    vier    een  twee   drie    vier";
 }
Пример #7
0
        /*
         * (non-Javadoc)
         *
         * @see
         * com.itextpdf.tool.xml.pipeline.AbstractPipeline#open(com.itextpdf.tool
         * .xml.Tag, com.itextpdf.tool.xml.pipeline.ProcessObject)
         */
        public override IPipeline Open(IWorkerContext context, Tag t, ProcessObject po)
        {
            try {
                String tagName = t.Name;
                if (tag.Equals(tagName))
                {
                    MapContext cc;
                    cc = (MapContext)context.Get(typeof(PdfWriterPipeline).FullName);
                    Document d = new Document(pagesize);
                    try {
                        Stream os = fm.GetStream();
                        cc[PdfWriterPipeline.DOCUMENT] = d;
                        cc[PdfWriterPipeline.WRITER]   = PdfWriter.GetInstance(d, os);
                    } catch (IOException e) {
                        throw new PipelineException(e);
                    } catch (DocumentException e) {
                        throw new PipelineException(e);
                    }
                }
                if (Util.EqualsIgnoreCase(t.Name, opentag))
                {
                    MapContext cc;
                    cc = (MapContext)context.Get(typeof(PdfWriterPipeline).FullName);
                    Document d                       = (Document)cc[PdfWriterPipeline.DOCUMENT];
                    CssUtils cssUtils                = CssUtils.GetInstance();
                    float    pageWidth               = d.PageSize.Width;
                    float    marginLeft              = 0;
                    float    marginRight             = 0;
                    float    marginTop               = 0;
                    float    marginBottom            = 0;
                    IDictionary <String, String> css = t.CSS;
                    foreach (KeyValuePair <String, String> entry in css)
                    {
                        String key   = entry.Key;
                        String value = entry.Value;
                        if (Util.EqualsIgnoreCase(key, CSS.Property.MARGIN_LEFT))
                        {
                            marginLeft = cssUtils.ParseValueToPt(value, pageWidth);
                        }
                        else if (Util.EqualsIgnoreCase(key, CSS.Property.MARGIN_RIGHT))
                        {
                            marginRight = cssUtils.ParseValueToPt(value, pageWidth);
                        }
                        else if (Util.EqualsIgnoreCase(key, CSS.Property.MARGIN_TOP))
                        {
                            marginTop = cssUtils.ParseValueToPt(value, pageWidth);
                        }
                        else if (Util.EqualsIgnoreCase(key, CSS.Property.MARGIN_BOTTOM))
                        {
                            marginBottom = cssUtils.ParseValueToPt(value, pageWidth);
                        }
                    }
                    d.SetMargins(marginLeft, marginRight, marginTop, marginBottom);
                    d.Open();
                }
            } catch (NoCustomContextException e) {
                throw new PipelineException(LocaleMessages.GetInstance().GetMessage(LocaleMessages.PIPELINE_AUTODOC), e);
            }

            return(GetNext());
        }
Пример #8
0
        /**
         * Applies CSS to an Image. Currently supported:
         * - width
         * - height
         * - borders (color, width)
         * - spacing before and after
         *
         * @param img the image
         * @param tag the tag with the css
         * @return a styled Image
         */
        virtual public Image Apply(Image img, Tag tag)
        {
            IDictionary <String, String> cssMap = tag.CSS;

            String widthValue = null;

            cssMap.TryGetValue(HTML.Attribute.WIDTH, out widthValue);
            if (widthValue == null)
            {
                tag.Attributes.TryGetValue(HTML.Attribute.WIDTH, out widthValue);
            }

            String heightValue = null;

            cssMap.TryGetValue(HTML.Attribute.HEIGHT, out heightValue);
            if (heightValue == null)
            {
                tag.Attributes.TryGetValue(HTML.Attribute.HEIGHT, out heightValue);
            }

            if (widthValue == null)
            {
                img.ScaleToFitLineWhenOverflow = true;
            }
            else
            {
                img.ScaleToFitLineWhenOverflow = false;
            }

            img.ScaleToFitHeight = false;


            CssUtils utils         = CssUtils.GetInstance();
            float    widthInPoints = utils.ParsePxInCmMmPcToPt(widthValue);

            float heightInPoints = utils.ParsePxInCmMmPcToPt(heightValue);

            if (widthInPoints > 0 && heightInPoints > 0)
            {
                img.ScaleAbsolute(widthInPoints, heightInPoints);
            }
            else if (widthInPoints > 0)
            {
                heightInPoints = img.Height * widthInPoints / img.Width;
                img.ScaleAbsolute(widthInPoints, heightInPoints);
            }
            else if (heightInPoints > 0)
            {
                widthInPoints = img.Width * heightInPoints / img.Height;
                img.ScaleAbsolute(widthInPoints, heightInPoints);
            }

            // apply border CSS
            String borderTopColor = null;

            cssMap.TryGetValue(CSS.Property.BORDER_TOP_COLOR, out borderTopColor);
            if (borderTopColor != null)
            {
                img.BorderColorTop = HtmlUtilities.DecodeColor(borderTopColor);
            }

            String borderTopWidth = null;

            cssMap.TryGetValue(CSS.Property.BORDER_TOP_WIDTH, out borderTopWidth);
            if (borderTopWidth != null)
            {
                img.BorderWidthTop = utils.ParseValueToPt(borderTopWidth, 1f);
            }

            String borderRightColor = null;

            cssMap.TryGetValue(CSS.Property.BORDER_RIGHT_COLOR, out borderRightColor);
            if (borderRightColor != null)
            {
                img.BorderColorRight = HtmlUtilities.DecodeColor(borderRightColor);
            }

            String borderRightWidth = null;

            cssMap.TryGetValue(CSS.Property.BORDER_RIGHT_WIDTH, out borderRightWidth);
            if (borderRightWidth != null)
            {
                img.BorderWidthRight = utils.ParseValueToPt(borderRightWidth, 1f);
            }

            String borderBottomColor = null;

            cssMap.TryGetValue(CSS.Property.BORDER_BOTTOM_COLOR, out borderBottomColor);
            if (borderBottomColor != null)
            {
                img.BorderColorBottom = HtmlUtilities.DecodeColor(borderBottomColor);
            }

            String borderBottomWidth = null;

            cssMap.TryGetValue(CSS.Property.BORDER_BOTTOM_WIDTH, out borderBottomWidth);
            if (borderBottomWidth != null)
            {
                img.BorderWidthBottom = utils.ParseValueToPt(borderBottomWidth, 1f);
            }

            String borderLeftColor = null;

            cssMap.TryGetValue(CSS.Property.BORDER_LEFT_COLOR, out borderLeftColor);
            if (borderLeftColor != null)
            {
                img.BorderColorLeft = HtmlUtilities.DecodeColor(borderLeftColor);
            }

            String borderLeftWidth = null;

            cssMap.TryGetValue(CSS.Property.BORDER_LEFT_WIDTH, out borderLeftWidth);
            if (borderLeftWidth != null)
            {
                img.BorderWidthLeft = utils.ParseValueToPt(borderLeftWidth, 1f);
            }
            // end of border CSS

            String before = null;

            cssMap.TryGetValue(CSS.Property.BEFORE, out before);
            if (before != null)
            {
                img.SpacingBefore = float.Parse(before);
            }
            String after = null;

            cssMap.TryGetValue(CSS.Property.AFTER, out after);
            if (after != null)
            {
                img.SpacingAfter = float.Parse(after);
            }

            img.WidthPercentage = 0;
            return(img);
        }
Пример #9
0
        /* (non-Javadoc)
         * @see com.itextpdf.tool.xml.css.CssApplier#apply(com.itextpdf.text.Element, com.itextpdf.tool.xml.Tag)
         */
        public Image Apply(Image img, Tag tag)
        {
            String widthValue;

            tag.CSS.TryGetValue(HTML.Attribute.WIDTH, out widthValue);
            if (widthValue == null)
            {
                tag.Attributes.TryGetValue(HTML.Attribute.WIDTH, out widthValue);
            }

            String heightValue;

            tag.CSS.TryGetValue(HTML.Attribute.HEIGHT, out heightValue);
            if (heightValue == null)
            {
                tag.Attributes.TryGetValue(HTML.Attribute.HEIGHT, out heightValue);
            }

            if (widthValue == null)
            {
                img.ScaleToFitLineWhenOverflow = true;
            }
            else
            {
                img.ScaleToFitLineWhenOverflow = false;
            }

            if (heightValue == null)
            {
                img.ScaleToFitHeight = true;
            }
            else
            {
                img.ScaleToFitHeight = false;
            }


            CssUtils utils         = CssUtils.GetInstance();
            float    widthInPoints = utils.ParsePxInCmMmPcToPt(widthValue);

            float heightInPoints = utils.ParsePxInCmMmPcToPt(heightValue);

            if (widthInPoints > 0 && heightInPoints > 0)
            {
                img.ScaleAbsolute(widthInPoints, heightInPoints);
            }
            else if (widthInPoints > 0)
            {
                heightInPoints = img.Height * widthInPoints / img.Width;
                img.ScaleAbsolute(widthInPoints, heightInPoints);
            }
            else if (heightInPoints > 0)
            {
                widthInPoints = img.Width * heightInPoints / img.Height;
                img.ScaleAbsolute(widthInPoints, heightInPoints);
            }

            String before;

            tag.CSS.TryGetValue(CSS.Property.BEFORE, out before);
            if (before != null)
            {
                img.SpacingBefore = float.Parse(before, CultureInfo.InvariantCulture);
            }
            String after;

            tag.CSS.TryGetValue(CSS.Property.AFTER, out after);
            if (after != null)
            {
                img.SpacingAfter = float.Parse(after, CultureInfo.InvariantCulture);
            }
            img.WidthPercentage = 0;
            return(img);
        }