-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
textFont() not correctly using fonts with multi-word names #6652
Comments
I've verified this issue, though the problem doesn't have to do with spaces, rather the parsing of the font string by the browser, which doesn't like the leading "2" in the 3rd word of the font name (I'm guessing that it is trying to parse this as a font size and failing, which results in no change to the You can see the issue below: function setup() {
createCanvas(400, 400);
textFont("Press Start 2P");
console.log(drawingContext.font); // -> 12px sans-serif FAILS
textFont("Press Start P");
console.log(drawingContext.font); // -> 12px "Press Start P" OK
textFont("\"Press Start 2P\"");
console.log(drawingContext.font); // -> 12px "Press Start 2P" OK
} The solution, as @dipamsen discovered, is to wrap the font name in double-quotes, which appears to work in all cases, though I have not tested this extensively. I've provisionally made the change in this commit, which perhaps someone will test and make into a PR. There is another interesting issue that arises in this example, due to the fact the font-loading via css is not synchronous, which means that setting the font does NOT mean it can be immediately used available. For example, the following code will render with the wrong font, even though it has been set successfully, it has just not loaded yet (and this may be quite confusing for new users): function setup() {
createCanvas(400, 400);
textFont("\"Press Start 2P\"");
text("Hi There", 200, 100);
} I'm not sure how to solve this (the only solutions I can think of are rather convoluted/complex), but at very least it should be noted in the documentation. Also, I've noticed that the documentation for textFont does not mention using css-loaded fonts at all, so this should probably be updated to include the approach that @dipamsen uses in the editor link above... |
Most appropriate sub-area of p5.js?
p5.js version
1.9.0
Web browser and version
Google Chrome 120.0.6099.111 (Official Build) (64-bit)
Operating System
Windows 11
Steps to reproduce this
Steps:
While loading Google Fonts from their cdn, the font should be available to use through
textFont()
, but fonts having multi-word names are only usable if additional quotes are added to the font name.Example:
p5.js web editor example
(the font may not load the first time, run the sketch again to load the font properly)
The text was updated successfully, but these errors were encountered: