Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1 | // Copyright 2019 Google LLC. |
| 2 | #include <sstream> |
Julia Lavrova | db543f6 | 2020-01-29 11:43:41 -0500 | [diff] [blame^] | 3 | #include <thread> |
Julia Lavrova | 6e6333f | 2019-06-17 10:34:10 -0400 | [diff] [blame] | 4 | #include "modules/skparagraph/include/TypefaceFontProvider.h" |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 5 | #include "modules/skparagraph/src/ParagraphBuilderImpl.h" |
| 6 | #include "modules/skparagraph/src/ParagraphImpl.h" |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 7 | #include "modules/skparagraph/utils/TestFontCollection.h" |
Julia Lavrova | db543f6 | 2020-01-29 11:43:41 -0500 | [diff] [blame^] | 8 | #include "src/core/SkFontMgrPriv.h" |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 9 | #include "src/core/SkOSFile.h" |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 10 | #include "src/utils/SkOSPath.h" |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 11 | #include "src/utils/SkShaperJSONWriter.h" |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 12 | #include "tests/CodecPriv.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 13 | #include "tests/Test.h" |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 14 | #include "tools/Resources.h" |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 15 | #include "tools/ToolUtils.h" |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 16 | |
Julia Lavrova | b30d481 | 2019-11-08 14:51:16 -0500 | [diff] [blame] | 17 | #include "third_party/icu/SkLoadICU.h" |
| 18 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 19 | #define VeryLongCanvasWidth 1000000 |
| 20 | #define TestCanvasWidth 1000 |
| 21 | #define TestCanvasHeight 600 |
| 22 | |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 23 | #define DEF_TEST_DISABLED(name, reporter) \ |
| 24 | static void test_##name(skiatest::Reporter* reporter, const GrContextOptions&); \ |
| 25 | skiatest::TestRegistry name##TestRegistry(skiatest::Test(#name, false, test_##name)); \ |
Herb Derby | 126d7a2 | 2019-11-27 11:38:16 -0500 | [diff] [blame] | 26 | void test_##name(skiatest::Reporter* reporter, const GrContextOptions&) { /* SkDebugf("Disabled:"#name "\n"); */ } \ |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 27 | void disabled_##name(skiatest::Reporter* reporter, const GrContextOptions&) |
| 28 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 29 | using namespace skia::textlayout; |
| 30 | namespace { |
| 31 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 32 | SkScalar EPSILON100 = 0.01f; |
| 33 | SkScalar EPSILON50 = 0.02f; |
| 34 | SkScalar EPSILON20 = 0.05f; |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 35 | SkScalar EPSILON10 = 0.1f; |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 36 | SkScalar EPSILON5 = 0.20f; |
| 37 | SkScalar EPSILON2 = 0.50f; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 38 | |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 39 | bool equal(const char* base, TextRange a, const char* b) { |
| 40 | return std::strncmp(b, base + a.start, a.width()) == 0; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 41 | } |
Julia Lavrova | 3281b96 | 2019-12-02 11:32:25 -0500 | [diff] [blame] | 42 | |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 43 | class ResourceFontCollection : public FontCollection { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 44 | public: |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 45 | ResourceFontCollection(bool testOnly = false) |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 46 | : fFontsFound(false) |
| 47 | , fResolvedFonts(0) |
| 48 | , fResourceDir(GetResourcePath("fonts").c_str()) |
| 49 | , fFontProvider(sk_make_sp<TypefaceFontProvider>()) { |
| 50 | std::vector<SkString> fonts; |
| 51 | SkOSFile::Iter iter(fResourceDir.c_str()); |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 52 | |
Julia Lavrova | b30d481 | 2019-11-08 14:51:16 -0500 | [diff] [blame] | 53 | if (!SkLoadICU()) { |
| 54 | SkDebugf("ICU not loaded, skipping all the tests\n"); |
| 55 | return; |
| 56 | } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 57 | SkString path; |
| 58 | while (iter.next(&path)) { |
| 59 | if (path.endsWith("Roboto-Italic.ttf")) { |
| 60 | fFontsFound = true; |
| 61 | } |
| 62 | fonts.emplace_back(path); |
| 63 | } |
| 64 | |
| 65 | if (!fFontsFound) { |
Herb Derby | 126d7a2 | 2019-11-27 11:38:16 -0500 | [diff] [blame] | 66 | // SkDebugf("Fonts not found, skipping all the tests\n"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 67 | return; |
| 68 | } |
| 69 | // Only register fonts if we have to |
| 70 | for (auto& font : fonts) { |
| 71 | SkString file_path; |
| 72 | file_path.printf("%s/%s", fResourceDir.c_str(), font.c_str()); |
| 73 | fFontProvider->registerTypeface(SkTypeface::MakeFromFile(file_path.c_str())); |
| 74 | } |
| 75 | |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 76 | if (testOnly) { |
| 77 | this->setTestFontManager(std::move(fFontProvider)); |
| 78 | } else { |
| 79 | this->setAssetFontManager(std::move(fFontProvider)); |
| 80 | } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 81 | this->disableFontFallback(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 82 | } |
| 83 | |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 84 | ~ResourceFontCollection() = default; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 85 | |
| 86 | size_t resolvedFonts() const { return fResolvedFonts; } |
| 87 | |
| 88 | // TODO: temp solution until we check in fonts |
| 89 | bool fontsFound() const { return fFontsFound; } |
| 90 | |
| 91 | private: |
| 92 | bool fFontsFound; |
| 93 | size_t fResolvedFonts; |
| 94 | std::string fResourceDir; |
| 95 | sk_sp<TypefaceFontProvider> fFontProvider; |
| 96 | }; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 97 | |
| 98 | class TestCanvas { |
| 99 | public: |
| 100 | TestCanvas(const char* testName) : name(testName) { |
| 101 | bits.allocN32Pixels(TestCanvasWidth, TestCanvasHeight); |
| 102 | canvas = new SkCanvas(bits); |
| 103 | canvas->clear(SK_ColorWHITE); |
| 104 | } |
| 105 | |
| 106 | ~TestCanvas() { |
Julia Lavrova | aae845f | 2019-08-16 16:44:40 -0400 | [diff] [blame] | 107 | SkString tmpDir = skiatest::GetTmpDir(); |
| 108 | if (!tmpDir.isEmpty()) { |
| 109 | SkString path = SkOSPath::Join(tmpDir.c_str(), name); |
| 110 | SkFILEWStream file(path.c_str()); |
| 111 | if (!SkEncodeImage(&file, bits, SkEncodedImageFormat::kPNG, 100)) { |
| 112 | SkDebugf("Cannot write a picture %s\n", name); |
| 113 | } |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 114 | } |
| 115 | delete canvas; |
| 116 | } |
| 117 | |
| 118 | void drawRects(SkColor color, std::vector<TextBox>& result, bool fill = false) { |
| 119 | |
| 120 | SkPaint paint; |
| 121 | if (!fill) { |
| 122 | paint.setStyle(SkPaint::kStroke_Style); |
| 123 | paint.setAntiAlias(true); |
| 124 | paint.setStrokeWidth(1); |
| 125 | } |
| 126 | paint.setColor(color); |
| 127 | for (auto& r : result) { |
| 128 | canvas->drawRect(r.rect, paint); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | void drawLine(SkColor color, SkRect rect, bool vertical = true) { |
| 133 | |
| 134 | SkPaint paint; |
| 135 | paint.setStyle(SkPaint::kStroke_Style); |
| 136 | paint.setAntiAlias(true); |
| 137 | paint.setStrokeWidth(1); |
| 138 | paint.setColor(color); |
| 139 | if (vertical) { |
| 140 | canvas->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint); |
| 141 | } else { |
| 142 | canvas->drawLine(rect.fLeft, rect.fTop, rect.fRight, rect.fTop, paint); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | void drawLines(SkColor color, std::vector<TextBox>& result) { |
| 147 | |
| 148 | for (auto& r : result) { |
| 149 | drawLine(color, r.rect); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | SkCanvas* get() { return canvas; } |
| 154 | private: |
| 155 | SkBitmap bits; |
| 156 | SkCanvas* canvas; |
| 157 | const char* name; |
| 158 | }; |
| 159 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 160 | } // namespace |
| 161 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 162 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 163 | DEF_TEST(SkParagraph_SimpleParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 164 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 165 | if (!fontCollection->fontsFound()) return; |
| 166 | const char* text = "Hello World Text Dialog"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 167 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 168 | |
| 169 | ParagraphStyle paragraph_style; |
| 170 | paragraph_style.turnHintingOff(); |
| 171 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 172 | |
| 173 | TextStyle text_style; |
| 174 | text_style.setFontFamilies({SkString("Roboto")}); |
| 175 | text_style.setColor(SK_ColorBLACK); |
| 176 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 177 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 178 | builder.pop(); |
| 179 | |
| 180 | auto paragraph = builder.Build(); |
| 181 | paragraph->layout(TestCanvasWidth); |
Julia Lavrova | 3281b96 | 2019-12-02 11:32:25 -0500 | [diff] [blame] | 182 | REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 183 | |
| 184 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 185 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 186 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); // paragraph style does not count |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 187 | REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 188 | |
| 189 | size_t index = 0; |
| 190 | for (auto& line : impl->lines()) { |
| 191 | line.scanStyles(StyleType::kDecorations, |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 192 | [&index, reporter] |
| 193 | (TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 194 | REPORTER_ASSERT(reporter, index == 0); |
| 195 | REPORTER_ASSERT(reporter, style.getColor() == SK_ColorBLACK); |
| 196 | ++index; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 197 | }); |
| 198 | } |
| 199 | } |
| 200 | |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 201 | DEF_TEST(SkParagraph_InlinePlaceholderParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 202 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 203 | TestCanvas canvas("SkParagraph_InlinePlaceholderParagraph.png"); |
| 204 | if (!fontCollection->fontsFound()) return; |
| 205 | |
| 206 | const char* text = "012 34"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 207 | const size_t len = strlen(text); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 208 | |
| 209 | ParagraphStyle paragraph_style; |
| 210 | paragraph_style.turnHintingOff(); |
| 211 | paragraph_style.setMaxLines(14); |
| 212 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 213 | |
| 214 | TextStyle text_style; |
| 215 | text_style.setFontFamilies({SkString("Roboto")}); |
| 216 | text_style.setColor(SK_ColorBLACK); |
| 217 | text_style.setFontSize(26); |
| 218 | text_style.setWordSpacing(5); |
| 219 | text_style.setLetterSpacing(1); |
| 220 | text_style.setDecoration(TextDecoration::kUnderline); |
| 221 | text_style.setDecorationColor(SK_ColorBLACK); |
| 222 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 223 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 224 | |
| 225 | PlaceholderStyle placeholder1(50, 50, PlaceholderAlignment::kBaseline, TextBaseline::kAlphabetic, 0); |
| 226 | builder.addPlaceholder(placeholder1); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 227 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 228 | builder.addPlaceholder(placeholder1); |
| 229 | |
| 230 | PlaceholderStyle placeholder2(5, 50, PlaceholderAlignment::kBaseline, TextBaseline::kAlphabetic, 50); |
| 231 | builder.addPlaceholder(placeholder2); |
| 232 | builder.addPlaceholder(placeholder1); |
| 233 | builder.addPlaceholder(placeholder2); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 234 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 235 | builder.addPlaceholder(placeholder2); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 236 | builder.addText(text, len); |
| 237 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 238 | builder.addPlaceholder(placeholder2); |
| 239 | builder.addPlaceholder(placeholder2); |
| 240 | builder.addPlaceholder(placeholder2); |
| 241 | builder.addPlaceholder(placeholder2); |
| 242 | builder.addPlaceholder(placeholder2); |
| 243 | builder.addPlaceholder(placeholder1); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 244 | builder.addText(text, len); |
| 245 | builder.addText(text, len); |
| 246 | builder.addText(text, len); |
| 247 | builder.addText(text, len); |
| 248 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 249 | builder.addPlaceholder(placeholder2); |
| 250 | builder.addPlaceholder(placeholder1); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 251 | builder.addText(text, len); |
| 252 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 253 | |
| 254 | builder.pop(); |
| 255 | |
| 256 | auto paragraph = builder.Build(); |
| 257 | paragraph->layout(TestCanvasWidth); |
| 258 | paragraph->paint(canvas.get(), 0, 0); |
| 259 | |
| 260 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 261 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 262 | |
| 263 | auto boxes = paragraph->getRectsForRange(0, 3, rect_height_style, rect_width_style); |
| 264 | canvas.drawRects(SK_ColorRED, boxes); |
| 265 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 266 | |
| 267 | boxes = paragraph->getRectsForRange(0, 3, rect_height_style, rect_width_style); |
| 268 | canvas.drawRects(SK_ColorGREEN, boxes); |
| 269 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 270 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 271 | boxes = paragraph->getRectsForPlaceholders(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 272 | canvas.drawRects(SK_ColorRED, boxes); |
| 273 | |
| 274 | boxes = paragraph->getRectsForRange(4, 17, rect_height_style, rect_width_style); |
| 275 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 276 | |
| 277 | REPORTER_ASSERT(reporter, boxes.size() == 7); |
| 278 | |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 279 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.left(), 90.921f, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 280 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.top(), 50, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 281 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.right(), 90.921f + 50, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 282 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.bottom(), 100, EPSILON100)); |
| 283 | |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 284 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[3].rect.left(), 231.343f, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 285 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[3].rect.top(), 50, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 286 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[3].rect.right(), 231.343f + 50, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 287 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[3].rect.bottom(), 100, EPSILON100)); |
| 288 | |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 289 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[4].rect.left(), 281.343f, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 290 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[4].rect.top(), 0, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 291 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[4].rect.right(), 281.343f + 5, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 292 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[4].rect.bottom(), 50, EPSILON100)); |
| 293 | |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 294 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[6].rect.left(), 336.343f, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 295 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[6].rect.top(), 0, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 296 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[6].rect.right(), 336.343f + 5, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 297 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[6].rect.bottom(), 50, EPSILON100)); |
| 298 | } |
| 299 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 300 | DEF_TEST(SkParagraph_InlinePlaceholderBaselineParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 301 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 302 | TestCanvas canvas("SkParagraph_InlinePlaceholderBaselineParagraph.png"); |
| 303 | if (!fontCollection->fontsFound()) return; |
| 304 | |
| 305 | const char* text = "012 34"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 306 | const size_t len = strlen(text); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 307 | |
| 308 | ParagraphStyle paragraph_style; |
| 309 | paragraph_style.turnHintingOff(); |
| 310 | paragraph_style.setMaxLines(14); |
| 311 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 312 | |
| 313 | TextStyle text_style; |
| 314 | text_style.setFontFamilies({SkString("Roboto")}); |
| 315 | text_style.setColor(SK_ColorBLACK); |
| 316 | text_style.setFontSize(26); |
| 317 | text_style.setWordSpacing(5); |
| 318 | text_style.setLetterSpacing(1); |
| 319 | text_style.setDecoration(TextDecoration::kUnderline); |
| 320 | text_style.setDecorationColor(SK_ColorBLACK); |
| 321 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 322 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 323 | |
| 324 | PlaceholderStyle placeholder(55, 50, PlaceholderAlignment::kBaseline, TextBaseline::kAlphabetic, 38.347f); |
| 325 | builder.addPlaceholder(placeholder); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 326 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 327 | |
| 328 | builder.pop(); |
| 329 | |
| 330 | auto paragraph = builder.Build(); |
| 331 | paragraph->layout(TestCanvasWidth); |
| 332 | paragraph->paint(canvas.get(), 0, 0); |
| 333 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 334 | auto boxes = paragraph->getRectsForPlaceholders(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 335 | canvas.drawRects(SK_ColorRED, boxes); |
| 336 | |
| 337 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 338 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 90.921f, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 339 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 340 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f + 55, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 341 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 50, EPSILON100)); |
| 342 | |
| 343 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 344 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 345 | |
| 346 | boxes = paragraph->getRectsForRange(5, 6, rect_height_style, rect_width_style); |
| 347 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 348 | |
| 349 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 350 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 75.324f, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 351 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 14.226f, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 352 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 353 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 44.694f, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 354 | } |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 355 | |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 356 | DEF_TEST(SkParagraph_InlinePlaceholderAboveBaselineParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 357 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 358 | TestCanvas canvas("SkParagraph_InlinePlaceholderAboveBaselineParagraph.png"); |
| 359 | if (!fontCollection->fontsFound()) return; |
| 360 | |
| 361 | const char* text = "012 34"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 362 | const size_t len = strlen(text); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 363 | |
| 364 | ParagraphStyle paragraph_style; |
| 365 | paragraph_style.turnHintingOff(); |
| 366 | paragraph_style.setMaxLines(14); |
| 367 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 368 | |
| 369 | TextStyle text_style; |
| 370 | text_style.setFontFamilies({SkString("Roboto")}); |
| 371 | text_style.setColor(SK_ColorBLACK); |
| 372 | text_style.setFontSize(26); |
| 373 | text_style.setWordSpacing(5); |
| 374 | text_style.setLetterSpacing(1); |
| 375 | text_style.setDecoration(TextDecoration::kUnderline); |
| 376 | text_style.setDecorationColor(SK_ColorBLACK); |
| 377 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 378 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 379 | |
| 380 | PlaceholderStyle placeholder(55, 50, PlaceholderAlignment::kAboveBaseline, TextBaseline::kAlphabetic, 903129.129308f); |
| 381 | builder.addPlaceholder(placeholder); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 382 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 383 | |
| 384 | builder.pop(); |
| 385 | |
| 386 | auto paragraph = builder.Build(); |
| 387 | paragraph->layout(TestCanvasWidth); |
| 388 | paragraph->paint(canvas.get(), 0, 0); |
| 389 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 390 | auto boxes = paragraph->getRectsForPlaceholders(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 391 | canvas.drawRects(SK_ColorRED, boxes); |
| 392 | |
| 393 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 394 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 90.921f, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 395 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), -0.347f, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 396 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f + 55, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 397 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 49.652f, EPSILON100)); |
| 398 | |
| 399 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 400 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 401 | |
| 402 | boxes = paragraph->getRectsForRange(5, 6, rect_height_style, rect_width_style); |
| 403 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 404 | |
| 405 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 406 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 75.324f, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 407 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 25.531f, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 408 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 409 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 56, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 410 | } |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 411 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 412 | DEF_TEST(SkParagraph_InlinePlaceholderBelowBaselineParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 413 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 414 | TestCanvas canvas("SkParagraph_InlinePlaceholderBelowBaselineParagraph.png"); |
| 415 | if (!fontCollection->fontsFound()) return; |
| 416 | |
| 417 | const char* text = "012 34"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 418 | const size_t len = strlen(text); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 419 | |
| 420 | ParagraphStyle paragraph_style; |
| 421 | paragraph_style.turnHintingOff(); |
| 422 | paragraph_style.setMaxLines(14); |
| 423 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 424 | |
| 425 | TextStyle text_style; |
| 426 | text_style.setFontFamilies({SkString("Roboto")}); |
| 427 | text_style.setColor(SK_ColorBLACK); |
| 428 | text_style.setFontSize(26); |
| 429 | text_style.setWordSpacing(5); |
| 430 | text_style.setLetterSpacing(1); |
| 431 | text_style.setDecoration(TextDecoration::kUnderline); |
| 432 | text_style.setDecorationColor(SK_ColorBLACK); |
| 433 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 434 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 435 | |
| 436 | PlaceholderStyle placeholder(55, 50, PlaceholderAlignment::kBelowBaseline, TextBaseline::kAlphabetic, 903129.129308f); |
| 437 | builder.addPlaceholder(placeholder); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 438 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 439 | |
| 440 | builder.pop(); |
| 441 | |
| 442 | auto paragraph = builder.Build(); |
| 443 | paragraph->layout(TestCanvasWidth); |
| 444 | paragraph->paint(canvas.get(), 0, 0); |
| 445 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 446 | auto boxes = paragraph->getRectsForPlaceholders(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 447 | canvas.drawRects(SK_ColorRED, boxes); |
| 448 | |
| 449 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 450 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 90.921f, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 451 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 24, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 452 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f + 55, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 453 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 74, EPSILON100)); |
| 454 | |
| 455 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 456 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 457 | |
| 458 | boxes = paragraph->getRectsForRange(5, 6, rect_height_style, rect_width_style); |
| 459 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 460 | |
| 461 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 462 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 75.324f, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 463 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), -0.121f, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 464 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 465 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 30.347f, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 466 | } |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 467 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 468 | DEF_TEST(SkParagraph_InlinePlaceholderBottomParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 469 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 470 | TestCanvas canvas("SkParagraph_InlinePlaceholderBottomParagraph.png"); |
| 471 | if (!fontCollection->fontsFound()) return; |
| 472 | |
| 473 | const char* text = "012 34"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 474 | const size_t len = strlen(text); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 475 | |
| 476 | ParagraphStyle paragraph_style; |
| 477 | paragraph_style.turnHintingOff(); |
| 478 | paragraph_style.setMaxLines(14); |
| 479 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 480 | |
| 481 | TextStyle text_style; |
| 482 | text_style.setFontFamilies({SkString("Roboto")}); |
| 483 | text_style.setColor(SK_ColorBLACK); |
| 484 | text_style.setFontSize(26); |
| 485 | text_style.setWordSpacing(5); |
| 486 | text_style.setLetterSpacing(1); |
| 487 | text_style.setDecoration(TextDecoration::kUnderline); |
| 488 | text_style.setDecorationColor(SK_ColorBLACK); |
| 489 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 490 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 491 | |
| 492 | PlaceholderStyle placeholder(55, 50, PlaceholderAlignment::kBottom, TextBaseline::kAlphabetic, 0); |
| 493 | builder.addPlaceholder(placeholder); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 494 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 495 | |
| 496 | builder.pop(); |
| 497 | |
| 498 | auto paragraph = builder.Build(); |
| 499 | paragraph->layout(TestCanvasWidth); |
| 500 | paragraph->paint(canvas.get(), 0, 0); |
| 501 | |
| 502 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 503 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 504 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 505 | auto boxes = paragraph->getRectsForPlaceholders(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 506 | canvas.drawRects(SK_ColorRED, boxes); |
| 507 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 508 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 90.921f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 509 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 510 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f + 55, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 511 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 50, EPSILON100)); |
| 512 | |
| 513 | boxes = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style); |
| 514 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 515 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 516 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0.5f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 517 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 19.531f, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 518 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 16.097f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 519 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 50, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 520 | } |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 521 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 522 | DEF_TEST(SkParagraph_InlinePlaceholderTopParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 523 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 524 | TestCanvas canvas("SkParagraph_InlinePlaceholderTopParagraph.png"); |
| 525 | if (!fontCollection->fontsFound()) return; |
| 526 | |
| 527 | const char* text = "012 34"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 528 | const size_t len = strlen(text); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 529 | |
| 530 | ParagraphStyle paragraph_style; |
| 531 | paragraph_style.turnHintingOff(); |
| 532 | paragraph_style.setMaxLines(14); |
| 533 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 534 | |
| 535 | TextStyle text_style; |
| 536 | text_style.setFontFamilies({SkString("Roboto")}); |
| 537 | text_style.setColor(SK_ColorBLACK); |
| 538 | text_style.setFontSize(26); |
| 539 | text_style.setWordSpacing(5); |
| 540 | text_style.setLetterSpacing(1); |
| 541 | text_style.setDecoration(TextDecoration::kUnderline); |
| 542 | text_style.setDecorationColor(SK_ColorBLACK); |
| 543 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 544 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 545 | |
| 546 | PlaceholderStyle placeholder(55, 50, PlaceholderAlignment::kTop, TextBaseline::kAlphabetic, 0); |
| 547 | builder.addPlaceholder(placeholder); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 548 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 549 | |
| 550 | builder.pop(); |
| 551 | |
| 552 | auto paragraph = builder.Build(); |
| 553 | paragraph->layout(TestCanvasWidth); |
| 554 | paragraph->paint(canvas.get(), 0, 0); |
| 555 | |
| 556 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 557 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 558 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 559 | auto boxes = paragraph->getRectsForPlaceholders(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 560 | canvas.drawRects(SK_ColorRED, boxes); |
| 561 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 562 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 90.921f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 563 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 564 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f + 55, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 565 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 50, EPSILON100)); |
| 566 | |
| 567 | boxes = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style); |
| 568 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 569 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 570 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0.5f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 571 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 572 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 16.097f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 573 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 30.468f, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 574 | } |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 575 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 576 | DEF_TEST(SkParagraph_InlinePlaceholderMiddleParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 577 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 578 | TestCanvas canvas("SkParagraph_InlinePlaceholderMiddleParagraph.png"); |
| 579 | if (!fontCollection->fontsFound()) return; |
| 580 | |
| 581 | const char* text = "012 34"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 582 | const size_t len = strlen(text); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 583 | |
| 584 | ParagraphStyle paragraph_style; |
| 585 | paragraph_style.turnHintingOff(); |
| 586 | paragraph_style.setMaxLines(14); |
| 587 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 588 | |
| 589 | TextStyle text_style; |
| 590 | text_style.setFontFamilies({SkString("Roboto")}); |
| 591 | text_style.setColor(SK_ColorBLACK); |
| 592 | text_style.setFontSize(26); |
| 593 | text_style.setWordSpacing(5); |
| 594 | text_style.setLetterSpacing(1); |
| 595 | text_style.setDecoration(TextDecoration::kUnderline); |
| 596 | text_style.setDecorationColor(SK_ColorBLACK); |
| 597 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 598 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 599 | |
| 600 | PlaceholderStyle placeholder(55, 50, PlaceholderAlignment::kMiddle, TextBaseline::kAlphabetic, 0); |
| 601 | builder.addPlaceholder(placeholder); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 602 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 603 | |
| 604 | builder.pop(); |
| 605 | |
| 606 | auto paragraph = builder.Build(); |
| 607 | paragraph->layout(TestCanvasWidth); |
| 608 | paragraph->paint(canvas.get(), 0, 0); |
| 609 | |
| 610 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 611 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 612 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 613 | auto boxes = paragraph->getRectsForPlaceholders(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 614 | canvas.drawRects(SK_ColorRED, boxes); |
| 615 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 616 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 90.921f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 617 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 618 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f + 55, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 619 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 50, EPSILON100)); |
| 620 | |
| 621 | boxes = paragraph->getRectsForRange(5, 6, rect_height_style, rect_width_style); |
| 622 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 623 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 624 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 75.324f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 625 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 9.765f, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 626 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 627 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 40.234f, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 628 | } |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 629 | |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 630 | DEF_TEST(SkParagraph_InlinePlaceholderIdeographicBaselineParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 631 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 632 | TestCanvas canvas("SkParagraph_InlinePlaceholderIdeographicBaselineParagraph.png"); |
| 633 | if (!fontCollection->fontsFound()) return; |
| 634 | |
| 635 | const char* text = "給能上目秘使"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 636 | const size_t len = strlen(text); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 637 | |
| 638 | ParagraphStyle paragraph_style; |
| 639 | paragraph_style.turnHintingOff(); |
| 640 | paragraph_style.setMaxLines(14); |
| 641 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 642 | |
| 643 | TextStyle text_style; |
| 644 | text_style.setFontFamilies({SkString("Source Han Serif CN")}); |
| 645 | text_style.setColor(SK_ColorBLACK); |
| 646 | text_style.setFontSize(26); |
| 647 | text_style.setWordSpacing(5); |
| 648 | text_style.setLetterSpacing(1); |
| 649 | text_style.setDecoration(TextDecoration::kUnderline); |
| 650 | text_style.setDecorationColor(SK_ColorBLACK); |
| 651 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 652 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 653 | PlaceholderStyle placeholder(55, 50, PlaceholderAlignment::kBaseline, TextBaseline::kIdeographic, 38.347f); |
| 654 | builder.addPlaceholder(placeholder); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 655 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 656 | |
| 657 | builder.pop(); |
| 658 | |
| 659 | auto paragraph = builder.Build(); |
| 660 | paragraph->layout(TestCanvasWidth); |
| 661 | paragraph->paint(canvas.get(), 0, 0); |
| 662 | |
| 663 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 664 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 665 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 666 | auto boxes = paragraph->getRectsForPlaceholders(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 667 | canvas.drawRects(SK_ColorRED, boxes); |
| 668 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 669 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 162.5f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 670 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 671 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 162.5f + 55, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 672 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 50, EPSILON100)); |
| 673 | |
| 674 | boxes = paragraph->getRectsForRange(5, 6, rect_height_style, rect_width_style); |
| 675 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 676 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 677 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 135.5f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 678 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 4.703f, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 679 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 162.5f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 680 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 42.065f, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 681 | } |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 682 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 683 | DEF_TEST(SkParagraph_InlinePlaceholderBreakParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 684 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 685 | TestCanvas canvas("SkParagraph_InlinePlaceholderBreakParagraph.png"); |
| 686 | if (!fontCollection->fontsFound()) return; |
| 687 | |
| 688 | const char* text = "012 34"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 689 | const size_t len = strlen(text); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 690 | |
| 691 | ParagraphStyle paragraph_style; |
| 692 | paragraph_style.turnHintingOff(); |
| 693 | paragraph_style.setMaxLines(14); |
| 694 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 695 | |
| 696 | TextStyle text_style; |
| 697 | text_style.setFontFamilies({SkString("Roboto")}); |
| 698 | text_style.setColor(SK_ColorBLACK); |
| 699 | text_style.setFontSize(26); |
| 700 | text_style.setWordSpacing(5); |
| 701 | text_style.setLetterSpacing(1); |
| 702 | text_style.setDecoration(TextDecoration::kUnderline); |
| 703 | text_style.setDecorationColor(SK_ColorBLACK); |
| 704 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 705 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 706 | |
| 707 | PlaceholderStyle placeholder1(50, 50, PlaceholderAlignment::kBaseline, TextBaseline::kAlphabetic, 50); |
| 708 | PlaceholderStyle placeholder2(25, 25, PlaceholderAlignment::kBaseline, TextBaseline::kAlphabetic, 12.5f); |
| 709 | |
| 710 | builder.addPlaceholder(placeholder1); |
| 711 | builder.addPlaceholder(placeholder1); |
| 712 | builder.addPlaceholder(placeholder1); |
| 713 | builder.addPlaceholder(placeholder2); |
| 714 | builder.addPlaceholder(placeholder1); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 715 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 716 | |
| 717 | builder.addPlaceholder(placeholder1); |
| 718 | builder.addPlaceholder(placeholder1); |
| 719 | builder.addPlaceholder(placeholder1); |
| 720 | builder.addPlaceholder(placeholder1); |
| 721 | builder.addPlaceholder(placeholder2); // 4 + 1 |
| 722 | builder.addPlaceholder(placeholder1); |
| 723 | builder.addPlaceholder(placeholder1); |
| 724 | builder.addPlaceholder(placeholder1); |
| 725 | builder.addPlaceholder(placeholder1); |
| 726 | builder.addPlaceholder(placeholder1); |
| 727 | builder.addPlaceholder(placeholder1); |
| 728 | builder.addPlaceholder(placeholder2); // 6 + 1 |
| 729 | builder.addPlaceholder(placeholder1); |
| 730 | builder.addPlaceholder(placeholder1); |
| 731 | builder.addPlaceholder(placeholder1); |
| 732 | builder.addPlaceholder(placeholder1); |
| 733 | builder.addPlaceholder(placeholder1); |
| 734 | builder.addPlaceholder(placeholder1); |
| 735 | builder.addPlaceholder(placeholder1); |
| 736 | builder.addPlaceholder(placeholder2); // 7 + 1 |
| 737 | |
| 738 | builder.addPlaceholder(placeholder1); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 739 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 740 | builder.addPlaceholder(placeholder1); |
| 741 | builder.addPlaceholder(placeholder2); |
| 742 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 743 | builder.addText(text, len); |
| 744 | builder.addText(text, len); |
| 745 | builder.addText(text, len); |
| 746 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 747 | |
| 748 | builder.addPlaceholder(placeholder2); |
| 749 | builder.addPlaceholder(placeholder1); |
| 750 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 751 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 752 | |
| 753 | builder.addPlaceholder(placeholder2); |
| 754 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 755 | builder.addText(text, len); |
| 756 | builder.addText(text, len); |
| 757 | builder.addText(text, len); |
| 758 | builder.addText(text, len); |
| 759 | builder.addText(text, len); |
| 760 | builder.addText(text, len); |
| 761 | builder.addText(text, len); |
| 762 | builder.addText(text, len); |
| 763 | builder.addText(text, len); |
| 764 | builder.addText(text, len); |
| 765 | builder.addText(text, len); |
| 766 | builder.addText(text, len); |
| 767 | builder.addText(text, len); |
| 768 | builder.addText(text, len); |
| 769 | builder.addText(text, len); |
| 770 | builder.addText(text, len); |
| 771 | builder.addText(text, len); |
| 772 | builder.addText(text, len); |
| 773 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 774 | |
| 775 | builder.pop(); |
| 776 | |
| 777 | auto paragraph = builder.Build(); |
| 778 | paragraph->layout(TestCanvasWidth - 100); |
| 779 | paragraph->paint(canvas.get(), 0, 0); |
| 780 | |
| 781 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 782 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 783 | |
| 784 | auto boxes = paragraph->getRectsForRange(0, 3, rect_height_style, rect_width_style); |
| 785 | canvas.drawRects(SK_ColorRED, boxes); |
| 786 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 787 | |
| 788 | boxes = paragraph->getRectsForRange(175, 176, rect_height_style, rect_width_style); |
| 789 | canvas.drawRects(SK_ColorGREEN, boxes); |
| 790 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 791 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 31.695f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 792 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 218.531f, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 793 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 47.292f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 794 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 249, EPSILON100)); |
| 795 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 796 | boxes = paragraph->getRectsForPlaceholders(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 797 | canvas.drawRects(SK_ColorRED, boxes); |
| 798 | |
| 799 | boxes = paragraph->getRectsForRange(4, 45, rect_height_style, rect_width_style); |
| 800 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 801 | REPORTER_ASSERT(reporter, boxes.size() == 30); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 802 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 59.726f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 803 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 26.378f, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 804 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 805 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 56.847f, EPSILON100)); |
| 806 | |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 807 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[11].rect.left(), 606.343f, EPSILON20)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 808 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[11].rect.top(), 38, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 809 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[11].rect.right(), 631.343f, EPSILON20)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 810 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[11].rect.bottom(), 63, EPSILON100)); |
| 811 | |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 812 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[17].rect.left(), 0.5f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 813 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[17].rect.top(), 63.5f, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 814 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[17].rect.right(), 50.5f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 815 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[17].rect.bottom(), 113.5f, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 816 | } |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 817 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 818 | DEF_TEST(SkParagraph_InlinePlaceholderGetRectsParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 819 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 820 | TestCanvas canvas("SkParagraph_InlinePlaceholderGetRectsParagraph.png"); |
| 821 | if (!fontCollection->fontsFound()) return; |
| 822 | |
| 823 | const char* text = "012 34"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 824 | const size_t len = strlen(text); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 825 | |
| 826 | ParagraphStyle paragraph_style; |
| 827 | paragraph_style.turnHintingOff(); |
| 828 | paragraph_style.setMaxLines(14); |
| 829 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 830 | |
| 831 | TextStyle text_style; |
| 832 | text_style.setFontFamilies({SkString("Roboto")}); |
| 833 | text_style.setColor(SK_ColorBLACK); |
| 834 | text_style.setFontSize(26); |
| 835 | text_style.setWordSpacing(5); |
| 836 | text_style.setLetterSpacing(1); |
| 837 | text_style.setDecoration(TextDecoration::kUnderline); |
| 838 | text_style.setDecorationColor(SK_ColorBLACK); |
| 839 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 840 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 841 | |
| 842 | PlaceholderStyle placeholder1(50, 50, PlaceholderAlignment::kBaseline, TextBaseline::kAlphabetic, 50); |
| 843 | PlaceholderStyle placeholder2(5, 20, PlaceholderAlignment::kBaseline, TextBaseline::kAlphabetic, 10); |
| 844 | |
| 845 | builder.addPlaceholder(placeholder1); |
| 846 | builder.addPlaceholder(placeholder1); |
| 847 | builder.addPlaceholder(placeholder1); |
| 848 | builder.addPlaceholder(placeholder1); |
| 849 | builder.addPlaceholder(placeholder1); |
| 850 | builder.addPlaceholder(placeholder1); |
| 851 | builder.addPlaceholder(placeholder1); |
| 852 | builder.addPlaceholder(placeholder1); |
| 853 | builder.addPlaceholder(placeholder2); // 8 + 1 |
| 854 | builder.addPlaceholder(placeholder1); |
| 855 | builder.addPlaceholder(placeholder1); |
| 856 | builder.addPlaceholder(placeholder1); |
| 857 | builder.addPlaceholder(placeholder1); |
| 858 | builder.addPlaceholder(placeholder1); |
| 859 | builder.addPlaceholder(placeholder2); // 5 + 1 |
| 860 | builder.addPlaceholder(placeholder1); |
| 861 | builder.addPlaceholder(placeholder1); |
| 862 | builder.addPlaceholder(placeholder1); |
| 863 | builder.addPlaceholder(placeholder1); |
| 864 | builder.addPlaceholder(placeholder1); |
| 865 | builder.addPlaceholder(placeholder1); |
| 866 | builder.addPlaceholder(placeholder1); |
| 867 | builder.addPlaceholder(placeholder1); // 8 + 0 |
| 868 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 869 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 870 | |
| 871 | builder.addPlaceholder(placeholder1); |
| 872 | builder.addPlaceholder(placeholder2); |
| 873 | builder.addPlaceholder(placeholder2); // 1 + 2 |
| 874 | builder.addPlaceholder(placeholder1); |
| 875 | builder.addPlaceholder(placeholder2); |
| 876 | builder.addPlaceholder(placeholder2); // 1 + 2 |
| 877 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 878 | builder.addText(text, len); |
| 879 | builder.addText(text, len); |
| 880 | builder.addText(text, len); |
| 881 | builder.addText(text, len); |
| 882 | builder.addText(text, len); |
| 883 | builder.addText(text, len); |
| 884 | builder.addText(text, len); |
| 885 | builder.addText(text, len); |
| 886 | builder.addText(text, len); |
| 887 | builder.addText(text, len); |
| 888 | builder.addText(text, len); // 11 |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 889 | |
| 890 | builder.addPlaceholder(placeholder2); |
| 891 | builder.addPlaceholder(placeholder1); |
| 892 | builder.addPlaceholder(placeholder2); |
| 893 | builder.addPlaceholder(placeholder1); |
| 894 | builder.addPlaceholder(placeholder2); |
| 895 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 896 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 897 | |
| 898 | builder.pop(); |
| 899 | |
| 900 | auto paragraph = builder.Build(); |
| 901 | paragraph->layout(TestCanvasWidth); |
| 902 | paragraph->paint(canvas.get(), 0, 0); |
| 903 | |
| 904 | RectHeightStyle rect_height_style = RectHeightStyle::kMax; |
| 905 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 906 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 907 | auto boxes = paragraph->getRectsForPlaceholders(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 908 | canvas.drawRects(SK_ColorRED, boxes); |
| 909 | |
| 910 | REPORTER_ASSERT(reporter, boxes.size() == 34); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 911 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 90.921f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 912 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 913 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 140.921f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 914 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 50, EPSILON100)); |
| 915 | |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 916 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[16].rect.left(), 800.921f, EPSILON20)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 917 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[16].rect.top(), 0, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 918 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[16].rect.right(), 850.921f, EPSILON20)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 919 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[16].rect.bottom(), 50, EPSILON100)); |
| 920 | |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 921 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[33].rect.left(), 503.382f, EPSILON10)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 922 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[33].rect.top(), 160, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 923 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[33].rect.right(), 508.382f, EPSILON10)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 924 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[33].rect.bottom(), 180, EPSILON100)); |
| 925 | |
| 926 | boxes = paragraph->getRectsForRange(30, 50, rect_height_style, rect_width_style); |
| 927 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 928 | |
| 929 | REPORTER_ASSERT(reporter, boxes.size() == 8); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 930 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 216.097f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 931 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 60, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 932 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 290.921f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 933 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 120, EPSILON100)); |
| 934 | |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 935 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.left(), 290.921f, EPSILON20)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 936 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.top(), 60, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 937 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.right(), 340.921f, EPSILON20)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 938 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.bottom(), 120, EPSILON100)); |
| 939 | |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 940 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[2].rect.left(), 340.921f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 941 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[2].rect.top(), 60, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 942 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[2].rect.right(), 345.921f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 943 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[2].rect.bottom(), 120, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 947 | DEF_TEST(SkParagraph_SimpleRedParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 948 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 949 | if (!fontCollection->fontsFound()) return; |
| 950 | const char* text = "I am RED"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 951 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 952 | |
| 953 | ParagraphStyle paragraph_style; |
| 954 | paragraph_style.turnHintingOff(); |
| 955 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 956 | |
| 957 | TextStyle text_style; |
| 958 | text_style.setFontFamilies({SkString("Roboto")}); |
| 959 | text_style.setColor(SK_ColorRED); |
| 960 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 961 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 962 | builder.pop(); |
| 963 | |
| 964 | auto paragraph = builder.Build(); |
| 965 | paragraph->layout(TestCanvasWidth); |
Julia Lavrova | 3281b96 | 2019-12-02 11:32:25 -0500 | [diff] [blame] | 966 | REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 967 | |
| 968 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 969 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 970 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); // paragraph style does not count |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 971 | REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 972 | |
| 973 | size_t index = 0; |
| 974 | for (auto& line : impl->lines()) { |
| 975 | line.scanStyles(StyleType::kDecorations, |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 976 | [reporter, &index](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
| 977 | REPORTER_ASSERT(reporter, index == 0); |
| 978 | REPORTER_ASSERT(reporter, style.getColor() == SK_ColorRED); |
| 979 | ++index; |
| 980 | return true; |
| 981 | }); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 982 | } |
| 983 | } |
| 984 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 985 | // Checked: DIFF+ (Space between 1 & 2 style blocks) |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 986 | DEF_TEST(SkParagraph_RainbowParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 987 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 988 | TestCanvas canvas("SkParagraph_RainbowParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 989 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 990 | const char* text1 = "Red Roboto"; // [0:10) |
| 991 | const char* text2 = "big Greeen Default"; // [10:28) |
| 992 | const char* text3 = "Defcolor Homemade Apple"; // [28:51) |
| 993 | const char* text4 = "Small Blue Roboto"; // [51:68) |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 994 | const char* text41 = "Small Blue "; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 995 | const char* text5 = |
| 996 | "Continue Last Style With lots of words to check if it overlaps " |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 997 | "properly or not"; // [68:) |
| 998 | const char* text42 = |
| 999 | "Roboto" |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1000 | "Continue Last Style With lots of words to check if it overlaps " |
| 1001 | "properly or not"; |
| 1002 | |
| 1003 | ParagraphStyle paragraph_style; |
| 1004 | paragraph_style.turnHintingOff(); |
| 1005 | paragraph_style.setTextAlign(TextAlign::kLeft); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1006 | paragraph_style.setMaxLines(2); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1007 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1008 | |
| 1009 | TextStyle text_style1; |
| 1010 | text_style1.setFontFamilies({SkString("Roboto")}); |
| 1011 | |
| 1012 | text_style1.setColor(SK_ColorRED); |
| 1013 | builder.pushStyle(text_style1); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1014 | builder.addText(text1, strlen(text1)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1015 | |
| 1016 | TextStyle text_style2; |
| 1017 | text_style2.setFontFamilies({SkString("Roboto")}); |
| 1018 | text_style2.setFontSize(50); |
| 1019 | text_style2.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 1020 | SkFontStyle::kUpright_Slant)); |
| 1021 | text_style2.setLetterSpacing(10); |
| 1022 | text_style2.setDecorationColor(SK_ColorBLACK); |
| 1023 | text_style2.setDecoration((TextDecoration)( |
| 1024 | TextDecoration::kUnderline | TextDecoration::kOverline | TextDecoration::kLineThrough)); |
| 1025 | text_style2.setWordSpacing(30); |
| 1026 | text_style2.setColor(SK_ColorGREEN); |
| 1027 | builder.pushStyle(text_style2); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1028 | builder.addText(text2, strlen(text2)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1029 | |
| 1030 | TextStyle text_style3; |
| 1031 | text_style3.setFontFamilies({SkString("Homemade Apple")}); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1032 | text_style3.setColor(SK_ColorBLACK); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1033 | builder.pushStyle(text_style3); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1034 | builder.addText(text3, strlen(text3)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1035 | |
| 1036 | TextStyle text_style4; |
| 1037 | text_style4.setFontFamilies({SkString("Roboto")}); |
| 1038 | text_style4.setFontSize(14); |
| 1039 | text_style4.setDecorationColor(SK_ColorBLACK); |
| 1040 | text_style4.setDecoration((TextDecoration)( |
| 1041 | TextDecoration::kUnderline | TextDecoration::kOverline | TextDecoration::kLineThrough)); |
| 1042 | text_style4.setColor(SK_ColorBLUE); |
| 1043 | builder.pushStyle(text_style4); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1044 | builder.addText(text4, strlen(text4)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1045 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1046 | builder.addText(text5, strlen(text5)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1047 | builder.pop(); |
| 1048 | |
| 1049 | auto paragraph = builder.Build(); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1050 | paragraph->layout(1000); |
| 1051 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1052 | |
Julia Lavrova | 3281b96 | 2019-12-02 11:32:25 -0500 | [diff] [blame] | 1053 | REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == 0); |
| 1054 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1055 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 1056 | REPORTER_ASSERT(reporter, impl->runs().size() == 4); |
| 1057 | REPORTER_ASSERT(reporter, impl->styles().size() == 4); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1058 | REPORTER_ASSERT(reporter, impl->lines().size() == 2); |
| 1059 | |
| 1060 | auto rects = paragraph->getRectsForRange(0, impl->text().size(), RectHeightStyle::kMax, RectWidthStyle::kTight); |
| 1061 | canvas.drawRects(SK_ColorMAGENTA, rects); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1062 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1063 | size_t index = 0; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1064 | impl->lines()[0].scanStyles( |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1065 | StyleType::kAllAttributes, |
| 1066 | [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1067 | switch (index) { |
| 1068 | case 0: |
| 1069 | REPORTER_ASSERT(reporter, style.equals(text_style1)); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1070 | REPORTER_ASSERT(reporter, equal(impl->text().begin(), textRange, text1)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1071 | break; |
| 1072 | case 1: |
| 1073 | REPORTER_ASSERT(reporter, style.equals(text_style2)); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1074 | REPORTER_ASSERT(reporter, equal(impl->text().begin(), textRange, text2)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1075 | break; |
| 1076 | case 2: |
| 1077 | REPORTER_ASSERT(reporter, style.equals(text_style3)); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1078 | REPORTER_ASSERT(reporter, equal(impl->text().begin(), textRange, text3)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1079 | break; |
| 1080 | case 3: |
| 1081 | REPORTER_ASSERT(reporter, style.equals(text_style4)); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1082 | REPORTER_ASSERT(reporter, equal(impl->text().begin(), textRange, text41)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1083 | break; |
| 1084 | default: |
| 1085 | REPORTER_ASSERT(reporter, false); |
| 1086 | break; |
| 1087 | } |
| 1088 | ++index; |
| 1089 | return true; |
| 1090 | }); |
| 1091 | impl->lines()[1].scanStyles( |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1092 | StyleType::kAllAttributes, |
| 1093 | [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1094 | switch (index) { |
| 1095 | case 4: |
| 1096 | REPORTER_ASSERT(reporter, style.equals(text_style4)); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1097 | REPORTER_ASSERT(reporter, equal(impl->text().begin(), textRange, text42)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1098 | break; |
| 1099 | default: |
| 1100 | REPORTER_ASSERT(reporter, false); |
| 1101 | break; |
| 1102 | } |
| 1103 | ++index; |
| 1104 | return true; |
| 1105 | }); |
| 1106 | REPORTER_ASSERT(reporter, index == 5); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1107 | } |
| 1108 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1109 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1110 | DEF_TEST(SkParagraph_DefaultStyleParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 1111 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1112 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1113 | TestCanvas canvas("SkParagraph_DefaultStyleParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1114 | const char* text = "No TextStyle! Uh Oh!"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1115 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1116 | |
| 1117 | ParagraphStyle paragraph_style; |
| 1118 | TextStyle defaultStyle; |
| 1119 | defaultStyle.setFontFamilies({SkString("Roboto")}); |
| 1120 | paragraph_style.setTextStyle(defaultStyle); |
| 1121 | paragraph_style.turnHintingOff(); |
| 1122 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1123 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1124 | |
| 1125 | auto paragraph = builder.Build(); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1126 | paragraph->layout(TestCanvasWidth); |
| 1127 | paragraph->paint(canvas.get(), 10.0, 15.0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1128 | |
Julia Lavrova | 3281b96 | 2019-12-02 11:32:25 -0500 | [diff] [blame] | 1129 | REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == 0); |
| 1130 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1131 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 1132 | |
| 1133 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 1134 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
| 1135 | REPORTER_ASSERT(reporter, impl->lines().size() == 1); |
| 1136 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1137 | size_t index = 0; |
| 1138 | impl->lines()[0].scanStyles( |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1139 | StyleType::kAllAttributes, |
| 1140 | [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1141 | REPORTER_ASSERT(reporter, style.equals(paragraph_style.getTextStyle())); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1142 | REPORTER_ASSERT(reporter, equal(impl->text().begin(), textRange, text)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1143 | ++index; |
| 1144 | return true; |
| 1145 | }); |
| 1146 | REPORTER_ASSERT(reporter, index == 1); |
| 1147 | } |
| 1148 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1149 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1150 | DEF_TEST(SkParagraph_BoldParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 1151 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1152 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1153 | TestCanvas canvas("SkParagraph_BoldParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1154 | const char* text = "This is Red max bold text!"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1155 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1156 | |
| 1157 | ParagraphStyle paragraph_style; |
| 1158 | paragraph_style.turnHintingOff(); |
| 1159 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1160 | |
| 1161 | TextStyle text_style; |
| 1162 | text_style.setFontFamilies({SkString("Roboto")}); |
| 1163 | text_style.setColor(SK_ColorRED); |
| 1164 | text_style.setFontSize(60); |
| 1165 | text_style.setLetterSpacing(0); |
| 1166 | text_style.setFontStyle(SkFontStyle(SkFontStyle::kBlack_Weight, SkFontStyle::kNormal_Width, |
| 1167 | SkFontStyle::kUpright_Slant)); |
| 1168 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1169 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1170 | builder.pop(); |
| 1171 | |
| 1172 | auto paragraph = builder.Build(); |
| 1173 | paragraph->layout(VeryLongCanvasWidth); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1174 | paragraph->paint(canvas.get(), 10.0, 60.0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1175 | |
Julia Lavrova | 3281b96 | 2019-12-02 11:32:25 -0500 | [diff] [blame] | 1176 | REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == 0); |
| 1177 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1178 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 1179 | |
| 1180 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 1181 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
| 1182 | REPORTER_ASSERT(reporter, impl->lines().size() == 1); |
| 1183 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1184 | size_t index = 0; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1185 | impl->lines()[0].scanStyles( |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1186 | StyleType::kAllAttributes, |
| 1187 | [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1188 | REPORTER_ASSERT(reporter, style.equals(text_style)); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1189 | REPORTER_ASSERT(reporter, equal(impl->text().begin(), textRange, text)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1190 | ++index; |
| 1191 | return true; |
| 1192 | }); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1193 | REPORTER_ASSERT(reporter, index == 1); |
| 1194 | } |
| 1195 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1196 | // Checked: NO DIFF (line height rounding error) |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1197 | DEF_TEST(SkParagraph_HeightOverrideParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 1198 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1199 | if (!fontCollection->fontsFound()) return; |
| 1200 | TestCanvas canvas("SkParagraph_HeightOverrideParagraph.png"); |
| 1201 | const char* text = "01234満毎冠行来昼本可\nabcd\n満毎冠行来昼本可"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1202 | const size_t len = strlen(text); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1203 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1204 | ParagraphStyle paragraph_style; |
| 1205 | paragraph_style.turnHintingOff(); |
| 1206 | paragraph_style.setMaxLines(10); |
| 1207 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1208 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1209 | TextStyle text_style; |
| 1210 | text_style.setFontFamilies({SkString("Roboto")}); |
| 1211 | text_style.setFontSize(20); |
| 1212 | text_style.setColor(SK_ColorBLACK); |
| 1213 | text_style.setHeight(3.6345f); |
| 1214 | text_style.setHeightOverride(true); |
| 1215 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1216 | builder.addText(text, len); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1217 | builder.pop(); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1218 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1219 | auto paragraph = builder.Build(); |
| 1220 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1221 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1222 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 1223 | REPORTER_ASSERT(reporter, impl->runs().size() == 5); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1224 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); // paragraph style does not count |
| 1225 | REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1226 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1227 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1228 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1229 | SkPaint paint; |
| 1230 | paint.setStyle(SkPaint::kStroke_Style); |
| 1231 | paint.setAntiAlias(true); |
| 1232 | paint.setStrokeWidth(1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1233 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1234 | // Tests for GetRectsForRange() |
| 1235 | RectHeightStyle rect_height_style = RectHeightStyle::kIncludeLineSpacingMiddle; |
| 1236 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 1237 | paint.setColor(SK_ColorRED); |
| 1238 | std::vector<TextBox> boxes = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style); |
| 1239 | canvas.drawRects(SK_ColorRED, boxes); |
| 1240 | REPORTER_ASSERT(reporter, boxes.size() == 0ull); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1241 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1242 | boxes = paragraph->getRectsForRange(0, 40, rect_height_style, rect_width_style); |
| 1243 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 1244 | REPORTER_ASSERT(reporter, boxes.size() == 3ull); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1245 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1246 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.left(), 0, EPSILON100)); |
| 1247 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.top(), 92.805f, EPSILON5)); |
| 1248 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.right(), 43.843f, EPSILON100)); |
| 1249 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.bottom(), 165.495f, EPSILON5)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1250 | } |
| 1251 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1252 | // Checked: DIFF+ |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1253 | DEF_TEST(SkParagraph_LeftAlignParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 1254 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1255 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1256 | TestCanvas canvas("SkParagraph_LeftAlignParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1257 | const char* text = |
| 1258 | "This is a very long sentence to test if the text will properly wrap " |
| 1259 | "around and go to the next line. Sometimes, short sentence. Longer " |
| 1260 | "sentences are okay too because they are nessecary. Very short. " |
| 1261 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod " |
| 1262 | "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim " |
| 1263 | "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea " |
| 1264 | "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate " |
| 1265 | "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint " |
| 1266 | "occaecat cupidatat non proident, sunt in culpa qui officia deserunt " |
| 1267 | "mollit anim id est laborum. " |
| 1268 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod " |
| 1269 | "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim " |
| 1270 | "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea " |
| 1271 | "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate " |
| 1272 | "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint " |
| 1273 | "occaecat cupidatat non proident, sunt in culpa qui officia deserunt " |
| 1274 | "mollit anim id est laborum."; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1275 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1276 | |
| 1277 | ParagraphStyle paragraph_style; |
| 1278 | paragraph_style.setMaxLines(14); |
| 1279 | paragraph_style.setTextAlign(TextAlign::kLeft); |
| 1280 | paragraph_style.turnHintingOff(); |
| 1281 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1282 | |
| 1283 | TextStyle text_style; |
| 1284 | text_style.setFontFamilies({SkString("Roboto")}); |
| 1285 | text_style.setFontSize(26); |
| 1286 | text_style.setLetterSpacing(1); |
| 1287 | text_style.setWordSpacing(5); |
| 1288 | text_style.setColor(SK_ColorBLACK); |
| 1289 | text_style.setHeight(1); |
| 1290 | text_style.setDecoration(TextDecoration::kUnderline); |
| 1291 | text_style.setDecorationColor(SK_ColorBLACK); |
| 1292 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1293 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1294 | builder.pop(); |
| 1295 | |
| 1296 | auto paragraph = builder.Build(); |
| 1297 | paragraph->layout(TestCanvasWidth - 100); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1298 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1299 | |
| 1300 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1301 | |
| 1302 | REPORTER_ASSERT(reporter, impl->text().size() == std::string{text}.length()); |
| 1303 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 1304 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 1305 | REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1306 | REPORTER_ASSERT(reporter, impl->lines().size() == paragraph_style.getMaxLines()); |
| 1307 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1308 | double expected_y = 0; |
| 1309 | double epsilon = 0.01f; |
| 1310 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].baseline(), 24.121f, epsilon)); |
| 1311 | REPORTER_ASSERT(reporter, |
| 1312 | SkScalarNearlyEqual(impl->lines()[0].offset().fY, expected_y, epsilon)); |
| 1313 | expected_y += 30; |
| 1314 | REPORTER_ASSERT(reporter, |
| 1315 | SkScalarNearlyEqual(impl->lines()[1].offset().fY, expected_y, epsilon)); |
| 1316 | expected_y += 30; |
| 1317 | REPORTER_ASSERT(reporter, |
| 1318 | SkScalarNearlyEqual(impl->lines()[2].offset().fY, expected_y, epsilon)); |
| 1319 | expected_y += 30; |
| 1320 | REPORTER_ASSERT(reporter, |
| 1321 | SkScalarNearlyEqual(impl->lines()[3].offset().fY, expected_y, epsilon)); |
| 1322 | expected_y += 30 * 10; |
| 1323 | REPORTER_ASSERT(reporter, |
| 1324 | SkScalarNearlyEqual(impl->lines()[13].offset().fY, expected_y, epsilon)); |
| 1325 | |
| 1326 | REPORTER_ASSERT(reporter, |
| 1327 | paragraph_style.getTextAlign() == impl->paragraphStyle().getTextAlign()); |
| 1328 | |
| 1329 | // Tests for GetGlyphPositionAtCoordinate() |
| 1330 | REPORTER_ASSERT(reporter, impl->getGlyphPositionAtCoordinate(0, 0).position == 0); |
| 1331 | REPORTER_ASSERT(reporter, impl->getGlyphPositionAtCoordinate(1, 1).position == 0); |
| 1332 | REPORTER_ASSERT(reporter, impl->getGlyphPositionAtCoordinate(1, 35).position == 68); |
| 1333 | REPORTER_ASSERT(reporter, impl->getGlyphPositionAtCoordinate(1, 70).position == 134); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 1334 | REPORTER_ASSERT(reporter, impl->getGlyphPositionAtCoordinate(2000, 35).position == 134);// |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1335 | } |
| 1336 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1337 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1338 | DEF_TEST(SkParagraph_RightAlignParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 1339 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1340 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1341 | TestCanvas canvas("SkParagraph_RightAlignParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1342 | const char* text = |
| 1343 | "This is a very long sentence to test if the text will properly wrap " |
| 1344 | "around and go to the next line. Sometimes, short sentence. Longer " |
| 1345 | "sentences are okay too because they are nessecary. Very short. " |
| 1346 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod " |
| 1347 | "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim " |
| 1348 | "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea " |
| 1349 | "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate " |
| 1350 | "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint " |
| 1351 | "occaecat cupidatat non proident, sunt in culpa qui officia deserunt " |
| 1352 | "mollit anim id est laborum. " |
| 1353 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod " |
| 1354 | "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim " |
| 1355 | "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea " |
| 1356 | "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate " |
| 1357 | "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint " |
| 1358 | "occaecat cupidatat non proident, sunt in culpa qui officia deserunt " |
| 1359 | "mollit anim id est laborum."; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1360 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1361 | |
| 1362 | ParagraphStyle paragraph_style; |
| 1363 | paragraph_style.setMaxLines(14); |
| 1364 | paragraph_style.setTextAlign(TextAlign::kRight); |
| 1365 | paragraph_style.turnHintingOff(); |
| 1366 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1367 | |
| 1368 | TextStyle text_style; |
| 1369 | text_style.setFontFamilies({SkString("Roboto")}); |
| 1370 | text_style.setFontSize(26); |
| 1371 | text_style.setLetterSpacing(1); |
| 1372 | text_style.setWordSpacing(5); |
| 1373 | text_style.setColor(SK_ColorBLACK); |
| 1374 | text_style.setHeight(1); |
| 1375 | text_style.setDecoration(TextDecoration::kUnderline); |
| 1376 | text_style.setDecorationColor(SK_ColorBLACK); |
| 1377 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1378 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1379 | builder.pop(); |
| 1380 | |
| 1381 | auto paragraph = builder.Build(); |
| 1382 | paragraph->layout(TestCanvasWidth - 100); |
| 1383 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1384 | paragraph->paint(canvas.get(), 0, 0); |
| 1385 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1386 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1387 | |
| 1388 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 1389 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 1390 | REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1391 | REPORTER_ASSERT(reporter, impl->lines().size() == paragraph_style.getMaxLines()); |
| 1392 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1393 | double expected_y = 0; |
| 1394 | double epsilon = 0.01f; |
| 1395 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].baseline(), 24.121f, epsilon)); |
| 1396 | REPORTER_ASSERT(reporter, |
| 1397 | SkScalarNearlyEqual(impl->lines()[0].offset().fY, expected_y, epsilon)); |
| 1398 | expected_y += 30; |
| 1399 | REPORTER_ASSERT(reporter, |
| 1400 | SkScalarNearlyEqual(impl->lines()[1].offset().fY, expected_y, epsilon)); |
| 1401 | expected_y += 30; |
| 1402 | REPORTER_ASSERT(reporter, |
| 1403 | SkScalarNearlyEqual(impl->lines()[2].offset().fY, expected_y, epsilon)); |
| 1404 | expected_y += 30; |
| 1405 | REPORTER_ASSERT(reporter, |
| 1406 | SkScalarNearlyEqual(impl->lines()[3].offset().fY, expected_y, epsilon)); |
| 1407 | expected_y += 30 * 10; |
| 1408 | REPORTER_ASSERT(reporter, |
| 1409 | SkScalarNearlyEqual(impl->lines()[13].offset().fY, expected_y, epsilon)); |
| 1410 | |
| 1411 | auto calculate = [](const TextLine& line) -> SkScalar { |
| 1412 | return TestCanvasWidth - 100 - line.offset().fX - line.width(); |
| 1413 | }; |
| 1414 | |
| 1415 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[0]), 0, epsilon)); |
| 1416 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[1]), 0, epsilon)); |
| 1417 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[2]), 0, epsilon)); |
| 1418 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[3]), 0, epsilon)); |
| 1419 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[13]), 0, epsilon)); |
| 1420 | |
| 1421 | REPORTER_ASSERT(reporter, |
| 1422 | paragraph_style.getTextAlign() == impl->paragraphStyle().getTextAlign()); |
| 1423 | } |
| 1424 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1425 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1426 | DEF_TEST(SkParagraph_CenterAlignParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 1427 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1428 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1429 | TestCanvas canvas("SkParagraph_CenterAlignParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1430 | const char* text = |
| 1431 | "This is a very long sentence to test if the text will properly wrap " |
| 1432 | "around and go to the next line. Sometimes, short sentence. Longer " |
| 1433 | "sentences are okay too because they are nessecary. Very short. " |
| 1434 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod " |
| 1435 | "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim " |
| 1436 | "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea " |
| 1437 | "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate " |
| 1438 | "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint " |
| 1439 | "occaecat cupidatat non proident, sunt in culpa qui officia deserunt " |
| 1440 | "mollit anim id est laborum. " |
| 1441 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod " |
| 1442 | "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim " |
| 1443 | "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea " |
| 1444 | "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate " |
| 1445 | "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint " |
| 1446 | "occaecat cupidatat non proident, sunt in culpa qui officia deserunt " |
| 1447 | "mollit anim id est laborum."; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1448 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1449 | |
| 1450 | ParagraphStyle paragraph_style; |
| 1451 | paragraph_style.setMaxLines(14); |
| 1452 | paragraph_style.setTextAlign(TextAlign::kCenter); |
| 1453 | paragraph_style.turnHintingOff(); |
| 1454 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1455 | |
| 1456 | TextStyle text_style; |
| 1457 | text_style.setFontFamilies({SkString("Roboto")}); |
| 1458 | text_style.setFontSize(26); |
| 1459 | text_style.setLetterSpacing(1); |
| 1460 | text_style.setWordSpacing(5); |
| 1461 | text_style.setColor(SK_ColorBLACK); |
| 1462 | text_style.setHeight(1); |
| 1463 | text_style.setDecoration(TextDecoration::kUnderline); |
| 1464 | text_style.setDecorationColor(SK_ColorBLACK); |
| 1465 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1466 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1467 | builder.pop(); |
| 1468 | |
| 1469 | auto paragraph = builder.Build(); |
| 1470 | paragraph->layout(TestCanvasWidth - 100); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1471 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1472 | |
| 1473 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1474 | |
| 1475 | REPORTER_ASSERT(reporter, impl->text().size() == std::string{text}.length()); |
| 1476 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 1477 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 1478 | REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1479 | REPORTER_ASSERT(reporter, impl->lines().size() == paragraph_style.getMaxLines()); |
| 1480 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1481 | double expected_y = 0; |
| 1482 | double epsilon = 0.01f; |
| 1483 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].baseline(), 24.121f, epsilon)); |
| 1484 | REPORTER_ASSERT(reporter, |
| 1485 | SkScalarNearlyEqual(impl->lines()[0].offset().fY, expected_y, epsilon)); |
| 1486 | expected_y += 30; |
| 1487 | REPORTER_ASSERT(reporter, |
| 1488 | SkScalarNearlyEqual(impl->lines()[1].offset().fY, expected_y, epsilon)); |
| 1489 | expected_y += 30; |
| 1490 | REPORTER_ASSERT(reporter, |
| 1491 | SkScalarNearlyEqual(impl->lines()[2].offset().fY, expected_y, epsilon)); |
| 1492 | expected_y += 30; |
| 1493 | REPORTER_ASSERT(reporter, |
| 1494 | SkScalarNearlyEqual(impl->lines()[3].offset().fY, expected_y, epsilon)); |
| 1495 | expected_y += 30 * 10; |
| 1496 | REPORTER_ASSERT(reporter, |
| 1497 | SkScalarNearlyEqual(impl->lines()[13].offset().fY, expected_y, epsilon)); |
| 1498 | |
| 1499 | auto calculate = [](const TextLine& line) -> SkScalar { |
| 1500 | return TestCanvasWidth - 100 - (line.offset().fX * 2 + line.width()); |
| 1501 | }; |
| 1502 | |
| 1503 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[0]), 0, epsilon)); |
| 1504 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[1]), 0, epsilon)); |
| 1505 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[2]), 0, epsilon)); |
| 1506 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[3]), 0, epsilon)); |
| 1507 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[13]), 0, epsilon)); |
| 1508 | |
| 1509 | REPORTER_ASSERT(reporter, |
| 1510 | paragraph_style.getTextAlign() == impl->paragraphStyle().getTextAlign()); |
| 1511 | } |
| 1512 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1513 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1514 | DEF_TEST(SkParagraph_JustifyAlignParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 1515 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1516 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1517 | TestCanvas canvas("SkParagraph_JustifyAlignParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1518 | const char* text = |
| 1519 | "This is a very long sentence to test if the text will properly wrap " |
| 1520 | "around and go to the next line. Sometimes, short sentence. Longer " |
| 1521 | "sentences are okay too because they are nessecary. Very short. " |
| 1522 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod " |
| 1523 | "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim " |
| 1524 | "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea " |
| 1525 | "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate " |
| 1526 | "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint " |
| 1527 | "occaecat cupidatat non proident, sunt in culpa qui officia deserunt " |
| 1528 | "mollit anim id est laborum. " |
| 1529 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod " |
| 1530 | "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim " |
| 1531 | "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea " |
| 1532 | "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate " |
| 1533 | "velit esse cillum dolore eu fugiat."; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1534 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1535 | |
| 1536 | ParagraphStyle paragraph_style; |
| 1537 | paragraph_style.setMaxLines(14); |
| 1538 | paragraph_style.setTextAlign(TextAlign::kJustify); |
| 1539 | paragraph_style.turnHintingOff(); |
| 1540 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1541 | |
| 1542 | TextStyle text_style; |
| 1543 | text_style.setFontFamilies({SkString("Roboto")}); |
| 1544 | text_style.setFontSize(26); |
| 1545 | text_style.setLetterSpacing(0); |
| 1546 | text_style.setWordSpacing(5); |
| 1547 | text_style.setColor(SK_ColorBLACK); |
| 1548 | text_style.setHeight(1); |
| 1549 | text_style.setDecoration(TextDecoration::kUnderline); |
| 1550 | text_style.setDecorationColor(SK_ColorBLACK); |
| 1551 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1552 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1553 | builder.pop(); |
| 1554 | |
| 1555 | auto paragraph = builder.Build(); |
| 1556 | paragraph->layout(TestCanvasWidth - 100); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1557 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1558 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1559 | RectHeightStyle rect_height_style = RectHeightStyle::kMax; |
| 1560 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 1561 | auto boxes = paragraph->getRectsForRange(0, 100, rect_height_style, rect_width_style); |
| 1562 | canvas.drawRects(SK_ColorRED, boxes); |
| 1563 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1564 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1565 | |
| 1566 | REPORTER_ASSERT(reporter, impl->text().size() == std::string{text}.length()); |
| 1567 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 1568 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 1569 | REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1570 | |
| 1571 | double expected_y = 0; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1572 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].baseline(), 24.121f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1573 | REPORTER_ASSERT(reporter, |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1574 | SkScalarNearlyEqual(impl->lines()[0].offset().fY, expected_y, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1575 | expected_y += 30; |
| 1576 | REPORTER_ASSERT(reporter, |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1577 | SkScalarNearlyEqual(impl->lines()[1].offset().fY, expected_y, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1578 | expected_y += 30; |
| 1579 | REPORTER_ASSERT(reporter, |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1580 | SkScalarNearlyEqual(impl->lines()[2].offset().fY, expected_y, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1581 | expected_y += 30; |
| 1582 | REPORTER_ASSERT(reporter, |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1583 | SkScalarNearlyEqual(impl->lines()[3].offset().fY, expected_y, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1584 | expected_y += 30 * 9; |
| 1585 | REPORTER_ASSERT(reporter, |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1586 | SkScalarNearlyEqual(impl->lines()[12].offset().fY, expected_y, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1587 | |
| 1588 | auto calculate = [](const TextLine& line) -> SkScalar { |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 1589 | return line.offset().fX; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1590 | }; |
| 1591 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1592 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[0]), 0, EPSILON100)); |
| 1593 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[1]), 0, EPSILON100)); |
| 1594 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[2]), 0, EPSILON100)); |
| 1595 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[3]), 0, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1596 | |
| 1597 | REPORTER_ASSERT(reporter, |
| 1598 | paragraph_style.getTextAlign() == impl->paragraphStyle().getTextAlign()); |
| 1599 | } |
| 1600 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1601 | // Checked: DIFF (ghost spaces as a separate box in TxtLib) |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1602 | DEF_TEST(SkParagraph_JustifyRTL, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 1603 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1604 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1605 | TestCanvas canvas("SkParagraph_JustifyRTL.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1606 | const char* text = |
| 1607 | "אאא בּבּבּבּ אאאא בּבּ אאא בּבּבּ אאאאא בּבּבּבּ אאאא בּבּבּבּבּ " |
| 1608 | "אאאאא בּבּבּבּבּ אאאבּבּבּבּבּבּאאאאא בּבּבּבּבּבּאאאאאבּבּבּבּבּבּ אאאאא בּבּבּבּבּ " |
| 1609 | "אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1610 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1611 | |
| 1612 | ParagraphStyle paragraph_style; |
| 1613 | paragraph_style.setMaxLines(14); |
| 1614 | paragraph_style.setTextAlign(TextAlign::kJustify); |
| 1615 | paragraph_style.turnHintingOff(); |
| 1616 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1617 | |
| 1618 | TextStyle text_style; |
| 1619 | text_style.setFontFamilies({SkString("Ahem")}); |
| 1620 | text_style.setFontSize(26); |
| 1621 | text_style.setColor(SK_ColorBLACK); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1622 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1623 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1624 | builder.pop(); |
| 1625 | |
| 1626 | auto paragraph = builder.Build(); |
| 1627 | paragraph->layout(TestCanvasWidth - 100); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1628 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1629 | |
| 1630 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1631 | |
| 1632 | auto calculate = [](const TextLine& line) -> SkScalar { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1633 | return TestCanvasWidth - 100 - line.width(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1634 | }; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1635 | for (auto& line : impl->lines()) { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1636 | if (&line == &impl->lines().back()) { |
| 1637 | REPORTER_ASSERT(reporter, calculate(line) > EPSILON100); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1638 | } else { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1639 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(line), 0, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | // Just make sure the the text is actually RTL |
| 1644 | for (auto& run : impl->runs()) { |
| 1645 | REPORTER_ASSERT(reporter, !run.leftToRight()); |
| 1646 | } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1647 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1648 | // Tests for GetRectsForRange() |
| 1649 | RectHeightStyle rect_height_style = RectHeightStyle::kMax; |
| 1650 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 1651 | auto boxes = paragraph->getRectsForRange(0, 100, rect_height_style, rect_width_style); |
| 1652 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 1653 | REPORTER_ASSERT(reporter, boxes.size() == 5); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1654 | |
| 1655 | boxes = paragraph->getRectsForRange(240, 250, rect_height_style, rect_width_style); |
| 1656 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 1657 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 1658 | |
| 1659 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 588, EPSILON100)); |
| 1660 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 130, EPSILON100)); |
| 1661 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 640, EPSILON100)); |
| 1662 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 156, EPSILON100)); |
| 1663 | } |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1664 | |
Julia Lavrova | 90bfd1c | 2019-12-04 11:43:32 -0500 | [diff] [blame] | 1665 | DEF_TEST_DISABLED(SkParagraph_JustifyRTLNewLine, reporter) { |
| 1666 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true); |
| 1667 | if (!fontCollection->fontsFound()) return; |
| 1668 | TestCanvas canvas("SkParagraph_JustifyRTLNewLine.png"); |
| 1669 | const char* text = |
| 1670 | "אאא בּבּבּבּ אאאא\nבּבּ אאא בּבּבּ אאאאא בּבּבּבּ אאאא בּבּבּבּבּ " |
| 1671 | "אאאאא בּבּבּבּבּ אאאבּבּבּבּבּבּאאאאא בּבּבּבּבּבּאאאאאבּבּבּבּבּבּ אאאאא בּבּבּבּבּ " |
| 1672 | "אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ"; |
| 1673 | |
| 1674 | auto icu_text = icu::UnicodeString::fromUTF8(text); |
| 1675 | std::u16string u16_text(icu_text.getBuffer(), icu_text.getBuffer() + icu_text.length()); |
| 1676 | const size_t len = strlen(text); |
| 1677 | |
| 1678 | ParagraphStyle paragraph_style; |
| 1679 | paragraph_style.setMaxLines(14); |
| 1680 | paragraph_style.setTextAlign(TextAlign::kJustify); |
| 1681 | paragraph_style.setTextDirection(TextDirection::kRtl); |
| 1682 | paragraph_style.turnHintingOff(); |
| 1683 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1684 | |
| 1685 | TextStyle text_style; |
| 1686 | text_style.setFontFamilies({SkString("Ahem")}); |
| 1687 | text_style.setFontSize(26); |
| 1688 | text_style.setColor(SK_ColorBLACK); |
| 1689 | builder.pushStyle(text_style); |
| 1690 | builder.addText(text, len); |
| 1691 | builder.pop(); |
| 1692 | |
| 1693 | auto paragraph = builder.Build(); |
| 1694 | paragraph->layout(TestCanvasWidth - 100); |
| 1695 | paragraph->paint(canvas.get(), 0, 0); |
| 1696 | |
| 1697 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 1698 | |
| 1699 | SkPaint paint; |
| 1700 | paint.setStyle(SkPaint::kStroke_Style); |
| 1701 | paint.setAntiAlias(true); |
| 1702 | paint.setStrokeWidth(1); |
| 1703 | |
| 1704 | // Tests for GetRectsForRange() |
| 1705 | RectHeightStyle rect_height_style = RectHeightStyle::kMax; |
| 1706 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 1707 | paint.setColor(SK_ColorRED); |
| 1708 | auto boxes = paragraph->getRectsForRange(0, 30, rect_height_style, rect_width_style); |
| 1709 | for (size_t i = 0; i < boxes.size(); ++i) { |
| 1710 | canvas.get()->drawRect(boxes[i].rect, paint); |
| 1711 | } |
| 1712 | REPORTER_ASSERT(reporter, boxes.size() == 2ull); |
| 1713 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 562, EPSILON100)); |
| 1714 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100)); |
| 1715 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 900, EPSILON100)); |
| 1716 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 26, EPSILON100)); |
| 1717 | |
| 1718 | paint.setColor(SK_ColorBLUE); |
| 1719 | boxes = paragraph->getRectsForRange(240, 250, rect_height_style, rect_width_style); |
| 1720 | for (size_t i = 0; i < boxes.size(); ++i) { |
| 1721 | canvas.get()->drawRect(boxes[i].rect, paint); |
| 1722 | } |
| 1723 | REPORTER_ASSERT(reporter, boxes.size() == 1ull); |
| 1724 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 68, EPSILON100)); |
| 1725 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 130, EPSILON100)); |
| 1726 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 120, EPSILON100)); |
| 1727 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 156, EPSILON100)); |
| 1728 | |
| 1729 | // All lines should be justified to the width of the |
| 1730 | // paragraph. |
| 1731 | for (auto& line : impl->lines()) { |
| 1732 | REPORTER_ASSERT(reporter, |
| 1733 | SkScalarNearlyEqual(line.width(), TestCanvasWidth - 100, EPSILON100)); |
| 1734 | } |
| 1735 | } |
| 1736 | |
| 1737 | DEF_TEST_DISABLED(SkParagraph_LeadingSpaceRTL, reporter) { |
| 1738 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true); |
| 1739 | if (!fontCollection->fontsFound()) return; |
| 1740 | TestCanvas canvas("SkParagraph_LeadingSpaceRTL.png"); |
| 1741 | |
| 1742 | const char* text = " leading space"; |
| 1743 | |
| 1744 | auto icu_text = icu::UnicodeString::fromUTF8(text); |
| 1745 | std::u16string u16_text(icu_text.getBuffer(), icu_text.getBuffer() + icu_text.length()); |
| 1746 | const size_t len = strlen(text); |
| 1747 | |
| 1748 | ParagraphStyle paragraph_style; |
| 1749 | paragraph_style.setMaxLines(14); |
| 1750 | paragraph_style.setTextAlign(TextAlign::kJustify); |
| 1751 | paragraph_style.setTextDirection(TextDirection::kRtl); |
| 1752 | paragraph_style.turnHintingOff(); |
| 1753 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1754 | |
| 1755 | TextStyle text_style; |
| 1756 | text_style.setFontFamilies({SkString("Ahem")}); |
| 1757 | text_style.setFontSize(26); |
| 1758 | text_style.setColor(SK_ColorBLACK); |
| 1759 | builder.pushStyle(text_style); |
| 1760 | builder.addText(text, len); |
| 1761 | builder.pop(); |
| 1762 | |
| 1763 | auto paragraph = builder.Build(); |
| 1764 | paragraph->layout(TestCanvasWidth - 100); |
| 1765 | paragraph->paint(canvas.get(), 0, 0); |
| 1766 | |
| 1767 | SkPaint paint; |
| 1768 | paint.setStyle(SkPaint::kStroke_Style); |
| 1769 | paint.setAntiAlias(true); |
| 1770 | paint.setStrokeWidth(1); |
| 1771 | |
| 1772 | // Tests for GetRectsForRange() |
| 1773 | RectHeightStyle rect_height_style = RectHeightStyle::kMax; |
| 1774 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 1775 | paint.setColor(SK_ColorRED); |
| 1776 | auto boxes = paragraph->getRectsForRange(0, 100, rect_height_style, rect_width_style); |
| 1777 | for (size_t i = 0; i < boxes.size(); ++i) { |
| 1778 | canvas.get()->drawRect(boxes[i].rect, paint); |
| 1779 | } |
| 1780 | REPORTER_ASSERT(reporter, boxes.size() == 2ull); |
| 1781 | } |
| 1782 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1783 | // Checked: NO DIFF (some minor decoration differences, probably) |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1784 | DEF_TEST(SkParagraph_DecorationsParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 1785 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1786 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1787 | TestCanvas canvas("SkParagraph_DecorationsParagraph.png"); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1788 | const char* text1 = "This text should be"; |
| 1789 | const char* text2 = " decorated even when"; |
| 1790 | const char* text3 = " wrapped around to"; |
| 1791 | const char* text4 = " the next line."; |
| 1792 | const char* text5 = " Otherwise, bad things happen."; |
| 1793 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1794 | ParagraphStyle paragraph_style; |
| 1795 | paragraph_style.setMaxLines(14); |
| 1796 | paragraph_style.setTextAlign(TextAlign::kLeft); |
| 1797 | paragraph_style.turnHintingOff(); |
| 1798 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1799 | |
| 1800 | TextStyle text_style; |
| 1801 | text_style.setFontFamilies({SkString("Roboto")}); |
| 1802 | text_style.setFontSize(26); |
| 1803 | text_style.setLetterSpacing(0); |
| 1804 | text_style.setWordSpacing(5); |
| 1805 | text_style.setColor(SK_ColorBLACK); |
| 1806 | text_style.setHeight(2); |
| 1807 | text_style.setDecoration(TextDecoration::kUnderline); |
| 1808 | text_style.setDecorationColor(SK_ColorBLACK); |
| 1809 | text_style.setDecoration((TextDecoration)( |
| 1810 | TextDecoration::kUnderline | TextDecoration::kOverline | TextDecoration::kLineThrough)); |
| 1811 | text_style.setDecorationStyle(TextDecorationStyle::kSolid); |
| 1812 | text_style.setDecorationColor(SK_ColorBLACK); |
| 1813 | text_style.setDecorationThicknessMultiplier(2.0); |
| 1814 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1815 | builder.addText(text1, strlen(text1)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1816 | |
| 1817 | text_style.setDecorationStyle(TextDecorationStyle::kDouble); |
| 1818 | text_style.setDecorationColor(SK_ColorBLUE); |
| 1819 | text_style.setDecorationThicknessMultiplier(1.0); |
| 1820 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1821 | builder.addText(text2, strlen(text2)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1822 | |
| 1823 | text_style.setDecorationStyle(TextDecorationStyle::kDotted); |
| 1824 | text_style.setDecorationColor(SK_ColorBLACK); |
| 1825 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1826 | builder.addText(text3, strlen(text3)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1827 | |
| 1828 | text_style.setDecorationStyle(TextDecorationStyle::kDashed); |
| 1829 | text_style.setDecorationColor(SK_ColorBLACK); |
| 1830 | text_style.setDecorationThicknessMultiplier(3.0); |
| 1831 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1832 | builder.addText(text4, strlen(text4)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1833 | |
| 1834 | text_style.setDecorationStyle(TextDecorationStyle::kWavy); |
| 1835 | text_style.setDecorationColor(SK_ColorRED); |
| 1836 | text_style.setDecorationThicknessMultiplier(1.0); |
| 1837 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1838 | builder.addText(text5, strlen(text5)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1839 | builder.pop(); |
| 1840 | |
| 1841 | auto paragraph = builder.Build(); |
| 1842 | paragraph->layout(TestCanvasWidth - 100); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1843 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1844 | |
| 1845 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1846 | |
| 1847 | size_t index = 0; |
| 1848 | for (auto& line : impl->lines()) { |
| 1849 | line.scanStyles( |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1850 | StyleType::kDecorations, |
| 1851 | [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1852 | auto decoration = (TextDecoration)(TextDecoration::kUnderline | |
| 1853 | TextDecoration::kOverline | |
| 1854 | TextDecoration::kLineThrough); |
| 1855 | REPORTER_ASSERT(reporter, style.getDecorationType() == decoration); |
| 1856 | switch (index) { |
| 1857 | case 0: |
| 1858 | REPORTER_ASSERT(reporter, style.getDecorationStyle() == |
| 1859 | TextDecorationStyle::kSolid); |
| 1860 | REPORTER_ASSERT(reporter, style.getDecorationColor() == SK_ColorBLACK); |
| 1861 | REPORTER_ASSERT(reporter, |
| 1862 | style.getDecorationThicknessMultiplier() == 2.0); |
| 1863 | break; |
| 1864 | case 1: // The style appears on 2 lines so it has 2 pieces |
| 1865 | REPORTER_ASSERT(reporter, style.getDecorationStyle() == |
| 1866 | TextDecorationStyle::kDouble); |
| 1867 | REPORTER_ASSERT(reporter, style.getDecorationColor() == SK_ColorBLUE); |
| 1868 | REPORTER_ASSERT(reporter, |
| 1869 | style.getDecorationThicknessMultiplier() == 1.0); |
| 1870 | break; |
| 1871 | case 2: |
| 1872 | REPORTER_ASSERT(reporter, style.getDecorationStyle() == |
| 1873 | TextDecorationStyle::kDotted); |
| 1874 | REPORTER_ASSERT(reporter, style.getDecorationColor() == SK_ColorBLACK); |
| 1875 | REPORTER_ASSERT(reporter, |
| 1876 | style.getDecorationThicknessMultiplier() == 1.0); |
| 1877 | break; |
| 1878 | case 3: |
| 1879 | case 4: |
| 1880 | REPORTER_ASSERT(reporter, style.getDecorationStyle() == |
| 1881 | TextDecorationStyle::kDashed); |
| 1882 | REPORTER_ASSERT(reporter, style.getDecorationColor() == SK_ColorBLACK); |
| 1883 | REPORTER_ASSERT(reporter, |
| 1884 | style.getDecorationThicknessMultiplier() == 3.0); |
| 1885 | break; |
| 1886 | case 5: |
| 1887 | REPORTER_ASSERT(reporter, style.getDecorationStyle() == |
| 1888 | TextDecorationStyle::kWavy); |
| 1889 | REPORTER_ASSERT(reporter, style.getDecorationColor() == SK_ColorRED); |
| 1890 | REPORTER_ASSERT(reporter, |
| 1891 | style.getDecorationThicknessMultiplier() == 1.0); |
| 1892 | break; |
| 1893 | default: |
| 1894 | REPORTER_ASSERT(reporter, false); |
| 1895 | break; |
| 1896 | } |
| 1897 | ++index; |
| 1898 | return true; |
| 1899 | }); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1900 | } |
| 1901 | } |
| 1902 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1903 | DEF_TEST(SkParagraph_WavyDecorationParagraph, reporter) { |
Julia Lavrova | 90bfd1c | 2019-12-04 11:43:32 -0500 | [diff] [blame] | 1904 | SkDebugf("TODO: Add test for wavy decorations\n"); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1905 | } |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1906 | |
| 1907 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1908 | DEF_TEST(SkParagraph_ItalicsParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 1909 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1910 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1911 | TestCanvas canvas("SkParagraph_ItalicsParagraph.png"); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1912 | const char* text1 = "No italic "; |
| 1913 | const char* text2 = "Yes Italic "; |
| 1914 | const char* text3 = "No Italic again."; |
| 1915 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1916 | ParagraphStyle paragraph_style; |
| 1917 | paragraph_style.turnHintingOff(); |
| 1918 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1919 | |
| 1920 | TextStyle text_style; |
| 1921 | text_style.setFontFamilies({SkString("Roboto")}); |
| 1922 | text_style.setFontSize(10); |
| 1923 | text_style.setColor(SK_ColorRED); |
| 1924 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1925 | builder.addText(text1, strlen(text1)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1926 | |
| 1927 | text_style.setFontStyle(SkFontStyle(SkFontStyle::kNormal_Weight, SkFontStyle::kNormal_Width, |
| 1928 | SkFontStyle::kItalic_Slant)); |
| 1929 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1930 | builder.addText(text2, strlen(text2)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1931 | builder.pop(); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1932 | builder.addText(text3, strlen(text3)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1933 | |
| 1934 | auto paragraph = builder.Build(); |
| 1935 | paragraph->layout(TestCanvasWidth); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1936 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1937 | |
| 1938 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1939 | |
| 1940 | REPORTER_ASSERT(reporter, impl->runs().size() == 3); |
| 1941 | REPORTER_ASSERT(reporter, impl->styles().size() == 3); |
| 1942 | REPORTER_ASSERT(reporter, impl->lines().size() == 1); |
| 1943 | auto& line = impl->lines()[0]; |
| 1944 | size_t index = 0; |
| 1945 | line.scanStyles( |
| 1946 | StyleType::kForeground, |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1947 | [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1948 | switch (index) { |
| 1949 | case 0: |
| 1950 | REPORTER_ASSERT( |
| 1951 | reporter, |
| 1952 | style.getFontStyle().slant() == SkFontStyle::kUpright_Slant); |
| 1953 | break; |
| 1954 | case 1: |
| 1955 | REPORTER_ASSERT(reporter, |
| 1956 | style.getFontStyle().slant() == SkFontStyle::kItalic_Slant); |
| 1957 | break; |
| 1958 | case 2: |
| 1959 | REPORTER_ASSERT( |
| 1960 | reporter, |
| 1961 | style.getFontStyle().slant() == SkFontStyle::kUpright_Slant); |
| 1962 | break; |
| 1963 | default: |
| 1964 | REPORTER_ASSERT(reporter, false); |
| 1965 | break; |
| 1966 | } |
| 1967 | ++index; |
| 1968 | return true; |
| 1969 | }); |
| 1970 | } |
| 1971 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1972 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1973 | DEF_TEST(SkParagraph_ChineseParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 1974 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1975 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1976 | TestCanvas canvas("SkParagraph_ChineseParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1977 | const char* text = |
| 1978 | "左線読設重説切後碁給能上目秘使約。満毎冠行来昼本可必図将発確年。今属場育" |
| 1979 | "図情闘陰野高備込制詩西校客。審対江置講今固残必託地集済決維駆年策。立得庭" |
| 1980 | "際輝求佐抗蒼提夜合逃表。注統天言件自謙雅載報紙喪。作画稿愛器灯女書利変探" |
| 1981 | "訃第金線朝開化建。子戦年帝励害表月幕株漠新期刊人秘。図的海力生禁挙保天戦" |
| 1982 | "聞条年所在口。"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1983 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1984 | |
| 1985 | ParagraphStyle paragraph_style; |
| 1986 | paragraph_style.setMaxLines(14); |
| 1987 | paragraph_style.setTextAlign(TextAlign::kJustify); |
| 1988 | paragraph_style.turnHintingOff(); |
| 1989 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1990 | |
| 1991 | auto decoration = (TextDecoration)(TextDecoration::kUnderline | TextDecoration::kOverline | |
| 1992 | TextDecoration::kLineThrough); |
| 1993 | |
| 1994 | TextStyle text_style; |
| 1995 | text_style.setFontFamilies({SkString("Source Han Serif CN")}); |
| 1996 | text_style.setFontSize(35); |
| 1997 | text_style.setColor(SK_ColorBLACK); |
| 1998 | text_style.setLetterSpacing(2); |
| 1999 | text_style.setHeight(1); |
| 2000 | text_style.setDecoration(decoration); |
| 2001 | text_style.setDecorationColor(SK_ColorBLACK); |
| 2002 | text_style.setDecorationStyle(TextDecorationStyle::kSolid); |
| 2003 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2004 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2005 | builder.pop(); |
| 2006 | |
| 2007 | auto paragraph = builder.Build(); |
| 2008 | paragraph->layout(TestCanvasWidth - 100); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2009 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2010 | |
Julia Lavrova | 3281b96 | 2019-12-02 11:32:25 -0500 | [diff] [blame] | 2011 | REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == 0); |
| 2012 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2013 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2014 | |
| 2015 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 2016 | REPORTER_ASSERT(reporter, impl->lines().size() == 7); |
| 2017 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 2018 | REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2019 | } |
| 2020 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2021 | // Checked: NO DIFF (disabled) |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2022 | DEF_TEST(SkParagraph_ArabicParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 2023 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2024 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2025 | TestCanvas canvas("SkParagraph_ArabicParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2026 | const char* text = |
| 2027 | "من أسر وإعلان الخاصّة وهولندا،, عل قائمة الضغوط بالمطالبة تلك. الصفحة " |
| 2028 | "بمباركة التقليدية قام عن. تصفح"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2029 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2030 | |
| 2031 | ParagraphStyle paragraph_style; |
| 2032 | paragraph_style.setMaxLines(14); |
| 2033 | paragraph_style.setTextAlign(TextAlign::kJustify); |
| 2034 | paragraph_style.turnHintingOff(); |
| 2035 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 2036 | |
| 2037 | auto decoration = (TextDecoration)(TextDecoration::kUnderline | TextDecoration::kOverline | |
| 2038 | TextDecoration::kLineThrough); |
| 2039 | |
| 2040 | TextStyle text_style; |
| 2041 | text_style.setFontFamilies({SkString("Katibeh")}); |
| 2042 | text_style.setFontSize(35); |
| 2043 | text_style.setColor(SK_ColorBLACK); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2044 | text_style.setLetterSpacing(2); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2045 | text_style.setDecoration(decoration); |
| 2046 | text_style.setDecorationColor(SK_ColorBLACK); |
| 2047 | text_style.setDecorationStyle(TextDecorationStyle::kSolid); |
| 2048 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2049 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2050 | builder.pop(); |
| 2051 | |
| 2052 | auto paragraph = builder.Build(); |
| 2053 | paragraph->layout(TestCanvasWidth - 100); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2054 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2055 | |
Julia Lavrova | 3281b96 | 2019-12-02 11:32:25 -0500 | [diff] [blame] | 2056 | REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == 0); |
| 2057 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2058 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2059 | |
| 2060 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 2061 | REPORTER_ASSERT(reporter, impl->lines().size() == 2); |
| 2062 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 2063 | REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2064 | } |
| 2065 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2066 | // Checked: DIFF (2 boxes and each space is a word) |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2067 | DEF_TEST(SkParagraph_ArabicRectsParagraph, reporter) { |
| 2068 | |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 2069 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2070 | if (!fontCollection->fontsFound()) return; |
| 2071 | TestCanvas canvas("SkParagraph_ArabicRectsParagraph.png"); |
| 2072 | const char* text = "بمباركة التقليدية قام عن. تصفح يد "; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2073 | const size_t len = strlen(text); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2074 | |
| 2075 | ParagraphStyle paragraph_style; |
| 2076 | paragraph_style.turnHintingOff(); |
| 2077 | paragraph_style.setMaxLines(14); |
| 2078 | paragraph_style.setTextAlign(TextAlign::kRight); |
| 2079 | paragraph_style.setTextDirection(TextDirection::kRtl); |
| 2080 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 2081 | |
| 2082 | TextStyle text_style; |
| 2083 | text_style.setFontFamilies({SkString("Noto Naskh Arabic")}); |
| 2084 | text_style.setFontSize(26); |
| 2085 | text_style.setWordSpacing(5); |
| 2086 | text_style.setColor(SK_ColorBLACK); |
| 2087 | text_style.setDecoration(TextDecoration::kUnderline); |
| 2088 | text_style.setDecorationColor(SK_ColorBLACK); |
| 2089 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2090 | builder.addText(text, len); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2091 | builder.pop(); |
| 2092 | |
| 2093 | auto paragraph = builder.Build(); |
| 2094 | paragraph->layout(TestCanvasWidth - 100); |
| 2095 | |
| 2096 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 2097 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 2098 | |
| 2099 | paragraph->paint(canvas.get(), 0, 0); |
| 2100 | |
| 2101 | RectHeightStyle rect_height_style = RectHeightStyle::kMax; |
| 2102 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 2103 | std::vector<TextBox> boxes = paragraph->getRectsForRange(0, 100, rect_height_style, rect_width_style); |
| 2104 | canvas.drawRects(SK_ColorRED, boxes); |
| 2105 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2106 | REPORTER_ASSERT(reporter, boxes.size() == 1ull); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2107 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2108 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 538.548f, EPSILON100)); // DIFF: 510.09375 |
| 2109 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), -0.268f, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2110 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 900, EPSILON100)); |
| 2111 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 44, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2112 | } |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2113 | |
| 2114 | // Checked DIFF+ |
Julia Lavrova | c88a3bc | 2020-01-23 10:16:26 -0500 | [diff] [blame] | 2115 | // This test shows now 2 boxes for [36:40) range: |
| 2116 | // [36:38) for arabic text and [38:39) for the last space |
| 2117 | // that has default paragraph direction (LTR) and is placed at the end of the paragraph |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2118 | DEF_TEST(SkParagraph_ArabicRectsLTRLeftAlignParagraph, reporter) { |
| 2119 | |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 2120 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2121 | if (!fontCollection->fontsFound()) return; |
| 2122 | TestCanvas canvas("SkParagraph_ArabicRectsLTRLeftAlignParagraph.png"); |
| 2123 | const char* text = "Helloبمباركة التقليدية قام عن. تصفح يد "; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2124 | const size_t len = strlen(text); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2125 | |
| 2126 | ParagraphStyle paragraph_style; |
| 2127 | paragraph_style.turnHintingOff(); |
| 2128 | paragraph_style.setMaxLines(14); |
| 2129 | paragraph_style.setTextAlign(TextAlign::kLeft); |
| 2130 | paragraph_style.setTextDirection(TextDirection::kLtr); |
| 2131 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 2132 | |
| 2133 | TextStyle text_style; |
| 2134 | text_style.setFontFamilies({SkString("Noto Naskh Arabic")}); |
| 2135 | text_style.setFontSize(26); |
| 2136 | text_style.setWordSpacing(5); |
| 2137 | text_style.setColor(SK_ColorBLACK); |
| 2138 | text_style.setDecoration(TextDecoration::kUnderline); |
| 2139 | text_style.setDecorationColor(SK_ColorBLACK); |
| 2140 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2141 | builder.addText(text, len); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2142 | builder.pop(); |
| 2143 | |
| 2144 | auto paragraph = builder.Build(); |
| 2145 | paragraph->layout(TestCanvasWidth - 100); |
| 2146 | |
| 2147 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 2148 | REPORTER_ASSERT(reporter, impl->runs().size() == 3); |
| 2149 | |
| 2150 | paragraph->paint(canvas.get(), 0, 0); |
| 2151 | |
| 2152 | RectHeightStyle rect_height_style = RectHeightStyle::kMax; |
| 2153 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
Julia Lavrova | c88a3bc | 2020-01-23 10:16:26 -0500 | [diff] [blame] | 2154 | // There are 39 codepoints: [0:39); asking for [36:40) would give the same as for [36:39) |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2155 | std::vector<TextBox> boxes = paragraph->getRectsForRange(36, 40, rect_height_style, rect_width_style); |
| 2156 | canvas.drawRects(SK_ColorRED, boxes); |
| 2157 | |
Julia Lavrova | c88a3bc | 2020-01-23 10:16:26 -0500 | [diff] [blame] | 2158 | REPORTER_ASSERT(reporter, boxes.size() == 2ull); |
| 2159 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 83.92f, EPSILON100)); // DIFF: 89.40625 |
| 2160 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), -0.27f, EPSILON100)); |
| 2161 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 110.16f, EPSILON100)); // DIFF: 121.87891 |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2162 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 44, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2163 | } |
| 2164 | |
| 2165 | // Checked DIFF+ |
| 2166 | DEF_TEST(SkParagraph_ArabicRectsLTRRightAlignParagraph, reporter) { |
| 2167 | |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 2168 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2169 | if (!fontCollection->fontsFound()) return; |
| 2170 | TestCanvas canvas("SkParagraph_ArabicRectsLTRRightAlignParagraph.png"); |
| 2171 | const char* text = "Helloبمباركة التقليدية قام عن. تصفح يد "; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2172 | const size_t len = strlen(text); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2173 | |
| 2174 | ParagraphStyle paragraph_style; |
| 2175 | paragraph_style.turnHintingOff(); |
| 2176 | paragraph_style.setMaxLines(14); |
| 2177 | paragraph_style.setTextAlign(TextAlign::kRight); |
| 2178 | paragraph_style.setTextDirection(TextDirection::kLtr); |
| 2179 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 2180 | |
| 2181 | TextStyle text_style; |
| 2182 | text_style.setFontFamilies({SkString("Noto Naskh Arabic")}); |
| 2183 | text_style.setFontSize(26); |
| 2184 | text_style.setWordSpacing(5); |
| 2185 | text_style.setColor(SK_ColorBLACK); |
| 2186 | text_style.setDecoration(TextDecoration::kUnderline); |
| 2187 | text_style.setDecorationColor(SK_ColorBLACK); |
| 2188 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2189 | builder.addText(text, len); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2190 | builder.pop(); |
| 2191 | |
| 2192 | auto paragraph = builder.Build(); |
| 2193 | paragraph->layout(TestCanvasWidth - 100); |
| 2194 | |
| 2195 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 2196 | REPORTER_ASSERT(reporter, impl->runs().size() == 3); |
| 2197 | |
| 2198 | paragraph->paint(canvas.get(), 0, 0); |
| 2199 | |
| 2200 | RectHeightStyle rect_height_style = RectHeightStyle::kMax; |
| 2201 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 2202 | std::vector<TextBox> boxes = |
| 2203 | paragraph->getRectsForRange(36, 40, rect_height_style, rect_width_style); |
| 2204 | canvas.drawRects(SK_ColorRED, boxes); |
| 2205 | |
Julia Lavrova | c88a3bc | 2020-01-23 10:16:26 -0500 | [diff] [blame] | 2206 | REPORTER_ASSERT(reporter, boxes.size() == 2ull); // DIFF |
| 2207 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 561.5f, EPSILON100)); // DIFF |
| 2208 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), -0.27f, EPSILON100)); |
| 2209 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 587.74f, EPSILON100)); // DIFF |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2210 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 44, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2211 | } |
| 2212 | |
| 2213 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2214 | DEF_TEST(SkParagraph_GetGlyphPositionAtCoordinateParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 2215 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2216 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2217 | TestCanvas canvas("SkParagraph_GetGlyphPositionAtCoordinateParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2218 | const char* text = |
| 2219 | "12345 67890 12345 67890 12345 67890 12345 67890 12345 67890 12345 " |
| 2220 | "67890 12345"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2221 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2222 | |
| 2223 | ParagraphStyle paragraphStyle; |
| 2224 | paragraphStyle.setTextAlign(TextAlign::kLeft); |
| 2225 | paragraphStyle.setMaxLines(10); |
| 2226 | paragraphStyle.turnHintingOff(); |
| 2227 | TextStyle textStyle; |
| 2228 | textStyle.setFontFamilies({SkString("Roboto")}); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2229 | textStyle.setFontStyle(SkFontStyle(SkFontStyle::kNormal_Weight, SkFontStyle::kNormal_Width, |
| 2230 | SkFontStyle::kUpright_Slant)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2231 | textStyle.setFontSize(50); |
| 2232 | textStyle.setLetterSpacing(1); |
| 2233 | textStyle.setWordSpacing(5); |
| 2234 | textStyle.setHeight(1); |
| 2235 | textStyle.setColor(SK_ColorBLACK); |
| 2236 | |
| 2237 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 2238 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2239 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2240 | builder.pop(); |
| 2241 | |
| 2242 | auto paragraph = builder.Build(); |
| 2243 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2244 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2245 | |
| 2246 | // Tests for getGlyphPositionAtCoordinate() |
| 2247 | // NOTE: resulting values can be a few off from their respective positions in |
| 2248 | // the original text because the final trailing whitespaces are sometimes not |
| 2249 | // drawn (namely, when using "justify" alignment) and therefore are not active |
| 2250 | // glyphs. |
| 2251 | REPORTER_ASSERT(reporter, |
| 2252 | paragraph->getGlyphPositionAtCoordinate(-10000, -10000).position == 0); |
| 2253 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(-1, -1).position == 0); |
| 2254 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(0, 0).position == 0); |
| 2255 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(3, 3).position == 0); |
| 2256 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(35, 1).position == 1); |
| 2257 | REPORTER_ASSERT(reporter, |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2258 | paragraph->getGlyphPositionAtCoordinate(300, 2).position == 11); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2259 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(301, 2.2f).position == 11); |
| 2260 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(302, 2.6f).position == 11); |
| 2261 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(301, 2.1f).position == 11); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2262 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(100000, 20).position == 18);// |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2263 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(450, 20).position == 16); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2264 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(100000, 90).position == 36);// |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2265 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(-100000, 90).position == 18); |
| 2266 | REPORTER_ASSERT(reporter, |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2267 | paragraph->getGlyphPositionAtCoordinate(20, -80).position == 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2268 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(1, 90).position == 18); |
| 2269 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(1, 170).position == 36); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2270 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(10000, 180).position == 72);// |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2271 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(70, 180).position == 56); |
| 2272 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(1, 270).position == 72); |
| 2273 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(35, 90).position == 19); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2274 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(10000, 10000).position == 77);// |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2275 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(85, 10000).position == 75); |
| 2276 | } |
| 2277 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2278 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2279 | DEF_TEST(SkParagraph_GetRectsForRangeParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 2280 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2281 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2282 | TestCanvas canvas("SkParagraph_GetRectsForRangeParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2283 | const char* text = |
| 2284 | "12345, \"67890\" 12345 67890 12345 67890 12345 67890 12345 67890 12345 " |
| 2285 | "67890 12345"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2286 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2287 | |
| 2288 | ParagraphStyle paragraphStyle; |
| 2289 | paragraphStyle.setTextAlign(TextAlign::kLeft); |
| 2290 | paragraphStyle.setMaxLines(10); |
| 2291 | paragraphStyle.turnHintingOff(); |
| 2292 | TextStyle textStyle; |
| 2293 | textStyle.setFontFamilies({SkString("Roboto")}); |
| 2294 | textStyle.setFontSize(50); |
| 2295 | textStyle.setColor(SK_ColorBLACK); |
| 2296 | textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 2297 | SkFontStyle::kUpright_Slant)); |
| 2298 | |
| 2299 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 2300 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2301 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2302 | builder.pop(); |
| 2303 | |
| 2304 | auto paragraph = builder.Build(); |
| 2305 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2306 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2307 | |
| 2308 | RectHeightStyle heightStyle = RectHeightStyle::kMax; |
| 2309 | RectWidthStyle widthStyle = RectWidthStyle::kTight; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2310 | |
| 2311 | SkPaint paint; |
| 2312 | paint.setStyle(SkPaint::kStroke_Style); |
| 2313 | paint.setAntiAlias(true); |
| 2314 | paint.setStrokeWidth(1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2315 | |
| 2316 | { |
| 2317 | auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle); |
| 2318 | REPORTER_ASSERT(reporter, result.empty()); |
| 2319 | } |
| 2320 | { |
| 2321 | auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2322 | canvas.drawRects(SK_ColorRED, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2323 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2324 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 0, EPSILON100)); |
| 2325 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100)); |
| 2326 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 28.417f, EPSILON100)); |
| 2327 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2328 | } |
| 2329 | { |
| 2330 | auto result = paragraph->getRectsForRange(2, 8, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2331 | canvas.drawRects(SK_ColorBLUE, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2332 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2333 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 56.835f, EPSILON100)); |
| 2334 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100)); |
| 2335 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 177.97f, EPSILON100)); |
| 2336 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2337 | } |
| 2338 | { |
| 2339 | auto result = paragraph->getRectsForRange(8, 21, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2340 | canvas.drawRects(SK_ColorGREEN, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2341 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2342 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 177.97f, EPSILON100)); |
| 2343 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100)); |
| 2344 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 507.031f, EPSILON100)); |
| 2345 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2346 | } |
| 2347 | { |
| 2348 | auto result = paragraph->getRectsForRange(30, 100, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2349 | canvas.drawRects(SK_ColorRED, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2350 | REPORTER_ASSERT(reporter, result.size() == 4); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2351 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 211.375f, EPSILON100)); |
| 2352 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 59.40625f, EPSILON100)); |
| 2353 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 463.623f, EPSILON100)); |
| 2354 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 118, EPSILON100)); |
| 2355 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.left(), 0, EPSILON100)); |
| 2356 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.top(), 236.406f, EPSILON100)); |
| 2357 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.right(), 142.089f, EPSILON100)); |
| 2358 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.bottom(), 295, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2359 | } |
| 2360 | { |
| 2361 | auto result = paragraph->getRectsForRange(19, 22, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2362 | canvas.drawRects(SK_ColorBLUE, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2363 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2364 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 450.1875f, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2365 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100)); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2366 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 519.47266f, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2367 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2368 | } |
| 2369 | { |
| 2370 | auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle); |
| 2371 | REPORTER_ASSERT(reporter, result.empty()); |
| 2372 | } |
| 2373 | } |
| 2374 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2375 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2376 | DEF_TEST(SkParagraph_GetRectsForRangeTight, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 2377 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2378 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2379 | TestCanvas canvas("SkParagraph_GetRectsForRangeTight.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2380 | const char* text = |
| 2381 | "( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)(" |
| 2382 | " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)(" |
| 2383 | " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2384 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2385 | |
| 2386 | ParagraphStyle paragraphStyle; |
| 2387 | paragraphStyle.setTextAlign(TextAlign::kLeft); |
| 2388 | paragraphStyle.setMaxLines(10); |
| 2389 | paragraphStyle.turnHintingOff(); |
| 2390 | TextStyle textStyle; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2391 | textStyle.setFontFamilies({SkString("Noto Sans CJK JP")}); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2392 | textStyle.setFontSize(50); |
| 2393 | textStyle.setColor(SK_ColorBLACK); |
| 2394 | textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 2395 | SkFontStyle::kUpright_Slant)); |
| 2396 | |
| 2397 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 2398 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2399 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2400 | builder.pop(); |
| 2401 | |
| 2402 | auto paragraph = builder.Build(); |
| 2403 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2404 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2405 | |
| 2406 | RectHeightStyle heightStyle = RectHeightStyle::kTight; |
| 2407 | RectWidthStyle widthStyle = RectWidthStyle::kTight; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2408 | { |
| 2409 | auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle); |
| 2410 | REPORTER_ASSERT(reporter, result.empty()); |
| 2411 | } |
| 2412 | { |
| 2413 | auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2414 | canvas.drawRects(SK_ColorRED, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2415 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2416 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 0, EPSILON100)); |
| 2417 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0, EPSILON100)); |
| 2418 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 16.898f, EPSILON100)); |
| 2419 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 74, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2420 | } |
| 2421 | { |
| 2422 | auto result = paragraph->getRectsForRange(2, 8, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2423 | canvas.drawRects(SK_ColorBLUE, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2424 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2425 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 66.899f, EPSILON100)); |
| 2426 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0, EPSILON100)); |
| 2427 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 264.099f, EPSILON100)); |
| 2428 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 74, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2429 | } |
| 2430 | { |
| 2431 | auto result = paragraph->getRectsForRange(8, 21, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2432 | canvas.drawRects(SK_ColorGREEN, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2433 | REPORTER_ASSERT(reporter, result.size() == 2); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2434 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 264.099f, EPSILON100)); |
| 2435 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0, EPSILON100)); |
| 2436 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 595.085f, EPSILON50)); |
| 2437 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 74, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2438 | } |
| 2439 | } |
| 2440 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2441 | // Checked: DIFF+ |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2442 | DEF_TEST(SkParagraph_GetRectsForRangeIncludeLineSpacingMiddle, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 2443 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2444 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2445 | TestCanvas canvas("SkParagraph_GetRectsForRangeIncludeLineSpacingMiddle.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2446 | const char* text = |
| 2447 | "( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)(" |
| 2448 | " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)(" |
| 2449 | " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2450 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2451 | |
| 2452 | ParagraphStyle paragraphStyle; |
| 2453 | paragraphStyle.setTextAlign(TextAlign::kLeft); |
| 2454 | paragraphStyle.setMaxLines(10); |
| 2455 | paragraphStyle.turnHintingOff(); |
| 2456 | TextStyle textStyle; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2457 | textStyle.setFontFamilies({SkString("Roboto")}); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2458 | textStyle.setFontSize(50); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2459 | textStyle.setHeight(1.6f); |
| 2460 | textStyle.setHeightOverride(true); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2461 | textStyle.setColor(SK_ColorBLACK); |
| 2462 | textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 2463 | SkFontStyle::kUpright_Slant)); |
| 2464 | |
| 2465 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 2466 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2467 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2468 | builder.pop(); |
| 2469 | |
| 2470 | auto paragraph = builder.Build(); |
| 2471 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2472 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2473 | |
| 2474 | RectHeightStyle heightStyle = RectHeightStyle::kIncludeLineSpacingMiddle; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2475 | RectWidthStyle widthStyle = RectWidthStyle::kMax; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2476 | { |
| 2477 | auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle); |
| 2478 | REPORTER_ASSERT(reporter, result.empty()); |
| 2479 | } |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2480 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2481 | { |
| 2482 | auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2483 | canvas.drawRects(SK_ColorRED, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2484 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2485 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 0, EPSILON100)); |
| 2486 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946615f, EPSILON100)); |
| 2487 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 17.4296889f, EPSILON100)); |
| 2488 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 88.473305f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2489 | } |
| 2490 | { |
| 2491 | auto result = paragraph->getRectsForRange(2, 8, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2492 | canvas.drawRects(SK_ColorBLUE, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2493 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2494 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 67.429688f, EPSILON100)); |
| 2495 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946615f, EPSILON100)); |
| 2496 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 190.00781f, EPSILON100)); |
| 2497 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 88.473305f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2498 | } |
| 2499 | { |
| 2500 | auto result = paragraph->getRectsForRange(8, 21, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2501 | canvas.drawRects(SK_ColorGREEN, result); |
| 2502 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2503 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 190.00781f, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2504 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946615f, EPSILON100)); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2505 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 508.0625f, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2506 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 88.473305f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2507 | } |
| 2508 | { |
| 2509 | auto result = paragraph->getRectsForRange(30, 150, heightStyle, widthStyle); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2510 | canvas.drawRects(SK_ColorRED, result); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2511 | REPORTER_ASSERT(reporter, result.size() == 8); |
| 2512 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2513 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 190.00781f, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2514 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 88.473305f, EPSILON100)); |
| 2515 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 525.687f, EPSILON20)); |
| 2516 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 168.47331f, EPSILON100)); |
| 2517 | |
| 2518 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.left(), 525.687f, EPSILON20)); |
| 2519 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.top(), 88.473305f, EPSILON100)); |
| 2520 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.right(), 570.02344f, EPSILON20)); |
| 2521 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.bottom(), 168.47331f, EPSILON100)); |
| 2522 | |
| 2523 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.left(), 0, EPSILON100)); |
| 2524 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.top(), 168.47331f, EPSILON100)); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2525 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.right(), 531.574f, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2526 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.bottom(), 248.47331f, EPSILON100)); |
| 2527 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2528 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.left(), 531.574f, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2529 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.top(), 168.47331f, EPSILON100)); |
| 2530 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.right(), 570.02344f, EPSILON20)); |
| 2531 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.bottom(), 248.47331f, EPSILON100)); |
| 2532 | |
| 2533 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.left(), 0, EPSILON100)); |
| 2534 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.top(), 248.47331f, EPSILON100)); |
| 2535 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.right(), 570.02344f, EPSILON20)); |
| 2536 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.bottom(), 328.47333f, EPSILON100)); |
| 2537 | |
| 2538 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.left(), 0, EPSILON100)); |
| 2539 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.top(), 328.47333f, EPSILON100)); |
| 2540 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.right(), 570.02344f, EPSILON20)); |
| 2541 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.bottom(), 408.4733f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2542 | } |
| 2543 | { |
| 2544 | auto result = paragraph->getRectsForRange(19, 22, heightStyle, widthStyle); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2545 | canvas.drawRects(SK_ColorBLUE, result); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2546 | REPORTER_ASSERT(reporter, result.size() == 2); // DIFF |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2547 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 463.72656f, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2548 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946615f, EPSILON100)); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2549 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 530.23047f, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2550 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 88.473305f, EPSILON100)); |
| 2551 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2552 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.left(), 530.23047f, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2553 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.top(), 16.946615f, EPSILON100)); |
| 2554 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.right(), 570.02344f, EPSILON20)); |
| 2555 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.bottom(), 88.473305f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2556 | } |
| 2557 | { |
| 2558 | auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle); |
| 2559 | REPORTER_ASSERT(reporter, result.empty()); |
| 2560 | } |
| 2561 | } |
| 2562 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2563 | // Checked: NO DIFF+ |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2564 | DEF_TEST(SkParagraph_GetRectsForRangeIncludeLineSpacingTop, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 2565 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2566 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2567 | TestCanvas canvas("SkParagraph_GetRectsForRangeIncludeLineSpacingTop.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2568 | const char* text = |
| 2569 | "( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)(" |
| 2570 | " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)(" |
| 2571 | " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2572 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2573 | |
| 2574 | ParagraphStyle paragraphStyle; |
| 2575 | paragraphStyle.setTextAlign(TextAlign::kLeft); |
| 2576 | paragraphStyle.setMaxLines(10); |
| 2577 | paragraphStyle.turnHintingOff(); |
| 2578 | TextStyle textStyle; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2579 | textStyle.setFontFamilies({SkString("Roboto")}); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2580 | textStyle.setFontSize(50); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2581 | textStyle.setHeight(1.6f); |
| 2582 | textStyle.setHeightOverride(true); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2583 | textStyle.setColor(SK_ColorBLACK); |
| 2584 | textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 2585 | SkFontStyle::kUpright_Slant)); |
| 2586 | |
| 2587 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 2588 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2589 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2590 | builder.pop(); |
| 2591 | |
| 2592 | auto paragraph = builder.Build(); |
| 2593 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2594 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2595 | |
| 2596 | RectHeightStyle heightStyle = RectHeightStyle::kIncludeLineSpacingTop; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2597 | RectWidthStyle widthStyle = RectWidthStyle::kMax; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2598 | { |
| 2599 | auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle); |
| 2600 | REPORTER_ASSERT(reporter, result.empty()); |
| 2601 | } |
| 2602 | |
| 2603 | { |
| 2604 | auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2605 | canvas.drawRects(SK_ColorRED, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2606 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2607 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 0, EPSILON100)); |
| 2608 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946615f, EPSILON100)); |
| 2609 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 17.4296889f, EPSILON100)); |
| 2610 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 80, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2611 | } |
| 2612 | { |
| 2613 | auto result = paragraph->getRectsForRange(2, 8, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2614 | canvas.drawRects(SK_ColorBLUE, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2615 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2616 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 67.429688f, EPSILON100)); |
| 2617 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946615f, EPSILON100)); |
| 2618 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 190.00781f, EPSILON100)); |
| 2619 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 80, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2620 | } |
| 2621 | { |
| 2622 | auto result = paragraph->getRectsForRange(8, 21, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2623 | canvas.drawRects(SK_ColorGREEN, result); |
| 2624 | REPORTER_ASSERT(reporter, result.size() == 1); |
| 2625 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 190.00781f, EPSILON100)); |
| 2626 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946615f, EPSILON100)); |
| 2627 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 508.0625f, EPSILON50)); |
| 2628 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 80, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2629 | } |
| 2630 | { |
| 2631 | auto result = paragraph->getRectsForRange(30, 150, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2632 | canvas.drawRects(SK_ColorMAGENTA, result); |
| 2633 | REPORTER_ASSERT(reporter, result.size() == 8); |
| 2634 | |
| 2635 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 190.00781f, EPSILON100)); |
| 2636 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 80, EPSILON100)); |
| 2637 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 525.687f, EPSILON20)); |
| 2638 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 160, EPSILON100)); |
| 2639 | |
| 2640 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.left(), 525.687f, EPSILON20)); |
| 2641 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.top(), 80, EPSILON100)); |
| 2642 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.right(), 570.02344f, EPSILON20)); |
| 2643 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.bottom(), 160, EPSILON100)); |
| 2644 | |
| 2645 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.left(), 0, EPSILON100)); |
| 2646 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.top(), 160, EPSILON100)); |
| 2647 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.right(), 531.574f, EPSILON20)); |
| 2648 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.bottom(), 240, EPSILON100)); |
| 2649 | |
| 2650 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.left(), 531.574f, EPSILON20)); |
| 2651 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.top(), 160, EPSILON100)); |
| 2652 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.right(), 570.02344f, EPSILON20)); |
| 2653 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.bottom(), 240, EPSILON100)); |
| 2654 | |
| 2655 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.left(), 0, EPSILON100)); |
| 2656 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.top(), 240, EPSILON100)); |
| 2657 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.right(), 570.02344f, EPSILON20)); |
| 2658 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.bottom(), 320, EPSILON100)); |
| 2659 | |
| 2660 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.left(), 0, EPSILON100)); |
| 2661 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.top(), 320, EPSILON100)); |
| 2662 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.right(), 570.02344f, EPSILON20)); |
| 2663 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.bottom(), 400, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2664 | } |
| 2665 | { |
| 2666 | auto result = paragraph->getRectsForRange(19, 22, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2667 | canvas.drawRects(SK_ColorBLACK, result); |
| 2668 | REPORTER_ASSERT(reporter, result.size() == 2); // DIFF |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2669 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 463.72656f, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2670 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946615f, EPSILON100)); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2671 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 530.23047f, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2672 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 80, EPSILON100)); |
| 2673 | |
| 2674 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.left(), 530.23047f, EPSILON50)); |
| 2675 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.top(), 16.946615f, EPSILON100)); |
| 2676 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.right(), 570.02344f, EPSILON20)); |
| 2677 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.bottom(), 80, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2678 | } |
| 2679 | { |
| 2680 | auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle); |
| 2681 | REPORTER_ASSERT(reporter, result.empty()); |
| 2682 | } |
| 2683 | } |
| 2684 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2685 | // Checked: NO DIFF+ |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2686 | DEF_TEST(SkParagraph_GetRectsForRangeIncludeLineSpacingBottom, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 2687 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2688 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2689 | TestCanvas canvas("SkParagraph_GetRectsForRangeIncludeLineSpacingBottom.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2690 | const char* text = |
| 2691 | "( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)(" |
| 2692 | " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)(" |
| 2693 | " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2694 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2695 | |
| 2696 | ParagraphStyle paragraphStyle; |
| 2697 | paragraphStyle.setTextAlign(TextAlign::kLeft); |
| 2698 | paragraphStyle.setMaxLines(10); |
| 2699 | paragraphStyle.turnHintingOff(); |
| 2700 | TextStyle textStyle; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2701 | textStyle.setFontFamilies({SkString("Roboto")}); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2702 | textStyle.setFontSize(50); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2703 | textStyle.setHeight(1.6f); |
| 2704 | textStyle.setHeightOverride(true); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2705 | textStyle.setColor(SK_ColorBLACK); |
| 2706 | textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 2707 | SkFontStyle::kUpright_Slant)); |
| 2708 | |
| 2709 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 2710 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2711 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2712 | builder.pop(); |
| 2713 | |
| 2714 | auto paragraph = builder.Build(); |
| 2715 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2716 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2717 | |
| 2718 | RectHeightStyle heightStyle = RectHeightStyle::kIncludeLineSpacingBottom; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2719 | RectWidthStyle widthStyle = RectWidthStyle::kMax; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2720 | { |
| 2721 | auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle); |
| 2722 | REPORTER_ASSERT(reporter, result.empty()); |
| 2723 | } |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2724 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2725 | { |
| 2726 | auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2727 | canvas.drawRects(SK_ColorRED, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2728 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2729 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 0, EPSILON100)); |
| 2730 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946f, EPSILON100)); |
| 2731 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 17.429f, EPSILON100)); |
| 2732 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 96.946f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2733 | } |
| 2734 | { |
| 2735 | auto result = paragraph->getRectsForRange(2, 8, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2736 | canvas.drawRects(SK_ColorBLUE, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2737 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2738 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 67.4298f, EPSILON100)); |
| 2739 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946f, EPSILON100)); |
| 2740 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 190.007f, EPSILON100)); |
| 2741 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 96.946f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2742 | } |
| 2743 | { |
| 2744 | auto result = paragraph->getRectsForRange(8, 21, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2745 | canvas.drawRects(SK_ColorGREEN, result); |
| 2746 | REPORTER_ASSERT(reporter, result.size() == 1); |
| 2747 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 190.007f, EPSILON100)); |
| 2748 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946f, EPSILON100)); |
| 2749 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 508.062f, EPSILON50)); |
| 2750 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 96.946f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2751 | } |
| 2752 | { |
| 2753 | auto result = paragraph->getRectsForRange(30, 150, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2754 | canvas.drawRects(SK_ColorMAGENTA, result); |
| 2755 | REPORTER_ASSERT(reporter, result.size() == 8); |
| 2756 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2757 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 190.007f, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2758 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 96.946f, EPSILON100)); |
| 2759 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 525.687f, EPSILON20)); |
| 2760 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 176.946f, EPSILON100)); |
| 2761 | |
| 2762 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.left(), 525.687f, EPSILON20)); |
| 2763 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.top(), 96.946f, EPSILON100)); |
| 2764 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.right(), 570.023f, EPSILON20)); |
| 2765 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.bottom(), 176.946f, EPSILON100)); |
| 2766 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2767 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.left(), 0, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2768 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.top(), 176.946f, EPSILON100)); |
| 2769 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.right(), 531.574f, EPSILON20)); |
| 2770 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.bottom(), 256.946f, EPSILON100)); |
| 2771 | |
| 2772 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.left(), 531.574f, EPSILON20)); |
| 2773 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.top(), 176.946f, EPSILON100)); |
| 2774 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.right(), 570.023f, EPSILON20)); |
| 2775 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.bottom(), 256.946f, EPSILON100)); |
| 2776 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2777 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.left(), 0, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2778 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.top(), 256.946f, EPSILON100)); |
| 2779 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.right(), 570.023f, EPSILON20)); |
| 2780 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.bottom(), 336.946f, EPSILON100)); |
| 2781 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2782 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.left(), 0, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2783 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.top(), 336.946f, EPSILON100)); |
| 2784 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.right(), 570.023f, EPSILON20)); |
| 2785 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.bottom(), 416.946f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2786 | } |
| 2787 | { |
| 2788 | auto result = paragraph->getRectsForRange(19, 22, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2789 | canvas.drawRects(SK_ColorBLACK, result); |
| 2790 | REPORTER_ASSERT(reporter, result.size() == 2); // DIFF |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2791 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 463.726f, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2792 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946f, EPSILON100)); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2793 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 530.230f, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2794 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 96.946f, EPSILON100)); |
| 2795 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2796 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.left(), 530.230f, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2797 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.top(), 16.946f, EPSILON100)); |
| 2798 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.right(), 570.023f, EPSILON20)); |
| 2799 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.bottom(), 96.946f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2800 | } |
| 2801 | { |
| 2802 | auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle); |
| 2803 | REPORTER_ASSERT(reporter, result.empty()); |
| 2804 | } |
| 2805 | } |
| 2806 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2807 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2808 | DEF_TEST(SkParagraph_GetRectsForRangeIncludeCombiningCharacter, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 2809 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2810 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2811 | TestCanvas canvas("SkParagraph_GetRectsForRangeIncludeCombiningCharacter.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2812 | const char* text = "ดีสวัสดีชาวโลกที่น่ารัก"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2813 | const size_t len = strlen(text); |
| 2814 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2815 | ParagraphStyle paragraphStyle; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2816 | paragraphStyle.setTextAlign(TextAlign::kLeft); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2817 | paragraphStyle.setMaxLines(10); |
| 2818 | paragraphStyle.turnHintingOff(); |
| 2819 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 2820 | |
| 2821 | TextStyle textStyle; |
| 2822 | textStyle.setFontFamilies({SkString("Roboto")}); |
| 2823 | textStyle.setFontSize(50); |
| 2824 | textStyle.setLetterSpacing(1); |
| 2825 | textStyle.setWordSpacing(5); |
| 2826 | textStyle.setHeight(1); |
| 2827 | textStyle.setColor(SK_ColorBLACK); |
| 2828 | |
| 2829 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2830 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2831 | builder.pop(); |
| 2832 | |
| 2833 | auto paragraph = builder.Build(); |
| 2834 | paragraph->layout(TestCanvasWidth - 100); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2835 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2836 | |
| 2837 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2838 | REPORTER_ASSERT(reporter, impl->lines().size() == 1); |
| 2839 | |
| 2840 | RectHeightStyle heightStyle = RectHeightStyle::kTight; |
| 2841 | RectWidthStyle widthStyle = RectWidthStyle::kTight; |
| 2842 | { |
| 2843 | auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle); |
| 2844 | REPORTER_ASSERT(reporter, result.empty()); |
| 2845 | } |
| 2846 | { |
| 2847 | auto first = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle); |
| 2848 | auto second = paragraph->getRectsForRange(1, 2, heightStyle, widthStyle); |
| 2849 | auto last = paragraph->getRectsForRange(0, 2, heightStyle, widthStyle); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2850 | REPORTER_ASSERT(reporter, first.size() == 0 && second.size() == 1 && last.size() == 1); |
| 2851 | REPORTER_ASSERT(reporter, second[0].rect == last[0].rect); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2852 | } |
| 2853 | { |
| 2854 | auto first = paragraph->getRectsForRange(3, 4, heightStyle, widthStyle); |
| 2855 | auto second = paragraph->getRectsForRange(4, 5, heightStyle, widthStyle); |
| 2856 | auto last = paragraph->getRectsForRange(3, 5, heightStyle, widthStyle); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2857 | REPORTER_ASSERT(reporter, first.size() == 0 && second.size() == 1 && last.size() == 1); |
| 2858 | REPORTER_ASSERT(reporter, second[0].rect == last[0].rect); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2859 | } |
| 2860 | { |
| 2861 | auto first = paragraph->getRectsForRange(14, 15, heightStyle, widthStyle); |
| 2862 | auto second = paragraph->getRectsForRange(15, 16, heightStyle, widthStyle); |
| 2863 | auto third = paragraph->getRectsForRange(16, 17, heightStyle, widthStyle); |
| 2864 | auto last = paragraph->getRectsForRange(14, 17, heightStyle, widthStyle); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2865 | REPORTER_ASSERT(reporter, first.size() == 0 && second.size() == 0 && third.size() == 1 && last.size() == 1); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2866 | REPORTER_ASSERT(reporter, third[0].rect == last[0].rect); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2867 | } |
| 2868 | } |
| 2869 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2870 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2871 | DEF_TEST(SkParagraph_GetRectsForRangeCenterParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 2872 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2873 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2874 | TestCanvas canvas("SkParagraph_GetRectsForRangeCenterParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2875 | // Minikin uses a hard coded list of unicode characters that he treats as invisible - as spaces. |
| 2876 | // It's absolutely wrong - invisibility is a glyph attribute, not character/grapheme. |
| 2877 | // Any attempt to substitute one for another leads to errors |
| 2878 | // (for instance, some fonts can use these hard coded characters for something that is visible) |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2879 | const char* text = "01234 "; // includes ideographic space and english space. |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2880 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2881 | |
| 2882 | ParagraphStyle paragraphStyle; |
| 2883 | paragraphStyle.setTextAlign(TextAlign::kCenter); |
| 2884 | paragraphStyle.setMaxLines(10); |
| 2885 | paragraphStyle.turnHintingOff(); |
| 2886 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 2887 | |
| 2888 | TextStyle textStyle; |
| 2889 | textStyle.setFontFamilies({SkString("Roboto")}); |
| 2890 | textStyle.setFontSize(50); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2891 | textStyle.setHeight(1); |
| 2892 | textStyle.setColor(SK_ColorBLACK); |
| 2893 | textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 2894 | SkFontStyle::kUpright_Slant)); |
| 2895 | |
| 2896 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2897 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2898 | builder.pop(); |
| 2899 | |
| 2900 | auto paragraph = builder.Build(); |
| 2901 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2902 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2903 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2904 | // Some of the formatting lazily done on paint |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2905 | RectHeightStyle heightStyle = RectHeightStyle::kMax; |
| 2906 | RectWidthStyle widthStyle = RectWidthStyle::kTight; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2907 | { |
| 2908 | auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle); |
| 2909 | REPORTER_ASSERT(reporter, result.empty()); |
| 2910 | } |
| 2911 | |
| 2912 | { |
| 2913 | auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2914 | canvas.drawRects(SK_ColorRED, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2915 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2916 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 203.955f, EPSILON100)); |
| 2917 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100)); |
| 2918 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 232.373f, EPSILON100)); |
| 2919 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2920 | } |
| 2921 | |
| 2922 | { |
| 2923 | auto result = paragraph->getRectsForRange(2, 4, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2924 | canvas.drawRects(SK_ColorBLUE, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2925 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2926 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 260.791f, EPSILON100)); |
| 2927 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100)); |
| 2928 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 317.626f, EPSILON100)); |
| 2929 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2930 | } |
| 2931 | |
| 2932 | { |
| 2933 | auto result = paragraph->getRectsForRange(4, 5, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2934 | canvas.drawRects(SK_ColorGREEN, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2935 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2936 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 317.626f, EPSILON100)); |
| 2937 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100)); |
| 2938 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 346.044f, EPSILON100)); |
| 2939 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2940 | } |
| 2941 | |
| 2942 | { |
| 2943 | auto result = paragraph->getRectsForRange(4, 6, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2944 | canvas.drawRects(SK_ColorBLACK, result); |
| 2945 | REPORTER_ASSERT(reporter, result.size() == 1); // DIFF |
| 2946 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 317.626f, EPSILON100)); |
| 2947 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100)); |
| 2948 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 358.494f, EPSILON100)); |
| 2949 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2950 | } |
| 2951 | |
| 2952 | { |
| 2953 | auto result = paragraph->getRectsForRange(5, 6, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2954 | canvas.drawRects(SK_ColorRED, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2955 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2956 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 346.044f, EPSILON100)); |
| 2957 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100)); |
| 2958 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 358.494f, EPSILON100)); |
| 2959 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2960 | } |
| 2961 | |
| 2962 | { |
| 2963 | auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle); |
| 2964 | REPORTER_ASSERT(reporter, result.empty()); |
| 2965 | } |
| 2966 | } |
| 2967 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2968 | // Checked DIFF+ |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2969 | DEF_TEST(SkParagraph_GetRectsForRangeCenterParagraphNewlineCentered, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 2970 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2971 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2972 | TestCanvas canvas("SkParagraph_GetRectsForRangeCenterParagraphNewlineCentered.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2973 | const char* text = "01234\n"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2974 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2975 | |
| 2976 | ParagraphStyle paragraphStyle; |
| 2977 | paragraphStyle.setTextAlign(TextAlign::kCenter); |
| 2978 | paragraphStyle.setMaxLines(10); |
| 2979 | paragraphStyle.turnHintingOff(); |
| 2980 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 2981 | |
| 2982 | TextStyle textStyle; |
| 2983 | textStyle.setFontFamilies({SkString("Roboto")}); |
| 2984 | textStyle.setFontSize(50); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2985 | textStyle.setHeight(1); |
| 2986 | textStyle.setColor(SK_ColorBLACK); |
| 2987 | textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 2988 | SkFontStyle::kUpright_Slant)); |
| 2989 | |
| 2990 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2991 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2992 | builder.pop(); |
| 2993 | |
| 2994 | auto paragraph = builder.Build(); |
| 2995 | paragraph->layout(550); |
| 2996 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2997 | paragraph->paint(canvas.get(), 0, 0); |
| 2998 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2999 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3000 | REPORTER_ASSERT(reporter, impl->lines().size() == 2); |
| 3001 | |
| 3002 | RectHeightStyle heightStyle = RectHeightStyle::kMax; |
| 3003 | RectWidthStyle widthStyle = RectWidthStyle::kTight; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3004 | { |
| 3005 | auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle); |
| 3006 | REPORTER_ASSERT(reporter, result.empty()); |
| 3007 | } |
| 3008 | |
| 3009 | { |
| 3010 | auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 3011 | canvas.drawRects(SK_ColorRED, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3012 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3013 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 203.955f, EPSILON100)); |
| 3014 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100)); |
| 3015 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 232.373f, EPSILON100)); |
| 3016 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3017 | } |
| 3018 | |
| 3019 | { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3020 | auto result = paragraph->getRectsForRange(6, 7, heightStyle, widthStyle); |
| 3021 | canvas.drawRects(SK_ColorBLUE, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3022 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3023 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 275.0f, EPSILON100)); |
| 3024 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 59.406f, EPSILON100)); |
| 3025 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 275.0f, EPSILON100)); |
| 3026 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 118, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3027 | } |
| 3028 | } |
| 3029 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3030 | // Checked NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3031 | DEF_TEST(SkParagraph_GetRectsForRangeCenterMultiLineParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3032 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3033 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3034 | TestCanvas canvas("SkParagraph_GetRectsForRangeCenterMultiLineParagraph.png"); |
| 3035 | const char* text = "01234 \n0123 "; // includes ideographic space and english space. |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3036 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3037 | |
| 3038 | ParagraphStyle paragraphStyle; |
| 3039 | paragraphStyle.setTextAlign(TextAlign::kCenter); |
| 3040 | paragraphStyle.setMaxLines(10); |
| 3041 | paragraphStyle.turnHintingOff(); |
| 3042 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 3043 | |
| 3044 | TextStyle textStyle; |
| 3045 | textStyle.setFontFamilies({SkString("Roboto")}); |
| 3046 | textStyle.setFontSize(50); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3047 | textStyle.setHeight(1); |
| 3048 | textStyle.setColor(SK_ColorBLACK); |
| 3049 | textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 3050 | SkFontStyle::kUpright_Slant)); |
| 3051 | |
| 3052 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3053 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3054 | builder.pop(); |
| 3055 | |
| 3056 | auto paragraph = builder.Build(); |
| 3057 | paragraph->layout(550); |
| 3058 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3059 | paragraph->paint(canvas.get(), 0, 0); |
| 3060 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3061 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3062 | |
| 3063 | REPORTER_ASSERT(reporter, impl->lines().size() == 2); |
| 3064 | |
| 3065 | RectHeightStyle heightStyle = RectHeightStyle::kMax; |
| 3066 | RectWidthStyle widthStyle = RectWidthStyle::kTight; |
| 3067 | SkScalar epsilon = 0.01f; |
| 3068 | { |
| 3069 | auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle); |
| 3070 | REPORTER_ASSERT(reporter, result.empty()); |
| 3071 | } |
| 3072 | { |
| 3073 | auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3074 | canvas.drawRects(SK_ColorRED, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3075 | REPORTER_ASSERT(reporter, result.size() == 1); |
| 3076 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 203.955f, epsilon)); |
| 3077 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon)); |
| 3078 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 232.373f, epsilon)); |
| 3079 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon)); |
| 3080 | } |
| 3081 | { |
| 3082 | auto result = paragraph->getRectsForRange(2, 4, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3083 | canvas.drawRects(SK_ColorBLUE, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3084 | REPORTER_ASSERT(reporter, result.size() == 1); |
| 3085 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 260.791f, epsilon)); |
| 3086 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon)); |
| 3087 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 317.626f, epsilon)); |
| 3088 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon)); |
| 3089 | } |
| 3090 | { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3091 | auto result = paragraph->getRectsForRange(4, 6, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3092 | canvas.drawRects(SK_ColorGREEN, result); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3093 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3094 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 317.626f, epsilon)); |
| 3095 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon)); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3096 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 358.494f, epsilon)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3097 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon)); |
| 3098 | } |
| 3099 | { |
| 3100 | auto result = paragraph->getRectsForRange(5, 6, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3101 | canvas.drawRects(SK_ColorYELLOW, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3102 | REPORTER_ASSERT(reporter, result.size() == 1); |
| 3103 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 346.044f, epsilon)); |
| 3104 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3105 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 358.494f, epsilon)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3106 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon)); |
| 3107 | } |
| 3108 | { |
| 3109 | auto result = paragraph->getRectsForRange(10, 12, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3110 | canvas.drawRects(SK_ColorCYAN, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3111 | REPORTER_ASSERT(reporter, result.size() == 1); |
| 3112 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 218.164f, epsilon)); |
| 3113 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 59.40625f, epsilon)); |
| 3114 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 275, epsilon)); |
| 3115 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 118, epsilon)); |
| 3116 | } |
| 3117 | { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3118 | auto result = paragraph->getRectsForRange(14, 18, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3119 | canvas.drawRects(SK_ColorBLACK, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3120 | REPORTER_ASSERT(reporter, result.size() == 1); |
| 3121 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 331.835f, epsilon)); |
| 3122 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 59.40625f, epsilon)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3123 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 419.189f, epsilon)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3124 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 118, epsilon)); |
| 3125 | } |
| 3126 | { |
| 3127 | auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle); |
| 3128 | REPORTER_ASSERT(reporter, result.empty()); |
| 3129 | } |
| 3130 | } |
| 3131 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3132 | // Checked: DIFF (line height rounding error) |
| 3133 | DEF_TEST(SkParagraph_GetRectsForRangeStrut, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3134 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3135 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3136 | TestCanvas canvas("SkParagraph_GetRectsForRangeStrut.png"); |
| 3137 | const char* text = "Chinese 字典"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3138 | const size_t len = strlen(text); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3139 | |
| 3140 | StrutStyle strutStyle; |
| 3141 | strutStyle.setStrutEnabled(true); |
| 3142 | strutStyle.setFontFamilies({SkString("Roboto")}); |
| 3143 | strutStyle.setFontSize(14.0); |
| 3144 | |
| 3145 | ParagraphStyle paragraphStyle; |
| 3146 | paragraphStyle.setStrutStyle(strutStyle); |
| 3147 | |
| 3148 | TextStyle textStyle; |
| 3149 | textStyle.setFontFamilies({SkString("Noto Sans CJK JP")}); |
| 3150 | textStyle.setFontSize(20); |
| 3151 | textStyle.setColor(SK_ColorBLACK); |
| 3152 | |
| 3153 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 3154 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3155 | builder.addText(text, len); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3156 | builder.pop(); |
| 3157 | |
| 3158 | auto paragraph = builder.Build(); |
| 3159 | paragraph->layout(550); |
| 3160 | paragraph->paint(canvas.get(), 0, 0); |
| 3161 | |
| 3162 | { |
| 3163 | auto result = paragraph->getRectsForRange(0, 10, RectHeightStyle::kTight, RectWidthStyle::kMax); |
| 3164 | canvas.drawRects(SK_ColorGREEN, result); |
| 3165 | REPORTER_ASSERT(reporter, result.size() == 1); |
| 3166 | } |
| 3167 | |
| 3168 | { |
| 3169 | auto result = paragraph->getRectsForRange(0, 10, RectHeightStyle::kStrut, RectWidthStyle::kMax); |
| 3170 | canvas.drawRects(SK_ColorRED, result); |
| 3171 | REPORTER_ASSERT(reporter, result.size() == 1); |
| 3172 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 0, EPSILON100)); |
| 3173 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 10.611f, EPSILON2)); |
| 3174 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 118.605f, EPSILON50)); |
| 3175 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 27.017f, EPSILON2)); |
| 3176 | } |
| 3177 | } |
| 3178 | |
| 3179 | // Checked: NO DIFF |
| 3180 | DEF_TEST(SkParagraph_GetRectsForRangeStrutFallback, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3181 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3182 | if (!fontCollection->fontsFound()) return; |
| 3183 | TestCanvas canvas("SkParagraph_GetRectsForRangeStrutFallback.png"); |
| 3184 | const char* text = "Chinese 字典"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3185 | const size_t len = strlen(text); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3186 | |
| 3187 | StrutStyle strutStyle; |
| 3188 | strutStyle.setStrutEnabled(false); |
| 3189 | |
| 3190 | ParagraphStyle paragraphStyle; |
| 3191 | paragraphStyle.setStrutStyle(strutStyle); |
| 3192 | |
| 3193 | TextStyle textStyle; |
| 3194 | textStyle.setFontFamilies({SkString("Noto Sans CJK JP")}); |
| 3195 | textStyle.setFontSize(20); |
| 3196 | textStyle.setColor(SK_ColorBLACK); |
| 3197 | |
| 3198 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 3199 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3200 | builder.addText(text, len); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3201 | builder.pop(); |
| 3202 | |
| 3203 | auto paragraph = builder.Build(); |
| 3204 | paragraph->layout(550); |
| 3205 | paragraph->paint(canvas.get(), 0, 0); |
| 3206 | |
| 3207 | |
| 3208 | auto result1 = paragraph->getRectsForRange(0, 10, RectHeightStyle::kTight, RectWidthStyle::kMax); |
| 3209 | canvas.drawRects(SK_ColorGREEN, result1); |
| 3210 | REPORTER_ASSERT(reporter, result1.size() == 1); |
| 3211 | |
| 3212 | auto result2 = paragraph->getRectsForRange(0, 10, RectHeightStyle::kStrut, RectWidthStyle::kMax); |
| 3213 | canvas.drawRects(SK_ColorRED, result2); |
| 3214 | REPORTER_ASSERT(reporter, result2.size() == 1); |
| 3215 | |
| 3216 | REPORTER_ASSERT(reporter, result1[0].rect == result2[0].rect); |
| 3217 | } |
| 3218 | |
| 3219 | // Checked: DIFF (small in numbers) |
| 3220 | DEF_TEST(SkParagraph_GetWordBoundaryParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3221 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3222 | if (!fontCollection->fontsFound()) return; |
| 3223 | TestCanvas canvas("SkParagraph_GetWordBoundaryParagraph.png"); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3224 | const char* text = "12345 67890 12345 67890 12345 67890 12345 " |
| 3225 | "67890 12345 67890 12345 67890 12345"; |
| 3226 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3227 | ParagraphStyle paragraphStyle; |
| 3228 | paragraphStyle.setTextAlign(TextAlign::kLeft); |
| 3229 | paragraphStyle.setMaxLines(10); |
| 3230 | paragraphStyle.turnHintingOff(); |
| 3231 | TextStyle textStyle; |
| 3232 | textStyle.setFontFamilies({SkString("Roboto")}); |
| 3233 | textStyle.setFontSize(52); |
| 3234 | textStyle.setLetterSpacing(1.19039f); |
| 3235 | textStyle.setWordSpacing(5); |
| 3236 | textStyle.setHeight(1.5); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3237 | textStyle.setHeightOverride(true); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3238 | textStyle.setColor(SK_ColorBLACK); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3239 | |
| 3240 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 3241 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3242 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3243 | builder.pop(); |
| 3244 | |
| 3245 | auto paragraph = builder.Build(); |
| 3246 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3247 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3248 | |
| 3249 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(0) == SkRange<size_t>(0, 5)); |
| 3250 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(1) == SkRange<size_t>(0, 5)); |
| 3251 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(2) == SkRange<size_t>(0, 5)); |
| 3252 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(3) == SkRange<size_t>(0, 5)); |
| 3253 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(4) == SkRange<size_t>(0, 5)); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3254 | auto boxes = paragraph->getRectsForRange(5, 6, RectHeightStyle::kMax, RectWidthStyle::kTight); |
| 3255 | canvas.drawLines(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3256 | |
| 3257 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(5) == SkRange<size_t>(5, 7)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3258 | boxes = paragraph->getRectsForRange(6, 7, RectHeightStyle::kMax, RectWidthStyle::kTight); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3259 | canvas.drawLines(SK_ColorRED, boxes); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3260 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3261 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(6) == SkRange<size_t>(5, 7)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3262 | boxes = paragraph->getRectsForRange(7, 8, RectHeightStyle::kMax, RectWidthStyle::kTight); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3263 | canvas.drawLines(SK_ColorRED, boxes); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3264 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3265 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(7) == SkRange<size_t>(7, 12)); |
| 3266 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(8) == SkRange<size_t>(7, 12)); |
| 3267 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(9) == SkRange<size_t>(7, 12)); |
| 3268 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(10) == SkRange<size_t>(7, 12)); |
| 3269 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(11) == SkRange<size_t>(7, 12)); |
| 3270 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(12) == SkRange<size_t>(12, 13)); |
| 3271 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(13) == SkRange<size_t>(13, 18)); |
| 3272 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(30) == SkRange<size_t>(30, 31)); |
| 3273 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3274 | boxes = paragraph->getRectsForRange(12, 13, RectHeightStyle::kMax, RectWidthStyle::kTight); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3275 | canvas.drawLines(SK_ColorRED, boxes); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3276 | boxes = paragraph->getRectsForRange(13, 14, RectHeightStyle::kMax, RectWidthStyle::kTight); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3277 | canvas.drawLines(SK_ColorRED, boxes); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3278 | boxes = paragraph->getRectsForRange(18, 19, RectHeightStyle::kMax, RectWidthStyle::kTight); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3279 | canvas.drawLines(SK_ColorRED, boxes); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3280 | boxes = paragraph->getRectsForRange(19, 20, RectHeightStyle::kMax, RectWidthStyle::kTight); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3281 | canvas.drawLines(SK_ColorRED, boxes); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3282 | boxes = paragraph->getRectsForRange(24, 25, RectHeightStyle::kMax, RectWidthStyle::kTight); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3283 | canvas.drawLines(SK_ColorRED, boxes); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3284 | boxes = paragraph->getRectsForRange(25, 26, RectHeightStyle::kMax, RectWidthStyle::kTight); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3285 | canvas.drawLines(SK_ColorRED, boxes); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3286 | boxes = paragraph->getRectsForRange(30, 31, RectHeightStyle::kMax, RectWidthStyle::kTight); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3287 | canvas.drawLines(SK_ColorRED, boxes); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3288 | boxes = paragraph->getRectsForRange(31, 32, RectHeightStyle::kMax, RectWidthStyle::kTight); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3289 | canvas.drawLines(SK_ColorRED, boxes); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3290 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3291 | auto outLen = static_cast<ParagraphImpl*>(paragraph.get())->text().size(); |
| 3292 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(outLen - 1) == SkRange<size_t>(outLen - 5, outLen)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3293 | } |
| 3294 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3295 | // Checked: DIFF (unclear) |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3296 | DEF_TEST(SkParagraph_SpacingParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3297 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3298 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3299 | TestCanvas canvas("SkParagraph_SpacingParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3300 | ParagraphStyle paragraph_style; |
| 3301 | paragraph_style.setMaxLines(10); |
| 3302 | paragraph_style.setTextAlign(TextAlign::kLeft); |
| 3303 | paragraph_style.turnHintingOff(); |
| 3304 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3305 | |
| 3306 | TextStyle text_style; |
| 3307 | text_style.setFontFamilies({SkString("Roboto")}); |
| 3308 | text_style.setFontSize(50); |
| 3309 | text_style.setLetterSpacing(20); |
| 3310 | text_style.setWordSpacing(0); |
| 3311 | text_style.setColor(SK_ColorBLACK); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3312 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3313 | builder.addText("H", 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3314 | builder.pop(); |
| 3315 | |
| 3316 | text_style.setLetterSpacing(10); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3317 | text_style.setWordSpacing(0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3318 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3319 | builder.addText("H", 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3320 | builder.pop(); |
| 3321 | |
| 3322 | text_style.setLetterSpacing(20); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3323 | text_style.setWordSpacing(0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3324 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3325 | builder.addText("H", 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3326 | builder.pop(); |
| 3327 | |
| 3328 | text_style.setLetterSpacing(0); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3329 | text_style.setWordSpacing(0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3330 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3331 | builder.addText("|", 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3332 | builder.pop(); |
| 3333 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3334 | const char* hSpace = "H "; |
| 3335 | const size_t len = strlen(hSpace); |
| 3336 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3337 | text_style.setLetterSpacing(0); |
| 3338 | text_style.setWordSpacing(20); |
| 3339 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3340 | builder.addText(hSpace, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3341 | builder.pop(); |
| 3342 | |
| 3343 | text_style.setLetterSpacing(0); |
| 3344 | text_style.setWordSpacing(0); |
| 3345 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3346 | builder.addText(hSpace, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3347 | builder.pop(); |
| 3348 | |
| 3349 | text_style.setLetterSpacing(0); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3350 | text_style.setLetterSpacing(0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3351 | text_style.setWordSpacing(20); |
| 3352 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3353 | builder.addText(hSpace, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3354 | builder.pop(); |
| 3355 | |
| 3356 | auto paragraph = builder.Build(); |
| 3357 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3358 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3359 | |
| 3360 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 3361 | REPORTER_ASSERT(reporter, impl->lines().size() == 1); |
| 3362 | size_t index = 0; |
| 3363 | impl->lines().begin()->scanStyles(StyleType::kLetterSpacing, |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 3364 | [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
| 3365 | ++index; |
| 3366 | return true; |
| 3367 | }); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3368 | REPORTER_ASSERT(reporter, index == 4); |
| 3369 | index = 0; |
| 3370 | impl->lines().begin()->scanStyles(StyleType::kWordSpacing, |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 3371 | [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
| 3372 | ++index; |
| 3373 | return true; |
| 3374 | }); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3375 | REPORTER_ASSERT(reporter, index == 4); |
| 3376 | } |
| 3377 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3378 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3379 | DEF_TEST(SkParagraph_LongWordParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3380 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3381 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3382 | TestCanvas canvas("SkParagraph_LongWordParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3383 | const char* text = |
| 3384 | "A " |
| 3385 | "veryverylongwordtoseewherethiswillwraporifitwillatallandifitdoesthenthat" |
| 3386 | "wouldbeagoodthingbecausethebreakingisworking."; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3387 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3388 | |
| 3389 | ParagraphStyle paragraph_style; |
| 3390 | paragraph_style.turnHintingOff(); |
| 3391 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3392 | |
| 3393 | TextStyle text_style; |
| 3394 | text_style.setFontFamilies({SkString("Roboto")}); |
| 3395 | text_style.setColor(SK_ColorRED); |
| 3396 | text_style.setFontSize(31); |
| 3397 | text_style.setLetterSpacing(0); |
| 3398 | text_style.setWordSpacing(0); |
| 3399 | text_style.setColor(SK_ColorBLACK); |
| 3400 | text_style.setHeight(1); |
| 3401 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3402 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3403 | builder.pop(); |
| 3404 | |
| 3405 | auto paragraph = builder.Build(); |
| 3406 | paragraph->layout(TestCanvasWidth / 2); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3407 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3408 | |
| 3409 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 3410 | REPORTER_ASSERT(reporter, impl->text().size() == std::string{text}.length()); |
| 3411 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 3412 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 3413 | REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3414 | REPORTER_ASSERT(reporter, impl->lines().size() == 4); |
| 3415 | |
| 3416 | REPORTER_ASSERT(reporter, impl->lines()[0].width() > TestCanvasWidth / 2 - 20); |
| 3417 | REPORTER_ASSERT(reporter, impl->lines()[1].width() > TestCanvasWidth / 2 - 20); |
| 3418 | REPORTER_ASSERT(reporter, impl->lines()[2].width() > TestCanvasWidth / 2 - 20); |
| 3419 | } |
| 3420 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3421 | // Checked: DIFF? |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3422 | DEF_TEST(SkParagraph_KernScaleParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3423 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3424 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3425 | TestCanvas canvas("SkParagraph_KernScaleParagraph.png"); |
| 3426 | |
| 3427 | const char* text1 = "AVAVAWAH A0 V0 VA To The Lo"; |
| 3428 | const char* text2 = " Dialog Text List lots of words to see " |
| 3429 | "if kerning works on a bigger set of characters AVAVAW"; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3430 | float scale = 3.0f; |
| 3431 | ParagraphStyle paragraph_style; |
| 3432 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3433 | TextStyle text_style; |
| 3434 | text_style.setFontFamilies({SkString("Droid Serif")}); |
| 3435 | text_style.setFontSize(100 / scale); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3436 | text_style.setColor(SK_ColorBLACK); |
| 3437 | |
| 3438 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3439 | builder.addText(text1, strlen(text1)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3440 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3441 | builder.addText("A", 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3442 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3443 | builder.addText("V", 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3444 | text_style.setFontSize(14 / scale); |
| 3445 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3446 | builder.addText(text2, strlen(text2)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3447 | builder.pop(); |
| 3448 | |
| 3449 | auto paragraph = builder.Build(); |
| 3450 | paragraph->layout(TestCanvasWidth / scale); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3451 | canvas.get()->scale(scale, scale); |
| 3452 | paragraph->paint(canvas.get(), 0, 0); |
| 3453 | canvas.get()->scale(1, 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3454 | |
| 3455 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3456 | |
Julia Lavrova | 6e6333f | 2019-06-17 10:34:10 -0400 | [diff] [blame] | 3457 | // First and second lines must have the same width, the third one must be bigger |
Julia Lavrova | 6e6333f | 2019-06-17 10:34:10 -0400 | [diff] [blame] | 3458 | REPORTER_ASSERT(reporter, impl->lines().size() == 3); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3459 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].width(), 285.858f, EPSILON100)); |
| 3460 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[1].width(), 329.709f, EPSILON100)); |
| 3461 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[2].width(), 120.619f, EPSILON100)); |
| 3462 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].height(), 39.00f, EPSILON100)); |
| 3463 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[1].height(), 39.00f, EPSILON100)); |
| 3464 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[2].height(), 05.00f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3465 | } |
| 3466 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3467 | // Checked: DIFF+ |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3468 | DEF_TEST(SkParagraph_NewlineParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3469 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3470 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3471 | TestCanvas canvas("SkParagraph_NewlineParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3472 | const char* text = |
| 3473 | "line1\nline2 test1 test2 test3 test4 test5 test6 test7\nline3\n\nline4 " |
| 3474 | "test1 test2 test3 test4"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3475 | const size_t len = strlen(text); |
| 3476 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3477 | ParagraphStyle paragraph_style; |
| 3478 | paragraph_style.turnHintingOff(); |
| 3479 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3480 | |
| 3481 | TextStyle text_style; |
| 3482 | text_style.setFontFamilies({SkString("Roboto")}); |
| 3483 | text_style.setColor(SK_ColorRED); |
| 3484 | text_style.setFontSize(60); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3485 | text_style.setColor(SK_ColorBLACK); |
| 3486 | text_style.setHeight(1); |
| 3487 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3488 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3489 | builder.pop(); |
| 3490 | |
| 3491 | auto paragraph = builder.Build(); |
| 3492 | paragraph->layout(TestCanvasWidth - 300); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3493 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3494 | |
| 3495 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 3496 | // Minikin does not count empty lines but SkParagraph does |
| 3497 | REPORTER_ASSERT(reporter, impl->lines().size() == 7); |
| 3498 | |
| 3499 | REPORTER_ASSERT(reporter, impl->lines()[0].offset().fY == 0); |
| 3500 | REPORTER_ASSERT(reporter, impl->lines()[1].offset().fY == 70); |
| 3501 | REPORTER_ASSERT(reporter, impl->lines()[2].offset().fY == 140); |
| 3502 | REPORTER_ASSERT(reporter, impl->lines()[3].offset().fY == 210); |
| 3503 | REPORTER_ASSERT(reporter, impl->lines()[4].offset().fY == 280); // Empty line |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 3504 | REPORTER_ASSERT(reporter, impl->lines()[5].offset().fY == 296); |
| 3505 | REPORTER_ASSERT(reporter, impl->lines()[6].offset().fY == 366); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3506 | } |
| 3507 | |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 3508 | // TODO: Fix underline |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3509 | DEF_TEST(SkParagraph_EmojiParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3510 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3511 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3512 | TestCanvas canvas("SkParagraph_EmojiParagraph.png"); |
| 3513 | const char* text = |
| 3514 | "😀😃😄😁😆😅😂🤣☺😇🙂😍😡😟😢😻👽💩👍👎🙏👌👋👄👁👦👼👨🚀👨🚒🙋♂️👳👨👨👧👧\ |
| 3515 | 💼👡👠☂🐶🐰🐻🐼🐷🐒🐵🐔🐧🐦🐋🐟🐡🕸🐌🐴🐊🐄🐪🐘🌸🌏🔥🌟🌚🌝💦💧\ |
| 3516 | ❄🍕🍔🍟🥝🍱🕶🎩🏈⚽🚴♀️🎻🎼🎹🚨🚎🚐⚓🛳🚀🚁🏪🏢🖱⏰📱💾💉📉🛏🔑🔓\ |
| 3517 | 📁🗓📊❤💯🚫🔻♠♣🕓❗🏳🏁🏳️🌈🇮🇹🇱🇷🇺🇸🇬🇧🇨🇳🇧🇴"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3518 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3519 | |
| 3520 | ParagraphStyle paragraph_style; |
| 3521 | paragraph_style.turnHintingOff(); |
| 3522 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3523 | |
| 3524 | TextStyle text_style; |
| 3525 | text_style.setFontFamilies({SkString("Noto Color Emoji")}); |
| 3526 | text_style.setFontSize(50); |
| 3527 | text_style.setDecoration(TextDecoration::kUnderline); |
| 3528 | text_style.setColor(SK_ColorBLACK); |
| 3529 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3530 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3531 | builder.pop(); |
| 3532 | |
| 3533 | auto paragraph = builder.Build(); |
| 3534 | paragraph->layout(TestCanvasWidth); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3535 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3536 | |
Julia Lavrova | 3281b96 | 2019-12-02 11:32:25 -0500 | [diff] [blame] | 3537 | REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == 0); |
| 3538 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3539 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3540 | |
| 3541 | REPORTER_ASSERT(reporter, impl->lines().size() == 8); |
| 3542 | for (auto& line : impl->lines()) { |
| 3543 | if (&line != impl->lines().end() - 1) { |
| 3544 | REPORTER_ASSERT(reporter, line.width() == 998.25f); |
| 3545 | } else { |
| 3546 | REPORTER_ASSERT(reporter, line.width() < 998.25f); |
| 3547 | } |
| 3548 | REPORTER_ASSERT(reporter, line.height() == 59); |
| 3549 | } |
| 3550 | } |
| 3551 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3552 | // Checked: DIFF+ |
| 3553 | DEF_TEST(SkParagraph_EmojiMultiLineRectsParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3554 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3555 | if (!fontCollection->fontsFound()) return; |
| 3556 | TestCanvas canvas("SkParagraph_EmojiMultiLineRectsParagraph.png"); |
| 3557 | const char* text = |
| 3558 | "👩👩👦👩👩👧👧🇺🇸👩👩👦👩👩👧👧i🇺🇸👩👩👦👩👩👧👧🇺🇸👩👩👦👩👩👧👧🇺🇸" |
| 3559 | "👩👩👦👩👩👧👧🇺🇸👩👩👦👩👩👧👧🇺🇸👩👩👦👩👩👧👧🇺🇸👩👩👦👩👩👧👧🇺🇸" |
| 3560 | "👩👩👦👩👩👧👧🇺🇸👩👩👦👩👩👧👧🇺🇸👩👩👦👩👩👧👧🇺🇸👩👩👦👩👩👧👧🇺🇸" |
| 3561 | "👩👩👦👩👩👧👧🇺🇸👩👩👦👩👩👧👧🇺🇸👩👩👦👩👩👧👧🇺🇸👩👩👦👩👩👧👧🇺🇸" |
| 3562 | "❄🍕🍔🍟🥝🍱🕶🎩🏈⚽🚴♀️🎻🎼🎹🚨🚎🚐⚓🛳🚀🚁🏪🏢🖱⏰📱💾💉📉🛏🔑🔓" |
| 3563 | "📁🗓📊❤💯🚫🔻♠♣🕓❗🏳🏁🏳️🌈🇮🇹🇱🇷🇺🇸🇬🇧🇨🇳🇧🇴"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3564 | const size_t len = strlen(text); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3565 | |
| 3566 | ParagraphStyle paragraph_style; |
| 3567 | paragraph_style.turnHintingOff(); |
| 3568 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3569 | |
| 3570 | TextStyle text_style; |
| 3571 | text_style.setFontFamilies({SkString("Noto Color Emoji")}); |
| 3572 | text_style.setFontSize(50); |
| 3573 | text_style.setColor(SK_ColorBLACK); |
| 3574 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3575 | builder.addText(text, len); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3576 | builder.pop(); |
| 3577 | |
| 3578 | auto paragraph = builder.Build(); |
| 3579 | paragraph->layout(TestCanvasWidth - 300); |
| 3580 | paragraph->paint(canvas.get(), 0, 0); |
| 3581 | |
| 3582 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 3583 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 3584 | |
| 3585 | auto result = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style); |
| 3586 | REPORTER_ASSERT(reporter, result.size() == 0); |
| 3587 | |
| 3588 | result = paragraph->getRectsForRange(0, 119, rect_height_style, rect_width_style); |
| 3589 | REPORTER_ASSERT(reporter, result.size() == 2); |
| 3590 | canvas.drawRects(SK_ColorRED, result); |
| 3591 | |
| 3592 | result = paragraph->getRectsForRange(122, 132, rect_height_style, rect_width_style); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 3593 | REPORTER_ASSERT(reporter, result.size() == 0); // There is no single glyph |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3594 | canvas.drawRects(SK_ColorBLUE, result); |
| 3595 | |
| 3596 | auto pos = paragraph->getGlyphPositionAtCoordinate(610, 100).position; |
| 3597 | result = paragraph->getRectsForRange(0, pos, rect_height_style, rect_width_style); |
| 3598 | REPORTER_ASSERT(reporter, result.size() == 2); |
| 3599 | canvas.drawRects(SK_ColorGREEN, result); |
| 3600 | |
| 3601 | pos = paragraph->getGlyphPositionAtCoordinate(580, 100).position; |
| 3602 | result = paragraph->getRectsForRange(0, pos, rect_height_style, rect_width_style); |
| 3603 | REPORTER_ASSERT(reporter, result.size() == 2); |
| 3604 | canvas.drawRects(SK_ColorGREEN, result); |
| 3605 | |
| 3606 | pos = paragraph->getGlyphPositionAtCoordinate(560, 100).position; |
| 3607 | result = paragraph->getRectsForRange(0, pos, rect_height_style, rect_width_style); |
| 3608 | REPORTER_ASSERT(reporter, result.size() == 2); |
| 3609 | canvas.drawRects(SK_ColorGREEN, result); |
| 3610 | } |
| 3611 | |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3612 | DEF_TEST(SkParagraph_HyphenBreakParagraph, reporter) { |
| 3613 | SkDebugf("Hyphens are not implemented, and will not be implemented soon.\n"); |
| 3614 | } |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3615 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3616 | // Checked: DIFF (line breaking) |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3617 | DEF_TEST(SkParagraph_RepeatLayoutParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3618 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3619 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3620 | TestCanvas canvas("SkParagraph_RepeatLayoutParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3621 | const char* text = |
| 3622 | "Sentence to layout at diff widths to get diff line counts. short words " |
| 3623 | "short words short words short words short words short words short words " |
| 3624 | "short words short words short words short words short words short words " |
| 3625 | "end"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3626 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3627 | |
| 3628 | ParagraphStyle paragraph_style; |
| 3629 | paragraph_style.turnHintingOff(); |
| 3630 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3631 | |
| 3632 | TextStyle text_style; |
| 3633 | text_style.setFontFamilies({SkString("Roboto")}); |
| 3634 | text_style.setFontSize(31); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3635 | text_style.setColor(SK_ColorBLACK); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3636 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3637 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3638 | builder.pop(); |
| 3639 | |
| 3640 | auto paragraph = builder.Build(); |
| 3641 | paragraph->layout(300); |
| 3642 | |
| 3643 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 3644 | // Some of the formatting lazily done on paint |
| 3645 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 3646 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
| 3647 | REPORTER_ASSERT(reporter, impl->lines().size() == 12); |
| 3648 | |
| 3649 | paragraph->layout(600); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3650 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3651 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 3652 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
| 3653 | REPORTER_ASSERT(reporter, impl->lines().size() == 6); |
| 3654 | } |
| 3655 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3656 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3657 | DEF_TEST(SkParagraph_Ellipsize, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3658 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3659 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3660 | TestCanvas canvas("SkParagraph_Ellipsize.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3661 | const char* text = |
| 3662 | "This is a very long sentence to test if the text will properly wrap " |
| 3663 | "around and go to the next line. Sometimes, short sentence. Longer " |
| 3664 | "sentences are okay too because they are nessecary. Very short. "; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3665 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3666 | |
| 3667 | ParagraphStyle paragraph_style; |
| 3668 | paragraph_style.setMaxLines(1); |
| 3669 | paragraph_style.setEllipsis(u"\u2026"); |
| 3670 | paragraph_style.turnHintingOff(); |
| 3671 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3672 | |
| 3673 | TextStyle text_style; |
| 3674 | text_style.setFontFamilies({SkString("Roboto")}); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3675 | text_style.setColor(SK_ColorBLACK); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3676 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3677 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3678 | builder.pop(); |
| 3679 | |
| 3680 | auto paragraph = builder.Build(); |
| 3681 | paragraph->layout(TestCanvasWidth); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3682 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3683 | |
| 3684 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3685 | |
| 3686 | // Check that the ellipsizer limited the text to one line and did not wrap to a second line. |
| 3687 | REPORTER_ASSERT(reporter, impl->lines().size() == 1); |
| 3688 | |
| 3689 | auto& line = impl->lines()[0]; |
| 3690 | REPORTER_ASSERT(reporter, line.ellipsis() != nullptr); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 3691 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3692 | } |
| 3693 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3694 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3695 | DEF_TEST(SkParagraph_UnderlineShiftParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3696 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3697 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3698 | TestCanvas canvas("SkParagraph_UnderlineShiftParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3699 | const char* text1 = "fluttser "; |
| 3700 | const char* text2 = "mdje"; |
| 3701 | const char* text3 = "fluttser mdje"; |
| 3702 | |
| 3703 | ParagraphStyle paragraph_style; |
| 3704 | paragraph_style.turnHintingOff(); |
| 3705 | paragraph_style.setTextAlign(TextAlign::kLeft); |
| 3706 | paragraph_style.setMaxLines(2); |
| 3707 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3708 | |
| 3709 | TextStyle text_style; |
| 3710 | text_style.setFontFamilies({SkString("Roboto")}); |
| 3711 | text_style.setColor(SK_ColorBLACK); |
| 3712 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3713 | builder.addText(text1, strlen(text1)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3714 | text_style.setDecoration(TextDecoration::kUnderline); |
| 3715 | text_style.setDecorationColor(SK_ColorBLACK); |
| 3716 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3717 | builder.addText(text2, strlen(text2)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3718 | builder.pop(); |
| 3719 | |
| 3720 | auto paragraph = builder.Build(); |
| 3721 | paragraph->layout(TestCanvasWidth); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3722 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3723 | |
| 3724 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 3725 | |
| 3726 | ParagraphBuilderImpl builder1(paragraph_style, fontCollection); |
| 3727 | text_style.setDecoration(TextDecoration::kNoDecoration); |
| 3728 | builder1.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3729 | builder1.addText(text3, strlen(text3)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3730 | builder1.pop(); |
| 3731 | |
| 3732 | auto paragraph1 = builder1.Build(); |
| 3733 | paragraph1->layout(TestCanvasWidth); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3734 | paragraph1->paint(canvas.get(), 0, 25); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3735 | |
| 3736 | auto impl1 = static_cast<ParagraphImpl*>(paragraph1.get()); |
| 3737 | |
| 3738 | REPORTER_ASSERT(reporter, impl->lines().size() == 1); |
| 3739 | REPORTER_ASSERT(reporter, impl1->lines().size() == 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3740 | |
| 3741 | auto rect = paragraph->getRectsForRange(0, 12, RectHeightStyle::kMax, RectWidthStyle::kTight) |
| 3742 | .front() |
| 3743 | .rect; |
| 3744 | auto rect1 = paragraph1->getRectsForRange(0, 12, RectHeightStyle::kMax, RectWidthStyle::kTight) |
| 3745 | .front() |
| 3746 | .rect; |
| 3747 | REPORTER_ASSERT(reporter, rect.fLeft == rect1.fLeft); |
| 3748 | REPORTER_ASSERT(reporter, rect.fRight == rect1.fRight); |
| 3749 | |
| 3750 | for (size_t i = 0; i < 12; ++i) { |
| 3751 | auto r = |
| 3752 | paragraph->getRectsForRange(i, i + 1, RectHeightStyle::kMax, RectWidthStyle::kTight) |
| 3753 | .front() |
| 3754 | .rect; |
| 3755 | auto r1 = |
| 3756 | paragraph1 |
| 3757 | ->getRectsForRange(i, i + 1, RectHeightStyle::kMax, RectWidthStyle::kTight) |
| 3758 | .front() |
| 3759 | .rect; |
| 3760 | |
| 3761 | REPORTER_ASSERT(reporter, r.fLeft == r1.fLeft); |
| 3762 | REPORTER_ASSERT(reporter, r.fRight == r1.fRight); |
| 3763 | } |
| 3764 | } |
| 3765 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3766 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3767 | DEF_TEST(SkParagraph_SimpleShadow, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3768 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3769 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3770 | TestCanvas canvas("SkParagraph_SimpleShadow.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3771 | const char* text = "Hello World Text Dialog"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3772 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3773 | |
| 3774 | ParagraphStyle paragraph_style; |
| 3775 | paragraph_style.turnHintingOff(); |
| 3776 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3777 | |
| 3778 | TextStyle text_style; |
| 3779 | text_style.setFontFamilies({SkString("Roboto")}); |
| 3780 | text_style.setColor(SK_ColorBLACK); |
| 3781 | text_style.addShadow(TextShadow(SK_ColorBLACK, SkPoint::Make(2.0f, 2.0f), 1.0)); |
| 3782 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3783 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3784 | |
| 3785 | auto paragraph = builder.Build(); |
| 3786 | paragraph->layout(TestCanvasWidth); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3787 | paragraph->paint(canvas.get(), 10.0, 15.0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3788 | |
| 3789 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 3790 | |
| 3791 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 3792 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
| 3793 | size_t index = 0; |
| 3794 | for (auto& line : impl->lines()) { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3795 | line.scanStyles(StyleType::kShadow, |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 3796 | [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3797 | REPORTER_ASSERT(reporter, index == 0 && style.equals(text_style)); |
| 3798 | ++index; |
| 3799 | return true; |
| 3800 | }); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3801 | } |
| 3802 | } |
| 3803 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3804 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3805 | DEF_TEST(SkParagraph_ComplexShadow, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3806 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3807 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3808 | TestCanvas canvas("SkParagraph_ComplexShadow.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3809 | const char* text = "Text Chunk "; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3810 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3811 | |
| 3812 | ParagraphStyle paragraph_style; |
| 3813 | paragraph_style.turnHintingOff(); |
| 3814 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3815 | |
| 3816 | TextStyle text_style; |
| 3817 | text_style.setFontFamilies({SkString("Roboto")}); |
| 3818 | text_style.setColor(SK_ColorBLACK); |
| 3819 | text_style.addShadow(TextShadow(SK_ColorBLACK, SkPoint::Make(2.0f, 2.0f), 1.0f)); |
| 3820 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3821 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3822 | |
| 3823 | text_style.addShadow(TextShadow(SK_ColorRED, SkPoint::Make(2.0f, 2.0f), 5.0f)); |
| 3824 | text_style.addShadow(TextShadow(SK_ColorGREEN, SkPoint::Make(10.0f, -5.0f), 3.0f)); |
| 3825 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3826 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3827 | builder.pop(); |
| 3828 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3829 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3830 | |
| 3831 | text_style.addShadow(TextShadow(SK_ColorRED, SkPoint::Make(0.0f, 1.0f), 0.0f)); |
| 3832 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3833 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3834 | builder.pop(); |
| 3835 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3836 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3837 | |
| 3838 | auto paragraph = builder.Build(); |
| 3839 | paragraph->layout(TestCanvasWidth); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3840 | paragraph->paint(canvas.get(), 10.0, 15.0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3841 | |
| 3842 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 3843 | |
| 3844 | size_t index = 0; |
| 3845 | for (auto& line : impl->lines()) { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3846 | line.scanStyles(StyleType::kShadow, |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 3847 | [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3848 | ++index; |
| 3849 | switch (index) { |
| 3850 | case 1: |
| 3851 | REPORTER_ASSERT(reporter, style.getShadowNumber() == 1); |
| 3852 | break; |
| 3853 | case 2: |
| 3854 | REPORTER_ASSERT(reporter, style.getShadowNumber() == 3); |
| 3855 | break; |
| 3856 | case 3: |
| 3857 | REPORTER_ASSERT(reporter, style.getShadowNumber() == 1); |
| 3858 | break; |
| 3859 | case 4: |
| 3860 | REPORTER_ASSERT(reporter, style.getShadowNumber() == 4); |
| 3861 | REPORTER_ASSERT(reporter, style.equals(text_style)); |
| 3862 | break; |
| 3863 | case 5: |
| 3864 | REPORTER_ASSERT(reporter, style.getShadowNumber() == 1); |
| 3865 | break; |
| 3866 | default: |
| 3867 | REPORTER_ASSERT(reporter, false); |
| 3868 | } |
| 3869 | return true; |
| 3870 | }); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3871 | } |
| 3872 | } |
| 3873 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3874 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3875 | DEF_TEST(SkParagraph_BaselineParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3876 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3877 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3878 | TestCanvas canvas("SkParagraph_BaselineParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3879 | const char* text = |
| 3880 | "左線読設Byg後碁給能上目秘使約。満毎冠行来昼本可必図将発確年。今属場育" |
| 3881 | "図情闘陰野高備込制詩西校客。審対江置講今固残必託地集済決維駆年策。立得"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3882 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3883 | |
| 3884 | ParagraphStyle paragraph_style; |
| 3885 | paragraph_style.turnHintingOff(); |
| 3886 | paragraph_style.setMaxLines(14); |
| 3887 | paragraph_style.setTextAlign(TextAlign::kJustify); |
| 3888 | paragraph_style.setHeight(1.5); |
| 3889 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3890 | |
| 3891 | TextStyle text_style; |
| 3892 | text_style.setFontFamilies({SkString("Source Han Serif CN")}); |
| 3893 | text_style.setColor(SK_ColorBLACK); |
| 3894 | text_style.setFontSize(55); |
| 3895 | text_style.setLetterSpacing(2); |
| 3896 | text_style.setDecorationStyle(TextDecorationStyle::kSolid); |
| 3897 | text_style.setDecorationColor(SK_ColorBLACK); |
| 3898 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3899 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3900 | builder.pop(); |
| 3901 | |
| 3902 | auto paragraph = builder.Build(); |
| 3903 | paragraph->layout(TestCanvasWidth - 100); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3904 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3905 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3906 | SkRect rect1 = SkRect::MakeXYWH(0, paragraph->getIdeographicBaseline(), |
| 3907 | paragraph->getMaxWidth(), |
| 3908 | paragraph->getIdeographicBaseline()); |
| 3909 | SkRect rect2 = SkRect::MakeXYWH(0, paragraph->getAlphabeticBaseline(), |
| 3910 | paragraph->getMaxWidth(), |
| 3911 | paragraph->getAlphabeticBaseline()); |
| 3912 | canvas.drawLine(SK_ColorRED, rect1, false); |
| 3913 | canvas.drawLine(SK_ColorGREEN, rect2, false); |
| 3914 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3915 | REPORTER_ASSERT(reporter, |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3916 | SkScalarNearlyEqual(paragraph->getIdeographicBaseline(), 79.035f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3917 | REPORTER_ASSERT(reporter, |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3918 | SkScalarNearlyEqual(paragraph->getAlphabeticBaseline(), 63.305f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3919 | } |
| 3920 | |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3921 | // Checked: NO DIFF (number of runs only) |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3922 | DEF_TEST(SkParagraph_FontFallbackParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3923 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3924 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3925 | TestCanvas canvas("SkParagraph_FontFallbackParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3926 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3927 | const char* text1 = "Roboto 字典 "; // Roboto + unresolved |
| 3928 | const char* text2 = "Homemade Apple 字典"; // Homemade Apple + Noto Sans... |
| 3929 | const char* text3 = "Chinese 字典"; // Homemade Apple + Source Han |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3930 | |
| 3931 | ParagraphStyle paragraph_style; |
| 3932 | paragraph_style.turnHintingOff(); |
| 3933 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3934 | |
| 3935 | TextStyle text_style; |
| 3936 | text_style.setFontFamilies({ |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3937 | SkString("Not a real font"), |
| 3938 | SkString("Also a fake font"), |
| 3939 | SkString("So fake it is obvious"), |
| 3940 | SkString("Next one should be a real font..."), |
| 3941 | SkString("Roboto"), |
| 3942 | SkString("another fake one in between"), |
| 3943 | SkString("Homemade Apple"), |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3944 | }); |
| 3945 | text_style.setColor(SK_ColorBLACK); |
| 3946 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3947 | builder.addText(text1, strlen(text1)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3948 | |
| 3949 | text_style.setFontFamilies({ |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3950 | SkString("Not a real font"), |
| 3951 | SkString("Also a fake font"), |
| 3952 | SkString("So fake it is obvious"), |
| 3953 | SkString("Homemade Apple"), |
| 3954 | SkString("Next one should be a real font..."), |
| 3955 | SkString("Roboto"), |
| 3956 | SkString("another fake one in between"), |
| 3957 | SkString("Noto Sans CJK JP"), |
| 3958 | SkString("Source Han Serif CN"), |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3959 | }); |
| 3960 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3961 | builder.addText(text2, strlen(text2)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3962 | |
| 3963 | text_style.setFontFamilies({ |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3964 | SkString("Not a real font"), |
| 3965 | SkString("Also a fake font"), |
| 3966 | SkString("So fake it is obvious"), |
| 3967 | SkString("Homemade Apple"), |
| 3968 | SkString("Next one should be a real font..."), |
| 3969 | SkString("Roboto"), |
| 3970 | SkString("another fake one in between"), |
| 3971 | SkString("Source Han Serif CN"), |
| 3972 | SkString("Noto Sans CJK JP"), |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3973 | }); |
| 3974 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3975 | builder.addText(text3, strlen(text3)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3976 | |
| 3977 | builder.pop(); |
| 3978 | |
| 3979 | auto paragraph = builder.Build(); |
Julia Lavrova | 3281b96 | 2019-12-02 11:32:25 -0500 | [diff] [blame] | 3980 | REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == -1); // Not shaped yet |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3981 | paragraph->layout(TestCanvasWidth); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3982 | paragraph->paint(canvas.get(), 10.0, 15.0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3983 | |
Julia Lavrova | 3281b96 | 2019-12-02 11:32:25 -0500 | [diff] [blame] | 3984 | REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == 2); // From the text1 |
| 3985 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3986 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 3987 | |
| 3988 | // Font resolution in Skia produces 6 runs because 2 parts of "Roboto 字典 " have different |
| 3989 | // script (Minikin merges the first 2 into one because of unresolved) [Apple + Unresolved ] |
| 3990 | // [Apple + Noto] [Apple + Han] |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3991 | REPORTER_ASSERT(reporter, impl->runs().size() == 7); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3992 | |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3993 | auto robotoAdvance = impl->runs()[0].advance().fX + |
| 3994 | impl->runs()[1].advance().fX + |
| 3995 | impl->runs()[2].advance().fX; |
| 3996 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(robotoAdvance, 64.199f, EPSILON50)); |
| 3997 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->runs()[3].advance().fX, 139.125f, EPSILON100)); |
| 3998 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->runs()[4].advance().fX, 27.999f, EPSILON100)); |
| 3999 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->runs()[5].advance().fX, 62.248f, EPSILON100)); |
| 4000 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->runs()[6].advance().fX, 27.999f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4001 | |
| 4002 | // When a different font is resolved, then the metrics are different. |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4003 | REPORTER_ASSERT(reporter, impl->runs()[4].correctAscent() != impl->runs()[6].correctAscent()); |
| 4004 | REPORTER_ASSERT(reporter, impl->runs()[4].correctDescent() != impl->runs()[6].correctDescent()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4005 | } |
| 4006 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4007 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4008 | DEF_TEST(SkParagraph_StrutParagraph1, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4009 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4010 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4011 | TestCanvas canvas("SkParagraph_StrutParagraph1.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4012 | // The chinese extra height should be absorbed by the strut. |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4013 | const char* text = "01234満毎冠p来É本可\nabcd\n満毎É行p昼本可"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4014 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4015 | |
| 4016 | ParagraphStyle paragraph_style; |
| 4017 | paragraph_style.setMaxLines(10); |
| 4018 | paragraph_style.setTextAlign(TextAlign::kLeft); |
| 4019 | paragraph_style.turnHintingOff(); |
| 4020 | |
| 4021 | StrutStyle strut_style; |
| 4022 | strut_style.setStrutEnabled(true); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4023 | strut_style.setFontFamilies({SkString("BlahFake"), SkString("Ahem")}); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4024 | strut_style.setFontSize(50); |
| 4025 | strut_style.setHeight(1.8f); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4026 | strut_style.setHeightOverride(true); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4027 | strut_style.setLeading(0.1f); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4028 | paragraph_style.setStrutStyle(strut_style); |
| 4029 | |
| 4030 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4031 | |
| 4032 | TextStyle text_style; |
| 4033 | text_style.setFontFamilies({SkString("Ahem")}); |
| 4034 | text_style.setFontSize(50); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4035 | text_style.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, SkFontStyle::kUpright_Slant)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4036 | text_style.setColor(SK_ColorBLACK); |
| 4037 | text_style.setHeight(0.5f); |
| 4038 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4039 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4040 | builder.pop(); |
| 4041 | |
| 4042 | auto paragraph = builder.Build(); |
| 4043 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4044 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4045 | |
| 4046 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4047 | REPORTER_ASSERT(reporter, impl->lines().size() == 4); |
| 4048 | |
| 4049 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 4050 | RectHeightStyle rect_height_max_style = RectHeightStyle::kMax; |
| 4051 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4052 | { |
| 4053 | auto boxes = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style); |
| 4054 | REPORTER_ASSERT(reporter, boxes.empty()); |
| 4055 | } |
| 4056 | { |
| 4057 | auto boxes = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4058 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4059 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4060 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, EPSILON100)); |
| 4061 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 34.5f, EPSILON100)); |
| 4062 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, EPSILON100)); |
| 4063 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 84.5f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4064 | } |
| 4065 | { |
| 4066 | auto boxes = paragraph->getRectsForRange(0, 1, rect_height_max_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4067 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4068 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4069 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, EPSILON100)); |
| 4070 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 34.5f, EPSILON100)); |
| 4071 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, EPSILON100)); |
| 4072 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 95, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4073 | } |
| 4074 | { |
| 4075 | auto boxes = paragraph->getRectsForRange(6, 10, rect_height_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4076 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4077 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4078 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, EPSILON100)); |
| 4079 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 34.5f, EPSILON100)); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4080 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4081 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 84.5f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4082 | } |
| 4083 | { |
| 4084 | auto boxes = paragraph->getRectsForRange(6, 10, rect_height_max_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4085 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4086 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4087 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, EPSILON100)); |
| 4088 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 34.5f, EPSILON100)); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4089 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4090 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 95, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4091 | } |
| 4092 | { |
| 4093 | auto boxes = paragraph->getRectsForRange(14, 16, rect_height_max_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4094 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4095 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4096 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, EPSILON100)); |
| 4097 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 224.5f, EPSILON100)); |
| 4098 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 100, EPSILON100)); |
| 4099 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 285, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4100 | } |
| 4101 | { |
| 4102 | auto boxes = paragraph->getRectsForRange(20, 25, rect_height_max_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4103 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4104 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4105 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 50, EPSILON100)); |
| 4106 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 319.5f, EPSILON100)); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4107 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 300, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4108 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 380, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4109 | } |
| 4110 | } |
| 4111 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4112 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4113 | DEF_TEST(SkParagraph_StrutParagraph2, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4114 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4115 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4116 | TestCanvas canvas("SkParagraph_StrutParagraph2.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4117 | // The chinese extra height should be absorbed by the strut. |
| 4118 | const char* text = "01234ABCDEFGH\nabcd\nABCDEFGH"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4119 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4120 | |
| 4121 | ParagraphStyle paragraph_style; |
| 4122 | paragraph_style.setMaxLines(10); |
| 4123 | paragraph_style.setTextAlign(TextAlign::kLeft); |
| 4124 | paragraph_style.turnHintingOff(); |
| 4125 | |
| 4126 | StrutStyle strut_style; |
| 4127 | |
| 4128 | strut_style.setStrutEnabled(true); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4129 | strut_style.setFontFamilies({SkString("Ahem")}); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4130 | strut_style.setFontSize(50); |
| 4131 | strut_style.setHeight(1.6f); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4132 | strut_style.setHeightOverride(true); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4133 | paragraph_style.setStrutStyle(strut_style); |
| 4134 | |
| 4135 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4136 | |
| 4137 | TextStyle text_style; |
| 4138 | text_style.setFontFamilies({SkString("Ahem")}); |
| 4139 | text_style.setFontSize(50); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4140 | text_style.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 4141 | SkFontStyle::kUpright_Slant)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4142 | text_style.setColor(SK_ColorBLACK); |
| 4143 | text_style.setHeight(1); |
| 4144 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4145 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4146 | builder.pop(); |
| 4147 | |
| 4148 | auto paragraph = builder.Build(); |
| 4149 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4150 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4151 | |
| 4152 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 4153 | // Font is not resolved and the first line does not fit |
| 4154 | REPORTER_ASSERT(reporter, impl->lines().size() == 4); |
| 4155 | |
| 4156 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 4157 | RectHeightStyle rect_height_max_style = RectHeightStyle::kMax; |
| 4158 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4159 | { |
| 4160 | auto boxes = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style); |
| 4161 | REPORTER_ASSERT(reporter, boxes.empty()); |
| 4162 | } |
| 4163 | { |
| 4164 | auto boxes = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4165 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4166 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4167 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, EPSILON100)); |
| 4168 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 24, EPSILON100)); |
| 4169 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, EPSILON100)); |
| 4170 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 74, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4171 | } |
| 4172 | { |
| 4173 | auto boxes = paragraph->getRectsForRange(0, 1, rect_height_max_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4174 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4175 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4176 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, EPSILON100)); |
| 4177 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 24, EPSILON100)); |
| 4178 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, EPSILON100)); |
| 4179 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 80, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4180 | } |
| 4181 | { |
| 4182 | auto boxes = paragraph->getRectsForRange(6, 10, rect_height_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4183 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4184 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4185 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, EPSILON100)); |
| 4186 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 24, EPSILON100)); |
| 4187 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, EPSILON100)); |
| 4188 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 74, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4189 | } |
| 4190 | { |
| 4191 | auto boxes = paragraph->getRectsForRange(6, 10, rect_height_max_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4192 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4193 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4194 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, EPSILON100)); |
| 4195 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 24, EPSILON100)); |
| 4196 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, EPSILON100)); |
| 4197 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 80, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4198 | } |
| 4199 | { |
| 4200 | auto boxes = paragraph->getRectsForRange(14, 16, rect_height_max_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4201 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4202 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4203 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, EPSILON100)); |
| 4204 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 184, EPSILON100)); |
| 4205 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 100, EPSILON100)); |
| 4206 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 240, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4207 | } |
| 4208 | { |
| 4209 | auto boxes = paragraph->getRectsForRange(20, 25, rect_height_max_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4210 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4211 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4212 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 50, EPSILON100)); |
| 4213 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 264, EPSILON100)); |
| 4214 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 300, EPSILON100)); |
| 4215 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 320, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4216 | } |
| 4217 | } |
| 4218 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4219 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4220 | DEF_TEST(SkParagraph_StrutParagraph3, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4221 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4222 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4223 | TestCanvas canvas("SkParagraph_StrutParagraph3.png"); |
| 4224 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4225 | // The chinese extra height should be absorbed by the strut. |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4226 | const char* text = "01234満毎p行来昼本可\nabcd\n満毎冠行来昼本可"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4227 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4228 | |
| 4229 | ParagraphStyle paragraph_style; |
| 4230 | paragraph_style.setMaxLines(10); |
| 4231 | paragraph_style.setTextAlign(TextAlign::kLeft); |
| 4232 | paragraph_style.turnHintingOff(); |
| 4233 | |
| 4234 | StrutStyle strut_style; |
| 4235 | strut_style.setStrutEnabled(true); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4236 | strut_style.setFontFamilies({SkString("Ahem")}); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4237 | strut_style.setFontSize(50); |
| 4238 | strut_style.setHeight(1.2f); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4239 | strut_style.setHeightOverride(true); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4240 | paragraph_style.setStrutStyle(strut_style); |
| 4241 | |
| 4242 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4243 | |
| 4244 | TextStyle text_style; |
| 4245 | text_style.setFontFamilies({SkString("Ahem")}); |
| 4246 | text_style.setFontSize(50); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4247 | text_style.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 4248 | SkFontStyle::kUpright_Slant)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4249 | text_style.setColor(SK_ColorBLACK); |
| 4250 | text_style.setHeight(1); |
| 4251 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4252 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4253 | builder.pop(); |
| 4254 | |
| 4255 | auto paragraph = builder.Build(); |
| 4256 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4257 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4258 | |
| 4259 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 4260 | // Font is not resolved and the first line does not fit |
| 4261 | REPORTER_ASSERT(reporter, impl->lines().size() == 4); |
| 4262 | |
| 4263 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 4264 | RectHeightStyle rect_height_max_style = RectHeightStyle::kMax; |
| 4265 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 4266 | SkScalar epsilon = 0.001f; |
| 4267 | { |
| 4268 | auto boxes = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style); |
| 4269 | REPORTER_ASSERT(reporter, boxes.empty()); |
| 4270 | } |
| 4271 | { |
| 4272 | auto boxes = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4273 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4274 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 4275 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, epsilon)); |
| 4276 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 8, epsilon)); |
| 4277 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, epsilon)); |
| 4278 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 58, epsilon)); |
| 4279 | } |
| 4280 | { |
| 4281 | auto boxes = paragraph->getRectsForRange(0, 1, rect_height_max_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4282 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4283 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 4284 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, epsilon)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4285 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 8, epsilon)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4286 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, epsilon)); |
| 4287 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 60, epsilon)); |
| 4288 | } |
| 4289 | { |
| 4290 | auto boxes = paragraph->getRectsForRange(6, 10, rect_height_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4291 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4292 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4293 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, epsilon)); |
| 4294 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 8, epsilon)); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4295 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, epsilon)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4296 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 58, epsilon)); |
| 4297 | } |
| 4298 | { |
| 4299 | auto boxes = paragraph->getRectsForRange(6, 10, rect_height_max_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4300 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4301 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4302 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, epsilon)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4303 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 8, epsilon)); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4304 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, epsilon)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4305 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 60, epsilon)); |
| 4306 | } |
| 4307 | { |
| 4308 | auto boxes = paragraph->getRectsForRange(14, 16, rect_height_max_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4309 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4310 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 4311 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, epsilon)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4312 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 128, epsilon)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4313 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 100, epsilon)); |
| 4314 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 180, epsilon)); |
| 4315 | } |
| 4316 | { |
| 4317 | auto boxes = paragraph->getRectsForRange(20, 25, rect_height_max_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4318 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4319 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 4320 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 50, epsilon)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4321 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 188, epsilon)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4322 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 300, epsilon)); |
| 4323 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 240, epsilon)); |
| 4324 | } |
| 4325 | } |
| 4326 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4327 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4328 | DEF_TEST(SkParagraph_StrutForceParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4329 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4330 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4331 | TestCanvas canvas("SkParagraph_StrutForceParagraph.png"); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4332 | const char* text = "01234満毎冠行来昼本可\nabcd\n満毎冠行来昼本可"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4333 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4334 | |
| 4335 | ParagraphStyle paragraph_style; |
| 4336 | paragraph_style.setMaxLines(10); |
| 4337 | paragraph_style.setTextAlign(TextAlign::kLeft); |
| 4338 | paragraph_style.turnHintingOff(); |
| 4339 | |
| 4340 | StrutStyle strut_style; |
| 4341 | strut_style.setStrutEnabled(true); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4342 | strut_style.setFontFamilies({SkString("Ahem")}); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4343 | strut_style.setFontSize(50); |
| 4344 | strut_style.setHeight(1.5f); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4345 | strut_style.setHeightOverride(true); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4346 | strut_style.setLeading(0.1f); |
| 4347 | strut_style.setForceStrutHeight(true); |
| 4348 | paragraph_style.setStrutStyle(strut_style); |
| 4349 | |
| 4350 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4351 | |
| 4352 | TextStyle text_style; |
| 4353 | text_style.setFontFamilies({SkString("Ahem")}); |
| 4354 | text_style.setFontSize(50); |
| 4355 | text_style.setLetterSpacing(0); |
| 4356 | text_style.setColor(SK_ColorBLACK); |
| 4357 | text_style.setHeight(1); |
| 4358 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4359 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4360 | builder.pop(); |
| 4361 | |
| 4362 | auto paragraph = builder.Build(); |
| 4363 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4364 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4365 | |
| 4366 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 4367 | // Font is not resolved and the first line does not fit |
| 4368 | REPORTER_ASSERT(reporter, impl->lines().size() == 4); |
| 4369 | |
| 4370 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 4371 | RectHeightStyle rect_height_max_style = RectHeightStyle::kMax; |
| 4372 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4373 | |
| 4374 | auto boxes1 = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style); |
| 4375 | REPORTER_ASSERT(reporter, boxes1.empty()); |
| 4376 | |
| 4377 | auto boxes2 = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4378 | canvas.drawRects(SK_ColorRED, boxes2); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4379 | REPORTER_ASSERT(reporter, boxes2.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4380 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes2[0].rect.left(), 0, EPSILON100)); |
| 4381 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes2[0].rect.top(), 22.5f, EPSILON100)); |
| 4382 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes2[0].rect.right(), 50, EPSILON100)); |
| 4383 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes2[0].rect.bottom(), 72.5f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4384 | |
| 4385 | auto boxes3 = paragraph->getRectsForRange(0, 1, rect_height_max_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4386 | canvas.drawRects(SK_ColorRED, boxes3); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4387 | REPORTER_ASSERT(reporter, boxes3.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4388 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes3[0].rect.left(), 0, EPSILON100)); |
| 4389 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes3[0].rect.top(), 22.5f, EPSILON100)); |
| 4390 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes3[0].rect.right(), 50, EPSILON100)); |
| 4391 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes3[0].rect.bottom(), 80, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4392 | |
| 4393 | auto boxes4 = paragraph->getRectsForRange(6, 10, rect_height_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4394 | canvas.drawRects(SK_ColorRED, boxes4); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4395 | REPORTER_ASSERT(reporter, boxes4.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4396 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes4[0].rect.left(), 300, EPSILON100)); |
| 4397 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes4[0].rect.top(), 22.5f, EPSILON100)); |
| 4398 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes4[0].rect.right(), 500, EPSILON100)); |
| 4399 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes4[0].rect.bottom(), 72.5f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4400 | |
| 4401 | auto boxes5 = paragraph->getRectsForRange(6, 10, rect_height_max_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4402 | canvas.drawRects(SK_ColorRED, boxes5); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4403 | REPORTER_ASSERT(reporter, boxes5.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4404 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes5[0].rect.left(), 300, EPSILON100)); |
| 4405 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes5[0].rect.top(), 22.5f, EPSILON100)); |
| 4406 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes5[0].rect.right(), 500, EPSILON100)); |
| 4407 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes5[0].rect.bottom(), 80, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4408 | |
| 4409 | auto boxes6 = paragraph->getRectsForRange(14, 16, rect_height_max_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4410 | canvas.drawRects(SK_ColorRED, boxes6); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4411 | REPORTER_ASSERT(reporter, boxes6.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4412 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes6[0].rect.left(), 0, EPSILON100)); |
| 4413 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes6[0].rect.top(), 182.5f, EPSILON100)); |
| 4414 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes6[0].rect.right(), 100, EPSILON100)); |
| 4415 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes6[0].rect.bottom(), 240, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4416 | |
| 4417 | auto boxes7 = paragraph->getRectsForRange(20, 25, rect_height_max_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4418 | canvas.drawRects(SK_ColorRED, boxes7); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4419 | REPORTER_ASSERT(reporter, boxes7.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4420 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes7[0].rect.left(), 50, EPSILON100)); |
| 4421 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes7[0].rect.top(), 262.5f, EPSILON100)); |
| 4422 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes7[0].rect.right(), 300, EPSILON100)); |
| 4423 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes7[0].rect.bottom(), 320, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4424 | } |
| 4425 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4426 | // Checked: NO DIFF |
| 4427 | DEF_TEST(SkParagraph_StrutDefaultParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4428 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4429 | if (!fontCollection->fontsFound()) return; |
| 4430 | TestCanvas canvas("SkParagraph_StrutDefaultParagraph.png"); |
| 4431 | |
| 4432 | const char* text = "01234満毎冠行来昼本可\nabcd\n満毎冠行来昼本可"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4433 | const size_t len = strlen(text); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4434 | |
| 4435 | ParagraphStyle paragraph_style; |
| 4436 | paragraph_style.setMaxLines(10); |
| 4437 | paragraph_style.setTextAlign(TextAlign::kLeft); |
| 4438 | paragraph_style.turnHintingOff(); |
| 4439 | |
| 4440 | StrutStyle strut_style; |
| 4441 | strut_style.setStrutEnabled(true); |
| 4442 | strut_style.setFontFamilies({SkString("Ahem")}); |
| 4443 | strut_style.setFontSize(50); |
| 4444 | strut_style.setHeight(1.5f); |
| 4445 | strut_style.setLeading(0.1f); |
| 4446 | strut_style.setForceStrutHeight(false); |
| 4447 | paragraph_style.setStrutStyle(strut_style); |
| 4448 | |
| 4449 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4450 | |
| 4451 | TextStyle text_style; |
| 4452 | text_style.setFontFamilies({SkString("Ahem")}); |
| 4453 | text_style.setFontSize(20); |
| 4454 | text_style.setColor(SK_ColorBLACK); |
| 4455 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4456 | builder.addText(text, len); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4457 | builder.pop(); |
| 4458 | |
| 4459 | auto paragraph = builder.Build(); |
| 4460 | paragraph->layout(550); |
| 4461 | paragraph->paint(canvas.get(), 0, 0); |
| 4462 | |
| 4463 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 4464 | RectHeightStyle rect_height_strut_style = RectHeightStyle::kStrut; |
| 4465 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 4466 | { |
| 4467 | auto boxes = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style); |
| 4468 | REPORTER_ASSERT(reporter, boxes.empty()); |
| 4469 | } |
| 4470 | { |
| 4471 | auto boxes = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style); |
| 4472 | canvas.drawRects(SK_ColorRED, boxes); |
| 4473 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 4474 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, EPSILON100)); |
| 4475 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 26.5f, EPSILON100)); |
| 4476 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 20, EPSILON100)); |
| 4477 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 46.5f, EPSILON100)); |
| 4478 | } |
| 4479 | { |
| 4480 | auto boxes = paragraph->getRectsForRange(0, 2, rect_height_strut_style, rect_width_style); |
| 4481 | canvas.drawRects(SK_ColorRED, boxes); |
| 4482 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 4483 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, EPSILON100)); |
| 4484 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 2.5f, EPSILON100)); |
| 4485 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 40, EPSILON100)); |
| 4486 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 52.5f, EPSILON100)); |
| 4487 | } |
| 4488 | } |
| 4489 | |
Julia Lavrova | c5313e6 | 2019-12-10 12:11:17 -0500 | [diff] [blame] | 4490 | DEF_TEST(SkParagraph_FontFeaturesParagraph, reporter) { |
| 4491 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
| 4492 | if (!fontCollection->fontsFound()) return; |
| 4493 | TestCanvas canvas("SkParagraph_FontFeaturesParagraph.png"); |
| 4494 | |
| 4495 | const char* text = "12ab\n"; |
| 4496 | |
| 4497 | ParagraphStyle paragraph_style; |
| 4498 | paragraph_style.turnHintingOff(); |
| 4499 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4500 | |
| 4501 | TextStyle text_style; |
| 4502 | text_style.setFontStyle(SkFontStyle::Italic()); |
| 4503 | text_style.setFontFamilies({SkString("Roboto")}); |
| 4504 | text_style.setColor(SK_ColorBLACK); |
| 4505 | |
| 4506 | text_style.addFontFeature(SkString("tnum"), 1); |
| 4507 | builder.pushStyle(text_style); |
| 4508 | builder.addText(text); |
| 4509 | |
| 4510 | text_style.resetFontFeatures(); |
| 4511 | text_style.addFontFeature(SkString("tnum"), 0); |
| 4512 | text_style.addFontFeature(SkString("pnum"), 1); |
| 4513 | builder.pushStyle(text_style); |
| 4514 | builder.addText(text); |
| 4515 | |
| 4516 | builder.pop(); |
| 4517 | builder.pop(); |
| 4518 | |
| 4519 | auto paragraph = builder.Build(); |
| 4520 | paragraph->layout(TestCanvasWidth); |
| 4521 | |
| 4522 | paragraph->paint(canvas.get(), 10.0, 15.0); |
| 4523 | |
| 4524 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 4525 | REPORTER_ASSERT(reporter, paragraph->lineNumber() == 3ull); |
| 4526 | |
| 4527 | auto& tnum_line = impl->lines()[0]; |
| 4528 | auto& pnum_line = impl->lines()[1]; |
| 4529 | |
| 4530 | REPORTER_ASSERT(reporter, tnum_line.clusters().width() == 4ull); |
| 4531 | REPORTER_ASSERT(reporter, pnum_line.clusters().width() == 4ull); |
| 4532 | // Tabular numbers should have equal widths. |
| 4533 | REPORTER_ASSERT(reporter, impl->clusters()[0].width() == impl->clusters()[1].width()); |
| 4534 | // Proportional numbers should have variable widths. |
| 4535 | REPORTER_ASSERT(reporter, impl->clusters()[5].width() != impl->clusters()[6].width()); |
| 4536 | // Alphabetic characters should be unaffected. |
| 4537 | REPORTER_ASSERT(reporter, impl->clusters()[2].width() == impl->clusters()[7].width()); |
| 4538 | } |
| 4539 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4540 | // Not in Minikin |
| 4541 | DEF_TEST(SkParagraph_WhitespacesInMultipleFonts, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4542 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4543 | if (!fontCollection->fontsFound()) return; |
| 4544 | const char* text = "English English 字典 字典 😀😃😄 😀😃😄"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4545 | const size_t len = strlen(text); |
| 4546 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4547 | ParagraphStyle paragraph_style; |
| 4548 | paragraph_style.turnHintingOff(); |
| 4549 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4550 | |
| 4551 | TextStyle text_style; |
| 4552 | text_style.setFontFamilies( |
| 4553 | {SkString("Roboto"), SkString("Noto Color Emoji"), SkString("Source Han Serif CN")}); |
| 4554 | text_style.setFontSize(60); |
| 4555 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4556 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4557 | builder.pop(); |
| 4558 | |
| 4559 | auto paragraph = builder.Build(); |
| 4560 | paragraph->layout(TestCanvasWidth); |
Julia Lavrova | 3281b96 | 2019-12-02 11:32:25 -0500 | [diff] [blame] | 4561 | |
| 4562 | REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == 0); |
| 4563 | |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4564 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 4565 | for (size_t i = 0; i < impl->runs().size() - 1; ++i) { |
| 4566 | auto first = impl->runs()[i].textRange(); |
| 4567 | auto next = impl->runs()[i + 1].textRange(); |
| 4568 | REPORTER_ASSERT(reporter, first.end == next.start); |
| 4569 | } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4570 | } |
| 4571 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4572 | DEF_TEST(SkParagraph_JSON1, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4573 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4574 | if (!fontCollection->fontsFound()) return; |
| 4575 | const char* text = "👨👩👧👦"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4576 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4577 | |
| 4578 | ParagraphStyle paragraph_style; |
| 4579 | paragraph_style.turnHintingOff(); |
| 4580 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4581 | |
| 4582 | TextStyle text_style; |
| 4583 | text_style.setFontFamilies({SkString("Noto Color Emoji")}); |
| 4584 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4585 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4586 | builder.pop(); |
| 4587 | |
| 4588 | auto paragraph = builder.Build(); |
| 4589 | paragraph->layout(TestCanvasWidth); |
| 4590 | |
| 4591 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 4592 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4593 | auto& run = impl->runs().front(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4594 | |
| 4595 | auto cluster = 0; |
| 4596 | SkShaperJSONWriter::VisualizeClusters( |
| 4597 | text, 0, std::strlen(text), run.glyphs(), run.clusterIndexes(), |
| 4598 | [&](int codePointCount, SkSpan<const char> utf1to1, SkSpan<const SkGlyphID> glyph1to1) { |
| 4599 | if (cluster == 0) { |
| 4600 | std::string toCheckUtf8{utf1to1.data(), utf1to1.size()}; |
| 4601 | SkASSERT(std::strcmp(text, utf1to1.data()) == 0); |
| 4602 | SkASSERT(glyph1to1.size() == 1); |
| 4603 | SkASSERT(*glyph1to1.begin() == 1611); |
| 4604 | } |
| 4605 | ++cluster; |
| 4606 | }); |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4607 | REPORTER_ASSERT(reporter, cluster <= 2); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4608 | } |
| 4609 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4610 | DEF_TEST(SkParagraph_JSON2, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4611 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4612 | if (!fontCollection->fontsFound()) return; |
| 4613 | const char* text = "p〠q"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4614 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4615 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4616 | ParagraphStyle paragraph_style; |
| 4617 | paragraph_style.turnHintingOff(); |
| 4618 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4619 | |
| 4620 | TextStyle text_style; |
| 4621 | text_style.setFontFamilies({SkString("Noto Sans CJK JP")}); |
| 4622 | text_style.setColor(SK_ColorBLACK); |
| 4623 | text_style.setFontSize(50); |
| 4624 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4625 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4626 | builder.pop(); |
| 4627 | |
| 4628 | auto paragraph = builder.Build(); |
| 4629 | paragraph->layout(TestCanvasWidth); |
| 4630 | |
| 4631 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 4632 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4633 | |
| 4634 | auto cluster = 0; |
| 4635 | for (auto& run : impl->runs()) { |
| 4636 | SkShaperJSONWriter::VisualizeClusters( |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4637 | impl->text().begin() + run.textRange().start, 0, run.textRange().width(), |
| 4638 | run.glyphs(), run.clusterIndexes(), |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4639 | [&](int codePointCount, SkSpan<const char> utf1to1, |
| 4640 | SkSpan<const SkGlyphID> glyph1to1) { |
| 4641 | if (cluster == 0) { |
| 4642 | std::string toCheckUtf8{utf1to1.data(), utf1to1.size()}; |
| 4643 | SkASSERT(std::strcmp(text, utf1to1.data()) == 0); |
| 4644 | SkASSERT(glyph1to1.size() == 3); |
| 4645 | } |
| 4646 | ++cluster; |
| 4647 | }); |
| 4648 | } |
| 4649 | |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4650 | REPORTER_ASSERT(reporter, cluster <= 2); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4651 | } |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4652 | |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4653 | DEF_TEST(SkParagraph_CacheText, reporter) { |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4654 | ParagraphCache cache; |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4655 | cache.turnOn(true); |
| 4656 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4657 | if (!fontCollection->fontsFound()) return; |
| 4658 | |
| 4659 | ParagraphStyle paragraph_style; |
| 4660 | paragraph_style.turnHintingOff(); |
| 4661 | |
| 4662 | TextStyle text_style; |
| 4663 | text_style.setFontFamilies({SkString("Roboto")}); |
| 4664 | text_style.setColor(SK_ColorBLACK); |
| 4665 | |
| 4666 | auto test = [&](const char* text, int count, bool expectedToBeFound) { |
| 4667 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4668 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4669 | builder.addText(text, strlen(text)); |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4670 | builder.pop(); |
| 4671 | auto paragraph = builder.Build(); |
| 4672 | |
| 4673 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 4674 | REPORTER_ASSERT(reporter, count == cache.count()); |
| 4675 | auto found = cache.findParagraph(impl); |
| 4676 | REPORTER_ASSERT(reporter, found == expectedToBeFound); |
| 4677 | auto added = cache.updateParagraph(impl); |
| 4678 | REPORTER_ASSERT(reporter, added != expectedToBeFound); |
| 4679 | }; |
| 4680 | |
| 4681 | test("text1", 0, false); |
| 4682 | test("text1", 1, true); |
| 4683 | test("text2", 1, false); |
| 4684 | test("text2", 2, true); |
| 4685 | test("text3", 2, false); |
| 4686 | } |
| 4687 | |
| 4688 | DEF_TEST(SkParagraph_CacheFonts, reporter) { |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4689 | ParagraphCache cache; |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4690 | cache.turnOn(true); |
| 4691 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4692 | if (!fontCollection->fontsFound()) return; |
| 4693 | |
| 4694 | ParagraphStyle paragraph_style; |
| 4695 | paragraph_style.turnHintingOff(); |
| 4696 | |
| 4697 | TextStyle text_style; |
| 4698 | text_style.setColor(SK_ColorBLACK); |
| 4699 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4700 | const char* text = "text"; |
| 4701 | const size_t len = strlen(text); |
| 4702 | |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4703 | auto test = [&](int count, bool expectedToBeFound) { |
| 4704 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4705 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4706 | builder.addText(text, len); |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4707 | builder.pop(); |
| 4708 | auto paragraph = builder.Build(); |
| 4709 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 4710 | |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4711 | REPORTER_ASSERT(reporter, count == cache.count()); |
| 4712 | auto found = cache.findParagraph(impl); |
| 4713 | REPORTER_ASSERT(reporter, found == expectedToBeFound); |
| 4714 | auto added = cache.updateParagraph(impl); |
| 4715 | REPORTER_ASSERT(reporter, added != expectedToBeFound); |
| 4716 | }; |
| 4717 | |
| 4718 | text_style.setFontFamilies({SkString("Roboto")}); |
| 4719 | test(0, false); |
| 4720 | test(1, true); |
| 4721 | text_style.setFontFamilies({SkString("Homemade Apple")}); |
| 4722 | test(1, false); |
| 4723 | test(2, true); |
| 4724 | text_style.setFontFamilies({SkString("Noto Color Emoji")}); |
| 4725 | test(2, false); |
| 4726 | } |
| 4727 | |
| 4728 | DEF_TEST(SkParagraph_CacheFontRanges, reporter) { |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4729 | ParagraphCache cache; |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4730 | cache.turnOn(true); |
| 4731 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4732 | if (!fontCollection->fontsFound()) return; |
| 4733 | |
| 4734 | ParagraphStyle paragraph_style; |
| 4735 | paragraph_style.turnHintingOff(); |
| 4736 | |
| 4737 | TextStyle text_style; |
| 4738 | text_style.setFontFamilies({SkString("Roboto")}); |
| 4739 | text_style.setColor(SK_ColorBLACK); |
| 4740 | |
| 4741 | auto test = [&](const char* text1, |
| 4742 | const char* text2, |
| 4743 | const char* font1, |
| 4744 | const char* font2, |
| 4745 | int count, |
| 4746 | bool expectedToBeFound) { |
| 4747 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4748 | text_style.setFontFamilies({SkString(font1)}); |
| 4749 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4750 | builder.addText(text1, strlen(text1)); |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4751 | builder.pop(); |
| 4752 | text_style.setFontFamilies({SkString(font2)}); |
| 4753 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4754 | builder.addText(text2, strlen(text2)); |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4755 | builder.pop(); |
| 4756 | auto paragraph = builder.Build(); |
| 4757 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 4758 | |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4759 | REPORTER_ASSERT(reporter, count == cache.count()); |
| 4760 | auto found = cache.findParagraph(impl); |
| 4761 | REPORTER_ASSERT(reporter, found == expectedToBeFound); |
| 4762 | auto added = cache.updateParagraph(impl); |
| 4763 | REPORTER_ASSERT(reporter, added != expectedToBeFound); |
| 4764 | }; |
| 4765 | |
| 4766 | test("text", "", "Roboto", "Homemade Apple", 0, false); |
| 4767 | test("t", "ext", "Roboto", "Homemade Apple", 1, false); |
| 4768 | test("te", "xt", "Roboto", "Homemade Apple", 2, false); |
| 4769 | test("tex", "t", "Roboto", "Homemade Apple", 3, false); |
| 4770 | test("text", "", "Roboto", "Homemade Apple", 4, true); |
| 4771 | } |
| 4772 | |
| 4773 | DEF_TEST(SkParagraph_CacheStyles, reporter) { |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4774 | ParagraphCache cache; |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4775 | cache.turnOn(true); |
| 4776 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4777 | if (!fontCollection->fontsFound()) return; |
| 4778 | |
| 4779 | ParagraphStyle paragraph_style; |
| 4780 | paragraph_style.turnHintingOff(); |
| 4781 | |
| 4782 | TextStyle text_style; |
| 4783 | text_style.setFontFamilies({SkString("Roboto")}); |
| 4784 | text_style.setColor(SK_ColorBLACK); |
| 4785 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4786 | const char* text = "text"; |
| 4787 | const size_t len = strlen(text); |
| 4788 | |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4789 | auto test = [&](int count, bool expectedToBeFound) { |
| 4790 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4791 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4792 | builder.addText(text, len); |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4793 | builder.pop(); |
| 4794 | auto paragraph = builder.Build(); |
| 4795 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 4796 | |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4797 | REPORTER_ASSERT(reporter, count == cache.count()); |
| 4798 | auto found = cache.findParagraph(impl); |
| 4799 | REPORTER_ASSERT(reporter, found == expectedToBeFound); |
| 4800 | auto added = cache.updateParagraph(impl); |
| 4801 | REPORTER_ASSERT(reporter, added != expectedToBeFound); |
| 4802 | }; |
| 4803 | |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4804 | test(0, false); |
| 4805 | test(1, true); |
| 4806 | text_style.setLetterSpacing(10); |
| 4807 | test(1, false); |
| 4808 | test(2, true); |
| 4809 | text_style.setWordSpacing(10); |
| 4810 | test(2, false); |
| 4811 | } |
Julia Lavrova | a40db42 | 2019-08-21 13:49:15 -0400 | [diff] [blame] | 4812 | |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4813 | DEF_TEST(SkParagraph_EmptyParagraphWithLineBreak, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4814 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a40db42 | 2019-08-21 13:49:15 -0400 | [diff] [blame] | 4815 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4816 | fontCollection->setDefaultFontManager(SkFontMgr::RefDefault()); |
| 4817 | TestCanvas canvas("SkParagraph_EmptyParagraphWithLineBreak.png"); |
Julia Lavrova | a40db42 | 2019-08-21 13:49:15 -0400 | [diff] [blame] | 4818 | |
| 4819 | ParagraphStyle paragraph_style; |
Julia Lavrova | a40db42 | 2019-08-21 13:49:15 -0400 | [diff] [blame] | 4820 | TextStyle text_style; |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4821 | text_style.setFontSize(16); |
| 4822 | text_style.setFontFamilies({SkString("Roboto")}); |
| 4823 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4824 | builder.addText("\n", 1); |
Julia Lavrova | a40db42 | 2019-08-21 13:49:15 -0400 | [diff] [blame] | 4825 | |
| 4826 | auto paragraph = builder.Build(); |
| 4827 | paragraph->layout(TestCanvasWidth); |
| 4828 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 4829 | auto result = paragraph->getRectsForPlaceholders(); |
Julia Lavrova | a40db42 | 2019-08-21 13:49:15 -0400 | [diff] [blame] | 4830 | } |
| 4831 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4832 | DEF_TEST(SkParagraph_NullInMiddleOfText, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4833 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4834 | if (!fontCollection->fontsFound()) return; |
| 4835 | fontCollection->setDefaultFontManager(SkFontMgr::RefDefault()); |
| 4836 | TestCanvas canvas("SkParagraph_NullInMiddleOfText.png"); |
| 4837 | |
| 4838 | const SkString text("null terminator ->\u0000<- on purpose did you see it?"); |
| 4839 | |
| 4840 | ParagraphStyle paragraph_style; |
| 4841 | TextStyle text_style; |
| 4842 | text_style.setFontSize(16); |
| 4843 | text_style.setFontFamilies({SkString("Roboto")}); |
| 4844 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4845 | builder.addText(text.c_str(), text.size()); |
| 4846 | |
| 4847 | auto paragraph = builder.Build(); |
| 4848 | paragraph->layout(TestCanvasWidth); |
| 4849 | paragraph->paint(canvas.get(), 0, 0); |
| 4850 | } |
| 4851 | |
Julia Lavrova | a40db42 | 2019-08-21 13:49:15 -0400 | [diff] [blame] | 4852 | DEF_TEST(SkParagraph_PlaceholderOnly, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4853 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a40db42 | 2019-08-21 13:49:15 -0400 | [diff] [blame] | 4854 | if (!fontCollection->fontsFound()) return; |
| 4855 | TestCanvas canvas("SkParagraph_PlaceholderOnly.png"); |
| 4856 | |
| 4857 | ParagraphStyle paragraph_style; |
| 4858 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4859 | |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4860 | PlaceholderStyle placeholder(0, 0, PlaceholderAlignment::kBaseline, TextBaseline::kAlphabetic, 0); |
Julia Lavrova | a40db42 | 2019-08-21 13:49:15 -0400 | [diff] [blame] | 4861 | builder.addPlaceholder(placeholder); |
| 4862 | |
| 4863 | auto paragraph = builder.Build(); |
| 4864 | paragraph->layout(TestCanvasWidth); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 4865 | auto result = paragraph->getRectsForPlaceholders(); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4866 | paragraph->paint(canvas.get(), 0, 0); |
| 4867 | } |
| 4868 | |
| 4869 | DEF_TEST(SkParagraph_Fallbacks, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4870 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4871 | if (!fontCollection->fontsFound()) return; |
| 4872 | fontCollection->setDefaultFontManager(SkFontMgr::RefDefault(), "Arial"); |
| 4873 | TestCanvas canvas("SkParagraph_Fallbacks.png"); |
| 4874 | |
| 4875 | const char* multiScript = "A1!aÀàĀāƁƀḂⱠꜲꬰəͲἀἏЀЖԠꙐꙮՁخࡔࠇܦআਉઐଘஇఘಧൺඣᭆᯔᮯ᳇ꠈᜅᩌꪈ༇ꥄꡙꫤ᧰៘꧁꧂ᜰᨏᯤᢆᣭᗗꗃⵞ𐒎߷ጩꬤ𖠺‡₩℻Ⅷ↹⋇⏳ⓖ╋▒◛⚧⑆שׁ🅕㊼龜ポ䷤🂡\n"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4876 | const size_t len = strlen(multiScript); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4877 | |
| 4878 | const char* androidFonts[] = { |
| 4879 | "sans-serif", |
| 4880 | "sans-serif-condensed", |
| 4881 | "serif", |
| 4882 | "monospace", |
| 4883 | "serif-monospace", |
| 4884 | "casual", |
| 4885 | "cursive", |
| 4886 | "sans-serif-smallcaps", |
| 4887 | }; |
| 4888 | |
| 4889 | for (auto& font : androidFonts) { |
| 4890 | |
| 4891 | ParagraphStyle paragraph_style; |
| 4892 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4893 | |
| 4894 | TextStyle text_style; |
| 4895 | text_style.setColor(SK_ColorBLACK); |
| 4896 | text_style.setLocale(SkString("en_US")); |
| 4897 | text_style.setFontSize(20); |
| 4898 | |
| 4899 | text_style.setFontFamilies({ SkString(font) }); |
| 4900 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4901 | builder.addText(multiScript, len); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4902 | |
| 4903 | builder.pop(); |
| 4904 | |
| 4905 | auto paragraph = builder.Build(); |
| 4906 | paragraph->layout(TestCanvasWidth); |
| 4907 | paragraph->paint(canvas.get(), 0, 0); |
| 4908 | canvas.get()->translate(0, paragraph.get()->getHeight() + 10); |
| 4909 | } |
| 4910 | } |
| 4911 | |
| 4912 | DEF_TEST(SkParagraph_Bidi1, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4913 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4914 | if (!fontCollection->fontsFound()) return; |
| 4915 | fontCollection->setDefaultFontManager(SkFontMgr::RefDefault()); |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4916 | fontCollection->enableFontFallback(); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4917 | TestCanvas canvas("SkParagraph_Bidi1.png"); |
| 4918 | |
| 4919 | std::u16string abc = u"\u202Dabc"; |
| 4920 | std::u16string DEF = u"\u202EDEF"; |
| 4921 | std::u16string ghi = u"\u202Dghi"; |
| 4922 | std::u16string JKL = u"\u202EJKL"; |
| 4923 | std::u16string mno = u"\u202Dmno"; |
| 4924 | |
| 4925 | std::u16string abcDEFghiJKLmno = u"\u202Dabc\u202EDEF\u202Dghi\u202EJKL\u202Dmno"; |
| 4926 | |
| 4927 | ParagraphStyle paragraph_style; |
| 4928 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4929 | |
| 4930 | TextStyle text_style; |
| 4931 | text_style.setFontFamilies({ SkString("sans-serif")}); |
| 4932 | text_style.setFontSize(40); |
| 4933 | |
| 4934 | text_style.setColor(SK_ColorCYAN); |
| 4935 | text_style.setFontStyle(SkFontStyle(SkFontStyle::kThin_Weight, SkFontStyle::kNormal_Width, SkFontStyle::kUpright_Slant)); |
| 4936 | builder.pushStyle(text_style); |
| 4937 | builder.addText(abc); |
| 4938 | |
| 4939 | text_style.setColor(SK_ColorGREEN); |
| 4940 | text_style.setFontStyle(SkFontStyle(SkFontStyle::kLight_Weight, SkFontStyle::kNormal_Width, SkFontStyle::kUpright_Slant)); |
| 4941 | builder.pushStyle(text_style); |
| 4942 | builder.addText(DEF); |
| 4943 | |
| 4944 | text_style.setColor(SK_ColorYELLOW); |
| 4945 | text_style.setFontStyle(SkFontStyle(SkFontStyle::kNormal_Weight, SkFontStyle::kNormal_Width, SkFontStyle::kUpright_Slant)); |
| 4946 | builder.pushStyle(text_style); |
| 4947 | builder.addText(ghi); |
| 4948 | |
| 4949 | text_style.setColor(SK_ColorMAGENTA); |
| 4950 | text_style.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, SkFontStyle::kUpright_Slant)); |
| 4951 | builder.pushStyle(text_style); |
| 4952 | builder.addText(JKL); |
| 4953 | |
| 4954 | text_style.setColor(SK_ColorBLUE); |
| 4955 | text_style.setFontStyle(SkFontStyle(SkFontStyle::kBlack_Weight, SkFontStyle::kNormal_Width, SkFontStyle::kUpright_Slant)); |
| 4956 | builder.pushStyle(text_style); |
| 4957 | builder.addText(mno); |
| 4958 | |
| 4959 | auto paragraph = builder.Build(); |
| 4960 | paragraph->layout(400); |
| 4961 | paragraph->paint(canvas.get(), 0, 0); |
| 4962 | } |
| 4963 | |
| 4964 | DEF_TEST(SkParagraph_Bidi2, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4965 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4966 | if (!fontCollection->fontsFound()) return; |
| 4967 | fontCollection->setDefaultFontManager(SkFontMgr::RefDefault()); |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4968 | fontCollection->enableFontFallback(); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4969 | TestCanvas canvas("SkParagraph_Bidi2.png"); |
| 4970 | |
| 4971 | std::u16string abcD = u"\u202Dabc\u202ED"; |
| 4972 | std::u16string EFgh = u"EF\u202Dgh"; |
| 4973 | std::u16string iJKLmno = u"i\u202EJKL\u202Dmno"; |
| 4974 | |
| 4975 | std::u16string abcDEFghiJKLmno = u"\u202Dabc\u202EDEF\u202Dghi\u202EJKL\u202Dmno"; |
| 4976 | |
| 4977 | ParagraphStyle paragraph_style; |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4978 | TextStyle text_style; |
| 4979 | text_style.setFontFamilies({ SkString("sans-serif")}); |
| 4980 | text_style.setFontSize(40); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4981 | |
Julia Lavrova | c88a3bc | 2020-01-23 10:16:26 -0500 | [diff] [blame] | 4982 | { |
| 4983 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4984 | builder.pushStyle(text_style); |
| 4985 | builder.addText(abcD); |
| 4986 | builder.pushStyle(text_style); |
| 4987 | builder.addText(EFgh); |
| 4988 | builder.pushStyle(text_style); |
| 4989 | builder.addText(iJKLmno); |
| 4990 | auto paragraph = builder.Build(); |
| 4991 | paragraph->layout(360); |
| 4992 | paragraph->paint(canvas.get(), 0, 0); |
| 4993 | } |
| 4994 | canvas.get()->translate(0, 400); |
| 4995 | { |
| 4996 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4997 | builder.pushStyle(text_style); |
| 4998 | builder.addText(abcDEFghiJKLmno); |
| 4999 | auto paragraph = builder.Build(); |
| 5000 | paragraph->layout(360); |
| 5001 | paragraph->paint(canvas.get(), 0, 0); |
| 5002 | } |
Julia Lavrova | a40db42 | 2019-08-21 13:49:15 -0400 | [diff] [blame] | 5003 | } |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 5004 | |
| 5005 | DEF_TEST(SkParagraph_NewlineOnly, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 5006 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 5007 | if (!fontCollection->fontsFound()) return; |
| 5008 | fontCollection->setDefaultFontManager(SkFontMgr::RefDefault()); |
| 5009 | TestCanvas canvas("SkParagraph_Newline.png"); |
| 5010 | |
| 5011 | TextStyle text_style; |
| 5012 | text_style.setFontFamilies({SkString("Ahem")}); |
| 5013 | text_style.setColor(SK_ColorBLACK); |
| 5014 | StrutStyle strut_style; |
| 5015 | strut_style.setStrutEnabled(false); |
| 5016 | ParagraphStyle paragraph_style; |
| 5017 | paragraph_style.setStrutStyle(strut_style); |
| 5018 | paragraph_style.setTextStyle(text_style); |
| 5019 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 5020 | builder.addText("\n"); |
| 5021 | auto paragraph = builder.Build(); |
| 5022 | paragraph->layout(1000); |
| 5023 | REPORTER_ASSERT(reporter, paragraph->getHeight() == 28); |
| 5024 | } |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 5025 | |
Julia Lavrova | b30d481 | 2019-11-08 14:51:16 -0500 | [diff] [blame] | 5026 | DEF_TEST_DISABLED(SkParagraph_FontResolutions, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 5027 | TestCanvas canvas("SkParagraph_FontResolutions.png"); |
| 5028 | |
| 5029 | sk_sp<TestFontCollection> fontCollection = |
| 5030 | sk_make_sp<TestFontCollection>(GetResourcePath("fonts").c_str(), false); |
| 5031 | if (!fontCollection->fontsFound()) return; |
| 5032 | |
| 5033 | if (!fontCollection->addFontFromFile("abc/abc.ttf", "abc")) { |
| 5034 | return; |
| 5035 | } |
| 5036 | if (!fontCollection->addFontFromFile("abc/abc+grave.ttf", "abc+grave")) { |
| 5037 | return; |
| 5038 | } |
| 5039 | if (!fontCollection->addFontFromFile("abc/abc_agrave.ttf", "abc_agrave")) { |
| 5040 | return; |
| 5041 | } |
| 5042 | |
| 5043 | TextStyle text_style; |
| 5044 | text_style.setFontFamilies({SkString("abc")}); |
| 5045 | text_style.setFontSize(50); |
| 5046 | |
| 5047 | ParagraphStyle paragraph_style; |
| 5048 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 5049 | |
| 5050 | text_style.setFontFamilies({SkString("abc"), SkString("abc+grave")}); |
| 5051 | text_style.setColor(SK_ColorBLUE); |
| 5052 | builder.pushStyle(text_style); |
| 5053 | builder.addText(u"a\u0300"); |
| 5054 | text_style.setColor(SK_ColorMAGENTA); |
| 5055 | builder.pushStyle(text_style); |
| 5056 | builder.addText(u"à"); |
| 5057 | |
| 5058 | text_style.setFontFamilies({SkString("abc"), SkString("abc_agrave")}); |
| 5059 | |
| 5060 | text_style.setColor(SK_ColorRED); |
| 5061 | builder.pushStyle(text_style); |
| 5062 | builder.addText(u"a\u0300"); |
| 5063 | text_style.setColor(SK_ColorGREEN); |
| 5064 | builder.pushStyle(text_style); |
| 5065 | builder.addText(u"à"); |
| 5066 | |
| 5067 | auto paragraph = builder.Build(); |
| 5068 | paragraph->layout(TestCanvasWidth); |
| 5069 | |
| 5070 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 5071 | REPORTER_ASSERT(reporter, impl->runs().size() == 2); |
| 5072 | |
| 5073 | REPORTER_ASSERT(reporter, impl->runs().front().size() == 4); |
| 5074 | REPORTER_ASSERT(reporter, impl->runs().front().glyphs()[0] == impl->runs().front().glyphs()[2]); |
| 5075 | REPORTER_ASSERT(reporter, impl->runs().front().glyphs()[1] == impl->runs().front().glyphs()[3]); |
| 5076 | |
| 5077 | REPORTER_ASSERT(reporter, impl->runs().back().size() == 2); |
| 5078 | REPORTER_ASSERT(reporter, impl->runs().back().glyphs()[0] == impl->runs().back().glyphs()[1]); |
| 5079 | |
| 5080 | paragraph->paint(canvas.get(), 100, 100); |
| 5081 | } |
| 5082 | |
Julia Lavrova | b30d481 | 2019-11-08 14:51:16 -0500 | [diff] [blame] | 5083 | DEF_TEST_DISABLED(SkParagraph_FontStyle, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 5084 | TestCanvas canvas("SkParagraph_FontStyle.png"); |
| 5085 | |
| 5086 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(GetResourcePath("fonts").c_str(), false, true); |
| 5087 | if (!fontCollection->fontsFound()) return; |
| 5088 | |
| 5089 | TextStyle text_style; |
| 5090 | text_style.setFontFamilies({SkString("Roboto")}); |
| 5091 | text_style.setColor(SK_ColorBLACK); |
| 5092 | text_style.setFontSize(20); |
| 5093 | SkFontStyle fs = SkFontStyle( |
| 5094 | SkFontStyle::Weight::kLight_Weight, |
| 5095 | SkFontStyle::Width::kNormal_Width, |
| 5096 | SkFontStyle::Slant::kUpright_Slant |
| 5097 | ); |
| 5098 | text_style.setFontStyle(fs); |
| 5099 | ParagraphStyle paragraph_style; |
| 5100 | paragraph_style.setTextStyle(text_style); |
| 5101 | TextStyle boldItalic; |
| 5102 | boldItalic.setFontFamilies({SkString("Roboto")}); |
| 5103 | boldItalic.setColor(SK_ColorRED); |
| 5104 | SkFontStyle bi = SkFontStyle( |
| 5105 | SkFontStyle::Weight::kBold_Weight, |
| 5106 | SkFontStyle::Width::kNormal_Width, |
| 5107 | SkFontStyle::Slant::kItalic_Slant |
| 5108 | ); |
| 5109 | boldItalic.setFontStyle(bi); |
| 5110 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 5111 | builder.addText("Default text\n"); |
| 5112 | builder.pushStyle(boldItalic); |
| 5113 | builder.addText("Bold and Italic\n"); |
| 5114 | builder.pop(); |
| 5115 | builder.addText("back to normal"); |
| 5116 | auto paragraph = builder.Build(); |
| 5117 | paragraph->layout(250); |
| 5118 | paragraph->paint(canvas.get(), 0, 0); |
| 5119 | } |
| 5120 | |
Julia Lavrova | b30d481 | 2019-11-08 14:51:16 -0500 | [diff] [blame] | 5121 | DEF_TEST_DISABLED(SkParagraph_Shaping, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 5122 | TestCanvas canvas("SkParagraph_Shaping.png"); |
| 5123 | |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 5124 | auto dir = "/usr/local/google/home/jlavrova/Sources/flutter/engine/src/out/host_debug_unopt_x86/gen/flutter/third_party/txt/assets"; |
| 5125 | sk_sp<TestFontCollection> fontCollection = |
| 5126 | sk_make_sp<TestFontCollection>(dir, /*GetResourcePath("fonts").c_str(), */ false); |
| 5127 | if (!fontCollection->fontsFound()) return; |
| 5128 | |
| 5129 | |
| 5130 | TextStyle text_style; |
| 5131 | text_style.setFontFamilies({SkString("Roboto")}); |
| 5132 | text_style.setColor(SK_ColorGRAY); |
| 5133 | text_style.setFontSize(14); |
| 5134 | SkFontStyle b = SkFontStyle( |
| 5135 | SkFontStyle::Weight::kNormal_Weight, |
| 5136 | SkFontStyle::Width::kNormal_Width, |
| 5137 | SkFontStyle::Slant::kUpright_Slant |
| 5138 | ); |
| 5139 | text_style.setFontStyle(b); |
| 5140 | ParagraphStyle paragraph_style; |
| 5141 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 5142 | builder.pushStyle(text_style); |
| 5143 | builder.addText("Eat0 apple0 pies0 | Eat1 apple1 pies1 | Eat2 apple2 pies2"); |
| 5144 | auto paragraph = builder.Build(); |
| 5145 | paragraph->layout(380); |
| 5146 | paragraph->paint(canvas.get(), 0, 0); |
| 5147 | } |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 5148 | |
| 5149 | DEF_TEST(SkParagraph_Ellipsis, reporter) { |
| 5150 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
| 5151 | if (!fontCollection->fontsFound()) return; |
| 5152 | fontCollection->setDefaultFontManager(SkFontMgr::RefDefault()); |
| 5153 | TestCanvas canvas("SkParagraph_Ellipsis.png"); |
| 5154 | |
| 5155 | const char* text = "This\n" |
| 5156 | "is a wrapping test. It should wrap at manual newlines, and if softWrap is true, also at spaces."; |
| 5157 | TextStyle text_style; |
| 5158 | text_style.setFontFamilies({SkString("Ahem")}); |
| 5159 | text_style.setColor(SK_ColorBLACK); |
| 5160 | text_style.setFontSize(10); |
| 5161 | |
| 5162 | auto relayout = [&](size_t lines, bool ellipsis, |
| 5163 | SkScalar width, SkScalar height, SkScalar minWidth, SkScalar maxWidth, SkColor bg) { |
| 5164 | ParagraphStyle paragraph_style; |
| 5165 | SkPaint paint; |
| 5166 | paint.setColor(bg); |
| 5167 | text_style.setForegroundColor(paint); |
| 5168 | paragraph_style.setTextStyle(text_style); |
| 5169 | paragraph_style.setMaxLines(lines); |
| 5170 | if (ellipsis) { |
| 5171 | paragraph_style.setEllipsis(u"\u2026"); |
| 5172 | } |
| 5173 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 5174 | builder.addText(text); |
| 5175 | auto paragraph = builder.Build(); |
| 5176 | paragraph->layout(50); |
| 5177 | paragraph->paint(canvas.get(), 0, 0); |
| 5178 | canvas.get()->translate(50, paragraph->getHeight() + 10); |
| 5179 | auto result = paragraph->getRectsForRange(0, strlen(text), RectHeightStyle::kTight, RectWidthStyle::kTight); |
| 5180 | SkPaint background; |
| 5181 | background.setColor(SK_ColorRED); |
| 5182 | background.setStyle(SkPaint::kStroke_Style); |
| 5183 | background.setAntiAlias(true); |
| 5184 | background.setStrokeWidth(1); |
| 5185 | canvas.get()->drawRect(result.front().rect, background); |
| 5186 | |
| 5187 | SkASSERT(width == paragraph->getMaxWidth()); |
| 5188 | SkASSERT(height == paragraph->getHeight()); |
| 5189 | SkASSERT(minWidth == paragraph->getMinIntrinsicWidth()); |
| 5190 | SkASSERT(maxWidth == paragraph->getMaxIntrinsicWidth()); |
| 5191 | }; |
| 5192 | |
| 5193 | SkPaint paint; |
| 5194 | paint.setColor(SK_ColorLTGRAY); |
| 5195 | canvas.get()->drawRect(SkRect::MakeXYWH(0, 0, 50, 500), paint); |
| 5196 | |
| 5197 | relayout(1, false, 50, 10, 950, 950, SK_ColorRED); |
Julia Lavrova | 90bfd1c | 2019-12-04 11:43:32 -0500 | [diff] [blame] | 5198 | relayout(3, false, 50, 30, 90, 950, SK_ColorBLUE); |
| 5199 | relayout(std::numeric_limits<size_t>::max(), false, 50, 200, 90, 950, SK_ColorGREEN); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 5200 | |
| 5201 | relayout(1, true, 50, 10, 950, 950, SK_ColorYELLOW); |
Julia Lavrova | 90bfd1c | 2019-12-04 11:43:32 -0500 | [diff] [blame] | 5202 | relayout(3, true, 50, 30, 90, 950, SK_ColorMAGENTA); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 5203 | relayout(std::numeric_limits<size_t>::max(), true, 50, 20, 950, 950, SK_ColorCYAN); |
| 5204 | |
| 5205 | relayout(1, false, 50, 10, 950, 950, SK_ColorRED); |
Julia Lavrova | 90bfd1c | 2019-12-04 11:43:32 -0500 | [diff] [blame] | 5206 | relayout(3, false, 50, 30, 90, 950, SK_ColorBLUE); |
| 5207 | relayout(std::numeric_limits<size_t>::max(), false, 50, 200, 90, 950, SK_ColorGREEN); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 5208 | } |
Julia Lavrova | db543f6 | 2020-01-29 11:43:41 -0500 | [diff] [blame^] | 5209 | |
| 5210 | DEF_TEST(SkParagraph_MemoryLeak, reporter) { |
| 5211 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
| 5212 | if (!fontCollection->fontsFound()) return; |
| 5213 | fontCollection->setDefaultFontManager(SkFontMgr::RefDefault()); |
| 5214 | |
| 5215 | std::string text; |
| 5216 | for (size_t i = 0; i < 10; i++) |
| 5217 | { |
| 5218 | SkPaint paint; |
| 5219 | paint.setAntiAlias(true); |
| 5220 | paint.setColor(SK_ColorBLACK); |
| 5221 | |
| 5222 | TextStyle textStyle; |
| 5223 | textStyle.setForegroundColor(paint); |
| 5224 | textStyle.setFontFamilies({ SkString("Roboto") }); |
| 5225 | |
| 5226 | ParagraphStyle paragraphStyle; |
| 5227 | paragraphStyle.setTextStyle(textStyle); |
| 5228 | |
| 5229 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 5230 | text += "Text "; |
| 5231 | builder.addText(text.c_str()); |
| 5232 | |
| 5233 | auto paragraph = builder.Build(); |
| 5234 | paragraph->layout(100); |
| 5235 | |
| 5236 | //used to add a delay so I can monitor memory usage |
| 5237 | //std::this_thread::sleep_for(std::chrono::milliseconds(1000)); |
| 5238 | } |
| 5239 | }; |