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 Mark Lundquist <ml...@comcast.net> on 2006/09/11 20:36:24 UTC
need help w/ + getBBox()
Hi,
I need to flow a bit of text into a container, and I gather that I
should be able to set the height of the container in the onload()
handler based on the bounding box of... something, but I can't figure
out what.
I'm using Batik 1.6 and using Squiggle to test my pathetic attempts at
hacking SVG. Here's my markup, it's pretty simple:
<svg
xmlns="http://www.w3.org/2000/svg"
version="1.2"
>
<flowRoot
style="
fill: black;
font-size: 30;
"
>
<flowRegion
vertical-align="left"
>
<rect x="0" y="0" width="150" height="75"/>
</flowRegion>
<flowDiv>
<flowPara>
Hi from Batik!
</flowPara>
</flowDiv>
</flowRoot>
</svg>
So, what am I supposed to take the getBBox() of? Batik says getBBox is
undefined for the flowRoot, flowRegion, flowDiv and flowPara elements.
The only thing left is the rect inside the flowRegion, and its getBBox()
always returns 150x75, which I guess does not surprise me :-/
And then... on which element will I set the height attribute? The root
element, the rect, or both? Am I even setting this <flowRoot> business
up correctly in the first place?
Thanks for any help!
—ml—
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
Re: need help w/ + getBBox()
Posted by th...@kodak.com.
Hi Mark,
news <ne...@sea.gmane.org> wrote on 09/15/2006 12:43:02 PM:
> thomas.deweese@kodak.com wrote:
> >
> > Did you try transcoding the resulting Document? It should
generate
> > a PNG/JPEG of the right size.
>
> Hmm, OK... I gave it a shot and here is what happened:
>
> java.lang.NullPointerException
> at
>
org.apache.batik.dom.util.SAXDocumentFactory.startElement(SAXDocumentFactory.java:562)
> at
>
org.apache.cocoon.components.sax.XMLTeePipe.startElement(XMLTeePipe.java:83)
> Now, for all I know this might be a bug in Batik trunk, or it could just
> be that Cocoon's Batik-based SVG serializer isn't compatible with Batik
> trunk (at least in Cocoon 2.1.8, which is what I'm using for this
> project)... does this exception suggest anything to you?
Hmm, I don't think this is going to work. Our DocumentFactory wants to
check some info from the Parser (which isn't provided this way, don't
know if it's OK to require a parser in a DocumentFactory or not ;).
Can the Cocoon code take just a DOMImplementation? You could give
it our SVG12 DomImplementation.
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
Re: need help w/ + getBBox()
Posted by Mark Lundquist <ml...@comcast.net>.
thomas.deweese@kodak.com wrote:
>
> Did you try transcoding the resulting Document? It should generate
> a PNG/JPEG of the right size.
Hmm, OK... I gave it a shot and here is what happened:
java.lang.NullPointerException
at
org.apache.batik.dom.util.SAXDocumentFactory.startElement(SAXDocumentFactory.java:562)
at
org.apache.cocoon.components.sax.XMLTeePipe.startElement(XMLTeePipe.java:83)
at
net.sf.saxon.event.ContentHandlerProxy.startContent(ContentHandlerProxy.java:259)
Now, for all I know this might be a bug in Batik trunk, or it could just
be that Cocoon's Batik-based SVG serializer isn't compatible with Batik
trunk (at least in Cocoon 2.1.8, which is what I'm using for this
project)... does this exception suggest anything to you?
thx a lot,
—ml—
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
Re: need help w/ + getBBox()
Posted by th...@kodak.com.
Hi Mark,
news <ne...@sea.gmane.org> wrote on 09/14/2006 02:07:08 PM:
> No exceptions thrown, the computed boxHeight is what I expect, but for
> some reason setting the heights on the rect and the root don't appear to
> have any effect...?
Did you try transcoding the resulting Document? It should generate
a PNG/JPEG of the right size.
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
Re: need help w/ + getBBox()
Posted by Mark Lundquist <ml...@comcast.net>.
Hi Thomas,
All right, I think we're getting there... Here's my new onload handler:
function adjustHeight() {
var text = document.getElementById ('text');
var nch = text.getNumberOfChars();
var lastCharBox = text.getExtentOfChar(nch-1);
var boxHeight = lastCharBox.y + lastCharBox.height;
var textContainer = document.getElementById ('text-container');
textContainer.setAttributeNS (null, 'height', boxHeight);
document.rootElement.setAttributeNS (null, 'height', boxHeight);
}
(I didn't include the markup this time, it's the same as before with an
@id="text-container" on the <rect>).
No exceptions thrown, the computed boxHeight is what I expect, but for
some reason setting the heights on the rect and the root don't appear to
have any effect...?
Thanks a lot,
—ml—
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
Re: need help w/ + getBBox()
Posted by th...@kodak.com.
Hi Mark,
Mark wrote on 09/13/2006 11:43:02 PM:
> > It is possible. Can you send a small standalone
> > example that shows the problem?
>
> Certainly, here it is!
Ok this is now fixed in SVN.
> <!-- BTW, how do I make this transparent now? -->
> <rect style="fill: white" x="0" y="0" width="150" height="75"/>
syle="fill:none; stroke:none"
or
style="visibility:hidden"
BTW I used the following to get the Y0 and Y1 of the
text:
var nch = text.getNumberOfChars();
var r0 = text.getExtentOfChar(0);
var r1 = text.getExtentOfChar(nch-1);
var y0 = r0.y;
var y1 = r1.y + r1.height;
If you want your output graphic to have this as it's height
you should set the 'height' attribute on the root SVG element
(you should also set the width of the root SVG element to
the width of your rect, but that doesn't need to be done
in script unless you want to):
document.rootElement.setAttributeNS(null, "height", y1);
(This actually glosses over a number of things like possible
transform changes, the flowRoot element not starting at 0,0 etc.
But it sounds like you don't need to worry about most of those
things).
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
Re: need help w/ + getBBox()
Posted by Mark Lundquist <ml...@comcast.net>.
thomas.deweese@kodak.com wrote:
>
>>1) Am I on the right track here with getExtentOfChar()?
>
>
> Yes, absolutely.
Sweet :-)
>
>
>>2) What's the deal w/ the NPE, is trunk broken?
>
>
> It is possible. Can you send a small standalone
> example that shows the problem?
Certainly, here it is!
======================
<?xml version="1.0"?>
<svg
xmlns="http://www.w3.org/2000/svg"
version="1.2"
onload="onload()"
>
<flowRoot
style="
fill: black;
font-size: 30;
"
>
<flowRegion
vertical-align="middle"
>
<!-- BTW, how do I make this transparent now? -->
<rect style="fill: white" x="0" y="0" width="150" height="75"/>
</flowRegion>
<flowDiv
>
<flowPara id="text">
Hi from Batik!
</flowPara>
</flowDiv>
</flowRoot>
<script><![CDATA[
function onload() {
var text = document.getElementById ('text');
alert (text); // so far so good...
alert (text.getExtentOfChar); // yeah baby...
alert (text.getExtentOfChar(1)); // D'oh!!!!
}
]]></script>
</svg>
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
Re: need help w/ + getBBox()
Posted by th...@kodak.com.
Hi Mark,
news <ne...@sea.gmane.org> wrote on 09/13/2006 06:09:07 PM:
>
http://xmlgraphics.apache.org/batik/javadoc/org/apache/batik/dom/svg/SVGTextContent.html
Yes, sorry I forgot that textContent was a standard DOM attribute.
You found the interface I intended.
> ...and so I thought I'd try getExtentOfChar(). I added this:
> alert (text.getExtentOfChar(0));
> But then:
>
> java.lang.NullPointerException
> at
> org.apache.batik.dom.svg.SVGTextContentSupport.getNumberOfChars(Unknown
> Source)
Hmm where is this code? Is it in a JavaScript onload handler?
> 1) Am I on the right track here with getExtentOfChar()?
Yes, absolutely.
> 2) What's the deal w/ the NPE, is trunk broken?
It is possible. Can you send a small standalone
example that shows the problem?
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
Re: need help w/ + getBBox()
Posted by Mark Lundquist <ml...@comcast.net>.
OK, I ran across this:
http://xmlgraphics.apache.org/batik/javadoc/org/apache/batik/dom/svg/SVGTextContent.html
...and so I thought I'd try getExtentOfChar(). I added this:
alert (text.getExtentOfChar);
alert (text.getExtentOfChar(0));
The first alert shows
function getExtentOfChar() {/*
org.wc3.dom.svg.SVGRect getExtentOfChar(int)
*/}
So far so good...
But then:
java.lang.NullPointerException
at
org.apache.batik.dom.svg.SVGTextContentSupport.getNumberOfChars(Unknown
Source)
at
org.apache.batik.dom.svg.SVGTextContentSupport.getExtentOfChar(Unknown
Source)
at
org.apache.batik.dom.svg.SVGOMTextContentElement.getExtentOfChar(Unknown
Source)
Questions:
1) Am I on the right track here with getExtentOfChar()?
2) What's the deal w/ the NPE, is trunk broken?
Thx-a-lot,
—ml—
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
Re: need help w/ + getBBox()
Posted by Mark Lundquist <ml...@comcast.net>.
thomas.deweese@kodak.com wrote:
> Hi Mike,
Mark :-)
>
> news <ne...@sea.gmane.org> wrote on 09/13/2006 05:42:59 PM:
Say, I gather that there's a textFlow thing that's apparently a
non-standard, maybe earlier, Batik extension, or something... I really
have no idea what that is, but I'm sure you do :-). Would that be a
better approach? (I don't care about it being portable 100%, legit SVG,
I'm really just using Batik in Cocoon as a rasterizer for some text into
some exotic font). Maybe textFlow provides something I can hang a
getBBox() on? I meant to ask about this earlier today.
>
>>is it a bad sign when the very first thing a clueless newbie wants
>>to do with SVG turns out to require "esoterica" to get it working?
>
>
> Well I think you just got lucky (or not as the case may be).
ah -- well, that is good to know! :-)
cheers,
—ml—
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
Re: need help w/ + getBBox()
Posted by th...@kodak.com.
Hi Mike,
news <ne...@sea.gmane.org> wrote on 09/13/2006 05:42:59 PM:
> I have to admit that all this is a wee bit discouraging... "All I Want
> To Do"(TM) is render some text and have the hight of the graphic be
> constrained to the height of the wrapped text. It seems like that
> should be dead simple, but it turns out that SVG has no concept of this,
> and I need to write an onload handler to fixup the container size.
Yes, this is not the way SVG is designed. Given the intent of
SVG I am not sure it could do it much better.
> OK, no problem... oh, but wait, it's not as easy as just asking for a
> bounding box anymore, now you have to walk over the glyphs and calculate
> that yourself...
As I said originally, this is really a bug in Batik. The FlowRoot
element should implement the SVGLocatable interface which provides
getBBox and a number of other useful features. I'd even fix this
for you but flowRoot is a dead end right now. The SVG WG did an
about face after 2 years and went in a different direction.
> is it a bad sign when the very first thing a clueless newbie wants
> to do with SVG turns out to require "esoterica" to get it working?
Well I think you just got lucky (or not as the case may be).
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
Re: need help w/ + getBBox()
Posted by Mark Lundquist <ml...@comcast.net>.
thomas.deweese@kodak.com wrote:
>
> It appears you are right. However I don't think we are apt to fix this
> as SVG 1.2 changed directions after we implemented this. Post
> Batik 1.6 we updated the flowDiv/flowPara elements to implement the
> TextContent interface. So you can get the bbox of individual chars
> which you could use to build this up.
OK, thanks... I still have more questions :-/
1) From what I could scrounge up, it looks like textContent is just a
string. How does that help?
2) I can alert() the textContent of the <flowPara> element... and it
does appear to be just a string...?
3) Yes, I did have to build Batik from trunk to get that... but then,
my image didn't display correctly; all I got was a black rectangle. If
I add style="fill: white" on the <rect>, then I can see my text again...
but I don't really want a white background, I need a transparent
background. How do I make that happen? (Sorry, I don't really know
SVG, I'm just bumbling along).
I have to admit that all this is a wee bit discouraging... "All I Want
To Do"(TM) is render some text and have the hight of the graphic be
constrained to the height of the wrapped text. It seems like that
should be dead simple, but it turns out that SVG has no concept of this,
and I need to write an onload handler to fixup the container size. OK,
no problem... oh, but wait, it's not as easy as just asking for a
bounding box anymore, now you have to walk over the glyphs and calculate
that yourself... This feels like punishment for what must be some abuse
of SVG, or else... is it a bad sign when the very first thing a clueless
newbie wants to do with SVG turns out to require "esoterica" to get it
working? (It feels like something in the same league as HTML+CSS not
supporting vertical centering or grid layouts).
OK, whiney rant over. Getting this working is the only thing holding up
a web site launch that's already late, so... you know :-). Anyway, I
can see that the flowPara element has a textContent attribute but I have
no idea how to use that to build up a composite bounding box (as it
appears this attribute is just a string). Any help is much appreciated!
Thanks,
—ml—
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
Re: need help w/ + getBBox()
Posted by th...@kodak.com.
Hi Mark,
news <ne...@sea.gmane.org> wrote on 09/11/2006 02:36:24 PM:
> I'm using Batik 1.6 and using Squiggle to test my pathetic attempts at
> hacking SVG. Here's my markup, it's pretty simple:
I think you may need to upgrade to the SVN version of Batik
> <flowRoot [...]>
> <flowRegion vertical-align="left">
> <rect x="0" y="0" width="150" height="75"/>
> </flowRegion>
> <flowDiv><flowPara>Hi from Batik!</flowPara></flowDiv>
> </flowRoot>
> </svg>
> So, what am I supposed to take the getBBox() of? Batik says getBBox is
> undefined for the flowRoot, flowRegion, flowDiv and flowPara elements.
It appears you are right. However I don't think we are apt to fix this
as SVG 1.2 changed directions after we implemented this. Post
Batik 1.6 we updated the flowDiv/flowPara elements to implement the
TextContent interface. So you can get the bbox of individual chars
which you could use to build this up.
> And then... on which element will I set the height attribute? The root
> element, the rect, or both?
flowRoot doesn't have a height attribute. You would set the height
attribute on the rect. You should make sure the rect is 'oversized'
before you start otherwise the last chars won't be displayed at all.
> Am I even setting this <flowRoot> business up correctly in the first
place?
It looks good to me.
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org