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

Username:   Password: 
 RegisterRegister   
 Text manipulating with fonts
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Raknarg




PostPosted: Tue Jan 24, 2012 10:34 pm   Post subject: Text manipulating with fonts

Lets say you're given a font with a certain text, and the text can only be so long. Lets also say that this text can only be a 100 pixels long, and Font.Width returns a value of 150. How would I find the character of the text at 100 pixels? Is that possible?
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Tue Jan 24, 2012 10:53 pm   Post subject: RE:Text manipulating with fonts

Easy to do with fixed-width fonts, but presumably that's not the case. You could simply iterate over the string -- building the substring, and checking its length with Font.Width, until you find a closest match.

If the strings are long, you can do faster matches with smarter searches -- e.g. binary search
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Velocity




PostPosted: Tue Jan 24, 2012 11:53 pm   Post subject: RE:Text manipulating with fonts

font.width - 50?
Raknarg




PostPosted: Wed Jan 25, 2012 9:19 am   Post subject: RE:Text manipulating with fonts

@Velocity if only it were that easy Razz
@Toni Yeah, I figured that out last night, I'll probably do that.
Also, idk what a binary search is.
smool




PostPosted: Wed Feb 01, 2012 7:37 pm   Post subject: RE:Text manipulating with fonts

a binary search is a method of searching a sorted array. If we have an example of a string of numbers:
2 2 4 5 6 8 9 9

Say we want to find '9'. First we look at the element that is halfway through (element (8 div 2) = 4) which is 5. we know 9 is greater than 5, and we know the array is sorted. So we now look at the top half, and look at the element that is halfway through the top half (3/4), so element 6, which is 8. again, 9 > 8, so we continue by cutting the area in half once more, and now look at the seventh element, which is 9, which is what we were looking for.

TL;DR recursively searches through the array by looking halfway through, comparing, then doing the same on whatever half of the previous array.
Raknarg




PostPosted: Thu Feb 02, 2012 8:12 am   Post subject: RE:Text manipulating with fonts

Oh, yeah i get it.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: