@ng-native/desktop 0.23.0 → 0.25.0

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 (73) hide show
  1. package/bin/package.mjs +65 -9
  2. package/bin/prebuilt.mjs +25 -4
  3. package/native/CMakeLists.txt +7 -2
  4. package/native/appkit/CMakeLists.txt +39 -5
  5. package/native/appkit/appkit-backend.mm +28 -3
  6. package/native/appkit/camera-sheet.h +59 -0
  7. package/native/appkit/camera-sheet.mm +344 -0
  8. package/native/appkit/camera-view.mm +5 -1
  9. package/native/appkit/camera.h +22 -0
  10. package/native/appkit/camera.mm +203 -69
  11. package/native/appkit/cg-canvas.h +8 -1
  12. package/native/appkit/cg-canvas.mm +166 -21
  13. package/native/appkit/header-toolbar.mm +4 -1
  14. package/native/appkit/host-view.mm +29 -1
  15. package/native/appkit/image-view.mm +31 -2
  16. package/native/appkit/main.mm +29 -69
  17. package/native/appkit/photo-library.mm +6 -6
  18. package/native/appkit/scroll-view.mm +57 -16
  19. package/native/appkit/scrollers.h +22 -2
  20. package/native/appkit/scrollers.mm +221 -2
  21. package/native/appkit/split-view.h +4 -0
  22. package/native/appkit/split-view.mm +57 -3
  23. package/native/appkit/surface-view.mm +5 -1
  24. package/native/appkit/svg-view.h +2 -1
  25. package/native/appkit/svg-view.mm +49 -3
  26. package/native/appkit/tab-walk.h +31 -0
  27. package/native/appkit/tab-walk.mm +64 -0
  28. package/native/appkit/text-input-field.h +5 -0
  29. package/native/appkit/text-input-field.mm +101 -20
  30. package/native/appkit/text-layout.h +7 -1
  31. package/native/appkit/text-layout.mm +161 -30
  32. package/native/appkit/web-view-component.h +4 -3
  33. package/native/appkit/web-view-component.mm +8 -2
  34. package/native/appkit/web-view.mm +7 -0
  35. package/native/cmake/react-native.cmake +9 -0
  36. package/native/cmake/runtime-dependencies.cmake +21 -0
  37. package/native/core/include/ngn/app-menus.h +8 -3
  38. package/native/core/include/ngn/back-swipe.h +16 -7
  39. package/native/core/include/ngn/camera.h +103 -9
  40. package/native/core/include/ngn/core-modules.h +3 -0
  41. package/native/core/include/ngn/dom-web-view.h +53 -0
  42. package/native/core/include/ngn/photo-library.h +7 -3
  43. package/native/core/include/ngn/runtime.h +10 -8
  44. package/native/core/include/ngn/safe-areas.h +5 -0
  45. package/native/core/include/ngn/scrollers.h +5 -0
  46. package/native/core/include/ngn/surface-metrics.h +51 -0
  47. package/native/core/include/ngn/svg.h +49 -8
  48. package/native/core/include/ngn/text-input.h +9 -0
  49. package/native/core/include/ngn/text-measurer.h +11 -0
  50. package/native/core/include/ngn/web-view.h +6 -2
  51. package/native/core/src/app-menus.cpp +10 -3
  52. package/native/core/src/back-swipe.cpp +12 -5
  53. package/native/core/src/dom-web-view.cpp +45 -0
  54. package/native/core/src/libraries/rnsvg.cpp +13 -0
  55. package/native/core/src/modules/app-modules.cpp +24 -15
  56. package/native/core/src/modules/camera.cpp +203 -23
  57. package/native/core/src/modules/photo-library.cpp +22 -8
  58. package/native/core/src/mounting-manager.cpp +4 -0
  59. package/native/core/src/runtime.cpp +6 -8
  60. package/native/core/src/safe-areas.cpp +7 -0
  61. package/native/core/src/scrollers.cpp +11 -0
  62. package/native/core/src/svg.cpp +324 -17
  63. package/native/core/src/text-input.cpp +13 -0
  64. package/native/core/src/text-layout-manager.cpp +13 -0
  65. package/native/core/src/web-view.cpp +1 -1
  66. package/native/scripts/build.sh +12 -2
  67. package/native/scripts/fetch-hermes.sh +70 -0
  68. package/native/scripts/launch-probe.sh +60 -0
  69. package/native/scripts/make-prebuilt.mjs +13 -6
  70. package/native/scripts/prebuilt-host-cache.mjs +1 -1
  71. package/native/shim/text-platform/react/renderer/textlayoutmanager/TextLayoutManager.h +52 -0
  72. package/package.json +2 -2
  73. package/prebuilt.json +6 -6
@@ -62,10 +62,6 @@ class DevUI final : public IDevUIDelegate {
62
62
  std::function<void()> &resumed_;
63
63
  };
64
64
 
65
- LayoutConstraints constraints(const SurfaceMetrics &metrics) {
66
- return {.minimumSize = metrics.size, .maximumSize = metrics.size};
67
- }
68
-
69
65
  LayoutContext context(const SurfaceMetrics &metrics) {
70
66
  LayoutContext context;
71
67
  context.pointScaleFactor = metrics.pointScale;
@@ -239,6 +235,7 @@ bool Runtime::loadBundle() {
239
235
  }
240
236
  // With dev mode on, ReactHost downloads the source path through the dev server helper, which
241
237
  // takes the bundle URL as it is.
238
+ direction_.instanceStarted();
242
239
  return devServer_ ? host_->loadScript("", bundle_) : host_->loadScript(bundle_, bundle_);
243
240
  }
244
241
 
@@ -249,7 +246,7 @@ void Runtime::startSurface(SurfaceId surfaceId, const std::string &moduleName, c
249
246
  }
250
247
  std::lock_guard lock(mutex_);
251
248
  if (host_) {
252
- host_->startSurface(surfaceId, moduleName, folly::dynamic::object(), constraints(metrics), context(metrics));
249
+ host_->startSurface(surfaceId, moduleName, folly::dynamic::object(), surfaceConstraints(metrics, direction_.rightToLeft()), context(metrics));
253
250
  }
254
251
  }
255
252
 
@@ -272,7 +269,7 @@ void Runtime::updateSurface(SurfaceId surfaceId, const SurfaceMetrics &metrics)
272
269
  }
273
270
  std::lock_guard lock(mutex_);
274
271
  if (host_) {
275
- host_->setSurfaceConstraints(surfaceId, constraints(metrics), context(metrics));
272
+ host_->setSurfaceConstraints(surfaceId, surfaceConstraints(metrics, direction_.rightToLeft()), context(metrics));
276
273
  }
277
274
  }
278
275
 
@@ -340,6 +337,7 @@ void Runtime::reload(std::function<void(bool)> done) {
340
337
  workletHost().runOnScheduler.set(nullptr);
341
338
  host_.reset();
342
339
  createHost();
340
+ direction_.instanceStarted();
343
341
  *loaded = devServer_ ? host_->loadScript("", bundle_) : host_->loadScript(bundle_, bundle_);
344
342
  restarted = true;
345
343
  }
@@ -366,12 +364,12 @@ void Runtime::reload(std::function<void(bool)> done) {
366
364
  std::lock_guard lock(mutex_);
367
365
  for (const auto &[surfaceId, surface] : step->start) {
368
366
  host_->startSurface(
369
- surfaceId, surface.moduleName, folly::dynamic::object(), constraints(surface.metrics), context(surface.metrics));
367
+ surfaceId, surface.moduleName, folly::dynamic::object(), surfaceConstraints(surface.metrics, direction_.rightToLeft()), context(surface.metrics));
370
368
  started.insert(surfaceId);
371
369
  }
372
370
  // A surface resized after the reload started it takes its size now.
373
371
  for (const auto &[surfaceId, surface] : step->update) {
374
- host_->setSurfaceConstraints(surfaceId, constraints(surface.metrics), context(surface.metrics));
372
+ host_->setSurfaceConstraints(surfaceId, surfaceConstraints(surface.metrics, direction_.rightToLeft()), context(surface.metrics));
375
373
  }
376
374
  for (auto surfaceId : step->stop) {
377
375
  host_->stopSurface(surfaceId);
@@ -17,6 +17,13 @@ EdgeInsets insetsWithin(const ContainerInsets &container, const Rect &frame) {
17
17
  };
18
18
  }
19
19
 
20
+ double titlebarLeading(double minX, double maxX, double width, bool rightToLeft) {
21
+ if (maxX <= 0 || minX >= width) {
22
+ return 0;
23
+ }
24
+ return rightToLeft ? width - minX : maxX;
25
+ }
26
+
20
27
  void SafeAreas::created(const ShadowView &view) {
21
28
  std::string_view name = view.componentName;
22
29
  if (name == "RNCSafeAreaProvider") {
@@ -1,5 +1,8 @@
1
1
  #include "ngn/scrollers.h"
2
2
 
3
+ #include <algorithm>
4
+ #include <cmath>
5
+
3
6
  using namespace facebook::react;
4
7
  namespace yoga = facebook::yoga;
5
8
 
@@ -20,6 +23,14 @@ ScrollerVisibility scrollerVisibility(
20
23
  };
21
24
  }
22
25
 
26
+ float pagedOffset(float offset, float velocity, float page, float max) {
27
+ // ponytail: a flick is a fixed speed, as UIKit's paging roughly is; tune here if it feels off.
28
+ constexpr float flick = 0.3f;
29
+ float at = page > 0 ? offset / page : 0;
30
+ float index = velocity > flick ? std::floor(at) + 1 : velocity < -flick ? std::ceil(at) - 1 : std::round(at);
31
+ return std::clamp(index * page, 0.0f, std::max(0.0f, max));
32
+ }
33
+
23
34
  ScrollerGutter scrollerGutter(ScrollerStyle style, float legacyWidth, const ScrollerVisibility &visibility) {
24
35
  if (style == ScrollerStyle::Overlay) {
25
36
  return {};
@@ -648,6 +648,79 @@ bool listed(const std::vector<std::string> &list, const char *name) {
648
648
  return std::find(list.begin(), list.end(), name) != list.end();
649
649
  }
650
650
 
651
+ // A path as lines, for text set along it, measured from its start.
652
+ struct TextTrack {
653
+ struct Line {
654
+ double x0, y0, x1, y1, length;
655
+ };
656
+ std::vector<Line> lines;
657
+ double length{0};
658
+
659
+ // The point `distance` along the track and its direction there in radians, the next line's at a
660
+ // corner; nothing off the track, whose very end is off it, as Chrome places glyphs.
661
+ std::optional<std::array<double, 3>> at(double distance) const {
662
+ if (distance < 0) {
663
+ return std::nullopt;
664
+ }
665
+ double from = 0;
666
+ for (const auto &l : lines) {
667
+ if (distance < from + l.length) {
668
+ double t = (distance - from) / l.length;
669
+ return std::array<double, 3>{l.x0 + (l.x1 - l.x0) * t, l.y0 + (l.y1 - l.y0) * t, std::atan2(l.y1 - l.y0, l.x1 - l.x0)};
670
+ }
671
+ from += l.length;
672
+ }
673
+ return std::nullopt;
674
+ }
675
+ };
676
+
677
+ // `path` as lines, each curve as 128, in order or reversed. A move between subpaths adds no length.
678
+ TextTrack textTrack(const SvgPath &path, bool reversed) {
679
+ TextTrack track;
680
+ double x = 0, y = 0, startX = 0, startY = 0;
681
+ auto line = [&](double toX, double toY) {
682
+ track.lines.push_back({x, y, toX, toY, std::hypot(toX - x, toY - y)});
683
+ x = toX;
684
+ y = toY;
685
+ };
686
+ for (const auto &s : path.segments) {
687
+ const double *p = s.points;
688
+ switch (s.op) {
689
+ case SvgPath::Op::Move:
690
+ x = startX = p[0];
691
+ y = startY = p[1];
692
+ break;
693
+ case SvgPath::Op::Line:
694
+ line(p[0], p[1]);
695
+ break;
696
+ case SvgPath::Op::Cubic: {
697
+ double x0 = x, y0 = y;
698
+ constexpr int steps = 128;
699
+ for (int i = 1; i <= steps; ++i) {
700
+ double t = static_cast<double>(i) / steps, u = 1 - t;
701
+ double a = u * u * u, b = 3 * u * u * t, c = 3 * u * t * t, d = t * t * t;
702
+ line(a * x0 + b * p[0] + c * p[2] + d * p[4], a * y0 + b * p[1] + c * p[3] + d * p[5]);
703
+ }
704
+ break;
705
+ }
706
+ case SvgPath::Op::Close:
707
+ line(startX, startY);
708
+ break;
709
+ }
710
+ }
711
+ if (reversed) {
712
+ std::reverse(track.lines.begin(), track.lines.end());
713
+ for (auto &l : track.lines) {
714
+ std::swap(l.x0, l.x1);
715
+ std::swap(l.y0, l.y1);
716
+ }
717
+ }
718
+ for (const auto &l : track.lines) {
719
+ track.length += l.length;
720
+ }
721
+ return track;
722
+ }
723
+
651
724
  // The presentation an element draws with, after inheritance.
652
725
  struct Style {
653
726
  SvgBrush fill{.type = SvgBrush::Type::Color, .argb = 0xff000000};
@@ -665,6 +738,7 @@ struct Style {
665
738
  double fontSize{12};
666
739
  std::string fontFamily;
667
740
  std::string fontWeight;
741
+ std::string textAnchor{"start"};
668
742
  // The attributes set somewhere above or on the element, which its children inherit.
669
743
  std::unordered_set<std::string> list;
670
744
  };
@@ -757,6 +831,7 @@ class Renderer {
757
831
  s.fontSize = parent.fontSize;
758
832
  s.fontFamily = e.fontFamily.empty() ? parent.fontFamily : e.fontFamily;
759
833
  s.fontWeight = e.fontWeight.empty() ? parent.fontWeight : e.fontWeight;
834
+ s.textAnchor = e.textAnchor.empty() ? parent.textAnchor : e.textAnchor;
760
835
  if (e.fontSize.set) {
761
836
  s.fontSize = resolveIn(e.fontSize, parent.fontSize, parent.fontSize);
762
837
  }
@@ -838,12 +913,21 @@ class Renderer {
838
913
  }
839
914
  }
840
915
 
916
+ // A foreignObject's box: all of the viewport where it says nothing, as react-native-svg's
917
+ // ForeignObject defaults to 0%, 0%, 100% and 100%.
918
+ SvgRect foreignBox(const SvgElement &e) const {
919
+ return {length(e, "x", Axis::X), length(e, "y", Axis::Y), length(e, "width", Axis::X, vw_), length(e, "height", Axis::Y, vh_)};
920
+ }
921
+
841
922
  // What `node` covers in its own user space (before its own transform), for a gradient's or a
842
923
  // mask's objectBoundingBox.
843
924
  std::optional<SvgRect> bounds(const SvgNode &node) const {
844
925
  if (auto p = shape(node.element)) {
845
926
  return p->bounds();
846
927
  }
928
+ if (node.element.kind == SvgKind::ForeignObject) {
929
+ return foreignBox(node.element);
930
+ }
847
931
  if (node.element.kind != SvgKind::Group) {
848
932
  return std::nullopt;
849
933
  }
@@ -1111,12 +1195,23 @@ class Renderer {
1111
1195
  }
1112
1196
  break;
1113
1197
  }
1114
- case SvgKind::Text:
1115
- case SvgKind::TSpan: {
1116
- double x = 0, y = 0;
1117
- drawText(node, style, x, y);
1198
+ case SvgKind::ForeignObject: {
1199
+ // Its views, at their frames from its corner, cut to its box, as react-native-svg on iOS
1200
+ // draws them.
1201
+ SvgRect box = foreignBox(e);
1202
+ if (e.view != -1 && box.width > 0 && box.height > 0) {
1203
+ canvas_->save();
1204
+ canvas_->concat(SvgMatrix::translate(box.x, box.y));
1205
+ canvas_->clip(rectPath(0, 0, box.width, box.height), false);
1206
+ canvas_->views(e.view);
1207
+ canvas_->restore();
1208
+ }
1118
1209
  break;
1119
1210
  }
1211
+ case SvgKind::Text:
1212
+ case SvgKind::TSpan:
1213
+ drawText(node, style);
1214
+ break;
1120
1215
  default:
1121
1216
  break;
1122
1217
  }
@@ -1410,32 +1505,205 @@ class Renderer {
1410
1505
  }
1411
1506
  }
1412
1507
 
1413
- // A text or a span: its own position, if it sets one, then its characters, then its spans, each
1414
- // starting where the last one ended.
1415
- void drawText(const SvgNode &node, const Style &style, double &x, double &y) {
1508
+ // Text is laid out before it is drawn: runs of characters in chunks, as SVG has them, each chunk
1509
+ // set back by its anchor once its advance is known.
1510
+ struct TextRun {
1511
+ std::string text;
1512
+ double x{0}, y{0}, advance{0};
1513
+ double fontSize{12};
1514
+ std::string fontFamily, fontWeight;
1515
+ std::optional<SvgPaint> fill;
1516
+ // The spans and textPath it is in with an opacity below 1, outermost first, which draw through
1517
+ // no drawNode of their own: each a group, numbered in its layout, and its opacity.
1518
+ std::vector<std::pair<int, double>> groups;
1519
+ };
1520
+ struct TextChunk {
1521
+ std::string anchor;
1522
+ std::vector<TextRun> runs;
1523
+ // On a path: the path, and how far along it a run's x of 0 is; a run's y is then off the path.
1524
+ std::shared_ptr<const TextTrack> track;
1525
+ double startOffset{0};
1526
+ };
1527
+ struct TextLayout {
1528
+ std::vector<TextChunk> chunks;
1529
+ double x{0}, y{0};
1530
+ // The next characters start a chunk: an absolute position or a path came before them.
1531
+ bool chunkStarts{true};
1532
+ std::shared_ptr<const TextTrack> track;
1533
+ double startOffset{0};
1534
+ std::vector<std::pair<int, double>> groups;
1535
+ int nextGroup{0};
1536
+ // The x, y, dx and dy for the next character, from the innermost element that sets one and
1537
+ // holds it, and how many characters have been laid out.
1538
+ std::optional<double> pendingX, pendingY, pendingDx, pendingDy;
1539
+ size_t characters{0};
1540
+ };
1541
+
1542
+ static double anchorShift(const TextChunk &chunk) {
1543
+ double advance = chunk.runs.back().x + chunk.runs.back().advance - chunk.runs.front().x;
1544
+ return chunk.anchor == "middle" ? -advance / 2 : chunk.anchor == "end" ? -advance : 0;
1545
+ }
1546
+
1547
+ // Runs are drawn in the order they were laid out, so what a group holds is drawn together: each
1548
+ // group is one layer around its runs, its opacity applied once, as drawNode composites an element.
1549
+ void drawText(const SvgNode &node, const Style &style) {
1550
+ TextLayout layout;
1551
+ layoutText(node, style, layout);
1552
+ std::vector<int> open;
1553
+ for (const auto &chunk : layout.chunks) {
1554
+ double shift = anchorShift(chunk);
1555
+ for (const auto &run : chunk.runs) {
1556
+ bool hidden = std::any_of(run.groups.begin(), run.groups.end(), [](const auto &g) { return g.second <= 0; });
1557
+ if (!run.fill || hidden) {
1558
+ continue;
1559
+ }
1560
+ size_t shared = 0;
1561
+ while (shared < open.size() && shared < run.groups.size() && open[shared] == run.groups[shared].first) {
1562
+ ++shared;
1563
+ }
1564
+ for (; open.size() > shared; open.pop_back()) {
1565
+ canvas_->endLayer();
1566
+ }
1567
+ for (size_t i = shared; i < run.groups.size(); ++i) {
1568
+ canvas_->beginLayer(run.groups[i].second);
1569
+ open.push_back(run.groups[i].first);
1570
+ }
1571
+ drawRun(chunk, run, shift);
1572
+ }
1573
+ }
1574
+ for (; !open.empty(); open.pop_back()) {
1575
+ canvas_->endLayer();
1576
+ }
1577
+ }
1578
+
1579
+ void layoutChildren(const SvgNode &node, const Style &style, TextLayout &out) {
1580
+ for (const auto &child : node.children) {
1581
+ auto kind = child.element.kind;
1582
+ if (!child.element.hidden && (kind == SvgKind::TSpan || kind == SvgKind::Text || kind == SvgKind::TextPath)) {
1583
+ bool group = child.element.opacity < 1;
1584
+ if (group) {
1585
+ out.groups.emplace_back(out.nextGroup++, std::max(0.0, child.element.opacity));
1586
+ }
1587
+ layoutText(child, merge(style, child.element), out);
1588
+ if (group) {
1589
+ out.groups.pop_back();
1590
+ }
1591
+ }
1592
+ }
1593
+ }
1594
+
1595
+ // A text or a span: its own position, if it sets one, then its characters, then what it holds,
1596
+ // each starting where the last ended. An x or a y starts a chunk. On a path, as Chrome sets it,
1597
+ // x and dx are along the path from startOffset, dy is off it, and y places nothing.
1598
+ void layoutText(const SvgNode &node, const Style &style, TextLayout &out) {
1416
1599
  const auto &e = node.element;
1600
+ if (e.kind == SvgKind::TextPath) {
1601
+ layoutTextPath(node, style, out);
1602
+ return;
1603
+ }
1604
+ // An element's positions are its first character's, wherever among what it holds that is, and
1605
+ // one inside it sets them for the same character instead; one with no characters places nothing.
1606
+ auto saved = std::tuple{out.pendingX, out.pendingY, out.pendingDx, out.pendingDy};
1607
+ size_t before = out.characters;
1417
1608
  if (!e.xs.empty()) {
1418
- x = resolve(e.xs[0], Axis::X, style.fontSize);
1609
+ out.pendingX = resolve(e.xs[0], Axis::X, style.fontSize);
1419
1610
  }
1420
1611
  if (!e.ys.empty()) {
1421
- y = resolve(e.ys[0], Axis::Y, style.fontSize);
1612
+ out.pendingY = resolve(e.ys[0], Axis::Y, style.fontSize);
1422
1613
  }
1423
1614
  if (!e.dxs.empty()) {
1424
- x += resolve(e.dxs[0], Axis::X, style.fontSize);
1615
+ out.pendingDx = resolve(e.dxs[0], Axis::X, style.fontSize);
1425
1616
  }
1426
1617
  if (!e.dys.empty()) {
1427
- y += resolve(e.dys[0], Axis::Y, style.fontSize);
1618
+ out.pendingDy = resolve(e.dys[0], Axis::Y, style.fontSize);
1428
1619
  }
1429
1620
  if (!e.content.empty()) {
1430
- auto fill = paint(style.fill, style.fillOpacity, style, std::nullopt);
1431
- x += canvas_->text(e.content, x, y, style.fontSize, style.fontFamily, style.fontWeight, fill ? &*fill : nullptr);
1621
+ placeNextCharacter(out);
1622
+ if (out.chunkStarts) {
1623
+ out.chunks.push_back({.anchor = style.textAnchor, .track = out.track, .startOffset = out.startOffset});
1624
+ out.chunkStarts = false;
1625
+ }
1626
+ double advance = canvas_->measureText(e.content, style.fontSize, style.fontFamily, style.fontWeight);
1627
+ out.chunks.back().runs.push_back(
1628
+ {.text = e.content,
1629
+ .x = out.x,
1630
+ .y = out.y,
1631
+ .advance = advance,
1632
+ .fontSize = style.fontSize,
1633
+ .fontFamily = style.fontFamily,
1634
+ .fontWeight = style.fontWeight,
1635
+ .fill = paint(style.fill, style.fillOpacity, style, std::nullopt),
1636
+ .groups = out.groups});
1637
+ out.x += advance;
1638
+ ++out.characters;
1639
+ }
1640
+ layoutChildren(node, style, out);
1641
+ if (out.characters == before) {
1642
+ std::tie(out.pendingX, out.pendingY, out.pendingDx, out.pendingDy) = saved;
1643
+ }
1644
+ }
1645
+
1646
+ // The pending positions applied to the next character: an x or a y starts a chunk; on a path x
1647
+ // and dx are along it from startOffset, dy is off it, and y places nothing.
1648
+ static void placeNextCharacter(TextLayout &out) {
1649
+ if (out.pendingX) {
1650
+ out.x = *out.pendingX;
1651
+ out.chunkStarts = true;
1652
+ }
1653
+ if (out.pendingY && !out.track) {
1654
+ out.y = *out.pendingY;
1655
+ out.chunkStarts = true;
1656
+ }
1657
+ out.x += out.pendingDx.value_or(0);
1658
+ out.y += out.pendingDy.value_or(0);
1659
+ out.pendingX = out.pendingY = out.pendingDx = out.pendingDy = std::nullopt;
1660
+ }
1661
+
1662
+ // A textPath: what it holds along the shape it names, from startOffset, as a chunk of its own;
1663
+ // what follows carries on from the path's end, as Chrome places it. One that names no shape, or
1664
+ // is on a path already, draws nothing.
1665
+ void layoutTextPath(const SvgNode &node, const Style &style, TextLayout &out) {
1666
+ const auto &e = node.element;
1667
+ const SvgNode *target = out.track ? nullptr : named(e.href);
1668
+ auto shapePath = target ? shape(target->element) : std::nullopt;
1669
+ if (!shapePath) {
1670
+ return;
1432
1671
  }
1433
- for (const auto &child : node.children) {
1434
- if (child.element.hidden || (child.element.kind != SvgKind::TSpan && child.element.kind != SvgKind::Text)) {
1672
+ SvgPath path;
1673
+ path.append(*shapePath, target->element.matrix.value_or(SvgMatrix{}));
1674
+ auto track = std::make_shared<const TextTrack>(textTrack(path, e.side == "right"));
1675
+ if (track->length <= 0) {
1676
+ return;
1677
+ }
1678
+ auto offset = e.lengths.find("startOffset");
1679
+ out.track = track;
1680
+ out.startOffset = offset == e.lengths.end() ? 0 : resolveIn(offset->second, track->length, style.fontSize);
1681
+ out.x = out.y = 0;
1682
+ out.chunkStarts = true;
1683
+ layoutChildren(node, style, out);
1684
+ out.track.reset();
1685
+ out.x = track->lines.back().x1;
1686
+ out.y = track->lines.back().y1;
1687
+ out.chunkStarts = true;
1688
+ }
1689
+
1690
+ void drawRun(const TextChunk &chunk, const TextRun &run, double shift) {
1691
+ if (!chunk.track) {
1692
+ canvas_->text(run.text, run.x + shift, run.y, run.fontSize, run.fontFamily, run.fontWeight, *run.fill);
1693
+ return;
1694
+ }
1695
+ // Each glyph at the point its middle reaches along the path, turned with the path there, from
1696
+ // where the shaped run has it and as it shaped it, so kerning and joined forms are kept.
1697
+ for (const auto &glyph : canvas_->glyphClusters(run.text, run.fontSize, run.fontFamily, run.fontWeight)) {
1698
+ auto at = chunk.track->at(chunk.startOffset + shift + run.x + glyph.x + glyph.advance / 2);
1699
+ if (!at) {
1435
1700
  continue;
1436
1701
  }
1437
- Style inner = merge(style, child.element);
1438
- drawText(child, inner, x, y);
1702
+ double cos = std::cos((*at)[2]), sin = std::sin((*at)[2]);
1703
+ canvas_->save();
1704
+ canvas_->concat({cos, sin, -sin, cos, (*at)[0], (*at)[1]});
1705
+ canvas_->glyphCluster(glyph, -glyph.advance / 2, run.y, *run.fill);
1706
+ canvas_->restore();
1439
1707
  }
1440
1708
  }
1441
1709
 
@@ -1826,6 +2094,20 @@ std::optional<SvgElement> SvgDrawings::element(const ViewTree &tree, Tag tag) co
1826
2094
  }
1827
2095
 
1828
2096
  void SvgDrawings::changed(const ViewTree &tree, Tag tag) {
2097
+ if (auto e = element(tree, tag); e && e->kind == SvgKind::ForeignObject) {
2098
+ foreign_.insert(tag);
2099
+ }
2100
+ mark(tree, tag, false);
2101
+ repaintAbove(tree, tag);
2102
+ }
2103
+
2104
+ void SvgDrawings::relaidOut(const ViewTree &tree, Tag tag) {
2105
+ repaintAbove(tree, tag);
2106
+ }
2107
+
2108
+ // Marks the svg `tag` is an element of, if any, to be drawn again: when its document changed, or
2109
+ // always with `repaint`.
2110
+ void SvgDrawings::mark(const ViewTree &tree, Tag tag, bool repaint) {
1829
2111
  for (Tag at = tag; at != -1;) {
1830
2112
  auto e = element(tree, at);
1831
2113
  if (!e) {
@@ -1833,6 +2115,9 @@ void SvgDrawings::changed(const ViewTree &tree, Tag tag) {
1833
2115
  }
1834
2116
  if (e->kind == SvgKind::Svg) {
1835
2117
  dirty_.insert(at);
2118
+ if (repaint) {
2119
+ repaint_.insert(at);
2120
+ }
1836
2121
  return;
1837
2122
  }
1838
2123
  const auto *found = tree.find(at);
@@ -1840,9 +2125,23 @@ void SvgDrawings::changed(const ViewTree &tree, Tag tag) {
1840
2125
  }
1841
2126
  }
1842
2127
 
2128
+ // A view in a foreignObject, or the foreignObject itself, changed: each svg that draws it draws
2129
+ // again, though its document is the same.
2130
+ void SvgDrawings::repaintAbove(const ViewTree &tree, Tag tag) {
2131
+ if (foreign_.empty()) {
2132
+ return;
2133
+ }
2134
+ for (const auto *found = tree.find(tag); found != nullptr; found = tree.find(found->parent)) {
2135
+ if (foreign_.contains(found->view.tag)) {
2136
+ mark(tree, found->view.tag, true);
2137
+ }
2138
+ }
2139
+ }
2140
+
1843
2141
  void SvgDrawings::deleted(Tag tag) {
1844
2142
  dirty_.erase(tag);
1845
2143
  drawn_.erase(tag);
2144
+ foreign_.erase(tag);
1846
2145
  }
1847
2146
 
1848
2147
  std::optional<SvgNode> SvgDrawings::node(const ViewTree &tree, Tag tag) const {
@@ -1851,6 +2150,9 @@ std::optional<SvgNode> SvgDrawings::node(const ViewTree &tree, Tag tag) const {
1851
2150
  return std::nullopt;
1852
2151
  }
1853
2152
  SvgNode out{.element = std::move(*e)};
2153
+ if (out.element.kind == SvgKind::ForeignObject) {
2154
+ out.element.view = tag;
2155
+ }
1854
2156
  for (Tag child : tree.find(tag)->children) {
1855
2157
  if (auto c = node(tree, child); c && c->element.kind != SvgKind::Svg) {
1856
2158
  out.children.push_back(std::move(*c));
@@ -1862,6 +2164,8 @@ std::optional<SvgNode> SvgDrawings::node(const ViewTree &tree, Tag tag) const {
1862
2164
  std::vector<SvgDrawings::Drawing> SvgDrawings::settle(const ViewTree &tree) {
1863
2165
  std::vector<Tag> dirty(dirty_.begin(), dirty_.end());
1864
2166
  dirty_.clear();
2167
+ auto repaint = std::move(repaint_);
2168
+ repaint_.clear();
1865
2169
  std::sort(dirty.begin(), dirty.end());
1866
2170
  std::vector<Drawing> out;
1867
2171
  for (Tag tag : dirty) {
@@ -1872,6 +2176,9 @@ std::vector<SvgDrawings::Drawing> SvgDrawings::settle(const ViewTree &tree) {
1872
2176
  auto document = std::make_shared<const SvgDocument>(SvgDocument{.root = std::move(*root)});
1873
2177
  auto known = drawn_.find(tag);
1874
2178
  if (known != drawn_.end() && *known->second == *document) {
2179
+ if (repaint.contains(tag)) {
2180
+ out.push_back({.svg = tag, .document = known->second});
2181
+ }
1875
2182
  continue;
1876
2183
  }
1877
2184
  drawn_[tag] = document;
@@ -63,6 +63,19 @@ TextInputOptions textInputOptions(const DesktopTextInputProps &props) {
63
63
  return options;
64
64
  }
65
65
 
66
+ TextAlignment inputAlignment(std::optional<TextAlignment> alignment, LayoutDirection layoutDirection) {
67
+ if (!alignment) {
68
+ return layoutDirection == LayoutDirection::RightToLeft ? TextAlignment::Right : TextAlignment::Left;
69
+ }
70
+ switch (*alignment) {
71
+ case TextAlignment::Center:
72
+ case TextAlignment::Justified: return *alignment;
73
+ case TextAlignment::Right:
74
+ case TextAlignment::End: return TextAlignment::Right;
75
+ default: return TextAlignment::Left;
76
+ }
77
+ }
78
+
66
79
  std::size_t insertionThatFits(
67
80
  std::size_t current,
68
81
  std::size_t replaced,
@@ -60,4 +60,17 @@ TextMeasurement TextLayoutManager::measure(
60
60
  compute);
61
61
  }
62
62
 
63
+ LinesMeasurements TextLayoutManager::measureLines(
64
+ const AttributedStringBox &attributedStringBox,
65
+ const ParagraphAttributes &paragraphAttributes,
66
+ const Size &size) const {
67
+ auto measurer = contextContainer_
68
+ ? contextContainer_->find<std::shared_ptr<const ngn::TextMeasurer>>(ngn::kTextMeasurerKey)
69
+ : std::nullopt;
70
+ if (!measurer || !*measurer) {
71
+ return {};
72
+ }
73
+ return (*measurer)->lines(attributedStringBox.getValue(), paragraphAttributes, size.width);
74
+ }
75
+
63
76
  } // namespace facebook::react
@@ -43,7 +43,7 @@ NavigationStep navigationStep(const WebViewConfig &config, const std::string &ur
43
43
  bool userActivated = navigationType == "click" || navigationType == "formsubmit";
44
44
  return userActivated && externalUrlAllowed(url) ? NavigationStep::OpenExternally : NavigationStep::Cancel;
45
45
  }
46
- return NavigationStep::AskApp;
46
+ return config.asksApp ? NavigationStep::AskApp : NavigationStep::Allow;
47
47
  }
48
48
 
49
49
  std::string webViewMessagingScript() {
@@ -12,7 +12,7 @@
12
12
  # --universal arm64 and x86_64 in one binary (default: this machine's architecture)
13
13
  # --debug a Debug build (default: RelWithDebInfo)
14
14
  # --release a Release build without the core tests, as a packaged app ships it
15
- # --test run the core tests after building
15
+ # --test run the core tests after building, and the AppKit view tests when it built them
16
16
  # --no-tests leave the core tests out, as an app's build does: the published package has
17
17
  # none of their sources
18
18
  # --no-debug-info the default RelWithDebInfo build without its debug info, for a host that is
@@ -142,7 +142,13 @@ fi
142
142
 
143
143
  arch_key="${archs//;/-}"
144
144
  hermes_args=()
145
- if [ "$backend" != "none" ]; then
145
+ if [ "$backend" = "appkit" ]; then
146
+ # React Native's own prebuilt Hermes, universal: with the debugger for a dev build, as React
147
+ # Native's Debug builds have it, and without for Release.
148
+ hermes_flavour=debug
149
+ [ "$config" = Release ] && hermes_flavour=release
150
+ hermes_args=(-DNGN_HERMES_PREBUILT="$("$here/fetch-hermes.sh" "$RN_PKG" "$hermes_flavour")")
151
+ elif [ "$backend" != "none" ]; then
146
152
  hermes="$CACHE/hermes-$arch_key"
147
153
  # Built again when missing, or built for another macOS than the host's.
148
154
  if ! "$here/hermes-built.sh" "$hermes"; then
@@ -167,5 +173,9 @@ cmake --log-level=WARNING -G Ninja -S "$native_dir" -B "$BUILD" \
167
173
  nice -n 10 cmake --build "$BUILD" -j "$JOBS" >&2
168
174
  if [ "$run_tests" = 1 ]; then
169
175
  "$BUILD/ngn_core_tests" >&2
176
+ # The AppKit view tests, which a build with the AppKit backend has.
177
+ if [ -x "$BUILD/appkit/ngn_appkit_tests" ]; then
178
+ "$BUILD/appkit/ngn_appkit_tests" >&2
179
+ fi
170
180
  fi
171
181
  echo "$BUILD"