@excaliburjs/plugin-sdf-text 0.0.0-stage → 1.0.0
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/LICENSE +25 -0
- package/dist/cli.js +294 -0
- package/dist/excalibur-sdf-text.js +712 -0
- package/dist/excalibur-sdf-text.js.map +1 -0
- package/dist/excalibur-sdf-text.min.js +498 -0
- package/dist/excalibur-sdf-text.min.js.map +1 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/sdf-font.d.ts +227 -0
- package/dist/types/sdf-text-plugin.d.ts +13 -0
- package/dist/types/sdf-text-renderer.d.ts +79 -0
- package/dist/types/sdf-text.d.ts +59 -0
- package/package.json +68 -4
- package/readme.md +112 -0
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
BSD 2-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023, Excalibur.js
|
|
4
|
+
All rights reserved.
|
|
5
|
+
|
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
|
8
|
+
|
|
9
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
10
|
+
list of conditions and the following disclaimer.
|
|
11
|
+
|
|
12
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
and/or other materials provided with the distribution.
|
|
15
|
+
|
|
16
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
17
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
18
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
19
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
20
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
21
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
22
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
23
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
24
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
25
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// cli.ts
|
|
4
|
+
import * as fs from "node:fs";
|
|
5
|
+
import * as path from "node:path";
|
|
6
|
+
import { Canvas, FontLibrary, ImageData as ImageData2 } from "skia-canvas";
|
|
7
|
+
import TinySDF2 from "@mapbox/tiny-sdf";
|
|
8
|
+
|
|
9
|
+
// src/sdf-font.ts
|
|
10
|
+
import TinySDF from "@mapbox/tiny-sdf";
|
|
11
|
+
import { BoundingBox, Color, FontSource, ImageFiltering, ImageSource, ImageSourceAttributeConstants, Resource, Vector } from "excalibur";
|
|
12
|
+
var SDF_CUTOFF = 0.25;
|
|
13
|
+
function getCharactersFromUnicodeRanges(ranges) {
|
|
14
|
+
return ranges.flatMap(([start, end]) => Array.from({ length: end - start + 1 }, (_, i) => String.fromCodePoint(start + i))).join("");
|
|
15
|
+
}
|
|
16
|
+
var SDFSource = class {
|
|
17
|
+
/**
|
|
18
|
+
* Code point to glyph placement on the atlas
|
|
19
|
+
*/
|
|
20
|
+
glyphs = /* @__PURE__ */ new Map();
|
|
21
|
+
/**
|
|
22
|
+
* Default text color when an {@link SDFText} does not specify one
|
|
23
|
+
*/
|
|
24
|
+
color = Color.Black;
|
|
25
|
+
/**
|
|
26
|
+
* Font size the atlas was generated at, text is scaled relative to this
|
|
27
|
+
*/
|
|
28
|
+
fontSize = 16;
|
|
29
|
+
/**
|
|
30
|
+
* Padding around every glyph on the atlas in pixels
|
|
31
|
+
*/
|
|
32
|
+
buffer = 2;
|
|
33
|
+
/**
|
|
34
|
+
* Max ascent above the baseline of any glyph in atlas pixels, used to place the first line
|
|
35
|
+
*/
|
|
36
|
+
ascent = 0;
|
|
37
|
+
/**
|
|
38
|
+
* Normalized distance at which a pixel counts as inside a glyph (0..1). tiny-sdf encodes the
|
|
39
|
+
* edge at `1 - cutoff`, with the default cutoff of 0.25 that is 0.75.
|
|
40
|
+
*/
|
|
41
|
+
halo = 1 - SDF_CUTOFF;
|
|
42
|
+
/**
|
|
43
|
+
* Antialiasing ramp width around the glyph edge
|
|
44
|
+
*/
|
|
45
|
+
gamma = 0.1;
|
|
46
|
+
_cachedText;
|
|
47
|
+
_cachedSize;
|
|
48
|
+
_cachedMaxWidth;
|
|
49
|
+
_cachedLines = [];
|
|
50
|
+
/**
|
|
51
|
+
* Split text into lines, honoring explicit newlines and wrapping at `maxWidth` pixels (when given)
|
|
52
|
+
*/
|
|
53
|
+
getLines(text, size, maxWidth) {
|
|
54
|
+
if (this._cachedText === text && this._cachedSize === size && this._cachedMaxWidth === maxWidth) {
|
|
55
|
+
return this._cachedLines;
|
|
56
|
+
}
|
|
57
|
+
const lines = [];
|
|
58
|
+
for (const paragraph of text.split("\n")) {
|
|
59
|
+
if (maxWidth == null || this.measureLine(paragraph, size) <= maxWidth) {
|
|
60
|
+
lines.push(paragraph);
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
let line = "";
|
|
64
|
+
for (const word of paragraph.split(" ")) {
|
|
65
|
+
const candidate = line ? line + " " + word : word;
|
|
66
|
+
if (this.measureLine(candidate, size) <= maxWidth) {
|
|
67
|
+
line = candidate;
|
|
68
|
+
} else {
|
|
69
|
+
if (line) {
|
|
70
|
+
lines.push(line);
|
|
71
|
+
}
|
|
72
|
+
line = word;
|
|
73
|
+
while (this.measureLine(line, size) > maxWidth && line.length > 1) {
|
|
74
|
+
let fit = line.length - 1;
|
|
75
|
+
while (fit > 1 && this.measureLine(line.slice(0, fit), size) > maxWidth) {
|
|
76
|
+
fit--;
|
|
77
|
+
}
|
|
78
|
+
lines.push(line.slice(0, fit));
|
|
79
|
+
line = line.slice(fit);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
lines.push(line);
|
|
84
|
+
}
|
|
85
|
+
this._cachedText = text;
|
|
86
|
+
this._cachedSize = size;
|
|
87
|
+
this._cachedMaxWidth = maxWidth;
|
|
88
|
+
this._cachedLines = lines;
|
|
89
|
+
return lines;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Width in pixels of a single line of text at `size`
|
|
93
|
+
*/
|
|
94
|
+
measureLine(line, size) {
|
|
95
|
+
const scale = size / this.fontSize;
|
|
96
|
+
let width = 0;
|
|
97
|
+
for (const char of line) {
|
|
98
|
+
const glyph = this.glyphs.get(char);
|
|
99
|
+
if (glyph) {
|
|
100
|
+
width += glyph.glyphAdvance * scale;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return width;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Bounds of a block of text at `size`, every line is `size * lineHeight` tall
|
|
107
|
+
*/
|
|
108
|
+
measureText(text, size, maxWidth, lineHeight = 1.2) {
|
|
109
|
+
if (!this.isLoaded) {
|
|
110
|
+
throw new Error(`Cannot measureText("${text}") on an SDF source that is not loaded yet`);
|
|
111
|
+
}
|
|
112
|
+
const lines = this.getLines(text, size, maxWidth);
|
|
113
|
+
let width = 0;
|
|
114
|
+
for (const line of lines) {
|
|
115
|
+
width = Math.max(width, this.measureLine(line, size));
|
|
116
|
+
}
|
|
117
|
+
return BoundingBox.fromDimension(width, lines.length * size * lineHeight, Vector.Zero);
|
|
118
|
+
}
|
|
119
|
+
_computeAscent() {
|
|
120
|
+
let ascent = 0;
|
|
121
|
+
for (const glyph of this.glyphs.values()) {
|
|
122
|
+
ascent = Math.max(ascent, glyph.glyphTop);
|
|
123
|
+
}
|
|
124
|
+
this.ascent = ascent;
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
var SDFFont = class extends SDFSource {
|
|
128
|
+
atlas;
|
|
129
|
+
_atlasCtx;
|
|
130
|
+
get data() {
|
|
131
|
+
return this.atlas;
|
|
132
|
+
}
|
|
133
|
+
_isLoaded = false;
|
|
134
|
+
get isLoaded() {
|
|
135
|
+
return this._isLoaded;
|
|
136
|
+
}
|
|
137
|
+
_sdfProvider = (options) => new TinySDF(options);
|
|
138
|
+
_canvasProvider = (width, height) => {
|
|
139
|
+
const canvas = document.createElement("canvas");
|
|
140
|
+
canvas.width = width;
|
|
141
|
+
canvas.height = height;
|
|
142
|
+
return canvas;
|
|
143
|
+
};
|
|
144
|
+
_imageDataProvider = (width, height) => new ImageData(width, height);
|
|
145
|
+
_fontSourceLoader = async (file, family) => {
|
|
146
|
+
await new FontSource(file, family).load();
|
|
147
|
+
};
|
|
148
|
+
fontFamily;
|
|
149
|
+
fontFile;
|
|
150
|
+
fontWeight;
|
|
151
|
+
fontStyle;
|
|
152
|
+
alphabet;
|
|
153
|
+
/**
|
|
154
|
+
* Distance field radius in pixels
|
|
155
|
+
*/
|
|
156
|
+
radius;
|
|
157
|
+
constructor(options) {
|
|
158
|
+
super();
|
|
159
|
+
this._sdfProvider = options.sdfProvider ?? this._sdfProvider;
|
|
160
|
+
this._canvasProvider = options.canvasProvider ?? this._canvasProvider;
|
|
161
|
+
this._imageDataProvider = options.imageDataProvider ?? this._imageDataProvider;
|
|
162
|
+
this._fontSourceLoader = options.fontSourceLoader ?? this._fontSourceLoader;
|
|
163
|
+
this.color = options.color ?? Color.Black;
|
|
164
|
+
this.fontFile = options.fontFile;
|
|
165
|
+
this.fontFamily = options.fontFamily;
|
|
166
|
+
this.fontSize = options.fontSize ?? 48;
|
|
167
|
+
this.fontWeight = options.fontWeight?.toString() ?? "400";
|
|
168
|
+
this.fontStyle = options.fontStyle ?? "normal";
|
|
169
|
+
this.alphabet = Array.isArray(options.alphabet) ? getCharactersFromUnicodeRanges(options.alphabet) : options.alphabet ?? getCharactersFromUnicodeRanges([[32, 126]]);
|
|
170
|
+
this.buffer = Math.ceil(this.fontSize / 8);
|
|
171
|
+
this.radius = Math.ceil(this.fontSize / 3);
|
|
172
|
+
this.gamma = 2 * 1.4142 / this.fontSize;
|
|
173
|
+
const cell = this.fontSize + this.buffer * 2;
|
|
174
|
+
const dimension = Math.ceil(Math.sqrt([...this.alphabet].length)) * cell;
|
|
175
|
+
this.atlas = this._canvasProvider(dimension, dimension);
|
|
176
|
+
if (!this.atlas) {
|
|
177
|
+
throw new Error("Cannot build the SDF atlas canvas");
|
|
178
|
+
}
|
|
179
|
+
this.atlas.setAttribute?.(ImageSourceAttributeConstants.Filtering, ImageFiltering.Blended);
|
|
180
|
+
const ctx = this.atlas.getContext("2d");
|
|
181
|
+
if (!ctx) {
|
|
182
|
+
throw new Error("Cannot build the SDF atlas 2D context");
|
|
183
|
+
}
|
|
184
|
+
this._atlasCtx = ctx;
|
|
185
|
+
}
|
|
186
|
+
_toRGBAImageData(alphaChannel, width, height) {
|
|
187
|
+
const imageData = this._imageDataProvider(width, height);
|
|
188
|
+
for (let i = 0; i < alphaChannel.length; i++) {
|
|
189
|
+
imageData.data[4 * i + 0] = alphaChannel[i];
|
|
190
|
+
imageData.data[4 * i + 1] = alphaChannel[i];
|
|
191
|
+
imageData.data[4 * i + 2] = alphaChannel[i];
|
|
192
|
+
imageData.data[4 * i + 3] = 255;
|
|
193
|
+
}
|
|
194
|
+
return imageData;
|
|
195
|
+
}
|
|
196
|
+
async load() {
|
|
197
|
+
if (this._isLoaded) {
|
|
198
|
+
return this.atlas;
|
|
199
|
+
}
|
|
200
|
+
if (this.fontFile) {
|
|
201
|
+
await this._fontSourceLoader(this.fontFile, this.fontFamily);
|
|
202
|
+
}
|
|
203
|
+
const tinySdf = this._sdfProvider({
|
|
204
|
+
fontSize: this.fontSize,
|
|
205
|
+
fontFamily: this.fontFamily,
|
|
206
|
+
fontStyle: this.fontStyle,
|
|
207
|
+
fontWeight: this.fontWeight,
|
|
208
|
+
buffer: this.buffer,
|
|
209
|
+
radius: this.radius,
|
|
210
|
+
cutoff: SDF_CUTOFF
|
|
211
|
+
});
|
|
212
|
+
const glyphs = [];
|
|
213
|
+
for (const char of this.alphabet) {
|
|
214
|
+
glyphs.push([char, tinySdf.draw(char)]);
|
|
215
|
+
}
|
|
216
|
+
let x = 0;
|
|
217
|
+
let y = 0;
|
|
218
|
+
let rowHeight = 0;
|
|
219
|
+
for (const [char, glyph] of glyphs) {
|
|
220
|
+
if (x + glyph.width > this.atlas.width) {
|
|
221
|
+
x = 0;
|
|
222
|
+
y += rowHeight;
|
|
223
|
+
rowHeight = 0;
|
|
224
|
+
}
|
|
225
|
+
if (y + glyph.height > this.atlas.height) {
|
|
226
|
+
throw new Error(`SDF atlas is too small for the alphabet of "${this.fontFamily}", this is a bug`);
|
|
227
|
+
}
|
|
228
|
+
this._atlasCtx.putImageData(this._toRGBAImageData(glyph.data, glyph.width, glyph.height), x, y);
|
|
229
|
+
const { data, ...placement } = glyph;
|
|
230
|
+
this.glyphs.set(char, { ...placement, x, y });
|
|
231
|
+
x += glyph.width;
|
|
232
|
+
rowHeight = Math.max(rowHeight, glyph.height);
|
|
233
|
+
}
|
|
234
|
+
this._computeAscent();
|
|
235
|
+
this._isLoaded = true;
|
|
236
|
+
return this.atlas;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Metadata describing this atlas, write it next to the atlas image to load later with {@link SDFAtlas}
|
|
240
|
+
*/
|
|
241
|
+
toMetadata(atlasPath) {
|
|
242
|
+
const glyphs = {};
|
|
243
|
+
for (const [char, glyph] of this.glyphs) {
|
|
244
|
+
glyphs[char] = glyph;
|
|
245
|
+
}
|
|
246
|
+
return {
|
|
247
|
+
atlas: atlasPath,
|
|
248
|
+
color: this.color.toHex(),
|
|
249
|
+
alphabet: this.alphabet,
|
|
250
|
+
fontSize: this.fontSize,
|
|
251
|
+
buffer: this.buffer,
|
|
252
|
+
ascent: this.ascent,
|
|
253
|
+
halo: this.halo,
|
|
254
|
+
gamma: this.gamma,
|
|
255
|
+
glyphs
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
// cli.ts
|
|
261
|
+
var TinySDFHeadless = class extends TinySDF2 {
|
|
262
|
+
/**
|
|
263
|
+
* Overrides the built in canvas creation to use the skia canvas for headless generation
|
|
264
|
+
*/
|
|
265
|
+
_createCanvas(size) {
|
|
266
|
+
return new Canvas(size, size);
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
var [fontFile, fontFamily, outName, fontSizeArg, startHex, endHex] = process.argv.slice(2);
|
|
270
|
+
if (!fontFile || !fontFamily || !outName) {
|
|
271
|
+
console.error("Usage: ex-generate-sdf <font-file> <font-family> <out-name> [font-size=64] [alphabet-start-hex=20 alphabet-end-hex=7E]");
|
|
272
|
+
process.exit(1);
|
|
273
|
+
}
|
|
274
|
+
var fontSize = fontSizeArg ? Number(fontSizeArg) : 64;
|
|
275
|
+
var alphabet = [parseInt(startHex ?? "20", 16), parseInt(endHex ?? "7E", 16)];
|
|
276
|
+
var sdfFont = new SDFFont({
|
|
277
|
+
fontFile,
|
|
278
|
+
fontFamily,
|
|
279
|
+
alphabet: [alphabet],
|
|
280
|
+
fontSize,
|
|
281
|
+
canvasProvider: (width, height) => new Canvas(width, height),
|
|
282
|
+
sdfProvider: (options) => new TinySDFHeadless(options),
|
|
283
|
+
fontSourceLoader: async (file, family) => {
|
|
284
|
+
FontLibrary.use(family, [file]);
|
|
285
|
+
},
|
|
286
|
+
imageDataProvider: (width, height) => new ImageData2(width, height)
|
|
287
|
+
});
|
|
288
|
+
await sdfFont.load();
|
|
289
|
+
var atlasFile = `${outName}.png`;
|
|
290
|
+
var metadataFile = `${outName}.json`;
|
|
291
|
+
sdfFont.atlas.toFileSync(atlasFile);
|
|
292
|
+
var metadata = sdfFont.toMetadata("./" + path.basename(atlasFile));
|
|
293
|
+
fs.writeFileSync(metadataFile, JSON.stringify(metadata, null, 2));
|
|
294
|
+
console.log(`wrote ${atlasFile} (${sdfFont.atlas.width}x${sdfFont.atlas.height}, ${sdfFont.glyphs.size} glyphs) and ${metadataFile}`);
|