Commit 716e1076 authored by David Quiroz Marin's avatar David Quiroz Marin Committed by Commit Bot

Change TextMetrics to return a dictionary of baselines instead of separate values

Some fonts do not set values for all baselines (hanging, ideographic), and in
those cases we're setting 0, which is a valid value. This new interface
getBaselines(), returns a dictionary with the set baselines and their values.

Bug: 277215
Change-Id: Ie94c74d1c95c7205ed47acaed589ae965f0e32f6
Reviewed-on: https://chromium-review.googlesource.com/1141084Reviewed-by: default avatarChris Harrelson <chrishtr@chromium.org>
Reviewed-by: default avatarFernando Serboncini <fserb@chromium.org>
Commit-Queue: David Quiroz Marin <davidqu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#577670}
parent a1a134d4
...@@ -646,12 +646,8 @@ PASS TextMetrics interface: attribute emHeightAscent ...@@ -646,12 +646,8 @@ PASS TextMetrics interface: attribute emHeightAscent
PASS Unscopable handled correctly for emHeightAscent property on TextMetrics PASS Unscopable handled correctly for emHeightAscent property on TextMetrics
PASS TextMetrics interface: attribute emHeightDescent PASS TextMetrics interface: attribute emHeightDescent
PASS Unscopable handled correctly for emHeightDescent property on TextMetrics PASS Unscopable handled correctly for emHeightDescent property on TextMetrics
PASS TextMetrics interface: attribute hangingBaseline PASS TextMetrics interface: operation getBaselines()
PASS Unscopable handled correctly for hangingBaseline property on TextMetrics PASS Unscopable handled correctly for getBaselines() on TextMetrics
PASS TextMetrics interface: attribute alphabeticBaseline
PASS Unscopable handled correctly for alphabeticBaseline property on TextMetrics
PASS TextMetrics interface: attribute ideographicBaseline
PASS Unscopable handled correctly for ideographicBaseline property on TextMetrics
PASS ImageData interface: existence and properties of interface object PASS ImageData interface: existence and properties of interface object
PASS ImageData interface object length PASS ImageData interface object length
PASS ImageData interface object name PASS ImageData interface object name
......
...@@ -1357,9 +1357,7 @@ interface TextMetrics { ...@@ -1357,9 +1357,7 @@ interface TextMetrics {
readonly attribute double actualBoundingBoxDescent; readonly attribute double actualBoundingBoxDescent;
readonly attribute double emHeightAscent; readonly attribute double emHeightAscent;
readonly attribute double emHeightDescent; readonly attribute double emHeightDescent;
readonly attribute double hangingBaseline; Baselines getBaselines();
readonly attribute double alphabeticBaseline;
readonly attribute double ideographicBaseline;
}; };
[Constructor(unsigned long sw, unsigned long sh), [Constructor(unsigned long sw, unsigned long sh),
......
...@@ -13,9 +13,9 @@ ...@@ -13,9 +13,9 @@
function testTextMetrics(textMetrics, expected) function testTextMetrics(textMetrics, expected)
{ {
var w = textMetrics.width; var w = textMetrics.width;
var ab = textMetrics.alphabeticBaseline; var ba = textMetrics.getBaselines().alphabetic;
var hb = textMetrics.hangingBaseline; var bh = textMetrics.getBaselines().hanging;
var ib = textMetrics.ideographicBaseline; var bi = textMetrics.getBaselines().ideographic;
var fa = textMetrics.fontBoundingBoxAscent; var fa = textMetrics.fontBoundingBoxAscent;
var fd = textMetrics.fontBoundingBoxDescent; var fd = textMetrics.fontBoundingBoxDescent;
var ea = textMetrics.emHeightAscent; var ea = textMetrics.emHeightAscent;
...@@ -27,9 +27,9 @@ function testTextMetrics(textMetrics, expected) ...@@ -27,9 +27,9 @@ function testTextMetrics(textMetrics, expected)
var epsilon = 2; var epsilon = 2;
assert_approx_equals(w, expected[0], epsilon, "testing width"); assert_approx_equals(w, expected[0], epsilon, "testing width");
assert_approx_equals(ab, expected[1], epsilon, "testing alphabeticBaseline"); assert_approx_equals(ba, expected[1], epsilon, "testing getBaselines().alphabetic");
assert_approx_equals(hb, expected[2], epsilon, "testing hangingBaseline"); assert_approx_equals(bh, expected[2], epsilon, "testing getBaselines().hanging");
assert_approx_equals(ib, expected[3], epsilon, "testing ideographicBaseline"); assert_approx_equals(bi, expected[3], epsilon, "testing getBaselines().ideographic");
assert_approx_equals(fa, expected[4], epsilon, "testing fontBoundingBoxAscent"); assert_approx_equals(fa, expected[4], epsilon, "testing fontBoundingBoxAscent");
assert_approx_equals(fd, expected[5], epsilon, "testing fontBoundingBoxDescent"); assert_approx_equals(fd, expected[5], epsilon, "testing fontBoundingBoxDescent");
assert_approx_equals(ea, expected[6], epsilon, "testing emHeightAscent"); assert_approx_equals(ea, expected[6], epsilon, "testing emHeightAscent");
......
...@@ -21,8 +21,11 @@ function getMetric(ctx, text, baseline, align, rtl, metric) { ...@@ -21,8 +21,11 @@ function getMetric(ctx, text, baseline, align, rtl, metric) {
const tm = ctx.measureText(text); const tm = ctx.measureText(text);
tm.actualBoundingBoxWidth = Math.abs(tm.actualBoundingBoxRight + tm.actualBoundingBoxLeft); tm.actualBoundingBoxWidth = Math.abs(tm.actualBoundingBoxRight + tm.actualBoundingBoxLeft);
tm.actualBoundingBoxHeight = Math.abs(tm.actualBoundingBoxDescent + tm.actualBoundingBoxAscent); tm.actualBoundingBoxHeight = Math.abs(tm.actualBoundingBoxDescent + tm.actualBoundingBoxAscent);
if (["alphabetic", "ideographic", "hanging"].includes(metric)) {
return tm.getBaselines()[metric];
} else {
return tm[metric]; return tm[metric];
}
} }
function testMetric(name, ctx, text, baseline, align, rtl, metric, expected_value, epsilon = 0) { function testMetric(name, ctx, text, baseline, align, rtl, metric, expected_value, epsilon = 0) {
...@@ -85,22 +88,22 @@ function testBaselines(ctx) { ...@@ -85,22 +88,22 @@ function testBaselines(ctx) {
for (const ex of kTexts) { for (const ex of kTexts) {
for (const bline of ["alphabetic", "ideographic", "hanging"]) { for (const bline of ["alphabetic", "ideographic", "hanging"]) {
for (const align of kAligns) { for (const align of kAligns) {
testMetric("own baselines", ctx, ex.text, bline, align, ex.rtl, bline + "Baseline", 0); testMetric("own baselines", ctx, ex.text, bline, align, ex.rtl, bline, 0);
} }
} }
} }
// Those 3 baselines are font size dependent, so everything should be fixed. // Those 3 baselines are font size dependent, so everything should be fixed.
forEachExample((ex, _, align) => { forEachExample((ex, _, align) => {
testMetric("top baseline", ctx, ex.text, "top", align, ex.rtl, "alphabeticBaseline", -44); testMetric("top baseline", ctx, ex.text, "top", align, ex.rtl, "alphabetic", -44);
}); });
forEachExample((ex, _, align) => { forEachExample((ex, _, align) => {
testMetric("middle baseline", ctx, ex.text, "middle", align, ex.rtl, "alphabeticBaseline", -15); testMetric("middle baseline", ctx, ex.text, "middle", align, ex.rtl, "alphabetic", -15);
}); });
forEachExample((ex, _, align) => { forEachExample((ex, _, align) => {
testMetric("bottom baseline", ctx, ex.text, "bottom", align, ex.rtl, "alphabeticBaseline", 13); testMetric("bottom baseline", ctx, ex.text, "bottom", align, ex.rtl, "alphabetic", 13);
}); });
} }
......
...@@ -7115,15 +7115,13 @@ interface TextMetrics ...@@ -7115,15 +7115,13 @@ interface TextMetrics
getter actualBoundingBoxLeft getter actualBoundingBoxLeft
getter actualBoundingBoxRight getter actualBoundingBoxRight
getter advances getter advances
getter alphabeticBaseline
getter emHeightAscent getter emHeightAscent
getter emHeightDescent getter emHeightDescent
getter fontBoundingBoxAscent getter fontBoundingBoxAscent
getter fontBoundingBoxDescent getter fontBoundingBoxDescent
getter hangingBaseline
getter ideographicBaseline
getter width getter width
method constructor method constructor
method getBaselines
interface TextTrack : EventTarget interface TextTrack : EventTarget
attribute @@toStringTag attribute @@toStringTag
getter activeCues getter activeCues
......
...@@ -629,6 +629,7 @@ core_dictionary_idl_files = ...@@ -629,6 +629,7 @@ core_dictionary_idl_files =
"geometry/dom_rect_init.idl", "geometry/dom_rect_init.idl",
"html/focus_options.idl", "html/focus_options.idl",
"html/assigned_nodes_options.idl", "html/assigned_nodes_options.idl",
"html/canvas/baselines.idl",
"html/canvas/image_data_color_settings.idl", "html/canvas/image_data_color_settings.idl",
"html/canvas/image_encode_options.idl", "html/canvas/image_encode_options.idl",
"html/track/track_event_init.idl", "html/track/track_event_init.idl",
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
dictionary Baselines {
double alphabetic;
double hanging;
double ideographic;
};
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
// found in the LICENSE file. // found in the LICENSE file.
#include "third_party/blink/renderer/core/html/canvas/text_metrics.h" #include "third_party/blink/renderer/core/html/canvas/text_metrics.h"
#include "third_party/blink/renderer/core/html/canvas/baselines.h"
#include "third_party/blink/renderer/platform/fonts/character_range.h" #include "third_party/blink/renderer/platform/fonts/character_range.h"
namespace blink { namespace blink {
...@@ -101,9 +102,10 @@ void TextMetrics::Update(const Font& font, ...@@ -101,9 +102,10 @@ void TextMetrics::Update(const Font& font,
em_height_descent_ = baseline_y; em_height_descent_ = baseline_y;
// TODO(fserb): hanging/ideographic baselines are broken. // TODO(fserb): hanging/ideographic baselines are broken.
hanging_baseline_ = ascent * kHangingAsPercentOfAscent / 100.0f - baseline_y; baselines_.setAlphabetic(-baseline_y);
ideographic_baseline_ = -descent - baseline_y; baselines_.setHanging(ascent * kHangingAsPercentOfAscent / 100.0f -
alphabetic_baseline_ = -baseline_y; baseline_y);
baselines_.setIdeographic(-descent - baseline_y);
} }
} // namespace blink } // namespace blink
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#define THIRD_PARTY_BLINK_RENDERER_CORE_HTML_CANVAS_TEXT_METRICS_H_ #define THIRD_PARTY_BLINK_RENDERER_CORE_HTML_CANVAS_TEXT_METRICS_H_
#include "third_party/blink/renderer/core/core_export.h" #include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/html/canvas/baselines.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h" #include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
#include "third_party/blink/renderer/platform/fonts/font.h" #include "third_party/blink/renderer/platform/fonts/font.h"
#include "third_party/blink/renderer/platform/graphics/graphics_types.h" #include "third_party/blink/renderer/platform/graphics/graphics_types.h"
...@@ -62,9 +63,8 @@ class CORE_EXPORT TextMetrics final : public ScriptWrappable { ...@@ -62,9 +63,8 @@ class CORE_EXPORT TextMetrics final : public ScriptWrappable {
} }
double emHeightAscent() const { return em_height_ascent_; } double emHeightAscent() const { return em_height_ascent_; }
double emHeightDescent() const { return em_height_descent_; } double emHeightDescent() const { return em_height_descent_; }
double hangingBaseline() const { return hanging_baseline_; } void getBaselines(Baselines& baselines) const { baselines = baselines_; }
double alphabeticBaseline() const { return alphabetic_baseline_; } Baselines getBaselines() const { return baselines_; }
double ideographicBaseline() const { return ideographic_baseline_; }
static float GetFontBaseline(const TextBaseline&, const FontMetrics&); static float GetFontBaseline(const TextBaseline&, const FontMetrics&);
...@@ -89,9 +89,7 @@ class CORE_EXPORT TextMetrics final : public ScriptWrappable { ...@@ -89,9 +89,7 @@ class CORE_EXPORT TextMetrics final : public ScriptWrappable {
double actual_bounding_box_descent_ = 0.0; double actual_bounding_box_descent_ = 0.0;
double em_height_ascent_ = 0.0; double em_height_ascent_ = 0.0;
double em_height_descent_ = 0.0; double em_height_descent_ = 0.0;
double hanging_baseline_ = 0.0; Baselines baselines_;
double alphabetic_baseline_ = 0.0;
double ideographic_baseline_ = 0.0;
}; };
} // namespace blink } // namespace blink
......
...@@ -40,7 +40,5 @@ interface TextMetrics { ...@@ -40,7 +40,5 @@ interface TextMetrics {
[RuntimeEnabled=ExtendedTextMetrics] readonly attribute double actualBoundingBoxDescent; [RuntimeEnabled=ExtendedTextMetrics] readonly attribute double actualBoundingBoxDescent;
[RuntimeEnabled=ExtendedTextMetrics] readonly attribute double emHeightAscent; [RuntimeEnabled=ExtendedTextMetrics] readonly attribute double emHeightAscent;
[RuntimeEnabled=ExtendedTextMetrics] readonly attribute double emHeightDescent; [RuntimeEnabled=ExtendedTextMetrics] readonly attribute double emHeightDescent;
[RuntimeEnabled=ExtendedTextMetrics] readonly attribute double hangingBaseline; [RuntimeEnabled=ExtendedTextMetrics] Baselines getBaselines();
[RuntimeEnabled=ExtendedTextMetrics] readonly attribute double alphabeticBaseline;
[RuntimeEnabled=ExtendedTextMetrics] readonly attribute double ideographicBaseline;
}; };
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment