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

Fonts preloading is handled incorrectly #587

Open
HoshinoKoji opened this issue Nov 20, 2023 · 0 comments
Open

Fonts preloading is handled incorrectly #587

HoshinoKoji opened this issue Nov 20, 2023 · 0 comments

Comments

@HoshinoKoji
Copy link

In src/core/ServerManager.js, preloading of fonts is specifically handled in its method ‎ServerManager._downloadResources, which is quoted as follows, starting from Line 1115:

// start loading fonts:
for (const name of fontResources)
{
	const pathStatusData = this._resources.get(name);
	pathStatusData.status = ServerManager.ResourceStatus.DOWNLOADING;
	this.emit(ServerManager.Event.RESOURCE, {
		message: ServerManager.Event.DOWNLOADING_RESOURCE,
		resource: name,
	});

	const pathExtension = pathStatusData.path.toLowerCase().split(".").pop();
	try
	{
		const newFont = await new FontFace(name, `url('${pathStatusData.path}') format('${pathExtension}')`).load();
		document.fonts.add(newFont);

		++this._nbLoadedResources;

		pathStatusData.status = ServerManager.ResourceStatus.DOWNLOADED;
		this.emit(ServerManager.Event.RESOURCE, {
			message: ServerManager.Event.RESOURCE_DOWNLOADED,
			resource: name,
		});

		if (this._nbLoadedResources === resources.size)
		{
			this.setStatus(ServerManager.Status.READY);
			this.emit(ServerManager.Event.RESOURCE, {
				message: ServerManager.Event.DOWNLOAD_COMPLETED,
			});
		}
	}
        catch (error)
	{
		console.error(error);
		this.setStatus(ServerManager.Status.ERROR);
		pathStatusData.status = ServerManager.ResourceStatus.ERROR;
		throw Object.assign(response, {
			error: `unable to download resource: ${name}: ${error}`
		});
	}
}

Bug is introduced in Line 1128:

const newFont = await new FontFace(name, `url('${pathStatusData.path}') format('${pathExtension}')`).load();

This line tries to use its name in resources as font family name, and the extension as font format. For example, if the font to be preloaded is fonts/OpenSans-Bold.ttf, it attempts to use the full path fonts/OpenSans-Bold.ttf as font family name, which is illegal. The format specified format('ttf') would also be wrong, since format('truetype') is expected in this case.

For an independent project this could be easily fixed, but I have no idea how this should be fixed in the context of PsychoJS and the builder project. I hope this would help.

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

1 participant