@lightningjs/blits 0.9.9 → 0.10.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/CHANGELOG.md +14 -0
- package/boilerplate/common/public/fonts/Lato-Regular.ttf +0 -0
- package/boilerplate/common/public/fonts/Raleway-ExtraBold.ttf +0 -0
- package/package.json +3 -4
- package/src/components/FPScounter.js +208 -112
- package/src/engines/L3/fontLoader.js +16 -2
- package/src/lib/codegenerator/generator.js +12 -3
- package/vite/msdfGenerator.js +52 -53
- package/boilerplate/common/public/fonts/Lato-Regular.msdf.json +0 -5383
- package/boilerplate/common/public/fonts/Lato-Regular.msdf.png +0 -0
- package/boilerplate/common/public/fonts/Raleway-ExtraBold.msdf.json +0 -10838
- package/boilerplate/common/public/fonts/Raleway-ExtraBold.msdf.png +0 -0
package/vite/msdfGenerator.js
CHANGED
|
@@ -1,7 +1,25 @@
|
|
|
1
1
|
import path from 'path'
|
|
2
2
|
import * as fs from 'fs'
|
|
3
|
-
import
|
|
3
|
+
import { genFont, setGeneratePaths } from '@lightningjs/msdf-generator'
|
|
4
|
+
import { adjustFont } from '@lightningjs/msdf-generator/adjustFont'
|
|
4
5
|
|
|
6
|
+
class TaskQueue {
|
|
7
|
+
constructor() {
|
|
8
|
+
this.currentTask = Promise.resolve()
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
async enqueue(task) {
|
|
12
|
+
// Wait for the current task to complete before starting a new one
|
|
13
|
+
this.currentTask = this.currentTask.then(task, this.handleError)
|
|
14
|
+
await this.currentTask
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
handleError(error) {
|
|
18
|
+
console.error('Error during task execution:', error)
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const fontGenerationQueue = new TaskQueue()
|
|
5
23
|
let config
|
|
6
24
|
|
|
7
25
|
export default function () {
|
|
@@ -15,14 +33,13 @@ export default function () {
|
|
|
15
33
|
config = resolvedConfig
|
|
16
34
|
buildOutputPath = config.build.outDir
|
|
17
35
|
publicDir = path.join(config.root, 'public')
|
|
18
|
-
msdfOutputDir = path.resolve(config.root, 'node_modules', '.tmp-msdf-fonts')
|
|
36
|
+
msdfOutputDir = path.resolve(config.root, 'node_modules', '.tmp-msdf-fonts-v2')
|
|
19
37
|
},
|
|
20
38
|
async configureServer(server) {
|
|
21
39
|
server.middlewareMode = true
|
|
22
40
|
server.middlewares.use('/', async (req, res, next) => {
|
|
23
41
|
const file = req.url.substring(req.url.lastIndexOf('/') + 1)
|
|
24
42
|
const match = file.match(/(.+)\.msdf\.(json|png)/)
|
|
25
|
-
|
|
26
43
|
// msdf font request
|
|
27
44
|
if (match) {
|
|
28
45
|
const fontPath = path.dirname(req.url.split('?')[0]).replace(/^\//, '')
|
|
@@ -42,21 +59,28 @@ export default function () {
|
|
|
42
59
|
)
|
|
43
60
|
const mimeType = ext === 'png' ? 'image/png' : 'application/json'
|
|
44
61
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
if (
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
62
|
+
await fontGenerationQueue.enqueue(async () => {
|
|
63
|
+
if (!fs.existsSync(generatedFontFile)) {
|
|
64
|
+
// Check if generation is needed
|
|
65
|
+
console.log(`\nGenerating ${targetDir}/${fontName}.msdf.${ext}`)
|
|
66
|
+
await generateSDF(fontFile, path.join(targetDir))
|
|
67
|
+
}
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
if (fs.existsSync(generatedFontFile)) {
|
|
71
|
+
const fileContent = fs.readFileSync(generatedFontFile)
|
|
72
|
+
|
|
73
|
+
// Check if headers have already been sent
|
|
74
|
+
if (!res.headersSent) {
|
|
75
|
+
res.setHeader('Content-Type', mimeType)
|
|
76
|
+
res.setHeader('Cache-Control', 'public, max-age=31536000, immutable')
|
|
77
|
+
res.end(fileContent)
|
|
78
|
+
} else {
|
|
79
|
+
// this should never happen except some edge cases
|
|
80
|
+
console.error(`ERROR: Headers already sent for ${req.url}`, res.getHeaders())
|
|
81
|
+
}
|
|
57
82
|
} else {
|
|
58
|
-
//
|
|
59
|
-
console.error(`ERROR: Headers already sent for ${req.url}`, res.getHeaders())
|
|
83
|
+
next() // Handle case where generation might have failed
|
|
60
84
|
}
|
|
61
85
|
} else {
|
|
62
86
|
next() // ttf file does not exist
|
|
@@ -71,7 +95,6 @@ export default function () {
|
|
|
71
95
|
},
|
|
72
96
|
// after build ends, the first hook is renderStart where we can modify the output
|
|
73
97
|
async renderStart() {
|
|
74
|
-
// Find all TTF files under publicDir
|
|
75
98
|
const ttfFiles = findAllTtfFiles(publicDir)
|
|
76
99
|
|
|
77
100
|
for (const ttfFile of ttfFiles) {
|
|
@@ -83,7 +106,7 @@ export default function () {
|
|
|
83
106
|
// Check if MSDF files are generated, if not, generate them
|
|
84
107
|
if (!fs.existsSync(msdfJsonPath) || !fs.existsSync(msdfPngPath)) {
|
|
85
108
|
console.log(`Generating missing MSDF files for ${ttfFile}`)
|
|
86
|
-
await
|
|
109
|
+
await generateSDF(ttfFile, path.join(msdfOutputDir, relativePath))
|
|
87
110
|
}
|
|
88
111
|
}
|
|
89
112
|
|
|
@@ -94,40 +117,16 @@ export default function () {
|
|
|
94
117
|
}
|
|
95
118
|
}
|
|
96
119
|
|
|
97
|
-
const
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
(err, textures, font) => {
|
|
108
|
-
if (err) {
|
|
109
|
-
console.error(err)
|
|
110
|
-
reject(err)
|
|
111
|
-
} else {
|
|
112
|
-
textures.forEach((texture) => {
|
|
113
|
-
try {
|
|
114
|
-
fs.writeFileSync(path.resolve(fontDir, `${fontName}.msdf.png`), texture.texture)
|
|
115
|
-
} catch (e) {
|
|
116
|
-
console.error(e)
|
|
117
|
-
reject(e)
|
|
118
|
-
}
|
|
119
|
-
})
|
|
120
|
-
try {
|
|
121
|
-
fs.writeFileSync(path.resolve(fontDir, `${fontName}.msdf.json`), font.data)
|
|
122
|
-
resolve()
|
|
123
|
-
} catch (e) {
|
|
124
|
-
console.error(err)
|
|
125
|
-
reject(e)
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
)
|
|
130
|
-
})
|
|
120
|
+
const generateSDF = async (inputFilePath, outputDirPath) => {
|
|
121
|
+
// Ensure the destination directory exists
|
|
122
|
+
fs.mkdirSync(outputDirPath, { recursive: true })
|
|
123
|
+
|
|
124
|
+
setGeneratePaths(path.dirname(inputFilePath), outputDirPath)
|
|
125
|
+
|
|
126
|
+
let font = await genFont(path.basename(inputFilePath), 'msdf')
|
|
127
|
+
|
|
128
|
+
if (font) await adjustFont(font)
|
|
129
|
+
else console.error('Failed to generate MSDF file')
|
|
131
130
|
}
|
|
132
131
|
|
|
133
132
|
// finds all TTF files in a directory
|