Text manipulating with fonts
Author |
Message |
Raknarg
|
Posted: 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
|
|
|
Tony
|
Posted: 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 |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
Velocity
|
Posted: Tue Jan 24, 2012 11:53 pm Post subject: RE:Text manipulating with fonts |
|
|
font.width - 50? |
|
|
|
|
|
Raknarg
|
Posted: Wed Jan 25, 2012 9:19 am Post subject: RE:Text manipulating with fonts |
|
|
@Velocity if only it were that easy
@Toni Yeah, I figured that out last night, I'll probably do that.
Also, idk what a binary search is. |
|
|
|
|
|
smool
|
Posted: 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
|
Posted: Thu Feb 02, 2012 8:12 am Post subject: RE:Text manipulating with fonts |
|
|
Oh, yeah i get it. |
|
|
|
|
|
|
|