perf: font-compression improvements (#1056)
## Purpose This PR includes some preparatory changes that are needed for an upcoming performant CJK font feature. The changes have no impact on render time and heap allocation for latin text. **Despite this, I think these changes stand on their own as a better font compression/decompression implementation.** ## Summary - Font decompressor rewrite: Replaced the 4-slot LRU group cache with a two-tier system — a page buffer (glyphs prewarmed before rendering begins) and a hot-group fallback (last decompressed group retained for non-prewarmed glyphs). - Byte-aligned compressed bitmap format: Glyph bitmaps within compressed groups are now stored row-padded rather than tightly packed before DEFLATE compression, improving compression ratios by making identical pixel rows produce identical byte patterns. Glyphs are compacted back to packed format on demand at render time. Reduces flash size by 155 KB. - Page prewarm system: Added `Page::collectText` and `Page::getDominantStyle` to extract per-style glyph requirements before rendering, and `GfxRenderer::prewarmFontCache` to pre-decompress only the groups needed for the dominant style — eliminating mid-render decompression for the common case. - UTF-8 robustness fixes: `utf8NextCodepoint` now validates continuation bytes and returns a replacement glyph on malformed input; `ChapterHtmlSlimParser` correctly preserves incomplete multi-byte sequences across word-buffer flush boundaries rather than splitting them. --- ### AI Usage While CrossPoint doesn't have restrictions on AI tools in contributing, please be transparent about their usage as it helps set the right context for reviewers. Did you use AI tools to help write this code? _**YES**_ Architecture and design was done by me, refined a bit by Claude. Code mostly by Claude, but not entirely.
This commit is contained in:
committed by
GitHub
parent
b467ea7973
commit
f1e9dc7f30
@@ -689,7 +689,38 @@ print(f"ligatures: {len(ligature_pairs)} pairs extracted", file=sys.stderr)
|
||||
|
||||
compress = args.compress
|
||||
|
||||
|
||||
def to_byte_aligned(packed, width, height):
|
||||
"""Convert packed 2-bit bitmap to byte-aligned format (rows padded to byte boundary).
|
||||
|
||||
In packed format, pixels flow continuously across row boundaries (4 pixels/byte).
|
||||
In byte-aligned format, each row starts at a byte boundary, padding the last byte
|
||||
of each row with zero bits if width % 4 != 0. This improves DEFLATE compression
|
||||
because identical pixel rows produce identical byte patterns regardless of position.
|
||||
"""
|
||||
if width == 0 or height == 0:
|
||||
return b''
|
||||
row_stride = (width + 3) // 4 # bytes per byte-aligned row
|
||||
aligned = bytearray(row_stride * height)
|
||||
for y in range(height):
|
||||
for x in range(width):
|
||||
# Read pixel from packed format (continuous bit stream)
|
||||
packed_pos = y * width + x
|
||||
packed_byte_idx = packed_pos // 4
|
||||
packed_shift = (3 - (packed_pos % 4)) * 2
|
||||
pixel = (packed[packed_byte_idx] >> packed_shift) & 0x3
|
||||
|
||||
# Write pixel to byte-aligned format (row-aligned)
|
||||
aligned_byte_idx = y * row_stride + x // 4
|
||||
aligned_shift = (3 - (x % 4)) * 2
|
||||
aligned[aligned_byte_idx] |= (pixel << aligned_shift)
|
||||
return bytes(aligned)
|
||||
|
||||
|
||||
# Build groups for compression
|
||||
if compress and not is2Bit:
|
||||
print("Error: --compress requires --2bit (byte-aligned compression only supports 2-bit format)", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
if compress:
|
||||
# Script-based grouping: glyphs that co-occur in typical text rendering
|
||||
# are grouped together for efficient LRU caching on the embedded target.
|
||||
@@ -747,11 +778,12 @@ if compress:
|
||||
|
||||
for first_idx, count in groups:
|
||||
# Concatenate bitmap data for this group
|
||||
group_data = b''
|
||||
packed_len = 0
|
||||
group_aligned = bytearray()
|
||||
for gi in range(first_idx, first_idx + count):
|
||||
props, packed = all_glyphs[gi]
|
||||
# Update glyph's dataOffset to be within-group offset
|
||||
within_group_offset = len(group_data)
|
||||
# Update glyph's dataOffset to be within-group offset (packed offset)
|
||||
within_group_offset = packed_len
|
||||
old_props = modified_glyph_props[gi]
|
||||
modified_glyph_props[gi] = GlyphProps(
|
||||
width=old_props.width,
|
||||
@@ -763,13 +795,14 @@ if compress:
|
||||
data_offset=within_group_offset,
|
||||
code_point=old_props.code_point,
|
||||
)
|
||||
group_data += packed
|
||||
packed_len += len(packed)
|
||||
group_aligned.extend(to_byte_aligned(packed, old_props.width, old_props.height))
|
||||
|
||||
# Compress with raw DEFLATE (no zlib/gzip header)
|
||||
# Compress byte-aligned data with raw DEFLATE (no zlib/gzip header)
|
||||
compressor = zlib.compressobj(level=9, wbits=-15)
|
||||
compressed = compressor.compress(group_data) + compressor.flush()
|
||||
compressed = compressor.compress(bytes(group_aligned)) + compressor.flush()
|
||||
|
||||
compressed_groups.append((compressed, len(group_data), count, first_idx))
|
||||
compressed_groups.append((compressed, len(group_aligned), count, first_idx))
|
||||
compressed_bitmap_data.extend(compressed)
|
||||
compressed_offset += len(compressed)
|
||||
|
||||
@@ -862,8 +895,10 @@ if compress:
|
||||
print(f" {font_name}Groups,")
|
||||
print(f" {len(compressed_groups)},")
|
||||
else:
|
||||
print(f" nullptr,")
|
||||
print(f" 0,")
|
||||
print(" nullptr,")
|
||||
print(" 0,")
|
||||
# glyphToGroup (not used for script-grouped fonts)
|
||||
print(" nullptr,")
|
||||
if kern_map:
|
||||
print(f" {font_name}KernLeftClasses,")
|
||||
print(f" {font_name}KernRightClasses,")
|
||||
|
||||
Reference in New Issue
Block a user