@mlightcad/pdf-renderer 1.7.3 → 1.7.5

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.
Files changed (55) hide show
  1. package/dist/index.js +6170 -5920
  2. package/dist/index.umd.cjs +66 -66
  3. package/lib/AcPdfExportOptions.d.ts +8 -0
  4. package/lib/AcPdfExportOptions.d.ts.map +1 -1
  5. package/lib/index.d.ts +1 -1
  6. package/lib/index.d.ts.map +1 -1
  7. package/lib/index.js.map +1 -1
  8. package/lib/pdf/AcPdfContentWriter.d.ts +7 -0
  9. package/lib/pdf/AcPdfContentWriter.d.ts.map +1 -1
  10. package/lib/pdf/AcPdfContentWriter.js +46 -21
  11. package/lib/pdf/AcPdfContentWriter.js.map +1 -1
  12. package/lib/pdf/AcPdfDocumentWriter.d.ts +5 -0
  13. package/lib/pdf/AcPdfDocumentWriter.d.ts.map +1 -1
  14. package/lib/pdf/AcPdfDocumentWriter.js +1 -0
  15. package/lib/pdf/AcPdfDocumentWriter.js.map +1 -1
  16. package/lib/pdf/AcPdfFontManager.d.ts +18 -5
  17. package/lib/pdf/AcPdfFontManager.d.ts.map +1 -1
  18. package/lib/pdf/AcPdfFontManager.js +53 -10
  19. package/lib/pdf/AcPdfFontManager.js.map +1 -1
  20. package/lib/pdf/AcPdfMarkedContent.d.ts +19 -0
  21. package/lib/pdf/AcPdfMarkedContent.d.ts.map +1 -1
  22. package/lib/pdf/AcPdfMarkedContent.js +93 -0
  23. package/lib/pdf/AcPdfMarkedContent.js.map +1 -1
  24. package/lib/renderer/AcPdfEntity.d.ts +13 -0
  25. package/lib/renderer/AcPdfEntity.d.ts.map +1 -1
  26. package/lib/renderer/AcPdfEntity.js +16 -2
  27. package/lib/renderer/AcPdfEntity.js.map +1 -1
  28. package/lib/renderer/AcPdfMTextParser.d.ts +17 -2
  29. package/lib/renderer/AcPdfMTextParser.d.ts.map +1 -1
  30. package/lib/renderer/AcPdfMTextParser.js +24 -18
  31. package/lib/renderer/AcPdfMTextParser.js.map +1 -1
  32. package/lib/renderer/AcPdfRenderer.d.ts +27 -0
  33. package/lib/renderer/AcPdfRenderer.d.ts.map +1 -1
  34. package/lib/renderer/AcPdfRenderer.js +249 -73
  35. package/lib/renderer/AcPdfRenderer.js.map +1 -1
  36. package/lib/renderer/AcPdfStyle.d.ts +1 -1
  37. package/lib/renderer/AcPdfStyle.d.ts.map +1 -1
  38. package/lib/renderer/AcPdfStyleUtil.d.ts +15 -1
  39. package/lib/renderer/AcPdfStyleUtil.d.ts.map +1 -1
  40. package/lib/renderer/AcPdfStyleUtil.js +22 -0
  41. package/lib/renderer/AcPdfStyleUtil.js.map +1 -1
  42. package/lib/renderer/AcPdfTextLayout.d.ts +10 -1
  43. package/lib/renderer/AcPdfTextLayout.d.ts.map +1 -1
  44. package/lib/renderer/AcPdfTextLayout.js +105 -50
  45. package/lib/renderer/AcPdfTextLayout.js.map +1 -1
  46. package/lib/text/AcPdfGlyphProvider.d.ts +34 -1
  47. package/lib/text/AcPdfGlyphProvider.d.ts.map +1 -1
  48. package/lib/viewport/AcPdfPaperSpaceExport.d.ts +9 -5
  49. package/lib/viewport/AcPdfPaperSpaceExport.d.ts.map +1 -1
  50. package/lib/viewport/AcPdfPaperSpaceExport.js +26 -11
  51. package/lib/viewport/AcPdfPaperSpaceExport.js.map +1 -1
  52. package/lib/viewport/AcPdfViewportContent.d.ts.map +1 -1
  53. package/lib/viewport/AcPdfViewportContent.js +3 -2
  54. package/lib/viewport/AcPdfViewportContent.js.map +1 -1
  55. package/package.json +3 -3
@@ -65,6 +65,7 @@ import { stripMtextCodes } from '../pdf/AcPdfMarkedContent';
65
65
  import { buildPointSymbol, POINT_CENTER_DOT_RATIO, resolvePointDisplayScale } from '../point/AcPdfPointSymbol';
66
66
  import { AcPdfEntity } from './AcPdfEntity';
67
67
  import { AcPdfGroup } from './AcPdfGroup';
68
+ import { rgbFromPacked } from './AcPdfStyle';
68
69
  import { AcPdfStyleUtil } from './AcPdfStyleUtil';
69
70
  import { ASCENT_RATIO, DESCENT_RATIO, layoutMText, OVERLINE_OFFSET_RATIO, STRIKE_OFFSET_RATIO, UNDERLINE_OFFSET_RATIO } from './AcPdfTextLayout';
70
71
  var DEFAULT_POINT_RADIUS = 0.5;
@@ -83,9 +84,16 @@ var AcPdfRenderer = /** @class */ (function () {
83
84
  this._insunits = 4;
84
85
  this._pageBackground = 'none';
85
86
  this._embedTextActualText = true;
87
+ this._embedEntityMarkedContent = false;
86
88
  /** `'text'` paints MTEXT/TEXT as real PDF text through `_fonts`. */
87
89
  this._textMode = 'vector';
88
90
  this._pending = [];
91
+ /**
92
+ * Counts async glyph/font/image work scheduled during the current collect
93
+ * pass. {@link collectBlockRoots} re-walks until a pass schedules none, so
94
+ * nested INSERT clones capture already-filled leaf templates.
95
+ */
96
+ this._deferredWork = 0;
89
97
  /**
90
98
  * Reuses rendered glyph primitives for identical text content + style.
91
99
  *
@@ -223,6 +231,7 @@ var AcPdfRenderer = /** @class */ (function () {
223
231
  this._glyphProvider = options.glyphProvider;
224
232
  this._fitBox = options.fitBox;
225
233
  this._embedTextActualText = options.embedTextActualText !== false;
234
+ this._embedEntityMarkedContent = options.embedEntityMarkedContent === true;
226
235
  this._textMode = options.textMode === 'text' ? 'text' : 'vector';
227
236
  if (options.fontMapping) {
228
237
  this.setFontMapping(options.fontMapping);
@@ -251,6 +260,40 @@ var AcPdfRenderer = /** @class */ (function () {
251
260
  enumerable: false,
252
261
  configurable: true
253
262
  });
263
+ /**
264
+ * Resolves ByLayer / ByBlock swatches for MTEXT inline `\C256` / `\C0`.
265
+ *
266
+ * Matches cad-viewer screen semantics: ByBlock on a non-ByBlock entity
267
+ * displays as white; ByLayer uses the layer-table colour when available.
268
+ */
269
+ AcPdfRenderer.prototype.resolveMTextColorContext = function () {
270
+ var _a = this.resolveGlyphColorSettings(), byLayerColor = _a.byLayerColor, byBlockColor = _a.byBlockColor;
271
+ return { byLayerColor: byLayerColor, byBlockColor: byBlockColor };
272
+ };
273
+ /** Colour settings passed into the vector glyph provider / text layout. */
274
+ AcPdfRenderer.prototype.resolveGlyphColorSettings = function () {
275
+ var traits = this._subEntityTraits;
276
+ var ctx = this.styleContext;
277
+ var database = this._context.database;
278
+ var layerRecord = database === null || database === void 0 ? void 0 : database.tables.layerTable.getAt(traits.layer);
279
+ var layerColor = layerRecord === null || layerRecord === void 0 ? void 0 : layerRecord.color;
280
+ var byLayerColor = layerColor != null
281
+ ? AcGiContext.fromBackgroundColor(ctx.backgroundColor).resolveSubEntityTraitsRgb(__assign(__assign({}, traits), { color: layerColor }))
282
+ : AcPdfStyleUtil.resolveRgb(traits, ctx, 'text');
283
+ var byBlockColor = traits.color.isByBlock
284
+ ? AcPdfStyleUtil.resolveRgb(traits, ctx, 'text')
285
+ : 0xffffff;
286
+ return {
287
+ byLayerColor: byLayerColor,
288
+ byBlockColor: byBlockColor,
289
+ layer: traits.layer,
290
+ entityAci: traits.color.isByACI ? traits.color.colorIndex : null,
291
+ entityRgb: traits.color.isByColor ? traits.color.RGB : null,
292
+ entityIsByLayer: traits.color.isByLayer,
293
+ entityIsByBlock: traits.color.isByBlock,
294
+ entityIsForeground: traits.color.isForeground
295
+ };
296
+ };
254
297
  AcPdfRenderer.prototype.pushEntity = function (entity) {
255
298
  var _a;
256
299
  entity.drawOrder = (_a = this._subEntityTraits.drawOrder) !== null && _a !== void 0 ? _a : 0;
@@ -485,32 +528,55 @@ var AcPdfRenderer = /** @class */ (function () {
485
528
  return this.area(area);
486
529
  };
487
530
  AcPdfRenderer.prototype.mtext = function (mtext, style, _delay) {
488
- var _this = this;
489
531
  var _a;
490
532
  var entity = new AcPdfEntity();
491
533
  var mapped = this.resolveStyle(style);
492
534
  var fill = AcPdfStyleUtil.fillStyle(this._subEntityTraits, this.styleContext);
493
535
  var stroke = AcPdfStyleUtil.strokeStyle(this._subEntityTraits, this.styleContext);
494
536
  // Capture synchronously: the source entity may move before promises resolve.
537
+ // Bake the WCS/insertion translation onto the entity *before* returning so
538
+ // AcDbRenderingCache can apply the INSERT inverse next (inverse × T). If
539
+ // translation were deferred until async glyph/font work finishes, the
540
+ // inverse would land first and compose as T × inverse — ATTRIB labels on
541
+ // scaled/rotated INSERTs then paint at a far outlier (title-block text
542
+ // appears as a distant speck while the block itself is empty of labels).
495
543
  var position = { x: mtext.position.x, y: mtext.position.y };
544
+ if (position.x !== 0 || position.y !== 0) {
545
+ entity.applyMatrix(new AcGeMatrix3d().makeTranslation(position.x, position.y, 0));
546
+ }
547
+ var localPos = { x: 0, y: 0 };
496
548
  if (this._textMode === 'text' && this._fonts) {
497
549
  var fonts = this._fonts;
498
- var fontName_1 = mapped.font;
550
+ var fontName = mapped.font;
499
551
  var contents = (_a = mtext.text) !== null && _a !== void 0 ? _a : '';
500
552
  if (contents.trim() !== '') {
501
- var pending_1 = Promise.resolve(fonts.load(fontName_1)).then(function (ok) {
502
- if (ok &&
503
- _this.applyTextLayout(entity, mtext, fontName_1, fill, position)) {
504
- return;
553
+ // Prefer a synchronous path when the font is already loaded. Nested
554
+ // INSERT templates are cloned in the same turn as worldDraw; deferring
555
+ // layout through `Promise.then` leaves empty ops in the parent cache.
556
+ if (fonts.has(fontName)) {
557
+ if (this.applyTextLayout(entity, mtext, fontName, fill, localPos, mapped.widthFactor)) {
558
+ return this.pushEntity(entity);
505
559
  }
506
- // No embeddable program or the font misses glyphs: vector glyphs.
507
- _this.applyVectorMtext(entity, mtext, mapped, fill, stroke, position);
508
- });
509
- this._pending.push(pending_1);
560
+ this.applyVectorMtext(entity, mtext, mapped, fill, stroke, localPos);
561
+ return this.pushEntity(entity);
562
+ }
563
+ // Font not ready yet. Keep collectBlockRoots looping until load
564
+ // settles — otherwise a warm glyph cache can finalize the export as
565
+ // vector-only while fonts.load() is still in flight, and searchable
566
+ // textMode:'text' output is lost. Once settled (success or miss),
567
+ // stop deferring so failed resolves do not spin forever.
568
+ if (!fonts.isSettled(fontName)) {
569
+ this.trackPending(fonts.load(fontName).then(function () {
570
+ /* next collect pass uses sync text when load succeeded */
571
+ }));
572
+ }
573
+ // Paint vector glyphs immediately (same warm-up as textMode:'vector')
574
+ // so nested INSERT clones capture filled leaf templates while we wait.
575
+ this.applyVectorMtext(entity, mtext, mapped, fill, stroke, localPos);
510
576
  return this.pushEntity(entity);
511
577
  }
512
578
  }
513
- this.applyVectorMtext(entity, mtext, mapped, fill, stroke, position);
579
+ this.applyVectorMtext(entity, mtext, mapped, fill, stroke, localPos);
514
580
  return this.pushEntity(entity);
515
581
  };
516
582
  /**
@@ -518,10 +584,10 @@ var AcPdfRenderer = /** @class */ (function () {
518
584
  * to `position`). Returns `false` when a laid-out run's mapped font does
519
585
  * not cover its characters so the caller falls back to vector glyphs.
520
586
  */
521
- AcPdfRenderer.prototype.applyTextLayout = function (entity, mtext, fontName, fill, position) {
587
+ AcPdfRenderer.prototype.applyTextLayout = function (entity, mtext, fontName, fill, position, styleWidthFactor) {
522
588
  var e_8, _a, e_9, _b, e_10, _c, e_11, _d;
523
589
  var _this = this;
524
- var _e, _f;
590
+ var _e;
525
591
  var fonts = this._fonts;
526
592
  if (!fonts) {
527
593
  return false;
@@ -539,7 +605,14 @@ var AcPdfRenderer = /** @class */ (function () {
539
605
  }
540
606
  return fonts.widthOfText(fontName, text, size);
541
607
  };
542
- var layout = layoutMText({ data: mtext, measure: measure });
608
+ var layout = layoutMText({
609
+ data: mtext,
610
+ measure: measure,
611
+ // Style group-41 width factor when the entity does not set its own —
612
+ // without this, text-mode PDF paints unflattened glyphs vs the viewer.
613
+ styleWidthFactor: styleWidthFactor,
614
+ colors: this.resolveMTextColorContext()
615
+ });
543
616
  var joined = layout.lines.map(function (line) { return line.text; }).join('\n');
544
617
  if (joined.trim() === '') {
545
618
  return false;
@@ -552,11 +625,11 @@ var AcPdfRenderer = /** @class */ (function () {
552
625
  return fonts.has(mapped) ? mapped : fontName;
553
626
  };
554
627
  try {
555
- for (var _g = __values(layout.lines), _h = _g.next(); !_h.done; _h = _g.next()) {
556
- var line = _h.value;
628
+ for (var _f = __values(layout.lines), _g = _f.next(); !_g.done; _g = _f.next()) {
629
+ var line = _g.value;
557
630
  try {
558
- for (var _j = (e_9 = void 0, __values(line.runs)), _k = _j.next(); !_k.done; _k = _j.next()) {
559
- var run = _k.value;
631
+ for (var _h = (e_9 = void 0, __values(line.runs)), _j = _h.next(); !_j.done; _j = _h.next()) {
632
+ var run = _j.value;
560
633
  if (run.text !== '' && !fonts.covers(fontFor(run.font), run.text)) {
561
634
  return false;
562
635
  }
@@ -565,7 +638,7 @@ var AcPdfRenderer = /** @class */ (function () {
565
638
  catch (e_9_1) { e_9 = { error: e_9_1 }; }
566
639
  finally {
567
640
  try {
568
- if (_k && !_k.done && (_b = _j.return)) _b.call(_j);
641
+ if (_j && !_j.done && (_b = _h.return)) _b.call(_h);
569
642
  }
570
643
  finally { if (e_9) throw e_9.error; }
571
644
  }
@@ -574,7 +647,7 @@ var AcPdfRenderer = /** @class */ (function () {
574
647
  catch (e_8_1) { e_8 = { error: e_8_1 }; }
575
648
  finally {
576
649
  try {
577
- if (_h && !_h.done && (_a = _g.return)) _a.call(_g);
650
+ if (_g && !_g.done && (_a = _f.return)) _a.call(_f);
578
651
  }
579
652
  finally { if (e_8) throw e_8.error; }
580
653
  }
@@ -601,20 +674,29 @@ var AcPdfRenderer = /** @class */ (function () {
601
674
  var upY = nz * cos;
602
675
  var flipX = nz < 0;
603
676
  var angleDeg = flipX ? rotationDeg + 180 : rotationDeg;
677
+ var styleCtx = this.styleContext;
604
678
  try {
605
- for (var _l = __values(layout.lines), _m = _l.next(); !_m.done; _m = _l.next()) {
606
- var line = _m.value;
679
+ for (var _k = __values(layout.lines), _l = _k.next(); !_l.done; _l = _k.next()) {
680
+ var line = _l.value;
607
681
  var _loop_1 = function (run) {
608
- var e_12, _q;
682
+ var e_12, _p;
609
683
  var x = line.dx + run.dx;
610
684
  var y = line.dy + run.dy;
611
685
  if (run.text !== '') {
686
+ var font = fontFor(run.font);
687
+ // Capital-A scale: CAD height → TrueType em so glyphs match the
688
+ // viewer mesh-font size (mtext-renderer fontScaleFactor).
689
+ var size = run.size * fonts.scaleFactor(font);
690
+ // Inline `\C` whites (ACI 7 / true white from a dark canvas) must
691
+ // contrast against PDF paper or they vanish on a white page.
692
+ var runFill = run.rgb
693
+ ? __assign(__assign({}, fill), { rgb: AcPdfStyleUtil.contrastRgb(run.rgb, styleCtx) }) : fill;
612
694
  entity.addOp({
613
695
  kind: 'text',
614
696
  text: run.text,
615
697
  hex: '',
616
- font: fontFor(run.font),
617
- size: run.size,
698
+ font: font,
699
+ size: size,
618
700
  x: x * cos + y * upX,
619
701
  y: x * sin + y * upY,
620
702
  angleDeg: angleDeg,
@@ -622,13 +704,16 @@ var AcPdfRenderer = /** @class */ (function () {
622
704
  tracking: run.tracking,
623
705
  obliqueDeg: run.obliqueDeg || undefined,
624
706
  flipX: flipX,
625
- style: run.rgb ? __assign(__assign({}, fill), { rgb: run.rgb }) : fill
707
+ style: runFill
626
708
  });
627
709
  }
628
710
  // Stacked-fraction rules and run decorations draw as strokes in the
629
711
  // same rotated frame, colored like the run.
712
+ var ruleRgb = run.rgb
713
+ ? AcPdfStyleUtil.contrastRgb(run.rgb, styleCtx)
714
+ : fill.rgb;
630
715
  var ruleStyle = {
631
- rgb: (_f = run.rgb) !== null && _f !== void 0 ? _f : fill.rgb,
716
+ rgb: ruleRgb,
632
717
  opacity: fill.opacity,
633
718
  lineWidth: 0.03 * run.size
634
719
  };
@@ -679,22 +764,22 @@ var AcPdfRenderer = /** @class */ (function () {
679
764
  catch (e_12_1) { e_12 = { error: e_12_1 }; }
680
765
  finally {
681
766
  try {
682
- if (corners_1_1 && !corners_1_1.done && (_q = corners_1.return)) _q.call(corners_1);
767
+ if (corners_1_1 && !corners_1_1.done && (_p = corners_1.return)) _p.call(corners_1);
683
768
  }
684
769
  finally { if (e_12) throw e_12.error; }
685
770
  }
686
771
  }
687
772
  };
688
773
  try {
689
- for (var _o = (e_11 = void 0, __values(line.runs)), _p = _o.next(); !_p.done; _p = _o.next()) {
690
- var run = _p.value;
774
+ for (var _m = (e_11 = void 0, __values(line.runs)), _o = _m.next(); !_o.done; _o = _m.next()) {
775
+ var run = _o.value;
691
776
  _loop_1(run);
692
777
  }
693
778
  }
694
779
  catch (e_11_1) { e_11 = { error: e_11_1 }; }
695
780
  finally {
696
781
  try {
697
- if (_p && !_p.done && (_d = _o.return)) _d.call(_o);
782
+ if (_o && !_o.done && (_d = _m.return)) _d.call(_m);
698
783
  }
699
784
  finally { if (e_11) throw e_11.error; }
700
785
  }
@@ -703,11 +788,17 @@ var AcPdfRenderer = /** @class */ (function () {
703
788
  catch (e_10_1) { e_10 = { error: e_10_1 }; }
704
789
  finally {
705
790
  try {
706
- if (_m && !_m.done && (_c = _l.return)) _c.call(_l);
791
+ if (_l && !_l.done && (_c = _k.return)) _c.call(_k);
707
792
  }
708
793
  finally { if (e_10) throw e_10.error; }
709
794
  }
710
- entity.applyMatrix(new AcGeMatrix3d().makeTranslation(position.x, position.y, 0));
795
+ if (position.x !== 0 || position.y !== 0) {
796
+ entity.applyMatrix(new AcGeMatrix3d().makeTranslation(position.x, position.y, 0));
797
+ }
798
+ else {
799
+ // Position was pre-baked in mtext(); rebase the local layout box.
800
+ entity.rebaseLocalBoxThroughMatrix();
801
+ }
711
802
  return true;
712
803
  };
713
804
  /**
@@ -721,7 +812,8 @@ var AcPdfRenderer = /** @class */ (function () {
721
812
  if (!provider) {
722
813
  return;
723
814
  }
724
- var key = mtextGlyphKey(mtext, mapped);
815
+ var glyphColors = this.resolveGlyphColorSettings();
816
+ var key = mtextGlyphKey(mtext, mapped, glyphColors);
725
817
  var cached = this._mtextGlyphCache.get(key);
726
818
  if (cached) {
727
819
  this.applyCachedGlyphs(entity, cached, position, stroke, fill);
@@ -733,9 +825,11 @@ var AcPdfRenderer = /** @class */ (function () {
733
825
  var contents = (_a = mtext.contents) !== null && _a !== void 0 ? _a : '';
734
826
  var inflight = this._mtextGlyphInflight.get(key);
735
827
  if (!inflight) {
736
- var render = Promise.resolve(provider.renderMText(mtext, mapped)).then(function (result) {
828
+ var render = Promise.resolve(provider.renderMText(mtext, mapped, glyphColors)).then(function (result) {
829
+ var _a, _b;
737
830
  var entry = {
738
831
  primitives: result.primitives,
832
+ colorGroups: result.colorGroups,
739
833
  box: result.box,
740
834
  actualText: _this._embedTextActualText
741
835
  ? result.actualText || stripMtextCodes(contents)
@@ -744,20 +838,24 @@ var AcPdfRenderer = /** @class */ (function () {
744
838
  // Empty results are usually "font not ready yet"; caching them would
745
839
  // permanently suppress every later instance with the same key.
746
840
  var hasGeometry = entry.primitives.triangles.length >= 6 ||
747
- entry.primitives.polylines.length >= 2;
841
+ entry.primitives.polylines.length >= 2 ||
842
+ ((_b = (_a = entry.colorGroups) === null || _a === void 0 ? void 0 : _a.some(function (g) {
843
+ return g.primitives.triangles.length >= 6 ||
844
+ g.primitives.polylines.length >= 2;
845
+ })) !== null && _b !== void 0 ? _b : false);
748
846
  if (hasGeometry) {
749
847
  _this._mtextGlyphCache.set(key, entry);
750
848
  }
751
849
  return entry;
752
850
  });
753
851
  this.trackGlyphInflight(this._mtextGlyphInflight, key, render);
754
- var pending_2 = render.then(function (entry) {
852
+ var pending_1 = render.then(function (entry) {
755
853
  _this.applyCachedGlyphs(entity, entry, position, stroke, fill);
756
854
  if (_this._embedTextActualText && entry.actualText) {
757
855
  entity.actualText = entry.actualText;
758
856
  }
759
857
  });
760
- this._pending.push(pending_2);
858
+ this.trackPending(pending_1);
761
859
  return;
762
860
  }
763
861
  // Identical glyph work already in flight: reuse its result instead of
@@ -768,7 +866,7 @@ var AcPdfRenderer = /** @class */ (function () {
768
866
  entity.actualText = entry.actualText;
769
867
  }
770
868
  });
771
- this._pending.push(pending);
869
+ this.trackPending(pending);
772
870
  };
773
871
  AcPdfRenderer.prototype.shape = function (shape, style, _delay) {
774
872
  var _this = this;
@@ -781,10 +879,15 @@ var AcPdfRenderer = /** @class */ (function () {
781
879
  var fill = AcPdfStyleUtil.fillStyle(this._subEntityTraits, this.styleContext);
782
880
  var stroke = AcPdfStyleUtil.strokeStyle(this._subEntityTraits, this.styleContext);
783
881
  var position = { x: shape.position.x, y: shape.position.y };
882
+ // Same race as mtext: bake translation before INSERT inverse can land.
883
+ if (position.x !== 0 || position.y !== 0) {
884
+ entity.applyMatrix(new AcGeMatrix3d().makeTranslation(position.x, position.y, 0));
885
+ }
886
+ var localPos = { x: 0, y: 0 };
784
887
  var key = shapeGlyphKey(shape, mapped);
785
888
  var cached = this._shapeGlyphCache.get(key);
786
889
  if (cached) {
787
- this.applyCachedGlyphs(entity, cached, position, stroke, fill);
890
+ this.applyCachedGlyphs(entity, cached, localPos, stroke, fill);
788
891
  return this.pushEntity(entity);
789
892
  }
790
893
  var inflight = this._shapeGlyphInflight.get(key);
@@ -800,9 +903,9 @@ var AcPdfRenderer = /** @class */ (function () {
800
903
  this.trackGlyphInflight(this._shapeGlyphInflight, key, render);
801
904
  }
802
905
  var pending = (inflight !== null && inflight !== void 0 ? inflight : this._shapeGlyphInflight.get(key)).then(function (entry) {
803
- _this.applyCachedGlyphs(entity, entry, position, stroke, fill);
906
+ _this.applyCachedGlyphs(entity, entry, localPos, stroke, fill);
804
907
  });
805
- this._pending.push(pending);
908
+ this.trackPending(pending);
806
909
  return this.pushEntity(entity);
807
910
  };
808
911
  AcPdfRenderer.prototype.image = function (blob, style) {
@@ -815,7 +918,7 @@ var AcPdfRenderer = /** @class */ (function () {
815
918
  entity.box.expandByPoint({ x: op.x, y: op.y });
816
919
  entity.box.expandByPoint({ x: op.x + op.width, y: op.y + op.height });
817
920
  });
818
- this._pending.push(pending);
921
+ this.trackPending(pending);
819
922
  return this.pushEntity(entity);
820
923
  };
821
924
  /**
@@ -850,6 +953,24 @@ var AcPdfRenderer = /** @class */ (function () {
850
953
  });
851
954
  });
852
955
  };
956
+ /**
957
+ * Starts a collect pass that tracks whether any async draw work is queued.
958
+ * Pair with {@link endCollectPass}.
959
+ */
960
+ AcPdfRenderer.prototype.beginCollectPass = function () {
961
+ this._deferredWork = 0;
962
+ };
963
+ /**
964
+ * Returns true when the pass scheduled async glyph/font/image work that
965
+ * will fill entity ops only after {@link awaitPending}.
966
+ */
967
+ AcPdfRenderer.prototype.endCollectPass = function () {
968
+ return this._deferredWork > 0;
969
+ };
970
+ AcPdfRenderer.prototype.trackPending = function (pending) {
971
+ this._deferredWork++;
972
+ this._pending.push(pending);
973
+ };
853
974
  /** Keeps one in-flight render per key; drops the entry once settled. */
854
975
  AcPdfRenderer.prototype.trackGlyphInflight = function (map, key, promise) {
855
976
  map.set(key, promise);
@@ -868,10 +989,14 @@ var AcPdfRenderer = /** @class */ (function () {
868
989
  * numeric bake).
869
990
  */
870
991
  AcPdfRenderer.prototype.applyCachedGlyphs = function (entity, entry, position, stroke, fill) {
871
- applyGlyphs(entity, entry.primitives, entry.box, stroke, fill);
992
+ applyGlyphs(entity, entry.primitives, entry.box, stroke, fill, entry.colorGroups, this.styleContext);
872
993
  if (position.x !== 0 || position.y !== 0) {
873
994
  entity.applyMatrix(new AcGeMatrix3d().makeTranslation(position.x, position.y, 0));
874
995
  }
996
+ else {
997
+ // Position was pre-baked (mtext/shape); box is still text-local.
998
+ entity.rebaseLocalBoxThroughMatrix();
999
+ }
875
1000
  };
876
1001
  AcPdfRenderer.prototype.exportAsync = function (roots) {
877
1002
  return __awaiter(this, void 0, void 0, function () {
@@ -930,7 +1055,7 @@ var AcPdfRenderer = /** @class */ (function () {
930
1055
  };
931
1056
  AcPdfRenderer.prototype.writeOptions = function (overrides) {
932
1057
  if (overrides === void 0) { overrides = {}; }
933
- return __assign({ insunits: this._insunits, background: this._pageBackground, title: this._title, paper: this._paper, marginMm: this._marginMm, fitBox: this._fitBox, embedTextActualText: this._embedTextActualText, fonts: this._fonts }, overrides);
1058
+ return __assign({ insunits: this._insunits, background: this._pageBackground, title: this._title, paper: this._paper, marginMm: this._marginMm, fitBox: this._fitBox, embedTextActualText: this._embedTextActualText, embedEntityMarkedContent: this._embedEntityMarkedContent, fonts: this._fonts }, overrides);
934
1059
  };
935
1060
  AcPdfRenderer.prototype.appendStrokedPoints = function (entity, pts, style) {
936
1061
  var e_13, _a, e_14, _b;
@@ -980,6 +1105,7 @@ var AcPdfRenderer = /** @class */ (function () {
980
1105
  * SHAPE via shape glyphs, with LibreDWG blank-text description fallback.
981
1106
  */
982
1107
  AcPdfRenderer.prototype.appendComplexLineTypePlacement = function (entity, placement, lineTypeScale, dashless) {
1108
+ var _this = this;
983
1109
  var _a;
984
1110
  var provider = this._glyphProvider;
985
1111
  if (!provider) {
@@ -1009,8 +1135,8 @@ var AcPdfRenderer = /** @class */ (function () {
1009
1135
  // that stay blocky at any zoom. Real PDF text is resolution-independent;
1010
1136
  // the vector fallback uses a width proportional to the text height.
1011
1137
  var glyphStroke = __assign(__assign({}, dashless), { lineWidth: size * 0.06, exactWidth: true });
1012
- var pending_3 = this.paintComplexLineTypeText(entity, mtext, mapped, fill, glyphStroke, provider);
1013
- this._pending.push(pending_3);
1138
+ var pending_2 = this.paintComplexLineTypeText(entity, mtext, mapped, fill, glyphStroke, provider);
1139
+ this.trackPending(pending_2);
1014
1140
  return;
1015
1141
  }
1016
1142
  if (isComplexShapeElement(flag) ||
@@ -1026,13 +1152,13 @@ var AcPdfRenderer = /** @class */ (function () {
1026
1152
  };
1027
1153
  var mapped = this.resolveStyle(textStyle);
1028
1154
  var glyphStroke_1 = __assign(__assign({}, dashless), { lineWidth: size * 0.06, exactWidth: true });
1029
- var pending_4 = Promise.resolve(provider.renderShape(shape, mapped)).then(function (result) {
1155
+ var pending_3 = Promise.resolve(provider.renderShape(shape, mapped)).then(function (result) {
1030
1156
  var glyphNode = new AcPdfEntity();
1031
- applyGlyphs(glyphNode, result.primitives, result.box, glyphStroke_1, fill);
1157
+ applyGlyphs(glyphNode, result.primitives, result.box, glyphStroke_1, fill, undefined, _this.styleContext);
1032
1158
  glyphNode.applyMatrix(new AcGeMatrix3d().makeTranslation(placement.x, placement.y, 0));
1033
1159
  entity.addChild(glyphNode);
1034
1160
  });
1035
- this._pending.push(pending_4);
1161
+ this.trackPending(pending_3);
1036
1162
  }
1037
1163
  };
1038
1164
  /**
@@ -1043,7 +1169,7 @@ var AcPdfRenderer = /** @class */ (function () {
1043
1169
  */
1044
1170
  AcPdfRenderer.prototype.paintComplexLineTypeText = function (entity, mtext, style, fill, glyphStroke, provider) {
1045
1171
  return __awaiter(this, void 0, void 0, function () {
1046
- var position, ok, textNode, result, glyphNode;
1172
+ var position, ok, textNode, glyphColors, result, glyphNode;
1047
1173
  return __generator(this, function (_a) {
1048
1174
  switch (_a.label) {
1049
1175
  case 0:
@@ -1054,17 +1180,19 @@ var AcPdfRenderer = /** @class */ (function () {
1054
1180
  ok = _a.sent();
1055
1181
  if (ok) {
1056
1182
  textNode = new AcPdfEntity();
1057
- if (this.applyTextLayout(textNode, mtext, style.font, fill, position)) {
1183
+ if (this.applyTextLayout(textNode, mtext, style.font, fill, position, style.widthFactor)) {
1058
1184
  entity.addChild(textNode);
1059
1185
  return [2 /*return*/];
1060
1186
  }
1061
1187
  }
1062
1188
  _a.label = 2;
1063
- case 2: return [4 /*yield*/, provider.renderMText(mtext, style)];
1189
+ case 2:
1190
+ glyphColors = this.resolveGlyphColorSettings();
1191
+ return [4 /*yield*/, provider.renderMText(mtext, style, glyphColors)];
1064
1192
  case 3:
1065
1193
  result = _a.sent();
1066
1194
  glyphNode = new AcPdfEntity();
1067
- applyGlyphs(glyphNode, result.primitives, result.box, glyphStroke, fill);
1195
+ applyGlyphs(glyphNode, result.primitives, result.box, glyphStroke, fill, result.colorGroups, this.styleContext);
1068
1196
  glyphNode.applyMatrix(new AcGeMatrix3d().makeTranslation(position.x, position.y, 0));
1069
1197
  entity.addChild(glyphNode);
1070
1198
  return [2 /*return*/];
@@ -1133,8 +1261,8 @@ function toPdfPoints(points) {
1133
1261
  * `position` is excluded on purpose: reuse translates shared primitives.
1134
1262
  * `style.lastHeight` is a scratch value that does not affect rendering.
1135
1263
  */
1136
- function mtextGlyphKey(mtext, style) {
1137
- var _a, _b, _c, _d, _e, _f, _g;
1264
+ function mtextGlyphKey(mtext, style, colors) {
1265
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
1138
1266
  return JSON.stringify([
1139
1267
  (_a = mtext.text) !== null && _a !== void 0 ? _a : '',
1140
1268
  mtext.height,
@@ -1145,7 +1273,15 @@ function mtextGlyphKey(mtext, style) {
1145
1273
  (_e = mtext.drawingDirection) !== null && _e !== void 0 ? _e : null,
1146
1274
  (_f = mtext.lineSpaceFactor) !== null && _f !== void 0 ? _f : null,
1147
1275
  (_g = mtext.widthFactor) !== null && _g !== void 0 ? _g : null,
1148
- glyphStyleKey(style)
1276
+ glyphStyleKey(style),
1277
+ (_h = colors === null || colors === void 0 ? void 0 : colors.byLayerColor) !== null && _h !== void 0 ? _h : null,
1278
+ (_j = colors === null || colors === void 0 ? void 0 : colors.byBlockColor) !== null && _j !== void 0 ? _j : null,
1279
+ (_k = colors === null || colors === void 0 ? void 0 : colors.layer) !== null && _k !== void 0 ? _k : null,
1280
+ (_l = colors === null || colors === void 0 ? void 0 : colors.entityAci) !== null && _l !== void 0 ? _l : null,
1281
+ (_m = colors === null || colors === void 0 ? void 0 : colors.entityRgb) !== null && _m !== void 0 ? _m : null,
1282
+ (_o = colors === null || colors === void 0 ? void 0 : colors.entityIsByLayer) !== null && _o !== void 0 ? _o : null,
1283
+ (_p = colors === null || colors === void 0 ? void 0 : colors.entityIsByBlock) !== null && _p !== void 0 ? _p : null,
1284
+ (_q = colors === null || colors === void 0 ? void 0 : colors.entityIsForeground) !== null && _q !== void 0 ? _q : null
1149
1285
  ]);
1150
1286
  }
1151
1287
  /** Cache key covering every shape field that changes rendered geometry. */
@@ -1180,20 +1316,60 @@ function glyphStyleKey(style) {
1180
1316
  * kind. Geometry is text-local (relative to the text's insertion point); the
1181
1317
  * caller positions it via `entity.applyMatrix`.
1182
1318
  */
1183
- function applyGlyphs(entity, primitives, box, strokeStyle, fillStyle) {
1184
- if (primitives.triangles.length >= 6) {
1185
- entity.addOp({
1186
- kind: 'triangles',
1187
- data: primitives.triangles,
1188
- style: fillStyle
1189
- });
1319
+ function applyGlyphs(entity, primitives, box, strokeStyle, fillStyle, colorGroups, styleCtx) {
1320
+ var e_15, _a;
1321
+ if (colorGroups && colorGroups.length > 0) {
1322
+ try {
1323
+ for (var colorGroups_1 = __values(colorGroups), colorGroups_1_1 = colorGroups_1.next(); !colorGroups_1_1.done; colorGroups_1_1 = colorGroups_1.next()) {
1324
+ var group = colorGroups_1_1.value;
1325
+ // Viewer mesh materials bake ACI 7 as white on a dark canvas; contrast
1326
+ // against PDF paper so those glyphs stay visible (and green `\C256`
1327
+ // groups remain unchanged).
1328
+ var packed = styleCtx
1329
+ ? AcPdfStyleUtil.contrastAgainstPaper(group.rgb, styleCtx)
1330
+ : group.rgb;
1331
+ var rgb = rgbFromPacked(packed);
1332
+ var groupFill = __assign(__assign({}, fillStyle), { rgb: rgb });
1333
+ var groupStroke = __assign(__assign({}, strokeStyle), { rgb: rgb });
1334
+ if (group.primitives.triangles.length >= 6) {
1335
+ entity.addOp({
1336
+ kind: 'triangles',
1337
+ data: group.primitives.triangles,
1338
+ style: groupFill
1339
+ });
1340
+ }
1341
+ if (group.primitives.polylines.length >= 2) {
1342
+ entity.addOp({
1343
+ kind: 'polylines',
1344
+ data: group.primitives.polylines,
1345
+ style: groupStroke
1346
+ });
1347
+ }
1348
+ }
1349
+ }
1350
+ catch (e_15_1) { e_15 = { error: e_15_1 }; }
1351
+ finally {
1352
+ try {
1353
+ if (colorGroups_1_1 && !colorGroups_1_1.done && (_a = colorGroups_1.return)) _a.call(colorGroups_1);
1354
+ }
1355
+ finally { if (e_15) throw e_15.error; }
1356
+ }
1190
1357
  }
1191
- if (primitives.polylines.length >= 2) {
1192
- entity.addOp({
1193
- kind: 'polylines',
1194
- data: primitives.polylines,
1195
- style: strokeStyle
1196
- });
1358
+ else {
1359
+ if (primitives.triangles.length >= 6) {
1360
+ entity.addOp({
1361
+ kind: 'triangles',
1362
+ data: primitives.triangles,
1363
+ style: fillStyle
1364
+ });
1365
+ }
1366
+ if (primitives.polylines.length >= 2) {
1367
+ entity.addOp({
1368
+ kind: 'polylines',
1369
+ data: primitives.polylines,
1370
+ style: strokeStyle
1371
+ });
1372
+ }
1197
1373
  }
1198
1374
  entity.box.min.set(box.min.x, box.min.y);
1199
1375
  entity.box.max.set(box.max.x, box.max.y);
@@ -1201,7 +1377,7 @@ function applyGlyphs(entity, primitives, box, strokeStyle, fillStyle) {
1201
1377
  function blobToImageOp(blob, style) {
1202
1378
  return __awaiter(this, void 0, void 0, function () {
1203
1379
  var boundary, type, format, raster, converted, minX, minY, maxX, maxY, boundary_1, boundary_1_1, pt, bytes, _a;
1204
- var e_15, _b;
1380
+ var e_16, _b;
1205
1381
  return __generator(this, function (_c) {
1206
1382
  switch (_c.label) {
1207
1383
  case 0:
@@ -1244,12 +1420,12 @@ function blobToImageOp(blob, style) {
1244
1420
  maxY = Math.max(maxY, pt.y);
1245
1421
  }
1246
1422
  }
1247
- catch (e_15_1) { e_15 = { error: e_15_1 }; }
1423
+ catch (e_16_1) { e_16 = { error: e_16_1 }; }
1248
1424
  finally {
1249
1425
  try {
1250
1426
  if (boundary_1_1 && !boundary_1_1.done && (_b = boundary_1.return)) _b.call(boundary_1);
1251
1427
  }
1252
- finally { if (e_15) throw e_15.error; }
1428
+ finally { if (e_16) throw e_16.error; }
1253
1429
  }
1254
1430
  _a = Uint8Array.bind;
1255
1431
  return [4 /*yield*/, raster.arrayBuffer()];