Skip to content

Commit

Permalink
Update canvas to read baselines info from font_data
Browse files Browse the repository at this point in the history
After saving the actual baseline values from OpenType Table to
simple_font_data,
(cl: https://chromium-review.googlesource.com/c/chromium/src/+/4794157)
we can now load these values to text metrics instead of calculating
the estimated values from font ascent and descent. This helped to
fixed the failures in draw.baselines.hanging/ideographic.

Bug: 1473140

Change-Id: I0ff5c276865b1ba9043c2c5a8d7d39daa4909ee0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4794477
Reviewed-by: Dominik Röttsches <drott@chromium.org>
Commit-Queue: Yi Xu <yiyix@google.com>
Cr-Commit-Position: refs/heads/main@{#1188101}
  • Loading branch information
Yi Xu authored and Chromium LUCI CQ committed Aug 25, 2023
1 parent d8537e5 commit 9acad7b
Show file tree
Hide file tree
Showing 22 changed files with 42 additions and 91 deletions.
53 changes: 42 additions & 11 deletions third_party/blink/renderer/core/html/canvas/text_metrics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "third_party/blink/renderer/bindings/core/v8/v8_baselines.h"
#include "third_party/blink/renderer/core/layout/ng/inline/ng_bidi_paragraph.h"
#include "third_party/blink/renderer/platform/fonts/character_range.h"
#include "third_party/blink/renderer/platform/fonts/font_metrics.h"

namespace blink {

Expand All @@ -18,25 +19,37 @@ float TextMetrics::GetFontBaseline(const TextBaseline& text_baseline,
case kTopTextBaseline:
return font_data.NormalizedTypoAscent().ToFloat();
case kHangingTextBaseline:
if (font_metrics.HangingBaseline().has_value()) {
return font_metrics.HangingBaseline().value();
}
// According to
// http://wiki.apache.org/xmlgraphics-fop/LineLayout/AlignmentHandling
// "FOP (Formatting Objects Processor) puts the hanging baseline at 80% of
// the ascender height"
return font_metrics.FloatAscent() * kHangingAsPercentOfAscent / 100.0;
return font_metrics.FloatAscent(kAlphabeticBaseline,
FontMetrics::ApplyBaselineTable(true)) *
kHangingAsPercentOfAscent / 100.0;
case kIdeographicTextBaseline:
return -font_metrics.FloatDescent();
if (font_metrics.IdeographicBaseline().has_value()) {
return font_metrics.IdeographicBaseline().value();
}
return -font_metrics.FloatDescent(kAlphabeticBaseline,
FontMetrics::ApplyBaselineTable(true));
case kBottomTextBaseline:
return -font_data.NormalizedTypoDescent().ToFloat();
case kMiddleTextBaseline: {
const FontHeight metrics = font_data.NormalizedTypoAscentAndDescent();
return (metrics.ascent.ToFloat() - metrics.descent.ToFloat()) / 2.0f;
}
case kAlphabeticTextBaseline:
if (font_metrics.AlphabeticBaseline().has_value()) {
return font_metrics.AlphabeticBaseline().value();
}
return 0;
default:
// Do nothing.
break;
return 0;
}
return 0;
}

void TextMetrics::Trace(Visitor* visitor) const {
Expand Down Expand Up @@ -111,8 +124,10 @@ void TextMetrics::Update(const Font& font,

// y direction
const FontMetrics& font_metrics = font_data->GetFontMetrics();
const float ascent = font_metrics.FloatAscent();
const float descent = font_metrics.FloatDescent();
const float ascent = font_metrics.FloatAscent(
kAlphabeticBaseline, FontMetrics::ApplyBaselineTable(true));
const float descent = font_metrics.FloatDescent(
kAlphabeticBaseline, FontMetrics::ApplyBaselineTable(true));
const float baseline_y = GetFontBaseline(baseline, *font_data);
font_bounding_box_ascent_ = ascent - baseline_y;
font_bounding_box_descent_ = descent + baseline_y;
Expand All @@ -125,11 +140,27 @@ void TextMetrics::Update(const Font& font,
em_height_ascent_ = normalized_typo_metrics.ascent - baseline_y;
em_height_descent_ = normalized_typo_metrics.descent + baseline_y;

// TODO(fserb): hanging/ideographic baselines are broken.
baselines_->setAlphabetic(-baseline_y);
baselines_->setHanging(ascent * kHangingAsPercentOfAscent / 100.0f -
baseline_y);
baselines_->setIdeographic(-descent - baseline_y);
// Setting baselines:
if (font_metrics.AlphabeticBaseline().has_value()) {
baselines_->setAlphabetic(font_metrics.AlphabeticBaseline().value() -
baseline_y);
} else {
baselines_->setAlphabetic(-baseline_y);
}

if (font_metrics.HangingBaseline().has_value()) {
baselines_->setHanging(font_metrics.HangingBaseline().value() - baseline_y);
} else {
baselines_->setHanging(ascent * kHangingAsPercentOfAscent / 100.0f -
baseline_y);
}

if (font_metrics.IdeographicBaseline().has_value()) {
baselines_->setIdeographic(font_metrics.IdeographicBaseline().value() -
baseline_y);
} else {
baselines_->setIdeographic(-descent - baseline_y);
}
}

} // namespace blink

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 9acad7b

Please sign in to comment.