aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / ui / allen_font.c
| 1 | /* allen_font.c -- Allen's stroke typeface; see allen_font.h. */ |
| 2 | #include "allen_font.h" |
| 3 | |
| 4 | /* Each glyph: "wN" (advance in design units), then pen commands: |
| 5 | * Mx,y move the pen |
| 6 | * Lx,y line from the pen to x,y |
| 7 | * Acx,cy,rx,ry,a0,a1 elliptical arc from angle a0 to a1 (degrees; 0 is |
| 8 | * right, 90 is down), starting fresh; the pen ends at a1 |
| 9 | * Dx,y a dot |
| 10 | * Every stroke has round caps and joins (distance to the centre line). */ |
| 11 | static const char *const glyphs[CK_AF_COUNT] = { |
| 12 | /* */ "w6", |
| 13 | /* ! */ "w6 M3,0 L3,14 D3,20", |
| 14 | /* " */ "w10 M3,0 L3,5 M7,0 L7,5", |
| 15 | /* # */ "w15 M6,2 L4,20 M11,2 L9,20 M2,8 L13,8 M1,14 L12,14", |
| 16 | /* $ */ "w15 A8,5,5,5,-30,-270 A8,15,5,5,270,510 M8,-2 L8,22", |
| 17 | /* % */ "w16 A4,4,2,3,0,360 A12,16,2,3,0,360 M13,1 L3,19", |
| 18 | /* & */ "w16 M14,20 L5,9 A7,5,3,4,150,400 L3,14 A7,15,4,5,180,60 L13,11", |
| 19 | /* ' */ "w6 M3,0 L3,5", |
| 20 | /* ( */ "w9 A10,10,6,12,240,120", |
| 21 | /* ) */ "w9 A-1,10,6,12,-60,60", |
| 22 | /* * */ "w14 M7,2 L7,10 M3,4 L11,8 M11,4 L3,8", |
| 23 | /* + */ "w14 M2,12 L12,12 M7,7 L7,17", |
| 24 | /* , */ "w6 M3,19 L2,23", |
| 25 | /* - */ "w11 M2,13 L9,13", |
| 26 | /* . */ "w6 D3,20", |
| 27 | /* / */ "w12 M10,0 L2,20", |
| 28 | /* 0 */ "w14 A7,10,5,10,0,360", |
| 29 | /* 1 */ "w14 M3,4 L8,0 L8,20", |
| 30 | /* 2 */ "w14 A7,6,5,6,200,400 L2,20 L12,20", |
| 31 | /* 3 */ "w14 A7,5,5,5,200,450 A7,15,5,5,270,520", |
| 32 | /* 4 */ "w14 M10,20 L10,0 L1,14 L13,14", |
| 33 | /* 5 */ "w14 M12,0 L4,0 L4,9 A7,14,5,6,235,520", |
| 34 | /* 6 */ "w14 A7,14,5,6,0,360 M10,0 L3,11", |
| 35 | /* 7 */ "w14 M2,0 L12,0 L5,20", |
| 36 | /* 8 */ "w14 A7,5,4,5,0,360 A7,15,5,5,0,360", |
| 37 | /* 9 */ "w14 A7,6,5,6,0,360 M4,20 L11,9", |
| 38 | /* : */ "w6 D3,8 D3,20", |
| 39 | /* ; */ "w6 D3,8 M3,19 L2,23", |
| 40 | /* < */ "w14 M12,6 L2,12 L12,18", |
| 41 | /* = */ "w14 M2,10 L12,10 M2,15 L12,15", |
| 42 | /* > */ "w14 M2,6 L12,12 L2,18", |
| 43 | /* ? */ "w14 A7,5,5,5,190,450 L7,14 D7,20", |
| 44 | /* @ */ "w19 A9,12,3,4,0,360 M12,8 L12,14 A14,14,2,2,180,0 A9,11,7,9,0,-300", |
| 45 | /* A */ "w16 M2,20 L8,0 L14,20 M4,13 L12,13", |
| 46 | /* B */ "w15 M2,0 L2,20 M2,0 L8,0 A8,5,4,5,270,450 L2,10 M2,10 L9,10 A9,15,4,5,270,450 L2,20", |
| 47 | /* C */ "w17 A9,10,7,10,45,315", |
| 48 | /* D */ "w16 M2,0 L2,20 M2,0 L6,0 A6,10,8,10,270,450 L2,20", |
| 49 | /* E */ "w14 M12,0 L2,0 L2,20 L12,20 M2,10 L10,10", |
| 50 | /* F */ "w13 M12,0 L2,0 L2,20 M2,10 L10,10", |
| 51 | /* G */ "w18 A9,10,7,10,-45,-360 M11,10 L16,10", |
| 52 | /* H */ "w16 M2,0 L2,20 M14,0 L14,20 M2,10 L14,10", |
| 53 | /* I */ "w6 M3,0 L3,20", |
| 54 | /* J */ "w13 M10,0 L10,14 A6,14,4,6,0,180", |
| 55 | /* K */ "w15 M2,0 L2,20 M13,0 L2,12 M6,8 L14,20", |
| 56 | /* L */ "w13 M2,0 L2,20 L12,20", |
| 57 | /* M */ "w20 M2,20 L2,0 L10,14 L18,0 L18,20", |
| 58 | /* N */ "w16 M2,20 L2,0 L14,20 L14,0", |
| 59 | /* O */ "w20 A10,10,8,10,0,360", |
| 60 | /* P */ "w15 M2,20 L2,0 L8,0 A8,5,5,5,270,450 L2,10", |
| 61 | /* Q */ "w20 A10,10,8,10,0,360 M12,14 L18,21", |
| 62 | /* R */ "w15 M2,20 L2,0 L8,0 A8,5,5,5,270,450 L2,10 M8,10 L14,20", |
| 63 | /* S */ "w16 A8,5,5,5,-30,-270 A8,15,5,5,270,510", |
| 64 | /* T */ "w16 M1,0 L15,0 M8,0 L8,20", |
| 65 | /* U */ "w16 M2,0 L2,13 A8,13,6,7,180,0 L14,0", |
| 66 | /* V */ "w16 M1,0 L8,20 L15,0", |
| 67 | /* W */ "w22 M1,0 L6,20 L11,0 L16,20 L21,0", |
| 68 | /* X */ "w16 M2,0 L14,20 M14,0 L2,20", |
| 69 | /* Y */ "w16 M1,0 L8,10 L15,0 M8,10 L8,20", |
| 70 | /* Z */ "w16 M2,0 L14,0 L2,20 L14,20", |
| 71 | /* [ */ "w9 M7,-1 L3,-1 L3,21 L7,21", |
| 72 | /* \ */ "w12 M2,0 L10,20", |
| 73 | /* ] */ "w9 M2,-1 L6,-1 L6,21 L2,21", |
| 74 | /* ^ */ "w12 M2,8 L6,1 L10,8", |
| 75 | /* _ */ "w14 M1,23 L13,23", |
| 76 | /* ` */ "w7 M2,0 L4,4", |
| 77 | /* a */ "w15 A7,14,5,6,0,360 M12,8 L12,20", |
| 78 | /* b */ "w15 M2,0 L2,20 A7,14,5,6,0,360", |
| 79 | /* c */ "w13 A7,14,5,6,40,320", |
| 80 | /* d */ "w15 A7,14,5,6,0,360 M12,0 L12,20", |
| 81 | /* e */ "w14 M2,14 L12,14 A7,14,5,6,0,-320", |
| 82 | /* f */ "w11 M5,20 L5,4 A9,4,4,4,180,300 M2,8 L10,8", |
| 83 | /* g */ "w15 A7,14,5,6,0,360 M12,8 L12,22 A7,22,5,5,0,150", |
| 84 | /* h */ "w15 M2,0 L2,20 A7,13,5,5,180,360 L12,20", |
| 85 | /* i */ "w6 M3,8 L3,20 D3,3", |
| 86 | /* j */ "w7 M4,8 L4,22 A0,22,4,5,0,120 D4,3", |
| 87 | /* k */ "w13 M2,0 L2,20 M11,8 L2,15 M5,13 L12,20", |
| 88 | /* l */ "w6 M3,0 L3,20", |
| 89 | /* m */ "w21 M2,8 L2,20 A6,12,4,4,180,360 L10,20 A14,12,4,4,180,360 L18,20", |
| 90 | /* n */ "w15 M2,8 L2,20 A7,13,5,5,180,360 L12,20", |
| 91 | /* o */ "w14 A7,14,5,6,0,360", |
| 92 | /* p */ "w15 M2,8 L2,27 A7,14,5,6,0,360", |
| 93 | /* q */ "w15 A7,14,5,6,0,360 M12,8 L12,27", |
| 94 | /* r */ "w10 M2,8 L2,20 A7,13,5,5,180,290", |
| 95 | /* s */ "w14 A7,11,4,3,-30,-270 A7,17,4,3,270,510", |
| 96 | /* t */ "w11 M5,2 L5,17 A8,17,3,3,180,40 M2,8 L10,8", |
| 97 | /* u */ "w15 M2,8 L2,15 A7,15,5,5,180,0 M12,8 L12,20", |
| 98 | /* v */ "w14 M2,8 L7,20 L12,8", |
| 99 | /* w */ "w20 M2,8 L6,20 L10,8 L14,20 L18,8", |
| 100 | /* x */ "w14 M2,8 L12,20 M12,8 L2,20", |
| 101 | /* y */ "w14 M2,8 L7,20 M12,8 L4,27", |
| 102 | /* z */ "w14 M2,8 L12,8 L2,20 L12,20", |
| 103 | /* { */ "w10 M8,-1 L6,0 L6,8 L4,10 L6,12 L6,20 L8,21", |
| 104 | /* | */ "w6 M3,-1 L3,23", |
| 105 | /* } */ "w10 M2,-1 L4,0 L4,8 L6,10 L4,12 L4,20 L2,21", |
| 106 | /* ~ */ "w14 M2,13 L5,11 L9,14 L12,12", |
| 107 | }; |
| 108 | |
| 109 | static const uint16_t sin_q[91] = { |
| 110 | 0, 286, 572, 857, 1143, 1428, 1713, 1997, 2280, 2563, 2845, 3126, 3406, 3686, 3964, |
| 111 | 4240, 4516, 4790, 5063, 5334, 5604, 5872, 6138, 6402, 6664, 6924, 7182, 7438, 7692, 7943, |
| 112 | 8192, 8438, 8682, 8923, 9162, 9397, 9630, 9860, 10087, 10311, 10531, 10749, 10963, 11174, 11381, |
| 113 | 11585, 11786, 11982, 12176, 12365, 12551, 12733, 12911, 13085, 13255, 13421, 13583, 13741, 13894, 14044, |
| 114 | 14189, 14330, 14466, 14598, 14726, 14849, 14968, 15082, 15191, 15296, 15396, 15491, 15582, 15668, 15749, |
| 115 | 15826, 15897, 15964, 16026, 16083, 16135, 16182, 16225, 16262, 16294, 16322, 16344, 16362, 16374, 16382, |
| 116 | 16384, |
| 117 | }; |
| 118 | |
| 119 | int32_t ck_af_sin(int32_t deg) |
| 120 | { |
| 121 | deg %= 360; |
| 122 | if (deg < 0) |
| 123 | deg += 360; |
| 124 | if (deg <= 90) |
| 125 | return sin_q[deg]; |
| 126 | if (deg <= 180) |
| 127 | return sin_q[180 - deg]; |
| 128 | if (deg <= 270) |
| 129 | return -(int32_t)sin_q[deg - 180]; |
| 130 | return -(int32_t)sin_q[360 - deg]; |
| 131 | } |
| 132 | |
| 133 | int32_t ck_af_cos(int32_t deg) { return ck_af_sin(deg + 90); } |
| 134 | |
| 135 | uint64_t ck_af_isqrt(uint64_t v) |
| 136 | { |
| 137 | uint64_t r = 0, bit = (uint64_t)1 << 62; |
| 138 | while (bit > v) |
| 139 | bit >>= 2; |
| 140 | while (bit) { |
| 141 | if (v >= r + bit) { |
| 142 | v -= r + bit; |
| 143 | r = (r >> 1) + bit; |
| 144 | } else { |
| 145 | r >>= 1; |
| 146 | } |
| 147 | bit >>= 2; |
| 148 | } |
| 149 | return r; |
| 150 | } |
| 151 | |
| 152 | static int idx(char c) |
| 153 | { |
| 154 | unsigned char u = (unsigned char)c; |
| 155 | return u >= CK_AF_FIRST && u <= CK_AF_LAST ? u - CK_AF_FIRST : '?' - CK_AF_FIRST; |
| 156 | } |
| 157 | |
| 158 | void ck_af_face_init(ck_af_face *f, unsigned cap_px, unsigned weight, uint8_t *pool, size_t pool_len) |
| 159 | { |
| 160 | f->s256 = cap_px * 256u / 20u; |
| 161 | f->hw256 = (uint32_t)((uint64_t)weight * f->s256 / 1000u); |
| 162 | f->pool = pool; |
| 163 | f->pool_len = pool_len; |
| 164 | f->pool_used = 0; |
| 165 | for (unsigned i = 0; i < CK_AF_COUNT; i++) |
| 166 | f->g[i] = (ck_af_glyph){0}; |
| 167 | } |
| 168 | |
| 169 | /* Reads a signed integer at *p; advances past it and one ',' if present. */ |
| 170 | static int num(const char **p) |
| 171 | { |
| 172 | int neg = 0, v = 0; |
| 173 | if (**p == '-') { |
| 174 | neg = 1; |
| 175 | (*p)++; |
| 176 | } |
| 177 | while (**p >= '0' && **p <= '9') |
| 178 | v = v * 10 + (*(*p)++ - '0'); |
| 179 | if (**p == ',') |
| 180 | (*p)++; |
| 181 | return neg ? -v : v; |
| 182 | } |
| 183 | |
| 184 | uint32_t ck_af_advance(const ck_af_face *f, char c) |
| 185 | { |
| 186 | const char *p = glyphs[idx(c)] + 1; /* past 'w' */ |
| 187 | return (uint32_t)num(&p) * f->s256; |
| 188 | } |
| 189 | |
| 190 | uint32_t ck_af_measure(const ck_af_face *f, const char *s, size_t n) |
| 191 | { |
| 192 | uint64_t w = 0; |
| 193 | for (size_t i = 0; i < n && s[i]; i++) |
| 194 | w += ck_af_advance(f, s[i]); |
| 195 | return (uint32_t)((w + 255) / 256); |
| 196 | } |
| 197 | |
| 198 | #define SEG_MAX 160 |
| 199 | struct seg { |
| 200 | int32_t x0, y0, x1, y1; /* 1/256 px, relative to the pen; y up from the baseline is negative */ |
| 201 | }; |
| 202 | |
| 203 | /* Turns one glyph's commands into segments. Returns the count. */ |
| 204 | static unsigned segments(const ck_af_face *f, const char *p, struct seg *out) |
| 205 | { |
| 206 | int32_t s = (int32_t)f->s256; |
| 207 | unsigned n = 0; |
| 208 | int32_t px = 0, py = 0; |
| 209 | #define PT_X(u) ((int32_t)(u) * s) |
| 210 | #define PT_Y(u) (((int32_t)(u) - 20) * s) |
| 211 | while (*p) { |
| 212 | char cmd = *p++; |
| 213 | if (cmd == ' ') |
| 214 | continue; |
| 215 | if (cmd == 'w') { |
| 216 | (void)num(&p); |
| 217 | } else if (cmd == 'M') { |
| 218 | int x = num(&p), y = num(&p); |
| 219 | px = PT_X(x); |
| 220 | py = PT_Y(y); |
| 221 | } else if (cmd == 'L' || cmd == 'D') { |
| 222 | int x = num(&p), y = num(&p); |
| 223 | int32_t nx = PT_X(x), ny = PT_Y(y); |
| 224 | if (cmd == 'D') |
| 225 | px = nx, py = ny; |
| 226 | if (n < SEG_MAX) |
| 227 | out[n++] = (struct seg){px, py, nx, ny}; |
| 228 | px = nx; |
| 229 | py = ny; |
| 230 | } else if (cmd == 'A') { |
| 231 | int cx = num(&p), cy = num(&p), rx = num(&p), ry = num(&p), a0 = num(&p), a1 = num(&p); |
| 232 | int span = a1 > a0 ? a1 - a0 : a0 - a1; |
| 233 | int steps = span / 9 < 2 ? 2 : span / 9; |
| 234 | int32_t lx = 0, ly = 0; |
| 235 | for (int k = 0; k <= steps; k++) { |
| 236 | int32_t a = a0 + (a1 - a0) * k / steps; |
| 237 | int32_t x = PT_X(cx) + (int32_t)((int64_t)rx * s * ck_af_cos(a) / 16384); |
| 238 | int32_t y = PT_Y(cy) + (int32_t)((int64_t)ry * s * ck_af_sin(a) / 16384); |
| 239 | if (k && n < SEG_MAX) |
| 240 | out[n++] = (struct seg){lx, ly, x, y}; |
| 241 | lx = x; |
| 242 | ly = y; |
| 243 | } |
| 244 | px = lx; |
| 245 | py = ly; |
| 246 | } |
| 247 | } |
| 248 | #undef PT_X |
| 249 | #undef PT_Y |
| 250 | return n; |
| 251 | } |
| 252 | |
| 253 | static int32_t floor_px(int32_t v) { return v >= 0 ? v / 256 : -((-v + 255) / 256); } |
| 254 | |
| 255 | const ck_af_glyph *ck_af_glyph_get(ck_af_face *f, char c) |
| 256 | { |
| 257 | ck_af_glyph *g = &f->g[idx(c)]; |
| 258 | if (g->alpha || (g->w == 0 && g->h == 1)) /* h == 1, w == 0: an empty glyph already done */ |
| 259 | return g; |
| 260 | struct seg sg[SEG_MAX]; |
| 261 | unsigned n = segments(f, glyphs[idx(c)], sg); |
| 262 | if (n == 0) { |
| 263 | g->w = 0; |
| 264 | g->h = 1; |
| 265 | return g; |
| 266 | } |
| 267 | int32_t minx = sg[0].x0, maxx = minx, miny = sg[0].y0, maxy = miny; |
| 268 | for (unsigned i = 0; i < n; i++) { |
| 269 | int32_t xs[2] = {sg[i].x0, sg[i].x1}, ys[2] = {sg[i].y0, sg[i].y1}; |
| 270 | for (int k = 0; k < 2; k++) { |
| 271 | if (xs[k] < minx) minx = xs[k]; |
| 272 | if (xs[k] > maxx) maxx = xs[k]; |
| 273 | if (ys[k] < miny) miny = ys[k]; |
| 274 | if (ys[k] > maxy) maxy = ys[k]; |
| 275 | } |
| 276 | } |
| 277 | int32_t pad = (int32_t)f->hw256 + 256; |
| 278 | int32_t x0 = floor_px(minx - pad), y0 = floor_px(miny - pad); |
| 279 | int32_t x1 = floor_px(maxx + pad) + 1, y1 = floor_px(maxy + pad) + 1; |
| 280 | size_t w = (size_t)(x1 - x0), h = (size_t)(y1 - y0); |
| 281 | if (f->pool_len - f->pool_used < w * h) |
| 282 | return 0; |
| 283 | uint8_t *a = f->pool + f->pool_used; |
| 284 | int64_t hw = f->hw256; |
| 285 | for (size_t yy = 0; yy < h; yy++) { |
| 286 | int64_t py = (int64_t)(y0 + (int32_t)yy) * 256 + 128; |
| 287 | for (size_t xx = 0; xx < w; xx++) { |
| 288 | int64_t pxc = (int64_t)(x0 + (int32_t)xx) * 256 + 128; |
| 289 | int64_t best = INT64_MAX; |
| 290 | for (unsigned i = 0; i < n; i++) { |
| 291 | int64_t ax = sg[i].x0, ay = sg[i].y0, dx = sg[i].x1 - ax, dy = sg[i].y1 - ay; |
| 292 | int64_t qx = pxc - ax, qy = py - ay, len2 = dx * dx + dy * dy, ex, ey; |
| 293 | if (len2 == 0) { |
| 294 | ex = qx; |
| 295 | ey = qy; |
| 296 | } else { |
| 297 | int64_t t = qx * dx + qy * dy; /* projection, scaled by len2 */ |
| 298 | if (t <= 0) { |
| 299 | ex = qx; |
| 300 | ey = qy; |
| 301 | } else if (t >= len2) { |
| 302 | ex = qx - dx; |
| 303 | ey = qy - dy; |
| 304 | } else { |
| 305 | ex = qx - dx * t / len2; |
| 306 | ey = qy - dy * t / len2; |
| 307 | } |
| 308 | } |
| 309 | int64_t d2 = ex * ex + ey * ey; |
| 310 | if (d2 < best) |
| 311 | best = d2; |
| 312 | } |
| 313 | int64_t d = (int64_t)ck_af_isqrt((uint64_t)best); |
| 314 | int64_t cov = hw + 128 - d; /* 1/256 px of the pixel inside the stroke */ |
| 315 | a[yy * w + xx] = cov <= 0 ? 0 : cov >= 256 ? 255 : (uint8_t)(cov * 255 / 256); |
| 316 | } |
| 317 | } |
| 318 | f->pool_used += w * h; |
| 319 | g->w = (uint16_t)w; |
| 320 | g->h = (uint16_t)h; |
| 321 | g->ox = (int16_t)x0; |
| 322 | g->oy = (int16_t)y0; |
| 323 | g->alpha = a; |
| 324 | return g; |
| 325 | } |