@geastack/engine 0.1.15 → 0.1.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/canvas.cpp CHANGED
@@ -11,6 +11,7 @@
11
11
  #include <cstdio>
12
12
  #include <cstdlib>
13
13
  #include <cstring>
14
+ #include <limits>
14
15
  #include <type_traits>
15
16
  #include <vector>
16
17
 
@@ -104,6 +105,11 @@ std::uint8_t gTextStampBackdrop = 15;
104
105
 
105
106
  namespace {
106
107
  int nextUtf8Codepoint(const char *&p); // defined below (same anon namespace); used by the text sprite cache
108
+ int peekUtf8Codepoint(const char *p)
109
+ {
110
+ if (!p || !*p || *p == '\n') return 0;
111
+ return nextUtf8Codepoint(p);
112
+ }
107
113
  #if GEA_PIXEL_STORAGE_PACKED_BITS == 4
108
114
  void glyphStampCacheReset(); // defined below (same anon namespace); used by setTextSolidBackdrop
109
115
  #endif
@@ -265,7 +271,6 @@ bool buildTextSprite(TextSprite &sprite, const char *text, const RasterizedFont
265
271
  {
266
272
  const int lineH = font.lineHeight();
267
273
  const int ascender = font.ascender();
268
- const int fallbackAdvance = font.sizePx() / 2;
269
274
 
270
275
  // Pass 1: bbox of all glyph ink relative to the pen origin (0,0).
271
276
  int penX = 0, penY = 0;
@@ -274,7 +279,7 @@ bool buildTextSprite(TextSprite &sprite, const char *text, const RasterizedFont
274
279
  const int cp = nextUtf8Codepoint(p);
275
280
  if (cp == '\n') { penX = 0; penY += lineH; continue; }
276
281
  Glyph glyph{};
277
- if (!font.glyph(cp, &glyph)) { penX += fallbackAdvance; continue; }
282
+ if (!font.glyph(cp, &glyph)) { penX += font.advance(cp, peekUtf8Codepoint(p)); continue; }
278
283
  if (glyph.width > 0 && glyph.height > 0) {
279
284
  const int gx = penX + glyph.bearingX;
280
285
  const int gy = penY + ascender - glyph.bearingY;
@@ -283,7 +288,7 @@ bool buildTextSprite(TextSprite &sprite, const char *text, const RasterizedFont
283
288
  if (gx + glyph.width - 1 > maxLX) maxLX = gx + glyph.width - 1;
284
289
  if (gy + glyph.height - 1 > maxLY) maxLY = gy + glyph.height - 1;
285
290
  }
286
- penX += glyph.advance;
291
+ penX += font.advance(cp, peekUtf8Codepoint(p));
287
292
  }
288
293
  if (maxLX < minLX || maxLY < minLY) return false; // no ink
289
294
 
@@ -324,7 +329,7 @@ bool buildTextSprite(TextSprite &sprite, const char *text, const RasterizedFont
324
329
  const int cp = nextUtf8Codepoint(p);
325
330
  if (cp == '\n') { penX = 0; penY += lineH; continue; }
326
331
  Glyph glyph{};
327
- if (!font.glyph(cp, &glyph)) { penX += fallbackAdvance; continue; }
332
+ if (!font.glyph(cp, &glyph)) { penX += font.advance(cp, peekUtf8Codepoint(p)); continue; }
328
333
  const int gx = penX + glyph.bearingX;
329
334
  const int gy = penY + ascender - glyph.bearingY;
330
335
  for (int row = 0; row < glyph.height; row++) {
@@ -341,7 +346,7 @@ bool buildTextSprite(TextSprite &sprite, const char *text, const RasterizedFont
341
346
  if (bx > sprite.rowX1[by]) sprite.rowX1[by] = static_cast<std::int16_t>(bx);
342
347
  }
343
348
  }
344
- penX += glyph.advance;
349
+ penX += font.advance(cp, peekUtf8Codepoint(p));
345
350
  }
346
351
 
347
352
  sprite.minLX = minLX;
@@ -1617,7 +1622,10 @@ void Canvas::clear(pixel::native_t color)
1617
1622
  #if GEA_EMBEDDED_DISPLAY_ROTATE_LANDSCAPE
1618
1623
  if (landscapeRot_) clearPixels = static_cast<std::size_t>(stride_) * width_;
1619
1624
  #endif
1620
- std::fill_n(pixels_, clearPixels, color);
1625
+ if (clearPixels <= static_cast<std::size_t>(std::numeric_limits<int>::max()))
1626
+ pixel::fillNative(pixels_, static_cast<int>(clearPixels), color);
1627
+ else
1628
+ std::fill_n(pixels_, clearPixels, color);
1621
1629
  #endif
1622
1630
  markDirty(0, 0, width_ - 1, height_ - 1);
1623
1631
  }
@@ -1748,6 +1756,17 @@ void Canvas::fillRect(int x, int y, int w, int h, pixel::native_t color)
1748
1756
  }
1749
1757
  #endif
1750
1758
  #if !GEA_PIXEL_STORAGE_PACKED
1759
+ // Entire physical rows are one contiguous span. In particular, an owned
1760
+ // canvas background needs one PIE pattern setup rather than one per row.
1761
+ const std::size_t fillPixels = static_cast<std::size_t>(count) * (y1 - y0 + 1);
1762
+ if (x0 == 0 && count == stride_ && regionH_ <= 0 &&
1763
+ fillPixels >= static_cast<std::size_t>(width_) * height_ / 4 &&
1764
+ fillPixels <= static_cast<std::size_t>(std::numeric_limits<int>::max())) {
1765
+ pixel::fillNative(pixels_ + static_cast<std::size_t>(y0) * stride_,
1766
+ static_cast<int>(fillPixels), c);
1767
+ markDirty(x0, y0, x1, y1);
1768
+ return;
1769
+ }
1751
1770
  // Large opaque fills (e.g. a full-screen page/paper background) skip the
1752
1771
  // per-pixel occlusion scan below. That scan READS every pixel just to
1753
1772
  // minimize the dirty rect — worth it for a small changed region, but pure
@@ -1908,6 +1927,16 @@ void Canvas::fillRectOpaque(int x, int y, int w, int h, pixel::native_t color)
1908
1927
  #endif
1909
1928
 
1910
1929
  const int count = x1 - x0 + 1;
1930
+ #if !GEA_PIXEL_STORAGE_PACKED
1931
+ const std::size_t fillPixels = static_cast<std::size_t>(count) * (y1 - y0 + 1);
1932
+ if (x0 == 0 && count == stride_ && regionH_ <= 0 &&
1933
+ fillPixels <= static_cast<std::size_t>(std::numeric_limits<int>::max())) {
1934
+ pixel::fillNative(pixels_ + static_cast<std::size_t>(y0) * stride_,
1935
+ static_cast<int>(fillPixels), color);
1936
+ markDirty(x0, y0, x1, y1);
1937
+ return;
1938
+ }
1939
+ #endif
1911
1940
  for (int row = y0; row <= y1; row++) {
1912
1941
  fillSpanColor(rowToPhysical(row), x0, count, color);
1913
1942
  }
@@ -2210,14 +2239,127 @@ void Canvas::drawArc(int cx, int cy, int r, int start_deg, int end_deg, pixel::n
2210
2239
  }
2211
2240
  }
2212
2241
 
2242
+ namespace {
2243
+
2244
+ #ifndef GEA_EMBEDDED_TRIANGLE_RATIO_CACHE
2245
+ #ifdef ESP_PLATFORM
2246
+ #define GEA_EMBEDDED_TRIANGLE_RATIO_CACHE 1
2247
+ #else
2248
+ #define GEA_EMBEDDED_TRIANGLE_RATIO_CACHE 0
2249
+ #endif
2250
+ #endif
2251
+
2252
+ #if GEA_EMBEDDED_TRIANGLE_RATIO_CACHE
2253
+ // Float division is a software call on embedded targets. Cache the exact
2254
+ // quotient bits, not a reciprocal: changing the rounding changes edge pixels.
2255
+ // The triangular table is bounded to 32.8 KiB and never uses internal ESP RAM.
2256
+ constexpr int kTriangleRatioMaxDenominator = 128;
2257
+ constexpr std::size_t kTriangleRatioCount =
2258
+ kTriangleRatioMaxDenominator * (kTriangleRatioMaxDenominator + 3) / 2;
2259
+ // Publication states live in internal RAM. Published PSRAM rows are immutable;
2260
+ // no atomic operations (or concurrent writes) ever touch the PSRAM allocation.
2261
+ std::atomic<float *> s_triangleRatios{nullptr};
2262
+ std::atomic<unsigned> s_triangleRatioRows[kTriangleRatioMaxDenominator + 1]{};
2263
+ // 0: uninitialized, 1: initializing, 2: ready, 3: allocation unavailable.
2264
+ std::atomic<unsigned> s_triangleRatioState{0};
2265
+
2266
+ float *triangleRatioTable()
2267
+ {
2268
+ if (auto *table = s_triangleRatios.load(std::memory_order_acquire))
2269
+ return table;
2270
+ if (s_triangleRatioState.load(std::memory_order_relaxed) != 0)
2271
+ return nullptr;
2272
+ unsigned state = 0;
2273
+ if (!s_triangleRatioState.compare_exchange_strong(state, 1, std::memory_order_relaxed))
2274
+ return nullptr;
2275
+ void *storage = nullptr;
2276
+ #ifndef GEA_TEST_TRIANGLE_RATIO_ALLOCATION_FAILURE
2277
+ #ifdef ESP_PLATFORM
2278
+ storage =
2279
+ heap_caps_malloc(kTriangleRatioCount * sizeof(float), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
2280
+ #else
2281
+ storage = std::malloc(kTriangleRatioCount * sizeof(float));
2282
+ #endif
2283
+ #endif
2284
+ if (!storage)
2285
+ {
2286
+ s_triangleRatioState.store(3, std::memory_order_relaxed);
2287
+ return nullptr;
2288
+ }
2289
+ auto *table = static_cast<float *>(storage);
2290
+ s_triangleRatios.store(table, std::memory_order_release);
2291
+ s_triangleRatioState.store(2, std::memory_order_relaxed);
2292
+ return table;
2293
+ }
2294
+ #endif
2295
+
2296
+ float triangleRatio(int numerator, int denominator)
2297
+ {
2298
+ #if GEA_EMBEDDED_TRIANGLE_RATIO_CACHE
2299
+ if (denominator > 0 && denominator <= kTriangleRatioMaxDenominator && numerator >= 0 &&
2300
+ numerator <= denominator)
2301
+ {
2302
+ if (auto *table = triangleRatioTable())
2303
+ {
2304
+ const std::size_t offset = denominator * (denominator + 1) / 2 - 1;
2305
+ auto &state = s_triangleRatioRows[denominator];
2306
+ if (state.load(std::memory_order_acquire) == 2)
2307
+ return table[offset + numerator];
2308
+ unsigned empty = 0;
2309
+ if (state.compare_exchange_strong(empty, 1, std::memory_order_relaxed))
2310
+ {
2311
+ for (int n = 0; n <= denominator; ++n)
2312
+ table[offset + n] = static_cast<float>(n) / denominator;
2313
+ state.store(2, std::memory_order_release);
2314
+ return table[offset + numerator];
2315
+ }
2316
+ // Another raster thread is filling this row. It owns all writes;
2317
+ // retain the exact division until the immutable row is published.
2318
+ }
2319
+ }
2320
+ #endif
2321
+ return static_cast<float>(numerator) / denominator;
2322
+ }
2323
+
2324
+ // All triangle entry points share the public canvas pixel definition. Truncate
2325
+ // the interpolated displacement toward zero, rather than flooring an absolute
2326
+ // fixed-point position: those differ on descending/negative triangle edges.
2327
+ void triangleRowSpan(int x0, int y0, int x1, int y1, int x2, int y2, int y, int &ax, int &bx)
2328
+ {
2329
+ const bool second = y >= y1;
2330
+ const int segmentHeight = std::max(1, second ? y2 - y1 : y1 - y0);
2331
+ const float alpha = triangleRatio(y - y0, y2 - y0);
2332
+ const float beta = triangleRatio(y - (second ? y1 : y0), segmentHeight);
2333
+ ax = x0 + static_cast<int>((x2 - x0) * alpha);
2334
+ bx = second ? x1 + static_cast<int>((x2 - x1) * beta) : x0 + static_cast<int>((x1 - x0) * beta);
2335
+ if (ax > bx)
2336
+ std::swap(ax, bx);
2337
+ }
2338
+
2339
+ } // namespace
2340
+
2213
2341
  void Canvas::fillTriangle(int x0, int y0, int x1, int y1, int x2, int y2, pixel::native_t color)
2214
2342
  {
2215
- if (!pixels_) return;
2216
- if (y0 > y1) { std::swap(x0, x1); std::swap(y0, y1); }
2217
- if (y0 > y2) { std::swap(x0, x2); std::swap(y0, y2); }
2218
- if (y1 > y2) { std::swap(x1, x2); std::swap(y1, y2); }
2343
+ if (!pixels_)
2344
+ return;
2345
+ if (y0 > y1)
2346
+ {
2347
+ std::swap(x0, x1);
2348
+ std::swap(y0, y1);
2349
+ }
2350
+ if (y0 > y2)
2351
+ {
2352
+ std::swap(x0, x2);
2353
+ std::swap(y0, y2);
2354
+ }
2355
+ if (y1 > y2)
2356
+ {
2357
+ std::swap(x1, x2);
2358
+ std::swap(y1, y2);
2359
+ }
2219
2360
  int total_h = y2 - y0;
2220
- if (total_h == 0) {
2361
+ if (total_h == 0)
2362
+ {
2221
2363
  int mn = x0 < x1 ? (x0 < x2 ? x0 : x2) : (x1 < x2 ? x1 : x2);
2222
2364
  int mx = x0 > x1 ? (x0 > x2 ? x0 : x2) : (x1 > x2 ? x1 : x2);
2223
2365
  fillRect(mn, y0, mx - mn + 1, 1, color);
@@ -2228,77 +2370,110 @@ void Canvas::fillTriangle(int x0, int y0, int x1, int y1, int x2, int y2, pixel:
2228
2370
  const ClipRect *clip = &clipStack_[clipDepth_];
2229
2371
  int draw_y0 = y0;
2230
2372
  int draw_y1 = y2;
2231
- if (draw_y0 < clip->y0) draw_y0 = clip->y0;
2232
- if (draw_y1 > clip->y1) draw_y1 = clip->y1;
2233
- if (draw_y0 < 0) draw_y0 = 0;
2234
- if (draw_y1 >= height_) draw_y1 = height_ - 1;
2235
- if (draw_y0 > draw_y1) return;
2236
- if (globalAlpha_ != 255 && globalAlpha_ != 0) markDirtyClipped(min_x, y0, max_x, y2);
2237
- for (int y = draw_y0; y <= draw_y1; y++) {
2238
- int second = (y >= y1);
2239
- int seg_h = second ? (y2 - y1) : (y1 - y0);
2240
- if (seg_h == 0) seg_h = 1;
2241
- float al = (float)(y - y0) / total_h;
2242
- float beta = second ? (float)(y - y1) / seg_h : (float)(y - y0) / seg_h;
2243
- int ax = x0 + (int)((x2 - x0) * al);
2244
- int bx = second ? x1 + (int)((x2 - x1) * beta) : x0 + (int)((x1 - x0) * beta);
2245
- if (ax > bx) { int t = ax; ax = bx; bx = t; }
2246
- if (ax < clip->x0) ax = clip->x0;
2247
- if (bx > clip->x1) bx = clip->x1;
2248
- if (ax < 0) ax = 0;
2249
- if (bx >= width_) bx = width_ - 1;
2250
- if (ax > bx) continue;
2373
+ if (draw_y0 < clip->y0)
2374
+ draw_y0 = clip->y0;
2375
+ if (draw_y1 > clip->y1)
2376
+ draw_y1 = clip->y1;
2377
+ if (draw_y0 < 0)
2378
+ draw_y0 = 0;
2379
+ if (draw_y1 >= height_)
2380
+ draw_y1 = height_ - 1;
2381
+ if (draw_y0 > draw_y1)
2382
+ return;
2383
+ if (globalAlpha_ != 255 && globalAlpha_ != 0)
2384
+ markDirtyClipped(min_x, y0, max_x, y2);
2385
+ for (int y = draw_y0; y <= draw_y1; y++)
2386
+ {
2387
+ int ax, bx;
2388
+ triangleRowSpan(x0, y0, x1, y1, x2, y2, y, ax, bx);
2389
+ if (ax < clip->x0)
2390
+ ax = clip->x0;
2391
+ if (bx > clip->x1)
2392
+ bx = clip->x1;
2393
+ if (ax < 0)
2394
+ ax = 0;
2395
+ if (bx >= width_)
2396
+ bx = width_ - 1;
2397
+ if (ax > bx)
2398
+ continue;
2251
2399
  const pixel::native_t c = color;
2252
2400
  #if GEA_EMBEDDED_DISPLAY_ROTATE_LANDSCAPE
2253
- if (landscapeRot_) {
2254
- if (globalAlpha_ == 255) {
2255
- for (int px = ax; px <= bx; px++) rotSet(px, y, c);
2401
+ if (landscapeRot_)
2402
+ {
2403
+ if (globalAlpha_ == 255)
2404
+ {
2405
+ for (int px = ax; px <= bx; px++)
2406
+ rotSet(px, y, c);
2256
2407
  markDirty(ax, y, bx, y);
2257
- } else if (globalAlpha_ != 0) {
2408
+ }
2409
+ else if (globalAlpha_ != 0)
2410
+ {
2258
2411
  const int a = globalAlpha_;
2259
- for (int px = ax; px <= bx; px++) rotBlend(px, y, c, a);
2412
+ for (int px = ax; px <= bx; px++)
2413
+ rotBlend(px, y, c, a);
2260
2414
  }
2261
2415
  continue;
2262
2416
  }
2263
2417
  #endif
2264
2418
  #if GEA_PIXEL_STORAGE_PACKED
2265
2419
  std::uint8_t *dnib = packedRow(rowToPhysical(y));
2266
- if (globalAlpha_ == 255) {
2420
+ if (globalAlpha_ == 255)
2421
+ {
2267
2422
  const int count = bx - ax + 1;
2268
2423
  int runStart = -1;
2269
- for (int i = 0; i < count; i++) {
2270
- if (pixel::packed::get(dnib, ax + i) == c) {
2271
- if (runStart >= 0) { markDirty(ax + runStart, y, ax + i - 1, y); runStart = -1; }
2424
+ for (int i = 0; i < count; i++)
2425
+ {
2426
+ if (pixel::packed::get(dnib, ax + i) == c)
2427
+ {
2428
+ if (runStart >= 0)
2429
+ {
2430
+ markDirty(ax + runStart, y, ax + i - 1, y);
2431
+ runStart = -1;
2432
+ }
2272
2433
  continue;
2273
2434
  }
2274
- if (runStart < 0) runStart = i;
2435
+ if (runStart < 0)
2436
+ runStart = i;
2275
2437
  pixel::packed::set(dnib, ax + i, c);
2276
2438
  }
2277
- if (runStart >= 0) markDirty(ax + runStart, y, bx, y);
2278
- } else if (globalAlpha_ != 0) {
2439
+ if (runStart >= 0)
2440
+ markDirty(ax + runStart, y, bx, y);
2441
+ }
2442
+ else if (globalAlpha_ != 0)
2443
+ {
2279
2444
  const int a = globalAlpha_;
2280
- for (int px = ax; px <= bx; px++) pixel::packed::blendPixel(dnib, px, c, a);
2445
+ for (int px = ax; px <= bx; px++)
2446
+ pixel::packed::blendPixel(dnib, px, c, a);
2281
2447
  }
2282
2448
  #else
2283
2449
  pixel::native_t *dst = &pixels_[rowToPhysical(y) * stride_ + ax];
2284
- if (globalAlpha_ == 255) {
2450
+ if (globalAlpha_ == 255)
2451
+ {
2285
2452
  const int count = bx - ax + 1;
2286
2453
  int runStart = -1;
2287
- for (int i = 0; i < count; i++) {
2288
- if (dst[i] == c) {
2289
- if (runStart >= 0) {
2454
+ for (int i = 0; i < count; i++)
2455
+ {
2456
+ if (dst[i] == c)
2457
+ {
2458
+ if (runStart >= 0)
2459
+ {
2290
2460
  markDirty(ax + runStart, y, ax + i - 1, y);
2291
2461
  runStart = -1;
2292
2462
  }
2293
2463
  continue;
2294
2464
  }
2295
- if (runStart < 0) runStart = i;
2465
+ if (runStart < 0)
2466
+ runStart = i;
2296
2467
  dst[i] = c;
2297
2468
  }
2298
- if (runStart >= 0) markDirty(ax + runStart, y, bx, y);
2299
- } else if (globalAlpha_ != 0) {
2469
+ if (runStart >= 0)
2470
+ markDirty(ax + runStart, y, bx, y);
2471
+ }
2472
+ else if (globalAlpha_ != 0)
2473
+ {
2300
2474
  const int a = globalAlpha_;
2301
- for (int px = ax; px <= bx; px++, dst++) *dst = pixel::blendNative(c, *dst, a);
2475
+ for (int px = ax; px <= bx; px++, dst++)
2476
+ *dst = pixel::blendNative(c, *dst, a);
2302
2477
  }
2303
2478
  #endif
2304
2479
  }
@@ -2307,15 +2482,23 @@ void Canvas::fillTriangle(int x0, int y0, int x1, int y1, int x2, int y2, pixel:
2307
2482
  // Present-replay variant: each flush chunk starts from scratch, so fillTriangle's
2308
2483
  // persistent-surface bookkeeping (per-pixel dst-compare + run-based markDirty)
2309
2484
  // is pure overhead there — flat span fills, one bbox markDirty.
2310
- // Edges step incrementally in 48.16 fixed point: one divide per edge instead of
2311
- // two float divides per scanline. The present replay rasterizes thousands of
2312
- // triangle rows per frame and those divides dominated the raster time. 64-bit
2313
- // accumulators keep arbitrary int coordinates overflow-safe.
2314
- void Canvas::fillTriangleOpaque(int x0, int y0, int x1, int y1, int x2, int y2, pixel::native_t color)
2485
+ // Span geometry shares fillTriangle's floating interpolation and truncation;
2486
+ // opaque replay skips persistent-surface comparisons and dirty run tracking.
2487
+ void Canvas::fillTriangleOpaque(int x0, int y0, int x1, int y1, int x2, int y2,
2488
+ pixel::native_t color)
2315
2489
  {
2316
- if (!pixels_) return;
2317
- if (y0 > y1) { std::swap(x0, x1); std::swap(y0, y1); }
2318
- if (y0 > y2) { std::swap(x0, x2); std::swap(y0, y2); }
2490
+ if (!pixels_)
2491
+ return;
2492
+ if (y0 > y1)
2493
+ {
2494
+ std::swap(x0, x1);
2495
+ std::swap(y0, y1);
2496
+ }
2497
+ if (y0 > y2)
2498
+ {
2499
+ std::swap(x0, x2);
2500
+ std::swap(y0, y2);
2501
+ }
2319
2502
  if (y1 > y2) { std::swap(x1, x2); std::swap(y1, y2); }
2320
2503
  const int total_h = y2 - y0;
2321
2504
  if (total_h == 0) {
@@ -2336,43 +2519,15 @@ void Canvas::fillTriangleOpaque(int x0, int y0, int x1, int y1, int x2, int y2,
2336
2519
  int clip_x1 = clip->x1 >= width_ ? width_ - 1 : clip->x1;
2337
2520
  if (clip_x0 > clip_x1) return;
2338
2521
 
2339
- // Long edge y0→y2 spans both segments with one continuous accumulator, so
2340
- // the two mesh triangles sharing it rasterize identical columns (no cracks).
2341
- const std::int64_t stepA = ((static_cast<std::int64_t>(x2) - x0) << 16) / total_h;
2342
- std::int64_t accA = (static_cast<std::int64_t>(x0) << 16) + stepA * (draw_y0 - y0);
2343
-
2344
2522
  int dirty_x0 = width_;
2345
2523
  int dirty_x1 = -1;
2346
- auto rasterRows = [&](int yStart, int yEnd, std::int64_t accB, std::int64_t stepB) {
2347
- for (int y = yStart; y <= yEnd; y++) {
2348
- int ax = static_cast<int>(accA >> 16);
2349
- int bx = static_cast<int>(accB >> 16);
2350
- accA += stepA;
2351
- accB += stepB;
2352
- if (ax > bx) { const int t = ax; ax = bx; bx = t; }
2353
- if (ax < clip_x0) ax = clip_x0;
2354
- if (bx > clip_x1) bx = clip_x1;
2355
- if (ax > bx) continue;
2356
- fillSpanColor(rowToPhysical(y), ax, bx - ax + 1, color);
2357
- if (ax < dirty_x0) dirty_x0 = ax;
2358
- if (bx > dirty_x1) dirty_x1 = bx;
2359
- }
2360
- };
2361
-
2362
- if (draw_y0 < y1) {
2363
- // Upper segment rows [y0, y1): draw_y0 < y1 with draw_y0 >= y0 implies y1 > y0.
2364
- const int yEnd = draw_y1 < y1 - 1 ? draw_y1 : y1 - 1;
2365
- const std::int64_t stepB = ((static_cast<std::int64_t>(x1) - x0) << 16) / (y1 - y0);
2366
- const std::int64_t accB = (static_cast<std::int64_t>(x0) << 16) + stepB * (draw_y0 - y0);
2367
- rasterRows(draw_y0, yEnd, accB, stepB);
2368
- }
2369
- if (draw_y1 >= y1) {
2370
- // Lower segment rows [y1, y2]; accA is already positioned at yStart.
2371
- const int yStart = draw_y0 > y1 ? draw_y0 : y1;
2372
- const int segh = y2 - y1;
2373
- const std::int64_t stepB = segh > 0 ? ((static_cast<std::int64_t>(x2) - x1) << 16) / segh : 0;
2374
- const std::int64_t accB = (static_cast<std::int64_t>(x1) << 16) + stepB * (yStart - y1);
2375
- rasterRows(yStart, draw_y1, accB, stepB);
2524
+ for (int y = draw_y0; y <= draw_y1; ++y) {
2525
+ int ax, bx;
2526
+ triangleRowSpan(x0, y0, x1, y1, x2, y2, y, ax, bx);
2527
+ ax = std::max(ax, clip_x0); bx = std::min(bx, clip_x1);
2528
+ if (ax > bx) continue;
2529
+ fillSpanColor(rowToPhysical(y), ax, bx - ax + 1, color);
2530
+ dirty_x0 = std::min(dirty_x0, ax); dirty_x1 = std::max(dirty_x1, bx);
2376
2531
  }
2377
2532
  if (dirty_x1 >= dirty_x0) markDirty(dirty_x0, draw_y0, dirty_x1, draw_y1);
2378
2533
  }
@@ -2471,33 +2626,11 @@ void Canvas::fillTrianglesOpaqueOccluded(const TriangleEntry *tris, int count, i
2471
2626
  if (mn <= mx) fillCovered(y0, mn, mx, color);
2472
2627
  continue;
2473
2628
  }
2474
- const std::int64_t stepA = ((static_cast<std::int64_t>(x2) - x0) << 16) / total_h;
2475
- std::int64_t accA = (static_cast<std::int64_t>(x0) << 16) + stepA * (dy0 - y0);
2476
- auto walk = [&](int yStart, int yEnd, std::int64_t accB, std::int64_t stepB) {
2477
- for (int y = yStart; y <= yEnd; y++) {
2478
- int ax = static_cast<int>(accA >> 16);
2479
- int bx = static_cast<int>(accB >> 16);
2480
- accA += stepA;
2481
- accB += stepB;
2482
- if (ax > bx) { const int t = ax; ax = bx; bx = t; }
2483
- if (ax < clip_x0) ax = clip_x0;
2484
- if (bx > clip_x1) bx = clip_x1;
2485
- if (ax > bx) continue;
2486
- fillCovered(y, ax, bx, color);
2487
- }
2488
- };
2489
- if (dy0 < y1) {
2490
- const int yEnd = dy1 < y1 - 1 ? dy1 : y1 - 1;
2491
- const std::int64_t stepB = ((static_cast<std::int64_t>(x1) - x0) << 16) / (y1 - y0);
2492
- const std::int64_t accB = (static_cast<std::int64_t>(x0) << 16) + stepB * (dy0 - y0);
2493
- walk(dy0, yEnd, accB, stepB);
2494
- }
2495
- if (dy1 >= y1) {
2496
- const int yStart = dy0 > y1 ? dy0 : y1;
2497
- const int segh = y2 - y1;
2498
- const std::int64_t stepB = segh > 0 ? ((static_cast<std::int64_t>(x2) - x1) << 16) / segh : 0;
2499
- const std::int64_t accB = (static_cast<std::int64_t>(x1) << 16) + stepB * (yStart - y1);
2500
- walk(yStart, dy1, accB, stepB);
2629
+ for (int y = dy0; y <= dy1; ++y) {
2630
+ int ax, bx;
2631
+ triangleRowSpan(x0, y0, x1, y1, x2, y2, y, ax, bx);
2632
+ ax = std::max(ax, clip_x0); bx = std::min(bx, clip_x1);
2633
+ if (ax <= bx) fillCovered(y, ax, bx, color);
2501
2634
  }
2502
2635
  }
2503
2636
  if (dirty_x1 >= dirty_x0 && dirty_y1 >= dirty_y0) markDirty(dirty_x0, dirty_y0, dirty_x1, dirty_y1);
@@ -2626,7 +2759,7 @@ void Canvas::drawTextFontFamilyRotated90(const char *text, int lx, int ly, pixel
2626
2759
  const int cp = nextUtf8Codepoint(p);
2627
2760
  if (cp == '\n') { pen_x = lx; pen_y += font.lineHeight(); continue; }
2628
2761
  Glyph glyph{};
2629
- if (!font.glyph(cp, &glyph)) { pen_x += font.sizePx() / 2; continue; }
2762
+ if (!font.glyph(cp, &glyph)) { pen_x += font.advance(cp, peekUtf8Codepoint(p)); continue; }
2630
2763
  const int gx = pen_x + glyph.bearingX;
2631
2764
  const int gy = pen_y + font.ascender() - glyph.bearingY;
2632
2765
  const pixel::native_t c = color;
@@ -2650,7 +2783,7 @@ void Canvas::drawTextFontFamilyRotated90(const char *text, int lx, int ly, pixel
2650
2783
  #endif
2651
2784
  }
2652
2785
  }
2653
- pen_x += glyph.advance;
2786
+ pen_x += font.advance(cp, peekUtf8Codepoint(p));
2654
2787
  }
2655
2788
  }
2656
2789
 
@@ -2686,7 +2819,7 @@ void Canvas::drawTextFontFamilyOnBackground(const char *text, int x, int y, pixe
2686
2819
 
2687
2820
  SolidGlyphCacheEntry *entry = solidGlyphCacheEntry(font, color, background, cp);
2688
2821
  if (!entry) {
2689
- penX += font.sizePx() / 2;
2822
+ penX += font.advance(cp, peekUtf8Codepoint(p));
2690
2823
  continue;
2691
2824
  }
2692
2825
 
@@ -2695,7 +2828,7 @@ void Canvas::drawTextFontFamilyOnBackground(const char *text, int x, int y, pixe
2695
2828
  const int gy = penY + font.ascender() - glyph.bearingY;
2696
2829
  if (gx > clipX1 || gy > clipY1 ||
2697
2830
  gx + glyph.width - 1 < clipX0 || gy + glyph.height - 1 < clipY0) {
2698
- penX += glyph.advance;
2831
+ penX += font.advance(cp, peekUtf8Codepoint(p));
2699
2832
  continue;
2700
2833
  }
2701
2834
 
@@ -2741,7 +2874,7 @@ void Canvas::drawTextFontFamilyOnBackground(const char *text, int x, int y, pixe
2741
2874
  }
2742
2875
 
2743
2876
  if (dirtyX0 <= dirtyX1 && dirtyY0 <= dirtyY1) markDirty(dirtyX0, dirtyY0, dirtyX1, dirtyY1);
2744
- penX += glyph.advance;
2877
+ penX += font.advance(cp, peekUtf8Codepoint(p));
2745
2878
  }
2746
2879
  }
2747
2880
 
@@ -2996,7 +3129,7 @@ void Canvas::drawRasterizedText(const char *text, int x, int y, pixel::native_t
2996
3129
  const int cp = nextUtf8Codepoint(p);
2997
3130
  if (cp == '\n') { pen_x = x; pen_y += font.lineHeight(); continue; }
2998
3131
  Glyph glyph{};
2999
- if (!font.glyph(cp, &glyph)) { pen_x += font.sizePx() / 2; continue; }
3132
+ if (!font.glyph(cp, &glyph)) { pen_x += font.advance(cp, peekUtf8Codepoint(p)); continue; }
3000
3133
 
3001
3134
  int gx = pen_x + glyph.bearingX;
3002
3135
  int gy = pen_y + font.ascender() - glyph.bearingY;
@@ -3004,7 +3137,7 @@ void Canvas::drawRasterizedText(const char *text, int x, int y, pixel::native_t
3004
3137
  /* Skip glyph entirely if outside clip */
3005
3138
  if (gx > clip->x1 || gy > clip->y1 ||
3006
3139
  gx + glyph.width - 1 < clip->x0 || gy + glyph.height - 1 < clip->y0) {
3007
- pen_x += glyph.advance;
3140
+ pen_x += font.advance(cp, peekUtf8Codepoint(p));
3008
3141
  continue;
3009
3142
  }
3010
3143
 
@@ -3050,7 +3183,7 @@ void Canvas::drawRasterizedText(const char *text, int x, int y, pixel::native_t
3050
3183
  stampPackedGlyphRow(packedRow(rowToPhysical(gy + row)), gx,
3051
3184
  src + static_cast<std::size_t>(row) * rb, glyph.width);
3052
3185
  markDirty(gx, gy, gx + glyph.width - 1, gy + glyph.height - 1);
3053
- pen_x += glyph.advance;
3186
+ pen_x += font.advance(cp, peekUtf8Codepoint(p));
3054
3187
  continue;
3055
3188
  }
3056
3189
  }
@@ -3093,7 +3226,7 @@ void Canvas::drawRasterizedText(const char *text, int x, int y, pixel::native_t
3093
3226
  }
3094
3227
  markDirty(gx + col_start, gy + row_start,
3095
3228
  gx + col_end, gy + row_end);
3096
- pen_x += glyph.advance;
3229
+ pen_x += font.advance(cp, peekUtf8Codepoint(p));
3097
3230
  }
3098
3231
  }
3099
3232
 
@@ -3269,8 +3402,7 @@ void Canvas::measureTextFont(const char *text, int max_width, int font_id, int *
3269
3402
  lines++;
3270
3403
  continue;
3271
3404
  }
3272
- Glyph glyph{};
3273
- int adv = font.glyph(cp, &glyph) ? glyph.advance : (font.sizePx() / 2);
3405
+ int adv = font.advance(cp, peekUtf8Codepoint(p));
3274
3406
  int next_w = line_w + adv;
3275
3407
  if (next_w > max_width && line_w > 0) {
3276
3408
  if (line_w > max_line_w) max_line_w = line_w;
@@ -3308,7 +3440,7 @@ void Canvas::drawTextFontFamily(const char *text, int x, int y, pixel::native_t
3308
3440
  const int cp = nextUtf8Codepoint(p);
3309
3441
  if (cp == '\n') { pen_x = x; pen_y += font.lineHeight(); continue; }
3310
3442
  Glyph glyph{};
3311
- if (!font.glyph(cp, &glyph)) { pen_x += font.sizePx() / 2; continue; }
3443
+ if (!font.glyph(cp, &glyph)) { pen_x += font.advance(cp, peekUtf8Codepoint(p)); continue; }
3312
3444
  const int gx = pen_x + glyph.bearingX;
3313
3445
  const int gy = pen_y + font.ascender() - glyph.bearingY;
3314
3446
  const pixel::native_t c = color;
@@ -3336,7 +3468,7 @@ void Canvas::drawTextFontFamily(const char *text, int x, int y, pixel::native_t
3336
3468
  #endif
3337
3469
  }
3338
3470
  }
3339
- pen_x += glyph.advance;
3471
+ pen_x += font.advance(cp, peekUtf8Codepoint(p));
3340
3472
  }
3341
3473
  return;
3342
3474
  }
@@ -3362,7 +3494,7 @@ void Canvas::drawTextFontFamilyRotated90(const char *text, int lx, int ly, pixel
3362
3494
  const int cp = nextUtf8Codepoint(p);
3363
3495
  if (cp == '\n') { pen_x = lx; pen_y += font.lineHeight(); continue; }
3364
3496
  Glyph glyph{};
3365
- if (!font.glyph(cp, &glyph)) { pen_x += font.sizePx() / 2; continue; }
3497
+ if (!font.glyph(cp, &glyph)) { pen_x += font.advance(cp, peekUtf8Codepoint(p)); continue; }
3366
3498
  const int gx = pen_x + glyph.bearingX;
3367
3499
  const int gy = pen_y + font.ascender() - glyph.bearingY;
3368
3500
  const pixel::native_t c = color;
@@ -3386,7 +3518,7 @@ void Canvas::drawTextFontFamilyRotated90(const char *text, int lx, int ly, pixel
3386
3518
  #endif
3387
3519
  }
3388
3520
  }
3389
- pen_x += glyph.advance;
3521
+ pen_x += font.advance(cp, peekUtf8Codepoint(p));
3390
3522
  }
3391
3523
  }
3392
3524
 
@@ -3421,10 +3553,9 @@ int Canvas::measureTextFontFamily(const char *text, int family_id, int size_px)
3421
3553
  lineWidth = 0;
3422
3554
  continue;
3423
3555
  }
3424
- Glyph glyph{};
3425
3556
  // Same fallback the draw loop uses for a missing glyph, so measure and
3426
3557
  // draw agree rather than drifting apart on unmapped codepoints.
3427
- lineWidth += font.glyph(cp, &glyph) ? glyph.advance : font.sizePx() / 2;
3558
+ lineWidth += font.advance(cp, peekUtf8Codepoint(p));
3428
3559
  }
3429
3560
  return lineWidth > width ? lineWidth : width;
3430
3561
  }