Yu-Ping Wu | d71b445 | 2020-06-16 11:00:26 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
Hung-Te Lin | 707e2ef | 2013-08-06 10:20:04 +0800 | [diff] [blame] | 2 | # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 5 | """Script to generate bitmaps for firmware screens.""" |
Hung-Te Lin | 707e2ef | 2013-08-06 10:20:04 +0800 | [diff] [blame] | 6 | |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 7 | import argparse |
| 8 | from collections import defaultdict, namedtuple |
| 9 | import copy |
Yu-Ping Wu | e445e04 | 2020-11-19 15:53:42 +0800 | [diff] [blame^] | 10 | import fractions |
Hung-Te Lin | 707e2ef | 2013-08-06 10:20:04 +0800 | [diff] [blame] | 11 | import glob |
Jes Klinke | 1687a99 | 2020-06-16 13:47:17 -0700 | [diff] [blame] | 12 | import json |
Hung-Te Lin | 04addcc | 2015-03-23 18:43:30 +0800 | [diff] [blame] | 13 | import multiprocessing |
Hung-Te Lin | 707e2ef | 2013-08-06 10:20:04 +0800 | [diff] [blame] | 14 | import os |
| 15 | import re |
Jes Klinke | 1687a99 | 2020-06-16 13:47:17 -0700 | [diff] [blame] | 16 | import shutil |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 17 | import signal |
Hung-Te Lin | 707e2ef | 2013-08-06 10:20:04 +0800 | [diff] [blame] | 18 | import subprocess |
| 19 | import sys |
Jes Klinke | 1687a99 | 2020-06-16 13:47:17 -0700 | [diff] [blame] | 20 | import tempfile |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 21 | from xml.etree import ElementTree |
Hung-Te Lin | 04addcc | 2015-03-23 18:43:30 +0800 | [diff] [blame] | 22 | |
Hung-Te Lin | 707e2ef | 2013-08-06 10:20:04 +0800 | [diff] [blame] | 23 | import yaml |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 24 | from PIL import Image |
Hung-Te Lin | 707e2ef | 2013-08-06 10:20:04 +0800 | [diff] [blame] | 25 | |
| 26 | SCRIPT_BASE = os.path.dirname(os.path.abspath(__file__)) |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 27 | |
| 28 | STRINGS_GRD_FILE = 'firmware_strings.grd' |
| 29 | STRINGS_JSON_FILE_TMPL = '{}.json' |
| 30 | FORMAT_FILE = 'format.yaml' |
| 31 | BOARDS_CONFIG_FILE = 'boards.yaml' |
| 32 | |
| 33 | TXT_TO_PNG_SVG = os.path.join(SCRIPT_BASE, 'text_to_png_svg') |
| 34 | STRINGS_DIR = os.path.join(SCRIPT_BASE, 'strings') |
| 35 | LOCALE_DIR = os.path.join(STRINGS_DIR, 'locale') |
| 36 | OUTPUT_DIR = os.getenv('OUTPUT', os.path.join(SCRIPT_BASE, 'build')) |
| 37 | STAGE_DIR = os.path.join(OUTPUT_DIR, '.stage') |
| 38 | STAGE_LOCALE_DIR = os.path.join(STAGE_DIR, 'locale') |
| 39 | STAGE_FONT_DIR = os.path.join(STAGE_DIR, 'font') |
| 40 | |
| 41 | ONE_LINE_DIR = 'one_line' |
| 42 | SVG_FILES = '*.svg' |
| 43 | PNG_FILES = '*.png' |
| 44 | |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 45 | DIAGNOSTIC_UI = os.getenv('DIAGNOSTIC_UI') == '1' |
| 46 | |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 47 | # String format YAML key names. |
Yu-Ping Wu | 177f12c | 2020-11-04 15:55:37 +0800 | [diff] [blame] | 48 | KEY_DEFAULT = '_DEFAULT_' |
Hung-Te Lin | 707e2ef | 2013-08-06 10:20:04 +0800 | [diff] [blame] | 49 | KEY_LOCALES = 'locales' |
Yu-Ping Wu | 338f083 | 2020-10-23 16:14:40 +0800 | [diff] [blame] | 50 | KEY_GENERIC_FILES = 'generic_files' |
| 51 | KEY_LOCALIZED_FILES = 'localized_files' |
Yu-Ping Wu | 177f12c | 2020-11-04 15:55:37 +0800 | [diff] [blame] | 52 | KEY_DIAGNOSTIC_FILES = 'diagnostic_files' |
| 53 | KEY_SPRITE_FILES = 'sprite_files' |
Hung-Te Lin | 707e2ef | 2013-08-06 10:20:04 +0800 | [diff] [blame] | 54 | KEY_STYLES = 'styles' |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 55 | KEY_BGCOLOR = 'bgcolor' |
| 56 | KEY_FGCOLOR = 'fgcolor' |
| 57 | KEY_HEIGHT = 'height' |
Yu-Ping Wu | ed95df3 | 2020-11-04 17:08:15 +0800 | [diff] [blame] | 58 | KEY_MAX_WIDTH = 'max_width' |
Yu-Ping Wu | 177f12c | 2020-11-04 15:55:37 +0800 | [diff] [blame] | 59 | KEY_FONTS = 'fonts' |
Hung-Te Lin | 707e2ef | 2013-08-06 10:20:04 +0800 | [diff] [blame] | 60 | |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 61 | # Board config YAML key names. |
| 62 | SCREEN_KEY = 'screen' |
| 63 | PANEL_KEY = 'panel' |
| 64 | SDCARD_KEY = 'sdcard' |
| 65 | BAD_USB3_KEY = 'bad_usb3' |
| 66 | LOCALES_KEY = 'locales' |
| 67 | RTL_KEY = 'rtl' |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 68 | TEXT_COLORS_KEY = 'text_colors' |
| 69 | RW_OVERRIDE_KEY = 'rw_override' |
| 70 | |
| 71 | BMP_HEADER_OFFSET_NUM_LINES = 6 |
Hung-Te Lin | 707e2ef | 2013-08-06 10:20:04 +0800 | [diff] [blame] | 72 | |
Jes Klinke | 1687a99 | 2020-06-16 13:47:17 -0700 | [diff] [blame] | 73 | # Regular expressions used to eliminate spurious spaces and newlines in |
| 74 | # translation strings. |
| 75 | NEWLINE_PATTERN = re.compile(r'([^\n])\n([^\n])') |
| 76 | NEWLINE_REPLACEMENT = r'\1 \2' |
| 77 | CRLF_PATTERN = re.compile(r'\r\n') |
| 78 | MULTIBLANK_PATTERN = re.compile(r' *') |
| 79 | |
Yu-Ping Wu | 11027f0 | 2020-10-14 17:35:42 +0800 | [diff] [blame] | 80 | GLYPH_FONT = 'Noto Sans Mono' |
| 81 | |
Yu-Ping Wu | abb9afb | 2020-10-27 17:15:22 +0800 | [diff] [blame] | 82 | LocaleInfo = namedtuple('LocaleInfo', ['code', 'rtl']) |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 83 | |
Yu-Ping Wu | 6b282c5 | 2020-03-19 12:54:15 +0800 | [diff] [blame] | 84 | |
Hung-Te Lin | 707e2ef | 2013-08-06 10:20:04 +0800 | [diff] [blame] | 85 | class DataError(Exception): |
| 86 | pass |
| 87 | |
| 88 | |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 89 | class BuildImageError(Exception): |
| 90 | """The exception class for all errors generated during build image process.""" |
| 91 | |
| 92 | |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 93 | def get_config_with_defaults(configs, key): |
| 94 | """Gets config of `key` from `configs`. |
| 95 | |
| 96 | If `key` is not present in `configs`, the default config will be returned. |
| 97 | Similarly, if some config values are missing for `key`, the default ones will |
| 98 | be used. |
| 99 | """ |
Yu-Ping Wu | 177f12c | 2020-11-04 15:55:37 +0800 | [diff] [blame] | 100 | config = configs[KEY_DEFAULT].copy() |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 101 | config.update(configs.get(key, {})) |
| 102 | return config |
| 103 | |
| 104 | |
Yu-Ping Wu | ed95df3 | 2020-11-04 17:08:15 +0800 | [diff] [blame] | 105 | def convert_text_to_png(locale, input_file, font, output_dir, height=None, |
| 106 | max_width=None, margin='0', bgcolor='#000000', |
| 107 | fgcolor='#ffffff', **options): |
Yu-Ping Wu | 11027f0 | 2020-10-14 17:35:42 +0800 | [diff] [blame] | 108 | """Converts text files into PNG image files. |
| 109 | |
| 110 | Args: |
| 111 | locale: Locale (language) to select implicit rendering options. None for |
| 112 | locale-independent strings. |
| 113 | input_file: Path of input text file. |
| 114 | font: Font spec. |
Yu-Ping Wu | ed95df3 | 2020-11-04 17:08:15 +0800 | [diff] [blame] | 115 | height: Height. |
| 116 | max_width: Maximum width. |
Yu-Ping Wu | 11027f0 | 2020-10-14 17:35:42 +0800 | [diff] [blame] | 117 | margin: CSS-style margin. |
| 118 | output_dir: Directory to generate image files. |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 119 | bgcolor: Background color (#rrggbb). |
| 120 | fgcolor: Foreground color (#rrggbb). |
Yu-Ping Wu | 338f083 | 2020-10-23 16:14:40 +0800 | [diff] [blame] | 121 | **options: Other options to be added. |
Yu-Ping Wu | 11027f0 | 2020-10-14 17:35:42 +0800 | [diff] [blame] | 122 | """ |
| 123 | name, _ = os.path.splitext(os.path.basename(input_file)) |
| 124 | command = [TXT_TO_PNG_SVG, '--outdir=%s' % output_dir] |
| 125 | if locale: |
| 126 | command.append('--lan=%s' % locale) |
| 127 | if font: |
| 128 | command.append("--font='%s'" % font) |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 129 | font_size = os.getenv('FONT_SIZE') |
Yu-Ping Wu | 11027f0 | 2020-10-14 17:35:42 +0800 | [diff] [blame] | 130 | if font_size: |
| 131 | command.append('--point=%r' % font_size) |
Yu-Ping Wu | ed95df3 | 2020-11-04 17:08:15 +0800 | [diff] [blame] | 132 | if max_width: |
| 133 | # Without the --width option set, the minimum height of the output SVG |
| 134 | # image is roughly 22px (for locale 'en'). With --width=WIDTH passed to |
| 135 | # pango-view, the width of the output seems to always be (WIDTH * 4 / 3), |
| 136 | # regardless of the font being used. Therefore, set the max_width in |
| 137 | # points as follows to prevent drawing from exceeding canvas boundary in |
| 138 | # depthcharge runtime. |
| 139 | max_width_pt = int(22 * max_width / height / (4 / 3)) |
| 140 | command.append('--width=%d' % max_width_pt) |
Yu-Ping Wu | 11027f0 | 2020-10-14 17:35:42 +0800 | [diff] [blame] | 141 | if margin: |
| 142 | command.append('--margin="%s"' % margin) |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 143 | command.append('--bgcolor="%s"' % bgcolor) |
| 144 | command.append('--color="%s"' % fgcolor) |
| 145 | |
Yu-Ping Wu | 338f083 | 2020-10-23 16:14:40 +0800 | [diff] [blame] | 146 | for k, v in options.items(): |
| 147 | command.append('--%s="%s"' % (k, v)) |
Yu-Ping Wu | 11027f0 | 2020-10-14 17:35:42 +0800 | [diff] [blame] | 148 | command.append(input_file) |
| 149 | |
| 150 | return subprocess.call(' '.join(command), shell=True, |
| 151 | stdout=subprocess.PIPE) == 0 |
| 152 | |
| 153 | |
| 154 | def convert_glyphs(): |
| 155 | """Converts glyphs of ascii characters.""" |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 156 | os.makedirs(STAGE_FONT_DIR, exist_ok=True) |
Yu-Ping Wu | 11027f0 | 2020-10-14 17:35:42 +0800 | [diff] [blame] | 157 | # Remove the extra whitespace at the top/bottom within the glyphs |
| 158 | margin = '-3 0 -1 0' |
Yu-Ping Wu | 11027f0 | 2020-10-14 17:35:42 +0800 | [diff] [blame] | 159 | for c in range(ord(' '), ord('~') + 1): |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 160 | txt_file = os.path.join(STAGE_FONT_DIR, f'idx{c:03d}_{c:02x}.txt') |
Yu-Ping Wu | 11027f0 | 2020-10-14 17:35:42 +0800 | [diff] [blame] | 161 | with open(txt_file, 'w', encoding='ascii') as f: |
| 162 | f.write(chr(c)) |
| 163 | f.write('\n') |
| 164 | # TODO(b/163109632): Parallelize the conversion of glyphs |
Yu-Ping Wu | ed95df3 | 2020-11-04 17:08:15 +0800 | [diff] [blame] | 165 | convert_text_to_png(None, txt_file, GLYPH_FONT, STAGE_FONT_DIR, |
| 166 | margin=margin) |
Yu-Ping Wu | 11027f0 | 2020-10-14 17:35:42 +0800 | [diff] [blame] | 167 | |
| 168 | |
Yu-Ping Wu | ae79af6 | 2020-09-23 16:48:06 +0800 | [diff] [blame] | 169 | def _load_locale_json_file(locale, json_dir): |
Jes Klinke | 1687a99 | 2020-06-16 13:47:17 -0700 | [diff] [blame] | 170 | result = {} |
Yu-Ping Wu | ae79af6 | 2020-09-23 16:48:06 +0800 | [diff] [blame] | 171 | filename = os.path.join(json_dir, STRINGS_JSON_FILE_TMPL.format(locale)) |
Yu-Ping Wu | d71b445 | 2020-06-16 11:00:26 +0800 | [diff] [blame] | 172 | with open(filename, encoding='utf-8-sig') as input_file: |
Jes Klinke | 1687a99 | 2020-06-16 13:47:17 -0700 | [diff] [blame] | 173 | for tag, msgdict in json.load(input_file).items(): |
| 174 | msgtext = msgdict['message'] |
| 175 | msgtext = re.sub(CRLF_PATTERN, '\n', msgtext) |
| 176 | msgtext = re.sub(NEWLINE_PATTERN, NEWLINE_REPLACEMENT, msgtext) |
| 177 | msgtext = re.sub(MULTIBLANK_PATTERN, ' ', msgtext) |
| 178 | # Strip any trailing whitespace. A trailing newline appears to make |
| 179 | # Pango report a larger layout size than what's actually visible. |
| 180 | msgtext = msgtext.strip() |
| 181 | result[tag] = msgtext |
| 182 | return result |
| 183 | |
Yu-Ping Wu | ae79af6 | 2020-09-23 16:48:06 +0800 | [diff] [blame] | 184 | |
| 185 | def parse_locale_json_file(locale, json_dir): |
Yu-Ping Wu | 338f083 | 2020-10-23 16:14:40 +0800 | [diff] [blame] | 186 | """Parses given firmware string json file. |
Mathew King | 89d48c6 | 2019-02-15 10:08:39 -0700 | [diff] [blame] | 187 | |
| 188 | Args: |
Yu-Ping Wu | 8f633b8 | 2020-09-22 14:27:57 +0800 | [diff] [blame] | 189 | locale: The name of the locale, e.g. "da" or "pt-BR". |
Jes Klinke | 1687a99 | 2020-06-16 13:47:17 -0700 | [diff] [blame] | 190 | json_dir: Directory containing json output from grit. |
Mathew King | 89d48c6 | 2019-02-15 10:08:39 -0700 | [diff] [blame] | 191 | |
| 192 | Returns: |
| 193 | A dictionary for mapping of "name to content" for files to be generated. |
| 194 | """ |
Yu-Ping Wu | ae79af6 | 2020-09-23 16:48:06 +0800 | [diff] [blame] | 195 | result = _load_locale_json_file(locale, json_dir) |
| 196 | original = _load_locale_json_file('en', json_dir) |
| 197 | for tag in original: |
| 198 | if tag not in result: |
| 199 | # Use original English text, in case translation is not yet available |
| 200 | print('WARNING: locale "%s", missing entry %s' % (locale, tag)) |
| 201 | result[tag] = original[tag] |
Mathew King | 89d48c6 | 2019-02-15 10:08:39 -0700 | [diff] [blame] | 202 | |
Yu-Ping Wu | ae79af6 | 2020-09-23 16:48:06 +0800 | [diff] [blame] | 203 | return result |
Mathew King | 89d48c6 | 2019-02-15 10:08:39 -0700 | [diff] [blame] | 204 | |
Yu-Ping Wu | ae79af6 | 2020-09-23 16:48:06 +0800 | [diff] [blame] | 205 | |
| 206 | def parse_locale_input_files(locale, json_dir): |
| 207 | """Parses all firmware string files for the given locale. |
| 208 | |
| 209 | Args: |
| 210 | locale: The name of the locale, e.g. "da" or "pt-BR". |
| 211 | json_dir: Directory containing json output from grit. |
| 212 | |
| 213 | Returns: |
| 214 | A dictionary for mapping of "name to content" for files to be generated. |
| 215 | """ |
| 216 | result = parse_locale_json_file(locale, json_dir) |
| 217 | |
| 218 | # Walk locale directory to add pre-generated texts such as language names. |
Yu-Ping Wu | 8f633b8 | 2020-09-22 14:27:57 +0800 | [diff] [blame] | 219 | for input_file in glob.glob(os.path.join(LOCALE_DIR, locale, "*.txt")): |
Mathew King | 89d48c6 | 2019-02-15 10:08:39 -0700 | [diff] [blame] | 220 | name, _ = os.path.splitext(os.path.basename(input_file)) |
Yu-Ping Wu | d71b445 | 2020-06-16 11:00:26 +0800 | [diff] [blame] | 221 | with open(input_file, 'r', encoding='utf-8-sig') as f: |
Mathew King | 89d48c6 | 2019-02-15 10:08:39 -0700 | [diff] [blame] | 222 | result[name] = f.read().strip() |
Shelley Chen | 2f616ac | 2017-05-22 13:19:40 -0700 | [diff] [blame] | 223 | |
Hung-Te Lin | 707e2ef | 2013-08-06 10:20:04 +0800 | [diff] [blame] | 224 | return result |
| 225 | |
| 226 | |
Yu-Ping Wu | ae79af6 | 2020-09-23 16:48:06 +0800 | [diff] [blame] | 227 | def build_text_files(inputs, files, output_dir): |
Hung-Te Lin | 707e2ef | 2013-08-06 10:20:04 +0800 | [diff] [blame] | 228 | """Builds text files from given input data. |
| 229 | |
| 230 | Args: |
| 231 | inputs: Dictionary of contents for given file name. |
Yu-Ping Wu | 338f083 | 2020-10-23 16:14:40 +0800 | [diff] [blame] | 232 | files: List of files. |
Hung-Te Lin | 707e2ef | 2013-08-06 10:20:04 +0800 | [diff] [blame] | 233 | output_dir: Directory to generate text files. |
| 234 | """ |
Yu-Ping Wu | 338f083 | 2020-10-23 16:14:40 +0800 | [diff] [blame] | 235 | for name in files: |
| 236 | file_name = os.path.join(output_dir, name + '.txt') |
| 237 | with open(file_name, 'w', encoding='utf-8-sig') as f: |
| 238 | f.write(inputs[name] + '\n') |
Hung-Te Lin | 707e2ef | 2013-08-06 10:20:04 +0800 | [diff] [blame] | 239 | |
| 240 | |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 241 | def convert_localized_strings(formats): |
| 242 | """Converts localized strings.""" |
Yu-Ping Wu | 7f6639a | 2020-09-28 15:31:35 +0800 | [diff] [blame] | 243 | # Make a copy of formats to avoid modifying it |
| 244 | formats = copy.deepcopy(formats) |
Hung-Te Lin | 707e2ef | 2013-08-06 10:20:04 +0800 | [diff] [blame] | 245 | |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 246 | env_locales = os.getenv('LOCALES') |
| 247 | if env_locales: |
| 248 | locales = env_locales.split() |
| 249 | else: |
| 250 | locales = formats[KEY_LOCALES] |
Yu-Ping Wu | 7f6639a | 2020-09-28 15:31:35 +0800 | [diff] [blame] | 251 | |
Yu-Ping Wu | 338f083 | 2020-10-23 16:14:40 +0800 | [diff] [blame] | 252 | files = formats[KEY_LOCALIZED_FILES] |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 253 | if DIAGNOSTIC_UI: |
Yu-Ping Wu | 177f12c | 2020-11-04 15:55:37 +0800 | [diff] [blame] | 254 | files.update(formats[KEY_DIAGNOSTIC_FILES]) |
Yu-Ping Wu | 338f083 | 2020-10-23 16:14:40 +0800 | [diff] [blame] | 255 | |
Yu-Ping Wu | 7f6639a | 2020-09-28 15:31:35 +0800 | [diff] [blame] | 256 | styles = formats[KEY_STYLES] |
| 257 | fonts = formats[KEY_FONTS] |
Yu-Ping Wu | 177f12c | 2020-11-04 15:55:37 +0800 | [diff] [blame] | 258 | default_font = fonts[KEY_DEFAULT] |
Hung-Te Lin | 707e2ef | 2013-08-06 10:20:04 +0800 | [diff] [blame] | 259 | |
Yu-Ping Wu | 5194035 | 2020-09-17 08:48:55 +0800 | [diff] [blame] | 260 | # Sources are one .grd file with identifiers chosen by engineers and |
| 261 | # corresponding English texts, as well as a set of .xlt files (one for each |
| 262 | # language other than US english) with a mapping from hash to translation. |
| 263 | # Because the keys in the xlt files are a hash of the English source text, |
| 264 | # rather than our identifiers, such as "btn_cancel", we use the "grit" |
| 265 | # command line tool to process the .grd and .xlt files, producing a set of |
| 266 | # .json files mapping our identifier to the translated string, one for every |
| 267 | # language including US English. |
Jes Klinke | 1687a99 | 2020-06-16 13:47:17 -0700 | [diff] [blame] | 268 | |
Yu-Ping Wu | 5194035 | 2020-09-17 08:48:55 +0800 | [diff] [blame] | 269 | # Create a temporary directory to place the translation output from grit in. |
| 270 | json_dir = tempfile.mkdtemp() |
Yu-Ping Wu | 7f6639a | 2020-09-28 15:31:35 +0800 | [diff] [blame] | 271 | |
Yu-Ping Wu | 5194035 | 2020-09-17 08:48:55 +0800 | [diff] [blame] | 272 | # This invokes the grit build command to generate JSON files from the XTB |
| 273 | # files containing translations. The results are placed in `json_dir` as |
| 274 | # specified in firmware_strings.grd, i.e. one JSON file per locale. |
| 275 | subprocess.check_call([ |
| 276 | 'grit', |
Yu-Ping Wu | 8f633b8 | 2020-09-22 14:27:57 +0800 | [diff] [blame] | 277 | '-i', os.path.join(LOCALE_DIR, STRINGS_GRD_FILE), |
Yu-Ping Wu | 5194035 | 2020-09-17 08:48:55 +0800 | [diff] [blame] | 278 | 'build', |
| 279 | '-o', os.path.join(json_dir) |
| 280 | ]) |
Jes Klinke | 1687a99 | 2020-06-16 13:47:17 -0700 | [diff] [blame] | 281 | |
Yu-Ping Wu | c90a22f | 2020-04-24 11:17:15 +0800 | [diff] [blame] | 282 | # Ignore SIGINT in child processes |
| 283 | sigint_handler = signal.signal(signal.SIGINT, signal.SIG_IGN) |
Hung-Te Lin | 04addcc | 2015-03-23 18:43:30 +0800 | [diff] [blame] | 284 | pool = multiprocessing.Pool(multiprocessing.cpu_count()) |
Yu-Ping Wu | c90a22f | 2020-04-24 11:17:15 +0800 | [diff] [blame] | 285 | signal.signal(signal.SIGINT, sigint_handler) |
| 286 | |
Hung-Te Lin | 04addcc | 2015-03-23 18:43:30 +0800 | [diff] [blame] | 287 | results = [] |
Hung-Te Lin | 707e2ef | 2013-08-06 10:20:04 +0800 | [diff] [blame] | 288 | for locale in locales: |
Yu-Ping Wu | d71b445 | 2020-06-16 11:00:26 +0800 | [diff] [blame] | 289 | print(locale, end=' ', flush=True) |
Yu-Ping Wu | ae79af6 | 2020-09-23 16:48:06 +0800 | [diff] [blame] | 290 | inputs = parse_locale_input_files(locale, json_dir) |
| 291 | output_dir = os.path.normpath(os.path.join(STAGE_DIR, 'locale', locale)) |
Hung-Te Lin | 707e2ef | 2013-08-06 10:20:04 +0800 | [diff] [blame] | 292 | if not os.path.exists(output_dir): |
| 293 | os.makedirs(output_dir) |
Matt Delco | 4c5580d | 2019-03-07 14:00:28 -0800 | [diff] [blame] | 294 | |
Yu-Ping Wu | ae79af6 | 2020-09-23 16:48:06 +0800 | [diff] [blame] | 295 | build_text_files(inputs, files, output_dir) |
Shelley Chen | 2f616ac | 2017-05-22 13:19:40 -0700 | [diff] [blame] | 296 | |
Yu-Ping Wu | 338f083 | 2020-10-23 16:14:40 +0800 | [diff] [blame] | 297 | for name, category in files.items(): |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 298 | style = get_config_with_defaults(styles, category) |
| 299 | args = ( |
| 300 | locale, |
| 301 | os.path.join(output_dir, '%s.txt' % name), |
| 302 | fonts.get(locale, default_font), |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 303 | output_dir, |
| 304 | ) |
| 305 | kwargs = { |
Yu-Ping Wu | ed95df3 | 2020-11-04 17:08:15 +0800 | [diff] [blame] | 306 | 'height': style[KEY_HEIGHT], |
| 307 | 'max_width': style[KEY_MAX_WIDTH], |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 308 | 'bgcolor': style[KEY_BGCOLOR], |
| 309 | 'fgcolor': style[KEY_FGCOLOR], |
| 310 | } |
| 311 | results.append(pool.apply_async(convert_text_to_png, args, kwargs)) |
Hung-Te Lin | 04addcc | 2015-03-23 18:43:30 +0800 | [diff] [blame] | 312 | pool.close() |
Jes Klinke | 1687a99 | 2020-06-16 13:47:17 -0700 | [diff] [blame] | 313 | if json_dir is not None: |
| 314 | shutil.rmtree(json_dir) |
Yu-Ping Wu | d71b445 | 2020-06-16 11:00:26 +0800 | [diff] [blame] | 315 | print() |
Yu-Ping Wu | c90a22f | 2020-04-24 11:17:15 +0800 | [diff] [blame] | 316 | |
| 317 | try: |
| 318 | success = [r.get() for r in results] |
| 319 | except KeyboardInterrupt: |
| 320 | pool.terminate() |
| 321 | pool.join() |
| 322 | exit('Aborted by user') |
| 323 | else: |
| 324 | pool.join() |
| 325 | if not all(success): |
| 326 | exit('Failed to render some locales') |
Hung-Te Lin | 707e2ef | 2013-08-06 10:20:04 +0800 | [diff] [blame] | 327 | |
| 328 | |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 329 | def build_strings(formats): |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 330 | """Builds text strings.""" |
Yu-Ping Wu | 11027f0 | 2020-10-14 17:35:42 +0800 | [diff] [blame] | 331 | # Convert glyphs |
| 332 | print('Converting glyphs...') |
| 333 | convert_glyphs() |
| 334 | |
Yu-Ping Wu | 7f6639a | 2020-09-28 15:31:35 +0800 | [diff] [blame] | 335 | # Convert generic (locale-independent) strings |
Yu-Ping Wu | 338f083 | 2020-10-23 16:14:40 +0800 | [diff] [blame] | 336 | files = formats[KEY_GENERIC_FILES] |
Yu-Ping Wu | 7f6639a | 2020-09-28 15:31:35 +0800 | [diff] [blame] | 337 | styles = formats[KEY_STYLES] |
| 338 | fonts = formats[KEY_FONTS] |
Yu-Ping Wu | 177f12c | 2020-11-04 15:55:37 +0800 | [diff] [blame] | 339 | default_font = fonts[KEY_DEFAULT] |
Yu-Ping Wu | 7f6639a | 2020-09-28 15:31:35 +0800 | [diff] [blame] | 340 | |
| 341 | for input_file in glob.glob(os.path.join(STRINGS_DIR, '*.txt')): |
| 342 | name, _ = os.path.splitext(os.path.basename(input_file)) |
Yu-Ping Wu | 338f083 | 2020-10-23 16:14:40 +0800 | [diff] [blame] | 343 | category = files[name] |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 344 | style = get_config_with_defaults(styles, category) |
Yu-Ping Wu | ed95df3 | 2020-11-04 17:08:15 +0800 | [diff] [blame] | 345 | if not convert_text_to_png(None, input_file, default_font, STAGE_DIR, |
| 346 | height=style[KEY_HEIGHT], |
| 347 | max_width=style[KEY_MAX_WIDTH], |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 348 | bgcolor=style[KEY_BGCOLOR], |
| 349 | fgcolor=style[KEY_FGCOLOR]): |
Yu-Ping Wu | 7f6639a | 2020-09-28 15:31:35 +0800 | [diff] [blame] | 350 | exit('Failed to convert text %s' % input_file) |
| 351 | |
| 352 | # Convert localized strings |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 353 | convert_localized_strings(formats) |
| 354 | |
| 355 | |
| 356 | def load_boards_config(filename): |
| 357 | """Loads the configuration of all boards from `filename`. |
| 358 | |
| 359 | Args: |
| 360 | filename: File name of a YAML config file. |
| 361 | |
| 362 | Returns: |
| 363 | A dictionary mapping each board name to its config. |
| 364 | """ |
| 365 | with open(filename, 'rb') as file: |
| 366 | raw = yaml.load(file) |
| 367 | |
| 368 | configs = {} |
Yu-Ping Wu | 177f12c | 2020-11-04 15:55:37 +0800 | [diff] [blame] | 369 | default = raw[KEY_DEFAULT] |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 370 | if not default: |
| 371 | raise BuildImageError('Default configuration is not found') |
| 372 | for boards, params in raw.items(): |
Yu-Ping Wu | 177f12c | 2020-11-04 15:55:37 +0800 | [diff] [blame] | 373 | if boards == KEY_DEFAULT: |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 374 | continue |
| 375 | config = copy.deepcopy(default) |
| 376 | if params: |
| 377 | config.update(params) |
| 378 | for board in boards.replace(',', ' ').split(): |
| 379 | configs[board] = config |
| 380 | |
| 381 | return configs |
| 382 | |
| 383 | |
| 384 | class Converter(object): |
| 385 | """Converter from assets, texts, URLs, and fonts to bitmap images. |
| 386 | |
| 387 | Attributes: |
| 388 | ASSET_DIR (str): Directory of image assets. |
| 389 | DEFAULT_OUTPUT_EXT (str): Default output file extension. |
| 390 | DEFAULT_REPLACE_MAP (dict): Default mapping of file replacement. For |
| 391 | {'a': 'b'}, "a.*" will be converted to "b.*". |
Yu-Ping Wu | 177f12c | 2020-11-04 15:55:37 +0800 | [diff] [blame] | 392 | SCALE_BASE (int): The base for bitmap scales, same as UI_SCALE in |
| 393 | depthcharge. For example, if `SCALE_BASE` is 1000, then height = 200 means |
| 394 | 20% of the screen height. Also see the 'styles' section in format.yaml. |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 395 | DEFAULT_FONT_HEIGHT (tuple): Height of the font images. |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 396 | ASSET_MAX_COLORS (int): Maximum colors to use for converting image assets |
| 397 | to bitmaps. |
| 398 | DEFAULT_BACKGROUND (tuple): Default background color. |
| 399 | BACKGROUND_COLORS (dict): Background color of each image. Key is the image |
| 400 | name and value is a tuple of RGB values. |
| 401 | """ |
| 402 | |
| 403 | ASSET_DIR = 'assets' |
| 404 | DEFAULT_OUTPUT_EXT = '.bmp' |
| 405 | |
| 406 | DEFAULT_REPLACE_MAP = { |
| 407 | 'rec_sel_desc1_no_sd': '', |
| 408 | 'rec_sel_desc1_no_phone_no_sd': '', |
| 409 | 'rec_disk_step1_desc0_no_sd': '', |
| 410 | 'rec_to_dev_desc1_phyrec': '', |
| 411 | 'rec_to_dev_desc1_power': '', |
| 412 | 'navigate0_tablet': '', |
| 413 | 'navigate1_tablet': '', |
| 414 | 'nav-button_power': '', |
| 415 | 'nav-button_volume_up': '', |
| 416 | 'nav-button_volume_down': '', |
| 417 | 'broken_desc_phyrec': '', |
| 418 | 'broken_desc_detach': '', |
| 419 | } |
| 420 | |
| 421 | # scales |
Yu-Ping Wu | 177f12c | 2020-11-04 15:55:37 +0800 | [diff] [blame] | 422 | SCALE_BASE = 1000 |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 423 | DEFAULT_FONT_HEIGHT = 20 |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 424 | |
| 425 | # background colors |
| 426 | DEFAULT_BACKGROUND = (0x20, 0x21, 0x24) |
| 427 | LANG_HEADER_BACKGROUND = (0x16, 0x17, 0x19) |
| 428 | LINK_SELECTED_BACKGROUND = (0x2a, 0x2f, 0x39) |
| 429 | ASSET_MAX_COLORS = 128 |
| 430 | |
| 431 | BACKGROUND_COLORS = { |
| 432 | 'ic_dropdown': LANG_HEADER_BACKGROUND, |
| 433 | 'ic_dropleft_focus': LINK_SELECTED_BACKGROUND, |
| 434 | 'ic_dropright_focus': LINK_SELECTED_BACKGROUND, |
| 435 | 'ic_globe': LANG_HEADER_BACKGROUND, |
| 436 | 'ic_search_focus': LINK_SELECTED_BACKGROUND, |
| 437 | 'ic_settings_focus': LINK_SELECTED_BACKGROUND, |
| 438 | 'ic_power_focus': LINK_SELECTED_BACKGROUND, |
| 439 | } |
| 440 | |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 441 | def __init__(self, board, formats, board_config, output): |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 442 | """Inits converter. |
| 443 | |
| 444 | Args: |
| 445 | board: Board name. |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 446 | formats: A dictionary of string formats. |
| 447 | board_config: A dictionary of board configurations. |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 448 | output: Output directory. |
| 449 | """ |
| 450 | self.board = board |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 451 | self.formats = formats |
| 452 | self.config = board_config |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 453 | self.set_dirs(output) |
| 454 | self.set_screen() |
| 455 | self.set_replace_map() |
| 456 | self.set_locales() |
| 457 | self.text_max_colors = self.config[TEXT_COLORS_KEY] |
| 458 | |
| 459 | def set_dirs(self, output): |
| 460 | """Sets board output directory and stage directory. |
| 461 | |
| 462 | Args: |
| 463 | output: Output directory. |
| 464 | """ |
| 465 | self.output_dir = os.path.join(output, self.board) |
| 466 | self.output_ro_dir = os.path.join(self.output_dir, 'locale', 'ro') |
| 467 | self.output_rw_dir = os.path.join(self.output_dir, 'locale', 'rw') |
| 468 | self.stage_dir = os.path.join(output, '.stage') |
| 469 | self.temp_dir = os.path.join(self.stage_dir, 'tmp') |
| 470 | |
| 471 | def set_screen(self): |
| 472 | """Sets screen width and height.""" |
| 473 | self.screen_width, self.screen_height = self.config[SCREEN_KEY] |
| 474 | |
Yu-Ping Wu | e445e04 | 2020-11-19 15:53:42 +0800 | [diff] [blame^] | 475 | self.panel_stretch = fractions.Fraction(1) |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 476 | if self.config[PANEL_KEY]: |
Yu-Ping Wu | e445e04 | 2020-11-19 15:53:42 +0800 | [diff] [blame^] | 477 | # Calculate `panel_stretch`. It's used to shrink images horizontally so |
| 478 | # that the resulting images will look proportional to the original image |
| 479 | # on the stretched display. If the display is not stretched, meaning the |
| 480 | # aspect ratio is same as the screen where images were rendered, no |
| 481 | # shrinking is performed. |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 482 | panel_width, panel_height = self.config[PANEL_KEY] |
Yu-Ping Wu | e445e04 | 2020-11-19 15:53:42 +0800 | [diff] [blame^] | 483 | self.panel_stretch = fractions.Fraction(self.screen_width * panel_height, |
| 484 | self.screen_height * panel_width) |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 485 | |
Yu-Ping Wu | e445e04 | 2020-11-19 15:53:42 +0800 | [diff] [blame^] | 486 | if self.panel_stretch > 1: |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 487 | raise BuildImageError('Panel aspect ratio (%f) is smaller than screen ' |
| 488 | 'aspect ratio (%f). It indicates screen will be ' |
| 489 | 'shrunk horizontally. It is currently unsupported.' |
| 490 | % (panel_width / panel_height, |
| 491 | self.screen_width / self.screen_height)) |
| 492 | |
| 493 | # Set up square drawing area |
| 494 | self.canvas_px = min(self.screen_width, self.screen_height) |
| 495 | |
| 496 | def set_replace_map(self): |
| 497 | """Sets a map replacing images. |
| 498 | |
| 499 | For each (key, value), image 'key' will be replaced by image 'value'. |
| 500 | """ |
| 501 | replace_map = self.DEFAULT_REPLACE_MAP.copy() |
| 502 | |
| 503 | if os.getenv('DETACHABLE') == '1': |
| 504 | replace_map.update({ |
| 505 | 'nav-key_enter': 'nav-button_power', |
| 506 | 'nav-key_up': 'nav-button_volume_up', |
| 507 | 'nav-key_down': 'nav-button_volume_down', |
| 508 | 'navigate0': 'navigate0_tablet', |
| 509 | 'navigate1': 'navigate1_tablet', |
| 510 | 'broken_desc': 'broken_desc_detach', |
| 511 | }) |
| 512 | |
| 513 | physical_presence = os.getenv('PHYSICAL_PRESENCE') |
| 514 | if physical_presence == 'recovery': |
| 515 | replace_map['rec_to_dev_desc1'] = 'rec_to_dev_desc1_phyrec' |
| 516 | replace_map['broken_desc'] = 'broken_desc_phyrec' |
| 517 | elif physical_presence == 'power': |
| 518 | replace_map['rec_to_dev_desc1'] = 'rec_to_dev_desc1_power' |
| 519 | elif physical_presence != 'keyboard': |
| 520 | raise BuildImageError('Invalid physical presence setting %s for board %s' |
| 521 | % (physical_presence, self.board)) |
| 522 | |
| 523 | if not self.config[SDCARD_KEY]: |
| 524 | replace_map.update({ |
| 525 | 'rec_sel_desc1': 'rec_sel_desc1_no_sd', |
| 526 | 'rec_sel_desc1_no_phone': 'rec_sel_desc1_no_phone_no_sd', |
| 527 | 'rec_disk_step1_desc0': 'rec_disk_step1_desc0_no_sd', |
| 528 | }) |
| 529 | |
| 530 | self.replace_map = replace_map |
| 531 | |
| 532 | def set_locales(self): |
| 533 | """Sets a list of locales for which localized images are converted.""" |
| 534 | # LOCALES environment variable can overwrite boards.yaml |
| 535 | env_locales = os.getenv('LOCALES') |
| 536 | rtl_locales = set(self.config[RTL_KEY]) |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 537 | if env_locales: |
| 538 | locales = env_locales.split() |
| 539 | else: |
| 540 | locales = self.config[LOCALES_KEY] |
| 541 | # Check rtl_locales are contained in locales. |
| 542 | unknown_rtl_locales = rtl_locales - set(locales) |
| 543 | if unknown_rtl_locales: |
| 544 | raise BuildImageError('Unknown locales %s in %s' % |
| 545 | (list(unknown_rtl_locales), RTL_KEY)) |
Yu-Ping Wu | abb9afb | 2020-10-27 17:15:22 +0800 | [diff] [blame] | 546 | self.locales = [LocaleInfo(code, code in rtl_locales) |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 547 | for code in locales] |
| 548 | |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 549 | def _get_svg_height(self, svg_file): |
| 550 | tree = ElementTree.parse(svg_file) |
| 551 | height = tree.getroot().attrib['height'] |
| 552 | m = re.match('([0-9]+)pt', height) |
| 553 | if not m: |
| 554 | raise BuildImageError('Cannot get height from %s' % svg_file) |
| 555 | return int(m.group(1)) |
| 556 | |
| 557 | def get_num_lines(self, file, one_line_dir): |
| 558 | """Gets the number of lines of text in `file`.""" |
| 559 | name, _ = os.path.splitext(os.path.basename(file)) |
| 560 | svg_name = name + '.svg' |
| 561 | multi_line_file = os.path.join(os.path.dirname(file), svg_name) |
| 562 | one_line_file = os.path.join(one_line_dir, svg_name) |
| 563 | # The number of lines id determined by comparing the height of |
| 564 | # `multi_line_file` with `one_line_file`, where the latter is generated |
| 565 | # without the '--width' option passed to pango-view. |
| 566 | height = self._get_svg_height(multi_line_file) |
| 567 | line_height = self._get_svg_height(one_line_file) |
| 568 | return int(round(height / line_height)) |
| 569 | |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 570 | def convert_svg_to_png(self, svg_file, png_file, height, num_lines, |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 571 | background): |
| 572 | """Converts .svg file to .png file.""" |
| 573 | background_hex = ''.join(format(x, '02x') for x in background) |
| 574 | # If the width/height of the SVG file is specified in points, the |
| 575 | # rsvg-convert command with default 90DPI will potentially cause the pixels |
| 576 | # at the right/bottom border of the output image to be transparent (or |
| 577 | # filled with the specified background color). This seems like an |
| 578 | # rsvg-convert issue regarding image scaling. Therefore, use 72DPI here |
| 579 | # to avoid the scaling. |
| 580 | command = ['rsvg-convert', |
| 581 | '--background-color', "'#%s'" % background_hex, |
| 582 | '--dpi-x', '72', |
| 583 | '--dpi-y', '72', |
| 584 | '-o', png_file] |
Yu-Ping Wu | e445e04 | 2020-11-19 15:53:42 +0800 | [diff] [blame^] | 585 | height_px = int(self.canvas_px * height / self.SCALE_BASE) * num_lines |
| 586 | if height_px <= 0: |
| 587 | raise BuildImageError('Height of %r <= 0 (%dpx)' % |
| 588 | (os.path.basename(svg_file), height_px)) |
| 589 | command.extend(['--height', '%d' % height_px]) |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 590 | command.append(svg_file) |
| 591 | subprocess.check_call(' '.join(command), shell=True) |
| 592 | |
Yu-Ping Wu | e445e04 | 2020-11-19 15:53:42 +0800 | [diff] [blame^] | 593 | def convert_to_bitmap(self, input_file, num_lines, background, output, |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 594 | max_colors): |
| 595 | """Converts an image file `input_file` to a BMP file `output`.""" |
| 596 | image = Image.open(input_file) |
| 597 | |
| 598 | # Process alpha channel and transparency. |
| 599 | if image.mode == 'RGBA': |
| 600 | target = Image.new('RGB', image.size, background) |
| 601 | image.load() # required for image.split() |
| 602 | mask = image.split()[-1] |
| 603 | target.paste(image, mask=mask) |
| 604 | elif (image.mode == 'P') and ('transparency' in image.info): |
| 605 | exit('Sorry, PNG with RGBA palette is not supported.') |
| 606 | elif image.mode != 'RGB': |
| 607 | target = image.convert('RGB') |
| 608 | else: |
| 609 | target = image |
| 610 | |
Yu-Ping Wu | e445e04 | 2020-11-19 15:53:42 +0800 | [diff] [blame^] | 611 | # Stretch image horizontally for stretched display |
| 612 | if self.panel_stretch != 1: |
| 613 | new_width_px = int(image.size[0] * self.panel_stretch) |
| 614 | new_size = (new_width_px, image.size[1]) |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 615 | target = target.resize(new_size, Image.BICUBIC) |
| 616 | |
| 617 | # Export and downsample color space. |
| 618 | target.convert('P', dither=None, colors=max_colors, palette=Image.ADAPTIVE |
| 619 | ).save(output) |
| 620 | |
| 621 | with open(output, 'rb+') as f: |
| 622 | f.seek(BMP_HEADER_OFFSET_NUM_LINES) |
| 623 | f.write(bytearray([num_lines])) |
| 624 | |
Yu-Ping Wu | ed95df3 | 2020-11-04 17:08:15 +0800 | [diff] [blame] | 625 | def convert(self, files, output_dir, heights, max_widths, max_colors, |
| 626 | one_line_dir=None): |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 627 | """Converts file(s) to bitmap format.""" |
| 628 | if not files: |
| 629 | raise BuildImageError('Unable to find file(s) to convert') |
| 630 | |
| 631 | for file in files: |
| 632 | name, ext = os.path.splitext(os.path.basename(file)) |
| 633 | output = os.path.join(output_dir, name + self.DEFAULT_OUTPUT_EXT) |
| 634 | |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 635 | if name in self.replace_map: |
| 636 | name = self.replace_map[name] |
| 637 | if not name: |
| 638 | continue |
| 639 | print('Replace: %s => %s' % (file, name)) |
| 640 | file = os.path.join(os.path.dirname(file), name + ext) |
| 641 | |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 642 | background = self.BACKGROUND_COLORS.get(name, self.DEFAULT_BACKGROUND) |
| 643 | height = heights[name] |
Yu-Ping Wu | ed95df3 | 2020-11-04 17:08:15 +0800 | [diff] [blame] | 644 | max_width = max_widths[name] |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 645 | |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 646 | # Determine num_lines in order to scale the image |
Yu-Ping Wu | ed95df3 | 2020-11-04 17:08:15 +0800 | [diff] [blame] | 647 | if one_line_dir and max_width: |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 648 | num_lines = self.get_num_lines(file, one_line_dir) |
| 649 | else: |
| 650 | num_lines = 1 |
| 651 | |
| 652 | if ext == '.svg': |
| 653 | png_file = os.path.join(self.temp_dir, name + '.png') |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 654 | self.convert_svg_to_png(file, png_file, height, num_lines, background) |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 655 | file = png_file |
| 656 | |
Yu-Ping Wu | e445e04 | 2020-11-19 15:53:42 +0800 | [diff] [blame^] | 657 | self.convert_to_bitmap(file, num_lines, background, output, max_colors) |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 658 | |
Yu-Ping Wu | 177f12c | 2020-11-04 15:55:37 +0800 | [diff] [blame] | 659 | def convert_sprite_images(self): |
| 660 | """Converts sprite images.""" |
| 661 | names = self.formats[KEY_SPRITE_FILES] |
| 662 | styles = self.formats[KEY_STYLES] |
| 663 | # Check redundant images |
| 664 | for filename in glob.glob(os.path.join(self.ASSET_DIR, SVG_FILES)): |
| 665 | name, _ = os.path.splitext(os.path.basename(filename)) |
| 666 | if name not in names: |
| 667 | raise BuildImageError('Sprite image %r not specified in %s' % |
| 668 | (filename, FORMAT_FILE)) |
| 669 | # Convert images |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 670 | files = [] |
Yu-Ping Wu | 177f12c | 2020-11-04 15:55:37 +0800 | [diff] [blame] | 671 | heights = {} |
| 672 | for name, category in names.items(): |
| 673 | style = get_config_with_defaults(styles, category) |
| 674 | files.append(os.path.join(self.ASSET_DIR, name + '.svg')) |
| 675 | heights[name] = style[KEY_HEIGHT] |
Yu-Ping Wu | ed95df3 | 2020-11-04 17:08:15 +0800 | [diff] [blame] | 676 | max_widths = defaultdict(lambda: None) |
| 677 | self.convert(files, self.output_dir, heights, max_widths, |
| 678 | self.ASSET_MAX_COLORS) |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 679 | |
| 680 | def convert_generic_strings(self): |
| 681 | """Converts generic (locale-independent) strings.""" |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 682 | names = self.formats[KEY_GENERIC_FILES] |
| 683 | styles = self.formats[KEY_STYLES] |
| 684 | heights = {} |
Yu-Ping Wu | ed95df3 | 2020-11-04 17:08:15 +0800 | [diff] [blame] | 685 | max_widths = {} |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 686 | for name, category in names.items(): |
| 687 | style = get_config_with_defaults(styles, category) |
| 688 | heights[name] = style[KEY_HEIGHT] |
Yu-Ping Wu | ed95df3 | 2020-11-04 17:08:15 +0800 | [diff] [blame] | 689 | max_widths[name] = style[KEY_MAX_WIDTH] |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 690 | |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 691 | files = glob.glob(os.path.join(self.stage_dir, SVG_FILES)) |
Yu-Ping Wu | ed95df3 | 2020-11-04 17:08:15 +0800 | [diff] [blame] | 692 | self.convert(files, self.output_dir, heights, max_widths, |
| 693 | self.text_max_colors) |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 694 | |
| 695 | def convert_localized_strings(self): |
| 696 | """Converts localized strings.""" |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 697 | names = self.formats[KEY_LOCALIZED_FILES].copy() |
| 698 | if DIAGNOSTIC_UI: |
Yu-Ping Wu | 177f12c | 2020-11-04 15:55:37 +0800 | [diff] [blame] | 699 | names.update(self.formats[KEY_DIAGNOSTIC_FILES]) |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 700 | styles = self.formats[KEY_STYLES] |
| 701 | heights = {} |
Yu-Ping Wu | ed95df3 | 2020-11-04 17:08:15 +0800 | [diff] [blame] | 702 | max_widths = {} |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 703 | for name, category in names.items(): |
| 704 | style = get_config_with_defaults(styles, category) |
| 705 | heights[name] = style[KEY_HEIGHT] |
Yu-Ping Wu | ed95df3 | 2020-11-04 17:08:15 +0800 | [diff] [blame] | 706 | max_widths[name] = style[KEY_MAX_WIDTH] |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 707 | |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 708 | # Using stderr to report progress synchronously |
| 709 | print(' processing:', end='', file=sys.stderr, flush=True) |
| 710 | for locale_info in self.locales: |
| 711 | locale = locale_info.code |
| 712 | ro_locale_dir = os.path.join(self.output_ro_dir, locale) |
| 713 | stage_locale_dir = os.path.join(STAGE_LOCALE_DIR, locale) |
Yu-Ping Wu | abb9afb | 2020-10-27 17:15:22 +0800 | [diff] [blame] | 714 | print(' ' + locale, end='', file=sys.stderr, flush=True) |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 715 | os.makedirs(ro_locale_dir) |
| 716 | self.convert( |
| 717 | glob.glob(os.path.join(stage_locale_dir, SVG_FILES)), |
Yu-Ping Wu | ed95df3 | 2020-11-04 17:08:15 +0800 | [diff] [blame] | 718 | ro_locale_dir, heights, max_widths, self.text_max_colors, |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 719 | one_line_dir=os.path.join(stage_locale_dir, ONE_LINE_DIR)) |
| 720 | print(file=sys.stderr) |
| 721 | |
| 722 | def move_language_images(self): |
| 723 | """Renames language bitmaps and move to self.output_dir. |
| 724 | |
| 725 | The directory self.output_dir contains locale-independent images, and is |
| 726 | used for creating vbgfx.bin by archive_images.py. |
| 727 | """ |
| 728 | for locale_info in self.locales: |
| 729 | locale = locale_info.code |
| 730 | ro_locale_dir = os.path.join(self.output_ro_dir, locale) |
| 731 | old_file = os.path.join(ro_locale_dir, 'language.bmp') |
| 732 | new_file = os.path.join(self.output_dir, 'language_%s.bmp' % locale) |
| 733 | if os.path.exists(new_file): |
| 734 | raise BuildImageError('File already exists: %s' % new_file) |
| 735 | shutil.move(old_file, new_file) |
| 736 | |
| 737 | def convert_fonts(self): |
| 738 | """Converts font images""" |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 739 | heights = defaultdict(lambda: self.DEFAULT_FONT_HEIGHT) |
Yu-Ping Wu | ed95df3 | 2020-11-04 17:08:15 +0800 | [diff] [blame] | 740 | max_widths = defaultdict(lambda: None) |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 741 | files = glob.glob(os.path.join(STAGE_FONT_DIR, SVG_FILES)) |
| 742 | font_output_dir = os.path.join(self.output_dir, 'font') |
| 743 | os.makedirs(font_output_dir) |
Yu-Ping Wu | ed95df3 | 2020-11-04 17:08:15 +0800 | [diff] [blame] | 744 | self.convert(files, font_output_dir, heights, max_widths, |
| 745 | self.text_max_colors) |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 746 | |
| 747 | def copy_images_to_rw(self): |
| 748 | """Copies localized images specified in boards.yaml for RW override.""" |
| 749 | if not self.config[RW_OVERRIDE_KEY]: |
| 750 | print(' No localized images are specified for RW, skipping') |
| 751 | return |
| 752 | |
| 753 | for locale_info in self.locales: |
| 754 | locale = locale_info.code |
| 755 | rw_locale_dir = os.path.join(self.output_ro_dir, locale) |
| 756 | ro_locale_dir = os.path.join(self.output_rw_dir, locale) |
| 757 | os.makedirs(rw_locale_dir) |
| 758 | |
| 759 | for name in self.config[RW_OVERRIDE_KEY]: |
| 760 | ro_src = os.path.join(ro_locale_dir, name + self.DEFAULT_OUTPUT_EXT) |
| 761 | rw_dst = os.path.join(rw_locale_dir, name + self.DEFAULT_OUTPUT_EXT) |
| 762 | shutil.copyfile(ro_src, rw_dst) |
| 763 | |
| 764 | def create_locale_list(self): |
| 765 | """Creates locale list as a CSV file. |
| 766 | |
| 767 | Each line in the file is of format "code,rtl", where |
| 768 | - "code": language code of the locale |
| 769 | - "rtl": "1" for right-to-left language, "0" otherwise |
| 770 | """ |
| 771 | with open(os.path.join(self.output_dir, 'locales'), 'w') as f: |
| 772 | for locale_info in self.locales: |
| 773 | f.write('{},{}\n'.format(locale_info.code, |
| 774 | int(locale_info.rtl))) |
| 775 | |
| 776 | def build(self): |
| 777 | """Builds all images required by a board.""" |
| 778 | # Clean up output directory |
| 779 | if os.path.exists(self.output_dir): |
| 780 | shutil.rmtree(self.output_dir) |
| 781 | os.makedirs(self.output_dir) |
| 782 | |
| 783 | if not os.path.exists(self.stage_dir): |
| 784 | raise BuildImageError('Missing stage folder. Run make in strings dir.') |
| 785 | |
| 786 | # Clean up temp directory |
| 787 | if os.path.exists(self.temp_dir): |
| 788 | shutil.rmtree(self.temp_dir) |
| 789 | os.makedirs(self.temp_dir) |
| 790 | |
Yu-Ping Wu | 177f12c | 2020-11-04 15:55:37 +0800 | [diff] [blame] | 791 | print('Converting sprite images...') |
| 792 | self.convert_sprite_images() |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 793 | |
| 794 | print('Converting generic strings...') |
| 795 | self.convert_generic_strings() |
| 796 | |
| 797 | print('Converting localized strings...') |
| 798 | self.convert_localized_strings() |
| 799 | |
| 800 | print('Moving language images to locale-independent directory...') |
| 801 | self.move_language_images() |
| 802 | |
| 803 | print('Creating locale list file...') |
| 804 | self.create_locale_list() |
| 805 | |
| 806 | print('Converting fonts...') |
| 807 | self.convert_fonts() |
| 808 | |
| 809 | print('Copying specified images to RW packing directory...') |
| 810 | self.copy_images_to_rw() |
| 811 | |
| 812 | |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 813 | def build_images(board, formats): |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 814 | """Builds images for `board`.""" |
| 815 | configs = load_boards_config(BOARDS_CONFIG_FILE) |
| 816 | print('Building for ' + board) |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 817 | converter = Converter(board, formats, configs[board], OUTPUT_DIR) |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 818 | converter.build() |
| 819 | |
| 820 | |
| 821 | def main(): |
| 822 | """Builds bitmaps for firmware screens.""" |
| 823 | parser = argparse.ArgumentParser() |
| 824 | parser.add_argument('board', help='Target board') |
| 825 | args = parser.parse_args() |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 826 | |
| 827 | with open(FORMAT_FILE, encoding='utf-8') as f: |
| 828 | formats = yaml.load(f) |
| 829 | build_strings(formats) |
| 830 | build_images(args.board, formats) |
Yu-Ping Wu | 7f6639a | 2020-09-28 15:31:35 +0800 | [diff] [blame] | 831 | |
| 832 | |
Hung-Te Lin | 707e2ef | 2013-08-06 10:20:04 +0800 | [diff] [blame] | 833 | if __name__ == '__main__': |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 834 | main() |