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

textFont() not correctly using fonts with multi-word names #6652

Closed
1 of 17 tasks
dipamsen opened this issue Dec 24, 2023 · 1 comment · Fixed by #6667
Closed
1 of 17 tasks

textFont() not correctly using fonts with multi-word names #6652

dipamsen opened this issue Dec 24, 2023 · 1 comment · Fixed by #6667

Comments

@dipamsen
Copy link

dipamsen commented Dec 24, 2023

Most appropriate sub-area of p5.js?

  • Accessibility
  • Color
  • Core/Environment/Rendering
  • Data
  • DOM
  • Events
  • Image
  • IO
  • Math
  • Typography
  • Utilities
  • WebGL
  • Build Process
  • Unit Testing
  • Internalization
  • Friendly Errors
  • Other (specify if possible)

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)

@dipamsen dipamsen added the Bug label Dec 24, 2023
dhowe added a commit that referenced this issue Dec 24, 2023
@dhowe
Copy link
Contributor

dhowe commented Dec 24, 2023

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 drawingContext.font property).

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...

@dhowe dhowe changed the title textFont not correctly using google font having multi-word name textFont() not correctly using fonts with multi-word names Dec 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants