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