Bug: renpy.render().get_size() hates spaces

In this forum we discuss the future of Ren'Py, both bug fixes and longer-term development. Pre-releases are announced and discussed here.
Post Reply
Message
Author
User avatar
Milkymalk
Miko-Class Veteran
Posts: 614
Joined: Wed Nov 23, 2011 5:30 pm
Completed: Don't Look (AGS game)
Projects: KANPEKI! ★Perfect Play★
Organization: Crappy White Wings
Location: Germany
Contact:

Bug: renpy.render().get_size() hates spaces

#1 Post by Milkymalk » Thu Jun 29, 2017 9:30 pm

I had a screen where I wanted to center variable lines of text on a vertical column of images. To get the size of the text displayable, I used this, which I got from somewhere in the forum:

Code: Select all

python:
    def get_size(displayable):
        return (int(renpy.render(renpy.easy.displayable(displayable), 0, 0, 0, 0).get_size()[0]),
            int(renpy.render(renpy.easy.displayable(displayable), 0, 0, 0, 0).get_size()[1]))
I had used it before with images and it always worked. But this time, some texts were properly centered and some just weren't. Applying my Sherlock-Holmes-skills, I found out that spaces inside the string throw off the returned values, depending on how many spaces there are.

Try-and-error has brought me to an approximation of this correction IF spaces are present:

Code: Select all

correcttextx = returned_xsize + 70 + 14*textstring.count(' ')
correcttexty = returned_ysize - 21*textstring.count(' ') # <-- This is especially strange
This was tested with 1 and 9 spaces. It might be off for other numbers and even depend on which letters the space divides.
So in conclusion, it seems that spaces make the measured x-size smaller and the measured y-size larger than it should be.
Crappy White Wings (currently quite inactive)
Working on: KANPEKI!
(On Hold: New Eden, Imperial Sea, Pure Light)

User avatar
PyTom
Ren'Py Creator
Posts: 15056
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Bug: renpy.render().get_size() hates spaces

#2 Post by PyTom » Fri Jun 30, 2017 12:06 am

That's not a particularly good way to do it.

The low-level problem is that the second argument to renpy.render should be a width. Since it's 0, the text wraps after every line. If you made it, say, 800 or 1280, you'd likely get the numbers you want.

That being said, this is kind of overkill. Why not just do:

Code: Select all

vbox:
    add "myimage" xalign 0.5
    text "This is the description." xalign 0.5
And let Ren'Py do this for you?
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
"Silly and fun things are important." - Elon Musk
Software > Drama • https://www.patreon.com/renpytom

User avatar
Milkymalk
Miko-Class Veteran
Posts: 614
Joined: Wed Nov 23, 2011 5:30 pm
Completed: Don't Look (AGS game)
Projects: KANPEKI! ★Perfect Play★
Organization: Crappy White Wings
Location: Germany
Contact:

Re: Bug: renpy.render().get_size() hates spaces

#3 Post by Milkymalk » Fri Jun 30, 2017 12:21 am

There's several images of varying height that are put in a vbox. If I want the text for each image to be exactly centered, I need to know its dimensions and also the image's. I guess I could put each image in its own cell or something and xalign 0.5 yalign 0.5 both, but when I wrote it it felt more natural to me to make it a LiveComposite.

Ah, so the line breaks explain this. I didn't put anything other than 0 there because I expected it to actually return that number if the displayable was smaller than that. If that isn't the case, I know what to do. Thanks!

Line breaks, huh. I could have gotten this by myself based on my observations :D
Crappy White Wings (currently quite inactive)
Working on: KANPEKI!
(On Hold: New Eden, Imperial Sea, Pure Light)

User avatar
PyTom
Ren'Py Creator
Posts: 15056
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Bug: renpy.render().get_size() hates spaces

#4 Post by PyTom » Fri Jun 30, 2017 10:16 pm

Yep. Try:

image weirdtext = Text("The quick frown box jumped over the lazy dog.", xsize=0)

to see what it looks like.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
"Silly and fun things are important." - Elon Musk
Software > Drama • https://www.patreon.com/renpytom

Post Reply

Who is online

Users browsing this forum: No registered users