|
28 | 28 | ) |
29 | 29 | CONSTANT_RE = re.compile(r"^\s*public static final char MATERIAL_") |
30 | 30 | SENTINEL = " private static Font materialDesignFont;" |
| 31 | +MAX_JAVA_CHAR_CODEPOINT = 0xFFFF |
31 | 32 |
|
32 | 33 |
|
33 | 34 | def download_text(url: str) -> str: |
@@ -61,13 +62,28 @@ def parse_codepoints(codepoints_text: str) -> list[tuple[str, str]]: |
61 | 62 | return out |
62 | 63 |
|
63 | 64 |
|
64 | | -def generate_constants_block(entries: list[tuple[str, str]]) -> str: |
| 65 | +def java_char_literal(codepoint: str) -> str | None: |
| 66 | + value = int(codepoint, 16) |
| 67 | + if value > MAX_JAVA_CHAR_CODEPOINT: |
| 68 | + return None |
| 69 | + return f"\\u{value:04X}" |
| 70 | + |
| 71 | + |
| 72 | +def generate_constants_block(entries: list[tuple[str, str]]) -> tuple[str, list[tuple[str, str]]]: |
65 | 73 | lines: list[str] = [] |
| 74 | + skipped: list[tuple[str, str]] = [] |
66 | 75 | for icon_name, codepoint in entries: |
67 | 76 | const_name = material_constant_name(icon_name) |
| 77 | + char_literal = java_char_literal(codepoint) |
| 78 | + if char_literal is None: |
| 79 | + skipped.append((const_name, codepoint.upper())) |
| 80 | + lines.append( |
| 81 | + f" // {const_name} omitted: U+{codepoint.upper()} is outside the Java char range." |
| 82 | + ) |
| 83 | + continue |
68 | 84 | lines.append(CONSTANT_DOC.rstrip("\n")) |
69 | | - lines.append(f" public static final char {const_name} = '\\u{codepoint.upper()}';") |
70 | | - return "\n".join(lines) + "\n" |
| 85 | + lines.append(f" public static final char {const_name} = '{char_literal}';") |
| 86 | + return "\n".join(lines) + "\n", skipped |
71 | 87 |
|
72 | 88 |
|
73 | 89 | def is_material_constant_doc_line(line: str) -> bool: |
@@ -126,7 +142,15 @@ def main() -> int: |
126 | 142 | remote_font = download_bytes(MATERIAL_FONT_URL) |
127 | 143 | codepoints = download_text(MATERIAL_CODEPOINTS_URL) |
128 | 144 | entries = parse_codepoints(codepoints) |
129 | | - constants_block = generate_constants_block(entries) |
| 145 | + constants_block, skipped = generate_constants_block(entries) |
| 146 | + if skipped: |
| 147 | + skipped_names = ", ".join( |
| 148 | + f"{const_name} (U+{codepoint})" for const_name, codepoint in skipped |
| 149 | + ) |
| 150 | + print( |
| 151 | + "Skipping Material icons that cannot be represented as Java char constants: " |
| 152 | + f"{skipped_names}" |
| 153 | + ) |
130 | 154 |
|
131 | 155 | font_changed = update_font(remote_font, check_only=args.check) |
132 | 156 | constants_changed = update_fontimage(constants_block, check_only=args.check) |
|
0 commit comments