@babylonjs/addons 8.6.2 → 8.7.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/index.d.ts +1 -0
- package/index.js +1 -0
- package/index.js.map +1 -1
- package/msdfText/fontAsset.d.ts +34 -0
- package/msdfText/fontAsset.js +83 -0
- package/msdfText/fontAsset.js.map +1 -0
- package/msdfText/index.d.ts +3 -0
- package/msdfText/index.js +4 -0
- package/msdfText/index.js.map +1 -0
- package/msdfText/paragraphOptions.d.ts +13 -0
- package/msdfText/paragraphOptions.js +13 -0
- package/msdfText/paragraphOptions.js.map +1 -0
- package/msdfText/sdf/bmFont.d.ts +129 -0
- package/msdfText/sdf/bmFont.js +2 -0
- package/msdfText/sdf/bmFont.js.map +1 -0
- package/msdfText/sdf/font.d.ts +8 -0
- package/msdfText/sdf/font.js +2 -0
- package/msdfText/sdf/font.js.map +1 -0
- package/msdfText/sdf/glyph.d.ts +11 -0
- package/msdfText/sdf/glyph.js +2 -0
- package/msdfText/sdf/glyph.js.map +1 -0
- package/msdfText/sdf/line.d.ts +9 -0
- package/msdfText/sdf/line.js +2 -0
- package/msdfText/sdf/line.js.map +1 -0
- package/msdfText/sdf/paragraph.d.ts +21 -0
- package/msdfText/sdf/paragraph.js +130 -0
- package/msdfText/sdf/paragraph.js.map +1 -0
- package/msdfText/textRenderer.d.ts +99 -0
- package/msdfText/textRenderer.js +282 -0
- package/msdfText/textRenderer.js.map +1 -0
- package/msdfText/webgl/fragment.d.ts +5 -0
- package/msdfText/webgl/fragment.js +45 -0
- package/msdfText/webgl/fragment.js.map +1 -0
- package/msdfText/webgl/vertex.d.ts +5 -0
- package/msdfText/webgl/vertex.js +25 -0
- package/msdfText/webgl/vertex.js.map +1 -0
- package/msdfText/webgpu/fragment.d.ts +5 -0
- package/msdfText/webgpu/fragment.js +47 -0
- package/msdfText/webgpu/fragment.js.map +1 -0
- package/msdfText/webgpu/vertex.d.ts +5 -0
- package/msdfText/webgpu/vertex.js +31 -0
- package/msdfText/webgpu/vertex.js.map +1 -0
- package/package.json +2 -2
package/index.d.ts
CHANGED
package/index.js
CHANGED
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../dev/addons/src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC","sourcesContent":["export * from \"./htmlMesh\";\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../dev/addons/src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC","sourcesContent":["export * from \"./htmlMesh\";\nexport * from \"./msdfText\";\n"]}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { BMFontChar } from "./sdf/bmFont.js";
|
|
2
|
+
import type { SdfFont } from "./sdf/font.js";
|
|
3
|
+
import { Texture } from "@babylonjs/core/Materials/Textures/texture.js";
|
|
4
|
+
/**
|
|
5
|
+
* Class representing a font asset for SDF (Signed Distance Field) rendering.
|
|
6
|
+
*/
|
|
7
|
+
export declare class FontAsset {
|
|
8
|
+
private readonly _chars;
|
|
9
|
+
private readonly _charsRegex;
|
|
10
|
+
private readonly _kernings;
|
|
11
|
+
/** @internal */
|
|
12
|
+
readonly _font: SdfFont;
|
|
13
|
+
/**
|
|
14
|
+
* Gets the font scale value
|
|
15
|
+
*/
|
|
16
|
+
readonly scale: number;
|
|
17
|
+
/**
|
|
18
|
+
* Gets the list of used textures
|
|
19
|
+
*/
|
|
20
|
+
readonly textures: Texture[];
|
|
21
|
+
/**
|
|
22
|
+
* Creates a new FontAsset instance.
|
|
23
|
+
* @param definitionData defines the font data in JSON format.
|
|
24
|
+
* @param textureUrl defines the url of the texture to use for the font.
|
|
25
|
+
*/
|
|
26
|
+
constructor(definitionData: string, textureUrl: string);
|
|
27
|
+
private _updateFallbacks;
|
|
28
|
+
/** @internal */
|
|
29
|
+
_getChar(charCode: number): BMFontChar;
|
|
30
|
+
/** @internal */
|
|
31
|
+
_getKerning(first: number, second: number): number;
|
|
32
|
+
/** @internal */
|
|
33
|
+
_unsupportedChars(text: string): string;
|
|
34
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { Texture } from "@babylonjs/core/Materials/Textures/texture.js";
|
|
2
|
+
var CharCode;
|
|
3
|
+
(function (CharCode) {
|
|
4
|
+
CharCode[CharCode["SPACE"] = 32] = "SPACE";
|
|
5
|
+
CharCode[CharCode["TOFU"] = 65532] = "TOFU";
|
|
6
|
+
})(CharCode || (CharCode = {}));
|
|
7
|
+
/**
|
|
8
|
+
* Class representing a font asset for SDF (Signed Distance Field) rendering.
|
|
9
|
+
*/
|
|
10
|
+
export class FontAsset {
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new FontAsset instance.
|
|
13
|
+
* @param definitionData defines the font data in JSON format.
|
|
14
|
+
* @param textureUrl defines the url of the texture to use for the font.
|
|
15
|
+
*/
|
|
16
|
+
constructor(definitionData, textureUrl) {
|
|
17
|
+
this._chars = new Map();
|
|
18
|
+
this._kernings = new Map();
|
|
19
|
+
this._font = JSON.parse(definitionData);
|
|
20
|
+
// So far we only consider one page
|
|
21
|
+
this._font.pages = [textureUrl];
|
|
22
|
+
this._font.chars.forEach((char) => this._chars.set(char.id, char));
|
|
23
|
+
this._font.kernings.forEach((kerning) => {
|
|
24
|
+
let submap = this._kernings.get(kerning.first);
|
|
25
|
+
if (!submap) {
|
|
26
|
+
submap = new Map();
|
|
27
|
+
this._kernings.set(kerning.first, submap);
|
|
28
|
+
}
|
|
29
|
+
submap.set(kerning.second, kerning.amount);
|
|
30
|
+
});
|
|
31
|
+
this._charsRegex = new RegExp(`[${this._font.chars.map((c) => c.char.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&")).join("")}]`, "g");
|
|
32
|
+
this._updateFallbacks();
|
|
33
|
+
this.scale = 1 / this._font.info.size;
|
|
34
|
+
this.textures = this._font.pages.map((page) => new Texture(page, undefined, { noMipmap: true, invertY: false }));
|
|
35
|
+
}
|
|
36
|
+
_updateFallbacks() {
|
|
37
|
+
if (!this._chars.has(CharCode.SPACE)) {
|
|
38
|
+
this._chars.set(CharCode.SPACE, {
|
|
39
|
+
id: CharCode.SPACE,
|
|
40
|
+
x: 0,
|
|
41
|
+
y: 0,
|
|
42
|
+
width: 0,
|
|
43
|
+
height: 0,
|
|
44
|
+
xoffset: 0,
|
|
45
|
+
yoffset: 0,
|
|
46
|
+
xadvance: this._font.info.size * 0.5,
|
|
47
|
+
page: -1,
|
|
48
|
+
chnl: -1,
|
|
49
|
+
index: -1,
|
|
50
|
+
char: " ",
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
if (!this._chars.has(CharCode.TOFU)) {
|
|
54
|
+
this._chars.set(CharCode.TOFU, {
|
|
55
|
+
id: CharCode.TOFU,
|
|
56
|
+
x: 0,
|
|
57
|
+
y: 0,
|
|
58
|
+
width: this._font.info.size,
|
|
59
|
+
height: this._font.info.size,
|
|
60
|
+
xoffset: 0,
|
|
61
|
+
yoffset: 0,
|
|
62
|
+
xadvance: this._font.info.size * 0.5,
|
|
63
|
+
page: -1,
|
|
64
|
+
chnl: -1,
|
|
65
|
+
index: -1,
|
|
66
|
+
char: "",
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/** @internal */
|
|
71
|
+
_getChar(charCode) {
|
|
72
|
+
return this._chars.get(charCode) || this._chars.get(CharCode.TOFU);
|
|
73
|
+
}
|
|
74
|
+
/** @internal */
|
|
75
|
+
_getKerning(first, second) {
|
|
76
|
+
return this._kernings.get(first)?.get(second) || 0;
|
|
77
|
+
}
|
|
78
|
+
/** @internal */
|
|
79
|
+
_unsupportedChars(text) {
|
|
80
|
+
return text.replace(this._charsRegex, "");
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=fontAsset.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"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AAC,IAAI,CAAC,EAAE,CAAC;YAClC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,EAAE;gBAC3B,EAAE,EAAE,QAAQ,CAAC,IAAI;gBACjB,CAAC,EAAE,CAAC;gBACJ,CAAC,EAAE,CAAC;gBACJ,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI;gBAC3B,MAAM,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI;gBAC5B,OAAO,EAAE,CAAC;gBACV,OAAO,EAAE,CAAC;gBACV,QAAQ,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,GAAG,GAAG;gBACpC,IAAI,EAAE,CAAC,CAAC;gBACR,IAAI,EAAE,CAAC,CAAC;gBACR,KAAK,EAAE,CAAC,CAAC;gBACT,IAAI,EAAE,GAAG;aACZ,CAAC,CAAC;QACP,CAAC;IACL,CAAC;IAED,gBAAgB;IACT,QAAQ,CAAC,QAAgB;QAC5B,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAE,CAAC;IACxE,CAAC;IAED,gBAAgB;IACT,WAAW,CAAC,KAAa,EAAE,MAAc;QAC5C,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACvD,CAAC;IAED,gBAAgB;IACT,iBAAiB,CAAC,IAAY;QACjC,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,WAAW,EAAE,EAAE,CAAC,CAAC;IAC9C,CAAC;CACJ","sourcesContent":["import type { BMFontChar } from \"./sdf/bmFont\";\r\nimport type { SdfFont } from \"./sdf/font\";\r\nimport { Texture } from \"core/Materials/Textures/texture\";\r\n\r\nenum CharCode {\r\n SPACE = 32,\r\n TOFU = 0xfffc,\r\n}\r\n\r\n/**\r\n * Class representing a font asset for SDF (Signed Distance Field) rendering.\r\n */\r\nexport class FontAsset {\r\n private readonly _chars = new Map<number, BMFontChar>();\r\n private readonly _charsRegex: RegExp;\r\n private readonly _kernings = new Map<number, Map<number, number>>();\r\n\r\n /** @internal */\r\n public readonly _font: SdfFont;\r\n\r\n /**\r\n * Gets the font scale value\r\n */\r\n public readonly scale: number;\r\n\r\n /**\r\n * Gets the list of used textures\r\n */\r\n public readonly textures: Texture[];\r\n\r\n /**\r\n * Creates a new FontAsset instance.\r\n * @param definitionData defines the font data in JSON format.\r\n * @param textureUrl defines the url of the texture to use for the font.\r\n */\r\n public constructor(definitionData: string, textureUrl: string) {\r\n this._font = JSON.parse(definitionData) as SdfFont;\r\n // So far we only consider one page\r\n this._font.pages = [textureUrl];\r\n\r\n this._font.chars.forEach((char) => this._chars.set(char.id, char));\r\n this._font.kernings.forEach((kerning) => {\r\n let submap = this._kernings.get(kerning.first);\r\n if (!submap) {\r\n submap = new Map();\r\n this._kernings.set(kerning.first, submap);\r\n }\r\n submap.set(kerning.second, kerning.amount);\r\n });\r\n this._charsRegex = new RegExp(`[${this._font.chars.map((c) => c.char.replace(/[-[\\]{}()*+?.,\\\\^$|#\\s]/g, \"\\\\$&\")).join(\"\")}]`, \"g\");\r\n\r\n this._updateFallbacks();\r\n\r\n this.scale = 1 / this._font.info.size;\r\n this.textures = this._font.pages.map((page) => new Texture(page, undefined, { noMipmap: true, invertY: false }));\r\n }\r\n\r\n private _updateFallbacks() {\r\n if (!this._chars.has(CharCode.SPACE)) {\r\n this._chars.set(CharCode.SPACE, {\r\n id: CharCode.SPACE,\r\n x: 0,\r\n y: 0,\r\n width: 0,\r\n height: 0,\r\n xoffset: 0,\r\n yoffset: 0,\r\n xadvance: this._font.info.size * 0.5,\r\n page: -1,\r\n chnl: -1,\r\n index: -1,\r\n char: \" \",\r\n });\r\n }\r\n\r\n if (!this._chars.has(CharCode.TOFU)) {\r\n this._chars.set(CharCode.TOFU, {\r\n id: CharCode.TOFU,\r\n x: 0,\r\n y: 0,\r\n width: this._font.info.size,\r\n height: this._font.info.size,\r\n xoffset: 0,\r\n yoffset: 0,\r\n xadvance: this._font.info.size * 0.5,\r\n page: -1,\r\n chnl: -1,\r\n index: -1,\r\n char: \"\",\r\n });\r\n }\r\n }\r\n\r\n /** @internal */\r\n public _getChar(charCode: number) {\r\n return this._chars.get(charCode) || this._chars.get(CharCode.TOFU)!;\r\n }\r\n\r\n /** @internal */\r\n public _getKerning(first: number, second: number) {\r\n return this._kernings.get(first)?.get(second) || 0;\r\n }\r\n\r\n /** @internal */\r\n public _unsupportedChars(text: string) {\r\n return text.replace(this._charsRegex, \"\");\r\n }\r\n}\r\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../dev/addons/src/msdfText/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,oBAAoB,CAAC;AACnC,cAAc,gBAAgB,CAAC","sourcesContent":["export * from \"./fontAsset\";\r\nexport * from \"./paragraphOptions\";\r\nexport * from \"./textRenderer\";\r\n"]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Vector2 } from "@babylonjs/core/Maths/math.vector.js";
|
|
2
|
+
/** @internal */
|
|
3
|
+
export type ParagraphOptions = {
|
|
4
|
+
maxWidth: number;
|
|
5
|
+
lineHeight: number;
|
|
6
|
+
letterSpacing: number;
|
|
7
|
+
tabSize: number;
|
|
8
|
+
whiteSpace: "pre-line";
|
|
9
|
+
textAlign: "left" | "right" | "center";
|
|
10
|
+
translate: Vector2 | undefined;
|
|
11
|
+
};
|
|
12
|
+
/** @internal */
|
|
13
|
+
export declare const DefaultParagraphOptions: ParagraphOptions;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/* eslint-disable jsdoc/require-jsdoc */
|
|
2
|
+
import { Vector2 } from "@babylonjs/core/Maths/math.vector.js";
|
|
3
|
+
/** @internal */
|
|
4
|
+
export const DefaultParagraphOptions = {
|
|
5
|
+
maxWidth: Infinity,
|
|
6
|
+
lineHeight: 1,
|
|
7
|
+
letterSpacing: 1,
|
|
8
|
+
tabSize: 4,
|
|
9
|
+
whiteSpace: "pre-line",
|
|
10
|
+
textAlign: "center",
|
|
11
|
+
translate: new Vector2(-0.5, -0.5),
|
|
12
|
+
};
|
|
13
|
+
//# sourceMappingURL=paragraphOptions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"paragraphOptions.js","sourceRoot":"","sources":["../../../../dev/addons/src/msdfText/paragraphOptions.ts"],"names":[],"mappings":"AAAA,wCAAwC;AAExC,OAAO,EAAE,OAAO,EAAE,6CAA+B;AAajD,gBAAgB;AAChB,MAAM,CAAC,MAAM,uBAAuB,GAAqB;IACrD,QAAQ,EAAE,QAAQ;IAClB,UAAU,EAAE,CAAC;IACb,aAAa,EAAE,CAAC;IAChB,OAAO,EAAE,CAAC;IACV,UAAU,EAAE,UAAU;IACtB,SAAS,EAAE,QAAQ;IACnB,SAAS,EAAE,IAAI,OAAO,CAAC,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC;CACrC,CAAC","sourcesContent":["/* eslint-disable jsdoc/require-jsdoc */\r\n\r\nimport { Vector2 } from \"core/Maths/math.vector\";\r\n\r\n/** @internal */\r\nexport type ParagraphOptions = {\r\n maxWidth: number;\r\n lineHeight: number;\r\n letterSpacing: number;\r\n tabSize: number;\r\n whiteSpace: /* 'normal' | 'nowrap' | 'pre' | 'pre-wrap' | */ \"pre-line\" /* | 'break-spaces'*/;\r\n textAlign: \"left\" | \"right\" | \"center\" /* | 'justify'*/;\r\n translate: Vector2 | undefined;\r\n};\r\n\r\n/** @internal */\r\nexport const DefaultParagraphOptions: ParagraphOptions = {\r\n maxWidth: Infinity,\r\n lineHeight: 1,\r\n letterSpacing: 1,\r\n tabSize: 4,\r\n whiteSpace: \"pre-line\",\r\n textAlign: \"center\",\r\n translate: new Vector2(-0.5, -0.5),\r\n};\r\n"]}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Holds information on how the font was generated.
|
|
3
|
+
*/
|
|
4
|
+
export type BMFontInfo = {
|
|
5
|
+
/** The name of the font */
|
|
6
|
+
face: string;
|
|
7
|
+
/** The size of the font */
|
|
8
|
+
size: number;
|
|
9
|
+
/** The font is bold */
|
|
10
|
+
bold: number;
|
|
11
|
+
/** The font is italic */
|
|
12
|
+
italic: number;
|
|
13
|
+
/** The charset of the font */
|
|
14
|
+
charset: string[];
|
|
15
|
+
/** The charset is unicode */
|
|
16
|
+
unicode: number;
|
|
17
|
+
/** The font height stretch in percentage. 100% means no stretch. */
|
|
18
|
+
stretchH: number;
|
|
19
|
+
/** Set to 1 if smoothing was turned on. */
|
|
20
|
+
smooth: number;
|
|
21
|
+
/** The supersampling level used. 1 means no supersampling was used. */
|
|
22
|
+
aa: number;
|
|
23
|
+
/** The padding for each character (up, right, down, left). */
|
|
24
|
+
padding: [number, number, number, number];
|
|
25
|
+
/** The spacing for each character (horizontal, vertical). */
|
|
26
|
+
spacing: [number, number];
|
|
27
|
+
/**
|
|
28
|
+
* The outline thickness for the characters.
|
|
29
|
+
*
|
|
30
|
+
* @remark missing in msdf-bmfont-xml
|
|
31
|
+
*/
|
|
32
|
+
outline?: number;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Holds information common to all characters.
|
|
36
|
+
*/
|
|
37
|
+
export type BMFontCommon = {
|
|
38
|
+
/** Distance in pixels between each line of text */
|
|
39
|
+
lineHeight: number;
|
|
40
|
+
/** The number of pixels from the absolute top of the line to the base of the characters */
|
|
41
|
+
base: number;
|
|
42
|
+
/** The width of the texture, normally used to scale the x pos of the character image */
|
|
43
|
+
scaleW: number;
|
|
44
|
+
/** The height of the texture, normally used to scale the y pos of the character image */
|
|
45
|
+
scaleH: number;
|
|
46
|
+
/** The number of pages in the font */
|
|
47
|
+
pages: number;
|
|
48
|
+
/** Set to 1 if the monochrome characters have been packed into each of the texture channels. In this case alphaChnl describes what is stored in each channel. */
|
|
49
|
+
packed: number;
|
|
50
|
+
/** Set to 0 if the channel holds the glyph data, 1 if it holds the outline, 2 if it holds the glyph and the outline, 3 if its set to zero, and 4 if its set to one. */
|
|
51
|
+
alphaChnl: number;
|
|
52
|
+
/** Set to 0 if the channel holds the glyph data, 1 if it holds the outline, 2 if it holds the glyph and the outline, 3 if its set to zero, and 4 if its set to one. */
|
|
53
|
+
redChnl: number;
|
|
54
|
+
/** Set to 0 if the channel holds the glyph data, 1 if it holds the outline, 2 if it holds the glyph and the outline, 3 if its set to zero, and 4 if its set to one. */
|
|
55
|
+
greenChnl: number;
|
|
56
|
+
/** Set to 0 if the channel holds the glyph data, 1 if it holds the outline, 2 if it holds the glyph and the outline, 3 if its set to zero, and 4 if its set to one. */
|
|
57
|
+
blueChnl: number;
|
|
58
|
+
};
|
|
59
|
+
/** Name of a texture file. There is one for each page in the font. */
|
|
60
|
+
export type BMFontPages = {
|
|
61
|
+
[id: number]: string;
|
|
62
|
+
} & Array<string>;
|
|
63
|
+
/**
|
|
64
|
+
* Describes a single character in the font
|
|
65
|
+
*/
|
|
66
|
+
export type BMFontChar = {
|
|
67
|
+
/** Character id (charCode) */
|
|
68
|
+
id: number;
|
|
69
|
+
/** Left position of the character image in the texture. */
|
|
70
|
+
x: number;
|
|
71
|
+
/** Right position of the character image in the texture */
|
|
72
|
+
y: number;
|
|
73
|
+
/** Width of the chracter image in the texture */
|
|
74
|
+
width: number;
|
|
75
|
+
/** Height of the chracter image in the texture */
|
|
76
|
+
height: number;
|
|
77
|
+
/** Horizontal offset to be applied on screen */
|
|
78
|
+
xoffset: number;
|
|
79
|
+
/** Vertical offset to be applied on screen */
|
|
80
|
+
yoffset: number;
|
|
81
|
+
/** Horizontal advance after the character */
|
|
82
|
+
xadvance: number;
|
|
83
|
+
/** Page index where the character image is found */
|
|
84
|
+
page: number;
|
|
85
|
+
/** Texture channel where the chracter image is found
|
|
86
|
+
* - 1 = blue
|
|
87
|
+
* - 2 = green
|
|
88
|
+
* - 3 = red
|
|
89
|
+
* - 8 = alpha
|
|
90
|
+
* - 15 = all channels
|
|
91
|
+
*/
|
|
92
|
+
chnl: number;
|
|
93
|
+
} & BMFontCharExtra;
|
|
94
|
+
/**
|
|
95
|
+
* additional context from msdf-bmfont-xml
|
|
96
|
+
*/
|
|
97
|
+
export type BMFontCharExtra = {
|
|
98
|
+
/** index of opentype.js glyph */
|
|
99
|
+
index: number;
|
|
100
|
+
/** actual character*/
|
|
101
|
+
char: string;
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* The kerning information is used to adjust the distance between certain characters, e.g. some characters should be placed closer to each other than others.
|
|
105
|
+
*/
|
|
106
|
+
export type BMFontKerning = {
|
|
107
|
+
/** The first character id. */
|
|
108
|
+
first: number;
|
|
109
|
+
/** The second character id. */
|
|
110
|
+
second: number;
|
|
111
|
+
/** How much the x position should be adjusted when drawing the second character immediately following the first. */
|
|
112
|
+
amount: number;
|
|
113
|
+
};
|
|
114
|
+
/**
|
|
115
|
+
* Compatible with [msdf-bmfont-xml](https://github.com/soimy/msdf-bmfont-xml)
|
|
116
|
+
* @see https://www.angelcode.com/products/bmfont/doc/file_format.html
|
|
117
|
+
*/
|
|
118
|
+
export type BMFont = {
|
|
119
|
+
/** {@inheritDoc BMFontInfo} */
|
|
120
|
+
info: BMFontInfo;
|
|
121
|
+
/** {@inheritDoc BMFontCommon} */
|
|
122
|
+
common: BMFontCommon;
|
|
123
|
+
/** {@inheritDoc BMFontPages} */
|
|
124
|
+
pages: BMFontPages;
|
|
125
|
+
/** {@inheritDoc BMFontChar} */
|
|
126
|
+
chars: BMFontChar[];
|
|
127
|
+
/** {@inheritDoc BMFontKerning} */
|
|
128
|
+
kernings: BMFontKerning[];
|
|
129
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bmFont.js","sourceRoot":"","sources":["../../../../../dev/addons/src/msdfText/sdf/bmFont.ts"],"names":[],"mappings":"","sourcesContent":["/* eslint-disable @typescript-eslint/naming-convention */\r\n/**\r\n * Holds information on how the font was generated.\r\n */\r\nexport type BMFontInfo = {\r\n /** The name of the font */\r\n face: string;\r\n /** The size of the font */\r\n size: number;\r\n /** The font is bold */\r\n bold: number;\r\n /** The font is italic */\r\n italic: number;\r\n /** The charset of the font */\r\n charset: string[];\r\n /** The charset is unicode */\r\n unicode: number;\r\n /** The font height stretch in percentage. 100% means no stretch. */\r\n stretchH: number;\r\n /** Set to 1 if smoothing was turned on. */\r\n smooth: number;\r\n /** The supersampling level used. 1 means no supersampling was used. */\r\n aa: number;\r\n /** The padding for each character (up, right, down, left). */\r\n padding: [number, number, number, number];\r\n /** The spacing for each character (horizontal, vertical). */\r\n spacing: [number, number];\r\n /**\r\n * The outline thickness for the characters.\r\n *\r\n * @remark missing in msdf-bmfont-xml\r\n */\r\n outline?: number;\r\n};\r\n\r\n/**\r\n * Holds information common to all characters.\r\n */\r\nexport type BMFontCommon = {\r\n /** Distance in pixels between each line of text */\r\n lineHeight: number;\r\n /** The number of pixels from the absolute top of the line to the base of the characters */\r\n base: number;\r\n /** The width of the texture, normally used to scale the x pos of the character image */\r\n scaleW: number;\r\n /** The height of the texture, normally used to scale the y pos of the character image */\r\n scaleH: number;\r\n /** The number of pages in the font */\r\n pages: number;\r\n /** Set to 1 if the monochrome characters have been packed into each of the texture channels. In this case alphaChnl describes what is stored in each channel. */\r\n packed: number;\r\n /** Set to 0 if the channel holds the glyph data, 1 if it holds the outline, 2 if it holds the glyph and the outline, 3 if its set to zero, and 4 if its set to one. */\r\n alphaChnl: number;\r\n /** Set to 0 if the channel holds the glyph data, 1 if it holds the outline, 2 if it holds the glyph and the outline, 3 if its set to zero, and 4 if its set to one. */\r\n redChnl: number;\r\n /** Set to 0 if the channel holds the glyph data, 1 if it holds the outline, 2 if it holds the glyph and the outline, 3 if its set to zero, and 4 if its set to one. */\r\n greenChnl: number;\r\n /** Set to 0 if the channel holds the glyph data, 1 if it holds the outline, 2 if it holds the glyph and the outline, 3 if its set to zero, and 4 if its set to one. */\r\n blueChnl: number;\r\n};\r\n\r\n/** Name of a texture file. There is one for each page in the font. */\r\nexport type BMFontPages = {\r\n [id: number]: string;\r\n} & Array<string>;\r\n\r\n/**\r\n * Describes a single character in the font\r\n */\r\nexport type BMFontChar = {\r\n /** Character id (charCode) */\r\n id: number;\r\n /** Left position of the character image in the texture. */\r\n x: number;\r\n /** Right position of the character image in the texture */\r\n y: number;\r\n /** Width of the chracter image in the texture */\r\n width: number;\r\n /** Height of the chracter image in the texture */\r\n height: number;\r\n /** Horizontal offset to be applied on screen */\r\n xoffset: number;\r\n /** Vertical offset to be applied on screen */\r\n yoffset: number;\r\n /** Horizontal advance after the character */\r\n xadvance: number;\r\n /** Page index where the character image is found */\r\n page: number;\r\n /** Texture channel where the chracter image is found\r\n * - 1 = blue\r\n * - 2 = green\r\n * - 3 = red\r\n * - 8 = alpha\r\n * - 15 = all channels\r\n */\r\n chnl: number;\r\n} & BMFontCharExtra;\r\n\r\n/**\r\n * additional context from msdf-bmfont-xml\r\n */\r\nexport type BMFontCharExtra = {\r\n /** index of opentype.js glyph */\r\n index: number;\r\n /** actual character*/\r\n char: string;\r\n};\r\n\r\n/**\r\n * The kerning information is used to adjust the distance between certain characters, e.g. some characters should be placed closer to each other than others.\r\n */\r\nexport type BMFontKerning = {\r\n /** The first character id. */\r\n first: number;\r\n /** The second character id. */\r\n second: number;\r\n /** How much the x position should be adjusted when drawing the second character immediately following the first. */\r\n amount: number;\r\n};\r\n\r\n/**\r\n * Compatible with [msdf-bmfont-xml](https://github.com/soimy/msdf-bmfont-xml)\r\n * @see https://www.angelcode.com/products/bmfont/doc/file_format.html\r\n */\r\nexport type BMFont = {\r\n /** {@inheritDoc BMFontInfo} */\r\n info: BMFontInfo;\r\n /** {@inheritDoc BMFontCommon} */\r\n common: BMFontCommon;\r\n /** {@inheritDoc BMFontPages} */\r\n pages: BMFontPages;\r\n /** {@inheritDoc BMFontChar} */\r\n chars: BMFontChar[];\r\n /** {@inheritDoc BMFontKerning} */\r\n kernings: BMFontKerning[];\r\n};\r\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"font.js","sourceRoot":"","sources":["../../../../../dev/addons/src/msdfText/sdf/font.ts"],"names":[],"mappings":"","sourcesContent":["/* eslint-disable jsdoc/require-jsdoc */\r\nimport type { BMFont } from \"./bmFont\";\r\n\r\nexport type SdfFontDistanceField = {\r\n fieldType: \"sdf\" | \"msdf\";\r\n distanceRange: number;\r\n};\r\n\r\nexport type SdfFont = BMFont & {\r\n distanceField: SdfFontDistanceField;\r\n};\r\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"glyph.js","sourceRoot":"","sources":["../../../../../dev/addons/src/msdfText/sdf/glyph.ts"],"names":[],"mappings":"","sourcesContent":["/* eslint-disable jsdoc/require-jsdoc */\r\nimport type { BMFontChar } from \"./bmFont\";\r\n\r\n/** @internal */\r\nexport type SdfGlyph = {\r\n char: BMFontChar;\r\n /** index of the line */\r\n line: number;\r\n /** position within the line */\r\n position: number;\r\n x: number;\r\n y: number;\r\n};\r\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"line.js","sourceRoot":"","sources":["../../../../../dev/addons/src/msdfText/sdf/line.ts"],"names":[],"mappings":"","sourcesContent":["/* eslint-disable jsdoc/require-jsdoc */\r\nimport type { SdfGlyph } from \"./glyph\";\r\n\r\n/** @internal */\r\nexport type SdfTextLine = {\r\n text: string;\r\n glyphs: SdfGlyph[];\r\n start: number;\r\n end: number;\r\n width: number;\r\n};\r\n"]}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { FontAsset } from "../fontAsset.js";
|
|
2
|
+
import { type ParagraphOptions } from "../paragraphOptions.js";
|
|
3
|
+
import type { SdfGlyph } from "./glyph.js";
|
|
4
|
+
import type { SdfTextLine } from "./line.js";
|
|
5
|
+
/** @internal */
|
|
6
|
+
export declare class SdfTextParagraph {
|
|
7
|
+
readonly text: string;
|
|
8
|
+
readonly fontAsset: FontAsset;
|
|
9
|
+
readonly options: ParagraphOptions;
|
|
10
|
+
get lineHeight(): number;
|
|
11
|
+
readonly paragraph: string;
|
|
12
|
+
readonly lines: SdfTextLine[];
|
|
13
|
+
readonly width: number;
|
|
14
|
+
readonly height: number;
|
|
15
|
+
readonly glyphs: SdfGlyph[];
|
|
16
|
+
constructor(text: string, fontAsset: FontAsset, options?: Partial<ParagraphOptions>);
|
|
17
|
+
private _computeMetrics;
|
|
18
|
+
private _breakLines;
|
|
19
|
+
private _collapse;
|
|
20
|
+
private _wrap;
|
|
21
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { DefaultParagraphOptions } from "../paragraphOptions.js";
|
|
2
|
+
/** @internal */
|
|
3
|
+
export class SdfTextParagraph {
|
|
4
|
+
get lineHeight() {
|
|
5
|
+
return this.fontAsset._font.common.lineHeight * this.options.lineHeight;
|
|
6
|
+
}
|
|
7
|
+
constructor(text, fontAsset, options) {
|
|
8
|
+
this.text = text;
|
|
9
|
+
this.fontAsset = fontAsset;
|
|
10
|
+
this.options = { ...DefaultParagraphOptions, ...options };
|
|
11
|
+
const { paragraph, lines, glyphs, width, height } = this._computeMetrics(text);
|
|
12
|
+
this.paragraph = paragraph;
|
|
13
|
+
this.lines = lines;
|
|
14
|
+
this.glyphs = glyphs;
|
|
15
|
+
this.width = width;
|
|
16
|
+
this.height = height;
|
|
17
|
+
}
|
|
18
|
+
_computeMetrics(text) {
|
|
19
|
+
const collapsed = this._collapse(text);
|
|
20
|
+
const breaked = this._breakLines(collapsed);
|
|
21
|
+
const trimmed = breaked.map((line) => line.trim());
|
|
22
|
+
const lines = [];
|
|
23
|
+
for (const line of trimmed) {
|
|
24
|
+
lines.push(...this._wrap(line, lines.length));
|
|
25
|
+
}
|
|
26
|
+
const width = Math.max(...lines.map((line) => line.width));
|
|
27
|
+
const height = this.lineHeight * lines.length;
|
|
28
|
+
if (this.options.textAlign !== "left" || this.options.translate) {
|
|
29
|
+
lines.forEach((line) => {
|
|
30
|
+
const anchor = (() => {
|
|
31
|
+
switch (this.options.textAlign) {
|
|
32
|
+
case "right":
|
|
33
|
+
return width - line.width;
|
|
34
|
+
case "center":
|
|
35
|
+
return (width - line.width) / 2;
|
|
36
|
+
case "left":
|
|
37
|
+
default:
|
|
38
|
+
return 0;
|
|
39
|
+
}
|
|
40
|
+
})();
|
|
41
|
+
const translate = this.options.translate?.multiplyByFloats(width, height);
|
|
42
|
+
line.glyphs.forEach((glyph) => {
|
|
43
|
+
glyph.x += anchor;
|
|
44
|
+
if (translate) {
|
|
45
|
+
glyph.x += translate.x;
|
|
46
|
+
glyph.y += translate.y;
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
const glyphs = lines.flatMap((line) => line.glyphs);
|
|
52
|
+
return {
|
|
53
|
+
paragraph: trimmed.join("\n"),
|
|
54
|
+
lines,
|
|
55
|
+
glyphs,
|
|
56
|
+
width,
|
|
57
|
+
height,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
_breakLines(text) {
|
|
61
|
+
return text.split("\n");
|
|
62
|
+
}
|
|
63
|
+
_collapse(text) {
|
|
64
|
+
return text.replace(/\t/g, " ".repeat(this.options.tabSize)).replace(/ +/g, " ");
|
|
65
|
+
}
|
|
66
|
+
_wrap(text, lineOffset = 0) {
|
|
67
|
+
const lines = new Array();
|
|
68
|
+
let currentLine = lineOffset;
|
|
69
|
+
let currentGlyphs = new Array();
|
|
70
|
+
let currentCursor = 0;
|
|
71
|
+
let currentWidth = 0;
|
|
72
|
+
let lastChar;
|
|
73
|
+
let start = 0;
|
|
74
|
+
let end = start;
|
|
75
|
+
const pushCurrentLine = () => {
|
|
76
|
+
lines.push({
|
|
77
|
+
text: text.slice(start, end),
|
|
78
|
+
glyphs: currentGlyphs,
|
|
79
|
+
start: start,
|
|
80
|
+
end: end,
|
|
81
|
+
width: currentWidth,
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
while (end < text.length) {
|
|
85
|
+
const i = end;
|
|
86
|
+
const charCode = text.charCodeAt(i);
|
|
87
|
+
const char = this.fontAsset._getChar(charCode);
|
|
88
|
+
const charWidth = char.width;
|
|
89
|
+
const kerning = lastChar ? this.fontAsset._getKerning(lastChar.id, char.id) : 0;
|
|
90
|
+
currentCursor += kerning + char.xoffset;
|
|
91
|
+
const newWidth = currentCursor + charWidth;
|
|
92
|
+
const cursorProgress = char.xadvance + this.options.letterSpacing;
|
|
93
|
+
const nextPosition = currentCursor + cursorProgress;
|
|
94
|
+
const shouldBreak = nextPosition > this.options.maxWidth || newWidth > this.options.maxWidth;
|
|
95
|
+
if (shouldBreak) {
|
|
96
|
+
pushCurrentLine();
|
|
97
|
+
currentLine++;
|
|
98
|
+
lastChar = undefined;
|
|
99
|
+
currentCursor = 0;
|
|
100
|
+
currentWidth = 0;
|
|
101
|
+
start = end;
|
|
102
|
+
end = start + 1;
|
|
103
|
+
currentGlyphs = [];
|
|
104
|
+
}
|
|
105
|
+
const x = currentCursor;
|
|
106
|
+
const y = currentLine * this.lineHeight + char.yoffset;
|
|
107
|
+
currentGlyphs.push({
|
|
108
|
+
char,
|
|
109
|
+
line: currentLine,
|
|
110
|
+
position: currentGlyphs.length,
|
|
111
|
+
x: x,
|
|
112
|
+
y: y,
|
|
113
|
+
});
|
|
114
|
+
if (!shouldBreak) {
|
|
115
|
+
lastChar = char;
|
|
116
|
+
currentCursor = nextPosition;
|
|
117
|
+
currentWidth = newWidth;
|
|
118
|
+
end++;
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
currentCursor = cursorProgress;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (currentGlyphs.length > 0) {
|
|
125
|
+
pushCurrentLine();
|
|
126
|
+
}
|
|
127
|
+
return lines;
|
|
128
|
+
}
|
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AAG,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC;YAEvD,aAAa,CAAC,IAAI,CAAC;gBACf,IAAI;gBACJ,IAAI,EAAE,WAAW;gBACjB,QAAQ,EAAE,aAAa,CAAC,MAAM;gBAC9B,CAAC,EAAE,CAAC;gBACJ,CAAC,EAAE,CAAC;aACP,CAAC,CAAC;YAEH,IAAI,CAAC,WAAW,EAAE,CAAC;gBACf,QAAQ,GAAG,IAAI,CAAC;gBAChB,aAAa,GAAG,YAAY,CAAC;gBAC7B,YAAY,GAAG,QAAQ,CAAC;gBACxB,GAAG,EAAE,CAAC;YACV,CAAC;iBAAM,CAAC;gBACJ,aAAa,GAAG,cAAc,CAAC;YACnC,CAAC;QACL,CAAC;QAED,IAAI,aAAa,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAC3B,eAAe,EAAE,CAAC;QACtB,CAAC;QAED,OAAO,KAAK,CAAC;IACjB,CAAC;CACJ","sourcesContent":["/* eslint-disable babylonjs/available */\r\n/* eslint-disable jsdoc/require-jsdoc */\r\nimport type { FontAsset } from \"../fontAsset\";\r\nimport { DefaultParagraphOptions, type ParagraphOptions } from \"../paragraphOptions\";\r\nimport type { BMFontChar } from \"./bmFont\";\r\nimport type { SdfGlyph } from \"./glyph\";\r\nimport type { SdfTextLine } from \"./line\";\r\n\r\n/** @internal */\r\nexport class SdfTextParagraph {\r\n public readonly options: ParagraphOptions;\r\n\r\n get lineHeight() {\r\n return this.fontAsset._font.common.lineHeight * this.options.lineHeight;\r\n }\r\n\r\n readonly paragraph;\r\n readonly lines;\r\n readonly width;\r\n readonly height;\r\n readonly glyphs;\r\n\r\n constructor(\r\n public readonly text: string,\r\n public readonly fontAsset: FontAsset,\r\n options?: Partial<ParagraphOptions>\r\n ) {\r\n this.options = { ...DefaultParagraphOptions, ...options };\r\n\r\n const { paragraph, lines, glyphs, width, height } = this._computeMetrics(text);\r\n\r\n this.paragraph = paragraph;\r\n this.lines = lines;\r\n this.glyphs = glyphs;\r\n this.width = width;\r\n this.height = height;\r\n }\r\n\r\n private _computeMetrics(text: string) {\r\n const collapsed = this._collapse(text);\r\n const breaked = this._breakLines(collapsed);\r\n const trimmed = breaked.map((line) => line.trim());\r\n\r\n const lines: SdfTextLine[] = [];\r\n for (const line of trimmed) {\r\n lines.push(...this._wrap(line, lines.length));\r\n }\r\n\r\n const width = Math.max(...lines.map((line) => line.width));\r\n const height = this.lineHeight * lines.length;\r\n\r\n if (this.options.textAlign !== \"left\" || this.options.translate) {\r\n lines.forEach((line) => {\r\n const anchor = (() => {\r\n switch (this.options.textAlign) {\r\n case \"right\":\r\n return width - line.width;\r\n case \"center\":\r\n return (width - line.width) / 2;\r\n case \"left\":\r\n default:\r\n return 0;\r\n }\r\n })();\r\n const translate = this.options.translate?.multiplyByFloats(width, height);\r\n line.glyphs.forEach((glyph) => {\r\n glyph.x += anchor;\r\n if (translate) {\r\n glyph.x += translate.x;\r\n glyph.y += translate.y;\r\n }\r\n });\r\n });\r\n }\r\n\r\n const glyphs = lines.flatMap((line) => line.glyphs);\r\n\r\n return {\r\n paragraph: trimmed.join(\"\\n\"),\r\n lines,\r\n glyphs,\r\n width,\r\n height,\r\n };\r\n }\r\n\r\n private _breakLines(text: string) {\r\n return text.split(\"\\n\");\r\n }\r\n\r\n private _collapse(text: string) {\r\n return text.replace(/\\t/g, \" \".repeat(this.options.tabSize)).replace(/ +/g, \" \");\r\n }\r\n\r\n private _wrap(text: string, lineOffset = 0) {\r\n const lines = new Array<SdfTextLine>();\r\n\r\n let currentLine = lineOffset;\r\n let currentGlyphs = new Array<SdfGlyph>();\r\n let currentCursor = 0;\r\n let currentWidth = 0;\r\n let lastChar: BMFontChar | undefined;\r\n let start = 0;\r\n let end = start;\r\n\r\n const pushCurrentLine = () => {\r\n lines.push({\r\n text: text.slice(start, end),\r\n glyphs: currentGlyphs,\r\n start: start,\r\n end: end,\r\n width: currentWidth,\r\n });\r\n };\r\n\r\n while (end < text.length) {\r\n const i = end;\r\n const charCode = text.charCodeAt(i);\r\n const char = this.fontAsset._getChar(charCode);\r\n const charWidth = char.width;\r\n const kerning = lastChar ? this.fontAsset._getKerning(lastChar.id, char.id) : 0;\r\n\r\n currentCursor += kerning + char.xoffset;\r\n const newWidth = currentCursor + charWidth;\r\n const cursorProgress = char.xadvance + this.options.letterSpacing;\r\n const nextPosition = currentCursor + cursorProgress;\r\n\r\n const shouldBreak = nextPosition > this.options.maxWidth || newWidth > this.options.maxWidth;\r\n\r\n if (shouldBreak) {\r\n pushCurrentLine();\r\n\r\n currentLine++;\r\n lastChar = undefined;\r\n currentCursor = 0;\r\n currentWidth = 0;\r\n start = end;\r\n end = start + 1;\r\n currentGlyphs = [];\r\n }\r\n\r\n const x = currentCursor;\r\n const y = currentLine * this.lineHeight + char.yoffset;\r\n\r\n currentGlyphs.push({\r\n char,\r\n line: currentLine,\r\n position: currentGlyphs.length,\r\n x: x,\r\n y: y,\r\n });\r\n\r\n if (!shouldBreak) {\r\n lastChar = char;\r\n currentCursor = nextPosition;\r\n currentWidth = newWidth;\r\n end++;\r\n } else {\r\n currentCursor = cursorProgress;\r\n }\r\n }\r\n\r\n if (currentGlyphs.length > 0) {\r\n pushCurrentLine();\r\n }\r\n\r\n return lines;\r\n }\r\n}\r\n"]}
|