"""A painted tile's words as its painter set them, for the tile import scripts. from tile_words import words_of words = words_of(layers) # None where the layout names no type`layout.json` beside the layers says how the phone sets the tile's live words(`type`): their colour, the name's first baseline and the count's under aname of one line and of two, in pixels on the 474 x 696 stock. The tilecarries them as `fan_tile.words` (`crates/cotton-core/src/definitions/fan_tile.rs`, `TileWords`), so a re-import never loses them; a key the layoutdoes not name is left out, and the phone keeps the type's own."""import jsonimport pathlibimport sysdef words_of(layers): path = pathlib.Path(layers) / 'layout.json'ifnot path.exists():returnNone kind = json.loads(path.read_text()).get('type')ifnot isinstance(kind, dict):returnNone name, count = kind.get('name') or {}, kind.get('count') or {} named = {'ink': kind.get('colour'),'name_baseline': name.get('baseline'),'count_baseline_one': count.get('baselineForOneLine'),'count_baseline_two': count.get('baselineForTwoLines'), } words = {key: value for key, value in named.items() if value isnotNone}for key, value in words.items():if key == 'ink':ifnot (isinstance(value, str) and len(value) == 7and value.startswith('#') and all(c in'0123456789abcdefABCDEF'for c in value[1:])): sys.exit(f'{path}: type.colour {value!r} is no #rrggbb') words[key] = value.lower()elifnot (isinstance(value, int) and0 < value <= 696): sys.exit(f'{path}: {key} {value!r} is no pixel row of the 696-high stock')# In the order the core writes them (`TileWords`'s field order).return {key: words[key] for key in named if key in words} orNone