[plt-scheme] get-text-extent problem
At Mon, 4 Aug 2008 09:41:24 +0100, "Stephen De Gabrielle" wrote:
> I'm using get-text-extent to determine the width a string to be
> included in a snip,
>
> (define/override get-extent
> (lambda (dc x y w h descent space lspace rspace)
> ;(for-each (lambda (b) (when (box? b) (set-box! b 0)))
> ; (list descent space lspace rspace))
>
> (let-values (((width height b s) (send dc get-text-extent
> (format "~A" my-string))))
> (when (box? w) (set-box! w width))
> (when (box? h) (set-box! h height)))))
>
> The problem is the width is wider when get-extent calls
> get-text-extent than when get-text-extent is called by the (snip)draw
> method.
When `draw' is called, the snip's font is already set in the DC. When
`get-extent' is called, the font is not automatically set.
So, to get the right size, you need to specify the font in
`get-extent':
(inherit get-style) ....
(send dc get-text-extent (format "~A" my-string)
(send (get-style) get-font))
Matthew