Skip to content

Fonts preloading is handled incorrectly #587

@HoshinoKoji

Description

@HoshinoKoji

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions