Skip to content

Commit

Permalink
move font.verticalAlign to _verticalAlign, minor refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
dhowe committed Dec 21, 2024
1 parent 5b4a87b commit 7235640
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 40 deletions.
78 changes: 39 additions & 39 deletions src/type/p5.Font.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,45 +54,6 @@ function font(p5, fn) {
this.face = fontFace;
}

verticalAlign(size) {
const { sCapHeight } = this.data?.['OS/2'] || {};
const { unitsPerEm = 1000 } = this.data?.head || {};
const { ascender = 0, descender = 0 } = this.data?.hhea || {};
const current = ascender / 2;
const target = (sCapHeight || (ascender + descender)) / 2;
const offset = target - current;
return offset * size / unitsPerEm;
}

variations() {
let vars = {};
if (this.data) {
let axes = this.face?.axes;
if (axes) {
axes.forEach(ax => {
vars[ax.tag] = ax.value;
});
}
}
fontFaceVariations.forEach(v => {
let val = this.face[v];
if (val !== 'normal') {
vars[v] = vars[v] || val;
}
});
return vars;
}

metadata() {
let meta = this.data?.name || {};
for (let p in this.face) {
if (!/^load/.test(p)) {
meta[p] = meta[p] || this.face[p];
}
}
return meta;
}

fontBounds(str, x, y, width, height, options) {
({ width, height, options } = this._parseArgs(width, height, options));
let renderer = options?.graphics?._renderer || this._pInst._renderer;
Expand Down Expand Up @@ -204,6 +165,35 @@ function font(p5, fn) {
return geom;
}

variations() {
let vars = {};
if (this.data) {
let axes = this.face?.axes;
if (axes) {
axes.forEach(ax => {
vars[ax.tag] = ax.value;
});
}
}
fontFaceVariations.forEach(v => {
let val = this.face[v];
if (val !== 'normal') {
vars[v] = vars[v] || val;
}
});
return vars;
}

metadata() {
let meta = this.data?.name || {};
for (let p in this.face) {
if (!/^load/.test(p)) {
meta[p] = meta[p] || this.face[p];
}
}
return meta;
}

static async list(log = false) { // tmp
if (log) {
console.log('There are', document.fonts.size, 'font-faces\n');
Expand All @@ -224,6 +214,16 @@ function font(p5, fn) {
}

/////////////////////////////// HELPERS ////////////////////////////////

_verticalAlign(size) {
const { sCapHeight } = this.data?.['OS/2'] || {};
const { unitsPerEm = 1000 } = this.data?.head || {};
const { ascender = 0, descender = 0 } = this.data?.hhea || {};
const current = ascender / 2;
const target = (sCapHeight || (ascender + descender)) / 2;
const offset = target - current;
return offset * size / unitsPerEm;
}

/*
Returns an array of line objects, each containing { text, x, y, glyphs: [ {g, path} ] }
Expand Down
2 changes: 1 addition & 1 deletion src/type/text2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -1250,7 +1250,7 @@ function text2d(p5, fn) {
console.warn(`${textBaseline} is not supported in WebGL mode.`); // FES?
break;
}
yOff += this.states.textFont.font?.verticalAlign(textSize) || 0;
yOff += this.states.textFont.font?._verticalAlign(textSize) || 0;
dataArr.forEach(ele => ele.y += yOff);
return dataArr;
}
Expand Down

0 comments on commit 7235640

Please sign in to comment.