Use the texture cache and builtin-font drawing firmware extensions

This commit is contained in:
James Babcock committed 2026-08-19 10:36:16 -07:00
1 parent deac81f98f
commit 90603b5aa4
9 files changed
+674 -60

No files matched your search

+5
View File
@@ -139,6 +139,11 @@ export function compositePage(page: EvenHubPage | null, size: { width: number; h
for (const container of paintOrder(page.containers)) {
switch (container.kind) {
case "image":
// Text painted so far is deferred draws (firmware-text runs), which
// would otherwise render above this container's raster. Stock puts a
// later image container OVER earlier text, so bake the pending draws
// first; text painted after this container stays deferred (and above).
image.bakeDeferredDrawsInPlace();
paintImageContainer(image, container);
break;
case "text":
+20 -20
View File
@@ -19,7 +19,7 @@ export type FirmwarePatchSet = {
export const CFW_PATCH_SET: FirmwarePatchSet = {
"base": "g2_2.2.6.10.bin",
"baseSha256": "f4dfb0b49ad3de3c2daf17f8a27a157c3dc98411d6a0d3ab2cfd0918f41b9afa",
"outputSha256": "401598582a01b1fd8243cdd9eb53689183cbda37f1dc8a5e2967065dea310b0c",
"outputSha256": "82726bbe4d1d82f06a3d345ba3e5a4a39161077e5d26dcd7af24c4677df87fd1",
"patches": [
{
"offset": 1089903,
@@ -48,61 +48,61 @@ export const CFW_PATCH_SET: FirmwarePatchSet = {
{
"offset": 1447919,
"old": "7ef7fefd",
"new": "bcf272fb",
"new": "bcf24cfa",
"desc": "bl snapshot_side @ 0x4db968"
},
{
"offset": 1448931,
"old": "7ef704fc",
"new": "bcf278f9",
"new": "bcf252f8",
"desc": "bl snapshot_side @ 0x4dbd5c"
},
{
"offset": 1165461,
"old": "45f0cefd",
"new": "01f323fb",
"new": "01f3fdf9",
"desc": "bl image_deferred (deferred consumer -> FIFO restore + worker, both lenses)"
},
{
"offset": 1186287,
"old": "d9f7d4ff",
"new": "fdf250fa",
"new": "fdf258f8",
"desc": "bl settings_send_wrapper (append caps field 100)"
},
{
"offset": 1183983,
"old": "f4f75aff",
"new": "fdf210fc",
"new": "fdf21afa",
"desc": "bl settings_decode_wrapper (Faceclaw lease field 101)"
},
{
"offset": 926945,
"old": "08f068fa",
"new": "3cf333f9",
"new": "3bf33dff",
"desc": "bl faceclaw_display_start @ 0x45c65a (fail-open double-tap takeover)"
},
{
"offset": 927137,
"old": "08f008fa",
"new": "3cf3d3f8",
"new": "3bf3ddfe",
"desc": "bl faceclaw_display_start @ 0x45c71a (fail-open double-tap takeover)"
},
{
"offset": 822553,
"old": "28f003f8",
"new": "56f315f9",
"new": "55f34dff",
"desc": "bl evenhub_longpress (Faceclaw lease gates custom event vs stock dialog)"
},
{
"offset": 823369,
"old": "1cf09bfb",
"new": "55f3a3ff",
"new": "55f3dbfd",
"desc": "bl ring_release (forward ring release-long-press)"
},
{
"offset": 1474137,
"old": "7fb50600",
"new": "b7f209b8",
"new": "b6f211be",
"desc": "even_ai_display_ctrl entry -> conditional Faceclaw lease trampoline"
},
{
@@ -120,13 +120,13 @@ export const CFW_PATCH_SET: FirmwarePatchSet = {
{
"offset": 1198787,
"old": "dff770fb",
"new": "f9f23cfd",
"new": "f9f28cfb",
"desc": "bl faceclaw_send_wear_event @ 0x49ec3c (outside onboarding)"
},
{
"offset": 1198881,
"old": "dff741fb",
"new": "f9f20dfd",
"new": "f9f25dfb",
"desc": "bl faceclaw_send_wear_event @ 0x49ec9a (outside onboarding)"
},
{
@@ -138,43 +138,43 @@ export const CFW_PATCH_SET: FirmwarePatchSet = {
{
"offset": 4301227,
"old": "",
"new": "02fb01f000e000bfb0b546f27424c2f22a040146206846f23f52c5f28802b2eb500f40f2543044f281124cf67e25c2f20000c0f24802caf2101518bf25609047002804bf691c2160b0bd00bf0846ffe7014640f2543044f29f22c2f20000c0f24802104770b500284ed001684ef66662ccf2ff02914218bf70bd90f8081190f8095142f6c52c8d42c0f2500c2ad205eb8501014491f80d2191f80e4191f80ae191f80b3191f80c1142ea0424012c4ff0010298bf14466e1c054680f80961b9b164294eea032028bf6421012888bf024644f62060824228bf02460cf5e2731046984706e0002180f80811bde870406047e047d5f8040138b149f29942c0f244022146bde87040104770bd00bf70b54ff6fd03c0f2450314460d46984746f27021c2f22a010e6831466ff39601b1f1005f0ad131684ef66662ccf2ff02914218bf0026412d03d070bd0026412dfbd1002cf9d04af26951c0f2450105468847002e01462846efd00129edd196f807120029e9d02168002948bf70bd47f25d02c0f2580208469047284670bd00bf2de9f04f9db046f2702ac2f22a0adaf800b058466ff39600b0f1005f40f07d81dbf800004ef66667ccf2ff07b84240f074819bf82802002800f05d81dbf82442002c00f0588140f2b870c2f20000d0f8008004a802f0d8ffb8f1000f00f02782032045f24075cff68070a4f1600108f160026ff0bf03c0f2020500bf0e6ed41826664e6ec03366668e6eab42a666ce6ee6660e6f26674e6f66678e6fa667ce6fe667d1f88060c4f88060d1f88460c4f88460d1f88860c4f88860d1f88c60c4f88c60d1f89060c4f89060d1f89460c4f89460d1f89860c4f89860d1f89c60c4f89c60d1f8a060c4f8a060d1f8a460c4f8a460d1f8a860c4f8a860d1f8ac60c4f8ac60d1f8b060c4f8b060d1f8b460c4f8b460d1f8b860c4f8b860d1f8bc60c4f8bc6051f8c06fc4f8c0604e68c4f8c4608e68c4f8c860ce68c4f8cc600e69c4f8d0604e69c4f8d4608e69c4f8d860ce69c4f8dc600e6ac4f8e0604e6ac4f8e4608e6ac4f8e860ce6ac4f8ec600e6bc4f8f0604e6bc4f8f4608e6bc4f8f860ce6bc4f8fc600e6cc4f800614e6cc4f804618e6cc4f80861ce6cc4f80c610e6dc4f810614e6dc4f814618e6dc4f81861ce6dc4f81c617ff462afdaf800603040b0f1005f40f0b2803068b84240f0ae8096f8ce00002840f07e8196f8dc10002000298df8140000f0f68105a90a5c002a00f0c9810a185378002b00f087819378002b00f08581d378002b00f083811379002b00f081815379002b00f07f819379002b00f07d81d379002b00f07b81137a002b00f07981537a002b00f07781937a002b00f07581d37a002b00f07381137b002b00f07181537b002b00f06f81937b002b00f06d81d37b002b00f06b81137c002b00f06981537c002b00f06781937c002b00f06581d37c002b00f06381137d002b00f06181537d002b00f05f81937d002b00f05d81d37d002b00f05b81137e002b00f05981537e002b00f05781937e002b00f05581d37e002b00f05381137f002b00f05181537f002b00f04f81937f002b00f04d81d27f002a00f04b81203060287ff47baf6ae19bf82a0270b1dbf82c0240b144f63421c2f207010968401a002800f3f88000208bf82a024cf61520c0f246001db0bde8f04f004704f0cdfc002800f0cd804ff6d051cff6ff71002200bf4318463183f8302283f8312283f8322283f8332283f8342283f8352283f8362283f8372283f8382283f8392283f83a2283f83b2283f83c2283f83d2283f83e2283f83f2283f8402283f8412283f8422283f8432283f8442283f8452283f8462283f8472283f8482283f8492283f84a2283f84b2283f84c2283f84d2283f84e2283f84f2283f8502283f8512283f8522283f8532283f8542283f8552283f8562283f8572283f8582283f8592283f85a2283f85b2283f85c2283f85d2283f85e2283f85f2283f8602283f8612283f8622283f8632283f8642283f8652283f8662283f8672283f8682283f8692283f86a2283f86b2283f86c2283f86d2283f86e2283f86f2283f8702283f8712283f8722283f8732283f8742283f875227ff470af012180f8ce104ff0ff310760a0f8e010a0f8e210a0f8e410a0f8e610a0f8e810a0f8ea10a0f8ec10a0f8ee10a0f8f010a0f8f210a0f8f410a0f8f610a0f8f810a0f8fa10a0f8fc10a0f8fe1010e000208bf82802cbf824028bf82a0201208bf829024cf61520c0f24600804715e00020caf8000000208bf82802cbf824024ff4163045f20f11069005a8c0f24701cdf81480884701208bf82a0204a802f0b8fecbf8d4001db0bde8f08f01303ae0023038e0033036e0043034e0053032e0063030e007302ee008302ce009302ae00a3028e00b3026e00c3024e00d3022e00e3020e00f301ee010301ce011301ae0123018e0133016e0143014e0153012e0163010e017300ee018300ce019300ae01a3008e01b3006e01c3004e01d3002e01e3000e01f305f2821d84ff052020a541dd0014445225e284a7018d04f225d288a7014d052225c28ca7010d044225b280a710cd045225a284a7108d0522259288a7104d020225828ca7142f0168500208df87300012096f8dd10002900f0c280002105aa535c002b00f0a08057187b78002b5ed0bb78002b5dd0fb78002b5cd03b79002b5bd07b79002b5ad0bb79002b59d0fb79002b58d03b7a002b57d07b7a002b56d0bb7a002b55d0fb7a002b54d03b7b002b53d07b7b002b52d0bb7b002b51d0fb7b002b50d03b7c002b4fd07b7c002b4ed0bb7c002b4dd0fb7c002b4cd03b7d002b4bd07b7d002b4ad0bb7d002b49d0fb7d002b48d03b7e002b47d07b7e002b46d0bb7e002b45d0fb7e002b44d03b7f002b43d07b7f002b42d0bb7f002b41d0fb7f002b40d0203160297ff49aaf54e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2915d84ff05303535411d00a444b235e2953700cd049235d29937008d050235c29d37004d020235b29137140f0c28200218df87310013096f8de10002900f0be80002105aa535c002b00f0a08057187b78002b5ed0bb78002b5dd0fb78002b5cd03b79002b5bd07b79002b5ad0bb79002b59d0fb79002b58d03b7a002b57d07b7a002b56d0bb7a002b55d0fb7a002b54d03b7b002b53d07b7b002b52d0bb7b002b51d0fb7b002b50d03b7c002b4fd07b7c002b4ed0bb7c002b4dd0fb7c002b4cd03b7d002b4bd07b7d002b4ad0bb7d002b49d0fb7d002b48d03b7e002b47d07b7e002b46d0bb7e002b45d0fb7e002b44d03b7f002b43d07b7f002b42d0bb7f002b41d0fb7f0Line truncated
"new": "02fb01f000e000bfb0b546f27424c2f22a040146206846f23f52c5f28802b2eb500f40f2543044f281124cf67e25c2f20000c0f24802caf2101518bf25609047002804bf691c2160b0bd00bf0846ffe7014640f2543044f29f22c2f20000c0f24802104770b500284ed001684ef66762ccf2ff02914218bf70bd90f8081190f8095142f6c52c8d42c0f2500c2ad205eb8501014491f80d2191f80e4191f80ae191f80b3191f80c1142ea0424012c4ff0010298bf14466e1c054680f80961b9b164294eea032028bf6421012888bf024644f62060824228bf02460cf5e2731046984706e0002180f80811bde870406047e047d5f8040138b149f29942c0f244022146bde87040104770bd00bf70b54ff6fd03c0f2450314460d46984746f27021c2f22a010e6831466ff39601b1f1005f0ad131684ef66762ccf2ff02914218bf0026412d03d070bd0026412dfbd1002cf9d04af26951c0f2450105468847002e01462846efd00129edd196f807120029e9d02168002948bf70bd47f25d02c0f2580208469047284670bd00bf2de9f04f9db046f2702ac2f22a0adaf800b058466ff39600b0f1005f40f07d81dbf800004ef66767ccf2ff07b84240f074819bf82802002800f05d81dbf82442002c00f0588140f2b870c2f20000d0f8008004a802f08effb8f1000f00f0dd81032045f24075cff68070a4f1600108f160026ff0bf03c0f2020500bf0e6ed41826664e6ec03366668e6eab42a666ce6ee6660e6f26674e6f66678e6fa667ce6fe667d1f88060c4f88060d1f88460c4f88460d1f88860c4f88860d1f88c60c4f88c60d1f89060c4f89060d1f89460c4f89460d1f89860c4f89860d1f89c60c4f89c60d1f8a060c4f8a060d1f8a460c4f8a460d1f8a860c4f8a860d1f8ac60c4f8ac60d1f8b060c4f8b060d1f8b460c4f8b460d1f8b860c4f8b860d1f8bc60c4f8bc6051f8c06fc4f8c0604e68c4f8c4608e68c4f8c860ce68c4f8cc600e69c4f8d0604e69c4f8d4608e69c4f8d860ce69c4f8dc600e6ac4f8e0604e6ac4f8e4608e6ac4f8e860ce6ac4f8ec600e6bc4f8f0604e6bc4f8f4608e6bc4f8f860ce6bc4f8fc600e6cc4f800614e6cc4f804618e6cc4f80861ce6cc4f80c610e6dc4f810614e6dc4f814618e6dc4f81861ce6dc4f81c617ff462afdaf800603040b0f1005f40f0b2803068b84240f0ae8096f8ce00002840f0348196f8dc10002000298df8140000f0ac8105a90a5c002a00f07f810a185378002b00f03d819378002b00f03b81d378002b00f039811379002b00f037815379002b00f035819379002b00f03381d379002b00f03181137a002b00f02f81537a002b00f02d81937a002b00f02b81d37a002b00f02981137b002b00f02781537b002b00f02581937b002b00f02381d37b002b00f02181137c002b00f01f81537c002b00f01d81937c002b00f01b81d37c002b00f01981137d002b00f01781537d002b00f01581937d002b00f01381d37d002b00f01181137e002b00f00f81537e002b00f00d81937e002b00f00b81d37e002b00f00981137f002b00f00781537f002b00f00581937f002b00f00381d27f002a00f00181203060287ff47baf20e19bf82a0270b1dbf82c0240b144f63421c2f207010968401a002800f3ae8000208bf82a024cf61520c0f246001db0bde8f04f004704f005fb002800f0838000216ff017020023c518c1542f338618b3f50d7f02f12f026970a970e97029716971a971e97129726972a972e97229736973a973e97329746974a974e97429756975a97586f82f1086f830106976a976e97629776977a977e97785f8201085f8211085f8221085f8231085f8241085f8251085f8261085f8271085f8281085f8291085f82a1085f82b1085f82c1085f82d1085f82e10b7d1012180f8ce104ff0ff310760a0f8e010a0f8e210a0f8e410a0f8e610a0f8e810a0f8ea10a0f8ec10a0f8ee10a0f8f010a0f8f210a0f8f410a0f8f610a0f8f810a0f8fa10a0f8fc10a0f8fe1010e000208bf82802cbf824028bf82a0201208bf829024cf61520c0f24600804715e00020caf8000000208bf82802cbf824024ff4163045f20f11069005a8c0f24701cdf81480884701208bf82a0204a802f06efecbf8d4001db0bde8f08f01303ae0023038e0033036e0043034e0053032e0063030e007302ee008302ce009302ae00a3028e00b3026e00c3024e00d3022e00e3020e00f301ee010301ce011301ae0123018e0133016e0143014e0153012e0163010e017300ee018300ce019300ae01a3008e01b3006e01c3004e01d3002e01e3000e01f305f2821d84ff052020a541dd0014445225e284a7018d04f225d288a7014d052225c28ca7010d044225b280a710cd045225a284a7108d0522259288a7104d020225828ca7142f0168500208df87300012096f8dd10002900f0c280002105aa535c002b00f0a08057187b78002b5ed0bb78002b5dd0fb78002b5cd03b79002b5bd07b79002b5ad0bb79002b59d0fb79002b58d03b7a002b57d07b7a002b56d0bb7a002b55d0fb7a002b54d03b7b002b53d07b7b002b52d0bb7b002b51d0fb7b002b50d03b7c002b4fd07b7c002b4ed0bb7c002b4dd0fb7c002b4cd03b7d002b4bd07b7d002b4ad0bb7d002b49d0fb7d002b48d03b7e002b47d07b7e002b46d0bb7e002b45d0fb7e002b44d03b7f002b43d07b7f002b42d0bb7f002b41d0fb7f002b40d0203160297ff49aaf54e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2915d84ff05303535411d00a444b235e2953700cd049235d29937008d050235c29d37004d020235b29137140f0c28200218df87310013096f8de10002900f0be80002105aa535c002b00f0a08057187b78002b5ed0bb78002b5dd0fb78002b5cd03b79002b5bd07b79002b5ad0bb79002b59d0fb79002b58d03b7a002b57d07b7a002b56d0bb7a002b55d0fb7a002b54d03b7b002b53d07b7b002b52d0bb7b002b51d0fb7b002b50d03b7c002b4fd07b7c002b4ed0bb7c002b4dd0fb7c002b4cd03b7d002b4bd07b7d002b4ad0bb7d002b49d0fb7d002b48d03b7e002b47d07b7e002b46d0bb7e002b45d0fb7e002b44d03b7f002b43d07b7f002b42d0bb7f002b41d0fb7f002b40d0203160297ff49aaf50e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2911d84ff0440353540dd00Line truncated
"desc": "append injected blobs to main-app payload"
},
{
"offset": 777711,
"old": "44c33500",
"new": "12353600",
"new": "604b3600",
"desc": "main-app subheader payload size (ps)"
},
{
"offset": 152,
"old": "c4c33500",
"new": "92353600",
"new": "e04b3600",
"desc": "main-app TOC entry size (ps + 128)"
},
{
"offset": 777831,
"old": "44c33504",
"new": "12353604",
"new": "604b3604",
"desc": "main-app preamble length (low 24 bits)"
},
{
"offset": 777835,
"old": "df8110c9",
"new": "cb656261",
"new": "562ec508",
"desc": "[5] ota/s200_firmware_ota.bin preamble crc32"
},
{
"offset": 156,
"old": "c1be0fb1",
"new": "b053052f",
"new": "ef84d21c",
"desc": "[5] ota/s200_firmware_ota.bin component crc32c (TOC)"
},
{
"offset": 777715,
"old": "c1be0fb1",
"new": "b053052f",
"new": "ef84d21c",
"desc": "[5] ota/s200_firmware_ota.bin component crc32c (subheader)"
}
]
+127 -5
View File
@@ -21,7 +21,7 @@ import { File, knownFolders } from "@nativescript/core";
import { getTextWidth } from "@evenrealities/pretext";
import { EVENHUB_RUNTIME_FONT_FILENAME, isEvenHubFontAsset } from "../g2/firmware-fonts";
import { toUint8Array } from "../util/array-util";
import { GrayImage } from "./image";
import { GrayImage, type FwTextFont, type PlacedFwText, type PlacedFwTextGlyph } from "./image";
declare const com: any;
@@ -61,7 +61,35 @@ function base64Decode(text: string): Uint8Array {
return out.subarray(0, outIndex);
}
export class EvenHubFont {
/**
* Codepoint ranges whose glyphs the font asset merged from the flash CJK
* font with a baseline-shifted ofsY (see scripts/build-evenhub-font.mjs).
* The shift keeps the phone-side composite right, but the firmware's mode-15
* draw uses the root font's baseline with the glyph's TRUE ofsY, so the two
* may disagree by the shift — keep these baked until the fallback placement
* is hardware-verified. Mirrors the build script's SYMBOL_RANGES.
*/
const MODE15_UNCERTAIN_RANGES: ReadonlyArray<readonly [number, number]> = [
[0x2000, 0x206f], [0x2100, 0x214f], [0x2190, 0x21ff], [0x2200, 0x22ff],
[0x2500, 0x257f], [0x2580, 0x259f], [0x25a0, 0x25ff], [0x2600, 0x26ff],
[0x2700, 0x27bf], [0x3000, 0x303f], [0xff00, 0xffef],
];
function inMode15UncertainRange(cp: number): boolean {
for (const [start, end] of MODE15_UNCERTAIN_RANGES) {
if (cp >= start && cp <= end) return true;
}
return false;
}
/** The 4bpp level an 8-bit value packs to (BmpUtil's GRAY_TO_NIBBLE). */
function quantNibble(value: number): number {
return value <= 0 ? 0 : Math.min(15, (value + 8) >> 4);
}
export class EvenHubFont implements FwTextFont {
/** One builtin firmware font today; see FwTextFont. */
readonly atlasTag = 1;
readonly lineHeight: number;
readonly baseline: number;
private readonly glyphs: Map<number, GlyphRecord>;
@@ -195,22 +223,110 @@ export class EvenHubFont {
return lines;
}
/** Draw one line of text with its top-left at (x, y). Missing glyphs are skipped. */
/**
* Draw one line of text with its top-left at (x, y). Missing glyphs are
* skipped.
*
* Glyphs the firmware's mode-15 builtin-font draw renders identically to
* our raster (native-table entries outside MODE15_UNCERTAIN_RANGES) are
* recorded as deferred firmware-text runs: maximal stretches laid out with
* consecutive pretext advances, spaces included as inkless connectors so
* the firmware's own advance/kerning walk stays in lockstep. Anything else
* (CJK fallback, shifted symbols, unknown codepoints) rasters immediately
* and breaks the run — a break means the next run restarts at an explicit
* pen x, so no cursor prediction ever spans an uncertain glyph.
*/
drawText(image: GrayImage, x: number, y: number, text: string, value = 255): void {
const cps = Array.from(text).map((c) => c.codePointAt(0)!);
const baselineY = y + this.baseline;
let penX = x;
let run: PlacedFwTextGlyph[] = [];
let runX = 0;
const flushRun = () => {
if (run.length) {
this.emitRun(image, runX, y, value, run);
run = [];
}
};
for (let i = 0; i < cps.length; i++) {
const cp = cps[i]!;
const glyph = this.glyphs.get(cp);
if (glyph) {
if (glyph && !inMode15UncertainRange(cp)) {
if (run.length === 0) runX = penX;
run.push({
cp,
dx: penX - runX,
ink: glyph[0] > 0 && glyph[1] > 0 && glyph[5] > 0,
});
} else if (glyph) {
flushRun();
this.drawGlyph(image, glyph, this.bitmap, penX, baselineY, value);
} else {
flushRun();
const cjk = this.getCjkGlyph(cp);
if (cjk) this.drawGlyph(image, cjk.record, cjk.bitmap, penX, y + this.cjkBaseline, value);
}
penX += this.advanceOf(cp, cps[i + 1] ?? 0);
}
flushRun();
}
/** Record one run, trimmed of inkless leading/trailing connectors. */
private emitRun(
image: GrayImage,
runX: number,
lineY: number,
value: number,
members: PlacedFwTextGlyph[],
): void {
let first = 0;
let last = members.length - 1;
while (first <= last && !members[first]!.ink) first++;
while (last >= first && !members[last]!.ink) last--;
if (first > last) return; // nothing visible
const startX = runX + members[first]!.dx;
const trimmed = members.slice(first, last + 1).map((member) =>
first === 0 ? member : { ...member, dx: member.dx - members[first]!.dx },
);
image.drawFwText(this, startX, lineY, value, trimmed);
}
/** FwTextFont: bake a recorded run with firmware-exact pixel math. */
bakeFwTextRun(target: GrayImage, run: PlacedFwText, dx: number, dy: number): void {
const baselineY = run.y + dy + this.baseline;
for (const member of run.glyphs) {
if (!member.ink) continue;
const glyph = this.glyphs.get(member.cp);
if (glyph) this.drawGlyph(target, glyph, this.bitmap, run.x + member.dx + dx, baselineY, run.value);
}
}
/** FwTextFont: raster registration data for the Java-side planner. */
fwGlyphWireData(cp: number): {
ofsX: number;
inkTop: number;
boxW: number;
boxH: number;
nibbles: Uint8Array;
} | null {
const glyph = this.glyphs.get(cp);
if (!glyph) return null;
const [boxW, boxH, ofsX, ofsY, off, len] = glyph;
if (boxW <= 0 || boxH <= 0 || len <= 0 || boxW > 255 || boxH > 255) return null;
if (ofsX < -32768 || ofsX > 32767) return null;
const inkTop = this.baseline - boxH - ofsY;
if (inkTop < -32768 || inkTop > 32767) return null;
// Repack from the source stride (floor(boxW/2)+1, pad nibble at odd
// widths) to the wire stride ceil(boxW/2).
const srcStride = Math.floor(boxW / 2) + 1;
const outStride = (boxW + 1) >> 1;
const nibbles = new Uint8Array(outStride * boxH);
for (let row = 0; row < boxH; row++) {
for (let byte = 0; byte < outStride; byte++) {
nibbles[row * outStride + byte] = this.bitmap[off + row * srcStride + byte] ?? 0;
}
}
return { ofsX, inkTop, boxW, boxH, nibbles };
}
/** Draw wrapped text within a box; returns the number of lines drawn. */
@@ -257,13 +373,19 @@ export class EvenHubFont {
const stride = Math.floor(boxW / 2) + 1;
const left = penX + ofsX;
const top = baselineY - boxH - ofsY;
// Firmware-exact shading: the CFW draws through a LUT mapping source
// nibble n to n*top/15 (integer), skipping only n == 0. Writing 16*level
// makes the composite quantize (min(15, (v+8)>>4)) back to exactly that
// level, so the texture-cache planner's pixel checks — and the shadow
// after an on-glasses mode-15 draw — match this bake bit-for-bit.
const top4 = quantNibble(value);
for (let row = 0; row < boxH; row++) {
const rowStart = off + row * stride;
for (let col = 0; col < boxW; col++) {
const byte = bitmap[rowStart + (col >> 1)] ?? 0;
const nibble = col % 2 === 0 ? byte >> 4 : byte & 0x0f;
if (nibble === 0) continue;
image.setPixel(left + col, top + row, Math.round((nibble / 15) * value));
image.setPixel(left + col, top + row, Math.floor((nibble * top4) / 15) * 16);
}
}
}
+76 -7
View File
@@ -20,12 +20,14 @@
* only means "no wire savings for this draw".
*/
import { BdfFont, Glyph } from "./bdffont";
import { GrayImage, type DeferredDraw, type PlacedImage } from "./image";
import { GrayImage, type DeferredDraw, type PlacedFwText, type PlacedImage } from "./image";
declare const com: any;
const GLYPH_RECORD_BYTES = 12; // [0][fontId u16][encoding u32][penX s16][lineY s16][value u8]
const IMAGE_RECORD_BYTES = 9; // [1][imageId u32][x s16][y s16]
const GLYPH_RECORD_BYTES = 12; // [0][fontId u16][encoding u32][penX s16][lineY s16][value u8]
const IMAGE_RECORD_BYTES = 9; // [1][imageId u32][x s16][y s16]
const FWTEXT_HEADER_BYTES = 7; // [2][x s16][y s16][value u8][count u8]
const FWTEXT_MEMBER_BYTES = 7; // [cp u32][dx s16][ink u8]
type FontWireState = {
fontId: number;
@@ -74,6 +76,51 @@ function representableGlyph(font: BdfFont, glyph: Glyph): boolean {
return inkTop >= 0 && inkTop + glyph.bbxHeight <= font.lineHeight;
}
/** Firmware-font codepoints already registered with the Java FwGlyphAtlas. */
const fwRegistered = new Set<string>();
/**
* Whether a firmware-text run is expressible on the wire, registering any
* unseen ink members' rasters with the Java FwGlyphAtlas as a side effect.
*/
function prepareFwTextRun(placed: PlacedFwText): boolean {
if (!inRange16(placed.x) || !inRange16(placed.y)) return false;
if (placed.glyphs.length === 0 || placed.glyphs.length > 255) return false;
let registration: Array<{ cp: number; data: NonNullable<ReturnType<PlacedFwText["font"]["fwGlyphWireData"]>> }> | null = null;
for (const member of placed.glyphs) {
if (member.cp < 0 || member.cp > 0x10ffff || !inRange16(member.dx)) return false;
if (!member.ink) continue;
const key = `${placed.font.atlasTag}:${member.cp}`;
if (fwRegistered.has(key)) continue;
const data = placed.font.fwGlyphWireData(member.cp);
if (!data) return false; // ink without raster data: keep the run baked
fwRegistered.add(key);
registration ??= [];
registration.push({ cp: member.cp, data });
}
if (registration) {
let total = 0;
for (const entry of registration) {
total += 10 + entry.data.nibbles.length;
}
const buffer = new ArrayBuffer(total);
const view = new DataView(buffer);
const bytes = new Uint8Array(buffer);
let offset = 0;
for (const entry of registration) {
view.setUint32(offset, entry.cp, true);
view.setInt16(offset + 4, entry.data.ofsX, true);
view.setInt16(offset + 6, entry.data.inkTop, true);
view.setUint8(offset + 8, entry.data.boxW);
view.setUint8(offset + 9, entry.data.boxH);
bytes.set(entry.data.nibbles, offset + 10);
offset += 10 + entry.data.nibbles.length;
}
com.faceclaw.app.FwGlyphAtlas.register(buffer);
}
return true;
}
/** Resolve (registering on first sight) the Java image id for a drawImage source. */
function imageId(placed: PlacedImage): number | null {
if (!global.isAndroid) return null;
@@ -100,13 +147,16 @@ function inRange16(v: number): boolean {
* alongside the frame's pixels: little-endian tagged records
* [0][fontId u16][encoding u32][penX s16][lineY s16][value u8] (glyph)
* [1][imageId u32][x s16][y s16] (image)
* [2][x s16][y s16][value u8][count u8] (fw text run)
* then count x [cp u32][dx s16][ink u8]
* in draw order. Returns null when nothing is expressible (or off-Android).
*/
export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer | null {
if (!global.isAndroid || draws.length === 0) return null;
// Pass 1: resolve ids, collect unregistered glyph rasters, size the buffer.
// Pass 1: resolve ids, register unseen rasters, size the buffer.
let registration: Map<FontWireState, { font: BdfFont; glyphs: Glyph[] }> | null = null;
const fwRunOk = new Map<PlacedFwText, boolean>();
let bytes = 0;
for (const placed of draws) {
if (placed.kind === "glyph") {
@@ -123,9 +173,13 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer |
registration.set(state, group);
}
group.glyphs.push(placed.glyph);
} else {
} else if (placed.kind === "image") {
if (!inRange16(placed.x) || !inRange16(placed.y)) continue;
if (imageId(placed) !== null) bytes += IMAGE_RECORD_BYTES;
} else {
const ok = prepareFwTextRun(placed);
fwRunOk.set(placed, ok);
if (ok) bytes += FWTEXT_HEADER_BYTES + placed.glyphs.length * FWTEXT_MEMBER_BYTES;
}
}
if (registration) {
@@ -137,8 +191,8 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer |
const out = new DataView(new ArrayBuffer(bytes));
let offset = 0;
for (const placed of draws) {
if (!inRange16(placed.x) || !inRange16(placed.y)) continue;
if (placed.kind === "glyph") {
if (!inRange16(placed.x) || !inRange16(placed.y)) continue;
const state = fontWireState(placed.font);
if (!state || !representableGlyph(placed.font, placed.glyph)) continue;
out.setUint8(offset, 0);
@@ -148,7 +202,8 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer |
out.setInt16(offset + 9, placed.y, true);
out.setUint8(offset + 11, placed.value);
offset += GLYPH_RECORD_BYTES;
} else {
} else if (placed.kind === "image") {
if (!inRange16(placed.x) || !inRange16(placed.y)) continue;
const id = imageId(placed);
if (id === null) continue;
out.setUint8(offset, 1);
@@ -156,6 +211,20 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer |
out.setInt16(offset + 5, placed.x, true);
out.setInt16(offset + 7, placed.y, true);
offset += IMAGE_RECORD_BYTES;
} else {
if (!fwRunOk.get(placed)) continue;
out.setUint8(offset, 2);
out.setInt16(offset + 1, placed.x, true);
out.setInt16(offset + 3, placed.y, true);
out.setUint8(offset + 5, placed.value);
out.setUint8(offset + 6, placed.glyphs.length);
offset += FWTEXT_HEADER_BYTES;
for (const member of placed.glyphs) {
out.setUint32(offset, member.cp, true);
out.setInt16(offset + 4, member.dx, true);
out.setUint8(offset + 6, member.ink ? 1 : 0);
offset += FWTEXT_MEMBER_BYTES;
}
}
}
return out.buffer;
+97 -3
View File
@@ -50,13 +50,62 @@ export type PlacedImage = {
y: number;
};
/** One member of a firmware-font text run: codepoint at run.x + dx; ink =
* whether it has visible pixels (spaces are inkless connectors kept for the
* firmware's own advance/kerning continuity). */
export type PlacedFwTextGlyph = {
cp: number;
dx: number;
ink: boolean;
};
/**
* The provider behind firmware-font text runs (implemented by EvenHubFont).
* Structural interface so image.ts needs no import: the font both bakes runs
* phone-side (with firmware-exact pixel math) and serves per-glyph raster
* data for the Java-side eligibility checks.
*/
export interface FwTextFont {
/** Stable tag for fingerprints/registration (one builtin font today). */
readonly atlasTag: number;
bakeFwTextRun(target: GrayImage, run: PlacedFwText, dx: number, dy: number): void;
/** Wire-registration raster for a run member, or null when unavailable. */
fwGlyphWireData(cp: number): {
ofsX: number;
/** First ink row relative to the run's line-top y. */
inkTop: number;
boxW: number;
boxH: number;
/** 4bpp rows, stride ceil(boxW/2), high nibble = left pixel. */
nibbles: Uint8Array;
} | null;
}
/**
* One deferred firmware-font text run (CFW mode 15): a maximal stretch of
* text that EvenHubFont laid out with consecutive pretext advances, drawn by
* the glasses' builtin 20px font chain. Members' dx are relative to x so
* translation only touches x/y. Runs must only be built over glyphs whose
* on-glasses rendering the phone predicts exactly (the font enforces this).
*/
export type PlacedFwText = {
kind: "fwtext";
font: FwTextFont;
/** Pen x of the first member; the wire draw starts exactly here. */
x: number;
/** Line top y (the mode-15 y argument). */
y: number;
value: number;
glyphs: readonly PlacedFwTextGlyph[];
};
/**
* A retained draw, always rendered on top of the raster pixels regardless of
* draw order (deferred draws keep their order among themselves). An overlay
* that must cover deferred draws below it needs its own plane (see plane.ts)
* or must suppress the covered draws.
*/
export type DeferredDraw = PlacedGlyph | PlacedImage;
export type DeferredDraw = PlacedGlyph | PlacedImage | PlacedFwText;
export class GrayImage {
readonly width: number;
@@ -321,11 +370,19 @@ export class GrayImage {
hash = mixInt(hash, placed.font.fingerprintId);
hash = mixInt(hash, placed.glyph.encoding);
hash = mixInt(hash, placed.value);
} else {
} else if (placed.kind === "image") {
hash = mixInt(hash, 0x1c0e5);
hash = mixInt(hash, placed.source.width);
hash = mixInt(hash, placed.source.height);
hash = mixInt(hash, placed.source.contentHash32());
} else {
hash = mixInt(hash, 0xf17e);
hash = mixInt(hash, placed.font.atlasTag);
hash = mixInt(hash, placed.value);
for (const member of placed.glyphs) {
hash = mixInt(hash, member.cp);
hash = mixInt(hash, member.dx);
}
}
hash = mixInt(hash, placed.x);
hash = mixInt(hash, placed.y);
@@ -373,6 +430,41 @@ export class GrayImage {
this.drawList.push({ kind: "image", source: source.withDrawsBaked(), x, y });
}
/**
* Record a firmware-font text run (see PlacedFwText). Called by
* EvenHubFont, which owns run construction; the members array must be
* immutable from here on.
*/
drawFwText(
font: FwTextFont,
x: number,
y: number,
value: number,
glyphs: readonly PlacedFwTextGlyph[],
): void {
if (glyphs.length === 0) return;
this.drawList.push({ kind: "fwtext", font, x, y, value: clampByte(value), glyphs });
}
/**
* Bake this image's deferred draws into its own pixels and clear the list.
* Used where later raster must be able to cover earlier draws within the
* same image (e.g. the EvenHub compositor's image-over-text z-order).
*/
bakeDeferredDrawsInPlace(): void {
if (!this.drawList.length) return;
const draws = this.drawList.splice(0, this.drawList.length);
for (const placed of draws) {
if (placed.kind === "glyph") {
rasterizeGlyph(this, placed.font, placed.glyph, placed.x, placed.y, placed.value);
} else if (placed.kind === "image") {
this.bitBlt(placed.source, placed.x, placed.y, { transparentZero: true });
} else {
placed.font.bakeFwTextRun(this, placed, 0, 0);
}
}
}
/**
* This image with its deferred draws rasterized into the pixel buffer.
* Returns this image unchanged when there are none, otherwise a baked copy
@@ -391,8 +483,10 @@ export class GrayImage {
for (const placed of this.drawList) {
if (placed.kind === "glyph") {
rasterizeGlyph(target, placed.font, placed.glyph, placed.x + dx, placed.y + dy, placed.value);
} else {
} else if (placed.kind === "image") {
target.bitBlt(placed.source, placed.x + dx, placed.y + dy, { transparentZero: true });
} else {
placed.font.bakeFwTextRun(target, placed, dx, dy);
}
}
}