@pixldocs/canvas-renderer 0.5.39 → 0.5.40

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/index.d.ts CHANGED
@@ -221,7 +221,7 @@ export declare function normalizeFontFamily(fontStack: string): string;
221
221
  * Package version banner. Bump alongside package.json so we can confirm
222
222
  * (via browser:log) that the deployed bundle matches the expected build.
223
223
  */
224
- export declare const PACKAGE_VERSION = "0.5.39";
224
+ export declare const PACKAGE_VERSION = "0.5.40";
225
225
 
226
226
  export declare interface PageSettings {
227
227
  backgroundColor?: string;
package/dist/index.js CHANGED
@@ -12053,7 +12053,7 @@ function PixldocsPreview(props) {
12053
12053
  !canvasSettled && /* @__PURE__ */ jsx("div", { style: { position: "absolute", inset: 0, display: "flex", alignItems: "center", justifyContent: "center", minHeight: 200 }, children: /* @__PURE__ */ jsx("div", { style: { color: "#888", fontSize: 14 }, children: "Loading preview..." }) })
12054
12054
  ] });
12055
12055
  }
12056
- const PACKAGE_VERSION = "0.5.39";
12056
+ const PACKAGE_VERSION = "0.5.40";
12057
12057
  let __underlineFixInstalled = false;
12058
12058
  function installUnderlineFix(fab) {
12059
12059
  var _a;
@@ -12061,6 +12061,24 @@ function installUnderlineFix(fab) {
12061
12061
  const TextProto = (_a = fab.Text) == null ? void 0 : _a.prototype;
12062
12062
  if (!TextProto || typeof TextProto._renderTextDecoration !== "function") return;
12063
12063
  const original = TextProto._renderTextDecoration;
12064
+ const measureLineTextWidth = (obj, ctx, lineIndex) => {
12065
+ var _a2, _b, _c, _d, _e, _f;
12066
+ const rawLine = (_a2 = obj._textLines) == null ? void 0 : _a2[lineIndex];
12067
+ const lineText = Array.isArray(rawLine) ? rawLine.join("") : String(rawLine ?? "");
12068
+ if (!lineText) return 0;
12069
+ const fontSize = Number(((_b = obj.getValueOfPropertyAt) == null ? void 0 : _b.call(obj, lineIndex, 0, "fontSize")) ?? obj.fontSize ?? 0);
12070
+ const fontStyle = String(((_c = obj.getValueOfPropertyAt) == null ? void 0 : _c.call(obj, lineIndex, 0, "fontStyle")) ?? obj.fontStyle ?? "normal");
12071
+ const fontWeight = String(((_d = obj.getValueOfPropertyAt) == null ? void 0 : _d.call(obj, lineIndex, 0, "fontWeight")) ?? obj.fontWeight ?? "400");
12072
+ const fontFamily = String(((_e = obj.getValueOfPropertyAt) == null ? void 0 : _e.call(obj, lineIndex, 0, "fontFamily")) ?? obj.fontFamily ?? "sans-serif");
12073
+ const charSpacing = Number(((_f = obj.getValueOfPropertyAt) == null ? void 0 : _f.call(obj, lineIndex, 0, "charSpacing")) ?? obj.charSpacing ?? 0);
12074
+ ctx.save();
12075
+ ctx.font = `${fontStyle} normal ${fontWeight} ${fontSize}px ${fontFamily}`;
12076
+ const measured = ctx.measureText(lineText).width;
12077
+ ctx.restore();
12078
+ const graphemeCount = Array.from(lineText).length;
12079
+ const spacingWidth = graphemeCount > 1 ? charSpacing / 1e3 * fontSize * (graphemeCount - 1) : 0;
12080
+ return Math.max(0, measured + spacingWidth);
12081
+ };
12064
12082
  TextProto._renderTextDecoration = function patchedRenderTextDecoration(ctx, type) {
12065
12083
  try {
12066
12084
  const hasOwn = !!this[type];
@@ -12091,20 +12109,16 @@ function installUnderlineFix(fab) {
12091
12109
  topOffset += heightOfLine;
12092
12110
  continue;
12093
12111
  }
12094
- if (Array.isArray(this.__lineWidths)) {
12095
- this.__lineWidths[i] = void 0;
12096
- }
12097
- let lineWidth = 0;
12098
- try {
12099
- lineWidth = this.getLineWidth(i);
12100
- } catch {
12101
- lineWidth = 0;
12102
- }
12112
+ const lineWidth = measureLineTextWidth(this, ctx, i);
12103
12113
  if (!lineWidth) {
12104
12114
  topOffset += heightOfLine;
12105
12115
  continue;
12106
12116
  }
12107
- const lineLeftOffset = this._getLineLeftOffset(i);
12117
+ const availableWidth = Number(this.width ?? lineWidth);
12118
+ let lineLeftOffset = 0;
12119
+ const align = String(this.textAlign ?? "left");
12120
+ if (align === "center") lineLeftOffset = (availableWidth - lineWidth) / 2;
12121
+ else if (align === "right" || align === "end") lineLeftOffset = availableWidth - lineWidth;
12108
12122
  let drawStart = leftOffset + lineLeftOffset;
12109
12123
  if (this.direction === "rtl") {
12110
12124
  drawStart = this.width - drawStart - lineWidth;