You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by Urs Reupke <ur...@gmx.net> on 2004/10/23 23:55:59 UTC

Setting Text Colors

Hello,
after good progress due to your last tips (thanks again), I have encountered another problem:
Trying to set a text color, which I thought to be one of the easiest of tasks.
I wrote this code:

public void setTextColor(SVGGElement element, Color color) {
    NodeList list = element.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
      if (list.item(i) instanceof SVGTextElement) {
        ((SVGElement) list.item(i)).setAttribute("fill", "rgb("+ color.getRed()+ ","+ color.getGreen()+ ","+ color.getBlue()+ ")");
      }
   }
}

After some initial hindrances were quickly solved by use of the SVG specification, everything
seemed to be in order to me, but things still are not working: Upon calling the method, the text simply
disappears. 
Using the Eclipse debugger, I had  a look into the object structure, and noted that the SVGTextElement
has no direct "fill"-attribute, instead, the color value seems to be stored in a "ComputedStyleMap" - which
I don't know how to access.
'.hasAttribute("fill")', however, returned true when called and '.getAttribute("fill")' confirmed that
the fill attribute was set to the RGB-value I specified.

What bothers me is that a mostly identical piece of code is used to set an area's background-color
and works perfectly well - do you know of a way to solve this?

Thank you
-Urs

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: Setting Text Colors

Posted by Tonny Kohar <to...@kiyut.com>.
Hi,

> Could it be a problem with the text having attributes from a <style> property
> as well as in XML form? (I don't think so, as you said presentation attributes
> are stored in CSS-form and a hardcoded experiment at setting text-color worked,
> but you never know.)

In the case that you set both. The style is the one used for rendering.

Regards
Tonny Kohar
-- 
Sketsa 
SVG Graphics Editor
http://www.kiyut.com



---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: Setting Text Colors

Posted by Thomas DeWeese <Th...@Kodak.com>.
Hi Urs,

   One other suggestion, set stroke to a third color (black or
something) for the text.  This will let you know if the problem is
that the fill is being set on the text and the background.


Thomas DeWeese wrote:

> Hi Urs,
> 
> Urs Reupke wrote:
> 
>>>   This code looks right to me.  is there any chance that you are
>>> doing something silly like setting the text to the same color as
>>> the background?
>>
>> Chances were :), but I thought of this, too, and explicitly set
>> the colors to be of pure red (255,0,0) for the background and pure 
>> green (0,255,0) for the text for testing purposes. I don't think that 
>> this is the problem.
>> Maybe I'm doing some other silly thing and just not seeing it.
>>
>>>    Can you still select the text?
>>>    I take it other modifications in the document work
>>> (i.e. the problem isn't in supporting dynamic updates in general).
>>
>>
>> I can select the text, though the select-Box appears to be empty (no 
>> glyphs
>> are shown, just the surrounding box), and onClick-Events are still 
>> handled correctly. Dynamic updates are supported, I explicitly set the 
>> document to be dynamic on loading.
> 
> 
>    My guess would be that the code that is setting the fill on the
> polygon is going overboard and setting the fill on the text as well.
> 
>>>    What do you mean by setting an area's background color.  You mean
>>> setting the fill for a rectangle or something?
>>
>>
>> Setting the fill-attribute on a polygon, in this case, but I tried 
>> more simple forms in the beginning with the same results.
>>
>> Could it be a problem with the text having attributes from a <style> 
>> property as well as in XML form? (I don't think so, as you said 
>> presentation attributes are stored in CSS-form and a hardcoded 
>> experiment at setting text-color worked, but you never know.)
> 
> 
>    Well it would only be a problem if the style attribute set
> the fill property.  In this case the style will win (the only
> thing presentation attributes override is inherited values).
> If you want to set the CSS value directly you can use:
> 
>    elem.getStyle().setProperty("fill", "red", "");
> 
>    This is a bit more efficient than setting the presentation
> attribute (this will also update the 'style' attribute.  If
> you want to know what CSS thinks the value is:
> 
>    String val = elem.getStyle.getPropertyValue("fill");
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
> For additional commands, e-mail: batik-users-help@xml.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: Setting Text Colors

Posted by Urs Reupke <ur...@gmx.net>.
Hi Thomas,

>Do the tspan element's have the fill attribute set?

As far as I know, they haven't - the parent text element
has no fill set (at first), and I don't set fill on them
programatically.

>Also does this explain why the text 'disappeared'?

Not to me, but I thought you could make more sense
of it :) I didn't change anything but parsed the list of 
the children tspan elements and set their fill-attribute.

>If it really isn't updating for you I would be interested in
>getting a reproducible test case that I could check.

Alright, I will see to it - I have quite a busy week ahead of me,
so please be patient.

Bye
-Urs

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: Setting Text Colors

Posted by Thomas DeWeese <Th...@Kodak.com>.
Hi Urs,

    Do the tspan element's have the fill attribute set?
If so this will 'win' over the inherited value from the
text element.  I just tested and in my test case it
inherits and updates the text just fine.

    Also does this explain why the text 'disappeared'?

    If it really isn't updating for you I would be interested in
getting a reproducible test case that I could check.

Urs Reupke wrote:

>> Something else just crosses my mind: Is there anything special I 
>> should consider
>> with the text if it is divided into TSpanElements? After reading the 
>> Specification
>> I thought, that fill-attributes are inherited to all children, but 
>> this wouldn't be the
>> first exception from the rule made for a TextElement.
> 
> 
> I just checked on that idea, and indeed: When setting the fill-attribute 
> for the
> TSpanElements beneath the TextElement, everything works fine.
> Now I need an additional for-loop to traverse all TSpan-Children, but 
> that's
> a minor inefficiency, I guess.
> I'd just like to know whether I misunderstood the specification or if it 
> should
> be the way I thought and Batik doesn't update the properties of 
> TSpanElements
> correctly when the parent TextElement is changed.
> Inheritance does work when the image is ínitially loaded on the JSVGCanvas
> (with colors hardcoded in the .svg-file).
> I replace the content of  the TextElements with TSpanElements, and if a 
> color is set, the text of the TSpanElements has that set color.
> 
> Anyway: Thanks for your support on this one, Tonny and Thomas.
> I'm very close to completing this part of the project.
> 
> Bye
> -Urs
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
> For additional commands, e-mail: batik-users-help@xml.apache.org
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: Setting Text Colors

Posted by Urs Reupke <ur...@gmx.net>.
> Something else just crosses my mind: Is there anything special I should consider
> with the text if it is divided into TSpanElements? After reading the Specification
> I thought, that fill-attributes are inherited to all children, but this wouldn't be the
> first exception from the rule made for a TextElement.

I just checked on that idea, and indeed: When setting the fill-attribute for the
TSpanElements beneath the TextElement, everything works fine.
Now I need an additional for-loop to traverse all TSpan-Children, but that's
a minor inefficiency, I guess.
I'd just like to know whether I misunderstood the specification or if it should
be the way I thought and Batik doesn't update the properties of TSpanElements
correctly when the parent TextElement is changed.
Inheritance does work when the image is ínitially loaded on the JSVGCanvas
(with colors hardcoded in the .svg-file).
I replace the content of  the TextElements with TSpanElements, and if a color
is set, the text of the TSpanElements has that set color.

Anyway: Thanks for your support on this one, Tonny and Thomas.
I'm very close to completing this part of the project.

Bye
-Urs



---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: Setting Text Colors

Posted by Urs Reupke <ur...@gmx.net>.
Hi again, Thomas,

>    My guess would be that the code that is setting the fill on the
> polygon is going overboard and setting the fill on the text as well.

>   One other suggestion, set stroke to a third color (black or
>something) for the text.  This will let you know if the problem is
>that the fill is being set on the text and the background.

I had already checked on this by simply disabling the code for the polygon-fill,
but the behaviour didn't change the slightest.

I tried, however, to follow your suggestion, but the text kept on disappearing.
(The text, however, looked interesting before I triggered my "setTextColor"
method, what part of a text are stroked, what filled? I thought that text-color
was determined only by the fill-attributes, not by stroke.)
 
>    Well it would only be a problem if the style attribute set
> the fill property.  In this case the style will win (the only
> thing presentation attributes override is inherited values).

> If you want to set the CSS value directly you can use:
>    elem.getStyle().setProperty("fill", "red", "");

Unfortunately, I only use CSS to set fontsize and fontfamily.
Using the methods you mentioned I tried to set the textcolor,
resulting in the same behaviour as before.
Asking for the value by calling getPropertyValue invariably returned 
the value I set before, so *that* part of the code seems to work.

I had a hunch that the (totally opaque) polygon might be painted in front
of the text after I clicked, but even setting the polygon opacity to 0 didn't help
the text.

Something else just crosses my mind: Is there anything special I should consider
with the text if it is divided into TSpanElements? After reading the Specification
I thought, that fill-attributes are inherited to all children, but this wouldn't be the
first exception from the rule made for a TextElement.

Good Night,
-Urs

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: Setting Text Colors

Posted by Thomas DeWeese <Th...@Kodak.com>.
Hi Urs,

Urs Reupke wrote:

>>   This code looks right to me.  is there any chance that you are
>> doing something silly like setting the text to the same color as
>> the background?
> 
> Chances were :), but I thought of this, too, and explicitly set
> the colors to be of pure red (255,0,0) for the background and pure green 
> (0,255,0) for the text for testing purposes. I don't think that this is 
> the problem.
> Maybe I'm doing some other silly thing and just not seeing it.
> 
>>    Can you still select the text?
>>    I take it other modifications in the document work
>> (i.e. the problem isn't in supporting dynamic updates in general).
> 
> I can select the text, though the select-Box appears to be empty (no glyphs
> are shown, just the surrounding box), and onClick-Events are still 
> handled correctly. Dynamic updates are supported, I explicitly set the 
> document to be dynamic on loading.

    My guess would be that the code that is setting the fill on the
polygon is going overboard and setting the fill on the text as well.

>>    What do you mean by setting an area's background color.  You mean
>> setting the fill for a rectangle or something?
> 
> Setting the fill-attribute on a polygon, in this case, but I tried more 
> simple forms in the beginning with the same results.
> 
> Could it be a problem with the text having attributes from a <style> 
> property as well as in XML form? (I don't think so, as you said 
> presentation attributes are stored in CSS-form and a hardcoded 
> experiment at setting text-color worked, but you never know.)

    Well it would only be a problem if the style attribute set
the fill property.  In this case the style will win (the only
thing presentation attributes override is inherited values).
If you want to set the CSS value directly you can use:

    elem.getStyle().setProperty("fill", "red", "");

    This is a bit more efficient than setting the presentation
attribute (this will also update the 'style' attribute.  If
you want to know what CSS thinks the value is:

    String val = elem.getStyle.getPropertyValue("fill");



---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: Setting Text Colors

Posted by Urs Reupke <ur...@gmx.net>.
Hi Thomas,

>   This code looks right to me.  is there any chance that you are
> doing something silly like setting the text to the same color as
> the background?

Chances were :), but I thought of this, too, and explicitly set
the colors to be of pure red (255,0,0) for the background and 
pure green (0,255,0) for the text for testing purposes. 
I don't think that this is the problem.
Maybe I'm doing some other silly thing and just not seeing it.
 
>    Can you still select the text?
>    I take it other modifications in the document work
> (i.e. the problem isn't in supporting dynamic updates in general).

I can select the text, though the select-Box appears to be empty (no glyphs
are shown, just the surrounding box), and onClick-Events are still handled 
correctly. Dynamic updates are supported, I explicitly set the document to
be dynamic on loading.

>    What do you mean by setting an area's background color.  You mean
> setting the fill for a rectangle or something?
Setting the fill-attribute on a polygon, in this case, but I tried more simple
forms in the beginning with the same results.

Could it be a problem with the text having attributes from a <style> property
as well as in XML form? (I don't think so, as you said presentation attributes
are stored in CSS-form and a hardcoded experiment at setting text-color worked,
but you never know.)

Regards
-Urs

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: Setting Text Colors

Posted by Thomas DeWeese <Th...@Kodak.com>.
Hi Urs,

Urs Reupke wrote:

> after good progress due to your last tips (thanks again),

    No problem.

> Trying to set a text color, which I thought to be one of the easiest of 
> tasks. I wrote this code:

   This code looks right to me.  is there any chance that you are
doing something silly like setting the text to the same color as
the background?

> After some initial hindrances were quickly solved by use of the SVG 
> specification, everything seemed to be in order to me, but things 
> still are not working: Upon calling the method, the text simply 
> disappears. 

    Can you still select the text?
    I take it other modifications in the document work
(i.e. the problem isn't in supporting dynamic updates in general).

> Using the Eclipse debugger, I had  a look into the object 
> structure, and noted that the SVGTextElement
> has no direct "fill"-attribute, instead, the color value seems to be 
> stored in a "ComputedStyleMap" - which
> I don't know how to access.

    This is because fill is a CSS property not an XML attribute
what you are setting is called a 'presentation attribute' which
feeds into the CSS Cascade.  In the end you shouldn't generally
need to know much about all this, it should just work.

> '.hasAttribute("fill")', however, returned true when called and 
> '.getAttribute("fill")' confirmed that
> the fill attribute was set to the RGB-value I specified.
> 
> What bothers me is that a mostly identical piece of code is used to set 
> an area's background-color
> and works perfectly well - do you know of a way to solve this?

    What do you mean by setting an area's background color.  You mean
setting the fill for a rectangle or something?


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org