public static String getInheritedAttribute( String name, DOMInput in, List<Map<String, String>> styles) { List<String> values = in.getInheritedAttribute(name); for (int i = values.size() - 1; i >= 0; i--) { if (values.get(i) != null) { return values.get(i); } if (styles.get(i) != null && styles.get(i).containsKey(name)) { return styles.get(i).get(name); } } return null; }
public static double getDimensionValue(DOMInput in, String str) throws IOException { double scaleFactor = 1d; if (str == null || str.length() == 0) { return 0d; } if (str.endsWith("%")) { str = str.substring(0, str.length() - 1); } else if (str.endsWith("px")) { str = str.substring(0, str.length() - 2); } else if (str.endsWith("pt")) { str = str.substring(0, str.length() - 2); scaleFactor = 1.25; } else if (str.endsWith("pc")) { str = str.substring(0, str.length() - 2); scaleFactor = 15; } else if (str.endsWith("mm")) { str = str.substring(0, str.length() - 2); scaleFactor = 3.543307; } else if (str.endsWith("cm")) { str = str.substring(0, str.length() - 2); scaleFactor = 35.43307; } else if (str.endsWith("in")) { str = str.substring(0, str.length() - 2); scaleFactor = 90; } else if (str.endsWith("em")) { str = str.substring(0, str.length() - 2); List<Map<String, String>> styles = new ArrayList<Map<String, String>>(); List<String> values = in.getInheritedAttribute("style"); for (String v : values) { styles.add(getStyles(v)); } String value = getInheritedAttribute("font-size", in, styles); if (value != null && !value.endsWith("em")) { scaleFactor = getDimensionValue(in, value); } } return Double.parseDouble(str) * scaleFactor; }
/** Reads the attributes from the specified DOMInput and assigns them to the figure. */ public static void readAttributes(Figure f, DOMInput in) throws IOException { // FIXME - This method is not working, when "style" and individual attributes // are both used in an SVG document. List<Map<String, String>> styles = new ArrayList<Map<String, String>>(); List<String> values = in.getInheritedAttribute("style"); for (String v : values) { styles.add(getStyles(v)); } String value; // Fill color value = getInheritedAttribute("fill", in, styles); if (value != null) { Color color = getColor(value); if (color != INHERIT_COLOR && color != CURRENT_COLOR) { FILL_COLOR.set(f, color); } } value = getInheritedAttribute("fill-rule", in, styles); if (value != null) { WINDING_RULE.set( f, value.toUpperCase().equals("NONZERO") ? WindingRule.NON_ZERO : WindingRule.EVEN_ODD); } else { WINDING_RULE.set(f, WindingRule.NON_ZERO); } // Stroke color value = getInheritedAttribute("stroke", in, styles); if (value != null) { Color color = getColor(value); if (color != INHERIT_COLOR && color != CURRENT_COLOR) { STROKE_COLOR.set(f, color); } } value = getInheritedAttribute("stroke-width", in, styles); if (value != null) { STROKE_WIDTH.set(f, Double.valueOf(value)); } value = getInheritedAttribute("stroke-miterlimit", in, styles); if (value != null) { STROKE_MITER_LIMIT_FACTOR.set(f, Double.valueOf(value)); } value = getInheritedAttribute("stroke-dasharray", in, styles); if (value != null) { StringTokenizer tt = new StringTokenizer(value, " ,"); double[] dashes = new double[tt.countTokens()]; for (int i = 0, n = dashes.length; i < n; i++) { dashes[i] = Double.valueOf(tt.nextToken()); } STROKE_DASHES.set(f, dashes); } value = getInheritedAttribute("stroke-dashoffset", in, styles); if (value != null) { STROKE_DASH_PHASE.set(f, Math.abs(Double.valueOf(value))); } value = getInheritedAttribute("font-size", in, styles); if (value != null) { FONT_SIZE.set(f, getDimensionValue(in, value)); } value = getInheritedAttribute("text-anchor", in, styles); if (value != null) { SVGText.TEXT_ANCHOR.set(f, Enum.valueOf(SVGText.TextAnchor.class, value.toUpperCase())); } }