This commit is contained in:
Peterino2 2026-09-22 12:27:51 -07:00
parent ce76333d32
commit 9fa0a88e43
5 changed files with 37 additions and 53 deletions

View File

@ -459,8 +459,9 @@ UHDK_API const uhdk_gl_functions *UHDK_CALL uhdk_get_gl(uhdk_session *session);
UHDK_API uhdk_window *UHDK_CALL uhdk_get_window(uhdk_session *session); UHDK_API uhdk_window *UHDK_CALL uhdk_get_window(uhdk_session *session);
/* NULL safe. Discards an unfinished frame without presenting. Destroy app-owned /* NULL safe. Valid after the session stops or enters a terminal error state, and
* GL resources before close; all session handles become invalid on return. */ * discards an unfinished frame without presenting. Destroy app-owned GL resources
* before close; all session handles become invalid on return. */
UHDK_API void UHDK_CALL uhdk_close(uhdk_session *session); UHDK_API void UHDK_CALL uhdk_close(uhdk_session *session);
/* Synchronously create an additional native window for an external renderer. /* Synchronously create an additional native window for an external renderer.

View File

@ -245,7 +245,6 @@ pub export fn uhdk_get_window(session: ?*c.uhdk_session) ?*c.uhdk_window {
pub export fn uhdk_close(session: ?*c.uhdk_session) void { pub export fn uhdk_close(session: ?*c.uhdk_session) void {
const a = state(session orelse return); const a = state(session orelse return);
std.debug.assert(runtime.validateMainThreadSessionReady(a) == abi.OK);
_ = release(a); _ = release(a);
app.destroy(handle(a)); app.destroy(handle(a));
} }

View File

@ -13,21 +13,22 @@ pub fn queueCreate(
if (out == null) return abi.INVALID; if (out == null) return abi.INVALID;
const d = desc.*; const d = desc.*;
if (!validStyle(&d.style)
or !abi.validText(d.title, true) if (d.content_size.width == 0 or d.content_size.height == 0)
or !abi.finiteSize(d.content_size) return abi.INVALID;
or d.content_size.width == 0
or d.content_size.height == 0 if (!abi.finiteSize(d.content_size) or !abi.finiteSize(d.content_size) or !abi.finiteSize(d.min_size) or !abi.finiteSize(d.max_size))
or !abi.finiteSize(d.min_size) return abi.INVALID;
or !abi.finiteSize(d.max_size)
or d.flags & ~@as(u32, c.Z_WINDOW_OPENGL) != 0) return abi.INVALID; if (!validStyle(&d.style) or d.content_size.width == 0 or d.content_size.height == 0 or d.flags & ~@as(u32, c.Z_WINDOW_OPENGL) != 0)
if ((d.max_size.width != 0 return abi.INVALID;
and d.max_size.width < d.min_size.width)
or (d.max_size.height != 0 if ((d.max_size.width != 0 and d.max_size.width < d.min_size.width) or (d.max_size.height != 0 and d.max_size.height < d.min_size.height))
and d.max_size.height < d.min_size.height)) return abi.INVALID; return abi.INVALID;
if (d.parent != null
and (runtime.fromObjectHandle(d.parent).app != runtime.fromAppHandle(handle) if (d.parent != null and (runtime.fromObjectHandle(d.parent).app != runtime.fromAppHandle(handle) or !runtime.fromObjectHandle(d.parent).alive))
or !runtime.fromObjectHandle(d.parent).alive)) return abi.INVALID; return abi.INVALID;
const command = runtime.createAppCommand(handle, c.Z_WINDOW) catch |err| return runtime.applicationError(err); const command = runtime.createAppCommand(handle, c.Z_WINDOW) catch |err| return runtime.applicationError(err);
const object = runtime.createObject(runtime.fromAppHandle(handle), c.Z_KIND_WINDOW) catch { const object = runtime.createObject(runtime.fromAppHandle(handle), c.Z_KIND_WINDOW) catch {
runtime.abandon(command); runtime.abandon(command);

View File

@ -14,6 +14,8 @@ The source is the unmodified Google Fonts Roboto variable TTF, pinned in
`manifest.json`, with weight 400 and width 100. The generated bitmap is a font `manifest.json`, with weight 400 and width 100. The generated bitmap is a font
derivative distributed under the accompanying SIL Open Font License (`OFL.txt`). derivative distributed under the accompanying SIL Open Font License (`OFL.txt`).
The TTF is downloaded only for maintainer regeneration and is not shipped here. The TTF is downloaded only for maintainer regeneration and is not shipped here.
The Python generator rasterizes the TTF and writes the packed `.fontatlas`
directly, with no intermediate file format or separate packing tool.
Binary format, little-endian: `FAP1`, u16 glyph count, u16 fallback glyph index, Binary format, little-endian: `FAP1`, u16 glyph count, u16 fallback glyph index,
then sorted 12-byte records (u16 codepoint, u16 absolute data offset, u16 data then sorted 12-byte records (u16 codepoint, u16 absolute data offset, u16 data

View File

@ -33,9 +33,9 @@ def generate(manifest, font_path):
axes = font.get_variation_axes() axes = font.get_variation_axes()
font.set_variation_by_axes([400 if a['name'] == b'Weight' else 100 if a['name'] == b'Width' else a['default'] for a in axes]) font.set_variation_by_axes([400 if a['name'] == b'Weight' else 100 if a['name'] == b'Width' else a['default'] for a in axes])
codepoints = list(range(32, 127)) codepoints = list(range(32, 127))
header_size = 8 + len(codepoints) * 16 records = bytearray(12 * len(codepoints))
records, pixels = bytearray(), bytearray() streams = bytearray()
for cp in codepoints: for index, cp in enumerate(codepoints):
character = chr(cp) character = chr(cp)
x, y, right, bottom = font.getbbox(character, anchor='ls') x, y, right, bottom = font.getbbox(character, anchor='ls')
width, height = right - x, bottom - y width, height = right - x, bottom - y
@ -43,9 +43,18 @@ def generate(manifest, font_path):
if width and height: if width and height:
ImageDraw.Draw(image).text((-x, -y), character, font=font, fill=255, anchor='ls') ImageDraw.Draw(image).text((-x, -y), character, font=font, fill=255, anchor='ls')
advance = int(font.getlength(character) + .5) advance = int(font.getlength(character) + .5)
records += struct.pack('<IIBBbbB3x', cp, header_size + len(pixels), width, height, x, y, advance) samples = [(value + 8) // 17 for value in image.tobytes()]
pixels += image.tobytes() samples += [0] * (len(samples) % 2)
return b'UF16' + struct.pack('<I', len(codepoints)) + records + pixels nibbles = bytes(samples[i] | samples[i + 1] << 4 for i in range(0, len(samples), 2))
encoded = zero_rle(nibbles)
offset = 8 + len(records) + len(streams)
assert offset <= 0xffff and len(encoded) <= 0xffff
struct.pack_into('<HHHBBbbBB', records, index * 12, cp, offset, len(encoded), width, height, x, y, advance, 0)
streams += encoded
fallback = codepoints.index(ord('?'))
result = b'FAP1' + struct.pack('<HH', len(codepoints), fallback) + records + streams
assert len(result) <= 0xffff
return result
def zero_rle(data): def zero_rle(data):
@ -63,34 +72,6 @@ def zero_rle(data):
return result return result
def pack(data):
assert data[:4] == b'UF16'
count, = struct.unpack_from('<I', data, 4)
records = bytearray(12 * count)
streams = bytearray()
fallback = None
for index in range(count):
cp, offset, width, height, x, y, advance = struct.unpack_from('<IIBBbbB', data, 8 + index * 16)
assert cp <= 0xffff
pixels = data[offset:offset + width * height]
samples = [(value + 8) // 17 for value in pixels]
samples += [0] * (len(samples) % 2)
nibbles = bytes(samples[i] | samples[i + 1] << 4 for i in range(0, len(samples), 2))
encoded = zero_rle(nibbles)
packed_offset = 8 + len(records) + len(streams)
assert packed_offset <= 0xffff and len(encoded) <= 0xffff
struct.pack_into('<HHHBBbbBB', records, index * 12, cp, packed_offset, len(encoded), width, height, x, y, advance, 0)
streams += encoded
if cp == ord('?') and fallback is None:
fallback = index
if cp == 0xfffd:
fallback = index
assert fallback is not None
result = b'FAP1' + struct.pack('<HH', count, fallback) + records + streams
assert len(result) <= 0xffff
return result
def main(): def main():
parser = argparse.ArgumentParser(description=__doc__) parser = argparse.ArgumentParser(description=__doc__)
mode = parser.add_mutually_exclusive_group(required=True) mode = parser.add_mutually_exclusive_group(required=True)
@ -101,7 +82,7 @@ def main():
args = parser.parse_args() args = parser.parse_args()
manifest = json.loads((ROOT / 'manifest.json').read_text()) manifest = json.loads((ROOT / 'manifest.json').read_text())
if args.regenerate or args.verify_generation: if args.regenerate or args.verify_generation:
data = pack(generate(manifest, args.font)) data = generate(manifest, args.font)
if args.verify_generation: if args.verify_generation:
assert data == (ROOT / 'roboto-16.fontatlas').read_bytes(), 'Generated bitmap differs' assert data == (ROOT / 'roboto-16.fontatlas').read_bytes(), 'Generated bitmap differs'
else: else: