@leafer-draw/node 1.0.0-rc.10

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