@ng-native/desktop 0.23.0 → 0.24.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/native/CMakeLists.txt +3 -2
- package/native/appkit/CMakeLists.txt +25 -4
- package/native/appkit/camera-sheet.h +59 -0
- package/native/appkit/camera-sheet.mm +344 -0
- package/native/appkit/camera-view.mm +5 -1
- package/native/appkit/camera.h +22 -0
- package/native/appkit/camera.mm +203 -69
- package/native/appkit/cg-canvas.h +8 -1
- package/native/appkit/cg-canvas.mm +166 -21
- package/native/appkit/header-toolbar.mm +4 -1
- package/native/appkit/host-view.mm +29 -1
- package/native/appkit/main.mm +29 -69
- package/native/appkit/photo-library.mm +6 -6
- package/native/appkit/surface-view.mm +5 -1
- package/native/appkit/svg-view.h +2 -1
- package/native/appkit/svg-view.mm +49 -3
- package/native/appkit/tab-walk.h +31 -0
- package/native/appkit/tab-walk.mm +64 -0
- package/native/appkit/text-input-field.h +5 -0
- package/native/appkit/text-input-field.mm +101 -20
- package/native/appkit/text-layout.h +1 -1
- package/native/appkit/text-layout.mm +16 -8
- package/native/appkit/web-view-component.h +4 -3
- package/native/appkit/web-view-component.mm +8 -2
- package/native/appkit/web-view.mm +7 -0
- package/native/core/include/ngn/app-menus.h +8 -3
- package/native/core/include/ngn/back-swipe.h +16 -7
- package/native/core/include/ngn/camera.h +103 -9
- package/native/core/include/ngn/core-modules.h +3 -0
- package/native/core/include/ngn/dom-web-view.h +53 -0
- package/native/core/include/ngn/photo-library.h +7 -3
- package/native/core/include/ngn/runtime.h +10 -8
- package/native/core/include/ngn/safe-areas.h +5 -0
- package/native/core/include/ngn/surface-metrics.h +51 -0
- package/native/core/include/ngn/svg.h +49 -8
- package/native/core/include/ngn/text-input.h +9 -0
- package/native/core/include/ngn/web-view.h +6 -2
- package/native/core/src/app-menus.cpp +10 -3
- package/native/core/src/back-swipe.cpp +12 -5
- package/native/core/src/dom-web-view.cpp +45 -0
- package/native/core/src/libraries/rnsvg.cpp +13 -0
- package/native/core/src/modules/app-modules.cpp +20 -13
- package/native/core/src/modules/camera.cpp +203 -23
- package/native/core/src/modules/photo-library.cpp +22 -8
- package/native/core/src/mounting-manager.cpp +4 -0
- package/native/core/src/runtime.cpp +6 -8
- package/native/core/src/safe-areas.cpp +7 -0
- package/native/core/src/svg.cpp +324 -17
- package/native/core/src/text-input.cpp +13 -0
- package/native/core/src/web-view.cpp +1 -1
- package/native/scripts/build.sh +5 -1
- package/native/scripts/launch-probe.sh +60 -0
- package/package.json +2 -2
- package/prebuilt.json +6 -6
package/native/appkit/camera.mm
CHANGED
|
@@ -3,53 +3,54 @@
|
|
|
3
3
|
#import <ImageIO/ImageIO.h>
|
|
4
4
|
|
|
5
5
|
#include <atomic>
|
|
6
|
+
#include <cmath>
|
|
7
|
+
#include <optional>
|
|
6
8
|
|
|
7
|
-
// Answers one photo capture:
|
|
9
|
+
// Answers one photo capture: its image and the camera's metadata, or why there is none.
|
|
8
10
|
@interface NGNPhotoCapture : NSObject <AVCapturePhotoCaptureDelegate>
|
|
9
|
-
- (instancetype)
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
- (instancetype)initWithDone:(ngn::appkit::PhotoDone)done;
|
|
12
|
+
// Answers that no photo is coming, for a capture the output refused.
|
|
13
|
+
- (void)abandon:(const char *)reason;
|
|
12
14
|
@end
|
|
13
15
|
|
|
14
16
|
@implementation NGNPhotoCapture {
|
|
15
|
-
|
|
16
|
-
std::string _path;
|
|
17
|
-
std::function<void(int, int, const std::string &)> _done;
|
|
17
|
+
ngn::appkit::PhotoDone _done;
|
|
18
18
|
NGNPhotoCapture *_keep;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
- (instancetype)
|
|
21
|
+
- (instancetype)initWithDone:(ngn::appkit::PhotoDone)done {
|
|
22
22
|
if ((self = [super init])) {
|
|
23
|
-
_quality = quality;
|
|
24
|
-
_path = std::move(path);
|
|
25
23
|
_done = std::move(done);
|
|
26
24
|
_keep = self;
|
|
27
25
|
}
|
|
28
26
|
return self;
|
|
29
27
|
}
|
|
30
28
|
|
|
29
|
+
- (void)abandon:(const char *)reason {
|
|
30
|
+
auto done = std::move(_done);
|
|
31
|
+
_keep = nil;
|
|
32
|
+
if (done) {
|
|
33
|
+
done(nullptr, nil, reason);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
31
37
|
- (void)captureOutput:(AVCapturePhotoOutput *)output didFinishProcessingPhoto:(AVCapturePhoto *)photo error:(NSError *)error {
|
|
32
38
|
auto done = std::move(_done);
|
|
33
39
|
_keep = nil;
|
|
34
40
|
CGImageRef image = error == nil ? photo.CGImageRepresentation : nullptr;
|
|
35
41
|
if (image == nullptr) {
|
|
36
|
-
done(
|
|
42
|
+
done(nullptr, nil, error != nil ? error.localizedDescription.UTF8String : "the camera gave no picture");
|
|
37
43
|
return;
|
|
38
44
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
CFRelease(destination);
|
|
47
|
-
}
|
|
48
|
-
if (!written) {
|
|
49
|
-
done(0, 0, "the picture could not be saved");
|
|
50
|
-
return;
|
|
45
|
+
// macOS has no AVCapturePhoto.metadata: the photo's own file carries it.
|
|
46
|
+
NSDictionary *metadata = nil;
|
|
47
|
+
if (NSData *data = photo.fileDataRepresentation) {
|
|
48
|
+
if (CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, nullptr)) {
|
|
49
|
+
metadata = CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(source, 0, nullptr));
|
|
50
|
+
CFRelease(source);
|
|
51
|
+
}
|
|
51
52
|
}
|
|
52
|
-
done(
|
|
53
|
+
done(image, metadata, "");
|
|
53
54
|
}
|
|
54
55
|
|
|
55
56
|
@end
|
|
@@ -155,6 +156,79 @@ AVVideoCodecType codecOf(const std::string &name) {
|
|
|
155
156
|
return nil;
|
|
156
157
|
}
|
|
157
158
|
|
|
159
|
+
// The session preset a recording request names (CameraRecordingRequest::preset).
|
|
160
|
+
AVCaptureSessionPreset presetOf(const std::string &name) {
|
|
161
|
+
if (name == "Medium") return AVCaptureSessionPresetMedium;
|
|
162
|
+
if (name == "Low") return AVCaptureSessionPresetLow;
|
|
163
|
+
if (name == "640x480") return AVCaptureSessionPreset640x480;
|
|
164
|
+
if (name == "iFrame1280x720") return AVCaptureSessionPresetiFrame1280x720;
|
|
165
|
+
if (name == "iFrame960x540") return AVCaptureSessionPresetiFrame960x540;
|
|
166
|
+
return AVCaptureSessionPresetHigh;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// A session set up to record: the movie output and microphone added to it, and the preset it had
|
|
170
|
+
// before. Only touched on the session's queue.
|
|
171
|
+
struct RecordingSetup {
|
|
172
|
+
AVCaptureMovieFileOutput *movie{nil};
|
|
173
|
+
AVCaptureDeviceInput *microphone{nil};
|
|
174
|
+
NSString *before{nil};
|
|
175
|
+
std::string preset;
|
|
176
|
+
bool sound{false};
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
// Takes the setup off the session and puts its preset back.
|
|
180
|
+
void takeDown(AVCaptureSession *session, std::optional<RecordingSetup> &setup) {
|
|
181
|
+
if (!setup) {
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
[session beginConfiguration];
|
|
185
|
+
if (setup->movie != nil) {
|
|
186
|
+
[session removeOutput:setup->movie];
|
|
187
|
+
}
|
|
188
|
+
if (setup->microphone != nil) {
|
|
189
|
+
[session removeInput:setup->microphone];
|
|
190
|
+
}
|
|
191
|
+
if ([session canSetSessionPreset:setup->before]) {
|
|
192
|
+
session.sessionPreset = setup->before;
|
|
193
|
+
}
|
|
194
|
+
[session commitConfiguration];
|
|
195
|
+
setup.reset();
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// Sets the session up for `request`, unless it already is.
|
|
199
|
+
void setUp(AVCaptureSession *session, const CameraRecordingRequest &request, std::optional<RecordingSetup> &setup) {
|
|
200
|
+
if (setup && setup->preset == request.preset && setup->sound == request.sound) {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
takeDown(session, setup);
|
|
204
|
+
RecordingSetup made{.before = session.sessionPreset, .preset = request.preset, .sound = request.sound};
|
|
205
|
+
[session beginConfiguration];
|
|
206
|
+
// The preset asked for, or High when this camera has no such preset.
|
|
207
|
+
AVCaptureSessionPreset recordAt = presetOf(request.preset);
|
|
208
|
+
if (![session canSetSessionPreset:recordAt]) {
|
|
209
|
+
recordAt = AVCaptureSessionPresetHigh;
|
|
210
|
+
}
|
|
211
|
+
if ([session canSetSessionPreset:recordAt]) {
|
|
212
|
+
session.sessionPreset = recordAt;
|
|
213
|
+
}
|
|
214
|
+
// Never without the microphone allowed: a microphone input in a session asks for it.
|
|
215
|
+
if (request.sound && [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio] == AVAuthorizationStatusAuthorized) {
|
|
216
|
+
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
|
|
217
|
+
AVCaptureDeviceInput *microphone = device != nil ? [AVCaptureDeviceInput deviceInputWithDevice:device error:nil] : nil;
|
|
218
|
+
if (microphone != nil && [session canAddInput:microphone]) {
|
|
219
|
+
[session addInput:microphone];
|
|
220
|
+
made.microphone = microphone;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
AVCaptureMovieFileOutput *movie = [AVCaptureMovieFileOutput new];
|
|
224
|
+
if ([session canAddOutput:movie]) {
|
|
225
|
+
[session addOutput:movie];
|
|
226
|
+
made.movie = movie;
|
|
227
|
+
}
|
|
228
|
+
[session commitConfiguration];
|
|
229
|
+
setup = made;
|
|
230
|
+
}
|
|
231
|
+
|
|
158
232
|
class AVSessionCapture final : public SessionCapture {
|
|
159
233
|
public:
|
|
160
234
|
AVSessionCapture(AVCaptureSession *session, AVCapturePhotoOutput *output)
|
|
@@ -188,8 +262,33 @@ class AVSessionCapture final : public SessionCapture {
|
|
|
188
262
|
});
|
|
189
263
|
}
|
|
190
264
|
|
|
191
|
-
//
|
|
192
|
-
//
|
|
265
|
+
// Reconfiguring a running session restarts the camera's stream, which takes it a moment, so a
|
|
266
|
+
// recording set up only as it starts begins late.
|
|
267
|
+
void prepareRecording(const CameraRecordingRequest &asked) override {
|
|
268
|
+
CameraRecordingRequest request = asked;
|
|
269
|
+
AVCaptureSession *session = session_;
|
|
270
|
+
auto setup = setup_;
|
|
271
|
+
auto recording = recording_;
|
|
272
|
+
dispatch_async(queue_, ^{
|
|
273
|
+
if (!recording->load()) {
|
|
274
|
+
setUp(session, request, *setup);
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
void unprepareRecording() override {
|
|
279
|
+
AVCaptureSession *session = session_;
|
|
280
|
+
auto setup = setup_;
|
|
281
|
+
auto recording = recording_;
|
|
282
|
+
dispatch_async(queue_, ^{
|
|
283
|
+
if (!recording->load()) {
|
|
284
|
+
takeDown(session, *setup);
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// The movie output and, with sound, the microphone are on the session for the recording (added
|
|
290
|
+
// now unless prepareRecording added them) and taken off after it, on the session's queue, since
|
|
291
|
+
// configuring a running session blocks.
|
|
193
292
|
void startRecording(const CameraRecordingRequest &asked, std::function<void(const std::string &)> done) override {
|
|
194
293
|
// The block runs later on the session's queue: a reference it captured would be to the
|
|
195
294
|
// caller's request, gone by then.
|
|
@@ -205,53 +304,28 @@ class AVSessionCapture final : public SessionCapture {
|
|
|
205
304
|
AVCaptureSession *session = session_;
|
|
206
305
|
auto recording = recording_;
|
|
207
306
|
auto movieHolder = movie_;
|
|
307
|
+
auto setup = setup_;
|
|
208
308
|
auto answer = std::make_shared<std::function<void(const std::string &)>>(std::move(done));
|
|
209
309
|
dispatch_queue_t queue = queue_;
|
|
210
310
|
dispatch_async(queue_, ^{
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
session
|
|
215
|
-
}
|
|
216
|
-
AVCaptureDeviceInput *microphone = nil;
|
|
217
|
-
// Never without the microphone allowed: a microphone input in a session asks for it.
|
|
218
|
-
if (request.sound &&
|
|
219
|
-
[AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio] == AVAuthorizationStatusAuthorized) {
|
|
220
|
-
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
|
|
221
|
-
microphone = device != nil ? [AVCaptureDeviceInput deviceInputWithDevice:device error:nil] : nil;
|
|
222
|
-
if (microphone != nil && [session canAddInput:microphone]) {
|
|
223
|
-
[session addInput:microphone];
|
|
224
|
-
} else {
|
|
225
|
-
microphone = nil;
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
AVCaptureMovieFileOutput *movie = [AVCaptureMovieFileOutput new];
|
|
229
|
-
bool added = [session canAddOutput:movie];
|
|
230
|
-
if (added) {
|
|
231
|
-
[session addOutput:movie];
|
|
232
|
-
}
|
|
233
|
-
[session commitConfiguration];
|
|
234
|
-
void (^takeDown)(void) = ^{
|
|
235
|
-
[session beginConfiguration];
|
|
236
|
-
if (added) {
|
|
237
|
-
[session removeOutput:movie];
|
|
238
|
-
}
|
|
239
|
-
if (microphone != nil) {
|
|
240
|
-
[session removeInput:microphone];
|
|
241
|
-
}
|
|
242
|
-
if ([session canSetSessionPreset:preset]) {
|
|
243
|
-
session.sessionPreset = preset;
|
|
244
|
-
}
|
|
245
|
-
[session commitConfiguration];
|
|
311
|
+
setUp(session, request, *setup);
|
|
312
|
+
AVCaptureMovieFileOutput *movie = (*setup)->movie;
|
|
313
|
+
void (^finished)(void) = ^{
|
|
314
|
+
takeDown(session, *setup);
|
|
246
315
|
*movieHolder = nil;
|
|
247
316
|
recording->store(false);
|
|
248
317
|
};
|
|
249
|
-
AVCaptureConnection *video =
|
|
318
|
+
AVCaptureConnection *video = movie != nil ? [movie connectionWithMediaType:AVMediaTypeVideo] : nil;
|
|
250
319
|
if (video == nil || !video.active || !video.enabled) {
|
|
251
|
-
|
|
320
|
+
finished();
|
|
252
321
|
(*answer)("the camera gives no video to record");
|
|
253
322
|
return;
|
|
254
323
|
}
|
|
324
|
+
// The movie is never mirrored, as on iOS, whatever its preview shows.
|
|
325
|
+
if (video.isVideoMirroringSupported) {
|
|
326
|
+
video.automaticallyAdjustsVideoMirroring = NO;
|
|
327
|
+
video.videoMirrored = NO;
|
|
328
|
+
}
|
|
255
329
|
// A codec this Mac cannot write is refused by an exception; the default stays.
|
|
256
330
|
if (AVVideoCodecType codec = codecOf(request.codec)) {
|
|
257
331
|
@try {
|
|
@@ -265,10 +339,10 @@ class AVSessionCapture final : public SessionCapture {
|
|
|
265
339
|
[NSFileManager.defaultManager removeItemAtURL:url error:nil];
|
|
266
340
|
*movieHolder = movie;
|
|
267
341
|
// The answer follows the take-down, so a recording asked for at once finds none running.
|
|
268
|
-
NGNMovieRecording *delegate = [[NGNMovieRecording alloc] initWithDone:[answer,
|
|
342
|
+
NGNMovieRecording *delegate = [[NGNMovieRecording alloc] initWithDone:[answer, finished, queue](const std::string &error) {
|
|
269
343
|
std::string message = error;
|
|
270
344
|
dispatch_async(queue, ^{
|
|
271
|
-
|
|
345
|
+
finished();
|
|
272
346
|
(*answer)(message);
|
|
273
347
|
});
|
|
274
348
|
}];
|
|
@@ -286,13 +360,30 @@ class AVSessionCapture final : public SessionCapture {
|
|
|
286
360
|
bool recording() const override {
|
|
287
361
|
return recording_->load();
|
|
288
362
|
}
|
|
289
|
-
void
|
|
363
|
+
void capturePhoto(PhotoDone done) override {
|
|
290
364
|
if (!session_.running) {
|
|
291
|
-
done(
|
|
365
|
+
done(nullptr, nil, "the camera is not running");
|
|
292
366
|
return;
|
|
293
367
|
}
|
|
294
|
-
|
|
295
|
-
|
|
368
|
+
// On the session's queue, after any change to the session queued before it (a recording's setup
|
|
369
|
+
// coming down), which a still taken mid-change would find with no video to take.
|
|
370
|
+
auto answer = std::make_shared<PhotoDone>(std::move(done));
|
|
371
|
+
AVCaptureSession *session = session_;
|
|
372
|
+
AVCapturePhotoOutput *output = output_;
|
|
373
|
+
dispatch_async(queue_, ^{
|
|
374
|
+
if (!session.running) {
|
|
375
|
+
(*answer)(nullptr, nil, "the camera is not running");
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
NGNPhotoCapture *capture = [[NGNPhotoCapture alloc] initWithDone:*answer];
|
|
379
|
+
@try {
|
|
380
|
+
[output capturePhotoWithSettings:[AVCapturePhotoSettings photoSettings] delegate:capture];
|
|
381
|
+
} @catch (NSException *exception) {
|
|
382
|
+
// The output refuses with no active video connection; the delegate never hears.
|
|
383
|
+
const char *reason = exception.reason.UTF8String;
|
|
384
|
+
[capture abandon:reason != nullptr ? reason : "the camera gave no picture"];
|
|
385
|
+
}
|
|
386
|
+
});
|
|
296
387
|
}
|
|
297
388
|
|
|
298
389
|
private:
|
|
@@ -303,10 +394,53 @@ class AVSessionCapture final : public SessionCapture {
|
|
|
303
394
|
// and its output while it does (set and read on the queue).
|
|
304
395
|
std::shared_ptr<std::atomic<bool>> recording_ = std::make_shared<std::atomic<bool>>(false);
|
|
305
396
|
std::shared_ptr<AVCaptureMovieFileOutput *> movie_ = std::make_shared<AVCaptureMovieFileOutput *>(nil);
|
|
397
|
+
// What the session is set up to record with, prepared or for the recording running.
|
|
398
|
+
std::shared_ptr<std::optional<RecordingSetup>> setup_ = std::make_shared<std::optional<RecordingSetup>>();
|
|
306
399
|
};
|
|
307
400
|
|
|
308
401
|
} // namespace
|
|
309
402
|
|
|
403
|
+
void SessionCapture::takePicture(const CameraPictureRequest &request, std::function<void(int, int, const std::string &)> done) {
|
|
404
|
+
capturePhoto([request, done = std::move(done)](CGImageRef image, NSDictionary *, const std::string &error) {
|
|
405
|
+
if (image == nullptr) {
|
|
406
|
+
done(0, 0, error);
|
|
407
|
+
} else if (!writePicture(image, request)) {
|
|
408
|
+
done(0, 0, "the picture could not be saved");
|
|
409
|
+
} else {
|
|
410
|
+
done(static_cast<int>(CGImageGetWidth(image)), static_cast<int>(CGImageGetHeight(image)), "");
|
|
411
|
+
}
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
bool writePicture(CGImageRef image, const CameraPictureRequest &request) {
|
|
416
|
+
bool png = request.format == "png";
|
|
417
|
+
NSURL *url = [NSURL fileURLWithPath:@(request.path.c_str())];
|
|
418
|
+
CGImageDestinationRef destination =
|
|
419
|
+
CGImageDestinationCreateWithURL((__bridge CFURLRef)url, png ? CFSTR("public.png") : CFSTR("public.jpeg"), 1, nullptr);
|
|
420
|
+
if (destination == nullptr) {
|
|
421
|
+
return false;
|
|
422
|
+
}
|
|
423
|
+
// A PNG is lossless, so the quality is a JPEG's only, as on iOS.
|
|
424
|
+
NSDictionary *properties = png ? @{} : @{(__bridge NSString *)kCGImageDestinationLossyCompressionQuality : @(request.quality)};
|
|
425
|
+
CGImageDestinationAddImage(destination, image, (__bridge CFDictionaryRef)properties);
|
|
426
|
+
bool written = CGImageDestinationFinalize(destination);
|
|
427
|
+
CFRelease(destination);
|
|
428
|
+
return written;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
std::optional<CameraClip> movieClip(const std::string &path) {
|
|
432
|
+
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:@(path.c_str())] options:nil];
|
|
433
|
+
AVAssetTrack *track = [asset tracksWithMediaType:AVMediaTypeVideo].firstObject;
|
|
434
|
+
if (track == nil) {
|
|
435
|
+
return std::nullopt;
|
|
436
|
+
}
|
|
437
|
+
// A track turned a quarter is its size turned too, which the transform's sign would make negative.
|
|
438
|
+
CGSize size = CGSizeApplyAffineTransform(track.naturalSize, track.preferredTransform);
|
|
439
|
+
return CameraClip{.width = static_cast<int>(std::lround(std::fabs(size.width))),
|
|
440
|
+
.height = static_cast<int>(std::lround(std::fabs(size.height))),
|
|
441
|
+
.duration = CMTimeGetSeconds(asset.duration) * 1000};
|
|
442
|
+
}
|
|
443
|
+
|
|
310
444
|
std::shared_ptr<CameraHardware> makeCameraHardware() {
|
|
311
445
|
return std::make_shared<AVHardware>();
|
|
312
446
|
}
|
|
@@ -14,4 +14,11 @@ CGPathRef NGNCreatePath(const ngn::SvgPath &path);
|
|
|
14
14
|
|
|
15
15
|
// A canvas over `context`, whose coordinates are the view's: top left, y down, in points, `size`
|
|
16
16
|
// big at `scale` pixels a point. `imageLoaded` runs when an image it was asked for has loaded.
|
|
17
|
-
|
|
17
|
+
// `viewFor` finds a foreignObject's view by its tag, whose children the canvas draws; without it,
|
|
18
|
+
// a foreignObject draws nothing.
|
|
19
|
+
std::unique_ptr<ngn::SvgCanvas> NGNMakeCGCanvas(
|
|
20
|
+
CGContextRef context,
|
|
21
|
+
CGSize size,
|
|
22
|
+
CGFloat scale,
|
|
23
|
+
void (^imageLoaded)(void),
|
|
24
|
+
NSView * (^viewFor)(ngn::Tag) = nil);
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
#include "ngn/file-system.h"
|
|
4
4
|
|
|
5
|
+
#include <algorithm>
|
|
5
6
|
#include <cmath>
|
|
6
7
|
#include <vector>
|
|
7
8
|
|
|
@@ -46,6 +47,32 @@ CGPathRef createPathImpl(const ngn::SvgPath &path) {
|
|
|
46
47
|
}
|
|
47
48
|
|
|
48
49
|
// Images an svg draws, by URI, loaded once for every svg.
|
|
50
|
+
// The glyphs a cluster drew as in its shaped run: per font, each glyph and where it sits from the
|
|
51
|
+
// cluster's start.
|
|
52
|
+
struct ShapedGlyphs {
|
|
53
|
+
struct Piece {
|
|
54
|
+
CTFontRef font{nullptr};
|
|
55
|
+
std::vector<CGGlyph> glyphs;
|
|
56
|
+
std::vector<CGPoint> positions;
|
|
57
|
+
};
|
|
58
|
+
std::vector<Piece> pieces;
|
|
59
|
+
ShapedGlyphs() = default;
|
|
60
|
+
ShapedGlyphs(const ShapedGlyphs &) = delete;
|
|
61
|
+
ShapedGlyphs &operator=(const ShapedGlyphs &) = delete;
|
|
62
|
+
~ShapedGlyphs() {
|
|
63
|
+
for (auto &p : pieces) {
|
|
64
|
+
CFRelease(p.font);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
void add(CTFontRef font, CGGlyph glyph, CGPoint position) {
|
|
68
|
+
if (pieces.empty() || pieces.back().font != font) {
|
|
69
|
+
pieces.push_back({.font = static_cast<CTFontRef>(CFRetain(font))});
|
|
70
|
+
}
|
|
71
|
+
pieces.back().glyphs.push_back(glyph);
|
|
72
|
+
pieces.back().positions.push_back(position);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
|
|
49
76
|
NSCache<NSString *, NSImage *> *imageCache() {
|
|
50
77
|
static NSCache<NSString *, NSImage *> *cache = [NSCache new];
|
|
51
78
|
return cache;
|
|
@@ -56,8 +83,8 @@ NSCache<NSString *, NSImage *> *imageCache() {
|
|
|
56
83
|
// starts where the drawing is.
|
|
57
84
|
class CGCanvas final : public ngn::SvgCanvas {
|
|
58
85
|
public:
|
|
59
|
-
CGCanvas(CGContextRef context, CGSize size, CGFloat scale, void (^imageLoaded)(void))
|
|
60
|
-
: context_(context), size_(size), scale_(scale), imageLoaded_(imageLoaded) {}
|
|
86
|
+
CGCanvas(CGContextRef context, CGSize size, CGFloat scale, void (^imageLoaded)(void), NSView * (^viewFor)(ngn::Tag))
|
|
87
|
+
: context_(context), size_(size), scale_(scale), imageLoaded_(imageLoaded), viewFor_(viewFor) {}
|
|
61
88
|
|
|
62
89
|
void save() override {
|
|
63
90
|
CGContextSaveGState(context_);
|
|
@@ -266,7 +293,7 @@ class CGCanvas final : public ngn::SvgCanvas {
|
|
|
266
293
|
CGContextTranslateCTM(bitmap, 0, height);
|
|
267
294
|
CGContextScaleCTM(bitmap, width / tile.tile.width, -(height / tile.tile.height));
|
|
268
295
|
{
|
|
269
|
-
CGCanvas inner(bitmap, CGSizeMake(tile.tile.width, tile.tile.height), density, imageLoaded_);
|
|
296
|
+
CGCanvas inner(bitmap, CGSizeMake(tile.tile.width, tile.tile.height), density, imageLoaded_, viewFor_);
|
|
270
297
|
drawTile(inner);
|
|
271
298
|
}
|
|
272
299
|
CGImageRef image = CGBitmapContextCreateImage(bitmap);
|
|
@@ -366,14 +393,139 @@ class CGCanvas final : public ngn::SvgCanvas {
|
|
|
366
393
|
CGContextRestoreGState(context_);
|
|
367
394
|
}
|
|
368
395
|
|
|
369
|
-
double text
|
|
396
|
+
double measureText(const std::string &text, double fontSize, const std::string &fontFamily, const std::string &fontWeight) override {
|
|
397
|
+
CTLineRef line = createLine(text, fontSize, fontFamily, fontWeight, nullptr);
|
|
398
|
+
double width = CTLineGetTypographicBounds(line, nullptr, nullptr, nullptr);
|
|
399
|
+
CFRelease(line);
|
|
400
|
+
return width;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// The composed character sequences of `text` (Foundation's grapheme clusters), each with the
|
|
404
|
+
// glyphs Core Text's shaped line has for it, where it has them. A cluster with no glyph of its
|
|
405
|
+
// own, the tail of a ligature, joins the one before it.
|
|
406
|
+
std::vector<ngn::SvgGlyphCluster> glyphClusters(
|
|
407
|
+
const std::string &text, double fontSize, const std::string &fontFamily, const std::string &fontWeight) override {
|
|
408
|
+
NSString *string = @(text.c_str());
|
|
409
|
+
if (string == nil) {
|
|
410
|
+
string = @"";
|
|
411
|
+
}
|
|
412
|
+
std::vector<NSRange> ranges;
|
|
413
|
+
std::vector<size_t> clusterOf(string.length);
|
|
414
|
+
for (NSUInteger i = 0; i < string.length;) {
|
|
415
|
+
NSRange range = [string rangeOfComposedCharacterSequenceAtIndex:i];
|
|
416
|
+
std::fill(clusterOf.begin() + static_cast<long>(range.location), clusterOf.begin() + static_cast<long>(NSMaxRange(range)), ranges.size());
|
|
417
|
+
ranges.push_back(range);
|
|
418
|
+
i = NSMaxRange(range);
|
|
419
|
+
}
|
|
420
|
+
std::vector<double> left(ranges.size(), INFINITY), right(ranges.size(), -INFINITY);
|
|
421
|
+
struct Glyph {
|
|
422
|
+
CTFontRef font;
|
|
423
|
+
CGGlyph glyph;
|
|
424
|
+
CGPoint position;
|
|
425
|
+
};
|
|
426
|
+
std::vector<std::vector<Glyph>> glyphs(ranges.size());
|
|
427
|
+
CTLineRef line = createLine(text, fontSize, fontFamily, fontWeight, nullptr);
|
|
428
|
+
for (id item in (__bridge NSArray *)CTLineGetGlyphRuns(line)) {
|
|
429
|
+
auto run = (__bridge CTRunRef)item;
|
|
430
|
+
auto font = (CTFontRef)CFDictionaryGetValue(CTRunGetAttributes(run), kCTFontAttributeName);
|
|
431
|
+
CFIndex count = CTRunGetGlyphCount(run);
|
|
432
|
+
std::vector<CGGlyph> ids(static_cast<size_t>(count));
|
|
433
|
+
std::vector<CGPoint> positions(static_cast<size_t>(count));
|
|
434
|
+
std::vector<CGSize> advances(static_cast<size_t>(count));
|
|
435
|
+
std::vector<CFIndex> indices(static_cast<size_t>(count));
|
|
436
|
+
CTRunGetGlyphs(run, CFRangeMake(0, 0), ids.data());
|
|
437
|
+
CTRunGetPositions(run, CFRangeMake(0, 0), positions.data());
|
|
438
|
+
CTRunGetAdvances(run, CFRangeMake(0, 0), advances.data());
|
|
439
|
+
CTRunGetStringIndices(run, CFRangeMake(0, 0), indices.data());
|
|
440
|
+
for (size_t g = 0; g < positions.size(); ++g) {
|
|
441
|
+
if (font == nullptr || indices[g] < 0 || static_cast<size_t>(indices[g]) >= clusterOf.size()) {
|
|
442
|
+
continue;
|
|
443
|
+
}
|
|
444
|
+
size_t c = clusterOf[static_cast<size_t>(indices[g])];
|
|
445
|
+
left[c] = std::min(left[c], static_cast<double>(positions[g].x));
|
|
446
|
+
right[c] = std::max(right[c], static_cast<double>(positions[g].x + advances[g].width));
|
|
447
|
+
glyphs[c].push_back({font, ids[g], positions[g]});
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
std::vector<ngn::SvgGlyphCluster> out;
|
|
451
|
+
for (size_t c = 0; c < ranges.size(); ++c) {
|
|
452
|
+
std::string part = [string substringWithRange:ranges[c]].UTF8String;
|
|
453
|
+
if (glyphs[c].empty() && !out.empty()) {
|
|
454
|
+
out.back().text += part;
|
|
455
|
+
continue;
|
|
456
|
+
}
|
|
457
|
+
auto shaped = std::make_shared<ShapedGlyphs>();
|
|
458
|
+
for (const auto &g : glyphs[c]) {
|
|
459
|
+
shaped->add(g.font, g.glyph, CGPointMake(g.position.x - left[c], g.position.y));
|
|
460
|
+
}
|
|
461
|
+
bool placed = !glyphs[c].empty();
|
|
462
|
+
out.push_back({.text = std::move(part), .x = placed ? left[c] : 0, .advance = placed ? right[c] - left[c] : 0, .glyphs = shaped});
|
|
463
|
+
}
|
|
464
|
+
CFRelease(line);
|
|
465
|
+
return out;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
void glyphCluster(const ngn::SvgGlyphCluster &cluster, double x, double y, const ngn::SvgPaint &fill) override {
|
|
469
|
+
auto shaped = std::static_pointer_cast<const ShapedGlyphs>(cluster.glyphs);
|
|
470
|
+
if (!shaped) {
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
uint32_t argb = fill.gradient && !fill.gradient->stops.empty() ? fill.gradient->stops[0].second : fill.argb;
|
|
474
|
+
CGColorRef color = createColorImpl(argb, fill.opacity);
|
|
475
|
+
CGContextSaveGState(context_);
|
|
476
|
+
CGContextTranslateCTM(context_, x, y);
|
|
477
|
+
CGContextScaleCTM(context_, 1, -1);
|
|
478
|
+
CGContextSetTextMatrix(context_, CGAffineTransformIdentity);
|
|
479
|
+
CGContextSetFillColorWithColor(context_, color);
|
|
480
|
+
for (const auto &p : shaped->pieces) {
|
|
481
|
+
CTFontDrawGlyphs(p.font, p.glyphs.data(), p.positions.data(), p.glyphs.size(), context_);
|
|
482
|
+
}
|
|
483
|
+
CGContextRestoreGState(context_);
|
|
484
|
+
CGColorRelease(color);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
void text(
|
|
370
488
|
const std::string &text,
|
|
371
489
|
double x,
|
|
372
490
|
double y,
|
|
373
491
|
double fontSize,
|
|
374
492
|
const std::string &fontFamily,
|
|
375
493
|
const std::string &fontWeight,
|
|
376
|
-
const ngn::SvgPaint
|
|
494
|
+
const ngn::SvgPaint &fill) override {
|
|
495
|
+
CTLineRef line = createLine(text, fontSize, fontFamily, fontWeight, &fill);
|
|
496
|
+
CGContextSaveGState(context_);
|
|
497
|
+
CGContextTranslateCTM(context_, x, y);
|
|
498
|
+
CGContextScaleCTM(context_, 1, -1);
|
|
499
|
+
CGContextSetTextMatrix(context_, CGAffineTransformIdentity);
|
|
500
|
+
CGContextSetTextPosition(context_, 0, 0);
|
|
501
|
+
CTLineDraw(line, context_);
|
|
502
|
+
CGContextRestoreGState(context_);
|
|
503
|
+
CFRelease(line);
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
// Each child of the view as its layer draws it, as react-native-svg's ForeignObject draws them on
|
|
507
|
+
// iOS: its frame's corner, its transform (which rendering a layer leaves out), then its layer and
|
|
508
|
+
// those under it. A layer renders though an ancestor view is hidden, so the views stay hidden and
|
|
509
|
+
// take no clicks; a hidden one's layer renders nothing.
|
|
510
|
+
void views(ngn::Tag view) override {
|
|
511
|
+
NSView *object = viewFor_ != nil ? viewFor_(view) : nil;
|
|
512
|
+
for (NSView *child in object.subviews) {
|
|
513
|
+
CGContextSaveGState(context_);
|
|
514
|
+
CGContextTranslateCTM(context_, child.frame.origin.x, child.frame.origin.y);
|
|
515
|
+
CGContextConcatCTM(context_, CATransform3DGetAffineTransform(child.layer.transform));
|
|
516
|
+
[child.layer renderInContext:context_];
|
|
517
|
+
CGContextRestoreGState(context_);
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
private:
|
|
522
|
+
// A +1 line of `text` in the font, in `fill`'s colour (a gradient's first stop) when given.
|
|
523
|
+
static CTLineRef createLine(
|
|
524
|
+
const std::string &text,
|
|
525
|
+
double fontSize,
|
|
526
|
+
const std::string &fontFamily,
|
|
527
|
+
const std::string &fontWeight,
|
|
528
|
+
const ngn::SvgPaint *fill) {
|
|
377
529
|
NSFont *font = fontFamily.empty() ? nil : [NSFont fontWithName:@(fontFamily.c_str()) size:fontSize];
|
|
378
530
|
if (font == nil) {
|
|
379
531
|
int weight = fontWeight == "bold" ? 700 : std::atoi(fontWeight.c_str());
|
|
@@ -385,22 +537,9 @@ class CGCanvas final : public ngn::SvgCanvas {
|
|
|
385
537
|
initWithString:@(text.c_str())
|
|
386
538
|
attributes:@{NSFontAttributeName : font, (__bridge id)kCTForegroundColorAttributeName : (__bridge id)color}];
|
|
387
539
|
CGColorRelease(color);
|
|
388
|
-
|
|
389
|
-
double width = CTLineGetTypographicBounds(line, nullptr, nullptr, nullptr);
|
|
390
|
-
if (fill != nullptr) {
|
|
391
|
-
CGContextSaveGState(context_);
|
|
392
|
-
CGContextTranslateCTM(context_, x, y);
|
|
393
|
-
CGContextScaleCTM(context_, 1, -1);
|
|
394
|
-
CGContextSetTextMatrix(context_, CGAffineTransformIdentity);
|
|
395
|
-
CGContextSetTextPosition(context_, 0, 0);
|
|
396
|
-
CTLineDraw(line, context_);
|
|
397
|
-
CGContextRestoreGState(context_);
|
|
398
|
-
}
|
|
399
|
-
CFRelease(line);
|
|
400
|
-
return width;
|
|
540
|
+
return CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)string);
|
|
401
541
|
}
|
|
402
542
|
|
|
403
|
-
private:
|
|
404
543
|
struct Mask {
|
|
405
544
|
CGContextRef parent;
|
|
406
545
|
CGContextRef mask;
|
|
@@ -497,6 +636,7 @@ class CGCanvas final : public ngn::SvgCanvas {
|
|
|
497
636
|
CGSize size_;
|
|
498
637
|
CGFloat scale_;
|
|
499
638
|
void (^imageLoaded_)(void);
|
|
639
|
+
NSView * (^viewFor_)(ngn::Tag);
|
|
500
640
|
ngn::SvgMatrix matrix_;
|
|
501
641
|
std::vector<ngn::SvgMatrix> saved_;
|
|
502
642
|
std::vector<Mask> masks_;
|
|
@@ -513,6 +653,11 @@ CGPathRef NGNCreatePath(const ngn::SvgPath &path) {
|
|
|
513
653
|
return createPathImpl(path);
|
|
514
654
|
}
|
|
515
655
|
|
|
516
|
-
std::unique_ptr<ngn::SvgCanvas> NGNMakeCGCanvas(
|
|
517
|
-
|
|
656
|
+
std::unique_ptr<ngn::SvgCanvas> NGNMakeCGCanvas(
|
|
657
|
+
CGContextRef context,
|
|
658
|
+
CGSize size,
|
|
659
|
+
CGFloat scale,
|
|
660
|
+
void (^imageLoaded)(void),
|
|
661
|
+
NSView * (^viewFor)(ngn::Tag)) {
|
|
662
|
+
return std::make_unique<CGCanvas>(context, size, scale, imageLoaded, viewFor);
|
|
518
663
|
}
|
|
@@ -122,7 +122,10 @@ NSImage *symbol(const std::string &name, NSString *description) {
|
|
|
122
122
|
@implementation NGNHistoryControl
|
|
123
123
|
- (NSMenu *)menuForEvent:(NSEvent *)event {
|
|
124
124
|
NSPoint point = [self convertPoint:event.locationInWindow fromView:nil];
|
|
125
|
-
|
|
125
|
+
// Segment 0 is back, which AppKit lays out on the right in a right-to-left layout.
|
|
126
|
+
bool rightToLeft = self.userInterfaceLayoutDirection == NSUserInterfaceLayoutDirectionRightToLeft;
|
|
127
|
+
NSInteger segment =
|
|
128
|
+
ngn::chevronAt(point.x, NSWidth(self.bounds), rightToLeft) == ngn::NavigationDirection::Back ? 0 : 1;
|
|
126
129
|
return [self isEnabledForSegment:segment] ? [self menuForSegment:segment] : nil;
|
|
127
130
|
}
|
|
128
131
|
@end
|