Skip to content

Commit 514df2a

Browse files
authored
Merge pull request #8317 from processing/fix/font-align
Text alignment fixes
2 parents 48737a5 + 9edd8e5 commit 514df2a

File tree

61 files changed

+13
-19
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+13
-19
lines changed

src/type/p5.Font.js

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -737,25 +737,14 @@ export class Font {
737737

738738
/////////////////////////////// HELPERS ////////////////////////////////
739739

740-
_verticalAlign(size) {
741-
const { sCapHeight } = this.data?.['OS/2'] || {};
742-
const { unitsPerEm = 1000 } = this.data?.head || {};
743-
const { ascender = 0, descender = 0 } = this.data?.hhea || {};
744-
const current = ascender / 2;
745-
const target = (sCapHeight || (ascender + descender)) / 2;
746-
const offset = target - current;
747-
return offset * size / unitsPerEm;
748-
}
749-
750740
/*
751741
Returns an array of line objects, each containing { text, x, y, glyphs: [ {g, path} ] }
752742
*/
753743
_lineateAndPathify(str, x, y, width, height, options = {}) {
754744

755745
let renderer = options?.graphics?._renderer || this._pInst._renderer;
756-
757-
// save the baseline
758-
let setBaseline = renderer.drawingContext.textBaseline;
746+
renderer.push();
747+
renderer.textFont(this);
759748

760749
// lineate and compute bounds for the text
761750
let { lines, bounds } = renderer._computeBounds
@@ -772,8 +761,7 @@ export class Font {
772761
const axs = this._currentAxes(renderer);
773762
let pathsForLine = lines.map(l => this._lineToGlyphs(l, { scale, axs }));
774763

775-
// restore the baseline
776-
renderer.drawingContext.textBaseline = setBaseline;
764+
renderer.pop();
777765

778766
return pathsForLine;
779767
}
@@ -857,7 +845,7 @@ export class Font {
857845

858846
_position(renderer, lines, bounds, width, height) {
859847

860-
let { textAlign, textLeading } = renderer.states;
848+
let { textAlign, textLeading, textSize } = renderer.states;
861849
let metrics = this._measureTextDefault(renderer, 'X');
862850
let ascent = metrics.fontBoundingBoxAscent;
863851

src/type/textCore.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2688,6 +2688,13 @@ function textCore(p5, fn) {
26882688
return this._yAlignOffset(lineData, adjustedH);
26892689
};
26902690

2691+
p5.RendererGL.prototype._verticalAlignFont = function() {
2692+
const ctx = this.textDrawingContext();
2693+
const metrics = ctx.measureText('X');
2694+
return -metrics.alphabeticBaseline ||
2695+
(-metrics.fontBoundingBoxAscent + metrics.fontBoundingBoxDescent);
2696+
}
2697+
26912698
p5.RendererGL.prototype._yAlignOffset = function (dataArr, height) {
26922699

26932700
if (typeof height === 'undefined') {
@@ -2700,12 +2707,12 @@ function textCore(p5, fn) {
27002707
((textLeading - textSize) * (numLines - 1));
27012708
switch (textBaseline) { // drawingContext ?
27022709
case fn.TOP:
2703-
yOff = textSize;
2710+
yOff = this._verticalAlignFont();
27042711
break;
27052712
case fn.BASELINE:
27062713
break;
27072714
case textCoreConstants._CTX_MIDDLE:
2708-
yOff = -totalHeight / 2 + textSize + (height || 0) / 2;
2715+
yOff = (-totalHeight + textSize + (height || 0)) / 2 + this._verticalAlignFont();
27092716
break;
27102717
case fn.BOTTOM:
27112718
yOff = -(totalHeight - textSize) + (height || 0);
@@ -2714,7 +2721,6 @@ function textCore(p5, fn) {
27142721
console.warn(`${textBaseline} is not supported in WebGL mode.`); // FES?
27152722
break;
27162723
}
2717-
yOff += this.states.textFont.font?._verticalAlign(textSize) || 0; // Does this function exist?
27182724
dataArr.forEach(ele => ele.y += yOff);
27192725
return dataArr;
27202726
};
-40 Bytes
-109 Bytes
-109 Bytes
-17 Bytes

0 commit comments

Comments
 (0)