@windowkit/appkit 0.13.0 → 0.14.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.
package/README.md CHANGED
@@ -555,6 +555,38 @@ const { width, height, data } = native.layoutCoverage(label.handle, 4);
555
555
  // data[y * width + x] is 0-255, and the text's origin is at (4, 4)
556
556
  ```
557
557
 
558
+ ## Text: a paragraph laid out again
559
+
560
+ Most of a `createLayout` is the text becoming glyphs: the attributed string built from the
561
+ spans, and the typesetter CoreText shapes it with — two thirds of the call over a long
562
+ document. A layout of the same paragraph at another width breaks the same glyphs into other
563
+ lines, which is what a window resize does to every paragraph of a document, and what a
564
+ paragraph's max-content and wrapped measurements are.
565
+
566
+ - **`keep: true`** — the result carries the paragraph's typesetter as `typesetter`.
567
+ - **`typesetter`** — handed to a later `createLayout` in place of `spans`, it lays the same
568
+ text out at that call's `maxWidth`, `align`, `lineHeight`, `maxLines` and `ellipsis`,
569
+ and the layout is the one the spans would have made, line for line and pixel for pixel.
570
+ `rtl` is the typesetter's and ignored beside one.
571
+ - **`native.releaseTypesetter(typesetter)`** — frees it now, where a cache lets one go.
572
+ Idempotent; a released one handed to `createLayout` throws. The finalizer is the safety
573
+ net, and the memory is reported to V8, at about 25 bytes a UTF-16 unit.
574
+ - **`packed: true`** — the geometry as two `Float64Array`s in place of `lines`: `lineData`,
575
+ ten numbers a line (`x`, `y`, `width`, `height`, `baseline`, `ascent`, `descent`,
576
+ `start`, `end`, and how many runs it has), and `runData`, five a run in line order (`x`,
577
+ `width`, `start`, `end`, and `1` when it runs right to left). An object a line and a run
578
+ is a call into V8 a field, and for short paragraphs building them was a sixth of the call.
579
+
580
+ A 50-character paragraph on an M1 Pro: 34 µs a layout from its spans, 16 µs from its kept
581
+ typesetter, 8.6 µs packed.
582
+
583
+ ```js
584
+ const first = native.createLayout({ spans, maxWidth: 600, keep: true });
585
+ const narrower = native.createLayout({ typesetter: first.typesetter, maxWidth: 420, packed: true });
586
+ // narrower.lineData, narrower.runData
587
+ native.releaseTypesetter(first.typesetter);
588
+ ```
589
+
558
590
  ## SF Symbols in a surface
559
591
 
560
592
  The system's icons by name — what `createStatusItem`'s `image` and a menu item's `iconName`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@windowkit/appkit",
3
- "version": "0.13.0",
3
+ "version": "0.14.0",
4
4
  "description": "Retained-mode AppKit backend for Node.js: Core Animation (CALayer) layer trees, CoreText layout and drawing, IOSurface presentation, NSMenu, and native control bezels — the macOS half of react-x11.",
5
5
  "author": "Andrey Sidorov <andrey.sidorov@gmail.com>",
6
6
  "license": "MIT",
@@ -43,7 +43,7 @@
43
43
  "install": "node scripts/install.js",
44
44
  "build": "node-gyp rebuild",
45
45
  "demo": "node demo/demo.js",
46
- "test": "node test/app-lifecycle.js && node test/animation.js && node test/accessibility-display.js && node test/ignores-mouse-events.js && node test/surface-blit.js && node test/text-features.js && node test/text-coverage.js && node test/symbols.js && node test/eventkit-permissions.js && node test/calendars.js && node test/calendar-writes.js && node test/screencolor.js && node test/threaded.js && node test/threaded-verbs.js && node test/threaded-frames.js && node test/threaded-resize.js && node test/ending-worker.js && node test/live-resize.js && node test/activation-policy.js"
46
+ "test": "node test/app-lifecycle.js && node test/animation.js && node test/accessibility-display.js && node test/ignores-mouse-events.js && node test/surface-blit.js && node test/text-features.js && node test/text-coverage.js && node test/text-typesetter.js && node test/symbols.js && node test/eventkit-permissions.js && node test/calendars.js && node test/calendar-writes.js && node test/screencolor.js && node test/threaded.js && node test/threaded-verbs.js && node test/threaded-frames.js && node test/threaded-resize.js && node test/ending-worker.js && node test/live-resize.js && node test/activation-policy.js"
47
47
  },
48
48
  "dependencies": {
49
49
  "node-addon-api": "^8.3.0"
Binary file
package/src/backend.mm CHANGED
@@ -4981,27 +4981,45 @@ static CALLayout* LayoutFrom(Napi::Value v) {
4981
4981
  return (CALLayout*)v.As<Napi::External<void>>().Data();
4982
4982
  }
4983
4983
 
4984
- // createLayout({ spans: [{text, font (handle), color:[r,g,b,a]}],
4985
- // maxWidth?, align: 0 left | 0.5 center | 1 right,
4986
- // lineHeight?, maxLines?, ellipsis?, rtl? })
4987
- // -> { handle, width, height,
4988
- // lines: [{x,y,width,height,baseline,descent,start,end,
4989
- // runs:[{x,width,start,end,rtl}]}] }
4990
- static Napi::Value CreateLayout(const Napi::CallbackInfo& info) {
4991
- Napi::Env env = info.Env();
4992
- Napi::Object o = info[0].As<Napi::Object>();
4993
- double maxWidth = BNumOr(o, "maxWidth", 0);
4994
- bool bounded = maxWidth > 0 && std::isfinite(maxWidth);
4995
- double flush = BNumOr(o, "align", 0);
4996
- double lineHeight = BNumOr(o, "lineHeight", 0);
4997
- long maxLines = (long)BNumOr(o, "maxLines", 0);
4998
- bool ellipsis = BBoolOr(o, "ellipsis", false);
4999
- bool rtl = BBoolOr(o, "rtl", false);
5000
- if (ellipsis && maxLines <= 0) maxLines = 1;
4984
+ // --- a paragraph's typesetter, kept -----------------------------------------
4985
+ //
4986
+ // Most of a layout is the text becoming glyphs: the attributed string built
4987
+ // from the spans, and the typesetter CoreText shapes it with — two thirds of
4988
+ // a createLayout over a long document, the lines broken from it the rest. A
4989
+ // layout of the same paragraph at another width breaks the same glyphs into
4990
+ // other lines, so a caller that asks with `keep: true` gets the typesetter
4991
+ // back (`typesetter` on the result) and hands it to the next createLayout of
4992
+ // that text in place of the spans. It holds the shaped text and nothing
4993
+ // about a width: align, lineHeight, maxLines and the ellipsis are the
4994
+ // layout's own. `releaseTypesetter` frees one now; the finalizer is the
4995
+ // safety net, and the bytes are reported to V8 so that it runs.
4996
+ struct CALTypesetter {
4997
+ CTTypesetterRef ts = nullptr;
4998
+ NSAttributedString* text = nil; // strong, under ARC
4999
+ NSDictionary* lastAttrs = nil; // the last span's: the ellipsis token's
5000
+ bool rtl = false;
5001
+ int64_t bytes = 0;
5002
+ void Release(Napi::Env env) {
5003
+ if (ts) CFRelease(ts);
5004
+ ts = nullptr;
5005
+ text = nil;
5006
+ lastAttrs = nil;
5007
+ if (bytes) Napi::MemoryManagement::AdjustExternalMemory(env, -bytes);
5008
+ bytes = 0;
5009
+ }
5010
+ };
5011
+
5012
+ // About what CoreText keeps for a shaped paragraph — its glyphs, their
5013
+ // positions and advances, the string — measured at 25 bytes a UTF-16 unit.
5014
+ static const int64_t kTypesetterBytesPerUnit = 25;
5001
5015
 
5016
+ // The attributed string for `spans` and its typesetter; `ts` stays null for
5017
+ // an empty paragraph.
5018
+ static CALTypesetter* TypesetSpans(Napi::Array spans, bool rtl) {
5019
+ auto* shaped = new CALTypesetter();
5020
+ shaped->rtl = rtl;
5002
5021
  NSMutableAttributedString* as = [[NSMutableAttributedString alloc] init];
5003
5022
  NSDictionary* lastAttrs = nil;
5004
- Napi::Array spans = o.Get("spans").As<Napi::Array>();
5005
5023
  for (uint32_t i = 0; i < spans.Length(); i++) {
5006
5024
  Napi::Object span = spans.Get(i).As<Napi::Object>();
5007
5025
  NSString* text = span.Has("text") && span.Get("text").IsString()
@@ -5035,12 +5053,88 @@ static Napi::Value CreateLayout(const Napi::CallbackInfo& info) {
5035
5053
  [as appendAttributedString:[[NSAttributedString alloc] initWithString:text
5036
5054
  attributes:attrs]];
5037
5055
  }
5056
+ shaped->text = as;
5057
+ shaped->lastAttrs = lastAttrs;
5058
+ if (as.length > 0) {
5059
+ shaped->ts = CTTypesetterCreateWithAttributedString(
5060
+ (__bridge CFAttributedStringRef)as);
5061
+ }
5062
+ return shaped;
5063
+ }
5064
+
5065
+ static void TypesetterFinalize(Napi::Env env, void* d) {
5066
+ auto* shaped = (CALTypesetter*)d;
5067
+ shaped->Release(env);
5068
+ delete shaped;
5069
+ }
5070
+
5071
+ // releaseTypesetter(handle) — free a kept typesetter now. Idempotent; a
5072
+ // createLayout handed a released one throws.
5073
+ static Napi::Value ReleaseTypesetter(const Napi::CallbackInfo& info) {
5074
+ Napi::Env env = info.Env();
5075
+ if (!info[0].IsExternal()) {
5076
+ Napi::TypeError::New(env, "releaseTypesetter: expected a typesetter")
5077
+ .ThrowAsJavaScriptException();
5078
+ return env.Undefined();
5079
+ }
5080
+ ((CALTypesetter*)info[0].As<Napi::External<void>>().Data())->Release(env);
5081
+ return env.Undefined();
5082
+ }
5083
+
5084
+ // createLayout({ spans: [{text, font (handle), color:[r,g,b,a]}],
5085
+ // maxWidth?, align: 0 left | 0.5 center | 1 right,
5086
+ // lineHeight?, maxLines?, ellipsis?, rtl?,
5087
+ // keep?, typesetter?, packed? })
5088
+ // -> { handle, width, height,
5089
+ // lines: [{x,y,width,height,baseline,descent,start,end,
5090
+ // runs:[{x,width,start,end,rtl}]}],
5091
+ // typesetter? }
5092
+ //
5093
+ // `keep: true` returns the paragraph's typesetter as `typesetter`, and a
5094
+ // later createLayout given it in place of `spans` breaks the same shaped
5095
+ // text at its own options — see CALTypesetter; `rtl` is the typesetter's,
5096
+ // and ignored beside one. `packed: true` returns the geometry as two
5097
+ // Float64Arrays in place of `lines`, which is a tenth of the calls into the
5098
+ // engine for a paragraph of a few lines: `lineData`, ten numbers a line —
5099
+ // x, y, width, height, baseline, ascent, descent, start, end and how many
5100
+ // runs it has — and `runData`, five a run in line order — x, width, start,
5101
+ // end, and 1 when it runs right to left.
5102
+ static Napi::Value CreateLayout(const Napi::CallbackInfo& info) {
5103
+ Napi::Env env = info.Env();
5104
+ Napi::Object o = info[0].As<Napi::Object>();
5105
+ double maxWidth = BNumOr(o, "maxWidth", 0);
5106
+ bool bounded = maxWidth > 0 && std::isfinite(maxWidth);
5107
+ double flush = BNumOr(o, "align", 0);
5108
+ double lineHeight = BNumOr(o, "lineHeight", 0);
5109
+ long maxLines = (long)BNumOr(o, "maxLines", 0);
5110
+ bool ellipsis = BBoolOr(o, "ellipsis", false);
5111
+ bool rtl = BBoolOr(o, "rtl", false);
5112
+ bool keep = BBoolOr(o, "keep", false);
5113
+ bool packed = BBoolOr(o, "packed", false);
5114
+ if (ellipsis && maxLines <= 0) maxLines = 1;
5115
+
5116
+ CALTypesetter* shaped = nullptr;
5117
+ bool owned = false;
5118
+ if (o.Has("typesetter") && o.Get("typesetter").IsExternal()) {
5119
+ shaped = (CALTypesetter*)o.Get("typesetter")
5120
+ .As<Napi::External<void>>()
5121
+ .Data();
5122
+ if (!shaped->text) {
5123
+ Napi::Error::New(env, "createLayout: the typesetter was released")
5124
+ .ThrowAsJavaScriptException();
5125
+ return env.Undefined();
5126
+ }
5127
+ } else {
5128
+ shaped = TypesetSpans(o.Get("spans").As<Napi::Array>(), rtl);
5129
+ owned = true;
5130
+ }
5131
+ NSAttributedString* as = shaped->text;
5132
+ NSDictionary* lastAttrs = shaped->lastAttrs;
5133
+ CTTypesetterRef ts = shaped->ts;
5038
5134
 
5039
5135
  auto* layout = new CALLayout();
5040
5136
  long total = (long)as.length;
5041
- if (total > 0) {
5042
- CTTypesetterRef ts =
5043
- CTTypesetterCreateWithAttributedString((__bridge CFAttributedStringRef)as);
5137
+ if (total > 0 && ts) {
5044
5138
  double y = 0;
5045
5139
  long start = 0;
5046
5140
  long lineIndex = 0;
@@ -5132,7 +5226,6 @@ static Napi::Value CreateLayout(const Napi::CallbackInfo& info) {
5132
5226
  if (maxLines > 0 && lineIndex >= maxLines) break;
5133
5227
  }
5134
5228
  layout->height = y;
5135
- CFRelease(ts);
5136
5229
  }
5137
5230
 
5138
5231
  Napi::Object r = Napi::Object::New(env);
@@ -5141,6 +5234,46 @@ static Napi::Value CreateLayout(const Napi::CallbackInfo& info) {
5141
5234
  }));
5142
5235
  r.Set("width", layout->width);
5143
5236
  r.Set("height", layout->height);
5237
+ if (owned && keep) {
5238
+ shaped->bytes = (int64_t)total * kTypesetterBytesPerUnit;
5239
+ if (shaped->bytes)
5240
+ Napi::MemoryManagement::AdjustExternalMemory(env, shaped->bytes);
5241
+ r.Set("typesetter",
5242
+ Napi::External<void>::New(env, shaped, TypesetterFinalize));
5243
+ } else if (owned) {
5244
+ shaped->Release(env);
5245
+ delete shaped;
5246
+ }
5247
+ if (packed) {
5248
+ size_t runCount = 0;
5249
+ for (const CALLine& L : layout->lines) runCount += L.runs.size();
5250
+ Napi::Float64Array lineData =
5251
+ Napi::Float64Array::New(env, layout->lines.size() * 10);
5252
+ Napi::Float64Array runData = Napi::Float64Array::New(env, runCount * 5);
5253
+ size_t li = 0, ri = 0;
5254
+ for (const CALLine& L : layout->lines) {
5255
+ lineData[li++] = L.x;
5256
+ lineData[li++] = L.y;
5257
+ lineData[li++] = L.width;
5258
+ lineData[li++] = L.height;
5259
+ lineData[li++] = L.baseline;
5260
+ lineData[li++] = L.ascent;
5261
+ lineData[li++] = L.descent;
5262
+ lineData[li++] = (double)L.start;
5263
+ lineData[li++] = (double)L.end;
5264
+ lineData[li++] = (double)L.runs.size();
5265
+ for (const CALRun& R : L.runs) {
5266
+ runData[ri++] = R.x;
5267
+ runData[ri++] = R.width;
5268
+ runData[ri++] = (double)R.start;
5269
+ runData[ri++] = (double)R.end;
5270
+ runData[ri++] = R.rtl ? 1 : 0;
5271
+ }
5272
+ }
5273
+ r.Set("lineData", lineData);
5274
+ r.Set("runData", runData);
5275
+ return r;
5276
+ }
5144
5277
  Napi::Array lines = Napi::Array::New(env, layout->lines.size());
5145
5278
  for (size_t i = 0; i < layout->lines.size(); i++) {
5146
5279
  const CALLine& L = layout->lines[i];
@@ -6937,6 +7070,7 @@ void InitBackend(Napi::Env env, Napi::Object exports) {
6937
7070
  BFN("listFonts", ListFonts);
6938
7071
  BFN("loadFontData", LoadFontData);
6939
7072
  BFN("createLayout", CreateLayout);
7073
+ BFN("releaseTypesetter", ReleaseTypesetter);
6940
7074
  BFN("drawLayout", DrawLayout);
6941
7075
  BFN("ctxDrawGlyphs", CtxDrawGlyphs);
6942
7076
  BFN("ctxDrawSymbol", CtxDrawSymbol);