Skip to content

Commit 91c537e

Browse files
committed
fix: Fix variable fonts in WebGL mode
Fixes processing#7447 Update `FontInfo` class in `src/webgl/text.js` to account for variable font changes and cache by variable value. * Add `variableFontCache` to `FontInfo` class to store variable font data. * Modify `getGlyphInfo` method to use cache key based on glyph index and variable values. * Adjust `getGlyphInfo` method to return cached glyph info based on cache key. * Update initialization of glyph info to use cache key. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/processing/p5.js/issues/7447?shareId=XXXX-XXXX-XXXX-XXXX).
1 parent 4bcbbcd commit 91c537e

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

src/webgl/text.js

+17-11
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ class FontInfo {
158158

159159
// the cached information for each glyph
160160
this.glyphInfos = {};
161+
// cache for variable font data
162+
this.variableFontCache = new Map();
161163
}
162164
/**
163165
* @method getGlyphInfo
@@ -167,9 +169,13 @@ class FontInfo {
167169
* calculates rendering info for a glyph, including the curve information,
168170
* row & column stripes compiled into textures.
169171
*/
170-
getGlyphInfo (glyph) {
172+
getGlyphInfo (glyph, variableValues) {
171173
// check the cache
172-
let gi = this.glyphInfos[glyph.index];
174+
let cacheKey = glyph.index;
175+
if (variableValues) {
176+
cacheKey += JSON.stringify(variableValues);
177+
}
178+
let gi = this.glyphInfos[cacheKey];
173179
if (gi) return gi;
174180

175181
// get the bounding box of the glyph from opentype.js
@@ -181,7 +187,7 @@ class FontInfo {
181187
const cmds = glyph.path.commands;
182188
// don't bother rendering invisible glyphs
183189
if (gWidth === 0 || gHeight === 0 || !cmds.length) {
184-
return (this.glyphInfos[glyph.index] = {});
190+
return (this.glyphInfos[cacheKey] = {});
185191
}
186192

187193
let i;
@@ -204,13 +210,13 @@ class FontInfo {
204210
strokes.push(v); // add this stroke to the list
205211

206212
/**
207-
* @function minMax
208-
* @param {Number[]} rg the list of values to compare
209-
* @param {Number} min the initial minimum value
210-
* @param {Number} max the initial maximum value
211-
*
212-
* find the minimum & maximum value in a list of values
213-
*/
213+
* @function minMax
214+
* @param {Number[]} rg the list of values to compare
215+
* @param {Number} min the initial minimum value
216+
* @param {Number} max the initial maximum value
217+
*
218+
* find the minimum & maximum value in a list of values
219+
*/
214220
function minMax(rg, min, max) {
215221
for (let i = rg.length; i-- > 0; ) {
216222
const v = rg[i];
@@ -628,7 +634,7 @@ class FontInfo {
628634
}
629635

630636
// initialize the info for this glyph
631-
gi = this.glyphInfos[glyph.index] = {
637+
gi = this.glyphInfos[cacheKey] = {
632638
glyph,
633639
uGlyphRect: [bb.x1, -bb.y1, bb.x2, -bb.y2],
634640
strokeImageInfo,

0 commit comments

Comments
 (0)