Skip to content

Commit 7235640

Browse files
committed
move font.verticalAlign to _verticalAlign, minor refactors
1 parent 5b4a87b commit 7235640

File tree

2 files changed

+40
-40
lines changed

2 files changed

+40
-40
lines changed

src/type/p5.Font.js

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -54,45 +54,6 @@ function font(p5, fn) {
5454
this.face = fontFace;
5555
}
5656

57-
verticalAlign(size) {
58-
const { sCapHeight } = this.data?.['OS/2'] || {};
59-
const { unitsPerEm = 1000 } = this.data?.head || {};
60-
const { ascender = 0, descender = 0 } = this.data?.hhea || {};
61-
const current = ascender / 2;
62-
const target = (sCapHeight || (ascender + descender)) / 2;
63-
const offset = target - current;
64-
return offset * size / unitsPerEm;
65-
}
66-
67-
variations() {
68-
let vars = {};
69-
if (this.data) {
70-
let axes = this.face?.axes;
71-
if (axes) {
72-
axes.forEach(ax => {
73-
vars[ax.tag] = ax.value;
74-
});
75-
}
76-
}
77-
fontFaceVariations.forEach(v => {
78-
let val = this.face[v];
79-
if (val !== 'normal') {
80-
vars[v] = vars[v] || val;
81-
}
82-
});
83-
return vars;
84-
}
85-
86-
metadata() {
87-
let meta = this.data?.name || {};
88-
for (let p in this.face) {
89-
if (!/^load/.test(p)) {
90-
meta[p] = meta[p] || this.face[p];
91-
}
92-
}
93-
return meta;
94-
}
95-
9657
fontBounds(str, x, y, width, height, options) {
9758
({ width, height, options } = this._parseArgs(width, height, options));
9859
let renderer = options?.graphics?._renderer || this._pInst._renderer;
@@ -204,6 +165,35 @@ function font(p5, fn) {
204165
return geom;
205166
}
206167

168+
variations() {
169+
let vars = {};
170+
if (this.data) {
171+
let axes = this.face?.axes;
172+
if (axes) {
173+
axes.forEach(ax => {
174+
vars[ax.tag] = ax.value;
175+
});
176+
}
177+
}
178+
fontFaceVariations.forEach(v => {
179+
let val = this.face[v];
180+
if (val !== 'normal') {
181+
vars[v] = vars[v] || val;
182+
}
183+
});
184+
return vars;
185+
}
186+
187+
metadata() {
188+
let meta = this.data?.name || {};
189+
for (let p in this.face) {
190+
if (!/^load/.test(p)) {
191+
meta[p] = meta[p] || this.face[p];
192+
}
193+
}
194+
return meta;
195+
}
196+
207197
static async list(log = false) { // tmp
208198
if (log) {
209199
console.log('There are', document.fonts.size, 'font-faces\n');
@@ -224,6 +214,16 @@ function font(p5, fn) {
224214
}
225215

226216
/////////////////////////////// HELPERS ////////////////////////////////
217+
218+
_verticalAlign(size) {
219+
const { sCapHeight } = this.data?.['OS/2'] || {};
220+
const { unitsPerEm = 1000 } = this.data?.head || {};
221+
const { ascender = 0, descender = 0 } = this.data?.hhea || {};
222+
const current = ascender / 2;
223+
const target = (sCapHeight || (ascender + descender)) / 2;
224+
const offset = target - current;
225+
return offset * size / unitsPerEm;
226+
}
227227

228228
/*
229229
Returns an array of line objects, each containing { text, x, y, glyphs: [ {g, path} ] }

src/type/text2d.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1250,7 +1250,7 @@ function text2d(p5, fn) {
12501250
console.warn(`${textBaseline} is not supported in WebGL mode.`); // FES?
12511251
break;
12521252
}
1253-
yOff += this.states.textFont.font?.verticalAlign(textSize) || 0;
1253+
yOff += this.states.textFont.font?._verticalAlign(textSize) || 0;
12541254
dataArr.forEach(ele => ele.y += yOff);
12551255
return dataArr;
12561256
}

0 commit comments

Comments
 (0)