def wrap(font, text, max_line_width) -> str:
    """
    Wraps the text onto newlines accordingly, based on the given boundaries.

    Parameters
    ----------
    font: PIL.ImageFont
        The font to be used for measurement.
    text: str
        The text to wrap.
    max_line_width: int
        The maximum width of a line.

    Returns
    -------
    str
        The text, split where needed to ensure no overflow.
    """
    words = text.split()

    lines = []
    line = []

    for word in words:
        newline = ' '.join(line + [word])

        w, h = font.getsize(newline)

        if w > max_line_width:
            lines.append(' '.join(line))
            line = [word]
        else:
            line.append(word)

    if line:
        lines.append(' '.join(line))

    return ('\n'.join(lines)).strip()