gizlang/graphics
Graphics: every drawing of Gizlang in one module, with no operating system call. A Canvas is XRGB8888 pixels at an address the host gives. Shapes (rounded rectangles, rings, shadows, gradients, polygons, strokes) and OpenType text share one coverage rule and one blend. A Scene is a display list of them in float pixels: an engine records it once (GizBrowser's paint, a desktop's shell) and replays it at any scale onto a canvas, a damaged region or a GPU later.
import * as Graphics from "gizlang/graphics";Types
Canvas
type Canvas = { at: i64; width: i64; height: i64; stride: i64; }WIDTH x HEIGHT pixels at AT, rows STRIDE pixels apart: a whole picture, or a view of a rectangle of one.
at:i64width:i64height:i64stride:i64
Rect
type Rect = { x: f64; y: f64; w: f64; h: f64; }A rectangle in pixels; its edges may fall between pixels.
x:f64y:f64w:f64h:f64
Bitmap
type Bitmap = { width: i64; height: i64; pixels: bytes; }A picture in memory: WIDTH x HEIGHT pixels of four bytes each, red, green, blue and straight alpha, row by row.
width:i64height:i64pixels:bytes
Radii
type Radii = { topLeft: f64; topRight: f64; bottomRight: f64; bottomLeft: f64; }The radius of each corner.
topLeft:f64topRight:f64bottomRight:f64bottomLeft:f64
Stop
type Stop = { at: f64; argb: i64; }A color stop: its place on the gradient line (0 to 1) and its ARGB.
at:f64argb:i64
Insets
type Insets = { top: f64; right: f64; bottom: f64; left: f64; }The four sides of a box: border widths.
top:f64right:f64bottom:f64left:f64
Shadow
type Shadow = { dx: f64; dy: f64; blur: f64; spread: f64; argb: i64; }A shadow: its offset, its blur (CSS box-shadow: twice the Gaussian's deviation), its spread and its color.
dx:f64dy:f64blur:f64spread:f64argb:i64
Gradient
type Gradient = { angle: f64; radial: bool; stops: Vec<Stop>; }A gradient: its stops along ANGLE degrees (0 points up, 90 right, as CSS linear-gradient lays its line), or circles from the centre to the farthest corner when RADIAL.
angle:f64radial:boolstops:Vec<gizlang_graphics.Stop>
Font
type Font = { at: i64; }A loaded OpenType font: a record of words at an address, its caches beside it. A negative at is a face of a
terminal's grid (gridFont): -1 - STYLE, with CELL_BOLD and CELL_ITALIC in STYLE.
at:i64
Scene
class Scene { ops: Vec<Op> = []; bitmaps: Vec<Bitmap> = []; constructor() {} fill(rect: Rect, corners: Radii, color: i64): void { this.ops.push(op(OP_FILL, rect, corners, color)); } border(rect: Rect, corners: Radii, widths: Insets, color: i64): void { let o = op(OP_BORDER, rect, corners, color); o.numbers = [widths.top, widths.right, widths.bottom, widths.left]; this.ops.push(o); } shadow(rect: Rect, corners: Radii, s: Shadow): void { let o = op(OP_SHADOW, rect, corners, s.argb); o.shadow = s; this.ops.push(o); } gradient(rect: Rect, corners: Radii, g: Gradient): void { let o = op(OP_GRADIENT, rect, corners, 0); o.gradient = g; this.ops.push(o); } text(font: Font, size: f64, x: f64, baseline: f64, text: string, color: i64, spacing: f64, rtl: bool = false): void { let o = op(OP_TEXT, { x, y: baseline, w: 0.0, h: 0.0 }, SQUARE, color); o.font = font; o.text = text; o.numbers = [size, spacing, rtl ? 1.0: 0.0]; this.ops.push(o); } stroke(points: Vec<f64>, width: f64, color: i64): void { let o = op(OP_STROKE, NOWHERE, SQUARE, color); o.numbers = [width].concat(points); this.ops.push(o); } bitmap(b: Bitmap): i64 { this.bitmaps.push(b); return this.bitmaps.length - 1; } image(id: i64, src: Rect, dst: Rect, corners: Radii = SQUARE): void { let o = op(OP_IMAGE, dst, corners, id); o.numbers = [src.x, src.y, src.w, src.h]; this.ops.push(o); } scales(scale: f64): Vec<f64> { let out: Vec<f64> = new Array(this.bitmaps.length).fill(0.0); let ks: Vec<Vec<f64>> = [[scale, scale]]; for (const o of this.ops) { const t = ks[ks.length - 1]; if (o.kind === OP_TRANSFORM) { ks.push([t[0] * o.numbers[0], t[1] * o.numbers[1]]); continue; } if (o.kind === OP_UNTRANSFORM) { if (ks.length> 1) { ks.pop(); } continue; } const n = o.numbers; if (o.kind !== OP_IMAGE || n[2] <= 0.0 || n[3] <= 0.0) { continue; } const k = max(o.rect.w * (t[0] <0.0 ? -t[0]: t[0]) / n[2], o.rect.h * (t[1] <0.0 ? -t[1]: t[1]) / n[3]); if (k> out[o.color]) { out[o.color] = k; } } return out; } replaceBitmap(id: i64, b: Bitmap): void { const old = this.bitmaps[id]; const fx = old.width> 0 ? f64(b.width) / f64(old.width): 1.0; const fy = old.height> 0 ? f64(b.height) / f64(old.height): 1.0; for (let i = 0; i <this.ops.length; i + = 1) { let o = this.ops[i]; if (o.kind !== OP_IMAGE || o.color !== id) { continue; } const n = o.numbers; o.numbers = [n[0] * fx, n[1] * fy, n[2] * fx, n[3] * fy]; this.ops[i] = o; } this.bitmaps[id] = b; } polygon(xs: Vec<f64>, ys: Vec<f64>, ends: Vec<i64>, color: i64): void { let o = op(OP_POLYGON, NOWHERE, SQUARE, color); let n: Vec<f64> = [f64(ends.length)]; for (const e of ends) { n.push(f64(e)); } for (let i = 0; i <xs.length; i + = 1) { n.push(xs[i]); n.push(ys[i]); } o.numbers = n; this.ops.push(o); } transform(sx: f64, sy: f64, tx: f64, ty: f64): void { let o = op(OP_TRANSFORM, NOWHERE, SQUARE, 0); o.numbers = [sx, sy, tx, ty]; this.ops.push(o); } untransform(): void { this.ops.push(op(OP_UNTRANSFORM, NOWHERE, SQUARE, 0)); } clip(rect: Rect, corners: Radii = SQUARE): void { this.ops.push(op(OP_CLIP, rect, corners, 0)); } unclip(): void { this.ops.push(op(OP_UNCLIP, NOWHERE, SQUARE, 0)); } size(): i64 { return this.ops.length; } replay(canvas: Canvas, scale: f64, scratch: mut Arena, dx: f64 = 0.0, dy: f64 = 0.0): void { let clips: Vec<Rect> = [{ x: 0.0, y: 0.0, w: f64(canvas.width), h: f64(canvas.height) }]; let bends: Vec<Rect> = [NOWHERE]; let bent: Vec<Radii> = [SQUARE]; let ks: Vec<Vec<f64>> = [[scale, scale, dx, dy]]; for (const o of this.ops) { const clip = clips[clips.length - 1]; const t = ks[ks.length - 1]; const kx = t[0]; const ky = t[1]; const ox = t[2]; const oy = t[3]; if (o.kind === OP_TRANSFORM) { const n = o.numbers; ks.push([kx * n[0], ky * n[1], ox + kx * n[2], oy + ky * n[3]]); continue; } if (o.kind === OP_UNTRANSFORM) { if (ks.length> 1) { ks.pop(); } continue; } if (o.kind === OP_CLIP) { const ax = o.rect.x * kx + ox; const bx = (o.rect.x + o.rect.w) * kx + ox; const ay = o.rect.y * ky + oy; const by = (o.rect.y + o.rect.h) * ky + oy; const x0 = max(clip.x, floor(least(ax, bx))); const y0 = max(clip.y, floor(least(ay, by))); const x1 = least(clip.x + clip.w, ceil(max(ax, bx))); const y1 = least(clip.y + clip.h, ceil(max(ay, by))); clips.push({ x: x0, y: y0, w: max(x1 - x0, 0.0), h: max(y1 - y0, 0.0) }); const k = o.corners; const kr = least(kx, ky); const round = k.topLeft + k.topRight + k.bottomRight + k.bottomLeft> 0.0; bends.push(round ? { x: o.rect.x * kx + ox, y: o.rect.y * ky + oy, w: o.rect.w * kx, h: o.rect.h * ky }: bends[bends.length - 1]); bent.push(round ? { topLeft: k.topLeft * kr, topRight: k.topRight * kr, bottomRight: k.bottomRight * kr, bottomLeft: k.bottomLeft * kr }: bent[bent.length - 1]); continue; } if (o.kind === OP_UNCLIP) { if (clips.length> 1) { clips.pop(); bends.pop(); bent.pop(); } continue; } if (clip.w <= 0.0 || clip.h <= 0.0 || kx === 0.0 || ky === 0.0) { continue; } const target = view(canvas, i64(clip.x), i64(clip.y), i64(clip.w), i64(clip.h)); const fx = kx <0.0; const fy = ky <0.0; const sx = fx ? -kx: kx; const sy = fy ? -ky: ky; const r: Rect = { x: (o.rect.x + (fx ? o.rect.w: 0.0)) * kx + ox - clip.x, y: (o.rect.y + (fy ? o.rect.h: 0.0)) * ky + oy - clip.y, w: o.rect.w * sx, h: o.rect.h * sy }; const k0 = o.corners; const kr = least(sx, sy); const k = fx ? { topLeft: k0.topRight, topRight: k0.topLeft, bottomRight: k0.bottomLeft, bottomLeft: k0.bottomRight }: k0; const c: Radii = fy ? { topLeft: k.bottomLeft * kr, topRight: k.bottomRight * kr, bottomRight: k.topRight * kr, bottomLeft: k.topLeft * kr }: { topLeft: k.topLeft * kr, topRight: k.topRight * kr, bottomRight: k.bottomRight * kr, bottomLeft: k.bottomLeft * kr }; const n = o.numbers; if (outside(o, r, n, kr, sy, clip)) { continue; } if (o.kind === OP_POLYGON) { const count = i64(n[0]); if (n.length <count + 7) { continue; } let ends: Vec<i64> = []; for (let i = 1; i <= count; i + = 1) { ends.push(i64(n[i])); } let xs: Vec<f64> = []; let ys: Vec<f64> = []; let x0 = n[count + 1] * kx + ox - clip.x; let y0 = n[count + 2] * ky + oy - clip.y; let x1 = x0; let y1 = y0; for (let i = count + 1; i + 1 <n.length; i + = 2) { const x = n[i] * kx + ox - clip.x; const y = n[i + 1] * ky + oy - clip.y; xs.push(x); ys.push(y); x0 = least(x0, x); x1 = max(x1, x); y0 = least(y0, y); y1 = max(y1, y); } const need = (ceil(x1) - floor(x0) + 3.0) * (ceil(y1) - floor(y0) + 1.0) * 9.0 + 64.0; if (xs.length <3 || need> f64(scratch.size - scratch.used)) { continue; } polygon(target, xs, ys, ends, o.color & 0xffffff, (o.color>> 24) & 255, scratch); continue; } switch (o.kind) { case OP_FILL: { rounded(target, r, c, o.color); break; } case OP_BORDER: { ring(target, r, c, n[fy ? 2: 0] * sy, n[fx ? 3: 1] * sx, n[fy ? 0: 2] * sy, n[fx ? 1: 3] * sx, o.color); break; } case OP_SHADOW: { const s = o.shadow; boxShadow(target, r, c, s.dx * kx, s.dy * ky, s.blur * kr, s.spread * kr, s.argb); break; } case OP_GRADIENT: { gradient(target, r, c, o.gradient.angle, o.gradient.radial, o.gradient.stops); break; } case OP_TEXT: { textAt(target, o.font.at, n[0] * sy, r.x, r.y, o.text, n[1] * sx, o.color, n[2] !== 0.0); break; } case OP_IMAGE: { const own = c.topLeft + c.topRight + c.bottomRight + c.bottomLeft> 0.0; const b = bends[bends.length - 1]; const shape = own ? r: { x: b.x - clip.x, y: b.y - clip.y, w: b.w, h: b.h }; image(target, this.bitmaps[o.color], { x: fx ? n[0] + n[2]: n[0], y: fy ? n[1] + n[3]: n[1], w: fx ? -n[2]: n[2], h: fy ? -n[3]: n[3] }, r, own ? c: bent[bent.length - 1], shape); break; } case OP_STROKE: { let points: Vec<f64> = []; for (let i = 1; i + 1 <n.length; i + = 2) { points.push(n[i] * kx + ox - clip.x); points.push(n[i + 1] * ky + oy - clip.y); } stroke(target, points, n[0] * kr, o.color); break; } default: {} } } } replayCells(cells: Cells, dx: f64 = 0.0, dy: f64 = 0.0): void { let clips: Vec<Rect> = [{ x: 0.0, y: 0.0, w: f64(cells.columns) * GRID_W, h: f64(cells.rows) * GRID_H }]; let ks: Vec<Vec<f64>> = [[1.0, 1.0, dx, dy]]; for (const o of this.ops) { const clip = clips[clips.length - 1]; const t = ks[ks.length - 1]; const kx = t[0]; const ky = t[1]; const ox = t[2]; const oy = t[3]; if (o.kind === OP_TRANSFORM) { const n = o.numbers; ks.push([kx * n[0], ky * n[1], ox + kx * n[2], oy + ky * n[3]]); continue; } if (o.kind === OP_UNTRANSFORM) { if (ks.length> 1) { ks.pop(); } continue; } const ax = o.rect.x * kx + ox; const bx = (o.rect.x + o.rect.w) * kx + ox; const ay = o.rect.y * ky + oy; const by = (o.rect.y + o.rect.h) * ky + oy; const r: Rect = { x: least(ax, bx), y: least(ay, by), w: abs(bx - ax), h: abs(by - ay) }; if (o.kind === OP_CLIP) { const x0 = max(clip.x, r.x); const y0 = max(clip.y, r.y); const x1 = least(clip.x + clip.w, r.x + r.w); const y1 = least(clip.y + clip.h, r.y + r.h); clips.push({ x: x0, y: y0, w: max(x1 - x0, 0.0), h: max(y1 - y0, 0.0) }); continue; } if (o.kind === OP_UNCLIP) { if (clips.length> 1) { clips.pop(); } continue; } if (clip.w <= 0.0 || clip.h <= 0.0) { continue; } switch (o.kind) { case OP_FILL: { cells.area(r, clip, o.color); break; } case OP_GRADIENT: { cells.area(r, clip, meanOf(o.gradient.stops)); break; } case OP_BORDER: { cells.frame(r, clip, o.numbers, o.corners.topLeft> 0.0, o.color); break; } case OP_TEXT: { cells.write(r.x, r.y, clip, o.text, o.color, o.font.at <0 ? -1 - o.font.at: 0); break; } case OP_IMAGE: { cells.place(this.bitmaps[o.color], r, clip); break; } default: {} } } cells.join(); } }A display list: what a Canvas draws, recorded in order with nested clips, in float pixels. Recording draws nothing; replay() draws it at any scale. An engine records a frame once and replays it onto a screen, a region or a file.
ops:Vec<gizlang_graphics.Op>bitmaps:Vec<gizlang_graphics.Bitmap>
Pictured
type Pictured = { bitmap: Bitmap; column: i64; row: i64; columns: i64; rows: i64; }A picture over the cells COLUMNS x ROWS from (COLUMN, ROW).
bitmap:gizlang_graphics.Bitmapcolumn:i64row:i64columns:i64rows:i64
Cells
class Cells { columns: i64; rows: i64; text: Vec<string> = []; fg: Vec<i64> = []; bg: Vec<i64> = []; style: Vec<i64> = []; pictures: Vec<Pictured> = []; base: i64 = 0; cursor: Vec<i64> = []; constructor(columns: i64, rows: i64) { this.columns = columns; this.rows = rows; this.clear(); } clear(): void { const n = this.columns * this.rows; this.text = new Array(n).fill(" "); this.fg = new Array(n).fill(0); this.bg = new Array(n).fill(0); this.style = new Array(n).fill(0); this.pictures = []; this.base = 0; this.cursor = []; } put(c: i64, r: i64, text: string, fg: i64, bg: i64, style: i64): void { const w = graphemeWidth(text); if (r <0 || r> = this.rows || c <0 || c + (w> 1 ? w: 1)> this.columns) { return; } const i = r * this.columns + c; this.text[i] = text; this.fg[i] = fg; this.bg[i] = bg; this.style[i] = style; if (w === 2) { this.text[i + 1] = ""; this.fg[i + 1] = fg; this.bg[i + 1] = bg; this.style[i + 1] = style; } } line(c: i64, r: i64, text: string, fg: i64, bg: i64, style: i64): i64 { let at = c; for (const g of graphemes(text)) { const w = graphemeWidth(g); if (w === 0) { continue; } if (at + w> this.columns) { break; } this.put(at, r, g, fg, bg, style); at + = w; } return at; } area(rect: Rect, clip: Rect, color: i64): void { if (((color>> 24) & 255) === 0) { return; } const columns = this.columns; const rows = this.rows; const x0 = max(rect.x, clip.x); const y0 = max(rect.y, clip.y); const x1 = least(rect.x + rect.w, clip.x + clip.w); const y1 = least(rect.y + rect.h, clip.y + clip.h); if (x1 <= x0 || y1 <= y0) { return; } const flat = y1 - y0 <GRID_H * 0.5; const narrow = x1 - x0 <GRID_W * 0.5; if (flat && narrow) { return; } if (flat) { const row = i64(floor(y0 / GRID_H)); const part = (y0 - f64(row) * GRID_H) / GRID_H; for (let c = i64(floor(x0 / GRID_W)); c <i64(ceil(x1 / GRID_W)); c + = 1) { if (row <0 || row> = rows || c <0 || c> = columns) { continue; } const i = row * columns + c; if (this.text[i] !== " ") { if (this.text[i] !== ACROSS) { this.style[i] = this.style[i] | (part> 0.6 ? CELL_UNDERLINE: CELL_STRIKE); } } else { this.text[i] = ACROSS; this.fg[i] = color; } } return; } if (narrow) { if (y1 - y0 <GRID_H * 1.5) { return; } const c = i64(floor(x0 / GRID_W)); for (let row = i64(floor(y0 / GRID_H + 0.5)); row <i64(floor(y1 / GRID_H + 0.5)); row + = 1) { if (row <0 || row> = rows || c <0 || c> = columns) { continue; } const i = row * columns + c; if (this.text[i] === " ") { this.text[i] = DOWN; this.fg[i] = color; } } return; } const c0 = edge(x0, GRID_W); const c1 = edge(x1, GRID_W); const r0 = edge(y0, GRID_H); const r1 = edge(y1, GRID_H); const opaque = ((color>> 24) & 255) === 255; if (this.base === 0 && c0 <= 0 && r0 <= 0 && c1> = columns && opaque) { this.base = color; } const base = this.base; const left = c0> 0 ? c0: 0; const right = c1 <columns ? c1: columns; const top = r0> 0 ? r0: 0; const bottom = r1 <rows ? r1: rows; if (opaque) { fillCellRows(this.bg, columns, left, right, top, bottom, color); } else { blendCellRows(this.bg, columns, left, right, top, bottom, color, base); } fillCellRows(this.text, columns, left, right, top, bottom, " "); fillCellRows(this.style, columns, left, right, top, bottom, 0); } frame(rect: Rect, clip: Rect, widths: Vec<f64>, round: bool, color: i64): void { if (((color>> 24) & 255) === 0) { return; } const columns = this.columns; const rows = this.rows; const top = edge(rect.y, GRID_H); const bottom = edge(rect.y + rect.h, GRID_H) - 1; const left = edge(rect.x, GRID_W); const right = edge(rect.x + rect.w, GRID_W) - 1; const c0 = edge(clip.x, GRID_W); const c1 = edge(clip.x + clip.w, GRID_W); const r0 = edge(clip.y, GRID_H); const r1 = edge(clip.y + clip.h, GRID_H); const inside = (c: i64, r: i64): bool => c> = c0 && c <c1 && r> = r0 && r <r1; const mark = (c: i64, r: i64, g: string): void => { if (!inside(c, r) || r <0 || r> = rows || c <0 || c> = columns) { return; } const i = r * columns + c; this.text[i] = g; this.fg[i] = color; }; if (top === bottom) { if (widths[3]> 0.0) { mark(left, top, "["); } if (widths[1]> 0.0) { mark(right, top, "]"); } return; } for (let c = left + 1; c <right; c + = 1) { if (widths[0]> 0.0) { mark(c, top, ACROSS); } if (widths[2]> 0.0) { mark(c, bottom, ACROSS); } } for (let r = top + 1; r <bottom; r + = 1) { if (widths[3]> 0.0) { mark(left, r, DOWN); } if (widths[1]> 0.0) { mark(right, r, DOWN); } } const corner = (c: i64, r: i64, side: f64, end: f64, g: string): void => { if (side> 0.0 && end> 0.0) { mark(c, r, g); } else if (side> 0.0) { mark(c, r, DOWN); } else if (end> 0.0) { mark(c, r, ACROSS); } }; corner(left, top, widths[3], widths[0], round ? "╭": "┌"); corner(right, top, widths[1], widths[0], round ? "╮": "┐"); corner(left, bottom, widths[3], widths[2], round ? "╰": "└"); corner(right, bottom, widths[1], widths[2], round ? "╯": "┘"); } write(x: f64, baseline: f64, clip: Rect, text: string, color: i64, style: i64): void { if (((color>> 24) & 255) === 0) { return; } const row = edge(baseline - GRID_ASCENT, GRID_H); if (row <0 || row> = this.rows || row <edge(clip.y, GRID_H) || row> = edge(clip.y + clip.h, GRID_H)) { return; } const first = edge(clip.x, GRID_W); const last = edge(clip.x + clip.w, GRID_W); let c = edge(x, GRID_W); let previous = -1; for (const g of graphemes(text)) { const w = graphemeWidth(g); if (w === 0) { if (previous> = 0) { this.text[previous] = this.text[previous] + g; } continue; } if (c> = first && c + w <= last && c> = 0 && c + w <= this.columns) { const i = row * this.columns + c; const keep = this.bg[i]; this.put(c, row, g, color, keep, style); previous = i; } c + = w; } } place(bitmap: Bitmap, rect: Rect, clip: Rect): void { const columns = this.columns; const rows = this.rows; const x0 = max(rect.x, clip.x); const y0 = max(rect.y, clip.y); const x1 = least(rect.x + rect.w, clip.x + clip.w); const y1 = least(rect.y + rect.h, clip.y + clip.h); const c0 = edge(x0, GRID_W); const r0 = edge(y0, GRID_H); const c1 = edge(x1, GRID_W); const r1 = edge(y1, GRID_H); if (c1 <= c0 || r1 <= r0 || c0 <0 || r0 <0 || c1> columns || r1> rows) { return; } fillCellRows(this.text, columns, c0, c1, r0, r1, " "); this.pictures.push({ bitmap, column: c0, row: r0, columns: c1 - c0, rows: r1 - r0 }); } join(): void { const columns = this.columns; const rows = this.rows; joinCellRules(this.text, columns, rows); } }A terminal's surface: COLUMNS x ROWS cells, each a grapheme ("" in the cell after a wide one), its foreground and background (ARGB; 0 is the terminal's own) and its style bits (CELL_*), the pictures placed over cells, the background of the page under all (BASE: the terminal shows its own there) and where the terminal's cursor stands ([column, row], or none). Scene.replayCells draws a scene on it; gizlang/terminal sends what changed.
columns:i64rows:i64text:Vec<string>fg:Vec<i64>bg:Vec<i64>style:Vec<i64>pictures:Vec<gizlang_graphics.Pictured>base:i64cursor:Vec<i64>
Face
type Face = { font: Font; family: string; weight: i64; italic: bool; }One face: its font, its family (lower case), weight and slant.
font:gizlang_graphics.Fontfamily:stringweight:i64italic:bool
Run
type Run = { face: i64; text: string; count: i64; }A run of text in one face (Fonts.runs): the face, the text and its code points.
face:i64text:stringcount:i64
Fonts
class Fonts { faces: Vec<Face> = []; aliases: Map<string, string> = new Map(); constructor() {} loaders: Map<i64, () => Font> = new Map(); covered: Map<i64, i64> = new Map(); private picked: Map<string, i64> = new Map(); add(font: Font, family: string, weight: i64, italic: bool): void { this.faces.push({ font, family: family.toLowerCase(), weight, italic }); this.picked = new Map(); } later(load: () => Font, family: string, weight: i64, italic: bool): void { this.loaders.set(this.faces.length, load); this.faces.push({ font: { at: 0 }, family: family.toLowerCase(), weight, italic }); this.picked = new Map(); } font(i: i64): Font { const load = this.loaders.get(i); if (load !== undefined) { this.loaders.delete(i); this.faces[i].font = load!(); } return this.faces[i].font; } holds(i: i64, code: i64): bool { const f = this.font(i); return f.at !== 0 && f.has(code); } alias(name: string, family: string): void { this.aliases.set(name.toLowerCase(), family.toLowerCase()); this.picked = new Map(); } pick(families: Vec<string>, weight: i64, italic: bool): i64 { const key = `${weight }${ italic ? "i": "n" }${ families.join("\n") }`; const known = this.picked.get(key); if (known !== undefined) { return known!; } const face = this.search(families, weight, italic); this.picked.set(key, face); return face; } private search(families: Vec<string>, weight: i64, italic: bool): i64 { for (const name of families) { let family = name.toLowerCase(); if (!this.has(family)) { family = this.aliases.get(family) ?? ""; } if (family === "" || !this.has(family)) { continue; } return this.nearest(family, weight, italic); } const fallback = this.aliases.get("sans-serif") ?? (this.faces.length> 0 ? this.faces[0].family: ""); return this.faces.length === 0 ? -1: this.nearest(fallback, weight, italic); } has(family: string): bool { for (const f of this.faces) { if (f.family === family) { return true; } } return false; } nearest(family: string, weight: i64, italic: bool): i64 { let best = -1; let bestScore = 1 <<40; for (let i = 0; i <this.faces.length; i + = 1) { const f = this.faces[i]; if (f.family !== family) { continue; } let score = f.weight> weight ? (f.weight - weight) * (weight> = 500 ? 1: 3): (weight - f.weight) * (weight> = 500 ? 3: 1); if (f.italic !== italic) { score + = 100000; } if (score <bestScore) { bestScore = score; best = i; } } return best; } covering(face: i64, code: i64): i64 { if (face <0 || code <0x80) { return face; } const key = (face <<21) | code; const known = this.covered.get(key); if (known !== undefined) { return known!; } let found = face; if (!this.holds(face, code)) { const weight = this.faces[face].weight; for (let i = 0; i <this.faces.length && found === face; i + = 1) { if (this.faces[i].weight === weight && this.holds(i, code)) { found = i; } } for (let i = 0; i <this.faces.length && found === face; i + = 1) { if (this.faces[i].weight === 400 && this.holds(i, code)) { found = i; } } } this.covered.set(key, found); return found; } advance(face: i64, code: i64, size: f64): f64 { const f = face <0 ? NO_FONT: this.font(face); if (f.at === 0) { return size * 0.5; } return f.advance(code, size); } measure(face: i64, text: string, size: f64, letter: f64): f64 { let width = 0.0; for (const run of this.runs(face, text)) { width + = (run.face <0 ? 0.0: this.font(run.face).measure(run.text, size)) + letter * f64(run.count); } return width; } runs(face: i64, text: string): Vec<Run> { let out: Vec<Run> = []; const data = utf8(text); let at = 0; let start = 0; let runFace = -1; let count = 0; while (at <data.length) { const cp = codePointAt(data, at); const own = this.covering(face, cp.value); if (own !== runFace && count> 0) { out.push({ face: runFace, text: string(data.slice(start, at)), count }); start = at; count = 0; } runFace = own; at = cp.next; count + = 1; } if (count> 0) { out.push({ face: runFace, text: string(data.slice(start, data.length)), count }); } return out; } ascent(face: i64, size: f64): f64 { const f = face <0 ? NO_FONT: this.font(face); return f.at === 0 ? size * 0.8: f.ascent(size); } descent(face: i64, size: f64): f64 { const f = face <0 ? NO_FONT: this.font(face); return f.at === 0 ? size * 0.2: f.descent(size); } normal(face: i64, size: f64): f64 { const f = face <0 ? NO_FONT: this.font(face); if (f.at === 0) { return size * 1.2; } return f.ascent(size) + f.descent(size) + f.gap(size); } }The faces a program draws text with, and the generic families it maps onto them: the face of each character (the style's families, the nearest weight, then any face that has the glyph) and exact advances. A program adds faces loaded, or loaders that run at a face's first use (folderFonts).
faces:Vec<gizlang_graphics.Face>aliases:_gizlang_sdk_collections.Map<string, string>— A generic or missing family name (sans-serif, Inter, system-ui) and the family it stands for.loaders:_gizlang_sdk_collections.Map<i64, () => gizlang_graphics.Font>— The loaders of the faces added by later that have not been used yet, by face.covered:_gizlang_sdk_collections.Map<i64, i64>— The face that draws each character in each face (covering), by face << 21 | code.picked:_gizlang_sdk_collections.Map<string, i64>— The face pick gave for each weight, slant and list of families: a layout asks for the same few for every run of text, and a search of all the faces for each was a twentieth of a page's layout. A new face or alias empties it.
Functions
argb
function argb(color: i64, alpha: i64): i64COLOR (0xRRGGBB) at ALPHA (0 to 255) as one 0xAARRGGBB value.
Effects: fail.
area
function area(x: i64, y: i64, w: i64, h: i64): RectThe rectangle of whole pixels at (X, Y) of W x H.
radii
function radii(r: f64): RadiiOne radius for every corner.
polygonMask
function polygonMask(area: i64, mask: i64, w: i64, h: i64, xs: Vec<f64>, ys: Vec<f64>, dx: f64, dy: f64, ends: Vec<i64> = []): voidThe coverage mask of the closed polygon through (XS[i], YS[i]), each point moved by (DX, DY), in the W x H MASK, through AREA (of W + 2 by H f64 deltas, cleared here): the non-zero winding of its edges. ENDS splits the points into closed contours, each the index after its last point (empty: one contour).
Effects: fail, read, write.
insets
function insets(w: f64): InsetsThe same width on every side.
rgb
function rgb(r: i64, g: i64, b: i64): i64An opaque color of red, green and blue (each clamped to 0 to 255).
Effects: fail.
over
function over(to: i64, from: i64, count: i64): voidCOUNT premultiplied ARGB pixels at FROM over the XRGB pixels at TO (Porter-Duff over): a client buffer onto a screen.
Effects: fail, read, write.
gridFont
function gridFont(style: i64): FontThe face of a terminal's grid in STYLE (CELL_BOLD, CELL_ITALIC, and a text decoration's CELL_UNDERLINE or CELL_STRIKE): it draws nothing on a Canvas, and each grapheme takes its cells (gizlang/unicode) times GRID_W at any size, one line GRID_H. Cells draws its text.
Effects: fail.
gridFonts
function gridFonts(): FontsThe faces of a terminal's grid, regular, bold and italic, as the family "monospace" that every family stands for: a page laid out with them falls on the cells of Viewport.cells.
Effects: fail, read, write, alloc.
fontBudget
function fontBudget(): i64The arena bytes that loading one font takes: its record, tables and caches (the file is the caller's).
Effects: fail.
loadFont
function loadFont(data: i64, size: i64, a: mut Arena): Font throws stringThe font in the OpenType file of SIZE bytes at DATA (TrueType or CFF outlines; the first face of a collection), its record and caches from A. DATA is the caller's and stays as it is while the font is used.
Effects: fail, read, write, alloc.
constructor
constructor()An empty scene.
Effects: fail, write, alloc.
constructor
constructor(columns: i64, rows: i64)COLUMNS x ROWS blank cells.
Effects: fail, read, write, alloc.
constructor
constructor()No faces and no aliases: add faces with add or later.
Effects: fail, write, alloc.
breakBetween
function breakBetween(previous: i64, code: i64): boolWhether a line may break before CODE when PREVIOUS precedes it, without a space between: CJK ideographs, kana and Hangul break between any two, and a line may break after a hyphen or a slash (UAX #14, cut to the text of a UI).
wide
function wide(code: i64): boolCJK and Hangul: each character is a word of its own for line breaking.
fontFolders
function fontFolders(): Vec<string>The system's font folders, in order. On Linux and GizOS: Pretendard first (the faces of the UI), then Noto (CJK) and DejaVu (Greek, Cyrillic, Hebrew, Arabic) of each XDG data folder; the others only draw what Pretendard lacks (Fonts.covering). On macOS the system's and the user's Library folders; on Windows its Fonts folder.
Effects: fail, io, alloc.
folderFonts
function folderFonts(folders: Vec<string>): Fonts throws stringEvery OpenType face in the folders FOLDERS (one level, missing folders skipped), named Family-Style.ttf, .otf or .ttc (its first face): the style gives the weight (WEIGHTS) and Italic the slant. A font the text does not name falls back to the first family; a character its face lacks, to the first face that has it, in folder order.
Effects: fail, read, write, io, alloc.
rightToLeftCode
function rightToLeftCode(code: i64): boolWhether CODE is written right to left (Hebrew, Arabic, Syriac, Thaana, N'Ko and their presentation forms).
strongOf
function strongOf(text: string): i64The class of TEXT for bidi: its first strong letter's, else a number, else none.
Effects: fail.
Methods
Canvas.view
function Canvas.view(self: Canvas, x: i64, y: i64, w: i64, h: i64): CanvasThe rectangle at (X, Y) of W x H of CANVAS as a canvas of its own.
Effects: fail.
Canvas.fill
function Canvas.fill(self: Canvas, rect: Rect, corners: Radii, color: i64): voidRECT with corners RADII filled with ARGB, its edges by exact coverage.
Effects: fail, read, write, alloc.
Canvas.border
function Canvas.border(self: Canvas, rect: Rect, corners: Radii, widths: Insets, color: i64): voidThe border of RECT (corners RADII) WIDTHS wide on each side, in ARGB; inner corners follow the outer ones.
Effects: fail, read, write, alloc.
Canvas.shadow
function Canvas.shadow(self: Canvas, rect: Rect, corners: Radii, s: Shadow): voidThe shadow that RECT (corners RADII) casts. Its inside is left alone: the shape that casts it covers it.
Effects: fail, read, write, alloc.
Canvas.gradient
function Canvas.gradient(self: Canvas, rect: Rect, corners: Radii, g: Gradient): voidRECT (corners RADII) filled with G.
Effects: fail, read, write, alloc.
Canvas.stroke
function Canvas.stroke(self: Canvas, points: Vec<f64>, width: f64, color: i64): voidThe polyline through POINTS (x, y pairs) stroked WIDTH wide with round joins and caps.
Effects: fail, read, write.
Canvas.polygon
function Canvas.polygon(self: Canvas, xs: Vec<f64>, ys: Vec<f64>, color: i64, scratch: mut Arena, ends: Vec<i64> = []): voidThe closed polygon through (XS[i], YS[i]) by non-zero winding, in contours that ENDS splits (polygon); its masks come from SCRATCH and go back to it.
Effects: fail, read, write.
Canvas.text
function Canvas.text(self: Canvas, font: Font, size: f64, x: f64, baseline: f64, text: string, color: i64, spacing: f64 = 0.0, rtl: bool = false): f64TEXT in FONT at SIZE pixels, the pen from X on BASELINE, SPACING added after each character; the pen's end.
Effects: fail, read, write, alloc.
Canvas.image
function Canvas.image(self: Canvas, bitmap: Bitmap, src: Rect, dst: Rect): voidThe part SRC of BITMAP drawn into DST, scaled to fit it.
Effects: fail, read, write, console, io, alloc.
Canvas.blur
function Canvas.blur(self: Canvas, x: i64, y: i64, w: i64, h: i64, radius: i64, scratch: mut Arena): voidBlurs the rectangle at (X, Y) of W x H: three box passes of RADIUS, close to a Gaussian.
Effects: fail, read, write.
Canvas.darken
function Canvas.darken(self: Canvas, keep: i64): voidScales every pixel's red, green and blue by KEEP / 256: the backdrop under an overlay.
Effects: fail, read, write.
Canvas.saveCorners
function Canvas.saveCorners(self: Canvas, x: i64, y: i64, w: i64, h: i64, radius: i64): Vec<i64>The pixels under the corners of a window of RADIUS at (X, Y) of W x H, for restoreCorners after it is drawn square.
Effects: fail, read, alloc.
Canvas.restoreCorners
function Canvas.restoreCorners(self: Canvas, saved: Vec<i64>, x: i64, y: i64, w: i64, h: i64, radius: i64): voidPuts back the pixels SAVED by saveCorners at the corners of the window of RADIUS at (X, Y) of W x H.
Effects: fail, read, write, alloc.
Font.grid
function Font.grid(self: Font): boolWhether FONT is a face of the terminal's grid (gridFont).
Font.has
function Font.has(self: Font, code: i64): boolWhether FONT has a glyph for CODE.
Effects: fail, read.
Font.advance
function Font.advance(self: Font, code: i64, size: f64): f64The advance of CODE at SIZE pixels.
Effects: fail, read, alloc.
Font.measure
function Font.measure(self: Font, text: string, size: f64): f64The width of TEXT at SIZE pixels.
Effects: fail, read, alloc.
Font.ascent
function Font.ascent(self: Font, size: f64): f64The ascent above the baseline, the descent below it and the line gap, at SIZE pixels (hhea).
Effects: fail, read.
Font.descent
function Font.descent(self: Font, size: f64): f64The descent below the baseline at SIZE pixels.
Effects: fail, read.
Font.gap
function Font.gap(self: Font, size: f64): f64The line gap at SIZE pixels.
Effects: fail, read.
Scene.fill
fill(rect: Rect, corners: Radii, color: i64): voidFills RECT with its CORNERS rounded in COLOR (ARGB).
Effects: fail, read, write, console, io, alloc.
Scene.border
border(rect: Rect, corners: Radii, widths: Insets, color: i64): voidThe border of RECT, WIDTHS wide on each side, in COLOR.
Effects: fail, read, write, console, io, alloc.
Scene.shadow
shadow(rect: Rect, corners: Radii, s: Shadow): voidThe shadow S of the box RECT.
Effects: fail, read, write, console, io, alloc.
Scene.gradient
gradient(rect: Rect, corners: Radii, g: Gradient): voidFills RECT with the gradient G.
Effects: fail, read, write, console, io, alloc.
Scene.text
text(font: Font, size: f64, x: f64, baseline: f64, text: string, color: i64, spacing: f64, rtl: bool = false): voidTEXT in FONT at SIZE from X on BASELINE, in COLOR, with SPACING between its characters.
Effects: fail, read, write, console, io, alloc.
Scene.stroke
stroke(points: Vec<f64>, width: f64, color: i64): voidA line WIDTH wide through POINTS (x, y pairs), in COLOR.
Effects: fail, read, write, console, io, alloc.
Scene.bitmap
bitmap(b: Bitmap): i64Keeps BITMAP for image() and answers its id: a picture drawn often (a sprite) is kept once.
Effects: fail, read, write, alloc.
Scene.image
image(id: i64, src: Rect, dst: Rect, corners: Radii = SQUARE): voidThe part SRC of the bitmap ID (bitmap()) drawn into DST.
Effects: fail, read, write, console, io, alloc.
Scene.scales
scales(scale: f64): Vec<f64>The most canvas pixels that one pixel of each bitmap covers where the scene draws it, replayed at SCALE (0 for one it never draws): a host that decodes a picture only to draw it decodes it at that size (replaceBitmap).
Effects: fail, read, alloc.
Scene.replaceBitmap
replaceBitmap(id: i64, b: Bitmap): voidReplaces the bitmap ID by B, the same picture at another size: the parts its operations draw scale with it.
Effects: fail, read, write, alloc.
Scene.polygon
polygon(xs: Vec<f64>, ys: Vec<f64>, ends: Vec<i64>, color: i64): voidThe closed polygon through (XS[i], YS[i]) by non-zero winding, in contours that ENDS splits (Canvas.polygon).
Effects: fail, read, write, console, io, alloc.
Scene.transform
transform(sx: f64, sy: f64, tx: f64, ty: f64): voidDraws what follows with each point (x, y) at (x * SX + TX, y * SY + TY), within any transform already set, until untransform(): CSS's translate and scale. A text's size scales by SY.
Effects: fail, read, write, console, io, alloc.
Scene.untransform
untransform(): voidEnds the last transform().
Effects: fail, read, write, console, io, alloc.
Scene.clip
clip(rect: Rect, corners: Radii = SQUARE): voidClips what follows to RECT, within any clip already set, until unclip().
Effects: fail, read, write, console, io, alloc.
Scene.unclip
unclip(): voidEnds the last clip().
Effects: fail, read, write, console, io, alloc.
Scene.size
size(): i64How many operations the scene holds.
Effects: fail, read.
Scene.replay
replay(canvas: Canvas, scale: f64, scratch: mut Arena, dx: f64 = 0.0, dy: f64 = 0.0): voidDraws the scene onto CANVAS, one scene pixel as SCALE canvas pixels, moved by (DX, DY) canvas pixels (a scrolled view draws its page at -scroll). An operation wholly outside the canvas draws nothing and costs only its test, so a long page costs what its visible part holds. A polygon's masks come from SCRATCH; one whose masks do not fit in it is skipped.
Effects: fail, read, write, console, io, alloc.
Scene.replayCells
replayCells(cells: Cells, dx: f64 = 0.0, dy: f64 = 0.0): voidDraws the scene onto the terminal surface CELLS, moved by (DX, DY) CSS pixels, each cell GRID_W x GRID_H of them (Viewport.cells): a fill gives its cells their background and covers their text, a thin one underlines or strikes the text in its row or is a rule; a border is a frame of box-drawing characters; text takes a cell for each cell of its graphemes, in the style of its grid face (gridFont); a picture is placed over its cells, which the terminal draws its own way (gizlang/terminal). Shadows, strokes and polygons have no cells.
Effects: fail, read, write, alloc.
Cells.clear
clear(): voidEvery cell blank, no picture, no cursor.
Effects: fail, read, write, alloc.
Cells.put
put(c: i64, r: i64, text: string, fg: i64, bg: i64, style: i64): voidGrapheme TEXT (one cell, or two for a wide one) at (C, R) in FG on BG with STYLE; nothing outside.
Effects: fail, read, write, alloc.
Cells.line
line(c: i64, r: i64, text: string, fg: i64, bg: i64, style: i64): i64TEXT from (C, R) on, a cell for each cell of its graphemes, as far as the row goes; the column after it.
Effects: fail, read, write, alloc.
Cells.area
area(rect: Rect, clip: Rect, color: i64): voidThe cells whose centres RECT (in CSS pixels, within CLIP) covers, filled with COLOR over their background and cleared of text. A band thinner than half a cell is a line: under or through the text of its row, or a rule.
Effects: fail, read, write, alloc.
Cells.frame
frame(rect: Rect, clip: Rect, widths: Vec<f64>, round: bool, color: i64): voidThe frame of RECT within CLIP, WIDTHS wide on each side (top, right, bottom, left), in COLOR: rules on its rows and columns, round corners when ROUND; a frame one row high is brackets, as a button's.
Effects: fail, read, write, alloc.
Cells.write
write(x: f64, baseline: f64, clip: Rect, text: string, color: i64, style: i64): voidTEXT from X on BASELINE (CSS pixels) within CLIP, in COLOR and STYLE: its row is the one its line (GRID_ASCENT above the baseline) falls on by edge, as its box's background does.
Effects: fail, read, write, alloc.
Cells.place
place(bitmap: Bitmap, rect: Rect, clip: Rect): voidBITMAP over the cells RECT covers within CLIP: their text goes, the terminal draws the picture there.
Effects: fail, read, write, alloc.
Cells.join
join(): voidRules that meet join: a rule's end beside a crossing one takes its corner, tee or cross.
Effects: fail, read, write, alloc.
Canvas.pixel
function Canvas.pixel(self: Canvas, x: i64, y: i64, color: i64): voidThe pixel at (X, Y) in ARGB over what is there; nothing outside the canvas.
Effects: fail, read, write.
Fonts.add
add(font: Font, family: string, weight: i64, italic: bool): voidAdds the loaded FONT as FAMILY at WEIGHT.
Effects: fail, read, write, alloc.
Fonts.later
later(load: () => Font, family: string, weight: i64, italic: bool): voidAdds a face that LOAD gives at its first use (a font at 0 when it cannot be read: a face with no glyphs). A page that shows none of its characters never reads its file: a Korean face on an English page was 14 MB.
Effects: fail, read, write, alloc.
Fonts.font
font(i: i64): FontThe font of face I, loaded now if it is the first use.
Effects: fail, read, write, console, io, alloc.
Fonts.holds
holds(i: i64, code: i64): boolWhether face I has a font with the glyph of CODE.
Effects: fail, read, write, console, io, alloc.
Fonts.alias
alias(name: string, family: string): voidMakes NAME (a generic family such as sans-serif, or a family the program lacks) stand for FAMILY.
Effects: fail, read, write, alloc.
Fonts.pick
pick(families: Vec<string>, weight: i64, italic: bool): i64The face for FAMILIES (in order of preference) at WEIGHT and slant ITALIC.
Effects: fail, read, write, alloc.
Fonts.search
private search(families: Vec<string>, weight: i64, italic: bool): i64The face of the first of FAMILIES that was added (by name or alias), nearest WEIGHT and slant; the sans-serif face when none was, -1 with no face.
Effects: fail, read, alloc.
Fonts.has
has(family: string): boolWhether a face of FAMILY was added (lower case).
Effects: fail, read.
Fonts.nearest
nearest(family: string, weight: i64, italic: bool): i64CSS font matching cut short: the same slant first, then the nearest weight (heavier above 500).
Effects: fail, read.
Fonts.covering
covering(face: i64, code: i64): i64The face that draws CODE: FACE if it has the glyph, else the first face of its weight that does, else the first regular one (FACE when none has it). Only faces of those two weights are read: a character no face has does not load every file of a folder of fallbacks. The answer is kept.
Effects: fail, read, write, console, io, alloc.
Fonts.advance
advance(face: i64, code: i64, size: f64): f64The advance of CODE in FACE at SIZE CSS pixels.
Effects: fail, read, write, console, io, alloc.
Fonts.measure
measure(face: i64, text: string, size: f64, letter: f64): f64The width of TEXT in FACE at SIZE, with LETTER spacing after each character.
Effects: fail, read, write, console, io, alloc.
Fonts.runs
runs(face: i64, text: string): Vec<Run>TEXT in runs of one face each: a code point FACE lacks goes to a face that has it. Each run is measured and drawn whole, so its letters shape together.
Effects: fail, read, write, console, io, alloc.
Fonts.ascent
ascent(face: i64, size: f64): f64The ascent and descent of FACE at SIZE: the content area of a line of its text.
Effects: fail, read, write, console, io, alloc.
Fonts.descent
descent(face: i64, size: f64): f64How far the font of FACE reaches below the baseline at SIZE pixels; a missing face reaches a fifth of SIZE.
Effects: fail, read, write, console, io, alloc.
Fonts.normal
normal(face: i64, size: f64): f64line-height: normal of FACE at SIZE.
Effects: fail, read, write, console, io, alloc.
Constants
GRID_W
const GRID_W: f64 = 8.0The CSS pixels of one terminal cell: a page shown on a terminal is laid out on this grid (Viewport.cells).
GRID_H
const GRID_H: f64 = 16.0The CSS pixel height of one terminal cell (GRID_W is its width).
CELL_BOLD
const CELL_BOLD = 1The style bit bold; a cell's style is an OR of CELL_*.
CELL_ITALIC
const CELL_ITALIC = 2The style bit italic.
CELL_UNDERLINE
const CELL_UNDERLINE = 4The style bit underline.
CELL_STRIKE
const CELL_STRIKE = 8The style bit strike.
CELL_DIM
const CELL_DIM = 16The style bit dim.
CELL_INVERSE
const CELL_INVERSE = 32The style bit inverse.
STRONG_NONE
const STRONG_NONE = 0The bidi class of a run of text (strongOf): no strong letter, a left-to-right or right-to-left letter first, or a number with no letter.
STRONG_L
const STRONG_L = 1A run whose first strong letter is written left to right (bidi class L).
STRONG_R
const STRONG_R = 2A run whose first strong letter is written right to left (bidi class R or AL).
STRONG_NUMBER
const STRONG_NUMBER = 3A run with digits and no strong letter (bidi class EN or AN).