font: the console draws outlines now, and a byte could not have held Ą
"Ą Ę Ś Ł Ż" drew as "?????" and the obvious diagnosis -- the font has no glyphs for them -- was wrong twice over. `struct vtcell.ch` was **one byte** holding a CP437 index, and the console mapped UTF-8 down to it on the way in. U+0104 does not fit in a byte. No font could have fixed that; the cell had to get wider first. So: `ch` is a uint32 codepoint, the mapping on the way in is gone, and the cell draws through kernel/dev/ttf.c against JetBrains Mono linked into the image. Cell size comes from the font's measured advance instead of a hardcoded 8x16. The grid costs 864 KB of BSS instead of 432 KB, which is the honest price of holding a codepoint per cell for six terminals at 240x75. The rasteriser needed three fixes before it could be trusted with this. **21 KB of locals on a 4 KB kernel stack.** `ttf_render` held an outline (512 points as three arrays) and a raster (1024 edges) as automatics. KSTACKSIZE is 4096. They walked off the bottom of the stack into whatever was underneath, which surfaced as user processes faulting on addresses they never touched and then a panic inside freevm() on an overwritten page directory. Nothing pointed anywhere near a font. **And then again, recursively.** load_outline() held a 4736-byte outline as a local *and recursed* for composite glyphs. Composite glyphs are exactly Ą, Ę, Ś, Ł, Ż -- so the second crash arrived the moment the first fix let Unicode reach the screen, looking like a fresh bug. Now a static outline per recursion level with a depth cap of 4 (the font's deepest real nesting is 2), and the leaf parse split into load_simple() so its 512-byte flag array is not multiplied by the depth. Worst chain measured from the object file: ~2.3 KB. Neither showed in tools/ttftest, which has an 8 MB stack. That is the standing hazard of one source file built for host and kernel: the host is most forgiving about exactly the resource the kernel is strictest about. **Component transforms were being skipped**, on the stated theory that a wrong transform is worse than none. It is not. `‘` in this font is the comma glyph with a scale of -1 -- a 180-degree rotation -- and the offset that ships with it only lands correctly after the rotation. Without it the mark went twelve pixels into a ten-pixel cell and was clipped away entirely, so the character read as missing from the font rather than as misplaced. F2Dot14 2x2 now, with SCALED_COMPONENT_OFFSET honoured. The glyph cache was ASCII-only, so every accented letter re-rasterised on every repaint: a screenful of Polish did hundreds of bezier flattenings per scroll where the same screenful of English did none. 95 reserved ASCII slots plus 224 shared slots keyed by codepoint, 64 KB pool. Verified 13680 cached renders byte-identical to direct ones across collisions and a pool flush. tools/ttftest speaks UTF-8 now. Iterating bytes meant it could only ever ask for ASCII, and ASCII is entirely simple glyphs -- it could not reach the code path that mattered. Also: resolvconf's success line moves behind /proc/bootmode, same rule as vgpud. A quiet boot opened with "resolvconf: nameserver 10.0.2.3" where the one line was supposed to be "Welcome to SchOS!". Failures stay loud. Verified on screen, not just on the host: Ą Ę Ś Ł Ż ć ń ó ź, Ω λ π Œ Ð Æ ß, ∞ ‘x’ → ≤ all render, panic=0 across four boots. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
ca28978995
commit
5618164754
11 changed files with 568 additions and 132 deletions
153
kernel/dev/gfx.c
153
kernel/dev/gfx.c
|
|
@ -22,12 +22,37 @@
|
|||
#include "display.h"
|
||||
#include "gfx.h"
|
||||
#include "vt.h"
|
||||
#include "text.h"
|
||||
#include "ttf.h"
|
||||
|
||||
#define BACKSPACE 0x100
|
||||
#define CHAR_HEIGHT 16
|
||||
#define CHAR_WIDTH 8
|
||||
#define COLOR_BG SCH_BG
|
||||
|
||||
// The cell size, and why it is not a constant any more.
|
||||
//
|
||||
// It was 8x16, the size of the built-in bitmap font, and everything in this
|
||||
// file multiplied by it. That font is CP437: 256 glyphs, no Ą, no Ę, no Ś, no
|
||||
// Ω, no ∞. Those did not come out wrong, they came out as `?`, because the
|
||||
// grid could not hold a codepoint above 255 and mapped what it could not keep.
|
||||
//
|
||||
// So the console draws from a real typeface now, and a real typeface decides
|
||||
// its own metrics. These start at the bitmap's size because the console has to
|
||||
// work from the first panic onwards -- before gfx_font_init() has run there is
|
||||
// no font, and 8x16 against font_data is exactly what there always was.
|
||||
static int cell_w = 8;
|
||||
static int cell_h = 16;
|
||||
|
||||
#define CHAR_WIDTH cell_w
|
||||
#define CHAR_HEIGHT cell_h
|
||||
|
||||
// The typeface, once there is one. `cfont` is the switch: null means fall back
|
||||
// to the bitmap, which is what early boot and any font failure get.
|
||||
static struct ttf consttf;
|
||||
static struct font *cfont;
|
||||
static int cfont_baseline;
|
||||
|
||||
static uint32 blend(uint32 bg, uint32 fg, uint a);
|
||||
|
||||
// The scanout, as published by whichever driver owns the display. Zeroed until
|
||||
// one does, and every entry point checks `fb` before touching memory -- a
|
||||
// kernel that panics before any display driver has run still has to be able to
|
||||
|
|
@ -222,21 +247,131 @@ gfx_putc(int c, uint32 color)
|
|||
// right for a console that has one background and wrong for a terminal where
|
||||
// every cell carries its own.
|
||||
void
|
||||
gfx_draw_cell(int col, int row, uchar glyph, uint32 fg, uint32 bg)
|
||||
gfx_draw_cell(int col, int row, uint32 cp, uint32 fg, uint32 bg)
|
||||
{
|
||||
if (!fb)
|
||||
return;
|
||||
|
||||
int x = col * CHAR_WIDTH, y = row * CHAR_HEIGHT;
|
||||
int x = col * cell_w, y = row * cell_h;
|
||||
if (x < 0 || y < 0 ||
|
||||
x + CHAR_WIDTH > (int)scanout.width || y + CHAR_HEIGHT > (int)scanout.height)
|
||||
x + cell_w > (int)scanout.width || y + cell_h > (int)scanout.height)
|
||||
return;
|
||||
|
||||
for (int r = 0; r < CHAR_HEIGHT; r++) {
|
||||
uchar bits = font_data[glyph][r];
|
||||
for (int c = 0; c < CHAR_WIDTH; c++)
|
||||
gfx_draw_pixel(x + c, y + r, (bits & (0x80 >> c)) ? fg : bg);
|
||||
// No font yet: the bitmap, exactly as before. Anything it cannot hold is
|
||||
// drawn as a space rather than as the wrong glyph -- `?` was at least honest
|
||||
// about being a substitute, and a CP437 picture of something unrelated is
|
||||
// not.
|
||||
if (!cfont) {
|
||||
uchar g = cp < 256 ? (uchar)cp : ' ';
|
||||
for (int r = 0; r < cell_h; r++) {
|
||||
uchar bits = font_data[g][r];
|
||||
for (int c = 0; c < cell_w; c++)
|
||||
gfx_draw_pixel(x + c, y + r, (bits & (0x80 >> c)) ? fg : bg);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Background first, then the glyph blended over it. The cell is always
|
||||
// filled: a terminal cell owns its rectangle, and leaving the old pixels
|
||||
// where a narrow glyph does not reach is how ghosting gets in.
|
||||
for (int r = 0; r < cell_h; r++)
|
||||
for (int c = 0; c < cell_w; c++)
|
||||
gfx_draw_pixel(x + c, y + r, bg);
|
||||
|
||||
struct glyph gl;
|
||||
if (cp == ' ' || cfont->ops->glyph(cfont, cp, &gl) != 0)
|
||||
return; // no such glyph: the cell stays background
|
||||
|
||||
// `cov` is w*h bytes of coverage, 0..255, positioned by the bearing from the
|
||||
// pen. The pen sits on the baseline at the left of the cell.
|
||||
int gx = x + gl.bearing_x;
|
||||
int gy = y + cfont_baseline - gl.bearing_y;
|
||||
|
||||
for (int r = 0; r < gl.h; r++) {
|
||||
int py = gy + r;
|
||||
if (py < y || py >= y + cell_h)
|
||||
continue; // clip to the cell: a tall glyph must not
|
||||
for (int c = 0; c < gl.w; c++) { // bleed into the line above or below
|
||||
int px = gx + c;
|
||||
if (px < x || px >= x + cell_w)
|
||||
continue;
|
||||
uint a = gl.cov[r * gl.w + c];
|
||||
if (a == 0)
|
||||
continue;
|
||||
gfx_draw_pixel(px, py, a == 255 ? fg : blend(bg, fg, a));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Bring the console typeface up. Called once, early, from the display path.
|
||||
//
|
||||
// The font is linked into the kernel (see CONSFONT in the Makefile) because
|
||||
// the console has to work before there is a filesystem -- a panic during boot
|
||||
// has to be readable.
|
||||
//
|
||||
// A size failure is not fatal and not silent: `cfont` stays null and every
|
||||
// cell keeps coming from the bitmap, which is what this system has always
|
||||
// drawn. A console that lost its font would be a much worse outcome than one
|
||||
// that never had a good one.
|
||||
void
|
||||
gfx_font_init(void)
|
||||
{
|
||||
extern unsigned char _binary_consfont_start[], _binary_consfont_end[];
|
||||
uint32 len = (uint32)(_binary_consfont_end - _binary_consfont_start);
|
||||
struct glyph gl;
|
||||
|
||||
if (cfont)
|
||||
return;
|
||||
// 16 pixels per em. The bitmap it replaces is 16 tall, so the terminal keeps
|
||||
// the number of rows it had and nothing that assumed a screenful has to
|
||||
// change.
|
||||
if (ttf_open(&consttf, _binary_consfont_start, len, 16) != 0)
|
||||
return;
|
||||
|
||||
struct font *f = ttf_font(&consttf);
|
||||
if (!f)
|
||||
return;
|
||||
|
||||
// The cell is as wide as the font's own advance. JetBrains Mono is
|
||||
// monospaced, so measuring one glyph measures all of them -- and measuring
|
||||
// rather than assuming is what keeps this correct if the font is ever
|
||||
// changed for another one.
|
||||
if (f->ops->glyph(f, 'M', &gl) != 0 || gl.advance <= 0)
|
||||
return;
|
||||
|
||||
int w = gl.advance, h = f->ops->height(f);
|
||||
|
||||
// Checked *before* they are stored. Writing them first and returning on a
|
||||
// bad value leaves the console with a cell size no font is drawing at, and
|
||||
// the bitmap fallback reading past its own 16-row glyphs.
|
||||
if (w < 4 || w > 64 || h < 6 || h > 64)
|
||||
return; // implausible metrics: keep the bitmap
|
||||
|
||||
cell_w = w;
|
||||
cell_h = h;
|
||||
cfont_baseline = f->ops->baseline(f);
|
||||
cfont = f;
|
||||
}
|
||||
|
||||
// **Nothing here prints.** gfx_font_init() runs before vt_init(), because
|
||||
// vt_init() has to know how big a cell is before it can work out how many fit
|
||||
// on the screen -- and by then cons_serial_only may already be clear, so a
|
||||
// cprintf() would reach vt_current() before there is a current terminal. That
|
||||
// is a fault during boot with no console to report it on, which is a
|
||||
// spectacularly unhelpful way to fail.
|
||||
//
|
||||
// So main() asks afterwards and says it then.
|
||||
int
|
||||
gfx_font_ready(void)
|
||||
{
|
||||
return cfont != 0;
|
||||
}
|
||||
|
||||
void
|
||||
gfx_cell_size(int *w, int *h)
|
||||
{
|
||||
if (w) *w = cell_w;
|
||||
if (h) *h = cell_h;
|
||||
}
|
||||
|
||||
// Move the whole screen up one text row and clear the row it exposes. The
|
||||
|
|
|
|||
295
kernel/dev/ttf.c
295
kernel/dev/ttf.c
|
|
@ -176,77 +176,13 @@ struct outline {
|
|||
int npts, ncont;
|
||||
};
|
||||
|
||||
// The simple (non-composite) glyph body, split out from load_outline so that
|
||||
// its flags[] array -- 512 bytes -- lives only in the leaf frame. Composite
|
||||
// glyphs recurse, and a half-kilobyte local multiplied by the recursion depth
|
||||
// is not affordable on a 4 KB kernel stack.
|
||||
static int
|
||||
load_outline(struct ttf *f, int gid, struct outline *o)
|
||||
load_simple(const uint8 *g, int nc, struct outline *o)
|
||||
{
|
||||
uint32 g0, g1;
|
||||
|
||||
if (gid < 0 || gid >= f->num_glyphs)
|
||||
return -1;
|
||||
if (f->long_loca) {
|
||||
g0 = rd32(f->data + f->loca + 4 * gid);
|
||||
g1 = rd32(f->data + f->loca + 4 * gid + 4);
|
||||
} else {
|
||||
g0 = (uint32)rd16(f->data + f->loca + 2 * gid) * 2;
|
||||
g1 = (uint32)rd16(f->data + f->loca + 2 * gid + 2) * 2;
|
||||
}
|
||||
o->npts = o->ncont = 0;
|
||||
if (g0 >= g1)
|
||||
return 0; // empty glyph, e.g. space -- not an error
|
||||
|
||||
const uint8 *g = f->data + f->glyf + g0;
|
||||
int nc = rs16(g);
|
||||
|
||||
// A negative contour count means a composite: the glyph is other glyphs
|
||||
// placed at offsets. This is not an exotic case to skip -- `i` is built from
|
||||
// a dotless i plus the dot, so skipping composites loses a letter from
|
||||
// "SchOS", which is exactly how the gap was found.
|
||||
//
|
||||
// Only the translated case is handled: components with a scale or a 2x2
|
||||
// matrix are rare outside of synthesised obliques, and applying a transform
|
||||
// wrongly is worse than not applying one.
|
||||
if (nc < 0) {
|
||||
const uint8 *p = g + 10;
|
||||
int flags;
|
||||
do {
|
||||
flags = rd16(p);
|
||||
int cgid = rd16(p + 2);
|
||||
p += 4;
|
||||
|
||||
int dx, dy;
|
||||
if (flags & 0x0001) { // ARG_1_AND_2_ARE_WORDS
|
||||
dx = rs16(p); dy = rs16(p + 2); p += 4;
|
||||
} else {
|
||||
dx = (signed char)p[0]; dy = (signed char)p[1]; p += 2;
|
||||
}
|
||||
if (!(flags & 0x0002)) // ARGS_ARE_XY_VALUES
|
||||
dx = dy = 0; // point matching: not supported
|
||||
|
||||
if (flags & 0x0008) p += 2; // WE_HAVE_A_SCALE
|
||||
else if (flags & 0x0040) p += 4; // X_AND_Y_SCALE
|
||||
else if (flags & 0x0080) p += 8; // TWO_BY_TWO
|
||||
|
||||
// Recurse, then merge the component's contours into ours with the offset
|
||||
// applied. Depth is bounded by the recursion into load_outline, which
|
||||
// rejects anything that would overflow the point arrays.
|
||||
struct outline sub;
|
||||
if (load_outline(f, cgid, &sub) != 0)
|
||||
continue;
|
||||
if (o->npts + sub.npts > MAXPTS || o->ncont + sub.ncont > MAXCONT)
|
||||
break;
|
||||
for (int i = 0; i < sub.npts; i++) {
|
||||
o->x[o->npts + i] = sub.x[i] + dx;
|
||||
o->y[o->npts + i] = sub.y[i] + dy;
|
||||
o->on[o->npts + i] = sub.on[i];
|
||||
}
|
||||
for (int i = 0; i < sub.ncont; i++)
|
||||
o->ends[o->ncont + i] = sub.ends[i] + o->npts;
|
||||
o->npts += sub.npts;
|
||||
o->ncont += sub.ncont;
|
||||
} while (flags & 0x0020); // MORE_COMPONENTS
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (nc > MAXCONT)
|
||||
return -1;
|
||||
o->ncont = nc;
|
||||
|
|
@ -305,6 +241,147 @@ load_outline(struct ttf *f, int gid, struct outline *o)
|
|||
return 0;
|
||||
}
|
||||
|
||||
#define F2DOT14_ONE 16384
|
||||
|
||||
// One row of a 2x2 F2Dot14 transform applied to a point, rounded to nearest
|
||||
// rather than truncated -- at 16 px a font unit is a sixteenth of a pixel, and
|
||||
// truncating every component point biases the whole mark down and left by
|
||||
// enough to see.
|
||||
//
|
||||
// long, not int: a coordinate can reach the em square's bounds and a matrix
|
||||
// entry reach two, so each product is comfortably inside 32 bits but the sum
|
||||
// of two is not guaranteed to be.
|
||||
static int
|
||||
f2d14(int m0, int v0, int m1, int v1)
|
||||
{
|
||||
long acc = (long)m0 * v0 + (long)m1 * v1;
|
||||
return (int)((acc + (acc >= 0 ? F2DOT14_ONE / 2 : -(F2DOT14_ONE / 2)))
|
||||
/ F2DOT14_ONE);
|
||||
}
|
||||
|
||||
// Composite glyphs nest: an accented letter is a base plus a mark, and the
|
||||
// mark can itself be a composite. Real fonts stay within two or three levels,
|
||||
// so cap it and refuse rather than let a malformed or hostile font drive the
|
||||
// recursion arbitrarily deep.
|
||||
#define MAXDEPTH 4
|
||||
|
||||
// One scratch outline per recursion level, allocated once. As a local this was
|
||||
// 4736 bytes per frame in a recursive function, which overflowed the kernel
|
||||
// stack the moment anything composite was drawn -- and composites are exactly
|
||||
// A-ogonek, E-ogonek, S-acute and the rest of the Polish alphabet, so the
|
||||
// crash only appeared once non-ASCII text reached the console.
|
||||
//
|
||||
// Static for the same reason and under the same rule as the buffers in
|
||||
// ttf_render: every caller is serialised behind cons.lock.
|
||||
static struct outline compose[MAXDEPTH];
|
||||
|
||||
static int
|
||||
load_outline_at(struct ttf *f, int gid, struct outline *o, int depth)
|
||||
{
|
||||
uint32 g0, g1;
|
||||
|
||||
if (gid < 0 || gid >= f->num_glyphs)
|
||||
return -1;
|
||||
if (f->long_loca) {
|
||||
g0 = rd32(f->data + f->loca + 4 * gid);
|
||||
g1 = rd32(f->data + f->loca + 4 * gid + 4);
|
||||
} else {
|
||||
g0 = (uint32)rd16(f->data + f->loca + 2 * gid) * 2;
|
||||
g1 = (uint32)rd16(f->data + f->loca + 2 * gid + 2) * 2;
|
||||
}
|
||||
o->npts = o->ncont = 0;
|
||||
if (g0 >= g1)
|
||||
return 0; // empty glyph, e.g. space -- not an error
|
||||
|
||||
const uint8 *g = f->data + f->glyf + g0;
|
||||
int nc = rs16(g);
|
||||
|
||||
// A negative contour count means a composite: the glyph is other glyphs
|
||||
// placed at offsets. This is not an exotic case to skip -- `i` is built from
|
||||
// a dotless i plus the dot, so skipping composites loses a letter from
|
||||
// "SchOS", which is exactly how the gap was found.
|
||||
//
|
||||
// The component transform is applied, not ignored. An earlier version read
|
||||
// the offset and stepped over the scale, on the theory that a wrong
|
||||
// transform is worse than none. It is not: a left single quote in this font
|
||||
// is the comma glyph with a scale of -1, which is a 180-degree rotation, and
|
||||
// the offset that goes with it only lands in the right place once the
|
||||
// rotation has happened. Dropping the scale put the mark twelve pixels into
|
||||
// a ten-pixel cell -- clipped away entirely, so the character read as
|
||||
// missing from the font rather than as misplaced.
|
||||
if (nc < 0) {
|
||||
if (depth >= MAXDEPTH)
|
||||
return -1;
|
||||
struct outline *sub = &compose[depth];
|
||||
const uint8 *p = g + 10;
|
||||
int flags;
|
||||
do {
|
||||
flags = rd16(p);
|
||||
int cgid = rd16(p + 2);
|
||||
p += 4;
|
||||
|
||||
int dx, dy;
|
||||
if (flags & 0x0001) { // ARG_1_AND_2_ARE_WORDS
|
||||
dx = rs16(p); dy = rs16(p + 2); p += 4;
|
||||
} else {
|
||||
dx = (signed char)p[0]; dy = (signed char)p[1]; p += 2;
|
||||
}
|
||||
if (!(flags & 0x0002)) // ARGS_ARE_XY_VALUES
|
||||
dx = dy = 0; // point matching: not supported
|
||||
|
||||
// The 2x2 in F2Dot14: a fixed-point format with two integer bits and
|
||||
// fourteen fractional, so the unit matrix is 16384 and -1 is -16384.
|
||||
// Identity unless the component says otherwise.
|
||||
int a = F2DOT14_ONE, b = 0, c = 0, d = F2DOT14_ONE;
|
||||
if (flags & 0x0008) { // WE_HAVE_A_SCALE
|
||||
a = d = rs16(p); p += 2;
|
||||
} else if (flags & 0x0040) { // X_AND_Y_SCALE
|
||||
a = rs16(p); d = rs16(p + 2); p += 4;
|
||||
} else if (flags & 0x0080) { // TWO_BY_TWO
|
||||
a = rs16(p); b = rs16(p + 2);
|
||||
c = rs16(p + 4); d = rs16(p + 6); p += 8;
|
||||
}
|
||||
|
||||
// Whether the offset is itself transformed. Neither bit set means
|
||||
// unscaled, which is what Microsoft's rasteriser does and what every
|
||||
// font in practice is drawn against.
|
||||
if (flags & 0x0800) { // SCALED_COMPONENT_OFFSET
|
||||
int tx = dx, ty = dy;
|
||||
dx = f2d14(a, tx, c, ty);
|
||||
dy = f2d14(b, tx, d, ty);
|
||||
}
|
||||
|
||||
// Recurse into the next level's scratch outline, then merge its contours
|
||||
// into ours with the transform applied. `sub` is only read back here,
|
||||
// and the callee only ever touches levels deeper than this one, so the
|
||||
// two never alias.
|
||||
if (load_outline_at(f, cgid, sub, depth + 1) != 0)
|
||||
continue;
|
||||
if (o->npts + sub->npts > MAXPTS || o->ncont + sub->ncont > MAXCONT)
|
||||
break;
|
||||
for (int i = 0; i < sub->npts; i++) {
|
||||
int X = sub->x[i], Y = sub->y[i];
|
||||
o->x[o->npts + i] = f2d14(a, X, c, Y) + dx;
|
||||
o->y[o->npts + i] = f2d14(b, X, d, Y) + dy;
|
||||
o->on[o->npts + i] = sub->on[i];
|
||||
}
|
||||
for (int i = 0; i < sub->ncont; i++)
|
||||
o->ends[o->ncont + i] = sub->ends[i] + o->npts;
|
||||
o->npts += sub->npts;
|
||||
o->ncont += sub->ncont;
|
||||
} while (flags & 0x0020); // MORE_COMPONENTS
|
||||
return 0;
|
||||
}
|
||||
|
||||
return load_simple(g, nc, o);
|
||||
}
|
||||
|
||||
static int
|
||||
load_outline(struct ttf *f, int gid, struct outline *o)
|
||||
{
|
||||
return load_outline_at(f, gid, o, 0);
|
||||
}
|
||||
|
||||
// --- rasterising -----------------------------------------------------------
|
||||
//
|
||||
// Scanline fill with the non-zero winding rule, four sub-scanlines per output
|
||||
|
|
@ -359,8 +436,25 @@ int
|
|||
ttf_render(struct ttf *f, uint32 cp, struct glyph *out, unsigned char *cov,
|
||||
int covcap, int *ox, int *oy)
|
||||
{
|
||||
struct outline o;
|
||||
struct raster r;
|
||||
// **Static, not automatic.** Together these are about 21 KB -- an outline
|
||||
// holds 512 points as three arrays, and a raster holds 1024 edges -- and
|
||||
// KSTACKSIZE is 4096. As locals they walked straight off the bottom of the
|
||||
// kernel stack and into whatever was underneath, which showed up as
|
||||
// user processes faulting at addresses they never touched and then a panic
|
||||
// in freevm() on a page directory that had been overwritten. None of it
|
||||
// pointed anywhere near a font.
|
||||
//
|
||||
// It never showed in the host harness, where the same code runs on an 8 MB
|
||||
// stack. That is the whole hazard of a rasteriser that builds host-side and
|
||||
// kernel-side from one source: the host is forgiving about exactly the
|
||||
// resource the kernel is strictest about.
|
||||
//
|
||||
// Safe as statics because every caller is serialised: the console path holds
|
||||
// cons.lock across a whole message, and the host harness is single-threaded.
|
||||
// If a second thread ever rasterises concurrently, these have to become a
|
||||
// caller-supplied scratch buffer rather than quietly go back on the stack.
|
||||
static struct outline o;
|
||||
static struct raster r;
|
||||
int gid = glyph_index(f, cp);
|
||||
|
||||
if (gid == 0 && cp != 0)
|
||||
|
|
@ -502,22 +596,39 @@ ttf_render(struct ttf *f, uint32 cp, struct glyph *out, unsigned char *cov,
|
|||
// worse: the single scratch buffer meant a glyph was only valid until the next
|
||||
// call, so a caller could not hold two at once.
|
||||
//
|
||||
// Direct-mapped over printable ASCII, which is the case that matters and needs
|
||||
// no eviction policy, no hashing and no allocation. Anything outside that range
|
||||
// still renders, just uncached, through the scratch buffer.
|
||||
// Two regions, both direct-mapped and neither needing hashing or allocation.
|
||||
//
|
||||
// The pool is filled linearly and never freed. A cache that can only fill up is
|
||||
// the right shape here because the contents are pure functions of (codepoint,
|
||||
// size) -- nothing ever becomes stale -- and the only event that invalidates it
|
||||
// is a size change, which drops the whole thing.
|
||||
// Printable ASCII gets a reserved slot each, one codepoint per slot, so the
|
||||
// characters that make up nearly every line can never be evicted by anything
|
||||
// else. Everything above ASCII shares a second block of slots keyed by
|
||||
// codepoint modulo the block size. That block was the missing half: with an
|
||||
// ASCII-only cache every accented letter re-rasterised on every redraw, so a
|
||||
// screenful of Polish did hundreds of bezier flattenings per scroll while the
|
||||
// same screenful of English did none.
|
||||
//
|
||||
// The modulus is chosen so that a run of consecutive codepoints lands in
|
||||
// consecutive slots. Latin-1 Supplement and Latin Extended-A together are
|
||||
// under 200 codepoints, so a European text in any one language fits without
|
||||
// collisions; mixing scripts costs collisions, not correctness.
|
||||
//
|
||||
// The pool is filled linearly and never freed. A cache that can only fill up
|
||||
// is the right shape here because the contents are pure functions of
|
||||
// (codepoint, size) -- nothing ever becomes stale. Two events empty it: a size
|
||||
// change, which makes every bitmap wrong, and the pool running out, which is
|
||||
// the price of letting a slot be re-keyed without being able to reclaim the
|
||||
// bytes its previous occupant held. Both are safe because the returned glyph
|
||||
// is only promised to be valid until the next call.
|
||||
|
||||
#define CACHE_LO 32 // space
|
||||
#define CACHE_HI 127 // exclusive
|
||||
#define CACHE_N (CACHE_HI - CACHE_LO)
|
||||
#define CACHE_POOL (48 * 1024)
|
||||
#define ASCII_N (CACHE_HI - CACHE_LO)
|
||||
#define EXTRA_N 224 // shared slots for everything above ASCII
|
||||
#define CACHE_N (ASCII_N + EXTRA_N)
|
||||
#define CACHE_POOL (64 * 1024)
|
||||
|
||||
struct gcache {
|
||||
struct glyph g;
|
||||
uint32 cp; // key; meaningful only when `have`
|
||||
uint32 off; // into pool, valid only when `have`
|
||||
int have;
|
||||
};
|
||||
|
|
@ -545,11 +656,13 @@ ttf_glyph_op(struct font *fo, uint32 cp, struct glyph *out)
|
|||
cache_px = f->px;
|
||||
}
|
||||
|
||||
if (cp < CACHE_LO || cp >= CACHE_HI)
|
||||
// Control characters are not glyphs and would waste a reserved slot each.
|
||||
if (cp < CACHE_LO)
|
||||
return ttf_render(f, cp, out, ttf_cov, (int)sizeof(ttf_cov), 0, 0);
|
||||
|
||||
struct gcache *c = &cache[cp - CACHE_LO];
|
||||
if (c->have) {
|
||||
struct gcache *c = cp < CACHE_HI ? &cache[cp - CACHE_LO]
|
||||
: &cache[ASCII_N + cp % EXTRA_N];
|
||||
if (c->have && c->cp == cp) {
|
||||
*out = c->g;
|
||||
out->cov = cache_pool + c->off; // rebind: the pool may have moved in
|
||||
return 0; // a future revision, the offset has not
|
||||
|
|
@ -560,9 +673,21 @@ ttf_glyph_op(struct font *fo, uint32 cp, struct glyph *out)
|
|||
return -1;
|
||||
|
||||
uint32 need = (uint32)(g.w * g.h);
|
||||
|
||||
// Out of pool. The bytes held by evicted slots cannot be reclaimed
|
||||
// individually, so reclaim all of them at once and start over rather than
|
||||
// degrading to permanently uncached the first time a page of mixed scripts
|
||||
// goes past. Rebuilding costs one rasterisation per glyph still on screen.
|
||||
if (cache_used + need > CACHE_POOL && need <= CACHE_POOL) {
|
||||
for (int i = 0; i < CACHE_N; i++)
|
||||
cache[i].have = 0;
|
||||
cache_used = 0;
|
||||
}
|
||||
|
||||
if (cache_used + need <= CACHE_POOL) {
|
||||
memmove(cache_pool + cache_used, g.cov, need);
|
||||
c->off = cache_used;
|
||||
c->cp = cp;
|
||||
c->g = g;
|
||||
c->g.cov = cache_pool + cache_used; // not the scratch buffer
|
||||
c->have = 1;
|
||||
|
|
@ -571,8 +696,8 @@ ttf_glyph_op(struct font *fo, uint32 cp, struct glyph *out)
|
|||
return 0;
|
||||
}
|
||||
|
||||
// Pool exhausted. Still correct, just uncached -- and the glyph is in the
|
||||
// scratch buffer, which is exactly the old contract.
|
||||
// A single glyph larger than the whole pool. Still correct, just uncached --
|
||||
// and it is in the scratch buffer, which is exactly the old contract.
|
||||
*out = g;
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -375,11 +375,16 @@ vt_putc(struct vt *v, int glyph)
|
|||
// the prompt on every line.
|
||||
if (glyph < 0x20)
|
||||
return;
|
||||
if (glyph < 0 || glyph > 255)
|
||||
glyph = '?';
|
||||
// No upper bound any more. The cell holds a codepoint, so a character the
|
||||
// font may not have is stored as itself and the font decides -- which is
|
||||
// the difference between a terminal that cannot show Ą and one whose font
|
||||
// happens not to. It used to be clamped to 255 and replaced with '?' here,
|
||||
// before any font was ever consulted.
|
||||
if (glyph < 0)
|
||||
return;
|
||||
wrap_now(v); // the previous glyph filled the line
|
||||
struct vtcell *k = cell_at(v, v->cx, v->cy);
|
||||
k->ch = (uint8)glyph;
|
||||
k->ch = (uint32)glyph;
|
||||
k->fg = v->fg;
|
||||
k->bg = v->bg;
|
||||
k->attr = (uint8)v->attr;
|
||||
|
|
@ -802,8 +807,10 @@ vt_feed(struct vt *v, int c)
|
|||
v->u8_cp = (v->u8_cp << 6) | (c & 0x3F);
|
||||
if (--v->u8_need > 0)
|
||||
return;
|
||||
int g = glyph_for(v->u8_cp);
|
||||
vt_putc(v, g ? g : '?');
|
||||
// The codepoint itself, not a CP437 approximation of it. glyph_for() is
|
||||
// still here for the bitmap fallback, but the grid no longer needs it:
|
||||
// what reaches the font is what the program sent.
|
||||
vt_putc(v, (int)v->u8_cp);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue