Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 48 additions & 11 deletions third_party/txt/src/txt/paragraph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
#include "paragraph.h"

#include <hb.h>
#include <minikin/Layout.h>

#include <algorithm>
#include <limits>
#include <map>
#include <numeric>
#include <utility>
#include <vector>

#include <minikin/Layout.h>
#include "flutter/fml/logging.h"
#include "font_collection.h"
#include "font_skia.h"
Expand All @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -549,6 +549,19 @@ void Paragraph::Layout(double width, bool force) {
? line_range.end_excluding_whitespace
: line_range.end;

// Build a map of styled runs indexed by start position to TextDirection.
// Once the direction of a non-ghost line run has been established for the
// styled run, we will enforce that the ghost run use the direction of the
// most recently seen bidi run that is part of the styled run. This is to
// ensure that default-LTR whitespace gets the proper directionality when
// used in a RTL run.
std::map<size_t, TextDirection> styled_run_direction_map;
for (size_t i = 0; i < runs_.size(); ++i) {
StyledRuns::Run run = runs_.GetRun(i);
// Initialize TextDirection to LTR as that is what ICU initializes it to.
styled_run_direction_map.emplace(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the emplace adds much value here as the enum value is not expensive to copy.

std::make_pair(run.start, TextDirection::ltr));
}
// Find the runs comprising this line.
std::vector<BidiRun> line_runs;
for (const BidiRun& bidi_run : bidi_runs) {
Expand All @@ -557,6 +570,11 @@ void Paragraph::Layout(double width, bool force) {
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());
// Track the the most recently used text direction for the styled run.
styled_run_direction_map[styled_run_direction_map
.lower_bound(std::max(bidi_run.start(),
line_range.start))
->first] = bidi_run.direction();
}
// A "ghost" run is a run that does not impact the layout, breaking,
// alignment, width, etc but is still "visible" though getRectsForRange.
Expand All @@ -571,9 +589,15 @@ void Paragraph::Layout(double width, bool force) {
line_range.end_excluding_whitespace < line_range.end &&
bidi_run.start() <= line_range.end &&
bidi_run.end() > line_end_index) {
// Use the most recent TextDirection instead of the defaulted LTR value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ParagraphStyle has a field that sets the base TextDirection for the paragraph. What happens in this case if you set that field to RTL?

// ICU provides.
TextDirection run_direction = styled_run_direction_map
[styled_run_direction_map
.lower_bound(std::max(bidi_run.start(), line_range.start))
->first];
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);
run_direction, bidi_run.style(), true);
}
}
bool line_runs_all_rtl =
Expand Down Expand Up @@ -808,12 +832,25 @@ 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<size_t>(run.start(), run.end()),
Range<double>(glyph_positions.front().x_pos.start,
glyph_positions.back().x_pos.end),
line_number, metrics, run.direction());
if (!(run.is_ghost() && run.is_rtl())) {
line_code_unit_runs.emplace_back(
std::move(code_unit_positions),
Range<size_t>(run.start(), run.end()),
Range<double>(glyph_positions.front().x_pos.start,
glyph_positions.back().x_pos.end),
line_number, metrics, run.direction());
} else {
// We custom handle ghost run for RTL as ICU interprets trailing
// whitespace as LTR, which causes trailing space to appear at the
// right side instead of the (correct) left side. Here, we manually
// add the equivalent width to the left side instead.
line_code_unit_runs.emplace_back(
std::move(code_unit_positions),
Range<size_t>(run.start(), run.end()),
Range<double>(run_x_offset - glyph_positions.back().x_pos.start,
run_x_offset - 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);
Expand Down
67 changes: 67 additions & 0 deletions third_party/txt/tests/paragraph_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,73 @@ 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;
txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());

txt::TextStyle text_style;
text_style.font_families = std::vector<std::string>(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<txt::Paragraph::TextBox> 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(), 556.04688);
EXPECT_FLOAT_EQ(boxes[1].rect.top(), -0.26855469);
EXPECT_FLOAT_EQ(boxes[1].rect.right(), 544.30859);
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 "
Expand Down
92 changes: 92 additions & 0 deletions third_party/txt/third_party/fonts/NotoNaskhArabic-LICENSE.txt
Original file line number Diff line number Diff line change
@@ -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.
Binary file not shown.