libtxt: use a placeholder run's width as the width of the placeholder character's glyph (flutter/engine#22451)

This commit is contained in:
Jason Simmons
2020-11-12 10:24:01 -08:00
committed by GitHub
parent 152265eda3
commit 5441ac8cf5
2 changed files with 56 additions and 25 deletions

View File

@@ -951,7 +951,16 @@ void ParagraphTxt::Layout(double width) {
}
grapheme_code_unit_counts.push_back(code_unit_count);
}
float glyph_advance = layout.getCharAdvance(glyph_code_units.start);
float glyph_advance;
if (run.is_placeholder_run()) {
// The placeholder run's layout should yield one glyph representing
// the object replacement character. Replace its width with the
// placeholder's width.
FML_DCHECK(layout.nGlyphs() == 1);
glyph_advance = run.placeholder_run()->width;
} else {
glyph_advance = layout.getCharAdvance(glyph_code_units.start);
}
float grapheme_advance =
glyph_advance / grapheme_code_unit_counts.size();
@@ -1016,19 +1025,10 @@ void ParagraphTxt::Layout(double width) {
Range<double> record_x_pos(
glyph_positions.front().x_pos.start - run_x_offset,
glyph_positions.back().x_pos.end - run_x_offset);
if (run.is_placeholder_run()) {
paint_records.emplace_back(
run.style(), SkPoint::Make(run_x_offset + justify_x_offset, 0),
builder.make(), *metrics, line_number, record_x_pos.start,
record_x_pos.start + run.placeholder_run()->width, run.is_ghost(),
run.placeholder_run());
run_x_offset += run.placeholder_run()->width;
} else {
paint_records.emplace_back(
run.style(), SkPoint::Make(run_x_offset + justify_x_offset, 0),
builder.make(), *metrics, line_number, record_x_pos.start,
record_x_pos.end, run.is_ghost());
}
paint_records.emplace_back(
run.style(), SkPoint::Make(run_x_offset + justify_x_offset, 0),
builder.make(), *metrics, line_number, record_x_pos.start,
record_x_pos.end, run.is_ghost(), run.placeholder_run());
justify_x_offset += justify_x_offset_delta;
line_glyph_positions.insert(line_glyph_positions.end(),
@@ -1043,10 +1043,7 @@ void ParagraphTxt::Layout(double width) {
});
double blob_x_pos_start = glyph_positions.front().x_pos.start;
double blob_x_pos_end = run.is_placeholder_run()
? glyph_positions.back().x_pos.start +
run.placeholder_run()->width
: glyph_positions.back().x_pos.end;
double blob_x_pos_end = glyph_positions.back().x_pos.end;
line_code_unit_runs.emplace_back(
std::move(code_unit_positions),
Range<size_t>(run.start(), run.end()),
@@ -1064,13 +1061,17 @@ void ParagraphTxt::Layout(double width) {
}
} // for each in glyph_blobs
// Do not increase x offset for LTR trailing ghost runs as it should not
// impact the layout of visible glyphs. RTL tailing ghost runs have the
// advance subtracted, so we do add the advance here to reset the
// run_x_offset. We do keep the record though so GetRectsForRange() can
// find metrics for trailing spaces.
if ((!run.is_ghost() || run.is_rtl()) && !run.is_placeholder_run()) {
run_x_offset += layout.getAdvance();
if (run.is_placeholder_run()) {
run_x_offset += run.placeholder_run()->width;
} else {
// Do not increase x offset for LTR trailing ghost runs as it should not
// impact the layout of visible glyphs. RTL tailing ghost runs have the
// advance subtracted, so we do add the advance here to reset the
// run_x_offset. We do keep the record though so GetRectsForRange() can
// find metrics for trailing spaces.
if (!run.is_ghost() || run.is_rtl()) {
run_x_offset += layout.getAdvance();
}
}
} // for each in line_runs

View File

@@ -1484,6 +1484,36 @@ TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(InlinePlaceholderLongestLine)) {
ASSERT_TRUE(paragraph->longest_line_ >= 50);
}
TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(InlinePlaceholderIntrinsicWidth)) {
const char* text = "A ";
auto icu_text = icu::UnicodeString::fromUTF8(text);
std::u16string u16_text(icu_text.getBuffer(),
icu_text.getBuffer() + icu_text.length());
txt::PlaceholderRun placeholder_run(50, 50, PlaceholderAlignment::kBaseline,
TextBaseline::kAlphabetic, 0);
txt::ParagraphStyle paragraph_style;
txt::ParagraphBuilderTxt builder(paragraph_style, GetTestFontCollection());
txt::TextStyle text_style;
text_style.font_families = std::vector<std::string>(1, "Roboto");
text_style.font_size = 20;
text_style.decoration_color = SK_ColorBLACK;
builder.PushStyle(text_style);
builder.AddText(u16_text);
builder.AddPlaceholder(placeholder_run);
builder.Pop();
auto paragraph = BuildParagraph(builder);
paragraph->Layout(GetTestCanvasWidth());
ASSERT_DOUBLE_EQ(paragraph->GetMinIntrinsicWidth(), 50);
ASSERT_DOUBLE_EQ(paragraph->GetMaxIntrinsicWidth(), 68);
}
#if OS_LINUX
// Tests if manually inserted 0xFFFC characters are replaced to 0xFFFD in order
// to not interfere with the placeholder box layout.