@windowkit/appkit 0.34.0 → 0.34.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
@@ -921,6 +921,21 @@ process with no audio, and — only where System Audio Recording is allowed —
921
921
  taps everything and one process; `test/system-audio-threaded.js` lists and
922
922
  refuses from a worker.
923
923
 
924
+ ## Text: fonts macOS would download
925
+
926
+ macOS lists some 1,300 faces it downloads on demand, Hei, Kai and Songti among them, and
927
+ asking AppKit or CoreText for one by name, in a process with an NSApp, puts up "<app> needs
928
+ to download the font …" and holds the call until someone answers. A family list is a
929
+ browser's to write, and Chinese fallback stacks name these families as a matter of course,
930
+ so `matchFont`, `fontByPostScriptName`, `listFonts({ family })` and a pop-up's `fontFamily`
931
+ match only a family or a PostScript name of a face available now — installed, or registered
932
+ by the process — and pass over any other, as they pass over a name nobody has. Nothing is
933
+ downloaded and nothing is asked.
934
+
935
+ - **`fontAvailable(name)`** — whether a family or PostScript name, in any case, is one of
936
+ those: what the verbs above ask first. Read from CoreText's collection of available fonts,
937
+ which activates nothing, and kept until the registered fonts change.
938
+
924
939
  ## Text: at a display's scale
925
940
 
926
941
  A text verb's size is the caller's pixels, and a renderer that draws in device pixels hands
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@windowkit/appkit",
3
- "version": "0.34.0",
3
+ "version": "0.34.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/layer-transform.js && node test/contents-gravity.js && node test/contents-center.js && node test/accessibility-display.js && node test/ignores-mouse-events.js && node test/popup-menu.js && node test/pasteboard-write.js && node test/surface-blit.js && node test/surface-faded.js && node test/surface-filtered.js && node test/image-smoothing.js && node test/surface-colorspace.js && node test/video-surface.js && node test/player.js && node test/player-vp9.js && node test/capture.js && node test/capture-threaded.js && node test/display-stream.js && node test/system-audio.js && node test/system-audio-threaded.js && node test/round-rect-xy.js && node test/clip-rule.js && node test/radial-gradient.js && node test/control-bezels.js && node test/text-features.js && node test/font-from-data.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/text-justify.js && node test/text-scale.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/notifications.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/resize-no-crossing.js && node test/window-states.js && node test/threaded-window-states.js && node test/activation-policy.js && node test/last-input.js"
46
+ "test": "node test/app-lifecycle.js && node test/animation.js && node test/layer-transform.js && node test/contents-gravity.js && node test/contents-center.js && node test/accessibility-display.js && node test/ignores-mouse-events.js && node test/popup-menu.js && node test/pasteboard-write.js && node test/surface-blit.js && node test/surface-faded.js && node test/surface-filtered.js && node test/image-smoothing.js && node test/surface-colorspace.js && node test/video-surface.js && node test/player.js && node test/player-vp9.js && node test/capture.js && node test/capture-threaded.js && node test/display-stream.js && node test/system-audio.js && node test/system-audio-threaded.js && node test/round-rect-xy.js && node test/clip-rule.js && node test/radial-gradient.js && node test/control-bezels.js && node test/text-features.js && node test/font-from-data.js && node test/font-download.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/text-justify.js && node test/text-scale.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/notifications.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/resize-no-crossing.js && node test/window-states.js && node test/threaded-window-states.js && node test/activation-policy.js && node test/last-input.js"
47
47
  },
48
48
  "dependencies": {
49
49
  "node-addon-api": "^8.3.0"
Binary file
package/src/backend.mm CHANGED
@@ -2505,11 +2505,14 @@ static PopUpSpec ParsePopUpSpec(Napi::Object o) {
2505
2505
  return s;
2506
2506
  }
2507
2507
 
2508
+ static bool BFontAvailable(NSString* name);
2509
+
2508
2510
  // The family asked for when the system has it — a face it matched under
2509
- // another name is not it — and the menu font at that size otherwise.
2511
+ // another name is not it, and one it would download is not had (see
2512
+ // BFontAvailable) — and the menu font at that size otherwise.
2510
2513
  static NSFont* PopUpFont(NSString* family, double size) {
2511
2514
  CGFloat sz = size > 0 ? size : [NSFont systemFontSize];
2512
- if (family.length) {
2515
+ if (BFontAvailable(family)) {
2513
2516
  NSFontDescriptor* d = [NSFontDescriptor
2514
2517
  fontDescriptorWithFontAttributes:@{NSFontFamilyAttribute : family}];
2515
2518
  NSFont* f = [NSFont fontWithDescriptor:d size:sz];
@@ -5168,6 +5171,86 @@ static CTFontRef BAtScale(CTFontRef font, double scale) {
5168
5171
  return scaled;
5169
5172
  }
5170
5173
 
5174
+ // --- what is installed -----------------------------------------------------
5175
+ //
5176
+ // macOS lists some 1,300 faces it will download the first time an app asks
5177
+ // for one — Hei, Kai, Songti and the rest of the CJK sets among them — and
5178
+ // asking by name is enough: `fontWithDescriptor:`, `fontWithName:` and
5179
+ // CTFontCreateWithName of such a family, in a process with an NSApp, put up
5180
+ // "<app> needs to download the font …" and block the call until it is
5181
+ // answered. A browser's font stacks name these families as a matter of
5182
+ // course ("PingFang SC", "Hei", …), so every page with a Chinese fallback
5183
+ // stopped its process on a prompt per font. So a name is matched only where
5184
+ // it is a family or a PostScript name of a face this process can use now —
5185
+ // installed, or registered by the process — read from CoreText's collection
5186
+ // of available fonts, which lists no downloadable face and activates
5187
+ // nothing. The names are kept until the registered fonts change.
5188
+
5189
+ static std::atomic<uint64_t> gFontsGeneration{1};
5190
+
5191
+ static void BFontsChanged(CFNotificationCenterRef, void*, CFStringRef,
5192
+ const void*, CFDictionaryRef) {
5193
+ gFontsGeneration++;
5194
+ }
5195
+
5196
+ /** Whether `name` (any case) is a family or a PostScript name of a face
5197
+ * available without a download. Names CoreText keeps hidden (".SF NS")
5198
+ * pass: no download is ever one of them. */
5199
+ static bool BFontAvailable(NSString* name) {
5200
+ if (name.length == 0) return false;
5201
+ if ([name hasPrefix:@"."]) return true;
5202
+ static std::mutex lock;
5203
+ static NSSet<NSString*>* names = nil;
5204
+ static uint64_t built = 0;
5205
+ static dispatch_once_t once;
5206
+ dispatch_once(&once, ^{
5207
+ CFNotificationCenterAddObserver(
5208
+ CFNotificationCenterGetLocalCenter(), NULL, BFontsChanged,
5209
+ kCTFontManagerRegisteredFontsChangedNotification, NULL,
5210
+ CFNotificationSuspensionBehaviorDeliverImmediately);
5211
+ });
5212
+ NSSet<NSString*>* now = nil;
5213
+ {
5214
+ std::lock_guard<std::mutex> guard(lock);
5215
+ const uint64_t generation = gFontsGeneration.load();
5216
+ if (!names || built != generation) {
5217
+ NSMutableSet<NSString*>* all = [NSMutableSet set];
5218
+ CTFontCollectionRef available =
5219
+ CTFontCollectionCreateFromAvailableFonts(NULL);
5220
+ CFArrayRef faces =
5221
+ available ? CTFontCollectionCreateMatchingFontDescriptors(available)
5222
+ : NULL;
5223
+ const CFIndex count = faces ? CFArrayGetCount(faces) : 0;
5224
+ for (CFIndex i = 0; i < count; i++) {
5225
+ CTFontDescriptorRef d =
5226
+ (CTFontDescriptorRef)CFArrayGetValueAtIndex(faces, i);
5227
+ for (CFStringRef attribute :
5228
+ {kCTFontFamilyNameAttribute, kCTFontNameAttribute}) {
5229
+ CFStringRef value =
5230
+ (CFStringRef)CTFontDescriptorCopyAttribute(d, attribute);
5231
+ if (!value) continue;
5232
+ [all addObject:((__bridge NSString*)value).lowercaseString];
5233
+ CFRelease(value);
5234
+ }
5235
+ }
5236
+ if (faces) CFRelease(faces);
5237
+ if (available) CFRelease(available);
5238
+ names = [all copy];
5239
+ built = generation;
5240
+ }
5241
+ now = names;
5242
+ }
5243
+ return [now containsObject:name.lowercaseString];
5244
+ }
5245
+
5246
+ // fontAvailable(name) -> whether a family or PostScript name answers without
5247
+ // macOS downloading a font: what matchFont, fontByPostScriptName and
5248
+ // listFonts ask before they match one.
5249
+ static Napi::Value FontAvailable(const Napi::CallbackInfo& info) {
5250
+ return Napi::Boolean::New(
5251
+ info.Env(), info[0].IsString() && BFontAvailable(BToNSString(info[0])));
5252
+ }
5253
+
5171
5254
  // matchFont({ families: [..], size, weight (100-900), italic, scale? })
5172
5255
  // -> font handle
5173
5256
  // `size` is in the caller's pixels; `scale`, the display's, makes the font
@@ -5192,6 +5275,10 @@ static NSFont* ResolveFamily(NSString* family, double size, double weight,
5192
5275
  font = [NSFont fontWithName:@"Times New Roman" size:size];
5193
5276
  } else if ([lower isEqualToString:@"cursive"]) {
5194
5277
  font = [NSFont fontWithName:@"Snell Roundhand" size:size];
5278
+ } else if (!BFontAvailable(family)) {
5279
+ // not here, or here only for a download (see BFontAvailable): not found,
5280
+ // and the next family in the list gets its turn
5281
+ return nil;
5195
5282
  } else {
5196
5283
  // A named family. Build a descriptor so weight/width participate in
5197
5284
  // matching; verify the match really is this family (CoreText silently
@@ -5817,6 +5904,7 @@ static Napi::Value FontByPostScriptName(const Napi::CallbackInfo& info) {
5817
5904
  NSString* name = BToNSString(info[0]);
5818
5905
  double size = info[1].As<Napi::Number>().DoubleValue();
5819
5906
  const double scale = BScaleOf(info[2]);
5907
+ if (!BFontAvailable(name)) return env.Null(); // nor a download's
5820
5908
  const CGAffineTransform m = CGAffineTransformMakeScale(scale, scale);
5821
5909
  CTFontRef ct = CTFontCreateWithName((__bridge CFStringRef)name,
5822
5910
  size / scale, scale == 1 ? NULL : &m);
@@ -5927,6 +6015,8 @@ static Napi::Value ListFonts(const Napi::CallbackInfo& info) {
5927
6015
  : nil;
5928
6016
  CFArrayRef matches = NULL;
5929
6017
  if (family && family.length > 0) {
6018
+ // a family macOS would download has no faces yet (see BFontAvailable)
6019
+ if (!BFontAvailable(family)) return Napi::Array::New(env);
5930
6020
  CTFontDescriptorRef d = CTFontDescriptorCreateWithAttributes(
5931
6021
  (__bridge CFDictionaryRef)
5932
6022
  @{(__bridge id)kCTFontFamilyNameAttribute : family});
@@ -5983,6 +6073,7 @@ static Napi::Value LoadFontData(const Napi::CallbackInfo& info) {
5983
6073
  CTFontManagerRegisterFontDescriptors((__bridge CFArrayRef)@[ (__bridge id)desc ],
5984
6074
  kCTFontManagerScopeProcess, YES, NULL);
5985
6075
  (void)err;
6076
+ gFontsGeneration++; // the face answers matchFont now, not at the notification
5986
6077
  CTFontRef font = CTFontCreateWithFontDescriptor(desc, 12, NULL);
5987
6078
  CFRelease(desc);
5988
6079
  if (!font) return env.Null();
@@ -8432,6 +8523,7 @@ void InitBackend(Napi::Env env, Napi::Object exports) {
8432
8523
  BFN("surfaceToLayer", SurfaceToLayer);
8433
8524
  BFN("scrollSurface", ScrollSurface);
8434
8525
  BFN("matchFont", MatchFont);
8526
+ BFN("fontAvailable", FontAvailable);
8435
8527
  BFN("fontMetrics", FontMetrics);
8436
8528
  BFN("fontHasGlyph", FontHasGlyph);
8437
8529
  BFN("fontGlyphForCodepoint", FontGlyphForCodepoint);