@blotch/angular-lynx-native 22.3.0 → 22.3.2

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.
Files changed (100) hide show
  1. package/README.md +41 -15
  2. package/dist/android/project.d.ts +90 -1
  3. package/dist/android/project.d.ts.map +1 -1
  4. package/dist/android/project.js +369 -5
  5. package/dist/eject.d.ts +15 -1
  6. package/dist/eject.d.ts.map +1 -1
  7. package/dist/eject.js +13 -2
  8. package/dist/features.d.ts +70 -0
  9. package/dist/features.d.ts.map +1 -0
  10. package/dist/features.js +115 -0
  11. package/dist/generate.d.ts +17 -1
  12. package/dist/generate.d.ts.map +1 -1
  13. package/dist/generate.js +12 -4
  14. package/dist/index.d.ts +1 -0
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +8 -1
  17. package/dist/ios/build.d.ts.map +1 -1
  18. package/dist/ios/build.js +8 -2
  19. package/dist/ios/frameworks.d.ts +6 -5
  20. package/dist/ios/frameworks.d.ts.map +1 -1
  21. package/dist/ios/frameworks.js +46 -15
  22. package/dist/ios/project.d.ts +173 -11
  23. package/dist/ios/project.d.ts.map +1 -1
  24. package/dist/ios/project.js +767 -33
  25. package/dist/options.d.ts +143 -2
  26. package/dist/options.d.ts.map +1 -1
  27. package/dist/options.js +127 -0
  28. package/dist/permissions.d.ts +18 -1
  29. package/dist/permissions.d.ts.map +1 -1
  30. package/dist/permissions.js +63 -3
  31. package/package.json +1 -1
  32. package/templates/android/README.md +37 -2
  33. package/templates/android/angular-lynx.properties +2 -0
  34. package/templates/android/app/build.gradle.kts +70 -1
  35. package/templates/android/app/src/main/AndroidManifest.xml +24 -3
  36. package/templates/android/app/src/main/java/dev/angularlynx/shell/BarcodeScannerModule.kt +144 -0
  37. package/templates/android/app/src/main/java/dev/angularlynx/shell/BiometricsModule.kt +433 -0
  38. package/templates/android/app/src/main/java/dev/angularlynx/shell/BitmapPresenter.kt +98 -0
  39. package/templates/android/app/src/main/java/dev/angularlynx/shell/BitmapRendererContext.kt +175 -0
  40. package/templates/android/app/src/main/java/dev/angularlynx/shell/BluetoothModule.kt +1288 -0
  41. package/templates/android/app/src/main/java/dev/angularlynx/shell/CalendarModule.kt +127 -0
  42. package/templates/android/app/src/main/java/dev/angularlynx/shell/Canvas2DContext.kt +1557 -0
  43. package/templates/android/app/src/main/java/dev/angularlynx/shell/Canvas2DOps.kt +63 -0
  44. package/templates/android/app/src/main/java/dev/angularlynx/shell/CanvasFilter.kt +357 -0
  45. package/templates/android/app/src/main/java/dev/angularlynx/shell/CanvasUI.kt +4 -3
  46. package/templates/android/app/src/main/java/dev/angularlynx/shell/FilePickerModule.kt +263 -0
  47. package/templates/android/app/src/main/java/dev/angularlynx/shell/HapticsModule.kt +82 -0
  48. package/templates/android/app/src/main/java/dev/angularlynx/shell/MainActivity.kt +376 -6
  49. package/templates/android/app/src/main/java/dev/angularlynx/shell/ModuleBytes.kt +58 -0
  50. package/templates/android/app/src/main/java/dev/angularlynx/shell/MotionModule.kt +332 -0
  51. package/templates/android/app/src/main/java/dev/angularlynx/shell/NfcModule.kt +274 -0
  52. package/templates/android/app/src/main/java/dev/angularlynx/shell/PermissionRequests.kt +25 -0
  53. package/templates/android/app/src/main/java/dev/angularlynx/shell/PhotoLibraryModule.kt +168 -0
  54. package/templates/android/app/src/main/java/dev/angularlynx/shell/ShareTargetModule.kt +234 -0
  55. package/templates/android/app/src/main/java/dev/angularlynx/shell/ShellApplication.kt +6 -0
  56. package/templates/android/app/src/main/java/dev/angularlynx/shell/ShellModule.kt +47 -3
  57. package/templates/android/app/src/main/java/dev/angularlynx/shell/StoreReviewModule.kt +66 -0
  58. package/templates/android/app/src/main/java/dev/angularlynx/shell/SystemFontFamilies.kt +68 -0
  59. package/templates/android/app/src/main/java/dev/angularlynx/shell/TrackingModule.kt +74 -0
  60. package/templates/android/app/src/main/java/dev/angularlynx/shell/VideoFrameTarget.kt +429 -0
  61. package/templates/android/app/src/main/java/dev/angularlynx/shell/VideoUI.kt +676 -0
  62. package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLContext.kt +128 -13
  63. package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLModule.kt +493 -65
  64. package/templates/android/app/src/main/java/dev/angularlynx/shell/WifiModule.kt +429 -0
  65. package/templates/android/app/src/main/res/values/biometrics.xml +9 -0
  66. package/templates/android/app/src/main/res/values/share_target.xml +11 -0
  67. package/templates/android/app/src/main/res/xml/backup_rules.xml +5 -3
  68. package/templates/android/app/src/main/res/xml/data_extraction_rules.xml +6 -3
  69. package/templates/android/build.gradle.kts +1 -1
  70. package/templates/android/gradle/wrapper/gradle-wrapper.properties +2 -2
  71. package/templates/ios/App/AngularLynxBitmapRendererContext.m +188 -0
  72. package/templates/ios/App/AngularLynxCanvas2DContext.m +2467 -0
  73. package/templates/ios/App/AngularLynxCanvas2DOps.h +61 -0
  74. package/templates/ios/App/AngularLynxVideo.h +37 -0
  75. package/templates/ios/App/AngularLynxVideo.m +435 -0
  76. package/templates/ios/App/AngularLynxWebGL.h +69 -7
  77. package/templates/ios/App/AngularLynxWebGL.m +538 -29
  78. package/templates/ios/App/AngularLynxWebGLContext.m +148 -46
  79. package/templates/ios/App/AppDelegate.swift +34 -2
  80. package/templates/ios/App/AppleSignInButton.swift +106 -0
  81. package/templates/ios/App/AppleSignInModule.swift +222 -0
  82. package/templates/ios/App/BarcodeScannerModule.swift +284 -0
  83. package/templates/ios/App/BiometricsModule.swift +256 -0
  84. package/templates/ios/App/BluetoothModule.swift +1086 -0
  85. package/templates/ios/App/CalendarModule.swift +158 -0
  86. package/templates/ios/App/FilePickerModule.swift +240 -0
  87. package/templates/ios/App/HapticsModule.swift +90 -0
  88. package/templates/ios/App/Info.plist +2 -0
  89. package/templates/ios/App/LynxViewController.swift +210 -6
  90. package/templates/ios/App/MotionModule.swift +430 -0
  91. package/templates/ios/App/NfcModule.swift +346 -0
  92. package/templates/ios/App/PhotoLibraryModule.swift +169 -0
  93. package/templates/ios/App/ShareTargetModule.swift +150 -0
  94. package/templates/ios/App/ShellModule.swift +17 -0
  95. package/templates/ios/App/StoreReviewModule.swift +61 -0
  96. package/templates/ios/App/TrackingModule.swift +121 -0
  97. package/templates/ios/App/WifiModule.swift +237 -0
  98. package/templates/ios/App/XElementRegistry.swift +13 -3
  99. package/templates/ios/ShareExtension/Info.plist +42 -0
  100. package/templates/ios/ShareExtension/ShareViewController.swift +387 -0
@@ -1,5 +1,7 @@
1
1
  #import "AngularLynxWebGL.h"
2
2
 
3
+ #import "AngularLynxVideo.h"
4
+
3
5
  #import <ImageIO/ImageIO.h>
4
6
  #import <Lynx/LynxPropsProcessor.h>
5
7
  #import <QuartzCore/QuartzCore.h>
@@ -18,7 +20,7 @@ static NSMapTable<NSString *, AngularLynxCanvasTarget *> *targets(void) {
18
20
  return table;
19
21
  }
20
22
 
21
- static NSMapTable<NSString *, AngularLynxWebGLContext *> *contexts(void) {
23
+ static NSMapTable<NSString *, id<AngularLynxCanvasContext>> *contexts(void) {
22
24
  static NSMapTable *table;
23
25
  static dispatch_once_t once;
24
26
  dispatch_once(&once, ^{
@@ -40,7 +42,7 @@ static NSMapTable<NSString *, AngularLynxWebGLContext *> *contexts(void) {
40
42
  return [targets() objectForKey:canvasId];
41
43
  }
42
44
 
43
- + (void)setContext:(AngularLynxWebGLContext *)context forCanvasId:(NSString *)canvasId {
45
+ + (void)setContext:(id<AngularLynxCanvasContext>)context forCanvasId:(NSString *)canvasId {
44
46
  if (context) {
45
47
  [contexts() setObject:context forKey:canvasId];
46
48
  [context attachTarget:[targets() objectForKey:canvasId]];
@@ -49,7 +51,7 @@ static NSMapTable<NSString *, AngularLynxWebGLContext *> *contexts(void) {
49
51
  }
50
52
  }
51
53
 
52
- + (AngularLynxWebGLContext *)contextForCanvasId:(NSString *)canvasId {
54
+ + (id<AngularLynxCanvasContext>)contextForCanvasId:(NSString *)canvasId {
53
55
  return [contexts() objectForKey:canvasId];
54
56
  }
55
57
 
@@ -108,19 +110,48 @@ NSData *AngularLynxBytesFromWords(NSArray *words) {
108
110
  return data;
109
111
  }
110
112
 
111
- /// Decodes an image file to RGBA bytes, as createImageBitmap's options ask:
113
+ /// RGBA pixels, rows top to bottom, as a PNG or JPEG file in base64:
114
+ /// `{ base64, type }`, or `{ error }`. Every kind of context encodes with
115
+ /// it, so it's here rather than in one of theirs: an app that draws only in
116
+ /// 2D has no AngularLynxWebGLContext.m (issue #847).
117
+ NSDictionary *AngularLynxEncodeImage(NSData *pixels, size_t width, size_t height,
118
+ CGBitmapInfo alphaInfo, NSString *type, double quality) {
119
+ size_t stride = width * 4;
120
+ BOOL jpeg = [type isEqualToString:@"image/jpeg"];
121
+ CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)pixels);
122
+ CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
123
+ CGImageRef image =
124
+ CGImageCreate(width, height, 8, 32, stride, colorSpace,
125
+ alphaInfo | kCGBitmapByteOrder32Big, provider, NULL, false,
126
+ kCGRenderingIntentDefault);
127
+ CGColorSpaceRelease(colorSpace);
128
+ CGDataProviderRelease(provider);
129
+ if (!image) return @{@"error" : @"Could not make an image of the canvas's pixels"};
130
+ NSMutableData *file = [NSMutableData data];
131
+ NSString *fileType = jpeg ? @"public.jpeg" : @"public.png";
132
+ CGImageDestinationRef destination = CGImageDestinationCreateWithData(
133
+ (__bridge CFMutableDataRef)file, (__bridge CFStringRef)fileType, 1, NULL);
134
+ BOOL written = NO;
135
+ if (destination) {
136
+ NSDictionary *properties =
137
+ jpeg ? @{(__bridge NSString *)kCGImageDestinationLossyCompressionQuality : @(quality)}
138
+ : @{};
139
+ CGImageDestinationAddImage(destination, image, (__bridge CFDictionaryRef)properties);
140
+ written = CGImageDestinationFinalize(destination);
141
+ CFRelease(destination);
142
+ }
143
+ CGImageRelease(image);
144
+ if (!written) return @{@"error" : @"Could not encode the canvas's pixels"};
145
+ return @{@"base64" : [file base64EncodedStringWithOptions:0], @"type" : type};
146
+ }
147
+
148
+ /// An image's RGBA bytes, as createImageBitmap's options ask:
112
149
  /// `imageOrientation: 'flipY'` flips it, `premultiplyAlpha: 'premultiply'`
113
150
  /// keeps CoreGraphics' premultiplied pixels (otherwise they're divided back),
114
- /// and `resizeWidth`/`resizeHeight` scale it.
115
- static AngularLynxDecodedImage *decodeImage(NSData *bytes, NSDictionary *options,
116
- NSString **error) {
117
- CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)bytes, NULL);
118
- CGImageRef image = source ? CGImageSourceCreateImageAtIndex(source, 0, NULL) : NULL;
119
- if (source) CFRelease(source);
120
- if (!image) {
121
- *error = @"The data is not an image iOS can decode";
122
- return nil;
123
- }
151
+ /// and `resizeWidth`/`resizeHeight` scale it. A size given alone keeps the
152
+ /// aspect ratio, the other side rounded up, as the HTML spec has it.
153
+ static AngularLynxDecodedImage *pixelsOfImage(CGImageRef image, NSDictionary *options,
154
+ NSString **error) {
124
155
  size_t width = CGImageGetWidth(image);
125
156
  size_t height = CGImageGetHeight(image);
126
157
  NSNumber *resizeWidth = options[@"resizeWidth"];
@@ -129,13 +160,18 @@ static AngularLynxDecodedImage *decodeImage(NSData *bytes, NSDictionary *options
129
160
  size_t w = resizeWidth.unsignedIntegerValue;
130
161
  height = [resizeHeight isKindOfClass:NSNumber.class] && resizeHeight.integerValue > 0
131
162
  ? resizeHeight.unsignedIntegerValue
132
- : MAX(1, (size_t)llround((double)height * w / width));
163
+ : MAX(1, (size_t)ceil((double)height * w / width));
133
164
  width = w;
134
165
  } else if ([resizeHeight isKindOfClass:NSNumber.class] && resizeHeight.integerValue > 0) {
135
166
  size_t h = resizeHeight.unsignedIntegerValue;
136
- width = MAX(1, (size_t)llround((double)width * h / height));
167
+ width = MAX(1, (size_t)ceil((double)width * h / height));
137
168
  height = h;
138
169
  }
170
+ // A resize can name sides up to 2^32 - 1, whose bytes would wrap round.
171
+ if (width > SIZE_MAX / 4 / height) {
172
+ *error = @"The image is too large";
173
+ return nil;
174
+ }
139
175
  NSMutableData *pixels = [NSMutableData dataWithLength:width * height * 4];
140
176
  CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
141
177
  // CoreGraphics draws RGBA only premultiplied.
@@ -144,7 +180,6 @@ static AngularLynxDecodedImage *decodeImage(NSData *bytes, NSDictionary *options
144
180
  kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
145
181
  CGColorSpaceRelease(colorSpace);
146
182
  if (!context) {
147
- CGImageRelease(image);
148
183
  *error = @"Could not allocate the image's pixels";
149
184
  return nil;
150
185
  }
@@ -159,7 +194,6 @@ static AngularLynxDecodedImage *decodeImage(NSData *bytes, NSDictionary *options
159
194
  }
160
195
  CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
161
196
  CGContextRelease(context);
162
- CGImageRelease(image);
163
197
  BOOL premultiply = [options[@"premultiplyAlpha"] isEqual:@"premultiply"];
164
198
  if (!premultiply) {
165
199
  uint8_t *p = pixels.mutableBytes;
@@ -178,10 +212,177 @@ static AngularLynxDecodedImage *decodeImage(NSData *bytes, NSDictionary *options
178
212
  premultiplied:premultiply];
179
213
  }
180
214
 
215
+ /// Decodes an image file to RGBA bytes, with createImageBitmap's options (see
216
+ /// pixelsOfImage).
217
+ static AngularLynxDecodedImage *decodeImage(NSData *bytes, NSDictionary *options,
218
+ NSString **error) {
219
+ CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)bytes, NULL);
220
+ CGImageRef image = source ? CGImageSourceCreateImageAtIndex(source, 0, NULL) : NULL;
221
+ if (source) CFRelease(source);
222
+ if (!image) {
223
+ *error = @"The data is not an image iOS can decode";
224
+ return nil;
225
+ }
226
+ AngularLynxDecodedImage *decoded = pixelsOfImage(image, options, error);
227
+ CGImageRelease(image);
228
+ return decoded;
229
+ }
230
+
231
+ /// An integer option, or 0 when it's missing or isn't a number.
232
+ static long long integerOption(NSDictionary *options, NSString *key) {
233
+ id value = options[key];
234
+ return [value isKindOfClass:NSNumber.class] ? [value longLongValue] : 0;
235
+ }
236
+
237
+ /// Whether createImageBitmap() asked for part of the source (its `sx, sy, sw,
238
+ /// sh`), which the runtime sends as cropX, cropY, cropWidth and cropHeight,
239
+ /// the sizes made positive.
240
+ static BOOL hasCrop(NSDictionary *options) {
241
+ return integerOption(options, @"cropWidth") > 0 && integerOption(options, @"cropHeight") > 0;
242
+ }
243
+
244
+ /// The part of an image createImageBitmap()'s crop rectangle covers, the
245
+ /// rectangle's size: what it covers outside the image is transparent black,
246
+ /// as the HTML spec has it, and the rest keeps its alpha as it was. The
247
+ /// image's rows are copied, so the source is left as it is.
248
+ static AngularLynxDecodedImage *cropImage(AngularLynxDecodedImage *image, NSDictionary *options,
249
+ NSString **error) {
250
+ long long x = integerOption(options, @"cropX");
251
+ long long y = integerOption(options, @"cropY");
252
+ long long width = integerOption(options, @"cropWidth");
253
+ long long height = integerOption(options, @"cropHeight");
254
+ // At most INT32_MAX bytes, Android's limit too (rgbaByteCount): a side can
255
+ // be 2^31 - 1 (a Web IDL long), and a crop of 8 GB, which pixelsOfSnapshot
256
+ // would copy again, can be allocated lazily and then end the app as the
257
+ // rows are written. Checked before it's multiplied, which could wrap round.
258
+ NSMutableData *pixels = (unsigned long long)width <= INT32_MAX / 4 / (unsigned long long)height
259
+ ? [NSMutableData dataWithLength:(NSUInteger)(width * height * 4)]
260
+ : nil;
261
+ if (!pixels) {
262
+ *error = @"The crop rectangle is too large";
263
+ return nil;
264
+ }
265
+ // The columns and rows the rectangle shares with the image.
266
+ long long left = MAX(x, 0);
267
+ long long right = MIN(x + width, (long long)image.width);
268
+ long long top = MAX(y, 0);
269
+ long long bottom = MIN(y + height, (long long)image.height);
270
+ if (left < right && top < bottom) {
271
+ const uint8_t *from = image.pixels.bytes;
272
+ uint8_t *to = pixels.mutableBytes;
273
+ size_t span = (size_t)(right - left) * 4;
274
+ for (long long row = top; row < bottom; row++) {
275
+ memcpy(to + ((row - y) * width + (left - x)) * 4, from + (row * image.width + left) * 4,
276
+ span);
277
+ }
278
+ }
279
+ return [[AngularLynxDecodedImage alloc] initWithPixels:pixels
280
+ width:(NSInteger)width
281
+ height:(NSInteger)height
282
+ premultiplied:image.premultiplied];
283
+ }
284
+
285
+ /// Pixels the shell has (a canvas's snapshot, an image it keeps, an
286
+ /// ImageData's) as createImageBitmap's options ask, for a new ImageBitmap:
287
+ /// cropped first, as the HTML spec orders it, so a resize and a flip apply to
288
+ /// the part the crop rectangle names. Without a resize, the bytes are copied,
289
+ /// flipped and premultiplied or divided as asked, exactly; a resize draws
290
+ /// them at the new size, as a decode does. The source is only read.
291
+ static AngularLynxDecodedImage *pixelsOfSnapshot(AngularLynxDecodedImage *snapshot,
292
+ NSDictionary *options, NSString **error) {
293
+ if (hasCrop(options)) {
294
+ snapshot = cropImage(snapshot, options, error);
295
+ if (!snapshot) return nil;
296
+ }
297
+ NSNumber *resizeWidth = options[@"resizeWidth"];
298
+ NSNumber *resizeHeight = options[@"resizeHeight"];
299
+ BOOL resize = ([resizeWidth isKindOfClass:NSNumber.class] && resizeWidth.integerValue > 0) ||
300
+ ([resizeHeight isKindOfClass:NSNumber.class] && resizeHeight.integerValue > 0);
301
+ if (resize) {
302
+ CGDataProviderRef provider =
303
+ CGDataProviderCreateWithCFData((__bridge CFDataRef)snapshot.pixels);
304
+ CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
305
+ CGImageRef image = CGImageCreate(
306
+ snapshot.width, snapshot.height, 8, 32, snapshot.width * 4, space,
307
+ (snapshot.premultiplied ? kCGImageAlphaPremultipliedLast : kCGImageAlphaLast) |
308
+ kCGBitmapByteOrder32Big,
309
+ provider, NULL, false, kCGRenderingIntentDefault);
310
+ CGColorSpaceRelease(space);
311
+ CGDataProviderRelease(provider);
312
+ if (!image) {
313
+ *error = @"Could not read the canvas's pixels";
314
+ return nil;
315
+ }
316
+ AngularLynxDecodedImage *pixels = pixelsOfImage(image, options, error);
317
+ CGImageRelease(image);
318
+ return pixels;
319
+ }
320
+ NSInteger width = snapshot.width;
321
+ NSInteger height = snapshot.height;
322
+ NSUInteger stride = (NSUInteger)width * 4;
323
+ NSMutableData *pixels = [NSMutableData dataWithLength:stride * height];
324
+ if (!pixels) {
325
+ *error = @"Could not allocate the image's pixels";
326
+ return nil;
327
+ }
328
+ const uint8_t *from = snapshot.pixels.bytes;
329
+ uint8_t *to = pixels.mutableBytes;
330
+ BOOL flipY = [options[@"imageOrientation"] isEqual:@"flipY"];
331
+ for (NSInteger y = 0; y < height; y++) {
332
+ memcpy(to + (NSUInteger)y * stride, from + (NSUInteger)(flipY ? height - 1 - y : y) * stride,
333
+ stride);
334
+ }
335
+ BOOL premultiply = [options[@"premultiplyAlpha"] isEqual:@"premultiply"];
336
+ if (premultiply != snapshot.premultiplied) {
337
+ for (NSUInteger i = 0; i < stride * height; i += 4) {
338
+ uint8_t alpha = to[i + 3];
339
+ if (alpha == 255) continue;
340
+ for (NSUInteger c = 0; c < 3; c++) {
341
+ if (premultiply) {
342
+ to[i + c] = (uint8_t)((to[i + c] * alpha + 127) / 255);
343
+ } else if (alpha != 0) {
344
+ to[i + c] = (uint8_t)MIN(255, (to[i + c] * 255 + alpha / 2) / alpha);
345
+ }
346
+ }
347
+ }
348
+ }
349
+ return [[AngularLynxDecodedImage alloc] initWithPixels:pixels
350
+ width:width
351
+ height:height
352
+ premultiplied:premultiply];
353
+ }
354
+
355
+ /// Keeps a new ImageBitmap's pixels under the id the runtime gave it, and
356
+ /// answers its size and, when it was asked for, that the crop was applied: a
357
+ /// shell from before cropping leaves `cropped` out, so the runtime can tell
358
+ /// it would get all of the source. Or answers why there's no image.
359
+ static NSDictionary *keptImage(NSNumber *imageId, AngularLynxDecodedImage *image,
360
+ NSDictionary *options, NSString *error, NSString *fallback) {
361
+ if (!image) return @{@"width" : @0, @"height" : @0, @"error" : error ?: fallback};
362
+ decodedImages()[imageId] = image;
363
+ return @{
364
+ @"width" : @(image.width),
365
+ @"height" : @(image.height),
366
+ @"cropped" : @(hasCrop(options)),
367
+ };
368
+ }
369
+
181
370
  #pragma mark - Module
182
371
 
372
+ // The WebGL2 context's lines, between the `angular-lynx:webgl` markers, go
373
+ // with AngularLynxWebGLContext.m, AngularLynxWebGLOps.h and ANGLE from an app
374
+ // that draws on a canvas only in 2D (@blotch/angular-lynx-native's
375
+ // features.ts): the runtime then has no WebGL2 context to call them, and
376
+ // nothing else names the class (issue #847).
183
377
  @implementation AngularLynxWebGLModule {
378
+ // angular-lynx:webgl
184
379
  NSMutableDictionary<NSNumber *, AngularLynxWebGLContext *> *_contexts;
380
+ // /angular-lynx:webgl
381
+ NSMutableDictionary<NSNumber *, AngularLynxCanvas2DContext *> *_contexts2D;
382
+ NSMutableDictionary<NSNumber *, AngularLynxBitmapRendererContext *> *_bitmapRenderers;
383
+ /// The 2D contexts' gradients and patterns, which any of them can paint
384
+ /// with.
385
+ NSMutableDictionary<NSNumber *, id> *_canvas2DObjects;
185
386
  }
186
387
 
187
388
  + (NSString *)name {
@@ -190,18 +391,40 @@ static AngularLynxDecodedImage *decodeImage(NSData *bytes, NSDictionary *options
190
391
 
191
392
  + (NSDictionary<NSString *, NSString *> *)methodLookup {
192
393
  return @{
394
+ // angular-lynx:webgl
193
395
  @"create" : NSStringFromSelector(@selector(create:canvasId:attributes:)),
194
396
  @"run" : NSStringFromSelector(@selector(run:commands:)),
195
397
  @"destroy" : NSStringFromSelector(@selector(destroy:)),
398
+ @"encodeImage" : NSStringFromSelector(@selector(encodeImage:options:)),
399
+ // /angular-lynx:webgl
196
400
  @"decodeImage" : NSStringFromSelector(@selector(decodeImage:bytes:options:)),
197
401
  @"releaseImage" : NSStringFromSelector(@selector(releaseImage:)),
198
- @"encodeImage" : NSStringFromSelector(@selector(encodeImage:options:)),
402
+ @"imageFromCanvas" : NSStringFromSelector(@selector(imageFromCanvas:canvasId:options:)),
403
+ @"imageFromImage" : NSStringFromSelector(@selector(imageFromImage:sourceId:options:)),
404
+ @"imageFromPixels" : NSStringFromSelector(@selector(imageFromPixels:bytes:options:)),
405
+ @"create2D" : NSStringFromSelector(@selector(create2D:canvasId:attributes:)),
406
+ @"run2D" : NSStringFromSelector(@selector(run2D:commands:)),
407
+ @"destroy2D" : NSStringFromSelector(@selector(destroy2D:)),
408
+ @"encode2D" : NSStringFromSelector(@selector(encode2D:options:)),
409
+ @"release2D" : NSStringFromSelector(@selector(release2D:)),
410
+ @"videoSize" : NSStringFromSelector(@selector(videoSize:)),
411
+ @"createBitmapRenderer" :
412
+ NSStringFromSelector(@selector(createBitmapRenderer:canvasId:attributes:)),
413
+ @"transferToBitmapRenderer" :
414
+ NSStringFromSelector(@selector(transferToBitmapRenderer:imageId:width:height:)),
415
+ @"destroyBitmapRenderer" : NSStringFromSelector(@selector(destroyBitmapRenderer:)),
416
+ @"encodeBitmapRenderer" : NSStringFromSelector(@selector(encodeBitmapRenderer:options:)),
199
417
  };
200
418
  }
201
419
 
202
420
  - (instancetype)init {
203
421
  if ((self = [super init])) {
422
+ // angular-lynx:webgl
204
423
  _contexts = [NSMutableDictionary dictionary];
424
+ // /angular-lynx:webgl
425
+ _contexts2D = [NSMutableDictionary dictionary];
426
+ _bitmapRenderers = [NSMutableDictionary dictionary];
427
+ _canvas2DObjects = [NSMutableDictionary dictionary];
205
428
  }
206
429
  return self;
207
430
  }
@@ -211,11 +434,20 @@ static AngularLynxDecodedImage *decodeImage(NSData *bytes, NSDictionary *options
211
434
  }
212
435
 
213
436
  - (void)dealloc {
437
+ // angular-lynx:webgl
214
438
  for (AngularLynxWebGLContext *context in _contexts.allValues) {
215
439
  [context destroy];
216
440
  }
441
+ // /angular-lynx:webgl
442
+ for (AngularLynxCanvas2DContext *context in _contexts2D.allValues) {
443
+ [AngularLynxCanvasRegistry setContext:nil forCanvasId:context.canvasId];
444
+ }
445
+ for (AngularLynxBitmapRendererContext *context in _bitmapRenderers.allValues) {
446
+ [AngularLynxCanvasRegistry setContext:nil forCanvasId:context.canvasId];
447
+ }
217
448
  }
218
449
 
450
+ // angular-lynx:webgl
219
451
  - (NSDictionary *)create:(NSNumber *)contextId
220
452
  canvasId:(NSString *)canvasId
221
453
  attributes:(NSDictionary *)attributes {
@@ -248,21 +480,41 @@ static AngularLynxDecodedImage *decodeImage(NSData *bytes, NSDictionary *options
248
480
  }
249
481
  return @YES;
250
482
  }
483
+ // /angular-lynx:webgl
484
+
485
+ /// The size of the frame a `<video>` shows (AngularLynxVideo.m), by its
486
+ /// `video-id`: the runtime's videoWidth and videoHeight, and the size a
487
+ /// canvas reads it at. Zero before its first frame.
488
+ - (NSDictionary *)videoSize:(NSString *)videoId {
489
+ CGSize size = CGSizeZero;
490
+ if ([videoId isKindOfClass:NSString.class]) {
491
+ id context = [AngularLynxCanvasRegistry contextForCanvasId:videoId];
492
+ if ([context isKindOfClass:AngularLynxVideoFrames.class]) {
493
+ size = [(AngularLynxVideoFrames *)context frameSize];
494
+ }
495
+ }
496
+ return @{@"width" : @(size.width), @"height" : @(size.height)};
497
+ }
251
498
 
252
499
  - (NSDictionary *)decodeImage:(NSNumber *)imageId
253
500
  bytes:(NSArray *)bytes
254
501
  options:(NSDictionary *)options {
502
+ if (![options isKindOfClass:NSDictionary.class]) options = @{};
255
503
  NSString *error = nil;
256
- AngularLynxDecodedImage *image =
257
- [bytes isKindOfClass:NSArray.class]
258
- ? decodeImage(AngularLynxBytesFromWords(bytes),
259
- [options isKindOfClass:NSDictionary.class] ? options : @{}, &error)
260
- : nil;
261
- if (!image) {
262
- return @{@"width" : @0, @"height" : @0, @"error" : error ?: @"No image data"};
504
+ AngularLynxDecodedImage *image = nil;
505
+ if ([bytes isKindOfClass:NSArray.class]) {
506
+ if (hasCrop(options)) {
507
+ // Cropped before it's resized or flipped, as the HTML spec orders it:
508
+ // the file is decoded as it is, with the alpha the bitmap asks for, so
509
+ // the copy needn't convert it, and then cut.
510
+ NSDictionary *plain = @{@"premultiplyAlpha" : options[@"premultiplyAlpha"] ?: @"default"};
511
+ image = decodeImage(AngularLynxBytesFromWords(bytes), plain, &error);
512
+ if (image) image = pixelsOfSnapshot(image, options, &error);
513
+ } else {
514
+ image = decodeImage(AngularLynxBytesFromWords(bytes), options, &error);
515
+ }
263
516
  }
264
- decodedImages()[imageId] = image;
265
- return @{@"width" : @(image.width), @"height" : @(image.height)};
517
+ return keptImage(imageId, image, options, error, @"No image data");
266
518
  }
267
519
 
268
520
  - (NSNumber *)releaseImage:(NSNumber *)imageId {
@@ -270,6 +522,105 @@ static AngularLynxDecodedImage *decodeImage(NSData *bytes, NSDictionary *options
270
522
  return @YES;
271
523
  }
272
524
 
525
+ /// An ImageBitmap of a canvas, for createImageBitmap() and
526
+ /// OffscreenCanvas.transferToImageBitmap(): a copy of the canvas's pixels
527
+ /// now, kept under `imageId` as a decoded image is, so texture uploads and
528
+ /// drawImage read it as they read one. The pixels never cross the bridge.
529
+ /// `options` holds createImageBitmap's options, the canvas's size (a canvas
530
+ /// with no context is transparent black, as in a browser) and `transfer`,
531
+ /// which clears the canvas after it's read. `{ width, height }`, or
532
+ /// `{ error }`.
533
+ - (NSDictionary *)imageFromCanvas:(NSNumber *)imageId
534
+ canvasId:(NSString *)canvasId
535
+ options:(NSDictionary *)options {
536
+ if (![options isKindOfClass:NSDictionary.class]) options = @{};
537
+ id<AngularLynxCanvasContext> context =
538
+ [canvasId isKindOfClass:NSString.class]
539
+ ? [AngularLynxCanvasRegistry contextForCanvasId:canvasId]
540
+ : nil;
541
+ // A WebGL context's snapshot makes it current; the next batch of any
542
+ // context makes its own current again (see AngularLynxWebGLContext run:).
543
+ AngularLynxDecodedImage *snapshot = [context snapshot];
544
+ // A context that has pixels but couldn't read them is a failure, not a
545
+ // blank canvas.
546
+ if (context && !snapshot) {
547
+ return @{@"width" : @0, @"height" : @0, @"error" : @"The canvas's pixels could not be read"};
548
+ }
549
+ if (!snapshot) {
550
+ NSInteger width = [options[@"width"] isKindOfClass:NSNumber.class]
551
+ ? [options[@"width"] integerValue]
552
+ : 0;
553
+ NSInteger height = [options[@"height"] isKindOfClass:NSNumber.class]
554
+ ? [options[@"height"] integerValue]
555
+ : 0;
556
+ if (width <= 0 || height <= 0) {
557
+ return @{@"width" : @0, @"height" : @0, @"error" : @"The canvas has no pixels"};
558
+ }
559
+ // Checked before it's multiplied: two sides near 2^32 would wrap round
560
+ // to a small count, and the copy would read past it.
561
+ NSMutableData *blank =
562
+ (NSUInteger)width <= NSIntegerMax / 4 / (NSUInteger)height
563
+ ? [NSMutableData dataWithLength:(NSUInteger)width * height * 4]
564
+ : nil;
565
+ if (!blank) {
566
+ return @{@"width" : @0, @"height" : @0, @"error" : @"The canvas is too large to copy"};
567
+ }
568
+ snapshot = [[AngularLynxDecodedImage alloc] initWithPixels:blank
569
+ width:width
570
+ height:height
571
+ premultiplied:YES];
572
+ }
573
+ if ([options[@"transfer"] isEqual:@YES]) [context clearForTransfer];
574
+ NSString *error = nil;
575
+ AngularLynxDecodedImage *image = pixelsOfSnapshot(snapshot, options, &error);
576
+ return keptImage(imageId, image, options, error, @"The canvas could not be read");
577
+ }
578
+
579
+ /// An ImageBitmap of an image the module keeps (an ImageBitmap's, or an
580
+ /// `<img>`'s), for createImageBitmap(): a copy of its pixels with
581
+ /// createImageBitmap's options, so neither the pixels nor the file cross the
582
+ /// bridge again. `{ width, height, cropped }`, or `{ error }`.
583
+ - (NSDictionary *)imageFromImage:(NSNumber *)imageId
584
+ sourceId:(NSNumber *)sourceId
585
+ options:(NSDictionary *)options {
586
+ if (![options isKindOfClass:NSDictionary.class]) options = @{};
587
+ AngularLynxDecodedImage *source =
588
+ [sourceId isKindOfClass:NSNumber.class] ? decodedImages()[sourceId] : nil;
589
+ if (!source) return @{@"width" : @0, @"height" : @0, @"error" : @"The image is gone"};
590
+ NSString *error = nil;
591
+ AngularLynxDecodedImage *image = pixelsOfSnapshot(source, options, &error);
592
+ return keptImage(imageId, image, options, error, @"The image could not be copied");
593
+ }
594
+
595
+ /// An ImageBitmap of an ImageData, for createImageBitmap(): its RGBA bytes,
596
+ /// straight, rows top to bottom, sent as words (see AngularLynxBytesFromWords),
597
+ /// with its size in `options`, and createImageBitmap's options applied.
598
+ /// `{ width, height, cropped }`, or `{ error }`.
599
+ - (NSDictionary *)imageFromPixels:(NSNumber *)imageId
600
+ bytes:(NSArray *)bytes
601
+ options:(NSDictionary *)options {
602
+ if (![options isKindOfClass:NSDictionary.class]) options = @{};
603
+ long long width = integerOption(options, @"width");
604
+ long long height = integerOption(options, @"height");
605
+ NSData *pixels = [bytes isKindOfClass:NSArray.class] ? AngularLynxBytesFromWords(bytes) : nil;
606
+ // Divided rather than multiplied, so two big sides can't wrap round to the
607
+ // byte count.
608
+ if (width <= 0 || height <= 0 || pixels.length == 0 || pixels.length % 4 ||
609
+ pixels.length / 4 % (unsigned long long)width ||
610
+ pixels.length / 4 / (unsigned long long)width != (unsigned long long)height) {
611
+ return @{@"width" : @0, @"height" : @0, @"error" : @"The pixels don't fill the image"};
612
+ }
613
+ AngularLynxDecodedImage *source =
614
+ [[AngularLynxDecodedImage alloc] initWithPixels:pixels
615
+ width:(NSInteger)width
616
+ height:(NSInteger)height
617
+ premultiplied:NO];
618
+ NSString *error = nil;
619
+ AngularLynxDecodedImage *image = pixelsOfSnapshot(source, options, &error);
620
+ return keptImage(imageId, image, options, error, @"The pixels could not be copied");
621
+ }
622
+
623
+ // angular-lynx:webgl
273
624
  /// The drawing buffer as an image file, for canvas.toBlob() and toDataURL():
274
625
  /// `options` holds the MIME type (image/jpeg, or image/png for anything
275
626
  /// else, as browsers fall back), the JPEG quality and premultipliedAlpha.
@@ -284,6 +635,133 @@ static AngularLynxDecodedImage *decodeImage(NSData *bytes, NSDictionary *options
284
635
  quality:[quality isKindOfClass:NSNumber.class] ? quality.doubleValue : 0.92
285
636
  premultiplied:![options[@"premultipliedAlpha"] isEqual:@NO]];
286
637
  }
638
+ // /angular-lynx:webgl
639
+
640
+ #pragma mark - 2D
641
+
642
+ /// A 2D context for the canvas: `{ ok }`, or `{ ok: false, error }`.
643
+ - (NSDictionary *)create2D:(NSNumber *)contextId
644
+ canvasId:(NSString *)canvasId
645
+ attributes:(NSDictionary *)attributes {
646
+ [self destroy2D:contextId];
647
+ NSString *error = nil;
648
+ AngularLynxCanvas2DContext *context = [[AngularLynxCanvas2DContext alloc]
649
+ initWithCanvasId:canvasId
650
+ attributes:[attributes isKindOfClass:NSDictionary.class] ? attributes : @{}
651
+ objects:_canvas2DObjects
652
+ error:&error];
653
+ if (!context) return @{@"ok" : @NO, @"error" : error ?: @"Could not make the canvas's bitmap"};
654
+ _contexts2D[contextId] = context;
655
+ [AngularLynxCanvasRegistry setContext:context forCanvasId:canvasId];
656
+ return @{@"ok" : @YES};
657
+ }
658
+
659
+ - (NSArray *)run2D:(NSNumber *)contextId commands:(NSArray *)commands {
660
+ AngularLynxCanvas2DContext *context = _contexts2D[contextId];
661
+ if (!context || ![commands isKindOfClass:NSArray.class]) return @[];
662
+ return [context run:commands];
663
+ }
664
+
665
+ - (NSNumber *)destroy2D:(NSNumber *)contextId {
666
+ AngularLynxCanvas2DContext *context = _contexts2D[contextId];
667
+ if (context) {
668
+ if ([AngularLynxCanvasRegistry contextForCanvasId:context.canvasId] == context) {
669
+ [AngularLynxCanvasRegistry setContext:nil forCanvasId:context.canvasId];
670
+ }
671
+ [context attachTarget:nil];
672
+ [_contexts2D removeObjectForKey:contextId];
673
+ }
674
+ return @YES;
675
+ }
676
+
677
+ /// Frees a gradient or pattern, which every 2D context in the LynxView
678
+ /// shares, whether or not the context that made it is still there.
679
+ - (NSNumber *)release2D:(NSNumber *)objectId {
680
+ if ([objectId isKindOfClass:NSNumber.class]) [_canvas2DObjects removeObjectForKey:objectId];
681
+ return @YES;
682
+ }
683
+
684
+ /// The bitmap as an image file, for canvas.toBlob() and toDataURL().
685
+ - (NSDictionary *)encode2D:(NSNumber *)contextId options:(NSDictionary *)options {
686
+ AngularLynxCanvas2DContext *context = _contexts2D[contextId];
687
+ if (!context) return @{@"error" : @"The canvas has no 2D context"};
688
+ if (![options isKindOfClass:NSDictionary.class]) options = @{};
689
+ NSString *type = [options[@"type"] isEqual:@"image/jpeg"] ? @"image/jpeg" : @"image/png";
690
+ NSNumber *quality = options[@"quality"];
691
+ return [context encodeImageOfType:type
692
+ quality:[quality isKindOfClass:NSNumber.class] ? quality.doubleValue
693
+ : 0.92];
694
+ }
695
+
696
+ #pragma mark - Bitmap renderer
697
+
698
+ /// A bitmaprenderer context for the canvas, showing a blank canvas of the
699
+ /// `width` and `height` in `attributes`: `{ ok }`.
700
+ - (NSDictionary *)createBitmapRenderer:(NSNumber *)contextId
701
+ canvasId:(NSString *)canvasId
702
+ attributes:(NSDictionary *)attributes {
703
+ [self destroyBitmapRenderer:contextId];
704
+ if (![canvasId isKindOfClass:NSString.class]) {
705
+ return @{@"ok" : @NO, @"error" : @"The canvas has no id"};
706
+ }
707
+ AngularLynxBitmapRendererContext *context = [[AngularLynxBitmapRendererContext alloc]
708
+ initWithCanvasId:canvasId
709
+ attributes:[attributes isKindOfClass:NSDictionary.class] ? attributes : @{}];
710
+ _bitmapRenderers[contextId] = context;
711
+ [AngularLynxCanvasRegistry setContext:context forCanvasId:canvasId];
712
+ return @{@"ok" : @YES};
713
+ }
714
+
715
+ /// transferFromImageBitmap(): shows the decoded image `imageId` in the
716
+ /// canvas, which owns it from then on, or a blank canvas of `width` by
717
+ /// `height` for an id of 0. The image leaves the module's images even when
718
+ /// the context has gone, as the runtime's bitmap is detached either way and
719
+ /// nothing else would free it. `{ ok }`, or `{ ok: false, error }`.
720
+ - (NSDictionary *)transferToBitmapRenderer:(NSNumber *)contextId
721
+ imageId:(NSNumber *)imageId
722
+ width:(NSNumber *)width
723
+ height:(NSNumber *)height {
724
+ AngularLynxDecodedImage *image = nil;
725
+ if ([imageId isKindOfClass:NSNumber.class] && imageId.integerValue != 0) {
726
+ image = decodedImages()[imageId];
727
+ [decodedImages() removeObjectForKey:imageId];
728
+ if (!image) return @{@"ok" : @NO, @"error" : @"The ImageBitmap's pixels are gone"};
729
+ }
730
+ AngularLynxBitmapRendererContext *context = _bitmapRenderers[contextId];
731
+ if (!context) return @{@"ok" : @NO, @"error" : @"The canvas has no bitmaprenderer context"};
732
+ if (image) {
733
+ [context showImage:image];
734
+ } else {
735
+ [context showBlankOfWidth:[width isKindOfClass:NSNumber.class] ? width.integerValue : 0
736
+ height:[height isKindOfClass:NSNumber.class] ? height.integerValue : 0];
737
+ }
738
+ return @{@"ok" : @YES};
739
+ }
740
+
741
+ - (NSNumber *)destroyBitmapRenderer:(NSNumber *)contextId {
742
+ AngularLynxBitmapRendererContext *context = _bitmapRenderers[contextId];
743
+ if (context) {
744
+ if ([AngularLynxCanvasRegistry contextForCanvasId:context.canvasId] == context) {
745
+ [AngularLynxCanvasRegistry setContext:nil forCanvasId:context.canvasId];
746
+ }
747
+ [context attachTarget:nil];
748
+ [_bitmapRenderers removeObjectForKey:contextId];
749
+ }
750
+ return @YES;
751
+ }
752
+
753
+ /// What the canvas shows as an image file, for canvas.toBlob() and
754
+ /// toDataURL().
755
+ - (NSDictionary *)encodeBitmapRenderer:(NSNumber *)contextId options:(NSDictionary *)options {
756
+ AngularLynxBitmapRendererContext *context = _bitmapRenderers[contextId];
757
+ if (!context) return @{@"error" : @"The canvas has no bitmaprenderer context"};
758
+ if (![options isKindOfClass:NSDictionary.class]) options = @{};
759
+ NSString *type = [options[@"type"] isEqual:@"image/jpeg"] ? @"image/jpeg" : @"image/png";
760
+ NSNumber *quality = options[@"quality"];
761
+ return [context encodeImageOfType:type
762
+ quality:[quality isKindOfClass:NSNumber.class] ? quality.doubleValue
763
+ : 0.92];
764
+ }
287
765
 
288
766
  @end
289
767
 
@@ -301,7 +779,11 @@ static AngularLynxDecodedImage *decodeImage(NSData *bytes, NSDictionary *options
301
779
 
302
780
  @end
303
781
 
304
- @implementation AngularLynxCanvasView
782
+ @implementation AngularLynxCanvasView {
783
+ /// Shows a 2D context's bitmap. Made the first time one is shown, over the
784
+ /// Metal layer a WebGL context draws in, which a 2D canvas leaves empty.
785
+ CALayer *_imageLayer;
786
+ }
305
787
 
306
788
  + (Class)layerClass {
307
789
  return [CAMetalLayer class];
@@ -336,6 +818,12 @@ static AngularLynxDecodedImage *decodeImage(NSData *bytes, NSDictionary *options
336
818
  // again; one drawing every frame never noticed.
337
819
  - (void)layoutSubviews {
338
820
  [super layoutSubviews];
821
+ if (_imageLayer) {
822
+ [CATransaction begin];
823
+ [CATransaction setDisableActions:YES];
824
+ _imageLayer.frame = self.layer.bounds;
825
+ [CATransaction commit];
826
+ }
339
827
  // ANGLE sizes the layer's next drawable from its bounds and scale, as here.
340
828
  CGSize size = self.bounds.size;
341
829
  CGFloat scale = self.layer.contentsScale;
@@ -348,8 +836,29 @@ static AngularLynxDecodedImage *decodeImage(NSData *bytes, NSDictionary *options
348
836
  }];
349
837
  }
350
838
 
839
+ - (void)showImage:(CGImageRef)image opaque:(BOOL)opaque canvasId:(NSString *)canvasId {
840
+ // A frame queued before the view was given another canvas isn't its.
841
+ if (![canvasId isEqualToString:_canvasId]) return;
842
+ if (!_imageLayer) {
843
+ if (!image) return;
844
+ _imageLayer = [CALayer layer];
845
+ // Scaled to the view, as a canvas's bitmap is to its CSS size, smoothly.
846
+ _imageLayer.contentsGravity = kCAGravityResize;
847
+ _imageLayer.minificationFilter = kCAFilterTrilinear;
848
+ _imageLayer.frame = self.layer.bounds;
849
+ [self.layer addSublayer:_imageLayer];
850
+ }
851
+ [CATransaction begin];
852
+ [CATransaction setDisableActions:YES];
853
+ _imageLayer.contents = (__bridge id)image;
854
+ _imageLayer.opaque = opaque;
855
+ [CATransaction commit];
856
+ }
857
+
351
858
  - (void)setCanvasId:(NSString *)canvasId {
352
859
  if ([_canvasId isEqualToString:canvasId]) return;
860
+ // Another canvas starts blank: the last one's bitmap isn't its.
861
+ _imageLayer.contents = nil;
353
862
  NSString *old = _canvasId;
354
863
  _canvasId = [canvasId copy];
355
864
  NSString *new = _canvasId;