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);
/* NULL safe. Discards an unfinished frame without presenting. Destroy app-owned
* GL resources before close; all session handles become invalid on return. */
/* NULL safe. Valid after the session stops or enters a terminal error state, and
* 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);
/* 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 {
const a = state(session orelse return);
std.debug.assert(runtime.validateMainThreadSessionReady(a) == abi.OK);
_ = release(a);
app.destroy(handle(a));
}

View File

@ -13,21 +13,22 @@ pub fn queueCreate(
if (out == null) return abi.INVALID;
const d = desc.*;
if (!validStyle(&d.style)
or !abi.validText(d.title, true)
or !abi.finiteSize(d.content_size)
or d.content_size.width == 0
or d.content_size.height == 0
or !abi.finiteSize(d.min_size)
or !abi.finiteSize(d.max_size)
or d.flags & ~@as(u32, c.Z_WINDOW_OPENGL) != 0) return abi.INVALID;
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)) return abi.INVALID;
if (d.parent != null
and (runtime.fromObjectHandle(d.parent).app != runtime.fromAppHandle(handle)
or !runtime.fromObjectHandle(d.parent).alive)) return abi.INVALID;
if (d.content_size.width == 0 or d.content_size.height == 0)
return abi.INVALID;
if (!abi.finiteSize(d.content_size) or !abi.finiteSize(d.content_size) or !abi.finiteSize(d.min_size) or !abi.finiteSize(d.max_size))
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)
return abi.INVALID;
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))
return abi.INVALID;
if (d.parent != null and (runtime.fromObjectHandle(d.parent).app != runtime.fromAppHandle(handle) or !runtime.fromObjectHandle(d.parent).alive))
return abi.INVALID;
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 {
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
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 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,
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()
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))
header_size = 8 + len(codepoints) * 16
records, pixels = bytearray(), bytearray()
for cp in codepoints:
records = bytearray(12 * len(codepoints))
streams = bytearray()
for index, cp in enumerate(codepoints):
character = chr(cp)
x, y, right, bottom = font.getbbox(character, anchor='ls')
width, height = right - x, bottom - y
@ -43,9 +43,18 @@ def generate(manifest, font_path):
if width and height:
ImageDraw.Draw(image).text((-x, -y), character, font=font, fill=255, anchor='ls')
advance = int(font.getlength(character) + .5)
records += struct.pack('<IIBBbbB3x', cp, header_size + len(pixels), width, height, x, y, advance)
pixels += image.tobytes()
return b'UF16' + struct.pack('<I', len(codepoints)) + records + pixels
samples = [(value + 8) // 17 for value in image.tobytes()]
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)
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):
@ -63,34 +72,6 @@ def zero_rle(data):
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():
parser = argparse.ArgumentParser(description=__doc__)
mode = parser.add_mutually_exclusive_group(required=True)
@ -101,7 +82,7 @@ def main():
args = parser.parse_args()
manifest = json.loads((ROOT / 'manifest.json').read_text())
if args.regenerate or args.verify_generation:
data = pack(generate(manifest, args.font))
data = generate(manifest, args.font)
if args.verify_generation:
assert data == (ROOT / 'roboto-16.fontatlas').read_bytes(), 'Generated bitmap differs'
else: