@windowkit/appkit 0.14.1 → 0.15.1

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
@@ -531,6 +531,17 @@ const label = native.createLayout({
531
531
  });
532
532
  ```
533
533
 
534
+ ## Text: where a line's glyphs sit
535
+
536
+ `createLayout`'s `lineHeight` is a multiplier over each line's natural height — its
537
+ ascent, descent and the face's line gap — and a line's box is that tall. Whatever the box
538
+ has beyond the glyphs' ascent and descent is split evenly above and below them: each line's
539
+ `baseline` is its `y`, plus half that leading, plus its `ascent`. That is CSS's
540
+ half-leading, and it is ntk's, the engine react-x11 draws text with on X11 and Wayland, so
541
+ one tree sets its text in the same place on both. Below 1 the same split takes room from
542
+ both sides and the glyphs overflow a short box evenly; `0` is a box of no height, as CSS's
543
+ `line-height: 0` is. An absent multiplier is 1.
544
+
534
545
  ## Text: a layout's coverage
535
546
 
536
547
  - **`native.layoutCoverage(layout, pad?)`** — `{ width, height, data }`: how much of each
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@windowkit/appkit",
3
- "version": "0.14.1",
3
+ "version": "0.15.1",
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/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"
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/text-line-height.js && node test/text-line-width.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
@@ -32,6 +32,7 @@
32
32
  #include <string>
33
33
  #include <unordered_map>
34
34
  #include <vector>
35
+ #include <vector>
35
36
 
36
37
  #include "channel.h"
37
38
 
@@ -5091,6 +5092,41 @@ static Napi::Value ReleaseTypesetter(const Napi::CallbackInfo& info) {
5091
5092
  return env.Undefined();
5092
5093
  }
5093
5094
 
5095
+ // The advance of the spaces, tabs and line breaks a line ends on — what
5096
+ // hangs past it, as ntk and CSS hang white space. CTLineGetTrailingWhitespace
5097
+ // Width counts everything CoreText calls white space, the no-break space
5098
+ // among it, where CSS measures a no-break space wherever it stands: `&nbsp;`
5099
+ // at the end of a cell or a span is there to take room. So the run is found
5100
+ // in the text, and its glyphs' advances summed, whichever way the line reads.
5101
+ static double HangingWhitespaceWidth(CTLineRef line, NSString* text,
5102
+ long start, long end) {
5103
+ long hang = end;
5104
+ while (hang > start) {
5105
+ unichar c = [text characterAtIndex:(NSUInteger)(hang - 1)];
5106
+ if (c != ' ' && c != '\t' && c != '\n' && c != '\r' && c != 0x2028 &&
5107
+ c != 0x2029)
5108
+ break;
5109
+ hang--;
5110
+ }
5111
+ if (hang == end) return 0;
5112
+ double width = 0;
5113
+ CFArrayRef runs = CTLineGetGlyphRuns(line);
5114
+ CFIndex n = CFArrayGetCount(runs);
5115
+ for (CFIndex i = 0; i < n; i++) {
5116
+ CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runs, i);
5117
+ CFIndex count = CTRunGetGlyphCount(run);
5118
+ if (count <= 0) continue;
5119
+ std::vector<CGSize> advances((size_t)count);
5120
+ std::vector<CFIndex> indices((size_t)count);
5121
+ CTRunGetAdvances(run, CFRangeMake(0, 0), advances.data());
5122
+ CTRunGetStringIndices(run, CFRangeMake(0, 0), indices.data());
5123
+ for (CFIndex g = 0; g < count; g++) {
5124
+ if (indices[g] >= hang && indices[g] < end) width += advances[g].width;
5125
+ }
5126
+ }
5127
+ return width;
5128
+ }
5129
+
5094
5130
  // createLayout({ spans: [{text, font (handle), color:[r,g,b,a]}],
5095
5131
  // maxWidth?, align: 0 left | 0.5 center | 1 right,
5096
5132
  // lineHeight?, maxLines?, ellipsis?, rtl?,
@@ -5115,7 +5151,11 @@ static Napi::Value CreateLayout(const Napi::CallbackInfo& info) {
5115
5151
  double maxWidth = BNumOr(o, "maxWidth", 0);
5116
5152
  bool bounded = maxWidth > 0 && std::isfinite(maxWidth);
5117
5153
  double flush = BNumOr(o, "align", 0);
5118
- double lineHeight = BNumOr(o, "lineHeight", 0);
5154
+ // A multiplier over each line's natural height, as ntk's is: absent is 1,
5155
+ // and 0 is a line box of no height — CSS's `line-height: 0`, which the
5156
+ // glyphs overflow evenly — not an option left unset.
5157
+ double lineHeight = BNumOr(o, "lineHeight", 1);
5158
+ if (!(lineHeight >= 0)) lineHeight = 1;
5119
5159
  long maxLines = (long)BNumOr(o, "maxLines", 0);
5120
5160
  bool ellipsis = BBoolOr(o, "ellipsis", false);
5121
5161
  bool rtl = BBoolOr(o, "rtl", false);
@@ -5157,7 +5197,9 @@ static Napi::Value CreateLayout(const Napi::CallbackInfo& info) {
5157
5197
  bool more = start + count < total;
5158
5198
  CTLineRef line = nullptr;
5159
5199
  long lineEnd = start + count;
5200
+ bool elided = false;
5160
5201
  if (lastAllowed && more && ellipsis && lastAttrs) {
5202
+ elided = true;
5161
5203
  // shape the whole remainder, then truncate it into the width
5162
5204
  CTLineRef whole =
5163
5205
  CTTypesetterCreateLine(ts, CFRangeMake(start, total - start));
@@ -5180,8 +5222,24 @@ static Napi::Value CreateLayout(const Napi::CallbackInfo& info) {
5180
5222
  }
5181
5223
  CGFloat ascent = 0, descent = 0, leading = 0;
5182
5224
  double lw = CTLineGetTypographicBounds(line, &ascent, &descent, &leading);
5225
+ // A line is as wide as its ink, as ntk measures it: the white space it
5226
+ // ends on hangs past it. CoreText counts that space in, so a paragraph
5227
+ // ending in one measured a space wider here than on X11 — and a box
5228
+ // sized to it grew by one — and every wrapped line counted the space
5229
+ // it broke at. Flush alignment already leaves it out. An elided line
5230
+ // ends on its ellipsis, whose glyph is no part of the text.
5231
+ lw -= elided ? CTLineGetTrailingWhitespaceWidth(line)
5232
+ : HangingWhitespaceWidth(line, as.string, start, lineEnd);
5183
5233
  double natural = ascent + descent + leading;
5184
- double advance = natural * (lineHeight > 0 ? lineHeight : 1);
5234
+ double advance = natural * lineHeight;
5235
+ // Half-leading, where ntk and CSS put a line's glyphs: whatever the
5236
+ // line box has beyond their ascent and descent — the font's own line
5237
+ // gap, and what the multiplier adds or takes away — is split evenly
5238
+ // above and below them. It all went under the text here, so a
5239
+ // paragraph set at 1.5 sat at the top of its lines on this backend and
5240
+ // in their middle on X11, and a line shorter than its glyphs
5241
+ // overflowed downwards only.
5242
+ double halfLeading = (advance - (ascent + descent)) / 2;
5185
5243
  CALLine L;
5186
5244
  L.line = line;
5187
5245
  L.width = lw;
@@ -5189,7 +5247,7 @@ static Napi::Value CreateLayout(const Napi::CallbackInfo& info) {
5189
5247
  L.ascent = ascent;
5190
5248
  L.descent = descent;
5191
5249
  L.y = y;
5192
- L.baseline = y + ascent;
5250
+ L.baseline = y + halfLeading + ascent;
5193
5251
  L.start = start;
5194
5252
  L.end = lineEnd;
5195
5253
  if (lineEnd > start) {