forked from Blendman974/faceclaw
Use TTF fonts throughout the UI; also add some more functions to app extension API
This commit is contained in:
1 parent
5abbe2a405
commit
96842ceb23
82 files changed
+2666
-288
No files matched your search
@@ -2248,6 +2248,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
+ " fw=" + tex.fwGlyphs + "/" + tex.fwRuns
|
||||
+ " baked=" + tex.bakedCandidates
|
||||
+ (tex.uploadBytes > 0 ? " upload=" + tex.uploadBytes + "B" : "")
|
||||
+ " cache=" + textureCache.usedBytes() + "B"
|
||||
+ " payload=" + tex.payload.length + "B";
|
||||
FrameTimings.getInstance().log(frameId, texLog);
|
||||
finishEnqueueDesiredImageLocked(plan, fingerprint, paintMs, frameId);
|
||||
|
||||
@@ -174,6 +174,132 @@ public final class FontFileRenderer {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Render one codepoint as a tight-ink-box antialiased glyph cell for the
|
||||
* texture-cache text pipeline (see app/graphics/ttf-font.ts). Ligatures
|
||||
* are disabled so a per-codepoint raster plus pairwise kerning (measured
|
||||
* via measureTextExact) fully describes a line. Little-endian packet:
|
||||
* [advance u16, 26.6 fixed point]
|
||||
* [bearingX s16] ink left relative to the pen position
|
||||
* [inkTop s16] ink top relative to the line top (ascent above baseline)
|
||||
* [width u16][height u16]
|
||||
* [width*height coverage bytes, gamma-mapped]
|
||||
* An ink-free glyph (space) has width = height = 0 but a valid advance.
|
||||
* Empty array when the font cannot be loaded or the render fails.
|
||||
*/
|
||||
public static byte[] renderGlyphCell(String path, float sizePx, int codePoint, double gamma) {
|
||||
Typeface typeface = loadTypeface(path);
|
||||
if (typeface == null || sizePx <= 0 || codePoint < 0 || codePoint > 0x10ffff) {
|
||||
return new byte[0];
|
||||
}
|
||||
try {
|
||||
Paint paint = glyphPaint(typeface, sizePx);
|
||||
String text = new String(Character.toChars(codePoint));
|
||||
float advance = paint.measureText(text);
|
||||
int advanceFixed = Math.max(0, Math.min(0xffff, Math.round(advance * 64f)));
|
||||
|
||||
android.graphics.Rect bounds = new android.graphics.Rect();
|
||||
paint.getTextBounds(text, 0, text.length(), bounds);
|
||||
if (bounds.isEmpty()) {
|
||||
return glyphCellPacket(advanceFixed, 0, 0, 0, 0, null);
|
||||
}
|
||||
// getTextBounds can be off by a hair on AA edges; render with
|
||||
// padding, then trim to the actually painted box.
|
||||
int pad = 2;
|
||||
int bw = bounds.width() + 2 * pad;
|
||||
int bh = bounds.height() + 2 * pad;
|
||||
if (bw <= 0 || bh <= 0 || bw * bh > 1_000_000) {
|
||||
return new byte[0];
|
||||
}
|
||||
int penX = pad - bounds.left;
|
||||
int baselineY = pad - bounds.top;
|
||||
Bitmap bitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ALPHA_8);
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
canvas.drawText(text, penX, baselineY, paint);
|
||||
byte[] pixels = alpha8Pixels(bitmap, bw, bh);
|
||||
bitmap.recycle();
|
||||
|
||||
int x0 = bw, x1 = -1, y0 = bh, y1 = -1;
|
||||
for (int y = 0; y < bh; y++) {
|
||||
int row = y * bw;
|
||||
for (int x = 0; x < bw; x++) {
|
||||
if (pixels[row + x] != 0) {
|
||||
if (x < x0) x0 = x;
|
||||
if (x > x1) x1 = x;
|
||||
if (y < y0) y0 = y;
|
||||
if (y > y1) y1 = y;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (x1 < x0) {
|
||||
return glyphCellPacket(advanceFixed, 0, 0, 0, 0, null);
|
||||
}
|
||||
int width = x1 - x0 + 1;
|
||||
int height = y1 - y0 + 1;
|
||||
Paint.FontMetricsInt fm = paint.getFontMetricsInt();
|
||||
int bearingX = x0 - penX;
|
||||
int inkTop = (-fm.ascent) + (y0 - baselineY);
|
||||
|
||||
byte[] lut = gammaLut(gamma);
|
||||
byte[] coverage = new byte[width * height];
|
||||
for (int y = 0; y < height; y++) {
|
||||
int src = (y0 + y) * bw + x0;
|
||||
int dst = y * width;
|
||||
for (int x = 0; x < width; x++) {
|
||||
coverage[dst + x] = lut[pixels[src + x] & 0xff];
|
||||
}
|
||||
}
|
||||
return glyphCellPacket(advanceFixed, bearingX, inkTop, width, height, coverage);
|
||||
} catch (Exception | OutOfMemoryError e) {
|
||||
Log.w(TAG, "glyph render failed: " + path + " U+" + Integer.toHexString(codePoint), e);
|
||||
return new byte[0];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Exact (float) advance width of a text run, with the same paint setup as
|
||||
* renderGlyphCell (subpixel advances, ligatures off), so pair kerning can
|
||||
* be derived as measure(ab) - measure(a) - measure(b).
|
||||
*/
|
||||
public static double measureTextExact(String path, String text, float sizePx) {
|
||||
Typeface typeface = loadTypeface(path);
|
||||
if (typeface == null || text == null || sizePx <= 0) {
|
||||
return 0;
|
||||
}
|
||||
return glyphPaint(typeface, sizePx).measureText(text);
|
||||
}
|
||||
|
||||
/** Paint used by both the glyph-cell renderer and its measurements. */
|
||||
private static Paint glyphPaint(Typeface typeface, float sizePx) {
|
||||
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
|
||||
paint.setTypeface(typeface);
|
||||
paint.setTextSize(sizePx);
|
||||
paint.setColor(Color.WHITE);
|
||||
// Ligatures merge codepoints into one glyph, which the per-codepoint
|
||||
// cache model cannot represent; kerning still applies.
|
||||
paint.setFontFeatureSettings("'liga' off, 'clig' off");
|
||||
return paint;
|
||||
}
|
||||
|
||||
private static byte[] glyphCellPacket(
|
||||
int advanceFixed, int bearingX, int inkTop, int width, int height, byte[] coverage) {
|
||||
byte[] out = new byte[10 + (coverage == null ? 0 : coverage.length)];
|
||||
out[0] = (byte) (advanceFixed & 0xff);
|
||||
out[1] = (byte) ((advanceFixed >> 8) & 0xff);
|
||||
out[2] = (byte) (bearingX & 0xff);
|
||||
out[3] = (byte) ((bearingX >> 8) & 0xff);
|
||||
out[4] = (byte) (inkTop & 0xff);
|
||||
out[5] = (byte) ((inkTop >> 8) & 0xff);
|
||||
out[6] = (byte) (width & 0xff);
|
||||
out[7] = (byte) ((width >> 8) & 0xff);
|
||||
out[8] = (byte) (height & 0xff);
|
||||
out[9] = (byte) ((height >> 8) & 0xff);
|
||||
if (coverage != null) {
|
||||
System.arraycopy(coverage, 0, out, 10, coverage.length);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Line metrics for a font at a pixel size, as "ascent descent lineGap"
|
||||
* (integers, pixels). Empty string if the font cannot be loaded.
|
||||
|
||||
@@ -15,8 +15,10 @@ import java.util.Map;
|
||||
* - as the ink mask for the "would this draw land correctly" check against
|
||||
* the composited frame,
|
||||
* - to punch the ink pixels out of the baked delta rect it replaces,
|
||||
* - pre-encoded as the CFW cached-image bytes ([w][h][4bpp RLE], ink = 15)
|
||||
* uploaded via mode 12 and drawn via mode 14 with a top-color LUT.
|
||||
* - pre-encoded as the CFW cached-image bytes ([w][h][4bpp RLE]) uploaded
|
||||
* via mode 12 and drawn via mode 14 with a top-color LUT. 1bpp (BDF)
|
||||
* glyphs store ink at 15 so the LUT maps them to exactly the requested
|
||||
* level; AA (TTF) glyphs store true coverage nibbles the LUT scales.
|
||||
*
|
||||
* Fonts are identified by a stable string key (the embedded font name), NOT a
|
||||
* per-JS-context counter: worker threads register independently and must agree
|
||||
@@ -44,36 +46,50 @@ public final class GlyphAtlas {
|
||||
public final int inkHeight;
|
||||
/** Horizontal bearing: draw x = pen x + bbxX. */
|
||||
public final int bbxX;
|
||||
/** Ink bitmap rows (inkHeight entries), bit (rowBitWidth-1-col) = ink. */
|
||||
/** 1bpp ink rows (inkHeight entries), bit (rowBitWidth-1-col) = ink; null for AA glyphs. */
|
||||
final int[] rows;
|
||||
final int rowBitWidth;
|
||||
/** CFW cached-image bytes: [w][cellHeight][RLE(w*cellHeight px, ink=15)]. */
|
||||
/** AA coverage, width*inkHeight nibble values 0..15; null for 1bpp glyphs. */
|
||||
final byte[] coverage;
|
||||
/** CFW cached-image bytes: [w][cellHeight][RLE(w*cellHeight px)]. */
|
||||
public final byte[] cachedBytes;
|
||||
|
||||
Glyph(int width, int cellHeight, int inkTop, int inkHeight, int bbxX, int[] rows) {
|
||||
Glyph(int width, int cellHeight, int inkTop, int inkHeight, int bbxX,
|
||||
int[] rows, byte[] coverage) {
|
||||
this.width = width;
|
||||
this.cellHeight = cellHeight;
|
||||
this.inkTop = inkTop;
|
||||
this.inkHeight = inkHeight;
|
||||
this.bbxX = bbxX;
|
||||
this.rows = rows;
|
||||
this.coverage = coverage;
|
||||
this.rowBitWidth = ((width + 7) >> 3) << 3;
|
||||
this.cachedBytes = encodeCachedImage();
|
||||
}
|
||||
|
||||
/** Whether the cell pixel at (col, row) is ink (row is cell-relative). */
|
||||
public boolean inkAt(int col, int row) {
|
||||
if (col < 0 || col >= width) return false;
|
||||
/**
|
||||
* The cell pixel's 4bpp source value at (col, row), row cell-relative.
|
||||
* 1bpp glyphs are stored at 15 so the draw-time LUT (source*top/15)
|
||||
* maps them to exactly the requested top color; AA glyphs carry their
|
||||
* true coverage level.
|
||||
*/
|
||||
public int nibbleAt(int col, int row) {
|
||||
if (col < 0 || col >= width) return 0;
|
||||
int inkRow = row - inkTop;
|
||||
if (inkRow < 0 || inkRow >= inkHeight) return false;
|
||||
return ((rows[inkRow] >>> (rowBitWidth - 1 - col)) & 1) != 0;
|
||||
if (inkRow < 0 || inkRow >= inkHeight) return 0;
|
||||
if (coverage != null) return coverage[inkRow * width + col];
|
||||
return ((rows[inkRow] >>> (rowBitWidth - 1 - col)) & 1) != 0 ? 15 : 0;
|
||||
}
|
||||
|
||||
/** Whether the cell pixel at (col, row) has any ink (row is cell-relative). */
|
||||
public boolean inkAt(int col, int row) {
|
||||
return nibbleAt(col, row) != 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* The firmware cached-image encoding: [width][height][RLE tokens]
|
||||
* covering exactly width*cellHeight pixels (no row padding), ink pixels
|
||||
* as color 15 so the draw-time LUT (top-color scaling) maps them to any
|
||||
* requested 4bpp level.
|
||||
* covering exactly width*cellHeight pixels (no row padding), each
|
||||
* pixel's stored color being its nibbleAt value.
|
||||
*/
|
||||
private byte[] encodeCachedImage() {
|
||||
int total = width * cellHeight;
|
||||
@@ -84,9 +100,9 @@ public final class GlyphAtlas {
|
||||
int o = 2;
|
||||
int i = 0;
|
||||
while (i < total) {
|
||||
int color = inkAt(i % width, i / width) ? 15 : 0;
|
||||
int color = nibbleAt(i % width, i / width);
|
||||
int j = i + 1;
|
||||
while (j < total && (inkAt(j % width, j / width) ? 15 : 0) == color) j++;
|
||||
while (j < total && nibbleAt(j % width, j / width) == color) j++;
|
||||
int run = j - i;
|
||||
while (run > 0) {
|
||||
int c = Math.min(run, 0xffff);
|
||||
@@ -183,7 +199,66 @@ public final class GlyphAtlas {
|
||||
long k = key(id, encoding);
|
||||
if (!glyphs.containsKey(k) && width > 0 && cellHeight > 0
|
||||
&& inkTop + inkHeight <= cellHeight) {
|
||||
glyphs.put(k, new Glyph(width, cellHeight, inkTop, inkHeight, bbxX, rows));
|
||||
glyphs.put(k, new Glyph(width, cellHeight, inkTop, inkHeight, bbxX,
|
||||
rows, null));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a batch of antialiased (4bpp) glyph rasters, e.g. TTF renders.
|
||||
* Same font-group framing as register(), but each glyph record carries
|
||||
* packed coverage instead of 1bpp rows:
|
||||
* [encoding u32][bbxX s8][inkTop u8][width u8][inkHeight u8]
|
||||
* [inkHeight x ceil(width/2) packed bytes, high nibble = left pixel]
|
||||
* Nibble values are the final 4bpp source levels (the draw-time LUT
|
||||
* scales them by the top color). Already-registered (font, encoding)
|
||||
* records are skipped, as in register().
|
||||
*/
|
||||
public static void registerAa(ByteBuffer buffer) {
|
||||
if (buffer == null) return;
|
||||
ByteBuffer in = buffer.order(ByteOrder.LITTLE_ENDIAN);
|
||||
synchronized (lock) {
|
||||
while (in.remaining() > 0) {
|
||||
int keyLen = in.get() & 0xff;
|
||||
byte[] keyBytes = new byte[keyLen];
|
||||
in.get(keyBytes);
|
||||
String fontKey = new String(keyBytes, StandardCharsets.UTF_8);
|
||||
int cellHeight = in.get() & 0xff;
|
||||
int count = in.getShort() & 0xffff;
|
||||
Integer idBoxed = fontIds.get(fontKey);
|
||||
int id;
|
||||
if (idBoxed == null) {
|
||||
id = nextFontId++;
|
||||
fontIds.put(fontKey, id);
|
||||
} else {
|
||||
id = idBoxed;
|
||||
}
|
||||
for (int g = 0; g < count; g++) {
|
||||
int encoding = in.getInt();
|
||||
int bbxX = in.get();
|
||||
int inkTop = in.get() & 0xff;
|
||||
int width = in.get() & 0xff;
|
||||
int inkHeight = in.get() & 0xff;
|
||||
int stride = (width + 1) >> 1;
|
||||
byte[] coverage = new byte[width * inkHeight];
|
||||
for (int r = 0; r < inkHeight; r++) {
|
||||
for (int b = 0; b < stride; b++) {
|
||||
int packed = in.get() & 0xff;
|
||||
int col = b * 2;
|
||||
coverage[r * width + col] = (byte) (packed >> 4);
|
||||
if (col + 1 < width) {
|
||||
coverage[r * width + col + 1] = (byte) (packed & 0x0f);
|
||||
}
|
||||
}
|
||||
}
|
||||
long k = key(id, encoding);
|
||||
if (!glyphs.containsKey(k) && width > 0 && cellHeight > 0
|
||||
&& inkTop + inkHeight <= cellHeight) {
|
||||
glyphs.put(k, new Glyph(width, cellHeight, inkTop, inkHeight, bbxX,
|
||||
null, coverage));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,6 +61,15 @@ public final class TextureCacheState {
|
||||
return generation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bytes currently allocated out of CACHE_SIZE, for logging/tuning (a
|
||||
* sudden drop in the frame log means a reset-on-full re-upload cycle —
|
||||
* the signal that the 64 KiB budget is being outgrown).
|
||||
*/
|
||||
public int usedBytes() {
|
||||
return allocEnd;
|
||||
}
|
||||
|
||||
/** The font's 96-entry table offset in the cache, or -1 when it exists but this glyph can't fit. */
|
||||
public int fontTableOffset(int fontId) {
|
||||
Integer existing = fontTableOffsets.get(fontId);
|
||||
|
||||
@@ -457,8 +457,10 @@ public final class TexturePlanner {
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether every in-panel ink pixel of the glyph equals `top` in the packed
|
||||
* new frame — i.e. the draw's writes all land correct. Ink outside the
|
||||
* Whether every in-panel ink pixel of the glyph lands correct in the
|
||||
* packed new frame: the composite's 4bpp value must equal the firmware's
|
||||
* LUT output source*top/15 (integer). For 1bpp glyphs (source 15) this is
|
||||
* exactly `== top`; AA glyphs get the general check. Ink outside the
|
||||
* panel is clipped by the firmware and irrelevant.
|
||||
*/
|
||||
private static boolean glyphMatchesComposite(byte[] packed, int stride, int width, int height,
|
||||
@@ -467,10 +469,11 @@ public final class TexturePlanner {
|
||||
int y = lineY + row;
|
||||
if (y < 0 || y >= height) continue;
|
||||
for (int col = 0; col < atlas.width; col++) {
|
||||
if (!atlas.inkAt(col, row)) continue;
|
||||
int source = atlas.nibbleAt(col, row);
|
||||
if (source == 0) continue;
|
||||
int x = gx + col;
|
||||
if (x < 0 || x >= width) continue;
|
||||
if (nibbleAt(packed, stride, x, y) != top) {
|
||||
if (nibbleAt(packed, stride, x, y) != (source * top) / 15) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user