modern-text 0.2.31 → 0.2.33
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 +44 -20
- package/dist/index.d.cts +5 -3
- package/dist/index.d.mts +5 -3
- package/dist/index.d.ts +5 -3
- package/dist/index.js +4 -4
- package/dist/index.mjs +44 -20
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -368,6 +368,13 @@ class Character {
|
|
|
368
368
|
} else {
|
|
369
369
|
min ?? (min = modernPath2d.Vector2.MAX);
|
|
370
370
|
max ?? (max = modernPath2d.Vector2.MIN);
|
|
371
|
+
const { left, top } = this.boundingBox;
|
|
372
|
+
const right = left + this.glyphWidth;
|
|
373
|
+
const bottom = top + this.glyphHeight;
|
|
374
|
+
min.x = Math.min(min.x, left);
|
|
375
|
+
min.y = Math.min(min.y, top);
|
|
376
|
+
max.x = Math.max(max.x, right);
|
|
377
|
+
max.y = Math.max(max.y, bottom);
|
|
371
378
|
return { min, max };
|
|
372
379
|
}
|
|
373
380
|
}
|
|
@@ -732,18 +739,18 @@ class Parser {
|
|
|
732
739
|
}
|
|
733
740
|
}
|
|
734
741
|
|
|
735
|
-
function
|
|
742
|
+
function definePlugin(options) {
|
|
736
743
|
return options;
|
|
737
744
|
}
|
|
738
745
|
|
|
739
746
|
const tempV1 = new modernPath2d.Vector2();
|
|
740
747
|
const tempM1 = new modernPath2d.Matrix3();
|
|
741
748
|
const tempM2 = new modernPath2d.Matrix3();
|
|
742
|
-
function effect(
|
|
743
|
-
return
|
|
749
|
+
function effect() {
|
|
750
|
+
return definePlugin({
|
|
744
751
|
name: "effect",
|
|
745
752
|
getBoundingBox: (text) => {
|
|
746
|
-
const { characters, fontSize } = text;
|
|
753
|
+
const { characters, fontSize, effects } = text;
|
|
747
754
|
const boxes = [];
|
|
748
755
|
characters.forEach((character) => {
|
|
749
756
|
effects?.forEach((style) => {
|
|
@@ -770,7 +777,7 @@ function effect(effects) {
|
|
|
770
777
|
return boxes.length ? modernPath2d.BoundingBox.from(...boxes) : void 0;
|
|
771
778
|
},
|
|
772
779
|
render: (ctx, text) => {
|
|
773
|
-
const { characters, renderBoundingBox } = text;
|
|
780
|
+
const { characters, renderBoundingBox, effects } = text;
|
|
774
781
|
if (effects) {
|
|
775
782
|
effects.forEach((style) => {
|
|
776
783
|
uploadColor(style, renderBoundingBox, ctx);
|
|
@@ -850,7 +857,7 @@ function highlight(options = {}) {
|
|
|
850
857
|
const referPaths = modernPath2d.parseSvg(config.referImage);
|
|
851
858
|
const paths = [];
|
|
852
859
|
const clipRects = [];
|
|
853
|
-
return
|
|
860
|
+
return definePlugin({
|
|
854
861
|
name: "highlight",
|
|
855
862
|
paths,
|
|
856
863
|
update: (text) => {
|
|
@@ -958,10 +965,12 @@ function parseScale(size, fontSize, total) {
|
|
|
958
965
|
}
|
|
959
966
|
function listStyle() {
|
|
960
967
|
const paths = [];
|
|
961
|
-
return
|
|
968
|
+
return definePlugin({
|
|
962
969
|
name: "listStyle",
|
|
970
|
+
paths,
|
|
963
971
|
update: (text) => {
|
|
964
|
-
|
|
972
|
+
paths.length = 0;
|
|
973
|
+
const { paragraphs, computedStyle: style, fontSize, isVertical } = text;
|
|
965
974
|
let listStyleSize = style.listStyleSize;
|
|
966
975
|
let image;
|
|
967
976
|
if (!isNone(style.listStyleImage)) {
|
|
@@ -980,24 +989,34 @@ function listStyle() {
|
|
|
980
989
|
if (!image) {
|
|
981
990
|
return;
|
|
982
991
|
}
|
|
983
|
-
const
|
|
992
|
+
const padding = fontSize * 0.45;
|
|
984
993
|
const imagePaths = modernPath2d.parseSvg(image);
|
|
985
994
|
const imageBox = modernPath2d.getPathsBoundingBox(imagePaths);
|
|
986
995
|
paragraphs.forEach((paragraph) => {
|
|
987
996
|
const box = paragraph.fragments[0]?.characters[0]?.getGlyphBoundingBox();
|
|
988
997
|
if (box) {
|
|
989
|
-
const scale = parseScale(listStyleSize, style.fontSize, box.height);
|
|
990
|
-
const reScale = box.height / imageBox.height * scale;
|
|
991
998
|
const m = new modernPath2d.Matrix3();
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
999
|
+
if (isVertical) {
|
|
1000
|
+
const scale = parseScale(listStyleSize, style.fontSize, box.width);
|
|
1001
|
+
const reScale = box.width / imageBox.height * scale;
|
|
1002
|
+
m.translate(-imageBox.left, -imageBox.top);
|
|
1003
|
+
m.rotate(Math.PI / 2);
|
|
1004
|
+
m.scale(reScale, reScale);
|
|
1005
|
+
m.translate(box.width / 2 - imageBox.height * reScale / 2, 0);
|
|
1006
|
+
m.translate(box.left, box.top - padding);
|
|
1007
|
+
} else {
|
|
1008
|
+
const scale = parseScale(listStyleSize, style.fontSize, box.height);
|
|
1009
|
+
const reScale = box.height / imageBox.height * scale;
|
|
1010
|
+
m.translate(-imageBox.left, -imageBox.top);
|
|
1011
|
+
m.translate(-imageBox.width, 0);
|
|
1012
|
+
m.scale(reScale, reScale);
|
|
1013
|
+
m.translate(0, box.height / 2 - imageBox.height * reScale / 2);
|
|
1014
|
+
m.translate(box.left - padding, box.top);
|
|
1015
|
+
}
|
|
996
1016
|
paths.push(...imagePaths.map((p) => p.clone().matrix(m)));
|
|
997
1017
|
}
|
|
998
1018
|
});
|
|
999
|
-
}
|
|
1000
|
-
paths
|
|
1019
|
+
}
|
|
1001
1020
|
});
|
|
1002
1021
|
}
|
|
1003
1022
|
|
|
@@ -1051,6 +1070,7 @@ class Text {
|
|
|
1051
1070
|
constructor(options = {}) {
|
|
1052
1071
|
__publicField(this, "content");
|
|
1053
1072
|
__publicField(this, "style");
|
|
1073
|
+
__publicField(this, "effects");
|
|
1054
1074
|
__publicField(this, "measureDom");
|
|
1055
1075
|
__publicField(this, "needsUpdate", true);
|
|
1056
1076
|
__publicField(this, "computedStyle", { ...defaultTextStyles });
|
|
@@ -1060,15 +1080,19 @@ class Text {
|
|
|
1060
1080
|
__publicField(this, "parser", new Parser(this));
|
|
1061
1081
|
__publicField(this, "measurer", new Measurer(this));
|
|
1062
1082
|
__publicField(this, "plugins", /* @__PURE__ */ new Map());
|
|
1063
|
-
const { content = "", style = {}, measureDom } = options;
|
|
1083
|
+
const { content = "", style = {}, measureDom, effects } = options;
|
|
1064
1084
|
this.content = content;
|
|
1065
1085
|
this.style = style;
|
|
1066
1086
|
this.measureDom = measureDom;
|
|
1067
|
-
this.
|
|
1087
|
+
this.effects = effects;
|
|
1088
|
+
this.use(effect()).use(highlight(options.highlight)).use(listStyle());
|
|
1068
1089
|
}
|
|
1069
1090
|
get fontSize() {
|
|
1070
1091
|
return this.computedStyle.fontSize;
|
|
1071
1092
|
}
|
|
1093
|
+
get isVertical() {
|
|
1094
|
+
return this.computedStyle.writingMode.includes("vertical");
|
|
1095
|
+
}
|
|
1072
1096
|
get characters() {
|
|
1073
1097
|
return this.paragraphs.flatMap((p) => p.fragments.flatMap((f) => f.characters));
|
|
1074
1098
|
}
|
|
@@ -1149,6 +1173,7 @@ exports.Paragraph = Paragraph;
|
|
|
1149
1173
|
exports.Parser = Parser;
|
|
1150
1174
|
exports.Text = Text;
|
|
1151
1175
|
exports.defaultTextStyles = defaultTextStyles;
|
|
1176
|
+
exports.definePlugin = definePlugin;
|
|
1152
1177
|
exports.drawPath = drawPath;
|
|
1153
1178
|
exports.effect = effect;
|
|
1154
1179
|
exports.fillBackground = fillBackground;
|
|
@@ -1158,7 +1183,6 @@ exports.highlight = highlight;
|
|
|
1158
1183
|
exports.isNone = isNone;
|
|
1159
1184
|
exports.listStyle = listStyle;
|
|
1160
1185
|
exports.parseColor = parseColor;
|
|
1161
|
-
exports.plugin = plugin;
|
|
1162
1186
|
exports.setupView = setupView;
|
|
1163
1187
|
exports.uploadColor = uploadColor;
|
|
1164
1188
|
exports.uploadColors = uploadColors;
|
package/dist/index.d.cts
CHANGED
|
@@ -223,6 +223,7 @@ declare const defaultTextStyles: TextStyle;
|
|
|
223
223
|
declare class Text {
|
|
224
224
|
content: TextContent;
|
|
225
225
|
style: Partial<TextStyle>;
|
|
226
|
+
effects?: TextEffect[];
|
|
226
227
|
measureDom?: HTMLElement;
|
|
227
228
|
needsUpdate: boolean;
|
|
228
229
|
computedStyle: {
|
|
@@ -264,6 +265,7 @@ declare class Text {
|
|
|
264
265
|
measurer: Measurer;
|
|
265
266
|
plugins: Map<string, Plugin>;
|
|
266
267
|
get fontSize(): number;
|
|
268
|
+
get isVertical(): boolean;
|
|
267
269
|
get characters(): Character[];
|
|
268
270
|
constructor(options?: TextOptions);
|
|
269
271
|
use(plugin: Plugin): this;
|
|
@@ -282,7 +284,7 @@ interface Plugin {
|
|
|
282
284
|
renderOrder?: number;
|
|
283
285
|
render?: (ctx: CanvasRenderingContext2D, text: Text) => void;
|
|
284
286
|
}
|
|
285
|
-
declare function
|
|
287
|
+
declare function definePlugin(options: Plugin): Plugin;
|
|
286
288
|
|
|
287
289
|
type TextEffect = Partial<TextDrawStyle & {
|
|
288
290
|
offsetX: number;
|
|
@@ -290,7 +292,7 @@ type TextEffect = Partial<TextDrawStyle & {
|
|
|
290
292
|
skewX: number;
|
|
291
293
|
skewY: number;
|
|
292
294
|
}>;
|
|
293
|
-
declare function effect(
|
|
295
|
+
declare function effect(): Plugin;
|
|
294
296
|
declare function getTransform2D(text: Text, style: TextEffect): Matrix3;
|
|
295
297
|
|
|
296
298
|
interface HighlightOptions {
|
|
@@ -317,4 +319,4 @@ declare function uploadColors(ctx: CanvasRenderingContext2D, text: Text): void;
|
|
|
317
319
|
declare function isNone(val: string | undefined): boolean;
|
|
318
320
|
declare function filterEmpty(val: Record<string, any> | undefined): Record<string, any> | undefined;
|
|
319
321
|
|
|
320
|
-
export { Character, type DrawShapePathsOptions, type FontKerning, type FontStyle, type FontWeight, Fragment, type FragmentContent, type HighlightImage, type HighlightOptions, type HighlightOverflow, type HighlightSize, type HighlightStrokeWidth, type LinearGradient, type ListStyleImage, type ListStylePosition, type ListStyleSize, type ListStyleType, type MeasuredCharacter, type MeasuredFragment, type MeasuredParagraph, type MeasuredResult, Measurer, Paragraph, type ParagraphContent, Parser, type Plugin, type Sizeable, Text, type TextAlign, type TextContent, type TextDecoration, type TextDrawStyle, type TextEffect, type TextHighlightStyle, type TextLayoutStyle, type TextListStyle, type TextOptions, type TextOrientation, type TextRenderOptions, type TextStyle, type TextTransform, type TextWrap, type VerticalAlign, type WritingMode, defaultTextStyles, drawPath, effect, fillBackground, filterEmpty, getTransform2D, highlight, isNone, listStyle, parseColor,
|
|
322
|
+
export { Character, type DrawShapePathsOptions, type FontKerning, type FontStyle, type FontWeight, Fragment, type FragmentContent, type HighlightImage, type HighlightOptions, type HighlightOverflow, type HighlightSize, type HighlightStrokeWidth, type LinearGradient, type ListStyleImage, type ListStylePosition, type ListStyleSize, type ListStyleType, type MeasuredCharacter, type MeasuredFragment, type MeasuredParagraph, type MeasuredResult, Measurer, Paragraph, type ParagraphContent, Parser, type Plugin, type Sizeable, Text, type TextAlign, type TextContent, type TextDecoration, type TextDrawStyle, type TextEffect, type TextHighlightStyle, type TextLayoutStyle, type TextListStyle, type TextOptions, type TextOrientation, type TextRenderOptions, type TextStyle, type TextTransform, type TextWrap, type VerticalAlign, type WritingMode, defaultTextStyles, definePlugin, drawPath, effect, fillBackground, filterEmpty, getTransform2D, highlight, isNone, listStyle, parseColor, setupView, uploadColor, uploadColors };
|
package/dist/index.d.mts
CHANGED
|
@@ -223,6 +223,7 @@ declare const defaultTextStyles: TextStyle;
|
|
|
223
223
|
declare class Text {
|
|
224
224
|
content: TextContent;
|
|
225
225
|
style: Partial<TextStyle>;
|
|
226
|
+
effects?: TextEffect[];
|
|
226
227
|
measureDom?: HTMLElement;
|
|
227
228
|
needsUpdate: boolean;
|
|
228
229
|
computedStyle: {
|
|
@@ -264,6 +265,7 @@ declare class Text {
|
|
|
264
265
|
measurer: Measurer;
|
|
265
266
|
plugins: Map<string, Plugin>;
|
|
266
267
|
get fontSize(): number;
|
|
268
|
+
get isVertical(): boolean;
|
|
267
269
|
get characters(): Character[];
|
|
268
270
|
constructor(options?: TextOptions);
|
|
269
271
|
use(plugin: Plugin): this;
|
|
@@ -282,7 +284,7 @@ interface Plugin {
|
|
|
282
284
|
renderOrder?: number;
|
|
283
285
|
render?: (ctx: CanvasRenderingContext2D, text: Text) => void;
|
|
284
286
|
}
|
|
285
|
-
declare function
|
|
287
|
+
declare function definePlugin(options: Plugin): Plugin;
|
|
286
288
|
|
|
287
289
|
type TextEffect = Partial<TextDrawStyle & {
|
|
288
290
|
offsetX: number;
|
|
@@ -290,7 +292,7 @@ type TextEffect = Partial<TextDrawStyle & {
|
|
|
290
292
|
skewX: number;
|
|
291
293
|
skewY: number;
|
|
292
294
|
}>;
|
|
293
|
-
declare function effect(
|
|
295
|
+
declare function effect(): Plugin;
|
|
294
296
|
declare function getTransform2D(text: Text, style: TextEffect): Matrix3;
|
|
295
297
|
|
|
296
298
|
interface HighlightOptions {
|
|
@@ -317,4 +319,4 @@ declare function uploadColors(ctx: CanvasRenderingContext2D, text: Text): void;
|
|
|
317
319
|
declare function isNone(val: string | undefined): boolean;
|
|
318
320
|
declare function filterEmpty(val: Record<string, any> | undefined): Record<string, any> | undefined;
|
|
319
321
|
|
|
320
|
-
export { Character, type DrawShapePathsOptions, type FontKerning, type FontStyle, type FontWeight, Fragment, type FragmentContent, type HighlightImage, type HighlightOptions, type HighlightOverflow, type HighlightSize, type HighlightStrokeWidth, type LinearGradient, type ListStyleImage, type ListStylePosition, type ListStyleSize, type ListStyleType, type MeasuredCharacter, type MeasuredFragment, type MeasuredParagraph, type MeasuredResult, Measurer, Paragraph, type ParagraphContent, Parser, type Plugin, type Sizeable, Text, type TextAlign, type TextContent, type TextDecoration, type TextDrawStyle, type TextEffect, type TextHighlightStyle, type TextLayoutStyle, type TextListStyle, type TextOptions, type TextOrientation, type TextRenderOptions, type TextStyle, type TextTransform, type TextWrap, type VerticalAlign, type WritingMode, defaultTextStyles, drawPath, effect, fillBackground, filterEmpty, getTransform2D, highlight, isNone, listStyle, parseColor,
|
|
322
|
+
export { Character, type DrawShapePathsOptions, type FontKerning, type FontStyle, type FontWeight, Fragment, type FragmentContent, type HighlightImage, type HighlightOptions, type HighlightOverflow, type HighlightSize, type HighlightStrokeWidth, type LinearGradient, type ListStyleImage, type ListStylePosition, type ListStyleSize, type ListStyleType, type MeasuredCharacter, type MeasuredFragment, type MeasuredParagraph, type MeasuredResult, Measurer, Paragraph, type ParagraphContent, Parser, type Plugin, type Sizeable, Text, type TextAlign, type TextContent, type TextDecoration, type TextDrawStyle, type TextEffect, type TextHighlightStyle, type TextLayoutStyle, type TextListStyle, type TextOptions, type TextOrientation, type TextRenderOptions, type TextStyle, type TextTransform, type TextWrap, type VerticalAlign, type WritingMode, defaultTextStyles, definePlugin, drawPath, effect, fillBackground, filterEmpty, getTransform2D, highlight, isNone, listStyle, parseColor, setupView, uploadColor, uploadColors };
|
package/dist/index.d.ts
CHANGED
|
@@ -223,6 +223,7 @@ declare const defaultTextStyles: TextStyle;
|
|
|
223
223
|
declare class Text {
|
|
224
224
|
content: TextContent;
|
|
225
225
|
style: Partial<TextStyle>;
|
|
226
|
+
effects?: TextEffect[];
|
|
226
227
|
measureDom?: HTMLElement;
|
|
227
228
|
needsUpdate: boolean;
|
|
228
229
|
computedStyle: {
|
|
@@ -264,6 +265,7 @@ declare class Text {
|
|
|
264
265
|
measurer: Measurer;
|
|
265
266
|
plugins: Map<string, Plugin>;
|
|
266
267
|
get fontSize(): number;
|
|
268
|
+
get isVertical(): boolean;
|
|
267
269
|
get characters(): Character[];
|
|
268
270
|
constructor(options?: TextOptions);
|
|
269
271
|
use(plugin: Plugin): this;
|
|
@@ -282,7 +284,7 @@ interface Plugin {
|
|
|
282
284
|
renderOrder?: number;
|
|
283
285
|
render?: (ctx: CanvasRenderingContext2D, text: Text) => void;
|
|
284
286
|
}
|
|
285
|
-
declare function
|
|
287
|
+
declare function definePlugin(options: Plugin): Plugin;
|
|
286
288
|
|
|
287
289
|
type TextEffect = Partial<TextDrawStyle & {
|
|
288
290
|
offsetX: number;
|
|
@@ -290,7 +292,7 @@ type TextEffect = Partial<TextDrawStyle & {
|
|
|
290
292
|
skewX: number;
|
|
291
293
|
skewY: number;
|
|
292
294
|
}>;
|
|
293
|
-
declare function effect(
|
|
295
|
+
declare function effect(): Plugin;
|
|
294
296
|
declare function getTransform2D(text: Text, style: TextEffect): Matrix3;
|
|
295
297
|
|
|
296
298
|
interface HighlightOptions {
|
|
@@ -317,4 +319,4 @@ declare function uploadColors(ctx: CanvasRenderingContext2D, text: Text): void;
|
|
|
317
319
|
declare function isNone(val: string | undefined): boolean;
|
|
318
320
|
declare function filterEmpty(val: Record<string, any> | undefined): Record<string, any> | undefined;
|
|
319
321
|
|
|
320
|
-
export { Character, type DrawShapePathsOptions, type FontKerning, type FontStyle, type FontWeight, Fragment, type FragmentContent, type HighlightImage, type HighlightOptions, type HighlightOverflow, type HighlightSize, type HighlightStrokeWidth, type LinearGradient, type ListStyleImage, type ListStylePosition, type ListStyleSize, type ListStyleType, type MeasuredCharacter, type MeasuredFragment, type MeasuredParagraph, type MeasuredResult, Measurer, Paragraph, type ParagraphContent, Parser, type Plugin, type Sizeable, Text, type TextAlign, type TextContent, type TextDecoration, type TextDrawStyle, type TextEffect, type TextHighlightStyle, type TextLayoutStyle, type TextListStyle, type TextOptions, type TextOrientation, type TextRenderOptions, type TextStyle, type TextTransform, type TextWrap, type VerticalAlign, type WritingMode, defaultTextStyles, drawPath, effect, fillBackground, filterEmpty, getTransform2D, highlight, isNone, listStyle, parseColor,
|
|
322
|
+
export { Character, type DrawShapePathsOptions, type FontKerning, type FontStyle, type FontWeight, Fragment, type FragmentContent, type HighlightImage, type HighlightOptions, type HighlightOverflow, type HighlightSize, type HighlightStrokeWidth, type LinearGradient, type ListStyleImage, type ListStylePosition, type ListStyleSize, type ListStyleType, type MeasuredCharacter, type MeasuredFragment, type MeasuredParagraph, type MeasuredResult, Measurer, Paragraph, type ParagraphContent, Parser, type Plugin, type Sizeable, Text, type TextAlign, type TextContent, type TextDecoration, type TextDrawStyle, type TextEffect, type TextHighlightStyle, type TextLayoutStyle, type TextListStyle, type TextOptions, type TextOrientation, type TextRenderOptions, type TextStyle, type TextTransform, type TextWrap, type VerticalAlign, type WritingMode, defaultTextStyles, definePlugin, drawPath, effect, fillBackground, filterEmpty, getTransform2D, highlight, isNone, listStyle, parseColor, setupView, uploadColor, uploadColors };
|