@windowkit/appkit 0.15.0 → 0.15.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@windowkit/appkit",
3
- "version": "0.15.0",
3
+ "version": "0.15.2",
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",
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,76 @@ 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
+
5130
+ // Where a line that CoreText broke inside a word should end instead.
5131
+ // CoreText breaks a word wider than the line between two of its clusters,
5132
+ // as ntk does; but where not even the word's first cluster fits it gives
5133
+ // the line that one cluster anyway, so a word in a box narrower than its
5134
+ // first letter came out a letter a line — and a width of 1, which is how a
5135
+ // layout is asked for its narrowest (a table column's minimum, the longest
5136
+ // word), measured the widest letter here and the longest word on X11. ntk
5137
+ // lets such a word overflow whole, and so does CSS: the line runs on to the
5138
+ // next place a line may break. Only a line of one cluster is looked at, so
5139
+ // ordinary wrapping costs a comparison.
5140
+ static long UnfitClusterRunsOn(CTTypesetterRef ts, NSString* text,
5141
+ CFStringTokenizerRef* breaks, long start,
5142
+ long count, long total, double width) {
5143
+ if (count > 16) return count;
5144
+ if ((long)CTTypesetterSuggestClusterBreak(ts, start, 0) != count) {
5145
+ return count;
5146
+ }
5147
+ CTLineRef probe = CTTypesetterCreateLine(ts, CFRangeMake(start, count));
5148
+ double w = CTLineGetTypographicBounds(probe, nullptr, nullptr, nullptr) -
5149
+ HangingWhitespaceWidth(probe, text, start, start + count);
5150
+ CFRelease(probe);
5151
+ if (w <= width) return count;
5152
+ if (!*breaks) {
5153
+ *breaks = CFStringTokenizerCreate(
5154
+ nullptr, (__bridge CFStringRef)text, CFRangeMake(0, total),
5155
+ kCFStringTokenizerUnitLineBreak, nullptr);
5156
+ }
5157
+ if (!*breaks) return count;
5158
+ CFStringTokenizerGoToTokenAtIndex(*breaks, start);
5159
+ CFRange word = CFStringTokenizerGetCurrentTokenRange(*breaks);
5160
+ if (word.location == kCFNotFound) return count;
5161
+ long end = std::min(total, (long)(word.location + word.length));
5162
+ return std::max(count, end - start);
5163
+ }
5164
+
5094
5165
  // createLayout({ spans: [{text, font (handle), color:[r,g,b,a]}],
5095
5166
  // maxWidth?, align: 0 left | 0.5 center | 1 right,
5096
5167
  // lineHeight?, maxLines?, ellipsis?, rtl?,
@@ -5153,15 +5224,22 @@ static Napi::Value CreateLayout(const Napi::CallbackInfo& info) {
5153
5224
  long start = 0;
5154
5225
  long lineIndex = 0;
5155
5226
  double breakWidth = bounded ? maxWidth : 1e9;
5227
+ CFStringTokenizerRef breaks = nullptr;
5156
5228
  while (start < total) {
5157
5229
  long count =
5158
5230
  (long)CTTypesetterSuggestLineBreak(ts, start, breakWidth);
5159
5231
  if (count <= 0) count = 1;
5232
+ if (bounded && start + count < total) {
5233
+ count = UnfitClusterRunsOn(ts, as.string, &breaks, start, count, total,
5234
+ breakWidth);
5235
+ }
5160
5236
  bool lastAllowed = maxLines > 0 && lineIndex == maxLines - 1;
5161
5237
  bool more = start + count < total;
5162
5238
  CTLineRef line = nullptr;
5163
5239
  long lineEnd = start + count;
5240
+ bool elided = false;
5164
5241
  if (lastAllowed && more && ellipsis && lastAttrs) {
5242
+ elided = true;
5165
5243
  // shape the whole remainder, then truncate it into the width
5166
5244
  CTLineRef whole =
5167
5245
  CTTypesetterCreateLine(ts, CFRangeMake(start, total - start));
@@ -5188,8 +5266,10 @@ static Napi::Value CreateLayout(const Napi::CallbackInfo& info) {
5188
5266
  // ends on hangs past it. CoreText counts that space in, so a paragraph
5189
5267
  // ending in one measured a space wider here than on X11 — and a box
5190
5268
  // sized to it grew by one — and every wrapped line counted the space
5191
- // it broke at. Flush alignment already leaves it out.
5192
- lw -= CTLineGetTrailingWhitespaceWidth(line);
5269
+ // it broke at. Flush alignment already leaves it out. An elided line
5270
+ // ends on its ellipsis, whose glyph is no part of the text.
5271
+ lw -= elided ? CTLineGetTrailingWhitespaceWidth(line)
5272
+ : HangingWhitespaceWidth(line, as.string, start, lineEnd);
5193
5273
  double natural = ascent + descent + leading;
5194
5274
  double advance = natural * lineHeight;
5195
5275
  // Half-leading, where ntk and CSS put a line's glyphs: whatever the
@@ -5253,6 +5333,7 @@ static Napi::Value CreateLayout(const Napi::CallbackInfo& info) {
5253
5333
  start = lineEnd;
5254
5334
  if (maxLines > 0 && lineIndex >= maxLines) break;
5255
5335
  }
5336
+ if (breaks) CFRelease(breaks);
5256
5337
  layout->height = y;
5257
5338
  }
5258
5339