File: keymap-render.py

package info (click to toggle)
plymouth 24.004.60-5.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,480 kB
  • sloc: ansic: 38,787; sh: 1,086; xml: 575; python: 101; makefile: 34
file content (119 lines) | stat: -rwxr-xr-x 3,956 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/python3
# coding: utf-8
import cairo
import subprocess
import math
import lxml
from lxml import etree


FONT_SIZE = 30
MARGIN = int(FONT_SIZE / 3)

#python3-lxml utility is required

def get_keymaps():
        xml = etree.XML(bytes(open('/usr/share/X11/xkb/rules/evdev.xml', 'r').read(), encoding="utf-8"))
        keymaps_x11 = xml.xpath('//layout/configItem/description/text()')
        keymaps = subprocess.check_output(["localectl", "list-keymaps"]).decode("utf-8").strip().split()

        # Note when changing this you MUST keep ply_keymap_normalize_keymap()
        # from src/libply-splash-graphics/ply-keymap-icon.c in sync
        def normalize_keymaps(keymap):
                parts = keymap.replace("_", "-").replace(".", "-").split("-")

                # Special case for dvorak, E.g. when mixing "us" and "us-dvorak"
                # on machines returning "us" for both is not useful.
                # Presumably users using dvorak now which variant they use
                # so we just describe all dvorak layouts as "dvorak".
                if "dvorak" in keymap:
                        return "dvorak"

                # mac / sun keymaps are prefixes with mac / sun / sun[4-6]t
                for prefix in ["mac", "sun" ]:
                        if keymap.startswith(prefix) and len(parts) > 1:
                                return parts[1]

                return parts[0]

        keymaps = list(map(normalize_keymaps ,keymaps))
        #Remove duplicates
        keymaps_dedupe = []
        for k in keymaps:
                if k not in keymaps_dedupe:
                        keymaps_dedupe.append(k)

        ret = []
        for k in keymaps_dedupe:
                ret.append((k, "PLY_LAYOUT_TERMINAL"))
        for k in keymaps_x11:
                ret.append((k, "PLY_LAYOUT_XKB"))
        return ret


# Calculate size
sf = cairo.ImageSurface(cairo.FORMAT_ARGB32, 1, 1)
ct = cairo.Context(sf)
ct.select_font_face("Cantarell", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
ct.set_font_size(FONT_SIZE)

max_height = 0.0
total_width = 0.0
for i in get_keymaps():
        text=i[0]
        extents = ct.text_extents(text)
        h = extents.height
        if h > max_height:
                max_height = h
        total_width += extents.width
        total_width += MARGIN * 2
ascent, descent, _h, _max_x, max_y = ct.font_extents()

# Create image
sf = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(total_width), int(max_height + MARGIN * 2))
ct = cairo.Context(sf)
ct.save()
ct.set_source_rgba(0, 0, 0, 0)
ct.set_operator (cairo.OPERATOR_SOURCE)
ct.paint()
ct.restore()
ct.select_font_face("Cantarell", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
ct.set_font_size(FONT_SIZE)
ct.set_source_rgba(0.75, 0.75, 0.75, 1.0)

ct.move_to(MARGIN, MARGIN + max_height - descent)
current_x, current_y = (MARGIN, MARGIN + max_height - descent)
metadata = []
for km in get_keymaps():
        km_text=km[0]
        km_type=km[1]
        extents = ct.text_extents(km_text)
        ct.show_text(km_text)
        metadata.append((km_text, current_x, extents.width + MARGIN * 2, km_type))
        current_x += extents.width + (MARGIN * 2)
        ct.move_to(current_x, current_y)

sf.write_to_png("keymap-render.png")

print("/* This file is autogenerated by running:")
print(" * scripts/keymap-render.py > src/libply-splash-graphics/ply-keymap-metadata.h")
print(" */")
print("typedef struct {")
print("        const char *name;")
print("        int offset;")
print("        int width;")
print("        int type;")
print("} ply_keymap_metadata_t;")
print("")
print("typedef enum {")
print("        PLY_LAYOUT_TERMINAL,")
print("        PLY_LAYOUT_XKB,")
print("} ply_layout_types_t;")
print("")
print("static ply_keymap_metadata_t ply_keymap_metadata[] = {")

for i in metadata:
        print(("        { \"%s\", %d, %d, %s}," % (i[0],i[1],i[2],i[3])))

print("        { NULL, } /* End of array marker */ ")
print("};")