Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 6e79dcd

Browse files
authored
RTL handling for ghost runs, NotoNaskhArabic test font (#8638)
1 parent c37d459 commit 6e79dcd

File tree

4 files changed

+228
-24
lines changed

4 files changed

+228
-24
lines changed

third_party/txt/src/txt/paragraph.cc

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717
#include "paragraph.h"
1818

1919
#include <hb.h>
20+
#include <minikin/Layout.h>
21+
2022
#include <algorithm>
2123
#include <limits>
2224
#include <map>
2325
#include <numeric>
2426
#include <utility>
2527
#include <vector>
2628

27-
#include <minikin/Layout.h>
2829
#include "flutter/fml/logging.h"
2930
#include "font_collection.h"
3031
#include "font_skia.h"
@@ -34,9 +35,6 @@
3435
#include "minikin/LayoutUtils.h"
3536
#include "minikin/LineBreaker.h"
3637
#include "minikin/MinikinFont.h"
37-
#include "unicode/ubidi.h"
38-
#include "unicode/utf16.h"
39-
4038
#include "third_party/skia/include/core/SkCanvas.h"
4139
#include "third_party/skia/include/core/SkFont.h"
4240
#include "third_party/skia/include/core/SkFontMetrics.h"
@@ -46,6 +44,8 @@
4644
#include "third_party/skia/include/core/SkTypeface.h"
4745
#include "third_party/skia/include/effects/SkDashPathEffect.h"
4846
#include "third_party/skia/include/effects/SkDiscretePathEffect.h"
47+
#include "unicode/ubidi.h"
48+
#include "unicode/utf16.h"
4949

5050
namespace txt {
5151
namespace {
@@ -552,28 +552,39 @@ void Paragraph::Layout(double width, bool force) {
552552
// Find the runs comprising this line.
553553
std::vector<BidiRun> line_runs;
554554
for (const BidiRun& bidi_run : bidi_runs) {
555-
if (bidi_run.start() < line_end_index &&
556-
bidi_run.end() > line_range.start) {
557-
line_runs.emplace_back(std::max(bidi_run.start(), line_range.start),
558-
std::min(bidi_run.end(), line_end_index),
559-
bidi_run.direction(), bidi_run.style());
560-
}
561555
// A "ghost" run is a run that does not impact the layout, breaking,
562-
// alignment, width, etc but is still "visible" though getRectsForRange.
556+
// alignment, width, etc but is still "visible" through getRectsForRange.
563557
// For example, trailing whitespace on centered text can be scrolled
564558
// through with the caret but will not wrap the line.
565559
//
566560
// Here, we add an additional run for the whitespace, but dont
567561
// let it impact metrics. After layout of the whitespace run, we do not
568562
// add its width into the x-offset adjustment, effectively nullifying its
569563
// impact on the layout.
564+
std::unique_ptr<BidiRun> ghost_run = nullptr;
570565
if (paragraph_style_.ellipsis.empty() &&
571566
line_range.end_excluding_whitespace < line_range.end &&
572567
bidi_run.start() <= line_range.end &&
573568
bidi_run.end() > line_end_index) {
574-
line_runs.emplace_back(std::max(bidi_run.start(), line_end_index),
575-
std::min(bidi_run.end(), line_range.end),
576-
bidi_run.direction(), bidi_run.style(), true);
569+
ghost_run = std::make_unique<BidiRun>(
570+
std::max(bidi_run.start(), line_end_index),
571+
std::min(bidi_run.end(), line_range.end), bidi_run.direction(),
572+
bidi_run.style(), true);
573+
}
574+
// Include the ghost run before normal run if RTL
575+
if (bidi_run.direction() == TextDirection::rtl && ghost_run != nullptr) {
576+
line_runs.push_back(*ghost_run);
577+
}
578+
// Emplace a normal line run.
579+
if (bidi_run.start() < line_end_index &&
580+
bidi_run.end() > line_range.start) {
581+
line_runs.emplace_back(std::max(bidi_run.start(), line_range.start),
582+
std::min(bidi_run.end(), line_end_index),
583+
bidi_run.direction(), bidi_run.style());
584+
}
585+
// Include the ghost run after normal run if LTR
586+
if (bidi_run.direction() == TextDirection::ltr && ghost_run != nullptr) {
587+
line_runs.push_back(*ghost_run);
577588
}
578589
}
579590
bool line_runs_all_rtl =
@@ -661,6 +672,17 @@ void Paragraph::Layout(double width, bool force) {
661672
if (layout.nGlyphs() == 0)
662673
continue;
663674

675+
// When laying out RTL ghost runs, shift the run_x_offset here by the
676+
// advance so that the ghost run is positioned to the left of the first
677+
// real run of text in the line. However, since we do not want it to
678+
// impact the layout of real text, this advance is subsequently added
679+
// back into the run_x_offset after the ghost run positions have been
680+
// calcuated and before the next real run of text is laid out, ensuring
681+
// later runs are laid out in the same position as if there were no ghost
682+
// run.
683+
if (run.is_ghost() && run.is_rtl())
684+
run_x_offset -= layout.getAdvance();
685+
664686
std::vector<float> layout_advances(text_count);
665687
layout.getAdvances(layout_advances.data());
666688

@@ -808,21 +830,25 @@ void Paragraph::Layout(double width, bool force) {
808830
[](const GlyphPosition& a, const GlyphPosition& b) {
809831
return a.code_units.start < b.code_units.start;
810832
});
833+
811834
line_code_unit_runs.emplace_back(
812835
std::move(code_unit_positions),
813836
Range<size_t>(run.start(), run.end()),
814837
Range<double>(glyph_positions.front().x_pos.start,
815838
glyph_positions.back().x_pos.end),
816839
line_number, metrics, run.direction());
817840

818-
min_left_ = std::min(min_left_, glyph_positions.front().x_pos.start);
819-
max_right_ = std::max(max_right_, glyph_positions.back().x_pos.end);
841+
if (!run.is_ghost()) {
842+
min_left_ = std::min(min_left_, glyph_positions.front().x_pos.start);
843+
max_right_ = std::max(max_right_, glyph_positions.back().x_pos.end);
844+
}
820845
} // for each in glyph_blobs
821-
822-
// Do not increase x offset for trailing ghost runs as it should not
823-
// impact the layout of visible glyphs. We do keep the record though so
824-
// GetRectsForRange() can find metrics for trailing spaces.
825-
if (!run.is_ghost()) {
846+
// Do not increase x offset for LTR trailing ghost runs as it should not
847+
// impact the layout of visible glyphs. RTL tailing ghost runs have the
848+
// advance subtracted, so we do add the advance here to reset the
849+
// run_x_offset. We do keep the record though so GetRectsForRange() can
850+
// find metrics for trailing spaces.
851+
if (!run.is_ghost() || run.is_rtl()) {
826852
run_x_offset += layout.getAdvance();
827853
}
828854
} // for each in line_runs

third_party/txt/tests/paragraph_unittests.cc

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,7 @@ TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(JustifyRTL)) {
706706
txt::ParagraphStyle paragraph_style;
707707
paragraph_style.max_lines = 14;
708708
paragraph_style.text_align = TextAlign::justify;
709+
paragraph_style.text_direction = TextDirection::rtl;
709710
txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
710711

711712
txt::TextStyle text_style;
@@ -725,16 +726,33 @@ TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(JustifyRTL)) {
725726

726727
paragraph->Paint(GetCanvas(), 0, 0);
727728

728-
ASSERT_TRUE(Snapshot());
729-
730729
auto glyph_line_width = [&paragraph](int index) {
731730
size_t second_to_last_position_index =
732-
paragraph->glyph_lines_[index].positions.size() - 2;
731+
paragraph->glyph_lines_[index].positions.size() - 1;
733732
return paragraph->glyph_lines_[index]
734733
.positions[second_to_last_position_index]
735734
.x_pos.end;
736735
};
737736

737+
SkPaint paint;
738+
paint.setStyle(SkPaint::kStroke_Style);
739+
paint.setAntiAlias(true);
740+
paint.setStrokeWidth(1);
741+
742+
// Tests for GetRectsForRange()
743+
Paragraph::RectHeightStyle rect_height_style =
744+
Paragraph::RectHeightStyle::kMax;
745+
Paragraph::RectWidthStyle rect_width_style =
746+
Paragraph::RectWidthStyle::kTight;
747+
paint.setColor(SK_ColorRED);
748+
std::vector<txt::Paragraph::TextBox> boxes =
749+
paragraph->GetRectsForRange(0, 100, rect_height_style, rect_width_style);
750+
for (size_t i = 0; i < boxes.size(); ++i) {
751+
GetCanvas()->drawRect(boxes[i].rect, paint);
752+
}
753+
ASSERT_EQ(boxes.size(), 5ull);
754+
ASSERT_TRUE(Snapshot());
755+
738756
// All lines except the last should be justified to the width of the
739757
// paragraph.
740758
for (size_t i = 0; i < paragraph->glyph_lines_.size() - 1; ++i) {
@@ -971,6 +989,74 @@ TEST_F(ParagraphTest, DISABLED_ArabicParagraph) {
971989
ASSERT_TRUE(Snapshot());
972990
}
973991

992+
// Checks if the rects are in the correct positions after typing spaces in
993+
// Arabic.
994+
TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(ArabicRectsParagraph)) {
995+
const char* text = "بمباركة التقليدية قام عن. تصفح يد ";
996+
auto icu_text = icu::UnicodeString::fromUTF8(text);
997+
std::u16string u16_text(icu_text.getBuffer(),
998+
icu_text.getBuffer() + icu_text.length());
999+
1000+
txt::ParagraphStyle paragraph_style;
1001+
paragraph_style.max_lines = 14;
1002+
paragraph_style.text_align = TextAlign::right;
1003+
paragraph_style.text_direction = TextDirection::rtl;
1004+
txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
1005+
1006+
txt::TextStyle text_style;
1007+
text_style.font_families = std::vector<std::string>(1, "Noto Naskh Arabic");
1008+
text_style.font_size = 26;
1009+
text_style.letter_spacing = 1;
1010+
text_style.word_spacing = 5;
1011+
text_style.color = SK_ColorBLACK;
1012+
text_style.height = 1;
1013+
text_style.decoration = TextDecoration::kUnderline;
1014+
text_style.decoration_color = SK_ColorBLACK;
1015+
builder.PushStyle(text_style);
1016+
1017+
builder.AddText(u16_text);
1018+
1019+
builder.Pop();
1020+
1021+
auto paragraph = builder.Build();
1022+
paragraph->Layout(GetTestCanvasWidth() - 100);
1023+
1024+
paragraph->Paint(GetCanvas(), 0, 0);
1025+
1026+
SkPaint paint;
1027+
paint.setStyle(SkPaint::kStroke_Style);
1028+
paint.setAntiAlias(true);
1029+
paint.setStrokeWidth(1);
1030+
1031+
// Tests for GetRectsForRange()
1032+
Paragraph::RectHeightStyle rect_height_style =
1033+
Paragraph::RectHeightStyle::kMax;
1034+
Paragraph::RectWidthStyle rect_width_style =
1035+
Paragraph::RectWidthStyle::kTight;
1036+
paint.setColor(SK_ColorRED);
1037+
std::vector<txt::Paragraph::TextBox> boxes =
1038+
paragraph->GetRectsForRange(0, 100, rect_height_style, rect_width_style);
1039+
for (size_t i = 0; i < boxes.size(); ++i) {
1040+
GetCanvas()->drawRect(boxes[i].rect, paint);
1041+
}
1042+
EXPECT_EQ(boxes.size(), 2ull);
1043+
1044+
EXPECT_FLOAT_EQ(boxes[0].rect.left(), 556.54688);
1045+
EXPECT_FLOAT_EQ(boxes[0].rect.top(), -0.26855469);
1046+
EXPECT_FLOAT_EQ(boxes[0].rect.right(), 900);
1047+
EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 44);
1048+
1049+
EXPECT_FLOAT_EQ(boxes[1].rect.left(), 510.09375);
1050+
EXPECT_FLOAT_EQ(boxes[1].rect.top(), -0.26855469);
1051+
EXPECT_FLOAT_EQ(boxes[1].rect.right(), 557.04688);
1052+
EXPECT_FLOAT_EQ(boxes[1].rect.bottom(), 44);
1053+
1054+
ASSERT_EQ(paragraph_style.text_align,
1055+
paragraph->GetParagraphStyle().text_align);
1056+
1057+
ASSERT_TRUE(Snapshot());
1058+
}
1059+
9741060
TEST_F(ParagraphTest, GetGlyphPositionAtCoordinateParagraph) {
9751061
const char* text =
9761062
"12345 67890 12345 67890 12345 67890 12345 67890 12345 67890 12345 "
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
This Font Software is licensed under the SIL Open Font License,
2+
Version 1.1.
3+
4+
This license is copied below, and is also available with a FAQ at:
5+
http://scripts.sil.org/OFL
6+
7+
-----------------------------------------------------------
8+
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
9+
-----------------------------------------------------------
10+
11+
PREAMBLE
12+
The goals of the Open Font License (OFL) are to stimulate worldwide
13+
development of collaborative font projects, to support the font
14+
creation efforts of academic and linguistic communities, and to
15+
provide a free and open framework in which fonts may be shared and
16+
improved in partnership with others.
17+
18+
The OFL allows the licensed fonts to be used, studied, modified and
19+
redistributed freely as long as they are not sold by themselves. The
20+
fonts, including any derivative works, can be bundled, embedded,
21+
redistributed and/or sold with any software provided that any reserved
22+
names are not used by derivative works. The fonts and derivatives,
23+
however, cannot be released under any other type of license. The
24+
requirement for fonts to remain under this license does not apply to
25+
any document created using the fonts or their derivatives.
26+
27+
DEFINITIONS
28+
"Font Software" refers to the set of files released by the Copyright
29+
Holder(s) under this license and clearly marked as such. This may
30+
include source files, build scripts and documentation.
31+
32+
"Reserved Font Name" refers to any names specified as such after the
33+
copyright statement(s).
34+
35+
"Original Version" refers to the collection of Font Software
36+
components as distributed by the Copyright Holder(s).
37+
38+
"Modified Version" refers to any derivative made by adding to,
39+
deleting, or substituting -- in part or in whole -- any of the
40+
components of the Original Version, by changing formats or by porting
41+
the Font Software to a new environment.
42+
43+
"Author" refers to any designer, engineer, programmer, technical
44+
writer or other person who contributed to the Font Software.
45+
46+
PERMISSION & CONDITIONS
47+
Permission is hereby granted, free of charge, to any person obtaining
48+
a copy of the Font Software, to use, study, copy, merge, embed,
49+
modify, redistribute, and sell modified and unmodified copies of the
50+
Font Software, subject to the following conditions:
51+
52+
1) Neither the Font Software nor any of its individual components, in
53+
Original or Modified Versions, may be sold by itself.
54+
55+
2) Original or Modified Versions of the Font Software may be bundled,
56+
redistributed and/or sold with any software, provided that each copy
57+
contains the above copyright notice and this license. These can be
58+
included either as stand-alone text files, human-readable headers or
59+
in the appropriate machine-readable metadata fields within text or
60+
binary files as long as those fields can be easily viewed by the user.
61+
62+
3) No Modified Version of the Font Software may use the Reserved Font
63+
Name(s) unless explicit written permission is granted by the
64+
corresponding Copyright Holder. This restriction only applies to the
65+
primary font name as presented to the users.
66+
67+
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
68+
Software shall not be used to promote, endorse or advertise any
69+
Modified Version, except to acknowledge the contribution(s) of the
70+
Copyright Holder(s) and the Author(s) or with their explicit written
71+
permission.
72+
73+
5) The Font Software, modified or unmodified, in part or in whole,
74+
must be distributed entirely under this license, and must not be
75+
distributed under any other license. The requirement for fonts to
76+
remain under this license does not apply to any document created using
77+
the Font Software.
78+
79+
TERMINATION
80+
This license becomes null and void if any of the above conditions are
81+
not met.
82+
83+
DISCLAIMER
84+
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
85+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
86+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
87+
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
88+
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
89+
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
90+
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
91+
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
92+
OTHER DEALINGS IN THE FONT SOFTWARE.
Binary file not shown.

0 commit comments

Comments
 (0)