This commit is contained in:
Akko
2025-08-04 18:57:35 +02:00
parent 8cf6e78a79
commit 9495868c2e
5030 changed files with 518594 additions and 17609 deletions

View File

@@ -0,0 +1,16 @@
import type { TextStyle } from '../../text/TextStyle';
import type { AbstractBitmapFont } from '../AbstractBitmapFont';
export interface BitmapTextLayoutData {
width: number;
height: number;
scale: number;
offsetY: number;
lines: {
width: number;
charPositions: number[];
chars: string[];
spaceWidth: number;
spacesIndex: number[];
}[];
}
export declare function getBitmapTextLayout(chars: string[], style: TextStyle, font: AbstractBitmapFont<any>, trimEnd: boolean): BitmapTextLayoutData;

View File

@@ -0,0 +1,161 @@
'use strict';
"use strict";
function getBitmapTextLayout(chars, style, font, trimEnd) {
const layoutData = {
width: 0,
height: 0,
offsetY: 0,
scale: style.fontSize / font.baseMeasurementFontSize,
lines: [{
width: 0,
charPositions: [],
spaceWidth: 0,
spacesIndex: [],
chars: []
}]
};
layoutData.offsetY = font.baseLineOffset;
let currentLine = layoutData.lines[0];
let previousChar = null;
let firstWord = true;
const currentWord = {
spaceWord: false,
width: 0,
start: 0,
index: 0,
// use index to not modify the array as we use it a lot!
positions: [],
chars: []
};
const nextWord = (word) => {
const start = currentLine.width;
for (let j = 0; j < currentWord.index; j++) {
const position = word.positions[j];
currentLine.chars.push(word.chars[j]);
currentLine.charPositions.push(position + start);
}
currentLine.width += word.width;
firstWord = false;
currentWord.width = 0;
currentWord.index = 0;
currentWord.chars.length = 0;
};
const nextLine = () => {
let index = currentLine.chars.length - 1;
if (trimEnd) {
let lastChar = currentLine.chars[index];
while (lastChar === " ") {
currentLine.width -= font.chars[lastChar].xAdvance;
lastChar = currentLine.chars[--index];
}
}
layoutData.width = Math.max(layoutData.width, currentLine.width);
currentLine = {
width: 0,
charPositions: [],
chars: [],
spaceWidth: 0,
spacesIndex: []
};
firstWord = true;
layoutData.lines.push(currentLine);
layoutData.height += font.lineHeight;
};
const scale = font.baseMeasurementFontSize / style.fontSize;
const adjustedLetterSpacing = style.letterSpacing * scale;
const adjustedWordWrapWidth = style.wordWrapWidth * scale;
for (let i = 0; i < chars.length + 1; i++) {
let char;
const isEnd = i === chars.length;
if (!isEnd) {
char = chars[i];
}
const charData = font.chars[char] || font.chars[" "];
const isSpace = /(?:\s)/.test(char);
const isWordBreak = isSpace || char === "\r" || char === "\n" || isEnd;
if (isWordBreak) {
const addWordToNextLine = !firstWord && style.wordWrap && currentLine.width + currentWord.width - adjustedLetterSpacing > adjustedWordWrapWidth;
if (addWordToNextLine) {
nextLine();
nextWord(currentWord);
if (!isEnd) {
currentLine.charPositions.push(0);
}
} else {
currentWord.start = currentLine.width;
nextWord(currentWord);
if (!isEnd) {
currentLine.charPositions.push(0);
}
}
if (char === "\r" || char === "\n") {
if (currentLine.width !== 0) {
nextLine();
}
} else if (!isEnd) {
const spaceWidth = charData.xAdvance + (charData.kerning[previousChar] || 0) + adjustedLetterSpacing;
currentLine.width += spaceWidth;
currentLine.spaceWidth = spaceWidth;
currentLine.spacesIndex.push(currentLine.charPositions.length);
currentLine.chars.push(char);
}
} else {
const kerning = charData.kerning[previousChar] || 0;
const nextCharWidth = charData.xAdvance + kerning + adjustedLetterSpacing;
currentWord.positions[currentWord.index++] = currentWord.width + kerning;
currentWord.chars.push(char);
currentWord.width += nextCharWidth;
}
previousChar = char;
}
nextLine();
if (style.align === "center") {
alignCenter(layoutData);
} else if (style.align === "right") {
alignRight(layoutData);
} else if (style.align === "justify") {
alignJustify(layoutData);
}
return layoutData;
}
function alignCenter(measurementData) {
for (let i = 0; i < measurementData.lines.length; i++) {
const line = measurementData.lines[i];
const offset = measurementData.width / 2 - line.width / 2;
for (let j = 0; j < line.charPositions.length; j++) {
line.charPositions[j] += offset;
}
}
}
function alignRight(measurementData) {
for (let i = 0; i < measurementData.lines.length; i++) {
const line = measurementData.lines[i];
const offset = measurementData.width - line.width;
for (let j = 0; j < line.charPositions.length; j++) {
line.charPositions[j] += offset;
}
}
}
function alignJustify(measurementData) {
const width = measurementData.width;
for (let i = 0; i < measurementData.lines.length; i++) {
const line = measurementData.lines[i];
let indy = 0;
let spaceIndex = line.spacesIndex[indy++];
let offset = 0;
const totalSpaces = line.spacesIndex.length;
const newSpaceWidth = (width - line.width) / totalSpaces;
const spaceWidth = newSpaceWidth;
for (let j = 0; j < line.charPositions.length; j++) {
if (j === spaceIndex) {
spaceIndex = line.spacesIndex[indy++];
offset += spaceWidth;
}
line.charPositions[j] += offset;
}
}
}
exports.getBitmapTextLayout = getBitmapTextLayout;
//# sourceMappingURL=getBitmapTextLayout.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,159 @@
"use strict";
function getBitmapTextLayout(chars, style, font, trimEnd) {
const layoutData = {
width: 0,
height: 0,
offsetY: 0,
scale: style.fontSize / font.baseMeasurementFontSize,
lines: [{
width: 0,
charPositions: [],
spaceWidth: 0,
spacesIndex: [],
chars: []
}]
};
layoutData.offsetY = font.baseLineOffset;
let currentLine = layoutData.lines[0];
let previousChar = null;
let firstWord = true;
const currentWord = {
spaceWord: false,
width: 0,
start: 0,
index: 0,
// use index to not modify the array as we use it a lot!
positions: [],
chars: []
};
const nextWord = (word) => {
const start = currentLine.width;
for (let j = 0; j < currentWord.index; j++) {
const position = word.positions[j];
currentLine.chars.push(word.chars[j]);
currentLine.charPositions.push(position + start);
}
currentLine.width += word.width;
firstWord = false;
currentWord.width = 0;
currentWord.index = 0;
currentWord.chars.length = 0;
};
const nextLine = () => {
let index = currentLine.chars.length - 1;
if (trimEnd) {
let lastChar = currentLine.chars[index];
while (lastChar === " ") {
currentLine.width -= font.chars[lastChar].xAdvance;
lastChar = currentLine.chars[--index];
}
}
layoutData.width = Math.max(layoutData.width, currentLine.width);
currentLine = {
width: 0,
charPositions: [],
chars: [],
spaceWidth: 0,
spacesIndex: []
};
firstWord = true;
layoutData.lines.push(currentLine);
layoutData.height += font.lineHeight;
};
const scale = font.baseMeasurementFontSize / style.fontSize;
const adjustedLetterSpacing = style.letterSpacing * scale;
const adjustedWordWrapWidth = style.wordWrapWidth * scale;
for (let i = 0; i < chars.length + 1; i++) {
let char;
const isEnd = i === chars.length;
if (!isEnd) {
char = chars[i];
}
const charData = font.chars[char] || font.chars[" "];
const isSpace = /(?:\s)/.test(char);
const isWordBreak = isSpace || char === "\r" || char === "\n" || isEnd;
if (isWordBreak) {
const addWordToNextLine = !firstWord && style.wordWrap && currentLine.width + currentWord.width - adjustedLetterSpacing > adjustedWordWrapWidth;
if (addWordToNextLine) {
nextLine();
nextWord(currentWord);
if (!isEnd) {
currentLine.charPositions.push(0);
}
} else {
currentWord.start = currentLine.width;
nextWord(currentWord);
if (!isEnd) {
currentLine.charPositions.push(0);
}
}
if (char === "\r" || char === "\n") {
if (currentLine.width !== 0) {
nextLine();
}
} else if (!isEnd) {
const spaceWidth = charData.xAdvance + (charData.kerning[previousChar] || 0) + adjustedLetterSpacing;
currentLine.width += spaceWidth;
currentLine.spaceWidth = spaceWidth;
currentLine.spacesIndex.push(currentLine.charPositions.length);
currentLine.chars.push(char);
}
} else {
const kerning = charData.kerning[previousChar] || 0;
const nextCharWidth = charData.xAdvance + kerning + adjustedLetterSpacing;
currentWord.positions[currentWord.index++] = currentWord.width + kerning;
currentWord.chars.push(char);
currentWord.width += nextCharWidth;
}
previousChar = char;
}
nextLine();
if (style.align === "center") {
alignCenter(layoutData);
} else if (style.align === "right") {
alignRight(layoutData);
} else if (style.align === "justify") {
alignJustify(layoutData);
}
return layoutData;
}
function alignCenter(measurementData) {
for (let i = 0; i < measurementData.lines.length; i++) {
const line = measurementData.lines[i];
const offset = measurementData.width / 2 - line.width / 2;
for (let j = 0; j < line.charPositions.length; j++) {
line.charPositions[j] += offset;
}
}
}
function alignRight(measurementData) {
for (let i = 0; i < measurementData.lines.length; i++) {
const line = measurementData.lines[i];
const offset = measurementData.width - line.width;
for (let j = 0; j < line.charPositions.length; j++) {
line.charPositions[j] += offset;
}
}
}
function alignJustify(measurementData) {
const width = measurementData.width;
for (let i = 0; i < measurementData.lines.length; i++) {
const line = measurementData.lines[i];
let indy = 0;
let spaceIndex = line.spacesIndex[indy++];
let offset = 0;
const totalSpaces = line.spacesIndex.length;
const newSpaceWidth = (width - line.width) / totalSpaces;
const spaceWidth = newSpaceWidth;
for (let j = 0; j < line.charPositions.length; j++) {
if (j === spaceIndex) {
spaceIndex = line.spacesIndex[indy++];
offset += spaceWidth;
}
line.charPositions[j] += offset;
}
}
}
export { getBitmapTextLayout };
//# sourceMappingURL=getBitmapTextLayout.mjs.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,9 @@
/**
* Processes the passed character set data and returns a flattened array of all the characters.
*
* Ignored because not directly exposed.
* @ignore
* @param {string | string[] | string[][] } chars
* @returns {string[]} the flattened array of characters
*/
export declare function resolveCharacters(chars: string | (string | string[])[]): string[];

View File

@@ -0,0 +1,40 @@
'use strict';
"use strict";
function resolveCharacters(chars) {
if (chars === "") {
return [];
}
if (typeof chars === "string") {
chars = [chars];
}
const result = [];
for (let i = 0, j = chars.length; i < j; i++) {
const item = chars[i];
if (Array.isArray(item)) {
if (item.length !== 2) {
throw new Error(`[BitmapFont]: Invalid character range length, expecting 2 got ${item.length}.`);
}
if (item[0].length === 0 || item[1].length === 0) {
throw new Error("[BitmapFont]: Invalid character delimiter.");
}
const startCode = item[0].charCodeAt(0);
const endCode = item[1].charCodeAt(0);
if (endCode < startCode) {
throw new Error("[BitmapFont]: Invalid character range.");
}
for (let i2 = startCode, j2 = endCode; i2 <= j2; i2++) {
result.push(String.fromCharCode(i2));
}
} else {
result.push(...Array.from(item));
}
}
if (result.length === 0) {
throw new Error("[BitmapFont]: Empty set when resolving characters.");
}
return result;
}
exports.resolveCharacters = resolveCharacters;
//# sourceMappingURL=resolveCharacters.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"resolveCharacters.js","sources":["../../../../src/scene/text-bitmap/utils/resolveCharacters.ts"],"sourcesContent":["/**\n * Processes the passed character set data and returns a flattened array of all the characters.\n *\n * Ignored because not directly exposed.\n * @ignore\n * @param {string | string[] | string[][] } chars\n * @returns {string[]} the flattened array of characters\n */\n\nexport function resolveCharacters(chars: string | (string | string[])[]): string[]\n{\n // Skip unexpected 'empty set' check at end\n if (chars === '')\n {\n return [];\n }\n\n // Split the chars string into individual characters\n if (typeof chars === 'string')\n {\n chars = [chars];\n }\n\n // Handle an array of characters+ranges\n const result: string[] = [];\n\n for (let i = 0, j = chars.length; i < j; i++)\n {\n const item = chars[i];\n\n // Handle range delimited by start/end chars\n if (Array.isArray(item))\n {\n if (item.length !== 2)\n {\n throw new Error(`[BitmapFont]: Invalid character range length, expecting 2 got ${item.length}.`);\n }\n if (item[0].length === 0 || item[1].length === 0)\n {\n throw new Error('[BitmapFont]: Invalid character delimiter.');\n }\n\n const startCode = item[0].charCodeAt(0);\n const endCode = item[1].charCodeAt(0);\n\n if (endCode < startCode)\n {\n throw new Error('[BitmapFont]: Invalid character range.');\n }\n\n for (let i = startCode, j = endCode; i <= j; i++)\n {\n result.push(String.fromCharCode(i));\n }\n }\n else\n {\n result.push(...Array.from(item));\n }\n }\n\n if (result.length === 0)\n {\n throw new Error('[BitmapFont]: Empty set when resolving characters.');\n }\n\n return result;\n}\n"],"names":["i","j"],"mappings":";;;AASO,SAAS,kBAAkB,KAClC,EAAA;AAEI,EAAA,IAAI,UAAU,EACd,EAAA;AACI,IAAA,OAAO,EAAC,CAAA;AAAA,GACZ;AAGA,EAAI,IAAA,OAAO,UAAU,QACrB,EAAA;AACI,IAAA,KAAA,GAAQ,CAAC,KAAK,CAAA,CAAA;AAAA,GAClB;AAGA,EAAA,MAAM,SAAmB,EAAC,CAAA;AAE1B,EAAA,KAAA,IAAS,IAAI,CAAG,EAAA,CAAA,GAAI,MAAM,MAAQ,EAAA,CAAA,GAAI,GAAG,CACzC,EAAA,EAAA;AACI,IAAM,MAAA,IAAA,GAAO,MAAM,CAAC,CAAA,CAAA;AAGpB,IAAI,IAAA,KAAA,CAAM,OAAQ,CAAA,IAAI,CACtB,EAAA;AACI,MAAI,IAAA,IAAA,CAAK,WAAW,CACpB,EAAA;AACI,QAAA,MAAM,IAAI,KAAA,CAAM,CAAiE,8DAAA,EAAA,IAAA,CAAK,MAAM,CAAG,CAAA,CAAA,CAAA,CAAA;AAAA,OACnG;AACA,MAAI,IAAA,IAAA,CAAK,CAAC,CAAE,CAAA,MAAA,KAAW,KAAK,IAAK,CAAA,CAAC,CAAE,CAAA,MAAA,KAAW,CAC/C,EAAA;AACI,QAAM,MAAA,IAAI,MAAM,4CAA4C,CAAA,CAAA;AAAA,OAChE;AAEA,MAAA,MAAM,SAAY,GAAA,IAAA,CAAK,CAAC,CAAA,CAAE,WAAW,CAAC,CAAA,CAAA;AACtC,MAAA,MAAM,OAAU,GAAA,IAAA,CAAK,CAAC,CAAA,CAAE,WAAW,CAAC,CAAA,CAAA;AAEpC,MAAA,IAAI,UAAU,SACd,EAAA;AACI,QAAM,MAAA,IAAI,MAAM,wCAAwC,CAAA,CAAA;AAAA,OAC5D;AAEA,MAAA,KAAA,IAASA,KAAI,SAAWC,EAAAA,EAAAA,GAAI,OAASD,EAAAA,EAAAA,IAAKC,IAAGD,EAC7C,EAAA,EAAA;AACI,QAAA,MAAA,CAAO,IAAK,CAAA,MAAA,CAAO,YAAaA,CAAAA,EAAC,CAAC,CAAA,CAAA;AAAA,OACtC;AAAA,KAGJ,MAAA;AACI,MAAA,MAAA,CAAO,IAAK,CAAA,GAAG,KAAM,CAAA,IAAA,CAAK,IAAI,CAAC,CAAA,CAAA;AAAA,KACnC;AAAA,GACJ;AAEA,EAAI,IAAA,MAAA,CAAO,WAAW,CACtB,EAAA;AACI,IAAM,MAAA,IAAI,MAAM,oDAAoD,CAAA,CAAA;AAAA,GACxE;AAEA,EAAO,OAAA,MAAA,CAAA;AACX;;;;"}

View File

@@ -0,0 +1,38 @@
"use strict";
function resolveCharacters(chars) {
if (chars === "") {
return [];
}
if (typeof chars === "string") {
chars = [chars];
}
const result = [];
for (let i = 0, j = chars.length; i < j; i++) {
const item = chars[i];
if (Array.isArray(item)) {
if (item.length !== 2) {
throw new Error(`[BitmapFont]: Invalid character range length, expecting 2 got ${item.length}.`);
}
if (item[0].length === 0 || item[1].length === 0) {
throw new Error("[BitmapFont]: Invalid character delimiter.");
}
const startCode = item[0].charCodeAt(0);
const endCode = item[1].charCodeAt(0);
if (endCode < startCode) {
throw new Error("[BitmapFont]: Invalid character range.");
}
for (let i2 = startCode, j2 = endCode; i2 <= j2; i2++) {
result.push(String.fromCharCode(i2));
}
} else {
result.push(...Array.from(item));
}
}
if (result.length === 0) {
throw new Error("[BitmapFont]: Empty set when resolving characters.");
}
return result;
}
export { resolveCharacters };
//# sourceMappingURL=resolveCharacters.mjs.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"resolveCharacters.mjs","sources":["../../../../src/scene/text-bitmap/utils/resolveCharacters.ts"],"sourcesContent":["/**\n * Processes the passed character set data and returns a flattened array of all the characters.\n *\n * Ignored because not directly exposed.\n * @ignore\n * @param {string | string[] | string[][] } chars\n * @returns {string[]} the flattened array of characters\n */\n\nexport function resolveCharacters(chars: string | (string | string[])[]): string[]\n{\n // Skip unexpected 'empty set' check at end\n if (chars === '')\n {\n return [];\n }\n\n // Split the chars string into individual characters\n if (typeof chars === 'string')\n {\n chars = [chars];\n }\n\n // Handle an array of characters+ranges\n const result: string[] = [];\n\n for (let i = 0, j = chars.length; i < j; i++)\n {\n const item = chars[i];\n\n // Handle range delimited by start/end chars\n if (Array.isArray(item))\n {\n if (item.length !== 2)\n {\n throw new Error(`[BitmapFont]: Invalid character range length, expecting 2 got ${item.length}.`);\n }\n if (item[0].length === 0 || item[1].length === 0)\n {\n throw new Error('[BitmapFont]: Invalid character delimiter.');\n }\n\n const startCode = item[0].charCodeAt(0);\n const endCode = item[1].charCodeAt(0);\n\n if (endCode < startCode)\n {\n throw new Error('[BitmapFont]: Invalid character range.');\n }\n\n for (let i = startCode, j = endCode; i <= j; i++)\n {\n result.push(String.fromCharCode(i));\n }\n }\n else\n {\n result.push(...Array.from(item));\n }\n }\n\n if (result.length === 0)\n {\n throw new Error('[BitmapFont]: Empty set when resolving characters.');\n }\n\n return result;\n}\n"],"names":["i","j"],"mappings":";AASO,SAAS,kBAAkB,KAClC,EAAA;AAEI,EAAA,IAAI,UAAU,EACd,EAAA;AACI,IAAA,OAAO,EAAC,CAAA;AAAA,GACZ;AAGA,EAAI,IAAA,OAAO,UAAU,QACrB,EAAA;AACI,IAAA,KAAA,GAAQ,CAAC,KAAK,CAAA,CAAA;AAAA,GAClB;AAGA,EAAA,MAAM,SAAmB,EAAC,CAAA;AAE1B,EAAA,KAAA,IAAS,IAAI,CAAG,EAAA,CAAA,GAAI,MAAM,MAAQ,EAAA,CAAA,GAAI,GAAG,CACzC,EAAA,EAAA;AACI,IAAM,MAAA,IAAA,GAAO,MAAM,CAAC,CAAA,CAAA;AAGpB,IAAI,IAAA,KAAA,CAAM,OAAQ,CAAA,IAAI,CACtB,EAAA;AACI,MAAI,IAAA,IAAA,CAAK,WAAW,CACpB,EAAA;AACI,QAAA,MAAM,IAAI,KAAA,CAAM,CAAiE,8DAAA,EAAA,IAAA,CAAK,MAAM,CAAG,CAAA,CAAA,CAAA,CAAA;AAAA,OACnG;AACA,MAAI,IAAA,IAAA,CAAK,CAAC,CAAE,CAAA,MAAA,KAAW,KAAK,IAAK,CAAA,CAAC,CAAE,CAAA,MAAA,KAAW,CAC/C,EAAA;AACI,QAAM,MAAA,IAAI,MAAM,4CAA4C,CAAA,CAAA;AAAA,OAChE;AAEA,MAAA,MAAM,SAAY,GAAA,IAAA,CAAK,CAAC,CAAA,CAAE,WAAW,CAAC,CAAA,CAAA;AACtC,MAAA,MAAM,OAAU,GAAA,IAAA,CAAK,CAAC,CAAA,CAAE,WAAW,CAAC,CAAA,CAAA;AAEpC,MAAA,IAAI,UAAU,SACd,EAAA;AACI,QAAM,MAAA,IAAI,MAAM,wCAAwC,CAAA,CAAA;AAAA,OAC5D;AAEA,MAAA,KAAA,IAASA,KAAI,SAAWC,EAAAA,EAAAA,GAAI,OAASD,EAAAA,EAAAA,IAAKC,IAAGD,EAC7C,EAAA,EAAA;AACI,QAAA,MAAA,CAAO,IAAK,CAAA,MAAA,CAAO,YAAaA,CAAAA,EAAC,CAAC,CAAA,CAAA;AAAA,OACtC;AAAA,KAGJ,MAAA;AACI,MAAA,MAAA,CAAO,IAAK,CAAA,GAAG,KAAM,CAAA,IAAA,CAAK,IAAI,CAAC,CAAA,CAAA;AAAA,KACnC;AAAA,GACJ;AAEA,EAAI,IAAA,MAAA,CAAO,WAAW,CACtB,EAAA;AACI,IAAM,MAAA,IAAI,MAAM,oDAAoD,CAAA,CAAA;AAAA,GACxE;AAEA,EAAO,OAAA,MAAA,CAAA;AACX;;;;"}