Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Helpful text drawing procedures
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Axlle




PostPosted: Wed Jun 20, 2007 2:50 pm   Post subject: Helpful text drawing procedures

This is my frist day using this forum but I thought since there are so many ppl using turing (well, more than i expected) that I should post some useful programs for others to use.

These three procedures are better than using Font.Draw since they will clip the string between x1-x2, center it vertically using 'h' and properly align the text.

%

procedure clip (x, y, w, h : int)
View.ClipSet (x, y - 1, x + w, y + h - 1)
end clip

procedure alignLeft (str : string, x1, y, x2, height, font, clr : int)
var right : int := x2
if x2 = 0 then
right := x1 + Font.Width (str, font) - 1
end if

var h, a, d, i : int
Font.Sizes (font, h, a, d, i)

var y2 : int := y + height - 1
if height = 0 then
y2 := y + h - 1
end if

clip (x1, y, right - x1 + 1, y2 - y + 1)
Font.Draw (str, x1, y + (y2 - y + 1) div 2 - h div 2 + d, font, clr)
View.ClipOff
end alignLeft

procedure alignCenter (str : string, x1, y, x2, height, font, clr : int)
if x2 < x1 then
return
end if
var midBox : int := (x2 - x1) div 2 + x1
var midStr : int := Font.Width (str, font) div 2

var h, a, d, i : int
Font.Sizes (font, h, a, d, i)

var y2 : int := y + h - 1
if height = 0 then
y2 := y + h - 1
end if

clip (x1, y, x2 - x1 + 1, y2 - y + 1)
Font.Draw (str, midBox - midStr + (Font.Width (str, font) + 1) mod 2,
y + (y2 - y + 1) div 2 - h div 2 + d, font, clr)
View.ClipOff
end alignCenter

procedure alignRight (str : string, x1, y, x2, height, font, clr : int)
var left : int := x1
if x1 = 0 then
left := x2 - Font.Width (str, font) + 1
end if

var h, a, d, i : int
Font.Sizes (font, h, a, d, i)

var y2 : int := y + height - 1
if height = 0 then
y2 := y + h - 1
end if

var strL : int := x2 - Font.Width (str, font) + 1

clip (left, y, x2 - left + 1, y2 - y + 1)
Font.Draw (str, strL, y + (y2 - y + 1) div 2 - h div 2 + d, font, clr)
View.ClipOff
end alignRight

%

helpful notes:

-setting x1 to 0 for alignRight and x2 to 0 for alignLeft will cause them to accomidate for the length of the string
-setting 'h' to 0 will set the height to Turings predefined character height

Axlle

edit: after posting this program ive realized many glitches with it and will fix them shortly
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: