@windowkit/appkit 0.15.1 → 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.1",
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
@@ -5127,6 +5127,41 @@ static double HangingWhitespaceWidth(CTLineRef line, NSString* text,
5127
5127
  return width;
5128
5128
  }
5129
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
+
5130
5165
  // createLayout({ spans: [{text, font (handle), color:[r,g,b,a]}],
5131
5166
  // maxWidth?, align: 0 left | 0.5 center | 1 right,
5132
5167
  // lineHeight?, maxLines?, ellipsis?, rtl?,
@@ -5189,10 +5224,15 @@ static Napi::Value CreateLayout(const Napi::CallbackInfo& info) {
5189
5224
  long start = 0;
5190
5225
  long lineIndex = 0;
5191
5226
  double breakWidth = bounded ? maxWidth : 1e9;
5227
+ CFStringTokenizerRef breaks = nullptr;
5192
5228
  while (start < total) {
5193
5229
  long count =
5194
5230
  (long)CTTypesetterSuggestLineBreak(ts, start, breakWidth);
5195
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
+ }
5196
5236
  bool lastAllowed = maxLines > 0 && lineIndex == maxLines - 1;
5197
5237
  bool more = start + count < total;
5198
5238
  CTLineRef line = nullptr;
@@ -5293,6 +5333,7 @@ static Napi::Value CreateLayout(const Napi::CallbackInfo& info) {
5293
5333
  start = lineEnd;
5294
5334
  if (maxLines > 0 && lineIndex >= maxLines) break;
5295
5335
  }
5336
+ if (breaks) CFRelease(breaks);
5296
5337
  layout->height = y;
5297
5338
  }
5298
5339