Skip to content

Commit

Permalink
Implement baselines attributes for TextMetrics
Browse files Browse the repository at this point in the history
According to the WHATWG spec,
https://html.spec.whatwg.org/multipage/canvas.html#textmetrics,
TextMetrics has 3 baseline attributes, alphabeticBaseline,
hangingBaseline and ideographicBaseline, which are missing in our
current implementation.

This cl implements these baseline attributes.

The intend to ship:
https://groups.google.com/a/chromium.org/g/blink-dev/c/AORzxU_ZNTk/m/Iw5E8lGlAgAJ

Bug: 1473117

Change-Id: Ia7c098065ebbe29979833c4f7fe3b7fcdd993ccf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4780555
Reviewed-by: Kent Tamura <tkent@chromium.org>
Commit-Queue: Yi Xu <yiyix@google.com>
Cr-Commit-Position: refs/heads/main@{#1188175}
  • Loading branch information
Yi Xu authored and Chromium LUCI CQ committed Aug 25, 2023
1 parent ef90206 commit 159eaad
Show file tree
Hide file tree
Showing 19 changed files with 54 additions and 33 deletions.
4 changes: 3 additions & 1 deletion third_party/blink/renderer/core/html/canvas/text_metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,14 @@ class CORE_EXPORT TextMetrics final : public ScriptWrappable {
double fontBoundingBoxAscent() const { return font_bounding_box_ascent_; }
double fontBoundingBoxDescent() const { return font_bounding_box_descent_; }
double actualBoundingBoxAscent() const { return actual_bounding_box_ascent_; }
double alphabeticBaseline() const { return baselines_->alphabetic(); }
double hangingBaseline() const { return baselines_->hanging(); }
double ideographicBaseline() const { return baselines_->ideographic(); }
double actualBoundingBoxDescent() const {
return actual_bounding_box_descent_;
}
double emHeightAscent() const { return em_height_ascent_; }
double emHeightDescent() const { return em_height_descent_; }
Baselines* getBaselines() const { return baselines_; }

static float GetFontBaseline(const TextBaseline&, const SimpleFontData&);

Expand Down
4 changes: 3 additions & 1 deletion third_party/blink/renderer/core/html/canvas/text_metrics.idl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ interface TextMetrics {
readonly attribute double fontBoundingBoxDescent;
readonly attribute double actualBoundingBoxAscent;
readonly attribute double actualBoundingBoxDescent;
[RuntimeEnabled=TextMetricsBaselines] readonly attribute double hangingBaseline;
[RuntimeEnabled=TextMetricsBaselines] readonly attribute double alphabeticBaseline;
[RuntimeEnabled=TextMetricsBaselines] readonly attribute double ideographicBaseline;
[RuntimeEnabled=ExtendedTextMetrics] readonly attribute double emHeightAscent;
[RuntimeEnabled=ExtendedTextMetrics] readonly attribute double emHeightDescent;
[RuntimeEnabled=ExtendedTextMetrics] Baselines getBaselines();
};
Original file line number Diff line number Diff line change
Expand Up @@ -3628,6 +3628,10 @@
status: {"Android": "stable"},
base_feature: "none",
},
{
name: "TextMetricsBaselines",
status: "stable",
},
{
// Support for the timeline-scope property.
//
Expand Down
4 changes: 4 additions & 0 deletions third_party/blink/web_tests/TestExpectations
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,10 @@ crbug.com/1418141 external/wpt/html/canvas/offscreen/layers/2d.layer.global-stat
crbug.com/1418141 external/wpt/html/canvas/offscreen/layers/2d.layer.global-states.filter.alpha.blending.shadow.w.html [ Failure ]
crbug.com/1418141 external/wpt/html/canvas/offscreen/layers/2d.layer.global-states.filter.blending.shadow.w.html [ Failure ]
crbug.com/1418141 external/wpt/html/canvas/offscreen/layers/2d.layer.global-states.blending.shadow.w.html [ Failure ]
# Tests failed due to missing OpenType table reading capabilities:
crbug.com/1473117 external/wpt/html/canvas/element/text/2d.text.measure.baselines.html [ Failure ]
crbug.com/1473117 external/wpt/html/canvas/offscreen/text/2d.text.measure.baselines.html [ Failure ]
crbug.com/1473117 external/wpt/html/canvas/offscreen/text/2d.text.measure.baselines.worker.html [ Failure ]

# Off by one pixel error on ref test
crbug.com/1259367 [ Mac11-arm64 Release ] printing/offscreencanvas-webgl-printing.html [ Failure ]
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
This is a testharness.js-based test.
Found 1425 tests; 1391 PASS, 34 FAIL, 0 TIMEOUT, 0 NOTRUN.
Found 1425 tests; 1394 PASS, 31 FAIL, 0 TIMEOUT, 0 NOTRUN.
PASS idl_test setup
PASS idl_test validation
PASS Partial interface Document: original interface defined
Expand Down Expand Up @@ -673,9 +673,9 @@ PASS TextMetrics interface: attribute actualBoundingBoxAscent
PASS TextMetrics interface: attribute actualBoundingBoxDescent
PASS TextMetrics interface: attribute emHeightAscent
PASS TextMetrics interface: attribute emHeightDescent
FAIL TextMetrics interface: attribute hangingBaseline assert_true: The prototype object must have a property "hangingBaseline" expected true got false
FAIL TextMetrics interface: attribute alphabeticBaseline assert_true: The prototype object must have a property "alphabeticBaseline" expected true got false
FAIL TextMetrics interface: attribute ideographicBaseline assert_true: The prototype object must have a property "ideographicBaseline" expected true got false
PASS TextMetrics interface: attribute hangingBaseline
PASS TextMetrics interface: attribute alphabeticBaseline
PASS TextMetrics interface: attribute ideographicBaseline
PASS ImageData interface: existence and properties of interface object
PASS ImageData interface object length
PASS ImageData interface object name
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
This is a testharness.js-based test.
Found 750 tests; 741 PASS, 9 FAIL, 0 TIMEOUT, 0 NOTRUN.
Found 750 tests; 744 PASS, 6 FAIL, 0 TIMEOUT, 0 NOTRUN.
PASS idl_test setup
PASS idl_test validation
PASS Partial interface Document: original interface defined
Expand Down Expand Up @@ -282,9 +282,9 @@ PASS TextMetrics interface: attribute actualBoundingBoxAscent
PASS TextMetrics interface: attribute actualBoundingBoxDescent
PASS TextMetrics interface: attribute emHeightAscent
PASS TextMetrics interface: attribute emHeightDescent
FAIL TextMetrics interface: attribute hangingBaseline assert_true: The prototype object must have a property "hangingBaseline" expected true got false
FAIL TextMetrics interface: attribute alphabeticBaseline assert_true: The prototype object must have a property "alphabeticBaseline" expected true got false
FAIL TextMetrics interface: attribute ideographicBaseline assert_true: The prototype object must have a property "ideographicBaseline" expected true got false
PASS TextMetrics interface: attribute hangingBaseline
PASS TextMetrics interface: attribute alphabeticBaseline
PASS TextMetrics interface: attribute ideographicBaseline
PASS ImageData interface: existence and properties of interface object
PASS ImageData interface object length
PASS ImageData interface object name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
function testTextMetrics(textMetrics, expected)
{
var w = textMetrics.width;
var ba = textMetrics.getBaselines().alphabetic;
var bh = textMetrics.getBaselines().hanging;
var bi = textMetrics.getBaselines().ideographic;
var ba = textMetrics.alphabeticBaseline;
var bh = textMetrics.hangingBaseline;
var bi = textMetrics.ideographicBaseline;
var fa = textMetrics.fontBoundingBoxAscent;
var fd = textMetrics.fontBoundingBoxDescent;
var ea = textMetrics.emHeightAscent;
Expand All @@ -27,9 +27,9 @@

var epsilon = 3;
assert_approx_equals(w, expected[0], epsilon, "testing width");
assert_approx_equals(ba, expected[1], epsilon, "testing getBaselines().alphabetic");
assert_approx_equals(bh, expected[2], epsilon, "testing getBaselines().hanging");
assert_approx_equals(bi, expected[3], epsilon, "testing getBaselines().ideographic");
assert_approx_equals(ba, expected[1], epsilon, "testing alphabeticBaseline");
assert_approx_equals(bh, expected[2], epsilon, "testing hangingBaseline");
assert_approx_equals(bi, expected[3], epsilon, "testing ideographicBaseline");
assert_approx_equals(fa, expected[4], epsilon, "testing fontBoundingBoxAscent");
assert_approx_equals(fd, expected[5], epsilon, "testing fontBoundingBoxDescent");
assert_approx_equals(ea, expected[6], epsilon, "testing emHeightAscent");
Expand Down
3 changes: 2 additions & 1 deletion third_party/blink/web_tests/fast/canvas-api/textMetrics.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
tm.actualBoundingBoxWidth = Math.abs(tm.actualBoundingBoxRight + tm.actualBoundingBoxLeft);
tm.actualBoundingBoxHeight = Math.abs(tm.actualBoundingBoxDescent + tm.actualBoundingBoxAscent);
if (["alphabetic", "ideographic", "hanging"].includes(metric)) {
return tm.getBaselines()[metric];
var bl = metric + "Baseline";
return tm[bl];
} else {
return tm[metric];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1481,13 +1481,15 @@ interface TextMetrics
getter actualBoundingBoxLeft
getter actualBoundingBoxRight
getter advances
getter alphabeticBaseline
getter emHeightAscent
getter emHeightDescent
getter fontBoundingBoxAscent
getter fontBoundingBoxDescent
getter hangingBaseline
getter ideographicBaseline
getter width
method constructor
method getBaselines
interface TimestampTrigger
attribute @@toStringTag
getter timestamp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1361,8 +1361,11 @@ interface TextMetrics
getter actualBoundingBoxDescent
getter actualBoundingBoxLeft
getter actualBoundingBoxRight
getter alphabeticBaseline
getter fontBoundingBoxAscent
getter fontBoundingBoxDescent
getter hangingBaseline
getter ideographicBaseline
getter width
method constructor
interface TransformStream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1771,8 +1771,11 @@ Starting worker: resources/global-interface-listing-worker.js
[Worker] getter actualBoundingBoxDescent
[Worker] getter actualBoundingBoxLeft
[Worker] getter actualBoundingBoxRight
[Worker] getter alphabeticBaseline
[Worker] getter fontBoundingBoxAscent
[Worker] getter fontBoundingBoxDescent
[Worker] getter hangingBaseline
[Worker] getter ideographicBaseline
[Worker] getter width
[Worker] method constructor
[Worker] interface TransformStream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8498,8 +8498,11 @@ interface TextMetrics
getter actualBoundingBoxDescent
getter actualBoundingBoxLeft
getter actualBoundingBoxRight
getter alphabeticBaseline
getter fontBoundingBoxAscent
getter fontBoundingBoxDescent
getter hangingBaseline
getter ideographicBaseline
getter width
method constructor
interface TextTrack : EventTarget
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1253,8 +1253,11 @@ Starting worker: resources/global-interface-listing-worker.js
[Worker] getter actualBoundingBoxDescent
[Worker] getter actualBoundingBoxLeft
[Worker] getter actualBoundingBoxRight
[Worker] getter alphabeticBaseline
[Worker] getter fontBoundingBoxAscent
[Worker] getter fontBoundingBoxDescent
[Worker] getter hangingBaseline
[Worker] getter ideographicBaseline
[Worker] getter width
[Worker] method constructor
[Worker] interface TransformStream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1978,13 +1978,15 @@ Starting worker: resources/global-interface-listing-worker.js
[Worker] getter actualBoundingBoxLeft
[Worker] getter actualBoundingBoxRight
[Worker] getter advances
[Worker] getter alphabeticBaseline
[Worker] getter emHeightAscent
[Worker] getter emHeightDescent
[Worker] getter fontBoundingBoxAscent
[Worker] getter fontBoundingBoxDescent
[Worker] getter hangingBaseline
[Worker] getter ideographicBaseline
[Worker] getter width
[Worker] method constructor
[Worker] method getBaselines
[Worker] interface TimestampTrigger
[Worker] attribute @@toStringTag
[Worker] getter timestamp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9492,13 +9492,15 @@ interface TextMetrics
getter actualBoundingBoxLeft
getter actualBoundingBoxRight
getter advances
getter alphabeticBaseline
getter emHeightAscent
getter emHeightDescent
getter fontBoundingBoxAscent
getter fontBoundingBoxDescent
getter hangingBaseline
getter ideographicBaseline
getter width
method constructor
method getBaselines
interface TextTrack : EventTarget
attribute @@toStringTag
getter activeCues
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1392,13 +1392,15 @@ Starting worker: resources/global-interface-listing-worker.js
[Worker] getter actualBoundingBoxLeft
[Worker] getter actualBoundingBoxRight
[Worker] getter advances
[Worker] getter alphabeticBaseline
[Worker] getter emHeightAscent
[Worker] getter emHeightDescent
[Worker] getter fontBoundingBoxAscent
[Worker] getter fontBoundingBoxDescent
[Worker] getter hangingBaseline
[Worker] getter ideographicBaseline
[Worker] getter width
[Worker] method constructor
[Worker] method getBaselines
[Worker] interface TimestampTrigger
[Worker] attribute @@toStringTag
[Worker] getter timestamp
Expand Down

0 comments on commit 159eaad

Please sign in to comment.