@windowkit/appkit 0.22.0 → 0.23.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
@@ -558,6 +558,24 @@ native.ctxSetGlobalAlpha(win, 0.6);
558
558
  native.ctxDrawSurfaceFaded(win, card, 0, 0, 556, 300, 100, 100, 556, 300, 0.6);
559
559
  ```
560
560
 
561
+ - **`native.ctxSetImageSmoothing(surface, quality)`** — how an image or a surface drawn
562
+ scaled or turned is resampled: `'none'` (the nearest pixel), `'low'`, `'medium'` or
563
+ `'high'`, the 2D canvas's `imageSmoothingEnabled` and `imageSmoothingQuality`. It is
564
+ part of the graphics state, so `ctxSave` and `ctxRestore` scope it, and every context
565
+ starts at `'medium'`. At `'medium'` CoreGraphics resamples the whole source image for a
566
+ draw through a matrix, whatever the clip, so a surface drawn a tile at a time — a box
567
+ in perspective, each tile under its own clip and the matrix of the plane over it —
568
+ costs each tile all of the surface: 784 tiles of a 1400x1120 surface take some 200ms
569
+ on an M1 Pro, and 27ms at `'low'`, which is bilinear, what a layer in perspective is
570
+ drawn with. A draw at 1:1 on whole pixels is the same at every quality.
571
+
572
+ ```js
573
+ native.ctxSave(win);
574
+ native.ctxSetImageSmoothing(win, 'low');
575
+ // …the tiles…
576
+ native.ctxRestore(win);
577
+ ```
578
+
561
579
  ## Video surfaces
562
580
 
563
581
  A frame a decoder hands over as bytes — ffmpeg over a pipe, a WASM decoder, an addon —
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@windowkit/appkit",
3
- "version": "0.22.0",
3
+ "version": "0.23.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/layer-transform.js && node test/accessibility-display.js && node test/ignores-mouse-events.js && node test/popup-menu.js && node test/surface-blit.js && node test/surface-faded.js && node test/surface-colorspace.js && node test/video-surface.js && node test/player.js && node test/round-rect-xy.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-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/activation-policy.js"
46
+ "test": "node test/app-lifecycle.js && node test/animation.js && node test/layer-transform.js && node test/accessibility-display.js && node test/ignores-mouse-events.js && node test/popup-menu.js && node test/surface-blit.js && node test/surface-faded.js && node test/image-smoothing.js && node test/surface-colorspace.js && node test/video-surface.js && node test/player.js && node test/round-rect-xy.js && node test/clip-rule.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-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/activation-policy.js"
47
47
  },
48
48
  "dependencies": {
49
49
  "node-addon-api": "^8.3.0"
Binary file
package/src/backend.mm CHANGED
@@ -4192,6 +4192,24 @@ static Napi::Value CtxSetGlobalAlpha(const Napi::CallbackInfo& info) {
4192
4192
  info[1].As<Napi::Number>().DoubleValue());
4193
4193
  return info.Env().Undefined();
4194
4194
  }
4195
+ // ctxSetImageSmoothing(surface, quality) — how an image drawn scaled or
4196
+ // turned is resampled: 'none' (the nearest pixel), 'low', 'medium' or
4197
+ // 'high', the 2D canvas's imageSmoothingEnabled and imageSmoothingQuality.
4198
+ // Part of the graphics state, so ctxSave/ctxRestore scope it. Every
4199
+ // context this bridge makes starts at 'medium', which resamples the whole
4200
+ // source image for a draw through a matrix whatever the clip: 784 tiles of
4201
+ // a 1400x1120 surface, each drawn under its own clip and matrix, took 214ms
4202
+ // on an M1 Pro, and 29.6ms at 'low', which is bilinear — what a layer in
4203
+ // perspective is drawn with.
4204
+ static Napi::Value CtxSetImageSmoothing(const Napi::CallbackInfo& info) {
4205
+ std::string q = info[1].As<Napi::String>().Utf8Value();
4206
+ CGContextSetInterpolationQuality(CtxOf(info[0]),
4207
+ q == "none" ? kCGInterpolationNone
4208
+ : q == "low" ? kCGInterpolationLow
4209
+ : q == "high" ? kCGInterpolationHigh
4210
+ : kCGInterpolationMedium);
4211
+ return info.Env().Undefined();
4212
+ }
4195
4213
  static Napi::Value CtxSetLineCap(const Napi::CallbackInfo& info) {
4196
4214
  std::string cap = info[1].As<Napi::String>().Utf8Value();
4197
4215
  CGContextSetLineCap(CtxOf(info[0]),
@@ -4299,10 +4317,17 @@ static Napi::Value CtxStroke(const Napi::CallbackInfo& info) {
4299
4317
  KeepPathAround(s->ctx, ^{ CGContextStrokePath(s->ctx); });
4300
4318
  return info.Env().Undefined();
4301
4319
  }
4320
+ // ctxClip(surface, evenOdd): the flag ctxFill takes, so canvas's
4321
+ // clip(path, 'evenodd') — an SVG <clipPath> with clip-rule="evenodd" — cuts
4322
+ // a ring rather than the square around it. Absent is nonzero, as before.
4302
4323
  static Napi::Value CtxClip(const Napi::CallbackInfo& info) {
4303
4324
  CALSurface* s = SurfaceFrom(info[0]);
4304
4325
  if (!s) return info.Env().Undefined();
4305
- KeepPathAround(s->ctx, ^{ CGContextClip(s->ctx); });
4326
+ bool evenOdd = info.Length() > 1 && info[1].ToBoolean().Value();
4327
+ KeepPathAround(s->ctx, ^{
4328
+ if (evenOdd) CGContextEOClip(s->ctx);
4329
+ else CGContextClip(s->ctx);
4330
+ });
4306
4331
  return info.Env().Undefined();
4307
4332
  }
4308
4333
 
@@ -7786,6 +7811,7 @@ void InitBackend(Napi::Env env, Napi::Object exports) {
7786
7811
  BFN("ctxSetStrokeColor", CtxSetStrokeColor);
7787
7812
  BFN("ctxSetLineWidth", CtxSetLineWidth);
7788
7813
  BFN("ctxSetGlobalAlpha", CtxSetGlobalAlpha);
7814
+ BFN("ctxSetImageSmoothing", CtxSetImageSmoothing);
7789
7815
  BFN("ctxSetLineCap", CtxSetLineCap);
7790
7816
  BFN("ctxSetLineJoin", CtxSetLineJoin);
7791
7817
  BFN("ctxSetBlendMode", CtxSetBlendMode);