processing / p5.js

p5.js is a client-side JS platform that empowers artists, designers, students, and anyone to learn to code and express themselves creatively on the web. It is based on the core principles of Processing. http://twitter.com/p5xjs —
http://p5js.org/
GNU Lesser General Public License v2.1
21.11k stars 3.22k forks source link

Fonts, Variable weight #5607

Open hellonearthis opened 2 years ago

hellonearthis commented 2 years ago

Increasing Access

Unsure

Most appropriate sub-area of p5.js?

Feature enhancement details

The current text() function doesn't allow for font weight on variable fonts.

The current canvas usage is like below, with a fontweight set to 500:

var c=document.getElementById('myCanvas');
var ctx=c.getContext('2d');
ctx.font='500 30px Arial';
ctx.fillText('Hello World', 10,30);

Thanks

alec-kr commented 2 years ago

Hey @hellonearthis, I would be happy to work on this issue.

Are you suggesting that we add a "fontWeight" attribute to the text() function? Example:

text(str, x, y, fontWeight, [x2], [y2])

Or should a fontWeight() function be added instead? Example:

fontWeight(500);
text("Hello World!", 10, 10, 70, 80)
dhowe commented 2 years ago

Also need to consider that a fontWeight param or function will have no effect on user-supplied fonts (open-type, etc.)

But in any case I would def. vote against: text(str, x, y, fontWeight, [x2], [y2]), as it breaks backwards compatibility

Maybe I am misunderstanding, but I'm also not seeing any difference between the two cases below:

image
function setup() {
  let c = createCanvas(400, 400);
  background(0);

  let ctx = c.elt.getContext('2d');
  ctx.font = "500 30px Arial";
  ctx.fillText("Hello World", 10, 30);

  fill(255);
  textFont("500 30px Arial");
  text("Hello World", 10, 60)
}
alec-kr commented 2 years ago

consider that a fontWeight param or function will have no effect on user-supplied fonts (open-type, etc.)

Yes, this function will only be able to manipulate built-in fonts.

I'm also not seeing any difference between the two cases below

I'm having the same issue here. I think @hellonearthis is referring to a function that can adjust the thickness of the font's letters. However, the example code provided does not seem to replicate the issue.

limzykenneth commented 2 years ago

I think with limited support (only built-in fonts) I'm less inclined to include this feature as is.

One is that it create special cases that can be easily missed and potentially create confusion from users about why their custom font doesn't work with font weight. Two is that even in the case that a developer is using built-in font, there is no guarantee that their users will have the same font and same variable weight support available in their system.

alec-kr commented 2 years ago

@limzykenneth Do you think this issue should be closed since it would not be practical to implement?

limzykenneth commented 2 years ago

I would prefer such a feature be available for user loaded font so unless than can be implemented I would suggest shelving this for now.

arthurcloche commented 10 months ago

Hey, i was just pointed to this by @davepagurek.

I did some extensive explorations and i came with this little library to handle variable fonts. p5.variableFont I think it provides some answer to some of the questions above and shows some directions on how to modify the current p5 functions without breaking anything and be backward compatible ( as far back as the implementation of the current textFont() function, which is where most of the font transformation takes place ).

davepagurek commented 10 months ago

For some extra context, this would help create CSS font definitions out of loadFont() fonts, allowing the 2D context to change the variables in the font when drawing it. It looks like that would unlock variable font support for the 2D renderer for all the ways you can load a font into p5 right now, which is great!

To make that also work in WebGL it looks like we'd still need to update opentype.js (or an equivalent) to be able to get the path information for a font given the values for variables. The p5.variableFont library already starts reading into the font tables to see what variables exist, so it looks like a great jumping off point for anyone who might be interested in trying to add that to opentype.js.

munusshih commented 10 months ago

This is great! @amehowc I'm so excited and will definitely use it for the future.

I feel like this is probably the best workaround so far as I too looked into the way p5 rendered fonts and noticed there are two ways (one through CSS @font-face, one through opentype.js). Initially I created this sketch here where I just created many html canvas and each control their css for font-variation-settings and render them back on the main p5 canvas.

But your workaround is definitely the better solution! I don't think, however, this should be integrated into the main p5 library though. I think before p5 sort out its relationship with opentype.js this should remain a separate community library!