Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I import a font in Layabox from FairyGUI #63

Open
skymen opened this issue Nov 14, 2020 · 2 comments
Open

How do I import a font in Layabox from FairyGUI #63

skymen opened this issue Nov 14, 2020 · 2 comments

Comments

@skymen
Copy link

skymen commented Nov 14, 2020

I have a FairyGUI project that uses a font (Changa One). I have it in both .ttf and .woff formats.
When I publish to Laya, the font isn't published with it and so the layabox project doesn't contain the font and fallsback to system default on other devices.

I tried dynamically importing the font in Laya, but it did not work :/

async function loadFont(name, url) {
	return new Promise((resolve, reject) => {
		var font = new (self as any).FontFace(name, `url(${url})`);
		font.load().then(function(loaded_face) {
			(document as any).fonts.ready.fonts.add(loaded_face);
			document.body.style.fontFamily = `"${name}, Arial`;
			console.log(`Font ${name} has been loaded`);
			resolve()
		}).catch(function(error) {
			console.error(error);
			reject();
		});
	});
}
loadFont("Changa One", "fonts/Changa-Bold.woff").then(()=> {
	new Main();
});

Any idea of how to make this work? I'm kinda lost and all the (accurate and detailed) documentation for laya is in chinese, and I can't make an account on the laya forum because wechat won't let me.

@xiaoguzhu
Copy link
Member

put these styles in html:
image

then you can set fgui.UIConfig.defaultFont = 'hu'

@skymen
Copy link
Author

skymen commented Nov 14, 2020

Thanks for the answer :)
I also realised why my code did not work: the Typescript compiler failed to parse the async keyword and Layabox never told me!

in the end, I have this piece of code that seems to work, in case anyone else might need it

function loadFont(name, url, isBold, isItalic) {
	return new Promise((resolve, reject) => {
		let descriptors:any = {family: name}
		if (isBold) descriptors.weight = "bold"
		if (isItalic) descriptors.style = "italic"
		var font = new (self as any).FontFace(name, `url(${url})`, descriptors);
		font.load().then(function(loaded_face) {
			(document as any).fonts.add(loaded_face);
			console.log(`Font ${name} has been loaded (${url})`);
			resolve()
		}).catch(function(error) {
			console.error(error);
			// This means the game will never run if one of the fonts fail to load.
			// Alternatively, resolve anyway to keep the game running in case of failure.
			// You can also use Promise.allSettled, but at the moment it is not supported in every browsers
			reject();
		});
	});
}
Promise.all([
	loadFont("Changa One", "fonts/Changa-Regular.woff2", false, false),
	loadFont("Changa One", "fonts/Changa-Italic.woff2", false, true),
	loadFont("Changa One", "fonts/Changa-Bold.woff2", true, false)
]).then(()=> {
	new Main();
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants