Index: candy/src/processing/candy/SVG.java =================================================================== --- candy/src/processing/candy/SVG.java (revision 3901) +++ candy/src/processing/candy/SVG.java (working copy) @@ -679,114 +679,180 @@ protected void getColors(XMLElement properties) { + + if (properties.hasAttribute("opacity")) { + String opacityText = properties.getStringAttribute("opacity"); + setOpacity(opacityText); + } + + if (properties.hasAttribute("stroke")) { + String strokeText = properties.getStringAttribute("stroke"); + setStroke(strokeText); + } + + if (properties.hasAttribute("stroke-width")) { + // if NaN (i.e. if it's 'inherit') then default back to the inherit setting + String lineweight = properties.getStringAttribute("stroke-width"); + setStrokeWeight(lineweight); + } - if (properties.hasAttribute("opacity")) { - opacity = properties.getFloatAttribute("opacity"); + if (properties.hasAttribute("stroke-linejoin")) { + String linejoin = properties.getStringAttribute("stroke-linejoin"); + setStrokeJoin(linejoin); + } + + if (properties.hasAttribute("stroke-linecap")) { + String linecap = properties.getStringAttribute("stroke-linecap"); + setStrokeCap(linecap); + } + + + // fill defaults to black (though stroke defaults to "none") + // http://www.w3.org/TR/SVG/painting.html#FillProperties + if (properties.hasAttribute("fill")) { + String fillText = properties.getStringAttribute("fill"); + setFill(fillText); + + } + + if (properties.hasAttribute("style")) { + String styleText = properties.getStringAttribute("style"); + String[] styleTokens = PApplet.splitTokens(styleText, ";"); + + for(int i = 0; i < styleTokens.length; i++){ + String[] tokens = PApplet.splitTokens(styleTokens[i], ":"); + + tokens[0] = PApplet.trim(tokens[0]); + + if(tokens[0].equals("fill")){ + setFill(tokens[1]); + + }else if(tokens[0].equals("fill-opacity")){ + //setFillAlpha(tokens[1]); + + }else if(tokens[0].equals("stroke")){ + setStroke(tokens[1]); + + }else if(tokens[0].equals("stroke-width")){ + setStrokeWeight(tokens[1]); + + }else if(tokens[0].equals("stroke-linecap")){ + setStrokeCap(tokens[1]); + + }else if(tokens[0].equals("stroke-linejoin")){ + setStrokeJoin(tokens[1]); + + }else if(tokens[0].equals("stroke-opacity")){ + //setStrokeAlpha(tokens[1]); + + }else if(tokens[0].equals("opacity")){ + setOpacity(tokens[1]); + + }else{ + // Other attributes are not yet implemented + } } - int opacityMask = ((int) (opacity * 255)) << 24; + } + } + + void setOpacity(String opacityText){ + opacity = PApplet.parseFloat(opacityText); + } - if (properties.hasAttribute("stroke")) { - String strokeText = properties.getStringAttribute("stroke"); - if (strokeText.equals("none")) { - stroke = false; - } else if (strokeText.startsWith("#")) { - stroke = true; - strokeColor = opacityMask | - (Integer.parseInt(strokeText.substring(1), 16)) & 0xFFFFFF; - } else if (strokeText.startsWith("rgb")) { - stroke = true; - strokeColor = opacityMask | parseRGB(strokeText); - } else if (strokeText.startsWith("url(#")) { - strokeName = strokeText.substring(5, strokeText.length() - 1); - Object strokeObject = table.get(strokeName); - if (strokeObject instanceof Gradient) { - strokeGradient = (Gradient) strokeObject; - strokeGradientPaint = calcGradientPaint(strokeGradient); //, opacity); - } else { - System.err.println("url " + strokeName + " refers to unexpected data"); - } - } - } + void setStrokeWeight(String lineweight){ + strokeWeight = PApplet.parseFloat(lineweight); + } - if (properties.hasAttribute("stroke-width")) { - // if NaN (i.e. if it's 'inherit') then default back to the inherit setting - strokeWeight = properties.getFloatAttribute("stroke-width", strokeWeight); - } + void setStroke(String strokeText){ + int opacityMask = ((int) (opacity * 255)) << 24; + if (strokeText.equals("none")) { + stroke = false; + } else if (strokeText.startsWith("#")) { + stroke = true; + strokeColor = opacityMask | + (Integer.parseInt(strokeText.substring(1), 16)) & 0xFFFFFF; + } else if (strokeText.startsWith("rgb")) { + stroke = true; + strokeColor = opacityMask | parseRGB(strokeText); + } else if (strokeText.startsWith("url(#")) { + strokeName = strokeText.substring(5, strokeText.length() - 1); + Object strokeObject = table.get(strokeName); + if (strokeObject instanceof Gradient) { + strokeGradient = (Gradient) strokeObject; + strokeGradientPaint = calcGradientPaint(strokeGradient); //, opacity); + } else { + System.err.println("url " + strokeName + " refers to unexpected data"); + } + } + } - if (properties.hasAttribute("stroke-linejoin")) { - String linejoin = properties.getStringAttribute("stroke-linejoin"); - if (linejoin.equals("inherit")) { - // do nothing, will inherit automatically + void setStrokeJoin(String linejoin){ + if (linejoin.equals("inherit")) { + // do nothing, will inherit automatically + + } else if (linejoin.equals("miter")) { + strokeJoin = PConstants.MITER; + + } else if (linejoin.equals("round")) { + strokeJoin = PConstants.ROUND; + + } else if (linejoin.equals("bevel")) { + strokeJoin = PConstants.BEVEL; + } + } - } else if (linejoin.equals("miter")) { - strokeJoin = PConstants.MITER; - - } else if (linejoin.equals("round")) { - strokeJoin = PConstants.ROUND; - - } else if (linejoin.equals("bevel")) { - strokeJoin = PConstants.BEVEL; - } - } - - if (properties.hasAttribute("stroke-linecap")) { - String linecap = properties.getStringAttribute("stroke-linecap"); - if (linecap.equals("inherit")) { - // do nothing, will inherit automatically - - } else if (linecap.equals("butt")) { - strokeCap = PConstants.SQUARE; - - } else if (linecap.equals("round")) { - strokeCap = PConstants.ROUND; - - } else if (linecap.equals("square")) { - strokeCap = PConstants.PROJECT; - } - } - - - // fill defaults to black (though stroke defaults to "none") - // http://www.w3.org/TR/SVG/painting.html#FillProperties - if (properties.hasAttribute("fill")) { - String fillText = properties.getStringAttribute("fill"); - if (fillText.equals("none")) { - fill = false; - } else if (fillText.startsWith("#")) { - fill = true; - fillColor = opacityMask | - (Integer.parseInt(fillText.substring(1), 16)) & 0xFFFFFF; - //System.out.println("hex for fill is " + PApplet.hex(fillColor)); - } else if (fillText.startsWith("rgb")) { - fill = true; - fillColor = opacityMask | parseRGB(fillText); - } else if (fillText.startsWith("url(#")) { - fillName = fillText.substring(5, fillText.length() - 1); - //PApplet.println("looking for " + fillName); - Object fillObject = table.get(fillName); - //PApplet.println("found " + fillObject); - if (fillObject instanceof Gradient) { - fill = true; - fillGradient = (Gradient) fillObject; - fillGradientPaint = calcGradientPaint(fillGradient); //, opacity); - //PApplet.println("got filla " + fillObject); - } else { - System.err.println("url " + fillName + " refers to unexpected data"); - } - } - } + void setStrokeCap(String linecap){ + if (linecap.equals("inherit")) { + // do nothing, will inherit automatically + + } else if (linecap.equals("butt")) { + strokeCap = PConstants.SQUARE; + + } else if (linecap.equals("round")) { + strokeCap = PConstants.ROUND; + + } else if (linecap.equals("square")) { + strokeCap = PConstants.PROJECT; } + } + + void setFill(String fillText){ + int opacityMask = ((int) (opacity * 255)) << 24; + if (fillText.equals("none")) { + fill = false; + } else if (fillText.startsWith("#")) { + fill = true; + fillColor = opacityMask | + (Integer.parseInt(fillText.substring(1), 16)) & 0xFFFFFF; + //System.out.println("hex for fill is " + PApplet.hex(fillColor)); + } else if (fillText.startsWith("rgb")) { + fill = true; + fillColor = opacityMask | parseRGB(fillText); + } else if (fillText.startsWith("url(#")) { + fillName = fillText.substring(5, fillText.length() - 1); + //PApplet.println("looking for " + fillName); + Object fillObject = table.get(fillName); + //PApplet.println("found " + fillObject); + if (fillObject instanceof Gradient) { + fill = true; + fillGradient = (Gradient) fillObject; + fillGradientPaint = calcGradientPaint(fillGradient); //, opacity); + //PApplet.println("got filla " + fillObject); + } else { + System.err.println("url " + fillName + " refers to unexpected data"); + } + } + } + int parseRGB(String what) { + int leftParen = what.indexOf('(') + 1; + int rightParen = what.indexOf(')'); + String sub = what.substring(leftParen, rightParen); + int[] values = PApplet.parseInt(PApplet.splitTokens(sub, ", ")); + return (values[0] << 16) | (values[1] << 8) | (values[2]); + } - int parseRGB(String what) { - int leftParen = what.indexOf('(') + 1; - int rightParen = what.indexOf(')'); - String sub = what.substring(leftParen, rightParen); - int[] values = PApplet.parseInt(PApplet.splitTokens(sub, ", ")); - return (values[0] << 16) | (values[1] << 8) | (values[2]); - } - protected Paint calcGradientPaint(Gradient gradient) { if (gradient instanceof LinearGradient) { LinearGradient grad = (LinearGradient) gradient;