Exemplo n.º 1
0
def deserialise(rows):
    # Initialize with defaults
    current = Key()
    meta, keys = {'backcolor': '#EEEEEE'}, []
    color_format = re.compile(r'^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$')
    default_size = current.label_sizes[0]
    with open('fonts/fa2unicode.json') as fa, open(
            'fonts/kbd-webfont2unicode.json') as kb:
        fa_subs, kb_subs = json.load(fa), json.load(kb)

    for row in rows:
        if isinstance(row, list):
            for key in row:
                if isinstance(key, str):
                    newKey = copy.copy(current)
                    newKey.labels, newKey.pic = get_labels(
                        key, fa_subs, kb_subs)
                    keys.append(newKey)

                    # Set up for the next key
                    current.x += current.width
                    current.width = current.height = 1.0
                    current.x2 = current.y2 = current.width2 = current.height2 = 0.0
                    current.pic = current.step = current.decal = False
                else:
                    if 'r' in key:
                        current.rotation_angle = key['r']
                    if 'rx' in key:
                        current.rotation_x = key['rx']
                        current.x = current.y = 0
                    if 'ry' in key:
                        current.rotation_y = key['ry']
                        current.y = current.y = 0
                    if 'a' in key:
                        current.align = int(key['a'])
                    if 'f' in key:
                        default_size = float(key['f'])
                        current.label_sizes = [default_size] * 12
                    if 'f2' in key:
                        current.label_sizes = [float(key['f2'])] * 12
                    if 'fa' in key:
                        label_sizes = [
                            float(size) if size > 0 else default_size
                            for size in key['fa']
                        ]
                        current.label_sizes = label_sizes[:12] + [
                            default_size
                        ] * (12 - len(label_sizes))
                    if 'p' in key:
                        current.str_profile = key['p']
                    if 'c' in key:
                        color = key['c'].replace(';', '')
                        current.color = color if color_format.match(
                            color) else current.color
                    if 't' in key:
                        colors = [
                            line.replace(';', '')
                            for line in key['t'].splitlines()
                        ]
                        default_color = colors[0] if color_format.match(
                            colors[0]) else '#000'
                        colors = [
                            color
                            if color_format.match(color) else default_color
                            for color in colors
                        ]
                        current.label_colors = colors[:12] + [
                            default_color
                        ] * (12 - len(colors))
                    if 'x' in key:
                        current.x += float(key['x'])
                    if 'y' in key:
                        current.y += float(key['y'])
                    if 'w' in key:
                        current.width = float(key['w'])
                    if 'h' in key:
                        current.height = float(key['h'])
                    if 'x2' in key:
                        current.x2 = float(key['x2'])
                    if 'y2' in key:
                        current.y2 = float(key['y2'])
                    if 'w2' in key:
                        current.width2 = float(key['w2'])
                        current.height2 = current.height
                    if 'h2' in key:
                        current.height2 = float(key['h2'])
                        current.width2 = current.width if current.width2 == 0.0 else current.width2
                    if 'l' in key:
                        current.step = key['l']
                    if 'd' in key:
                        current.decal = key['d']
            # End of the row
            current.y += 1.0
        elif 'backcolor' in row:
            color = row['backcolor'].replace(';', '')
            meta['backcolor'] = color if color_format.match(
                color) else meta['backcolor']
        current.x = 0
    return {'meta': meta, 'keys': keys}
Exemplo n.º 2
0
def deserialise(
    rows
):  # where rows is a dictionary version of Keyboard Layout Editor's JSON Output
    # Initialize with defaults
    current = Key()
    default_font_size = current.font_size[0]
    meta = {'backcolor': '#eeeeee'}
    keys = []
    color_format = re.compile(r'#[a-fA-F0-9]{3}(?:[a-fA-F0-9]{3})?$')

    for row in rows:
        if isinstance(row, list):
            for key in row:
                if isinstance(key, str):
                    newKey = copy.copy(current)
                    newKey.labels = [
                        html_to_unicode(text)
                        for text in html.unescape(key).split('\n')
                    ]
                    keys.append(newKey)

                    # Set up for the next key
                    current.x += current.width
                    current.width = current.height = 1.0
                    current.x2 = current.y2 = current.width2 = current.height2 = 0.0
                    current.nub = current.stepped = current.stepped = current.decal = False
                else:
                    if 'r' in key:
                        current.rotation_angle = key['r']
                    if 'rx' in key:
                        current.rotation_x = key['rx']
                        current.x = current.y = 0
                    if 'ry' in key:
                        current.rotation_y = key['ry']
                        current.y = current.y = 0
                    if 'a' in key:
                        current.align = int(key['a'])
                    if 'f' in key:
                        default_font_size = float(key['f'])
                        current.font_size = [default_font_size] * 12
                    if 'f2' in key:
                        current.font_size = [float(key['f2'])] * 12
                    if 'fa' in key:
                        current.font_size = [
                            float(key['fa'][i]) if i < len(key['fa'])
                            and key['fa'][i] > 0 else default_font_size
                            for i in range(12)
                        ]
                    if 'p' in key:
                        current.profile = key['p']
                    if 'c' in key:
                        current.color = key['c'].replace(';', '')
                    if 't' in key:
                        f_colors = [
                            ''.join(c for c in line
                                    if c in '0123456789abcdefABCDEF#')
                            for line in key['t'].splitlines()
                        ]
                        f_colors = [
                            color for color in f_colors
                            if color_format.match(color) or not color.strip()
                        ]  # more gracefully handle invalid colors
                        if len(f_colors) > 0:
                            current.font_color = f_colors
                    if 'x' in key:
                        current.x += float(key['x'])
                    if 'y' in key:
                        current.y += float(key['y'])
                    if 'w' in key:
                        current.width = float(key['w'])
                    if 'h' in key:
                        current.height = float(key['h'])
                    if 'x2' in key:
                        current.x2 = float(key['x2'])
                    if 'y2' in key:
                        current.y2 = float(key['y2'])
                    if 'w2' in key:
                        current.width2 = float(key['w2'])
                        current.height2 = current.height
                    if 'h2' in key:
                        current.height2 = float(key['h2'])
                        if current.width2 == 0.0:
                            current.width2 = current.width
                    if 'n' in key:
                        current.nub = key['n']
                    if 'l' in key:
                        current.stepped = key['l']
                    if 'g' in key:
                        current.ghost = key['g']
                    if 'd' in key:
                        current.decal = key['d']
            # End of the row
            current.y += 1.0
        elif 'backcolor' in row and len(row['backcolor']) > 0:
            meta['backcolor'] = row['backcolor'].replace(';', '')
        current.x = 0  #current.rotation_x
    return {'meta': meta, 'keys': keys}