xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From James Balnaves <li...@balnaves.com>
Subject Re: How do I parse style information to get Java Color?
Date Thu, 02 Feb 2006 20:26:11 GMT
When I try this, the CSSStyleDeclaration ends up containing an  
"element" member of type SVGOMPathElement that has attributes "fill"  
and "d", but the CSSStyleDeclaration "values" member HashMap is  
empty. When I call style.getPropertyCSSValue("stroke") I get a null  
pointer exception, even though this value exists up in the main svg  

Any ideas? Do I have to search all the way up the tree for the stroke  
attribute? (although getComputedStyle() on the SVG element does not  
seem to work either).
I have included the SVG file I am trying to work with below:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.0//EN' 'http://www.w3.org/TR/ 
<svg fill-opacity="1" xmlns:xlink="http://www.w3.org/1999/xlink"  
color-rendering="auto" color-interpolation="auto" text- 
rendering="auto" stroke="black" stroke-linecap="square" stroke- 
miterlimit="10" shape-rendering="auto" stroke-opacity="1"  
fill="black" stroke-dasharray="none" font-weight="normal" stroke- 
width="1" xmlns="http://www.w3.org/2000/svg" font- 
family="&apos;Helvetica&apos;" font-style="normal" stroke- 
linejoin="miter" font-size="12" stroke-dashoffset="0" image- 
   <!--Generated by the Batik Graphics2D SVG Generator-->
   <defs id="genericDefs" />
     <g id="Page 0">
       <g stroke-linecap="round" text-rendering="optimizeLegibility"  
image-rendering="optimizeQuality" color-rendering="optimizeQuality"  
stroke-linejoin="round" color-interpolation="linearRGB">
         <path fill="none" d="M146 123 L146 122" />

On Feb 1, 2006, at 5:19 PM, Cameron McCormack wrote:

> Hi James.
> James Balnaves:
>> I would like to use Batik to construct a java.awt.Color object from
>> the "stroke:rgb(11,11,138);" information but I understand that the
>> color information could be described in many different ways as CSS
>> allows.
> Something like this would do it, using the standard SVG/CSS DOM
> interfaces (untested):
>   Element path = ...; // the path element
>   Document doc = p.getOwnerDocument();
>   SVGSVGElement svg = (SVGSVGElement) doc.getDocumentElement();
>   CSSStyleDeclaration style = svg.getComputedStyle(path, "");
>   SVGPaint stroke = (SVGPaintValue) style.getPropertyCSSValue 
> ("stroke");
>   // assuming that it's a solid colour:
>   RGBColor rgb = stroke.getRgbColor();
>   float r = rgb.getRed().getFloatValue(CSSValue.CSS_NUMBER);
>   float g = rgb.getGreen().getFloatValue(CSSValue.CSS_NUMBER);
>   float b = rgb.getBlue().getFloatValue(CSSValue.CSS_NUMBER);
>   Color c = new Color(r / 255, g / 255, b / 255);

View raw message