Jump to content


Photo

Resizing text to fill a space.


  • This topic is locked This topic is locked
6 replies to this topic

#1 Shadow Archangel

Shadow Archangel

    GMC Member

  • GMC Member
  • 163 posts
  • Version:GM:Studio

Posted 09 April 2012 - 12:51 AM

Hi all.

I'm pretty sure this needs to be up here, because I don't think the solution is one to be found in the Novice/Intermediate forum.

I have a script that will re-size text to text within the bounds of a certain area. My script is as follows:

var card_lore, px, py, space_width, space_height, lore_height, factor;
draw_set_font(font_card_lore) ;

card_lore = argument0 ;
px = 35.2 ;
py = 398.2 ;
space_width = 272 ;
space_height = 50 ;
lore_height = string_height_ext(card_lore,-1,space_width) ;

if (lore_height > space_height)
{factor = space_height / lore_height}
else
{factor = 1} ;

draw_text_ext_transformed(px,py,card_lore,-1,space_width / factor,factor,factor,0) ;

This works, more or less, except that every time the text needs to get smaller, it ends up wrapping back onto less lines, and not utilizing all of it's vertical space. This is a bit of a problem because the text is already pretty small to start with.

Does anybody know how I might scale it down just enough so that it fills the space, rather than leaving empty lines at the bottom?

Edited by Shadow Archangel, 09 April 2012 - 12:52 AM.

  • 0

#2 slayer 64

slayer 64

    GMC Member

  • GMC Member
  • 3834 posts
  • Version:GM8.1

Posted 09 April 2012 - 02:26 AM

can i see a picture of what the text should look like on the screen? does the string being passed get longer? like if some in the game was talking
  • 0

#3 Shadow Archangel

Shadow Archangel

    GMC Member

  • GMC Member
  • 163 posts
  • Version:GM:Studio

Posted 09 April 2012 - 03:13 AM

Certainly.
Here's a picture of how the text looks now, auto scaled based on whether it fits vertically into the space. Notice the gap at the end of the card text above the horizontal line.
Posted Image

Here's how it should look. To achieve this effect I simply typed in the scale values manually.
Posted Image

The string being passed could be one of several thousand different card effects and lore, all with varying lengths. As I need a singe script to draw all of these texts in place, I need the text to be re-sized automatically, not to mention that it would be very time consuming to trial and error each string until I find the correct scale.
  • 0

#4 Shadow Archangel

Shadow Archangel

    GMC Member

  • GMC Member
  • 163 posts
  • Version:GM:Studio

Posted 14 April 2012 - 01:54 PM

Hi again,

I'm still struggling with this, and clearly nobody's found an answer yet, so I figured I'd post some of my thoughts on the matter.

One of the problems that I think contributes here is the lack of a string_height_transformed function, or something allowing us to measure the height of scaled text. Then I realised that this can be achieved, of sorts. To do this, we take string_width and then multiply that by factor, then divide that by space_width, which tells us how many lines there will be. Multiply that by string_height (not string_height_ext) and we get close, though this doesn't (to my knowledge) factor in the spacing between lines. Regardless, I think that this gives us an approximation on the height of the scaled text... am I wrong?
  • 0

#5 C_Pike

C_Pike

    GMC Member

  • GMC Member
  • 618 posts
  • Version:GM8.1

Posted 14 April 2012 - 08:31 PM

Hi again,

I'm still struggling with this, and clearly nobody's found an answer yet, so I figured I'd post some of my thoughts on the matter.

One of the problems that I think contributes here is the lack of a string_height_transformed function, or something allowing us to measure the height of scaled text. Then I realised that this can be achieved, of sorts. To do this, we take string_width and then multiply that by factor, then divide that by space_width, which tells us how many lines there will be. Multiply that by string_height (not string_height_ext) and we get close, though this doesn't (to my knowledge) factor in the spacing between lines. Regardless, I think that this gives us an approximation on the height of the scaled text... am I wrong?


I think you might be able to get the string_height(your_string) and compare it to your text box height. I suck at math, but you should be able to divide up the leftover space (from the bottom the the string to the bottom of the text box) between the lines of text.
  • 0

#6 torigara

torigara

    GMC Member

  • GMC Member
  • 6507 posts

Posted 23 April 2012 - 05:37 AM

The real problem is that you're calculating factor as if the text only shrink vertically, while it affects on both of width and height of each letter. As letters get smaller, more words can go into the same width of space, making it take less lines. Taking this into account, the final height will become around the height of original text * factor2. So, you can estimate the desired factor by taking a square root.
factor = sqrt(space_height / lore_height);

But it's only an approximation. The actual number of lines can vary depending on the contents of the text and can't be derived with a simple math because of word wraps. Consider the example below:
+-------------+
|a bc defghij |
|kl mno pq rs |
|tuv wxyz     |
+-------------+

+-------------+
|a bc kl mno  |
|defghij pq rs|
|tuv wxyz     |
+-------------+
Those two texts consit of the exact same letters; only the position of words are transposed. Now suppose that both text is scaled by 0.8 (the same effect as making the space 1/0.8=1.25 times wider):
+------------------+
|a bc defghij kl   |
|mno pq rs tuv wxyz|
+------------------+

+------------------+
|a bc kl mno       |
|defghij pq rs tuv |
|wxyz              |
+------------------+
Now the first one takes two lines, reducing the height into 0.8*2/3=0.53. On the other hand, the second one still takes three lines (because there isn't enough space to move the word "defghij" in the first line), only shrinking the text 0.8 times in height. The example indicates that you can't know the exact height of scaled text without actually rendering it inside the given space.

To calculate the exact factor, you may need numerical calcuration to find the answer close enough to the desired result. For starters, when the value of factor is given, the final height of the text can be calculated as below.
transformed_height = string_height_ext(str, -1, space_width / factor) * factor
You can, for example, keep reducing the factor until it fits in the given space.
factor = 1;
while (string_height_ext(card_lore, -1, space_width / factor) * factor > space_height)
{
    factor -= 0.05;
}

Edited by torigara, 23 April 2012 - 05:45 AM.

  • 1

#7 Shadow Archangel

Shadow Archangel

    GMC Member

  • GMC Member
  • 163 posts
  • Version:GM:Studio

Posted 01 May 2012 - 02:22 AM

The real problem is that you're calculating factor as if the text only shrink vertically, while it affects on both of width and height of each letter. As letters get smaller, more words can go into the same width of space, making it take less lines.

I knew this was where my problem was coming from, I was just struggling with the solution. :P


You can, for example, keep reducing the factor until it fits in the given space.

factor = 1;
while (string_height_ext(card_lore, -1, space_width / factor) * factor > space_height)
{
    factor -= 0.05;
}

This is absolutely perfect. Here I was trying to find a way to mathematically calculate the value I needed to scale the text by, when all that was really needed was to trial and error until the text fitted into the box. I guess it's sometimes easy to look past the easy solutions.

Thank you so much. You've helped me out in a big way.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users