@pixldocs/canvas-renderer 0.5.146 → 0.5.148

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.
@@ -3,7 +3,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const jspdf = require("jspdf");
4
4
  const svg2pdf_js = require("svg2pdf.js");
5
5
  const fabric = require("fabric");
6
- const index = require("./index-DoPXmxDJ.cjs");
6
+ const index = require("./index-Bt1m9Esp.cjs");
7
7
  const pdfFonts = require("./pdfFonts-BTEVnYX8.cjs");
8
8
  function _interopNamespaceDefault(e) {
9
9
  const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
@@ -1199,6 +1199,44 @@ async function bakeTextAnchorPositionsFromLiveSvg(svg) {
1199
1199
  tempContainer.appendChild(clone);
1200
1200
  document.body.appendChild(tempContainer);
1201
1201
  let baked = 0;
1202
+ const _measureCanvas = typeof document !== "undefined" ? document.createElement("canvas") : null;
1203
+ const _mctx = (_measureCanvas == null ? void 0 : _measureCanvas.getContext("2d")) || null;
1204
+ const _resolveFontSize = (node) => {
1205
+ let cur = node;
1206
+ while (cur) {
1207
+ const fs = cur.getAttribute("font-size");
1208
+ if (fs) {
1209
+ const n = parseFloat(fs);
1210
+ if (Number.isFinite(n) && n > 0) return n;
1211
+ }
1212
+ cur = cur.parentElement;
1213
+ }
1214
+ return 16;
1215
+ };
1216
+ const _resolveFontAttr = (node, attr, fallback) => {
1217
+ let cur = node;
1218
+ while (cur) {
1219
+ const v = cur.getAttribute(`data-source-${attr}`) || cur.getAttribute(attr);
1220
+ if (v) return v;
1221
+ cur = cur.parentElement;
1222
+ }
1223
+ return fallback;
1224
+ };
1225
+ const _measureWidthCanvas = (srcNode, content) => {
1226
+ if (!_mctx || !content) return null;
1227
+ const family = _resolveFontAttr(srcNode, "font-family", "").replace(/['"]/g, "").trim();
1228
+ const weight = _resolveFontAttr(srcNode, "font-weight", "400");
1229
+ const style = _resolveFontAttr(srcNode, "font-style", "normal");
1230
+ const size = _resolveFontSize(srcNode);
1231
+ if (!family) return null;
1232
+ try {
1233
+ _mctx.font = `${style} ${weight} ${size}px "${family}"`;
1234
+ const w = _mctx.measureText(content).width;
1235
+ return Number.isFinite(w) && w > 0 ? w : null;
1236
+ } catch {
1237
+ return null;
1238
+ }
1239
+ };
1202
1240
  try {
1203
1241
  const srcTexts = Array.from(svg.querySelectorAll("text"));
1204
1242
  const liveTexts = Array.from(clone.querySelectorAll("text"));
@@ -1218,6 +1256,32 @@ async function bakeTextAnchorPositionsFromLiveSvg(svg) {
1218
1256
  const bakeNode = (srcNode, liveNode, anchorOverride) => {
1219
1257
  const anchor = (anchorOverride || _resolveAnchor(srcNode)).trim().toLowerCase();
1220
1258
  if (anchor !== "middle" && anchor !== "end") return;
1259
+ try {
1260
+ const content = srcNode.textContent || "";
1261
+ if (content.length > 0) {
1262
+ const width = _measureWidthCanvas(srcNode, content);
1263
+ let refXAttr = null;
1264
+ let cur = srcNode;
1265
+ while (cur) {
1266
+ const v = cur.getAttribute("x");
1267
+ if (v) {
1268
+ refXAttr = v;
1269
+ break;
1270
+ }
1271
+ cur = cur.parentElement;
1272
+ }
1273
+ const refX = refXAttr !== null ? parseFloat(refXAttr.split(/[\s,]+/)[0]) : NaN;
1274
+ if (width !== null && Number.isFinite(refX)) {
1275
+ const newX = anchor === "middle" ? refX - width / 2 : refX - width;
1276
+ srcNode.setAttribute("x", String(newX));
1277
+ srcNode.setAttribute("text-anchor", "start");
1278
+ stripTextAnchorStyle(srcNode);
1279
+ baked++;
1280
+ return;
1281
+ }
1282
+ }
1283
+ } catch {
1284
+ }
1221
1285
  try {
1222
1286
  const n = typeof liveNode.getNumberOfChars === "function" ? liveNode.getNumberOfChars() : 0;
1223
1287
  if (!n) return;
@@ -2405,7 +2469,7 @@ async function fetchSvgAsElement(imageUrl, colorMap) {
2405
2469
  async function getRecoloredSvgDataUrl(imageUrl, colorMap) {
2406
2470
  if (!colorMap || Object.keys(colorMap).length === 0) return null;
2407
2471
  try {
2408
- const { getNormalizedSvgUrl } = await Promise.resolve().then(() => require("./index-DoPXmxDJ.cjs")).then((n) => n.canvasImageLoader);
2472
+ const { getNormalizedSvgUrl } = await Promise.resolve().then(() => require("./index-Bt1m9Esp.cjs")).then((n) => n.canvasImageLoader);
2409
2473
  return await getNormalizedSvgUrl(imageUrl, colorMap);
2410
2474
  } catch {
2411
2475
  return null;
@@ -3186,7 +3250,7 @@ async function fetchImageAsBase64(imageUrl, opts = {}) {
3186
3250
  }
3187
3251
  let fetchUrl = imageUrl;
3188
3252
  if (imageUrl.startsWith("http://") || imageUrl.startsWith("https://")) {
3189
- const { isPrivateUrl } = await Promise.resolve().then(() => require("./index-DoPXmxDJ.cjs")).then((n) => n.canvasImageLoader);
3253
+ const { isPrivateUrl } = await Promise.resolve().then(() => require("./index-Bt1m9Esp.cjs")).then((n) => n.canvasImageLoader);
3190
3254
  if (isPrivateUrl(imageUrl)) return null;
3191
3255
  const proxyUrl = new URL(`${index.API_URL}/image-proxy`);
3192
3256
  proxyUrl.searchParams.set("url", imageUrl);
@@ -5168,4 +5232,4 @@ exports.exportMultiPagePdf = exportMultiPagePdf;
5168
5232
  exports.logTextMeasurementDiagnostic = logTextMeasurementDiagnostic;
5169
5233
  exports.preparePagesForExport = preparePagesForExport;
5170
5234
  exports.rewriteSvgFontsForJsPDFWithSourceMeta = rewriteSvgFontsForJsPDFWithSourceMeta;
5171
- //# sourceMappingURL=vectorPdfExport-SMvSP0el.cjs.map
5235
+ //# sourceMappingURL=vectorPdfExport-JGntLQJQ.cjs.map