<!DOCTYPE html>
<html>
<body>
<button type="button" onclick='UseLocalFontsButton()'>Click Me!</button>
<script>
async function UseLocalFontsButton() {
try {
const array = await self.queryLocalFonts();
for (const font of array) {
// blob() returns a Blob containing the bytes of the font.
const bytes = await font.blob();
console.log(`bytes.size = ${bytes.size}`);
console.log(`bytes.type = ${bytes.type}`);
// process font
};
}
catch (e) {
console.warn(`Error: ${e.message}`);
}
};
</script>
</body>
</html>
Here I am getting local fonts and processing it. It works but I need to move font processing part to worker thread. I cant move everything because queryLocalFonts
only works on main thread.
Here is my try:
<!DOCTYPE html>
<html>
<body>
<button type="button" onclick='UseLocalFontsButton()'>Click Me!</button>
<script>
async function UseLocalFontsButton() {
try {
const array = await self.queryLocalFonts();
let worker = new Worker('worker.js');
worker.postMessage(JSON.stringify(array));
}
catch (e) {
console.warn(`Error: ${e.message}`);
}
};
</script>
</body>
</html>
And the worker:
(function () {
onmessage = async function handleMessageFromMain(msg)
{
var array = JSON.parse(msg.data);
console.log('array = ', array);
try {
for (const font of array) {
// blob() returns a Blob containing the bytes of the font.
const bytes = await font.blob();
console.log(`bytes.size = ${bytes.size}`);
console.log(`bytes.type = ${bytes.type}`);
// process font
};
} catch (e) {
console.warn(`Error: ${e.message}`);
}
};
})();
I am getting error: Error: font.blob is not a function
. Looks like font object is not properly copied to worker thread. Can you hint how that can be done?