@windowkit/appkit 0.2.0 → 0.3.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@windowkit/appkit",
3
- "version": "0.2.0",
3
+ "version": "0.3.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",
Binary file
package/src/backend.mm CHANGED
@@ -1966,6 +1966,106 @@ static Napi::Value FontFallbackFor(const Napi::CallbackInfo& info) {
1966
1966
  });
1967
1967
  }
1968
1968
 
1969
+ // fontWithSize(font, size) -> font handle | null
1970
+ // The same face at another size (CTFontCreateCopyWithAttributes). A matched
1971
+ // family re-resolves per size through matchFont; a face that arrived by
1972
+ // substitution — fontFallbackFor's answer, a fontShapeText run's font — has
1973
+ // no family to re-match by, and asking the cascade again at the new size is
1974
+ // a different question with a possibly different answer. This is how such a
1975
+ // face answers metrics and advances at every size, as itself.
1976
+ static Napi::Value FontWithSize(const Napi::CallbackInfo& info) {
1977
+ Napi::Env env = info.Env();
1978
+ if (!BCheckFontArg(info, "fontWithSize")) return env.Undefined();
1979
+ double size = info[1].IsNumber() ? info[1].As<Napi::Number>().DoubleValue() : 0;
1980
+ if (!(size > 0)) {
1981
+ Napi::TypeError::New(env, "fontWithSize: expected a size > 0")
1982
+ .ThrowAsJavaScriptException();
1983
+ return env.Undefined();
1984
+ }
1985
+ CTFontRef sized = CTFontCreateCopyWithAttributes(BFontFrom(info[0]),
1986
+ (CGFloat)size, NULL, NULL);
1987
+ if (!sized) return env.Null();
1988
+ return Napi::External<void>::New(env, (void*)sized, [](Napi::Env, void* d) {
1989
+ CFRelease(d);
1990
+ });
1991
+ }
1992
+
1993
+ // fontShapeText(font, text)
1994
+ // -> { width, runs: [{ font: handle | null, glyphs: Uint16Array,
1995
+ // positions: Float64Array x0,y0,x1,y1,…,
1996
+ // advances: Float64Array }] }
1997
+ // One CTLine over `text` in this font, read back run by run: glyph ids,
1998
+ // each glyph's origin relative to the line origin in CoreText's text space
1999
+ // (y up), and its advance. A run's `font` is null when it is the font asked
2000
+ // for, and a new handle when CoreText substituted a face for characters
2001
+ // this one lacks — the ids in that run are the substitute's, and drawing
2002
+ // them with the base font would draw its glyphs at those indices instead.
2003
+ // Runs come in visual order, so a right-to-left cluster reads back left to
2004
+ // right. The typesetter's whole answer for a cluster — a base with its
2005
+ // marks positioned, an emoji sequence joined, a variation selector honoured
2006
+ // — as ids a caller can hand to ctxDrawGlyphs beside the ids it looked up
2007
+ // itself; nothing here decides where a cluster goes.
2008
+ static Napi::Value FontShapeText(const Napi::CallbackInfo& info) {
2009
+ Napi::Env env = info.Env();
2010
+ if (!BCheckFontArg(info, "fontShapeText")) return env.Undefined();
2011
+ CTFontRef font = BFontFrom(info[0]);
2012
+ NSString* text = info[1].IsString() ? BToNSString(info[1]) : @"";
2013
+ Napi::Object out = Napi::Object::New(env);
2014
+ Napi::Array runsOut = Napi::Array::New(env);
2015
+ double width = 0;
2016
+ if (text.length > 0) {
2017
+ NSDictionary* attrs =
2018
+ @{(__bridge id)kCTFontAttributeName : (__bridge id)font};
2019
+ NSAttributedString* as =
2020
+ [[NSAttributedString alloc] initWithString:text attributes:attrs];
2021
+ CTLineRef line =
2022
+ CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)as);
2023
+ width = CTLineGetTypographicBounds(line, NULL, NULL, NULL);
2024
+ CFArrayRef runs = CTLineGetGlyphRuns(line);
2025
+ uint32_t written = 0;
2026
+ for (CFIndex ri = 0; ri < CFArrayGetCount(runs); ri++) {
2027
+ CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runs, ri);
2028
+ CFIndex count = CTRunGetGlyphCount(run);
2029
+ if (count <= 0) continue;
2030
+ std::vector<CGGlyph> glyphs((size_t)count);
2031
+ std::vector<CGPoint> positions((size_t)count);
2032
+ std::vector<CGSize> advances((size_t)count);
2033
+ CTRunGetGlyphs(run, CFRangeMake(0, 0), glyphs.data());
2034
+ CTRunGetPositions(run, CFRangeMake(0, 0), positions.data());
2035
+ CTRunGetAdvances(run, CFRangeMake(0, 0), advances.data());
2036
+ CFDictionaryRef rattrs = CTRunGetAttributes(run);
2037
+ CTFontRef rfont =
2038
+ (CTFontRef)CFDictionaryGetValue(rattrs, kCTFontAttributeName);
2039
+ Napi::Object ro = Napi::Object::New(env);
2040
+ if (rfont && !CFEqual(rfont, font)) {
2041
+ CFRetain(rfont);
2042
+ ro.Set("font", Napi::External<void>::New(
2043
+ env, (void*)rfont,
2044
+ [](Napi::Env, void* d) { CFRelease(d); }));
2045
+ } else {
2046
+ ro.Set("font", env.Null());
2047
+ }
2048
+ Napi::Uint16Array g = Napi::Uint16Array::New(env, (size_t)count);
2049
+ Napi::Float64Array p = Napi::Float64Array::New(env, (size_t)count * 2);
2050
+ Napi::Float64Array a = Napi::Float64Array::New(env, (size_t)count);
2051
+ for (size_t i = 0; i < (size_t)count; i++) {
2052
+ g[i] = glyphs[i];
2053
+ p[i * 2] = positions[i].x;
2054
+ p[i * 2 + 1] = positions[i].y;
2055
+ a[i] = advances[i].width;
2056
+ }
2057
+ ro.Set("glyphs", g);
2058
+ ro.Set("positions", p);
2059
+ ro.Set("advances", a);
2060
+ runsOut.Set(written++, ro);
2061
+ }
2062
+ CFRelease(line);
2063
+ }
2064
+ out.Set("width", width);
2065
+ out.Set("runs", runsOut);
2066
+ return out;
2067
+ }
2068
+
1969
2069
  // --- direct font handles (custom faces that bypass registry matching) -----
1970
2070
 
1971
2071
  static double CssWeightOfCTFont(CTFontRef ct) {
@@ -2790,6 +2890,8 @@ void InitBackend(Napi::Env env, Napi::Object exports) {
2790
2890
  BFN("fontGlyphForCodepoint", FontGlyphForCodepoint);
2791
2891
  BFN("fontGlyphAdvances", FontGlyphAdvances);
2792
2892
  BFN("fontFallbackFor", FontFallbackFor);
2893
+ BFN("fontWithSize", FontWithSize);
2894
+ BFN("fontShapeText", FontShapeText);
2793
2895
  BFN("fontFromData", FontFromData);
2794
2896
  BFN("cgFontWithSize", CgFontWithSize);
2795
2897
  BFN("fontByPostScriptName", FontByPostScriptName);