Is it possible to precalculate the height of text to be printed in renpy?

1.3k views Asked by At

Renpy uses a lot of python and custom made code, in order to show text that is displayed on screen using the say statement.

After running into some troubles with the nvl mode within renpy, I found it necessary to know how many lines are going to be displayed on screen (taking into account the font size, naturally and the size of the text window).

So my question:

As I didn't find anything in the documentation in regards to that, I'm wondering if there is any command or other possibility to precalculate the height of a text that is to be displayed?

1

There are 1 answers

6
rocksteady On BEST ANSWER

get_virtual_layout() is part of class Text in text.py.

I copied this from text.py:

# Find the virtual-resolution layout.
virtual_layout = self.get_virtual_layout()
# The laid-out size of this Text.
vw, vh = virtual_layout.size

This looks promising, I think.

With the virtual text size (width, height) you could possibly calculate the lines of text by using the text window size (width, height).

pseudo code:
lines = int(vw/text_window.width)
#the text height would then be
text_height_needed = int(lines*vh)
# does it fit in completely
complete_text_in_window = text_window.height >= text_height_needed
# visible lines
visible_lines = int(text_window.height/vh)

Also, it is worth to take a deeper look at text.py(e.g. def render(self, width, height, st, at)), in order to get to know the use of virtual_layout.

I hope it helps somehow.

Update:

def render(...) initializes the virtual layouts, so get_virtual_layout() is not None anymore, but represents an instance of Layout() with scaled width and height.