@leafer-draw/node 1.4.2 → 1.5.0

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 CHANGED
@@ -1966,13 +1966,14 @@ function toChar(data, charX, rowData, isOverflow) {
1966
1966
  }
1967
1967
 
1968
1968
  function layoutText(drawData, style) {
1969
- const { rows, bounds } = drawData;
1969
+ const { rows, bounds } = drawData, countRows = rows.length;
1970
1970
  const { __lineHeight, __baseLine, __letterSpacing, __clipText, textAlign, verticalAlign, paraSpacing, autoSizeAlign } = style;
1971
- let { x, y, width, height } = bounds, realHeight = __lineHeight * rows.length + (paraSpacing ? paraSpacing * (drawData.paraNumber - 1) : 0);
1971
+ let { x, y, width, height } = bounds, realHeight = __lineHeight * countRows + (paraSpacing ? paraSpacing * (drawData.paraNumber - 1) : 0);
1972
1972
  let starY = __baseLine;
1973
1973
  if (__clipText && realHeight > height) {
1974
1974
  realHeight = Math.max(height, __lineHeight);
1975
- drawData.overflow = rows.length;
1975
+ if (countRows > 1)
1976
+ drawData.overflow = countRows;
1976
1977
  }
1977
1978
  else if (height || autoSizeAlign) {
1978
1979
  switch (verticalAlign) {
@@ -1984,7 +1985,7 @@ function layoutText(drawData, style) {
1984
1985
  }
1985
1986
  starY += y;
1986
1987
  let row, rowX, rowWidth, layoutWidth = (width || autoSizeAlign) ? width : drawData.maxWidth;
1987
- for (let i = 0, len = rows.length; i < len; i++) {
1988
+ for (let i = 0, len = countRows; i < len; i++) {
1988
1989
  row = rows[i];
1989
1990
  row.x = x;
1990
1991
  if (row.width < width || (row.width > width && !__clipText)) {
@@ -2053,7 +2054,7 @@ function clipText(drawData, style, x, width) {
2053
2054
  if (i === end && charRight < right) {
2054
2055
  break;
2055
2056
  }
2056
- else if (charRight < right && char.char !== ' ') {
2057
+ else if ((charRight < right && char.char !== ' ') || !i) {
2057
2058
  row.data.splice(i + 1);
2058
2059
  row.width -= char.width;
2059
2060
  break;
@@ -2197,124 +2198,134 @@ function getTrimBounds(canvas) {
2197
2198
  }
2198
2199
 
2199
2200
  const ExportModule = {
2200
- export(leaf, filename, options) {
2201
+ syncExport(leaf, filename, options) {
2201
2202
  this.running = true;
2203
+ let result;
2202
2204
  const fileType = draw.FileHelper.fileType(filename);
2203
2205
  const isDownload = filename.includes('.');
2204
2206
  options = draw.FileHelper.getExportOptions(options);
2205
- return addTask((success) => new Promise((resolve) => {
2206
- const over = (result) => {
2207
- success(result);
2208
- resolve();
2209
- this.running = false;
2210
- };
2211
- const { toURL } = draw.Platform;
2212
- const { download } = draw.Platform.origin;
2213
- if (fileType === 'json') {
2214
- isDownload && download(toURL(JSON.stringify(leaf.toJSON(options.json)), 'text'), filename);
2215
- return over({ data: isDownload ? true : leaf.toJSON(options.json) });
2207
+ const { toURL } = draw.Platform;
2208
+ const { download } = draw.Platform.origin;
2209
+ if (fileType === 'json') {
2210
+ isDownload && download(toURL(JSON.stringify(leaf.toJSON(options.json)), 'text'), filename);
2211
+ result = { data: isDownload ? true : leaf.toJSON(options.json) };
2212
+ }
2213
+ else if (fileType === 'svg') {
2214
+ isDownload && download(toURL(leaf.toSVG(), 'svg'), filename);
2215
+ result = { data: isDownload ? true : leaf.toSVG() };
2216
+ }
2217
+ else {
2218
+ let renderBounds, trimBounds, scaleX = 1, scaleY = 1;
2219
+ const { worldTransform, isLeafer, leafer, isFrame } = leaf;
2220
+ const { slice, trim, padding, onCanvas } = options;
2221
+ const smooth = options.smooth === undefined ? (leafer ? leafer.config.smooth : true) : options.smooth;
2222
+ const contextSettings = options.contextSettings || (leafer ? leafer.config.contextSettings : undefined);
2223
+ const screenshot = options.screenshot || leaf.isApp;
2224
+ const fill = (isLeafer && screenshot) ? (options.fill === undefined ? leaf.fill : options.fill) : options.fill;
2225
+ const needFill = draw.FileHelper.isOpaqueImage(filename) || fill, matrix = new draw.Matrix();
2226
+ if (screenshot) {
2227
+ renderBounds = screenshot === true ? (isLeafer ? leafer.canvas.bounds : leaf.worldRenderBounds) : screenshot;
2216
2228
  }
2217
- if (fileType === 'svg') {
2218
- isDownload && download(toURL(leaf.toSVG(), 'svg'), filename);
2219
- return over({ data: isDownload ? true : leaf.toSVG() });
2229
+ else {
2230
+ let relative = options.relative || (isLeafer ? 'inner' : 'local');
2231
+ scaleX = worldTransform.scaleX;
2232
+ scaleY = worldTransform.scaleY;
2233
+ switch (relative) {
2234
+ case 'inner':
2235
+ matrix.set(worldTransform);
2236
+ break;
2237
+ case 'local':
2238
+ matrix.set(worldTransform).divide(leaf.localTransform);
2239
+ scaleX /= leaf.scaleX;
2240
+ scaleY /= leaf.scaleY;
2241
+ break;
2242
+ case 'world':
2243
+ scaleX = 1;
2244
+ scaleY = 1;
2245
+ break;
2246
+ case 'page':
2247
+ relative = leafer || leaf;
2248
+ default:
2249
+ matrix.set(worldTransform).divide(leaf.getTransform(relative));
2250
+ const l = relative.worldTransform;
2251
+ scaleX /= scaleX / l.scaleX;
2252
+ scaleY /= scaleY / l.scaleY;
2253
+ }
2254
+ renderBounds = leaf.getBounds('render', relative);
2220
2255
  }
2221
- const { leafer } = leaf;
2222
- if (leafer) {
2223
- checkLazy(leaf);
2224
- leafer.waitViewCompleted(() => __awaiter(this, void 0, void 0, function* () {
2225
- let renderBounds, trimBounds, scaleX = 1, scaleY = 1;
2226
- const { worldTransform, isLeafer, isFrame } = leaf;
2227
- const { slice, trim, padding, onCanvas } = options;
2228
- const smooth = options.smooth === undefined ? leafer.config.smooth : options.smooth;
2229
- const contextSettings = options.contextSettings || leafer.config.contextSettings;
2230
- const screenshot = options.screenshot || leaf.isApp;
2231
- const fill = (isLeafer && screenshot) ? (options.fill === undefined ? leaf.fill : options.fill) : options.fill;
2232
- const needFill = draw.FileHelper.isOpaqueImage(filename) || fill, matrix = new draw.Matrix();
2233
- if (screenshot) {
2234
- renderBounds = screenshot === true ? (isLeafer ? leafer.canvas.bounds : leaf.worldRenderBounds) : screenshot;
2235
- }
2236
- else {
2237
- let relative = options.relative || (isLeafer ? 'inner' : 'local');
2238
- scaleX = worldTransform.scaleX;
2239
- scaleY = worldTransform.scaleY;
2240
- switch (relative) {
2241
- case 'inner':
2242
- matrix.set(worldTransform);
2243
- break;
2244
- case 'local':
2245
- matrix.set(worldTransform).divide(leaf.localTransform);
2246
- scaleX /= leaf.scaleX;
2247
- scaleY /= leaf.scaleY;
2248
- break;
2249
- case 'world':
2250
- scaleX = 1;
2251
- scaleY = 1;
2252
- break;
2253
- case 'page':
2254
- relative = leaf.leafer;
2255
- default:
2256
- matrix.set(worldTransform).divide(leaf.getTransform(relative));
2257
- const l = relative.worldTransform;
2258
- scaleX /= scaleX / l.scaleX;
2259
- scaleY /= scaleY / l.scaleY;
2260
- }
2261
- renderBounds = leaf.getBounds('render', relative);
2262
- }
2263
- const scaleData = { scaleX: 1, scaleY: 1 };
2264
- draw.MathHelper.getScaleData(options.scale, options.size, renderBounds, scaleData);
2265
- let pixelRatio = options.pixelRatio || 1;
2266
- if (leaf.isApp) {
2267
- scaleData.scaleX *= pixelRatio;
2268
- scaleData.scaleY *= pixelRatio;
2269
- pixelRatio = leaf.app.pixelRatio;
2270
- }
2271
- const { x, y, width, height } = new draw.Bounds(renderBounds).scale(scaleData.scaleX, scaleData.scaleY);
2272
- const renderOptions = { matrix: matrix.scale(1 / scaleData.scaleX, 1 / scaleData.scaleY).invert().translate(-x, -y).withScale(1 / scaleX * scaleData.scaleX, 1 / scaleY * scaleData.scaleY) };
2273
- let canvas = draw.Creator.canvas({ width: Math.round(width), height: Math.round(height), pixelRatio, smooth, contextSettings });
2274
- let sliceLeaf;
2275
- if (slice) {
2276
- sliceLeaf = leaf;
2277
- sliceLeaf.__worldOpacity = 0;
2278
- leaf = leafer;
2279
- renderOptions.bounds = canvas.bounds;
2280
- }
2281
- canvas.save();
2282
- if (isFrame && fill !== undefined) {
2283
- const oldFill = leaf.get('fill');
2284
- leaf.fill = '';
2285
- leaf.__render(canvas, renderOptions);
2286
- leaf.fill = oldFill;
2287
- }
2288
- else {
2289
- leaf.__render(canvas, renderOptions);
2290
- }
2291
- canvas.restore();
2292
- if (sliceLeaf)
2293
- sliceLeaf.__updateWorldOpacity();
2294
- if (trim) {
2295
- trimBounds = getTrimBounds(canvas);
2296
- const old = canvas, { width, height } = trimBounds;
2297
- const config = { x: 0, y: 0, width, height, pixelRatio };
2298
- canvas = draw.Creator.canvas(config);
2299
- canvas.copyWorld(old, trimBounds, config);
2300
- }
2301
- if (padding) {
2302
- const [top, right, bottom, left] = draw.MathHelper.fourNumber(padding);
2303
- const old = canvas, { width, height } = old;
2304
- canvas = draw.Creator.canvas({ width: width + left + right, height: height + top + bottom, pixelRatio });
2305
- canvas.copyWorld(old, old.bounds, { x: left, y: top, width, height });
2306
- }
2307
- if (needFill)
2308
- canvas.fillWorld(canvas.bounds, fill || '#FFFFFF', 'destination-over');
2309
- if (onCanvas)
2310
- onCanvas(canvas);
2311
- const data = filename === 'canvas' ? canvas : yield canvas.export(filename, options);
2312
- over({ data, width: canvas.pixelWidth, height: canvas.pixelHeight, renderBounds, trimBounds });
2313
- }));
2256
+ const scaleData = { scaleX: 1, scaleY: 1 };
2257
+ draw.MathHelper.getScaleData(options.scale, options.size, renderBounds, scaleData);
2258
+ let pixelRatio = options.pixelRatio || 1;
2259
+ if (leaf.isApp) {
2260
+ scaleData.scaleX *= pixelRatio;
2261
+ scaleData.scaleY *= pixelRatio;
2262
+ pixelRatio = leaf.app.pixelRatio;
2263
+ }
2264
+ const { x, y, width, height } = new draw.Bounds(renderBounds).scale(scaleData.scaleX, scaleData.scaleY);
2265
+ const renderOptions = { matrix: matrix.scale(1 / scaleData.scaleX, 1 / scaleData.scaleY).invert().translate(-x, -y).withScale(1 / scaleX * scaleData.scaleX, 1 / scaleY * scaleData.scaleY) };
2266
+ let canvas = draw.Creator.canvas({ width: Math.floor(width), height: Math.floor(height), pixelRatio, smooth, contextSettings });
2267
+ let sliceLeaf;
2268
+ if (slice) {
2269
+ sliceLeaf = leaf;
2270
+ sliceLeaf.__worldOpacity = 0;
2271
+ leaf = leafer || leaf;
2272
+ renderOptions.bounds = canvas.bounds;
2273
+ }
2274
+ canvas.save();
2275
+ if (isFrame && fill !== undefined) {
2276
+ const oldFill = leaf.get('fill');
2277
+ leaf.fill = '';
2278
+ leaf.__render(canvas, renderOptions);
2279
+ leaf.fill = oldFill;
2314
2280
  }
2315
2281
  else {
2316
- over({ data: false });
2282
+ leaf.__render(canvas, renderOptions);
2283
+ }
2284
+ canvas.restore();
2285
+ if (sliceLeaf)
2286
+ sliceLeaf.__updateWorldOpacity();
2287
+ if (trim) {
2288
+ trimBounds = getTrimBounds(canvas);
2289
+ const old = canvas, { width, height } = trimBounds;
2290
+ const config = { x: 0, y: 0, width, height, pixelRatio };
2291
+ canvas = draw.Creator.canvas(config);
2292
+ canvas.copyWorld(old, trimBounds, config);
2293
+ }
2294
+ if (padding) {
2295
+ const [top, right, bottom, left] = draw.MathHelper.fourNumber(padding);
2296
+ const old = canvas, { width, height } = old;
2297
+ canvas = draw.Creator.canvas({ width: width + left + right, height: height + top + bottom, pixelRatio });
2298
+ canvas.copyWorld(old, old.bounds, { x: left, y: top, width, height });
2317
2299
  }
2300
+ if (needFill)
2301
+ canvas.fillWorld(canvas.bounds, fill || '#FFFFFF', 'destination-over');
2302
+ if (onCanvas)
2303
+ onCanvas(canvas);
2304
+ const data = filename === 'canvas' ? canvas : canvas.export(filename, options);
2305
+ result = { data, width: canvas.pixelWidth, height: canvas.pixelHeight, renderBounds, trimBounds };
2306
+ }
2307
+ this.running = false;
2308
+ return result;
2309
+ },
2310
+ export(leaf, filename, options) {
2311
+ this.running = true;
2312
+ return addTask((success) => new Promise((resolve) => {
2313
+ const getResult = () => __awaiter(this, void 0, void 0, function* () {
2314
+ if (!draw.Resource.isComplete)
2315
+ return draw.Platform.requestRender(getResult);
2316
+ const result = ExportModule.syncExport(leaf, filename, options);
2317
+ if (result.data instanceof Promise)
2318
+ result.data = yield result.data;
2319
+ success(result);
2320
+ resolve();
2321
+ });
2322
+ leaf.updateLayout();
2323
+ checkLazy(leaf);
2324
+ const { leafer } = leaf;
2325
+ if (leafer)
2326
+ leafer.waitViewCompleted(getResult);
2327
+ else
2328
+ getResult();
2318
2329
  }));
2319
2330
  }
2320
2331
  };
@@ -2373,6 +2384,9 @@ Object.assign(draw.Export, ExportModule);
2373
2384
  draw.UI.prototype.export = function (filename, options) {
2374
2385
  return draw.Export.export(this, filename, options);
2375
2386
  };
2387
+ draw.UI.prototype.syncExport = function (filename, options) {
2388
+ return draw.Export.syncExport(this, filename, options);
2389
+ };
2376
2390
 
2377
2391
  Object.defineProperty(exports, "LeaferImage", {
2378
2392
  enumerable: true,