Ejemplo n.º 1
0
    def layout(self, text, width, align, wrap):
        text = unicodedata.normalize('NFKC', text)
        widths = [str_util.get_width(ord(x)) for x in text]
        text_width = sum(widths)
        if text_width <= width:
            return [[(text_width, 0, text.encode('utf-8'))]]

        full_len = max(width - 1, 2)
        left = int(full_len / 2)
        right = full_len - left

        left_width = 0
        for i, e in enumerate(widths):
            left_width += e
            if left_width > left:
                i_left = i
                break

        right_width = 0
        for i, e in enumerate(reversed(widths)):
            right_width += e
            if right_width > right:
                i_right = len(text) - i
                break

        tilde_text = f'{text[:i_left]}~{text[i_right:]}'
        if len(tilde_text) <= 3:
            widths = [str_util.get_width(ord(x)) for x in tilde_text]
            text_width = sum(widths)
            while text_width > width:
                tilde_text = tilde_text[:-1]
                text_width -= widths.pop(-1)

        return [[(width, 0, tilde_text.encode('utf-8'))]]
Ejemplo n.º 2
0
def separate_glyphs(gdata, height):
    """return (dictionary of glyphs, utf8 required)"""
    gl = gdata.split("\n")
    del gl[0]
    del gl[-1]
    for g in gl:
        assert "\t" not in g
    assert len(gl) == height + 1, repr(gdata)
    key_line = gl[0]
    del gl[0]
    c = None  # current character
    key_index = 0  # index into character key line
    end_col = 0  # column position at end of glyph
    start_col = 0  # column position at start of glyph
    jl = [0] * height  # indexes into lines of gdata (gl)
    dout = {}
    utf8_required = False
    while True:
        if c is None:
            if key_index >= len(key_line):
                break
            c = key_line[key_index]
        if key_index < len(key_line) and key_line[key_index] == c:
            end_col += str_util.get_width(ord(c))
            key_index += 1
            continue
        out = []
        for k in range(height):
            l = gl[k]
            j = jl[k]
            y = 0
            fill = 0
            while y < end_col - start_col:
                if j >= len(l):
                    fill = end_col - start_col - y
                    break
                y += str_util.get_width(ord(l[j]))
                j += 1
            assert y + fill == end_col - start_col, \
                repr((y, fill, end_col))

            segment = l[jl[k]:j]
            if not SAFE_ASCII_DEC_SPECIAL_RE.match(segment):
                utf8_required = True

            out.append(segment + " " * fill)
            jl[k] = j

        start_col = end_col
        dout[c] = (y + fill, out)
        c = None
    return dout, utf8_required
Ejemplo n.º 3
0
def separate_glyphs(gdata, height):
    """return (dictionary of glyphs, utf8 required)"""
    gl = gdata.split("\n")
    del gl[0]
    del gl[-1]
    for g in gl:
        assert "\t" not in g
    assert len(gl) == height+1, repr(gdata)
    key_line = gl[0]
    del gl[0]
    c = None # current character
    key_index = 0 # index into character key line
    end_col = 0 # column position at end of glyph
    start_col = 0 # column position at start of glyph
    jl = [0]*height # indexes into lines of gdata (gl)
    dout = {}
    utf8_required = False
    while True:
        if c is None:
            if key_index >= len(key_line):
                break
            c = key_line[key_index]
        if key_index < len(key_line) and key_line[key_index] == c:
            end_col += str_util.get_width(ord(c))
            key_index += 1
            continue
        out = []
        for k in range(height):
            l = gl[k]
            j = jl[k]
            y = 0
            fill = 0
            while y < end_col - start_col:
                if j >= len(l):
                    fill = end_col - start_col - y
                    break
                y += str_util.get_width(ord(l[j]))
                j += 1
            assert y + fill == end_col - start_col, \
                repr((y, fill, end_col))

            segment = l[jl[k]:j]
            if not SAFE_ASCII_DEC_SPECIAL_RE.match(segment):
                utf8_required = True

            out.append(segment + " " * fill)
            jl[k] = j

        start_col = end_col
        dout[c] = (y + fill, out)
        c = None
    return dout, utf8_required
Ejemplo n.º 4
0
    def layout(self, text, width, align, wrap):
        text = unicodedata.normalize('NFKC', text)
        in_error = True
        while in_error:
            try:
                utf8_text = text.encode('utf-8')
                in_error = False
            except UnicodeError as e:
                text = ''.join([
                    text[:e.start], '\uFFFD' * (e.end - e.start), text[e.end:]
                ])

        widths = [str_util.get_width(ord(x)) for x in text]
        text_width = sum(widths)
        if text_width <= width:
            return [[(text_width, 0, utf8_text)]]

        full_len = max(width - 1, 2)
        left = int(full_len / 2)
        right = full_len - left

        left_width = 0
        for i, e in enumerate(widths):
            left_width += e
            if left_width > left:
                i_left = i
                break

        right_width = 0
        for i, e in enumerate(reversed(widths)):
            right_width += e
            if right_width > right:
                i_right = len(text) - i
                break

        tilde_text = f'{text[:i_left]}~{text[i_right:]}'
        if len(tilde_text) <= 3:
            widths = [str_util.get_width(ord(x)) for x in tilde_text]
            text_width = sum(widths)
            while text_width > width:
                tilde_text = tilde_text[:-1]
                text_width -= widths.pop(-1)

        return [[(width, 0, tilde_text.encode('utf-8'))]]
Ejemplo n.º 5
0
def check_character_width(text: str) -> (str, int):
    new_text = text
    char_width = 1

    half_width_char, full_width_char = 0, 0
    for char in text:
        char_wid = str_util.get_width(ord(char))
        if char_wid == 1:
            half_width_char += 1
        elif char_wid == 2:
            full_width_char += 1
    if full_width_char > 0:
        new_text = half2full(text)
        char_width = 2
    return new_text, char_width