Clipped text when dumping FloatCanvas

Hi everyone!

I have an application where the user can draw lines, bitmaps and text
on a FloatCanvas, and then export it to a file.

It used to work nicely on Ubuntu 9.04 (wxPython 2.8.9.1), but now on
Ubuntu 10.04 (wxPython 2.8.10.1) it's behaving erratically. Basically,
the export process seems to be "cutting" the ScaledText objects over a
certain y-coordinate, so for example if I write something above this
limit, the text shows up on the exported file, while if it's below it
doesn't show, or gets cut at midway if it's across the limit. Other
objects instead show up just fine, wherever they are.

What's really weird though is that I'm testing this on my laptop, a VM
and 3 other workstations, and the strange behaviour is present only on
two machines, even though every environment is running the same
distribution. What could I check?

Here's how I export the canvas to file:

canvas.Scale = 1
canvas.SetToNewScale(False)
canvas._ResetBoundingBox()

# Align the viewport
box = canvas.BoundingBox
canvas.ViewPortCenter -= (canvas.PixelToWorld((0,0)) -
numpy.array((box[0,0], box[1,1])))

# Let's draw
bmp = wx.EmptyBitmap(box.Width, box.Height)
dc = wx.MemoryDC()
dc.SelectObject(bmp)
dc.Clear()
canvas._DrawObjects(dc, canvas._DrawList, dc, canvas.BoundingBox)
dc.SelectObject(wx.NullBitmap)

Many thanks!

I have an application where the user can draw lines, bitmaps and text
on a FloatCanvas, and then export it to a file.

It used to work nicely on Ubuntu 9.04 (wxPython 2.8.9.1), but now on
Ubuntu 10.04 (wxPython 2.8.10.1) it's behaving erratically. Basically,
the export process seems to be "cutting" the ScaledText objects over a
certain y-coordinate, so for example if I write something above this
limit, the text shows up on the exported file, while if it's below it
doesn't show, or gets cut at midway if it's across the limit.

So you get part of the text, but not all?

Other
objects instead show up just fine, wherever they are.

very wierd -- it sounds like GTK is settign some kind of clipping box -- but only applying to text? very odd.

What's really weird though is that I'm testing this on my laptop, a VM
and 3 other workstations, and the strange behaviour is present only on
two machines, even though every environment is running the same
distribution. What could I check?

Are they running the same version of GTK? and the same theme?

Here's how I export the canvas to file:

This looks like you are doing a lot that FloatCanvas should take care of for you.

Have you tried simply:

canvas.SaveAsImage(filename)

That should save exactly what is currently displayed.

canvas.Scale = 1
canvas.SetToNewScale(False)
canvas._ResetBoundingBox()

The _ResetboundingBox only needs to be called if you've addded or removed DrawObjects, not if you change the Scale

# Align the viewport
box = canvas.BoundingBox
canvas.ViewPortCenter -= (canvas.PixelToWorld((0,0)) -
numpy.array((box[0,0], box[1,1])))

couldn't you simply use canvas.ZoomtoBB?

# Let's draw
bmp = wx.EmptyBitmap(box.Width, box.Height)
dc = wx.MemoryDC()
dc.SelectObject(bmp)
dc.Clear()
canvas._DrawObjects(dc, canvas._DrawList, dc, canvas.BoundingBox)
dc.SelectObject(wx.NullBitmap)

The Canvas should be doing all this to its internal buffer anyway, you shouldn't have to do it yourself.

But none of that explains you issue. Also, given that you are creating a dc here yourself, there shouldn't be any clipping set -- very strange indeed.

Do you have some sample output, just to make sure we understand your symptoms?

-Chris

···

On 11/30/2011 3:08 AM, Joril wrote:

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

NOTE:

sending this to the wxPython list -- hopefully someone else will have an idea.

seems to be "cutting" the ScaledText objects over a
certain y-coordinate, so for example if I write something above this
limit, the text shows up on the exported file, while if it's below it
doesn't show, or gets cut at midway if it's across the limit.

So you get part of the text, but not all?

Exactly.

...

Are they running the same version of GTK? and the same theme?

Yes, and to make things weirder still, the machines are configured via
Puppet, so they are all quite alike... :confused:

very wierd.

couldn't you simply use canvas.ZoomtoBB?

I'll try that, thanks!

note that we might want to add a parameter to that method -- at teh moment, it "pads" the bounding box a bit, to give a boto f extra space abound your objects -- it would be nice if teh user could set that padding parameter to what they want.

But none of that explains you issue. Also, given that you are creating a
dc here yourself, there shouldn't be any clipping set -- very strange
indeed.

Do you have some sample output, just to make sure we understand your
symptoms?

Sure, here it is: ImageShack - Best place for all of your image hosting and image sharing needs
Those should be a "3" and a "4" (two ScaledText objects)... The other
lines are Spline objects.

wow! very wierd. A couple ideas (shots in the dark?)

FloatCanvas is setting the font size to scale up the fonts -- I had issues way back when with trying to scale them up too big -- freetype (or whatever) choked on it. I think there is a hard limit in ther now to limit that. So you might want to check if the effect is triggered at a particular large font size.

Somehow it looks like the font is getting clipped. you might try calling wx.DC.DestroyClippingRegion() before rendering -- that shouldn't be it, as your other drawobjects aren't clipped, but you never know...

Can you reproduce this at all outside FloatCanvas? Simply drawing big fonts to a DC directly?

Are there any similar rendering issues when displayed on screen?

-Chris

···

On 12/1/2011 5:37 AM, Joril wrote:

On Nov 30, 8:40 pm, Chris Barker<Chris.Bar...@noaa.gov> wrote:

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

> canvas.Scale = 1
> canvas.SetToNewScale(False)
> canvas._ResetBoundingBox()

The _ResetboundingBox only needs to be called if you've addded or
removed DrawObjects, not if you change the Scale

I tried removing that call, but it looks like without it the
BoundingBox is null, and I need it to figure out the size to give to
the bitmap for the MemoryDC... Am I missing something?

NOTE:

sending this to the wxPython list -- hopefully someone else will have an
idea.

Sorry about that, Google Groups kept saying "We were unable to post
your message" and I tried "reply to author" to see if it had the same
issue... :confused:

>> couldn't you simply use canvas.ZoomtoBB?

> I'll try that, thanks!

note that we might want to add a parameter to that method -- at teh
moment, it "pads" the bounding box a bit, to give a boto f extra space
abound your objects -- it would be nice if teh user could set that
padding parameter to what they want.

That would be handy :slight_smile: Anyway I tried ZoomToBB, but it looks like it
tries to fit everything inside the size of the FloatCanvas panel,
while I need the exported emage to be 1:1... I have to call it without
parameters, right?

FloatCanvas is setting the font size to scale up the fonts -- I had
issues way back when with trying to scale them up too big -- freetype
(or whatever) choked on it. I think there is a hard limit in ther now to
limit that. So you might want to check if the effect is triggered at a
particular large font size.

The default font size in my application is 20, and the effect happens
with that too :frowning:

Somehow it looks like the font is getting clipped. you might try calling
wx.DC.DestroyClippingRegion() before rendering -- that shouldn't be it,
as your other drawobjects aren't clipped, but you never know...

Tried that, still the same...

Can you reproduce this at all outside FloatCanvas? Simply drawing big
fonts to a DC directly?

I did try that a few days ago, and it worked correctly... Anyway I'll
try again just to be sure :slight_smile:

Are there any similar rendering issues when displayed on screen?

No, on screen everything is OK, I can even pan around without any
problem.

Many thanks again for your assistance!

···

On Nov 30, 8:40 pm, Chris Barker <Chris.Bar...@noaa.gov> wrote:
On Dec 1, 9:18 pm, Chris Barker <Chris.Bar...@noaa.gov> wrote:

The _ResetboundingBox only needs to be called if you've addded or
removed DrawObjects, not if you change the Scale

I tried removing that call, but it looks like without it the
BoundingBox is null, and I need it to figure out the size to give to
the bitmap for the MemoryDC... Am I missing something?

OK, you may need it -- but not because you've changed the scale -- it probably isn't calculated already anyway -- It can be a bit expensive to calculate the bounding box, so it's only done when needed, not every time something changes.

couldn't you simply use canvas.ZoomtoBB?

I'll try that, thanks!

That would be handy :slight_smile: Anyway I tried ZoomToBB, but it looks like it
tries to fit everything inside the size of the FloatCanvas panel,
while I need the exported emage to be 1:1... I have to call it without
parameters, right?

well, you can specify the bounding box you want to zoom too, but that's not going to get what you want -- I guess I wasn't paying careful attention -- you want to fit the size of the image to the drawing, whereas ZoomToBB fits the drawing to the image size.

So what you have is probably the way to do it. We might want to add a method that does that to FloatCanvas itself -- it would be like SaveAsImage, but you could specify the image size as different than the current Window size, and specify the BB you want to show (or have it auto-fit) you might also be able to specify the scale instead.

Contributions welcome :wink:

The default font size in my application is 20, and the effect happens
with that too :frowning:

Can you reproduce this at all outside FloatCanvas? Simply drawing big
fonts to a DC directly?

I did try that a few days ago, and it worked correctly... Anyway I'll
try again just to be sure :slight_smile:

You mignt put a few print statement in the ScaledText )draw method -- so you can know exactly what it's doing.

Are there any similar rendering issues when displayed on screen?

No, on screen everything is OK, I can even pan around without any
problem.

This is getting really wierd -- on screen, FC is drawing everything to a buffer )MemoryDC also, so it should be EXACTLY the same as what you are doing.

I still have no idea what the problem is, but another idea to try:

rather than creating a new DC and all that on your own, can you set the size of the Window to what you want, then do the regular SaveAsImage?

If you don't want to see it on screen, I'm pretty sure you can do all that without ever calling Show().

-Chris

···

On 12/2/2011 12:42 AM, Joril wrote:

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov