@geastack/apple 0.2.26 → 0.2.27

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
@@ -10,22 +10,31 @@ npm install @geastack/apple
10
10
 
11
11
  ## Building an app
12
12
 
13
- The `gea` CLI drives the targets:
13
+ The `gea` CLI drives the targets. It is a separate package: install
14
+ `@geastack/cli` in the app too (0.1.110 or later for tvOS), and declare each
15
+ target in the app's `package.json`, for example
16
+ `"gea": { "targets": { "macos": true, "ios": true, "tvos": true } }`. From the
17
+ app folder:
14
18
 
15
19
  ```sh
16
- gea build --target macos
17
- gea build --target ios
18
- gea build --target tvos
20
+ npm install --save-dev @geastack/cli
21
+ npx gea build --target macos
22
+ npx gea build --target ios
23
+ npx gea build --target tvos
19
24
  ```
20
25
 
21
26
  The target build scripts can also be run directly from an app directory that
22
- has the GeaStack packages installed:
27
+ has the GeaStack packages (including `@geastack/cli`) installed. Run this way
28
+ they do not receive the app's `gea.targets.<target>.build` configuration, and
29
+ `build-ios.sh` boots, installs and launches the app on a simulator or device
30
+ after building, like `gea run`; prefix `GEA_IOS_SKIP_LAUNCH=1` to stop after
31
+ the build, which is what `gea build` does:
23
32
 
24
33
  ```sh
25
34
  node_modules/@geastack/apple/targets/macos/build-macos.sh my-app
26
- node_modules/@geastack/apple/targets/ios/build-ios.sh my-app simulator
35
+ GEA_IOS_SKIP_LAUNCH=1 node_modules/@geastack/apple/targets/ios/build-ios.sh my-app simulator
27
36
  GEA_IOS_DEVELOPMENT_TEAM=ABCDE12345 node_modules/@geastack/apple/targets/ios/build-ios.sh my-app device
28
- node_modules/@geastack/apple/targets/ios/build-ios.sh my-app simulator tvos
37
+ GEA_IOS_SKIP_LAUNCH=1 node_modules/@geastack/apple/targets/ios/build-ios.sh my-app simulator tvos
29
38
  ```
30
39
 
31
40
  Requires Xcode with the macOS, iOS and tvOS SDKs you target. Each target directory has its own
@@ -138,7 +147,8 @@ string on success. A located occurrence must lie within a week of its
138
147
  `occurrenceDate`.
139
148
 
140
149
  macOS ends a process that asks for calendar access without a usage
141
- description. Add both keys to `apps/<id>/macos.json`; its optional `infoPlist`
150
+ description. Add both keys to `macos.json` in the app folder (next to its
151
+ `package.json`); its optional `infoPlist`
142
152
  object is merged over the shared `Info.plist` template:
143
153
 
144
154
  ```json
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geastack/apple",
3
- "version": "0.2.26",
3
+ "version": "0.2.27",
4
4
  "description": "Generated Apple SDK bindings plus the macOS, iOS and tvOS native build targets for Gea apps.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -117,7 +117,7 @@
117
117
  "dependencies": {
118
118
  "@geastack/compiler": "^1.0.34",
119
119
  "@geastack/core": "^0.1.52",
120
- "@geastack/elements": "^0.1.6",
120
+ "@geastack/elements": "^0.1.7",
121
121
  "@geastack/engine": "^0.1.23",
122
122
  "@geastack/geaos": "^0.1.3",
123
123
  "@geastack/geatsc-plugin-apple-native": "^0.1.4",
@@ -1,9 +1,18 @@
1
1
  # iOS target
2
2
 
3
- Build a gea-embedded JSX app as a native UIKit app.
3
+ Build a Gea JSX app as a native UIKit app.
4
4
 
5
- From an app that depends on `@geastack/apple` (every iOS example does, and
6
- `gea create --targets ios` adds it), the CLI drives this target:
5
+ The app needs `@geastack/apple` and `@geastack/cli` (0.1.110 or later for tvOS)
6
+ in its own dependencies and `"ios": true` under `gea.targets` in its
7
+ `package.json`; `npx create-geastack my-app --targets ios` sets up all three,
8
+ and for an existing app:
9
+
10
+ ```bash
11
+ npm install @geastack/apple
12
+ npm install --save-dev @geastack/cli
13
+ ```
14
+
15
+ From the app folder, the CLI drives this target:
7
16
 
8
17
  ```bash
9
18
  npx gea build --target ios # simulator .app, no launch
@@ -12,11 +21,15 @@ npx gea run --target ios # build, install and launch on a simu
12
21
  npx gea run --target ios --mode device # build, sign, install and launch on an iPhone
13
22
  ```
14
23
 
15
- The same script can be run directly from the app folder; the CLI passes the
16
- app id and destination through unchanged:
24
+ The CLI runs `build-ios.sh <app-id> <simulator|device> [tvos]` from the
25
+ installed package, with `GEA_IOS_SKIP_LAUNCH=1` for `gea build` and the app's
26
+ build configuration from `gea.targets.ios`. The script can also be run
27
+ directly from the app folder; it then installs and launches the app after
28
+ building unless `GEA_IOS_SKIP_LAUNCH=1` is set, and does not apply the
29
+ `gea.targets.ios.build` settings:
17
30
 
18
31
  ```bash
19
- node_modules/@geastack/apple/targets/ios/build-ios.sh <app-id> simulator
32
+ GEA_IOS_SKIP_LAUNCH=1 node_modules/@geastack/apple/targets/ios/build-ios.sh <app-id> simulator
20
33
  ```
21
34
 
22
35
  The script resolves `@geastack/core`, `@geastack/compiler`, the apple-native
@@ -39,7 +52,7 @@ GEA_IOS_DEVELOPMENT_TEAM=ABCDE12345 npx gea run --target ios --mode device
39
52
  To see the automatically detected team id:
40
53
 
41
54
  ```bash
42
- targets/ios/detect-development-team.mjs
55
+ node node_modules/@geastack/apple/targets/ios/detect-development-team.mjs
43
56
  ```
44
57
 
45
58
  Signed device builds auto-pick an attached iPhone with Developer Mode enabled,
@@ -63,18 +76,20 @@ the source of truth.
63
76
  ## tvOS
64
77
 
65
78
  Apps that declare `gea.targets.tvos` can use the shared UIKit host on tvOS 17
66
- or later. Install the tvOS platform in Xcode's Components settings. From the
67
- Geastack examples project, run the JSX demo on an Apple TV simulator:
79
+ or later. Install the tvOS platform in Xcode's Components settings. The app
80
+ needs the same `@geastack/apple` and `@geastack/cli` dependencies as on iOS
81
+ (the `three-tvos` example lists both). From the app folder, run it on an Apple
82
+ TV simulator:
68
83
 
69
84
  ```sh
70
- gea run --target tvos --app bouncing-balls-jsx
85
+ npx gea run --target tvos
71
86
  ```
72
87
 
73
88
  For a physical Apple TV, pair it with Xcode over the local network and use an
74
89
  Apple development team configured for the device:
75
90
 
76
91
  ```sh
77
- GEA_IOS_DEVELOPMENT_TEAM=ABCDE12345 gea run --target tvos --mode device --app bouncing-balls-jsx
92
+ GEA_IOS_DEVELOPMENT_TEAM=ABCDE12345 npx gea run --target tvos --mode device
78
93
  ```
79
94
 
80
95
  Use `gea build` with the same options to generate the Xcode project and build
@@ -1,6 +1,6 @@
1
1
  # macOS target
2
2
 
3
- Build a gea-embedded app as a native Cocoa Mac app — `<Button>` becomes
3
+ Build a Gea app as a native Cocoa Mac app — `<Button>` becomes
4
4
  `NSButton`, `<Text>` becomes `NSTextField`, `<View>` becomes `NSView`,
5
5
  `<Image>` becomes `NSImageView`, `<canvas>` is a custom `NSView` backed by
6
6
  CoreGraphics, `<virtual-list>` is `NSScrollView`. The flex layout engine
@@ -8,8 +8,20 @@ in `@geastack/engine`'s `ui/layout.cpp` computes frames; AppKit draws.
8
8
 
9
9
  ## Build
10
10
 
11
- targets/macos/build-macos.sh <app-id>
12
- open dist/macos/<app-id>/<AppName>.app
11
+ The app needs `@geastack/apple` and `@geastack/cli` in its dependencies and
12
+ `"macos": true` under `gea.targets` in its `package.json`. From the app folder:
13
+
14
+ npx gea run --target macos
15
+
16
+ builds the app and opens it. `npx gea build --target macos` builds only; the
17
+ bundle is `dist/macos/<app-id>/<AppName>.app`. `gea run --target macos` needs
18
+ `@geastack/cli` 0.1.117 or later; with an older CLI, build and then
19
+ `open dist/macos/<app-id>/<AppName>.app`.
20
+
21
+ `gea build --target macos` runs this package's `build-macos.sh <app-id>` with
22
+ the app's build configuration. The script can also be run directly from the
23
+ app folder as `node_modules/@geastack/apple/targets/macos/build-macos.sh
24
+ <app-id>`.
13
25
 
14
26
  ## Where the build writes
15
27
 
@@ -24,49 +36,22 @@ Output does not go under the package because the package lives in the app's
24
36
  `node_modules`, which npm replaces on reinstall. Set `GEA_MACOS_OUTPUT_DIR` to
25
37
  put the whole tree elsewhere.
26
38
 
27
- `<app-id>` is currently any name; when no generated app is present under
28
- `dist/macos/.generated/<app-id>/` the build falls back to a built-in
29
- Phase B smoke app (`macos_smoke_app.cpp`) that exercises View / Text /
30
- Button / event dispatch. geatsc-compiled apps go into
31
- `dist/macos/.generated/` and supersede the smoke automatically — the
32
- JSX → C++ pipeline for the macOS target is a follow-up.
33
-
34
- ## Hard-muted builds must prove artifact provenance
35
-
36
- For any verification that must not produce audio, configure the final native
37
- mute source and its literal on the build itself:
38
-
39
- ```sh
40
- GEA_MACOS_HARD_MUTE_SOURCE=/absolute/path/to/the/committed/native/audio_source.mm \
41
- GEA_MACOS_HARD_MUTE_GATE=THE_NATIVE_MUTE_ENVIRONMENT_LITERAL \
42
- targets/macos/build-macos.sh <app-id>
43
- ```
44
-
45
- Both variables are required together. After code signing, the build writes
46
- `dist/<app-id>/build/hard-mute-artifact-provenance.json` and statically proves:
47
-
48
- - the selected source is the committed blob at the recorded dependency revision;
49
- - exactly one linked object depends on that source and contains the gate;
50
- - `link.sig` records that exact object;
51
- - source, translation unit, depfile, link signature, object, and executable
52
- SHA-256 values still match; and
53
- - the signed executable contains the same gate.
54
-
55
- Immediately before launch, rerun the no-launch check against the exact binary:
56
-
57
- ```sh
58
- node targets/macos/preflight-hard-muted-artifact.mjs verify \
59
- --manifest /absolute/path/to/build/hard-mute-artifact-provenance.json \
60
- --executable "/absolute/path/to/App.app/Contents/MacOS/App" \
61
- --source /absolute/path/to/the/committed/native/audio_source.mm \
62
- --gate THE_NATIVE_MUTE_ENVIRONMENT_LITERAL
63
- ```
64
-
65
- Do not launch unless this prints
66
- `PASS artifact-static-hard-mute-gate=present`. The checker reads files and Git
67
- metadata only; it does not execute the app or open an audio device. Any missing
68
- manifest, gate, changed dependency revision, or changed source/object/executable
69
- fails closed.
39
+ The build compiles the app's entry through geatsc into
40
+ `dist/macos/.generated/<app-id>/` and links the generated C++ into the bundle;
41
+ a geatsc failure for a resolved app fails the build. Only when `<app-id>`
42
+ resolves to no app with an entry file does the build fall back to the built-in
43
+ smoke app (`main/macos_smoke_app.cpp`), which exercises View / Text / Button /
44
+ event dispatch.
45
+
46
+ ## Hard-muted builds
47
+
48
+ Setting `GEA_MACOS_HARD_MUTE_SOURCE` (absolute path to the committed native
49
+ audio source) and `GEA_MACOS_HARD_MUTE_GATE` (the mute literal it contains)
50
+ together makes the build record and check that the signed executable was built
51
+ from that source with the gate present. It writes
52
+ `dist/macos/<app-id>/build/hard-mute-artifact-provenance.json`;
53
+ `preflight-hard-muted-artifact.mjs verify` re-checks a binary against it
54
+ without launching it. Setting only one of the two variables is an error.
70
55
 
71
56
  ## Architecture
72
57
 
@@ -93,11 +78,17 @@ display list, so other targets keep calling `refresh()` unchanged.
93
78
 
94
79
  ## Frame loop
95
80
 
96
- `macos_main.mm` schedules a 60Hz `NSTimer` on `NSRunLoopCommonModes` so
97
- the loop keeps firing during live window-resize and other event-
98
- tracking modes. Each tick:
99
-
100
- 1. `gea_embedded_app_frame(timestampMs)` lets the app update state
81
+ `main/macos_main.mm` drives frames from the window's `CADisplayLink` on
82
+ macOS 14 and later, at the screen's maximum refresh rate (following the window
83
+ between displays). On older macOS, or when the app's window config sets
84
+ `"vsync": false`, it uses a fast `NSTimer` instead; with `vsync: false` frames
85
+ run uncapped. Both are scheduled on `NSRunLoopCommonModes` so the loop keeps
86
+ firing during live window-resize and other event-tracking modes. The window
87
+ config is `macos.json` in the app folder overlaid with
88
+ `gea.targets.macos.window` from its `package.json` (`width`, `height`,
89
+ `vsync`, `fullscreen`, ...), bundled as `Resources/window.json`. Each tick:
90
+
91
+ 1. `Application::frame(timestampMs)` lets the app update state
101
92
  2. Force `style.width/height` on the mounted root to the current window
102
93
  content size — this is the macOS convention that makes the root
103
94
  viewport track resize automatically
@@ -124,8 +115,15 @@ runs the measurement harness (offline render, RMS + measured frequency);
124
115
  `GEA_AUDIO_CAPTURE_RAW=<path>` captures an app run for
125
116
  `targets/shared/analyze-audio-capture.mjs`.
126
117
 
127
- Stubbed for v1: WiFi (`macos_wifi`), BLE (`macos_ble`), IMU (`macos_imu`)
128
- — present only as link-satisfying no-ops; apps that depend on these will
129
- run but those calls return inert values. Custom font bundling, code
130
- signing with entitlements, notarisation, and the JSX → C++ pipeline for
131
- this target are follow-ups.
118
+ Also implemented in `main/`: HTTP(S) `fetch` over `NSURLSession`
119
+ (`macos_network.mm`), battery level from IOKit power sources and pointer
120
+ input as touch (`macos_sensors.cpp`), timers (`macos_timers.mm`), launching
121
+ other apps' bundles (`macos_app_platform.mm`), and the `runDeviceCommand`
122
+ shell escape hatch (`macos_device_control.mm`).
123
+
124
+ Stubbed in `main/macos_sensors.cpp`: the accelerometer/gyroscope (tilt 0,
125
+ gravity on Z) and the embedded memory diagnostics (all zeros).
126
+ `main/macos_display.mm` keeps the framebuffer display inert because AppKit
127
+ draws. `main/` has no WiFi or Bluetooth backend. Code signing with
128
+ entitlements and notarisation are not done by the build; development bundles
129
+ are signed ad hoc.
@@ -396,7 +396,7 @@ resolve_app_dir() {
396
396
  }
397
397
 
398
398
  # Info.plist comes from the shared template plus the optional `infoPlist`
399
- # object of apps/<id>/macos.json (usage descriptions and other per-app keys).
399
+ # object of the app's macos.json (usage descriptions and other per-app keys).
400
400
  INFO_PLIST_TMP="$BUILD_DIR/Info.plist.tmp.$$"
401
401
  sed -e "s/@APP_EXEC@/$APP_EXEC/g" \
402
402
  -e "s/@APP_ID@/$APP_ID/g" \
@@ -994,6 +994,7 @@ case "${GEA_MACOS_THERMALRIGHT_PRODUCT_ID:-}" in
994
994
  esac
995
995
  if [[ "$THERMALRIGHT_DISPLAY_TARGET" == "0" ]]; then
996
996
  CXX_DEFINES+=(-DGEA_EMBEDDED_PIXEL_FORMAT=1 -DGEA_CSS_FONT_STRETCH=1) # RGBA8888 and native variable-font style for AppKit
997
+ CXX_DEFINES+=(-DGEA_EMBEDDED_HAS_GENERATED_FONTS=1) # Enable retained text rasterization via the runtime TTF hooks
997
998
  fi
998
999
  if [[ "$THERMALRIGHT_DISPLAY_TARGET" == "1" ]]; then
999
1000
  THERMALRIGHT_WIDTH="${GEA_MACOS_THERMALRIGHT_WIDTH:-$THERMALRIGHT_DEFAULT_WIDTH}"
@@ -1124,6 +1125,8 @@ if [[ -n "$SINGLE_APP_DIR" ]]; then
1124
1125
  *) full_src="$SINGLE_APP_DIR/$native_src" ;;
1125
1126
  esac
1126
1127
  [[ -f "$full_src" ]] || continue
1128
+ # Generated host calls can include headers beside their native source.
1129
+ INCLUDES+=(-iquote "${full_src%/*}")
1127
1130
  case "$full_src" in
1128
1131
  *.c) C_SOURCES+=("$full_src") ;;
1129
1132
  *.m|*.mm) MM_SOURCES+=("$full_src") ;;
@@ -2047,8 +2050,8 @@ timing_mark link
2047
2050
 
2048
2051
  # Convert the app icon only when its resolved source, manifest metadata, or the
2049
2052
  # icon tooling changed. Ten `sips` subprocesses made this the largest historical
2050
- # no-op phase.
2051
- if [[ -n "$APP_META" ]]; then
2053
+ # no-op phase. A prerender bundle is run directly and needs no Finder icon.
2054
+ if [[ -n "$APP_META" && "${GEA_MACOS_PRERENDER:-0}" != "1" ]]; then
2052
2055
  ICON_OUTPUT="$RES_DIR/AppIcon.icns"
2053
2056
  ICON_INPUT_SIGNATURE_FILE="$BUILD_DIR/AppIcon.inputs"
2054
2057
  ICON_INPUT_SIGNATURE="$(node "$GEA_CLI" apps apple-icons "$APP_ID" --platform macos --signature)"
@@ -2072,7 +2075,7 @@ fi
2072
2075
  # Copy any TTF / OTF referenced by @font-face in CSS — for resident-app
2073
2076
  # builds we walk every resident's app dir so fonts used by other tiles
2074
2077
  # are also bundled.
2075
- if [[ "$MACOS_PIPELINE_CACHE" == "0" ]]; then
2078
+ if [[ "$MACOS_PIPELINE_CACHE" == "0" && "${GEA_MACOS_PRERENDER:-0}" != "1" ]]; then
2076
2079
  FONTS_OUT="$RES_DIR/Fonts"
2077
2080
  mkdir -p "$FONTS_OUT"
2078
2081
  APP_DIRS_FOR_FONTS=()
@@ -2128,7 +2131,7 @@ done
2128
2131
 
2129
2132
  # Per-app window config — the macOS main reads window.json out of Resources/
2130
2133
  # at launch to pick up size, transparent title bar, dark appearance, etc.
2131
- # Source is apps/<id>/macos.json overlaid with gea.targets.macos.window from
2134
+ # Source is the app's macos.json overlaid with gea.targets.macos.window from
2132
2135
  # the app's package.json (window-config.mjs), written as window.json so the runtime path is stable
2133
2136
  # regardless of which app id produced it.
2134
2137
  APP_DIR_FOR_CONFIG="$(resolve_app_dir "$APP_ID")"
@@ -25,6 +25,9 @@ NSFont *fontForId(int fontId, int sizePx, int weight, int stretch);
25
25
  // after the app bundle is known.
26
26
  int registerCustomTtfDirectory(NSString *directoryPath);
27
27
 
28
+ // Match software glyph layout and paint during offscreen capture.
29
+ void useSoftwareTextMeasurement();
30
+
28
31
  // Picks the right NSLineBreakMode for `text` rendered in `font` within
29
32
  // `maxWidth` points. Returns word-wrap when every space-separated chunk fits
30
33
  // on a line by itself, otherwise char-wrap so that a long unbreakable run
@@ -9,6 +9,7 @@
9
9
  #include <cmath>
10
10
  #include <cstdint>
11
11
  #include <cstring>
12
+ #include <memory>
12
13
  #include <mutex>
13
14
  #include <string>
14
15
  #include <unordered_map>
@@ -74,7 +75,8 @@ NSString *familyForId(int id)
74
75
  // initializer-ordering reason as FamilyRegistry above.
75
76
  struct TtfByteRegistry {
76
77
  std::mutex lock;
77
- std::unordered_map<std::string, std::vector<std::uint8_t>> byFamily;
78
+ std::unordered_map<std::string, std::shared_ptr<const std::vector<std::uint8_t>>> byFamily;
79
+ std::unordered_map<std::string, std::shared_ptr<const std::vector<std::uint8_t>>> byAlias;
78
80
  };
79
81
 
80
82
  TtfByteRegistry &ttfByteRegistry()
@@ -90,7 +92,9 @@ std::string lowercased(const std::string &value)
90
92
  return out;
91
93
  }
92
94
 
93
- void registerTtfBytesForFamily(NSString *path)
95
+ bool softwareTextMeasurement = false;
96
+
97
+ void registerTtfBytesForFamily(NSString *path, NSArray<NSString *> *aliases)
94
98
  {
95
99
  NSData *data = [NSData dataWithContentsOfFile:path];
96
100
  if (data.length == 0) return;
@@ -108,7 +112,9 @@ void registerTtfBytesForFamily(NSString *path)
108
112
  std::scoped_lock guard(registry.lock);
109
113
  // First face wins (e.g. Inter-Regular before other weights of the family):
110
114
  // the runtime rasterizer keys on the family alone.
111
- registry.byFamily.emplace(lowercased(buf), std::vector<std::uint8_t>(bytes, bytes + data.length));
115
+ auto font = std::make_shared<const std::vector<std::uint8_t>>(bytes, bytes + data.length);
116
+ registry.byFamily.emplace(lowercased(buf), font);
117
+ for (NSString *alias in aliases) registry.byAlias.emplace(alias.UTF8String, font);
112
118
  }
113
119
 
114
120
  } // namespace
@@ -122,10 +128,14 @@ const std::uint8_t *runtimeTtfBytesForFamilyId(int familyId, unsigned long *leng
122
128
  if (!family || family.length == 0) return nullptr;
123
129
  TtfByteRegistry &registry = ttfByteRegistry();
124
130
  std::scoped_lock guard(registry.lock);
125
- const auto it = registry.byFamily.find(lowercased(family.UTF8String));
126
- if (it == registry.byFamily.end()) return nullptr;
127
- if (length) *length = it->second.size();
128
- return it->second.data();
131
+ const auto key = lowercased(family.UTF8String);
132
+ const auto alias = registry.byAlias.find(key);
133
+ const auto fallback = registry.byFamily.find(key);
134
+ const auto *font = alias != registry.byAlias.end() ? alias->second.get()
135
+ : fallback != registry.byFamily.end() ? fallback->second.get() : nullptr;
136
+ if (!font) return nullptr;
137
+ if (length) *length = font->size();
138
+ return font->data();
129
139
  }
130
140
 
131
141
  static NSFont *self_resolveFont(int fontId, int sizePx, int weight, int stretch)
@@ -186,18 +196,35 @@ NSFont *fontForId(int fontId, int sizePx, int weight, int stretch)
186
196
 
187
197
  NSFont *fontForId(int fontId, int sizePx) { return fontForId(fontId, sizePx, 400, 1000); }
188
198
 
199
+ void useSoftwareTextMeasurement()
200
+ {
201
+ softwareTextMeasurement = true;
202
+ }
203
+
189
204
  int registerCustomTtfDirectory(NSString *directoryPath)
190
205
  {
191
206
  NSFileManager *fm = [NSFileManager defaultManager];
192
207
  BOOL isDir = NO;
193
208
  if (![fm fileExistsAtPath:directoryPath isDirectory:&isDir] || !isDir) return 0;
194
209
  NSArray<NSString *> *entries = [fm contentsOfDirectoryAtPath:directoryPath error:nil];
210
+ NSString *aliasPath = [directoryPath.stringByDeletingLastPathComponent stringByAppendingPathComponent:@"font-aliases.json"];
211
+ NSData *aliasData = [NSData dataWithContentsOfFile:aliasPath];
212
+ NSDictionary *aliases = aliasData ? [NSJSONSerialization JSONObjectWithData:aliasData options:0 error:nil] : nil;
213
+ NSMutableDictionary<NSString *, NSMutableArray<NSString *> *> *aliasesByFile = [NSMutableDictionary dictionary];
214
+ if ([aliases isKindOfClass:NSDictionary.class]) {
215
+ for (NSString *alias in aliases) {
216
+ NSString *filename = aliases[alias];
217
+ if (![filename isKindOfClass:NSString.class]) continue;
218
+ if (!aliasesByFile[filename]) aliasesByFile[filename] = [NSMutableArray array];
219
+ [aliasesByFile[filename] addObject:alias];
220
+ }
221
+ }
195
222
  int count = 0;
196
223
  for (NSString *name in entries) {
197
224
  NSString *lower = name.lowercaseString;
198
225
  if (![lower hasSuffix:@".ttf"] && ![lower hasSuffix:@".otf"]) continue;
199
226
  NSString *path = [directoryPath stringByAppendingPathComponent:name];
200
- registerTtfBytesForFamily(path);
227
+ registerTtfBytesForFamily(path, aliasesByFile[name]);
201
228
  CFErrorRef err = NULL;
202
229
  CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:path];
203
230
  if (CTFontManagerRegisterFontsForURL(url, kCTFontManagerScopeProcess, &err)) {
@@ -279,7 +306,7 @@ static bool measureNativeText(const char *text,
279
306
  int *outWidth,
280
307
  int *outHeight)
281
308
  {
282
- if (!outWidth || !outHeight) return false;
309
+ if (gea::macos::softwareTextMeasurement || !outWidth || !outHeight) return false;
283
310
  if (!text || !text[0]) {
284
311
  *outWidth = 0;
285
312
  *outHeight = 0;
@@ -1,6 +1,7 @@
1
1
  #import <Cocoa/Cocoa.h>
2
2
 
3
3
  #include "display.h"
4
+ #include "macos_prerender.h"
4
5
  #include "canvas.h"
5
6
  #include "host/display_orientation.h"
6
7
  #include "pixel.h"
@@ -29,6 +30,7 @@ int g_canvas_w = gea::platform::display::kWidth;
29
30
  int g_canvas_h = gea::platform::display::kHeight;
30
31
  int g_brightness = 100;
31
32
  std::uint8_t g_alpha = 255;
33
+ bool g_prerender = false;
32
34
  int g_flush_rows = 0;
33
35
  int g_flush_depth = 0;
34
36
  int g_flush_calls = 0;
@@ -43,13 +45,13 @@ void ensure_canvas()
43
45
 
44
46
  bool framebuffer_target()
45
47
  {
46
- return gea::macos::thermalright::enabled();
48
+ return g_prerender || gea::macos::thermalright::enabled();
47
49
  }
48
50
 
49
51
  void count_rect(int x0, int y0, int x1, int y1)
50
52
  {
51
- using gea::platform::display::kWidth;
52
- using gea::platform::display::kHeight;
53
+ const int kWidth = g_canvas_w;
54
+ const int kHeight = g_canvas_h;
53
55
  if (x0 < 0) x0 = 0;
54
56
  if (y0 < 0) y0 = 0;
55
57
  if (x1 >= kWidth) x1 = kWidth - 1;
@@ -60,7 +62,7 @@ void count_rect(int x0, int y0, int x1, int y1)
60
62
 
61
63
  void submit_full_frame()
62
64
  {
63
- if (!framebuffer_target()) return;
65
+ if (g_prerender || !framebuffer_target()) return;
64
66
  ensure_canvas();
65
67
  #if GEA_MACOS_THERMALRIGHT_DISPLAY_TARGET
66
68
  gea::macos::thermalright::submitRgb565(g_framebuffer,
@@ -71,6 +73,37 @@ void submit_full_frame()
71
73
 
72
74
  } // namespace
73
75
 
76
+ bool gea_macos_prepare_prerender(int width, int height)
77
+ {
78
+ g_canvas_w = width;
79
+ g_canvas_h = height;
80
+ g_prerender = true;
81
+ ensure_canvas();
82
+ return g_framebuffer != nullptr;
83
+ }
84
+
85
+ bool gea_macos_write_prerender_png(NSString *path)
86
+ {
87
+ NSBitmapImageRep *image = [[NSBitmapImageRep alloc]
88
+ initWithBitmapDataPlanes:nullptr pixelsWide:g_canvas_w pixelsHigh:g_canvas_h
89
+ bitsPerSample:8 samplesPerPixel:3 hasAlpha:NO isPlanar:NO
90
+ colorSpaceName:NSDeviceRGBColorSpace bytesPerRow:0 bitsPerPixel:0];
91
+ if (!image) return false;
92
+ for (int y = 0; y < g_canvas_h; ++y) {
93
+ auto *row = image.bitmapData + y * image.bytesPerRow;
94
+ for (int x = 0; x < g_canvas_w; ++x) {
95
+ int r, g, b, a;
96
+ gea::framework::graphics::pixel::unpackNative8(
97
+ g_framebuffer[static_cast<size_t>(y) * g_canvas_w + x], &r, &g, &b, &a);
98
+ row[x * 3] = r;
99
+ row[x * 3 + 1] = g;
100
+ row[x * 3 + 2] = b;
101
+ }
102
+ }
103
+ NSData *png = [image representationUsingType:NSBitmapImageFileTypePNG properties:@{}];
104
+ return [png writeToFile:path atomically:YES];
105
+ }
106
+
74
107
  namespace gea::platform::display {
75
108
 
76
109
  bool Display::init() { ensure_canvas(); return true; }
@@ -129,8 +162,8 @@ void Display::flushRectsRasterized(const DisplayFlushRect *rects, int count, Dis
129
162
  for (int i = 0; i < count; i++) {
130
163
  int x0 = std::max(0, rects[i].x0);
131
164
  int y0 = std::max(0, rects[i].y0);
132
- int x1 = std::min(kWidth - 1, rects[i].x1);
133
- int y1 = std::min(kHeight - 1, rects[i].y1);
165
+ int x1 = std::min(g_canvas_w - 1, rects[i].x1);
166
+ int y1 = std::min(g_canvas_h - 1, rects[i].y1);
134
167
  if (x0 > x1 || y0 > y1) continue;
135
168
  const int w = x1 - x0 + 1;
136
169
  const int h = y1 - y0 + 1;
@@ -138,7 +171,7 @@ void Display::flushRectsRasterized(const DisplayFlushRect *rects, int count, Dis
138
171
  raster(pixels.data(), w, h, x0, y0, user);
139
172
  for (int row = 0; row < h; row++) {
140
173
  for (int col = 0; col < w; ++col)
141
- g_framebuffer[static_cast<std::size_t>(y0 + row) * kWidth + x0 + col] = pixels[static_cast<std::size_t>(row) * w + col];
174
+ g_framebuffer[static_cast<std::size_t>(y0 + row) * g_canvas_w + x0 + col] = pixels[static_cast<std::size_t>(row) * w + col];
142
175
  }
143
176
  g_canvas.markDirty(x0, y0, x1, y1);
144
177
  }
@@ -158,8 +191,8 @@ bool Display::streamRect(int x, int y, int w, int h, DisplayStreamRasterFn raste
158
191
  if (!raster || w <= 0 || h <= 0) return false;
159
192
  int x0 = std::max(0, x);
160
193
  int y0 = std::max(0, y);
161
- int x1 = std::min(kWidth - 1, x + w - 1);
162
- int y1 = std::min(kHeight - 1, y + h - 1);
194
+ int x1 = std::min(g_canvas_w - 1, x + w - 1);
195
+ int y1 = std::min(g_canvas_h - 1, y + h - 1);
163
196
  if (x0 > x1 || y0 > y1) return true;
164
197
 
165
198
  const int rw = x1 - x0 + 1;
@@ -168,7 +201,7 @@ bool Display::streamRect(int x, int y, int w, int h, DisplayStreamRasterFn raste
168
201
  raster(pixels.data(), rw, rh, x0, y0, user);
169
202
  for (int row = 0; row < rh; row++) {
170
203
  for (int col = 0; col < rw; ++col)
171
- g_framebuffer[static_cast<std::size_t>(y0 + row) * kWidth + x0 + col] = pixels[static_cast<std::size_t>(row) * rw + col];
204
+ g_framebuffer[static_cast<std::size_t>(y0 + row) * g_canvas_w + x0 + col] = pixels[static_cast<std::size_t>(row) * rw + col];
172
205
  }
173
206
  g_canvas.markDirty(x0, y0, x1, y1);
174
207
  flush();
@@ -305,7 +338,7 @@ bool Display::present(const DisplayPresentCommand *commands, int command_count)
305
338
  void Display::setFlushConfig(int chunk_rows, int queue_depth) { g_flush_rows = chunk_rows; g_flush_depth = queue_depth; }
306
339
  int Display::flushChunkRows() { return g_flush_rows; }
307
340
  int Display::flushQueueDepth() { return g_flush_depth; }
308
- int Display::flushBufferBytes() { return kWidth * g_flush_rows * 2 * g_flush_depth; }
341
+ int Display::flushBufferBytes() { return g_canvas_w * g_flush_rows * 2 * g_flush_depth; }
309
342
 
310
343
  void Display::pushClip(int x, int y, int w, int h) { if (framebuffer_target()) canvas()->pushClip(x, y, w, h); }
311
344
  void Display::popClip() { if (framebuffer_target()) canvas()->popClip(); }
@@ -12,6 +12,7 @@ extern void gea_macos_debugger_start();
12
12
 
13
13
  #include "font_registry.h"
14
14
  #include "macos_native_shell.h"
15
+ #include "macos_prerender.h"
15
16
  #include "macos_renderer.h"
16
17
  #include "macos_background_test.h"
17
18
  #include "press_bridge.h"
@@ -60,6 +61,7 @@ namespace gea::macos { void installWifiDriver(); int nodeIdForView(NSView *view)
60
61
 
61
62
  #include <algorithm>
62
63
  #include <chrono>
64
+ #include <cmath>
63
65
  #include <cstdint>
64
66
  #include <cstdio>
65
67
  #include <cstdlib>
@@ -1236,12 +1238,58 @@ void gea::apple::AppKit::setScrollDocumentTopAligned(gea::apple::AppKit::NSScrol
1236
1238
  }
1237
1239
  #endif
1238
1240
 
1241
+ static int prerender(int argc, const char *argv[])
1242
+ {
1243
+ if (argc != 8) return 2;
1244
+ const int width = std::atoi(argv[3]);
1245
+ const int height = std::atoi(argv[4]);
1246
+ const double dpr = std::atof(argv[5]);
1247
+ const int fps = std::atoi(argv[6]);
1248
+ const int targetMs = std::atoi(argv[7]);
1249
+ if (width < 1 || width > 8192 || height < 1 || height > 8192 ||
1250
+ !std::isfinite(dpr) || dpr < 0.1 || dpr > 8 ||
1251
+ fps < 1 || fps > 1000 || targetMs < 0) return 2;
1252
+ if (!gea_macos_prepare_prerender(width, height)) return 1;
1253
+
1254
+ NSString *fontsDir = [NSBundle.mainBundle.resourcePath stringByAppendingPathComponent:@"Fonts"];
1255
+ gea::macos::registerCustomTtfDirectory(fontsDir);
1256
+ gea::macos::useSoftwareTextMeasurement();
1257
+ gea::platform::storage::setMountProvider(+[]() { return true; });
1258
+
1259
+ gea_macos_set_prerender_time(0);
1260
+ // Seed the timer scheduler before app initialization schedules any timers.
1261
+ gea::host::runAnimationFrameCallbacks(0);
1262
+ gea::framework::app::Application::init(width, height, dpr);
1263
+ auto &tree = gea::embedded::ui::Tree::instance();
1264
+ if (tree.mountedRoot() < 0) {
1265
+ std::fputs("Gea prerender requires a mounted UI tree\n", stderr);
1266
+ return 1;
1267
+ }
1268
+ gea::css::DeclarativeAnimations::scanAndStart(0);
1269
+
1270
+ for (std::int64_t frame = 0; ; ++frame) {
1271
+ const auto tickMs = (frame * 1000 + fps / 2) / fps;
1272
+ const int timeMs = static_cast<int>(std::min<std::int64_t>(tickMs, targetMs));
1273
+ @autoreleasepool {
1274
+ gea_macos_set_prerender_time(timeMs);
1275
+ gea::embedded::ui::StyleSheet::instance().startCssAnimations(timeMs);
1276
+ gea::css::AnimationEngine::instance().tick(static_cast<std::uint32_t>(timeMs));
1277
+ gea::framework::app::Application::frame(timeMs);
1278
+ }
1279
+ if (timeMs == targetMs) break;
1280
+ }
1281
+ if (!gea_macos_write_prerender_png([NSString stringWithUTF8String:argv[2]])) {
1282
+ std::fprintf(stderr, "Could not write PNG to %s\n", argv[2]);
1283
+ return 1;
1284
+ }
1285
+ return 0;
1286
+ }
1287
+
1239
1288
  int main(int argc, const char *argv[])
1240
1289
  {
1241
1290
  GeaNativeTerminationGuard terminationGuard;
1242
- (void)argc;
1243
- (void)argv;
1244
1291
  @autoreleasepool {
1292
+ if (argc > 1 && std::strcmp(argv[1], "--gea-prerender") == 0) return prerender(argc, argv);
1245
1293
  gea_macos_smoke_log("[gea-macos] main entered");
1246
1294
  const bool backgroundTest = gea::macos::background_test::enabled();
1247
1295
  NSApplication *app = [NSApplication sharedApplication];
@@ -0,0 +1,8 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #pragma once
3
+
4
+ @class NSString;
5
+
6
+ bool gea_macos_prepare_prerender(int width, int height);
7
+ bool gea_macos_write_prerender_png(NSString *path);
8
+ void gea_macos_set_prerender_time(int timestampMs);
@@ -1,12 +1,18 @@
1
1
  #import <Foundation/Foundation.h>
2
2
  #include "services/frame_scheduler.h"
3
+ #include "macos_prerender.h"
3
4
 
5
+ #include <atomic>
4
6
  #include <mach/mach_time.h>
5
7
 
6
8
  namespace {
7
9
 
10
+ std::atomic<int> prerenderMillis{-1};
11
+
8
12
  int monotonicMillis()
9
13
  {
14
+ const int captureTime = prerenderMillis.load(std::memory_order_relaxed);
15
+ if (captureTime >= 0) return captureTime;
10
16
  static mach_timebase_info_data_t tb = {0, 0};
11
17
  if (tb.denom == 0) mach_timebase_info(&tb);
12
18
  static const uint64_t start = mach_absolute_time();
@@ -18,6 +24,11 @@ int g_frame_interval_ms = gea::framework::services::FrameScheduler::kDefaultFram
18
24
 
19
25
  } // namespace
20
26
 
27
+ void gea_macos_set_prerender_time(int timestampMs)
28
+ {
29
+ prerenderMillis.store(timestampMs, std::memory_order_relaxed);
30
+ }
31
+
21
32
  extern "C" int gea_embedded_now_ms(void)
22
33
  {
23
34
  return monotonicMillis();
@@ -86,6 +86,7 @@ function walkCss(root, visit) {
86
86
  // not leave stale resources in the app bundle.
87
87
  const desiredFonts = new Map()
88
88
  const desiredFontLicenses = new Map()
89
+ const fontAliases = new Map()
89
90
  for (const root of [...appDirs, ...sharedCssDirs]) {
90
91
  walkCss(root, (cssPath) => {
91
92
  const css = fs.readFileSync(cssPath, 'utf8').replace(/\/\*[\s\S]*?\*\//g, '')
@@ -100,6 +101,11 @@ for (const root of [...appDirs, ...sharedCssDirs]) {
100
101
  const resolved = path.resolve(path.dirname(cssPath), clean)
101
102
  if (!fs.existsSync(resolved) || !fs.statSync(resolved).isFile()) continue
102
103
  const basename = path.basename(resolved)
104
+ const family = face[1].match(/(?:^|;)\s*font-family\s*:\s*(?:"([^"]+)"|'([^']+)'|([^;]+))/i)
105
+ const alias = (family?.[1] || family?.[2] || family?.[3] || '').trim().toLowerCase()
106
+ if (alias && (!desiredFonts.has(basename) || desiredFonts.get(basename) === resolved) && !fontAliases.has(alias)) {
107
+ fontAliases.set(alias, basename)
108
+ }
103
109
  if (!desiredFonts.has(basename)) {
104
110
  desiredFonts.set(basename, resolved)
105
111
  // Preserve licenses alongside packaged font bytes, using a font-specific
@@ -157,6 +163,17 @@ if (fs.existsSync(fontLicensesDir)) {
157
163
  for (const [name, source] of desiredFontLicenses) syncFile(source, path.join(fontLicensesDir, name))
158
164
  if (fs.existsSync(fontLicensesDir) && fs.readdirSync(fontLicensesDir).length === 0) fs.rmdirSync(fontLicensesDir)
159
165
 
166
+ const aliasesPath = path.join(resourcesDir, 'font-aliases.json')
167
+ if (fontAliases.size) {
168
+ const contents = JSON.stringify(Object.fromEntries(fontAliases), null, 2) + '\n'
169
+ if (!fs.existsSync(aliasesPath) || fs.readFileSync(aliasesPath, 'utf8') !== contents) {
170
+ const temporary = `${aliasesPath}.tmp.${process.pid}`
171
+ fs.writeFileSync(temporary, contents)
172
+ fs.renameSync(temporary, aliasesPath)
173
+ changed = true
174
+ }
175
+ } else removeFile(aliasesPath)
176
+
160
177
  const windowConfig = path.join(resourcesDir, 'window.json')
161
178
  for (const entry of fs.readdirSync(resourcesDir, { withFileTypes: true })) {
162
179
  if (entry.isFile() && /^window\.json\.tmp\.\d+$/.test(entry.name)) removeFile(path.join(resourcesDir, entry.name))