public static Color getColor(String value) { value = value.trim().toLowerCase(); if (value.equals("none")) { return null; } else if (value.equals("currentColor")) { // indicates that painting shall be done using the color specified by the 'color' property. return CURRENT_COLOR; } else if (value.equals("inherit")) { // Each property may also have a specified value of 'inherit', which // means that, for a given element, the property takes the same // computed value as the property for the element's parent return INHERIT_COLOR; } else if (SVG_COLORS.containsKey(value)) { return SVG_COLORS.get(value); } else if (value.startsWith("#") && value.length() == 7) { return new Color(Integer.decode(value)); } else if (value.startsWith("#") && value.length() == 4) { // Three digits hex value int th = Integer.decode(value); return new Color( (th & 0xf) | ((th & 0xf) << 4) | ((th & 0xf0) << 4) | ((th & 0xf0) << 8) | ((th & 0xf00) << 8) | ((th & 0xf00) << 12)); } else if (value.startsWith("rgb")) { StringTokenizer tt = new StringTokenizer(value, "() ,"); tt.nextToken(); Color c = new Color( Integer.decode(tt.nextToken()), Integer.decode(tt.nextToken()), Integer.decode(tt.nextToken())); return c; } else { return null; } }
/** 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())); } }