Dynamic Font Size in Skill Descriptions

Hello everyone,

I was wondering if there is a function in JS that allows for the size of the font to change dynamically depending of how long the string is inside a textbox is.

To be more specific, I am talking about skill descriptions. You know, when you hover over a skill in battle or in the game menu, you have a little box at the top that tells you what the skill does.

Now, I am working on a rogue-lite game in MV (think something along the lines of slay the spire, hades, Dead Cells etc.)
and so my skill descriptions have to be highly detailed and accurate.

The contents of skill descriptions change depending on the circumstances.
We are talking damage numbers, healing values, resource generation, conditions, skill classifications, keywords etc.
And depending on your choices, these things can change drastically for any given skill.

And in some circumstances, the amount of additional words, letters and numbers make the whole thing go beyond the textbox.
So to finally come to the main point, I'd like some kind of system where the size of the font shrinks if the textbox is exhausted of space.
Note: It would be optimal if the text would only ever shrink to accomodate the textbox and not increase in size ever.
Important: I currently have the ability to execute functions and script commands inside of the description box, which could help with the solution.

Think something like the wordwrap function, but instead of going to the next line, you make the font smaller to fit in everything.

Any suggestions are welcome.
I don't know if there's anything in the RPG Maker functions to already do this.

In the window method that draws the text, you could try a loop that says something like
Code:
while (this.textWidth(text) > /* pixel width of your window */)
    this.contents.fontSize--;
I made a topic a while back.
Though it seems description can only have 2 lines of text?
1721141383983.png
You can use something like the SRD supertools engine to make the help window longer than 2 lines, but it will always be that length, whether or not the help text is that long.

In the rpg_window file, you can see where the height of all help windows is set to 2 lines on creation:

JavaScript:
Window_Help.prototype.initialize = function(numLines) {
    var width = Graphics.boxWidth;
    var height = this.fittingHeight(numLines || 2);
    Window_Base.prototype.initialize.call(this, 0, 0, width, height);
    this._text = '';
};

From this you can see that it creates a help window that contains numLines worth of height, or 2 lines if numLines wasn't provided.

In Scene_Battle, we can see that when the help window is created, it isn't given a number of lines, so it defaults to 2:

JavaScript:
Scene_Battle.prototype.createHelpWindow = function() {
    this._helpWindow = new Window_Help();
    this._helpWindow.visible = false;
    this.addWindow(this._helpWindow);
};

At the point of creation, the text isn't yet written to the window, so you probably can't instantly base the height of the window on the contents of it, but you could likely change something in the update function for the help window in the battle scene to adjust the height based on how many line breaks the help text has.
At the point of creation, the text isn't yet written to the window, so you probably can't instantly base the height of the window on the contents of it, but you could likely change something in the update function for the help window in the battle scene to adjust the height based on how many line breaks the help text has.
I think the OP is asking for the opposite - for the font size to be changed to make it fit inside the static window size.

@utunnels I haven't looked at the other thread yet - does your plugin actually decrease the font size? The screenshot you showed looks like the letters are the same height but have been horizontally compacted, and is kind of hard to read.
Well no, it only changes the width. But the example is extreme, it should be readable if your text is not too long.
Thank you all for your quick responses.

@utunnels
So far it seems that your solution works. My texts *shouldn't* get that long, I was just afraid of some texts getting cut off a little bit from time to time.
Which means you basically provided exactly what I was looking for.

And luckily the font I am using is more or less readable even when a bit compacted.

So until I run into some new problem, I think I'll go with your solution.
Thank you very much!
I think the OP is asking for the opposite - for the font size to be changed to make it fit inside the static window size.

@utunnels I haven't looked at the other thread yet - does your plugin actually decrease the font size? The screenshot you showed looks like the letters are the same height but have been horizontally compacted, and is kind of hard to read.
That was more a response to @utunnels stating that the window was always only 2 lines tall, rather than specifically helping the OP. Although based on his initial description about skills needing long complicated and overly specific description text, it didn't seem unlikely that more lines in the help box would also help him.
@ramza
Honestly that might not be entirely wrong.
I might actually play around with multiple lines to organize the descriptions a little bit
and see what works better aesthethically and clarity wise.
I'm glad it helps.:LZScheeze:
By the way, yep message core also has a text wrap setting for description.
@ramza I gotcha 🙂

@ScorchedGround The other thing you could consider, based on your game setup, is having a Description command open up a separate window which can be larger and even scrollable.

Original thread: https://forums.rpgmakerweb.com/threads/dynamic-font-size-in-skill-descriptions.170358/