@leafer-ui/worker 1.0.0-beta.12 → 1.0.0-beta.16

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,2193 @@
1
+ import { LeafList, DataHelper, RenderEvent, ChildEvent, WatchEvent, PropertyEvent, LeafHelper, BranchHelper, Bounds, LeafBoundsHelper, BoundsHelper, Debug, LeafLevelList, LayoutEvent, Run, ImageManager, Platform, AnimateEvent, ResizeEvent, Creator, LeaferCanvasBase, canvasPatch, LeaferImage, InteractionBase, FileHelper, MatrixHelper, ImageEvent, PointHelper, MathHelper, TaskProcessor } from '@leafer/core';
2
+ export * from '@leafer/core';
3
+ export { LeaferImage } from '@leafer/core';
4
+ import { ColorConvert as ColorConvert$1, Paint, Effect, TextConvert as TextConvert$1, Export as Export$1 } from '@leafer-ui/core';
5
+ export * from '@leafer-ui/core';
6
+
7
+ class Watcher {
8
+ get childrenChanged() { return this.hasAdd || this.hasRemove || this.hasVisible; }
9
+ get updatedList() {
10
+ if (this.hasRemove) {
11
+ const updatedList = new LeafList();
12
+ this.__updatedList.list.forEach(item => { if (item.leafer)
13
+ updatedList.push(item); });
14
+ return updatedList;
15
+ }
16
+ else {
17
+ return this.__updatedList;
18
+ }
19
+ }
20
+ constructor(target, userConfig) {
21
+ this.totalTimes = 0;
22
+ this.config = {};
23
+ this.__updatedList = new LeafList();
24
+ this.target = target;
25
+ if (userConfig)
26
+ this.config = DataHelper.default(userConfig, this.config);
27
+ this.__listenEvents();
28
+ }
29
+ start() {
30
+ if (this.disabled)
31
+ return;
32
+ this.running = true;
33
+ }
34
+ stop() {
35
+ this.running = false;
36
+ }
37
+ disable() {
38
+ this.stop();
39
+ this.__removeListenEvents();
40
+ this.disabled = true;
41
+ }
42
+ update() {
43
+ this.changed = true;
44
+ if (this.running)
45
+ this.target.emit(RenderEvent.REQUEST);
46
+ }
47
+ __onAttrChange(event) {
48
+ this.__updatedList.push(event.target);
49
+ this.update();
50
+ }
51
+ __onChildEvent(event) {
52
+ if (event.type === ChildEvent.ADD) {
53
+ this.hasAdd = true;
54
+ this.__pushChild(event.child);
55
+ }
56
+ else {
57
+ this.hasRemove = true;
58
+ this.__updatedList.push(event.parent);
59
+ }
60
+ this.update();
61
+ }
62
+ __pushChild(child) {
63
+ this.__updatedList.push(child);
64
+ if (child.isBranch)
65
+ this.__loopChildren(child);
66
+ }
67
+ __loopChildren(parent) {
68
+ const { children } = parent;
69
+ for (let i = 0, len = children.length; i < len; i++)
70
+ this.__pushChild(children[i]);
71
+ }
72
+ __onRquestData() {
73
+ this.target.emitEvent(new WatchEvent(WatchEvent.DATA, { updatedList: this.updatedList }));
74
+ this.__updatedList = new LeafList();
75
+ this.totalTimes++;
76
+ this.changed = false;
77
+ this.hasVisible = false;
78
+ this.hasRemove = false;
79
+ this.hasAdd = false;
80
+ }
81
+ __listenEvents() {
82
+ const { target } = this;
83
+ this.__eventIds = [
84
+ target.on_(PropertyEvent.CHANGE, this.__onAttrChange, this),
85
+ target.on_([ChildEvent.ADD, ChildEvent.REMOVE], this.__onChildEvent, this),
86
+ target.on_(WatchEvent.REQUEST, this.__onRquestData, this)
87
+ ];
88
+ }
89
+ __removeListenEvents() {
90
+ this.target.off_(this.__eventIds);
91
+ }
92
+ destroy() {
93
+ if (this.target) {
94
+ this.stop();
95
+ this.__removeListenEvents();
96
+ this.target = null;
97
+ this.__updatedList = null;
98
+ }
99
+ }
100
+ }
101
+
102
+ const { updateAllWorldMatrix: updateAllWorldMatrix$1, updateAllWorldOpacity } = LeafHelper;
103
+ const { pushAllChildBranch, pushAllParent } = BranchHelper;
104
+ function updateMatrix(updateList, levelList) {
105
+ let layout;
106
+ updateList.list.forEach(leaf => {
107
+ layout = leaf.__layout;
108
+ if (levelList.without(leaf) && !layout.useZoomProxy) {
109
+ if (layout.matrixChanged) {
110
+ updateAllWorldMatrix$1(leaf);
111
+ levelList.push(leaf);
112
+ if (leaf.isBranch)
113
+ pushAllChildBranch(leaf, levelList);
114
+ pushAllParent(leaf, levelList);
115
+ }
116
+ else if (layout.boundsChanged) {
117
+ levelList.push(leaf);
118
+ if (leaf.isBranch)
119
+ leaf.__tempNumber = 0;
120
+ pushAllParent(leaf, levelList);
121
+ }
122
+ }
123
+ });
124
+ }
125
+ function updateBounds(boundsList) {
126
+ let itemList, branch;
127
+ boundsList.sort(true);
128
+ boundsList.levels.forEach(level => {
129
+ itemList = boundsList.levelMap[level];
130
+ for (let i = 0, len = itemList.length; i < len; i++) {
131
+ branch = itemList[i];
132
+ if (branch.isBranch && branch.__tempNumber) {
133
+ for (let j = 0, jLen = branch.children.length; j < jLen; j++) {
134
+ if (!branch.children[j].isBranch) {
135
+ branch.children[j].__updateWorldBounds();
136
+ }
137
+ }
138
+ }
139
+ branch.__updateWorldBounds();
140
+ }
141
+ });
142
+ }
143
+ function updateChange(updateList) {
144
+ updateList.list.forEach(leaf => {
145
+ if (leaf.__layout.opacityChanged)
146
+ updateAllWorldOpacity(leaf);
147
+ leaf.__updateChange();
148
+ });
149
+ }
150
+
151
+ const { worldBounds } = LeafBoundsHelper;
152
+ const { setByListWithHandle } = BoundsHelper;
153
+ class LayoutBlockData {
154
+ constructor(list) {
155
+ this.updatedBounds = new Bounds();
156
+ this.beforeBounds = new Bounds();
157
+ this.afterBounds = new Bounds();
158
+ if (list instanceof Array)
159
+ list = new LeafList(list);
160
+ this.updatedList = list;
161
+ }
162
+ setBefore() {
163
+ setByListWithHandle(this.beforeBounds, this.updatedList.list, worldBounds);
164
+ }
165
+ setAfter() {
166
+ setByListWithHandle(this.afterBounds, this.updatedList.list, worldBounds);
167
+ this.updatedBounds.setByList([this.beforeBounds, this.afterBounds]);
168
+ }
169
+ merge(data) {
170
+ this.updatedList.pushList(data.updatedList.list);
171
+ this.beforeBounds.add(data.beforeBounds);
172
+ this.afterBounds.add(data.afterBounds);
173
+ this.updatedBounds.add(data.updatedBounds);
174
+ }
175
+ destroy() {
176
+ this.updatedList = null;
177
+ }
178
+ }
179
+
180
+ const { updateAllWorldMatrix, updateAllChange } = LeafHelper;
181
+ const { pushAllBranchStack, updateWorldBoundsByBranchStack } = BranchHelper;
182
+ const debug$1 = Debug.get('Layouter');
183
+ class Layouter {
184
+ constructor(target, userConfig) {
185
+ this.totalTimes = 0;
186
+ this.config = {};
187
+ this.__levelList = new LeafLevelList();
188
+ this.target = target;
189
+ if (userConfig)
190
+ this.config = DataHelper.default(userConfig, this.config);
191
+ this.__listenEvents();
192
+ }
193
+ start() {
194
+ if (this.disabled)
195
+ return;
196
+ this.running = true;
197
+ }
198
+ stop() {
199
+ this.running = false;
200
+ }
201
+ disable() {
202
+ this.stop();
203
+ this.__removeListenEvents();
204
+ this.disabled = true;
205
+ }
206
+ layout() {
207
+ if (!this.running)
208
+ return;
209
+ const { target } = this;
210
+ this.times = 0;
211
+ try {
212
+ target.emit(LayoutEvent.START);
213
+ this.layoutOnce();
214
+ target.emitEvent(new LayoutEvent(LayoutEvent.END, this.layoutedBlocks, this.times));
215
+ }
216
+ catch (e) {
217
+ debug$1.error(e);
218
+ }
219
+ this.layoutedBlocks = null;
220
+ }
221
+ layoutAgain() {
222
+ if (this.layouting) {
223
+ this.waitAgain = true;
224
+ }
225
+ else {
226
+ this.layoutOnce();
227
+ }
228
+ }
229
+ layoutOnce() {
230
+ if (this.layouting)
231
+ return debug$1.warn('layouting');
232
+ if (this.times > 3)
233
+ return debug$1.warn('layout max times');
234
+ this.times++;
235
+ this.totalTimes++;
236
+ this.layouting = true;
237
+ this.target.emit(WatchEvent.REQUEST);
238
+ if (this.totalTimes > 1) {
239
+ this.partLayout();
240
+ }
241
+ else {
242
+ this.fullLayout();
243
+ }
244
+ this.layouting = false;
245
+ if (this.waitAgain) {
246
+ this.waitAgain = false;
247
+ this.layoutOnce();
248
+ }
249
+ }
250
+ partLayout() {
251
+ var _a;
252
+ if (!((_a = this.__updatedList) === null || _a === void 0 ? void 0 : _a.length))
253
+ return;
254
+ const t = Run.start('PartLayout');
255
+ const { target, __updatedList: updateList } = this;
256
+ const { BEFORE, LAYOUT, AFTER } = LayoutEvent;
257
+ const blocks = this.getBlocks(updateList);
258
+ blocks.forEach(item => { item.setBefore(); });
259
+ target.emitEvent(new LayoutEvent(BEFORE, blocks, this.times));
260
+ updateList.sort();
261
+ updateMatrix(updateList, this.__levelList);
262
+ updateBounds(this.__levelList);
263
+ updateChange(updateList);
264
+ blocks.forEach(item => item.setAfter());
265
+ target.emitEvent(new LayoutEvent(LAYOUT, blocks, this.times));
266
+ target.emitEvent(new LayoutEvent(AFTER, blocks, this.times));
267
+ this.addBlocks(blocks);
268
+ this.__levelList.reset();
269
+ this.__updatedList = null;
270
+ Run.end(t);
271
+ }
272
+ fullLayout() {
273
+ const t = Run.start('FullLayout');
274
+ const { target } = this;
275
+ const { BEFORE, LAYOUT, AFTER } = LayoutEvent;
276
+ const blocks = this.getBlocks(new LeafList(target));
277
+ target.emitEvent(new LayoutEvent(BEFORE, blocks, this.times));
278
+ Layouter.fullLayout(target);
279
+ blocks.forEach(item => { item.setAfter(); });
280
+ target.emitEvent(new LayoutEvent(LAYOUT, blocks, this.times));
281
+ target.emitEvent(new LayoutEvent(AFTER, blocks, this.times));
282
+ this.addBlocks(blocks);
283
+ Run.end(t);
284
+ }
285
+ static fullLayout(target) {
286
+ updateAllWorldMatrix(target);
287
+ if (target.isBranch) {
288
+ const branchStack = [target];
289
+ pushAllBranchStack(target, branchStack);
290
+ updateWorldBoundsByBranchStack(branchStack);
291
+ }
292
+ else {
293
+ target.__updateWorldBounds();
294
+ }
295
+ updateAllChange(target);
296
+ }
297
+ createBlock(data) {
298
+ return new LayoutBlockData(data);
299
+ }
300
+ getBlocks(list) {
301
+ return [this.createBlock(list)];
302
+ }
303
+ addBlocks(current) {
304
+ this.layoutedBlocks ? this.layoutedBlocks.push(...current) : this.layoutedBlocks = current;
305
+ }
306
+ __onReceiveWatchData(event) {
307
+ this.__updatedList = event.data.updatedList;
308
+ }
309
+ __listenEvents() {
310
+ const { target } = this;
311
+ this.__eventIds = [
312
+ target.on_(LayoutEvent.REQUEST, this.layout, this),
313
+ target.on_(LayoutEvent.AGAIN, this.layoutAgain, this),
314
+ target.on_(WatchEvent.DATA, this.__onReceiveWatchData, this)
315
+ ];
316
+ }
317
+ __removeListenEvents() {
318
+ this.target.off_(this.__eventIds);
319
+ }
320
+ destroy() {
321
+ if (this.target) {
322
+ this.stop();
323
+ this.__removeListenEvents();
324
+ this.target = null;
325
+ this.config = null;
326
+ }
327
+ }
328
+ }
329
+
330
+ const debug = Debug.get('Renderer');
331
+ class Renderer {
332
+ get needFill() { return !!(!this.canvas.allowBackgroundColor && this.config.fill); }
333
+ constructor(target, canvas, userConfig) {
334
+ this.FPS = 60;
335
+ this.totalTimes = 0;
336
+ this.times = 0;
337
+ this.config = {
338
+ usePartRender: true,
339
+ maxFPS: 60
340
+ };
341
+ this.target = target;
342
+ this.canvas = canvas;
343
+ if (userConfig)
344
+ this.config = DataHelper.default(userConfig, this.config);
345
+ this.__listenEvents();
346
+ this.__requestRender();
347
+ }
348
+ start() {
349
+ this.running = true;
350
+ }
351
+ stop() {
352
+ this.running = false;
353
+ }
354
+ update() {
355
+ this.changed = true;
356
+ }
357
+ requestLayout() {
358
+ this.target.emit(LayoutEvent.REQUEST);
359
+ }
360
+ render(callback) {
361
+ if (!(this.running && this.canvas.view)) {
362
+ this.changed = true;
363
+ return;
364
+ }
365
+ const { target } = this;
366
+ this.times = 0;
367
+ this.totalBounds = new Bounds();
368
+ debug.log(target.innerName, '--->');
369
+ try {
370
+ this.emitRender(RenderEvent.START);
371
+ this.renderOnce(callback);
372
+ this.emitRender(RenderEvent.END, this.totalBounds);
373
+ ImageManager.clearRecycled();
374
+ }
375
+ catch (e) {
376
+ this.rendering = false;
377
+ debug.error(e);
378
+ }
379
+ debug.log('-------------|');
380
+ }
381
+ renderAgain() {
382
+ if (this.rendering) {
383
+ this.waitAgain = true;
384
+ }
385
+ else {
386
+ this.renderOnce();
387
+ }
388
+ }
389
+ renderOnce(callback) {
390
+ if (this.rendering)
391
+ return debug.warn('rendering');
392
+ if (this.times > 3)
393
+ return debug.warn('render max times');
394
+ this.times++;
395
+ this.totalTimes++;
396
+ this.rendering = true;
397
+ this.changed = false;
398
+ this.renderBounds = new Bounds();
399
+ this.renderOptions = {};
400
+ if (callback) {
401
+ this.emitRender(RenderEvent.BEFORE);
402
+ callback();
403
+ }
404
+ else {
405
+ this.requestLayout();
406
+ this.emitRender(RenderEvent.BEFORE);
407
+ if (this.config.usePartRender && this.totalTimes > 1) {
408
+ this.partRender();
409
+ }
410
+ else {
411
+ this.fullRender();
412
+ }
413
+ }
414
+ this.emitRender(RenderEvent.RENDER, this.renderBounds, this.renderOptions);
415
+ this.emitRender(RenderEvent.AFTER, this.renderBounds, this.renderOptions);
416
+ this.updateBlocks = null;
417
+ this.rendering = false;
418
+ if (this.waitAgain) {
419
+ this.waitAgain = false;
420
+ this.renderOnce();
421
+ }
422
+ }
423
+ partRender() {
424
+ const { canvas, updateBlocks: list } = this;
425
+ if (!list)
426
+ return debug.warn('PartRender: need update attr');
427
+ if (list.some(block => block.includes(this.target.__world)))
428
+ this.mergeBlocks();
429
+ list.forEach(block => { if (canvas.bounds.hit(block) && !block.isEmpty())
430
+ this.clipRender(block); });
431
+ }
432
+ clipRender(block) {
433
+ const t = Run.start('PartRender');
434
+ const { canvas } = this;
435
+ const bounds = block.getIntersect(canvas.bounds);
436
+ const includes = block.includes(this.target.__world);
437
+ const realBounds = new Bounds().copy(bounds);
438
+ canvas.save();
439
+ if (includes && !Debug.showRepaint) {
440
+ canvas.clear();
441
+ }
442
+ else {
443
+ bounds.spread(1 + 1 / this.canvas.pixelRatio).ceil();
444
+ canvas.clearWorld(bounds, true);
445
+ canvas.clipWorld(bounds, true);
446
+ }
447
+ this.__render(bounds, realBounds);
448
+ canvas.restore();
449
+ Run.end(t);
450
+ }
451
+ fullRender() {
452
+ const t = Run.start('FullRender');
453
+ const { canvas } = this;
454
+ canvas.save();
455
+ canvas.clear();
456
+ this.__render(canvas.bounds);
457
+ canvas.restore();
458
+ Run.end(t);
459
+ }
460
+ __render(bounds, realBounds) {
461
+ const options = (bounds === null || bounds === void 0 ? void 0 : bounds.includes(this.target.__world)) ? {} : { bounds };
462
+ if (this.needFill)
463
+ this.canvas.fillWorld(bounds, this.config.fill);
464
+ if (Debug.showRepaint)
465
+ this.canvas.strokeWorld(bounds, 'red');
466
+ this.target.__render(this.canvas, options);
467
+ this.renderBounds = realBounds || bounds;
468
+ this.renderOptions = options;
469
+ this.totalBounds.isEmpty() ? this.totalBounds = this.renderBounds : this.totalBounds.add(this.renderBounds);
470
+ if (Debug.showHitView)
471
+ this.renderHitView(options);
472
+ if (Debug.showBoundsView)
473
+ this.renderBoundsView(options);
474
+ this.canvas.updateRender();
475
+ }
476
+ renderHitView(_options) { }
477
+ renderBoundsView(_options) { }
478
+ addBlock(block) {
479
+ if (!this.updateBlocks)
480
+ this.updateBlocks = [];
481
+ this.updateBlocks.push(block);
482
+ }
483
+ mergeBlocks() {
484
+ const { updateBlocks: list } = this;
485
+ if (list) {
486
+ const bounds = new Bounds();
487
+ bounds.setByList(list);
488
+ list.length = 0;
489
+ list.push(bounds);
490
+ }
491
+ }
492
+ __requestRender() {
493
+ const startTime = Date.now();
494
+ Platform.requestRender(() => {
495
+ this.FPS = Math.min(60, Math.ceil(1000 / (Date.now() - startTime)));
496
+ if (this.changed) {
497
+ if (this.running && this.canvas.view)
498
+ this.render();
499
+ }
500
+ if (this.running)
501
+ this.target.emit(AnimateEvent.FRAME);
502
+ if (this.target)
503
+ this.__requestRender();
504
+ });
505
+ }
506
+ __onResize(e) {
507
+ if (this.canvas.unreal)
508
+ return;
509
+ if (e.bigger || !e.samePixelRatio) {
510
+ const { width, height } = e.old;
511
+ const bounds = new Bounds(0, 0, width, height);
512
+ if (!bounds.includes(this.target.__world) || this.needFill || !e.samePixelRatio) {
513
+ this.addBlock(this.canvas.bounds);
514
+ this.target.forceUpdate('blendMode');
515
+ }
516
+ }
517
+ }
518
+ __onLayoutEnd(event) {
519
+ if (event.data)
520
+ event.data.map(item => {
521
+ let empty;
522
+ if (item.updatedList)
523
+ item.updatedList.list.some(leaf => {
524
+ empty = (!leaf.__world.width || !leaf.__world.height);
525
+ if (empty) {
526
+ if (!leaf.isLeafer)
527
+ debug.warn(leaf.innerName, ': empty');
528
+ empty = (!leaf.isBranch || leaf.isBranchLeaf);
529
+ }
530
+ return empty;
531
+ });
532
+ this.addBlock(empty ? this.canvas.bounds : item.updatedBounds);
533
+ });
534
+ }
535
+ emitRender(type, bounds, options) {
536
+ this.target.emitEvent(new RenderEvent(type, this.times, bounds, options));
537
+ }
538
+ __listenEvents() {
539
+ const { target } = this;
540
+ this.__eventIds = [
541
+ target.on_(RenderEvent.REQUEST, this.update, this),
542
+ target.on_(LayoutEvent.END, this.__onLayoutEnd, this),
543
+ target.on_(RenderEvent.AGAIN, this.renderAgain, this),
544
+ target.on_(ResizeEvent.RESIZE, this.__onResize, this)
545
+ ];
546
+ }
547
+ __removeListenEvents() {
548
+ this.target.off_(this.__eventIds);
549
+ }
550
+ destroy() {
551
+ if (this.target) {
552
+ this.stop();
553
+ this.__removeListenEvents();
554
+ this.target = null;
555
+ this.canvas = null;
556
+ this.config = null;
557
+ }
558
+ }
559
+ }
560
+
561
+ const { hitRadiusPoint } = BoundsHelper;
562
+ class FindPath {
563
+ constructor(target, selector) {
564
+ this.target = target;
565
+ this.selector = selector;
566
+ }
567
+ getByPoint(hitPoint, hitRadius, options) {
568
+ if (!hitRadius)
569
+ hitRadius = 0;
570
+ if (!options)
571
+ options = {};
572
+ const through = options.through || false;
573
+ const ignoreHittable = options.ignoreHittable || false;
574
+ this.exclude = options.exclude || null;
575
+ this.point = { x: hitPoint.x, y: hitPoint.y, radiusX: hitRadius, radiusY: hitRadius };
576
+ this.findList = [];
577
+ this.eachFind(this.target.children, this.target.__onlyHitMask);
578
+ const list = this.findList;
579
+ const leaf = this.getBestMatchLeaf();
580
+ const path = ignoreHittable ? this.getPath(leaf) : this.getHitablePath(leaf);
581
+ this.clear();
582
+ return through ? { path, leaf, throughPath: list.length ? this.getThroughPath(list) : path } : { path, leaf };
583
+ }
584
+ getBestMatchLeaf() {
585
+ const { findList: targets } = this;
586
+ if (targets.length > 1) {
587
+ let find;
588
+ this.findList = [];
589
+ const { x, y } = this.point;
590
+ const point = { x, y, radiusX: 0, radiusY: 0 };
591
+ for (let i = 0, len = targets.length; i < len; i++) {
592
+ find = targets[i];
593
+ if (LeafHelper.worldHittable(find)) {
594
+ this.hitChild(find, point);
595
+ if (this.findList.length)
596
+ return this.findList[0];
597
+ }
598
+ }
599
+ }
600
+ return targets[0];
601
+ }
602
+ getPath(leaf) {
603
+ const path = new LeafList();
604
+ while (leaf) {
605
+ path.push(leaf);
606
+ leaf = leaf.parent;
607
+ }
608
+ path.push(this.target);
609
+ return path;
610
+ }
611
+ getHitablePath(leaf) {
612
+ const path = this.getPath(leaf);
613
+ let item, hittablePath = new LeafList();
614
+ for (let i = path.list.length - 1; i > -1; i--) {
615
+ item = path.list[i];
616
+ if (!item.__.hittable)
617
+ break;
618
+ hittablePath.unshift(item);
619
+ if (!item.__.hitChildren)
620
+ break;
621
+ }
622
+ return hittablePath;
623
+ }
624
+ getThroughPath(list) {
625
+ const throughPath = new LeafList();
626
+ const pathList = [];
627
+ for (let i = list.length - 1; i > -1; i--) {
628
+ pathList.push(this.getPath(list[i]));
629
+ }
630
+ let path, nextPath, leaf;
631
+ for (let i = 0, len = pathList.length; i < len; i++) {
632
+ path = pathList[i], nextPath = pathList[i + 1];
633
+ for (let j = 0, jLen = path.length; j < jLen; j++) {
634
+ leaf = path.list[j];
635
+ if (nextPath && nextPath.has(leaf))
636
+ break;
637
+ throughPath.push(leaf);
638
+ }
639
+ }
640
+ return throughPath;
641
+ }
642
+ eachFind(children, hitMask) {
643
+ let child, hit;
644
+ const { point } = this, len = children.length;
645
+ for (let i = len - 1; i > -1; i--) {
646
+ child = children[i];
647
+ if (!child.__.visible || (hitMask && !child.__.isMask))
648
+ continue;
649
+ hit = child.__.hitRadius ? true : hitRadiusPoint(child.__world, point);
650
+ if (child.isBranch) {
651
+ if (hit || child.__ignoreHitWorld) {
652
+ this.eachFind(child.children, child.__onlyHitMask);
653
+ if (child.isBranchLeaf && !this.findList.length)
654
+ this.hitChild(child, point);
655
+ }
656
+ }
657
+ else {
658
+ if (hit)
659
+ this.hitChild(child, point);
660
+ }
661
+ }
662
+ }
663
+ hitChild(child, point) {
664
+ if (this.exclude && this.exclude.has(child))
665
+ return;
666
+ if (child.__hitWorld(point))
667
+ this.findList.push(child);
668
+ }
669
+ clear() {
670
+ this.point = null;
671
+ this.findList = null;
672
+ this.exclude = null;
673
+ }
674
+ destroy() {
675
+ this.clear();
676
+ }
677
+ }
678
+
679
+ class Selector {
680
+ constructor(target, userConfig) {
681
+ this.config = {};
682
+ this.innerIdList = {};
683
+ this.idList = {};
684
+ this.classNameList = {};
685
+ this.tagNameList = {};
686
+ this.target = target;
687
+ if (userConfig)
688
+ this.config = DataHelper.default(userConfig, this.config);
689
+ this.findPath = new FindPath(target, this);
690
+ this.__listenEvents();
691
+ }
692
+ getByPoint(hitPoint, hitRadius, options) {
693
+ if (Platform.name === 'node')
694
+ this.target.emit(LayoutEvent.CHECK_UPDATE);
695
+ return this.findPath.getByPoint(hitPoint, hitRadius, options);
696
+ }
697
+ find(name, branch) {
698
+ if (typeof name === 'number') {
699
+ return this.getByInnerId(name, branch);
700
+ }
701
+ else if (name.startsWith('#')) {
702
+ return this.getById(name.substring(1), branch);
703
+ }
704
+ else if (name.startsWith('.')) {
705
+ return this.getByClassName(name.substring(1), branch);
706
+ }
707
+ else {
708
+ return this.getByTagName(name, branch);
709
+ }
710
+ }
711
+ getByInnerId(name, branch) {
712
+ let cache = this.innerIdList[name];
713
+ if (cache)
714
+ return cache;
715
+ if (!branch)
716
+ branch = this.target;
717
+ let find;
718
+ this.loopFind(branch, (leaf) => {
719
+ if (leaf.innerId === name) {
720
+ find = leaf;
721
+ this.innerIdList[name] = find;
722
+ return true;
723
+ }
724
+ else {
725
+ return false;
726
+ }
727
+ });
728
+ return find;
729
+ }
730
+ getById(name, branch) {
731
+ let cache = this.idList[name];
732
+ if (cache)
733
+ return cache;
734
+ if (!branch)
735
+ branch = this.target;
736
+ let find;
737
+ this.loopFind(branch, (leaf) => {
738
+ if (leaf.id === name) {
739
+ find = leaf;
740
+ this.idList[name] = find;
741
+ return true;
742
+ }
743
+ else {
744
+ return false;
745
+ }
746
+ });
747
+ return find;
748
+ }
749
+ getByClassName(name, branch) {
750
+ if (!branch)
751
+ branch = this.target;
752
+ let find = [];
753
+ this.loopFind(branch, (leaf) => {
754
+ if (leaf.className === name)
755
+ find.push(leaf);
756
+ return false;
757
+ });
758
+ return find;
759
+ }
760
+ getByTagName(name, branch) {
761
+ if (!branch)
762
+ branch = this.target;
763
+ let find = [];
764
+ this.loopFind(branch, (leaf) => {
765
+ if (leaf.__tag === name)
766
+ find.push(leaf);
767
+ return false;
768
+ });
769
+ return find;
770
+ }
771
+ loopFind(branch, find) {
772
+ if (find(branch))
773
+ return;
774
+ const { children } = branch;
775
+ for (let i = 0, len = children.length; i < len; i++) {
776
+ branch = children[i];
777
+ if (find(branch))
778
+ return;
779
+ if (branch.isBranch)
780
+ this.loopFind(branch, find);
781
+ }
782
+ }
783
+ __onRemoveChild(event) {
784
+ const target = event.target;
785
+ if (this.idList[target.id])
786
+ this.idList[target.id] = null;
787
+ if (this.innerIdList[target.id])
788
+ this.innerIdList[target.innerId] = null;
789
+ }
790
+ __listenEvents() {
791
+ this.__eventIds = [
792
+ this.target.on_(ChildEvent.REMOVE, this.__onRemoveChild, this)
793
+ ];
794
+ }
795
+ __removeListenEvents() {
796
+ this.target.off_(this.__eventIds);
797
+ this.__eventIds.length = 0;
798
+ }
799
+ destroy() {
800
+ if (this.__eventIds.length) {
801
+ this.__removeListenEvents();
802
+ this.findPath.destroy();
803
+ this.innerIdList = {};
804
+ this.idList = {};
805
+ this.classNameList = {};
806
+ this.tagNameList = {};
807
+ }
808
+ }
809
+ }
810
+
811
+ Object.assign(Creator, {
812
+ watcher: (target, options) => new Watcher(target, options),
813
+ layouter: (target, options) => new Layouter(target, options),
814
+ renderer: (target, canvas, options) => new Renderer(target, canvas, options),
815
+ selector: (target, options) => new Selector(target, options)
816
+ });
817
+ Platform.layout = Layouter.fullLayout;
818
+
819
+ class LeaferCanvas extends LeaferCanvasBase {
820
+ get allowBackgroundColor() { return true; }
821
+ init() {
822
+ this.__createView();
823
+ this.__createContext();
824
+ this.resize(this.config);
825
+ }
826
+ __createView() {
827
+ this.view = Platform.origin.createCanvas(1, 1);
828
+ }
829
+ updateViewSize() {
830
+ const { width, height, pixelRatio } = this;
831
+ this.view.width = width * pixelRatio;
832
+ this.view.height = height * pixelRatio;
833
+ this.clientBounds = this.bounds;
834
+ }
835
+ }
836
+
837
+ canvasPatch(OffscreenCanvasRenderingContext2D.prototype);
838
+ canvasPatch(Path2D.prototype);
839
+
840
+ const { mineType } = FileHelper;
841
+ Object.assign(Creator, {
842
+ canvas: (options, manager) => new LeaferCanvas(options, manager),
843
+ image: (options) => new LeaferImage(options),
844
+ hitCanvas: (options, manager) => new LeaferCanvas(options, manager),
845
+ interaction: (target, canvas, selector, options) => new InteractionBase(target, canvas, selector, options),
846
+ });
847
+ function useCanvas(_canvasType, _power) {
848
+ Platform.origin = {
849
+ createCanvas: (width, height) => new OffscreenCanvas(width, height),
850
+ canvasToDataURL: (canvas, type, quality) => {
851
+ return new Promise((resolve, reject) => {
852
+ canvas.convertToBlob({ type: mineType(type), quality }).then((blob) => {
853
+ var reader = new FileReader();
854
+ reader.onload = (e) => resolve(e.target.result);
855
+ reader.onerror = (e) => reject(e);
856
+ reader.readAsDataURL(blob);
857
+ }).catch((e) => {
858
+ reject(e);
859
+ });
860
+ });
861
+ },
862
+ canvasToBolb: (canvas, type, quality) => canvas.convertToBlob({ type: mineType(type), quality }),
863
+ canvasSaveAs: (_canvas, _filename, _quality) => new Promise((resolve) => resolve()),
864
+ loadImage(src) {
865
+ return new Promise((resolve, reject) => {
866
+ if (!src.startsWith('data:') && Platform.imageSuffix)
867
+ src += (src.includes("?") ? "&" : "?") + Platform.imageSuffix;
868
+ let req = new XMLHttpRequest();
869
+ req.open('GET', src, true);
870
+ req.responseType = "blob";
871
+ req.onload = () => {
872
+ createImageBitmap(req.response).then(img => {
873
+ resolve(img);
874
+ }).catch(e => {
875
+ reject(e);
876
+ });
877
+ };
878
+ req.onerror = (e) => reject(e);
879
+ req.send();
880
+ });
881
+ }
882
+ };
883
+ Platform.canvas = Creator.canvas();
884
+ Platform.conicGradientSupport = !!Platform.canvas.context.createConicGradient;
885
+ }
886
+ Platform.name = 'web';
887
+ Platform.isWorker = true;
888
+ Platform.requestRender = function (render) { requestAnimationFrame(render); };
889
+ Platform.devicePixelRatio = 1;
890
+ Platform.realtimeLayout = true;
891
+ const { userAgent } = navigator;
892
+ if (userAgent.indexOf("Firefox") > -1) {
893
+ Platform.conicGradientRotate90 = true;
894
+ Platform.intWheelDeltaY = true;
895
+ }
896
+ else if (userAgent.indexOf("Safari") > -1 && userAgent.indexOf("Chrome") === -1) {
897
+ Platform.fullImageShadow = true;
898
+ }
899
+ if (userAgent.indexOf('Windows') > -1) {
900
+ Platform.os = 'Windows';
901
+ Platform.intWheelDeltaY = true;
902
+ }
903
+ else if (userAgent.indexOf('Mac') > -1) {
904
+ Platform.os = 'Mac';
905
+ }
906
+ else if (userAgent.indexOf('Linux') > -1) {
907
+ Platform.os = 'Linux';
908
+ }
909
+
910
+ const { get: get$4, rotateOfOuter: rotateOfOuter$2, translate: translate$1, scaleOfOuter: scaleOfOuter$2, scale: scaleHelper$1, rotate } = MatrixHelper;
911
+ function fillOrFitMode(data, mode, box, width, height, rotation) {
912
+ const transform = get$4();
913
+ const swap = rotation && rotation !== 180;
914
+ const sw = box.width / (swap ? height : width);
915
+ const sh = box.height / (swap ? width : height);
916
+ const scale = mode === 'fit' ? Math.min(sw, sh) : Math.max(sw, sh);
917
+ const x = box.x + (box.width - width * scale) / 2;
918
+ const y = box.y + (box.height - height * scale) / 2;
919
+ translate$1(transform, x, y);
920
+ scaleHelper$1(transform, scale);
921
+ if (rotation)
922
+ rotateOfOuter$2(transform, { x: box.x + box.width / 2, y: box.y + box.height / 2 }, rotation);
923
+ data.scaleX = data.scaleY = scale;
924
+ data.transform = transform;
925
+ }
926
+ function clipMode(data, box, offset, scale, rotation) {
927
+ const transform = get$4();
928
+ translate$1(transform, box.x, box.y);
929
+ if (offset)
930
+ translate$1(transform, offset.x, offset.y);
931
+ if (scale) {
932
+ typeof scale === 'number' ? scaleHelper$1(transform, scale) : scaleHelper$1(transform, scale.x, scale.y);
933
+ data.scaleX = transform.a;
934
+ data.scaleY = transform.d;
935
+ }
936
+ if (rotation)
937
+ rotate(transform, rotation);
938
+ data.transform = transform;
939
+ }
940
+ function repeatMode(data, box, width, height, scale, rotation) {
941
+ const transform = get$4();
942
+ if (rotation) {
943
+ rotate(transform, rotation);
944
+ switch (rotation) {
945
+ case 90:
946
+ translate$1(transform, height, 0);
947
+ break;
948
+ case 180:
949
+ translate$1(transform, width, height);
950
+ break;
951
+ case 270:
952
+ translate$1(transform, 0, width);
953
+ break;
954
+ }
955
+ }
956
+ translate$1(transform, box.x, box.y);
957
+ if (scale) {
958
+ scaleOfOuter$2(transform, box, scale);
959
+ data.scaleX = data.scaleY = scale;
960
+ }
961
+ data.transform = transform;
962
+ }
963
+
964
+ const { get: get$3, translate } = MatrixHelper;
965
+ function createData(leafPaint, image, paint, box) {
966
+ let { width, height } = image;
967
+ const { opacity, mode, offset, scale, rotation, blendMode } = paint;
968
+ const sameBox = box.width === width && box.height === height;
969
+ if (blendMode)
970
+ leafPaint.blendMode = blendMode;
971
+ const data = leafPaint.data = { mode };
972
+ switch (mode) {
973
+ case 'strench':
974
+ if (!sameBox)
975
+ width = box.width, height = box.height;
976
+ if (box.x || box.y) {
977
+ data.transform = get$3();
978
+ translate(data.transform, box.x, box.y);
979
+ }
980
+ break;
981
+ case 'clip':
982
+ if (offset || scale || rotation)
983
+ clipMode(data, box, offset, scale, rotation);
984
+ break;
985
+ case 'repeat':
986
+ if (!sameBox || scale || rotation)
987
+ repeatMode(data, box, width, height, scale, rotation);
988
+ break;
989
+ case 'fit':
990
+ case 'cover':
991
+ default:
992
+ if (!sameBox || rotation)
993
+ fillOrFitMode(data, mode, box, width, height, rotation);
994
+ }
995
+ data.width = width;
996
+ data.height = height;
997
+ if (opacity)
998
+ data.opacity = opacity;
999
+ }
1000
+
1001
+ function image(ui, attrName, attrValue, box, firstUse) {
1002
+ const leafPaint = { type: attrValue.type };
1003
+ const image = leafPaint.image = ImageManager.get(attrValue);
1004
+ const event = (firstUse || image.loading) && { target: ui, image, attrName, attrValue };
1005
+ if (image.ready) {
1006
+ if (hasNaturalSize(ui, attrName, image))
1007
+ createData(leafPaint, image, attrValue, box);
1008
+ if (firstUse) {
1009
+ emit(ImageEvent.LOAD, event);
1010
+ emit(ImageEvent.LOADED, event);
1011
+ }
1012
+ }
1013
+ else if (image.error) {
1014
+ if (firstUse) {
1015
+ ui.forceUpdate('surface');
1016
+ event.error = image.error;
1017
+ emit(ImageEvent.ERROR, event);
1018
+ }
1019
+ }
1020
+ else {
1021
+ if (firstUse)
1022
+ emit(ImageEvent.LOAD, event);
1023
+ leafPaint.loadId = image.load(() => {
1024
+ if (!ui.destroyed) {
1025
+ if (hasNaturalSize(ui, attrName, image)) {
1026
+ createData(leafPaint, image, attrValue, box);
1027
+ ui.forceUpdate('surface');
1028
+ }
1029
+ emit(ImageEvent.LOADED, event);
1030
+ }
1031
+ }, (error) => {
1032
+ ui.forceUpdate('surface');
1033
+ event.error = error;
1034
+ emit(ImageEvent.ERROR, event);
1035
+ });
1036
+ }
1037
+ return leafPaint;
1038
+ }
1039
+ function hasNaturalSize(ui, attrName, image) {
1040
+ if (attrName === 'fill' && !ui.__.__naturalWidth) {
1041
+ const { __: d } = ui;
1042
+ d.__naturalWidth = image.width;
1043
+ d.__naturalHeight = image.height;
1044
+ if (!d.__getInput('width') || !d.__getInput('height')) {
1045
+ ui.forceUpdate('width');
1046
+ return false;
1047
+ }
1048
+ }
1049
+ return true;
1050
+ }
1051
+ function emit(type, data) {
1052
+ if (data.target.hasEvent(type))
1053
+ data.target.emitEvent(new ImageEvent(type, data));
1054
+ }
1055
+
1056
+ /******************************************************************************
1057
+ Copyright (c) Microsoft Corporation.
1058
+
1059
+ Permission to use, copy, modify, and/or distribute this software for any
1060
+ purpose with or without fee is hereby granted.
1061
+
1062
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
1063
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
1064
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
1065
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
1066
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
1067
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
1068
+ PERFORMANCE OF THIS SOFTWARE.
1069
+ ***************************************************************************** */
1070
+ /* global Reflect, Promise, SuppressedError, Symbol */
1071
+
1072
+
1073
+ function __awaiter(thisArg, _arguments, P, generator) {
1074
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
1075
+ return new (P || (P = Promise))(function (resolve, reject) {
1076
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
1077
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
1078
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
1079
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
1080
+ });
1081
+ }
1082
+
1083
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
1084
+ var e = new Error(message);
1085
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
1086
+ };
1087
+
1088
+ const { get: get$2, scale: scaleHelper, copy: copy$1 } = MatrixHelper;
1089
+ function createPattern(ui, paint, pixelRatio) {
1090
+ let { scaleX, scaleY } = ui.__world;
1091
+ const id = scaleX + '-' + scaleY;
1092
+ if (paint.patternId !== id && !ui.destroyed) {
1093
+ paint.patternId = id;
1094
+ scaleX = Math.abs(scaleX);
1095
+ scaleY = Math.abs(scaleY);
1096
+ const { image, data } = paint;
1097
+ const maxWidth = image.isSVG ? 4096 : Math.min(image.width, 4096);
1098
+ const maxHeight = image.isSVG ? 4096 : Math.min(image.height, 4096);
1099
+ let scale, matrix, { width, height, scaleX: sx, scaleY: sy, opacity, transform, mode } = data;
1100
+ if (sx) {
1101
+ matrix = get$2();
1102
+ copy$1(matrix, transform);
1103
+ scaleHelper(matrix, 1 / sx, 1 / sy);
1104
+ scaleX *= sx;
1105
+ scaleY *= sy;
1106
+ }
1107
+ scaleX *= pixelRatio;
1108
+ scaleY *= pixelRatio;
1109
+ width *= scaleX;
1110
+ height *= scaleY;
1111
+ if (width > maxWidth || height > maxHeight) {
1112
+ scale = Math.max(width / maxWidth, height / maxHeight);
1113
+ }
1114
+ if (scale) {
1115
+ scaleX /= scale;
1116
+ scaleY /= scale;
1117
+ width /= scale;
1118
+ height /= scale;
1119
+ }
1120
+ if (sx) {
1121
+ scaleX /= sx;
1122
+ scaleY /= sy;
1123
+ }
1124
+ if (transform || scaleX !== 1 || scaleY !== 1) {
1125
+ if (!matrix) {
1126
+ matrix = get$2();
1127
+ if (transform)
1128
+ copy$1(matrix, transform);
1129
+ }
1130
+ scaleHelper(matrix, 1 / scaleX, 1 / scaleY);
1131
+ }
1132
+ const style = Platform.canvas.createPattern(image.getCanvas(width < 1 ? 1 : width, height < 1 ? 1 : height, opacity), mode === 'repeat' ? 'repeat' : (Platform.origin.noRepeat || 'no-repeat'));
1133
+ try {
1134
+ if (paint.transform)
1135
+ paint.transform = null;
1136
+ if (matrix)
1137
+ style.setTransform ? style.setTransform(matrix) : paint.transform = matrix;
1138
+ }
1139
+ catch (_a) {
1140
+ paint.transform = matrix;
1141
+ }
1142
+ paint.style = style;
1143
+ return true;
1144
+ }
1145
+ else {
1146
+ return false;
1147
+ }
1148
+ }
1149
+
1150
+ function checkImage(ui, canvas, paint, allowPaint) {
1151
+ const { scaleX, scaleY } = ui.__world;
1152
+ if (!paint.data || paint.patternId === scaleX + '-' + scaleY) {
1153
+ return false;
1154
+ }
1155
+ else {
1156
+ if (allowPaint) {
1157
+ if (paint.image.isSVG && paint.data.mode !== 'repeat') {
1158
+ let { width, height } = paint.data;
1159
+ width *= scaleX * canvas.pixelRatio;
1160
+ height *= scaleY * canvas.pixelRatio;
1161
+ allowPaint = width > 4096 || height > 4096;
1162
+ }
1163
+ else {
1164
+ allowPaint = false;
1165
+ }
1166
+ }
1167
+ if (allowPaint) {
1168
+ canvas.save();
1169
+ canvas.clip();
1170
+ const { data } = paint;
1171
+ if (paint.blendMode)
1172
+ canvas.blendMode = paint.blendMode;
1173
+ if (data.opacity)
1174
+ canvas.opacity *= data.opacity;
1175
+ if (data.transform)
1176
+ canvas.transform(data.transform);
1177
+ canvas.drawImage(paint.image.view, 0, 0, data.width, data.height);
1178
+ canvas.restore();
1179
+ return true;
1180
+ }
1181
+ else {
1182
+ if (!paint.style) {
1183
+ createPattern(ui, paint, canvas.pixelRatio);
1184
+ }
1185
+ else {
1186
+ ImageManager.patternTasker.add(() => __awaiter(this, void 0, void 0, function* () {
1187
+ if (canvas.bounds.hit(ui.__world) && createPattern(ui, paint, canvas.pixelRatio))
1188
+ ui.forceUpdate('surface');
1189
+ }), 300);
1190
+ }
1191
+ return false;
1192
+ }
1193
+ }
1194
+ }
1195
+
1196
+ function recycleImage(data, attrName) {
1197
+ const paints = (attrName === 'fill' ? data._fill : data._stroke);
1198
+ if (paints instanceof Array) {
1199
+ let image, recycleMap, input, url;
1200
+ for (let i = 0, len = paints.length; i < len; i++) {
1201
+ image = paints[i].image;
1202
+ url = image && image.url;
1203
+ if (url) {
1204
+ if (!recycleMap)
1205
+ recycleMap = {};
1206
+ recycleMap[url] = true;
1207
+ ImageManager.recycle(image);
1208
+ if (image.loading) {
1209
+ if (!input) {
1210
+ input = (data.__input && data.__input[attrName]) || [];
1211
+ if (!(input instanceof Array))
1212
+ input = [input];
1213
+ }
1214
+ image.unload(paints[i].loadId, !input.some((item) => item.url === url));
1215
+ }
1216
+ }
1217
+ }
1218
+ return recycleMap;
1219
+ }
1220
+ return null;
1221
+ }
1222
+
1223
+ function fillText(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.fillText(row.text, row.x, row.y);
1230
+ }
1231
+ else if (row.data) {
1232
+ row.data.forEach(charData => {
1233
+ canvas.fillText(charData.char, charData.x, row.y);
1234
+ });
1235
+ }
1236
+ if (decorationY)
1237
+ canvas.fillRect(row.x, row.y + decorationY, row.width, decorationHeight);
1238
+ }
1239
+ }
1240
+
1241
+ function fill(ui, canvas, fill) {
1242
+ canvas.fillStyle = fill;
1243
+ ui.__.__font ? fillText(ui, canvas) : (ui.__.windingRule ? canvas.fill(ui.__.windingRule) : canvas.fill());
1244
+ }
1245
+ function fills(ui, canvas, fills) {
1246
+ let item;
1247
+ const { windingRule, __font } = ui.__;
1248
+ for (let i = 0, len = fills.length; i < len; i++) {
1249
+ item = fills[i];
1250
+ if (item.image && checkImage(ui, canvas, item, !__font))
1251
+ continue;
1252
+ if (item.style) {
1253
+ canvas.fillStyle = item.style;
1254
+ if (item.transform) {
1255
+ canvas.save();
1256
+ canvas.transform(item.transform);
1257
+ if (item.blendMode)
1258
+ canvas.blendMode = item.blendMode;
1259
+ __font ? fillText(ui, canvas) : (windingRule ? canvas.fill(windingRule) : canvas.fill());
1260
+ canvas.restore();
1261
+ }
1262
+ else {
1263
+ if (item.blendMode) {
1264
+ canvas.saveBlendMode(item.blendMode);
1265
+ __font ? fillText(ui, canvas) : (windingRule ? canvas.fill(windingRule) : canvas.fill());
1266
+ canvas.restoreBlendMode();
1267
+ }
1268
+ else {
1269
+ __font ? fillText(ui, canvas) : (windingRule ? canvas.fill(windingRule) : canvas.fill());
1270
+ }
1271
+ }
1272
+ }
1273
+ }
1274
+ }
1275
+
1276
+ function strokeText(ui, canvas, stroke) {
1277
+ const { strokeAlign } = ui.__;
1278
+ const isStrokes = typeof stroke !== 'string';
1279
+ switch (strokeAlign) {
1280
+ case 'center':
1281
+ canvas.setStroke(isStrokes ? undefined : stroke, ui.__.strokeWidth, ui.__);
1282
+ isStrokes ? drawStrokesStyle(ui, stroke, canvas, true) : drawTextStroke(ui, canvas);
1283
+ break;
1284
+ case 'inside':
1285
+ drawAlignStroke(ui, canvas, stroke, 'inside', isStrokes);
1286
+ break;
1287
+ case 'outside':
1288
+ drawAlignStroke(ui, canvas, stroke, 'outside', isStrokes);
1289
+ break;
1290
+ }
1291
+ }
1292
+ function drawAlignStroke(ui, canvas, stroke, align, isStrokes) {
1293
+ const { strokeWidth, __font } = ui.__;
1294
+ const out = canvas.getSameCanvas(true);
1295
+ out.setStroke(isStrokes ? undefined : stroke, strokeWidth * 2, ui.__);
1296
+ out.font = __font;
1297
+ isStrokes ? drawStrokesStyle(ui, stroke, out, true) : drawTextStroke(ui, out);
1298
+ out.blendMode = align === 'outside' ? 'destination-out' : 'destination-in';
1299
+ fillText(ui, out);
1300
+ out.blendMode = 'normal';
1301
+ canvas.copyWorldToInner(out, ui.__world, ui.__layout.renderBounds);
1302
+ out.recycle();
1303
+ }
1304
+ function drawTextStroke(ui, canvas) {
1305
+ let row;
1306
+ const { rows, decorationY, decorationHeight } = ui.__.__textDrawData;
1307
+ for (let i = 0, len = rows.length; i < len; i++) {
1308
+ row = rows[i];
1309
+ if (row.text) {
1310
+ canvas.strokeText(row.text, row.x, row.y);
1311
+ }
1312
+ else if (row.data) {
1313
+ row.data.forEach(charData => {
1314
+ canvas.strokeText(charData.char, charData.x, row.y);
1315
+ });
1316
+ }
1317
+ if (decorationY)
1318
+ canvas.strokeRect(row.x, row.y + decorationY, row.width, decorationHeight);
1319
+ }
1320
+ }
1321
+ function drawStrokesStyle(ui, strokes, canvas, isText) {
1322
+ let item;
1323
+ for (let i = 0, len = strokes.length; i < len; i++) {
1324
+ item = strokes[i];
1325
+ if (item.image && checkImage(ui, canvas, item, false))
1326
+ continue;
1327
+ if (item.style) {
1328
+ canvas.strokeStyle = item.style;
1329
+ if (item.blendMode) {
1330
+ canvas.saveBlendMode(item.blendMode);
1331
+ isText ? drawTextStroke(ui, canvas) : canvas.stroke();
1332
+ canvas.restoreBlendMode();
1333
+ }
1334
+ else {
1335
+ isText ? drawTextStroke(ui, canvas) : canvas.stroke();
1336
+ }
1337
+ }
1338
+ }
1339
+ }
1340
+
1341
+ function stroke(ui, canvas, stroke) {
1342
+ const options = ui.__;
1343
+ const { strokeWidth, strokeAlign, __font } = options;
1344
+ if (!strokeWidth)
1345
+ return;
1346
+ if (__font) {
1347
+ strokeText(ui, canvas, stroke);
1348
+ }
1349
+ else {
1350
+ switch (strokeAlign) {
1351
+ case 'center':
1352
+ canvas.setStroke(stroke, strokeWidth, options);
1353
+ canvas.stroke();
1354
+ break;
1355
+ case 'inside':
1356
+ canvas.save();
1357
+ canvas.setStroke(stroke, strokeWidth * 2, options);
1358
+ options.windingRule ? canvas.clip(options.windingRule) : canvas.clip();
1359
+ canvas.stroke();
1360
+ canvas.restore();
1361
+ break;
1362
+ case 'outside':
1363
+ const out = canvas.getSameCanvas(true);
1364
+ out.setStroke(stroke, strokeWidth * 2, ui.__);
1365
+ ui.__drawRenderPath(out);
1366
+ out.stroke();
1367
+ options.windingRule ? out.clip(options.windingRule) : out.clip();
1368
+ out.clearWorld(ui.__layout.renderBounds);
1369
+ canvas.copyWorldToInner(out, ui.__world, ui.__layout.renderBounds);
1370
+ out.recycle();
1371
+ break;
1372
+ }
1373
+ }
1374
+ }
1375
+ function strokes(ui, canvas, strokes) {
1376
+ const options = ui.__;
1377
+ const { strokeWidth, strokeAlign, __font } = options;
1378
+ if (!strokeWidth)
1379
+ return;
1380
+ if (__font) {
1381
+ strokeText(ui, canvas, strokes);
1382
+ }
1383
+ else {
1384
+ switch (strokeAlign) {
1385
+ case 'center':
1386
+ canvas.setStroke(undefined, strokeWidth, options);
1387
+ drawStrokesStyle(ui, strokes, canvas);
1388
+ break;
1389
+ case 'inside':
1390
+ canvas.save();
1391
+ canvas.setStroke(undefined, strokeWidth * 2, options);
1392
+ options.windingRule ? canvas.clip(options.windingRule) : canvas.clip();
1393
+ drawStrokesStyle(ui, strokes, canvas);
1394
+ canvas.restore();
1395
+ break;
1396
+ case 'outside':
1397
+ const { renderBounds } = ui.__layout;
1398
+ const out = canvas.getSameCanvas(true);
1399
+ ui.__drawRenderPath(out);
1400
+ out.setStroke(undefined, strokeWidth * 2, ui.__);
1401
+ drawStrokesStyle(ui, strokes, out);
1402
+ options.windingRule ? out.clip(options.windingRule) : out.clip();
1403
+ out.clearWorld(renderBounds);
1404
+ canvas.copyWorldToInner(out, ui.__world, renderBounds);
1405
+ out.recycle();
1406
+ break;
1407
+ }
1408
+ }
1409
+ }
1410
+
1411
+ const { getSpread, getOuterOf, getByMove, getIntersectData } = BoundsHelper;
1412
+ function shape(ui, current, options) {
1413
+ const canvas = current.getSameCanvas();
1414
+ let bounds, matrix, shapeBounds;
1415
+ let worldCanvas;
1416
+ const { __world } = ui;
1417
+ let { scaleX, scaleY } = __world;
1418
+ if (scaleX < 0)
1419
+ scaleX = -scaleX;
1420
+ if (scaleY < 0)
1421
+ scaleY = -scaleY;
1422
+ if (!current.bounds.includes(__world, options.matrix)) {
1423
+ const { renderShapeSpread: spread } = ui.__layout;
1424
+ const worldClipBounds = getIntersectData(spread ? getSpread(current.bounds, spread * scaleX, spread * scaleY) : current.bounds, __world, options.matrix);
1425
+ matrix = current.bounds.getFitMatrix(worldClipBounds);
1426
+ if (matrix.a < 1) {
1427
+ worldCanvas = current.getSameCanvas();
1428
+ ui.__renderShape(worldCanvas, options);
1429
+ scaleX *= matrix.a;
1430
+ scaleY *= matrix.d;
1431
+ }
1432
+ shapeBounds = getOuterOf(__world, matrix);
1433
+ bounds = getByMove(shapeBounds, -matrix.e, -matrix.f);
1434
+ if (options.matrix)
1435
+ matrix.multiply(options.matrix);
1436
+ options = Object.assign(Object.assign({}, options), { matrix });
1437
+ }
1438
+ else {
1439
+ if (options.matrix) {
1440
+ scaleX *= options.matrix.a;
1441
+ scaleY *= options.matrix.d;
1442
+ bounds = shapeBounds = getOuterOf(__world, options.matrix);
1443
+ }
1444
+ else {
1445
+ bounds = shapeBounds = __world;
1446
+ }
1447
+ worldCanvas = canvas;
1448
+ }
1449
+ ui.__renderShape(canvas, options);
1450
+ return {
1451
+ canvas, matrix, bounds,
1452
+ worldCanvas, shapeBounds, scaleX, scaleY
1453
+ };
1454
+ }
1455
+
1456
+ const defaultFrom$2 = { x: 0.5, y: 0 };
1457
+ const defaultTo$2 = { x: 0.5, y: 1 };
1458
+ function linearGradient(paint, box) {
1459
+ let { from, to, type, blendMode, opacity } = paint;
1460
+ from || (from = defaultFrom$2);
1461
+ to || (to = defaultTo$2);
1462
+ 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);
1463
+ applyStops(style, paint.stops, opacity);
1464
+ const data = { type, style };
1465
+ if (blendMode)
1466
+ data.blendMode = blendMode;
1467
+ return data;
1468
+ }
1469
+ function applyStops(gradient, stops, opacity) {
1470
+ let stop;
1471
+ for (let i = 0, len = stops.length; i < len; i++) {
1472
+ stop = stops[i];
1473
+ gradient.addColorStop(stop.offset, ColorConvert$1.string(stop.color, opacity));
1474
+ }
1475
+ }
1476
+
1477
+ const { set: set$1, getAngle: getAngle$1, getDistance: getDistance$1 } = PointHelper;
1478
+ const { get: get$1, rotateOfOuter: rotateOfOuter$1, scaleOfOuter: scaleOfOuter$1 } = MatrixHelper;
1479
+ const defaultFrom$1 = { x: 0.5, y: 0.5 };
1480
+ const defaultTo$1 = { x: 0.5, y: 1 };
1481
+ const realFrom$1 = {};
1482
+ const realTo$1 = {};
1483
+ function radialGradient(paint, box) {
1484
+ let { from, to, type, opacity, blendMode, stretch } = paint;
1485
+ from || (from = defaultFrom$1);
1486
+ to || (to = defaultTo$1);
1487
+ const { x, y, width, height } = box;
1488
+ set$1(realFrom$1, x + from.x * width, y + from.y * height);
1489
+ set$1(realTo$1, x + to.x * width, y + to.y * height);
1490
+ let transform;
1491
+ if (width !== height || stretch) {
1492
+ transform = get$1();
1493
+ scaleOfOuter$1(transform, realFrom$1, width / height * (stretch || 1), 1);
1494
+ rotateOfOuter$1(transform, realFrom$1, getAngle$1(realFrom$1, realTo$1) + 90);
1495
+ }
1496
+ const style = Platform.canvas.createRadialGradient(realFrom$1.x, realFrom$1.y, 0, realFrom$1.x, realFrom$1.y, getDistance$1(realFrom$1, realTo$1));
1497
+ applyStops(style, paint.stops, opacity);
1498
+ const data = { type, style, transform };
1499
+ if (blendMode)
1500
+ data.blendMode = blendMode;
1501
+ return data;
1502
+ }
1503
+
1504
+ const { set, getAngle, getDistance } = PointHelper;
1505
+ const { get, rotateOfOuter, scaleOfOuter } = MatrixHelper;
1506
+ const defaultFrom = { x: 0.5, y: 0.5 };
1507
+ const defaultTo = { x: 0.5, y: 1 };
1508
+ const realFrom = {};
1509
+ const realTo = {};
1510
+ function conicGradient(paint, box) {
1511
+ let { from, to, type, opacity, blendMode, stretch } = paint;
1512
+ from || (from = defaultFrom);
1513
+ to || (to = defaultTo);
1514
+ const { x, y, width, height } = box;
1515
+ set(realFrom, x + from.x * width, y + from.y * height);
1516
+ set(realTo, x + to.x * width, y + to.y * height);
1517
+ const transform = get();
1518
+ const angle = getAngle(realFrom, realTo);
1519
+ if (Platform.conicGradientRotate90) {
1520
+ scaleOfOuter(transform, realFrom, width / height * (stretch || 1), 1);
1521
+ rotateOfOuter(transform, realFrom, angle + 90);
1522
+ }
1523
+ else {
1524
+ scaleOfOuter(transform, realFrom, 1, width / height * (stretch || 1));
1525
+ rotateOfOuter(transform, realFrom, angle);
1526
+ }
1527
+ 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));
1528
+ applyStops(style, paint.stops, opacity);
1529
+ const data = { type, style, transform };
1530
+ if (blendMode)
1531
+ data.blendMode = blendMode;
1532
+ return data;
1533
+ }
1534
+
1535
+ let recycleMap;
1536
+ function compute(ui, attrName) {
1537
+ const value = [];
1538
+ let item;
1539
+ let paints = ui.__.__input[attrName];
1540
+ if (!(paints instanceof Array))
1541
+ paints = [paints];
1542
+ recycleMap = recycleImage(ui.__, attrName);
1543
+ for (let i = 0, len = paints.length; i < len; i++) {
1544
+ item = getLeafPaint(ui, paints[i], attrName);
1545
+ if (item)
1546
+ value.push(item);
1547
+ }
1548
+ ui.__['_' + attrName] = value.length ? value : undefined;
1549
+ }
1550
+ function getLeafPaint(ui, paint, attrName) {
1551
+ if (typeof paint !== 'object' || paint.visible === false || paint.opacity === 0)
1552
+ return undefined;
1553
+ const { boxBounds } = ui.__layout;
1554
+ switch (paint.type) {
1555
+ case 'solid':
1556
+ let { type, blendMode, color, opacity } = paint;
1557
+ return { type, blendMode, style: ColorConvert$1.string(color, opacity) };
1558
+ case 'image':
1559
+ return image(ui, attrName, paint, boxBounds, !recycleMap || !recycleMap[paint.url]);
1560
+ case 'linear':
1561
+ return linearGradient(paint, boxBounds);
1562
+ case 'radial':
1563
+ return radialGradient(paint, boxBounds);
1564
+ case 'angular':
1565
+ return conicGradient(paint, boxBounds);
1566
+ default:
1567
+ return paint.r ? { type: 'solid', style: ColorConvert$1.string(paint) } : undefined;
1568
+ }
1569
+ }
1570
+
1571
+ var UIPaint = /*#__PURE__*/Object.freeze({
1572
+ __proto__: null,
1573
+ compute: compute,
1574
+ drawTextStroke: drawTextStroke,
1575
+ fill: fill,
1576
+ fillText: fillText,
1577
+ fills: fills,
1578
+ recycleImage: recycleImage,
1579
+ shape: shape,
1580
+ stroke: stroke,
1581
+ strokeText: strokeText,
1582
+ strokes: strokes
1583
+ });
1584
+
1585
+ const { copy, toOffsetOutBounds: toOffsetOutBounds$1 } = BoundsHelper;
1586
+ const tempBounds = {};
1587
+ const offsetOutBounds$1 = {};
1588
+ function shadow(ui, current, shape, _options) {
1589
+ let copyBounds, spreadScale;
1590
+ const { __world, __layout } = ui;
1591
+ const { shadow } = ui.__;
1592
+ const { worldCanvas, bounds, shapeBounds, scaleX, scaleY } = shape;
1593
+ const other = current.getSameCanvas();
1594
+ const end = shadow.length - 1;
1595
+ toOffsetOutBounds$1(bounds, offsetOutBounds$1);
1596
+ shadow.forEach((item, index) => {
1597
+ other.setWorldShadow((offsetOutBounds$1.offsetX + item.x * scaleX), (offsetOutBounds$1.offsetY + item.y * scaleY), item.blur * scaleX, item.color);
1598
+ spreadScale = item.spread ? 1 + item.spread * 2 / (__layout.boxBounds.width + (__layout.strokeBoxSpread || 0) * 2) : 0;
1599
+ drawWorldShadow(other, offsetOutBounds$1, spreadScale, shape);
1600
+ copyBounds = bounds;
1601
+ if (item.box) {
1602
+ other.restore();
1603
+ other.save();
1604
+ if (worldCanvas) {
1605
+ other.copyWorld(other, bounds, __world, 'copy');
1606
+ copyBounds = __world;
1607
+ }
1608
+ worldCanvas ? other.copyWorld(worldCanvas, __world, __world, 'destination-out') : other.copyWorld(shape.canvas, shapeBounds, bounds, 'destination-out');
1609
+ }
1610
+ if (ui.__hasMirror) {
1611
+ current.copyWorldByReset(other, copyBounds, __world, item.blendMode);
1612
+ }
1613
+ else {
1614
+ current.copyWorldToInner(other, copyBounds, __layout.renderBounds, item.blendMode);
1615
+ }
1616
+ if (end && index < end)
1617
+ other.clear();
1618
+ });
1619
+ other.recycle();
1620
+ }
1621
+ function drawWorldShadow(canvas, outBounds, spreadScale, shape) {
1622
+ const { bounds, shapeBounds } = shape;
1623
+ if (Platform.fullImageShadow) {
1624
+ copy(tempBounds, canvas.bounds);
1625
+ tempBounds.x += (outBounds.x - shapeBounds.x);
1626
+ tempBounds.y += (outBounds.y - shapeBounds.y);
1627
+ if (spreadScale) {
1628
+ const { matrix } = shape;
1629
+ tempBounds.x -= (bounds.x + (matrix ? matrix.e : 0) + bounds.width / 2) * (spreadScale - 1);
1630
+ tempBounds.y -= (bounds.y + (matrix ? matrix.f : 0) + bounds.height / 2) * (spreadScale - 1);
1631
+ tempBounds.width *= spreadScale;
1632
+ tempBounds.height *= spreadScale;
1633
+ }
1634
+ canvas.copyWorld(shape.canvas, canvas.bounds, tempBounds);
1635
+ }
1636
+ else {
1637
+ if (spreadScale) {
1638
+ copy(tempBounds, outBounds);
1639
+ tempBounds.x -= (outBounds.width / 2) * (spreadScale - 1);
1640
+ tempBounds.y -= (outBounds.height / 2) * (spreadScale - 1);
1641
+ tempBounds.width *= spreadScale;
1642
+ tempBounds.height *= spreadScale;
1643
+ }
1644
+ canvas.copyWorld(shape.canvas, shapeBounds, spreadScale ? tempBounds : outBounds);
1645
+ }
1646
+ }
1647
+
1648
+ const { toOffsetOutBounds } = BoundsHelper;
1649
+ const offsetOutBounds = {};
1650
+ function innerShadow(ui, current, shape, _options) {
1651
+ let copyBounds, spreadScale;
1652
+ const { __world, __layout: __layout } = ui;
1653
+ const { innerShadow } = ui.__;
1654
+ const { worldCanvas, bounds, shapeBounds, scaleX, scaleY } = shape;
1655
+ const other = current.getSameCanvas();
1656
+ const end = innerShadow.length - 1;
1657
+ toOffsetOutBounds(bounds, offsetOutBounds);
1658
+ innerShadow.forEach((item, index) => {
1659
+ other.save();
1660
+ other.setWorldShadow((offsetOutBounds.offsetX + item.x * scaleX), (offsetOutBounds.offsetY + item.y * scaleY), item.blur * scaleX);
1661
+ spreadScale = item.spread ? 1 - item.spread * 2 / (__layout.boxBounds.width + (__layout.strokeBoxSpread || 0) * 2) : 0;
1662
+ drawWorldShadow(other, offsetOutBounds, spreadScale, shape);
1663
+ other.restore();
1664
+ if (worldCanvas) {
1665
+ other.copyWorld(other, bounds, __world, 'copy');
1666
+ other.copyWorld(worldCanvas, __world, __world, 'source-out');
1667
+ copyBounds = __world;
1668
+ }
1669
+ else {
1670
+ other.copyWorld(shape.canvas, shapeBounds, bounds, 'source-out');
1671
+ copyBounds = bounds;
1672
+ }
1673
+ other.fillWorld(copyBounds, item.color, 'source-in');
1674
+ if (ui.__hasMirror) {
1675
+ current.copyWorldByReset(other, copyBounds, __world, item.blendMode);
1676
+ }
1677
+ else {
1678
+ current.copyWorldToInner(other, copyBounds, __layout.renderBounds, item.blendMode);
1679
+ }
1680
+ if (end && index < end)
1681
+ other.clear();
1682
+ });
1683
+ other.recycle();
1684
+ }
1685
+
1686
+ function blur(ui, current, origin) {
1687
+ const { blur } = ui.__;
1688
+ origin.setWorldBlur(blur * ui.__world.a);
1689
+ origin.copyWorldToInner(current, ui.__world, ui.__layout.renderBounds);
1690
+ origin.filter = 'none';
1691
+ }
1692
+
1693
+ var UIEffect = /*#__PURE__*/Object.freeze({
1694
+ __proto__: null,
1695
+ blur: blur,
1696
+ innerShadow: innerShadow,
1697
+ shadow: shadow
1698
+ });
1699
+
1700
+ const money = '¥¥$€££¢¢';
1701
+ const letter = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
1702
+ const langBefore = '《(「〈『〖【〔{┌<‘“=' + money;
1703
+ const langAfter = '》)」〉』〗】〕}┐>’”!?,、。:;‰';
1704
+ const langSymbol = '≮≯≈≠=…';
1705
+ const langBreak$1 = '—/~|┆·';
1706
+ const beforeChar = '{[(<\'"' + langBefore;
1707
+ const afterChar = '>)]}%!?,.:;\'"' + langAfter;
1708
+ const symbolChar = afterChar + '_#~&*+\\=|' + langSymbol;
1709
+ const breakChar = '- ' + langBreak$1;
1710
+ const cjkRangeList = [
1711
+ [0x4E00, 0x9FFF],
1712
+ [0x3400, 0x4DBF],
1713
+ [0x20000, 0x2A6DF],
1714
+ [0x2A700, 0x2B73F],
1715
+ [0x2B740, 0x2B81F],
1716
+ [0x2B820, 0x2CEAF],
1717
+ [0x2CEB0, 0x2EBEF],
1718
+ [0x30000, 0x3134F],
1719
+ [0x31350, 0x323AF],
1720
+ [0x2E80, 0x2EFF],
1721
+ [0x2F00, 0x2FDF],
1722
+ [0x2FF0, 0x2FFF],
1723
+ [0x3000, 0x303F],
1724
+ [0x31C0, 0x31EF],
1725
+ [0x3200, 0x32FF],
1726
+ [0x3300, 0x33FF],
1727
+ [0xF900, 0xFAFF],
1728
+ [0xFE30, 0xFE4F],
1729
+ [0x1F200, 0x1F2FF],
1730
+ [0x2F800, 0x2FA1F],
1731
+ ];
1732
+ const cjkReg = new RegExp(cjkRangeList.map(([start, end]) => `[\\u${start.toString(16)}-\\u${end.toString(16)}]`).join('|'));
1733
+ function mapChar(str) {
1734
+ const map = {};
1735
+ str.split('').forEach(char => map[char] = true);
1736
+ return map;
1737
+ }
1738
+ const letterMap = mapChar(letter);
1739
+ const beforeMap = mapChar(beforeChar);
1740
+ const afterMap = mapChar(afterChar);
1741
+ const symbolMap = mapChar(symbolChar);
1742
+ const breakMap = mapChar(breakChar);
1743
+ var CharType;
1744
+ (function (CharType) {
1745
+ CharType[CharType["Letter"] = 0] = "Letter";
1746
+ CharType[CharType["Single"] = 1] = "Single";
1747
+ CharType[CharType["Before"] = 2] = "Before";
1748
+ CharType[CharType["After"] = 3] = "After";
1749
+ CharType[CharType["Symbol"] = 4] = "Symbol";
1750
+ CharType[CharType["Break"] = 5] = "Break";
1751
+ })(CharType || (CharType = {}));
1752
+ const { Letter: Letter$1, Single: Single$1, Before: Before$1, After: After$1, Symbol: Symbol$1, Break: Break$1 } = CharType;
1753
+ function getCharType(char) {
1754
+ if (letterMap[char]) {
1755
+ return Letter$1;
1756
+ }
1757
+ else if (breakMap[char]) {
1758
+ return Break$1;
1759
+ }
1760
+ else if (beforeMap[char]) {
1761
+ return Before$1;
1762
+ }
1763
+ else if (afterMap[char]) {
1764
+ return After$1;
1765
+ }
1766
+ else if (symbolMap[char]) {
1767
+ return Symbol$1;
1768
+ }
1769
+ else if (cjkReg.test(char)) {
1770
+ return Single$1;
1771
+ }
1772
+ else {
1773
+ return Letter$1;
1774
+ }
1775
+ }
1776
+
1777
+ const TextRowHelper = {
1778
+ trimRight(row) {
1779
+ const { words } = row;
1780
+ let trimRight = 0, len = words.length, char;
1781
+ for (let i = len - 1; i > -1; i--) {
1782
+ char = words[i].data[0];
1783
+ if (char.char === ' ') {
1784
+ trimRight++;
1785
+ row.width -= char.width;
1786
+ }
1787
+ else {
1788
+ break;
1789
+ }
1790
+ }
1791
+ if (trimRight)
1792
+ words.splice(len - trimRight, trimRight);
1793
+ }
1794
+ };
1795
+
1796
+ function getTextCase(char, textCase, firstChar) {
1797
+ switch (textCase) {
1798
+ case 'title':
1799
+ return firstChar ? char.toUpperCase() : char;
1800
+ case 'upper':
1801
+ return char.toUpperCase();
1802
+ case 'lower':
1803
+ return char.toLowerCase();
1804
+ default:
1805
+ return char;
1806
+ }
1807
+ }
1808
+
1809
+ const { trimRight } = TextRowHelper;
1810
+ const { Letter, Single, Before, After, Symbol, Break } = CharType;
1811
+ let word, row, wordWidth, rowWidth, realWidth;
1812
+ let char, charWidth, charType, lastCharType, langBreak, afterBreak, paraStart;
1813
+ let textDrawData, rows = [], bounds;
1814
+ function createRows(drawData, content, style) {
1815
+ textDrawData = drawData;
1816
+ rows = drawData.rows;
1817
+ bounds = drawData.bounds;
1818
+ const { __letterSpacing, paraIndent, textCase } = style;
1819
+ const { canvas } = Platform;
1820
+ const { width, height } = bounds;
1821
+ const charMode = width || height || __letterSpacing || (textCase !== 'none');
1822
+ if (charMode) {
1823
+ paraStart = true;
1824
+ lastCharType = null;
1825
+ wordWidth = rowWidth = 0;
1826
+ word = { data: [] }, row = { words: [] };
1827
+ for (let i = 0, len = content.length; i < len; i++) {
1828
+ char = content[i];
1829
+ if (char === '\n') {
1830
+ if (wordWidth)
1831
+ addWord();
1832
+ row.paraEnd = true;
1833
+ addRow();
1834
+ paraStart = true;
1835
+ }
1836
+ else {
1837
+ charType = getCharType(char);
1838
+ if (charType === Letter && textCase !== 'none')
1839
+ char = getTextCase(char, textCase, !wordWidth);
1840
+ charWidth = canvas.measureText(char).width;
1841
+ if (__letterSpacing)
1842
+ charWidth += __letterSpacing;
1843
+ langBreak = (charType === Single && (lastCharType === Single || lastCharType === Letter)) || (lastCharType === Single && charType !== After);
1844
+ afterBreak = ((charType === Before || charType === Single) && (lastCharType === Symbol || lastCharType === After));
1845
+ realWidth = paraStart && paraIndent ? width - paraIndent : width;
1846
+ if (width && rowWidth + wordWidth + charWidth > realWidth) {
1847
+ if (!afterBreak)
1848
+ afterBreak = charType === Letter && lastCharType == After;
1849
+ if (langBreak || afterBreak || charType === Break || charType === Before || charType === Single || (wordWidth + charWidth > realWidth)) {
1850
+ if (wordWidth)
1851
+ addWord();
1852
+ addRow();
1853
+ }
1854
+ else {
1855
+ addRow();
1856
+ }
1857
+ }
1858
+ if (char === ' ' && paraStart !== true && (rowWidth + wordWidth) === 0) ;
1859
+ else {
1860
+ if (charType === Break) {
1861
+ if (char === ' ' && wordWidth)
1862
+ addWord();
1863
+ addChar(char, charWidth);
1864
+ addWord();
1865
+ }
1866
+ else if (langBreak || afterBreak) {
1867
+ if (wordWidth)
1868
+ addWord();
1869
+ addChar(char, charWidth);
1870
+ }
1871
+ else {
1872
+ addChar(char, charWidth);
1873
+ }
1874
+ }
1875
+ lastCharType = charType;
1876
+ }
1877
+ }
1878
+ if (wordWidth)
1879
+ addWord();
1880
+ if (rowWidth)
1881
+ addRow();
1882
+ rows.length > 0 && (rows[rows.length - 1].paraEnd = true);
1883
+ }
1884
+ else {
1885
+ content.split('\n').forEach(content => {
1886
+ textDrawData.paraNumber++;
1887
+ rows.push({ x: paraIndent || 0, text: content, width: canvas.measureText(content).width, paraStart: true });
1888
+ });
1889
+ }
1890
+ }
1891
+ function addChar(char, width) {
1892
+ word.data.push({ char, width });
1893
+ wordWidth += width;
1894
+ }
1895
+ function addWord() {
1896
+ rowWidth += wordWidth;
1897
+ word.width = wordWidth;
1898
+ row.words.push(word);
1899
+ word = { data: [] };
1900
+ wordWidth = 0;
1901
+ }
1902
+ function addRow() {
1903
+ if (paraStart) {
1904
+ textDrawData.paraNumber++;
1905
+ row.paraStart = true;
1906
+ paraStart = false;
1907
+ }
1908
+ row.width = rowWidth;
1909
+ if (bounds.width)
1910
+ trimRight(row);
1911
+ rows.push(row);
1912
+ row = { words: [] };
1913
+ rowWidth = 0;
1914
+ }
1915
+
1916
+ const CharMode = 0;
1917
+ const WordMode = 1;
1918
+ const RowMode = 2;
1919
+ function layoutChar(drawData, style, width, _height) {
1920
+ const { rows } = drawData;
1921
+ const { textAlign, paraIndent, letterSpacing } = style;
1922
+ let charX, addWordWidth, indentWidth, mode, wordChar;
1923
+ rows.forEach(row => {
1924
+ if (row.words) {
1925
+ indentWidth = paraIndent && row.paraStart ? paraIndent : 0;
1926
+ addWordWidth = (width && textAlign === 'justify' && row.words.length > 1) ? (width - row.width - indentWidth) / (row.words.length - 1) : 0;
1927
+ mode = (letterSpacing || row.isOverflow) ? CharMode : (addWordWidth > 0.01 ? WordMode : RowMode);
1928
+ if (mode === RowMode) {
1929
+ row.text = '';
1930
+ row.x += indentWidth;
1931
+ row.words.forEach(word => {
1932
+ word.data.forEach(char => {
1933
+ row.text += char.char;
1934
+ });
1935
+ });
1936
+ }
1937
+ else {
1938
+ row.x += indentWidth;
1939
+ charX = row.x;
1940
+ row.data = [];
1941
+ row.words.forEach(word => {
1942
+ if (mode === WordMode) {
1943
+ wordChar = { char: '', x: charX };
1944
+ charX = toWordChar(word.data, charX, wordChar);
1945
+ if (wordChar.char !== ' ')
1946
+ row.data.push(wordChar);
1947
+ }
1948
+ else {
1949
+ charX = toChar(word.data, charX, row.data);
1950
+ }
1951
+ if (!row.paraEnd && addWordWidth) {
1952
+ charX += addWordWidth;
1953
+ row.width += addWordWidth;
1954
+ }
1955
+ });
1956
+ }
1957
+ row.words = null;
1958
+ }
1959
+ });
1960
+ }
1961
+ function toWordChar(data, charX, wordChar) {
1962
+ data.forEach(char => {
1963
+ wordChar.char += char.char;
1964
+ charX += char.width;
1965
+ });
1966
+ return charX;
1967
+ }
1968
+ function toChar(data, charX, rowData) {
1969
+ data.forEach(char => {
1970
+ if (char.char !== ' ') {
1971
+ char.x = charX;
1972
+ rowData.push(char);
1973
+ }
1974
+ charX += char.width;
1975
+ });
1976
+ return charX;
1977
+ }
1978
+
1979
+ function layoutText(drawData, style) {
1980
+ const { rows, bounds } = drawData;
1981
+ const { __lineHeight, __baseLine, textAlign, verticalAlign, paraSpacing, textOverflow } = style;
1982
+ let { x, y, width, height } = bounds, realHeight = __lineHeight * rows.length + (paraSpacing ? paraSpacing * (drawData.paraNumber - 1) : 0);
1983
+ let starY = __baseLine;
1984
+ if (textOverflow !== 'show' && realHeight > height) {
1985
+ realHeight = Math.max(height, __lineHeight);
1986
+ drawData.overflow = rows.length;
1987
+ }
1988
+ else {
1989
+ switch (verticalAlign) {
1990
+ case 'middle':
1991
+ y += (height - realHeight) / 2;
1992
+ break;
1993
+ case 'bottom':
1994
+ y += (height - realHeight);
1995
+ }
1996
+ }
1997
+ starY += y;
1998
+ let row;
1999
+ for (let i = 0, len = rows.length; i < len; i++) {
2000
+ row = rows[i];
2001
+ row.x = x;
2002
+ switch (textAlign) {
2003
+ case 'center':
2004
+ row.x += (width - row.width) / 2;
2005
+ break;
2006
+ case 'right':
2007
+ row.x += width - row.width;
2008
+ }
2009
+ if (row.paraStart && paraSpacing && i > 0)
2010
+ starY += paraSpacing;
2011
+ row.y = starY;
2012
+ starY += __lineHeight;
2013
+ if (drawData.overflow > i && starY > realHeight) {
2014
+ row.isOverflow = true;
2015
+ drawData.overflow = i + 1;
2016
+ }
2017
+ if (row.width < 0) {
2018
+ const charWidth = row.words[0].data[0].width;
2019
+ const rowX = row.x + row.width;
2020
+ if (rowX < bounds.x)
2021
+ bounds.x = rowX - charWidth;
2022
+ if (-row.width > bounds.width)
2023
+ bounds.width = -row.width + style.fontSize + charWidth;
2024
+ }
2025
+ else {
2026
+ if (row.x < bounds.x)
2027
+ bounds.x = row.x;
2028
+ if (row.width > bounds.width)
2029
+ bounds.width = row.width;
2030
+ }
2031
+ }
2032
+ bounds.y = y;
2033
+ bounds.height = realHeight;
2034
+ }
2035
+
2036
+ function clipText(drawData, textOverflow) {
2037
+ const { rows, overflow } = drawData;
2038
+ rows.splice(overflow);
2039
+ if (textOverflow !== 'hide') {
2040
+ if (textOverflow === 'ellipsis')
2041
+ textOverflow = '...';
2042
+ const ellipsisWidth = Platform.canvas.measureText(textOverflow).width;
2043
+ const row = rows[overflow - 1];
2044
+ let char, end = row.data.length - 1, charRight;
2045
+ const { x, width } = drawData.bounds;
2046
+ const right = x + width - ellipsisWidth;
2047
+ for (let i = end; i > -1; i--) {
2048
+ char = row.data[i];
2049
+ charRight = char.x + char.width;
2050
+ if (i === end && charRight < right) {
2051
+ break;
2052
+ }
2053
+ else if (charRight < right && char.char !== ' ') {
2054
+ row.data.splice(i + 1);
2055
+ row.width -= char.width;
2056
+ break;
2057
+ }
2058
+ row.width -= char.width;
2059
+ }
2060
+ row.width += ellipsisWidth;
2061
+ row.data.push({ char: textOverflow, x: charRight });
2062
+ }
2063
+ }
2064
+
2065
+ function decorationText(drawData, style) {
2066
+ const { fontSize } = style;
2067
+ drawData.decorationHeight = fontSize / 11;
2068
+ switch (style.textDecoration) {
2069
+ case 'under':
2070
+ drawData.decorationY = fontSize * 0.15;
2071
+ break;
2072
+ case 'delete':
2073
+ drawData.decorationY = -fontSize * 0.35;
2074
+ }
2075
+ }
2076
+
2077
+ const TextConvert = {
2078
+ getDrawData(content, style) {
2079
+ if (typeof content !== 'string')
2080
+ content = String(content);
2081
+ let x = 0, y = 0;
2082
+ let { width, height, padding } = style;
2083
+ const { textDecoration, textOverflow, __font } = style;
2084
+ if (!width)
2085
+ width = 0;
2086
+ if (padding) {
2087
+ const [top, right, bottom, left] = MathHelper.fourNumber(padding);
2088
+ if (width) {
2089
+ x = left;
2090
+ width -= (right + left);
2091
+ }
2092
+ if (height) {
2093
+ y = top;
2094
+ height -= (top + bottom);
2095
+ }
2096
+ }
2097
+ const drawData = {
2098
+ bounds: { x, y, width, height },
2099
+ rows: [],
2100
+ paraNumber: 0,
2101
+ font: Platform.canvas.font = __font
2102
+ };
2103
+ createRows(drawData, content, style);
2104
+ layoutText(drawData, style);
2105
+ layoutChar(drawData, style, width);
2106
+ if (drawData.overflow)
2107
+ clipText(drawData, textOverflow);
2108
+ if (textDecoration !== 'none')
2109
+ decorationText(drawData, style);
2110
+ return drawData;
2111
+ }
2112
+ };
2113
+
2114
+ const ColorConvert = {
2115
+ string(color, opacity) {
2116
+ if (typeof color === 'string')
2117
+ return color;
2118
+ let a = color.a === undefined ? 1 : color.a;
2119
+ if (opacity)
2120
+ a *= opacity;
2121
+ const rgb = color.r + ',' + color.g + ',' + color.b;
2122
+ return a === 1 ? 'rgb(' + rgb + ')' : 'rgba(' + rgb + ',' + a + ')';
2123
+ }
2124
+ };
2125
+
2126
+ const Export = {
2127
+ export(leaf, filename, options) {
2128
+ return addTask((success) => new Promise((resolve) => {
2129
+ const { leafer } = leaf;
2130
+ if (leafer) {
2131
+ leafer.waitViewCompleted(() => __awaiter(this, void 0, void 0, function* () {
2132
+ let quality, blob;
2133
+ let { canvas } = leafer;
2134
+ let { unreal } = canvas;
2135
+ if (unreal) {
2136
+ canvas = canvas.getSameCanvas();
2137
+ canvas.backgroundColor = leafer.config.fill;
2138
+ leafer.__render(canvas, {});
2139
+ }
2140
+ switch (typeof options) {
2141
+ case 'object':
2142
+ if (options.quality)
2143
+ quality = options.quality;
2144
+ if (options.blob)
2145
+ blob = true;
2146
+ break;
2147
+ case 'number':
2148
+ quality = options;
2149
+ break;
2150
+ case 'boolean':
2151
+ blob = options;
2152
+ }
2153
+ let data;
2154
+ if (filename.includes('.')) {
2155
+ data = yield canvas.saveAs(filename, quality);
2156
+ }
2157
+ else if (blob) {
2158
+ data = yield canvas.toBlob(filename, quality);
2159
+ }
2160
+ else {
2161
+ data = yield canvas.toDataURL(filename, quality);
2162
+ }
2163
+ success({ data });
2164
+ resolve();
2165
+ if (unreal)
2166
+ canvas.recycle();
2167
+ }));
2168
+ }
2169
+ else {
2170
+ success({ data: false });
2171
+ resolve();
2172
+ }
2173
+ }));
2174
+ }
2175
+ };
2176
+ let tasker;
2177
+ function addTask(task) {
2178
+ if (!tasker)
2179
+ tasker = new TaskProcessor();
2180
+ return new Promise((resolve) => {
2181
+ tasker.add(() => __awaiter(this, void 0, void 0, function* () { return yield task(resolve); }), { parallel: false });
2182
+ });
2183
+ }
2184
+
2185
+ Object.assign(Paint, UIPaint);
2186
+ Object.assign(Effect, UIEffect);
2187
+ Object.assign(TextConvert$1, TextConvert);
2188
+ Object.assign(ColorConvert$1, ColorConvert);
2189
+ Object.assign(Export$1, Export);
2190
+
2191
+ useCanvas();
2192
+
2193
+ export { Layouter, LeaferCanvas, Renderer, Selector, Watcher, useCanvas };