@leafer-ui/miniapp 1.0.0-beta.9 → 1.0.0-rc.11

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.
@@ -0,0 +1,2729 @@
1
+ import { LeaferCanvasBase, Platform, canvasPatch, DataHelper, canvasSizeAttrs, ResizeEvent, Creator, LeaferImage, FileHelper, LeafList, RenderEvent, ChildEvent, WatchEvent, PropertyEvent, LeafHelper, BranchHelper, Bounds, LeafBoundsHelper, Debug, LeafLevelList, LayoutEvent, Run, ImageManager, AnimateEvent, BoundsHelper, Answer, MatrixHelper, ImageEvent, PointHelper, Direction4, TwoPointBoundsHelper, TaskProcessor, Matrix } from '@leafer/core';
2
+ export * from '@leafer/core';
3
+ export { LeaferImage } from '@leafer/core';
4
+ import { InteractionHelper, InteractionBase, HitCanvasManager, Platform as Platform$1 } from '@leafer-ui/core';
5
+ export * from '@leafer-ui/core';
6
+ import { PaintImage, ColorConvert, PaintGradient, Export, Group, TextConvert, Paint, Effect } from '@leafer-ui/draw';
7
+
8
+ class LeaferCanvas extends LeaferCanvasBase {
9
+ get allowBackgroundColor() { return false; }
10
+ init() {
11
+ let { view } = this.config;
12
+ if (view) {
13
+ if (typeof view === 'string') {
14
+ if (view[0] !== '#')
15
+ view = '#' + view;
16
+ this.viewSelect = Platform.miniapp.select(view);
17
+ }
18
+ else if (view.fields) {
19
+ this.viewSelect = view;
20
+ }
21
+ else {
22
+ this.initView(view);
23
+ }
24
+ if (this.viewSelect)
25
+ Platform.miniapp.getSizeView(this.viewSelect).then(sizeView => {
26
+ this.initView(sizeView);
27
+ });
28
+ }
29
+ else {
30
+ this.initView();
31
+ }
32
+ }
33
+ initView(view) {
34
+ if (!view) {
35
+ view = {};
36
+ this.__createView();
37
+ }
38
+ else {
39
+ this.view = view.view || view;
40
+ }
41
+ this.__createContext();
42
+ const { width, height, pixelRatio } = this.config;
43
+ const size = { width: width || view.width, height: height || view.height, pixelRatio };
44
+ this.resize(size);
45
+ if (this.context.roundRect) {
46
+ this.roundRect = function (x, y, width, height, radius) {
47
+ this.context.roundRect(x, y, width, height, typeof radius === 'number' ? [radius] : radius);
48
+ };
49
+ }
50
+ canvasPatch(this.context.__proto__);
51
+ }
52
+ __createView() {
53
+ this.view = Platform.origin.createCanvas(1, 1);
54
+ }
55
+ updateViewSize() {
56
+ const { width, height, pixelRatio } = this;
57
+ this.view.width = Math.ceil(width * pixelRatio);
58
+ this.view.height = Math.ceil(height * pixelRatio);
59
+ }
60
+ updateClientBounds(callback) {
61
+ if (this.viewSelect)
62
+ Platform.miniapp.getBounds(this.viewSelect).then(bounds => {
63
+ this.clientBounds = bounds;
64
+ if (callback)
65
+ callback();
66
+ });
67
+ }
68
+ startAutoLayout(_autoBounds, listener) {
69
+ this.resizeListener = listener;
70
+ this.checkSize = this.checkSize.bind(this);
71
+ Platform.miniapp.onWindowResize(this.checkSize);
72
+ }
73
+ checkSize() {
74
+ if (this.viewSelect) {
75
+ setTimeout(() => {
76
+ this.updateClientBounds(() => {
77
+ const { width, height } = this.clientBounds;
78
+ const { pixelRatio } = this;
79
+ const size = { width, height, pixelRatio };
80
+ if (!this.isSameSize(size)) {
81
+ const oldSize = {};
82
+ DataHelper.copyAttrs(oldSize, this, canvasSizeAttrs);
83
+ this.resize(size);
84
+ if (this.width !== undefined)
85
+ this.resizeListener(new ResizeEvent(size, oldSize));
86
+ }
87
+ });
88
+ }, 500);
89
+ }
90
+ }
91
+ stopAutoLayout() {
92
+ this.autoLayout = false;
93
+ this.resizeListener = null;
94
+ Platform.miniapp.offWindowResize(this.checkSize);
95
+ }
96
+ }
97
+
98
+ const { mineType, fileType } = FileHelper;
99
+ Object.assign(Creator, {
100
+ canvas: (options, manager) => new LeaferCanvas(options, manager),
101
+ image: (options) => new LeaferImage(options)
102
+ });
103
+ function useCanvas(_canvasType, app) {
104
+ if (!Platform.origin) {
105
+ Platform.origin = {
106
+ createCanvas: (width, height, _format) => app.createOffscreenCanvas({ type: '2d', width, height }),
107
+ canvasToDataURL: (canvas, type, quality) => canvas.toDataURL(mineType(type), quality),
108
+ canvasToBolb: (canvas, type, quality) => canvas.toBuffer(type, { quality }),
109
+ canvasSaveAs: (canvas, filePath, quality) => {
110
+ return new Promise((resolve, reject) => {
111
+ let data = canvas.toDataURL(mineType(fileType(filePath)), quality);
112
+ data = data.substring(data.indexOf('64,') + 3);
113
+ let toAlbum;
114
+ if (!filePath.includes('/')) {
115
+ filePath = `${app.env.USER_DATA_PATH}/` + filePath;
116
+ toAlbum = true;
117
+ }
118
+ const fs = app.getFileSystemManager();
119
+ fs.writeFile({
120
+ filePath,
121
+ data,
122
+ encoding: 'base64',
123
+ success() {
124
+ if (toAlbum) {
125
+ Platform.miniapp.saveToAlbum(filePath).then(() => {
126
+ fs.unlink({ filePath });
127
+ });
128
+ }
129
+ resolve();
130
+ },
131
+ fail(error) {
132
+ reject(error);
133
+ }
134
+ });
135
+ });
136
+ },
137
+ loadImage(url) {
138
+ return new Promise((resolve, reject) => {
139
+ const img = Platform.canvas.view.createImage();
140
+ img.onload = () => { resolve(img); };
141
+ img.onerror = (error) => { reject(error); };
142
+ img.src = url;
143
+ });
144
+ },
145
+ noRepeat: 'repeat-x'
146
+ };
147
+ Platform.miniapp = {
148
+ select(name) {
149
+ return app.createSelectorQuery().select(name);
150
+ },
151
+ getBounds(select) {
152
+ return new Promise((resolve) => {
153
+ select.boundingClientRect().exec((res) => {
154
+ const rect = res[1];
155
+ resolve({ x: rect.top, y: rect.left, width: rect.width, height: rect.height });
156
+ });
157
+ });
158
+ },
159
+ getSizeView(select) {
160
+ return new Promise((resolve) => {
161
+ select.fields({ node: true, size: true }).exec((res) => {
162
+ const data = res[0];
163
+ resolve({ view: data.node, width: data.width, height: data.height });
164
+ });
165
+ });
166
+ },
167
+ saveToAlbum(path) {
168
+ return new Promise((resolve) => {
169
+ app.getSetting({
170
+ success: (res) => {
171
+ if (res.authSetting['scope.writePhotosAlbum']) {
172
+ app.saveImageToPhotosAlbum({
173
+ filePath: path,
174
+ success() { resolve(true); }
175
+ });
176
+ }
177
+ else {
178
+ app.authorize({
179
+ scope: 'scope.writePhotosAlbum',
180
+ success: () => {
181
+ app.saveImageToPhotosAlbum({
182
+ filePath: path,
183
+ success() { resolve(true); }
184
+ });
185
+ },
186
+ fail: () => { }
187
+ });
188
+ }
189
+ }
190
+ });
191
+ });
192
+ },
193
+ onWindowResize(fun) {
194
+ app.onWindowResize(fun);
195
+ },
196
+ offWindowResize(fun) {
197
+ app.offWindowResize(fun);
198
+ }
199
+ };
200
+ Platform.event = {
201
+ stopDefault(_origin) { },
202
+ stopNow(_origin) { },
203
+ stop(_origin) { }
204
+ };
205
+ Platform.canvas = Creator.canvas();
206
+ Platform.conicGradientSupport = !!Platform.canvas.context.createConicGradient;
207
+ }
208
+ }
209
+ Platform.name = 'miniapp';
210
+ Platform.requestRender = function (render) { Platform.canvas.view.requestAnimationFrame(render); };
211
+ Platform.devicePixelRatio = wx.getSystemInfoSync().pixelRatio;
212
+
213
+ class Watcher {
214
+ get childrenChanged() { return this.hasAdd || this.hasRemove || this.hasVisible; }
215
+ get updatedList() {
216
+ if (this.hasRemove) {
217
+ const updatedList = new LeafList();
218
+ this.__updatedList.list.forEach(item => { if (item.leafer)
219
+ updatedList.add(item); });
220
+ return updatedList;
221
+ }
222
+ else {
223
+ return this.__updatedList;
224
+ }
225
+ }
226
+ constructor(target, userConfig) {
227
+ this.totalTimes = 0;
228
+ this.config = {};
229
+ this.__updatedList = new LeafList();
230
+ this.target = target;
231
+ if (userConfig)
232
+ this.config = DataHelper.default(userConfig, this.config);
233
+ this.__listenEvents();
234
+ }
235
+ start() {
236
+ if (this.disabled)
237
+ return;
238
+ this.running = true;
239
+ }
240
+ stop() {
241
+ this.running = false;
242
+ }
243
+ disable() {
244
+ this.stop();
245
+ this.__removeListenEvents();
246
+ this.disabled = true;
247
+ }
248
+ update() {
249
+ this.changed = true;
250
+ if (this.running)
251
+ this.target.emit(RenderEvent.REQUEST);
252
+ }
253
+ __onAttrChange(event) {
254
+ this.__updatedList.add(event.target);
255
+ this.update();
256
+ }
257
+ __onChildEvent(event) {
258
+ if (event.type === ChildEvent.ADD) {
259
+ this.hasAdd = true;
260
+ this.__pushChild(event.child);
261
+ }
262
+ else {
263
+ this.hasRemove = true;
264
+ this.__updatedList.add(event.parent);
265
+ }
266
+ this.update();
267
+ }
268
+ __pushChild(child) {
269
+ this.__updatedList.add(child);
270
+ if (child.isBranch)
271
+ this.__loopChildren(child);
272
+ }
273
+ __loopChildren(parent) {
274
+ const { children } = parent;
275
+ for (let i = 0, len = children.length; i < len; i++)
276
+ this.__pushChild(children[i]);
277
+ }
278
+ __onRquestData() {
279
+ this.target.emitEvent(new WatchEvent(WatchEvent.DATA, { updatedList: this.updatedList }));
280
+ this.__updatedList = new LeafList();
281
+ this.totalTimes++;
282
+ this.changed = false;
283
+ this.hasVisible = false;
284
+ this.hasRemove = false;
285
+ this.hasAdd = false;
286
+ }
287
+ __listenEvents() {
288
+ const { target } = this;
289
+ this.__eventIds = [
290
+ target.on_(PropertyEvent.CHANGE, this.__onAttrChange, this),
291
+ target.on_([ChildEvent.ADD, ChildEvent.REMOVE], this.__onChildEvent, this),
292
+ target.on_(WatchEvent.REQUEST, this.__onRquestData, this)
293
+ ];
294
+ }
295
+ __removeListenEvents() {
296
+ this.target.off_(this.__eventIds);
297
+ }
298
+ destroy() {
299
+ if (this.target) {
300
+ this.stop();
301
+ this.__removeListenEvents();
302
+ this.target = null;
303
+ this.__updatedList = null;
304
+ }
305
+ }
306
+ }
307
+
308
+ const { updateAllMatrix: updateAllMatrix$1, updateBounds: updateOneBounds, updateAllWorldOpacity } = LeafHelper;
309
+ const { pushAllChildBranch, pushAllParent } = BranchHelper;
310
+ function updateMatrix(updateList, levelList) {
311
+ let layout;
312
+ updateList.list.forEach(leaf => {
313
+ layout = leaf.__layout;
314
+ if (levelList.without(leaf) && !layout.proxyZoom) {
315
+ if (layout.matrixChanged) {
316
+ updateAllMatrix$1(leaf, true);
317
+ levelList.add(leaf);
318
+ if (leaf.isBranch)
319
+ pushAllChildBranch(leaf, levelList);
320
+ pushAllParent(leaf, levelList);
321
+ }
322
+ else if (layout.boundsChanged) {
323
+ levelList.add(leaf);
324
+ if (leaf.isBranch)
325
+ leaf.__tempNumber = 0;
326
+ pushAllParent(leaf, levelList);
327
+ }
328
+ }
329
+ });
330
+ }
331
+ function updateBounds(boundsList) {
332
+ let list, branch, children;
333
+ boundsList.sort(true);
334
+ boundsList.levels.forEach(level => {
335
+ list = boundsList.levelMap[level];
336
+ for (let i = 0, len = list.length; i < len; i++) {
337
+ branch = list[i];
338
+ if (branch.isBranch && branch.__tempNumber) {
339
+ children = branch.children;
340
+ for (let j = 0, jLen = children.length; j < jLen; j++) {
341
+ if (!children[j].isBranch) {
342
+ updateOneBounds(children[j]);
343
+ }
344
+ }
345
+ }
346
+ updateOneBounds(branch);
347
+ }
348
+ });
349
+ }
350
+ function updateChange(updateList) {
351
+ updateList.list.forEach(leaf => {
352
+ if (leaf.__layout.opacityChanged)
353
+ updateAllWorldOpacity(leaf);
354
+ leaf.__updateChange();
355
+ });
356
+ }
357
+
358
+ const { worldBounds } = LeafBoundsHelper;
359
+ const bigBounds = { x: 0, y: 0, width: 100000, height: 100000 };
360
+ class LayoutBlockData {
361
+ constructor(list) {
362
+ this.updatedBounds = new Bounds();
363
+ this.beforeBounds = new Bounds();
364
+ this.afterBounds = new Bounds();
365
+ if (list instanceof Array)
366
+ list = new LeafList(list);
367
+ this.updatedList = list;
368
+ }
369
+ setBefore() {
370
+ this.beforeBounds.setListWithFn(this.updatedList.list, worldBounds);
371
+ }
372
+ setAfter() {
373
+ const { list } = this.updatedList;
374
+ if (list.some(leaf => leaf.noBounds)) {
375
+ this.afterBounds.set(bigBounds);
376
+ }
377
+ else {
378
+ this.afterBounds.setListWithFn(list, worldBounds);
379
+ }
380
+ this.updatedBounds.setList([this.beforeBounds, this.afterBounds]);
381
+ }
382
+ merge(data) {
383
+ this.updatedList.addList(data.updatedList.list);
384
+ this.beforeBounds.add(data.beforeBounds);
385
+ this.afterBounds.add(data.afterBounds);
386
+ this.updatedBounds.add(data.updatedBounds);
387
+ }
388
+ destroy() {
389
+ this.updatedList = null;
390
+ }
391
+ }
392
+
393
+ const { updateAllMatrix, updateAllChange } = LeafHelper;
394
+ const debug$1 = Debug.get('Layouter');
395
+ class Layouter {
396
+ constructor(target, userConfig) {
397
+ this.totalTimes = 0;
398
+ this.config = {};
399
+ this.__levelList = new LeafLevelList();
400
+ this.target = target;
401
+ if (userConfig)
402
+ this.config = DataHelper.default(userConfig, this.config);
403
+ this.__listenEvents();
404
+ }
405
+ start() {
406
+ if (this.disabled)
407
+ return;
408
+ this.running = true;
409
+ }
410
+ stop() {
411
+ this.running = false;
412
+ }
413
+ disable() {
414
+ this.stop();
415
+ this.__removeListenEvents();
416
+ this.disabled = true;
417
+ }
418
+ layout() {
419
+ if (!this.running)
420
+ return;
421
+ const { target } = this;
422
+ this.times = 0;
423
+ try {
424
+ target.emit(LayoutEvent.START);
425
+ this.layoutOnce();
426
+ target.emitEvent(new LayoutEvent(LayoutEvent.END, this.layoutedBlocks, this.times));
427
+ }
428
+ catch (e) {
429
+ debug$1.error(e);
430
+ }
431
+ this.layoutedBlocks = null;
432
+ }
433
+ layoutAgain() {
434
+ if (this.layouting) {
435
+ this.waitAgain = true;
436
+ }
437
+ else {
438
+ this.layoutOnce();
439
+ }
440
+ }
441
+ layoutOnce() {
442
+ if (this.layouting)
443
+ return debug$1.warn('layouting');
444
+ if (this.times > 3)
445
+ return debug$1.warn('layout max times');
446
+ this.times++;
447
+ this.totalTimes++;
448
+ this.layouting = true;
449
+ this.target.emit(WatchEvent.REQUEST);
450
+ if (this.totalTimes > 1) {
451
+ this.partLayout();
452
+ }
453
+ else {
454
+ this.fullLayout();
455
+ }
456
+ this.layouting = false;
457
+ if (this.waitAgain) {
458
+ this.waitAgain = false;
459
+ this.layoutOnce();
460
+ }
461
+ }
462
+ partLayout() {
463
+ var _a;
464
+ if (!((_a = this.__updatedList) === null || _a === void 0 ? void 0 : _a.length))
465
+ return;
466
+ const t = Run.start('PartLayout');
467
+ const { target, __updatedList: updateList } = this;
468
+ const { BEFORE, LAYOUT, AFTER } = LayoutEvent;
469
+ const blocks = this.getBlocks(updateList);
470
+ blocks.forEach(item => item.setBefore());
471
+ target.emitEvent(new LayoutEvent(BEFORE, blocks, this.times));
472
+ this.extraBlock = null;
473
+ updateList.sort();
474
+ updateMatrix(updateList, this.__levelList);
475
+ updateBounds(this.__levelList);
476
+ updateChange(updateList);
477
+ if (this.extraBlock)
478
+ blocks.push(this.extraBlock);
479
+ blocks.forEach(item => item.setAfter());
480
+ target.emitEvent(new LayoutEvent(LAYOUT, blocks, this.times));
481
+ target.emitEvent(new LayoutEvent(AFTER, blocks, this.times));
482
+ this.addBlocks(blocks);
483
+ this.__levelList.reset();
484
+ this.__updatedList = null;
485
+ Run.end(t);
486
+ }
487
+ fullLayout() {
488
+ const t = Run.start('FullLayout');
489
+ const { target } = this;
490
+ const { BEFORE, LAYOUT, AFTER } = LayoutEvent;
491
+ const blocks = this.getBlocks(new LeafList(target));
492
+ target.emitEvent(new LayoutEvent(BEFORE, blocks, this.times));
493
+ Layouter.fullLayout(target);
494
+ blocks.forEach(item => { item.setAfter(); });
495
+ target.emitEvent(new LayoutEvent(LAYOUT, blocks, this.times));
496
+ target.emitEvent(new LayoutEvent(AFTER, blocks, this.times));
497
+ this.addBlocks(blocks);
498
+ Run.end(t);
499
+ }
500
+ static fullLayout(target) {
501
+ updateAllMatrix(target, true);
502
+ if (target.isBranch) {
503
+ BranchHelper.updateBounds(target);
504
+ }
505
+ else {
506
+ LeafHelper.updateBounds(target);
507
+ }
508
+ updateAllChange(target);
509
+ }
510
+ addExtra(leaf) {
511
+ const block = this.extraBlock || (this.extraBlock = new LayoutBlockData([]));
512
+ block.updatedList.add(leaf);
513
+ block.beforeBounds.add(leaf.__world);
514
+ }
515
+ createBlock(data) {
516
+ return new LayoutBlockData(data);
517
+ }
518
+ getBlocks(list) {
519
+ return [this.createBlock(list)];
520
+ }
521
+ addBlocks(current) {
522
+ this.layoutedBlocks ? this.layoutedBlocks.push(...current) : this.layoutedBlocks = current;
523
+ }
524
+ __onReceiveWatchData(event) {
525
+ this.__updatedList = event.data.updatedList;
526
+ }
527
+ __listenEvents() {
528
+ const { target } = this;
529
+ this.__eventIds = [
530
+ target.on_(LayoutEvent.REQUEST, this.layout, this),
531
+ target.on_(LayoutEvent.AGAIN, this.layoutAgain, this),
532
+ target.on_(WatchEvent.DATA, this.__onReceiveWatchData, this)
533
+ ];
534
+ }
535
+ __removeListenEvents() {
536
+ this.target.off_(this.__eventIds);
537
+ }
538
+ destroy() {
539
+ if (this.target) {
540
+ this.stop();
541
+ this.__removeListenEvents();
542
+ this.target = this.config = null;
543
+ }
544
+ }
545
+ }
546
+
547
+ const debug = Debug.get('Renderer');
548
+ class Renderer {
549
+ get needFill() { return !!(!this.canvas.allowBackgroundColor && this.config.fill); }
550
+ constructor(target, canvas, userConfig) {
551
+ this.FPS = 60;
552
+ this.totalTimes = 0;
553
+ this.times = 0;
554
+ this.config = {
555
+ usePartRender: true,
556
+ maxFPS: 60
557
+ };
558
+ this.target = target;
559
+ this.canvas = canvas;
560
+ if (userConfig)
561
+ this.config = DataHelper.default(userConfig, this.config);
562
+ this.__listenEvents();
563
+ this.__requestRender();
564
+ }
565
+ start() {
566
+ this.running = true;
567
+ }
568
+ stop() {
569
+ this.running = false;
570
+ }
571
+ update() {
572
+ this.changed = true;
573
+ }
574
+ requestLayout() {
575
+ this.target.emit(LayoutEvent.REQUEST);
576
+ }
577
+ render(callback) {
578
+ if (!(this.running && this.canvas.view)) {
579
+ this.changed = true;
580
+ return;
581
+ }
582
+ const { target } = this;
583
+ this.times = 0;
584
+ this.totalBounds = new Bounds();
585
+ debug.log(target.innerName, '--->');
586
+ try {
587
+ this.emitRender(RenderEvent.START);
588
+ this.renderOnce(callback);
589
+ this.emitRender(RenderEvent.END, this.totalBounds);
590
+ ImageManager.clearRecycled();
591
+ }
592
+ catch (e) {
593
+ this.rendering = false;
594
+ debug.error(e);
595
+ }
596
+ debug.log('-------------|');
597
+ }
598
+ renderAgain() {
599
+ if (this.rendering) {
600
+ this.waitAgain = true;
601
+ }
602
+ else {
603
+ this.renderOnce();
604
+ }
605
+ }
606
+ renderOnce(callback) {
607
+ if (this.rendering)
608
+ return debug.warn('rendering');
609
+ if (this.times > 3)
610
+ return debug.warn('render max times');
611
+ this.times++;
612
+ this.totalTimes++;
613
+ this.rendering = true;
614
+ this.changed = false;
615
+ this.renderBounds = new Bounds();
616
+ this.renderOptions = {};
617
+ if (callback) {
618
+ this.emitRender(RenderEvent.BEFORE);
619
+ callback();
620
+ }
621
+ else {
622
+ this.requestLayout();
623
+ this.emitRender(RenderEvent.BEFORE);
624
+ if (this.config.usePartRender && this.totalTimes > 1) {
625
+ this.partRender();
626
+ }
627
+ else {
628
+ this.fullRender();
629
+ }
630
+ }
631
+ this.emitRender(RenderEvent.RENDER, this.renderBounds, this.renderOptions);
632
+ this.emitRender(RenderEvent.AFTER, this.renderBounds, this.renderOptions);
633
+ this.updateBlocks = null;
634
+ this.rendering = false;
635
+ if (this.waitAgain) {
636
+ this.waitAgain = false;
637
+ this.renderOnce();
638
+ }
639
+ }
640
+ partRender() {
641
+ const { canvas, updateBlocks: list } = this;
642
+ if (!list)
643
+ return debug.warn('PartRender: need update attr');
644
+ this.mergeBlocks();
645
+ list.forEach(block => { if (canvas.bounds.hit(block) && !block.isEmpty())
646
+ this.clipRender(block); });
647
+ }
648
+ clipRender(block) {
649
+ const t = Run.start('PartRender');
650
+ const { canvas } = this;
651
+ const bounds = block.getIntersect(canvas.bounds);
652
+ const includes = block.includes(this.target.__world);
653
+ const realBounds = new Bounds(bounds);
654
+ canvas.save();
655
+ if (includes && !Debug.showRepaint) {
656
+ canvas.clear();
657
+ }
658
+ else {
659
+ bounds.spread(1 + 1 / this.canvas.pixelRatio).ceil();
660
+ canvas.clearWorld(bounds, true);
661
+ canvas.clipWorld(bounds, true);
662
+ }
663
+ this.__render(bounds, includes, realBounds);
664
+ canvas.restore();
665
+ Run.end(t);
666
+ }
667
+ fullRender() {
668
+ const t = Run.start('FullRender');
669
+ const { canvas } = this;
670
+ canvas.save();
671
+ canvas.clear();
672
+ this.__render(canvas.bounds, true);
673
+ canvas.restore();
674
+ Run.end(t);
675
+ }
676
+ __render(bounds, includes, realBounds) {
677
+ const options = bounds.includes(this.target.__world) ? { includes } : { bounds, includes };
678
+ if (this.needFill)
679
+ this.canvas.fillWorld(bounds, this.config.fill);
680
+ if (Debug.showRepaint)
681
+ this.canvas.strokeWorld(bounds, 'red');
682
+ this.target.__render(this.canvas, options);
683
+ this.renderBounds = realBounds || bounds;
684
+ this.renderOptions = options;
685
+ this.totalBounds.isEmpty() ? this.totalBounds = this.renderBounds : this.totalBounds.add(this.renderBounds);
686
+ if (Debug.showHitView)
687
+ this.renderHitView(options);
688
+ if (Debug.showBoundsView)
689
+ this.renderBoundsView(options);
690
+ this.canvas.updateRender();
691
+ }
692
+ renderHitView(_options) { }
693
+ renderBoundsView(_options) { }
694
+ addBlock(block) {
695
+ if (!this.updateBlocks)
696
+ this.updateBlocks = [];
697
+ this.updateBlocks.push(block);
698
+ }
699
+ mergeBlocks() {
700
+ const { updateBlocks: list } = this;
701
+ if (list) {
702
+ const bounds = new Bounds();
703
+ bounds.setList(list);
704
+ list.length = 0;
705
+ list.push(bounds);
706
+ }
707
+ }
708
+ __requestRender() {
709
+ const startTime = Date.now();
710
+ Platform.requestRender(() => {
711
+ this.FPS = Math.min(60, Math.ceil(1000 / (Date.now() - startTime)));
712
+ if (this.running) {
713
+ this.target.emit(AnimateEvent.FRAME);
714
+ if (this.changed && this.canvas.view)
715
+ this.render();
716
+ this.target.emit(RenderEvent.NEXT);
717
+ }
718
+ if (this.target)
719
+ this.__requestRender();
720
+ });
721
+ }
722
+ __onResize(e) {
723
+ if (this.canvas.unreal)
724
+ return;
725
+ if (e.bigger || !e.samePixelRatio) {
726
+ const { width, height } = e.old;
727
+ const bounds = new Bounds(0, 0, width, height);
728
+ if (!bounds.includes(this.target.__world) || this.needFill || !e.samePixelRatio) {
729
+ this.addBlock(this.canvas.bounds);
730
+ this.target.forceUpdate('surface');
731
+ }
732
+ }
733
+ }
734
+ __onLayoutEnd(event) {
735
+ if (event.data)
736
+ event.data.map(item => {
737
+ let empty;
738
+ if (item.updatedList)
739
+ item.updatedList.list.some(leaf => {
740
+ empty = (!leaf.__world.width || !leaf.__world.height);
741
+ if (empty) {
742
+ if (!leaf.isLeafer)
743
+ debug.tip(leaf.innerName, ': empty');
744
+ empty = (!leaf.isBranch || leaf.isBranchLeaf);
745
+ }
746
+ return empty;
747
+ });
748
+ this.addBlock(empty ? this.canvas.bounds : item.updatedBounds);
749
+ });
750
+ }
751
+ emitRender(type, bounds, options) {
752
+ this.target.emitEvent(new RenderEvent(type, this.times, bounds, options));
753
+ }
754
+ __listenEvents() {
755
+ const { target } = this;
756
+ this.__eventIds = [
757
+ target.on_(RenderEvent.REQUEST, this.update, this),
758
+ target.on_(LayoutEvent.END, this.__onLayoutEnd, this),
759
+ target.on_(RenderEvent.AGAIN, this.renderAgain, this),
760
+ target.on_(ResizeEvent.RESIZE, this.__onResize, this)
761
+ ];
762
+ }
763
+ __removeListenEvents() {
764
+ this.target.off_(this.__eventIds);
765
+ }
766
+ destroy() {
767
+ if (this.target) {
768
+ this.stop();
769
+ this.__removeListenEvents();
770
+ this.target = null;
771
+ this.canvas = null;
772
+ this.config = null;
773
+ }
774
+ }
775
+ }
776
+
777
+ const { hitRadiusPoint } = BoundsHelper;
778
+ class Picker {
779
+ constructor(target, selector) {
780
+ this.target = target;
781
+ this.selector = selector;
782
+ }
783
+ getByPoint(hitPoint, hitRadius, options) {
784
+ if (!hitRadius)
785
+ hitRadius = 0;
786
+ if (!options)
787
+ options = {};
788
+ const through = options.through || false;
789
+ const ignoreHittable = options.ignoreHittable || false;
790
+ const target = options.target || this.target;
791
+ this.exclude = options.exclude || null;
792
+ this.point = { x: hitPoint.x, y: hitPoint.y, radiusX: hitRadius, radiusY: hitRadius };
793
+ this.findList = options.findList || [];
794
+ if (!options.findList)
795
+ this.eachFind(target.children, target.__onlyHitMask);
796
+ const list = this.findList;
797
+ const leaf = this.getBestMatchLeaf();
798
+ const path = ignoreHittable ? this.getPath(leaf) : this.getHitablePath(leaf);
799
+ this.clear();
800
+ return through ? { path, target: leaf, throughPath: list.length ? this.getThroughPath(list) : path } : { path, target: leaf };
801
+ }
802
+ getBestMatchLeaf() {
803
+ const { findList: targets } = this;
804
+ if (targets.length > 1) {
805
+ let find;
806
+ this.findList = [];
807
+ const { x, y } = this.point;
808
+ const point = { x, y, radiusX: 0, radiusY: 0 };
809
+ for (let i = 0, len = targets.length; i < len; i++) {
810
+ find = targets[i];
811
+ if (LeafHelper.worldHittable(find)) {
812
+ this.hitChild(find, point);
813
+ if (this.findList.length)
814
+ return this.findList[0];
815
+ }
816
+ }
817
+ }
818
+ return targets[0];
819
+ }
820
+ getPath(leaf) {
821
+ const path = new LeafList();
822
+ while (leaf) {
823
+ path.add(leaf);
824
+ leaf = leaf.parent;
825
+ }
826
+ path.add(this.target);
827
+ return path;
828
+ }
829
+ getHitablePath(leaf) {
830
+ const path = this.getPath(leaf);
831
+ let item, hittablePath = new LeafList();
832
+ for (let i = path.list.length - 1; i > -1; i--) {
833
+ item = path.list[i];
834
+ if (!item.__.hittable)
835
+ break;
836
+ hittablePath.addAt(item, 0);
837
+ if (!item.__.hitChildren)
838
+ break;
839
+ }
840
+ return hittablePath;
841
+ }
842
+ getThroughPath(list) {
843
+ const throughPath = new LeafList();
844
+ const pathList = [];
845
+ for (let i = list.length - 1; i > -1; i--) {
846
+ pathList.push(this.getPath(list[i]));
847
+ }
848
+ let path, nextPath, leaf;
849
+ for (let i = 0, len = pathList.length; i < len; i++) {
850
+ path = pathList[i], nextPath = pathList[i + 1];
851
+ for (let j = 0, jLen = path.length; j < jLen; j++) {
852
+ leaf = path.list[j];
853
+ if (nextPath && nextPath.has(leaf))
854
+ break;
855
+ throughPath.add(leaf);
856
+ }
857
+ }
858
+ return throughPath;
859
+ }
860
+ eachFind(children, hitMask) {
861
+ let child, hit;
862
+ const { point } = this, len = children.length;
863
+ for (let i = len - 1; i > -1; i--) {
864
+ child = children[i];
865
+ if (!child.__.visible || (hitMask && !child.__.mask))
866
+ continue;
867
+ hit = child.__.hitRadius ? true : hitRadiusPoint(child.__world, point);
868
+ if (child.isBranch) {
869
+ if (hit || child.__ignoreHitWorld) {
870
+ this.eachFind(child.children, child.__onlyHitMask);
871
+ if (child.isBranchLeaf && !this.findList.length)
872
+ this.hitChild(child, point);
873
+ }
874
+ }
875
+ else {
876
+ if (hit)
877
+ this.hitChild(child, point);
878
+ }
879
+ }
880
+ }
881
+ hitChild(child, point) {
882
+ if (this.exclude && this.exclude.has(child))
883
+ return;
884
+ if (child.__hitWorld(point))
885
+ this.findList.push(child);
886
+ }
887
+ clear() {
888
+ this.point = null;
889
+ this.findList = null;
890
+ this.exclude = null;
891
+ }
892
+ destroy() {
893
+ this.clear();
894
+ }
895
+ }
896
+
897
+ const { Yes, NoAndSkip, YesAndSkip } = Answer;
898
+ class Selector {
899
+ constructor(target, userConfig) {
900
+ this.config = {};
901
+ this.innerIdMap = {};
902
+ this.idMap = {};
903
+ this.methods = {
904
+ id: (leaf, name) => leaf.id === name ? (this.idMap[name] = leaf, 1) : 0,
905
+ innerId: (leaf, innerId) => leaf.innerId === innerId ? (this.innerIdMap[innerId] = leaf, 1) : 0,
906
+ className: (leaf, name) => leaf.className === name ? 1 : 0,
907
+ tag: (leaf, name) => leaf.__tag === name ? 1 : 0
908
+ };
909
+ this.target = target;
910
+ if (userConfig)
911
+ this.config = DataHelper.default(userConfig, this.config);
912
+ this.picker = new Picker(target, this);
913
+ this.__listenEvents();
914
+ }
915
+ getBy(condition, branch, one, options) {
916
+ switch (typeof condition) {
917
+ case 'number':
918
+ const leaf = this.getByInnerId(condition, branch);
919
+ return one ? leaf : (leaf ? [leaf] : []);
920
+ case 'string':
921
+ switch (condition[0]) {
922
+ case '#':
923
+ const leaf = this.getById(condition.substring(1), branch);
924
+ return one ? leaf : (leaf ? [leaf] : []);
925
+ case '.':
926
+ return this.getByMethod(this.methods.className, branch, one, condition.substring(1));
927
+ default:
928
+ return this.getByMethod(this.methods.tag, branch, one, condition);
929
+ }
930
+ case 'function':
931
+ return this.getByMethod(condition, branch, one, options);
932
+ }
933
+ }
934
+ getByPoint(hitPoint, hitRadius, options) {
935
+ if (Platform.name === 'node')
936
+ this.target.emit(LayoutEvent.CHECK_UPDATE);
937
+ return this.picker.getByPoint(hitPoint, hitRadius, options);
938
+ }
939
+ getByInnerId(innerId, branch) {
940
+ const cache = this.innerIdMap[innerId];
941
+ if (cache)
942
+ return cache;
943
+ this.eachFind(this.toChildren(branch), this.methods.innerId, null, innerId);
944
+ return this.findLeaf;
945
+ }
946
+ getById(id, branch) {
947
+ const cache = this.idMap[id];
948
+ if (cache && LeafHelper.hasParent(cache, branch || this.target))
949
+ return cache;
950
+ this.eachFind(this.toChildren(branch), this.methods.id, null, id);
951
+ return this.findLeaf;
952
+ }
953
+ getByClassName(className, branch) {
954
+ return this.getByMethod(this.methods.className, branch, false, className);
955
+ }
956
+ getByTag(tag, branch) {
957
+ return this.getByMethod(this.methods.tag, branch, false, tag);
958
+ }
959
+ getByMethod(method, branch, one, options) {
960
+ const list = one ? null : [];
961
+ this.eachFind(this.toChildren(branch), method, list, options);
962
+ return list || this.findLeaf;
963
+ }
964
+ eachFind(children, method, list, options) {
965
+ let child, result;
966
+ for (let i = 0, len = children.length; i < len; i++) {
967
+ child = children[i];
968
+ result = method(child, options);
969
+ if (result === Yes || result === YesAndSkip) {
970
+ if (list) {
971
+ list.push(child);
972
+ }
973
+ else {
974
+ this.findLeaf = child;
975
+ return;
976
+ }
977
+ }
978
+ if (child.isBranch && result < NoAndSkip)
979
+ this.eachFind(child.children, method, list, options);
980
+ }
981
+ }
982
+ toChildren(branch) {
983
+ this.findLeaf = null;
984
+ return [branch || this.target];
985
+ }
986
+ __onRemoveChild(event) {
987
+ const { id, innerId } = event.child;
988
+ if (this.idMap[id])
989
+ delete this.idMap[id];
990
+ if (this.innerIdMap[innerId])
991
+ delete this.innerIdMap[innerId];
992
+ }
993
+ __checkIdChange(event) {
994
+ if (event.attrName === 'id') {
995
+ const id = event.oldValue;
996
+ if (this.idMap[id])
997
+ delete this.idMap[id];
998
+ }
999
+ }
1000
+ __listenEvents() {
1001
+ this.__eventIds = [
1002
+ this.target.on_(ChildEvent.REMOVE, this.__onRemoveChild, this),
1003
+ this.target.on_(PropertyEvent.CHANGE, this.__checkIdChange, this)
1004
+ ];
1005
+ }
1006
+ __removeListenEvents() {
1007
+ this.target.off_(this.__eventIds);
1008
+ this.__eventIds.length = 0;
1009
+ }
1010
+ destroy() {
1011
+ if (this.__eventIds.length) {
1012
+ this.__removeListenEvents();
1013
+ this.picker.destroy();
1014
+ this.findLeaf = null;
1015
+ this.innerIdMap = {};
1016
+ this.idMap = {};
1017
+ }
1018
+ }
1019
+ }
1020
+
1021
+ Object.assign(Creator, {
1022
+ watcher: (target, options) => new Watcher(target, options),
1023
+ layouter: (target, options) => new Layouter(target, options),
1024
+ renderer: (target, canvas, options) => new Renderer(target, canvas, options),
1025
+ selector: (target, options) => new Selector(target, options)
1026
+ });
1027
+ Platform.layout = Layouter.fullLayout;
1028
+
1029
+ const PointerEventHelper = {
1030
+ convertTouch(e, local) {
1031
+ const touch = PointerEventHelper.getTouch(e);
1032
+ const base = InteractionHelper.getBase(e);
1033
+ return Object.assign(Object.assign({}, base), { x: local.x, y: local.y, width: 1, height: 1, pointerType: 'touch', pressure: touch.force || 1 });
1034
+ },
1035
+ getTouch(e) {
1036
+ return e.touches[0] || e.changedTouches[0];
1037
+ }
1038
+ };
1039
+
1040
+ class Interaction extends InteractionBase {
1041
+ __listenEvents() {
1042
+ super.__listenEvents();
1043
+ if (this.config.eventer)
1044
+ this.config.eventer.receiveEvent = this.receive.bind(this);
1045
+ }
1046
+ receive(e) {
1047
+ switch (e.type) {
1048
+ case 'touchstart':
1049
+ this.onTouchStart(e);
1050
+ break;
1051
+ case 'touchmove':
1052
+ this.onTouchMove(e);
1053
+ break;
1054
+ case 'touchend':
1055
+ this.onTouchEnd(e);
1056
+ break;
1057
+ case 'touchcancel':
1058
+ this.onTouchCancel();
1059
+ }
1060
+ }
1061
+ getLocal(p, updateClient) {
1062
+ if (updateClient)
1063
+ this.canvas.updateClientBounds();
1064
+ if (p.x !== undefined) {
1065
+ return { x: p.x, y: p.y };
1066
+ }
1067
+ else {
1068
+ const { clientBounds } = this.canvas;
1069
+ return { x: p.clientX - clientBounds.x, y: p.clientY - clientBounds.y };
1070
+ }
1071
+ }
1072
+ getTouches(touches) {
1073
+ return touches;
1074
+ }
1075
+ onTouchStart(e) {
1076
+ this.multiTouchStart(e);
1077
+ const touch = PointerEventHelper.getTouch(e);
1078
+ this.pointerDown(PointerEventHelper.convertTouch(e, this.getLocal(touch, true)));
1079
+ }
1080
+ onTouchMove(e) {
1081
+ this.multiTouchMove(e);
1082
+ if (this.useMultiTouch)
1083
+ return;
1084
+ const touch = PointerEventHelper.getTouch(e);
1085
+ this.pointerMove(PointerEventHelper.convertTouch(e, this.getLocal(touch)));
1086
+ }
1087
+ onTouchEnd(e) {
1088
+ this.multiTouchEnd();
1089
+ const touch = PointerEventHelper.getTouch(e);
1090
+ this.pointerUp(PointerEventHelper.convertTouch(e, this.getLocal(touch)));
1091
+ }
1092
+ onTouchCancel() {
1093
+ this.pointerCancel();
1094
+ }
1095
+ multiTouchStart(e) {
1096
+ this.useMultiTouch = (e.touches.length >= 2);
1097
+ this.touches = this.useMultiTouch ? this.getTouches(e.touches) : undefined;
1098
+ if (this.useMultiTouch)
1099
+ this.pointerCancel();
1100
+ }
1101
+ multiTouchMove(e) {
1102
+ if (!this.useMultiTouch)
1103
+ return;
1104
+ if (e.touches.length > 1) {
1105
+ const touches = this.getTouches(e.touches);
1106
+ const list = this.getKeepTouchList(this.touches, touches);
1107
+ if (list.length > 1) {
1108
+ this.multiTouch(InteractionHelper.getBase(e), list);
1109
+ this.touches = touches;
1110
+ }
1111
+ }
1112
+ }
1113
+ multiTouchEnd() {
1114
+ this.touches = null;
1115
+ this.useMultiTouch = false;
1116
+ this.transformEnd();
1117
+ }
1118
+ getKeepTouchList(old, touches) {
1119
+ let to;
1120
+ const list = [];
1121
+ old.forEach(from => {
1122
+ to = touches.find(touch => touch.identifier === from.identifier);
1123
+ if (to)
1124
+ list.push({ from: this.getLocal(from), to: this.getLocal(to) });
1125
+ });
1126
+ return list;
1127
+ }
1128
+ getLocalTouchs(points) {
1129
+ return points.map(point => this.getLocal(point));
1130
+ }
1131
+ destroy() {
1132
+ super.destroy();
1133
+ this.touches = null;
1134
+ }
1135
+ }
1136
+
1137
+ function fillText(ui, canvas) {
1138
+ let row;
1139
+ const { rows, decorationY, decorationHeight } = ui.__.__textDrawData;
1140
+ for (let i = 0, len = rows.length; i < len; i++) {
1141
+ row = rows[i];
1142
+ if (row.text) {
1143
+ canvas.fillText(row.text, row.x, row.y);
1144
+ }
1145
+ else if (row.data) {
1146
+ row.data.forEach(charData => {
1147
+ canvas.fillText(charData.char, charData.x, row.y);
1148
+ });
1149
+ }
1150
+ if (decorationY)
1151
+ canvas.fillRect(row.x, row.y + decorationY, row.width, decorationHeight);
1152
+ }
1153
+ }
1154
+
1155
+ function fill(fill, ui, canvas) {
1156
+ canvas.fillStyle = fill;
1157
+ ui.__.__font ? fillText(ui, canvas) : (ui.__.windingRule ? canvas.fill(ui.__.windingRule) : canvas.fill());
1158
+ }
1159
+ function fills(fills, ui, canvas) {
1160
+ let item;
1161
+ const { windingRule, __font } = ui.__;
1162
+ for (let i = 0, len = fills.length; i < len; i++) {
1163
+ item = fills[i];
1164
+ if (item.image && PaintImage.checkImage(ui, canvas, item, !__font))
1165
+ continue;
1166
+ if (item.style) {
1167
+ canvas.fillStyle = item.style;
1168
+ if (item.transform) {
1169
+ canvas.save();
1170
+ canvas.transform(item.transform);
1171
+ if (item.blendMode)
1172
+ canvas.blendMode = item.blendMode;
1173
+ __font ? fillText(ui, canvas) : (windingRule ? canvas.fill(windingRule) : canvas.fill());
1174
+ canvas.restore();
1175
+ }
1176
+ else {
1177
+ if (item.blendMode) {
1178
+ canvas.saveBlendMode(item.blendMode);
1179
+ __font ? fillText(ui, canvas) : (windingRule ? canvas.fill(windingRule) : canvas.fill());
1180
+ canvas.restoreBlendMode();
1181
+ }
1182
+ else {
1183
+ __font ? fillText(ui, canvas) : (windingRule ? canvas.fill(windingRule) : canvas.fill());
1184
+ }
1185
+ }
1186
+ }
1187
+ }
1188
+ }
1189
+
1190
+ function strokeText(stroke, ui, canvas) {
1191
+ const { strokeAlign } = ui.__;
1192
+ const isStrokes = typeof stroke !== 'string';
1193
+ switch (strokeAlign) {
1194
+ case 'center':
1195
+ canvas.setStroke(isStrokes ? undefined : stroke, ui.__.strokeWidth, ui.__);
1196
+ isStrokes ? drawStrokesStyle(stroke, true, ui, canvas) : drawTextStroke(ui, canvas);
1197
+ break;
1198
+ case 'inside':
1199
+ drawAlignStroke('inside', stroke, isStrokes, ui, canvas);
1200
+ break;
1201
+ case 'outside':
1202
+ drawAlignStroke('outside', stroke, isStrokes, ui, canvas);
1203
+ break;
1204
+ }
1205
+ }
1206
+ function drawAlignStroke(align, stroke, isStrokes, ui, canvas) {
1207
+ const { __strokeWidth, __font } = ui.__;
1208
+ const out = canvas.getSameCanvas(true, true);
1209
+ out.setStroke(isStrokes ? undefined : stroke, __strokeWidth * 2, ui.__);
1210
+ out.font = __font;
1211
+ isStrokes ? drawStrokesStyle(stroke, true, ui, out) : drawTextStroke(ui, out);
1212
+ out.blendMode = align === 'outside' ? 'destination-out' : 'destination-in';
1213
+ fillText(ui, out);
1214
+ out.blendMode = 'normal';
1215
+ if (ui.__worldFlipped) {
1216
+ canvas.copyWorldByReset(out, ui.__nowWorld);
1217
+ }
1218
+ else {
1219
+ canvas.copyWorldToInner(out, ui.__nowWorld, ui.__layout.renderBounds);
1220
+ }
1221
+ out.recycle(ui.__nowWorld);
1222
+ }
1223
+ function drawTextStroke(ui, canvas) {
1224
+ let row;
1225
+ const { rows, decorationY, decorationHeight } = ui.__.__textDrawData;
1226
+ for (let i = 0, len = rows.length; i < len; i++) {
1227
+ row = rows[i];
1228
+ if (row.text) {
1229
+ canvas.strokeText(row.text, row.x, row.y);
1230
+ }
1231
+ else if (row.data) {
1232
+ row.data.forEach(charData => {
1233
+ canvas.strokeText(charData.char, charData.x, row.y);
1234
+ });
1235
+ }
1236
+ if (decorationY)
1237
+ canvas.strokeRect(row.x, row.y + decorationY, row.width, decorationHeight);
1238
+ }
1239
+ }
1240
+ function drawStrokesStyle(strokes, isText, ui, canvas) {
1241
+ let item;
1242
+ for (let i = 0, len = strokes.length; i < len; i++) {
1243
+ item = strokes[i];
1244
+ if (item.image && PaintImage.checkImage(ui, canvas, item, false))
1245
+ continue;
1246
+ if (item.style) {
1247
+ canvas.strokeStyle = item.style;
1248
+ if (item.blendMode) {
1249
+ canvas.saveBlendMode(item.blendMode);
1250
+ isText ? drawTextStroke(ui, canvas) : canvas.stroke();
1251
+ canvas.restoreBlendMode();
1252
+ }
1253
+ else {
1254
+ isText ? drawTextStroke(ui, canvas) : canvas.stroke();
1255
+ }
1256
+ }
1257
+ }
1258
+ }
1259
+
1260
+ function stroke(stroke, ui, canvas) {
1261
+ const options = ui.__;
1262
+ const { __strokeWidth, strokeAlign, __font } = options;
1263
+ if (!__strokeWidth)
1264
+ return;
1265
+ if (__font) {
1266
+ strokeText(stroke, ui, canvas);
1267
+ }
1268
+ else {
1269
+ switch (strokeAlign) {
1270
+ case 'center':
1271
+ canvas.setStroke(stroke, __strokeWidth, options);
1272
+ canvas.stroke();
1273
+ break;
1274
+ case 'inside':
1275
+ canvas.save();
1276
+ canvas.setStroke(stroke, __strokeWidth * 2, options);
1277
+ options.windingRule ? canvas.clip(options.windingRule) : canvas.clip();
1278
+ canvas.stroke();
1279
+ canvas.restore();
1280
+ break;
1281
+ case 'outside':
1282
+ const out = canvas.getSameCanvas(true, true);
1283
+ out.setStroke(stroke, __strokeWidth * 2, options);
1284
+ ui.__drawRenderPath(out);
1285
+ out.stroke();
1286
+ options.windingRule ? out.clip(options.windingRule) : out.clip();
1287
+ out.clearWorld(ui.__layout.renderBounds);
1288
+ if (ui.__worldFlipped) {
1289
+ canvas.copyWorldByReset(out, ui.__nowWorld);
1290
+ }
1291
+ else {
1292
+ canvas.copyWorldToInner(out, ui.__nowWorld, ui.__layout.renderBounds);
1293
+ }
1294
+ out.recycle(ui.__nowWorld);
1295
+ break;
1296
+ }
1297
+ }
1298
+ }
1299
+ function strokes(strokes, ui, canvas) {
1300
+ const options = ui.__;
1301
+ const { __strokeWidth, strokeAlign, __font } = options;
1302
+ if (!__strokeWidth)
1303
+ return;
1304
+ if (__font) {
1305
+ strokeText(strokes, ui, canvas);
1306
+ }
1307
+ else {
1308
+ switch (strokeAlign) {
1309
+ case 'center':
1310
+ canvas.setStroke(undefined, __strokeWidth, options);
1311
+ drawStrokesStyle(strokes, false, ui, canvas);
1312
+ break;
1313
+ case 'inside':
1314
+ canvas.save();
1315
+ canvas.setStroke(undefined, __strokeWidth * 2, options);
1316
+ options.windingRule ? canvas.clip(options.windingRule) : canvas.clip();
1317
+ drawStrokesStyle(strokes, false, ui, canvas);
1318
+ canvas.restore();
1319
+ break;
1320
+ case 'outside':
1321
+ const { renderBounds } = ui.__layout;
1322
+ const out = canvas.getSameCanvas(true, true);
1323
+ ui.__drawRenderPath(out);
1324
+ out.setStroke(undefined, __strokeWidth * 2, options);
1325
+ drawStrokesStyle(strokes, false, ui, out);
1326
+ options.windingRule ? out.clip(options.windingRule) : out.clip();
1327
+ out.clearWorld(renderBounds);
1328
+ if (ui.__worldFlipped) {
1329
+ canvas.copyWorldByReset(out, ui.__nowWorld);
1330
+ }
1331
+ else {
1332
+ canvas.copyWorldToInner(out, ui.__nowWorld, renderBounds);
1333
+ }
1334
+ out.recycle(ui.__nowWorld);
1335
+ break;
1336
+ }
1337
+ }
1338
+ }
1339
+
1340
+ const { getSpread, getOuterOf, getByMove, getIntersectData } = BoundsHelper;
1341
+ function shape(ui, current, options) {
1342
+ const canvas = current.getSameCanvas();
1343
+ const nowWorld = ui.__nowWorld;
1344
+ let bounds, fitMatrix, shapeBounds, worldCanvas;
1345
+ let { scaleX, scaleY } = nowWorld;
1346
+ if (scaleX < 0)
1347
+ scaleX = -scaleX;
1348
+ if (scaleY < 0)
1349
+ scaleY = -scaleY;
1350
+ if (current.bounds.includes(nowWorld)) {
1351
+ worldCanvas = canvas;
1352
+ bounds = shapeBounds = nowWorld;
1353
+ }
1354
+ else {
1355
+ const { renderShapeSpread: spread } = ui.__layout;
1356
+ const worldClipBounds = getIntersectData(spread ? getSpread(current.bounds, spread * scaleX, spread * scaleY) : current.bounds, nowWorld);
1357
+ fitMatrix = current.bounds.getFitMatrix(worldClipBounds);
1358
+ let { a: fitScaleX, d: fitScaleY } = fitMatrix;
1359
+ if (fitMatrix.a < 1) {
1360
+ worldCanvas = current.getSameCanvas();
1361
+ ui.__renderShape(worldCanvas, options);
1362
+ scaleX *= fitScaleX;
1363
+ scaleY *= fitScaleY;
1364
+ }
1365
+ shapeBounds = getOuterOf(nowWorld, fitMatrix);
1366
+ bounds = getByMove(shapeBounds, -fitMatrix.e, -fitMatrix.f);
1367
+ if (options.matrix) {
1368
+ const { matrix } = options;
1369
+ fitMatrix.multiply(matrix);
1370
+ fitScaleX *= matrix.scaleX;
1371
+ fitScaleY *= matrix.scaleY;
1372
+ }
1373
+ options = Object.assign(Object.assign({}, options), { matrix: fitMatrix.withScale(fitScaleX, fitScaleY) });
1374
+ }
1375
+ ui.__renderShape(canvas, options);
1376
+ return {
1377
+ canvas, matrix: fitMatrix, bounds,
1378
+ worldCanvas, shapeBounds, scaleX, scaleY
1379
+ };
1380
+ }
1381
+
1382
+ let recycleMap;
1383
+ function compute(attrName, ui) {
1384
+ const data = ui.__, leafPaints = [];
1385
+ let paints = data.__input[attrName], hasOpacityPixel;
1386
+ if (!(paints instanceof Array))
1387
+ paints = [paints];
1388
+ recycleMap = PaintImage.recycleImage(attrName, data);
1389
+ for (let i = 0, len = paints.length, item; i < len; i++) {
1390
+ item = getLeafPaint(attrName, paints[i], ui);
1391
+ if (item)
1392
+ leafPaints.push(item);
1393
+ }
1394
+ data['_' + attrName] = leafPaints.length ? leafPaints : undefined;
1395
+ if (leafPaints.length && leafPaints[0].image)
1396
+ hasOpacityPixel = leafPaints[0].image.hasOpacityPixel;
1397
+ if (attrName === 'fill') {
1398
+ data.__pixelFill = hasOpacityPixel;
1399
+ }
1400
+ else {
1401
+ data.__pixelStroke = hasOpacityPixel;
1402
+ }
1403
+ }
1404
+ function getLeafPaint(attrName, paint, ui) {
1405
+ if (typeof paint !== 'object' || paint.visible === false || paint.opacity === 0)
1406
+ return undefined;
1407
+ const { boxBounds } = ui.__layout;
1408
+ switch (paint.type) {
1409
+ case 'solid':
1410
+ let { type, blendMode, color, opacity } = paint;
1411
+ return { type, blendMode, style: ColorConvert.string(color, opacity) };
1412
+ case 'image':
1413
+ return PaintImage.image(ui, attrName, paint, boxBounds, !recycleMap || !recycleMap[paint.url]);
1414
+ case 'linear':
1415
+ return PaintGradient.linearGradient(paint, boxBounds);
1416
+ case 'radial':
1417
+ return PaintGradient.radialGradient(paint, boxBounds);
1418
+ case 'angular':
1419
+ return PaintGradient.conicGradient(paint, boxBounds);
1420
+ default:
1421
+ return paint.r ? { type: 'solid', style: ColorConvert.string(paint) } : undefined;
1422
+ }
1423
+ }
1424
+
1425
+ const PaintModule = {
1426
+ compute,
1427
+ fill,
1428
+ fills,
1429
+ fillText,
1430
+ stroke,
1431
+ strokes,
1432
+ strokeText,
1433
+ drawTextStroke,
1434
+ shape
1435
+ };
1436
+
1437
+ let origin = {};
1438
+ const { get: get$4, rotateOfOuter: rotateOfOuter$2, translate: translate$1, scaleOfOuter: scaleOfOuter$2, scale: scaleHelper, rotate } = MatrixHelper;
1439
+ function fillOrFitMode(data, mode, box, width, height, rotation) {
1440
+ const transform = get$4();
1441
+ const swap = rotation && rotation !== 180;
1442
+ const sw = box.width / (swap ? height : width);
1443
+ const sh = box.height / (swap ? width : height);
1444
+ const scale = mode === 'fit' ? Math.min(sw, sh) : Math.max(sw, sh);
1445
+ const x = box.x + (box.width - width * scale) / 2;
1446
+ const y = box.y + (box.height - height * scale) / 2;
1447
+ translate$1(transform, x, y);
1448
+ scaleHelper(transform, scale);
1449
+ if (rotation)
1450
+ rotateOfOuter$2(transform, { x: box.x + box.width / 2, y: box.y + box.height / 2 }, rotation);
1451
+ data.scaleX = data.scaleY = scale;
1452
+ data.transform = transform;
1453
+ }
1454
+ function clipMode(data, box, x, y, scaleX, scaleY, rotation) {
1455
+ const transform = get$4();
1456
+ translate$1(transform, box.x, box.y);
1457
+ if (x || y)
1458
+ translate$1(transform, x, y);
1459
+ if (scaleX) {
1460
+ scaleHelper(transform, scaleX, scaleY);
1461
+ data.scaleX = transform.a;
1462
+ data.scaleY = transform.d;
1463
+ }
1464
+ if (rotation)
1465
+ rotate(transform, rotation);
1466
+ data.transform = transform;
1467
+ }
1468
+ function repeatMode(data, box, width, height, x, y, scaleX, scaleY, rotation) {
1469
+ const transform = get$4();
1470
+ if (rotation) {
1471
+ rotate(transform, rotation);
1472
+ switch (rotation) {
1473
+ case 90:
1474
+ translate$1(transform, height, 0);
1475
+ break;
1476
+ case 180:
1477
+ translate$1(transform, width, height);
1478
+ break;
1479
+ case 270:
1480
+ translate$1(transform, 0, width);
1481
+ break;
1482
+ }
1483
+ }
1484
+ origin.x = box.x;
1485
+ origin.y = box.y;
1486
+ if (x || y)
1487
+ origin.x += x, origin.y += y;
1488
+ translate$1(transform, origin.x, origin.y);
1489
+ if (scaleX) {
1490
+ scaleOfOuter$2(transform, origin, scaleX, scaleY);
1491
+ data.scaleX = scaleX;
1492
+ data.scaleY = scaleY;
1493
+ }
1494
+ data.transform = transform;
1495
+ }
1496
+
1497
+ const { get: get$3, translate } = MatrixHelper;
1498
+ function createData(leafPaint, image, paint, box) {
1499
+ let { width, height } = image;
1500
+ const { opacity, mode, offset, scale, size, rotation, blendMode, repeat } = paint;
1501
+ const sameBox = box.width === width && box.height === height;
1502
+ if (blendMode)
1503
+ leafPaint.blendMode = blendMode;
1504
+ const data = leafPaint.data = { mode };
1505
+ let x, y, scaleX, scaleY;
1506
+ if (offset)
1507
+ x = offset.x, y = offset.y;
1508
+ if (size) {
1509
+ scaleX = (typeof size === 'number' ? size : size.width) / width;
1510
+ scaleY = (typeof size === 'number' ? size : size.height) / height;
1511
+ }
1512
+ else if (scale) {
1513
+ scaleX = typeof scale === 'number' ? scale : scale.x;
1514
+ scaleY = typeof scale === 'number' ? scale : scale.y;
1515
+ }
1516
+ switch (mode) {
1517
+ case 'strench':
1518
+ if (!sameBox)
1519
+ width = box.width, height = box.height;
1520
+ if (box.x || box.y) {
1521
+ data.transform = get$3();
1522
+ translate(data.transform, box.x, box.y);
1523
+ }
1524
+ break;
1525
+ case 'clip':
1526
+ if (offset || scaleX || rotation)
1527
+ clipMode(data, box, x, y, scaleX, scaleY, rotation);
1528
+ break;
1529
+ case 'repeat':
1530
+ if (!sameBox || scaleX || rotation)
1531
+ repeatMode(data, box, width, height, x, y, scaleX, scaleY, rotation);
1532
+ if (!repeat)
1533
+ data.repeat = 'repeat';
1534
+ break;
1535
+ case 'fit':
1536
+ case 'cover':
1537
+ default:
1538
+ if (!sameBox || rotation)
1539
+ fillOrFitMode(data, mode, box, width, height, rotation);
1540
+ }
1541
+ data.width = width;
1542
+ data.height = height;
1543
+ if (opacity)
1544
+ data.opacity = opacity;
1545
+ if (repeat)
1546
+ data.repeat = typeof repeat === 'string' ? (repeat === 'x' ? 'repeat-x' : 'repeat-y') : 'repeat';
1547
+ }
1548
+
1549
+ let cache, box = new Bounds();
1550
+ const { isSame } = BoundsHelper;
1551
+ function image(ui, attrName, paint, boxBounds, firstUse) {
1552
+ let leafPaint, event;
1553
+ const image = ImageManager.get(paint);
1554
+ if (cache && paint === cache.paint && isSame(boxBounds, cache.boxBounds)) {
1555
+ leafPaint = cache.leafPaint;
1556
+ }
1557
+ else {
1558
+ leafPaint = { type: paint.type };
1559
+ leafPaint.image = image;
1560
+ cache = image.use > 1 ? { leafPaint, paint, boxBounds: box.set(boxBounds) } : null;
1561
+ }
1562
+ if (firstUse || image.loading)
1563
+ event = { image, attrName, attrValue: paint };
1564
+ if (image.ready) {
1565
+ checkSizeAndCreateData(ui, attrName, paint, image, leafPaint, boxBounds);
1566
+ if (firstUse) {
1567
+ onLoad(ui, event);
1568
+ onLoadSuccess(ui, event);
1569
+ }
1570
+ }
1571
+ else if (image.error) {
1572
+ if (firstUse)
1573
+ onLoadError(ui, event, image.error);
1574
+ }
1575
+ else {
1576
+ if (firstUse)
1577
+ onLoad(ui, event);
1578
+ leafPaint.loadId = image.load(() => {
1579
+ if (!ui.destroyed) {
1580
+ if (checkSizeAndCreateData(ui, attrName, paint, image, leafPaint, boxBounds))
1581
+ ui.forceUpdate('surface');
1582
+ onLoadSuccess(ui, event);
1583
+ }
1584
+ leafPaint.loadId = null;
1585
+ }, (error) => {
1586
+ onLoadError(ui, event, error);
1587
+ leafPaint.loadId = null;
1588
+ });
1589
+ }
1590
+ return leafPaint;
1591
+ }
1592
+ function checkSizeAndCreateData(ui, attrName, paint, image, leafPaint, boxBounds) {
1593
+ if (attrName === 'fill' && !ui.__.__naturalWidth) {
1594
+ const data = ui.__;
1595
+ data.__naturalWidth = image.width;
1596
+ data.__naturalHeight = image.height;
1597
+ if (data.__autoWidth || data.__autoHeight) {
1598
+ ui.forceUpdate('width');
1599
+ if (ui.__proxyData) {
1600
+ ui.setProxyAttr('width', data.width);
1601
+ ui.setProxyAttr('height', data.height);
1602
+ }
1603
+ return false;
1604
+ }
1605
+ }
1606
+ if (!leafPaint.data)
1607
+ createData(leafPaint, image, paint, boxBounds);
1608
+ return true;
1609
+ }
1610
+ function onLoad(ui, event) {
1611
+ emit(ui, ImageEvent.LOAD, event);
1612
+ }
1613
+ function onLoadSuccess(ui, event) {
1614
+ emit(ui, ImageEvent.LOADED, event);
1615
+ }
1616
+ function onLoadError(ui, event, error) {
1617
+ event.error = error;
1618
+ ui.forceUpdate('surface');
1619
+ emit(ui, ImageEvent.ERROR, event);
1620
+ }
1621
+ function emit(ui, type, data) {
1622
+ if (ui.hasEvent(type))
1623
+ ui.emitEvent(new ImageEvent(type, data));
1624
+ }
1625
+
1626
+ const { get: get$2, scale, copy: copy$1 } = MatrixHelper;
1627
+ const { round, abs: abs$1 } = Math;
1628
+ function createPattern(ui, paint, pixelRatio) {
1629
+ let { scaleX, scaleY } = ui.__world;
1630
+ const id = scaleX + '-' + scaleY;
1631
+ if (paint.patternId !== id && !ui.destroyed) {
1632
+ scaleX = abs$1(scaleX);
1633
+ scaleY = abs$1(scaleY);
1634
+ const { image, data } = paint;
1635
+ let imageScale, imageMatrix, { width, height, scaleX: sx, scaleY: sy, opacity, transform, repeat } = data;
1636
+ if (sx) {
1637
+ imageMatrix = get$2();
1638
+ copy$1(imageMatrix, transform);
1639
+ scale(imageMatrix, 1 / sx, 1 / sy);
1640
+ scaleX *= sx;
1641
+ scaleY *= sy;
1642
+ }
1643
+ scaleX *= pixelRatio;
1644
+ scaleY *= pixelRatio;
1645
+ width *= scaleX;
1646
+ height *= scaleY;
1647
+ const size = width * height;
1648
+ if (!repeat) {
1649
+ if (size > Platform.image.maxCacheSize)
1650
+ return false;
1651
+ }
1652
+ let maxSize = Platform.image.maxPatternSize;
1653
+ if (!image.isSVG) {
1654
+ const imageSize = image.width * image.height;
1655
+ if (maxSize > imageSize)
1656
+ maxSize = imageSize;
1657
+ }
1658
+ if (size > maxSize)
1659
+ imageScale = Math.sqrt(size / maxSize);
1660
+ if (imageScale) {
1661
+ scaleX /= imageScale;
1662
+ scaleY /= imageScale;
1663
+ width /= imageScale;
1664
+ height /= imageScale;
1665
+ }
1666
+ if (sx) {
1667
+ scaleX /= sx;
1668
+ scaleY /= sy;
1669
+ }
1670
+ if (transform || scaleX !== 1 || scaleY !== 1) {
1671
+ if (!imageMatrix) {
1672
+ imageMatrix = get$2();
1673
+ if (transform)
1674
+ copy$1(imageMatrix, transform);
1675
+ }
1676
+ scale(imageMatrix, 1 / scaleX, 1 / scaleY);
1677
+ }
1678
+ const canvas = image.getCanvas(width < 1 ? 1 : round(width), height < 1 ? 1 : round(height), opacity);
1679
+ const pattern = image.getPattern(canvas, repeat || (Platform.origin.noRepeat || 'no-repeat'), imageMatrix, paint);
1680
+ paint.style = pattern;
1681
+ paint.patternId = id;
1682
+ return true;
1683
+ }
1684
+ else {
1685
+ return false;
1686
+ }
1687
+ }
1688
+
1689
+ /******************************************************************************
1690
+ Copyright (c) Microsoft Corporation.
1691
+
1692
+ Permission to use, copy, modify, and/or distribute this software for any
1693
+ purpose with or without fee is hereby granted.
1694
+
1695
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
1696
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
1697
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
1698
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
1699
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
1700
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
1701
+ PERFORMANCE OF THIS SOFTWARE.
1702
+ ***************************************************************************** */
1703
+ /* global Reflect, Promise, SuppressedError, Symbol */
1704
+
1705
+
1706
+ function __awaiter(thisArg, _arguments, P, generator) {
1707
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
1708
+ return new (P || (P = Promise))(function (resolve, reject) {
1709
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
1710
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
1711
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
1712
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
1713
+ });
1714
+ }
1715
+
1716
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
1717
+ var e = new Error(message);
1718
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
1719
+ };
1720
+
1721
+ const { abs } = Math;
1722
+ function checkImage(ui, canvas, paint, allowPaint) {
1723
+ const { scaleX, scaleY } = ui.__world;
1724
+ if (!paint.data || paint.patternId === scaleX + '-' + scaleY) {
1725
+ return false;
1726
+ }
1727
+ else {
1728
+ const { data } = paint;
1729
+ if (allowPaint) {
1730
+ if (!data.repeat) {
1731
+ let { width, height } = data;
1732
+ width *= abs(scaleX) * canvas.pixelRatio;
1733
+ height *= abs(scaleY) * canvas.pixelRatio;
1734
+ if (data.scaleX) {
1735
+ width *= data.scaleX;
1736
+ height *= data.scaleY;
1737
+ }
1738
+ allowPaint = width * height > Platform.image.maxCacheSize;
1739
+ }
1740
+ else {
1741
+ allowPaint = false;
1742
+ }
1743
+ }
1744
+ if (allowPaint) {
1745
+ canvas.save();
1746
+ canvas.clip();
1747
+ if (paint.blendMode)
1748
+ canvas.blendMode = paint.blendMode;
1749
+ if (data.opacity)
1750
+ canvas.opacity *= data.opacity;
1751
+ if (data.transform)
1752
+ canvas.transform(data.transform);
1753
+ canvas.drawImage(paint.image.view, 0, 0, data.width, data.height);
1754
+ canvas.restore();
1755
+ return true;
1756
+ }
1757
+ else {
1758
+ if (!paint.style || Export.running) {
1759
+ createPattern(ui, paint, canvas.pixelRatio);
1760
+ }
1761
+ else {
1762
+ if (!paint.patternTask) {
1763
+ paint.patternTask = ImageManager.patternTasker.add(() => __awaiter(this, void 0, void 0, function* () {
1764
+ paint.patternTask = null;
1765
+ if (canvas.bounds.hit(ui.__world))
1766
+ createPattern(ui, paint, canvas.pixelRatio);
1767
+ ui.forceUpdate('surface');
1768
+ }), 300);
1769
+ }
1770
+ }
1771
+ return false;
1772
+ }
1773
+ }
1774
+ }
1775
+
1776
+ function recycleImage(attrName, data) {
1777
+ const paints = data['_' + attrName];
1778
+ if (paints instanceof Array) {
1779
+ let image, recycleMap, input, url;
1780
+ for (let i = 0, len = paints.length; i < len; i++) {
1781
+ image = paints[i].image;
1782
+ url = image && image.url;
1783
+ if (url) {
1784
+ if (!recycleMap)
1785
+ recycleMap = {};
1786
+ recycleMap[url] = true;
1787
+ ImageManager.recycle(image);
1788
+ if (image.loading) {
1789
+ if (!input) {
1790
+ input = (data.__input && data.__input[attrName]) || [];
1791
+ if (!(input instanceof Array))
1792
+ input = [input];
1793
+ }
1794
+ image.unload(paints[i].loadId, !input.some((item) => item.url === url));
1795
+ }
1796
+ }
1797
+ }
1798
+ return recycleMap;
1799
+ }
1800
+ return null;
1801
+ }
1802
+
1803
+ const PaintImageModule = {
1804
+ image,
1805
+ createData,
1806
+ fillOrFitMode,
1807
+ clipMode,
1808
+ repeatMode,
1809
+ createPattern,
1810
+ checkImage,
1811
+ recycleImage
1812
+ };
1813
+
1814
+ const defaultFrom$2 = { x: 0.5, y: 0 };
1815
+ const defaultTo$2 = { x: 0.5, y: 1 };
1816
+ function linearGradient(paint, box) {
1817
+ let { from, to, type, blendMode, opacity } = paint;
1818
+ from || (from = defaultFrom$2);
1819
+ to || (to = defaultTo$2);
1820
+ const style = Platform.canvas.createLinearGradient(box.x + from.x * box.width, box.y + from.y * box.height, box.x + to.x * box.width, box.y + to.y * box.height);
1821
+ applyStops(style, paint.stops, opacity);
1822
+ const data = { type, style };
1823
+ if (blendMode)
1824
+ data.blendMode = blendMode;
1825
+ return data;
1826
+ }
1827
+ function applyStops(gradient, stops, opacity) {
1828
+ let stop;
1829
+ for (let i = 0, len = stops.length; i < len; i++) {
1830
+ stop = stops[i];
1831
+ gradient.addColorStop(stop.offset, ColorConvert.string(stop.color, opacity));
1832
+ }
1833
+ }
1834
+
1835
+ const { set: set$1, getAngle: getAngle$1, getDistance: getDistance$1 } = PointHelper;
1836
+ const { get: get$1, rotateOfOuter: rotateOfOuter$1, scaleOfOuter: scaleOfOuter$1 } = MatrixHelper;
1837
+ const defaultFrom$1 = { x: 0.5, y: 0.5 };
1838
+ const defaultTo$1 = { x: 0.5, y: 1 };
1839
+ const realFrom$1 = {};
1840
+ const realTo$1 = {};
1841
+ function radialGradient(paint, box) {
1842
+ let { from, to, type, opacity, blendMode, stretch } = paint;
1843
+ from || (from = defaultFrom$1);
1844
+ to || (to = defaultTo$1);
1845
+ const { x, y, width, height } = box;
1846
+ set$1(realFrom$1, x + from.x * width, y + from.y * height);
1847
+ set$1(realTo$1, x + to.x * width, y + to.y * height);
1848
+ let transform;
1849
+ if (width !== height || stretch) {
1850
+ transform = get$1();
1851
+ scaleOfOuter$1(transform, realFrom$1, width / height * (stretch || 1), 1);
1852
+ rotateOfOuter$1(transform, realFrom$1, getAngle$1(realFrom$1, realTo$1) + 90);
1853
+ }
1854
+ const style = Platform.canvas.createRadialGradient(realFrom$1.x, realFrom$1.y, 0, realFrom$1.x, realFrom$1.y, getDistance$1(realFrom$1, realTo$1));
1855
+ applyStops(style, paint.stops, opacity);
1856
+ const data = { type, style, transform };
1857
+ if (blendMode)
1858
+ data.blendMode = blendMode;
1859
+ return data;
1860
+ }
1861
+
1862
+ const { set, getAngle, getDistance } = PointHelper;
1863
+ const { get, rotateOfOuter, scaleOfOuter } = MatrixHelper;
1864
+ const defaultFrom = { x: 0.5, y: 0.5 };
1865
+ const defaultTo = { x: 0.5, y: 1 };
1866
+ const realFrom = {};
1867
+ const realTo = {};
1868
+ function conicGradient(paint, box) {
1869
+ let { from, to, type, opacity, blendMode, stretch } = paint;
1870
+ from || (from = defaultFrom);
1871
+ to || (to = defaultTo);
1872
+ const { x, y, width, height } = box;
1873
+ set(realFrom, x + from.x * width, y + from.y * height);
1874
+ set(realTo, x + to.x * width, y + to.y * height);
1875
+ const transform = get();
1876
+ const angle = getAngle(realFrom, realTo);
1877
+ if (Platform.conicGradientRotate90) {
1878
+ scaleOfOuter(transform, realFrom, width / height * (stretch || 1), 1);
1879
+ rotateOfOuter(transform, realFrom, angle + 90);
1880
+ }
1881
+ else {
1882
+ scaleOfOuter(transform, realFrom, 1, width / height * (stretch || 1));
1883
+ rotateOfOuter(transform, realFrom, angle);
1884
+ }
1885
+ const style = Platform.conicGradientSupport ? Platform.canvas.createConicGradient(0, realFrom.x, realFrom.y) : Platform.canvas.createRadialGradient(realFrom.x, realFrom.y, 0, realFrom.x, realFrom.y, getDistance(realFrom, realTo));
1886
+ applyStops(style, paint.stops, opacity);
1887
+ const data = { type, style, transform };
1888
+ if (blendMode)
1889
+ data.blendMode = blendMode;
1890
+ return data;
1891
+ }
1892
+
1893
+ const PaintGradientModule = {
1894
+ linearGradient,
1895
+ radialGradient,
1896
+ conicGradient
1897
+ };
1898
+
1899
+ const { copy, toOffsetOutBounds: toOffsetOutBounds$1 } = BoundsHelper;
1900
+ const tempBounds = {};
1901
+ const offsetOutBounds$1 = {};
1902
+ function shadow(ui, current, shape) {
1903
+ let copyBounds, spreadScale;
1904
+ const { __nowWorld: nowWorld, __layout } = ui;
1905
+ const { shadow } = ui.__;
1906
+ const { worldCanvas, bounds, shapeBounds, scaleX, scaleY } = shape;
1907
+ const other = current.getSameCanvas();
1908
+ const end = shadow.length - 1;
1909
+ toOffsetOutBounds$1(bounds, offsetOutBounds$1);
1910
+ shadow.forEach((item, index) => {
1911
+ other.setWorldShadow((offsetOutBounds$1.offsetX + item.x * scaleX), (offsetOutBounds$1.offsetY + item.y * scaleY), item.blur * scaleX, item.color);
1912
+ spreadScale = item.spread ? 1 + item.spread * 2 / (__layout.boxBounds.width + (__layout.strokeBoxSpread || 0) * 2) : 0;
1913
+ drawWorldShadow(other, offsetOutBounds$1, spreadScale, shape);
1914
+ copyBounds = bounds;
1915
+ if (item.box) {
1916
+ other.restore();
1917
+ other.save();
1918
+ if (worldCanvas) {
1919
+ other.copyWorld(other, bounds, nowWorld, 'copy');
1920
+ copyBounds = nowWorld;
1921
+ }
1922
+ worldCanvas ? other.copyWorld(worldCanvas, nowWorld, nowWorld, 'destination-out') : other.copyWorld(shape.canvas, shapeBounds, bounds, 'destination-out');
1923
+ }
1924
+ if (ui.__worldFlipped) {
1925
+ current.copyWorldByReset(other, copyBounds, nowWorld, item.blendMode);
1926
+ }
1927
+ else {
1928
+ current.copyWorldToInner(other, copyBounds, __layout.renderBounds, item.blendMode);
1929
+ }
1930
+ if (end && index < end)
1931
+ other.clearWorld(copyBounds, true);
1932
+ });
1933
+ other.recycle(copyBounds);
1934
+ }
1935
+ function drawWorldShadow(canvas, outBounds, spreadScale, shape) {
1936
+ const { bounds, shapeBounds } = shape;
1937
+ if (Platform.fullImageShadow) {
1938
+ copy(tempBounds, canvas.bounds);
1939
+ tempBounds.x += (outBounds.x - shapeBounds.x);
1940
+ tempBounds.y += (outBounds.y - shapeBounds.y);
1941
+ if (spreadScale) {
1942
+ const { matrix } = shape;
1943
+ tempBounds.x -= (bounds.x + (matrix ? matrix.e : 0) + bounds.width / 2) * (spreadScale - 1);
1944
+ tempBounds.y -= (bounds.y + (matrix ? matrix.f : 0) + bounds.height / 2) * (spreadScale - 1);
1945
+ tempBounds.width *= spreadScale;
1946
+ tempBounds.height *= spreadScale;
1947
+ }
1948
+ canvas.copyWorld(shape.canvas, canvas.bounds, tempBounds);
1949
+ }
1950
+ else {
1951
+ if (spreadScale) {
1952
+ copy(tempBounds, outBounds);
1953
+ tempBounds.x -= (outBounds.width / 2) * (spreadScale - 1);
1954
+ tempBounds.y -= (outBounds.height / 2) * (spreadScale - 1);
1955
+ tempBounds.width *= spreadScale;
1956
+ tempBounds.height *= spreadScale;
1957
+ }
1958
+ canvas.copyWorld(shape.canvas, shapeBounds, spreadScale ? tempBounds : outBounds);
1959
+ }
1960
+ }
1961
+
1962
+ const { toOffsetOutBounds } = BoundsHelper;
1963
+ const offsetOutBounds = {};
1964
+ function innerShadow(ui, current, shape) {
1965
+ let copyBounds, spreadScale;
1966
+ const { __nowWorld: nowWorld, __layout: __layout } = ui;
1967
+ const { innerShadow } = ui.__;
1968
+ const { worldCanvas, bounds, shapeBounds, scaleX, scaleY } = shape;
1969
+ const other = current.getSameCanvas();
1970
+ const end = innerShadow.length - 1;
1971
+ toOffsetOutBounds(bounds, offsetOutBounds);
1972
+ innerShadow.forEach((item, index) => {
1973
+ other.save();
1974
+ other.setWorldShadow((offsetOutBounds.offsetX + item.x * scaleX), (offsetOutBounds.offsetY + item.y * scaleY), item.blur * scaleX);
1975
+ spreadScale = item.spread ? 1 - item.spread * 2 / (__layout.boxBounds.width + (__layout.strokeBoxSpread || 0) * 2) : 0;
1976
+ drawWorldShadow(other, offsetOutBounds, spreadScale, shape);
1977
+ other.restore();
1978
+ if (worldCanvas) {
1979
+ other.copyWorld(other, bounds, nowWorld, 'copy');
1980
+ other.copyWorld(worldCanvas, nowWorld, nowWorld, 'source-out');
1981
+ copyBounds = nowWorld;
1982
+ }
1983
+ else {
1984
+ other.copyWorld(shape.canvas, shapeBounds, bounds, 'source-out');
1985
+ copyBounds = bounds;
1986
+ }
1987
+ other.fillWorld(copyBounds, item.color, 'source-in');
1988
+ if (ui.__worldFlipped) {
1989
+ current.copyWorldByReset(other, copyBounds, nowWorld, item.blendMode);
1990
+ }
1991
+ else {
1992
+ current.copyWorldToInner(other, copyBounds, __layout.renderBounds, item.blendMode);
1993
+ }
1994
+ if (end && index < end)
1995
+ other.clearWorld(copyBounds, true);
1996
+ });
1997
+ other.recycle(copyBounds);
1998
+ }
1999
+
2000
+ function blur(ui, current, origin) {
2001
+ const { blur } = ui.__;
2002
+ origin.setWorldBlur(blur * ui.__world.a);
2003
+ origin.copyWorldToInner(current, ui.__world, ui.__layout.renderBounds);
2004
+ origin.filter = 'none';
2005
+ }
2006
+
2007
+ function backgroundBlur(_ui, _current, _shape) {
2008
+ }
2009
+
2010
+ const EffectModule = {
2011
+ shadow,
2012
+ innerShadow,
2013
+ blur,
2014
+ backgroundBlur
2015
+ };
2016
+
2017
+ const { excludeRenderBounds } = LeafBoundsHelper;
2018
+ Group.prototype.__renderMask = function (canvas, options) {
2019
+ let child, maskCanvas, contentCanvas, maskOpacity, currentMask;
2020
+ const { children } = this;
2021
+ for (let i = 0, len = children.length; i < len; i++) {
2022
+ child = children[i];
2023
+ if (child.__.mask) {
2024
+ if (currentMask) {
2025
+ maskEnd(this, currentMask, canvas, contentCanvas, maskCanvas, maskOpacity);
2026
+ maskCanvas = contentCanvas = null;
2027
+ }
2028
+ if (child.__.maskType === 'path') {
2029
+ if (child.opacity < 1) {
2030
+ currentMask = 'opacity-path';
2031
+ maskOpacity = child.opacity;
2032
+ if (!contentCanvas)
2033
+ contentCanvas = getCanvas(canvas);
2034
+ }
2035
+ else {
2036
+ currentMask = 'path';
2037
+ canvas.save();
2038
+ }
2039
+ child.__clip(contentCanvas || canvas, options);
2040
+ }
2041
+ else {
2042
+ currentMask = 'alpha';
2043
+ if (!maskCanvas)
2044
+ maskCanvas = getCanvas(canvas);
2045
+ if (!contentCanvas)
2046
+ contentCanvas = getCanvas(canvas);
2047
+ child.__render(maskCanvas, options);
2048
+ }
2049
+ if (child.__.maskType !== 'clipping')
2050
+ continue;
2051
+ }
2052
+ if (excludeRenderBounds(child, options))
2053
+ continue;
2054
+ child.__render(contentCanvas || canvas, options);
2055
+ }
2056
+ maskEnd(this, currentMask, canvas, contentCanvas, maskCanvas, maskOpacity);
2057
+ };
2058
+ function maskEnd(leaf, maskMode, canvas, contentCanvas, maskCanvas, maskOpacity) {
2059
+ switch (maskMode) {
2060
+ case 'alpha':
2061
+ usePixelMask(leaf, canvas, contentCanvas, maskCanvas);
2062
+ break;
2063
+ case 'opacity-path':
2064
+ copyContent(leaf, canvas, contentCanvas, maskOpacity);
2065
+ break;
2066
+ case 'path':
2067
+ canvas.restore();
2068
+ }
2069
+ }
2070
+ function getCanvas(canvas) {
2071
+ return canvas.getSameCanvas(false, true);
2072
+ }
2073
+ function usePixelMask(leaf, canvas, content, mask) {
2074
+ const realBounds = leaf.__nowWorld;
2075
+ content.resetTransform();
2076
+ content.opacity = 1;
2077
+ content.useMask(mask, realBounds);
2078
+ mask.recycle(realBounds);
2079
+ copyContent(leaf, canvas, content, 1);
2080
+ }
2081
+ function copyContent(leaf, canvas, content, maskOpacity) {
2082
+ const realBounds = leaf.__nowWorld;
2083
+ canvas.resetTransform();
2084
+ canvas.opacity = maskOpacity;
2085
+ canvas.copyWorld(content, realBounds);
2086
+ content.recycle(realBounds);
2087
+ }
2088
+
2089
+ const money = '¥¥$€££¢¢';
2090
+ const letter = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
2091
+ const langBefore = '《(「〈『〖【〔{┌<‘“=' + money;
2092
+ const langAfter = '》)」〉』〗】〕}┐>’”!?,、。:;‰';
2093
+ const langSymbol = '≮≯≈≠=…';
2094
+ const langBreak$1 = '—/~|┆·';
2095
+ const beforeChar = '{[(<\'"' + langBefore;
2096
+ const afterChar = '>)]}%!?,.:;\'"' + langAfter;
2097
+ const symbolChar = afterChar + '_#~&*+\\=|' + langSymbol;
2098
+ const breakChar = '- ' + langBreak$1;
2099
+ const cjkRangeList = [
2100
+ [0x4E00, 0x9FFF],
2101
+ [0x3400, 0x4DBF],
2102
+ [0x20000, 0x2A6DF],
2103
+ [0x2A700, 0x2B73F],
2104
+ [0x2B740, 0x2B81F],
2105
+ [0x2B820, 0x2CEAF],
2106
+ [0x2CEB0, 0x2EBEF],
2107
+ [0x30000, 0x3134F],
2108
+ [0x31350, 0x323AF],
2109
+ [0x2E80, 0x2EFF],
2110
+ [0x2F00, 0x2FDF],
2111
+ [0x2FF0, 0x2FFF],
2112
+ [0x3000, 0x303F],
2113
+ [0x31C0, 0x31EF],
2114
+ [0x3200, 0x32FF],
2115
+ [0x3300, 0x33FF],
2116
+ [0xF900, 0xFAFF],
2117
+ [0xFE30, 0xFE4F],
2118
+ [0x1F200, 0x1F2FF],
2119
+ [0x2F800, 0x2FA1F],
2120
+ ];
2121
+ const cjkReg = new RegExp(cjkRangeList.map(([start, end]) => `[\\u${start.toString(16)}-\\u${end.toString(16)}]`).join('|'));
2122
+ function mapChar(str) {
2123
+ const map = {};
2124
+ str.split('').forEach(char => map[char] = true);
2125
+ return map;
2126
+ }
2127
+ const letterMap = mapChar(letter);
2128
+ const beforeMap = mapChar(beforeChar);
2129
+ const afterMap = mapChar(afterChar);
2130
+ const symbolMap = mapChar(symbolChar);
2131
+ const breakMap = mapChar(breakChar);
2132
+ var CharType;
2133
+ (function (CharType) {
2134
+ CharType[CharType["Letter"] = 0] = "Letter";
2135
+ CharType[CharType["Single"] = 1] = "Single";
2136
+ CharType[CharType["Before"] = 2] = "Before";
2137
+ CharType[CharType["After"] = 3] = "After";
2138
+ CharType[CharType["Symbol"] = 4] = "Symbol";
2139
+ CharType[CharType["Break"] = 5] = "Break";
2140
+ })(CharType || (CharType = {}));
2141
+ const { Letter: Letter$1, Single: Single$1, Before: Before$1, After: After$1, Symbol: Symbol$1, Break: Break$1 } = CharType;
2142
+ function getCharType(char) {
2143
+ if (letterMap[char]) {
2144
+ return Letter$1;
2145
+ }
2146
+ else if (breakMap[char]) {
2147
+ return Break$1;
2148
+ }
2149
+ else if (beforeMap[char]) {
2150
+ return Before$1;
2151
+ }
2152
+ else if (afterMap[char]) {
2153
+ return After$1;
2154
+ }
2155
+ else if (symbolMap[char]) {
2156
+ return Symbol$1;
2157
+ }
2158
+ else if (cjkReg.test(char)) {
2159
+ return Single$1;
2160
+ }
2161
+ else {
2162
+ return Letter$1;
2163
+ }
2164
+ }
2165
+
2166
+ const TextRowHelper = {
2167
+ trimRight(row) {
2168
+ const { words } = row;
2169
+ let trimRight = 0, len = words.length, char;
2170
+ for (let i = len - 1; i > -1; i--) {
2171
+ char = words[i].data[0];
2172
+ if (char.char === ' ') {
2173
+ trimRight++;
2174
+ row.width -= char.width;
2175
+ }
2176
+ else {
2177
+ break;
2178
+ }
2179
+ }
2180
+ if (trimRight)
2181
+ words.splice(len - trimRight, trimRight);
2182
+ }
2183
+ };
2184
+
2185
+ function getTextCase(char, textCase, firstChar) {
2186
+ switch (textCase) {
2187
+ case 'title':
2188
+ return firstChar ? char.toUpperCase() : char;
2189
+ case 'upper':
2190
+ return char.toUpperCase();
2191
+ case 'lower':
2192
+ return char.toLowerCase();
2193
+ default:
2194
+ return char;
2195
+ }
2196
+ }
2197
+
2198
+ const { trimRight } = TextRowHelper;
2199
+ const { Letter, Single, Before, After, Symbol, Break } = CharType;
2200
+ let word, row, wordWidth, rowWidth, realWidth;
2201
+ let char, charWidth, startCharSize, charSize, charType, lastCharType, langBreak, afterBreak, paraStart;
2202
+ let textDrawData, rows = [], bounds;
2203
+ function createRows(drawData, content, style) {
2204
+ textDrawData = drawData;
2205
+ rows = drawData.rows;
2206
+ bounds = drawData.bounds;
2207
+ const { __letterSpacing, paraIndent, textCase } = style;
2208
+ const { canvas } = Platform;
2209
+ const { width, height } = bounds;
2210
+ const charMode = width || height || __letterSpacing || (textCase !== 'none');
2211
+ if (charMode) {
2212
+ const wrap = style.textWrap !== 'none';
2213
+ const breakAll = style.textWrap === 'break';
2214
+ paraStart = true;
2215
+ lastCharType = null;
2216
+ startCharSize = charWidth = charSize = wordWidth = rowWidth = 0;
2217
+ word = { data: [] }, row = { words: [] };
2218
+ for (let i = 0, len = content.length; i < len; i++) {
2219
+ char = content[i];
2220
+ if (char === '\n') {
2221
+ if (wordWidth)
2222
+ addWord();
2223
+ row.paraEnd = true;
2224
+ addRow();
2225
+ paraStart = true;
2226
+ }
2227
+ else {
2228
+ charType = getCharType(char);
2229
+ if (charType === Letter && textCase !== 'none')
2230
+ char = getTextCase(char, textCase, !wordWidth);
2231
+ charWidth = canvas.measureText(char).width;
2232
+ if (__letterSpacing) {
2233
+ if (__letterSpacing < 0)
2234
+ charSize = charWidth;
2235
+ charWidth += __letterSpacing;
2236
+ }
2237
+ langBreak = (charType === Single && (lastCharType === Single || lastCharType === Letter)) || (lastCharType === Single && charType !== After);
2238
+ afterBreak = ((charType === Before || charType === Single) && (lastCharType === Symbol || lastCharType === After));
2239
+ realWidth = paraStart && paraIndent ? width - paraIndent : width;
2240
+ if (wrap && (width && rowWidth + wordWidth + charWidth > realWidth)) {
2241
+ if (breakAll) {
2242
+ if (wordWidth)
2243
+ addWord();
2244
+ addRow();
2245
+ }
2246
+ else {
2247
+ if (!afterBreak)
2248
+ afterBreak = charType === Letter && lastCharType == After;
2249
+ if (langBreak || afterBreak || charType === Break || charType === Before || charType === Single || (wordWidth + charWidth > realWidth)) {
2250
+ if (wordWidth)
2251
+ addWord();
2252
+ addRow();
2253
+ }
2254
+ else {
2255
+ addRow();
2256
+ }
2257
+ }
2258
+ }
2259
+ if (char === ' ' && paraStart !== true && (rowWidth + wordWidth) === 0) ;
2260
+ else {
2261
+ if (charType === Break) {
2262
+ if (char === ' ' && wordWidth)
2263
+ addWord();
2264
+ addChar(char, charWidth);
2265
+ addWord();
2266
+ }
2267
+ else if (langBreak || afterBreak) {
2268
+ if (wordWidth)
2269
+ addWord();
2270
+ addChar(char, charWidth);
2271
+ }
2272
+ else {
2273
+ addChar(char, charWidth);
2274
+ }
2275
+ }
2276
+ lastCharType = charType;
2277
+ }
2278
+ }
2279
+ if (wordWidth)
2280
+ addWord();
2281
+ if (rowWidth)
2282
+ addRow();
2283
+ rows.length > 0 && (rows[rows.length - 1].paraEnd = true);
2284
+ }
2285
+ else {
2286
+ content.split('\n').forEach(content => {
2287
+ textDrawData.paraNumber++;
2288
+ rows.push({ x: paraIndent || 0, text: content, width: canvas.measureText(content).width, paraStart: true });
2289
+ });
2290
+ }
2291
+ }
2292
+ function addChar(char, width) {
2293
+ if (charSize && !startCharSize)
2294
+ startCharSize = charSize;
2295
+ word.data.push({ char, width });
2296
+ wordWidth += width;
2297
+ }
2298
+ function addWord() {
2299
+ rowWidth += wordWidth;
2300
+ word.width = wordWidth;
2301
+ row.words.push(word);
2302
+ word = { data: [] };
2303
+ wordWidth = 0;
2304
+ }
2305
+ function addRow() {
2306
+ if (paraStart) {
2307
+ textDrawData.paraNumber++;
2308
+ row.paraStart = true;
2309
+ paraStart = false;
2310
+ }
2311
+ if (charSize) {
2312
+ row.startCharSize = startCharSize;
2313
+ row.endCharSize = charSize;
2314
+ startCharSize = 0;
2315
+ }
2316
+ row.width = rowWidth;
2317
+ if (bounds.width)
2318
+ trimRight(row);
2319
+ rows.push(row);
2320
+ row = { words: [] };
2321
+ rowWidth = 0;
2322
+ }
2323
+
2324
+ const CharMode = 0;
2325
+ const WordMode = 1;
2326
+ const TextMode = 2;
2327
+ function layoutChar(drawData, style, width, _height) {
2328
+ const { rows } = drawData;
2329
+ const { textAlign, paraIndent, letterSpacing } = style;
2330
+ let charX, addWordWidth, indentWidth, mode, wordChar;
2331
+ rows.forEach(row => {
2332
+ if (row.words) {
2333
+ indentWidth = paraIndent && row.paraStart ? paraIndent : 0;
2334
+ addWordWidth = (width && textAlign === 'justify' && row.words.length > 1) ? (width - row.width - indentWidth) / (row.words.length - 1) : 0;
2335
+ mode = (letterSpacing || row.isOverflow) ? CharMode : (addWordWidth > 0.01 ? WordMode : TextMode);
2336
+ if (row.isOverflow && !letterSpacing)
2337
+ row.textMode = true;
2338
+ if (mode === TextMode) {
2339
+ row.x += indentWidth;
2340
+ toTextChar$1(row);
2341
+ }
2342
+ else {
2343
+ row.x += indentWidth;
2344
+ charX = row.x;
2345
+ row.data = [];
2346
+ row.words.forEach(word => {
2347
+ if (mode === WordMode) {
2348
+ wordChar = { char: '', x: charX };
2349
+ charX = toWordChar(word.data, charX, wordChar);
2350
+ if (wordChar.char !== ' ')
2351
+ row.data.push(wordChar);
2352
+ }
2353
+ else {
2354
+ charX = toChar(word.data, charX, row.data);
2355
+ }
2356
+ if (!row.paraEnd && addWordWidth) {
2357
+ charX += addWordWidth;
2358
+ row.width += addWordWidth;
2359
+ }
2360
+ });
2361
+ }
2362
+ row.words = null;
2363
+ }
2364
+ });
2365
+ }
2366
+ function toTextChar$1(row) {
2367
+ row.text = '';
2368
+ row.words.forEach(word => {
2369
+ word.data.forEach(char => {
2370
+ row.text += char.char;
2371
+ });
2372
+ });
2373
+ }
2374
+ function toWordChar(data, charX, wordChar) {
2375
+ data.forEach(char => {
2376
+ wordChar.char += char.char;
2377
+ charX += char.width;
2378
+ });
2379
+ return charX;
2380
+ }
2381
+ function toChar(data, charX, rowData) {
2382
+ data.forEach(char => {
2383
+ if (char.char !== ' ') {
2384
+ char.x = charX;
2385
+ rowData.push(char);
2386
+ }
2387
+ charX += char.width;
2388
+ });
2389
+ return charX;
2390
+ }
2391
+
2392
+ function layoutText(drawData, style) {
2393
+ const { rows, bounds } = drawData;
2394
+ const { __lineHeight, __baseLine, __letterSpacing, __clipText, textAlign, verticalAlign, paraSpacing } = style;
2395
+ let { x, y, width, height } = bounds, realHeight = __lineHeight * rows.length + (paraSpacing ? paraSpacing * (drawData.paraNumber - 1) : 0);
2396
+ let starY = __baseLine;
2397
+ if (__clipText && realHeight > height) {
2398
+ realHeight = Math.max(height, __lineHeight);
2399
+ drawData.overflow = rows.length;
2400
+ }
2401
+ else {
2402
+ switch (verticalAlign) {
2403
+ case 'middle':
2404
+ y += (height - realHeight) / 2;
2405
+ break;
2406
+ case 'bottom':
2407
+ y += (height - realHeight);
2408
+ }
2409
+ }
2410
+ starY += y;
2411
+ let row, rowX, rowWidth;
2412
+ for (let i = 0, len = rows.length; i < len; i++) {
2413
+ row = rows[i];
2414
+ row.x = x;
2415
+ switch (textAlign) {
2416
+ case 'center':
2417
+ row.x += (width - row.width) / 2;
2418
+ break;
2419
+ case 'right':
2420
+ row.x += width - row.width;
2421
+ }
2422
+ if (row.paraStart && paraSpacing && i > 0)
2423
+ starY += paraSpacing;
2424
+ row.y = starY;
2425
+ starY += __lineHeight;
2426
+ if (drawData.overflow > i && starY > realHeight) {
2427
+ row.isOverflow = true;
2428
+ drawData.overflow = i + 1;
2429
+ }
2430
+ rowX = row.x;
2431
+ rowWidth = row.width;
2432
+ if (__letterSpacing < 0) {
2433
+ if (row.width < 0) {
2434
+ rowWidth = -row.width + style.fontSize + __letterSpacing;
2435
+ rowX -= rowWidth;
2436
+ rowWidth += style.fontSize;
2437
+ }
2438
+ else {
2439
+ rowWidth -= __letterSpacing;
2440
+ }
2441
+ }
2442
+ if (rowX < bounds.x)
2443
+ bounds.x = rowX;
2444
+ if (rowWidth > bounds.width)
2445
+ bounds.width = rowWidth;
2446
+ if (__clipText && width && width < rowWidth) {
2447
+ row.isOverflow = true;
2448
+ if (!drawData.overflow)
2449
+ drawData.overflow = rows.length;
2450
+ }
2451
+ }
2452
+ bounds.y = y;
2453
+ bounds.height = realHeight;
2454
+ }
2455
+
2456
+ function clipText(drawData, style) {
2457
+ const { rows, overflow } = drawData;
2458
+ let { textOverflow } = style;
2459
+ rows.splice(overflow);
2460
+ if (textOverflow !== 'hide') {
2461
+ if (textOverflow === 'ellipsis')
2462
+ textOverflow = '...';
2463
+ let char, charRight;
2464
+ const ellipsisWidth = Platform.canvas.measureText(textOverflow).width;
2465
+ const right = style.x + style.width - ellipsisWidth;
2466
+ const list = style.textWrap === 'none' ? rows : [rows[overflow - 1]];
2467
+ list.forEach(row => {
2468
+ if (row.isOverflow && row.data) {
2469
+ let end = row.data.length - 1;
2470
+ for (let i = end; i > -1; i--) {
2471
+ char = row.data[i];
2472
+ charRight = char.x + char.width;
2473
+ if (i === end && charRight < right) {
2474
+ break;
2475
+ }
2476
+ else if (charRight < right && char.char !== ' ') {
2477
+ row.data.splice(i + 1);
2478
+ row.width -= char.width;
2479
+ break;
2480
+ }
2481
+ row.width -= char.width;
2482
+ }
2483
+ row.width += ellipsisWidth;
2484
+ row.data.push({ char: textOverflow, x: charRight });
2485
+ if (row.textMode)
2486
+ toTextChar(row);
2487
+ }
2488
+ });
2489
+ }
2490
+ }
2491
+ function toTextChar(row) {
2492
+ row.text = '';
2493
+ row.data.forEach(char => {
2494
+ row.text += char.char;
2495
+ });
2496
+ row.data = null;
2497
+ }
2498
+
2499
+ function decorationText(drawData, style) {
2500
+ const { fontSize } = style;
2501
+ drawData.decorationHeight = fontSize / 11;
2502
+ switch (style.textDecoration) {
2503
+ case 'under':
2504
+ drawData.decorationY = fontSize * 0.15;
2505
+ break;
2506
+ case 'delete':
2507
+ drawData.decorationY = -fontSize * 0.35;
2508
+ }
2509
+ }
2510
+
2511
+ const { top, right, bottom, left } = Direction4;
2512
+ function getDrawData(content, style) {
2513
+ if (typeof content !== 'string')
2514
+ content = String(content);
2515
+ let x = 0, y = 0;
2516
+ let width = style.__getInput('width') || 0;
2517
+ let height = style.__getInput('height') || 0;
2518
+ const { textDecoration, __font, __padding: padding } = style;
2519
+ if (padding) {
2520
+ if (width) {
2521
+ x = padding[left];
2522
+ width -= (padding[right] + padding[left]);
2523
+ }
2524
+ if (height) {
2525
+ y = padding[top];
2526
+ height -= (padding[top] + padding[bottom]);
2527
+ }
2528
+ }
2529
+ const drawData = {
2530
+ bounds: { x, y, width, height },
2531
+ rows: [],
2532
+ paraNumber: 0,
2533
+ font: Platform.canvas.font = __font
2534
+ };
2535
+ createRows(drawData, content, style);
2536
+ if (padding)
2537
+ padAutoText(padding, drawData, style, width, height);
2538
+ layoutText(drawData, style);
2539
+ layoutChar(drawData, style, width);
2540
+ if (drawData.overflow)
2541
+ clipText(drawData, style);
2542
+ if (textDecoration !== 'none')
2543
+ decorationText(drawData, style);
2544
+ return drawData;
2545
+ }
2546
+ function padAutoText(padding, drawData, style, width, height) {
2547
+ if (!width) {
2548
+ switch (style.textAlign) {
2549
+ case 'left':
2550
+ offsetText(drawData, 'x', padding[left]);
2551
+ break;
2552
+ case 'right':
2553
+ offsetText(drawData, 'x', -padding[right]);
2554
+ }
2555
+ }
2556
+ if (!height) {
2557
+ switch (style.verticalAlign) {
2558
+ case 'top':
2559
+ offsetText(drawData, 'y', padding[top]);
2560
+ break;
2561
+ case 'bottom':
2562
+ offsetText(drawData, 'y', -padding[bottom]);
2563
+ }
2564
+ }
2565
+ }
2566
+ function offsetText(drawData, attrName, value) {
2567
+ const { bounds, rows } = drawData;
2568
+ bounds[attrName] += value;
2569
+ for (let i = 0; i < rows.length; i++)
2570
+ rows[i][attrName] += value;
2571
+ }
2572
+
2573
+ const TextConvertModule = {
2574
+ getDrawData
2575
+ };
2576
+
2577
+ function string(color, opacity) {
2578
+ if (typeof color === 'string')
2579
+ return color;
2580
+ let a = color.a === undefined ? 1 : color.a;
2581
+ if (opacity)
2582
+ a *= opacity;
2583
+ const rgb = color.r + ',' + color.g + ',' + color.b;
2584
+ return a === 1 ? 'rgb(' + rgb + ')' : 'rgba(' + rgb + ',' + a + ')';
2585
+ }
2586
+
2587
+ const ColorConvertModule = {
2588
+ string
2589
+ };
2590
+
2591
+ const { setPoint, addPoint, toBounds } = TwoPointBoundsHelper;
2592
+ function getTrimBounds(canvas) {
2593
+ const { width, height } = canvas.view;
2594
+ const { data } = canvas.context.getImageData(0, 0, width, height);
2595
+ let x, y, pointBounds, index = 0;
2596
+ for (let i = 0; i < data.length; i += 4) {
2597
+ if (data[i + 3] !== 0) {
2598
+ x = index % width;
2599
+ y = (index - x) / width;
2600
+ pointBounds ? addPoint(pointBounds, x, y) : setPoint(pointBounds = {}, x, y);
2601
+ }
2602
+ index++;
2603
+ }
2604
+ const bounds = new Bounds();
2605
+ toBounds(pointBounds, bounds);
2606
+ return bounds.scale(1 / canvas.pixelRatio).ceil();
2607
+ }
2608
+
2609
+ const ExportModule = {
2610
+ export(leaf, filename, options) {
2611
+ this.running = true;
2612
+ return addTask((success) => new Promise((resolve) => {
2613
+ const over = (result) => {
2614
+ success(result);
2615
+ resolve();
2616
+ this.running = false;
2617
+ };
2618
+ const { leafer } = leaf;
2619
+ if (leafer) {
2620
+ leafer.waitViewCompleted(() => __awaiter(this, void 0, void 0, function* () {
2621
+ let renderBounds, trimBounds, scaleX = 1, scaleY = 1;
2622
+ options = FileHelper.getExportOptions(options);
2623
+ const { scale, slice, trim } = options;
2624
+ const pixelRatio = options.pixelRatio || 1;
2625
+ const screenshot = options.screenshot || leaf.isApp;
2626
+ const fill = options.fill === undefined ? ((leaf.isLeafer && screenshot) ? leaf.fill : '') : options.fill;
2627
+ const needFill = FileHelper.isOpaqueImage(filename) || fill, matrix = new Matrix();
2628
+ if (screenshot) {
2629
+ renderBounds = screenshot === true ? (leaf.isLeafer ? leafer.canvas.bounds : leaf.worldRenderBounds) : screenshot;
2630
+ }
2631
+ else {
2632
+ const { localTransform, __world: world } = leaf;
2633
+ matrix.set(world).divide(localTransform).invert();
2634
+ scaleX = 1 / (world.scaleX / leaf.scaleX);
2635
+ scaleY = 1 / (world.scaleY / leaf.scaleY);
2636
+ renderBounds = leaf.getBounds('render', 'local');
2637
+ }
2638
+ let { x, y, width, height } = renderBounds;
2639
+ if (scale) {
2640
+ matrix.scale(scale);
2641
+ width *= scale, height *= scale;
2642
+ scaleX *= scale, scaleY *= scale;
2643
+ }
2644
+ let canvas = Creator.canvas({ width: Math.ceil(width), height: Math.ceil(width), pixelRatio });
2645
+ const renderOptions = { matrix: matrix.translate(-x, -y).withScale(scaleX, scaleY) };
2646
+ if (slice) {
2647
+ leaf = leafer;
2648
+ renderOptions.bounds = canvas.bounds;
2649
+ }
2650
+ canvas.save();
2651
+ leaf.__render(canvas, renderOptions);
2652
+ canvas.restore();
2653
+ if (trim) {
2654
+ trimBounds = getTrimBounds(canvas);
2655
+ const old = canvas, { width, height } = trimBounds;
2656
+ const config = { x: 0, y: 0, width, height, pixelRatio };
2657
+ canvas = Creator.canvas(config);
2658
+ canvas.copyWorld(old, trimBounds, config);
2659
+ }
2660
+ if (needFill)
2661
+ canvas.fillWorld(canvas.bounds, fill || '#FFFFFF', 'destination-over');
2662
+ const data = filename === 'canvas' ? canvas : yield canvas.export(filename, options);
2663
+ over({ data, width: canvas.pixelWidth, height: canvas.pixelHeight, renderBounds, trimBounds });
2664
+ }));
2665
+ }
2666
+ else {
2667
+ over({ data: false });
2668
+ }
2669
+ }));
2670
+ }
2671
+ };
2672
+ let tasker;
2673
+ function addTask(task) {
2674
+ if (!tasker)
2675
+ tasker = new TaskProcessor();
2676
+ return new Promise((resolve) => {
2677
+ tasker.add(() => __awaiter(this, void 0, void 0, function* () { return yield task(resolve); }), { parallel: false });
2678
+ });
2679
+ }
2680
+
2681
+ Object.assign(TextConvert, TextConvertModule);
2682
+ Object.assign(ColorConvert, ColorConvertModule);
2683
+ Object.assign(Paint, PaintModule);
2684
+ Object.assign(PaintImage, PaintImageModule);
2685
+ Object.assign(PaintGradient, PaintGradientModule);
2686
+ Object.assign(Effect, EffectModule);
2687
+ Object.assign(Export, ExportModule);
2688
+
2689
+ Object.assign(Creator, {
2690
+ interaction: (target, canvas, selector, options) => new Interaction(target, canvas, selector, options),
2691
+ hitCanvas: (options, manager) => new LeaferCanvas(options, manager),
2692
+ hitCanvasManager: () => new HitCanvasManager()
2693
+ });
2694
+ try {
2695
+ useCanvas('wx', wx);
2696
+ }
2697
+ catch (_a) { }
2698
+
2699
+ LeaferCanvas.prototype.__createContext = function () {
2700
+ if (this.viewSelect) {
2701
+ const offscreenView = Platform$1.origin.createCanvas(1, 1);
2702
+ const context = this.view.getContext('2d');
2703
+ this.testView = this.view;
2704
+ this.testContext = context;
2705
+ this.view = offscreenView;
2706
+ }
2707
+ this.context = this.view.getContext('2d');
2708
+ this.__bindContext();
2709
+ };
2710
+ LeaferCanvas.prototype.updateRender = function () {
2711
+ if (this.testView) {
2712
+ let pattern = this.context.createPattern(this.view, Platform$1.origin.noRepeat);
2713
+ this.testContext.clearRect(0, 0, this.view.width, this.view.height);
2714
+ this.testContext.fillStyle = pattern;
2715
+ this.testContext.fillRect(0, 0, this.view.width, this.view.height);
2716
+ this.testContext.fillStyle = pattern = null;
2717
+ }
2718
+ };
2719
+ LeaferCanvas.prototype.updateViewSize = function () {
2720
+ const { width, height, pixelRatio, view, testView } = this;
2721
+ view.width = width * pixelRatio;
2722
+ view.height = height * pixelRatio;
2723
+ if (testView) {
2724
+ testView.width = view.width;
2725
+ testView.height = view.height;
2726
+ }
2727
+ };
2728
+
2729
+ export { Interaction, Layouter, LeaferCanvas, Renderer, Selector, Watcher, useCanvas };