/** * Construct a new StyleAttrCSSResolver with the given {@link CssFiles} and {@link CssUtils}. * * @param rules the {@link CssInheritanceRules} to use. * @param cssFiles a {@link CssFiles} implementation. * @param utils the CssUtils to use. * @param fileRetrieve the {@link FileRetrieve} implementation */ public StyleAttrSvgCSSResolver(ICssInheritanceRules rules, ICssFiles cssFiles, CssUtils utils, IFileRetrieve fileRetrieve) { this.utils = utils; this.cssFiles = cssFiles; this.inherit = rules; this.retrieve = fileRetrieve; this.attributes = SVGAttributes.GetSVGAttributesList(); }
public void CreateSVGDependencyGraph(DependencyGraph graph) { SVG svg = new SVG(this.Document, SVGNameSpace, "1.1"); this.BodyElement.AppendChild((svg as ISVGElement).Element); SVGDefinitions definitions = new SVGDefinitions(svg); System.Collections.Generic.Dictionary <DependencyNode, SVGGroup> nodeToSVGGroup = new System.Collections.Generic.Dictionary <DependencyNode, SVGGroup>(); int rankCount = graph.RankCount; int numVerticalSlots = 2 * rankCount + 1; float verticalStride = 100 / numVerticalSlots; int y = (int)verticalStride;; for (int i = rankCount - 1; i >= 0; --i) { DependencyNodeCollection rank = graph[i]; int nodeCount = rank.Count; int numHorizontalSlots = 2 * nodeCount + 1; float horizontalStride = 100 / numHorizontalSlots; int x = (int)horizontalStride; System.Text.StringBuilder rankIdName = new System.Text.StringBuilder(); rankIdName.AppendFormat("Rank{0}", rank.Rank); SVGGroup rankGroup = new SVGGroup(svg, rankIdName.ToString()); SVGAttributes.Fill(rankGroup, "red"); SVGText rankText = new SVGText(rankGroup, rankIdName.ToString(), 0, y, 100, (int)verticalStride, "%"); SVGAttributes.Fill(rankText, "black"); foreach (DependencyNode node in rank) { SVGGroup nodeGroup = new SVGGroup(rankGroup, node.UniqueModuleName); SVGRect nodeRect = new SVGRect(nodeGroup, x, y, (int)horizontalStride, (int)verticalStride, "%"); SVGText nodeText = new SVGText(nodeGroup, node.UniqueModuleName, x, y + (int)verticalStride / 2, (int)horizontalStride, (int)verticalStride, "%"); SVGAttributes.Fill(nodeText, "black"); nodeToSVGGroup.Add(node, nodeGroup); x += (int)(2 * horizontalStride); } y += (int)(verticalStride * 2); } SVGArrow arrow = new SVGArrow(svg, 0, 0, 100, 100, ""); SVGAttributes.Stroke(arrow, "black"); SVGAttributes.StrokeWidth(arrow, 5); #if false System.Xml.XmlElement circle = this.Document.CreateElement("circle"); System.Xml.XmlAttribute cx = this.Document.CreateAttribute("cx"); cx.Value = "100"; System.Xml.XmlAttribute cy = this.Document.CreateAttribute("cy"); cy.Value = "50"; System.Xml.XmlAttribute r = this.Document.CreateAttribute("r"); r.Value = "40"; circle.Attributes.Append(cx); circle.Attributes.Append(cy); circle.Attributes.Append(r); svgElement.AppendChild(circle); #endif }
/** * 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; } } }