Skip to content

Commit

Permalink
fix two minor bugs: font-name discovery, textProps circular ref
Browse files Browse the repository at this point in the history
  • Loading branch information
dhowe committed Dec 29, 2024
1 parent 77e89ee commit e98a609
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
8 changes: 5 additions & 3 deletions src/type/p5.Font.js
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ function font(p5, fn) {
// load the raw font bytes
let result = await fn.loadBytes(path);
//console.log('result:', result);

if (!result) {
throw Error('Failed to load font data');
}
Expand All @@ -597,11 +597,13 @@ function font(p5, fn) {

} catch (err) {
// failed to parse the font, load it as a simple FontFace
let ident = name || path.substring(path.lastIndexOf('/') + 1);
let ident = name || path
.substring(path.lastIndexOf('/') + 1)
.replace(/\.[^/.]+$/, "");
console.warn(`WARN: No glyph data for '${ident}', retrying as FontFace`);
try {
// create a FontFace object and pass it to p5.Font
pfont = await create(this, name, path, descriptors);
pfont = await create(this, ident, path, descriptors);
}
catch (err) {
if (error) error(err);
Expand Down
23 changes: 16 additions & 7 deletions src/type/text2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ function text2d(p5, fn) {
textLeading: { default: 15 },
textSize: { default: 12 },
textWrap: { default: fn.WORD },

// added v2.0
fontStretch: { default: fn.NORMAL, isShorthand: true }, // font-stretch: { default: normal | ultra-condensed | extra-condensed | condensed | semi-condensed | semi-expanded | expanded | extra-expanded | ultra-expanded }
fontWeight: { default: fn.NORMAL, isShorthand: true }, // font-stretch: { default: normal | ultra-condensed | extra-condensed | condensed | semi-condensed | semi-expanded | expanded | extra-expanded | ultra-expanded }
lineHeight: { default: fn.NORMAL, isShorthand: true }, // line-height: { default: normal | number | length | percentage }
Expand Down Expand Up @@ -456,17 +454,28 @@ function text2d(p5, fn) {
return this._pInst;
}

// getter: get props from this.textDrawingContext()
// getter: get props from drawingContext
let context = this.textDrawingContext();
properties = ContextTextProps.reduce((props, p) => {
props[p] = this.textDrawingContext()[p];
props[p] = context[p];
return props;
}, {});

// add renderer.states props
// add renderer props
Object.keys(RendererTextProps).forEach(p => {
properties[p] = this.states[p];
if (RendererTextProps[p]?.type === 'Context2d') {
properties[p] = this.textDrawingContext()[p];
properties[p] = context[p];
}
else { // a renderer.states property
if (p === 'textFont') {
// avoid circular ref. inside textFont
properties[p] = Object.assign({}, this._currentTextFont());
delete properties[p]._pInst;
console.log('textFont: ', properties[p]);
}
else {
properties[p] = this.states[p];
}
}
});

Expand Down

0 comments on commit e98a609

Please sign in to comment.