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 |
Yu-Ping Wu | 49606eb | 2021-03-03 22:43:19 +0800 | [diff] [blame] | 8 | from collections import defaultdict, namedtuple, Counter |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 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 |
Jes Klinke | 1687a99 | 2020-06-16 13:47:17 -0700 | [diff] [blame] | 19 | import tempfile |
Hung-Te Lin | 04addcc | 2015-03-23 18:43:30 +0800 | [diff] [blame] | 20 | |
Hung-Te Lin | 707e2ef | 2013-08-06 10:20:04 +0800 | [diff] [blame] | 21 | import yaml |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 22 | from PIL import Image |
Hung-Te Lin | 707e2ef | 2013-08-06 10:20:04 +0800 | [diff] [blame] | 23 | |
| 24 | SCRIPT_BASE = os.path.dirname(os.path.abspath(__file__)) |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 25 | |
| 26 | STRINGS_GRD_FILE = 'firmware_strings.grd' |
| 27 | STRINGS_JSON_FILE_TMPL = '{}.json' |
| 28 | FORMAT_FILE = 'format.yaml' |
| 29 | BOARDS_CONFIG_FILE = 'boards.yaml' |
| 30 | |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 31 | OUTPUT_DIR = os.getenv('OUTPUT', os.path.join(SCRIPT_BASE, 'build')) |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 32 | |
| 33 | ONE_LINE_DIR = 'one_line' |
| 34 | SVG_FILES = '*.svg' |
| 35 | PNG_FILES = '*.png' |
| 36 | |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 37 | DIAGNOSTIC_UI = os.getenv('DIAGNOSTIC_UI') == '1' |
| 38 | |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 39 | # String format YAML key names. |
Yu-Ping Wu | 177f12c | 2020-11-04 15:55:37 +0800 | [diff] [blame] | 40 | KEY_DEFAULT = '_DEFAULT_' |
Hung-Te Lin | 707e2ef | 2013-08-06 10:20:04 +0800 | [diff] [blame] | 41 | KEY_LOCALES = 'locales' |
Yu-Ping Wu | 338f083 | 2020-10-23 16:14:40 +0800 | [diff] [blame] | 42 | KEY_GENERIC_FILES = 'generic_files' |
| 43 | KEY_LOCALIZED_FILES = 'localized_files' |
Yu-Ping Wu | 177f12c | 2020-11-04 15:55:37 +0800 | [diff] [blame] | 44 | KEY_DIAGNOSTIC_FILES = 'diagnostic_files' |
| 45 | KEY_SPRITE_FILES = 'sprite_files' |
Hung-Te Lin | 707e2ef | 2013-08-06 10:20:04 +0800 | [diff] [blame] | 46 | KEY_STYLES = 'styles' |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 47 | KEY_BGCOLOR = 'bgcolor' |
| 48 | KEY_FGCOLOR = 'fgcolor' |
| 49 | KEY_HEIGHT = 'height' |
Yu-Ping Wu | ed95df3 | 2020-11-04 17:08:15 +0800 | [diff] [blame] | 50 | KEY_MAX_WIDTH = 'max_width' |
Yu-Ping Wu | 177f12c | 2020-11-04 15:55:37 +0800 | [diff] [blame] | 51 | KEY_FONTS = 'fonts' |
Hung-Te Lin | 707e2ef | 2013-08-06 10:20:04 +0800 | [diff] [blame] | 52 | |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 53 | # Board config YAML key names. |
Yu-Ping Wu | 60b4537 | 2021-03-31 16:56:08 +0800 | [diff] [blame] | 54 | KEY_SCREEN = 'screen' |
| 55 | KEY_PANEL = 'panel' |
| 56 | KEY_SDCARD = 'sdcard' |
| 57 | KEY_DPI = 'dpi' |
| 58 | KEY_RTL = 'rtl' |
| 59 | KEY_RW_OVERRIDE = 'rw_override' |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 60 | |
| 61 | BMP_HEADER_OFFSET_NUM_LINES = 6 |
Hung-Te Lin | 707e2ef | 2013-08-06 10:20:04 +0800 | [diff] [blame] | 62 | |
Jes Klinke | 1687a99 | 2020-06-16 13:47:17 -0700 | [diff] [blame] | 63 | # Regular expressions used to eliminate spurious spaces and newlines in |
| 64 | # translation strings. |
| 65 | NEWLINE_PATTERN = re.compile(r'([^\n])\n([^\n])') |
| 66 | NEWLINE_REPLACEMENT = r'\1 \2' |
| 67 | CRLF_PATTERN = re.compile(r'\r\n') |
| 68 | MULTIBLANK_PATTERN = re.compile(r' *') |
| 69 | |
Yu-Ping Wu | 3d07a06 | 2021-01-26 18:10:32 +0800 | [diff] [blame] | 70 | # The base for bitmap scales, same as UI_SCALE in depthcharge. For example, if |
| 71 | # `SCALE_BASE` is 1000, then height = 200 means 20% of the screen height. Also |
| 72 | # see the 'styles' section in format.yaml. |
| 73 | SCALE_BASE = 1000 |
| 74 | DEFAULT_GLYPH_HEIGHT = 20 |
| 75 | |
Yu-Ping Wu | cc86d6a | 2020-11-27 12:48:19 +0800 | [diff] [blame] | 76 | GLYPH_FONT = 'Cousine' |
Yu-Ping Wu | 11027f0 | 2020-10-14 17:35:42 +0800 | [diff] [blame] | 77 | |
Yu-Ping Wu | abb9afb | 2020-10-27 17:15:22 +0800 | [diff] [blame] | 78 | LocaleInfo = namedtuple('LocaleInfo', ['code', 'rtl']) |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 79 | |
Yu-Ping Wu | 6b282c5 | 2020-03-19 12:54:15 +0800 | [diff] [blame] | 80 | |
Hung-Te Lin | 707e2ef | 2013-08-06 10:20:04 +0800 | [diff] [blame] | 81 | class DataError(Exception): |
| 82 | pass |
| 83 | |
| 84 | |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 85 | class BuildImageError(Exception): |
| 86 | """The exception class for all errors generated during build image process.""" |
| 87 | |
| 88 | |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 89 | def get_config_with_defaults(configs, key): |
| 90 | """Gets config of `key` from `configs`. |
| 91 | |
| 92 | If `key` is not present in `configs`, the default config will be returned. |
| 93 | Similarly, if some config values are missing for `key`, the default ones will |
| 94 | be used. |
| 95 | """ |
Yu-Ping Wu | 177f12c | 2020-11-04 15:55:37 +0800 | [diff] [blame] | 96 | config = configs[KEY_DEFAULT].copy() |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 97 | config.update(configs.get(key, {})) |
| 98 | return config |
| 99 | |
| 100 | |
Yu-Ping Wu | 675e7e8 | 2021-01-29 08:32:12 +0800 | [diff] [blame] | 101 | def load_boards_config(filename): |
| 102 | """Loads the configuration of all boards from `filename`. |
| 103 | |
| 104 | Args: |
| 105 | filename: File name of a YAML config file. |
| 106 | |
| 107 | Returns: |
| 108 | A dictionary mapping each board name to its config. |
| 109 | """ |
| 110 | with open(filename, 'rb') as file: |
| 111 | raw = yaml.load(file) |
| 112 | |
| 113 | configs = {} |
| 114 | default = raw[KEY_DEFAULT] |
| 115 | if not default: |
| 116 | raise BuildImageError('Default configuration is not found') |
| 117 | for boards, params in raw.items(): |
| 118 | if boards == KEY_DEFAULT: |
| 119 | continue |
| 120 | config = copy.deepcopy(default) |
| 121 | if params: |
| 122 | config.update(params) |
| 123 | for board in boards.replace(',', ' ').split(): |
| 124 | configs[board] = config |
| 125 | |
| 126 | return configs |
| 127 | |
| 128 | |
| 129 | def check_fonts(fonts): |
| 130 | """Check if all fonts are available.""" |
| 131 | for locale, font in fonts.items(): |
| 132 | if subprocess.run(['fc-list', '-q', font]).returncode != 0: |
| 133 | raise BuildImageError('Font %r not found for locale %r' |
| 134 | % (font, locale)) |
| 135 | |
| 136 | |
Yu-Ping Wu | 9704693 | 2021-01-25 17:38:56 +0800 | [diff] [blame] | 137 | def run_pango_view(input_file, output_file, locale, font, height, max_width, |
| 138 | dpi, bgcolor, fgcolor, hinting='full'): |
| 139 | """Run pango-view.""" |
| 140 | command = ['pango-view', '-q'] |
Yu-Ping Wu | 11027f0 | 2020-10-14 17:35:42 +0800 | [diff] [blame] | 141 | if locale: |
Yu-Ping Wu | 9704693 | 2021-01-25 17:38:56 +0800 | [diff] [blame] | 142 | command += ['--language', locale] |
| 143 | |
| 144 | # Font size should be proportional to the height. Here we use 2 as the |
| 145 | # divisor so that setting dpi to 96 (pango-view's default) in boards.yaml |
| 146 | # will be roughly equivalent to setting the screen resolution to 1366x768. |
| 147 | font_size = height / 2 |
| 148 | font_spec = '%s %r' % (font, font_size) |
| 149 | command += ['--font', font_spec] |
| 150 | |
Yu-Ping Wu | ed95df3 | 2020-11-04 17:08:15 +0800 | [diff] [blame] | 151 | if max_width: |
Yu-Ping Wu | e66a7b0 | 2020-11-19 15:18:08 +0800 | [diff] [blame] | 152 | # When converting text to PNG by pango-view, the ratio of image height to |
| 153 | # the font size is usually no more than 1.1875 (with Roboto). Therefore, |
| 154 | # set the `max_width_pt` as follows to prevent UI drawing from exceeding |
| 155 | # the canvas boundary in depthcharge runtime. The divisor 2 is the same in |
| 156 | # the calculation of `font_size` above. |
| 157 | max_width_pt = int(max_width / 2 * 1.1875) |
Yu-Ping Wu | ed95df3 | 2020-11-04 17:08:15 +0800 | [diff] [blame] | 158 | command.append('--width=%d' % max_width_pt) |
Yu-Ping Wu | e66a7b0 | 2020-11-19 15:18:08 +0800 | [diff] [blame] | 159 | if dpi: |
| 160 | command.append('--dpi=%d' % dpi) |
Yu-Ping Wu | cc86d6a | 2020-11-27 12:48:19 +0800 | [diff] [blame] | 161 | command.append('--margin=0') |
Yu-Ping Wu | 9704693 | 2021-01-25 17:38:56 +0800 | [diff] [blame] | 162 | command += ['--background', bgcolor] |
| 163 | command += ['--foreground', fgcolor] |
| 164 | command += ['--hinting', hinting] |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 165 | |
Yu-Ping Wu | 9704693 | 2021-01-25 17:38:56 +0800 | [diff] [blame] | 166 | command += ['--output', output_file] |
Yu-Ping Wu | 11027f0 | 2020-10-14 17:35:42 +0800 | [diff] [blame] | 167 | command.append(input_file) |
| 168 | |
Yu-Ping Wu | 9704693 | 2021-01-25 17:38:56 +0800 | [diff] [blame] | 169 | subprocess.check_call(command, stdout=subprocess.PIPE) |
| 170 | |
| 171 | |
Yu-Ping Wu | 703dcfd | 2021-01-08 10:52:10 +0800 | [diff] [blame] | 172 | def parse_locale_json_file(locale, json_dir): |
| 173 | """Parses given firmware string json file. |
| 174 | |
| 175 | Args: |
| 176 | locale: The name of the locale, e.g. "da" or "pt-BR". |
| 177 | json_dir: Directory containing json output from grit. |
| 178 | |
| 179 | Returns: |
| 180 | A dictionary for mapping of "name to content" for files to be generated. |
| 181 | """ |
Jes Klinke | 1687a99 | 2020-06-16 13:47:17 -0700 | [diff] [blame] | 182 | result = {} |
Yu-Ping Wu | ae79af6 | 2020-09-23 16:48:06 +0800 | [diff] [blame] | 183 | 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] | 184 | with open(filename, encoding='utf-8-sig') as input_file: |
Jes Klinke | 1687a99 | 2020-06-16 13:47:17 -0700 | [diff] [blame] | 185 | for tag, msgdict in json.load(input_file).items(): |
| 186 | msgtext = msgdict['message'] |
| 187 | msgtext = re.sub(CRLF_PATTERN, '\n', msgtext) |
| 188 | msgtext = re.sub(NEWLINE_PATTERN, NEWLINE_REPLACEMENT, msgtext) |
| 189 | msgtext = re.sub(MULTIBLANK_PATTERN, ' ', msgtext) |
| 190 | # Strip any trailing whitespace. A trailing newline appears to make |
| 191 | # Pango report a larger layout size than what's actually visible. |
| 192 | msgtext = msgtext.strip() |
| 193 | result[tag] = msgtext |
| 194 | return result |
| 195 | |
Yu-Ping Wu | ae79af6 | 2020-09-23 16:48:06 +0800 | [diff] [blame] | 196 | |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 197 | class Converter(object): |
Yu-Ping Wu | 2091367 | 2021-03-24 15:25:10 +0800 | [diff] [blame] | 198 | """Converter for converting sprites, texts, and glyphs to bitmaps. |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 199 | |
| 200 | Attributes: |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 201 | DEFAULT_OUTPUT_EXT (str): Default output file extension. |
Yu-Ping Wu | 2091367 | 2021-03-24 15:25:10 +0800 | [diff] [blame] | 202 | SPRITE_MAX_COLORS (int): Maximum colors to use for converting image sprites |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 203 | to bitmaps. |
Yu-Ping Wu | 22dc45f | 2021-03-24 14:54:36 +0800 | [diff] [blame] | 204 | GLYPH_MAX_COLORS (int): Maximum colors to use for glyph bitmaps. |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 205 | DEFAULT_BACKGROUND (tuple): Default background color. |
| 206 | BACKGROUND_COLORS (dict): Background color of each image. Key is the image |
| 207 | name and value is a tuple of RGB values. |
| 208 | """ |
| 209 | |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 210 | DEFAULT_OUTPUT_EXT = '.bmp' |
| 211 | |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 212 | # background colors |
| 213 | DEFAULT_BACKGROUND = (0x20, 0x21, 0x24) |
| 214 | LANG_HEADER_BACKGROUND = (0x16, 0x17, 0x19) |
| 215 | LINK_SELECTED_BACKGROUND = (0x2a, 0x2f, 0x39) |
Yu-Ping Wu | 2091367 | 2021-03-24 15:25:10 +0800 | [diff] [blame] | 216 | SPRITE_MAX_COLORS = 128 |
Yu-Ping Wu | 22dc45f | 2021-03-24 14:54:36 +0800 | [diff] [blame] | 217 | GLYPH_MAX_COLORS = 7 |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 218 | |
| 219 | BACKGROUND_COLORS = { |
| 220 | 'ic_dropdown': LANG_HEADER_BACKGROUND, |
| 221 | 'ic_dropleft_focus': LINK_SELECTED_BACKGROUND, |
| 222 | 'ic_dropright_focus': LINK_SELECTED_BACKGROUND, |
| 223 | 'ic_globe': LANG_HEADER_BACKGROUND, |
| 224 | 'ic_search_focus': LINK_SELECTED_BACKGROUND, |
| 225 | 'ic_settings_focus': LINK_SELECTED_BACKGROUND, |
| 226 | 'ic_power_focus': LINK_SELECTED_BACKGROUND, |
| 227 | } |
| 228 | |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 229 | def __init__(self, board, formats, board_config, output): |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 230 | """Inits converter. |
| 231 | |
| 232 | Args: |
| 233 | board: Board name. |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 234 | formats: A dictionary of string formats. |
| 235 | board_config: A dictionary of board configurations. |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 236 | output: Output directory. |
| 237 | """ |
| 238 | self.board = board |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 239 | self.formats = formats |
| 240 | self.config = board_config |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 241 | self.set_dirs(output) |
| 242 | self.set_screen() |
Yu-Ping Wu | 3d272e7 | 2021-03-01 12:01:55 +0800 | [diff] [blame] | 243 | self.set_rename_map() |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 244 | self.set_locales() |
Yu-Ping Wu | 60b4537 | 2021-03-31 16:56:08 +0800 | [diff] [blame] | 245 | self.text_max_colors = self.get_text_colors(self.config[KEY_DPI]) |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 246 | |
| 247 | def set_dirs(self, output): |
| 248 | """Sets board output directory and stage directory. |
| 249 | |
| 250 | Args: |
| 251 | output: Output directory. |
| 252 | """ |
Yu-Ping Wu | 675e7e8 | 2021-01-29 08:32:12 +0800 | [diff] [blame] | 253 | self.strings_dir = os.path.join(SCRIPT_BASE, 'strings') |
Yu-Ping Wu | 2091367 | 2021-03-24 15:25:10 +0800 | [diff] [blame] | 254 | self.sprite_dir = os.path.join(SCRIPT_BASE, 'sprite') |
Yu-Ping Wu | 675e7e8 | 2021-01-29 08:32:12 +0800 | [diff] [blame] | 255 | self.locale_dir = os.path.join(self.strings_dir, 'locale') |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 256 | self.output_dir = os.path.join(output, self.board) |
| 257 | self.output_ro_dir = os.path.join(self.output_dir, 'locale', 'ro') |
| 258 | self.output_rw_dir = os.path.join(self.output_dir, 'locale', 'rw') |
| 259 | self.stage_dir = os.path.join(output, '.stage') |
Yu-Ping Wu | 675e7e8 | 2021-01-29 08:32:12 +0800 | [diff] [blame] | 260 | self.stage_locale_dir = os.path.join(self.stage_dir, 'locale') |
Yu-Ping Wu | 31a6e6b | 2021-03-24 15:08:53 +0800 | [diff] [blame] | 261 | self.stage_glyph_dir = os.path.join(self.stage_dir, 'glyph') |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 262 | self.temp_dir = os.path.join(self.stage_dir, 'tmp') |
| 263 | |
| 264 | def set_screen(self): |
| 265 | """Sets screen width and height.""" |
Yu-Ping Wu | 60b4537 | 2021-03-31 16:56:08 +0800 | [diff] [blame] | 266 | self.screen_width, self.screen_height = self.config[KEY_SCREEN] |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 267 | |
Yu-Ping Wu | e445e04 | 2020-11-19 15:53:42 +0800 | [diff] [blame] | 268 | self.panel_stretch = fractions.Fraction(1) |
Yu-Ping Wu | 60b4537 | 2021-03-31 16:56:08 +0800 | [diff] [blame] | 269 | if self.config[KEY_PANEL]: |
Yu-Ping Wu | e445e04 | 2020-11-19 15:53:42 +0800 | [diff] [blame] | 270 | # Calculate `panel_stretch`. It's used to shrink images horizontally so |
| 271 | # that the resulting images will look proportional to the original image |
| 272 | # on the stretched display. If the display is not stretched, meaning the |
| 273 | # aspect ratio is same as the screen where images were rendered, no |
| 274 | # shrinking is performed. |
Yu-Ping Wu | 60b4537 | 2021-03-31 16:56:08 +0800 | [diff] [blame] | 275 | panel_width, panel_height = self.config[KEY_PANEL] |
Yu-Ping Wu | e445e04 | 2020-11-19 15:53:42 +0800 | [diff] [blame] | 276 | self.panel_stretch = fractions.Fraction(self.screen_width * panel_height, |
| 277 | self.screen_height * panel_width) |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 278 | |
Yu-Ping Wu | e445e04 | 2020-11-19 15:53:42 +0800 | [diff] [blame] | 279 | if self.panel_stretch > 1: |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 280 | raise BuildImageError('Panel aspect ratio (%f) is smaller than screen ' |
| 281 | 'aspect ratio (%f). It indicates screen will be ' |
| 282 | 'shrunk horizontally. It is currently unsupported.' |
| 283 | % (panel_width / panel_height, |
| 284 | self.screen_width / self.screen_height)) |
| 285 | |
| 286 | # Set up square drawing area |
| 287 | self.canvas_px = min(self.screen_width, self.screen_height) |
| 288 | |
Yu-Ping Wu | 3d272e7 | 2021-03-01 12:01:55 +0800 | [diff] [blame] | 289 | def set_rename_map(self): |
| 290 | """Initializes a dict `self.rename_map` for image renaming. |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 291 | |
Yu-Ping Wu | 3d272e7 | 2021-03-01 12:01:55 +0800 | [diff] [blame] | 292 | For each items in the dict, image `key` will be renamed to `value`. |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 293 | """ |
Yu-Ping Wu | 3d272e7 | 2021-03-01 12:01:55 +0800 | [diff] [blame] | 294 | is_detachable = os.getenv('DETACHABLE') == '1' |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 295 | physical_presence = os.getenv('PHYSICAL_PRESENCE') |
Yu-Ping Wu | 3d272e7 | 2021-03-01 12:01:55 +0800 | [diff] [blame] | 296 | rename_map = {} |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 297 | |
Yu-Ping Wu | 3d272e7 | 2021-03-01 12:01:55 +0800 | [diff] [blame] | 298 | # Navigation instructions |
| 299 | if is_detachable: |
| 300 | rename_map.update({ |
| 301 | 'nav-button_power': 'nav-key_enter', |
| 302 | 'nav-button_volume_up': 'nav-key_up', |
| 303 | 'nav-button_volume_down': 'nav-key_down', |
| 304 | 'navigate0_tablet': 'navigate0', |
| 305 | 'navigate1_tablet': 'navigate1', |
| 306 | }) |
| 307 | else: |
| 308 | rename_map.update({ |
| 309 | 'nav-button_power': None, |
| 310 | 'nav-button_volume_up': None, |
| 311 | 'nav-button_volume_down': None, |
| 312 | 'navigate0_tablet': None, |
| 313 | 'navigate1_tablet': None, |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 314 | }) |
| 315 | |
Yu-Ping Wu | 3d272e7 | 2021-03-01 12:01:55 +0800 | [diff] [blame] | 316 | # Physical presence confirmation |
| 317 | if physical_presence == 'recovery': |
| 318 | rename_map['rec_to_dev_desc1_phyrec'] = 'rec_to_dev_desc1' |
| 319 | rename_map['rec_to_dev_desc1_power'] = None |
| 320 | elif physical_presence == 'power': |
| 321 | rename_map['rec_to_dev_desc1_phyrec'] = None |
| 322 | rename_map['rec_to_dev_desc1_power'] = 'rec_to_dev_desc1' |
| 323 | else: |
| 324 | rename_map['rec_to_dev_desc1_phyrec'] = None |
| 325 | rename_map['rec_to_dev_desc1_power'] = None |
| 326 | if physical_presence != 'keyboard': |
| 327 | raise BuildImageError('Invalid physical presence setting %s for board ' |
| 328 | '%s' % (physical_presence, self.board)) |
| 329 | |
| 330 | # Broken screen |
| 331 | if physical_presence == 'recovery': |
| 332 | rename_map['broken_desc_phyrec'] = 'broken_desc' |
| 333 | rename_map['broken_desc_detach'] = None |
| 334 | elif is_detachable: |
| 335 | rename_map['broken_desc_phyrec'] = None |
| 336 | rename_map['broken_desc_detach'] = 'broken_desc' |
| 337 | else: |
| 338 | rename_map['broken_desc_phyrec'] = None |
| 339 | rename_map['broken_desc_detach'] = None |
| 340 | |
| 341 | # SD card |
Yu-Ping Wu | 60b4537 | 2021-03-31 16:56:08 +0800 | [diff] [blame] | 342 | if not self.config[KEY_SDCARD]: |
Yu-Ping Wu | 3d272e7 | 2021-03-01 12:01:55 +0800 | [diff] [blame] | 343 | rename_map.update({ |
| 344 | 'rec_sel_desc1_no_sd': 'rec_sel_desc1', |
| 345 | 'rec_sel_desc1_no_phone_no_sd': 'rec_sel_desc1_no_phone', |
| 346 | 'rec_disk_step1_desc0_no_sd': 'rec_disk_step1_desc0', |
| 347 | }) |
| 348 | else: |
| 349 | rename_map.update({ |
| 350 | 'rec_sel_desc1_no_sd': None, |
| 351 | 'rec_sel_desc1_no_phone_no_sd': None, |
| 352 | 'rec_disk_step1_desc0_no_sd': None, |
| 353 | }) |
| 354 | |
| 355 | # Check for duplicate new names |
| 356 | new_names = list(new_name for new_name in rename_map.values() if new_name) |
| 357 | if len(set(new_names)) != len(new_names): |
| 358 | raise BuildImageError('Duplicate values found in rename_map') |
| 359 | |
| 360 | # Map new_name to None to skip image generation for it |
| 361 | for new_name in new_names: |
| 362 | if new_name not in rename_map: |
| 363 | rename_map[new_name] = None |
| 364 | |
| 365 | # Print mapping |
| 366 | print('Rename map:') |
| 367 | for name, new_name in sorted(rename_map.items()): |
| 368 | print(' %s => %s' % (name, new_name)) |
| 369 | |
| 370 | self.rename_map = rename_map |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 371 | |
| 372 | def set_locales(self): |
| 373 | """Sets a list of locales for which localized images are converted.""" |
| 374 | # LOCALES environment variable can overwrite boards.yaml |
| 375 | env_locales = os.getenv('LOCALES') |
Yu-Ping Wu | 60b4537 | 2021-03-31 16:56:08 +0800 | [diff] [blame] | 376 | rtl_locales = set(self.config[KEY_RTL]) |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 377 | if env_locales: |
| 378 | locales = env_locales.split() |
| 379 | else: |
Yu-Ping Wu | 60b4537 | 2021-03-31 16:56:08 +0800 | [diff] [blame] | 380 | locales = self.config[KEY_LOCALES] |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 381 | # Check rtl_locales are contained in locales. |
| 382 | unknown_rtl_locales = rtl_locales - set(locales) |
| 383 | if unknown_rtl_locales: |
| 384 | raise BuildImageError('Unknown locales %s in %s' % |
Yu-Ping Wu | 60b4537 | 2021-03-31 16:56:08 +0800 | [diff] [blame] | 385 | (list(unknown_rtl_locales), KEY_RTL)) |
Yu-Ping Wu | abb9afb | 2020-10-27 17:15:22 +0800 | [diff] [blame] | 386 | self.locales = [LocaleInfo(code, code in rtl_locales) |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 387 | for code in locales] |
| 388 | |
Yu-Ping Wu | 96cf002 | 2021-01-07 15:55:49 +0800 | [diff] [blame] | 389 | @classmethod |
| 390 | def get_text_colors(cls, dpi): |
| 391 | """Derive maximum text colors from `dpi`.""" |
| 392 | if dpi < 64: |
| 393 | return 2 |
| 394 | elif dpi < 72: |
| 395 | return 3 |
| 396 | elif dpi < 80: |
| 397 | return 4 |
| 398 | elif dpi < 96: |
| 399 | return 5 |
| 400 | elif dpi < 112: |
| 401 | return 6 |
| 402 | else: |
| 403 | return 7 |
| 404 | |
Yu-Ping Wu | 08defcc | 2020-05-07 16:21:03 +0800 | [diff] [blame] | 405 | def _to_px(self, length, num_lines=1): |
| 406 | """Converts the relative coordinate to absolute one in pixels.""" |
Yu-Ping Wu | 3d07a06 | 2021-01-26 18:10:32 +0800 | [diff] [blame] | 407 | return int(self.canvas_px * length / SCALE_BASE) * num_lines |
Yu-Ping Wu | 08defcc | 2020-05-07 16:21:03 +0800 | [diff] [blame] | 408 | |
Yu-Ping Wu | e66a7b0 | 2020-11-19 15:18:08 +0800 | [diff] [blame] | 409 | def _get_png_height(self, png_file): |
Yu-Ping Wu | 49606eb | 2021-03-03 22:43:19 +0800 | [diff] [blame] | 410 | # With small DPI, pango-view may generate an empty file |
| 411 | if os.path.getsize(png_file) == 0: |
| 412 | return 0 |
Yu-Ping Wu | e66a7b0 | 2020-11-19 15:18:08 +0800 | [diff] [blame] | 413 | with Image.open(png_file) as image: |
| 414 | return image.size[1] |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 415 | |
| 416 | def get_num_lines(self, file, one_line_dir): |
| 417 | """Gets the number of lines of text in `file`.""" |
| 418 | name, _ = os.path.splitext(os.path.basename(file)) |
Yu-Ping Wu | e66a7b0 | 2020-11-19 15:18:08 +0800 | [diff] [blame] | 419 | png_name = name + '.png' |
| 420 | multi_line_file = os.path.join(os.path.dirname(file), png_name) |
| 421 | one_line_file = os.path.join(one_line_dir, png_name) |
| 422 | # The number of lines is determined by comparing the height of |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 423 | # `multi_line_file` with `one_line_file`, where the latter is generated |
| 424 | # without the '--width' option passed to pango-view. |
Yu-Ping Wu | e66a7b0 | 2020-11-19 15:18:08 +0800 | [diff] [blame] | 425 | height = self._get_png_height(multi_line_file) |
| 426 | line_height = self._get_png_height(one_line_file) |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 427 | return int(round(height / line_height)) |
| 428 | |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 429 | 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] | 430 | background): |
| 431 | """Converts .svg file to .png file.""" |
| 432 | background_hex = ''.join(format(x, '02x') for x in background) |
| 433 | # If the width/height of the SVG file is specified in points, the |
| 434 | # rsvg-convert command with default 90DPI will potentially cause the pixels |
| 435 | # at the right/bottom border of the output image to be transparent (or |
| 436 | # filled with the specified background color). This seems like an |
| 437 | # rsvg-convert issue regarding image scaling. Therefore, use 72DPI here |
| 438 | # to avoid the scaling. |
| 439 | command = ['rsvg-convert', |
| 440 | '--background-color', "'#%s'" % background_hex, |
| 441 | '--dpi-x', '72', |
| 442 | '--dpi-y', '72', |
| 443 | '-o', png_file] |
Yu-Ping Wu | 08defcc | 2020-05-07 16:21:03 +0800 | [diff] [blame] | 444 | height_px = self._to_px(height, num_lines) |
Yu-Ping Wu | e445e04 | 2020-11-19 15:53:42 +0800 | [diff] [blame] | 445 | if height_px <= 0: |
| 446 | raise BuildImageError('Height of %r <= 0 (%dpx)' % |
| 447 | (os.path.basename(svg_file), height_px)) |
| 448 | command.extend(['--height', '%d' % height_px]) |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 449 | command.append(svg_file) |
| 450 | subprocess.check_call(' '.join(command), shell=True) |
| 451 | |
Yu-Ping Wu | 49606eb | 2021-03-03 22:43:19 +0800 | [diff] [blame] | 452 | def convert_to_bitmap(self, input_file, num_lines, background, output, |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 453 | max_colors): |
| 454 | """Converts an image file `input_file` to a BMP file `output`.""" |
| 455 | image = Image.open(input_file) |
| 456 | |
| 457 | # Process alpha channel and transparency. |
| 458 | if image.mode == 'RGBA': |
| 459 | target = Image.new('RGB', image.size, background) |
| 460 | image.load() # required for image.split() |
| 461 | mask = image.split()[-1] |
| 462 | target.paste(image, mask=mask) |
| 463 | elif (image.mode == 'P') and ('transparency' in image.info): |
| 464 | exit('Sorry, PNG with RGBA palette is not supported.') |
| 465 | elif image.mode != 'RGB': |
| 466 | target = image.convert('RGB') |
| 467 | else: |
| 468 | target = image |
| 469 | |
Yu-Ping Wu | e66a7b0 | 2020-11-19 15:18:08 +0800 | [diff] [blame] | 470 | width_px, height_px = image.size |
Yu-Ping Wu | e66a7b0 | 2020-11-19 15:18:08 +0800 | [diff] [blame] | 471 | # Stretch image horizontally for stretched display. |
Yu-Ping Wu | e445e04 | 2020-11-19 15:53:42 +0800 | [diff] [blame] | 472 | if self.panel_stretch != 1: |
Yu-Ping Wu | e66a7b0 | 2020-11-19 15:18:08 +0800 | [diff] [blame] | 473 | width_px = int(width_px * self.panel_stretch) |
Yu-Ping Wu | 49606eb | 2021-03-03 22:43:19 +0800 | [diff] [blame] | 474 | target = target.resize((width_px, height_px), Image.BICUBIC) |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 475 | |
| 476 | # Export and downsample color space. |
| 477 | target.convert('P', dither=None, colors=max_colors, palette=Image.ADAPTIVE |
| 478 | ).save(output) |
| 479 | |
| 480 | with open(output, 'rb+') as f: |
| 481 | f.seek(BMP_HEADER_OFFSET_NUM_LINES) |
| 482 | f.write(bytearray([num_lines])) |
| 483 | |
Yu-Ping Wu | 95493a9 | 2021-03-10 13:10:51 +0800 | [diff] [blame] | 484 | def convert(self, file, output, height, max_width, max_colors, |
Yu-Ping Wu | ed95df3 | 2020-11-04 17:08:15 +0800 | [diff] [blame] | 485 | one_line_dir=None): |
Yu-Ping Wu | f946dd4 | 2021-02-08 16:32:28 +0800 | [diff] [blame] | 486 | """Converts image `file` to bitmap format.""" |
| 487 | name, ext = os.path.splitext(os.path.basename(file)) |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 488 | |
Yu-Ping Wu | f946dd4 | 2021-02-08 16:32:28 +0800 | [diff] [blame] | 489 | background = self.BACKGROUND_COLORS.get(name, self.DEFAULT_BACKGROUND) |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 490 | |
Yu-Ping Wu | f946dd4 | 2021-02-08 16:32:28 +0800 | [diff] [blame] | 491 | # Determine num_lines in order to scale the image |
| 492 | if one_line_dir and max_width: |
| 493 | num_lines = self.get_num_lines(file, one_line_dir) |
| 494 | else: |
| 495 | num_lines = 1 |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 496 | |
Yu-Ping Wu | f946dd4 | 2021-02-08 16:32:28 +0800 | [diff] [blame] | 497 | if ext == '.svg': |
| 498 | png_file = os.path.join(self.temp_dir, name + '.png') |
| 499 | self.convert_svg_to_png(file, png_file, height, num_lines, background) |
| 500 | file = png_file |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 501 | |
Yu-Ping Wu | b87a47d | 2021-03-30 14:10:22 +0800 | [diff] [blame] | 502 | self.convert_to_bitmap(file, num_lines, background, output, max_colors) |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 503 | |
Yu-Ping Wu | 49606eb | 2021-03-03 22:43:19 +0800 | [diff] [blame] | 504 | def _bisect_dpi(self, max_dpi, initial_dpi, max_height_px, get_height): |
| 505 | """Bisects to find the DPI that produces image height `max_height_px`. |
| 506 | |
| 507 | Args: |
| 508 | max_dpi: Maximum DPI for binary search. |
| 509 | initial_dpi: Initial DPI to try with in binary search. |
| 510 | If specified, the value must be no larger than `max_dpi`. |
| 511 | max_height_px: Maximum (target) height to search for. |
| 512 | get_height: A function converting DPI to height. The function is called |
| 513 | once before returning. |
| 514 | |
| 515 | Returns: |
| 516 | The best integer DPI within [1, `max_dpi`]. |
| 517 | """ |
| 518 | |
| 519 | min_dpi = 1 |
| 520 | first_iter = True |
| 521 | |
| 522 | min_height_px = get_height(min_dpi) |
| 523 | if min_height_px > max_height_px: |
| 524 | # For some font such as "Noto Sans CJK SC", the generated height cannot |
| 525 | # go below a certain value. In this case, find max DPI with |
| 526 | # height_px <= min_height_px. |
| 527 | while min_dpi < max_dpi: |
| 528 | if first_iter and initial_dpi: |
| 529 | mid_dpi = initial_dpi |
| 530 | else: |
| 531 | mid_dpi = (min_dpi + max_dpi + 1) // 2 |
| 532 | height_px = get_height(mid_dpi) |
| 533 | if height_px > min_height_px: |
| 534 | max_dpi = mid_dpi - 1 |
| 535 | else: |
| 536 | min_dpi = mid_dpi |
| 537 | first_iter = False |
| 538 | get_height(max_dpi) |
| 539 | return max_dpi |
| 540 | |
| 541 | # Find min DPI with height_px == max_height_px |
| 542 | while min_dpi < max_dpi: |
| 543 | if first_iter and initial_dpi: |
| 544 | mid_dpi = initial_dpi |
| 545 | else: |
| 546 | mid_dpi = (min_dpi + max_dpi) // 2 |
| 547 | height_px = get_height(mid_dpi) |
| 548 | if height_px == max_height_px: |
| 549 | return mid_dpi |
| 550 | elif height_px < max_height_px: |
| 551 | min_dpi = mid_dpi + 1 |
| 552 | else: |
| 553 | max_dpi = mid_dpi |
| 554 | first_iter = False |
| 555 | get_height(min_dpi) |
| 556 | return min_dpi |
| 557 | |
Yu-Ping Wu | 95493a9 | 2021-03-10 13:10:51 +0800 | [diff] [blame] | 558 | def convert_text_to_image(self, locale, input_file, output_file, font, |
Yu-Ping Wu | 22dc45f | 2021-03-24 14:54:36 +0800 | [diff] [blame] | 559 | stage_dir, max_colors, height=None, max_width=None, |
Yu-Ping Wu | 49606eb | 2021-03-03 22:43:19 +0800 | [diff] [blame] | 560 | dpi=None, initial_dpi=None, |
Yu-Ping Wu | f946dd4 | 2021-02-08 16:32:28 +0800 | [diff] [blame] | 561 | bgcolor='#000000', fgcolor='#ffffff', |
| 562 | use_svg=False): |
| 563 | """Converts text file `input_file` into image file. |
| 564 | |
| 565 | Because pango-view does not support assigning output format options for |
| 566 | bitmap, we must create images in SVG/PNG format and then post-process them |
| 567 | (e.g. convert into BMP by ImageMagick). |
| 568 | |
| 569 | Args: |
| 570 | locale: Locale (language) to select implicit rendering options. None for |
| 571 | locale-independent strings. |
| 572 | input_file: Path of input text file. |
Yu-Ping Wu | 95493a9 | 2021-03-10 13:10:51 +0800 | [diff] [blame] | 573 | output_file: Path of output image file. |
Yu-Ping Wu | f946dd4 | 2021-02-08 16:32:28 +0800 | [diff] [blame] | 574 | font: Font name. |
| 575 | stage_dir: Directory to store intermediate file(s). |
Yu-Ping Wu | 22dc45f | 2021-03-24 14:54:36 +0800 | [diff] [blame] | 576 | max_colors: Maximum colors to convert to bitmap. |
Yu-Ping Wu | f946dd4 | 2021-02-08 16:32:28 +0800 | [diff] [blame] | 577 | height: Image height relative to the screen resolution. |
| 578 | max_width: Maximum image width relative to the screen resolution. |
| 579 | dpi: DPI value passed to pango-view. |
Yu-Ping Wu | 49606eb | 2021-03-03 22:43:19 +0800 | [diff] [blame] | 580 | initial_dpi: Initial DPI to try with in binary search. |
Yu-Ping Wu | f946dd4 | 2021-02-08 16:32:28 +0800 | [diff] [blame] | 581 | bgcolor: Background color (#rrggbb). |
| 582 | fgcolor: Foreground color (#rrggbb). |
| 583 | use_svg: If set to True, generate SVG file. Otherwise, generate PNG file. |
| 584 | |
| 585 | Returns: |
| 586 | Effective DPI, or `None` when not applicable. |
| 587 | """ |
| 588 | one_line_dir = os.path.join(stage_dir, ONE_LINE_DIR) |
| 589 | os.makedirs(one_line_dir, exist_ok=True) |
Yu-Ping Wu | 49606eb | 2021-03-03 22:43:19 +0800 | [diff] [blame] | 590 | |
Yu-Ping Wu | f946dd4 | 2021-02-08 16:32:28 +0800 | [diff] [blame] | 591 | name, _ = os.path.splitext(os.path.basename(input_file)) |
| 592 | svg_file = os.path.join(stage_dir, name + '.svg') |
| 593 | png_file = os.path.join(stage_dir, name + '.png') |
| 594 | png_file_one_line = os.path.join(one_line_dir, name + '.png') |
| 595 | |
Yu-Ping Wu | 49606eb | 2021-03-03 22:43:19 +0800 | [diff] [blame] | 596 | def get_one_line_png_height(dpi): |
| 597 | """Generates a one-line PNG using DPI `dpi` and returns its height.""" |
| 598 | run_pango_view(input_file, png_file_one_line, locale, font, height, 0, |
| 599 | dpi, bgcolor, fgcolor) |
| 600 | return self._get_png_height(png_file_one_line) |
| 601 | |
Yu-Ping Wu | f946dd4 | 2021-02-08 16:32:28 +0800 | [diff] [blame] | 602 | if use_svg: |
| 603 | run_pango_view(input_file, svg_file, locale, font, height, 0, dpi, |
| 604 | bgcolor, fgcolor, hinting='none') |
Yu-Ping Wu | b87a47d | 2021-03-30 14:10:22 +0800 | [diff] [blame] | 605 | self.convert(svg_file, output_file, height, max_width, max_colors) |
| 606 | return None |
Yu-Ping Wu | f946dd4 | 2021-02-08 16:32:28 +0800 | [diff] [blame] | 607 | else: |
Yu-Ping Wu | 49606eb | 2021-03-03 22:43:19 +0800 | [diff] [blame] | 608 | if not dpi: |
| 609 | raise BuildImageError('DPI must be specified with use_svg=False') |
| 610 | |
| 611 | eff_dpi = dpi |
Yu-Ping Wu | f946dd4 | 2021-02-08 16:32:28 +0800 | [diff] [blame] | 612 | if locale: |
Yu-Ping Wu | 49606eb | 2021-03-03 22:43:19 +0800 | [diff] [blame] | 613 | max_height_px = self._to_px(height) |
| 614 | height_px = get_one_line_png_height(dpi) |
| 615 | if height_px > max_height_px: |
| 616 | eff_dpi = self._bisect_dpi(dpi, initial_dpi, max_height_px, |
| 617 | get_one_line_png_height) |
| 618 | # NOTE: With the same DPI, the height of multi-line PNG is not necessarily |
| 619 | # a multiple of the height of one-line PNG. Therefore, even with the |
| 620 | # binary search, the height of the resulting multi-line PNG might be |
| 621 | # less than "one_line_height * num_lines". We cannot binary-search DPI |
| 622 | # for multi-line PNGs because "num_lines" is dependent on DPI. |
| 623 | run_pango_view(input_file, png_file, locale, font, height, max_width, |
| 624 | eff_dpi, bgcolor, fgcolor) |
Yu-Ping Wu | 22dc45f | 2021-03-24 14:54:36 +0800 | [diff] [blame] | 625 | self.convert(png_file, output_file, height, max_width, max_colors, |
Yu-Ping Wu | 49606eb | 2021-03-03 22:43:19 +0800 | [diff] [blame] | 626 | one_line_dir=one_line_dir if locale else None) |
| 627 | return eff_dpi |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 628 | |
Yu-Ping Wu | 177f12c | 2020-11-04 15:55:37 +0800 | [diff] [blame] | 629 | def convert_sprite_images(self): |
| 630 | """Converts sprite images.""" |
| 631 | names = self.formats[KEY_SPRITE_FILES] |
| 632 | styles = self.formats[KEY_STYLES] |
| 633 | # Check redundant images |
Yu-Ping Wu | 2091367 | 2021-03-24 15:25:10 +0800 | [diff] [blame] | 634 | for filename in glob.glob(os.path.join(self.sprite_dir, SVG_FILES)): |
Yu-Ping Wu | 177f12c | 2020-11-04 15:55:37 +0800 | [diff] [blame] | 635 | name, _ = os.path.splitext(os.path.basename(filename)) |
| 636 | if name not in names: |
| 637 | raise BuildImageError('Sprite image %r not specified in %s' % |
| 638 | (filename, FORMAT_FILE)) |
| 639 | # Convert images |
Yu-Ping Wu | 177f12c | 2020-11-04 15:55:37 +0800 | [diff] [blame] | 640 | for name, category in names.items(): |
Yu-Ping Wu | 95493a9 | 2021-03-10 13:10:51 +0800 | [diff] [blame] | 641 | new_name = self.rename_map.get(name, name) |
| 642 | if not new_name: |
| 643 | continue |
Yu-Ping Wu | 177f12c | 2020-11-04 15:55:37 +0800 | [diff] [blame] | 644 | style = get_config_with_defaults(styles, category) |
Yu-Ping Wu | 2091367 | 2021-03-24 15:25:10 +0800 | [diff] [blame] | 645 | file = os.path.join(self.sprite_dir, name + '.svg') |
Yu-Ping Wu | 95493a9 | 2021-03-10 13:10:51 +0800 | [diff] [blame] | 646 | output = os.path.join(self.output_dir, new_name + self.DEFAULT_OUTPUT_EXT) |
Yu-Ping Wu | f946dd4 | 2021-02-08 16:32:28 +0800 | [diff] [blame] | 647 | height = style[KEY_HEIGHT] |
Yu-Ping Wu | 2091367 | 2021-03-24 15:25:10 +0800 | [diff] [blame] | 648 | self.convert(file, output, height, None, self.SPRITE_MAX_COLORS) |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 649 | |
Yu-Ping Wu | 675e7e8 | 2021-01-29 08:32:12 +0800 | [diff] [blame] | 650 | def build_generic_strings(self): |
| 651 | """Builds images of generic (locale-independent) strings.""" |
Yu-Ping Wu | 60b4537 | 2021-03-31 16:56:08 +0800 | [diff] [blame] | 652 | dpi = self.config[KEY_DPI] |
Yu-Ping Wu | 675e7e8 | 2021-01-29 08:32:12 +0800 | [diff] [blame] | 653 | |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 654 | names = self.formats[KEY_GENERIC_FILES] |
| 655 | styles = self.formats[KEY_STYLES] |
Yu-Ping Wu | 675e7e8 | 2021-01-29 08:32:12 +0800 | [diff] [blame] | 656 | fonts = self.formats[KEY_FONTS] |
| 657 | default_font = fonts[KEY_DEFAULT] |
| 658 | |
Yu-Ping Wu | 675e7e8 | 2021-01-29 08:32:12 +0800 | [diff] [blame] | 659 | for txt_file in glob.glob(os.path.join(self.strings_dir, '*.txt')): |
| 660 | name, _ = os.path.splitext(os.path.basename(txt_file)) |
Yu-Ping Wu | 95493a9 | 2021-03-10 13:10:51 +0800 | [diff] [blame] | 661 | new_name = self.rename_map.get(name, name) |
| 662 | if not new_name: |
| 663 | continue |
| 664 | output_file = os.path.join(self.output_dir, |
| 665 | new_name + self.DEFAULT_OUTPUT_EXT) |
Yu-Ping Wu | 675e7e8 | 2021-01-29 08:32:12 +0800 | [diff] [blame] | 666 | category = names[name] |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 667 | style = get_config_with_defaults(styles, category) |
Yu-Ping Wu | 95493a9 | 2021-03-10 13:10:51 +0800 | [diff] [blame] | 668 | self.convert_text_to_image(None, txt_file, output_file, default_font, |
Yu-Ping Wu | 22dc45f | 2021-03-24 14:54:36 +0800 | [diff] [blame] | 669 | self.stage_dir, self.text_max_colors, |
Yu-Ping Wu | f946dd4 | 2021-02-08 16:32:28 +0800 | [diff] [blame] | 670 | height=style[KEY_HEIGHT], |
| 671 | max_width=style[KEY_MAX_WIDTH], |
| 672 | dpi=dpi, |
| 673 | bgcolor=style[KEY_BGCOLOR], |
| 674 | fgcolor=style[KEY_FGCOLOR]) |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 675 | |
Yu-Ping Wu | 49606eb | 2021-03-03 22:43:19 +0800 | [diff] [blame] | 676 | def build_locale(self, locale, names, json_dir): |
| 677 | """Builds images of strings for `locale`.""" |
Yu-Ping Wu | 60b4537 | 2021-03-31 16:56:08 +0800 | [diff] [blame] | 678 | dpi = self.config[KEY_DPI] |
Yu-Ping Wu | 49606eb | 2021-03-03 22:43:19 +0800 | [diff] [blame] | 679 | styles = self.formats[KEY_STYLES] |
| 680 | fonts = self.formats[KEY_FONTS] |
| 681 | font = fonts.get(locale, fonts[KEY_DEFAULT]) |
| 682 | inputs = parse_locale_json_file(locale, json_dir) |
| 683 | |
| 684 | # Walk locale directory to add pre-generated texts such as language names. |
| 685 | for txt_file in glob.glob(os.path.join(self.locale_dir, locale, '*.txt')): |
| 686 | name, _ = os.path.splitext(os.path.basename(txt_file)) |
| 687 | with open(txt_file, 'r', encoding='utf-8-sig') as f: |
| 688 | inputs[name] = f.read().strip() |
| 689 | |
Yu-Ping Wu | 95493a9 | 2021-03-10 13:10:51 +0800 | [diff] [blame] | 690 | stage_dir = os.path.join(self.stage_locale_dir, locale) |
| 691 | os.makedirs(stage_dir, exist_ok=True) |
| 692 | output_dir = os.path.join(self.output_ro_dir, locale) |
Yu-Ping Wu | 49606eb | 2021-03-03 22:43:19 +0800 | [diff] [blame] | 693 | os.makedirs(output_dir, exist_ok=True) |
Yu-Ping Wu | 49606eb | 2021-03-03 22:43:19 +0800 | [diff] [blame] | 694 | |
| 695 | eff_dpi_counters = defaultdict(Counter) |
| 696 | results = [] |
| 697 | for name, category in sorted(names.items()): |
| 698 | # Ignore missing translation |
| 699 | if locale != 'en' and name not in inputs: |
| 700 | continue |
| 701 | |
Yu-Ping Wu | 95493a9 | 2021-03-10 13:10:51 +0800 | [diff] [blame] | 702 | new_name = self.rename_map.get(name, name) |
| 703 | if not new_name: |
| 704 | continue |
| 705 | output_file = os.path.join(output_dir, new_name + self.DEFAULT_OUTPUT_EXT) |
| 706 | |
Yu-Ping Wu | 49606eb | 2021-03-03 22:43:19 +0800 | [diff] [blame] | 707 | # Write to text file |
Yu-Ping Wu | 95493a9 | 2021-03-10 13:10:51 +0800 | [diff] [blame] | 708 | text_file = os.path.join(stage_dir, name + '.txt') |
Yu-Ping Wu | 49606eb | 2021-03-03 22:43:19 +0800 | [diff] [blame] | 709 | with open(text_file, 'w', encoding='utf-8-sig') as f: |
| 710 | f.write(inputs[name] + '\n') |
| 711 | |
| 712 | # Convert text to image |
| 713 | style = get_config_with_defaults(styles, category) |
| 714 | height = style[KEY_HEIGHT] |
| 715 | eff_dpi_counter = eff_dpi_counters[height] |
| 716 | if eff_dpi_counter: |
| 717 | # Find the effective DPI that appears most times for `height`. This |
| 718 | # avoid doing the same binary search again and again. In case of a tie, |
| 719 | # pick the largest DPI. |
| 720 | best_eff_dpi = max(eff_dpi_counter, |
| 721 | key=lambda dpi: (eff_dpi_counter[dpi], dpi)) |
| 722 | else: |
| 723 | best_eff_dpi = None |
| 724 | eff_dpi = self.convert_text_to_image(locale, |
| 725 | text_file, |
Yu-Ping Wu | 95493a9 | 2021-03-10 13:10:51 +0800 | [diff] [blame] | 726 | output_file, |
Yu-Ping Wu | 49606eb | 2021-03-03 22:43:19 +0800 | [diff] [blame] | 727 | font, |
Yu-Ping Wu | 95493a9 | 2021-03-10 13:10:51 +0800 | [diff] [blame] | 728 | stage_dir, |
Yu-Ping Wu | 22dc45f | 2021-03-24 14:54:36 +0800 | [diff] [blame] | 729 | self.text_max_colors, |
Yu-Ping Wu | 49606eb | 2021-03-03 22:43:19 +0800 | [diff] [blame] | 730 | height=height, |
| 731 | max_width=style[KEY_MAX_WIDTH], |
| 732 | dpi=dpi, |
| 733 | initial_dpi=best_eff_dpi, |
| 734 | bgcolor=style[KEY_BGCOLOR], |
| 735 | fgcolor=style[KEY_FGCOLOR]) |
| 736 | eff_dpi_counter[eff_dpi] += 1 |
| 737 | assert eff_dpi <= dpi |
| 738 | if eff_dpi != dpi: |
| 739 | results.append(eff_dpi) |
| 740 | return results |
| 741 | |
Yu-Ping Wu | 2e788b0 | 2021-03-09 13:01:31 +0800 | [diff] [blame] | 742 | def _check_text_width(self, names): |
| 743 | """Checks if text image will exceed the expected drawing area at runtime.""" |
| 744 | styles = self.formats[KEY_STYLES] |
| 745 | |
| 746 | for locale_info in self.locales: |
| 747 | locale = locale_info.code |
| 748 | ro_locale_dir = os.path.join(self.output_ro_dir, locale) |
| 749 | for filename in glob.glob(os.path.join(ro_locale_dir, |
| 750 | '*' + self.DEFAULT_OUTPUT_EXT)): |
| 751 | name, _ = os.path.splitext(os.path.basename(filename)) |
| 752 | category = names[name] |
| 753 | style = get_config_with_defaults(styles, category) |
| 754 | height = style[KEY_HEIGHT] |
| 755 | max_width = style[KEY_MAX_WIDTH] |
| 756 | if not max_width: |
| 757 | continue |
| 758 | max_width_px = self._to_px(max_width) |
| 759 | with open(filename, 'rb') as f: |
| 760 | f.seek(BMP_HEADER_OFFSET_NUM_LINES) |
| 761 | num_lines = f.read(1)[0] |
| 762 | height_px = self._to_px(height * num_lines) |
| 763 | with Image.open(filename) as image: |
| 764 | width_px = height_px * image.size[0] // image.size[1] |
| 765 | if width_px > max_width_px: |
| 766 | raise BuildImageError('%s: Image width %dpx greater than max width ' |
| 767 | '%dpx' % (filename, width_px, max_width_px)) |
Yu-Ping Wu | 08defcc | 2020-05-07 16:21:03 +0800 | [diff] [blame] | 768 | |
Yu-Ping Wu | 703dcfd | 2021-01-08 10:52:10 +0800 | [diff] [blame] | 769 | def _copy_missing_bitmaps(self): |
| 770 | """Copy missing (not yet translated) strings from locale 'en'.""" |
| 771 | en_files = glob.glob(os.path.join(self.output_ro_dir, 'en', |
| 772 | '*' + self.DEFAULT_OUTPUT_EXT)) |
| 773 | for locale_info in self.locales: |
| 774 | locale = locale_info.code |
| 775 | if locale == 'en': |
| 776 | continue |
| 777 | ro_locale_dir = os.path.join(self.output_ro_dir, locale) |
| 778 | for en_file in en_files: |
| 779 | filename = os.path.basename(en_file) |
| 780 | locale_file = os.path.join(ro_locale_dir, filename) |
| 781 | if not os.path.isfile(locale_file): |
| 782 | print("WARNING: Locale '%s': copying '%s'" % (locale, filename)) |
| 783 | shutil.copyfile(en_file, locale_file) |
| 784 | |
Yu-Ping Wu | f946dd4 | 2021-02-08 16:32:28 +0800 | [diff] [blame] | 785 | def build_localized_strings(self): |
| 786 | """Builds images of localized strings.""" |
| 787 | # Sources are one .grd file with identifiers chosen by engineers and |
| 788 | # corresponding English texts, as well as a set of .xtb files (one for each |
| 789 | # language other than US English) with a mapping from hash to translation. |
| 790 | # Because the keys in the .xtb files are a hash of the English source text, |
| 791 | # rather than our identifiers, such as "btn_cancel", we use the "grit" |
| 792 | # command line tool to process the .grd and .xtb files, producing a set of |
| 793 | # .json files mapping our identifier to the translated string, one for every |
| 794 | # language including US English. |
| 795 | |
| 796 | # Create a temporary directory to place the translation output from grit in. |
| 797 | json_dir = tempfile.mkdtemp() |
| 798 | |
| 799 | # This invokes the grit build command to generate JSON files from the XTB |
| 800 | # files containing translations. The results are placed in `json_dir` as |
| 801 | # specified in firmware_strings.grd, i.e. one JSON file per locale. |
| 802 | subprocess.check_call([ |
| 803 | 'grit', |
| 804 | '-i', os.path.join(self.locale_dir, STRINGS_GRD_FILE), |
| 805 | 'build', |
| 806 | '-o', os.path.join(json_dir), |
| 807 | ]) |
| 808 | |
| 809 | # Make a copy to avoid modifying `self.formats` |
| 810 | names = copy.deepcopy(self.formats[KEY_LOCALIZED_FILES]) |
| 811 | if DIAGNOSTIC_UI: |
| 812 | names.update(self.formats[KEY_DIAGNOSTIC_FILES]) |
| 813 | |
Yu-Ping Wu | 675e7e8 | 2021-01-29 08:32:12 +0800 | [diff] [blame] | 814 | # Ignore SIGINT in child processes |
| 815 | sigint_handler = signal.signal(signal.SIGINT, signal.SIG_IGN) |
| 816 | pool = multiprocessing.Pool(multiprocessing.cpu_count()) |
| 817 | signal.signal(signal.SIGINT, sigint_handler) |
| 818 | |
| 819 | results = [] |
| 820 | for locale_info in self.locales: |
| 821 | locale = locale_info.code |
| 822 | print(locale, end=' ', flush=True) |
Yu-Ping Wu | 49606eb | 2021-03-03 22:43:19 +0800 | [diff] [blame] | 823 | args = ( |
| 824 | locale, |
| 825 | names, |
| 826 | json_dir, |
| 827 | ) |
| 828 | results.append(pool.apply_async(self.build_locale, args)) |
Yu-Ping Wu | 675e7e8 | 2021-01-29 08:32:12 +0800 | [diff] [blame] | 829 | |
| 830 | print() |
| 831 | pool.close() |
| 832 | |
| 833 | try: |
Yu-Ping Wu | 49606eb | 2021-03-03 22:43:19 +0800 | [diff] [blame] | 834 | results = [r.get() for r in results] |
Yu-Ping Wu | 675e7e8 | 2021-01-29 08:32:12 +0800 | [diff] [blame] | 835 | except KeyboardInterrupt: |
| 836 | pool.terminate() |
| 837 | pool.join() |
| 838 | exit('Aborted by user') |
| 839 | else: |
| 840 | pool.join() |
| 841 | |
Yu-Ping Wu | 49606eb | 2021-03-03 22:43:19 +0800 | [diff] [blame] | 842 | effective_dpi = [dpi for r in results for dpi in r if dpi] |
Yu-Ping Wu | f946dd4 | 2021-02-08 16:32:28 +0800 | [diff] [blame] | 843 | if effective_dpi: |
| 844 | print('Reducing effective DPI to %d, limited by screen resolution' % |
| 845 | max(effective_dpi)) |
Yu-Ping Wu | 675e7e8 | 2021-01-29 08:32:12 +0800 | [diff] [blame] | 846 | |
Yu-Ping Wu | 675e7e8 | 2021-01-29 08:32:12 +0800 | [diff] [blame] | 847 | shutil.rmtree(json_dir) |
Yu-Ping Wu | 2e788b0 | 2021-03-09 13:01:31 +0800 | [diff] [blame] | 848 | self._check_text_width(names) |
Yu-Ping Wu | 703dcfd | 2021-01-08 10:52:10 +0800 | [diff] [blame] | 849 | self._copy_missing_bitmaps() |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 850 | |
| 851 | def move_language_images(self): |
| 852 | """Renames language bitmaps and move to self.output_dir. |
| 853 | |
| 854 | The directory self.output_dir contains locale-independent images, and is |
| 855 | used for creating vbgfx.bin by archive_images.py. |
| 856 | """ |
| 857 | for locale_info in self.locales: |
| 858 | locale = locale_info.code |
| 859 | ro_locale_dir = os.path.join(self.output_ro_dir, locale) |
| 860 | old_file = os.path.join(ro_locale_dir, 'language.bmp') |
| 861 | new_file = os.path.join(self.output_dir, 'language_%s.bmp' % locale) |
| 862 | if os.path.exists(new_file): |
| 863 | raise BuildImageError('File already exists: %s' % new_file) |
| 864 | shutil.move(old_file, new_file) |
| 865 | |
Yu-Ping Wu | 675e7e8 | 2021-01-29 08:32:12 +0800 | [diff] [blame] | 866 | def build_glyphs(self): |
| 867 | """Builds glyphs of ascii characters.""" |
Yu-Ping Wu | 31a6e6b | 2021-03-24 15:08:53 +0800 | [diff] [blame] | 868 | os.makedirs(self.stage_glyph_dir, exist_ok=True) |
| 869 | output_dir = os.path.join(self.output_dir, 'glyph') |
| 870 | os.makedirs(output_dir) |
Yu-Ping Wu | 675e7e8 | 2021-01-29 08:32:12 +0800 | [diff] [blame] | 871 | # TODO(b/163109632): Parallelize the conversion of glyphs |
| 872 | for c in range(ord(' '), ord('~') + 1): |
| 873 | name = f'idx{c:03d}_{c:02x}' |
Yu-Ping Wu | 31a6e6b | 2021-03-24 15:08:53 +0800 | [diff] [blame] | 874 | txt_file = os.path.join(self.stage_glyph_dir, name + '.txt') |
Yu-Ping Wu | 675e7e8 | 2021-01-29 08:32:12 +0800 | [diff] [blame] | 875 | with open(txt_file, 'w', encoding='ascii') as f: |
| 876 | f.write(chr(c)) |
| 877 | f.write('\n') |
Yu-Ping Wu | 31a6e6b | 2021-03-24 15:08:53 +0800 | [diff] [blame] | 878 | output_file = os.path.join(output_dir, name + self.DEFAULT_OUTPUT_EXT) |
Yu-Ping Wu | 95493a9 | 2021-03-10 13:10:51 +0800 | [diff] [blame] | 879 | self.convert_text_to_image(None, txt_file, output_file, GLYPH_FONT, |
Yu-Ping Wu | 31a6e6b | 2021-03-24 15:08:53 +0800 | [diff] [blame] | 880 | self.stage_glyph_dir, self.GLYPH_MAX_COLORS, |
Yu-Ping Wu | f946dd4 | 2021-02-08 16:32:28 +0800 | [diff] [blame] | 881 | height=DEFAULT_GLYPH_HEIGHT, |
| 882 | use_svg=True) |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 883 | |
| 884 | def copy_images_to_rw(self): |
| 885 | """Copies localized images specified in boards.yaml for RW override.""" |
Yu-Ping Wu | 60b4537 | 2021-03-31 16:56:08 +0800 | [diff] [blame] | 886 | if not self.config[KEY_RW_OVERRIDE]: |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 887 | print(' No localized images are specified for RW, skipping') |
| 888 | return |
| 889 | |
| 890 | for locale_info in self.locales: |
| 891 | locale = locale_info.code |
Chung-Sheng Wu | cd3b4e2 | 2021-04-01 18:50:20 +0800 | [diff] [blame] | 892 | ro_locale_dir = os.path.join(self.output_ro_dir, locale) |
| 893 | rw_locale_dir = os.path.join(self.output_rw_dir, locale) |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 894 | os.makedirs(rw_locale_dir) |
| 895 | |
Yu-Ping Wu | 60b4537 | 2021-03-31 16:56:08 +0800 | [diff] [blame] | 896 | for name in self.config[KEY_RW_OVERRIDE]: |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 897 | ro_src = os.path.join(ro_locale_dir, name + self.DEFAULT_OUTPUT_EXT) |
| 898 | rw_dst = os.path.join(rw_locale_dir, name + self.DEFAULT_OUTPUT_EXT) |
| 899 | shutil.copyfile(ro_src, rw_dst) |
| 900 | |
| 901 | def create_locale_list(self): |
| 902 | """Creates locale list as a CSV file. |
| 903 | |
| 904 | Each line in the file is of format "code,rtl", where |
| 905 | - "code": language code of the locale |
| 906 | - "rtl": "1" for right-to-left language, "0" otherwise |
| 907 | """ |
| 908 | with open(os.path.join(self.output_dir, 'locales'), 'w') as f: |
| 909 | for locale_info in self.locales: |
| 910 | f.write('{},{}\n'.format(locale_info.code, |
| 911 | int(locale_info.rtl))) |
| 912 | |
| 913 | def build(self): |
| 914 | """Builds all images required by a board.""" |
| 915 | # Clean up output directory |
| 916 | if os.path.exists(self.output_dir): |
| 917 | shutil.rmtree(self.output_dir) |
| 918 | os.makedirs(self.output_dir) |
| 919 | |
| 920 | if not os.path.exists(self.stage_dir): |
| 921 | raise BuildImageError('Missing stage folder. Run make in strings dir.') |
| 922 | |
| 923 | # Clean up temp directory |
| 924 | if os.path.exists(self.temp_dir): |
| 925 | shutil.rmtree(self.temp_dir) |
| 926 | os.makedirs(self.temp_dir) |
| 927 | |
Yu-Ping Wu | 177f12c | 2020-11-04 15:55:37 +0800 | [diff] [blame] | 928 | print('Converting sprite images...') |
| 929 | self.convert_sprite_images() |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 930 | |
Yu-Ping Wu | 675e7e8 | 2021-01-29 08:32:12 +0800 | [diff] [blame] | 931 | print('Building generic strings...') |
| 932 | self.build_generic_strings() |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 933 | |
Yu-Ping Wu | 675e7e8 | 2021-01-29 08:32:12 +0800 | [diff] [blame] | 934 | print('Building localized strings...') |
| 935 | self.build_localized_strings() |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 936 | |
| 937 | print('Moving language images to locale-independent directory...') |
| 938 | self.move_language_images() |
| 939 | |
| 940 | print('Creating locale list file...') |
| 941 | self.create_locale_list() |
| 942 | |
Yu-Ping Wu | 675e7e8 | 2021-01-29 08:32:12 +0800 | [diff] [blame] | 943 | print('Building glyphs...') |
| 944 | self.build_glyphs() |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 945 | |
| 946 | print('Copying specified images to RW packing directory...') |
| 947 | self.copy_images_to_rw() |
| 948 | |
| 949 | |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 950 | def main(): |
| 951 | """Builds bitmaps for firmware screens.""" |
| 952 | parser = argparse.ArgumentParser() |
| 953 | parser.add_argument('board', help='Target board') |
| 954 | args = parser.parse_args() |
Yu-Ping Wu | e66a7b0 | 2020-11-19 15:18:08 +0800 | [diff] [blame] | 955 | board = args.board |
Yu-Ping Wu | 8c8bfc7 | 2020-10-27 16:19:34 +0800 | [diff] [blame] | 956 | |
| 957 | with open(FORMAT_FILE, encoding='utf-8') as f: |
| 958 | formats = yaml.load(f) |
Yu-Ping Wu | e66a7b0 | 2020-11-19 15:18:08 +0800 | [diff] [blame] | 959 | board_config = load_boards_config(BOARDS_CONFIG_FILE)[board] |
| 960 | |
Yu-Ping Wu | e66a7b0 | 2020-11-19 15:18:08 +0800 | [diff] [blame] | 961 | print('Building for ' + board) |
Yu-Ping Wu | 675e7e8 | 2021-01-29 08:32:12 +0800 | [diff] [blame] | 962 | check_fonts(formats[KEY_FONTS]) |
| 963 | print('Output dir: ' + OUTPUT_DIR) |
Yu-Ping Wu | e66a7b0 | 2020-11-19 15:18:08 +0800 | [diff] [blame] | 964 | converter = Converter(board, formats, board_config, OUTPUT_DIR) |
| 965 | converter.build() |
Yu-Ping Wu | 7f6639a | 2020-09-28 15:31:35 +0800 | [diff] [blame] | 966 | |
| 967 | |
Hung-Te Lin | 707e2ef | 2013-08-06 10:20:04 +0800 | [diff] [blame] | 968 | if __name__ == '__main__': |
Yu-Ping Wu | 6e4d389 | 2020-10-19 14:09:37 +0800 | [diff] [blame] | 969 | main() |