Canvas>>font: bug or feature ?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Canvas>>font: bug or feature ?

Chris Uppal-3
I've suddenly started to hit problems caused by the implementation of
Canvas>>font: -- I don't know why it's started suddenly (not caused by
installing the patches), and I don't know whether it's my understanding that's
wrong or a bug in #font:.

Anyway, I have code that could be paraphrased like:

paintWithFontOn: aCanvas
    | newFont oldFont |
    newFont := ... create a font ...
    oldFont := aCanvas font: newFont.
    [self doSomeMorePaintingOn: aCanvas]
        ensure: [aCanvas font: oldFont.  newFont free].


Which is how I thought you are supposed to handle fonts.   The problem is that
if the canvas originally had a nil font, then restoring it will cause a
walkback when Canvas>>font: does:

    font resolution: self resolution.

That's easily "fixed" by adding an isNil test, but what I don't understand is
how the next line:

    self selectObject: aFont.

removes the temporary font from the canvas, since it's just doing a
#selectObject: of nil.  OTOH, if I extend the isNil test so that it doesn't do
the #selectObject: if the font's nil, but won't that leave the temporary font
selected into the canvas when the caller #free-s it ?

I've patched #font: in my image, and the walkbacks have gone away, but I'm not
sure what else I may have broken...

    -- chris


Reply | Threaded
Open this post in threaded view
|

Re: Canvas>>font: bug or feature ?

Blair McGlashan-2
"Chris Uppal" <[hidden email]> wrote in message
news:3fb4ec3c$0$107$[hidden email]...
> I've suddenly started to hit problems caused by the implementation of
> Canvas>>font: -- I don't know why it's started suddenly (not caused by
> installing the patches), and I don't know whether it's my understanding
that's
> wrong or a bug in #font:.
>

Hmmm, I think the bug is that Canvas>>font: should not really return nil.
The simplest patch for this is to replace the direct access to the 'font'
instance variable with an invocation of the #font accessor method.

> Anyway, I have code that could be paraphrased like:
>
> paintWithFontOn: aCanvas
>     | newFont oldFont |
>     newFont := ... create a font ...
>     oldFont := aCanvas font: newFont.
>     [self doSomeMorePaintingOn: aCanvas]
>         ensure: [aCanvas font: oldFont.  newFont free].

The workaround would be to write it like this:

paintWithFontOn: aCanvas
     | newFont oldFont |
     newFont := ... create a font ...
     oldFont := aCanvas font.
     aCanvas font: newFont.
     [self doSomeMorePaintingOn: aCanvas]
         ensure: [aCanvas font: oldFont.  newFont free].

Or you can apply the hotfix below.

Thanks for the report.

Blair

------------------
"#1390"

!Canvas methodsFor!

font: aFont
 "Select aFont into this canvas. Ensure that the font is realized for the
receiver's
 resolution. Answer the previously selected font."

 | oldFont |
 oldFont := self font.
 font := aFont.
 font resolution: self resolution.
 self selectObject: aFont.
 ^oldFont! !
!Canvas categoriesFor: #font:!public!tools! !


Reply | Threaded
Open this post in threaded view
|

Re: Canvas>>font: bug or feature ?

Chris Uppal-3
Blair McGlashan wrote:

> Or you can apply the hotfix below.

Thanks for that.  I've applied the hotfix and my problems have gone away.

Some of them anyway ;-)

    -- chris