Skip to content

Commit 92642e4

Browse files
Exclude the text origin from actual bounding boxes
1 parent efd8243 commit 92642e4

3 files changed

Lines changed: 42 additions & 4 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
1515
* `isPointInPath()` now ignores the current transform and no longer counts points that are only inside the stroke (#2634)
1616
* Fix off-by-one RGB values from `getImageData` on semi-transparent pixels (#1933)
1717
* Fix compilation on platforms that use ijg-libjpeg instead of libjpeg-turbo (#2563)
18+
* `actualBoundingBox*` from `measureText()` no longer always include the text origin, so `actualBoundingBoxLeft` can be negative when the ink starts right of the origin (#2620)
1819

1920
3.2.3
2021
==================

‎src/CanvasRenderingContext2d.cc‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2711,10 +2711,25 @@ Context2d::MeasureText(const Napi::CallbackInfo& info) {
27112711
for (GlyphRun& run : layout.runs) {
27122712
double toPx = fontSize / 1000;
27132713
for (Glyph& glyph : run.glyphs) {
2714-
actualBoundingBoxLeft = std::min(actualBoundingBoxLeft, ax + glyph.x_bearing * toPx);
2715-
actualBoundingBoxAscent = std::max(actualBoundingBoxAscent, ay + glyph.y_bearing * toPx);
2716-
actualBoundingBoxRight = std::max(actualBoundingBoxRight, ax + glyph.x_bearing * toPx + glyph.width * toPx);
2717-
actualBoundingBoxDescent = std::max(actualBoundingBoxDescent, ay - glyph.y_bearing * toPx - glyph.height * toPx);
2714+
// glyphs without ink (spaces) don't contribute to the box
2715+
if (glyph.width != 0 && glyph.height != 0) {
2716+
double left = ax + glyph.x_bearing * toPx;
2717+
double ascent = ay + glyph.y_bearing * toPx;
2718+
double right = left + glyph.width * toPx;
2719+
double descent = ay - glyph.y_bearing * toPx - glyph.height * toPx;
2720+
if (first) {
2721+
actualBoundingBoxLeft = left;
2722+
actualBoundingBoxAscent = ascent;
2723+
actualBoundingBoxRight = right;
2724+
actualBoundingBoxDescent = descent;
2725+
first = false;
2726+
} else {
2727+
actualBoundingBoxLeft = std::min(actualBoundingBoxLeft, left);
2728+
actualBoundingBoxAscent = std::max(actualBoundingBoxAscent, ascent);
2729+
actualBoundingBoxRight = std::max(actualBoundingBoxRight, right);
2730+
actualBoundingBoxDescent = std::max(actualBoundingBoxDescent, descent);
2731+
}
2732+
}
27182733
ax += glyph.x_advance * toPx;
27192734
ay += glyph.y_advance * toPx;
27202735
}

‎test/canvas.test.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,28 @@ describe('Canvas', function () {
11251125
assertApprox(rm.actualBoundingBoxRight, 1, 6)
11261126
})
11271127

1128+
it('actualBoundingBox does not include the origin when the ink is away from it (#2620)', function () {
1129+
const canvas = createCanvas(0, 0)
1130+
const ctx = canvas.getContext('2d')
1131+
ctx.font = '20px Arimo'
1132+
1133+
// the ink of '-' starts right of the pen and sits above the baseline
1134+
let metrics = ctx.measureText('-')
1135+
assertApprox(metrics.actualBoundingBoxLeft, -0.88, 0.1)
1136+
assertApprox(metrics.actualBoundingBoxRight, 5.78, 0.1)
1137+
assertApprox(metrics.actualBoundingBoxAscent, 6.1, 0.1)
1138+
assertApprox(metrics.actualBoundingBoxDescent, -4.54, 0.1)
1139+
1140+
// the leading space has no ink
1141+
metrics = ctx.measureText(' -')
1142+
assertApprox(metrics.actualBoundingBoxLeft, -6.44, 0.1)
1143+
assertApprox(metrics.actualBoundingBoxRight, 11.34, 0.1)
1144+
1145+
// the ink of 'j' overhangs left of the pen
1146+
metrics = ctx.measureText('j')
1147+
assertApprox(metrics.actualBoundingBoxLeft, 0.48, 0.1)
1148+
})
1149+
11281150
it('resolves text alignment wrt Context2d#direction #2508', function () {
11291151
const canvas = createCanvas(0, 0)
11301152
const ctx = canvas.getContext('2d')

0 commit comments

Comments
 (0)