diff --git a/third_party/txt/src/txt/paragraph.cc b/third_party/txt/src/txt/paragraph.cc index 393b28be4f331..01ced24889ca6 100644 --- a/third_party/txt/src/txt/paragraph.cc +++ b/third_party/txt/src/txt/paragraph.cc @@ -17,6 +17,8 @@ #include "paragraph.h" #include +#include + #include #include #include @@ -24,7 +26,6 @@ #include #include -#include #include "flutter/fml/logging.h" #include "font_collection.h" #include "font_skia.h" @@ -34,9 +35,6 @@ #include "minikin/LayoutUtils.h" #include "minikin/LineBreaker.h" #include "minikin/MinikinFont.h" -#include "unicode/ubidi.h" -#include "unicode/utf16.h" - #include "third_party/skia/include/core/SkCanvas.h" #include "third_party/skia/include/core/SkFont.h" #include "third_party/skia/include/core/SkFontMetrics.h" @@ -46,6 +44,8 @@ #include "third_party/skia/include/core/SkTypeface.h" #include "third_party/skia/include/effects/SkDashPathEffect.h" #include "third_party/skia/include/effects/SkDiscretePathEffect.h" +#include "unicode/ubidi.h" +#include "unicode/utf16.h" namespace txt { namespace { @@ -552,14 +552,8 @@ void Paragraph::Layout(double width, bool force) { // Find the runs comprising this line. std::vector line_runs; for (const BidiRun& bidi_run : bidi_runs) { - if (bidi_run.start() < line_end_index && - bidi_run.end() > line_range.start) { - line_runs.emplace_back(std::max(bidi_run.start(), line_range.start), - std::min(bidi_run.end(), line_end_index), - bidi_run.direction(), bidi_run.style()); - } // A "ghost" run is a run that does not impact the layout, breaking, - // alignment, width, etc but is still "visible" though getRectsForRange. + // alignment, width, etc but is still "visible" through getRectsForRange. // For example, trailing whitespace on centered text can be scrolled // through with the caret but will not wrap the line. // @@ -567,13 +561,30 @@ void Paragraph::Layout(double width, bool force) { // let it impact metrics. After layout of the whitespace run, we do not // add its width into the x-offset adjustment, effectively nullifying its // impact on the layout. + std::unique_ptr ghost_run = nullptr; if (paragraph_style_.ellipsis.empty() && line_range.end_excluding_whitespace < line_range.end && bidi_run.start() <= line_range.end && bidi_run.end() > line_end_index) { - line_runs.emplace_back(std::max(bidi_run.start(), line_end_index), - std::min(bidi_run.end(), line_range.end), - bidi_run.direction(), bidi_run.style(), true); + ghost_run = std::make_unique( + std::max(bidi_run.start(), line_end_index), + std::min(bidi_run.end(), line_range.end), bidi_run.direction(), + bidi_run.style(), true); + } + // Include the ghost run before normal run if RTL + if (bidi_run.direction() == TextDirection::rtl && ghost_run != nullptr) { + line_runs.push_back(*ghost_run); + } + // Emplace a normal line run. + if (bidi_run.start() < line_end_index && + bidi_run.end() > line_range.start) { + line_runs.emplace_back(std::max(bidi_run.start(), line_range.start), + std::min(bidi_run.end(), line_end_index), + bidi_run.direction(), bidi_run.style()); + } + // Include the ghost run after normal run if LTR + if (bidi_run.direction() == TextDirection::ltr && ghost_run != nullptr) { + line_runs.push_back(*ghost_run); } } bool line_runs_all_rtl = @@ -661,6 +672,17 @@ void Paragraph::Layout(double width, bool force) { if (layout.nGlyphs() == 0) continue; + // When laying out RTL ghost runs, shift the run_x_offset here by the + // advance so that the ghost run is positioned to the left of the first + // real run of text in the line. However, since we do not want it to + // impact the layout of real text, this advance is subsequently added + // back into the run_x_offset after the ghost run positions have been + // calcuated and before the next real run of text is laid out, ensuring + // later runs are laid out in the same position as if there were no ghost + // run. + if (run.is_ghost() && run.is_rtl()) + run_x_offset -= layout.getAdvance(); + std::vector layout_advances(text_count); layout.getAdvances(layout_advances.data()); @@ -808,6 +830,7 @@ void Paragraph::Layout(double width, bool force) { [](const GlyphPosition& a, const GlyphPosition& b) { return a.code_units.start < b.code_units.start; }); + line_code_unit_runs.emplace_back( std::move(code_unit_positions), Range(run.start(), run.end()), @@ -815,14 +838,17 @@ void Paragraph::Layout(double width, bool force) { glyph_positions.back().x_pos.end), line_number, metrics, run.direction()); - min_left_ = std::min(min_left_, glyph_positions.front().x_pos.start); - max_right_ = std::max(max_right_, glyph_positions.back().x_pos.end); + if (!run.is_ghost()) { + min_left_ = std::min(min_left_, glyph_positions.front().x_pos.start); + max_right_ = std::max(max_right_, glyph_positions.back().x_pos.end); + } } // for each in glyph_blobs - - // Do not increase x offset for trailing ghost runs as it should not - // impact the layout of visible glyphs. We do keep the record though so - // GetRectsForRange() can find metrics for trailing spaces. - if (!run.is_ghost()) { + // 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 diff --git a/third_party/txt/tests/paragraph_unittests.cc b/third_party/txt/tests/paragraph_unittests.cc index bab5430abc4b3..525bce6b5f9d9 100644 --- a/third_party/txt/tests/paragraph_unittests.cc +++ b/third_party/txt/tests/paragraph_unittests.cc @@ -452,7 +452,7 @@ TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(RightAlignParagraph)) { // than width_. ASSERT_DOUBLE_EQ(paragraph->width_, available_width); ASSERT_TRUE(paragraph->tight_width_ < available_width); - ASSERT_DOUBLE_EQ(paragraph->tight_width_, 893.19921875); + ASSERT_DOUBLE_EQ(paragraph->tight_width_, 880.765625); ASSERT_TRUE(paragraph->records_[2].style().equals(text_style)); ASSERT_DOUBLE_EQ(paragraph->records_[2].offset().y(), expected_y); @@ -706,6 +706,7 @@ TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(JustifyRTL)) { txt::ParagraphStyle paragraph_style; paragraph_style.max_lines = 14; paragraph_style.text_align = TextAlign::justify; + paragraph_style.text_direction = TextDirection::rtl; txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection()); txt::TextStyle text_style; @@ -725,16 +726,33 @@ TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(JustifyRTL)) { paragraph->Paint(GetCanvas(), 0, 0); - ASSERT_TRUE(Snapshot()); - auto glyph_line_width = [¶graph](int index) { size_t second_to_last_position_index = - paragraph->glyph_lines_[index].positions.size() - 2; + paragraph->glyph_lines_[index].positions.size() - 1; return paragraph->glyph_lines_[index] .positions[second_to_last_position_index] .x_pos.end; }; + SkPaint paint; + paint.setStyle(SkPaint::kStroke_Style); + paint.setAntiAlias(true); + paint.setStrokeWidth(1); + + // Tests for GetRectsForRange() + Paragraph::RectHeightStyle rect_height_style = + Paragraph::RectHeightStyle::kMax; + Paragraph::RectWidthStyle rect_width_style = + Paragraph::RectWidthStyle::kTight; + paint.setColor(SK_ColorRED); + std::vector boxes = + paragraph->GetRectsForRange(0, 100, rect_height_style, rect_width_style); + for (size_t i = 0; i < boxes.size(); ++i) { + GetCanvas()->drawRect(boxes[i].rect, paint); + } + ASSERT_EQ(boxes.size(), 5ull); + ASSERT_TRUE(Snapshot()); + // All lines except the last should be justified to the width of the // paragraph. for (size_t i = 0; i < paragraph->glyph_lines_.size() - 1; ++i) { @@ -971,6 +989,74 @@ TEST_F(ParagraphTest, DISABLED_ArabicParagraph) { ASSERT_TRUE(Snapshot()); } +// Checks if the rects are in the correct positions after typing spaces in +// Arabic. +TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(ArabicRectsParagraph)) { + const char* text = "بمباركة التقليدية قام عن. تصفح يد "; + auto icu_text = icu::UnicodeString::fromUTF8(text); + std::u16string u16_text(icu_text.getBuffer(), + icu_text.getBuffer() + icu_text.length()); + + txt::ParagraphStyle paragraph_style; + paragraph_style.max_lines = 14; + paragraph_style.text_align = TextAlign::right; + paragraph_style.text_direction = TextDirection::rtl; + txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection()); + + txt::TextStyle text_style; + text_style.font_families = std::vector(1, "Noto Naskh Arabic"); + text_style.font_size = 26; + text_style.letter_spacing = 1; + text_style.word_spacing = 5; + text_style.color = SK_ColorBLACK; + text_style.height = 1; + text_style.decoration = TextDecoration::kUnderline; + text_style.decoration_color = SK_ColorBLACK; + builder.PushStyle(text_style); + + builder.AddText(u16_text); + + builder.Pop(); + + auto paragraph = builder.Build(); + paragraph->Layout(GetTestCanvasWidth() - 100); + + paragraph->Paint(GetCanvas(), 0, 0); + + SkPaint paint; + paint.setStyle(SkPaint::kStroke_Style); + paint.setAntiAlias(true); + paint.setStrokeWidth(1); + + // Tests for GetRectsForRange() + Paragraph::RectHeightStyle rect_height_style = + Paragraph::RectHeightStyle::kMax; + Paragraph::RectWidthStyle rect_width_style = + Paragraph::RectWidthStyle::kTight; + paint.setColor(SK_ColorRED); + std::vector boxes = + paragraph->GetRectsForRange(0, 100, rect_height_style, rect_width_style); + for (size_t i = 0; i < boxes.size(); ++i) { + GetCanvas()->drawRect(boxes[i].rect, paint); + } + EXPECT_EQ(boxes.size(), 2ull); + + EXPECT_FLOAT_EQ(boxes[0].rect.left(), 556.54688); + EXPECT_FLOAT_EQ(boxes[0].rect.top(), -0.26855469); + EXPECT_FLOAT_EQ(boxes[0].rect.right(), 900); + EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 44); + + EXPECT_FLOAT_EQ(boxes[1].rect.left(), 510.09375); + EXPECT_FLOAT_EQ(boxes[1].rect.top(), -0.26855469); + EXPECT_FLOAT_EQ(boxes[1].rect.right(), 557.04688); + EXPECT_FLOAT_EQ(boxes[1].rect.bottom(), 44); + + ASSERT_EQ(paragraph_style.text_align, + paragraph->GetParagraphStyle().text_align); + + ASSERT_TRUE(Snapshot()); +} + TEST_F(ParagraphTest, GetGlyphPositionAtCoordinateParagraph) { const char* text = "12345 67890 12345 67890 12345 67890 12345 67890 12345 67890 12345 " diff --git a/third_party/txt/third_party/fonts/NotoNaskhArabic-LICENSE.txt b/third_party/txt/third_party/fonts/NotoNaskhArabic-LICENSE.txt new file mode 100644 index 0000000000000..d952d62c065f3 --- /dev/null +++ b/third_party/txt/third_party/fonts/NotoNaskhArabic-LICENSE.txt @@ -0,0 +1,92 @@ +This Font Software is licensed under the SIL Open Font License, +Version 1.1. + +This license is copied below, and is also available with a FAQ at: +http://scripts.sil.org/OFL + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font +creation efforts of academic and linguistic communities, and to +provide a free and open framework in which fonts may be shared and +improved in partnership with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply to +any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software +components as distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, +deleting, or substituting -- in part or in whole -- any of the +components of the Original Version, by changing formats or by porting +the Font Software to a new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, +modify, redistribute, and sell modified and unmodified copies of the +Font Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, in +Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the +corresponding Copyright Holder. This restriction only applies to the +primary font name as presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created using +the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/third_party/txt/third_party/fonts/NotoNaskhArabic-Regular.ttf b/third_party/txt/third_party/fonts/NotoNaskhArabic-Regular.ttf new file mode 100644 index 0000000000000..ee6cdaa35bc0e Binary files /dev/null and b/third_party/txt/third_party/fonts/NotoNaskhArabic-Regular.ttf differ