Пример #1
0
        /**
         * Also taking into account the CSS properties of any parent tag in the given tag.
         *
         * @see com.itextpdf.tool.xml.pipeline.css.CSSResolver#resolveStyles(com.itextpdf.tool.xml.Tag)
         */

        virtual public void ResolveStyles(Tag t)
        {
            // get css for this tag from resolver
            IDictionary <String, String> tagCss  = new Dictionary <String, String>();
            IDictionary <String, String> listCss = null;

            if (null != cssFiles && cssFiles.HasFiles())
            {
                tagCss = cssFiles.GetCSS(t);
                if (Util.EqualsIgnoreCase(t.Name, HTML.Tag.P) || Util.EqualsIgnoreCase(t.Name, HTML.Tag.TD))
                {
                    listCss = cssFiles.GetCSS(new Tag(HTML.Tag.UL));
                }
            }
            // get css from style attr
            if (null != t.Attributes && t.Attributes.Count != 0)
            {
                if (t.Attributes.ContainsKey(HTML.Attribute.CELLPADDING))
                {
                    CssUtils.MapPutAll(tagCss,
                                       utils.ParseBoxValues(t.Attributes[HTML.Attribute.CELLPADDING], "cellpadding-", ""));
                }
                if (t.Attributes.ContainsKey(HTML.Attribute.CELLSPACING))
                {
                    CssUtils.MapPutAll(tagCss,
                                       utils.ParseBoxValues(t.Attributes[HTML.Attribute.CELLSPACING], "cellspacing-", ""));
                }
                String styleAtt;
                t.Attributes.TryGetValue(HTML.Attribute.STYLE, out styleAtt);
                if (!string.IsNullOrEmpty(styleAtt))
                {
                    Dictionary <String, String> tagAttrCss = new Dictionary <string, string>();
                    String[] styles = styleAtt.Split(';');
                    foreach (String s in styles)
                    {
                        String[] part = s.Split(splitColon, 2);
                        if (part.Length == 2)
                        {
                            String key   = utils.StripDoubleSpacesTrimAndToLowerCase(part[0]);
                            String value = utils.StripDoubleSpacesAndTrim(part[1]);
                            SplitRules(tagAttrCss, key, value);
                        }
                    }
                    foreach (KeyValuePair <String, String> e in tagAttrCss)
                    {
                        tagCss[e.Key] = e.Value;
                    }
                }
            }
            // inherit css from parent tags, as defined in provided CssInheritanceRules or if property = inherit
            IDictionary <String, String> css = t.CSS;

            if (t.Name != null)
            {
                if (Util.EqualsIgnoreCase(t.Name, HTML.Tag.I) || Util.EqualsIgnoreCase(t.Name, HTML.Tag.CITE) ||
                    Util.EqualsIgnoreCase(t.Name, HTML.Tag.EM) || Util.EqualsIgnoreCase(t.Name, HTML.Tag.VAR) ||
                    Util.EqualsIgnoreCase(t.Name, HTML.Tag.DFN) || Util.EqualsIgnoreCase(t.Name, HTML.Tag.ADDRESS))
                {
                    tagCss[CSS.Property.FONT_STYLE] = CSS.Value.ITALIC;
                }
                else if (Util.EqualsIgnoreCase(t.Name, HTML.Tag.B) || Util.EqualsIgnoreCase(t.Name, HTML.Tag.STRONG))
                {
                    tagCss[CSS.Property.FONT_WEIGHT] = CSS.Value.BOLD;
                }
                else if (Util.EqualsIgnoreCase(t.Name, HTML.Tag.U) || Util.EqualsIgnoreCase(t.Name, HTML.Tag.INS))
                {
                    tagCss[CSS.Property.TEXT_DECORATION] = CSS.Value.UNDERLINE;
                }
                else if (Util.EqualsIgnoreCase(t.Name, HTML.Tag.S) || Util.EqualsIgnoreCase(t.Name, HTML.Tag.STRIKE) ||
                         Util.EqualsIgnoreCase(t.Name, HTML.Tag.DEL))
                {
                    tagCss[CSS.Property.TEXT_DECORATION] = CSS.Value.LINE_THROUGH;
                }
                else if (Util.EqualsIgnoreCase(t.Name, HTML.Tag.BIG))
                {
                    tagCss[CSS.Property.FONT_SIZE] = CSS.Value.LARGER;
                }
                else if (Util.EqualsIgnoreCase(t.Name, HTML.Tag.SMALL))
                {
                    tagCss[CSS.Property.FONT_SIZE] = CSS.Value.SMALLER;
                }
            }


            if (listCss != null && listCss.ContainsKey(CSS.Property.LIST_STYLE_TYPE))
            {
                css[CSS.Property.LIST_STYLE_TYPE] = listCss[CSS.Property.LIST_STYLE_TYPE];
            }

            if (MustInherit(t.Name) && null != t.Parent && null != t.Parent.CSS)
            {
                if (null != this.inherit)
                {
                    foreach (KeyValuePair <String, String> entry in t.Parent.CSS)
                    {
                        String key = entry.Key;
                        if ((tagCss.ContainsKey(key) && CSS.Value.INHERIT.Equals(tagCss[key])) || CanInherite(t, key))
                        {
                            if (key.Contains(CSS.Property.CELLPADDING) &&
                                (HTML.Tag.TD.Equals(t.Name) || HTML.Tag.TH.Equals(t.Name)))
                            {
                                String paddingKey = key.Replace(CSS.Property.CELLPADDING, CSS.Property.PADDING);
                                //if (!tagCss.containsKey(paddingKey)) {
                                tagCss[paddingKey] = entry.Value;
                                //continue;
                                //}
                            }
                            else
                            {
                                //splitRules(css, key, entry.getValue());
                                css[key] = entry.Value;
                            }
                        }
                    }
                }
                else
                {
                    foreach (KeyValuePair <string, string> entry in t.Parent.CSS)
                    {
                        css.Add(entry);
                    }
                }
            }

            if (t.Name != null)
            {
                if (t.Name.Equals(HTML.Tag.FONT))
                {
                    String font_family;
                    if (t.Attributes.TryGetValue(HTML.Attribute.FACE, out font_family))
                    {
                        css[CSS.Property.FONT_FAMILY] = font_family;
                    }
                    String color;
                    if (t.Attributes.TryGetValue(HTML.Attribute.COLOR, out color))
                    {
                        css[CSS.Property.COLOR] = color;
                    }
                    String size;
                    if (t.Attributes.TryGetValue(HTML.Attribute.SIZE, out size))
                    {
                        if (size.Equals("1"))
                        {
                            css[CSS.Property.FONT_SIZE] = CSS.Value.XX_SMALL;
                        }
                        else if (size.Equals("2"))
                        {
                            css[CSS.Property.FONT_SIZE] = CSS.Value.X_SMALL;
                        }
                        else if (size.Equals("3"))
                        {
                            css[CSS.Property.FONT_SIZE] = CSS.Value.SMALL;
                        }
                        else if (size.Equals("4"))
                        {
                            css[CSS.Property.FONT_SIZE] = CSS.Value.MEDIUM;
                        }
                        else if (size.Equals("5"))
                        {
                            css[CSS.Property.FONT_SIZE] = CSS.Value.LARGE;
                        }
                        else if (size.Equals("6"))
                        {
                            css[CSS.Property.FONT_SIZE] = CSS.Value.X_LARGE;
                        }
                        else if (size.Equals("7"))
                        {
                            css[CSS.Property.FONT_SIZE] = CSS.Value.XX_LARGE;
                        }
                    }
                }
                else if (t.Name.Equals(HTML.Tag.A))
                {
                    css[CSS.Property.TEXT_DECORATION] = CSS.Value.UNDERLINE;
                    css[CSS.Property.COLOR]           = "blue";
                }
            }


            // overwrite properties (if value != inherit)
            foreach (KeyValuePair <String, String> e in tagCss)
            {
                if (!Util.EqualsIgnoreCase(CSS.Value.INHERIT, e.Value))
                {
                    if (e.Key.Equals(CSS.Property.TEXT_DECORATION))
                    {
                        String oldValue = null;
                        css.TryGetValue(e.Key, out oldValue);
                        css[e.Key] = MergeTextDecorationRules(oldValue, e.Value);
                    }
                    else
                    {
                        css[e.Key] = e.Value;
                    }
                }
            }
        }
Пример #2
0
        /**
         * Also taking into account the CSS properties of any parent tag in the given tag.
         *
         * @see com.itextpdf.tool.xml.pipeline.css.CSSResolver#resolveStyles(com.itextpdf.tool.xml.Tag)
         */
        public void ResolveStyles(Tag t)
        {
            // get css for this tag from resolver
            IDictionary <String, String> tagCss  = new Dictionary <String, String>();
            IDictionary <String, String> listCss = null;

            if (null != cssFiles && cssFiles.HasFiles())
            {
                tagCss = cssFiles.GetCSS(t);
                if (Util.EqualsIgnoreCase(t.Name, HTML.Tag.P) || Util.EqualsIgnoreCase(t.Name, HTML.Tag.TD))
                {
                    listCss = cssFiles.GetCSS(new Tag(HTML.Tag.UL));
                }
            }
            // get css from style attr
            if (null != t.Attributes && t.Attributes.Count != 0)
            {
                if (t.Attributes.ContainsKey(HTML.Attribute.CELLPADDING))
                {
                    CssUtils.MapPutAll(tagCss, utils.ParseBoxValues(t.Attributes[HTML.Attribute.CELLPADDING], "cellpadding-", ""));
                }
                if (t.Attributes.ContainsKey(HTML.Attribute.CELLSPACING))
                {
                    CssUtils.MapPutAll(tagCss, utils.ParseBoxValues(t.Attributes[HTML.Attribute.CELLSPACING], "cellspacing-", ""));
                }
                String styleAtt;
                t.Attributes.TryGetValue(HTML.Attribute.STYLE, out styleAtt);
                if (!string.IsNullOrEmpty(styleAtt))
                {
                    Dictionary <String, String> tagAttrCss = new Dictionary <string, string>();
                    String[] styles = styleAtt.Split(';');
                    foreach (String s in styles)
                    {
                        String[] part = s.Split(splitColon, 2);
                        if (part.Length == 2)
                        {
                            String key   = utils.StripDoubleSpacesTrimAndToLowerCase(part[0]);
                            String value = utils.StripDoubleSpacesAndTrim(part[1]);
                            SplitRules(tagAttrCss, key, value);
                        }
                    }
                    foreach (KeyValuePair <String, String> e in tagAttrCss)
                    {
                        tagCss[e.Key] = e.Value;
                    }
                }
            }
            // inherit css from parent tags, as defined in provided CssInheritanceRules or if property = inherit
            IDictionary <String, String> css = t.CSS;

            if (listCss != null && listCss.ContainsKey(CSS.Property.LIST_STYLE_TYPE))
            {
                css[CSS.Property.LIST_STYLE_TYPE] = listCss[CSS.Property.LIST_STYLE_TYPE];
            }
            if (MustInherit(t.Name) && null != t.Parent && null != t.Parent.CSS)
            {
                if (null != this.inherit)
                {
                    foreach (KeyValuePair <String, String> entry in t.Parent.CSS)
                    {
                        String key = entry.Key;
                        if ((tagCss.ContainsKey(key) && CSS.Value.INHERIT.Equals(tagCss[key])) || CanInherite(t, key))
                        {
                            //splitRules(css, key, entry.GetValue());
                            css[key] = entry.Value;
                        }
                    }
                }
                else
                {
                    CssUtils.MapPutAll(css, t.Parent.CSS);
                }
            }
            // overwrite properties (if value != inherit)
            foreach (KeyValuePair <String, String> e in tagCss)
            {
                if (!Util.EqualsIgnoreCase(CSS.Value.INHERIT, e.Value))
                {
                    css[e.Key] = e.Value;
                }
            }
        }
        /**
         * Also taking into account the CSS properties of any parent tag in the given tag.
         *
         * @see com.itextpdf.tool.xml.pipeline.css.CSSResolver#resolveStyles(com.itextpdf.tool.xml.Tag)
         */

        //THE DIFFERENCE BETWEEN HTML AND SVG: SVG has for a lot of style properties the possibility to use still attributes that define the same
        public void ResolveStyles(Tag t)
        {
            // get css for this tag from resolver
            IDictionary <String, String> tagCss = new Dictionary <String, String>();

            if (null != cssFiles && cssFiles.HasFiles())
            {
                tagCss = cssFiles.GetCSS(t);
            }

            if (null != t.Attributes && t.Attributes.Count > 0)
            {
                //first get the attributes that related to style but aren't in a style attribute, these can be overwritten by the same element that is defined in a style
                //TODO check default values & incorrect values:
                //e.g. stroke="red" style="stroke:yellow" -> yellow but stroke="red" style="stroke:an non-existing color" -> red
                //if both are wrong or missing -> take from parent

                foreach (KeyValuePair <String, String> pair in t.Attributes)
                {
                    bool valid = SVGAttributes.IsValidAttribute(pair.Key, pair.Value, attributes);
                    if (valid)
                    {
                        tagCss[pair.Key] = pair.Value;
                    }
                }

                // get css from "style" attr
                String styleAtt;
                if (t.Attributes.TryGetValue(HTML.Attribute.STYLE, out styleAtt) && !string.IsNullOrEmpty(styleAtt))
                {
                    String[] styles = styleAtt.Split(new Char[] { ';' });
                    foreach (String s in styles)
                    {
                        String[] part = s.Split(new Char[] { ':' }, 2);
                        if (part.Length == 2)
                        {
                            String key   = part[0].Trim();
                            String value = utils.StripDoubleSpacesAndTrim(part[1]);

                            //ONLY add when it is a valid style attribute in SVG
                            if (SVGAttributes.IsValidAttribute(key, value, attributes))
                            {
                                tagCss[key] = value;
                            }
                            else
                            {
                                //System.out.Println(key + " " + value);
                            }

                            //splitRules(tagCss, key, value);
                        }
                    }
                }
            }
            // inherit css from parent tags, as defined in provided CssInheritanceRules or if property = inherit
            IDictionary <String, String> css = t.CSS;

            if (MustInherit(t.Name) && null != t.Parent && null != t.Parent.CSS)
            {
                if (null != this.inherit)
                {
                    foreach (KeyValuePair <String, String> entry in t.Parent.CSS)
                    {
                        String key = entry.Key;
                        if ((tagCss.ContainsKey(key) && CSS.Value.INHERIT.Equals(tagCss.ContainsKey(key) ? tagCss[key] : null)) || CanInherite(t, key))
                        {
                            //splitRules(css, key, entry.GetValue());

                            if (SVGAttributes.IsValidAttribute(key, entry.Value, attributes))
                            {
                                css[key] = entry.Value;
                            }
                        }
                    }
                }
                else
                {
                    CssUtils.MapPutAll(css, t.Parent.CSS);
                }
            }
            // overwrite properties (if value != inherit)
            foreach (KeyValuePair <String, String> e in tagCss)
            {
                if (!Util.EqualsIgnoreCase(CSS.Value.INHERIT, e.Value))
                {
                    css[e.Key] = e.Value;
                }
            }
        }