Lisp HUG Maillist Archive

Weird DRAW-ADJUSTED-STRING behaviour

Hi there,

I've been trying to get DRAW-ADJUSTED-STRING function to work properly for  
some time now but it appears suprisingly common sense proof. I was  
thinking to move on and spend few minutes writing my own operational  
version but at this stage getting it to do its job is the principle of  
thing. (It thinks it won't)

Have a look at this bit of code (LW Pro 5.0.2 on W7 64):

(let ((pane (capi:contain (make-instance 'capi:output-pane))))
   (gp:set-graphics-port-coordinates pane :left -5 :bottom -5 :top 105  
:right 105)
   (gp:draw-line pane 50 0 50 100 :dashed t)
   (dolist (adjustment '(:left :centre :right))
     (gp:draw-adjusted-string pane "X" 50 50 adjustment)))


It is supposed to draw a dashed vertical line in the middle of an output  
pane and then put three Xs:

1) One to the left of the line.
2) One with the line going through the middle of the X.
3) One to the right of the line.

However, something not exactly expected shows up (see the image attached.)

The one that is supposed to be drawn to the left works fine,
the one that is supposed to be in the centre is drawn to the right of the  
line,
and the one on the right is way too right.

Has anyone on the list dealt with it at some stage in the past?

Re: Weird DRAW-ADJUSTED-STRING behaviour

2010/10/22 Yuri Davidovsky <yury.davidouski2@mail.dcu.ie>:
> I've been trying to get DRAW-ADJUSTED-STRING function to work properly for
> some time now but it appears suprisingly common sense proof. I was thinking
> to move on and spend few minutes writing my own operational version but at
> this stage getting it to do its job is the principle of thing. (It thinks it
> won't)
>
> (let ((pane (capi:contain (make-instance 'capi:output-pane))))
>  (gp:set-graphics-port-coordinates pane :left -5 :bottom -5 :top 105 :right
> 105)
>  (gp:draw-line pane 50 0 50 100 :dashed t)
>  (dolist (adjustment '(:left :centre :right))
>    (gp:draw-adjusted-string pane "X" 50 50 adjustment)))
>
>
> It is supposed to draw a dashed vertical line in the middle of an output
> pane and then put three Xs:
>
> 1) One to the left of the line.
> 2) One with the line going through the middle of the X.
> 3) One to the right of the line.
>
> However, something not exactly expected shows up (see the image attached.)
>
> The one that is supposed to be drawn to the left works fine,
> the one that is supposed to be in the centre is drawn to the right of the
> line,
> and the one on the right is way too right.

I'm on OS X, but I get the same behavior. The issue seems to be the
port coordinates and the fact that gp:get-string-extent returns the
extent a string in pixels.  gp:draw-adjusted-string calls
gp:draw-x-y-adjusted string which in turn computes the extent of the
string.  Look what happens in the code with and without the
port-coordinates set, showing the string extent as computed by
gp:get-string-extent (which gp:draw-x-y-adjusted-string uses) with
tracing of gp:draw-string:

;; without coordinates set
(let ((pane (capi:contain (make-instance 'capi:output-pane))))
  ;; (gp:set-graphics-port-coordinates pane :left -5 :bottom -5 :top
105 :right 105)
  (gp:draw-line pane 50 0 50 100 :dashed t)
  (print (multiple-value-list (gp:get-string-extent pane "X")))
  (dolist (adjustment '(:left :center :right))
    (gp:draw-adjusted-string pane "X" 50 50 adjustment)))

(0 -11 6 3)
0 GRAPHICS-PORTS:DRAW-STRING > ...
  >> GRAPHICS-PORTS::PORT : #<CAPI:OUTPUT-PANE  2F68BD37>
  >> STRING               : "X"
  >> GRAPHICS-PORTS::X    : 50
  >> GRAPHICS-PORTS::Y    : 50
0 GRAPHICS-PORTS:DRAW-STRING < ...
0 GRAPHICS-PORTS:DRAW-STRING > ...
  >> GRAPHICS-PORTS::PORT : #<CAPI:OUTPUT-PANE  2F68BD37>
  >> STRING               : "X"
  >> GRAPHICS-PORTS::X    : 47
  >> GRAPHICS-PORTS::Y    : 50
0 GRAPHICS-PORTS:DRAW-STRING < ...
0 GRAPHICS-PORTS:DRAW-STRING > ...
  >> GRAPHICS-PORTS::PORT : #<CAPI:OUTPUT-PANE  2F68BD37>
  >> STRING               : "X"
  >> GRAPHICS-PORTS::X    : 44
  >> GRAPHICS-PORTS::Y    : 50
0 GRAPHICS-PORTS:DRAW-STRING < ...
NIL

;; with coordinates set
(let ((pane (capi:contain (make-instance 'capi:output-pane))))
  (gp:set-graphics-port-coordinates pane :left -5 :bottom -5 :top 105
:right 105)
  (gp:draw-line pane 50 0 50 100 :dashed t)
  (print (multiple-value-list (gp:get-string-extent pane "X")))
  (dolist (adjustment '(:left :center :right))
    (gp:draw-adjusted-string pane "X" 50 50 adjustment)))

(0 -11 6 3)
0 GRAPHICS-PORTS:DRAW-STRING > ...
  >> GRAPHICS-PORTS::PORT : #<CAPI:OUTPUT-PANE  2F1DC927>
  >> STRING               : "X"
  >> GRAPHICS-PORTS::X    : 50
  >> GRAPHICS-PORTS::Y    : 50
0 GRAPHICS-PORTS:DRAW-STRING < ...
0 GRAPHICS-PORTS:DRAW-STRING > ...
  >> GRAPHICS-PORTS::PORT : #<CAPI:OUTPUT-PANE  2F1DC927>
  >> STRING               : "X"
  >> GRAPHICS-PORTS::X    : 47
  >> GRAPHICS-PORTS::Y    : 50
0 GRAPHICS-PORTS:DRAW-STRING < ...
0 GRAPHICS-PORTS:DRAW-STRING > ...
  >> GRAPHICS-PORTS::PORT : #<CAPI:OUTPUT-PANE  2F1DC927>
  >> STRING               : "X"
  >> GRAPHICS-PORTS::X    : 44
  >> GRAPHICS-PORTS::Y    : 50
0 GRAPHICS-PORTS:DRAW-STRING < ...

In both cases the x and y passed to draw-string are the same, and this
produces different results since the coordinates have been set. As an
extreme example, we could push the right and left adjusted "X"'s off
the screen, since we (now) know that X is 6 pixels wide, by setting
the left and right coordinates to something small enough:

(let ((pane (capi:contain (make-instance 'capi:output-pane))))
  (gp:set-graphics-port-coordinates pane :left -2 :bottom -5 :top 5 :right 3)
  (print (multiple-value-list (gp:get-string-extent pane "X")))
  (dolist (adjustment '(:left :center :right))
    (gp:draw-adjusted-string pane "X" 0 0 adjustment)))

Since you know your port-coordinates, it won't be hard to write your
own draw-adjusted-string-respecting-port-coordinates.

//JT

-- 
Joshua Taylor, http://www.cs.rpi.edu/~tayloj/


Re: Weird DRAW-ADJUSTED-STRING behaviour

On Fri, 22 Oct 2010 18:29:50 +0100, Joshua TAYLOR <tayloj@cs.rpi.edu>  
wrote:

> In both cases the x and y passed to draw-string are the same, and this
> produces different results since the coordinates have been set.

I get the message, although the function takes x and y coords according to  
the scale of the pane the string extent is computed in pixels, I wrote a  
work-around for it, thanks Joshua.

1:0 to DRAW-ADJUSTED-STRING:)


Re: Weird DRAW-ADJUSTED-STRING behaviour

On Sat, 23 Oct 2010 08:37:23 +0100, Nick Levine <ndl@ravenbrook.com> wrote:

> Hmm. My problem with draw-adjusted-string is that I've never heard of
> it before, and it doesn't appear to be listed in the GP manual. Any
> pointers anyone?

Just to make matters clear, I came across it in one of the stock code  
examples, as far as I can remember.


Updated at: 2020-12-10 08:38 UTC