gfx: one glyph blitter, and a terminal cell is a surface

Gate 0's last item, "delete the cell grid" -- done as far as it should be, and
the rest of it retired, which is the part worth explaining.

include/text.h made the case against the grid in four charges: it cannot do
variable-width glyphs, cannot position text off a multiple of eight, cannot
anti-alias, and cannot hold a codepoint above 255. Every one of those was
about the *drawing layer*, and every one is now false -- glyphs carry metrics,
PSOGM positions text at arbitrary pixels, the TrueType path blends 8-bit
coverage, and vtcell.ch is a uint32.

What is left is a grid in vt.c, and deleting that would not modernise the
console; it would stop it being a terminal. VT100 escapes address columns,
TIOCGWINSZ reports rows and columns, and every program that draws a box
assumes both. So the item is checked with its remainder rewritten to say what
was actually wrong, rather than ticked as if the original wording had been
carried out.

**What was actually wrong was the duplicate.** gfx.c walked font_data and
blitted glyphs its own way; text.c did the same job into a struct surface, for
no caller but a sysrq self-test. Two implementations of bearings, coverage and
clipping, which had to agree about a cell size only one of them was told had
changed -- and they did not agree, which is why every line of PSOGM once drew
the next character's bitmap underneath itself.

There is one now, and the mechanism is the pleasing part: the console hands
text_draw_glyph() **the cell as a surface of its own** -- same pixels, same
stride, origin moved and bounds set to that cell. Clipping a tall ascender to
its line is then the same bounds check that clips to the edge of a screen, and
there is no second notion of clipping in the tree. `surface_sub()` is six
lines and replaced fourteen of hand-written comparisons.

The bitmap is reached through font_builtin() like any other provider, so the
early console and the real one are one path with different metrics, and the
`if (!cfont)` branch that walked the array is gone. gfx.c no longer includes
font.h; text.c does, being the only file left that reads it -- which also
retires the note in text.c explaining why it could not.

struct surface gained a bpp, because gfx_draw_pixel handles 16, 24 and 32 and
whatever replaces it has to as well. Zero means 32: a UEFI GOP has no other
format and the loader publishes 32 unconditionally, so callers that do not
care say nothing.

One behaviour change, and it is the documented one: a codepoint the font
cannot draw is now a substituted glyph rather than a blank cell. text.h argues
for that directly -- "a missing glyph that draws nothing makes text silently
shorter and the fault impossible to see".

Verified by screendump: the console renders correctly, PSOGM renders
correctly, and `echo t > /proc/sysrq` still draws its specimen through the
surface path with the back buffer at 32bpp. Both supported configurations: one
boot each, no traps, no panics, socktest and memfdtest green.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Nexoniarz 2026-09-23 17:57:16 +02:00
commit c84d06c5b1
6 changed files with 183 additions and 72 deletions

View file

@ -5,11 +5,15 @@
#include "param.h"
#include "text.h"
// Declared, not included. include/font.h *defines* font_data rather than
// declaring it, so including it here as well as in vga.c gives two definitions
// of the same array and the link fails. The header is generated data; leaving
// it alone and referring to the symbol is less fragile than editing it.
extern unsigned char font_data[256][16];
// Included, now that this is the only file that reads it.
//
// It used to be `extern unsigned char font_data[256][16];` with a note
// explaining that include/font.h *defines* the array rather than declaring
// it, so a second includer would break the link. The second includer was
// gfx.c, which walked the bitmap itself when there was no TrueType face --
// and that was the duplicate glyph path. With it gone, the header can live
// with its one user.
#include "font.h"
// --- UTF-8 -----------------------------------------------------------------
@ -160,22 +164,69 @@ font_builtin(void)
// Reads the destination, which a bitmap font never needs -- but an
// anti-aliased edge is defined as a blend with what is already there, and a
// renderer that cannot do it is a renderer that cannot draw a real typeface.
static uint32
sur_read(struct surface *s, unsigned char *at)
{
switch (s->bpp) {
case 24: return ((uint32)at[2] << 16) | ((uint32)at[1] << 8) | at[0];
case 16: {
uint16 v = *(uint16 *)at;
return (uint32)(((v >> 11) & 0x1F) << 19) |
(uint32)(((v >> 5) & 0x3F) << 10) |
(uint32)((v & 0x1F) << 3);
}
default: return *(uint32 *)at;
}
}
static void
sur_write(struct surface *s, unsigned char *at, uint32 color)
{
switch (s->bpp) {
case 24:
at[0] = color & 0xFF; at[1] = (color >> 8) & 0xFF; at[2] = (color >> 16) & 0xFF;
return;
case 16:
*(uint16 *)at = (uint16)((((color >> 19) & 0x1F) << 11) |
(((color >> 10) & 0x3F) << 5) |
((color >> 3) & 0x1F));
return;
default:
*(uint32 *)at = color;
return;
}
}
static void
blend_px(struct surface *s, int x, int y, uint32 color, unsigned a)
{
if (x < 0 || y < 0 || x >= s->width || y >= s->height)
return;
uint32 *px = (uint32 *)((unsigned char *)s->pixels + (uint64)y * s->stride) + x;
unsigned char *at = (unsigned char *)s->pixels + (uint64)y * s->stride +
(uint64)x * (s->bpp ? s->bpp / 8 : 4);
if (a >= 255) {
*px = color;
sur_write(s, at, color);
return;
}
uint32 dst = *px;
uint32 dst = sur_read(s, at);
uint32 r = (((color >> 16) & 0xFF) * a + ((dst >> 16) & 0xFF) * (255 - a)) / 255;
uint32 g = (((color >> 8) & 0xFF) * a + ((dst >> 8) & 0xFF) * (255 - a)) / 255;
uint32 b = (((color ) & 0xFF) * a + ((dst ) & 0xFF) * (255 - a)) / 255;
*px = (r << 16) | (g << 8) | b;
sur_write(s, at, (r << 16) | (g << 8) | b);
}
struct surface
surface_sub(const struct surface *s, int x, int y, int w, int h)
{
struct surface out = *s;
int bytes = s->bpp ? s->bpp / 8 : 4;
out.pixels = (unsigned char *)s->pixels + (uint64)y * s->stride +
(uint64)x * bytes;
out.width = w;
out.height = h;
return out;
}
int