@mlightcad/pdf-renderer 1.8.0-preview.6 → 1.8.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.
@@ -113,8 +113,32 @@ var AcPdfRenderer = /** @class */ (function () {
113
113
  */
114
114
  this._mtextGlyphCache = new Map();
115
115
  this._shapeGlyphCache = new Map();
116
+ /**
117
+ * Glyph keys that finished rendering with no drawable geometry. Remembered
118
+ * so later collect passes do not re-schedule the same empty work (which
119
+ * would keep {@link collectBlockRoots} looping until its pass budget).
120
+ */
121
+ this._mtextGlyphEmpty = new Set();
122
+ this._shapeGlyphEmpty = new Set();
123
+ /**
124
+ * Empty complex-linetype glyph keys. Kept separate from the entity empty
125
+ * sets so a one-shot miss here cannot suppress later {@link applyVectorMtext}
126
+ * / {@link shape} retries for the same key (SHX/CDN fonts often settle
127
+ * across collect passes). Complex path still converges after one empty.
128
+ */
129
+ this._complexMtextGlyphEmpty = new Set();
130
+ this._complexShapeGlyphEmpty = new Set();
116
131
  this._mtextGlyphInflight = new Map();
117
132
  this._shapeGlyphInflight = new Map();
133
+ /**
134
+ * Decoded image ops keyed by blob size/type + boundary. Blob identity is
135
+ * not stable across collect re-walks, so content-ish keys are required for
136
+ * the second pass to paint synchronously.
137
+ */
138
+ this._imageOpCache = new Map();
139
+ this._imageOpInflight = new Map();
140
+ /** Font names already queued for load during the current collect pass. */
141
+ this._fontLoadDeferred = new Set();
118
142
  this._context = AcGiContext.fromBackgroundColor(this._currentBackgroundColor);
119
143
  this._subEntityTraits = {
120
144
  color: new AcCmColor(),
@@ -589,7 +613,9 @@ var AcPdfRenderer = /** @class */ (function () {
589
613
  // vector-only while fonts.load() is still in flight, and searchable
590
614
  // textMode:'text' output is lost. Once settled (success or miss),
591
615
  // stop deferring so failed resolves do not spin forever.
592
- if (!fonts.isSettled(fontName)) {
616
+ if (!fonts.isSettled(fontName) &&
617
+ !this._fontLoadDeferred.has(fontName)) {
618
+ this._fontLoadDeferred.add(fontName);
593
619
  this.trackPending(fonts.load(fontName).then(function () {
594
620
  /* next collect pass uses sync text when load succeeded */
595
621
  }));
@@ -856,10 +882,12 @@ var AcPdfRenderer = /** @class */ (function () {
856
882
  entity.actualText = plain;
857
883
  }
858
884
  }
885
+ if (this._mtextGlyphEmpty.has(key)) {
886
+ return;
887
+ }
859
888
  var inflight = this._mtextGlyphInflight.get(key);
860
889
  if (!inflight) {
861
890
  var render = Promise.resolve(provider.renderMText(mtext, mapped, glyphColors)).then(function (result) {
862
- var _a, _b;
863
891
  var entry = {
864
892
  primitives: result.primitives,
865
893
  colorGroups: result.colorGroups,
@@ -870,15 +898,15 @@ var AcPdfRenderer = /** @class */ (function () {
870
898
  };
871
899
  // Empty results are usually "font not ready yet"; caching them would
872
900
  // permanently suppress every later instance with the same key.
873
- var hasGeometry = entry.primitives.triangles.length >= 6 ||
874
- entry.primitives.polylines.length >= 2 ||
875
- ((_b = (_a = entry.colorGroups) === null || _a === void 0 ? void 0 : _a.some(function (g) {
876
- return g.primitives.triangles.length >= 6 ||
877
- g.primitives.polylines.length >= 2;
878
- })) !== null && _b !== void 0 ? _b : false);
901
+ var hasGeometry = glyphEntryHasGeometry(entry);
879
902
  if (hasGeometry) {
880
903
  _this._mtextGlyphCache.set(key, entry);
881
904
  }
905
+ else if (_this.mtextGlyphMissIsFinal(mapped.font)) {
906
+ // Font face already settled (or no embeddable fonts): empty will
907
+ // not improve on the next pass — remember it so collect converges.
908
+ _this._mtextGlyphEmpty.add(key);
909
+ }
882
910
  return entry;
883
911
  });
884
912
  this.trackGlyphInflight(this._mtextGlyphInflight, key, render);
@@ -901,6 +929,19 @@ var AcPdfRenderer = /** @class */ (function () {
901
929
  });
902
930
  this.trackPending(pending);
903
931
  };
932
+ /**
933
+ * True when an empty mtext glyph result will not improve on a later collect
934
+ * pass. Vector-only exports (no font manager) must keep retrying — SHX /
935
+ * CDN glyph engines often return empty until fonts finish loading across
936
+ * collect passes. With embeddable fonts, an empty result after the face
937
+ * has settled is permanent.
938
+ */
939
+ AcPdfRenderer.prototype.mtextGlyphMissIsFinal = function (fontName) {
940
+ if (!this._fonts) {
941
+ return false;
942
+ }
943
+ return this._fonts.isSettled(fontName);
944
+ };
904
945
  AcPdfRenderer.prototype.shape = function (shape, style, _delay) {
905
946
  var _this = this;
906
947
  var entity = new AcPdfEntity();
@@ -923,6 +964,9 @@ var AcPdfRenderer = /** @class */ (function () {
923
964
  this.applyCachedGlyphs(entity, cached, localPos, stroke, fill);
924
965
  return this.pushEntity(entity);
925
966
  }
967
+ if (this._shapeGlyphEmpty.has(key)) {
968
+ return this.pushEntity(entity);
969
+ }
926
970
  var inflight = this._shapeGlyphInflight.get(key);
927
971
  if (!inflight) {
928
972
  var render = Promise.resolve(provider.renderShape(shape, mapped)).then(function (result) {
@@ -930,7 +974,12 @@ var AcPdfRenderer = /** @class */ (function () {
930
974
  primitives: result.primitives,
931
975
  box: result.box
932
976
  };
933
- _this._shapeGlyphCache.set(key, entry);
977
+ if (glyphEntryHasGeometry(entry)) {
978
+ _this._shapeGlyphCache.set(key, entry);
979
+ }
980
+ else {
981
+ _this._shapeGlyphEmpty.add(key);
982
+ }
934
983
  return entry;
935
984
  });
936
985
  this.trackGlyphInflight(this._shapeGlyphInflight, key, render);
@@ -942,14 +991,36 @@ var AcPdfRenderer = /** @class */ (function () {
942
991
  return this.pushEntity(entity);
943
992
  };
944
993
  AcPdfRenderer.prototype.image = function (blob, style) {
994
+ var _this = this;
945
995
  var entity = new AcPdfEntity();
946
- var pending = blobToImageOp(blob, style).then(function (op) {
947
- if (!op) {
948
- return;
996
+ var key = imageOpCacheKey(blob, style);
997
+ var cached = this._imageOpCache.get(key);
998
+ if (cached !== undefined) {
999
+ if (cached) {
1000
+ applyImageOp(entity, cached);
1001
+ }
1002
+ return this.pushEntity(entity);
1003
+ }
1004
+ var inflight = this._imageOpInflight.get(key);
1005
+ if (!inflight) {
1006
+ var render = blobToImageOp(blob, style).then(function (op) {
1007
+ _this._imageOpCache.set(key, op);
1008
+ _this._imageOpInflight.delete(key);
1009
+ return op;
1010
+ });
1011
+ this._imageOpInflight.set(key, render);
1012
+ var pending_2 = render.then(function (op) {
1013
+ if (op) {
1014
+ applyImageOp(entity, op);
1015
+ }
1016
+ });
1017
+ this.trackPending(pending_2);
1018
+ return this.pushEntity(entity);
1019
+ }
1020
+ var pending = inflight.then(function (op) {
1021
+ if (op) {
1022
+ applyImageOp(entity, op);
949
1023
  }
950
- entity.addOp(op);
951
- entity.box.expandByPoint({ x: op.x, y: op.y });
952
- entity.box.expandByPoint({ x: op.x + op.width, y: op.y + op.height });
953
1024
  });
954
1025
  this.trackPending(pending);
955
1026
  return this.pushEntity(entity);
@@ -992,6 +1063,7 @@ var AcPdfRenderer = /** @class */ (function () {
992
1063
  */
993
1064
  AcPdfRenderer.prototype.beginCollectPass = function () {
994
1065
  this._deferredWork = 0;
1066
+ this._fontLoadDeferred.clear();
995
1067
  };
996
1068
  /**
997
1069
  * Returns true when the pass scheduled async glyph/font/image work that
@@ -1136,10 +1208,14 @@ var AcPdfRenderer = /** @class */ (function () {
1136
1208
  *
1137
1209
  * Matches {@link AcTrComplexLineBuilder}: TEXT via MiddleCenter MTEXT,
1138
1210
  * SHAPE via shape glyphs, with LibreDWG blank-text description fallback.
1211
+ *
1212
+ * Uses the same glyph caches as {@link mtext}/{@link shape} and prefers a
1213
+ * synchronous path when fonts/glyphs are already warm. Always scheduling
1214
+ * async work here previously forced {@link collectBlockRoots} through its
1215
+ * full pass budget on drawings with many complex-linetype segments.
1139
1216
  */
1140
1217
  AcPdfRenderer.prototype.appendComplexLineTypePlacement = function (entity, placement, lineTypeScale, dashless) {
1141
- var _this = this;
1142
- var _a;
1218
+ var _a, _b;
1143
1219
  var provider = this._glyphProvider;
1144
1220
  if (!provider) {
1145
1221
  return;
@@ -1148,28 +1224,48 @@ var AcPdfRenderer = /** @class */ (function () {
1148
1224
  var size = Math.max(((_a = placement.element.scale) !== null && _a !== void 0 ? _a : 0.1) * lineTypeScale, 1e-6);
1149
1225
  var textStyle = this.resolveComplexLineTypeTextStyle(placement);
1150
1226
  var fill = AcPdfStyleUtil.fillStyle(this._subEntityTraits, this.styleContext);
1227
+ // Embedded labels are only a fraction of a unit tall. The page minimum
1228
+ // stroke is often thicker than the letter, so SHX strokes become blobs
1229
+ // that stay blocky at any zoom. Real PDF text is resolution-independent;
1230
+ // the vector fallback uses a width proportional to the text height.
1231
+ var glyphStroke = __assign(__assign({}, dashless), { lineWidth: size * 0.06, exactWidth: true });
1151
1232
  if (isComplexTextElement(flag)) {
1152
1233
  var text = resolveLinetypeEmbeddedText(placement.element.text, this._subEntityTraits.lineType.description);
1153
1234
  if (text.trim().length === 0) {
1154
1235
  return;
1155
1236
  }
1237
+ var position = { x: placement.x, y: placement.y };
1156
1238
  var mtext = {
1157
1239
  text: text,
1158
1240
  height: size,
1159
1241
  width: 0,
1160
- position: { x: placement.x, y: placement.y, z: 0 },
1242
+ position: { x: position.x, y: position.y, z: 0 },
1161
1243
  rotation: placement.angle,
1162
1244
  attachmentPoint: AcGiMTextAttachmentPoint.MiddleCenter,
1163
1245
  widthFactor: textStyle.widthFactor || 1
1164
1246
  };
1165
1247
  var mapped = this.resolveStyle(textStyle);
1166
- // Embedded labels are only a fraction of a unit tall. The page minimum
1167
- // stroke is often thicker than the letter, so SHX strokes become blobs
1168
- // that stay blocky at any zoom. Real PDF text is resolution-independent;
1169
- // the vector fallback uses a width proportional to the text height.
1170
- var glyphStroke = __assign(__assign({}, dashless), { lineWidth: size * 0.06, exactWidth: true });
1171
- var pending_2 = this.paintComplexLineTypeText(entity, mtext, mapped, fill, glyphStroke, provider);
1172
- this.trackPending(pending_2);
1248
+ // Sync PDF text when the face is already loaded.
1249
+ if (this._textMode === 'text' && ((_b = this._fonts) === null || _b === void 0 ? void 0 : _b.has(mapped.font))) {
1250
+ var textNode = new AcPdfEntity();
1251
+ if (this.applyTextLayout(textNode, __assign(__assign({}, mtext), { position: { x: 0, y: 0, z: 0 } }), mapped.font, fill, { x: 0, y: 0 }, mapped.widthFactor)) {
1252
+ if (position.x !== 0 || position.y !== 0) {
1253
+ textNode.applyMatrix(new AcGeMatrix3d().makeTranslation(position.x, position.y, 0));
1254
+ }
1255
+ entity.addChild(textNode);
1256
+ return;
1257
+ }
1258
+ }
1259
+ else if (this._textMode === 'text' &&
1260
+ this._fonts &&
1261
+ !this._fonts.isSettled(mapped.font) &&
1262
+ !this._fontLoadDeferred.has(mapped.font)) {
1263
+ this._fontLoadDeferred.add(mapped.font);
1264
+ this.trackPending(this._fonts.load(mapped.font).then(function () {
1265
+ /* next collect pass uses sync text when load succeeded */
1266
+ }));
1267
+ }
1268
+ this.appendCachedMtextGlyphChild(entity, mtext, mapped, fill, glyphStroke, position, provider);
1173
1269
  return;
1174
1270
  }
1175
1271
  if (isComplexShapeElement(flag) ||
@@ -1184,54 +1280,100 @@ var AcPdfRenderer = /** @class */ (function () {
1184
1280
  widthFactor: textStyle.widthFactor || 1
1185
1281
  };
1186
1282
  var mapped = this.resolveStyle(textStyle);
1187
- var glyphStroke_1 = __assign(__assign({}, dashless), { lineWidth: size * 0.06, exactWidth: true });
1188
- var pending_3 = Promise.resolve(provider.renderShape(shape, mapped)).then(function (result) {
1189
- var glyphNode = new AcPdfEntity();
1190
- applyGlyphs(glyphNode, result.primitives, result.box, glyphStroke_1, fill, undefined, _this.styleContext);
1191
- glyphNode.applyMatrix(new AcGeMatrix3d().makeTranslation(placement.x, placement.y, 0));
1192
- entity.addChild(glyphNode);
1193
- });
1194
- this.trackPending(pending_3);
1283
+ this.appendCachedShapeGlyphChild(entity, shape, mapped, fill, glyphStroke, { x: placement.x, y: placement.y }, provider);
1195
1284
  }
1196
1285
  };
1197
1286
  /**
1198
- * Paints one linetype TEXT placement.
1287
+ * Attaches a complex-linetype TEXT glyph child, sync when the mtext glyph
1288
+ * cache is warm; otherwise schedules one shared render for the key.
1199
1289
  *
1200
- * Prefers real PDF text (resolution-independent). Falls back to the glyph
1201
- * provider when the font cannot be embedded (typical for SHX).
1290
+ * Cache misses only warm the shared glyph cache and mark the collect pass
1291
+ * deferred. Attaching children asynchronously is skipped: the next sync
1292
+ * collect pass clones already-filled templates (see
1293
+ * {@link collectBlockRoots}), and per-placement `then` callbacks on
1294
+ * hundreds of thousands of segments would dominate pass-0 time.
1202
1295
  */
1203
- AcPdfRenderer.prototype.paintComplexLineTypeText = function (entity, mtext, style, fill, glyphStroke, provider) {
1204
- return __awaiter(this, void 0, void 0, function () {
1205
- var position, ok, textNode, glyphColors, result, glyphNode;
1206
- return __generator(this, function (_a) {
1207
- switch (_a.label) {
1208
- case 0:
1209
- position = { x: mtext.position.x, y: mtext.position.y };
1210
- if (!(this._textMode === 'text' && this._fonts)) return [3 /*break*/, 2];
1211
- return [4 /*yield*/, this._fonts.load(style.font)];
1212
- case 1:
1213
- ok = _a.sent();
1214
- if (ok) {
1215
- textNode = new AcPdfEntity();
1216
- if (this.applyTextLayout(textNode, mtext, style.font, fill, position, style.widthFactor)) {
1217
- entity.addChild(textNode);
1218
- return [2 /*return*/];
1219
- }
1220
- }
1221
- _a.label = 2;
1222
- case 2:
1223
- glyphColors = this.resolveGlyphColorSettings();
1224
- return [4 /*yield*/, provider.renderMText(mtext, style, glyphColors)];
1225
- case 3:
1226
- result = _a.sent();
1227
- glyphNode = new AcPdfEntity();
1228
- applyGlyphs(glyphNode, result.primitives, result.box, glyphStroke, fill, result.colorGroups, this.styleContext);
1229
- glyphNode.applyMatrix(new AcGeMatrix3d().makeTranslation(position.x, position.y, 0));
1230
- entity.addChild(glyphNode);
1231
- return [2 /*return*/];
1232
- }
1233
- });
1296
+ AcPdfRenderer.prototype.appendCachedMtextGlyphChild = function (parent, mtext, mapped, fill, stroke, position, provider) {
1297
+ var _this = this;
1298
+ var glyphColors = this.resolveGlyphColorSettings();
1299
+ var key = mtextGlyphKey(mtext, mapped, glyphColors);
1300
+ var cached = this._mtextGlyphCache.get(key);
1301
+ if (cached) {
1302
+ var glyphNode = new AcPdfEntity();
1303
+ this.applyCachedGlyphs(glyphNode, cached, position, stroke, fill);
1304
+ parent.addChild(glyphNode);
1305
+ return;
1306
+ }
1307
+ if (this._mtextGlyphEmpty.has(key) ||
1308
+ this._complexMtextGlyphEmpty.has(key)) {
1309
+ return;
1310
+ }
1311
+ if (this._mtextGlyphInflight.has(key)) {
1312
+ // Another placement already deferred this key for the pass.
1313
+ return;
1314
+ }
1315
+ var render = Promise.resolve(provider.renderMText(mtext, mapped, glyphColors)).then(function (result) {
1316
+ var entry = {
1317
+ primitives: result.primitives,
1318
+ colorGroups: result.colorGroups,
1319
+ box: result.box
1320
+ };
1321
+ if (glyphEntryHasGeometry(entry)) {
1322
+ _this._mtextGlyphCache.set(key, entry);
1323
+ }
1324
+ else {
1325
+ // One empty is enough to stop re-walking huge segment counts, but
1326
+ // stay off the shared entity empty set so applyVectorMtext can still
1327
+ // retry the same key after fonts settle.
1328
+ _this._complexMtextGlyphEmpty.add(key);
1329
+ }
1330
+ return entry;
1234
1331
  });
1332
+ this.trackGlyphInflight(this._mtextGlyphInflight, key, render);
1333
+ this.trackPending(render.then(function () {
1334
+ /* cache warm-up only; next collect pass attaches sync children */
1335
+ }));
1336
+ };
1337
+ /**
1338
+ * Attaches a complex-linetype SHAPE glyph child, sync when the shape glyph
1339
+ * cache is warm. Cache misses only warm the shared cache (see
1340
+ * {@link appendCachedMtextGlyphChild}).
1341
+ */
1342
+ AcPdfRenderer.prototype.appendCachedShapeGlyphChild = function (parent, shape, mapped, fill, stroke, position, provider) {
1343
+ var _this = this;
1344
+ var key = shapeGlyphKey(shape, mapped);
1345
+ var cached = this._shapeGlyphCache.get(key);
1346
+ if (cached) {
1347
+ var glyphNode = new AcPdfEntity();
1348
+ this.applyCachedGlyphs(glyphNode, cached, position, stroke, fill);
1349
+ parent.addChild(glyphNode);
1350
+ return;
1351
+ }
1352
+ if (this._shapeGlyphEmpty.has(key) ||
1353
+ this._complexShapeGlyphEmpty.has(key)) {
1354
+ return;
1355
+ }
1356
+ if (this._shapeGlyphInflight.has(key)) {
1357
+ return;
1358
+ }
1359
+ var render = Promise.resolve(provider.renderShape(shape, mapped)).then(function (result) {
1360
+ var entry = {
1361
+ primitives: result.primitives,
1362
+ box: result.box
1363
+ };
1364
+ if (glyphEntryHasGeometry(entry)) {
1365
+ _this._shapeGlyphCache.set(key, entry);
1366
+ }
1367
+ else {
1368
+ // Same split as complex MTEXT: converge without poisoning shape().
1369
+ _this._complexShapeGlyphEmpty.add(key);
1370
+ }
1371
+ return entry;
1372
+ });
1373
+ this.trackGlyphInflight(this._shapeGlyphInflight, key, render);
1374
+ this.trackPending(render.then(function () {
1375
+ /* cache warm-up only; next collect pass attaches sync children */
1376
+ }));
1235
1377
  };
1236
1378
  AcPdfRenderer.prototype.resolveComplexLineTypeTextStyle = function (placement) {
1237
1379
  var _a;
@@ -1325,6 +1467,29 @@ function openLoopCount(points) {
1325
1467
  }
1326
1468
  return count;
1327
1469
  }
1470
+ function glyphEntryHasGeometry(entry) {
1471
+ var _a, _b;
1472
+ return (entry.primitives.triangles.length >= 6 ||
1473
+ entry.primitives.polylines.length >= 2 ||
1474
+ ((_b = (_a = entry.colorGroups) === null || _a === void 0 ? void 0 : _a.some(function (g) {
1475
+ return g.primitives.triangles.length >= 6 ||
1476
+ g.primitives.polylines.length >= 2;
1477
+ })) !== null && _b !== void 0 ? _b : false));
1478
+ }
1479
+ function imageOpCacheKey(blob, style) {
1480
+ var _a;
1481
+ var boundary = (_a = style.boundary) !== null && _a !== void 0 ? _a : [];
1482
+ return JSON.stringify([
1483
+ blob.size,
1484
+ blob.type || '',
1485
+ boundary.map(function (p) { return [p.x, p.y]; })
1486
+ ]);
1487
+ }
1488
+ function applyImageOp(entity, op) {
1489
+ entity.addOp(op);
1490
+ entity.box.expandByPoint({ x: op.x, y: op.y });
1491
+ entity.box.expandByPoint({ x: op.x + op.width, y: op.y + op.height });
1492
+ }
1328
1493
  /**
1329
1494
  * Cache key covering every mtext field that changes rendered geometry.
1330
1495
  * `position` is excluded on purpose: reuse translates shared primitives.