scripts: Add keymap-render.py script

Add a script for generating a png with the short-name (ignoring variants)
of all keymaps listed by "localectl list-keymaps" pre-generated.

This scripts also generates a ply-keymap-metadata.h file with info on
which pre-generated keymap name is where in the png.

This will be used in a follow-up commit to add support for a new
keyboard-keymap-icon control to libply-splash-graphics.

Note that this commit does not add the generated keymap-render.png file,
this file will be added to each themes image-dir separately as the color
of the pre-generated text may differ per theme.

Changes by Hans de Goede:
- Change generated metadata into a C header file
- Drop drawing of curved corners, we just want the text
- Add special handling for dvorak

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
This commit is contained in:
Alberto Ruiz 2019-09-30 16:58:55 +02:00 committed by Hans de Goede
parent 9306edbc17
commit b26a956a11
2 changed files with 237 additions and 0 deletions

99
scripts/keymap-render.py Executable file
View file

@ -0,0 +1,99 @@
#!/usr/bin/python3
# coding: utf-8
import cairo
import subprocess
import math
FONT_SIZE = 30
MARGIN = int(FONT_SIZE / 3)
def get_keymaps():
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
ret = []
for k in keymaps:
if k not in ret:
ret.append(k)
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():
extents = ct.text_extents(i)
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.9, 0.9, 0.9, 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():
extents = ct.text_extents(km)
ct.show_text(km)
metadata.append((km, current_x, extents.width + MARGIN * 2))
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("struct ply_keymap_metadata {")
print(" const char *name;")
print(" int offset;")
print(" int width;")
print("};")
print("")
print("static struct ply_keymap_metadata ply_keymap_metadata[] = {")
for i in metadata:
print((" { \"%s\", %d, %d }," % (i[0],i[1],i[2])))
print(" { NULL, } /* End of array marker */ ")
print("};")

View file

@ -0,0 +1,138 @@
/* This file is autogenerated by running:
* scripts/keymap-render.py > src/libply-splash-graphics/ply-keymap-metadata.h
*/
struct ply_keymap_metadata {
const char *name;
int offset;
int width;
};
static struct ply_keymap_metadata ply_keymap_metadata[] = {
{ "dvorak", 10, 113 },
{ "al", 123, 44 },
{ "amiga", 167, 101 },
{ "applkey", 268, 126 },
{ "at", 394, 46 },
{ "atari", 440, 81 },
{ "az", 521, 49 },
{ "azerty", 570, 104 },
{ "ba", 674, 49 },
{ "backspace", 723, 157 },
{ "bashkir", 880, 119 },
{ "be", 999, 50 },
{ "bg", 1049, 51 },
{ "br", 1100, 48 },
{ "by", 1148, 51 },
{ "bywin", 1199, 98 },
{ "ca", 1297, 47 },
{ "carpalx", 1344, 118 },
{ "cf", 1462, 45 },
{ "ch", 1507, 49 },
{ "cm", 1556, 59 },
{ "cn", 1615, 49 },
{ "croat", 1664, 89 },
{ "ctrl", 1753, 65 },
{ "cz", 1818, 47 },
{ "de", 1865, 51 },
{ "defkeymap", 1916, 170 },
{ "dk", 2086, 53 },
{ "dz", 2139, 50 },
{ "ee", 2189, 50 },
{ "emacs", 2239, 106 },
{ "emacs2", 2345, 122 },
{ "en", 2467, 51 },
{ "epo", 2518, 68 },
{ "es", 2586, 49 },
{ "et", 2635, 46 },
{ "euro", 2681, 80 },
{ "euro1", 2761, 95 },
{ "euro2", 2856, 97 },
{ "fi", 2953, 38 },
{ "fo", 2991, 47 },
{ "fr", 3038, 44 },
{ "gb", 3082, 53 },
{ "ge", 3135, 51 },
{ "gh", 3186, 52 },
{ "gr", 3238, 49 },
{ "hr", 3287, 48 },
{ "hu", 3335, 50 },
{ "hu101", 3385, 99 },
{ "ie", 3484, 42 },
{ "il", 3526, 36 },
{ "in", 3562, 43 },
{ "iq", 3605, 43 },
{ "ir", 3648, 40 },
{ "is", 3688, 41 },
{ "it", 3729, 38 },
{ "it2", 3767, 54 },
{ "jp", 3821, 46 },
{ "jp106", 3867, 96 },
{ "kazakh", 3963, 112 },
{ "ke", 4075, 49 },
{ "keypad", 4124, 114 },
{ "kr", 4238, 47 },
{ "ky", 4285, 50 },
{ "kyrgyz", 4335, 107 },
{ "kz", 4442, 48 },
{ "la", 4490, 42 },
{ "latam", 4532, 97 },
{ "lk", 4629, 45 },
{ "lt", 4674, 39 },
{ "lv", 4713, 44 },
{ "ma", 4757, 59 },
{ "pl", 4816, 44 },
{ "pt", 4860, 46 },
{ "se", 4906, 48 },
{ "template", 4954, 142 },
{ "uk", 5096, 53 },
{ "us", 5149, 50 },
{ "md", 5199, 61 },
{ "me", 5260, 60 },
{ "mk", 5320, 62 },
{ "mk0", 5382, 79 },
{ "ml", 5461, 54 },
{ "mm", 5515, 71 },
{ "mt", 5586, 56 },
{ "ng", 5642, 51 },
{ "nl", 5693, 44 },
{ "nl2", 5737, 60 },
{ "no", 5797, 51 },
{ "pc110", 5848, 95 },
{ "ph", 5943, 51 },
{ "pl1", 5994, 58 },
{ "pl2", 6052, 60 },
{ "pl3", 6112, 60 },
{ "pl4", 6172, 62 },
{ "ro", 6234, 46 },
{ "rs", 6280, 44 },
{ "ru", 6324, 45 },
{ "ru1", 6369, 61 },
{ "ru2", 6430, 63 },
{ "ru3", 6493, 63 },
{ "ru4", 6556, 65 },
{ "ruwin", 6621, 95 },
{ "sg", 6716, 49 },
{ "si", 6765, 40 },
{ "sk", 6805, 50 },
{ "slovene", 6855, 122 },
{ "sr", 6977, 46 },
{ "sunkeymap", 7023, 174 },
{ "sv", 7197, 49 },
{ "sy", 7246, 49 },
{ "tj", 7295, 38 },
{ "tm", 7333, 57 },
{ "tr", 7390, 44 },
{ "tralt", 7434, 79 },
{ "trf", 7513, 55 },
{ "trq", 7568, 59 },
{ "ttwin", 7627, 90 },
{ "tw", 7717, 56 },
{ "ua", 7773, 50 },
{ "unicode", 7823, 124 },
{ "uz", 7947, 50 },
{ "vn", 7997, 51 },
{ "wangbe", 8048, 126 },
{ "wangbe2", 8174, 143 },
{ "windowkeys", 8317, 188 },
{ NULL, } /* End of array marker */
};