Drawing text on a canvas

Started by didi, July 16, 2007, 10:49:36 AM

Previous topic - Next topic

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

#1
Hi didi!



I hope the following will help:


(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:


(set
'half-width (/ (gs:font-metrics 0) 2)
'half-height (/ (gs:font-metrics 1) 2))

m i c h a e l

Lutz

#2
QuoteYou 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

#3
Thanks  Michael  and Lutz for your help  :-)

cormullion

#4
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:


(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

#5
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

#6
Thanks Lutz!



Will try the changes tomorrow...