processing / processing/p5.js

p5 loadFont() and its dilemma with opentype.js

Open
#6,391 14 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Area:Typography Discussion
Dominant language
JavaScript
Stars
24k
Forks
3.8k
Avg merge
3d 16h
Merged PRs (30d)
25

Description

Topic

As @amehowc mentioned in his p5.varaibleFont library
There are two primary methods for loading and using fonts in p5.js. One involves using loadFont(), and the other is by defining a font either through CSS with a @font-face tag or importing it via an API like the Google Fonts API. Both methods have the same end result when using text() in the 2D context. However, differences emerge when you delve into font data functions like font.textToPoints() or font.getBounds(), or when using the WEBGL context.

Basically it could be drawn into a table like this:

Loading Through WEBGL text() 2D text() variable font using CSS variation-font-settings native Canvas API p5.font (like textToPoint)
loadFont(), relying on opentype.js O O X X O
CSS @font-face (such as google fonts) X O O O X

O = supported feature, X = unsupported feature

The two ways of loading fonts create different supports in p5 for these typographic related functions.

Supports for native Canvas API
  • If you rely on using opentype.js to parse through the font, you get all the vertices of the font, and can draw them even in WEBGL mode.
  • However, you will lose important supports like controlling the texts with native Canvas API (because Canvas doesn't recognize this as text anymore, but vertices with text shapes). This is really major because p5 inherently is built on top of HTML Canvas, which is a relatively new element in web technologies. Many things that have long been requested by the community are slowly being added in, such as letter-spacing or fontStretch. We might be reinventing the wheels here if we decide to not have it support native Canvas APIs.
The loadingFont function

Basically if you import font using the loadFont() function, it will still create a CSS @font-face for you in the code as shown here in loading_displaying.js:

https://github.com/processing/p5.js/blob/36437b3d68bc651b8695a401159eef36aeec5881/src/typography/loading_displaying.js#L125-L133

However, it will not be referencing it, so I'm really unsure if this part of the code is even working.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with src/typography/loading_displaying.js at the linked lines and trace how loadFont() creates and uses its CSS @font-face entry. Compare the observed behavior with the issue's support table across 2D, WEBGL, Canvas, variable fonts, and p5 font functions; done requires an agreed, explicit direction for these loading paths.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend, web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.