@geastack/engine 0.1.5 → 0.1.7

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.
Files changed (45) hide show
  1. package/README.md +70 -0
  2. package/canvas.cpp +86 -30
  3. package/css/animation.h +64 -0
  4. package/css/declarative.h +6 -0
  5. package/css/engine.h +25 -1
  6. package/image_store.cpp +136 -33
  7. package/package.json +2 -2
  8. package/ui/CSS_FEATURES.md +261 -0
  9. package/ui/absolute_leaf_refresh.cpp +42 -39
  10. package/ui/core.cpp +32 -1
  11. package/ui/css_atom.cpp +6 -0
  12. package/ui/css_atom.h +2 -0
  13. package/ui/css_features.h +282 -0
  14. package/ui/display_invalidation.cpp +29 -0
  15. package/ui/flex_balance.h +73 -0
  16. package/ui/image_node.cpp +33 -28
  17. package/ui/input.cpp +88 -38
  18. package/ui/input_render.cpp +17 -13
  19. package/ui/internal.h +51 -5
  20. package/ui/layout.cpp +2601 -441
  21. package/ui/layout_snapshot.cpp +49 -6
  22. package/ui/node_features.h +18 -0
  23. package/ui/node_lifecycle.cpp +204 -71
  24. package/ui/node_lifecycle.h +1 -0
  25. package/ui/node_model.h +930 -70
  26. package/ui/node_text.h +82 -0
  27. package/ui/render.cpp +793 -352
  28. package/ui/renderer_features.h +19 -0
  29. package/ui/root_scroll_refresh.cpp +46 -24
  30. package/ui/style.cpp +4155 -1073
  31. package/ui/style.h +143 -6
  32. package/ui/style_values.h +16 -0
  33. package/ui/text.cpp +401 -106
  34. package/ui/tree_events.cpp +138 -95
  35. package/ui/tree_events.h +28 -19
  36. package/ui/tree_inspection.h +15 -0
  37. package/ui/tree_internal.h +19 -0
  38. package/ui/tree_nodes.cpp +38 -4
  39. package/ui/tree_render.cpp +119 -40
  40. package/ui/tree_scroll_mirror.cpp +26 -0
  41. package/ui/tree_state.cpp +261 -114
  42. package/ui/tree_state.h +121 -16
  43. package/ui/tree_style.cpp +609 -82
  44. package/ui/view.cpp +720 -299
  45. package/ui/virtual_keyboard.cpp +10 -3
package/ui/text.cpp CHANGED
@@ -14,6 +14,11 @@
14
14
  #include <cstring>
15
15
  #include <string>
16
16
 
17
+ // ESP-IDF system headers may define quad as a compatibility type macro.
18
+ #ifdef quad
19
+ #undef quad
20
+ #endif
21
+
17
22
  #ifndef GEA_EMBEDDED_RENDER_HOT_SRAM
18
23
  #define GEA_EMBEDDED_RENDER_HOT_SRAM 0
19
24
  #endif
@@ -46,26 +51,29 @@ struct ScopedRefreshStat
46
51
 
47
52
  bool hasTransformState(const Node &node)
48
53
  {
49
- return rstyle(node.style).transform_rotate != 0 ||
54
+ return hasIndividualLinearTransform(rstyle(node.style)) || hadIndividualLinearTransform(node.render) ||
55
+ rstyle(node.style).transform_rotate != 0 ||
50
56
  node.render.previous_transform_rotate != 0 ||
51
57
  rstyle(node.style).transform_rotate_x != 0 ||
52
58
  node.render.previous_transform_rotate_x != 0 ||
53
59
  rstyle(node.style).transform_rotate_y != 0 ||
54
60
  node.render.previous_transform_rotate_y != 0 ||
55
- rstyle(node.style).transform_translate_x != 0 ||
61
+ composedTranslateX(rstyle(node.style)) != 0 ||
56
62
  node.render.previous_transform_translate_x != 0 ||
57
- rstyle(node.style).transform_translate_y != 0 ||
63
+ composedTranslateY(rstyle(node.style)) != 0 ||
58
64
  node.render.previous_transform_translate_y != 0 ||
59
- rstyle(node.style).transform_translate_z != 0 ||
65
+ composedTranslateZ(rstyle(node.style)) != 0 ||
60
66
  node.render.previous_transform_translate_z != 0 ||
61
- rstyle(node.style).transform_translate_x_percent != 0 ||
67
+ composedTranslateXPercent(rstyle(node.style)) != 0 ||
62
68
  node.render.previous_transform_translate_x_percent != 0 ||
63
- rstyle(node.style).transform_translate_y_percent != 0 ||
69
+ composedTranslateYPercent(rstyle(node.style)) != 0 ||
64
70
  node.render.previous_transform_translate_y_percent != 0 ||
65
71
  rstyle(node.style).transform_scale_x != 1000 ||
66
72
  node.render.previous_transform_scale_x != 1000 ||
67
73
  rstyle(node.style).transform_scale_y != 1000 ||
74
+ rstyle(node.style).transform_scale_z != 1000 ||
68
75
  node.render.previous_transform_scale_y != 1000 ||
76
+ node.render.previous_transform_scale_z != 1000 ||
69
77
  rstyle(node.style).perspective > 0 ||
70
78
  node.render.previous_perspective > 0;
71
79
  }
@@ -207,19 +215,20 @@ bool currentTransformChainIsPureTranslate(const Node &node, float *outX, float *
207
215
  bool anyTranslate = false;
208
216
  for (;;) {
209
217
  const Node &n = id >= 0 ? tree.node(id) : node;
210
- if ((rstyle(n.style).transform_rotate % 3600) != 0 ||
218
+ if (hasIndividualLinearTransform(rstyle(n.style)) || (rstyle(n.style).transform_rotate % 3600) != 0 ||
211
219
  (rstyle(n.style).transform_rotate_x % 3600) != 0 ||
212
220
  (rstyle(n.style).transform_rotate_y % 3600) != 0 ||
213
- rstyle(n.style).transform_translate_z != 0 ||
221
+ composedTranslateZ(rstyle(n.style)) != 0 ||
214
222
  rstyle(n.style).transform_scale_x != 1000 ||
215
223
  rstyle(n.style).transform_scale_y != 1000 ||
224
+ rstyle(n.style).transform_scale_z != 1000 ||
216
225
  rstyle(n.style).perspective > 0)
217
226
  return false;
218
227
 
219
- const float localDx = static_cast<float>(rstyle(n.style).transform_translate_x) +
220
- static_cast<float>(n.layout.width) * static_cast<float>(rstyle(n.style).transform_translate_x_percent) * 0.001f;
221
- const float localDy = static_cast<float>(rstyle(n.style).transform_translate_y) +
222
- static_cast<float>(n.layout.height) * static_cast<float>(rstyle(n.style).transform_translate_y_percent) * 0.001f;
228
+ const float localDx = static_cast<float>(composedTranslateX(rstyle(n.style))) +
229
+ static_cast<float>(n.layout.width) * static_cast<float>(composedTranslateXPercent(rstyle(n.style))) * 0.001f;
230
+ const float localDy = static_cast<float>(composedTranslateY(rstyle(n.style))) +
231
+ static_cast<float>(n.layout.height) * static_cast<float>(composedTranslateYPercent(rstyle(n.style))) * 0.001f;
223
232
  if (localDx != 0.0f || localDy != 0.0f) {
224
233
  anyTranslate = true;
225
234
  dx += localDx;
@@ -322,11 +331,7 @@ struct WrappedLine {
322
331
  int renderBytes = 0;
323
332
  int consumedBytes = 0;
324
333
  int width = 0;
325
- // True when the line had to cut a word open because no break opportunity fit.
326
- // An inline formatting context uses this to move a run that cannot start on
327
- // the space left to the next line box, instead of splitting "technical" into
328
- // "t" + "echnical".
329
- bool hardSplit = false;
334
+
330
335
  };
331
336
 
332
337
  inline bool isWrappingSpace(int cp)
@@ -341,8 +346,8 @@ inline bool canBreakAfter(int cp)
341
346
  }
342
347
 
343
348
  // CSS white-space: normal/pre-line line breaking for the embedded renderer.
344
- // Prefer the last legal word boundary; only split a word when that single word
345
- // is wider than the line. `consumedBytes` includes discarded wrapping spaces or
349
+ // Break at legal word boundaries. An unbreakable word can overflow the line;
350
+ // normal wrapping must never manufacture a break inside that word. `consumedBytes` includes discarded wrapping spaces or
346
351
  // an explicit newline, while `renderBytes` contains only the visible run.
347
352
  template <typename AdvanceForCodepoint>
348
353
  WrappedLine nextWrappedLine(const char *text, int maxWidth, AdvanceForCodepoint advanceForCodepoint)
@@ -381,17 +386,11 @@ WrappedLine nextWrappedLine(const char *text, int maxWidth, AdvanceForCodepoint
381
386
  // renderer re-wraps the run at that width, so a disagreement drew one
382
387
  // more line than the box was tall and the run overlapped its next
383
388
  // sibling (typography's specimen paragraphs).
384
- if (!isWrappingSpace(cp) && width + advance > maxWidth && bytes > 0) {
385
- if (breakRenderBytes >= 0) {
386
- line.renderBytes = breakRenderBytes;
387
- line.consumedBytes = breakConsumedBytes;
388
- line.width = breakWidth;
389
- } else {
390
- line.renderBytes = bytes;
391
- line.consumedBytes = bytes;
392
- line.width = width;
393
- line.hardSplit = true;
394
- }
389
+ // Leading collapsible spaces cannot be an empty soft-wrapped line.
390
+ if (!isWrappingSpace(cp) && width + advance > maxWidth && bytes > 0 && breakRenderBytes > 0) {
391
+ line.renderBytes = breakRenderBytes;
392
+ line.consumedBytes = breakConsumedBytes;
393
+ line.width = breakWidth;
395
394
  return line;
396
395
  }
397
396
 
@@ -479,8 +478,34 @@ std::string transformedTextCopy(const char *text, int textTransform)
479
478
  return out;
480
479
  }
481
480
 
482
- const char *textWithTransform(const char *text, int textTransform, std::string &storage)
481
+ const char *textWithTransform(const char *text, int textTransform, std::string &storage, int whiteSpace = -1)
483
482
  {
483
+ // Perform CSS segment-break/space collapsing identically in measurement
484
+ // and painting, while retaining the authored Node::text for later mutation.
485
+ // Edge trimming belongs to line layout, not to individual inline runs.
486
+ if (text && (whiteSpace == 0 || whiteSpace == 1 || whiteSpace == 4) &&
487
+ (std::strpbrk(text, "\t\r\n\f") || std::strstr(text, " "))) {
488
+ std::string normalized;
489
+ normalized.reserve(std::strlen(text));
490
+ bool space = false;
491
+ for (const char *p = text; *p; ++p) {
492
+ char c = *p;
493
+ if (c == '\r') { if (p[1] == '\n') ++p; c = '\n'; }
494
+ if (whiteSpace == 4 && (c == '\n' || c == '\f')) {
495
+ if (!normalized.empty() && normalized.back() == ' ') normalized.pop_back();
496
+ normalized += '\n';
497
+ space = true;
498
+ } else if (c == ' ' || c == '\t' || c == '\n' || c == '\f') {
499
+ if (!space) normalized += ' ';
500
+ space = true;
501
+ } else {
502
+ normalized += c;
503
+ space = false;
504
+ }
505
+ }
506
+ storage = textTransform ? transformedTextCopy(normalized.c_str(), textTransform) : std::move(normalized);
507
+ return storage.c_str();
508
+ }
484
509
  if (!text || textTransform == 0) return text;
485
510
  storage = transformedTextCopy(text, textTransform);
486
511
  return storage.c_str();
@@ -684,26 +709,25 @@ int rasterizedSingleLineInkCenterOffsetY(const char *text,
684
709
  return offset;
685
710
  }
686
711
 
687
- bool recordProjectedRasterText(const Node &node, uint8_t /*parentAlpha*/, int tx, int ty, int tw)
712
+ bool recordProjectedRasterText(const Node &node, const char *text, uint8_t /*parentAlpha*/, int tx, int ty, int tw)
688
713
  {
689
- if (node.style.font_id < 0 || node.text.find('\n') != std::string::npos) return false;
714
+ if (node.style.font_id < 0 || std::strchr(text, '\n')) return false;
690
715
  const int fontSize = node.style.font_size > 0 ? node.style.font_size : kBitmapFontHeight;
691
716
  gea::framework::graphics::RasterizedFont font =
692
717
  gea::framework::graphics::FontRegistry::rasterizedFamily(node.style.font_id, fontSize);
693
718
  if (!font.valid()) return false;
694
719
 
695
720
  int lineWidth = 0;
696
- bool wordStart = true;
697
- for (const char *p = node.text.c_str(); *p;) {
721
+ for (const char *p = text; *p;) {
698
722
  gea::framework::graphics::Glyph glyph{};
699
- const int cp = transformedTextCodepoint(nextUtf8Codepoint(p), node.style.text_transform, wordStart);
723
+ const int cp = nextUtf8Codepoint(p);
700
724
  lineWidth += font.glyph(cp, &glyph) ? glyph.advance : (font.sizePx() / 2);
701
725
  }
702
726
 
703
727
  int bx0, by0, bx1, by1;
704
728
  ViewRenderer::transformedBounds(node, false, &bx0, &by0, &bx1, &by1);
705
729
 
706
- if (lineWidth <= 0 || node.style.text_alpha == 0) return true;
730
+ if (lineWidth <= 0) return true;
707
731
 
708
732
  const int fontLineHeight = font.lineHeight();
709
733
  const int lineAdvance = node.style.line_height > 0 ? node.style.line_height : fontLineHeight;
@@ -743,19 +767,10 @@ bool recordProjectedRasterText(const Node &node, uint8_t /*parentAlpha*/, int tx
743
767
  cmd->projectedText.color = node.style.text_color;
744
768
  cmd->projectedText.alpha = node.style.text_alpha;
745
769
  cmd->projectedText.textTransform = node.style.text_transform;
746
- // CSS flattening: the label is painted into its transformed ancestor's plane
747
- // (nothing below the face preserves 3D), so backface-visibility:hidden on any
748
- // ancestor in the projection chain culls the flattened text together with its
749
- // face. The property does not inherit, so the span itself never carries it —
750
- // reading only node.style left labels floating over culled faces.
751
- {
752
- bool backfaceHidden = node.style.backface_hidden != 0;
753
- const Tree &tree = Tree::instance();
754
- const Node *nodes = tree.nodes();
755
- for (int a = node.parent; !backfaceHidden && a >= 0 && a < tree.nodeCount(); a = nodes[a].parent)
756
- backfaceHidden = nodes[a].style.backface_hidden != 0;
757
- cmd->projectedText.backfaceHidden = backfaceHidden ? 1 : 0;
758
- }
770
+ cmd->projectedText.whiteSpace = node.style.white_space;
771
+ // Flattened ancestor groups are culled by the recorder for every paint type.
772
+ // This command's flag describes only its own face in a preserve-3d group.
773
+ cmd->projectedText.backfaceHidden = node.style.backface_hidden ? 1 : 0;
759
774
  return true;
760
775
  }
761
776
  #endif
@@ -979,11 +994,11 @@ TextLayoutMeasure measureTextLayoutForNode(const Node &node, const char *text, i
979
994
  {
980
995
  if (contentWidth < 0) contentWidth = 0;
981
996
  std::string transformed;
982
- const char *measureText = textWithTransform(text, node.style.text_transform, transformed);
997
+ const char *measureText = textWithTransform(text, node.style.text_transform, transformed, node.style.white_space);
983
998
  // white-space: nowrap lays the run out as a single line — measure against an
984
999
  // unbounded width so the content box sizes to the full text (then max-width /
985
1000
  // the parent box clamps it). Drawing truncates/ellipsizes the overflow.
986
- const int measureWidth = node.style.white_space == 1 ? 32767 : contentWidth;
1001
+ const int measureWidth = (node.style.white_space == 1 || node.style.white_space == 2) ? 32767 : contentWidth;
987
1002
  const TextMeasure measured = TextMetrics::measure(measureText,
988
1003
  measureWidth,
989
1004
  node.style.font_id,
@@ -991,14 +1006,14 @@ TextLayoutMeasure measureTextLayoutForNode(const Node &node, const char *text, i
991
1006
  node.style.line_height,
992
1007
  node.style.font_weight);
993
1008
  TextLayoutMeasure out;
994
- out.width = measured.width + node.style.padding[1] + node.style.padding[3];
995
- out.height = measured.height + node.style.padding[0] + node.style.padding[2];
996
- if (node.style.width != kUnset) out.width = node.style.width;
997
- else if (node.style.width_percent != kUnset && node.layout.width > 0) out.width = node.layout.width;
998
- if (node.style.height != kUnset) out.height = node.style.height;
999
- else if (node.style.height_percent != kUnset && node.layout.height > 0) out.height = node.layout.height;
1000
- out.width = LayoutEngine::instance().clampSize(out.width, node.style.min_width, node.style.max_width);
1001
- out.height = LayoutEngine::instance().clampSize(out.height, node.style.min_height, node.style.max_height);
1009
+ out.width = measured.width + boxInset(node.style, 1) + boxInset(node.style, 3);
1010
+ out.height = measured.height + boxInset(node.style, 0) + boxInset(node.style, 2);
1011
+ if (node.style.width != kUnset) out.width = contentSizeToBorderSize(node.style, node.style.width, true);
1012
+ else if ((node.style.width_percent != kUnset || node.style.width_expression >= 0) && node.layout.width > 0) out.width = node.layout.width;
1013
+ if (node.style.height != kUnset) out.height = contentSizeToBorderSize(node.style, node.style.height, false);
1014
+ else if ((node.style.height_percent != kUnset || node.style.height_expression >= 0) && node.layout.height > 0) out.height = node.layout.height;
1015
+ out.width = clampBorderBoxSize(node.style, out.width, true);
1016
+ out.height = clampBorderBoxSize(node.style, out.height, false);
1002
1017
  return out;
1003
1018
  }
1004
1019
 
@@ -1006,7 +1021,7 @@ int measureSingleLineTextWidthForNode(const Node &node, const char *text)
1006
1021
  {
1007
1022
  if (!text || !text[0]) return 0;
1008
1023
  std::string transformed;
1009
- const char *measureText = textWithTransform(text, node.style.text_transform, transformed);
1024
+ const char *measureText = textWithTransform(text, node.style.text_transform, transformed, node.style.white_space);
1010
1025
  return TextMetrics::measure(measureText,
1011
1026
  32767,
1012
1027
  node.style.font_id,
@@ -1015,19 +1030,124 @@ int measureSingleLineTextWidthForNode(const Node &node, const char *text)
1015
1030
  node.style.font_weight).width;
1016
1031
  }
1017
1032
 
1033
+ struct TextCoverageSink {
1034
+ int screenY = 0;
1035
+ int screenX = 0;
1036
+ int width = 0;
1037
+ int clipX0 = 0;
1038
+ int clipY0 = 0;
1039
+ int clipX1 = -1;
1040
+ int clipY1 = -1;
1041
+ std::uint8_t *coverage = nullptr;
1042
+
1043
+ void add(int x, int y, std::uint8_t value) const
1044
+ {
1045
+ if (!coverage || value == 0 || y != screenY || y < clipY0 || y > clipY1 ||
1046
+ x < screenX || x >= screenX + width || x < clipX0 || x > clipX1) return;
1047
+ auto &dst = coverage[x - screenX];
1048
+ if (value > dst) dst = value;
1049
+ }
1050
+ };
1051
+
1018
1052
  class TextDrawer {
1019
1053
  public:
1020
- static void drawWrapped(const char *text, int x, int y, int maxWidth, gea::framework::graphics::pixel::native_t color, float scale, int textAlign, int containerWidth, int fontId, int textTransform, int lineHeight, int whiteSpace, int textOverflow, int maxHeight = 0, int firstLineIndent = 0)
1054
+ static void unionCoverageRow(const DisplayCommand &command, int screenY, int screenX,
1055
+ int width, std::uint8_t *outCoverage)
1056
+ {
1057
+ if (!outCoverage || width <= 0) return;
1058
+ if (command.textDecorationInk) {
1059
+ if (command.type != DisplayCommandType::FillRect) return;
1060
+ if (screenY < command.fill.y || screenY >= command.fill.y + command.fill.h) return;
1061
+ int clipX0, clipY0, clipX1, clipY1;
1062
+ gea::platform::display::Display::clip(&clipX0, &clipY0, &clipX1, &clipY1);
1063
+ if (screenY < clipY0 || screenY > clipY1) return;
1064
+ const int left = std::max({screenX, static_cast<int>(command.fill.x), clipX0});
1065
+ const int right = std::min({screenX + width, static_cast<int>(command.fill.x) + command.fill.w, clipX1 + 1});
1066
+ for (int x = left; x < right; ++x) outCoverage[x - screenX] = 255;
1067
+ return;
1068
+ }
1069
+ if (command.type != DisplayCommandType::DrawText) return;
1070
+ TextCoverageSink sink;
1071
+ sink.screenY = screenY;
1072
+ sink.screenX = screenX;
1073
+ sink.width = width;
1074
+ sink.coverage = outCoverage;
1075
+ drawWrapped(command.text.text, command.text.x, command.text.y, command.text.maxWidth,
1076
+ command.text.color, command.text.scale, command.text.align, command.text.containerWidth,
1077
+ command.text.fontId, command.text.textTransform, command.text.lineHeight,
1078
+ command.text.whiteSpace, command.text.textOverflow, command.text.maxHeight,
1079
+ command.text.firstLineIndent, &sink);
1080
+ }
1081
+
1082
+ #ifdef GEA_EMBEDDED_HAS_GENERATED_FONTS
1083
+ static void rasterizedGlyphRun(const char *text, int x, int y, int fontId, int fontSize,
1084
+ const TextCoverageSink &sink)
1085
+ {
1086
+ gea::framework::graphics::RasterizedFont font =
1087
+ gea::framework::graphics::FontRegistry::rasterizedFamily(fontId, fontSize);
1088
+ if (!font.valid()) return;
1089
+ int penX = x;
1090
+ for (const char *p = text; p && *p;) {
1091
+ const int cp = nextUtf8Codepoint(p);
1092
+ gea::framework::graphics::Glyph glyph{};
1093
+ if (!font.glyph(cp, &glyph)) { penX += font.sizePx() / 2; continue; }
1094
+ const int gx = penX + glyph.bearingX;
1095
+ const int gy = y + font.ascender() - glyph.bearingY;
1096
+ const int row = sink.screenY - gy;
1097
+ if (row >= 0 && row < glyph.height)
1098
+ for (int col = 0; col < glyph.width; ++col)
1099
+ sink.add(gx + col, sink.screenY, font.coverage(glyph, row, col));
1100
+ penX += glyph.advance;
1101
+ }
1102
+ }
1103
+ #endif
1104
+
1105
+ static void bitmapGlyphRun(const char *text, int x, int y, float scale,
1106
+ const TextCoverageSink &sink)
1107
+ {
1108
+ if (scale < 0.1f) scale = 1.0f;
1109
+ const int glyphWidth = std::max(1, static_cast<int>(kBitmapFontWidth * scale + 0.5f));
1110
+ const int glyphHeight = std::max(1, static_cast<int>(kBitmapFontHeight * scale + 0.5f));
1111
+ const int row = sink.screenY - y;
1112
+ if (row < 0 || row >= glyphHeight) return;
1113
+ const int srcRow = row * kBitmapFontHeight / glyphHeight;
1114
+ const auto &font = gea::framework::graphics::FontRegistry::bitmap8x16();
1115
+ int penX = x;
1116
+ for (const char *p = text; p && *p;) {
1117
+ const int cp = nextUtf8Codepoint(p);
1118
+ if (cp == '\n') { penX = x; continue; }
1119
+ gea::framework::graphics::Glyph glyph{};
1120
+ font.glyph(cp, &glyph);
1121
+ for (int col = 0; col < glyphWidth; ++col) {
1122
+ const int srcCol = col * kBitmapFontWidth / glyphWidth;
1123
+ sink.add(penX + col, sink.screenY, font.coverage(glyph, srcRow, srcCol));
1124
+ }
1125
+ penX += glyphWidth;
1126
+ }
1127
+ }
1128
+
1129
+ static void drawWrapped(const char *text, int x, int y, int maxWidth, gea::framework::graphics::pixel::native_t color, float scale, int textAlign, int containerWidth, int fontId, int textTransform, int lineHeight, int whiteSpace, int textOverflow, int maxHeight = 0, int firstLineIndent = 0, TextCoverageSink *coverageSink = nullptr)
1021
1130
  {
1022
1131
  if (!text || !text[0]) return;
1023
1132
  std::string transformed;
1024
- text = textWithTransform(text, textTransform, transformed);
1133
+ text = textWithTransform(text, textTransform, transformed, whiteSpace);
1134
+ // Pre preserves forced breaks and paints every line without soft wrap.
1135
+ if (whiteSpace == 2) maxWidth = 32767;
1025
1136
 
1026
1137
  int clipX0;
1027
1138
  int clipY0;
1028
1139
  int clipX1;
1029
1140
  int clipY1;
1030
1141
  gea::platform::display::Display::clip(&clipX0, &clipY0, &clipX1, &clipY1);
1142
+ TextCoverageSink clippedSink;
1143
+ if (coverageSink) {
1144
+ clippedSink = *coverageSink;
1145
+ clippedSink.clipX0 = clipX0;
1146
+ clippedSink.clipY0 = clipY0;
1147
+ clippedSink.clipX1 = clipX1;
1148
+ clippedSink.clipY1 = clipY1;
1149
+ coverageSink = &clippedSink;
1150
+ }
1031
1151
 
1032
1152
  const bool noWrap = whiteSpace == 1;
1033
1153
  const bool ellipsis = textOverflow == 1;
@@ -1037,10 +1157,10 @@ public:
1037
1157
  const int fontSize = static_cast<int>(scale * kBitmapFontHeight + 0.5f);
1038
1158
  if (gea::framework::graphics::FontRegistry::rasterizedFamily(fontId, fontSize).valid()) {
1039
1159
  if (noWrap)
1040
- drawRasterizedSingleLine(text, x, y, maxWidth, color, textAlign, containerWidth, fontId, fontSize, lineHeight, ellipsis, clipX0, clipY0, clipX1, clipY1);
1160
+ drawRasterizedSingleLine(text, x, y, maxWidth, color, textAlign, containerWidth, fontId, fontSize, lineHeight, ellipsis, clipX0, clipY0, clipX1, clipY1, coverageSink);
1041
1161
  else
1042
1162
  drawRasterizedWrapped(text, x, y, maxWidth, color, textAlign, containerWidth, fontId, fontSize, lineHeight, clipX0, clipY0, clipX1, clipY1,
1043
- ellipsis ? maxHeight : 0, firstLineIndent);
1163
+ ellipsis ? maxHeight : 0, firstLineIndent, coverageSink);
1044
1164
  return;
1045
1165
  }
1046
1166
  }
@@ -1048,14 +1168,14 @@ public:
1048
1168
  (void)fontId;
1049
1169
  #endif
1050
1170
  if (noWrap)
1051
- drawBitmapSingleLine(text, x, y, maxWidth, color, scale, textAlign, containerWidth, lineHeight, ellipsis, clipX0, clipY0, clipX1, clipY1);
1171
+ drawBitmapSingleLine(text, x, y, maxWidth, color, scale, textAlign, containerWidth, lineHeight, ellipsis, clipX0, clipY0, clipX1, clipY1, coverageSink);
1052
1172
  else
1053
- drawBitmapWrapped(text, x, y, maxWidth, color, scale, textAlign, containerWidth, lineHeight, clipX0, clipY0, clipX1, clipY1, firstLineIndent);
1173
+ drawBitmapWrapped(text, x, y, maxWidth, color, scale, textAlign, containerWidth, lineHeight, clipX0, clipY0, clipX1, clipY1, firstLineIndent, coverageSink);
1054
1174
  }
1055
1175
 
1056
1176
  private:
1057
1177
  #ifdef GEA_EMBEDDED_HAS_GENERATED_FONTS
1058
- static void drawRasterizedWrapped(const char *text, int x, int y, int maxWidth, std::uint16_t color, int textAlign, int containerWidth, int fontId, int fontSize, int lineHeight, int clipX0, int clipY0, int clipX1, int clipY1, int ellipsisMaxHeight = 0, int firstLineIndent = 0)
1178
+ static void drawRasterizedWrapped(const char *text, int x, int y, int maxWidth, std::uint16_t color, int textAlign, int containerWidth, int fontId, int fontSize, int lineHeight, int clipX0, int clipY0, int clipX1, int clipY1, int ellipsisMaxHeight = 0, int firstLineIndent = 0, const TextCoverageSink *coverageSink = nullptr)
1059
1179
  {
1060
1180
  // Inline continuation: the run's first line starts `firstLineIndent` px in
1061
1181
  // from the box's left edge (the pen position it inherited from the box
@@ -1132,9 +1252,9 @@ private:
1132
1252
  if (penY + lineBoxOffset > clipY1) break;
1133
1253
  if (li == lineBudget - 1 && li < entry.lineCount - 1) {
1134
1254
  // Last budgeted line with more text behind it: ellipsize the rest.
1135
- drawRasterizedSingleLine(lineStart, lineOriginX_(li), penY, lineBudget_(li), color, textAlign, containerWidth,
1136
- fontId, fontSize, lineHeight, /*ellipsis=*/true,
1137
- clipX0, clipY0, clipX1, clipY1);
1255
+ drawRasterizedSingleLine(lineStart, lineOriginX_(li), penY, lineBudget_(li), color, textAlign, containerWidth,
1256
+ fontId, fontSize, lineHeight, /*ellipsis=*/true,
1257
+ clipX0, clipY0, clipX1, clipY1, coverageSink);
1138
1258
  return;
1139
1259
  }
1140
1260
  const int chars = entry.renderBytes[li];
@@ -1147,7 +1267,8 @@ private:
1147
1267
  const int copyLen = chars < kWrappedLineBufferBytes ? chars : kWrappedLineBufferBytes;
1148
1268
  std::memcpy(lineBuffer, lineStart, copyLen);
1149
1269
  lineBuffer[copyLen] = '\0';
1150
- gea::platform::display::Display::drawTextFontFamily(lineBuffer, lineX, lineBoxY, color, fontId, fontSize);
1270
+ if (coverageSink) rasterizedGlyphRun(lineBuffer, lineX, lineBoxY, fontId, fontSize, *coverageSink);
1271
+ else gea::platform::display::Display::drawTextFontFamily(lineBuffer, lineX, lineBoxY, color, fontId, fontSize);
1151
1272
  }
1152
1273
  }
1153
1274
  lineStart += entry.consumedBytes[li];
@@ -1166,7 +1287,7 @@ private:
1166
1287
  // Budget reached: ellipsize whatever remains onto this final line.
1167
1288
  drawRasterizedSingleLine(lineStart, lineOriginX_(li), penY, lineBudget_(li), color, textAlign, containerWidth,
1168
1289
  fontId, fontSize, lineHeight, /*ellipsis=*/true,
1169
- clipX0, clipY0, clipX1, clipY1);
1290
+ clipX0, clipY0, clipX1, clipY1, coverageSink);
1170
1291
  return;
1171
1292
  }
1172
1293
  const int lineIndex = li;
@@ -1185,7 +1306,8 @@ private:
1185
1306
  const int copyLen = line.renderBytes < kWrappedLineBufferBytes ? line.renderBytes : kWrappedLineBufferBytes;
1186
1307
  std::memcpy(lineBuffer, lineStart, copyLen);
1187
1308
  lineBuffer[copyLen] = '\0';
1188
- gea::platform::display::Display::drawTextFontFamily(lineBuffer, lineX, lineBoxY, color, fontId, fontSize);
1309
+ if (coverageSink) rasterizedGlyphRun(lineBuffer, lineX, lineBoxY, fontId, fontSize, *coverageSink);
1310
+ else gea::platform::display::Display::drawTextFontFamily(lineBuffer, lineX, lineBoxY, color, fontId, fontSize);
1189
1311
  }
1190
1312
  }
1191
1313
 
@@ -1199,7 +1321,7 @@ private:
1199
1321
  // appending an ASCII "..." when text-overflow: ellipsis is set, or hard-
1200
1322
  // clipping at the box edge otherwise. The truncated run always fits within
1201
1323
  // maxWidth, so it never wraps and never spills past the content box.
1202
- static void drawRasterizedSingleLine(const char *text, int x, int y, int maxWidth, std::uint16_t color, int textAlign, int containerWidth, int fontId, int fontSize, int lineHeight, bool ellipsis, int clipX0, int clipY0, int clipX1, int clipY1)
1324
+ static void drawRasterizedSingleLine(const char *text, int x, int y, int maxWidth, std::uint16_t color, int textAlign, int containerWidth, int fontId, int fontSize, int lineHeight, bool ellipsis, int clipX0, int clipY0, int clipX1, int clipY1, const TextCoverageSink *coverageSink = nullptr)
1203
1325
  {
1204
1326
  gea::framework::graphics::RasterizedFont font = gea::framework::graphics::FontRegistry::rasterizedFamily(fontId, fontSize);
1205
1327
  if (!font.valid()) return;
@@ -1265,11 +1387,12 @@ private:
1265
1387
  if (len <= 0) return;
1266
1388
  buf[len] = '\0';
1267
1389
  const int xOffset = alignedOffset(textAlign, containerWidth, drawnWidth);
1268
- gea::platform::display::Display::drawTextFontFamily(buf, x + xOffset, lineBoxY, color, fontId, fontSize);
1390
+ if (coverageSink) rasterizedGlyphRun(buf, x + xOffset, lineBoxY, fontId, fontSize, *coverageSink);
1391
+ else gea::platform::display::Display::drawTextFontFamily(buf, x + xOffset, lineBoxY, color, fontId, fontSize);
1269
1392
  }
1270
1393
  #endif
1271
1394
 
1272
- static void drawBitmapWrapped(const char *text, int x, int y, int maxWidth, std::uint16_t color, float scale, int textAlign, int containerWidth, int lineHeight, int clipX0, int clipY0, int clipX1, int clipY1, int firstLineIndent = 0)
1395
+ static void drawBitmapWrapped(const char *text, int x, int y, int maxWidth, std::uint16_t color, float scale, int textAlign, int containerWidth, int lineHeight, int clipX0, int clipY0, int clipX1, int clipY1, int firstLineIndent = 0, const TextCoverageSink *coverageSink = nullptr)
1273
1396
  {
1274
1397
  if (scale < 0.1f) scale = 1.0f;
1275
1398
  int glyphWidth = static_cast<int>(kBitmapFontWidth * scale + 0.5f);
@@ -1302,7 +1425,8 @@ private:
1302
1425
  char buf[5] = {};
1303
1426
  const int bytes = static_cast<int>(glyph - glyphStart);
1304
1427
  std::memcpy(buf, glyphStart, bytes);
1305
- gea::platform::display::Display::drawText(buf, penX, penY, color, scale);
1428
+ if (coverageSink) bitmapGlyphRun(buf, penX, penY, scale, *coverageSink);
1429
+ else gea::platform::display::Display::drawText(buf, penX, penY, color, scale);
1306
1430
  penX += glyphWidth;
1307
1431
  }
1308
1432
  }
@@ -1318,7 +1442,7 @@ private:
1318
1442
  // that is what `measureBitmap` charges for, so counting bytes here put the
1319
1443
  // truncation, the ellipsis budget and the alignment offset in different units
1320
1444
  // from the layout that placed the text.
1321
- static void drawBitmapSingleLine(const char *text, int x, int y, int maxWidth, std::uint16_t color, float scale, int textAlign, int containerWidth, int lineHeight, bool ellipsis, int clipX0, int clipY0, int clipX1, int clipY1)
1445
+ static void drawBitmapSingleLine(const char *text, int x, int y, int maxWidth, std::uint16_t color, float scale, int textAlign, int containerWidth, int lineHeight, bool ellipsis, int clipX0, int clipY0, int clipX1, int clipY1, const TextCoverageSink *coverageSink = nullptr)
1322
1446
  {
1323
1447
  (void)lineHeight;
1324
1448
  if (scale < 0.1f) scale = 1.0f;
@@ -1362,13 +1486,15 @@ private:
1362
1486
  nextUtf8Codepoint(glyph);
1363
1487
  char b[5] = {};
1364
1488
  std::memcpy(b, glyphStart, static_cast<std::size_t>(glyph - glyphStart));
1365
- gea::platform::display::Display::drawText(b, penX, penY, color, scale);
1489
+ if (coverageSink) bitmapGlyphRun(b, penX, penY, scale, *coverageSink);
1490
+ else gea::platform::display::Display::drawText(b, penX, penY, color, scale);
1366
1491
  penX += glyphWidth;
1367
1492
  }
1368
1493
  if (addEllipsis) {
1369
1494
  for (int i = 0; i < 3; ++i) {
1370
1495
  char b[2] = { '.', '\0' };
1371
- gea::platform::display::Display::drawText(b, penX, penY, color, scale);
1496
+ if (coverageSink) bitmapGlyphRun(b, penX, penY, scale, *coverageSink);
1497
+ else gea::platform::display::Display::drawText(b, penX, penY, color, scale);
1372
1498
  penX += glyphWidth;
1373
1499
  }
1374
1500
  }
@@ -1466,8 +1592,17 @@ void Tree::setText(int node, const char *text)
1466
1592
  // fixed-width label appear equally wide. A shrinking update such as
1467
1593
  // "San Francisco" -> "Lisbon" then dirtied only the new, shorter run (or even a
1468
1594
  // sliver at the box edge), leaving the old trailing glyphs on the physical panel.
1469
- const bool gid_canPartial = gid_singleLine && target.style.white_space == 1 &&
1595
+ const bool gid_hadPartial = target.render.text_partial_dirty;
1596
+ const int gid_previousX0 = gid_hadPartial ? target.render.text_dirty.x0 : 0;
1597
+ const int gid_previousX1 = gid_hadPartial ? target.render.text_dirty.x1 : 0;
1598
+ const bool gid_canPartial = (!target.render.dirty || gid_hadPartial) &&
1599
+ !target.render.bg_recolor_pending && gid_singleLine && target.style.white_space == 1 &&
1470
1600
  target.style.text_align == 0 && gid_prefix < gid_newLen;
1601
+ // Text and recolor shortcuts share their payload. Other pending paint
1602
+ // changes require a full node repaint, while consecutive text runs union
1603
+ // their extents so none of the earlier changes is forgotten.
1604
+ target.render.bg_recolor_pending = 0;
1605
+ target.render.text_partial_dirty = 0;
1471
1606
  int gid_preW = 0;
1472
1607
  int gid_rightW = 0;
1473
1608
  if (gid_canPartial) {
@@ -1493,7 +1628,9 @@ void Tree::setText(int node, const char *text)
1493
1628
  const bool gid_hadLayoutDirty = target.render.layout_dirty != 0;
1494
1629
  target.render.dirty = 1;
1495
1630
  target.render.layout_dirty = 1;
1631
+ #if GEA_CSS_SCROLLING
1496
1632
  target.render.non_scroll_dirty = 1;
1633
+ #endif
1497
1634
  target.render.text_layout_stable = 0;
1498
1635
  markNodeDisplayCommandsDirty(node);
1499
1636
  // assign() past capacity reallocates and frees the old buffer, leaving any
@@ -1544,8 +1681,10 @@ void Tree::setText(int node, const char *text)
1544
1681
  if (gid_sameHeight && gid_canPartial) {
1545
1682
  const int gid_originX = target.layout.x + target.style.padding[3];
1546
1683
  constexpr int kTextDirtyPadPx = 4; // AA / glyph-overhang guard
1547
- target.render.text_dirty_x0 = static_cast<int16_t>(gid_originX + gid_preW - kTextDirtyPadPx);
1548
- target.render.text_dirty_x1 = static_cast<int16_t>(gid_originX + gid_rightW + kTextDirtyPadPx);
1684
+ const int x0 = gid_originX + gid_preW - kTextDirtyPadPx;
1685
+ const int x1 = gid_originX + gid_rightW + kTextDirtyPadPx;
1686
+ target.render.text_dirty.x0 = static_cast<int16_t>(gid_hadPartial ? std::min(gid_previousX0, x0) : x0);
1687
+ target.render.text_dirty.x1 = static_cast<int16_t>(gid_hadPartial ? std::max(gid_previousX1, x1) : x1);
1549
1688
  target.render.text_partial_dirty = 1;
1550
1689
  }
1551
1690
  }
@@ -1580,6 +1719,25 @@ bool TextRenderer::canFragmentInlineRuns(const Node &node)
1580
1719
  return cached != 0;
1581
1720
  }
1582
1721
 
1722
+ int TextRenderer::baselineOffset(const Node &node, bool last)
1723
+ {
1724
+ const int fontSize = node.style.font_size > 0 ? node.style.font_size : 16;
1725
+ const auto font = gea::framework::graphics::FontRegistry::rasterizedFamily(node.style.font_id, fontSize);
1726
+ if (!font.valid()) return node.layout.height + node.style.margin[2];
1727
+ const int lineAdvance = node.style.line_height > 0 ? node.style.line_height : font.lineHeight();
1728
+ const int halfLeading = node.style.line_height > 0 ? (lineAdvance - font.lineHeight()) / 2 : 0;
1729
+ int offset = boxInset(node.style, 0) + halfLeading + font.ascender();
1730
+ if (last) {
1731
+ std::string storage;
1732
+ const char *text = prepareText(node.text.c_str(), node.style.text_transform, node.style.white_space, storage);
1733
+ const int width = node.style.white_space == 1 || node.style.white_space == 2
1734
+ ? 32767 : std::max(1, node.layout.width - boxInsets(node.style, true));
1735
+ const auto measured = TextMetrics::measure(text, width, node.style.font_id, fontSize, node.style.line_height, node.style.font_weight);
1736
+ offset += std::max(0, measured.height - lineAdvance);
1737
+ }
1738
+ return offset;
1739
+ }
1740
+
1583
1741
  InlineFlowMeasure TextRenderer::measureInlineFlow(const Node &node, int firstAvail, int contentWidth, bool atLineStart)
1584
1742
  {
1585
1743
  InlineFlowMeasure out;
@@ -1587,20 +1745,20 @@ InlineFlowMeasure TextRenderer::measureInlineFlow(const Node &node, int firstAva
1587
1745
  if (!raw || !raw[0]) return out;
1588
1746
  // nowrap is a single line by definition, and an author-sized box wraps within
1589
1747
  // its own width — neither participates in the block's line boxes.
1590
- if (node.style.white_space == 1) return out;
1748
+ if (node.style.white_space == 1 || node.style.white_space == 2) return out;
1591
1749
  if (!canFragmentInlineRuns(node)) return out;
1592
1750
 
1593
1751
  const GlyphAdvanceSource advance = advanceSourceForNode(node);
1594
1752
  if (!advance.valid || advance.lineAdvance <= 0) return out;
1595
1753
 
1596
1754
  std::string transformed;
1597
- const char *text = textWithTransform(raw, node.style.text_transform, transformed);
1755
+ const char *text = textWithTransform(raw, node.style.text_transform, transformed, node.style.white_space);
1598
1756
  if (!text || !text[0]) return out;
1599
1757
 
1600
1758
  if (contentWidth < 1) contentWidth = 1;
1601
1759
  if (firstAvail < 0) firstAvail = 0;
1602
1760
 
1603
- if (atLineStart) {
1761
+ if (atLineStart && (node.style.white_space == 0 || node.style.white_space == 4)) {
1604
1762
  const int lead = leadingCollapsibleBytes(text);
1605
1763
  if (lead > 0) {
1606
1764
  const char *p = text;
@@ -1620,7 +1778,14 @@ InlineFlowMeasure TextRenderer::measureInlineFlow(const Node &node, int firstAva
1620
1778
  const int visible = line.width + (lineIndex == 0 ? out.firstLineIndentAdjust : 0);
1621
1779
  if (lineIndex == 0) {
1622
1780
  out.firstLineWidth = visible;
1623
- out.firstLineForcedSplit = line.hardSplit;
1781
+ if (node.style.white_space == 0 || node.style.white_space == 4) {
1782
+ const char *end = p + line.renderBytes;
1783
+ for (const char *glyph = p; glyph < end;) {
1784
+ const int cp = nextUtf8Codepoint(glyph);
1785
+ out.firstLineTrailingSpace = isWrappingSpace(cp)
1786
+ ? out.firstLineTrailingSpace + advance(cp) : 0;
1787
+ }
1788
+ }
1624
1789
  }
1625
1790
  out.lastLineWidth = visible;
1626
1791
  if (visible > out.maxLineWidth) out.maxLineWidth = visible;
@@ -1632,14 +1797,57 @@ InlineFlowMeasure TextRenderer::measureInlineFlow(const Node &node, int firstAva
1632
1797
  return out;
1633
1798
  }
1634
1799
 
1800
+ int TextRenderer::firstUnbreakableWidth(const Node &node)
1801
+ {
1802
+ std::string storage;
1803
+ const char *text = prepareText(node.text.c_str(), node.style.text_transform, node.style.white_space, storage);
1804
+ if (!text) return 0;
1805
+ if (node.style.white_space == 0 || node.style.white_space == 1 || node.style.white_space == 4)
1806
+ text += leadingCollapsibleBytes(text);
1807
+ const bool wrapping = node.style.white_space != 1 && node.style.white_space != 2;
1808
+ const char *end = text;
1809
+ while (*end) {
1810
+ const char *start = end;
1811
+ const int cp = nextUtf8Codepoint(end);
1812
+ if (cp == '\n' || (wrapping && isWrappingSpace(cp))) { end = start; break; }
1813
+ if (wrapping && canBreakAfter(cp)) break;
1814
+ }
1815
+ return measureSingleLineTextWidthForNode(node, std::string(text, end - text).c_str());
1816
+ }
1817
+
1818
+ int TextRenderer::minContentWidth(const Node &node, int *pendingWord)
1819
+ {
1820
+ std::string storage;
1821
+ const char *text = prepareText(node.text.c_str(), node.style.text_transform, node.style.white_space, storage);
1822
+ const bool wrapping = node.style.white_space != 1 && node.style.white_space != 2;
1823
+ const char *start = text;
1824
+ int width = 0, current = pendingWord ? *pendingWord : 0;
1825
+ for (const char *p = text;; ++p) {
1826
+ if (!*p || *p == '\n' || (wrapping && (*p == ' ' || *p == '\t'))) {
1827
+ const std::string word(start, p - start);
1828
+ current += measureSingleLineTextWidthForNode(node, word.c_str());
1829
+ width = std::max(width, current);
1830
+ if (!*p) { if (pendingWord) *pendingWord = current; break; }
1831
+ current = 0;
1832
+ start = p + 1;
1833
+ }
1834
+ }
1835
+ return width;
1836
+ }
1837
+
1838
+ const char *TextRenderer::prepareText(const char *text, int textTransform, int whiteSpace, std::string &storage)
1839
+ {
1840
+ return textWithTransform(text, textTransform, storage, whiteSpace);
1841
+ }
1842
+
1635
1843
  void TextRenderer::layout(int id, int avail_w)
1636
1844
  {
1637
1845
  Node *n = &Tree::instance().nodes()[id];
1638
1846
 
1639
- int content_w = avail_w - n->style.padding[1] - n->style.padding[3]
1847
+ int content_w = avail_w - boxInset(n->style, 1) - boxInset(n->style, 3)
1640
1848
  - n->style.margin[1] - n->style.margin[3];
1641
- if (n->style.width != kUnset) content_w = n->style.width - n->style.padding[1] - n->style.padding[3];
1642
- else if (n->style.width_percent != kUnset && n->layout.width > 0) content_w = n->layout.width - n->style.padding[1] - n->style.padding[3];
1849
+ if (n->style.width != kUnset) content_w = contentSizeToBorderSize(n->style, n->style.width, true) - boxInset(n->style, 1) - boxInset(n->style, 3);
1850
+ else if ((n->style.width_percent != kUnset || n->style.width_expression >= 0) && n->layout.width > 0) content_w = n->layout.width - boxInset(n->style, 1) - boxInset(n->style, 3);
1643
1851
  if (content_w < 0) content_w = 0;
1644
1852
 
1645
1853
  const TextLayoutMeasure measured = measureTextLayoutForNode(*n, n->text.c_str(), content_w);
@@ -1664,9 +1872,9 @@ bool TextRenderer::remeasureContentBox(int id, bool keepBoxWidth)
1664
1872
  // explicit width re-wraps within the fixed box; natural-width text hugs the
1665
1873
  // new content (the 0x4000 bound matches setText's bbox predictor).
1666
1874
  int content_w = 0x4000;
1667
- if (keepBoxWidth) content_w = n.layout.width;
1668
- else if (n.style.width != kUnset) content_w = n.style.width - n.style.padding[1] - n.style.padding[3];
1669
- else if (n.style.width_percent != kUnset && n.layout.width > 0) content_w = n.layout.width - n.style.padding[1] - n.style.padding[3];
1875
+ if (keepBoxWidth) content_w = n.layout.width - boxInsets(n.style, true);
1876
+ else if (n.style.width != kUnset) content_w = contentSizeToBorderSize(n.style, n.style.width, true) - boxInset(n.style, 1) - boxInset(n.style, 3);
1877
+ else if ((n.style.width_percent != kUnset || n.style.width_expression >= 0) && n.layout.width > 0) content_w = n.layout.width - boxInset(n.style, 1) - boxInset(n.style, 3);
1670
1878
  if (content_w < 0) content_w = 0;
1671
1879
  const TextLayoutMeasure measured = measureTextLayoutForNode(n, n.text.c_str(), content_w);
1672
1880
  if (keepBoxWidth) {
@@ -1685,6 +1893,12 @@ void TextRenderer::drawWrapped(const char *text, int x, int y, int maxWidth, gea
1685
1893
  TextDrawer::drawWrapped(text, x, y, maxWidth, color, scale, text_align, containerWidth, fontId, textTransform, lineHeight, whiteSpace, textOverflow, maxHeight, firstLineIndent);
1686
1894
  }
1687
1895
 
1896
+ void TextRenderer::unionCoverageRow(const DisplayCommand &command, int screenY, int screenX,
1897
+ int width, std::uint8_t *outCoverage)
1898
+ {
1899
+ TextDrawer::unionCoverageRow(command, screenY, screenX, width, outCoverage);
1900
+ }
1901
+
1688
1902
  int TextRenderer::measureWidth(const char *text, int fontId, int fontSize, int textTransform)
1689
1903
  {
1690
1904
  if (!text || !text[0]) return 0;
@@ -1710,13 +1924,85 @@ void GEA_TEXT_HOT_SRAM TextRenderer::record(const Node &node, uint8_t parentAlph
1710
1924
  const Node *n = &node;
1711
1925
  if (n->type != NodeType::Text || n->text.empty()) return;
1712
1926
 
1927
+ #if GEA_CSS_FIRST_LINE
1928
+ // A first-line background belongs to the inline fragment, not the block's
1929
+ // entire width. Layout records this run's first line box/advance only when an
1930
+ // ancestor has a resolved ::first-line background. Emit it before the text
1931
+ // command so the glyphs remain painted above the inline background.
1932
+ const int textNodeId = static_cast<int>(n - Tree::instance().nodes());
1933
+ const NodeRareData *textRare = rareDataFor(textNodeId);
1934
+ const NodeRareData *lineStyleRare = nullptr;
1935
+ for (int ancestor = n->parent; ancestor >= 0 && ancestor < Tree::instance().nodeCount();
1936
+ ancestor = Tree::instance().node(ancestor).parent) {
1937
+ const NodeRareData *candidate = rareDataFor(ancestor);
1938
+ if (candidate && candidate->firstLineBackground.hasColor) {
1939
+ lineStyleRare = candidate;
1940
+ break;
1941
+ }
1942
+ }
1943
+ if (textRare && textRare->firstLineFragment.valid && lineStyleRare &&
1944
+ lineStyleRare->firstLineBackground.lineValid &&
1945
+ textRare->firstLineFragment.width > 0 && textRare->firstLineFragment.height > 0 &&
1946
+ lineStyleRare->firstLineBackground.alpha > 0) {
1947
+ const auto &fragment = textRare->firstLineFragment;
1948
+ const auto &background = lineStyleRare->firstLineBackground;
1949
+ const uint8_t backgroundAlpha = combineAlpha(parentAlpha, background.alpha);
1950
+ int fragmentX = fragment.x;
1951
+ int fragmentY = fragment.y;
1952
+ const Tree &tree = Tree::instance();
1953
+ if (fragment.contextNode >= 0 && fragment.contextNode < tree.nodeCount()) {
1954
+ fragmentX += tree.node(fragment.contextNode).layout.x;
1955
+ fragmentY += tree.node(fragment.contextNode).layout.y;
1956
+ }
1957
+ int ownerId = -1;
1958
+ for (int ancestor = n->parent; ancestor >= 0 && ancestor < tree.nodeCount();
1959
+ ancestor = tree.node(ancestor).parent) {
1960
+ const NodeRareData *candidate = rareDataFor(ancestor);
1961
+ if (candidate && &candidate->firstLineBackground == &lineStyleRare->firstLineBackground) {
1962
+ ownerId = ancestor;
1963
+ break;
1964
+ }
1965
+ }
1966
+ const bool onOwnerFirstLine = ownerId >= 0 && background.lineContextNode >= 0 &&
1967
+ background.lineContextNode < tree.nodeCount() &&
1968
+ fragmentY == tree.node(background.lineContextNode).layout.y + background.lineY;
1969
+ if (onOwnerFirstLine) {
1970
+ int16_t xs[4], ys[4];
1971
+ ViewRenderer::transformedRectCorners(*n, false, fragmentX, fragmentY,
1972
+ fragment.width, fragment.height, xs, ys);
1973
+ int bx0, by0, bx1, by1;
1974
+ boundsFromCorners(xs, ys, &bx0, &by0, &bx1, &by1);
1975
+ if (backgroundAlpha != parentAlpha)
1976
+ appendAlphaCommand(backgroundAlpha, bx0, by0, bx1 - bx0 + 1, by1 - by0 + 1);
1977
+ DisplayCommand *fill = DisplayList::instance().append();
1978
+ if (fill) {
1979
+ fill->type = DisplayCommandType::FillQuad;
1980
+ fill->bx = bx0; fill->by = by0;
1981
+ fill->bw = bx1 - bx0 + 1; fill->bh = by1 - by0 + 1;
1982
+ fill->quad.x0 = xs[0]; fill->quad.y0 = ys[0];
1983
+ fill->quad.x1 = xs[1]; fill->quad.y1 = ys[1];
1984
+ fill->quad.x2 = xs[2]; fill->quad.y2 = ys[2];
1985
+ fill->quad.x3 = xs[3]; fill->quad.y3 = ys[3];
1986
+ fill->quad.lx = static_cast<int16_t>(fragmentX); fill->quad.ly = static_cast<int16_t>(fragmentY);
1987
+ fill->quad.lw = fragment.width; fill->quad.lh = fragment.height;
1988
+ fill->quad.aux = 0;
1989
+ fill->quad.color = background.color;
1990
+ fill->quad.reprojectMode = 1;
1991
+ }
1992
+ if (backgroundAlpha != parentAlpha)
1993
+ appendAlphaCommand(parentAlpha, bx0, by0, bx1 - bx0 + 1, by1 - by0 + 1);
1994
+ }
1995
+ }
1996
+
1997
+
1998
+ #endif
1713
1999
  int x = n->layout.x;
1714
2000
  int y = n->layout.y;
1715
2001
  int w = n->layout.width;
1716
2002
  int h = n->layout.height;
1717
- int tx = x + n->style.padding[3];
1718
- int ty = y + n->style.padding[0];
1719
- int tw = w - n->style.padding[1] - n->style.padding[3];
2003
+ int tx = x + boxInset(n->style, 3);
2004
+ int ty = y + boxInset(n->style, 0);
2005
+ int tw = w - boxInset(n->style, 1) - boxInset(n->style, 3);
1720
2006
  if (tw < 0) tw = 0;
1721
2007
  float textScale = n->style.font_size > 0 ? static_cast<float>(n->style.font_size) / kBitmapFontHeight : 1.0f;
1722
2008
 
@@ -1755,14 +2041,14 @@ void GEA_TEXT_HOT_SRAM TextRenderer::record(const Node &node, uint8_t parentAlph
1755
2041
  }
1756
2042
  }
1757
2043
  std::string transformedText;
1758
- const char *measureText = textWithTransform(n->text.c_str(), n->style.text_transform, transformedText);
2044
+ const char *measureText = textWithTransform(n->text.c_str(), n->style.text_transform, transformedText, n->style.white_space);
1759
2045
  #ifdef GEA_EMBEDDED_HAS_GENERATED_FONTS
1760
- if (transformed && recordProjectedRasterText(*n, parentAlpha, tx, ty, tw)) return;
2046
+ if (transformed && recordProjectedRasterText(*n, measureText, parentAlpha, tx, ty, tw)) return;
1761
2047
  #endif
1762
2048
  if (transformed && transformedX1 >= transformedX0 && transformedY1 >= transformedY0) {
1763
2049
  const int projectedW = transformedX1 - transformedX0 + 1;
1764
2050
  const int projectedH = transformedY1 - transformedY0 + 1;
1765
- const int contentH = h - n->style.padding[0] - n->style.padding[2];
2051
+ const int contentH = h - boxInset(n->style, 0) - boxInset(n->style, 2);
1766
2052
  const int centerX = transformedX0 + projectedW / 2;
1767
2053
  const int centerY = transformedY0 + projectedH / 2;
1768
2054
  float projectedScaleX = tw > 0 ? static_cast<float>(projectedW) / static_cast<float>(tw) : 1.0f;
@@ -1822,6 +2108,7 @@ void GEA_TEXT_HOT_SRAM TextRenderer::record(const Node &node, uint8_t parentAlph
1822
2108
  }
1823
2109
 
1824
2110
  const bool noWrap = n->style.white_space == 1;
2111
+ const bool noSoftWrap = noWrap || n->style.white_space == 2;
1825
2112
  // Inline continuation (see LayoutBox::inline_indent): line 0 starts at
1826
2113
  // `drawX + inlineIndent`, every later line at `drawX`. The tight ink/paint
1827
2114
  // bounds below all assume one origin for the whole run, so an indented run
@@ -1837,7 +2124,7 @@ void GEA_TEXT_HOT_SRAM TextRenderer::record(const Node &node, uint8_t parentAlph
1837
2124
  if (!transformed && inlineIndent == 0) {
1838
2125
  const int commandFontSize = static_cast<int>(textScale * kBitmapFontHeight + 0.5f);
1839
2126
  const TextMeasure paintMeasure =
1840
- TextMetrics::measure(measureText, noWrap ? 32767 : drawW, n->style.font_id, commandFontSize, commandLineHeight, n->style.font_weight);
2127
+ TextMetrics::measure(measureText, noSoftWrap ? 32767 : drawW, n->style.font_id, commandFontSize, commandLineHeight, n->style.font_weight);
1841
2128
  if (paintMeasure.width > 0 && paintMeasure.height > 0) {
1842
2129
  // CSS: a flex container's bare text becomes an anonymous flex item, so
1843
2130
  // align-items centers (or end-aligns) it on the cross axis. This node
@@ -1847,11 +2134,17 @@ void GEA_TEXT_HOT_SRAM TextRenderer::record(const Node &node, uint8_t parentAlph
1847
2134
  // axis is vertical there); a child-bearing flex container aligns its
1848
2135
  // child BOXES through layout as before.
1849
2136
  if (n->style.display == kDisplayFlex && n->first_child < 0 &&
1850
- (n->style.align_items == 1 || n->style.align_items == 2) &&
1851
2137
  usesRowLayout(n->style)) {
1852
- const int contentH = h - n->style.padding[0] - n->style.padding[2];
2138
+ const int contentH = h - boxInset(n->style, 0) - boxInset(n->style, 2);
1853
2139
  const int slack = contentH - paintMeasure.height;
1854
- if (slack > 0) drawY += n->style.align_items == 1 ? slack / 2 : slack;
2140
+ #if GEA_CSS_FLEX_WRAP
2141
+ const bool reverseCrossAxis = (n->style.flex_wrap & 3) == 2;
2142
+ #else
2143
+ constexpr bool reverseCrossAxis = false;
2144
+ #endif
2145
+ const int alignment = usedAlignment(n->style.align_items, slack, reverseCrossAxis);
2146
+ if (alignment == 1) drawY += slack / 2;
2147
+ else if (alignment == 2) drawY += slack;
1855
2148
  }
1856
2149
  // nowrap truncates/clips the run to the content box, so the painted
1857
2150
  // width never exceeds drawW even though the unbounded measure may.
@@ -1892,7 +2185,7 @@ void GEA_TEXT_HOT_SRAM TextRenderer::record(const Node &node, uint8_t parentAlph
1892
2185
  int ix1 = -1;
1893
2186
  int iy1 = -1;
1894
2187
  if (rasterizedTextInkBounds(measureText,
1895
- noWrap ? 32767 : drawW,
2188
+ noSoftWrap ? 32767 : drawW,
1896
2189
  n->style.text_align,
1897
2190
  containerW,
1898
2191
  n->style.font_id,
@@ -1953,6 +2246,7 @@ void GEA_TEXT_HOT_SRAM TextRenderer::record(const Node &node, uint8_t parentAlph
1953
2246
  std::fflush(stdout);
1954
2247
  }
1955
2248
 
2249
+
1956
2250
  const uint8_t effectiveAlpha = combineAlpha(parentAlpha, n->style.text_alpha);
1957
2251
  if (effectiveAlpha != parentAlpha) appendAlphaCommand(effectiveAlpha, bx, by, bw, bh);
1958
2252
 
@@ -1977,7 +2271,7 @@ void GEA_TEXT_HOT_SRAM TextRenderer::record(const Node &node, uint8_t parentAlph
1977
2271
  cmd->text.textOverflow = n->style.text_overflow;
1978
2272
  cmd->text.firstLineIndent = static_cast<int16_t>(inlineIndent);
1979
2273
  {
1980
- const int contentH = h - n->style.padding[0] - n->style.padding[2];
2274
+ const int contentH = h - boxInset(n->style, 0) - boxInset(n->style, 2);
1981
2275
  cmd->text.maxHeight = static_cast<int16_t>(contentH > 0 ? (contentH > 32767 ? 32767 : contentH) : 0);
1982
2276
  }
1983
2277
 
@@ -1995,6 +2289,7 @@ void GEA_TEXT_HOT_SRAM TextRenderer::record(const Node &node, uint8_t parentAlph
1995
2289
  DisplayCommand *deco = DisplayList::instance().append();
1996
2290
  if (deco) {
1997
2291
  deco->type = DisplayCommandType::FillRect;
2292
+ deco->textDecorationInk = true;
1998
2293
  deco->bx = x; deco->by = y; deco->bw = w; deco->bh = h;
1999
2294
  deco->fill.x = tx;
2000
2295
  deco->fill.y = lineY;