include: one folder per subsystem, and includes that name it
Sixty-five headers sat side by side in include/, so `#include "irq.h"` said nothing about where irq.h was or what it belonged to, and errno.h, stat.h and fcntl.h at the top shadowed the system's for any host tool built with -I./include. They are in folders now -- arch/ core/ fs/ dev/ gfx/ net/ uapi/ (and ldls/ for the Linux driver layer) -- and every include names the folder, as Linux's do: "core/proc.h". Done mechanically and checked the hard way: the kernel built from this tree disassembles identically to the one built from the tree before it. The full image boots, and userland is the same set of programs. Paths in docs and comments follow; docs/guides/layout.md has a table of what is where, and tools/fix-includes.py rewrites old-style includes from the folders in the tree, for code written before this. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01S1giNvrHTrPMPw7V6x1kQ4
This commit is contained in:
parent
0e39e711f1
commit
a1c965da1e
267 changed files with 1560 additions and 1440 deletions
106
include/gfx/text.h
Normal file
106
include/gfx/text.h
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
#pragma once
|
||||
|
||||
// Text rendering.
|
||||
//
|
||||
// The console used to draw text by indexing `font_data[256][16]` and stepping
|
||||
// the cursor eight pixels. That is a *character cell grid*, and it was the
|
||||
// thing in the way -- not the font. A grid like that cannot do variable-width
|
||||
// glyphs, cannot position text anywhere except on a multiple of eight, cannot
|
||||
// anti-alias, and cannot represent a codepoint above 255. Swapping the bitmap
|
||||
// for a nicer bitmap changed none of that.
|
||||
//
|
||||
// None of it is true any more, and the console draws through this file. What
|
||||
// remains a grid is `vt.c`'s model of a terminal, which is not the same claim:
|
||||
// a terminal *is* rows and columns -- VT100 escapes address them and
|
||||
// TIOCGWINSZ reports them -- and it reaches this renderer by handing it one
|
||||
// cell at a time, as a surface.
|
||||
//
|
||||
// So this is a layer, not a font. Three things it fixes:
|
||||
//
|
||||
// *Glyphs have metrics.* A glyph knows its own size, its bearing and how far
|
||||
// to advance afterwards. That is what variable-width text is; everything else
|
||||
// follows from it.
|
||||
//
|
||||
// *It draws into a surface, not into the screen.* A compositor draws into an
|
||||
// off-screen buffer and presents it. Rendering that writes straight to the
|
||||
// VGA aperture is unusable to it, which is why this takes a target.
|
||||
//
|
||||
// *The font is behind an interface.* The bitmap font is one implementation.
|
||||
// A TrueType rasteriser is another, and arrives without any caller changing:
|
||||
// glyphs carry 8-bit coverage, so an anti-aliased outline renderer already
|
||||
// has somewhere to put its output.
|
||||
|
||||
struct font;
|
||||
|
||||
// Somewhere to draw. Deliberately not a `struct device` or a framebuffer: the
|
||||
// same code has to work on the virtio-gpu back buffer, on a window's own
|
||||
// buffer, and on the VGA aperture, and none of those know about each other.
|
||||
struct surface {
|
||||
void *pixels; // 0x00RRGGBB, at `bpp` bits each
|
||||
uint32 stride; // bytes per row, not pixels -- they differ on real hardware
|
||||
int width, height;
|
||||
// 32 nearly always: a UEFI GOP has no other format and the loader publishes
|
||||
// 32 unconditionally. 24 and 16 are here because a framebuffer is allowed to
|
||||
// be either and a glyph drawn at the wrong depth is not a subtle failure.
|
||||
// Zero means 32, so a caller that does not care does not have to say.
|
||||
int bpp;
|
||||
};
|
||||
|
||||
// A rectangle of a surface, as a surface of its own.
|
||||
//
|
||||
// Which is how a terminal cell is expressed: everything that draws into it is
|
||||
// then clipped to it by the same bounds check that clips to a screen, with no
|
||||
// second notion of clipping anywhere. A glyph with a tall ascender cannot
|
||||
// bleed into the line above, because as far as the renderer is concerned the
|
||||
// line above is off the edge of the world.
|
||||
struct surface surface_sub(const struct surface *s, int x, int y, int w, int h);
|
||||
|
||||
// One rendered glyph. `cov` is 8-bit coverage, not a bitmap: 0 is background,
|
||||
// 255 is solid, and everything between is an anti-aliased edge. A bitmap font
|
||||
// fills it with 0 and 255 only, and costs nothing for the generality.
|
||||
struct glyph {
|
||||
const unsigned char *cov; // w*h bytes, row-major
|
||||
int w, h; // size of the coverage bitmap
|
||||
int bearing_x, bearing_y; // offset from the pen to the top-left of `cov`
|
||||
int advance; // how far the pen moves afterwards
|
||||
};
|
||||
|
||||
struct font_ops {
|
||||
// Fill `out` for `cp`. Returns 0, or -1 if the font has no such glyph --
|
||||
// callers substitute rather than failing, because a missing glyph must not
|
||||
// stop a line of text.
|
||||
int (*glyph)(struct font *, uint32 cp, struct glyph *out);
|
||||
int (*height)(struct font *); // line height
|
||||
int (*baseline)(struct font *); // pen offset from the top of the line
|
||||
};
|
||||
|
||||
struct font {
|
||||
const struct font_ops *ops;
|
||||
void *priv;
|
||||
};
|
||||
|
||||
// The built-in bitmap font, as a font provider. Always available, needs no
|
||||
// allocation, and is what the console falls back to before any filesystem
|
||||
// exists to load a real typeface from.
|
||||
struct font *font_builtin(void);
|
||||
|
||||
// Draw one UTF-8 string. Returns the x advance.
|
||||
//
|
||||
// `x, y` is the pen position on the *baseline*, which is how every text API
|
||||
// that is not a terminal works -- glyphs of different heights then sit on a
|
||||
// common line instead of being top-aligned.
|
||||
int text_draw(struct surface *, struct font *, int x, int y,
|
||||
const char *utf8, uint32 color);
|
||||
|
||||
// Width the same string would occupy, without drawing it. Needed before you
|
||||
// draw, for centring and for laying out anything at all.
|
||||
int text_measure(struct font *, const char *utf8);
|
||||
|
||||
// One glyph, for callers doing their own layout.
|
||||
int text_draw_glyph(struct surface *, struct font *, int x, int y,
|
||||
uint32 cp, uint32 color);
|
||||
|
||||
// Decode one UTF-8 sequence. Returns the codepoint and advances *p.
|
||||
// Malformed input yields U+FFFD and consumes one byte, so a decoder loop
|
||||
// always terminates.
|
||||
uint32 utf8_next(const char **p);
|
||||
Loading…
Reference in a new issue