Forum Archive

Text-Size

NoneNone94

I want to make a code which shows a text on the screen and highlights any word when you touch it. so i need a function which returns size of any word which is drawn on the screen. so far the most relevant thing i found was scene.render_text. but it gives you an image too.

i am afraid that while computing a 300 word text, it loads 300 images (which i do not use) to the memory and have negative effect on performance.

what do you think i can do?

thnx

omz

The best I can think of right now would be to use the ImageDraw module for drawing your text into an image (and then load that image via load_pil_image). The ImageDraw class has a textsize method that you could use to calculate the width of individual words without actually having to draw them first.

Note that if you have a device with a retina screen, you might need to double all sizes when using the ImageDraw module (because it uses a pixel coordinate system and not points).

NoneNone94

Thank you. Actually i had seen the ImageDraw option but I preferred to stick to TextLayer. But I guess I have to know how images of scene.render_text are stored. As far as I realized they are not stored in the image directory so I guess they must be stored in the memory. If so if I do this:

For x in range(len(SomeStringList))
p=scene.render_text(SomeStringList[x],font,size)
S[x]=p[0]
del p

(storing just size and deleting the scene.render_text object) will it free the memory?

You know, actually I have the same question about remove_layer. Will remove_layer free the memory dedicated to removed layer? (because in my code I repeatedly remove and add the same layers)

Thnx again

polymerchm

How does the above apply when trying to draw text at a specific loction in a ui.View? I am writing a script to display fingering on a guitar,uke, etc neck and need to display note values in white on a filled black circle. The neck is set up as a custom View.

dgelessus

This thread is from 2012 where the ui module didn't exist yet, so the above all applies only to scene scripts. With a UI you're probably best off using a ui.Label and setting its font and text_color attributes.