@clypra/engine 1.2.0 → 1.2.1
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.cjs +22 -31
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +22 -31
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -462,7 +462,7 @@ interface CompositionPreset {
|
|
|
462
462
|
description?: string;
|
|
463
463
|
}
|
|
464
464
|
declare const COMPOSITION_PRESETS: CompositionPreset[];
|
|
465
|
-
/** Soft-wrap paragraphs to fit safe width */
|
|
465
|
+
/** Soft-wrap paragraphs to fit safe width character-by-character */
|
|
466
466
|
declare function wrapTextToWidth(ctx: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D, text: string, maxWidth: number, letterSpacing: number): string[];
|
|
467
467
|
declare function measureTextFits(ctx: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D, cfg: TextEffectConfig, fontSize: number, lines: string[]): boolean;
|
|
468
468
|
declare function computeAutoFitFontSize(ctx: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D, cfg: TextEffectConfig, wrappedLines: string[]): number;
|
package/dist/index.d.ts
CHANGED
|
@@ -462,7 +462,7 @@ interface CompositionPreset {
|
|
|
462
462
|
description?: string;
|
|
463
463
|
}
|
|
464
464
|
declare const COMPOSITION_PRESETS: CompositionPreset[];
|
|
465
|
-
/** Soft-wrap paragraphs to fit safe width */
|
|
465
|
+
/** Soft-wrap paragraphs to fit safe width character-by-character */
|
|
466
466
|
declare function wrapTextToWidth(ctx: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D, text: string, maxWidth: number, letterSpacing: number): string[];
|
|
467
467
|
declare function measureTextFits(ctx: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D, cfg: TextEffectConfig, fontSize: number, lines: string[]): boolean;
|
|
468
468
|
declare function computeAutoFitFontSize(ctx: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D, cfg: TextEffectConfig, wrappedLines: string[]): number;
|
package/dist/index.js
CHANGED
|
@@ -73,35 +73,26 @@ function wrapTextToWidth(ctx, text, maxWidth, letterSpacing) {
|
|
|
73
73
|
const paragraphs = text.split("\n");
|
|
74
74
|
const lines = [];
|
|
75
75
|
for (const para of paragraphs) {
|
|
76
|
-
if (
|
|
76
|
+
if (para === "") {
|
|
77
77
|
lines.push("");
|
|
78
78
|
continue;
|
|
79
79
|
}
|
|
80
|
-
const words = para.split(/\s+/).filter(Boolean);
|
|
81
80
|
let current = "";
|
|
82
|
-
for (
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
|
|
81
|
+
for (let i = 0; i < para.length; i++) {
|
|
82
|
+
const char = para[i];
|
|
83
|
+
const tryLine = current + char;
|
|
84
|
+
if (measureLine(ctx, tryLine, letterSpacing) <= maxWidth) {
|
|
85
|
+
current = tryLine;
|
|
86
86
|
} else {
|
|
87
|
-
if (current)
|
|
88
|
-
|
|
89
|
-
let chunk = "";
|
|
90
|
-
for (const ch of word) {
|
|
91
|
-
const tryChunk = chunk + ch;
|
|
92
|
-
if (measureLine(ctx, tryChunk, letterSpacing) <= maxWidth) chunk = tryChunk;
|
|
93
|
-
else {
|
|
94
|
-
if (chunk) lines.push(chunk);
|
|
95
|
-
chunk = ch;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
current = chunk;
|
|
99
|
-
} else {
|
|
100
|
-
current = word;
|
|
87
|
+
if (current) {
|
|
88
|
+
lines.push(current);
|
|
101
89
|
}
|
|
90
|
+
current = char;
|
|
102
91
|
}
|
|
103
92
|
}
|
|
104
|
-
if (current)
|
|
93
|
+
if (current) {
|
|
94
|
+
lines.push(current);
|
|
95
|
+
}
|
|
105
96
|
}
|
|
106
97
|
return lines.length > 0 ? lines : [""];
|
|
107
98
|
}
|
|
@@ -127,17 +118,17 @@ function layoutWithFontSize(ctx, cfg, fontSize, lines) {
|
|
|
127
118
|
} else {
|
|
128
119
|
startX = safe.x + safe.width / 2;
|
|
129
120
|
}
|
|
130
|
-
let startY = safe.y + (safe.height - textBlockHeight) / 2 + fontSize * 0.
|
|
121
|
+
let startY = safe.y + (safe.height - textBlockHeight) / 2 + fontSize * 0.85;
|
|
131
122
|
if (cfg.textPosY === "top") {
|
|
132
|
-
startY = safe.y + fontSize * 0.
|
|
123
|
+
startY = safe.y + fontSize * 0.85;
|
|
133
124
|
} else if (cfg.textPosY === "bottom") {
|
|
134
|
-
startY = safe.y + safe.height - textBlockHeight + fontSize * 0.
|
|
125
|
+
startY = safe.y + safe.height - textBlockHeight + fontSize * 0.85;
|
|
135
126
|
}
|
|
136
127
|
let xMin = startX;
|
|
137
128
|
if (align === "center") xMin = startX - maxLineWidth / 2;
|
|
138
129
|
else if (align === "right") xMin = startX - maxLineWidth;
|
|
139
130
|
const yMin = startY - fontSize * 0.85;
|
|
140
|
-
const yMax = startY + (lines.length - 1) * fontSize * lineHeight + fontSize * 0.
|
|
131
|
+
const yMax = startY + (lines.length - 1) * fontSize * lineHeight + fontSize * 0.15;
|
|
141
132
|
return {
|
|
142
133
|
lines,
|
|
143
134
|
fontSize,
|
|
@@ -576,11 +567,11 @@ var InkBrushEngine = class {
|
|
|
576
567
|
if (letterSpacing !== 0) {
|
|
577
568
|
ctx.letterSpacing = `${letterSpacing}px`;
|
|
578
569
|
}
|
|
579
|
-
let startY = (height - textBlockHeight) / 2 + fontSize * 0.
|
|
570
|
+
let startY = (height - textBlockHeight) / 2 + fontSize * 0.85;
|
|
580
571
|
if (textPosY === "top") {
|
|
581
|
-
startY = 40 + fontSize * 0.
|
|
572
|
+
startY = 40 + fontSize * 0.85;
|
|
582
573
|
} else if (textPosY === "bottom") {
|
|
583
|
-
startY = height - 40 - textBlockHeight + fontSize * 0.
|
|
574
|
+
startY = height - 40 - textBlockHeight + fontSize * 0.85;
|
|
584
575
|
}
|
|
585
576
|
ctx.save();
|
|
586
577
|
if (skewX !== 0) {
|
|
@@ -728,11 +719,11 @@ var InkBrushEngine = class {
|
|
|
728
719
|
if (letterSpacing !== 0) {
|
|
729
720
|
ctx.letterSpacing = `${letterSpacing}px`;
|
|
730
721
|
}
|
|
731
|
-
let startY = (height - textBlockHeight) / 2 + fontSize * 0.
|
|
722
|
+
let startY = (height - textBlockHeight) / 2 + fontSize * 0.85;
|
|
732
723
|
if (textPosY === "top") {
|
|
733
|
-
startY = 40 + fontSize * 0.
|
|
724
|
+
startY = 40 + fontSize * 0.85;
|
|
734
725
|
} else if (textPosY === "bottom") {
|
|
735
|
-
startY = height - 40 - textBlockHeight + fontSize * 0.
|
|
726
|
+
startY = height - 40 - textBlockHeight + fontSize * 0.85;
|
|
736
727
|
}
|
|
737
728
|
ctx.save();
|
|
738
729
|
if (skewX !== 0) {
|