space-engineers-utilities icon indicating copy to clipboard operation
space-engineers-utilities copied to clipboard

Rework text wrapping code

Open enenra opened this issue 3 years ago • 1 comments

This works reportedly correctly:

def wrap_text(text: str, width: int) -> List[str]:
    '''
    Wrap the given text to the given width.

    Args:
        width: The maximum pixel width of each row.
        text: The text to be split and returned.

    Returns:
        A list of the split up text and empty space if necessary.
    '''
    return_text = []
    row_text = ''

    system = bpy.context.preferences.system
    dpi = 72 if system.ui_scale >= 1 else system.dpi
    blf.size(0, 10, dpi)

    for word in text.split():
        word = f' {word}'
        line_len, _ = blf.dimensions(0, row_text + word)

        if line_len <= (width - 16):
            row_text += word
        else:
            return_text.append(row_text)
            row_text = word

    if row_text:
        return_text.append(row_text)

    return return_text

def draw(self, context):
    col = self.layout.column()
    width = context.region.width
    ui_scale = context.preferences.system.ui_scale
    MSG = f"Or move your already purchased {ds_props.product} pack into this folder:"
    for text in wrap_text(MSG, 0.8*(width/ui_scale)):
        col.label(text=text)

I'm thinking the main issue of my attempts has been forgetting to include uiscale.

enenra avatar Dec 14 '22 17:12 enenra

Also this: https://b3d.interplanety.org/en/multiline-text-in-blender-interface-panels/

enenra avatar Jan 20 '23 12:01 enenra