Drawing text on a canvas

Guiserver, GTK-server, OpenGL, PostScript,
HTML 5, MIDI, IDE
Locked
didi
Posts: 166
Joined: Fri May 04, 2007 8:24 pm
Location: Germany

Drawing text on a canvas

Post by didi »

I want to write a string centered below a certain point on the canvas. The text-origins are located on the lower-left corner . How can i get the center of the string ?
I can get the length of a string and i have the size of the font , but how can i get the graphical-width of the string ?

Or should we first draw the strings , check the size and then relocated it ... but if i have a longer list ofs strings this would be a bit complicated. Any idea ?

m i c h a e l
Posts: 394
Joined: Wed Apr 26, 2006 3:37 am
Location: Oregon, USA
Contact:

Post by m i c h a e l »

Hi didi!

I hope the following will help:

Code: Select all

(gs:set-font 'ACanvas "SomeFont" 24 "plain")
(gs:get-font-metrics 'ACanvas "Text you want to measure")
get-font-metrics returns a list containing the width and height of the string you pass it. Make sure to set the font you're using first.

You can also use gs:font-metrics after a call to gs:get-font-metrics:

Code: Select all

(set 
	'half-width (/ (gs:font-metrics 0) 2)
	'half-height (/ (gs:font-metrics 1) 2))
m i c h a e l

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

You can also use gs:font-metrics after a call to gs:get-font-metrics:
yes, the reason for this is that the original gs:font-metrics call is relative expensive time wise. The gs:font-metrics caches/remembers the last call to gs:get-font-metrics. Similar mechanics exist for gs:get-screen, gs:get-fonts, gs:get-bounds and gs:get-version. They all cache the last call in a variable without the get- part.

Lutz

didi
Posts: 166
Joined: Fri May 04, 2007 8:24 pm
Location: Germany

Post by didi »

Thanks Michael and Lutz for your help :-)

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Updating text in canvas in event loop

Post by cormullion »

I decided to put my functional programming attempts to one side and start playing with the nice graphics instead :-)

First, I'd like to say congratulations, Lutz, on a really impressive piece of work. Powerful yet approachable - a good match for newLISP itself!

My first hesitant steps (where's that tutorial?) have been reasonably successful, although I'm generating questions at the rate of 20 per hour. This question, though, is the main one at present:

Code: Select all

(while (gs:check-event 5000)
  (gs:hide-tag 'T true)
  (gs:draw-text 'T (date (date-value) 0 "%Y-%m-%d %H:%M:%S") font-size 50))
- just a simple clock display. But is this the right way of drawing flicker-free text? I couldn't see anything else that would do the job.




oops - sorry for adding this to another thread...

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

Several comments:

- the 5000 in gs:check-event are micro seconds. You allow for about 200 updates per second. 30 would be max for the human eye. I would put a sleep into the loop, i.e. (sleep 200), so it gets a chance to check for events at least 5 times per second. The gs:check-event allows the system to process events, e.g. from a button pressed on the clock.

- Each time you use a gs:draw or gs:fill command you create new little tagged object. gs:hide-tag will hide it but not delete it try using gs:delete-tag instead. If you just hide it and create a new text piece 200 times per second your memory will fill up quickly ;-). Always make sure that your canvas has a background color. If not the deleted tag objects will still be visible. The canvas is always erased before draw updates using the background color.

- For your app a label with the time text would be better and easier to get flicker free, see frame-less-demo.lsp

Lutz

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

Thanks Lutz!

Will try the changes tomorrow...

Locked