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.68k stars 3.33k forks source link

Add Reference to Default Font object #7193

Open blackboxlogic opened 2 months ago

blackboxlogic commented 2 months ago

Increasing access

Proposal would simplify working with fonts.

Most appropriate sub-area of p5.js?

Feature request details

To use textBounds() or textToPoints(), you first need to call loadFont() to get a reference to a p5.Font object. Creating a reference like defaultFont or currentFont would bypass the need to find a font file, include it in your project, and load it if the user is happy with the default p5js font.

welcome[bot] commented 2 months ago

Welcome! 👋 Thanks for opening your first issue here! And to ensure the community is able to respond to your issue, please make sure to fill out the inputs in the issue forms. Thank you!

jeanetteandrews commented 2 months ago

Hi! A quick alternative is to load a URL to a remote font, like this: font = loadFont('https://fonts.gstatic.com/s/roboto/v20/KFOmCnqEu92Fr1Mu4mxP.ttf'); This way you won't need to download the font file and include it in your project.

However I found that some URLs return Error: Unsupported OpenType signature wOF2: https://editor.p5js.org/jeanetteandrews/sketches/9qDCQXgG4 I'm not sure if this is a bug or is due to browser security.

dhowe commented 2 months ago

@jeanetteandrews this is likely a valid error from the opentype library - not all fonts can be parsed by opentype

dhowe commented 2 months ago

To use textBounds() or textToPoints(), you first need to call loadFont() to get a reference to a p5.Font object. Creating a reference like defaultFont or currentFont would bypass the need to find a font file, include it in your project, and load it if the user is happy with the default p5js font.

This is a good suggestion - there are issues to be considered with a) the size that it adds to the package, and b) the licensing for this 'default' font

blackboxlogic commented 2 months ago

I thought this could be possible without the p5js developers adding a whole font file. Since a 'text("hi", 0, 0)' works without referencing a font, it seems a default font gets acquired either from the browser or the operating system, and that same font could be exposed by reference.

dhowe commented 2 months ago

This default font is a font supplied by the browser... we can use it, but we don't get all the font data that would be needed to create a p5.Font object (for example, the paths for each glyph).

This was a compromise that allowed for simple uses (drawing text) but didn't t add to the size of the package. However its something we could potentially revisit in the new version, assuming there was a small enough font with appropriate licensing (of course the font's size is often proportional to the number of characters it supports, which may well raise other access-related issues)