Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1 | // Copyright 2019 Google LLC. |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 2 | #include "src/core/SkFontMgrPriv.h" |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3 | #include <sstream> |
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 | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 8 | #include "src/core/SkOSFile.h" |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 9 | #include "src/utils/SkOSPath.h" |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 10 | #include "src/utils/SkShaperJSONWriter.h" |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 11 | #include "tests/CodecPriv.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 12 | #include "tests/Test.h" |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 13 | #include "tools/Resources.h" |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 14 | #include "tools/ToolUtils.h" |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 15 | |
Julia Lavrova | b30d481 | 2019-11-08 14:51:16 -0500 | [diff] [blame] | 16 | #include "third_party/icu/SkLoadICU.h" |
| 17 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 18 | #define VeryLongCanvasWidth 1000000 |
| 19 | #define TestCanvasWidth 1000 |
| 20 | #define TestCanvasHeight 600 |
| 21 | |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 22 | #define DEF_TEST_DISABLED(name, reporter) \ |
| 23 | static void test_##name(skiatest::Reporter* reporter, const GrContextOptions&); \ |
| 24 | skiatest::TestRegistry name##TestRegistry(skiatest::Test(#name, false, test_##name)); \ |
Herb Derby | 126d7a2 | 2019-11-27 11:38:16 -0500 | [diff] [blame] | 25 | void test_##name(skiatest::Reporter* reporter, const GrContextOptions&) { /* SkDebugf("Disabled:"#name "\n"); */ } \ |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 26 | void disabled_##name(skiatest::Reporter* reporter, const GrContextOptions&) |
| 27 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 28 | using namespace skia::textlayout; |
| 29 | namespace { |
| 30 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 31 | SkScalar EPSILON100 = 0.01f; |
| 32 | SkScalar EPSILON50 = 0.02f; |
| 33 | SkScalar EPSILON20 = 0.05f; |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 34 | SkScalar EPSILON10 = 0.1f; |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 35 | SkScalar EPSILON5 = 0.20f; |
| 36 | SkScalar EPSILON2 = 0.50f; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 37 | |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 38 | bool equal(const char* base, TextRange a, const char* b) { |
| 39 | return std::strncmp(b, base + a.start, a.width()) == 0; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 40 | } |
Julia Lavrova | 3281b96 | 2019-12-02 11:32:25 -0500 | [diff] [blame] | 41 | |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 42 | class ResourceFontCollection : public FontCollection { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 43 | public: |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 44 | ResourceFontCollection(bool testOnly = false) |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 45 | : fFontsFound(false) |
| 46 | , fResolvedFonts(0) |
| 47 | , fResourceDir(GetResourcePath("fonts").c_str()) |
| 48 | , fFontProvider(sk_make_sp<TypefaceFontProvider>()) { |
| 49 | std::vector<SkString> fonts; |
| 50 | SkOSFile::Iter iter(fResourceDir.c_str()); |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 51 | |
Julia Lavrova | b30d481 | 2019-11-08 14:51:16 -0500 | [diff] [blame] | 52 | if (!SkLoadICU()) { |
| 53 | SkDebugf("ICU not loaded, skipping all the tests\n"); |
| 54 | return; |
| 55 | } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 56 | SkString path; |
| 57 | while (iter.next(&path)) { |
| 58 | if (path.endsWith("Roboto-Italic.ttf")) { |
| 59 | fFontsFound = true; |
| 60 | } |
| 61 | fonts.emplace_back(path); |
| 62 | } |
| 63 | |
| 64 | if (!fFontsFound) { |
Herb Derby | 126d7a2 | 2019-11-27 11:38:16 -0500 | [diff] [blame] | 65 | // SkDebugf("Fonts not found, skipping all the tests\n"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 66 | return; |
| 67 | } |
| 68 | // Only register fonts if we have to |
| 69 | for (auto& font : fonts) { |
| 70 | SkString file_path; |
| 71 | file_path.printf("%s/%s", fResourceDir.c_str(), font.c_str()); |
| 72 | fFontProvider->registerTypeface(SkTypeface::MakeFromFile(file_path.c_str())); |
| 73 | } |
| 74 | |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 75 | if (testOnly) { |
| 76 | this->setTestFontManager(std::move(fFontProvider)); |
| 77 | } else { |
| 78 | this->setAssetFontManager(std::move(fFontProvider)); |
| 79 | } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 80 | this->disableFontFallback(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 81 | } |
| 82 | |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 83 | ~ResourceFontCollection() = default; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 84 | |
| 85 | size_t resolvedFonts() const { return fResolvedFonts; } |
| 86 | |
| 87 | // TODO: temp solution until we check in fonts |
| 88 | bool fontsFound() const { return fFontsFound; } |
| 89 | |
| 90 | private: |
| 91 | bool fFontsFound; |
| 92 | size_t fResolvedFonts; |
| 93 | std::string fResourceDir; |
| 94 | sk_sp<TypefaceFontProvider> fFontProvider; |
| 95 | }; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 96 | |
| 97 | class TestCanvas { |
| 98 | public: |
| 99 | TestCanvas(const char* testName) : name(testName) { |
| 100 | bits.allocN32Pixels(TestCanvasWidth, TestCanvasHeight); |
| 101 | canvas = new SkCanvas(bits); |
| 102 | canvas->clear(SK_ColorWHITE); |
| 103 | } |
| 104 | |
| 105 | ~TestCanvas() { |
Julia Lavrova | aae845f | 2019-08-16 16:44:40 -0400 | [diff] [blame] | 106 | SkString tmpDir = skiatest::GetTmpDir(); |
| 107 | if (!tmpDir.isEmpty()) { |
| 108 | SkString path = SkOSPath::Join(tmpDir.c_str(), name); |
| 109 | SkFILEWStream file(path.c_str()); |
| 110 | if (!SkEncodeImage(&file, bits, SkEncodedImageFormat::kPNG, 100)) { |
| 111 | SkDebugf("Cannot write a picture %s\n", name); |
| 112 | } |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 113 | } |
| 114 | delete canvas; |
| 115 | } |
| 116 | |
| 117 | void drawRects(SkColor color, std::vector<TextBox>& result, bool fill = false) { |
| 118 | |
| 119 | SkPaint paint; |
| 120 | if (!fill) { |
| 121 | paint.setStyle(SkPaint::kStroke_Style); |
| 122 | paint.setAntiAlias(true); |
| 123 | paint.setStrokeWidth(1); |
| 124 | } |
| 125 | paint.setColor(color); |
| 126 | for (auto& r : result) { |
| 127 | canvas->drawRect(r.rect, paint); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | void drawLine(SkColor color, SkRect rect, bool vertical = true) { |
| 132 | |
| 133 | SkPaint paint; |
| 134 | paint.setStyle(SkPaint::kStroke_Style); |
| 135 | paint.setAntiAlias(true); |
| 136 | paint.setStrokeWidth(1); |
| 137 | paint.setColor(color); |
| 138 | if (vertical) { |
| 139 | canvas->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint); |
| 140 | } else { |
| 141 | canvas->drawLine(rect.fLeft, rect.fTop, rect.fRight, rect.fTop, paint); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | void drawLines(SkColor color, std::vector<TextBox>& result) { |
| 146 | |
| 147 | for (auto& r : result) { |
| 148 | drawLine(color, r.rect); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | SkCanvas* get() { return canvas; } |
| 153 | private: |
| 154 | SkBitmap bits; |
| 155 | SkCanvas* canvas; |
| 156 | const char* name; |
| 157 | }; |
| 158 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 159 | } // namespace |
| 160 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 161 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 162 | DEF_TEST(SkParagraph_SimpleParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 163 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 164 | if (!fontCollection->fontsFound()) return; |
| 165 | const char* text = "Hello World Text Dialog"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 166 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 167 | |
| 168 | ParagraphStyle paragraph_style; |
| 169 | paragraph_style.turnHintingOff(); |
| 170 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 171 | |
| 172 | TextStyle text_style; |
| 173 | text_style.setFontFamilies({SkString("Roboto")}); |
| 174 | text_style.setColor(SK_ColorBLACK); |
| 175 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 176 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 177 | builder.pop(); |
| 178 | |
| 179 | auto paragraph = builder.Build(); |
| 180 | paragraph->layout(TestCanvasWidth); |
Julia Lavrova | 3281b96 | 2019-12-02 11:32:25 -0500 | [diff] [blame] | 181 | REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 182 | |
| 183 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 184 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 185 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); // paragraph style does not count |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 186 | REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 187 | |
| 188 | size_t index = 0; |
| 189 | for (auto& line : impl->lines()) { |
| 190 | line.scanStyles(StyleType::kDecorations, |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 191 | [&index, reporter] |
| 192 | (TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 193 | REPORTER_ASSERT(reporter, index == 0); |
| 194 | REPORTER_ASSERT(reporter, style.getColor() == SK_ColorBLACK); |
| 195 | ++index; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 196 | }); |
| 197 | } |
| 198 | } |
| 199 | |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 200 | DEF_TEST(SkParagraph_InlinePlaceholderParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 201 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 202 | TestCanvas canvas("SkParagraph_InlinePlaceholderParagraph.png"); |
| 203 | if (!fontCollection->fontsFound()) return; |
| 204 | |
| 205 | const char* text = "012 34"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 206 | const size_t len = strlen(text); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 207 | |
| 208 | ParagraphStyle paragraph_style; |
| 209 | paragraph_style.turnHintingOff(); |
| 210 | paragraph_style.setMaxLines(14); |
| 211 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 212 | |
| 213 | TextStyle text_style; |
| 214 | text_style.setFontFamilies({SkString("Roboto")}); |
| 215 | text_style.setColor(SK_ColorBLACK); |
| 216 | text_style.setFontSize(26); |
| 217 | text_style.setWordSpacing(5); |
| 218 | text_style.setLetterSpacing(1); |
| 219 | text_style.setDecoration(TextDecoration::kUnderline); |
| 220 | text_style.setDecorationColor(SK_ColorBLACK); |
| 221 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 222 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 223 | |
| 224 | PlaceholderStyle placeholder1(50, 50, PlaceholderAlignment::kBaseline, TextBaseline::kAlphabetic, 0); |
| 225 | builder.addPlaceholder(placeholder1); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 226 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 227 | builder.addPlaceholder(placeholder1); |
| 228 | |
| 229 | PlaceholderStyle placeholder2(5, 50, PlaceholderAlignment::kBaseline, TextBaseline::kAlphabetic, 50); |
| 230 | builder.addPlaceholder(placeholder2); |
| 231 | builder.addPlaceholder(placeholder1); |
| 232 | builder.addPlaceholder(placeholder2); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 233 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 234 | builder.addPlaceholder(placeholder2); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 235 | builder.addText(text, len); |
| 236 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 237 | builder.addPlaceholder(placeholder2); |
| 238 | builder.addPlaceholder(placeholder2); |
| 239 | builder.addPlaceholder(placeholder2); |
| 240 | builder.addPlaceholder(placeholder2); |
| 241 | builder.addPlaceholder(placeholder2); |
| 242 | builder.addPlaceholder(placeholder1); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 243 | builder.addText(text, len); |
| 244 | builder.addText(text, len); |
| 245 | builder.addText(text, len); |
| 246 | builder.addText(text, len); |
| 247 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 248 | builder.addPlaceholder(placeholder2); |
| 249 | builder.addPlaceholder(placeholder1); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 250 | builder.addText(text, len); |
| 251 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 252 | |
| 253 | builder.pop(); |
| 254 | |
| 255 | auto paragraph = builder.Build(); |
| 256 | paragraph->layout(TestCanvasWidth); |
| 257 | paragraph->paint(canvas.get(), 0, 0); |
| 258 | |
| 259 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 260 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 261 | |
| 262 | auto boxes = paragraph->getRectsForRange(0, 3, rect_height_style, rect_width_style); |
| 263 | canvas.drawRects(SK_ColorRED, boxes); |
| 264 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 265 | |
| 266 | boxes = paragraph->getRectsForRange(0, 3, rect_height_style, rect_width_style); |
| 267 | canvas.drawRects(SK_ColorGREEN, boxes); |
| 268 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 269 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 270 | boxes = paragraph->getRectsForPlaceholders(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 271 | canvas.drawRects(SK_ColorRED, boxes); |
| 272 | |
| 273 | boxes = paragraph->getRectsForRange(4, 17, rect_height_style, rect_width_style); |
| 274 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 275 | |
| 276 | REPORTER_ASSERT(reporter, boxes.size() == 7); |
| 277 | |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 278 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.left(), 90.921f, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 279 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.top(), 50, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 280 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.right(), 90.921f + 50, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 281 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.bottom(), 100, EPSILON100)); |
| 282 | |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 283 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[3].rect.left(), 231.343f, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 284 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[3].rect.top(), 50, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 285 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[3].rect.right(), 231.343f + 50, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 286 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[3].rect.bottom(), 100, EPSILON100)); |
| 287 | |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 288 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[4].rect.left(), 281.343f, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 289 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[4].rect.top(), 0, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 290 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[4].rect.right(), 281.343f + 5, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 291 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[4].rect.bottom(), 50, EPSILON100)); |
| 292 | |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 293 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[6].rect.left(), 336.343f, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 294 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[6].rect.top(), 0, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 295 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[6].rect.right(), 336.343f + 5, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 296 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[6].rect.bottom(), 50, EPSILON100)); |
| 297 | } |
| 298 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 299 | DEF_TEST(SkParagraph_InlinePlaceholderBaselineParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 300 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 301 | TestCanvas canvas("SkParagraph_InlinePlaceholderBaselineParagraph.png"); |
| 302 | if (!fontCollection->fontsFound()) return; |
| 303 | |
| 304 | const char* text = "012 34"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 305 | const size_t len = strlen(text); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 306 | |
| 307 | ParagraphStyle paragraph_style; |
| 308 | paragraph_style.turnHintingOff(); |
| 309 | paragraph_style.setMaxLines(14); |
| 310 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 311 | |
| 312 | TextStyle text_style; |
| 313 | text_style.setFontFamilies({SkString("Roboto")}); |
| 314 | text_style.setColor(SK_ColorBLACK); |
| 315 | text_style.setFontSize(26); |
| 316 | text_style.setWordSpacing(5); |
| 317 | text_style.setLetterSpacing(1); |
| 318 | text_style.setDecoration(TextDecoration::kUnderline); |
| 319 | text_style.setDecorationColor(SK_ColorBLACK); |
| 320 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 321 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 322 | |
| 323 | PlaceholderStyle placeholder(55, 50, PlaceholderAlignment::kBaseline, TextBaseline::kAlphabetic, 38.347f); |
| 324 | builder.addPlaceholder(placeholder); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 325 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 326 | |
| 327 | builder.pop(); |
| 328 | |
| 329 | auto paragraph = builder.Build(); |
| 330 | paragraph->layout(TestCanvasWidth); |
| 331 | paragraph->paint(canvas.get(), 0, 0); |
| 332 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 333 | auto boxes = paragraph->getRectsForPlaceholders(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 334 | canvas.drawRects(SK_ColorRED, boxes); |
| 335 | |
| 336 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 337 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 90.921f, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 338 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 339 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f + 55, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 340 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 50, EPSILON100)); |
| 341 | |
| 342 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 343 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 344 | |
| 345 | boxes = paragraph->getRectsForRange(5, 6, rect_height_style, rect_width_style); |
| 346 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 347 | |
| 348 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 349 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 75.324f, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 350 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 14.226f, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 351 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 352 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 44.694f, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 353 | } |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 354 | |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 355 | DEF_TEST(SkParagraph_InlinePlaceholderAboveBaselineParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 356 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 357 | TestCanvas canvas("SkParagraph_InlinePlaceholderAboveBaselineParagraph.png"); |
| 358 | if (!fontCollection->fontsFound()) return; |
| 359 | |
| 360 | const char* text = "012 34"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 361 | const size_t len = strlen(text); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 362 | |
| 363 | ParagraphStyle paragraph_style; |
| 364 | paragraph_style.turnHintingOff(); |
| 365 | paragraph_style.setMaxLines(14); |
| 366 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 367 | |
| 368 | TextStyle text_style; |
| 369 | text_style.setFontFamilies({SkString("Roboto")}); |
| 370 | text_style.setColor(SK_ColorBLACK); |
| 371 | text_style.setFontSize(26); |
| 372 | text_style.setWordSpacing(5); |
| 373 | text_style.setLetterSpacing(1); |
| 374 | text_style.setDecoration(TextDecoration::kUnderline); |
| 375 | text_style.setDecorationColor(SK_ColorBLACK); |
| 376 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 377 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 378 | |
| 379 | PlaceholderStyle placeholder(55, 50, PlaceholderAlignment::kAboveBaseline, TextBaseline::kAlphabetic, 903129.129308f); |
| 380 | builder.addPlaceholder(placeholder); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 381 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 382 | |
| 383 | builder.pop(); |
| 384 | |
| 385 | auto paragraph = builder.Build(); |
| 386 | paragraph->layout(TestCanvasWidth); |
| 387 | paragraph->paint(canvas.get(), 0, 0); |
| 388 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 389 | auto boxes = paragraph->getRectsForPlaceholders(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 390 | canvas.drawRects(SK_ColorRED, boxes); |
| 391 | |
| 392 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 393 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 90.921f, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 394 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), -0.347f, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 395 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f + 55, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 396 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 49.652f, EPSILON100)); |
| 397 | |
| 398 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 399 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 400 | |
| 401 | boxes = paragraph->getRectsForRange(5, 6, rect_height_style, rect_width_style); |
| 402 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 403 | |
| 404 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 405 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 75.324f, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 406 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 25.531f, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 407 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 408 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 56, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 409 | } |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 410 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 411 | DEF_TEST(SkParagraph_InlinePlaceholderBelowBaselineParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 412 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 413 | TestCanvas canvas("SkParagraph_InlinePlaceholderBelowBaselineParagraph.png"); |
| 414 | if (!fontCollection->fontsFound()) return; |
| 415 | |
| 416 | const char* text = "012 34"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 417 | const size_t len = strlen(text); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 418 | |
| 419 | ParagraphStyle paragraph_style; |
| 420 | paragraph_style.turnHintingOff(); |
| 421 | paragraph_style.setMaxLines(14); |
| 422 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 423 | |
| 424 | TextStyle text_style; |
| 425 | text_style.setFontFamilies({SkString("Roboto")}); |
| 426 | text_style.setColor(SK_ColorBLACK); |
| 427 | text_style.setFontSize(26); |
| 428 | text_style.setWordSpacing(5); |
| 429 | text_style.setLetterSpacing(1); |
| 430 | text_style.setDecoration(TextDecoration::kUnderline); |
| 431 | text_style.setDecorationColor(SK_ColorBLACK); |
| 432 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 433 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 434 | |
| 435 | PlaceholderStyle placeholder(55, 50, PlaceholderAlignment::kBelowBaseline, TextBaseline::kAlphabetic, 903129.129308f); |
| 436 | builder.addPlaceholder(placeholder); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 437 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 438 | |
| 439 | builder.pop(); |
| 440 | |
| 441 | auto paragraph = builder.Build(); |
| 442 | paragraph->layout(TestCanvasWidth); |
| 443 | paragraph->paint(canvas.get(), 0, 0); |
| 444 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 445 | auto boxes = paragraph->getRectsForPlaceholders(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 446 | canvas.drawRects(SK_ColorRED, boxes); |
| 447 | |
| 448 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 449 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 90.921f, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 450 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 24, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 451 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f + 55, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 452 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 74, EPSILON100)); |
| 453 | |
| 454 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 455 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 456 | |
| 457 | boxes = paragraph->getRectsForRange(5, 6, rect_height_style, rect_width_style); |
| 458 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 459 | |
| 460 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 461 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 75.324f, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 462 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), -0.121f, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 463 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f, EPSILON2)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 464 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 30.347f, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 465 | } |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 466 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 467 | DEF_TEST(SkParagraph_InlinePlaceholderBottomParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 468 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 469 | TestCanvas canvas("SkParagraph_InlinePlaceholderBottomParagraph.png"); |
| 470 | if (!fontCollection->fontsFound()) return; |
| 471 | |
| 472 | const char* text = "012 34"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 473 | const size_t len = strlen(text); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 474 | |
| 475 | ParagraphStyle paragraph_style; |
| 476 | paragraph_style.turnHintingOff(); |
| 477 | paragraph_style.setMaxLines(14); |
| 478 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 479 | |
| 480 | TextStyle text_style; |
| 481 | text_style.setFontFamilies({SkString("Roboto")}); |
| 482 | text_style.setColor(SK_ColorBLACK); |
| 483 | text_style.setFontSize(26); |
| 484 | text_style.setWordSpacing(5); |
| 485 | text_style.setLetterSpacing(1); |
| 486 | text_style.setDecoration(TextDecoration::kUnderline); |
| 487 | text_style.setDecorationColor(SK_ColorBLACK); |
| 488 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 489 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 490 | |
| 491 | PlaceholderStyle placeholder(55, 50, PlaceholderAlignment::kBottom, TextBaseline::kAlphabetic, 0); |
| 492 | builder.addPlaceholder(placeholder); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 493 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 494 | |
| 495 | builder.pop(); |
| 496 | |
| 497 | auto paragraph = builder.Build(); |
| 498 | paragraph->layout(TestCanvasWidth); |
| 499 | paragraph->paint(canvas.get(), 0, 0); |
| 500 | |
| 501 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 502 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 503 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 504 | auto boxes = paragraph->getRectsForPlaceholders(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 505 | canvas.drawRects(SK_ColorRED, boxes); |
| 506 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 507 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 90.921f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 508 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 509 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f + 55, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 510 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 50, EPSILON100)); |
| 511 | |
| 512 | boxes = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style); |
| 513 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 514 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 515 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0.5f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 516 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 19.531f, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 517 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 16.097f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 518 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 50, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 519 | } |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 520 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 521 | DEF_TEST(SkParagraph_InlinePlaceholderTopParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 522 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 523 | TestCanvas canvas("SkParagraph_InlinePlaceholderTopParagraph.png"); |
| 524 | if (!fontCollection->fontsFound()) return; |
| 525 | |
| 526 | const char* text = "012 34"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 527 | const size_t len = strlen(text); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 528 | |
| 529 | ParagraphStyle paragraph_style; |
| 530 | paragraph_style.turnHintingOff(); |
| 531 | paragraph_style.setMaxLines(14); |
| 532 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 533 | |
| 534 | TextStyle text_style; |
| 535 | text_style.setFontFamilies({SkString("Roboto")}); |
| 536 | text_style.setColor(SK_ColorBLACK); |
| 537 | text_style.setFontSize(26); |
| 538 | text_style.setWordSpacing(5); |
| 539 | text_style.setLetterSpacing(1); |
| 540 | text_style.setDecoration(TextDecoration::kUnderline); |
| 541 | text_style.setDecorationColor(SK_ColorBLACK); |
| 542 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 543 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 544 | |
| 545 | PlaceholderStyle placeholder(55, 50, PlaceholderAlignment::kTop, TextBaseline::kAlphabetic, 0); |
| 546 | builder.addPlaceholder(placeholder); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 547 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 548 | |
| 549 | builder.pop(); |
| 550 | |
| 551 | auto paragraph = builder.Build(); |
| 552 | paragraph->layout(TestCanvasWidth); |
| 553 | paragraph->paint(canvas.get(), 0, 0); |
| 554 | |
| 555 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 556 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 557 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 558 | auto boxes = paragraph->getRectsForPlaceholders(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 559 | canvas.drawRects(SK_ColorRED, boxes); |
| 560 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 561 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 90.921f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 562 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 563 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f + 55, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 564 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 50, EPSILON100)); |
| 565 | |
| 566 | boxes = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style); |
| 567 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 568 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 569 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0.5f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 570 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 571 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 16.097f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 572 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 30.468f, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 573 | } |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 574 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 575 | DEF_TEST(SkParagraph_InlinePlaceholderMiddleParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 576 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 577 | TestCanvas canvas("SkParagraph_InlinePlaceholderMiddleParagraph.png"); |
| 578 | if (!fontCollection->fontsFound()) return; |
| 579 | |
| 580 | const char* text = "012 34"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 581 | const size_t len = strlen(text); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 582 | |
| 583 | ParagraphStyle paragraph_style; |
| 584 | paragraph_style.turnHintingOff(); |
| 585 | paragraph_style.setMaxLines(14); |
| 586 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 587 | |
| 588 | TextStyle text_style; |
| 589 | text_style.setFontFamilies({SkString("Roboto")}); |
| 590 | text_style.setColor(SK_ColorBLACK); |
| 591 | text_style.setFontSize(26); |
| 592 | text_style.setWordSpacing(5); |
| 593 | text_style.setLetterSpacing(1); |
| 594 | text_style.setDecoration(TextDecoration::kUnderline); |
| 595 | text_style.setDecorationColor(SK_ColorBLACK); |
| 596 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 597 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 598 | |
| 599 | PlaceholderStyle placeholder(55, 50, PlaceholderAlignment::kMiddle, TextBaseline::kAlphabetic, 0); |
| 600 | builder.addPlaceholder(placeholder); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 601 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 602 | |
| 603 | builder.pop(); |
| 604 | |
| 605 | auto paragraph = builder.Build(); |
| 606 | paragraph->layout(TestCanvasWidth); |
| 607 | paragraph->paint(canvas.get(), 0, 0); |
| 608 | |
| 609 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 610 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 611 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 612 | auto boxes = paragraph->getRectsForPlaceholders(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 613 | canvas.drawRects(SK_ColorRED, boxes); |
| 614 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 615 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 90.921f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 616 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 617 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f + 55, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 618 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 50, EPSILON100)); |
| 619 | |
| 620 | boxes = paragraph->getRectsForRange(5, 6, rect_height_style, rect_width_style); |
| 621 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 622 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 623 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 75.324f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 624 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 9.765f, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 625 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 626 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 40.234f, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 627 | } |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 628 | |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 629 | DEF_TEST(SkParagraph_InlinePlaceholderIdeographicBaselineParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 630 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 631 | TestCanvas canvas("SkParagraph_InlinePlaceholderIdeographicBaselineParagraph.png"); |
| 632 | if (!fontCollection->fontsFound()) return; |
| 633 | |
| 634 | const char* text = "給能上目秘使"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 635 | const size_t len = strlen(text); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 636 | |
| 637 | ParagraphStyle paragraph_style; |
| 638 | paragraph_style.turnHintingOff(); |
| 639 | paragraph_style.setMaxLines(14); |
| 640 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 641 | |
| 642 | TextStyle text_style; |
| 643 | text_style.setFontFamilies({SkString("Source Han Serif CN")}); |
| 644 | text_style.setColor(SK_ColorBLACK); |
| 645 | text_style.setFontSize(26); |
| 646 | text_style.setWordSpacing(5); |
| 647 | text_style.setLetterSpacing(1); |
| 648 | text_style.setDecoration(TextDecoration::kUnderline); |
| 649 | text_style.setDecorationColor(SK_ColorBLACK); |
| 650 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 651 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 652 | PlaceholderStyle placeholder(55, 50, PlaceholderAlignment::kBaseline, TextBaseline::kIdeographic, 38.347f); |
| 653 | builder.addPlaceholder(placeholder); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 654 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 655 | |
| 656 | builder.pop(); |
| 657 | |
| 658 | auto paragraph = builder.Build(); |
| 659 | paragraph->layout(TestCanvasWidth); |
| 660 | paragraph->paint(canvas.get(), 0, 0); |
| 661 | |
| 662 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 663 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 664 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 665 | auto boxes = paragraph->getRectsForPlaceholders(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 666 | canvas.drawRects(SK_ColorRED, boxes); |
| 667 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 668 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 162.5f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 669 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 670 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 162.5f + 55, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 671 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 50, EPSILON100)); |
| 672 | |
| 673 | boxes = paragraph->getRectsForRange(5, 6, rect_height_style, rect_width_style); |
| 674 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 675 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 676 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 135.5f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 677 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 4.703f, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 678 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 162.5f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 679 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 42.065f, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 680 | } |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 681 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 682 | DEF_TEST(SkParagraph_InlinePlaceholderBreakParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 683 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 684 | TestCanvas canvas("SkParagraph_InlinePlaceholderBreakParagraph.png"); |
| 685 | if (!fontCollection->fontsFound()) return; |
| 686 | |
| 687 | const char* text = "012 34"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 688 | const size_t len = strlen(text); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 689 | |
| 690 | ParagraphStyle paragraph_style; |
| 691 | paragraph_style.turnHintingOff(); |
| 692 | paragraph_style.setMaxLines(14); |
| 693 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 694 | |
| 695 | TextStyle text_style; |
| 696 | text_style.setFontFamilies({SkString("Roboto")}); |
| 697 | text_style.setColor(SK_ColorBLACK); |
| 698 | text_style.setFontSize(26); |
| 699 | text_style.setWordSpacing(5); |
| 700 | text_style.setLetterSpacing(1); |
| 701 | text_style.setDecoration(TextDecoration::kUnderline); |
| 702 | text_style.setDecorationColor(SK_ColorBLACK); |
| 703 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 704 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 705 | |
| 706 | PlaceholderStyle placeholder1(50, 50, PlaceholderAlignment::kBaseline, TextBaseline::kAlphabetic, 50); |
| 707 | PlaceholderStyle placeholder2(25, 25, PlaceholderAlignment::kBaseline, TextBaseline::kAlphabetic, 12.5f); |
| 708 | |
| 709 | builder.addPlaceholder(placeholder1); |
| 710 | builder.addPlaceholder(placeholder1); |
| 711 | builder.addPlaceholder(placeholder1); |
| 712 | builder.addPlaceholder(placeholder2); |
| 713 | builder.addPlaceholder(placeholder1); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 714 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 715 | |
| 716 | builder.addPlaceholder(placeholder1); |
| 717 | builder.addPlaceholder(placeholder1); |
| 718 | builder.addPlaceholder(placeholder1); |
| 719 | builder.addPlaceholder(placeholder1); |
| 720 | builder.addPlaceholder(placeholder2); // 4 + 1 |
| 721 | builder.addPlaceholder(placeholder1); |
| 722 | builder.addPlaceholder(placeholder1); |
| 723 | builder.addPlaceholder(placeholder1); |
| 724 | builder.addPlaceholder(placeholder1); |
| 725 | builder.addPlaceholder(placeholder1); |
| 726 | builder.addPlaceholder(placeholder1); |
| 727 | builder.addPlaceholder(placeholder2); // 6 + 1 |
| 728 | builder.addPlaceholder(placeholder1); |
| 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(placeholder2); // 7 + 1 |
| 736 | |
| 737 | builder.addPlaceholder(placeholder1); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 738 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 739 | builder.addPlaceholder(placeholder1); |
| 740 | builder.addPlaceholder(placeholder2); |
| 741 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 742 | builder.addText(text, len); |
| 743 | builder.addText(text, len); |
| 744 | builder.addText(text, len); |
| 745 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 746 | |
| 747 | builder.addPlaceholder(placeholder2); |
| 748 | builder.addPlaceholder(placeholder1); |
| 749 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 750 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 751 | |
| 752 | builder.addPlaceholder(placeholder2); |
| 753 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 754 | builder.addText(text, len); |
| 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); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 773 | |
| 774 | builder.pop(); |
| 775 | |
| 776 | auto paragraph = builder.Build(); |
| 777 | paragraph->layout(TestCanvasWidth - 100); |
| 778 | paragraph->paint(canvas.get(), 0, 0); |
| 779 | |
| 780 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 781 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 782 | |
| 783 | auto boxes = paragraph->getRectsForRange(0, 3, rect_height_style, rect_width_style); |
| 784 | canvas.drawRects(SK_ColorRED, boxes); |
| 785 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 786 | |
| 787 | boxes = paragraph->getRectsForRange(175, 176, rect_height_style, rect_width_style); |
| 788 | canvas.drawRects(SK_ColorGREEN, boxes); |
| 789 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 790 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 31.695f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 791 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 218.531f, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 792 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 47.292f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 793 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 249, EPSILON100)); |
| 794 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 795 | boxes = paragraph->getRectsForPlaceholders(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 796 | canvas.drawRects(SK_ColorRED, boxes); |
| 797 | |
| 798 | boxes = paragraph->getRectsForRange(4, 45, rect_height_style, rect_width_style); |
| 799 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 800 | REPORTER_ASSERT(reporter, boxes.size() == 30); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 801 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 59.726f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 802 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 26.378f, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 803 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 804 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 56.847f, EPSILON100)); |
| 805 | |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 806 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[11].rect.left(), 606.343f, EPSILON20)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 807 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[11].rect.top(), 38, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 808 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[11].rect.right(), 631.343f, EPSILON20)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 809 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[11].rect.bottom(), 63, EPSILON100)); |
| 810 | |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 811 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[17].rect.left(), 0.5f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 812 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[17].rect.top(), 63.5f, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 813 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[17].rect.right(), 50.5f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 814 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[17].rect.bottom(), 113.5f, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 815 | } |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 816 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 817 | DEF_TEST(SkParagraph_InlinePlaceholderGetRectsParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 818 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 819 | TestCanvas canvas("SkParagraph_InlinePlaceholderGetRectsParagraph.png"); |
| 820 | if (!fontCollection->fontsFound()) return; |
| 821 | |
| 822 | const char* text = "012 34"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 823 | const size_t len = strlen(text); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 824 | |
| 825 | ParagraphStyle paragraph_style; |
| 826 | paragraph_style.turnHintingOff(); |
| 827 | paragraph_style.setMaxLines(14); |
| 828 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 829 | |
| 830 | TextStyle text_style; |
| 831 | text_style.setFontFamilies({SkString("Roboto")}); |
| 832 | text_style.setColor(SK_ColorBLACK); |
| 833 | text_style.setFontSize(26); |
| 834 | text_style.setWordSpacing(5); |
| 835 | text_style.setLetterSpacing(1); |
| 836 | text_style.setDecoration(TextDecoration::kUnderline); |
| 837 | text_style.setDecorationColor(SK_ColorBLACK); |
| 838 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 839 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 840 | |
| 841 | PlaceholderStyle placeholder1(50, 50, PlaceholderAlignment::kBaseline, TextBaseline::kAlphabetic, 50); |
| 842 | PlaceholderStyle placeholder2(5, 20, PlaceholderAlignment::kBaseline, TextBaseline::kAlphabetic, 10); |
| 843 | |
| 844 | builder.addPlaceholder(placeholder1); |
| 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(placeholder2); // 8 + 1 |
| 853 | builder.addPlaceholder(placeholder1); |
| 854 | builder.addPlaceholder(placeholder1); |
| 855 | builder.addPlaceholder(placeholder1); |
| 856 | builder.addPlaceholder(placeholder1); |
| 857 | builder.addPlaceholder(placeholder1); |
| 858 | builder.addPlaceholder(placeholder2); // 5 + 1 |
| 859 | builder.addPlaceholder(placeholder1); |
| 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); // 8 + 0 |
| 867 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 868 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 869 | |
| 870 | builder.addPlaceholder(placeholder1); |
| 871 | builder.addPlaceholder(placeholder2); |
| 872 | builder.addPlaceholder(placeholder2); // 1 + 2 |
| 873 | builder.addPlaceholder(placeholder1); |
| 874 | builder.addPlaceholder(placeholder2); |
| 875 | builder.addPlaceholder(placeholder2); // 1 + 2 |
| 876 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 877 | builder.addText(text, len); |
| 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); // 11 |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 888 | |
| 889 | builder.addPlaceholder(placeholder2); |
| 890 | builder.addPlaceholder(placeholder1); |
| 891 | builder.addPlaceholder(placeholder2); |
| 892 | builder.addPlaceholder(placeholder1); |
| 893 | builder.addPlaceholder(placeholder2); |
| 894 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 895 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 896 | |
| 897 | builder.pop(); |
| 898 | |
| 899 | auto paragraph = builder.Build(); |
| 900 | paragraph->layout(TestCanvasWidth); |
| 901 | paragraph->paint(canvas.get(), 0, 0); |
| 902 | |
| 903 | RectHeightStyle rect_height_style = RectHeightStyle::kMax; |
| 904 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 905 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 906 | auto boxes = paragraph->getRectsForPlaceholders(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 907 | canvas.drawRects(SK_ColorRED, boxes); |
| 908 | |
| 909 | REPORTER_ASSERT(reporter, boxes.size() == 34); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 910 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 90.921f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 911 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 912 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 140.921f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 913 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 50, EPSILON100)); |
| 914 | |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 915 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[16].rect.left(), 800.921f, EPSILON20)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 916 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[16].rect.top(), 0, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 917 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[16].rect.right(), 850.921f, EPSILON20)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 918 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[16].rect.bottom(), 50, EPSILON100)); |
| 919 | |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 920 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[33].rect.left(), 503.382f, EPSILON10)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 921 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[33].rect.top(), 160, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 922 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[33].rect.right(), 508.382f, EPSILON10)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 923 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[33].rect.bottom(), 180, EPSILON100)); |
| 924 | |
| 925 | boxes = paragraph->getRectsForRange(30, 50, rect_height_style, rect_width_style); |
| 926 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 927 | |
| 928 | REPORTER_ASSERT(reporter, boxes.size() == 8); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 929 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 216.097f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 930 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 60, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 931 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 290.921f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 932 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 120, EPSILON100)); |
| 933 | |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 934 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.left(), 290.921f, EPSILON20)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 935 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.top(), 60, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 936 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.right(), 340.921f, EPSILON20)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 937 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.bottom(), 120, EPSILON100)); |
| 938 | |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 939 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[2].rect.left(), 340.921f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 940 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[2].rect.top(), 60, EPSILON100)); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 941 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[2].rect.right(), 345.921f, EPSILON50)); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 942 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[2].rect.bottom(), 120, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 946 | DEF_TEST(SkParagraph_SimpleRedParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 947 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 948 | if (!fontCollection->fontsFound()) return; |
| 949 | const char* text = "I am RED"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 950 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 951 | |
| 952 | ParagraphStyle paragraph_style; |
| 953 | paragraph_style.turnHintingOff(); |
| 954 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 955 | |
| 956 | TextStyle text_style; |
| 957 | text_style.setFontFamilies({SkString("Roboto")}); |
| 958 | text_style.setColor(SK_ColorRED); |
| 959 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 960 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 961 | builder.pop(); |
| 962 | |
| 963 | auto paragraph = builder.Build(); |
| 964 | paragraph->layout(TestCanvasWidth); |
Julia Lavrova | 3281b96 | 2019-12-02 11:32:25 -0500 | [diff] [blame] | 965 | REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 966 | |
| 967 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 968 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 969 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); // paragraph style does not count |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 970 | REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 971 | |
| 972 | size_t index = 0; |
| 973 | for (auto& line : impl->lines()) { |
| 974 | line.scanStyles(StyleType::kDecorations, |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 975 | [reporter, &index](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
| 976 | REPORTER_ASSERT(reporter, index == 0); |
| 977 | REPORTER_ASSERT(reporter, style.getColor() == SK_ColorRED); |
| 978 | ++index; |
| 979 | return true; |
| 980 | }); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 981 | } |
| 982 | } |
| 983 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 984 | // Checked: DIFF+ (Space between 1 & 2 style blocks) |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 985 | DEF_TEST(SkParagraph_RainbowParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 986 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 987 | TestCanvas canvas("SkParagraph_RainbowParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 988 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 989 | const char* text1 = "Red Roboto"; // [0:10) |
| 990 | const char* text2 = "big Greeen Default"; // [10:28) |
| 991 | const char* text3 = "Defcolor Homemade Apple"; // [28:51) |
| 992 | const char* text4 = "Small Blue Roboto"; // [51:68) |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 993 | const char* text41 = "Small Blue "; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 994 | const char* text5 = |
| 995 | "Continue Last Style With lots of words to check if it overlaps " |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 996 | "properly or not"; // [68:) |
| 997 | const char* text42 = |
| 998 | "Roboto" |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 999 | "Continue Last Style With lots of words to check if it overlaps " |
| 1000 | "properly or not"; |
| 1001 | |
| 1002 | ParagraphStyle paragraph_style; |
| 1003 | paragraph_style.turnHintingOff(); |
| 1004 | paragraph_style.setTextAlign(TextAlign::kLeft); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1005 | paragraph_style.setMaxLines(2); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1006 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1007 | |
| 1008 | TextStyle text_style1; |
| 1009 | text_style1.setFontFamilies({SkString("Roboto")}); |
| 1010 | |
| 1011 | text_style1.setColor(SK_ColorRED); |
| 1012 | builder.pushStyle(text_style1); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1013 | builder.addText(text1, strlen(text1)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1014 | |
| 1015 | TextStyle text_style2; |
| 1016 | text_style2.setFontFamilies({SkString("Roboto")}); |
| 1017 | text_style2.setFontSize(50); |
| 1018 | text_style2.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 1019 | SkFontStyle::kUpright_Slant)); |
| 1020 | text_style2.setLetterSpacing(10); |
| 1021 | text_style2.setDecorationColor(SK_ColorBLACK); |
| 1022 | text_style2.setDecoration((TextDecoration)( |
| 1023 | TextDecoration::kUnderline | TextDecoration::kOverline | TextDecoration::kLineThrough)); |
| 1024 | text_style2.setWordSpacing(30); |
| 1025 | text_style2.setColor(SK_ColorGREEN); |
| 1026 | builder.pushStyle(text_style2); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1027 | builder.addText(text2, strlen(text2)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1028 | |
| 1029 | TextStyle text_style3; |
| 1030 | text_style3.setFontFamilies({SkString("Homemade Apple")}); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1031 | text_style3.setColor(SK_ColorBLACK); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1032 | builder.pushStyle(text_style3); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1033 | builder.addText(text3, strlen(text3)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1034 | |
| 1035 | TextStyle text_style4; |
| 1036 | text_style4.setFontFamilies({SkString("Roboto")}); |
| 1037 | text_style4.setFontSize(14); |
| 1038 | text_style4.setDecorationColor(SK_ColorBLACK); |
| 1039 | text_style4.setDecoration((TextDecoration)( |
| 1040 | TextDecoration::kUnderline | TextDecoration::kOverline | TextDecoration::kLineThrough)); |
| 1041 | text_style4.setColor(SK_ColorBLUE); |
| 1042 | builder.pushStyle(text_style4); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1043 | builder.addText(text4, strlen(text4)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1044 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1045 | builder.addText(text5, strlen(text5)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1046 | builder.pop(); |
| 1047 | |
| 1048 | auto paragraph = builder.Build(); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1049 | paragraph->layout(1000); |
| 1050 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1051 | |
Julia Lavrova | 3281b96 | 2019-12-02 11:32:25 -0500 | [diff] [blame] | 1052 | REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == 0); |
| 1053 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1054 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 1055 | REPORTER_ASSERT(reporter, impl->runs().size() == 4); |
| 1056 | REPORTER_ASSERT(reporter, impl->styles().size() == 4); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1057 | REPORTER_ASSERT(reporter, impl->lines().size() == 2); |
| 1058 | |
| 1059 | auto rects = paragraph->getRectsForRange(0, impl->text().size(), RectHeightStyle::kMax, RectWidthStyle::kTight); |
| 1060 | canvas.drawRects(SK_ColorMAGENTA, rects); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1061 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1062 | size_t index = 0; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1063 | impl->lines()[0].scanStyles( |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1064 | StyleType::kAllAttributes, |
| 1065 | [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1066 | switch (index) { |
| 1067 | case 0: |
| 1068 | REPORTER_ASSERT(reporter, style.equals(text_style1)); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1069 | REPORTER_ASSERT(reporter, equal(impl->text().begin(), textRange, text1)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1070 | break; |
| 1071 | case 1: |
| 1072 | REPORTER_ASSERT(reporter, style.equals(text_style2)); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1073 | REPORTER_ASSERT(reporter, equal(impl->text().begin(), textRange, text2)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1074 | break; |
| 1075 | case 2: |
| 1076 | REPORTER_ASSERT(reporter, style.equals(text_style3)); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1077 | REPORTER_ASSERT(reporter, equal(impl->text().begin(), textRange, text3)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1078 | break; |
| 1079 | case 3: |
| 1080 | REPORTER_ASSERT(reporter, style.equals(text_style4)); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1081 | REPORTER_ASSERT(reporter, equal(impl->text().begin(), textRange, text41)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1082 | break; |
| 1083 | default: |
| 1084 | REPORTER_ASSERT(reporter, false); |
| 1085 | break; |
| 1086 | } |
| 1087 | ++index; |
| 1088 | return true; |
| 1089 | }); |
| 1090 | impl->lines()[1].scanStyles( |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1091 | StyleType::kAllAttributes, |
| 1092 | [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1093 | switch (index) { |
| 1094 | case 4: |
| 1095 | REPORTER_ASSERT(reporter, style.equals(text_style4)); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1096 | REPORTER_ASSERT(reporter, equal(impl->text().begin(), textRange, text42)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1097 | break; |
| 1098 | default: |
| 1099 | REPORTER_ASSERT(reporter, false); |
| 1100 | break; |
| 1101 | } |
| 1102 | ++index; |
| 1103 | return true; |
| 1104 | }); |
| 1105 | REPORTER_ASSERT(reporter, index == 5); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1106 | } |
| 1107 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1108 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1109 | DEF_TEST(SkParagraph_DefaultStyleParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 1110 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1111 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1112 | TestCanvas canvas("SkParagraph_DefaultStyleParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1113 | const char* text = "No TextStyle! Uh Oh!"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1114 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1115 | |
| 1116 | ParagraphStyle paragraph_style; |
| 1117 | TextStyle defaultStyle; |
| 1118 | defaultStyle.setFontFamilies({SkString("Roboto")}); |
| 1119 | paragraph_style.setTextStyle(defaultStyle); |
| 1120 | paragraph_style.turnHintingOff(); |
| 1121 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1122 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1123 | |
| 1124 | auto paragraph = builder.Build(); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1125 | paragraph->layout(TestCanvasWidth); |
| 1126 | paragraph->paint(canvas.get(), 10.0, 15.0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1127 | |
Julia Lavrova | 3281b96 | 2019-12-02 11:32:25 -0500 | [diff] [blame] | 1128 | REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == 0); |
| 1129 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1130 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 1131 | |
| 1132 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 1133 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
| 1134 | REPORTER_ASSERT(reporter, impl->lines().size() == 1); |
| 1135 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1136 | size_t index = 0; |
| 1137 | impl->lines()[0].scanStyles( |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1138 | StyleType::kAllAttributes, |
| 1139 | [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1140 | REPORTER_ASSERT(reporter, style.equals(paragraph_style.getTextStyle())); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1141 | REPORTER_ASSERT(reporter, equal(impl->text().begin(), textRange, text)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1142 | ++index; |
| 1143 | return true; |
| 1144 | }); |
| 1145 | REPORTER_ASSERT(reporter, index == 1); |
| 1146 | } |
| 1147 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1148 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1149 | DEF_TEST(SkParagraph_BoldParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 1150 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1151 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1152 | TestCanvas canvas("SkParagraph_BoldParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1153 | const char* text = "This is Red max bold text!"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1154 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1155 | |
| 1156 | ParagraphStyle paragraph_style; |
| 1157 | paragraph_style.turnHintingOff(); |
| 1158 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1159 | |
| 1160 | TextStyle text_style; |
| 1161 | text_style.setFontFamilies({SkString("Roboto")}); |
| 1162 | text_style.setColor(SK_ColorRED); |
| 1163 | text_style.setFontSize(60); |
| 1164 | text_style.setLetterSpacing(0); |
| 1165 | text_style.setFontStyle(SkFontStyle(SkFontStyle::kBlack_Weight, SkFontStyle::kNormal_Width, |
| 1166 | SkFontStyle::kUpright_Slant)); |
| 1167 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1168 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1169 | builder.pop(); |
| 1170 | |
| 1171 | auto paragraph = builder.Build(); |
| 1172 | paragraph->layout(VeryLongCanvasWidth); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1173 | paragraph->paint(canvas.get(), 10.0, 60.0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1174 | |
Julia Lavrova | 3281b96 | 2019-12-02 11:32:25 -0500 | [diff] [blame] | 1175 | REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == 0); |
| 1176 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1177 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 1178 | |
| 1179 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 1180 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
| 1181 | REPORTER_ASSERT(reporter, impl->lines().size() == 1); |
| 1182 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1183 | size_t index = 0; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1184 | impl->lines()[0].scanStyles( |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1185 | StyleType::kAllAttributes, |
| 1186 | [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1187 | REPORTER_ASSERT(reporter, style.equals(text_style)); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1188 | REPORTER_ASSERT(reporter, equal(impl->text().begin(), textRange, text)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1189 | ++index; |
| 1190 | return true; |
| 1191 | }); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1192 | REPORTER_ASSERT(reporter, index == 1); |
| 1193 | } |
| 1194 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1195 | // Checked: NO DIFF (line height rounding error) |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1196 | DEF_TEST(SkParagraph_HeightOverrideParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 1197 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1198 | if (!fontCollection->fontsFound()) return; |
| 1199 | TestCanvas canvas("SkParagraph_HeightOverrideParagraph.png"); |
| 1200 | const char* text = "01234満毎冠行来昼本可\nabcd\n満毎冠行来昼本可"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1201 | const size_t len = strlen(text); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1202 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1203 | ParagraphStyle paragraph_style; |
| 1204 | paragraph_style.turnHintingOff(); |
| 1205 | paragraph_style.setMaxLines(10); |
| 1206 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1207 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1208 | TextStyle text_style; |
| 1209 | text_style.setFontFamilies({SkString("Roboto")}); |
| 1210 | text_style.setFontSize(20); |
| 1211 | text_style.setColor(SK_ColorBLACK); |
| 1212 | text_style.setHeight(3.6345f); |
| 1213 | text_style.setHeightOverride(true); |
| 1214 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1215 | builder.addText(text, len); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1216 | builder.pop(); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1217 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1218 | auto paragraph = builder.Build(); |
| 1219 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1220 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1221 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 1222 | REPORTER_ASSERT(reporter, impl->runs().size() == 5); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1223 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); // paragraph style does not count |
| 1224 | REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1225 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1226 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1227 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1228 | SkPaint paint; |
| 1229 | paint.setStyle(SkPaint::kStroke_Style); |
| 1230 | paint.setAntiAlias(true); |
| 1231 | paint.setStrokeWidth(1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1232 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1233 | // Tests for GetRectsForRange() |
| 1234 | RectHeightStyle rect_height_style = RectHeightStyle::kIncludeLineSpacingMiddle; |
| 1235 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 1236 | paint.setColor(SK_ColorRED); |
| 1237 | std::vector<TextBox> boxes = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style); |
| 1238 | canvas.drawRects(SK_ColorRED, boxes); |
| 1239 | REPORTER_ASSERT(reporter, boxes.size() == 0ull); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1240 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1241 | boxes = paragraph->getRectsForRange(0, 40, rect_height_style, rect_width_style); |
| 1242 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 1243 | REPORTER_ASSERT(reporter, boxes.size() == 3ull); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1244 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1245 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.left(), 0, EPSILON100)); |
| 1246 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.top(), 92.805f, EPSILON5)); |
| 1247 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.right(), 43.843f, EPSILON100)); |
| 1248 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.bottom(), 165.495f, EPSILON5)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1249 | } |
| 1250 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1251 | // Checked: DIFF+ |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1252 | DEF_TEST(SkParagraph_LeftAlignParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 1253 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1254 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1255 | TestCanvas canvas("SkParagraph_LeftAlignParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1256 | const char* text = |
| 1257 | "This is a very long sentence to test if the text will properly wrap " |
| 1258 | "around and go to the next line. Sometimes, short sentence. Longer " |
| 1259 | "sentences are okay too because they are nessecary. Very short. " |
| 1260 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod " |
| 1261 | "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim " |
| 1262 | "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea " |
| 1263 | "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate " |
| 1264 | "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint " |
| 1265 | "occaecat cupidatat non proident, sunt in culpa qui officia deserunt " |
| 1266 | "mollit anim id est laborum. " |
| 1267 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod " |
| 1268 | "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim " |
| 1269 | "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea " |
| 1270 | "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate " |
| 1271 | "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint " |
| 1272 | "occaecat cupidatat non proident, sunt in culpa qui officia deserunt " |
| 1273 | "mollit anim id est laborum."; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1274 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1275 | |
| 1276 | ParagraphStyle paragraph_style; |
| 1277 | paragraph_style.setMaxLines(14); |
| 1278 | paragraph_style.setTextAlign(TextAlign::kLeft); |
| 1279 | paragraph_style.turnHintingOff(); |
| 1280 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1281 | |
| 1282 | TextStyle text_style; |
| 1283 | text_style.setFontFamilies({SkString("Roboto")}); |
| 1284 | text_style.setFontSize(26); |
| 1285 | text_style.setLetterSpacing(1); |
| 1286 | text_style.setWordSpacing(5); |
| 1287 | text_style.setColor(SK_ColorBLACK); |
| 1288 | text_style.setHeight(1); |
| 1289 | text_style.setDecoration(TextDecoration::kUnderline); |
| 1290 | text_style.setDecorationColor(SK_ColorBLACK); |
| 1291 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1292 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1293 | builder.pop(); |
| 1294 | |
| 1295 | auto paragraph = builder.Build(); |
| 1296 | paragraph->layout(TestCanvasWidth - 100); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1297 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1298 | |
| 1299 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1300 | |
| 1301 | REPORTER_ASSERT(reporter, impl->text().size() == std::string{text}.length()); |
| 1302 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 1303 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 1304 | REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1305 | REPORTER_ASSERT(reporter, impl->lines().size() == paragraph_style.getMaxLines()); |
| 1306 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1307 | double expected_y = 0; |
| 1308 | double epsilon = 0.01f; |
| 1309 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].baseline(), 24.121f, epsilon)); |
| 1310 | REPORTER_ASSERT(reporter, |
| 1311 | SkScalarNearlyEqual(impl->lines()[0].offset().fY, expected_y, epsilon)); |
| 1312 | expected_y += 30; |
| 1313 | REPORTER_ASSERT(reporter, |
| 1314 | SkScalarNearlyEqual(impl->lines()[1].offset().fY, expected_y, epsilon)); |
| 1315 | expected_y += 30; |
| 1316 | REPORTER_ASSERT(reporter, |
| 1317 | SkScalarNearlyEqual(impl->lines()[2].offset().fY, expected_y, epsilon)); |
| 1318 | expected_y += 30; |
| 1319 | REPORTER_ASSERT(reporter, |
| 1320 | SkScalarNearlyEqual(impl->lines()[3].offset().fY, expected_y, epsilon)); |
| 1321 | expected_y += 30 * 10; |
| 1322 | REPORTER_ASSERT(reporter, |
| 1323 | SkScalarNearlyEqual(impl->lines()[13].offset().fY, expected_y, epsilon)); |
| 1324 | |
| 1325 | REPORTER_ASSERT(reporter, |
| 1326 | paragraph_style.getTextAlign() == impl->paragraphStyle().getTextAlign()); |
| 1327 | |
| 1328 | // Tests for GetGlyphPositionAtCoordinate() |
| 1329 | REPORTER_ASSERT(reporter, impl->getGlyphPositionAtCoordinate(0, 0).position == 0); |
| 1330 | REPORTER_ASSERT(reporter, impl->getGlyphPositionAtCoordinate(1, 1).position == 0); |
| 1331 | REPORTER_ASSERT(reporter, impl->getGlyphPositionAtCoordinate(1, 35).position == 68); |
| 1332 | REPORTER_ASSERT(reporter, impl->getGlyphPositionAtCoordinate(1, 70).position == 134); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 1333 | REPORTER_ASSERT(reporter, impl->getGlyphPositionAtCoordinate(2000, 35).position == 134);// |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1334 | } |
| 1335 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1336 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1337 | DEF_TEST(SkParagraph_RightAlignParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 1338 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1339 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1340 | TestCanvas canvas("SkParagraph_RightAlignParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1341 | const char* text = |
| 1342 | "This is a very long sentence to test if the text will properly wrap " |
| 1343 | "around and go to the next line. Sometimes, short sentence. Longer " |
| 1344 | "sentences are okay too because they are nessecary. Very short. " |
| 1345 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod " |
| 1346 | "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim " |
| 1347 | "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea " |
| 1348 | "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate " |
| 1349 | "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint " |
| 1350 | "occaecat cupidatat non proident, sunt in culpa qui officia deserunt " |
| 1351 | "mollit anim id est laborum. " |
| 1352 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod " |
| 1353 | "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim " |
| 1354 | "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea " |
| 1355 | "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate " |
| 1356 | "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint " |
| 1357 | "occaecat cupidatat non proident, sunt in culpa qui officia deserunt " |
| 1358 | "mollit anim id est laborum."; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1359 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1360 | |
| 1361 | ParagraphStyle paragraph_style; |
| 1362 | paragraph_style.setMaxLines(14); |
| 1363 | paragraph_style.setTextAlign(TextAlign::kRight); |
| 1364 | paragraph_style.turnHintingOff(); |
| 1365 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1366 | |
| 1367 | TextStyle text_style; |
| 1368 | text_style.setFontFamilies({SkString("Roboto")}); |
| 1369 | text_style.setFontSize(26); |
| 1370 | text_style.setLetterSpacing(1); |
| 1371 | text_style.setWordSpacing(5); |
| 1372 | text_style.setColor(SK_ColorBLACK); |
| 1373 | text_style.setHeight(1); |
| 1374 | text_style.setDecoration(TextDecoration::kUnderline); |
| 1375 | text_style.setDecorationColor(SK_ColorBLACK); |
| 1376 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1377 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1378 | builder.pop(); |
| 1379 | |
| 1380 | auto paragraph = builder.Build(); |
| 1381 | paragraph->layout(TestCanvasWidth - 100); |
| 1382 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1383 | paragraph->paint(canvas.get(), 0, 0); |
| 1384 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1385 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1386 | |
| 1387 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 1388 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 1389 | REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1390 | REPORTER_ASSERT(reporter, impl->lines().size() == paragraph_style.getMaxLines()); |
| 1391 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1392 | double expected_y = 0; |
| 1393 | double epsilon = 0.01f; |
| 1394 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].baseline(), 24.121f, epsilon)); |
| 1395 | REPORTER_ASSERT(reporter, |
| 1396 | SkScalarNearlyEqual(impl->lines()[0].offset().fY, expected_y, epsilon)); |
| 1397 | expected_y += 30; |
| 1398 | REPORTER_ASSERT(reporter, |
| 1399 | SkScalarNearlyEqual(impl->lines()[1].offset().fY, expected_y, epsilon)); |
| 1400 | expected_y += 30; |
| 1401 | REPORTER_ASSERT(reporter, |
| 1402 | SkScalarNearlyEqual(impl->lines()[2].offset().fY, expected_y, epsilon)); |
| 1403 | expected_y += 30; |
| 1404 | REPORTER_ASSERT(reporter, |
| 1405 | SkScalarNearlyEqual(impl->lines()[3].offset().fY, expected_y, epsilon)); |
| 1406 | expected_y += 30 * 10; |
| 1407 | REPORTER_ASSERT(reporter, |
| 1408 | SkScalarNearlyEqual(impl->lines()[13].offset().fY, expected_y, epsilon)); |
| 1409 | |
| 1410 | auto calculate = [](const TextLine& line) -> SkScalar { |
| 1411 | return TestCanvasWidth - 100 - line.offset().fX - line.width(); |
| 1412 | }; |
| 1413 | |
| 1414 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[0]), 0, epsilon)); |
| 1415 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[1]), 0, epsilon)); |
| 1416 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[2]), 0, epsilon)); |
| 1417 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[3]), 0, epsilon)); |
| 1418 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[13]), 0, epsilon)); |
| 1419 | |
| 1420 | REPORTER_ASSERT(reporter, |
| 1421 | paragraph_style.getTextAlign() == impl->paragraphStyle().getTextAlign()); |
| 1422 | } |
| 1423 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1424 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1425 | DEF_TEST(SkParagraph_CenterAlignParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 1426 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1427 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1428 | TestCanvas canvas("SkParagraph_CenterAlignParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1429 | const char* text = |
| 1430 | "This is a very long sentence to test if the text will properly wrap " |
| 1431 | "around and go to the next line. Sometimes, short sentence. Longer " |
| 1432 | "sentences are okay too because they are nessecary. Very short. " |
| 1433 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod " |
| 1434 | "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim " |
| 1435 | "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea " |
| 1436 | "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate " |
| 1437 | "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint " |
| 1438 | "occaecat cupidatat non proident, sunt in culpa qui officia deserunt " |
| 1439 | "mollit anim id est laborum. " |
| 1440 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod " |
| 1441 | "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim " |
| 1442 | "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea " |
| 1443 | "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate " |
| 1444 | "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint " |
| 1445 | "occaecat cupidatat non proident, sunt in culpa qui officia deserunt " |
| 1446 | "mollit anim id est laborum."; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1447 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1448 | |
| 1449 | ParagraphStyle paragraph_style; |
| 1450 | paragraph_style.setMaxLines(14); |
| 1451 | paragraph_style.setTextAlign(TextAlign::kCenter); |
| 1452 | paragraph_style.turnHintingOff(); |
| 1453 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1454 | |
| 1455 | TextStyle text_style; |
| 1456 | text_style.setFontFamilies({SkString("Roboto")}); |
| 1457 | text_style.setFontSize(26); |
| 1458 | text_style.setLetterSpacing(1); |
| 1459 | text_style.setWordSpacing(5); |
| 1460 | text_style.setColor(SK_ColorBLACK); |
| 1461 | text_style.setHeight(1); |
| 1462 | text_style.setDecoration(TextDecoration::kUnderline); |
| 1463 | text_style.setDecorationColor(SK_ColorBLACK); |
| 1464 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1465 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1466 | builder.pop(); |
| 1467 | |
| 1468 | auto paragraph = builder.Build(); |
| 1469 | paragraph->layout(TestCanvasWidth - 100); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1470 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1471 | |
| 1472 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1473 | |
| 1474 | REPORTER_ASSERT(reporter, impl->text().size() == std::string{text}.length()); |
| 1475 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 1476 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 1477 | REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1478 | REPORTER_ASSERT(reporter, impl->lines().size() == paragraph_style.getMaxLines()); |
| 1479 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1480 | double expected_y = 0; |
| 1481 | double epsilon = 0.01f; |
| 1482 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].baseline(), 24.121f, epsilon)); |
| 1483 | REPORTER_ASSERT(reporter, |
| 1484 | SkScalarNearlyEqual(impl->lines()[0].offset().fY, expected_y, epsilon)); |
| 1485 | expected_y += 30; |
| 1486 | REPORTER_ASSERT(reporter, |
| 1487 | SkScalarNearlyEqual(impl->lines()[1].offset().fY, expected_y, epsilon)); |
| 1488 | expected_y += 30; |
| 1489 | REPORTER_ASSERT(reporter, |
| 1490 | SkScalarNearlyEqual(impl->lines()[2].offset().fY, expected_y, epsilon)); |
| 1491 | expected_y += 30; |
| 1492 | REPORTER_ASSERT(reporter, |
| 1493 | SkScalarNearlyEqual(impl->lines()[3].offset().fY, expected_y, epsilon)); |
| 1494 | expected_y += 30 * 10; |
| 1495 | REPORTER_ASSERT(reporter, |
| 1496 | SkScalarNearlyEqual(impl->lines()[13].offset().fY, expected_y, epsilon)); |
| 1497 | |
| 1498 | auto calculate = [](const TextLine& line) -> SkScalar { |
| 1499 | return TestCanvasWidth - 100 - (line.offset().fX * 2 + line.width()); |
| 1500 | }; |
| 1501 | |
| 1502 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[0]), 0, epsilon)); |
| 1503 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[1]), 0, epsilon)); |
| 1504 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[2]), 0, epsilon)); |
| 1505 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[3]), 0, epsilon)); |
| 1506 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[13]), 0, epsilon)); |
| 1507 | |
| 1508 | REPORTER_ASSERT(reporter, |
| 1509 | paragraph_style.getTextAlign() == impl->paragraphStyle().getTextAlign()); |
| 1510 | } |
| 1511 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1512 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1513 | DEF_TEST(SkParagraph_JustifyAlignParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 1514 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1515 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1516 | TestCanvas canvas("SkParagraph_JustifyAlignParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1517 | const char* text = |
| 1518 | "This is a very long sentence to test if the text will properly wrap " |
| 1519 | "around and go to the next line. Sometimes, short sentence. Longer " |
| 1520 | "sentences are okay too because they are nessecary. Very short. " |
| 1521 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod " |
| 1522 | "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim " |
| 1523 | "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea " |
| 1524 | "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate " |
| 1525 | "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint " |
| 1526 | "occaecat cupidatat non proident, sunt in culpa qui officia deserunt " |
| 1527 | "mollit anim id est laborum. " |
| 1528 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod " |
| 1529 | "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim " |
| 1530 | "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea " |
| 1531 | "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate " |
| 1532 | "velit esse cillum dolore eu fugiat."; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1533 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1534 | |
| 1535 | ParagraphStyle paragraph_style; |
| 1536 | paragraph_style.setMaxLines(14); |
| 1537 | paragraph_style.setTextAlign(TextAlign::kJustify); |
| 1538 | paragraph_style.turnHintingOff(); |
| 1539 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1540 | |
| 1541 | TextStyle text_style; |
| 1542 | text_style.setFontFamilies({SkString("Roboto")}); |
| 1543 | text_style.setFontSize(26); |
| 1544 | text_style.setLetterSpacing(0); |
| 1545 | text_style.setWordSpacing(5); |
| 1546 | text_style.setColor(SK_ColorBLACK); |
| 1547 | text_style.setHeight(1); |
| 1548 | text_style.setDecoration(TextDecoration::kUnderline); |
| 1549 | text_style.setDecorationColor(SK_ColorBLACK); |
| 1550 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1551 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1552 | builder.pop(); |
| 1553 | |
| 1554 | auto paragraph = builder.Build(); |
| 1555 | paragraph->layout(TestCanvasWidth - 100); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1556 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1557 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1558 | RectHeightStyle rect_height_style = RectHeightStyle::kMax; |
| 1559 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 1560 | auto boxes = paragraph->getRectsForRange(0, 100, rect_height_style, rect_width_style); |
| 1561 | canvas.drawRects(SK_ColorRED, boxes); |
| 1562 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1563 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1564 | |
| 1565 | REPORTER_ASSERT(reporter, impl->text().size() == std::string{text}.length()); |
| 1566 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 1567 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 1568 | REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1569 | |
| 1570 | double expected_y = 0; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1571 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].baseline(), 24.121f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1572 | REPORTER_ASSERT(reporter, |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1573 | SkScalarNearlyEqual(impl->lines()[0].offset().fY, expected_y, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1574 | expected_y += 30; |
| 1575 | REPORTER_ASSERT(reporter, |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1576 | SkScalarNearlyEqual(impl->lines()[1].offset().fY, expected_y, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1577 | expected_y += 30; |
| 1578 | REPORTER_ASSERT(reporter, |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1579 | SkScalarNearlyEqual(impl->lines()[2].offset().fY, expected_y, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1580 | expected_y += 30; |
| 1581 | REPORTER_ASSERT(reporter, |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1582 | SkScalarNearlyEqual(impl->lines()[3].offset().fY, expected_y, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1583 | expected_y += 30 * 9; |
| 1584 | REPORTER_ASSERT(reporter, |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1585 | SkScalarNearlyEqual(impl->lines()[12].offset().fY, expected_y, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1586 | |
| 1587 | auto calculate = [](const TextLine& line) -> SkScalar { |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 1588 | return line.offset().fX; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1589 | }; |
| 1590 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1591 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[0]), 0, EPSILON100)); |
| 1592 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[1]), 0, EPSILON100)); |
| 1593 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[2]), 0, EPSILON100)); |
| 1594 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[3]), 0, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1595 | |
| 1596 | REPORTER_ASSERT(reporter, |
| 1597 | paragraph_style.getTextAlign() == impl->paragraphStyle().getTextAlign()); |
| 1598 | } |
| 1599 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1600 | // Checked: DIFF (ghost spaces as a separate box in TxtLib) |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1601 | DEF_TEST(SkParagraph_JustifyRTL, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 1602 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1603 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1604 | TestCanvas canvas("SkParagraph_JustifyRTL.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1605 | const char* text = |
| 1606 | "אאא בּבּבּבּ אאאא בּבּ אאא בּבּבּ אאאאא בּבּבּבּ אאאא בּבּבּבּבּ " |
| 1607 | "אאאאא בּבּבּבּבּ אאאבּבּבּבּבּבּאאאאא בּבּבּבּבּבּאאאאאבּבּבּבּבּבּ אאאאא בּבּבּבּבּ " |
| 1608 | "אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1609 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1610 | |
| 1611 | ParagraphStyle paragraph_style; |
| 1612 | paragraph_style.setMaxLines(14); |
| 1613 | paragraph_style.setTextAlign(TextAlign::kJustify); |
| 1614 | paragraph_style.turnHintingOff(); |
| 1615 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1616 | |
| 1617 | TextStyle text_style; |
| 1618 | text_style.setFontFamilies({SkString("Ahem")}); |
| 1619 | text_style.setFontSize(26); |
| 1620 | text_style.setColor(SK_ColorBLACK); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1621 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1622 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1623 | builder.pop(); |
| 1624 | |
| 1625 | auto paragraph = builder.Build(); |
| 1626 | paragraph->layout(TestCanvasWidth - 100); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1627 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1628 | |
| 1629 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1630 | |
| 1631 | auto calculate = [](const TextLine& line) -> SkScalar { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1632 | return TestCanvasWidth - 100 - line.width(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1633 | }; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1634 | for (auto& line : impl->lines()) { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1635 | if (&line == &impl->lines().back()) { |
| 1636 | REPORTER_ASSERT(reporter, calculate(line) > EPSILON100); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1637 | } else { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1638 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(line), 0, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1639 | } |
| 1640 | } |
| 1641 | |
| 1642 | // Just make sure the the text is actually RTL |
| 1643 | for (auto& run : impl->runs()) { |
| 1644 | REPORTER_ASSERT(reporter, !run.leftToRight()); |
| 1645 | } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1646 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1647 | // Tests for GetRectsForRange() |
| 1648 | RectHeightStyle rect_height_style = RectHeightStyle::kMax; |
| 1649 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 1650 | auto boxes = paragraph->getRectsForRange(0, 100, rect_height_style, rect_width_style); |
| 1651 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 1652 | REPORTER_ASSERT(reporter, boxes.size() == 5); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1653 | |
| 1654 | boxes = paragraph->getRectsForRange(240, 250, rect_height_style, rect_width_style); |
| 1655 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 1656 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 1657 | |
| 1658 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 588, EPSILON100)); |
| 1659 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 130, EPSILON100)); |
| 1660 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 640, EPSILON100)); |
| 1661 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 156, EPSILON100)); |
| 1662 | } |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1663 | |
Julia Lavrova | 90bfd1c | 2019-12-04 11:43:32 -0500 | [diff] [blame] | 1664 | DEF_TEST_DISABLED(SkParagraph_JustifyRTLNewLine, reporter) { |
| 1665 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true); |
| 1666 | if (!fontCollection->fontsFound()) return; |
| 1667 | TestCanvas canvas("SkParagraph_JustifyRTLNewLine.png"); |
| 1668 | const char* text = |
| 1669 | "אאא בּבּבּבּ אאאא\nבּבּ אאא בּבּבּ אאאאא בּבּבּבּ אאאא בּבּבּבּבּ " |
| 1670 | "אאאאא בּבּבּבּבּ אאאבּבּבּבּבּבּאאאאא בּבּבּבּבּבּאאאאאבּבּבּבּבּבּ אאאאא בּבּבּבּבּ " |
| 1671 | "אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ"; |
| 1672 | |
| 1673 | auto icu_text = icu::UnicodeString::fromUTF8(text); |
| 1674 | std::u16string u16_text(icu_text.getBuffer(), icu_text.getBuffer() + icu_text.length()); |
| 1675 | const size_t len = strlen(text); |
| 1676 | |
| 1677 | ParagraphStyle paragraph_style; |
| 1678 | paragraph_style.setMaxLines(14); |
| 1679 | paragraph_style.setTextAlign(TextAlign::kJustify); |
| 1680 | paragraph_style.setTextDirection(TextDirection::kRtl); |
| 1681 | paragraph_style.turnHintingOff(); |
| 1682 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1683 | |
| 1684 | TextStyle text_style; |
| 1685 | text_style.setFontFamilies({SkString("Ahem")}); |
| 1686 | text_style.setFontSize(26); |
| 1687 | text_style.setColor(SK_ColorBLACK); |
| 1688 | builder.pushStyle(text_style); |
| 1689 | builder.addText(text, len); |
| 1690 | builder.pop(); |
| 1691 | |
| 1692 | auto paragraph = builder.Build(); |
| 1693 | paragraph->layout(TestCanvasWidth - 100); |
| 1694 | paragraph->paint(canvas.get(), 0, 0); |
| 1695 | |
| 1696 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 1697 | |
| 1698 | SkPaint paint; |
| 1699 | paint.setStyle(SkPaint::kStroke_Style); |
| 1700 | paint.setAntiAlias(true); |
| 1701 | paint.setStrokeWidth(1); |
| 1702 | |
| 1703 | // Tests for GetRectsForRange() |
| 1704 | RectHeightStyle rect_height_style = RectHeightStyle::kMax; |
| 1705 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 1706 | paint.setColor(SK_ColorRED); |
| 1707 | auto boxes = paragraph->getRectsForRange(0, 30, rect_height_style, rect_width_style); |
| 1708 | for (size_t i = 0; i < boxes.size(); ++i) { |
| 1709 | canvas.get()->drawRect(boxes[i].rect, paint); |
| 1710 | } |
| 1711 | REPORTER_ASSERT(reporter, boxes.size() == 2ull); |
| 1712 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 562, EPSILON100)); |
| 1713 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100)); |
| 1714 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 900, EPSILON100)); |
| 1715 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 26, EPSILON100)); |
| 1716 | |
| 1717 | paint.setColor(SK_ColorBLUE); |
| 1718 | boxes = paragraph->getRectsForRange(240, 250, rect_height_style, rect_width_style); |
| 1719 | for (size_t i = 0; i < boxes.size(); ++i) { |
| 1720 | canvas.get()->drawRect(boxes[i].rect, paint); |
| 1721 | } |
| 1722 | REPORTER_ASSERT(reporter, boxes.size() == 1ull); |
| 1723 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 68, EPSILON100)); |
| 1724 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 130, EPSILON100)); |
| 1725 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 120, EPSILON100)); |
| 1726 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 156, EPSILON100)); |
| 1727 | |
| 1728 | // All lines should be justified to the width of the |
| 1729 | // paragraph. |
| 1730 | for (auto& line : impl->lines()) { |
| 1731 | REPORTER_ASSERT(reporter, |
| 1732 | SkScalarNearlyEqual(line.width(), TestCanvasWidth - 100, EPSILON100)); |
| 1733 | } |
| 1734 | } |
| 1735 | |
| 1736 | DEF_TEST_DISABLED(SkParagraph_LeadingSpaceRTL, reporter) { |
| 1737 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true); |
| 1738 | if (!fontCollection->fontsFound()) return; |
| 1739 | TestCanvas canvas("SkParagraph_LeadingSpaceRTL.png"); |
| 1740 | |
| 1741 | const char* text = " leading space"; |
| 1742 | |
| 1743 | auto icu_text = icu::UnicodeString::fromUTF8(text); |
| 1744 | std::u16string u16_text(icu_text.getBuffer(), icu_text.getBuffer() + icu_text.length()); |
| 1745 | const size_t len = strlen(text); |
| 1746 | |
| 1747 | ParagraphStyle paragraph_style; |
| 1748 | paragraph_style.setMaxLines(14); |
| 1749 | paragraph_style.setTextAlign(TextAlign::kJustify); |
| 1750 | paragraph_style.setTextDirection(TextDirection::kRtl); |
| 1751 | paragraph_style.turnHintingOff(); |
| 1752 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1753 | |
| 1754 | TextStyle text_style; |
| 1755 | text_style.setFontFamilies({SkString("Ahem")}); |
| 1756 | text_style.setFontSize(26); |
| 1757 | text_style.setColor(SK_ColorBLACK); |
| 1758 | builder.pushStyle(text_style); |
| 1759 | builder.addText(text, len); |
| 1760 | builder.pop(); |
| 1761 | |
| 1762 | auto paragraph = builder.Build(); |
| 1763 | paragraph->layout(TestCanvasWidth - 100); |
| 1764 | paragraph->paint(canvas.get(), 0, 0); |
| 1765 | |
| 1766 | SkPaint paint; |
| 1767 | paint.setStyle(SkPaint::kStroke_Style); |
| 1768 | paint.setAntiAlias(true); |
| 1769 | paint.setStrokeWidth(1); |
| 1770 | |
| 1771 | // Tests for GetRectsForRange() |
| 1772 | RectHeightStyle rect_height_style = RectHeightStyle::kMax; |
| 1773 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 1774 | paint.setColor(SK_ColorRED); |
| 1775 | auto boxes = paragraph->getRectsForRange(0, 100, rect_height_style, rect_width_style); |
| 1776 | for (size_t i = 0; i < boxes.size(); ++i) { |
| 1777 | canvas.get()->drawRect(boxes[i].rect, paint); |
| 1778 | } |
| 1779 | REPORTER_ASSERT(reporter, boxes.size() == 2ull); |
| 1780 | } |
| 1781 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1782 | // Checked: NO DIFF (some minor decoration differences, probably) |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1783 | DEF_TEST(SkParagraph_DecorationsParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 1784 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1785 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1786 | TestCanvas canvas("SkParagraph_DecorationsParagraph.png"); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1787 | const char* text1 = "This text should be"; |
| 1788 | const char* text2 = " decorated even when"; |
| 1789 | const char* text3 = " wrapped around to"; |
| 1790 | const char* text4 = " the next line."; |
| 1791 | const char* text5 = " Otherwise, bad things happen."; |
| 1792 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1793 | ParagraphStyle paragraph_style; |
| 1794 | paragraph_style.setMaxLines(14); |
| 1795 | paragraph_style.setTextAlign(TextAlign::kLeft); |
| 1796 | paragraph_style.turnHintingOff(); |
| 1797 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1798 | |
| 1799 | TextStyle text_style; |
| 1800 | text_style.setFontFamilies({SkString("Roboto")}); |
| 1801 | text_style.setFontSize(26); |
| 1802 | text_style.setLetterSpacing(0); |
| 1803 | text_style.setWordSpacing(5); |
| 1804 | text_style.setColor(SK_ColorBLACK); |
| 1805 | text_style.setHeight(2); |
| 1806 | text_style.setDecoration(TextDecoration::kUnderline); |
| 1807 | text_style.setDecorationColor(SK_ColorBLACK); |
| 1808 | text_style.setDecoration((TextDecoration)( |
| 1809 | TextDecoration::kUnderline | TextDecoration::kOverline | TextDecoration::kLineThrough)); |
| 1810 | text_style.setDecorationStyle(TextDecorationStyle::kSolid); |
| 1811 | text_style.setDecorationColor(SK_ColorBLACK); |
| 1812 | text_style.setDecorationThicknessMultiplier(2.0); |
| 1813 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1814 | builder.addText(text1, strlen(text1)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1815 | |
| 1816 | text_style.setDecorationStyle(TextDecorationStyle::kDouble); |
| 1817 | text_style.setDecorationColor(SK_ColorBLUE); |
| 1818 | text_style.setDecorationThicknessMultiplier(1.0); |
| 1819 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1820 | builder.addText(text2, strlen(text2)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1821 | |
| 1822 | text_style.setDecorationStyle(TextDecorationStyle::kDotted); |
| 1823 | text_style.setDecorationColor(SK_ColorBLACK); |
| 1824 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1825 | builder.addText(text3, strlen(text3)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1826 | |
| 1827 | text_style.setDecorationStyle(TextDecorationStyle::kDashed); |
| 1828 | text_style.setDecorationColor(SK_ColorBLACK); |
| 1829 | text_style.setDecorationThicknessMultiplier(3.0); |
| 1830 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1831 | builder.addText(text4, strlen(text4)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1832 | |
| 1833 | text_style.setDecorationStyle(TextDecorationStyle::kWavy); |
| 1834 | text_style.setDecorationColor(SK_ColorRED); |
| 1835 | text_style.setDecorationThicknessMultiplier(1.0); |
| 1836 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1837 | builder.addText(text5, strlen(text5)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1838 | builder.pop(); |
| 1839 | |
| 1840 | auto paragraph = builder.Build(); |
| 1841 | paragraph->layout(TestCanvasWidth - 100); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1842 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1843 | |
| 1844 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1845 | |
| 1846 | size_t index = 0; |
| 1847 | for (auto& line : impl->lines()) { |
| 1848 | line.scanStyles( |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1849 | StyleType::kDecorations, |
| 1850 | [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1851 | auto decoration = (TextDecoration)(TextDecoration::kUnderline | |
| 1852 | TextDecoration::kOverline | |
| 1853 | TextDecoration::kLineThrough); |
| 1854 | REPORTER_ASSERT(reporter, style.getDecorationType() == decoration); |
| 1855 | switch (index) { |
| 1856 | case 0: |
| 1857 | REPORTER_ASSERT(reporter, style.getDecorationStyle() == |
| 1858 | TextDecorationStyle::kSolid); |
| 1859 | REPORTER_ASSERT(reporter, style.getDecorationColor() == SK_ColorBLACK); |
| 1860 | REPORTER_ASSERT(reporter, |
| 1861 | style.getDecorationThicknessMultiplier() == 2.0); |
| 1862 | break; |
| 1863 | case 1: // The style appears on 2 lines so it has 2 pieces |
| 1864 | REPORTER_ASSERT(reporter, style.getDecorationStyle() == |
| 1865 | TextDecorationStyle::kDouble); |
| 1866 | REPORTER_ASSERT(reporter, style.getDecorationColor() == SK_ColorBLUE); |
| 1867 | REPORTER_ASSERT(reporter, |
| 1868 | style.getDecorationThicknessMultiplier() == 1.0); |
| 1869 | break; |
| 1870 | case 2: |
| 1871 | REPORTER_ASSERT(reporter, style.getDecorationStyle() == |
| 1872 | TextDecorationStyle::kDotted); |
| 1873 | REPORTER_ASSERT(reporter, style.getDecorationColor() == SK_ColorBLACK); |
| 1874 | REPORTER_ASSERT(reporter, |
| 1875 | style.getDecorationThicknessMultiplier() == 1.0); |
| 1876 | break; |
| 1877 | case 3: |
| 1878 | case 4: |
| 1879 | REPORTER_ASSERT(reporter, style.getDecorationStyle() == |
| 1880 | TextDecorationStyle::kDashed); |
| 1881 | REPORTER_ASSERT(reporter, style.getDecorationColor() == SK_ColorBLACK); |
| 1882 | REPORTER_ASSERT(reporter, |
| 1883 | style.getDecorationThicknessMultiplier() == 3.0); |
| 1884 | break; |
| 1885 | case 5: |
| 1886 | REPORTER_ASSERT(reporter, style.getDecorationStyle() == |
| 1887 | TextDecorationStyle::kWavy); |
| 1888 | REPORTER_ASSERT(reporter, style.getDecorationColor() == SK_ColorRED); |
| 1889 | REPORTER_ASSERT(reporter, |
| 1890 | style.getDecorationThicknessMultiplier() == 1.0); |
| 1891 | break; |
| 1892 | default: |
| 1893 | REPORTER_ASSERT(reporter, false); |
| 1894 | break; |
| 1895 | } |
| 1896 | ++index; |
| 1897 | return true; |
| 1898 | }); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1899 | } |
| 1900 | } |
| 1901 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1902 | DEF_TEST(SkParagraph_WavyDecorationParagraph, reporter) { |
Julia Lavrova | 90bfd1c | 2019-12-04 11:43:32 -0500 | [diff] [blame] | 1903 | SkDebugf("TODO: Add test for wavy decorations\n"); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1904 | } |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1905 | |
| 1906 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1907 | DEF_TEST(SkParagraph_ItalicsParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 1908 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1909 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1910 | TestCanvas canvas("SkParagraph_ItalicsParagraph.png"); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1911 | const char* text1 = "No italic "; |
| 1912 | const char* text2 = "Yes Italic "; |
| 1913 | const char* text3 = "No Italic again."; |
| 1914 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1915 | ParagraphStyle paragraph_style; |
| 1916 | paragraph_style.turnHintingOff(); |
| 1917 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1918 | |
| 1919 | TextStyle text_style; |
| 1920 | text_style.setFontFamilies({SkString("Roboto")}); |
| 1921 | text_style.setFontSize(10); |
| 1922 | text_style.setColor(SK_ColorRED); |
| 1923 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1924 | builder.addText(text1, strlen(text1)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1925 | |
| 1926 | text_style.setFontStyle(SkFontStyle(SkFontStyle::kNormal_Weight, SkFontStyle::kNormal_Width, |
| 1927 | SkFontStyle::kItalic_Slant)); |
| 1928 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1929 | builder.addText(text2, strlen(text2)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1930 | builder.pop(); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1931 | builder.addText(text3, strlen(text3)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1932 | |
| 1933 | auto paragraph = builder.Build(); |
| 1934 | paragraph->layout(TestCanvasWidth); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1935 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1936 | |
| 1937 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1938 | |
| 1939 | REPORTER_ASSERT(reporter, impl->runs().size() == 3); |
| 1940 | REPORTER_ASSERT(reporter, impl->styles().size() == 3); |
| 1941 | REPORTER_ASSERT(reporter, impl->lines().size() == 1); |
| 1942 | auto& line = impl->lines()[0]; |
| 1943 | size_t index = 0; |
| 1944 | line.scanStyles( |
| 1945 | StyleType::kForeground, |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1946 | [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1947 | switch (index) { |
| 1948 | case 0: |
| 1949 | REPORTER_ASSERT( |
| 1950 | reporter, |
| 1951 | style.getFontStyle().slant() == SkFontStyle::kUpright_Slant); |
| 1952 | break; |
| 1953 | case 1: |
| 1954 | REPORTER_ASSERT(reporter, |
| 1955 | style.getFontStyle().slant() == SkFontStyle::kItalic_Slant); |
| 1956 | break; |
| 1957 | case 2: |
| 1958 | REPORTER_ASSERT( |
| 1959 | reporter, |
| 1960 | style.getFontStyle().slant() == SkFontStyle::kUpright_Slant); |
| 1961 | break; |
| 1962 | default: |
| 1963 | REPORTER_ASSERT(reporter, false); |
| 1964 | break; |
| 1965 | } |
| 1966 | ++index; |
| 1967 | return true; |
| 1968 | }); |
| 1969 | } |
| 1970 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1971 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1972 | DEF_TEST(SkParagraph_ChineseParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 1973 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1974 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1975 | TestCanvas canvas("SkParagraph_ChineseParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1976 | const char* text = |
| 1977 | "左線読設重説切後碁給能上目秘使約。満毎冠行来昼本可必図将発確年。今属場育" |
| 1978 | "図情闘陰野高備込制詩西校客。審対江置講今固残必託地集済決維駆年策。立得庭" |
| 1979 | "際輝求佐抗蒼提夜合逃表。注統天言件自謙雅載報紙喪。作画稿愛器灯女書利変探" |
| 1980 | "訃第金線朝開化建。子戦年帝励害表月幕株漠新期刊人秘。図的海力生禁挙保天戦" |
| 1981 | "聞条年所在口。"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1982 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1983 | |
| 1984 | ParagraphStyle paragraph_style; |
| 1985 | paragraph_style.setMaxLines(14); |
| 1986 | paragraph_style.setTextAlign(TextAlign::kJustify); |
| 1987 | paragraph_style.turnHintingOff(); |
| 1988 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1989 | |
| 1990 | auto decoration = (TextDecoration)(TextDecoration::kUnderline | TextDecoration::kOverline | |
| 1991 | TextDecoration::kLineThrough); |
| 1992 | |
| 1993 | TextStyle text_style; |
| 1994 | text_style.setFontFamilies({SkString("Source Han Serif CN")}); |
| 1995 | text_style.setFontSize(35); |
| 1996 | text_style.setColor(SK_ColorBLACK); |
| 1997 | text_style.setLetterSpacing(2); |
| 1998 | text_style.setHeight(1); |
| 1999 | text_style.setDecoration(decoration); |
| 2000 | text_style.setDecorationColor(SK_ColorBLACK); |
| 2001 | text_style.setDecorationStyle(TextDecorationStyle::kSolid); |
| 2002 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2003 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2004 | builder.pop(); |
| 2005 | |
| 2006 | auto paragraph = builder.Build(); |
| 2007 | paragraph->layout(TestCanvasWidth - 100); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2008 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2009 | |
Julia Lavrova | 3281b96 | 2019-12-02 11:32:25 -0500 | [diff] [blame] | 2010 | REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == 0); |
| 2011 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2012 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2013 | |
| 2014 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 2015 | REPORTER_ASSERT(reporter, impl->lines().size() == 7); |
| 2016 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 2017 | REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2018 | } |
| 2019 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2020 | // Checked: NO DIFF (disabled) |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2021 | DEF_TEST(SkParagraph_ArabicParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 2022 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2023 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2024 | TestCanvas canvas("SkParagraph_ArabicParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2025 | const char* text = |
| 2026 | "من أسر وإعلان الخاصّة وهولندا،, عل قائمة الضغوط بالمطالبة تلك. الصفحة " |
| 2027 | "بمباركة التقليدية قام عن. تصفح"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2028 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2029 | |
| 2030 | ParagraphStyle paragraph_style; |
| 2031 | paragraph_style.setMaxLines(14); |
| 2032 | paragraph_style.setTextAlign(TextAlign::kJustify); |
| 2033 | paragraph_style.turnHintingOff(); |
| 2034 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 2035 | |
| 2036 | auto decoration = (TextDecoration)(TextDecoration::kUnderline | TextDecoration::kOverline | |
| 2037 | TextDecoration::kLineThrough); |
| 2038 | |
| 2039 | TextStyle text_style; |
| 2040 | text_style.setFontFamilies({SkString("Katibeh")}); |
| 2041 | text_style.setFontSize(35); |
| 2042 | text_style.setColor(SK_ColorBLACK); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2043 | text_style.setLetterSpacing(2); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2044 | text_style.setDecoration(decoration); |
| 2045 | text_style.setDecorationColor(SK_ColorBLACK); |
| 2046 | text_style.setDecorationStyle(TextDecorationStyle::kSolid); |
| 2047 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2048 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2049 | builder.pop(); |
| 2050 | |
| 2051 | auto paragraph = builder.Build(); |
| 2052 | paragraph->layout(TestCanvasWidth - 100); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2053 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2054 | |
Julia Lavrova | 3281b96 | 2019-12-02 11:32:25 -0500 | [diff] [blame] | 2055 | REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == 0); |
| 2056 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2057 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2058 | |
| 2059 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 2060 | REPORTER_ASSERT(reporter, impl->lines().size() == 2); |
| 2061 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 2062 | REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2063 | } |
| 2064 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2065 | // Checked: DIFF (2 boxes and each space is a word) |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2066 | DEF_TEST(SkParagraph_ArabicRectsParagraph, reporter) { |
| 2067 | |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 2068 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2069 | if (!fontCollection->fontsFound()) return; |
| 2070 | TestCanvas canvas("SkParagraph_ArabicRectsParagraph.png"); |
| 2071 | const char* text = "بمباركة التقليدية قام عن. تصفح يد "; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2072 | const size_t len = strlen(text); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2073 | |
| 2074 | ParagraphStyle paragraph_style; |
| 2075 | paragraph_style.turnHintingOff(); |
| 2076 | paragraph_style.setMaxLines(14); |
| 2077 | paragraph_style.setTextAlign(TextAlign::kRight); |
| 2078 | paragraph_style.setTextDirection(TextDirection::kRtl); |
| 2079 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 2080 | |
| 2081 | TextStyle text_style; |
| 2082 | text_style.setFontFamilies({SkString("Noto Naskh Arabic")}); |
| 2083 | text_style.setFontSize(26); |
| 2084 | text_style.setWordSpacing(5); |
| 2085 | text_style.setColor(SK_ColorBLACK); |
| 2086 | text_style.setDecoration(TextDecoration::kUnderline); |
| 2087 | text_style.setDecorationColor(SK_ColorBLACK); |
| 2088 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2089 | builder.addText(text, len); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2090 | builder.pop(); |
| 2091 | |
| 2092 | auto paragraph = builder.Build(); |
| 2093 | paragraph->layout(TestCanvasWidth - 100); |
| 2094 | |
| 2095 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 2096 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 2097 | |
| 2098 | paragraph->paint(canvas.get(), 0, 0); |
| 2099 | |
| 2100 | RectHeightStyle rect_height_style = RectHeightStyle::kMax; |
| 2101 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 2102 | std::vector<TextBox> boxes = paragraph->getRectsForRange(0, 100, rect_height_style, rect_width_style); |
| 2103 | canvas.drawRects(SK_ColorRED, boxes); |
| 2104 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2105 | REPORTER_ASSERT(reporter, boxes.size() == 1ull); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2106 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2107 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 538.548f, EPSILON100)); // DIFF: 510.09375 |
| 2108 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), -0.268f, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2109 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 900, EPSILON100)); |
| 2110 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 44, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2111 | } |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2112 | |
| 2113 | // Checked DIFF+ |
Julia Lavrova | c88a3bc | 2020-01-23 10:16:26 -0500 | [diff] [blame] | 2114 | // This test shows now 2 boxes for [36:40) range: |
| 2115 | // [36:38) for arabic text and [38:39) for the last space |
| 2116 | // 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] | 2117 | DEF_TEST(SkParagraph_ArabicRectsLTRLeftAlignParagraph, reporter) { |
| 2118 | |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 2119 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2120 | if (!fontCollection->fontsFound()) return; |
| 2121 | TestCanvas canvas("SkParagraph_ArabicRectsLTRLeftAlignParagraph.png"); |
| 2122 | const char* text = "Helloبمباركة التقليدية قام عن. تصفح يد "; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2123 | const size_t len = strlen(text); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2124 | |
| 2125 | ParagraphStyle paragraph_style; |
| 2126 | paragraph_style.turnHintingOff(); |
| 2127 | paragraph_style.setMaxLines(14); |
| 2128 | paragraph_style.setTextAlign(TextAlign::kLeft); |
| 2129 | paragraph_style.setTextDirection(TextDirection::kLtr); |
| 2130 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 2131 | |
| 2132 | TextStyle text_style; |
| 2133 | text_style.setFontFamilies({SkString("Noto Naskh Arabic")}); |
| 2134 | text_style.setFontSize(26); |
| 2135 | text_style.setWordSpacing(5); |
| 2136 | text_style.setColor(SK_ColorBLACK); |
| 2137 | text_style.setDecoration(TextDecoration::kUnderline); |
| 2138 | text_style.setDecorationColor(SK_ColorBLACK); |
| 2139 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2140 | builder.addText(text, len); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2141 | builder.pop(); |
| 2142 | |
| 2143 | auto paragraph = builder.Build(); |
| 2144 | paragraph->layout(TestCanvasWidth - 100); |
| 2145 | |
| 2146 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 2147 | REPORTER_ASSERT(reporter, impl->runs().size() == 3); |
| 2148 | |
| 2149 | paragraph->paint(canvas.get(), 0, 0); |
| 2150 | |
| 2151 | RectHeightStyle rect_height_style = RectHeightStyle::kMax; |
| 2152 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
Julia Lavrova | c88a3bc | 2020-01-23 10:16:26 -0500 | [diff] [blame] | 2153 | // 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] | 2154 | std::vector<TextBox> boxes = paragraph->getRectsForRange(36, 40, rect_height_style, rect_width_style); |
| 2155 | canvas.drawRects(SK_ColorRED, boxes); |
| 2156 | |
Julia Lavrova | c88a3bc | 2020-01-23 10:16:26 -0500 | [diff] [blame] | 2157 | REPORTER_ASSERT(reporter, boxes.size() == 2ull); |
| 2158 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 83.92f, EPSILON100)); // DIFF: 89.40625 |
| 2159 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), -0.27f, EPSILON100)); |
| 2160 | 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] | 2161 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 44, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2162 | } |
| 2163 | |
| 2164 | // Checked DIFF+ |
| 2165 | DEF_TEST(SkParagraph_ArabicRectsLTRRightAlignParagraph, reporter) { |
| 2166 | |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 2167 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2168 | if (!fontCollection->fontsFound()) return; |
| 2169 | TestCanvas canvas("SkParagraph_ArabicRectsLTRRightAlignParagraph.png"); |
| 2170 | const char* text = "Helloبمباركة التقليدية قام عن. تصفح يد "; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2171 | const size_t len = strlen(text); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2172 | |
| 2173 | ParagraphStyle paragraph_style; |
| 2174 | paragraph_style.turnHintingOff(); |
| 2175 | paragraph_style.setMaxLines(14); |
| 2176 | paragraph_style.setTextAlign(TextAlign::kRight); |
| 2177 | paragraph_style.setTextDirection(TextDirection::kLtr); |
| 2178 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 2179 | |
| 2180 | TextStyle text_style; |
| 2181 | text_style.setFontFamilies({SkString("Noto Naskh Arabic")}); |
| 2182 | text_style.setFontSize(26); |
| 2183 | text_style.setWordSpacing(5); |
| 2184 | text_style.setColor(SK_ColorBLACK); |
| 2185 | text_style.setDecoration(TextDecoration::kUnderline); |
| 2186 | text_style.setDecorationColor(SK_ColorBLACK); |
| 2187 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2188 | builder.addText(text, len); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2189 | builder.pop(); |
| 2190 | |
| 2191 | auto paragraph = builder.Build(); |
| 2192 | paragraph->layout(TestCanvasWidth - 100); |
| 2193 | |
| 2194 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 2195 | REPORTER_ASSERT(reporter, impl->runs().size() == 3); |
| 2196 | |
| 2197 | paragraph->paint(canvas.get(), 0, 0); |
| 2198 | |
| 2199 | RectHeightStyle rect_height_style = RectHeightStyle::kMax; |
| 2200 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 2201 | std::vector<TextBox> boxes = |
| 2202 | paragraph->getRectsForRange(36, 40, rect_height_style, rect_width_style); |
| 2203 | canvas.drawRects(SK_ColorRED, boxes); |
| 2204 | |
Julia Lavrova | c88a3bc | 2020-01-23 10:16:26 -0500 | [diff] [blame] | 2205 | REPORTER_ASSERT(reporter, boxes.size() == 2ull); // DIFF |
| 2206 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 561.5f, EPSILON100)); // DIFF |
| 2207 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), -0.27f, EPSILON100)); |
| 2208 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 587.74f, EPSILON100)); // DIFF |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2209 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 44, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2210 | } |
| 2211 | |
| 2212 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2213 | DEF_TEST(SkParagraph_GetGlyphPositionAtCoordinateParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 2214 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2215 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2216 | TestCanvas canvas("SkParagraph_GetGlyphPositionAtCoordinateParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2217 | const char* text = |
| 2218 | "12345 67890 12345 67890 12345 67890 12345 67890 12345 67890 12345 " |
| 2219 | "67890 12345"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2220 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2221 | |
| 2222 | ParagraphStyle paragraphStyle; |
| 2223 | paragraphStyle.setTextAlign(TextAlign::kLeft); |
| 2224 | paragraphStyle.setMaxLines(10); |
| 2225 | paragraphStyle.turnHintingOff(); |
| 2226 | TextStyle textStyle; |
| 2227 | textStyle.setFontFamilies({SkString("Roboto")}); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2228 | textStyle.setFontStyle(SkFontStyle(SkFontStyle::kNormal_Weight, SkFontStyle::kNormal_Width, |
| 2229 | SkFontStyle::kUpright_Slant)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2230 | textStyle.setFontSize(50); |
| 2231 | textStyle.setLetterSpacing(1); |
| 2232 | textStyle.setWordSpacing(5); |
| 2233 | textStyle.setHeight(1); |
| 2234 | textStyle.setColor(SK_ColorBLACK); |
| 2235 | |
| 2236 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 2237 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2238 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2239 | builder.pop(); |
| 2240 | |
| 2241 | auto paragraph = builder.Build(); |
| 2242 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2243 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2244 | |
| 2245 | // Tests for getGlyphPositionAtCoordinate() |
| 2246 | // NOTE: resulting values can be a few off from their respective positions in |
| 2247 | // the original text because the final trailing whitespaces are sometimes not |
| 2248 | // drawn (namely, when using "justify" alignment) and therefore are not active |
| 2249 | // glyphs. |
| 2250 | REPORTER_ASSERT(reporter, |
| 2251 | paragraph->getGlyphPositionAtCoordinate(-10000, -10000).position == 0); |
| 2252 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(-1, -1).position == 0); |
| 2253 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(0, 0).position == 0); |
| 2254 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(3, 3).position == 0); |
| 2255 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(35, 1).position == 1); |
| 2256 | REPORTER_ASSERT(reporter, |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2257 | paragraph->getGlyphPositionAtCoordinate(300, 2).position == 11); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2258 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(301, 2.2f).position == 11); |
| 2259 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(302, 2.6f).position == 11); |
| 2260 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(301, 2.1f).position == 11); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2261 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(100000, 20).position == 18);// |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2262 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(450, 20).position == 16); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2263 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(100000, 90).position == 36);// |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2264 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(-100000, 90).position == 18); |
| 2265 | REPORTER_ASSERT(reporter, |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2266 | paragraph->getGlyphPositionAtCoordinate(20, -80).position == 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2267 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(1, 90).position == 18); |
| 2268 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(1, 170).position == 36); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2269 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(10000, 180).position == 72);// |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2270 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(70, 180).position == 56); |
| 2271 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(1, 270).position == 72); |
| 2272 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(35, 90).position == 19); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2273 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(10000, 10000).position == 77);// |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2274 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(85, 10000).position == 75); |
| 2275 | } |
| 2276 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2277 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2278 | DEF_TEST(SkParagraph_GetRectsForRangeParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 2279 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2280 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2281 | TestCanvas canvas("SkParagraph_GetRectsForRangeParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2282 | const char* text = |
| 2283 | "12345, \"67890\" 12345 67890 12345 67890 12345 67890 12345 67890 12345 " |
| 2284 | "67890 12345"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2285 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2286 | |
| 2287 | ParagraphStyle paragraphStyle; |
| 2288 | paragraphStyle.setTextAlign(TextAlign::kLeft); |
| 2289 | paragraphStyle.setMaxLines(10); |
| 2290 | paragraphStyle.turnHintingOff(); |
| 2291 | TextStyle textStyle; |
| 2292 | textStyle.setFontFamilies({SkString("Roboto")}); |
| 2293 | textStyle.setFontSize(50); |
| 2294 | textStyle.setColor(SK_ColorBLACK); |
| 2295 | textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 2296 | SkFontStyle::kUpright_Slant)); |
| 2297 | |
| 2298 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 2299 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2300 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2301 | builder.pop(); |
| 2302 | |
| 2303 | auto paragraph = builder.Build(); |
| 2304 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2305 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2306 | |
| 2307 | RectHeightStyle heightStyle = RectHeightStyle::kMax; |
| 2308 | RectWidthStyle widthStyle = RectWidthStyle::kTight; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2309 | |
| 2310 | SkPaint paint; |
| 2311 | paint.setStyle(SkPaint::kStroke_Style); |
| 2312 | paint.setAntiAlias(true); |
| 2313 | paint.setStrokeWidth(1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2314 | |
| 2315 | { |
| 2316 | auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle); |
| 2317 | REPORTER_ASSERT(reporter, result.empty()); |
| 2318 | } |
| 2319 | { |
| 2320 | auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2321 | canvas.drawRects(SK_ColorRED, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2322 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2323 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 0, EPSILON100)); |
| 2324 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100)); |
| 2325 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 28.417f, EPSILON100)); |
| 2326 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2327 | } |
| 2328 | { |
| 2329 | auto result = paragraph->getRectsForRange(2, 8, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2330 | canvas.drawRects(SK_ColorBLUE, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2331 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2332 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 56.835f, EPSILON100)); |
| 2333 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100)); |
| 2334 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 177.97f, EPSILON100)); |
| 2335 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2336 | } |
| 2337 | { |
| 2338 | auto result = paragraph->getRectsForRange(8, 21, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2339 | canvas.drawRects(SK_ColorGREEN, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2340 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2341 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 177.97f, EPSILON100)); |
| 2342 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100)); |
| 2343 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 507.031f, EPSILON100)); |
| 2344 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2345 | } |
| 2346 | { |
| 2347 | auto result = paragraph->getRectsForRange(30, 100, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2348 | canvas.drawRects(SK_ColorRED, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2349 | REPORTER_ASSERT(reporter, result.size() == 4); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2350 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 211.375f, EPSILON100)); |
| 2351 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 59.40625f, EPSILON100)); |
| 2352 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 463.623f, EPSILON100)); |
| 2353 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 118, EPSILON100)); |
| 2354 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.left(), 0, EPSILON100)); |
| 2355 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.top(), 236.406f, EPSILON100)); |
| 2356 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.right(), 142.089f, EPSILON100)); |
| 2357 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.bottom(), 295, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2358 | } |
| 2359 | { |
| 2360 | auto result = paragraph->getRectsForRange(19, 22, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2361 | canvas.drawRects(SK_ColorBLUE, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2362 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2363 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 450.1875f, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2364 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100)); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2365 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 519.47266f, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2366 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2367 | } |
| 2368 | { |
| 2369 | auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle); |
| 2370 | REPORTER_ASSERT(reporter, result.empty()); |
| 2371 | } |
| 2372 | } |
| 2373 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2374 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2375 | DEF_TEST(SkParagraph_GetRectsForRangeTight, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 2376 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2377 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2378 | TestCanvas canvas("SkParagraph_GetRectsForRangeTight.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2379 | const char* text = |
| 2380 | "( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)(" |
| 2381 | " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)(" |
| 2382 | " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2383 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2384 | |
| 2385 | ParagraphStyle paragraphStyle; |
| 2386 | paragraphStyle.setTextAlign(TextAlign::kLeft); |
| 2387 | paragraphStyle.setMaxLines(10); |
| 2388 | paragraphStyle.turnHintingOff(); |
| 2389 | TextStyle textStyle; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2390 | textStyle.setFontFamilies({SkString("Noto Sans CJK JP")}); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2391 | textStyle.setFontSize(50); |
| 2392 | textStyle.setColor(SK_ColorBLACK); |
| 2393 | textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 2394 | SkFontStyle::kUpright_Slant)); |
| 2395 | |
| 2396 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 2397 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2398 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2399 | builder.pop(); |
| 2400 | |
| 2401 | auto paragraph = builder.Build(); |
| 2402 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2403 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2404 | |
| 2405 | RectHeightStyle heightStyle = RectHeightStyle::kTight; |
| 2406 | RectWidthStyle widthStyle = RectWidthStyle::kTight; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2407 | { |
| 2408 | auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle); |
| 2409 | REPORTER_ASSERT(reporter, result.empty()); |
| 2410 | } |
| 2411 | { |
| 2412 | auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2413 | canvas.drawRects(SK_ColorRED, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2414 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2415 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 0, EPSILON100)); |
| 2416 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0, EPSILON100)); |
| 2417 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 16.898f, EPSILON100)); |
| 2418 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 74, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2419 | } |
| 2420 | { |
| 2421 | auto result = paragraph->getRectsForRange(2, 8, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2422 | canvas.drawRects(SK_ColorBLUE, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2423 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2424 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 66.899f, EPSILON100)); |
| 2425 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0, EPSILON100)); |
| 2426 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 264.099f, EPSILON100)); |
| 2427 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 74, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2428 | } |
| 2429 | { |
| 2430 | auto result = paragraph->getRectsForRange(8, 21, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2431 | canvas.drawRects(SK_ColorGREEN, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2432 | REPORTER_ASSERT(reporter, result.size() == 2); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2433 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 264.099f, EPSILON100)); |
| 2434 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0, EPSILON100)); |
| 2435 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 595.085f, EPSILON50)); |
| 2436 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 74, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2437 | } |
| 2438 | } |
| 2439 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2440 | // Checked: DIFF+ |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2441 | DEF_TEST(SkParagraph_GetRectsForRangeIncludeLineSpacingMiddle, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 2442 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2443 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2444 | TestCanvas canvas("SkParagraph_GetRectsForRangeIncludeLineSpacingMiddle.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2445 | const char* text = |
| 2446 | "( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)(" |
| 2447 | " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)(" |
| 2448 | " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2449 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2450 | |
| 2451 | ParagraphStyle paragraphStyle; |
| 2452 | paragraphStyle.setTextAlign(TextAlign::kLeft); |
| 2453 | paragraphStyle.setMaxLines(10); |
| 2454 | paragraphStyle.turnHintingOff(); |
| 2455 | TextStyle textStyle; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2456 | textStyle.setFontFamilies({SkString("Roboto")}); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2457 | textStyle.setFontSize(50); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2458 | textStyle.setHeight(1.6f); |
| 2459 | textStyle.setHeightOverride(true); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2460 | textStyle.setColor(SK_ColorBLACK); |
| 2461 | textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 2462 | SkFontStyle::kUpright_Slant)); |
| 2463 | |
| 2464 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 2465 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2466 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2467 | builder.pop(); |
| 2468 | |
| 2469 | auto paragraph = builder.Build(); |
| 2470 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2471 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2472 | |
| 2473 | RectHeightStyle heightStyle = RectHeightStyle::kIncludeLineSpacingMiddle; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2474 | RectWidthStyle widthStyle = RectWidthStyle::kMax; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2475 | { |
| 2476 | auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle); |
| 2477 | REPORTER_ASSERT(reporter, result.empty()); |
| 2478 | } |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2479 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2480 | { |
| 2481 | auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2482 | canvas.drawRects(SK_ColorRED, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2483 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2484 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 0, EPSILON100)); |
| 2485 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946615f, EPSILON100)); |
| 2486 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 17.4296889f, EPSILON100)); |
| 2487 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 88.473305f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2488 | } |
| 2489 | { |
| 2490 | auto result = paragraph->getRectsForRange(2, 8, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2491 | canvas.drawRects(SK_ColorBLUE, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2492 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2493 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 67.429688f, EPSILON100)); |
| 2494 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946615f, EPSILON100)); |
| 2495 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 190.00781f, EPSILON100)); |
| 2496 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 88.473305f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2497 | } |
| 2498 | { |
| 2499 | auto result = paragraph->getRectsForRange(8, 21, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2500 | canvas.drawRects(SK_ColorGREEN, result); |
| 2501 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2502 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 190.00781f, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2503 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946615f, EPSILON100)); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2504 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 508.0625f, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2505 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 88.473305f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2506 | } |
| 2507 | { |
| 2508 | auto result = paragraph->getRectsForRange(30, 150, heightStyle, widthStyle); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2509 | canvas.drawRects(SK_ColorRED, result); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2510 | REPORTER_ASSERT(reporter, result.size() == 8); |
| 2511 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2512 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 190.00781f, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2513 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 88.473305f, EPSILON100)); |
| 2514 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 525.687f, EPSILON20)); |
| 2515 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 168.47331f, EPSILON100)); |
| 2516 | |
| 2517 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.left(), 525.687f, EPSILON20)); |
| 2518 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.top(), 88.473305f, EPSILON100)); |
| 2519 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.right(), 570.02344f, EPSILON20)); |
| 2520 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.bottom(), 168.47331f, EPSILON100)); |
| 2521 | |
| 2522 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.left(), 0, EPSILON100)); |
| 2523 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.top(), 168.47331f, EPSILON100)); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2524 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.right(), 531.574f, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2525 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.bottom(), 248.47331f, EPSILON100)); |
| 2526 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2527 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.left(), 531.574f, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2528 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.top(), 168.47331f, EPSILON100)); |
| 2529 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.right(), 570.02344f, EPSILON20)); |
| 2530 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.bottom(), 248.47331f, EPSILON100)); |
| 2531 | |
| 2532 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.left(), 0, EPSILON100)); |
| 2533 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.top(), 248.47331f, EPSILON100)); |
| 2534 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.right(), 570.02344f, EPSILON20)); |
| 2535 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.bottom(), 328.47333f, EPSILON100)); |
| 2536 | |
| 2537 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.left(), 0, EPSILON100)); |
| 2538 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.top(), 328.47333f, EPSILON100)); |
| 2539 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.right(), 570.02344f, EPSILON20)); |
| 2540 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.bottom(), 408.4733f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2541 | } |
| 2542 | { |
| 2543 | auto result = paragraph->getRectsForRange(19, 22, heightStyle, widthStyle); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2544 | canvas.drawRects(SK_ColorBLUE, result); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2545 | REPORTER_ASSERT(reporter, result.size() == 2); // DIFF |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2546 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 463.72656f, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2547 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946615f, EPSILON100)); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2548 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 530.23047f, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2549 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 88.473305f, EPSILON100)); |
| 2550 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2551 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.left(), 530.23047f, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2552 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.top(), 16.946615f, EPSILON100)); |
| 2553 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.right(), 570.02344f, EPSILON20)); |
| 2554 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.bottom(), 88.473305f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2555 | } |
| 2556 | { |
| 2557 | auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle); |
| 2558 | REPORTER_ASSERT(reporter, result.empty()); |
| 2559 | } |
| 2560 | } |
| 2561 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2562 | // Checked: NO DIFF+ |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2563 | DEF_TEST(SkParagraph_GetRectsForRangeIncludeLineSpacingTop, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 2564 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2565 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2566 | TestCanvas canvas("SkParagraph_GetRectsForRangeIncludeLineSpacingTop.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2567 | const char* text = |
| 2568 | "( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)(" |
| 2569 | " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)(" |
| 2570 | " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2571 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2572 | |
| 2573 | ParagraphStyle paragraphStyle; |
| 2574 | paragraphStyle.setTextAlign(TextAlign::kLeft); |
| 2575 | paragraphStyle.setMaxLines(10); |
| 2576 | paragraphStyle.turnHintingOff(); |
| 2577 | TextStyle textStyle; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2578 | textStyle.setFontFamilies({SkString("Roboto")}); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2579 | textStyle.setFontSize(50); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2580 | textStyle.setHeight(1.6f); |
| 2581 | textStyle.setHeightOverride(true); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2582 | textStyle.setColor(SK_ColorBLACK); |
| 2583 | textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 2584 | SkFontStyle::kUpright_Slant)); |
| 2585 | |
| 2586 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 2587 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2588 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2589 | builder.pop(); |
| 2590 | |
| 2591 | auto paragraph = builder.Build(); |
| 2592 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2593 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2594 | |
| 2595 | RectHeightStyle heightStyle = RectHeightStyle::kIncludeLineSpacingTop; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2596 | RectWidthStyle widthStyle = RectWidthStyle::kMax; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2597 | { |
| 2598 | auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle); |
| 2599 | REPORTER_ASSERT(reporter, result.empty()); |
| 2600 | } |
| 2601 | |
| 2602 | { |
| 2603 | auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2604 | canvas.drawRects(SK_ColorRED, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2605 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2606 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 0, EPSILON100)); |
| 2607 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946615f, EPSILON100)); |
| 2608 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 17.4296889f, EPSILON100)); |
| 2609 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 80, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2610 | } |
| 2611 | { |
| 2612 | auto result = paragraph->getRectsForRange(2, 8, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2613 | canvas.drawRects(SK_ColorBLUE, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2614 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2615 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 67.429688f, EPSILON100)); |
| 2616 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946615f, EPSILON100)); |
| 2617 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 190.00781f, EPSILON100)); |
| 2618 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 80, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2619 | } |
| 2620 | { |
| 2621 | auto result = paragraph->getRectsForRange(8, 21, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2622 | canvas.drawRects(SK_ColorGREEN, result); |
| 2623 | REPORTER_ASSERT(reporter, result.size() == 1); |
| 2624 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 190.00781f, EPSILON100)); |
| 2625 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946615f, EPSILON100)); |
| 2626 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 508.0625f, EPSILON50)); |
| 2627 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 80, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2628 | } |
| 2629 | { |
| 2630 | auto result = paragraph->getRectsForRange(30, 150, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2631 | canvas.drawRects(SK_ColorMAGENTA, result); |
| 2632 | REPORTER_ASSERT(reporter, result.size() == 8); |
| 2633 | |
| 2634 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 190.00781f, EPSILON100)); |
| 2635 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 80, EPSILON100)); |
| 2636 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 525.687f, EPSILON20)); |
| 2637 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 160, EPSILON100)); |
| 2638 | |
| 2639 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.left(), 525.687f, EPSILON20)); |
| 2640 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.top(), 80, EPSILON100)); |
| 2641 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.right(), 570.02344f, EPSILON20)); |
| 2642 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.bottom(), 160, EPSILON100)); |
| 2643 | |
| 2644 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.left(), 0, EPSILON100)); |
| 2645 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.top(), 160, EPSILON100)); |
| 2646 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.right(), 531.574f, EPSILON20)); |
| 2647 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.bottom(), 240, EPSILON100)); |
| 2648 | |
| 2649 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.left(), 531.574f, EPSILON20)); |
| 2650 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.top(), 160, EPSILON100)); |
| 2651 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.right(), 570.02344f, EPSILON20)); |
| 2652 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.bottom(), 240, EPSILON100)); |
| 2653 | |
| 2654 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.left(), 0, EPSILON100)); |
| 2655 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.top(), 240, EPSILON100)); |
| 2656 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.right(), 570.02344f, EPSILON20)); |
| 2657 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.bottom(), 320, EPSILON100)); |
| 2658 | |
| 2659 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.left(), 0, EPSILON100)); |
| 2660 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.top(), 320, EPSILON100)); |
| 2661 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.right(), 570.02344f, EPSILON20)); |
| 2662 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.bottom(), 400, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2663 | } |
| 2664 | { |
| 2665 | auto result = paragraph->getRectsForRange(19, 22, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2666 | canvas.drawRects(SK_ColorBLACK, result); |
| 2667 | REPORTER_ASSERT(reporter, result.size() == 2); // DIFF |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2668 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 463.72656f, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2669 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946615f, EPSILON100)); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2670 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 530.23047f, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2671 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 80, EPSILON100)); |
| 2672 | |
| 2673 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.left(), 530.23047f, EPSILON50)); |
| 2674 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.top(), 16.946615f, EPSILON100)); |
| 2675 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.right(), 570.02344f, EPSILON20)); |
| 2676 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.bottom(), 80, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2677 | } |
| 2678 | { |
| 2679 | auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle); |
| 2680 | REPORTER_ASSERT(reporter, result.empty()); |
| 2681 | } |
| 2682 | } |
| 2683 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2684 | // Checked: NO DIFF+ |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2685 | DEF_TEST(SkParagraph_GetRectsForRangeIncludeLineSpacingBottom, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 2686 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2687 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2688 | TestCanvas canvas("SkParagraph_GetRectsForRangeIncludeLineSpacingBottom.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2689 | const char* text = |
| 2690 | "( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)(" |
| 2691 | " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)(" |
| 2692 | " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2693 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2694 | |
| 2695 | ParagraphStyle paragraphStyle; |
| 2696 | paragraphStyle.setTextAlign(TextAlign::kLeft); |
| 2697 | paragraphStyle.setMaxLines(10); |
| 2698 | paragraphStyle.turnHintingOff(); |
| 2699 | TextStyle textStyle; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2700 | textStyle.setFontFamilies({SkString("Roboto")}); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2701 | textStyle.setFontSize(50); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2702 | textStyle.setHeight(1.6f); |
| 2703 | textStyle.setHeightOverride(true); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2704 | textStyle.setColor(SK_ColorBLACK); |
| 2705 | textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 2706 | SkFontStyle::kUpright_Slant)); |
| 2707 | |
| 2708 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 2709 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2710 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2711 | builder.pop(); |
| 2712 | |
| 2713 | auto paragraph = builder.Build(); |
| 2714 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2715 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2716 | |
| 2717 | RectHeightStyle heightStyle = RectHeightStyle::kIncludeLineSpacingBottom; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2718 | RectWidthStyle widthStyle = RectWidthStyle::kMax; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2719 | { |
| 2720 | auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle); |
| 2721 | REPORTER_ASSERT(reporter, result.empty()); |
| 2722 | } |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2723 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2724 | { |
| 2725 | auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2726 | canvas.drawRects(SK_ColorRED, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2727 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2728 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 0, EPSILON100)); |
| 2729 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946f, EPSILON100)); |
| 2730 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 17.429f, EPSILON100)); |
| 2731 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 96.946f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2732 | } |
| 2733 | { |
| 2734 | auto result = paragraph->getRectsForRange(2, 8, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2735 | canvas.drawRects(SK_ColorBLUE, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2736 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2737 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 67.4298f, EPSILON100)); |
| 2738 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946f, EPSILON100)); |
| 2739 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 190.007f, EPSILON100)); |
| 2740 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 96.946f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2741 | } |
| 2742 | { |
| 2743 | auto result = paragraph->getRectsForRange(8, 21, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2744 | canvas.drawRects(SK_ColorGREEN, result); |
| 2745 | REPORTER_ASSERT(reporter, result.size() == 1); |
| 2746 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 190.007f, EPSILON100)); |
| 2747 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946f, EPSILON100)); |
| 2748 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 508.062f, EPSILON50)); |
| 2749 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 96.946f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2750 | } |
| 2751 | { |
| 2752 | auto result = paragraph->getRectsForRange(30, 150, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2753 | canvas.drawRects(SK_ColorMAGENTA, result); |
| 2754 | REPORTER_ASSERT(reporter, result.size() == 8); |
| 2755 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2756 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 190.007f, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2757 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 96.946f, EPSILON100)); |
| 2758 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 525.687f, EPSILON20)); |
| 2759 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 176.946f, EPSILON100)); |
| 2760 | |
| 2761 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.left(), 525.687f, EPSILON20)); |
| 2762 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.top(), 96.946f, EPSILON100)); |
| 2763 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.right(), 570.023f, EPSILON20)); |
| 2764 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.bottom(), 176.946f, EPSILON100)); |
| 2765 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2766 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.left(), 0, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2767 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.top(), 176.946f, EPSILON100)); |
| 2768 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.right(), 531.574f, EPSILON20)); |
| 2769 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.bottom(), 256.946f, EPSILON100)); |
| 2770 | |
| 2771 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.left(), 531.574f, EPSILON20)); |
| 2772 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.top(), 176.946f, EPSILON100)); |
| 2773 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.right(), 570.023f, EPSILON20)); |
| 2774 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.bottom(), 256.946f, EPSILON100)); |
| 2775 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2776 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.left(), 0, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2777 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.top(), 256.946f, EPSILON100)); |
| 2778 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.right(), 570.023f, EPSILON20)); |
| 2779 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.bottom(), 336.946f, EPSILON100)); |
| 2780 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2781 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.left(), 0, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2782 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.top(), 336.946f, EPSILON100)); |
| 2783 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.right(), 570.023f, EPSILON20)); |
| 2784 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.bottom(), 416.946f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2785 | } |
| 2786 | { |
| 2787 | auto result = paragraph->getRectsForRange(19, 22, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2788 | canvas.drawRects(SK_ColorBLACK, result); |
| 2789 | REPORTER_ASSERT(reporter, result.size() == 2); // DIFF |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2790 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 463.726f, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2791 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946f, EPSILON100)); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2792 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 530.230f, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2793 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 96.946f, EPSILON100)); |
| 2794 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2795 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.left(), 530.230f, EPSILON20)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2796 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.top(), 16.946f, EPSILON100)); |
| 2797 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.right(), 570.023f, EPSILON20)); |
| 2798 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.bottom(), 96.946f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2799 | } |
| 2800 | { |
| 2801 | auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle); |
| 2802 | REPORTER_ASSERT(reporter, result.empty()); |
| 2803 | } |
| 2804 | } |
| 2805 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2806 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2807 | DEF_TEST(SkParagraph_GetRectsForRangeIncludeCombiningCharacter, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 2808 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2809 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2810 | TestCanvas canvas("SkParagraph_GetRectsForRangeIncludeCombiningCharacter.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2811 | const char* text = "ดีสวัสดีชาวโลกที่น่ารัก"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2812 | const size_t len = strlen(text); |
| 2813 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2814 | ParagraphStyle paragraphStyle; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2815 | paragraphStyle.setTextAlign(TextAlign::kLeft); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2816 | paragraphStyle.setMaxLines(10); |
| 2817 | paragraphStyle.turnHintingOff(); |
| 2818 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 2819 | |
| 2820 | TextStyle textStyle; |
| 2821 | textStyle.setFontFamilies({SkString("Roboto")}); |
| 2822 | textStyle.setFontSize(50); |
| 2823 | textStyle.setLetterSpacing(1); |
| 2824 | textStyle.setWordSpacing(5); |
| 2825 | textStyle.setHeight(1); |
| 2826 | textStyle.setColor(SK_ColorBLACK); |
| 2827 | |
| 2828 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2829 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2830 | builder.pop(); |
| 2831 | |
| 2832 | auto paragraph = builder.Build(); |
| 2833 | paragraph->layout(TestCanvasWidth - 100); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2834 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2835 | |
| 2836 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2837 | REPORTER_ASSERT(reporter, impl->lines().size() == 1); |
| 2838 | |
| 2839 | RectHeightStyle heightStyle = RectHeightStyle::kTight; |
| 2840 | RectWidthStyle widthStyle = RectWidthStyle::kTight; |
| 2841 | { |
| 2842 | auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle); |
| 2843 | REPORTER_ASSERT(reporter, result.empty()); |
| 2844 | } |
| 2845 | { |
| 2846 | auto first = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle); |
| 2847 | auto second = paragraph->getRectsForRange(1, 2, heightStyle, widthStyle); |
| 2848 | auto last = paragraph->getRectsForRange(0, 2, heightStyle, widthStyle); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2849 | REPORTER_ASSERT(reporter, first.size() == 0 && second.size() == 1 && last.size() == 1); |
| 2850 | REPORTER_ASSERT(reporter, second[0].rect == last[0].rect); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2851 | } |
| 2852 | { |
| 2853 | auto first = paragraph->getRectsForRange(3, 4, heightStyle, widthStyle); |
| 2854 | auto second = paragraph->getRectsForRange(4, 5, heightStyle, widthStyle); |
| 2855 | auto last = paragraph->getRectsForRange(3, 5, heightStyle, widthStyle); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2856 | REPORTER_ASSERT(reporter, first.size() == 0 && second.size() == 1 && last.size() == 1); |
| 2857 | REPORTER_ASSERT(reporter, second[0].rect == last[0].rect); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2858 | } |
| 2859 | { |
| 2860 | auto first = paragraph->getRectsForRange(14, 15, heightStyle, widthStyle); |
| 2861 | auto second = paragraph->getRectsForRange(15, 16, heightStyle, widthStyle); |
| 2862 | auto third = paragraph->getRectsForRange(16, 17, heightStyle, widthStyle); |
| 2863 | auto last = paragraph->getRectsForRange(14, 17, heightStyle, widthStyle); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 2864 | 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] | 2865 | REPORTER_ASSERT(reporter, third[0].rect == last[0].rect); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2866 | } |
| 2867 | } |
| 2868 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2869 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2870 | DEF_TEST(SkParagraph_GetRectsForRangeCenterParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 2871 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2872 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2873 | TestCanvas canvas("SkParagraph_GetRectsForRangeCenterParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2874 | // Minikin uses a hard coded list of unicode characters that he treats as invisible - as spaces. |
| 2875 | // It's absolutely wrong - invisibility is a glyph attribute, not character/grapheme. |
| 2876 | // Any attempt to substitute one for another leads to errors |
| 2877 | // (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] | 2878 | const char* text = "01234 "; // includes ideographic space and english space. |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2879 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2880 | |
| 2881 | ParagraphStyle paragraphStyle; |
| 2882 | paragraphStyle.setTextAlign(TextAlign::kCenter); |
| 2883 | paragraphStyle.setMaxLines(10); |
| 2884 | paragraphStyle.turnHintingOff(); |
| 2885 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 2886 | |
| 2887 | TextStyle textStyle; |
| 2888 | textStyle.setFontFamilies({SkString("Roboto")}); |
| 2889 | textStyle.setFontSize(50); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2890 | textStyle.setHeight(1); |
| 2891 | textStyle.setColor(SK_ColorBLACK); |
| 2892 | textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 2893 | SkFontStyle::kUpright_Slant)); |
| 2894 | |
| 2895 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2896 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2897 | builder.pop(); |
| 2898 | |
| 2899 | auto paragraph = builder.Build(); |
| 2900 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2901 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2902 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2903 | // Some of the formatting lazily done on paint |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2904 | RectHeightStyle heightStyle = RectHeightStyle::kMax; |
| 2905 | RectWidthStyle widthStyle = RectWidthStyle::kTight; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2906 | { |
| 2907 | auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle); |
| 2908 | REPORTER_ASSERT(reporter, result.empty()); |
| 2909 | } |
| 2910 | |
| 2911 | { |
| 2912 | auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2913 | canvas.drawRects(SK_ColorRED, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2914 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2915 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 203.955f, EPSILON100)); |
| 2916 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100)); |
| 2917 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 232.373f, EPSILON100)); |
| 2918 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2919 | } |
| 2920 | |
| 2921 | { |
| 2922 | auto result = paragraph->getRectsForRange(2, 4, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2923 | canvas.drawRects(SK_ColorBLUE, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2924 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2925 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 260.791f, EPSILON100)); |
| 2926 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100)); |
| 2927 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 317.626f, EPSILON100)); |
| 2928 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2929 | } |
| 2930 | |
| 2931 | { |
| 2932 | auto result = paragraph->getRectsForRange(4, 5, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2933 | canvas.drawRects(SK_ColorGREEN, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2934 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2935 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 317.626f, EPSILON100)); |
| 2936 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100)); |
| 2937 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 346.044f, EPSILON100)); |
| 2938 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2939 | } |
| 2940 | |
| 2941 | { |
| 2942 | auto result = paragraph->getRectsForRange(4, 6, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2943 | canvas.drawRects(SK_ColorBLACK, result); |
| 2944 | REPORTER_ASSERT(reporter, result.size() == 1); // DIFF |
| 2945 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 317.626f, EPSILON100)); |
| 2946 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100)); |
| 2947 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 358.494f, EPSILON100)); |
| 2948 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2949 | } |
| 2950 | |
| 2951 | { |
| 2952 | auto result = paragraph->getRectsForRange(5, 6, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2953 | canvas.drawRects(SK_ColorRED, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2954 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2955 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 346.044f, EPSILON100)); |
| 2956 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100)); |
| 2957 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 358.494f, EPSILON100)); |
| 2958 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2959 | } |
| 2960 | |
| 2961 | { |
| 2962 | auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle); |
| 2963 | REPORTER_ASSERT(reporter, result.empty()); |
| 2964 | } |
| 2965 | } |
| 2966 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2967 | // Checked DIFF+ |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2968 | DEF_TEST(SkParagraph_GetRectsForRangeCenterParagraphNewlineCentered, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 2969 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2970 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2971 | TestCanvas canvas("SkParagraph_GetRectsForRangeCenterParagraphNewlineCentered.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2972 | const char* text = "01234\n"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2973 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2974 | |
| 2975 | ParagraphStyle paragraphStyle; |
| 2976 | paragraphStyle.setTextAlign(TextAlign::kCenter); |
| 2977 | paragraphStyle.setMaxLines(10); |
| 2978 | paragraphStyle.turnHintingOff(); |
| 2979 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 2980 | |
| 2981 | TextStyle textStyle; |
| 2982 | textStyle.setFontFamilies({SkString("Roboto")}); |
| 2983 | textStyle.setFontSize(50); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2984 | textStyle.setHeight(1); |
| 2985 | textStyle.setColor(SK_ColorBLACK); |
| 2986 | textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 2987 | SkFontStyle::kUpright_Slant)); |
| 2988 | |
| 2989 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2990 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2991 | builder.pop(); |
| 2992 | |
| 2993 | auto paragraph = builder.Build(); |
| 2994 | paragraph->layout(550); |
| 2995 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2996 | paragraph->paint(canvas.get(), 0, 0); |
| 2997 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2998 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2999 | REPORTER_ASSERT(reporter, impl->lines().size() == 2); |
| 3000 | |
| 3001 | RectHeightStyle heightStyle = RectHeightStyle::kMax; |
| 3002 | RectWidthStyle widthStyle = RectWidthStyle::kTight; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3003 | { |
| 3004 | auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle); |
| 3005 | REPORTER_ASSERT(reporter, result.empty()); |
| 3006 | } |
| 3007 | |
| 3008 | { |
| 3009 | auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 3010 | canvas.drawRects(SK_ColorRED, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3011 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3012 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 203.955f, EPSILON100)); |
| 3013 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100)); |
| 3014 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 232.373f, EPSILON100)); |
| 3015 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3016 | } |
| 3017 | |
| 3018 | { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3019 | auto result = paragraph->getRectsForRange(6, 7, heightStyle, widthStyle); |
| 3020 | canvas.drawRects(SK_ColorBLUE, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3021 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3022 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 275.0f, EPSILON100)); |
| 3023 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 59.406f, EPSILON100)); |
| 3024 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 275.0f, EPSILON100)); |
| 3025 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 118, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3026 | } |
| 3027 | } |
| 3028 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3029 | // Checked NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3030 | DEF_TEST(SkParagraph_GetRectsForRangeCenterMultiLineParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3031 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3032 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3033 | TestCanvas canvas("SkParagraph_GetRectsForRangeCenterMultiLineParagraph.png"); |
| 3034 | const char* text = "01234 \n0123 "; // includes ideographic space and english space. |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3035 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3036 | |
| 3037 | ParagraphStyle paragraphStyle; |
| 3038 | paragraphStyle.setTextAlign(TextAlign::kCenter); |
| 3039 | paragraphStyle.setMaxLines(10); |
| 3040 | paragraphStyle.turnHintingOff(); |
| 3041 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 3042 | |
| 3043 | TextStyle textStyle; |
| 3044 | textStyle.setFontFamilies({SkString("Roboto")}); |
| 3045 | textStyle.setFontSize(50); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3046 | textStyle.setHeight(1); |
| 3047 | textStyle.setColor(SK_ColorBLACK); |
| 3048 | textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 3049 | SkFontStyle::kUpright_Slant)); |
| 3050 | |
| 3051 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3052 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3053 | builder.pop(); |
| 3054 | |
| 3055 | auto paragraph = builder.Build(); |
| 3056 | paragraph->layout(550); |
| 3057 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3058 | paragraph->paint(canvas.get(), 0, 0); |
| 3059 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3060 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3061 | |
| 3062 | REPORTER_ASSERT(reporter, impl->lines().size() == 2); |
| 3063 | |
| 3064 | RectHeightStyle heightStyle = RectHeightStyle::kMax; |
| 3065 | RectWidthStyle widthStyle = RectWidthStyle::kTight; |
| 3066 | SkScalar epsilon = 0.01f; |
| 3067 | { |
| 3068 | auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle); |
| 3069 | REPORTER_ASSERT(reporter, result.empty()); |
| 3070 | } |
| 3071 | { |
| 3072 | auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3073 | canvas.drawRects(SK_ColorRED, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3074 | REPORTER_ASSERT(reporter, result.size() == 1); |
| 3075 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 203.955f, epsilon)); |
| 3076 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon)); |
| 3077 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 232.373f, epsilon)); |
| 3078 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon)); |
| 3079 | } |
| 3080 | { |
| 3081 | auto result = paragraph->getRectsForRange(2, 4, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3082 | canvas.drawRects(SK_ColorBLUE, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3083 | REPORTER_ASSERT(reporter, result.size() == 1); |
| 3084 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 260.791f, epsilon)); |
| 3085 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon)); |
| 3086 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 317.626f, epsilon)); |
| 3087 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon)); |
| 3088 | } |
| 3089 | { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3090 | auto result = paragraph->getRectsForRange(4, 6, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3091 | canvas.drawRects(SK_ColorGREEN, result); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3092 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3093 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 317.626f, epsilon)); |
| 3094 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon)); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3095 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 358.494f, epsilon)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3096 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon)); |
| 3097 | } |
| 3098 | { |
| 3099 | auto result = paragraph->getRectsForRange(5, 6, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3100 | canvas.drawRects(SK_ColorYELLOW, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3101 | REPORTER_ASSERT(reporter, result.size() == 1); |
| 3102 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 346.044f, epsilon)); |
| 3103 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3104 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 358.494f, epsilon)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3105 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon)); |
| 3106 | } |
| 3107 | { |
| 3108 | auto result = paragraph->getRectsForRange(10, 12, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3109 | canvas.drawRects(SK_ColorCYAN, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3110 | REPORTER_ASSERT(reporter, result.size() == 1); |
| 3111 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 218.164f, epsilon)); |
| 3112 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 59.40625f, epsilon)); |
| 3113 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 275, epsilon)); |
| 3114 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 118, epsilon)); |
| 3115 | } |
| 3116 | { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3117 | auto result = paragraph->getRectsForRange(14, 18, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3118 | canvas.drawRects(SK_ColorBLACK, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3119 | REPORTER_ASSERT(reporter, result.size() == 1); |
| 3120 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 331.835f, epsilon)); |
| 3121 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 59.40625f, epsilon)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3122 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 419.189f, epsilon)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3123 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 118, epsilon)); |
| 3124 | } |
| 3125 | { |
| 3126 | auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle); |
| 3127 | REPORTER_ASSERT(reporter, result.empty()); |
| 3128 | } |
| 3129 | } |
| 3130 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3131 | // Checked: DIFF (line height rounding error) |
| 3132 | DEF_TEST(SkParagraph_GetRectsForRangeStrut, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3133 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3134 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3135 | TestCanvas canvas("SkParagraph_GetRectsForRangeStrut.png"); |
| 3136 | const char* text = "Chinese 字典"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3137 | const size_t len = strlen(text); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3138 | |
| 3139 | StrutStyle strutStyle; |
| 3140 | strutStyle.setStrutEnabled(true); |
| 3141 | strutStyle.setFontFamilies({SkString("Roboto")}); |
| 3142 | strutStyle.setFontSize(14.0); |
| 3143 | |
| 3144 | ParagraphStyle paragraphStyle; |
| 3145 | paragraphStyle.setStrutStyle(strutStyle); |
| 3146 | |
| 3147 | TextStyle textStyle; |
| 3148 | textStyle.setFontFamilies({SkString("Noto Sans CJK JP")}); |
| 3149 | textStyle.setFontSize(20); |
| 3150 | textStyle.setColor(SK_ColorBLACK); |
| 3151 | |
| 3152 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 3153 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3154 | builder.addText(text, len); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3155 | builder.pop(); |
| 3156 | |
| 3157 | auto paragraph = builder.Build(); |
| 3158 | paragraph->layout(550); |
| 3159 | paragraph->paint(canvas.get(), 0, 0); |
| 3160 | |
| 3161 | { |
| 3162 | auto result = paragraph->getRectsForRange(0, 10, RectHeightStyle::kTight, RectWidthStyle::kMax); |
| 3163 | canvas.drawRects(SK_ColorGREEN, result); |
| 3164 | REPORTER_ASSERT(reporter, result.size() == 1); |
| 3165 | } |
| 3166 | |
| 3167 | { |
| 3168 | auto result = paragraph->getRectsForRange(0, 10, RectHeightStyle::kStrut, RectWidthStyle::kMax); |
| 3169 | canvas.drawRects(SK_ColorRED, result); |
| 3170 | REPORTER_ASSERT(reporter, result.size() == 1); |
| 3171 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 0, EPSILON100)); |
| 3172 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 10.611f, EPSILON2)); |
| 3173 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 118.605f, EPSILON50)); |
| 3174 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 27.017f, EPSILON2)); |
| 3175 | } |
| 3176 | } |
| 3177 | |
| 3178 | // Checked: NO DIFF |
| 3179 | DEF_TEST(SkParagraph_GetRectsForRangeStrutFallback, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3180 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3181 | if (!fontCollection->fontsFound()) return; |
| 3182 | TestCanvas canvas("SkParagraph_GetRectsForRangeStrutFallback.png"); |
| 3183 | const char* text = "Chinese 字典"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3184 | const size_t len = strlen(text); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3185 | |
| 3186 | StrutStyle strutStyle; |
| 3187 | strutStyle.setStrutEnabled(false); |
| 3188 | |
| 3189 | ParagraphStyle paragraphStyle; |
| 3190 | paragraphStyle.setStrutStyle(strutStyle); |
| 3191 | |
| 3192 | TextStyle textStyle; |
| 3193 | textStyle.setFontFamilies({SkString("Noto Sans CJK JP")}); |
| 3194 | textStyle.setFontSize(20); |
| 3195 | textStyle.setColor(SK_ColorBLACK); |
| 3196 | |
| 3197 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 3198 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3199 | builder.addText(text, len); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3200 | builder.pop(); |
| 3201 | |
| 3202 | auto paragraph = builder.Build(); |
| 3203 | paragraph->layout(550); |
| 3204 | paragraph->paint(canvas.get(), 0, 0); |
| 3205 | |
| 3206 | |
| 3207 | auto result1 = paragraph->getRectsForRange(0, 10, RectHeightStyle::kTight, RectWidthStyle::kMax); |
| 3208 | canvas.drawRects(SK_ColorGREEN, result1); |
| 3209 | REPORTER_ASSERT(reporter, result1.size() == 1); |
| 3210 | |
| 3211 | auto result2 = paragraph->getRectsForRange(0, 10, RectHeightStyle::kStrut, RectWidthStyle::kMax); |
| 3212 | canvas.drawRects(SK_ColorRED, result2); |
| 3213 | REPORTER_ASSERT(reporter, result2.size() == 1); |
| 3214 | |
| 3215 | REPORTER_ASSERT(reporter, result1[0].rect == result2[0].rect); |
| 3216 | } |
| 3217 | |
| 3218 | // Checked: DIFF (small in numbers) |
| 3219 | DEF_TEST(SkParagraph_GetWordBoundaryParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3220 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3221 | if (!fontCollection->fontsFound()) return; |
| 3222 | TestCanvas canvas("SkParagraph_GetWordBoundaryParagraph.png"); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3223 | const char* text = "12345 67890 12345 67890 12345 67890 12345 " |
| 3224 | "67890 12345 67890 12345 67890 12345"; |
| 3225 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3226 | ParagraphStyle paragraphStyle; |
| 3227 | paragraphStyle.setTextAlign(TextAlign::kLeft); |
| 3228 | paragraphStyle.setMaxLines(10); |
| 3229 | paragraphStyle.turnHintingOff(); |
| 3230 | TextStyle textStyle; |
| 3231 | textStyle.setFontFamilies({SkString("Roboto")}); |
| 3232 | textStyle.setFontSize(52); |
| 3233 | textStyle.setLetterSpacing(1.19039f); |
| 3234 | textStyle.setWordSpacing(5); |
| 3235 | textStyle.setHeight(1.5); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3236 | textStyle.setHeightOverride(true); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3237 | textStyle.setColor(SK_ColorBLACK); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3238 | |
| 3239 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 3240 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3241 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3242 | builder.pop(); |
| 3243 | |
| 3244 | auto paragraph = builder.Build(); |
| 3245 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3246 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3247 | |
| 3248 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(0) == SkRange<size_t>(0, 5)); |
| 3249 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(1) == SkRange<size_t>(0, 5)); |
| 3250 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(2) == SkRange<size_t>(0, 5)); |
| 3251 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(3) == SkRange<size_t>(0, 5)); |
| 3252 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(4) == SkRange<size_t>(0, 5)); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3253 | auto boxes = paragraph->getRectsForRange(5, 6, RectHeightStyle::kMax, RectWidthStyle::kTight); |
| 3254 | canvas.drawLines(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3255 | |
| 3256 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(5) == SkRange<size_t>(5, 7)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3257 | boxes = paragraph->getRectsForRange(6, 7, RectHeightStyle::kMax, RectWidthStyle::kTight); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3258 | canvas.drawLines(SK_ColorRED, boxes); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3259 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3260 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(6) == SkRange<size_t>(5, 7)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3261 | boxes = paragraph->getRectsForRange(7, 8, RectHeightStyle::kMax, RectWidthStyle::kTight); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3262 | canvas.drawLines(SK_ColorRED, boxes); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3263 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3264 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(7) == SkRange<size_t>(7, 12)); |
| 3265 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(8) == SkRange<size_t>(7, 12)); |
| 3266 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(9) == SkRange<size_t>(7, 12)); |
| 3267 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(10) == SkRange<size_t>(7, 12)); |
| 3268 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(11) == SkRange<size_t>(7, 12)); |
| 3269 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(12) == SkRange<size_t>(12, 13)); |
| 3270 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(13) == SkRange<size_t>(13, 18)); |
| 3271 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(30) == SkRange<size_t>(30, 31)); |
| 3272 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3273 | boxes = paragraph->getRectsForRange(12, 13, RectHeightStyle::kMax, RectWidthStyle::kTight); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3274 | canvas.drawLines(SK_ColorRED, boxes); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3275 | boxes = paragraph->getRectsForRange(13, 14, RectHeightStyle::kMax, RectWidthStyle::kTight); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3276 | canvas.drawLines(SK_ColorRED, boxes); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3277 | boxes = paragraph->getRectsForRange(18, 19, RectHeightStyle::kMax, RectWidthStyle::kTight); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3278 | canvas.drawLines(SK_ColorRED, boxes); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3279 | boxes = paragraph->getRectsForRange(19, 20, RectHeightStyle::kMax, RectWidthStyle::kTight); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3280 | canvas.drawLines(SK_ColorRED, boxes); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3281 | boxes = paragraph->getRectsForRange(24, 25, RectHeightStyle::kMax, RectWidthStyle::kTight); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3282 | canvas.drawLines(SK_ColorRED, boxes); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3283 | boxes = paragraph->getRectsForRange(25, 26, RectHeightStyle::kMax, RectWidthStyle::kTight); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3284 | canvas.drawLines(SK_ColorRED, boxes); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3285 | boxes = paragraph->getRectsForRange(30, 31, RectHeightStyle::kMax, RectWidthStyle::kTight); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3286 | canvas.drawLines(SK_ColorRED, boxes); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3287 | boxes = paragraph->getRectsForRange(31, 32, RectHeightStyle::kMax, RectWidthStyle::kTight); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3288 | canvas.drawLines(SK_ColorRED, boxes); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3289 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3290 | auto outLen = static_cast<ParagraphImpl*>(paragraph.get())->text().size(); |
| 3291 | 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] | 3292 | } |
| 3293 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3294 | // Checked: DIFF (unclear) |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3295 | DEF_TEST(SkParagraph_SpacingParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3296 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3297 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3298 | TestCanvas canvas("SkParagraph_SpacingParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3299 | ParagraphStyle paragraph_style; |
| 3300 | paragraph_style.setMaxLines(10); |
| 3301 | paragraph_style.setTextAlign(TextAlign::kLeft); |
| 3302 | paragraph_style.turnHintingOff(); |
| 3303 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3304 | |
| 3305 | TextStyle text_style; |
| 3306 | text_style.setFontFamilies({SkString("Roboto")}); |
| 3307 | text_style.setFontSize(50); |
| 3308 | text_style.setLetterSpacing(20); |
| 3309 | text_style.setWordSpacing(0); |
| 3310 | text_style.setColor(SK_ColorBLACK); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3311 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3312 | builder.addText("H", 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3313 | builder.pop(); |
| 3314 | |
| 3315 | text_style.setLetterSpacing(10); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3316 | text_style.setWordSpacing(0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3317 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3318 | builder.addText("H", 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3319 | builder.pop(); |
| 3320 | |
| 3321 | text_style.setLetterSpacing(20); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3322 | text_style.setWordSpacing(0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3323 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3324 | builder.addText("H", 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3325 | builder.pop(); |
| 3326 | |
| 3327 | text_style.setLetterSpacing(0); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3328 | text_style.setWordSpacing(0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3329 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3330 | builder.addText("|", 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3331 | builder.pop(); |
| 3332 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3333 | const char* hSpace = "H "; |
| 3334 | const size_t len = strlen(hSpace); |
| 3335 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3336 | text_style.setLetterSpacing(0); |
| 3337 | text_style.setWordSpacing(20); |
| 3338 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3339 | builder.addText(hSpace, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3340 | builder.pop(); |
| 3341 | |
| 3342 | text_style.setLetterSpacing(0); |
| 3343 | text_style.setWordSpacing(0); |
| 3344 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3345 | builder.addText(hSpace, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3346 | builder.pop(); |
| 3347 | |
| 3348 | text_style.setLetterSpacing(0); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3349 | text_style.setLetterSpacing(0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3350 | text_style.setWordSpacing(20); |
| 3351 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3352 | builder.addText(hSpace, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3353 | builder.pop(); |
| 3354 | |
| 3355 | auto paragraph = builder.Build(); |
| 3356 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3357 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3358 | |
| 3359 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 3360 | REPORTER_ASSERT(reporter, impl->lines().size() == 1); |
| 3361 | size_t index = 0; |
| 3362 | impl->lines().begin()->scanStyles(StyleType::kLetterSpacing, |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 3363 | [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
| 3364 | ++index; |
| 3365 | return true; |
| 3366 | }); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3367 | REPORTER_ASSERT(reporter, index == 4); |
| 3368 | index = 0; |
| 3369 | impl->lines().begin()->scanStyles(StyleType::kWordSpacing, |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 3370 | [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
| 3371 | ++index; |
| 3372 | return true; |
| 3373 | }); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3374 | REPORTER_ASSERT(reporter, index == 4); |
| 3375 | } |
| 3376 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3377 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3378 | DEF_TEST(SkParagraph_LongWordParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3379 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3380 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3381 | TestCanvas canvas("SkParagraph_LongWordParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3382 | const char* text = |
| 3383 | "A " |
| 3384 | "veryverylongwordtoseewherethiswillwraporifitwillatallandifitdoesthenthat" |
| 3385 | "wouldbeagoodthingbecausethebreakingisworking."; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3386 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3387 | |
| 3388 | ParagraphStyle paragraph_style; |
| 3389 | paragraph_style.turnHintingOff(); |
| 3390 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3391 | |
| 3392 | TextStyle text_style; |
| 3393 | text_style.setFontFamilies({SkString("Roboto")}); |
| 3394 | text_style.setColor(SK_ColorRED); |
| 3395 | text_style.setFontSize(31); |
| 3396 | text_style.setLetterSpacing(0); |
| 3397 | text_style.setWordSpacing(0); |
| 3398 | text_style.setColor(SK_ColorBLACK); |
| 3399 | text_style.setHeight(1); |
| 3400 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3401 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3402 | builder.pop(); |
| 3403 | |
| 3404 | auto paragraph = builder.Build(); |
| 3405 | paragraph->layout(TestCanvasWidth / 2); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3406 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3407 | |
| 3408 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 3409 | REPORTER_ASSERT(reporter, impl->text().size() == std::string{text}.length()); |
| 3410 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 3411 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 3412 | REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3413 | REPORTER_ASSERT(reporter, impl->lines().size() == 4); |
| 3414 | |
| 3415 | REPORTER_ASSERT(reporter, impl->lines()[0].width() > TestCanvasWidth / 2 - 20); |
| 3416 | REPORTER_ASSERT(reporter, impl->lines()[1].width() > TestCanvasWidth / 2 - 20); |
| 3417 | REPORTER_ASSERT(reporter, impl->lines()[2].width() > TestCanvasWidth / 2 - 20); |
| 3418 | } |
| 3419 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3420 | // Checked: DIFF? |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3421 | DEF_TEST(SkParagraph_KernScaleParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3422 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3423 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3424 | TestCanvas canvas("SkParagraph_KernScaleParagraph.png"); |
| 3425 | |
| 3426 | const char* text1 = "AVAVAWAH A0 V0 VA To The Lo"; |
| 3427 | const char* text2 = " Dialog Text List lots of words to see " |
| 3428 | "if kerning works on a bigger set of characters AVAVAW"; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3429 | float scale = 3.0f; |
| 3430 | ParagraphStyle paragraph_style; |
| 3431 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3432 | TextStyle text_style; |
| 3433 | text_style.setFontFamilies({SkString("Droid Serif")}); |
| 3434 | text_style.setFontSize(100 / scale); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3435 | text_style.setColor(SK_ColorBLACK); |
| 3436 | |
| 3437 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3438 | builder.addText(text1, strlen(text1)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3439 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3440 | builder.addText("A", 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3441 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3442 | builder.addText("V", 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3443 | text_style.setFontSize(14 / scale); |
| 3444 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3445 | builder.addText(text2, strlen(text2)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3446 | builder.pop(); |
| 3447 | |
| 3448 | auto paragraph = builder.Build(); |
| 3449 | paragraph->layout(TestCanvasWidth / scale); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3450 | canvas.get()->scale(scale, scale); |
| 3451 | paragraph->paint(canvas.get(), 0, 0); |
| 3452 | canvas.get()->scale(1, 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3453 | |
| 3454 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3455 | |
Julia Lavrova | 6e6333f | 2019-06-17 10:34:10 -0400 | [diff] [blame] | 3456 | // 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] | 3457 | REPORTER_ASSERT(reporter, impl->lines().size() == 3); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3458 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].width(), 285.858f, EPSILON100)); |
| 3459 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[1].width(), 329.709f, EPSILON100)); |
| 3460 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[2].width(), 120.619f, EPSILON100)); |
| 3461 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].height(), 39.00f, EPSILON100)); |
| 3462 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[1].height(), 39.00f, EPSILON100)); |
| 3463 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[2].height(), 05.00f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3464 | } |
| 3465 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3466 | // Checked: DIFF+ |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3467 | DEF_TEST(SkParagraph_NewlineParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3468 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3469 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3470 | TestCanvas canvas("SkParagraph_NewlineParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3471 | const char* text = |
| 3472 | "line1\nline2 test1 test2 test3 test4 test5 test6 test7\nline3\n\nline4 " |
| 3473 | "test1 test2 test3 test4"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3474 | const size_t len = strlen(text); |
| 3475 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3476 | ParagraphStyle paragraph_style; |
| 3477 | paragraph_style.turnHintingOff(); |
| 3478 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3479 | |
| 3480 | TextStyle text_style; |
| 3481 | text_style.setFontFamilies({SkString("Roboto")}); |
| 3482 | text_style.setColor(SK_ColorRED); |
| 3483 | text_style.setFontSize(60); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3484 | text_style.setColor(SK_ColorBLACK); |
| 3485 | text_style.setHeight(1); |
| 3486 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3487 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3488 | builder.pop(); |
| 3489 | |
| 3490 | auto paragraph = builder.Build(); |
| 3491 | paragraph->layout(TestCanvasWidth - 300); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3492 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3493 | |
| 3494 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 3495 | // Minikin does not count empty lines but SkParagraph does |
| 3496 | REPORTER_ASSERT(reporter, impl->lines().size() == 7); |
| 3497 | |
| 3498 | REPORTER_ASSERT(reporter, impl->lines()[0].offset().fY == 0); |
| 3499 | REPORTER_ASSERT(reporter, impl->lines()[1].offset().fY == 70); |
| 3500 | REPORTER_ASSERT(reporter, impl->lines()[2].offset().fY == 140); |
| 3501 | REPORTER_ASSERT(reporter, impl->lines()[3].offset().fY == 210); |
| 3502 | REPORTER_ASSERT(reporter, impl->lines()[4].offset().fY == 280); // Empty line |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 3503 | REPORTER_ASSERT(reporter, impl->lines()[5].offset().fY == 296); |
| 3504 | REPORTER_ASSERT(reporter, impl->lines()[6].offset().fY == 366); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3505 | } |
| 3506 | |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 3507 | // TODO: Fix underline |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3508 | DEF_TEST(SkParagraph_EmojiParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3509 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3510 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3511 | TestCanvas canvas("SkParagraph_EmojiParagraph.png"); |
| 3512 | const char* text = |
| 3513 | "😀😃😄😁😆😅😂🤣☺😇🙂😍😡😟😢😻👽💩👍👎🙏👌👋👄👁👦👼👨🚀👨🚒🙋♂️👳👨👨👧👧\ |
| 3514 | 💼👡👠☂🐶🐰🐻🐼🐷🐒🐵🐔🐧🐦🐋🐟🐡🕸🐌🐴🐊🐄🐪🐘🌸🌏🔥🌟🌚🌝💦💧\ |
| 3515 | ❄🍕🍔🍟🥝🍱🕶🎩🏈⚽🚴♀️🎻🎼🎹🚨🚎🚐⚓🛳🚀🚁🏪🏢🖱⏰📱💾💉📉🛏🔑🔓\ |
| 3516 | 📁🗓📊❤💯🚫🔻♠♣🕓❗🏳🏁🏳️🌈🇮🇹🇱🇷🇺🇸🇬🇧🇨🇳🇧🇴"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3517 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3518 | |
| 3519 | ParagraphStyle paragraph_style; |
| 3520 | paragraph_style.turnHintingOff(); |
| 3521 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3522 | |
| 3523 | TextStyle text_style; |
| 3524 | text_style.setFontFamilies({SkString("Noto Color Emoji")}); |
| 3525 | text_style.setFontSize(50); |
| 3526 | text_style.setDecoration(TextDecoration::kUnderline); |
| 3527 | text_style.setColor(SK_ColorBLACK); |
| 3528 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3529 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3530 | builder.pop(); |
| 3531 | |
| 3532 | auto paragraph = builder.Build(); |
| 3533 | paragraph->layout(TestCanvasWidth); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3534 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3535 | |
Julia Lavrova | 3281b96 | 2019-12-02 11:32:25 -0500 | [diff] [blame] | 3536 | REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == 0); |
| 3537 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3538 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3539 | |
| 3540 | REPORTER_ASSERT(reporter, impl->lines().size() == 8); |
| 3541 | for (auto& line : impl->lines()) { |
| 3542 | if (&line != impl->lines().end() - 1) { |
| 3543 | REPORTER_ASSERT(reporter, line.width() == 998.25f); |
| 3544 | } else { |
| 3545 | REPORTER_ASSERT(reporter, line.width() < 998.25f); |
| 3546 | } |
| 3547 | REPORTER_ASSERT(reporter, line.height() == 59); |
| 3548 | } |
| 3549 | } |
| 3550 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3551 | // Checked: DIFF+ |
| 3552 | DEF_TEST(SkParagraph_EmojiMultiLineRectsParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3553 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3554 | if (!fontCollection->fontsFound()) return; |
| 3555 | TestCanvas canvas("SkParagraph_EmojiMultiLineRectsParagraph.png"); |
| 3556 | const char* text = |
| 3557 | "👩👩👦👩👩👧👧🇺🇸👩👩👦👩👩👧👧i🇺🇸👩👩👦👩👩👧👧🇺🇸👩👩👦👩👩👧👧🇺🇸" |
| 3558 | "👩👩👦👩👩👧👧🇺🇸👩👩👦👩👩👧👧🇺🇸👩👩👦👩👩👧👧🇺🇸👩👩👦👩👩👧👧🇺🇸" |
| 3559 | "👩👩👦👩👩👧👧🇺🇸👩👩👦👩👩👧👧🇺🇸👩👩👦👩👩👧👧🇺🇸👩👩👦👩👩👧👧🇺🇸" |
| 3560 | "👩👩👦👩👩👧👧🇺🇸👩👩👦👩👩👧👧🇺🇸👩👩👦👩👩👧👧🇺🇸👩👩👦👩👩👧👧🇺🇸" |
| 3561 | "❄🍕🍔🍟🥝🍱🕶🎩🏈⚽🚴♀️🎻🎼🎹🚨🚎🚐⚓🛳🚀🚁🏪🏢🖱⏰📱💾💉📉🛏🔑🔓" |
| 3562 | "📁🗓📊❤💯🚫🔻♠♣🕓❗🏳🏁🏳️🌈🇮🇹🇱🇷🇺🇸🇬🇧🇨🇳🇧🇴"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3563 | const size_t len = strlen(text); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3564 | |
| 3565 | ParagraphStyle paragraph_style; |
| 3566 | paragraph_style.turnHintingOff(); |
| 3567 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3568 | |
| 3569 | TextStyle text_style; |
| 3570 | text_style.setFontFamilies({SkString("Noto Color Emoji")}); |
| 3571 | text_style.setFontSize(50); |
| 3572 | text_style.setColor(SK_ColorBLACK); |
| 3573 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3574 | builder.addText(text, len); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3575 | builder.pop(); |
| 3576 | |
| 3577 | auto paragraph = builder.Build(); |
| 3578 | paragraph->layout(TestCanvasWidth - 300); |
| 3579 | paragraph->paint(canvas.get(), 0, 0); |
| 3580 | |
| 3581 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 3582 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 3583 | |
| 3584 | auto result = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style); |
| 3585 | REPORTER_ASSERT(reporter, result.size() == 0); |
| 3586 | |
| 3587 | result = paragraph->getRectsForRange(0, 119, rect_height_style, rect_width_style); |
| 3588 | REPORTER_ASSERT(reporter, result.size() == 2); |
| 3589 | canvas.drawRects(SK_ColorRED, result); |
| 3590 | |
| 3591 | result = paragraph->getRectsForRange(122, 132, rect_height_style, rect_width_style); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 3592 | REPORTER_ASSERT(reporter, result.size() == 0); // There is no single glyph |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3593 | canvas.drawRects(SK_ColorBLUE, result); |
| 3594 | |
| 3595 | auto pos = paragraph->getGlyphPositionAtCoordinate(610, 100).position; |
| 3596 | result = paragraph->getRectsForRange(0, pos, rect_height_style, rect_width_style); |
| 3597 | REPORTER_ASSERT(reporter, result.size() == 2); |
| 3598 | canvas.drawRects(SK_ColorGREEN, result); |
| 3599 | |
| 3600 | pos = paragraph->getGlyphPositionAtCoordinate(580, 100).position; |
| 3601 | result = paragraph->getRectsForRange(0, pos, rect_height_style, rect_width_style); |
| 3602 | REPORTER_ASSERT(reporter, result.size() == 2); |
| 3603 | canvas.drawRects(SK_ColorGREEN, result); |
| 3604 | |
| 3605 | pos = paragraph->getGlyphPositionAtCoordinate(560, 100).position; |
| 3606 | result = paragraph->getRectsForRange(0, pos, rect_height_style, rect_width_style); |
| 3607 | REPORTER_ASSERT(reporter, result.size() == 2); |
| 3608 | canvas.drawRects(SK_ColorGREEN, result); |
| 3609 | } |
| 3610 | |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3611 | DEF_TEST(SkParagraph_HyphenBreakParagraph, reporter) { |
| 3612 | SkDebugf("Hyphens are not implemented, and will not be implemented soon.\n"); |
| 3613 | } |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3614 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3615 | // Checked: DIFF (line breaking) |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3616 | DEF_TEST(SkParagraph_RepeatLayoutParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3617 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3618 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3619 | TestCanvas canvas("SkParagraph_RepeatLayoutParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3620 | const char* text = |
| 3621 | "Sentence to layout at diff widths to get diff line counts. short words " |
| 3622 | "short words short words short words short words short words short words " |
| 3623 | "short words short words short words short words short words short words " |
| 3624 | "end"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3625 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3626 | |
| 3627 | ParagraphStyle paragraph_style; |
| 3628 | paragraph_style.turnHintingOff(); |
| 3629 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3630 | |
| 3631 | TextStyle text_style; |
| 3632 | text_style.setFontFamilies({SkString("Roboto")}); |
| 3633 | text_style.setFontSize(31); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3634 | text_style.setColor(SK_ColorBLACK); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3635 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3636 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3637 | builder.pop(); |
| 3638 | |
| 3639 | auto paragraph = builder.Build(); |
| 3640 | paragraph->layout(300); |
| 3641 | |
| 3642 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 3643 | // Some of the formatting lazily done on paint |
| 3644 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 3645 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
| 3646 | REPORTER_ASSERT(reporter, impl->lines().size() == 12); |
| 3647 | |
| 3648 | paragraph->layout(600); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3649 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3650 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 3651 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
| 3652 | REPORTER_ASSERT(reporter, impl->lines().size() == 6); |
| 3653 | } |
| 3654 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3655 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3656 | DEF_TEST(SkParagraph_Ellipsize, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3657 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3658 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3659 | TestCanvas canvas("SkParagraph_Ellipsize.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3660 | const char* text = |
| 3661 | "This is a very long sentence to test if the text will properly wrap " |
| 3662 | "around and go to the next line. Sometimes, short sentence. Longer " |
| 3663 | "sentences are okay too because they are nessecary. Very short. "; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3664 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3665 | |
| 3666 | ParagraphStyle paragraph_style; |
| 3667 | paragraph_style.setMaxLines(1); |
| 3668 | paragraph_style.setEllipsis(u"\u2026"); |
| 3669 | paragraph_style.turnHintingOff(); |
| 3670 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3671 | |
| 3672 | TextStyle text_style; |
| 3673 | text_style.setFontFamilies({SkString("Roboto")}); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3674 | text_style.setColor(SK_ColorBLACK); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3675 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3676 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3677 | builder.pop(); |
| 3678 | |
| 3679 | auto paragraph = builder.Build(); |
| 3680 | paragraph->layout(TestCanvasWidth); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3681 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3682 | |
| 3683 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3684 | |
| 3685 | // Check that the ellipsizer limited the text to one line and did not wrap to a second line. |
| 3686 | REPORTER_ASSERT(reporter, impl->lines().size() == 1); |
| 3687 | |
| 3688 | auto& line = impl->lines()[0]; |
| 3689 | REPORTER_ASSERT(reporter, line.ellipsis() != nullptr); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 3690 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3691 | } |
| 3692 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3693 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3694 | DEF_TEST(SkParagraph_UnderlineShiftParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3695 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3696 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3697 | TestCanvas canvas("SkParagraph_UnderlineShiftParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3698 | const char* text1 = "fluttser "; |
| 3699 | const char* text2 = "mdje"; |
| 3700 | const char* text3 = "fluttser mdje"; |
| 3701 | |
| 3702 | ParagraphStyle paragraph_style; |
| 3703 | paragraph_style.turnHintingOff(); |
| 3704 | paragraph_style.setTextAlign(TextAlign::kLeft); |
| 3705 | paragraph_style.setMaxLines(2); |
| 3706 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3707 | |
| 3708 | TextStyle text_style; |
| 3709 | text_style.setFontFamilies({SkString("Roboto")}); |
| 3710 | text_style.setColor(SK_ColorBLACK); |
| 3711 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3712 | builder.addText(text1, strlen(text1)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3713 | text_style.setDecoration(TextDecoration::kUnderline); |
| 3714 | text_style.setDecorationColor(SK_ColorBLACK); |
| 3715 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3716 | builder.addText(text2, strlen(text2)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3717 | builder.pop(); |
| 3718 | |
| 3719 | auto paragraph = builder.Build(); |
| 3720 | paragraph->layout(TestCanvasWidth); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3721 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3722 | |
| 3723 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 3724 | |
| 3725 | ParagraphBuilderImpl builder1(paragraph_style, fontCollection); |
| 3726 | text_style.setDecoration(TextDecoration::kNoDecoration); |
| 3727 | builder1.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3728 | builder1.addText(text3, strlen(text3)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3729 | builder1.pop(); |
| 3730 | |
| 3731 | auto paragraph1 = builder1.Build(); |
| 3732 | paragraph1->layout(TestCanvasWidth); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3733 | paragraph1->paint(canvas.get(), 0, 25); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3734 | |
| 3735 | auto impl1 = static_cast<ParagraphImpl*>(paragraph1.get()); |
| 3736 | |
| 3737 | REPORTER_ASSERT(reporter, impl->lines().size() == 1); |
| 3738 | REPORTER_ASSERT(reporter, impl1->lines().size() == 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3739 | |
| 3740 | auto rect = paragraph->getRectsForRange(0, 12, RectHeightStyle::kMax, RectWidthStyle::kTight) |
| 3741 | .front() |
| 3742 | .rect; |
| 3743 | auto rect1 = paragraph1->getRectsForRange(0, 12, RectHeightStyle::kMax, RectWidthStyle::kTight) |
| 3744 | .front() |
| 3745 | .rect; |
| 3746 | REPORTER_ASSERT(reporter, rect.fLeft == rect1.fLeft); |
| 3747 | REPORTER_ASSERT(reporter, rect.fRight == rect1.fRight); |
| 3748 | |
| 3749 | for (size_t i = 0; i < 12; ++i) { |
| 3750 | auto r = |
| 3751 | paragraph->getRectsForRange(i, i + 1, RectHeightStyle::kMax, RectWidthStyle::kTight) |
| 3752 | .front() |
| 3753 | .rect; |
| 3754 | auto r1 = |
| 3755 | paragraph1 |
| 3756 | ->getRectsForRange(i, i + 1, RectHeightStyle::kMax, RectWidthStyle::kTight) |
| 3757 | .front() |
| 3758 | .rect; |
| 3759 | |
| 3760 | REPORTER_ASSERT(reporter, r.fLeft == r1.fLeft); |
| 3761 | REPORTER_ASSERT(reporter, r.fRight == r1.fRight); |
| 3762 | } |
| 3763 | } |
| 3764 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3765 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3766 | DEF_TEST(SkParagraph_SimpleShadow, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3767 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3768 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3769 | TestCanvas canvas("SkParagraph_SimpleShadow.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3770 | const char* text = "Hello World Text Dialog"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3771 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3772 | |
| 3773 | ParagraphStyle paragraph_style; |
| 3774 | paragraph_style.turnHintingOff(); |
| 3775 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3776 | |
| 3777 | TextStyle text_style; |
| 3778 | text_style.setFontFamilies({SkString("Roboto")}); |
| 3779 | text_style.setColor(SK_ColorBLACK); |
| 3780 | text_style.addShadow(TextShadow(SK_ColorBLACK, SkPoint::Make(2.0f, 2.0f), 1.0)); |
| 3781 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3782 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3783 | |
| 3784 | auto paragraph = builder.Build(); |
| 3785 | paragraph->layout(TestCanvasWidth); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3786 | paragraph->paint(canvas.get(), 10.0, 15.0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3787 | |
| 3788 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 3789 | |
| 3790 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 3791 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
| 3792 | size_t index = 0; |
| 3793 | for (auto& line : impl->lines()) { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3794 | line.scanStyles(StyleType::kShadow, |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 3795 | [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3796 | REPORTER_ASSERT(reporter, index == 0 && style.equals(text_style)); |
| 3797 | ++index; |
| 3798 | return true; |
| 3799 | }); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3800 | } |
| 3801 | } |
| 3802 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3803 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3804 | DEF_TEST(SkParagraph_ComplexShadow, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3805 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3806 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3807 | TestCanvas canvas("SkParagraph_ComplexShadow.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3808 | const char* text = "Text Chunk "; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3809 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3810 | |
| 3811 | ParagraphStyle paragraph_style; |
| 3812 | paragraph_style.turnHintingOff(); |
| 3813 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3814 | |
| 3815 | TextStyle text_style; |
| 3816 | text_style.setFontFamilies({SkString("Roboto")}); |
| 3817 | text_style.setColor(SK_ColorBLACK); |
| 3818 | text_style.addShadow(TextShadow(SK_ColorBLACK, SkPoint::Make(2.0f, 2.0f), 1.0f)); |
| 3819 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3820 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3821 | |
| 3822 | text_style.addShadow(TextShadow(SK_ColorRED, SkPoint::Make(2.0f, 2.0f), 5.0f)); |
| 3823 | text_style.addShadow(TextShadow(SK_ColorGREEN, SkPoint::Make(10.0f, -5.0f), 3.0f)); |
| 3824 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3825 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3826 | builder.pop(); |
| 3827 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3828 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3829 | |
| 3830 | text_style.addShadow(TextShadow(SK_ColorRED, SkPoint::Make(0.0f, 1.0f), 0.0f)); |
| 3831 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3832 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3833 | builder.pop(); |
| 3834 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3835 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3836 | |
| 3837 | auto paragraph = builder.Build(); |
| 3838 | paragraph->layout(TestCanvasWidth); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3839 | paragraph->paint(canvas.get(), 10.0, 15.0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3840 | |
| 3841 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 3842 | |
| 3843 | size_t index = 0; |
| 3844 | for (auto& line : impl->lines()) { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3845 | line.scanStyles(StyleType::kShadow, |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 3846 | [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3847 | ++index; |
| 3848 | switch (index) { |
| 3849 | case 1: |
| 3850 | REPORTER_ASSERT(reporter, style.getShadowNumber() == 1); |
| 3851 | break; |
| 3852 | case 2: |
| 3853 | REPORTER_ASSERT(reporter, style.getShadowNumber() == 3); |
| 3854 | break; |
| 3855 | case 3: |
| 3856 | REPORTER_ASSERT(reporter, style.getShadowNumber() == 1); |
| 3857 | break; |
| 3858 | case 4: |
| 3859 | REPORTER_ASSERT(reporter, style.getShadowNumber() == 4); |
| 3860 | REPORTER_ASSERT(reporter, style.equals(text_style)); |
| 3861 | break; |
| 3862 | case 5: |
| 3863 | REPORTER_ASSERT(reporter, style.getShadowNumber() == 1); |
| 3864 | break; |
| 3865 | default: |
| 3866 | REPORTER_ASSERT(reporter, false); |
| 3867 | } |
| 3868 | return true; |
| 3869 | }); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3870 | } |
| 3871 | } |
| 3872 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3873 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3874 | DEF_TEST(SkParagraph_BaselineParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3875 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3876 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3877 | TestCanvas canvas("SkParagraph_BaselineParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3878 | const char* text = |
| 3879 | "左線読設Byg後碁給能上目秘使約。満毎冠行来昼本可必図将発確年。今属場育" |
| 3880 | "図情闘陰野高備込制詩西校客。審対江置講今固残必託地集済決維駆年策。立得"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3881 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3882 | |
| 3883 | ParagraphStyle paragraph_style; |
| 3884 | paragraph_style.turnHintingOff(); |
| 3885 | paragraph_style.setMaxLines(14); |
| 3886 | paragraph_style.setTextAlign(TextAlign::kJustify); |
| 3887 | paragraph_style.setHeight(1.5); |
| 3888 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3889 | |
| 3890 | TextStyle text_style; |
| 3891 | text_style.setFontFamilies({SkString("Source Han Serif CN")}); |
| 3892 | text_style.setColor(SK_ColorBLACK); |
| 3893 | text_style.setFontSize(55); |
| 3894 | text_style.setLetterSpacing(2); |
| 3895 | text_style.setDecorationStyle(TextDecorationStyle::kSolid); |
| 3896 | text_style.setDecorationColor(SK_ColorBLACK); |
| 3897 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3898 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3899 | builder.pop(); |
| 3900 | |
| 3901 | auto paragraph = builder.Build(); |
| 3902 | paragraph->layout(TestCanvasWidth - 100); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3903 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3904 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3905 | SkRect rect1 = SkRect::MakeXYWH(0, paragraph->getIdeographicBaseline(), |
| 3906 | paragraph->getMaxWidth(), |
| 3907 | paragraph->getIdeographicBaseline()); |
| 3908 | SkRect rect2 = SkRect::MakeXYWH(0, paragraph->getAlphabeticBaseline(), |
| 3909 | paragraph->getMaxWidth(), |
| 3910 | paragraph->getAlphabeticBaseline()); |
| 3911 | canvas.drawLine(SK_ColorRED, rect1, false); |
| 3912 | canvas.drawLine(SK_ColorGREEN, rect2, false); |
| 3913 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3914 | REPORTER_ASSERT(reporter, |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3915 | SkScalarNearlyEqual(paragraph->getIdeographicBaseline(), 79.035f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3916 | REPORTER_ASSERT(reporter, |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3917 | SkScalarNearlyEqual(paragraph->getAlphabeticBaseline(), 63.305f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3918 | } |
| 3919 | |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3920 | // Checked: NO DIFF (number of runs only) |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3921 | DEF_TEST(SkParagraph_FontFallbackParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3922 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3923 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3924 | TestCanvas canvas("SkParagraph_FontFallbackParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3925 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3926 | const char* text1 = "Roboto 字典 "; // Roboto + unresolved |
| 3927 | const char* text2 = "Homemade Apple 字典"; // Homemade Apple + Noto Sans... |
| 3928 | const char* text3 = "Chinese 字典"; // Homemade Apple + Source Han |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3929 | |
| 3930 | ParagraphStyle paragraph_style; |
| 3931 | paragraph_style.turnHintingOff(); |
| 3932 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3933 | |
| 3934 | TextStyle text_style; |
| 3935 | text_style.setFontFamilies({ |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3936 | SkString("Not a real font"), |
| 3937 | SkString("Also a fake font"), |
| 3938 | SkString("So fake it is obvious"), |
| 3939 | SkString("Next one should be a real font..."), |
| 3940 | SkString("Roboto"), |
| 3941 | SkString("another fake one in between"), |
| 3942 | SkString("Homemade Apple"), |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3943 | }); |
| 3944 | text_style.setColor(SK_ColorBLACK); |
| 3945 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3946 | builder.addText(text1, strlen(text1)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3947 | |
| 3948 | text_style.setFontFamilies({ |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3949 | SkString("Not a real font"), |
| 3950 | SkString("Also a fake font"), |
| 3951 | SkString("So fake it is obvious"), |
| 3952 | SkString("Homemade Apple"), |
| 3953 | SkString("Next one should be a real font..."), |
| 3954 | SkString("Roboto"), |
| 3955 | SkString("another fake one in between"), |
| 3956 | SkString("Noto Sans CJK JP"), |
| 3957 | SkString("Source Han Serif CN"), |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3958 | }); |
| 3959 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3960 | builder.addText(text2, strlen(text2)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3961 | |
| 3962 | text_style.setFontFamilies({ |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3963 | SkString("Not a real font"), |
| 3964 | SkString("Also a fake font"), |
| 3965 | SkString("So fake it is obvious"), |
| 3966 | SkString("Homemade Apple"), |
| 3967 | SkString("Next one should be a real font..."), |
| 3968 | SkString("Roboto"), |
| 3969 | SkString("another fake one in between"), |
| 3970 | SkString("Source Han Serif CN"), |
| 3971 | SkString("Noto Sans CJK JP"), |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3972 | }); |
| 3973 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3974 | builder.addText(text3, strlen(text3)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3975 | |
| 3976 | builder.pop(); |
| 3977 | |
| 3978 | auto paragraph = builder.Build(); |
Julia Lavrova | 3281b96 | 2019-12-02 11:32:25 -0500 | [diff] [blame] | 3979 | REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == -1); // Not shaped yet |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3980 | paragraph->layout(TestCanvasWidth); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3981 | paragraph->paint(canvas.get(), 10.0, 15.0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3982 | |
Julia Lavrova | 3281b96 | 2019-12-02 11:32:25 -0500 | [diff] [blame] | 3983 | REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == 2); // From the text1 |
| 3984 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3985 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 3986 | |
| 3987 | // Font resolution in Skia produces 6 runs because 2 parts of "Roboto 字典 " have different |
| 3988 | // script (Minikin merges the first 2 into one because of unresolved) [Apple + Unresolved ] |
| 3989 | // [Apple + Noto] [Apple + Han] |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3990 | REPORTER_ASSERT(reporter, impl->runs().size() == 7); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3991 | |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 3992 | auto robotoAdvance = impl->runs()[0].advance().fX + |
| 3993 | impl->runs()[1].advance().fX + |
| 3994 | impl->runs()[2].advance().fX; |
| 3995 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(robotoAdvance, 64.199f, EPSILON50)); |
| 3996 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->runs()[3].advance().fX, 139.125f, EPSILON100)); |
| 3997 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->runs()[4].advance().fX, 27.999f, EPSILON100)); |
| 3998 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->runs()[5].advance().fX, 62.248f, EPSILON100)); |
| 3999 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->runs()[6].advance().fX, 27.999f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4000 | |
| 4001 | // When a different font is resolved, then the metrics are different. |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4002 | REPORTER_ASSERT(reporter, impl->runs()[4].correctAscent() != impl->runs()[6].correctAscent()); |
| 4003 | REPORTER_ASSERT(reporter, impl->runs()[4].correctDescent() != impl->runs()[6].correctDescent()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4004 | } |
| 4005 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4006 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4007 | DEF_TEST(SkParagraph_StrutParagraph1, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4008 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4009 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4010 | TestCanvas canvas("SkParagraph_StrutParagraph1.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4011 | // The chinese extra height should be absorbed by the strut. |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4012 | const char* text = "01234満毎冠p来É本可\nabcd\n満毎É行p昼本可"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4013 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4014 | |
| 4015 | ParagraphStyle paragraph_style; |
| 4016 | paragraph_style.setMaxLines(10); |
| 4017 | paragraph_style.setTextAlign(TextAlign::kLeft); |
| 4018 | paragraph_style.turnHintingOff(); |
| 4019 | |
| 4020 | StrutStyle strut_style; |
| 4021 | strut_style.setStrutEnabled(true); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4022 | strut_style.setFontFamilies({SkString("BlahFake"), SkString("Ahem")}); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4023 | strut_style.setFontSize(50); |
| 4024 | strut_style.setHeight(1.8f); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4025 | strut_style.setHeightOverride(true); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4026 | strut_style.setLeading(0.1f); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4027 | paragraph_style.setStrutStyle(strut_style); |
| 4028 | |
| 4029 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4030 | |
| 4031 | TextStyle text_style; |
| 4032 | text_style.setFontFamilies({SkString("Ahem")}); |
| 4033 | text_style.setFontSize(50); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4034 | 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] | 4035 | text_style.setColor(SK_ColorBLACK); |
| 4036 | text_style.setHeight(0.5f); |
| 4037 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4038 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4039 | builder.pop(); |
| 4040 | |
| 4041 | auto paragraph = builder.Build(); |
| 4042 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4043 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4044 | |
| 4045 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4046 | REPORTER_ASSERT(reporter, impl->lines().size() == 4); |
| 4047 | |
| 4048 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 4049 | RectHeightStyle rect_height_max_style = RectHeightStyle::kMax; |
| 4050 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4051 | { |
| 4052 | auto boxes = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style); |
| 4053 | REPORTER_ASSERT(reporter, boxes.empty()); |
| 4054 | } |
| 4055 | { |
| 4056 | auto boxes = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4057 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4058 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4059 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, EPSILON100)); |
| 4060 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 34.5f, EPSILON100)); |
| 4061 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, EPSILON100)); |
| 4062 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 84.5f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4063 | } |
| 4064 | { |
| 4065 | 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] | 4066 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4067 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4068 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, EPSILON100)); |
| 4069 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 34.5f, EPSILON100)); |
| 4070 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, EPSILON100)); |
| 4071 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 95, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4072 | } |
| 4073 | { |
| 4074 | auto boxes = paragraph->getRectsForRange(6, 10, rect_height_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4075 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4076 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4077 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, EPSILON100)); |
| 4078 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 34.5f, EPSILON100)); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4079 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4080 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 84.5f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4081 | } |
| 4082 | { |
| 4083 | 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] | 4084 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4085 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4086 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, EPSILON100)); |
| 4087 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 34.5f, EPSILON100)); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4088 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4089 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 95, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4090 | } |
| 4091 | { |
| 4092 | 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] | 4093 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4094 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4095 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, EPSILON100)); |
| 4096 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 224.5f, EPSILON100)); |
| 4097 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 100, EPSILON100)); |
| 4098 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 285, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4099 | } |
| 4100 | { |
| 4101 | 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] | 4102 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4103 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4104 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 50, EPSILON100)); |
| 4105 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 319.5f, EPSILON100)); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4106 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 300, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4107 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 380, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4108 | } |
| 4109 | } |
| 4110 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4111 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4112 | DEF_TEST(SkParagraph_StrutParagraph2, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4113 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4114 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4115 | TestCanvas canvas("SkParagraph_StrutParagraph2.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4116 | // The chinese extra height should be absorbed by the strut. |
| 4117 | const char* text = "01234ABCDEFGH\nabcd\nABCDEFGH"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4118 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4119 | |
| 4120 | ParagraphStyle paragraph_style; |
| 4121 | paragraph_style.setMaxLines(10); |
| 4122 | paragraph_style.setTextAlign(TextAlign::kLeft); |
| 4123 | paragraph_style.turnHintingOff(); |
| 4124 | |
| 4125 | StrutStyle strut_style; |
| 4126 | |
| 4127 | strut_style.setStrutEnabled(true); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4128 | strut_style.setFontFamilies({SkString("Ahem")}); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4129 | strut_style.setFontSize(50); |
| 4130 | strut_style.setHeight(1.6f); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4131 | strut_style.setHeightOverride(true); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4132 | paragraph_style.setStrutStyle(strut_style); |
| 4133 | |
| 4134 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4135 | |
| 4136 | TextStyle text_style; |
| 4137 | text_style.setFontFamilies({SkString("Ahem")}); |
| 4138 | text_style.setFontSize(50); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4139 | text_style.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 4140 | SkFontStyle::kUpright_Slant)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4141 | text_style.setColor(SK_ColorBLACK); |
| 4142 | text_style.setHeight(1); |
| 4143 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4144 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4145 | builder.pop(); |
| 4146 | |
| 4147 | auto paragraph = builder.Build(); |
| 4148 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4149 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4150 | |
| 4151 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 4152 | // Font is not resolved and the first line does not fit |
| 4153 | REPORTER_ASSERT(reporter, impl->lines().size() == 4); |
| 4154 | |
| 4155 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 4156 | RectHeightStyle rect_height_max_style = RectHeightStyle::kMax; |
| 4157 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4158 | { |
| 4159 | auto boxes = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style); |
| 4160 | REPORTER_ASSERT(reporter, boxes.empty()); |
| 4161 | } |
| 4162 | { |
| 4163 | auto boxes = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4164 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4165 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4166 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, EPSILON100)); |
| 4167 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 24, EPSILON100)); |
| 4168 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, EPSILON100)); |
| 4169 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 74, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4170 | } |
| 4171 | { |
| 4172 | 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] | 4173 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4174 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4175 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, EPSILON100)); |
| 4176 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 24, EPSILON100)); |
| 4177 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, EPSILON100)); |
| 4178 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 80, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4179 | } |
| 4180 | { |
| 4181 | auto boxes = paragraph->getRectsForRange(6, 10, rect_height_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4182 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4183 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4184 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, EPSILON100)); |
| 4185 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 24, EPSILON100)); |
| 4186 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, EPSILON100)); |
| 4187 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 74, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4188 | } |
| 4189 | { |
| 4190 | 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] | 4191 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4192 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4193 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, EPSILON100)); |
| 4194 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 24, EPSILON100)); |
| 4195 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, EPSILON100)); |
| 4196 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 80, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4197 | } |
| 4198 | { |
| 4199 | 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] | 4200 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4201 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4202 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, EPSILON100)); |
| 4203 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 184, EPSILON100)); |
| 4204 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 100, EPSILON100)); |
| 4205 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 240, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4206 | } |
| 4207 | { |
| 4208 | 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] | 4209 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4210 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4211 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 50, EPSILON100)); |
| 4212 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 264, EPSILON100)); |
| 4213 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 300, EPSILON100)); |
| 4214 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 320, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4215 | } |
| 4216 | } |
| 4217 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4218 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4219 | DEF_TEST(SkParagraph_StrutParagraph3, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4220 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4221 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4222 | TestCanvas canvas("SkParagraph_StrutParagraph3.png"); |
| 4223 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4224 | // The chinese extra height should be absorbed by the strut. |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4225 | const char* text = "01234満毎p行来昼本可\nabcd\n満毎冠行来昼本可"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4226 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4227 | |
| 4228 | ParagraphStyle paragraph_style; |
| 4229 | paragraph_style.setMaxLines(10); |
| 4230 | paragraph_style.setTextAlign(TextAlign::kLeft); |
| 4231 | paragraph_style.turnHintingOff(); |
| 4232 | |
| 4233 | StrutStyle strut_style; |
| 4234 | strut_style.setStrutEnabled(true); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4235 | strut_style.setFontFamilies({SkString("Ahem")}); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4236 | strut_style.setFontSize(50); |
| 4237 | strut_style.setHeight(1.2f); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4238 | strut_style.setHeightOverride(true); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4239 | paragraph_style.setStrutStyle(strut_style); |
| 4240 | |
| 4241 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4242 | |
| 4243 | TextStyle text_style; |
| 4244 | text_style.setFontFamilies({SkString("Ahem")}); |
| 4245 | text_style.setFontSize(50); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4246 | text_style.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 4247 | SkFontStyle::kUpright_Slant)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4248 | text_style.setColor(SK_ColorBLACK); |
| 4249 | text_style.setHeight(1); |
| 4250 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4251 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4252 | builder.pop(); |
| 4253 | |
| 4254 | auto paragraph = builder.Build(); |
| 4255 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4256 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4257 | |
| 4258 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 4259 | // Font is not resolved and the first line does not fit |
| 4260 | REPORTER_ASSERT(reporter, impl->lines().size() == 4); |
| 4261 | |
| 4262 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 4263 | RectHeightStyle rect_height_max_style = RectHeightStyle::kMax; |
| 4264 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 4265 | SkScalar epsilon = 0.001f; |
| 4266 | { |
| 4267 | auto boxes = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style); |
| 4268 | REPORTER_ASSERT(reporter, boxes.empty()); |
| 4269 | } |
| 4270 | { |
| 4271 | auto boxes = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4272 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4273 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 4274 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, epsilon)); |
| 4275 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 8, epsilon)); |
| 4276 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, epsilon)); |
| 4277 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 58, epsilon)); |
| 4278 | } |
| 4279 | { |
| 4280 | 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] | 4281 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4282 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 4283 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, epsilon)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4284 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 8, epsilon)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4285 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, epsilon)); |
| 4286 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 60, epsilon)); |
| 4287 | } |
| 4288 | { |
| 4289 | auto boxes = paragraph->getRectsForRange(6, 10, rect_height_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4290 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4291 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4292 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, epsilon)); |
| 4293 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 8, epsilon)); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4294 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, epsilon)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4295 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 58, epsilon)); |
| 4296 | } |
| 4297 | { |
| 4298 | 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] | 4299 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4300 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4301 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, epsilon)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4302 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 8, epsilon)); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4303 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, epsilon)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4304 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 60, epsilon)); |
| 4305 | } |
| 4306 | { |
| 4307 | 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] | 4308 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4309 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 4310 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, epsilon)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4311 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 128, epsilon)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4312 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 100, epsilon)); |
| 4313 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 180, epsilon)); |
| 4314 | } |
| 4315 | { |
| 4316 | 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] | 4317 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4318 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 4319 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 50, epsilon)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4320 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 188, epsilon)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4321 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 300, epsilon)); |
| 4322 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 240, epsilon)); |
| 4323 | } |
| 4324 | } |
| 4325 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4326 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4327 | DEF_TEST(SkParagraph_StrutForceParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4328 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4329 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4330 | TestCanvas canvas("SkParagraph_StrutForceParagraph.png"); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4331 | const char* text = "01234満毎冠行来昼本可\nabcd\n満毎冠行来昼本可"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4332 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4333 | |
| 4334 | ParagraphStyle paragraph_style; |
| 4335 | paragraph_style.setMaxLines(10); |
| 4336 | paragraph_style.setTextAlign(TextAlign::kLeft); |
| 4337 | paragraph_style.turnHintingOff(); |
| 4338 | |
| 4339 | StrutStyle strut_style; |
| 4340 | strut_style.setStrutEnabled(true); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4341 | strut_style.setFontFamilies({SkString("Ahem")}); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4342 | strut_style.setFontSize(50); |
| 4343 | strut_style.setHeight(1.5f); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4344 | strut_style.setHeightOverride(true); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4345 | strut_style.setLeading(0.1f); |
| 4346 | strut_style.setForceStrutHeight(true); |
| 4347 | paragraph_style.setStrutStyle(strut_style); |
| 4348 | |
| 4349 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4350 | |
| 4351 | TextStyle text_style; |
| 4352 | text_style.setFontFamilies({SkString("Ahem")}); |
| 4353 | text_style.setFontSize(50); |
| 4354 | text_style.setLetterSpacing(0); |
| 4355 | text_style.setColor(SK_ColorBLACK); |
| 4356 | text_style.setHeight(1); |
| 4357 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4358 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4359 | builder.pop(); |
| 4360 | |
| 4361 | auto paragraph = builder.Build(); |
| 4362 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4363 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4364 | |
| 4365 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 4366 | // Font is not resolved and the first line does not fit |
| 4367 | REPORTER_ASSERT(reporter, impl->lines().size() == 4); |
| 4368 | |
| 4369 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 4370 | RectHeightStyle rect_height_max_style = RectHeightStyle::kMax; |
| 4371 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4372 | |
| 4373 | auto boxes1 = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style); |
| 4374 | REPORTER_ASSERT(reporter, boxes1.empty()); |
| 4375 | |
| 4376 | auto boxes2 = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4377 | canvas.drawRects(SK_ColorRED, boxes2); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4378 | REPORTER_ASSERT(reporter, boxes2.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4379 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes2[0].rect.left(), 0, EPSILON100)); |
| 4380 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes2[0].rect.top(), 22.5f, EPSILON100)); |
| 4381 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes2[0].rect.right(), 50, EPSILON100)); |
| 4382 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes2[0].rect.bottom(), 72.5f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4383 | |
| 4384 | 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] | 4385 | canvas.drawRects(SK_ColorRED, boxes3); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4386 | REPORTER_ASSERT(reporter, boxes3.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4387 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes3[0].rect.left(), 0, EPSILON100)); |
| 4388 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes3[0].rect.top(), 22.5f, EPSILON100)); |
| 4389 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes3[0].rect.right(), 50, EPSILON100)); |
| 4390 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes3[0].rect.bottom(), 80, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4391 | |
| 4392 | auto boxes4 = paragraph->getRectsForRange(6, 10, rect_height_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4393 | canvas.drawRects(SK_ColorRED, boxes4); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4394 | REPORTER_ASSERT(reporter, boxes4.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4395 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes4[0].rect.left(), 300, EPSILON100)); |
| 4396 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes4[0].rect.top(), 22.5f, EPSILON100)); |
| 4397 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes4[0].rect.right(), 500, EPSILON100)); |
| 4398 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes4[0].rect.bottom(), 72.5f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4399 | |
| 4400 | 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] | 4401 | canvas.drawRects(SK_ColorRED, boxes5); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4402 | REPORTER_ASSERT(reporter, boxes5.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4403 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes5[0].rect.left(), 300, EPSILON100)); |
| 4404 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes5[0].rect.top(), 22.5f, EPSILON100)); |
| 4405 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes5[0].rect.right(), 500, EPSILON100)); |
| 4406 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes5[0].rect.bottom(), 80, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4407 | |
| 4408 | 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] | 4409 | canvas.drawRects(SK_ColorRED, boxes6); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4410 | REPORTER_ASSERT(reporter, boxes6.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4411 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes6[0].rect.left(), 0, EPSILON100)); |
| 4412 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes6[0].rect.top(), 182.5f, EPSILON100)); |
| 4413 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes6[0].rect.right(), 100, EPSILON100)); |
| 4414 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes6[0].rect.bottom(), 240, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4415 | |
| 4416 | 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] | 4417 | canvas.drawRects(SK_ColorRED, boxes7); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4418 | REPORTER_ASSERT(reporter, boxes7.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4419 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes7[0].rect.left(), 50, EPSILON100)); |
| 4420 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes7[0].rect.top(), 262.5f, EPSILON100)); |
| 4421 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes7[0].rect.right(), 300, EPSILON100)); |
| 4422 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes7[0].rect.bottom(), 320, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4423 | } |
| 4424 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4425 | // Checked: NO DIFF |
| 4426 | DEF_TEST(SkParagraph_StrutDefaultParagraph, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4427 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4428 | if (!fontCollection->fontsFound()) return; |
| 4429 | TestCanvas canvas("SkParagraph_StrutDefaultParagraph.png"); |
| 4430 | |
| 4431 | const char* text = "01234満毎冠行来昼本可\nabcd\n満毎冠行来昼本可"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4432 | const size_t len = strlen(text); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4433 | |
| 4434 | ParagraphStyle paragraph_style; |
| 4435 | paragraph_style.setMaxLines(10); |
| 4436 | paragraph_style.setTextAlign(TextAlign::kLeft); |
| 4437 | paragraph_style.turnHintingOff(); |
| 4438 | |
| 4439 | StrutStyle strut_style; |
| 4440 | strut_style.setStrutEnabled(true); |
| 4441 | strut_style.setFontFamilies({SkString("Ahem")}); |
| 4442 | strut_style.setFontSize(50); |
| 4443 | strut_style.setHeight(1.5f); |
| 4444 | strut_style.setLeading(0.1f); |
| 4445 | strut_style.setForceStrutHeight(false); |
| 4446 | paragraph_style.setStrutStyle(strut_style); |
| 4447 | |
| 4448 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4449 | |
| 4450 | TextStyle text_style; |
| 4451 | text_style.setFontFamilies({SkString("Ahem")}); |
| 4452 | text_style.setFontSize(20); |
| 4453 | text_style.setColor(SK_ColorBLACK); |
| 4454 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4455 | builder.addText(text, len); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4456 | builder.pop(); |
| 4457 | |
| 4458 | auto paragraph = builder.Build(); |
| 4459 | paragraph->layout(550); |
| 4460 | paragraph->paint(canvas.get(), 0, 0); |
| 4461 | |
| 4462 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 4463 | RectHeightStyle rect_height_strut_style = RectHeightStyle::kStrut; |
| 4464 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 4465 | { |
| 4466 | auto boxes = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style); |
| 4467 | REPORTER_ASSERT(reporter, boxes.empty()); |
| 4468 | } |
| 4469 | { |
| 4470 | auto boxes = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style); |
| 4471 | canvas.drawRects(SK_ColorRED, boxes); |
| 4472 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 4473 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, EPSILON100)); |
| 4474 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 26.5f, EPSILON100)); |
| 4475 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 20, EPSILON100)); |
| 4476 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 46.5f, EPSILON100)); |
| 4477 | } |
| 4478 | { |
| 4479 | auto boxes = paragraph->getRectsForRange(0, 2, rect_height_strut_style, rect_width_style); |
| 4480 | canvas.drawRects(SK_ColorRED, boxes); |
| 4481 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 4482 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, EPSILON100)); |
| 4483 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 2.5f, EPSILON100)); |
| 4484 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 40, EPSILON100)); |
| 4485 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 52.5f, EPSILON100)); |
| 4486 | } |
| 4487 | } |
| 4488 | |
Julia Lavrova | c5313e6 | 2019-12-10 12:11:17 -0500 | [diff] [blame] | 4489 | DEF_TEST(SkParagraph_FontFeaturesParagraph, reporter) { |
| 4490 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
| 4491 | if (!fontCollection->fontsFound()) return; |
| 4492 | TestCanvas canvas("SkParagraph_FontFeaturesParagraph.png"); |
| 4493 | |
| 4494 | const char* text = "12ab\n"; |
| 4495 | |
| 4496 | ParagraphStyle paragraph_style; |
| 4497 | paragraph_style.turnHintingOff(); |
| 4498 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4499 | |
| 4500 | TextStyle text_style; |
| 4501 | text_style.setFontStyle(SkFontStyle::Italic()); |
| 4502 | text_style.setFontFamilies({SkString("Roboto")}); |
| 4503 | text_style.setColor(SK_ColorBLACK); |
| 4504 | |
| 4505 | text_style.addFontFeature(SkString("tnum"), 1); |
| 4506 | builder.pushStyle(text_style); |
| 4507 | builder.addText(text); |
| 4508 | |
| 4509 | text_style.resetFontFeatures(); |
| 4510 | text_style.addFontFeature(SkString("tnum"), 0); |
| 4511 | text_style.addFontFeature(SkString("pnum"), 1); |
| 4512 | builder.pushStyle(text_style); |
| 4513 | builder.addText(text); |
| 4514 | |
| 4515 | builder.pop(); |
| 4516 | builder.pop(); |
| 4517 | |
| 4518 | auto paragraph = builder.Build(); |
| 4519 | paragraph->layout(TestCanvasWidth); |
| 4520 | |
| 4521 | paragraph->paint(canvas.get(), 10.0, 15.0); |
| 4522 | |
| 4523 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 4524 | REPORTER_ASSERT(reporter, paragraph->lineNumber() == 3ull); |
| 4525 | |
| 4526 | auto& tnum_line = impl->lines()[0]; |
| 4527 | auto& pnum_line = impl->lines()[1]; |
| 4528 | |
| 4529 | REPORTER_ASSERT(reporter, tnum_line.clusters().width() == 4ull); |
| 4530 | REPORTER_ASSERT(reporter, pnum_line.clusters().width() == 4ull); |
| 4531 | // Tabular numbers should have equal widths. |
| 4532 | REPORTER_ASSERT(reporter, impl->clusters()[0].width() == impl->clusters()[1].width()); |
| 4533 | // Proportional numbers should have variable widths. |
| 4534 | REPORTER_ASSERT(reporter, impl->clusters()[5].width() != impl->clusters()[6].width()); |
| 4535 | // Alphabetic characters should be unaffected. |
| 4536 | REPORTER_ASSERT(reporter, impl->clusters()[2].width() == impl->clusters()[7].width()); |
| 4537 | } |
| 4538 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4539 | // Not in Minikin |
| 4540 | DEF_TEST(SkParagraph_WhitespacesInMultipleFonts, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4541 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4542 | if (!fontCollection->fontsFound()) return; |
| 4543 | const char* text = "English English 字典 字典 😀😃😄 😀😃😄"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4544 | const size_t len = strlen(text); |
| 4545 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4546 | ParagraphStyle paragraph_style; |
| 4547 | paragraph_style.turnHintingOff(); |
| 4548 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4549 | |
| 4550 | TextStyle text_style; |
| 4551 | text_style.setFontFamilies( |
| 4552 | {SkString("Roboto"), SkString("Noto Color Emoji"), SkString("Source Han Serif CN")}); |
| 4553 | text_style.setFontSize(60); |
| 4554 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4555 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4556 | builder.pop(); |
| 4557 | |
| 4558 | auto paragraph = builder.Build(); |
| 4559 | paragraph->layout(TestCanvasWidth); |
Julia Lavrova | 3281b96 | 2019-12-02 11:32:25 -0500 | [diff] [blame] | 4560 | |
| 4561 | REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == 0); |
| 4562 | |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4563 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 4564 | for (size_t i = 0; i < impl->runs().size() - 1; ++i) { |
| 4565 | auto first = impl->runs()[i].textRange(); |
| 4566 | auto next = impl->runs()[i + 1].textRange(); |
| 4567 | REPORTER_ASSERT(reporter, first.end == next.start); |
| 4568 | } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4569 | } |
| 4570 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4571 | DEF_TEST(SkParagraph_JSON1, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4572 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4573 | if (!fontCollection->fontsFound()) return; |
| 4574 | const char* text = "👨👩👧👦"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4575 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4576 | |
| 4577 | ParagraphStyle paragraph_style; |
| 4578 | paragraph_style.turnHintingOff(); |
| 4579 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4580 | |
| 4581 | TextStyle text_style; |
| 4582 | text_style.setFontFamilies({SkString("Noto Color Emoji")}); |
| 4583 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4584 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4585 | builder.pop(); |
| 4586 | |
| 4587 | auto paragraph = builder.Build(); |
| 4588 | paragraph->layout(TestCanvasWidth); |
| 4589 | |
| 4590 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 4591 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4592 | auto& run = impl->runs().front(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4593 | |
| 4594 | auto cluster = 0; |
| 4595 | SkShaperJSONWriter::VisualizeClusters( |
| 4596 | text, 0, std::strlen(text), run.glyphs(), run.clusterIndexes(), |
| 4597 | [&](int codePointCount, SkSpan<const char> utf1to1, SkSpan<const SkGlyphID> glyph1to1) { |
| 4598 | if (cluster == 0) { |
| 4599 | std::string toCheckUtf8{utf1to1.data(), utf1to1.size()}; |
| 4600 | SkASSERT(std::strcmp(text, utf1to1.data()) == 0); |
| 4601 | SkASSERT(glyph1to1.size() == 1); |
| 4602 | SkASSERT(*glyph1to1.begin() == 1611); |
| 4603 | } |
| 4604 | ++cluster; |
| 4605 | }); |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4606 | REPORTER_ASSERT(reporter, cluster <= 2); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4607 | } |
| 4608 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4609 | DEF_TEST(SkParagraph_JSON2, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4610 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4611 | if (!fontCollection->fontsFound()) return; |
| 4612 | const char* text = "p〠q"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4613 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4614 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4615 | ParagraphStyle paragraph_style; |
| 4616 | paragraph_style.turnHintingOff(); |
| 4617 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4618 | |
| 4619 | TextStyle text_style; |
| 4620 | text_style.setFontFamilies({SkString("Noto Sans CJK JP")}); |
| 4621 | text_style.setColor(SK_ColorBLACK); |
| 4622 | text_style.setFontSize(50); |
| 4623 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4624 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4625 | builder.pop(); |
| 4626 | |
| 4627 | auto paragraph = builder.Build(); |
| 4628 | paragraph->layout(TestCanvasWidth); |
| 4629 | |
| 4630 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 4631 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4632 | |
| 4633 | auto cluster = 0; |
| 4634 | for (auto& run : impl->runs()) { |
| 4635 | SkShaperJSONWriter::VisualizeClusters( |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4636 | impl->text().begin() + run.textRange().start, 0, run.textRange().width(), |
| 4637 | run.glyphs(), run.clusterIndexes(), |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4638 | [&](int codePointCount, SkSpan<const char> utf1to1, |
| 4639 | SkSpan<const SkGlyphID> glyph1to1) { |
| 4640 | if (cluster == 0) { |
| 4641 | std::string toCheckUtf8{utf1to1.data(), utf1to1.size()}; |
| 4642 | SkASSERT(std::strcmp(text, utf1to1.data()) == 0); |
| 4643 | SkASSERT(glyph1to1.size() == 3); |
| 4644 | } |
| 4645 | ++cluster; |
| 4646 | }); |
| 4647 | } |
| 4648 | |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4649 | REPORTER_ASSERT(reporter, cluster <= 2); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4650 | } |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4651 | |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4652 | DEF_TEST(SkParagraph_CacheText, reporter) { |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4653 | ParagraphCache cache; |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4654 | cache.turnOn(true); |
| 4655 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4656 | if (!fontCollection->fontsFound()) return; |
| 4657 | |
| 4658 | ParagraphStyle paragraph_style; |
| 4659 | paragraph_style.turnHintingOff(); |
| 4660 | |
| 4661 | TextStyle text_style; |
| 4662 | text_style.setFontFamilies({SkString("Roboto")}); |
| 4663 | text_style.setColor(SK_ColorBLACK); |
| 4664 | |
| 4665 | auto test = [&](const char* text, int count, bool expectedToBeFound) { |
| 4666 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4667 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4668 | builder.addText(text, strlen(text)); |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4669 | builder.pop(); |
| 4670 | auto paragraph = builder.Build(); |
| 4671 | |
| 4672 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 4673 | REPORTER_ASSERT(reporter, count == cache.count()); |
| 4674 | auto found = cache.findParagraph(impl); |
| 4675 | REPORTER_ASSERT(reporter, found == expectedToBeFound); |
| 4676 | auto added = cache.updateParagraph(impl); |
| 4677 | REPORTER_ASSERT(reporter, added != expectedToBeFound); |
| 4678 | }; |
| 4679 | |
| 4680 | test("text1", 0, false); |
| 4681 | test("text1", 1, true); |
| 4682 | test("text2", 1, false); |
| 4683 | test("text2", 2, true); |
| 4684 | test("text3", 2, false); |
| 4685 | } |
| 4686 | |
| 4687 | DEF_TEST(SkParagraph_CacheFonts, reporter) { |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4688 | ParagraphCache cache; |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4689 | cache.turnOn(true); |
| 4690 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4691 | if (!fontCollection->fontsFound()) return; |
| 4692 | |
| 4693 | ParagraphStyle paragraph_style; |
| 4694 | paragraph_style.turnHintingOff(); |
| 4695 | |
| 4696 | TextStyle text_style; |
| 4697 | text_style.setColor(SK_ColorBLACK); |
| 4698 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4699 | const char* text = "text"; |
| 4700 | const size_t len = strlen(text); |
| 4701 | |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4702 | auto test = [&](int count, bool expectedToBeFound) { |
| 4703 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4704 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4705 | builder.addText(text, len); |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4706 | builder.pop(); |
| 4707 | auto paragraph = builder.Build(); |
| 4708 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 4709 | |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4710 | REPORTER_ASSERT(reporter, count == cache.count()); |
| 4711 | auto found = cache.findParagraph(impl); |
| 4712 | REPORTER_ASSERT(reporter, found == expectedToBeFound); |
| 4713 | auto added = cache.updateParagraph(impl); |
| 4714 | REPORTER_ASSERT(reporter, added != expectedToBeFound); |
| 4715 | }; |
| 4716 | |
| 4717 | text_style.setFontFamilies({SkString("Roboto")}); |
| 4718 | test(0, false); |
| 4719 | test(1, true); |
| 4720 | text_style.setFontFamilies({SkString("Homemade Apple")}); |
| 4721 | test(1, false); |
| 4722 | test(2, true); |
| 4723 | text_style.setFontFamilies({SkString("Noto Color Emoji")}); |
| 4724 | test(2, false); |
| 4725 | } |
| 4726 | |
| 4727 | DEF_TEST(SkParagraph_CacheFontRanges, reporter) { |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4728 | ParagraphCache cache; |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4729 | cache.turnOn(true); |
| 4730 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4731 | if (!fontCollection->fontsFound()) return; |
| 4732 | |
| 4733 | ParagraphStyle paragraph_style; |
| 4734 | paragraph_style.turnHintingOff(); |
| 4735 | |
| 4736 | TextStyle text_style; |
| 4737 | text_style.setFontFamilies({SkString("Roboto")}); |
| 4738 | text_style.setColor(SK_ColorBLACK); |
| 4739 | |
| 4740 | auto test = [&](const char* text1, |
| 4741 | const char* text2, |
| 4742 | const char* font1, |
| 4743 | const char* font2, |
| 4744 | int count, |
| 4745 | bool expectedToBeFound) { |
| 4746 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4747 | text_style.setFontFamilies({SkString(font1)}); |
| 4748 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4749 | builder.addText(text1, strlen(text1)); |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4750 | builder.pop(); |
| 4751 | text_style.setFontFamilies({SkString(font2)}); |
| 4752 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4753 | builder.addText(text2, strlen(text2)); |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4754 | builder.pop(); |
| 4755 | auto paragraph = builder.Build(); |
| 4756 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 4757 | |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4758 | REPORTER_ASSERT(reporter, count == cache.count()); |
| 4759 | auto found = cache.findParagraph(impl); |
| 4760 | REPORTER_ASSERT(reporter, found == expectedToBeFound); |
| 4761 | auto added = cache.updateParagraph(impl); |
| 4762 | REPORTER_ASSERT(reporter, added != expectedToBeFound); |
| 4763 | }; |
| 4764 | |
| 4765 | test("text", "", "Roboto", "Homemade Apple", 0, false); |
| 4766 | test("t", "ext", "Roboto", "Homemade Apple", 1, false); |
| 4767 | test("te", "xt", "Roboto", "Homemade Apple", 2, false); |
| 4768 | test("tex", "t", "Roboto", "Homemade Apple", 3, false); |
| 4769 | test("text", "", "Roboto", "Homemade Apple", 4, true); |
| 4770 | } |
| 4771 | |
| 4772 | DEF_TEST(SkParagraph_CacheStyles, reporter) { |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4773 | ParagraphCache cache; |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4774 | cache.turnOn(true); |
| 4775 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4776 | if (!fontCollection->fontsFound()) return; |
| 4777 | |
| 4778 | ParagraphStyle paragraph_style; |
| 4779 | paragraph_style.turnHintingOff(); |
| 4780 | |
| 4781 | TextStyle text_style; |
| 4782 | text_style.setFontFamilies({SkString("Roboto")}); |
| 4783 | text_style.setColor(SK_ColorBLACK); |
| 4784 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4785 | const char* text = "text"; |
| 4786 | const size_t len = strlen(text); |
| 4787 | |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4788 | auto test = [&](int count, bool expectedToBeFound) { |
| 4789 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4790 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4791 | builder.addText(text, len); |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4792 | builder.pop(); |
| 4793 | auto paragraph = builder.Build(); |
| 4794 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 4795 | |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4796 | REPORTER_ASSERT(reporter, count == cache.count()); |
| 4797 | auto found = cache.findParagraph(impl); |
| 4798 | REPORTER_ASSERT(reporter, found == expectedToBeFound); |
| 4799 | auto added = cache.updateParagraph(impl); |
| 4800 | REPORTER_ASSERT(reporter, added != expectedToBeFound); |
| 4801 | }; |
| 4802 | |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4803 | test(0, false); |
| 4804 | test(1, true); |
| 4805 | text_style.setLetterSpacing(10); |
| 4806 | test(1, false); |
| 4807 | test(2, true); |
| 4808 | text_style.setWordSpacing(10); |
| 4809 | test(2, false); |
| 4810 | } |
Julia Lavrova | a40db42 | 2019-08-21 13:49:15 -0400 | [diff] [blame] | 4811 | |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4812 | DEF_TEST(SkParagraph_EmptyParagraphWithLineBreak, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4813 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a40db42 | 2019-08-21 13:49:15 -0400 | [diff] [blame] | 4814 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4815 | fontCollection->setDefaultFontManager(SkFontMgr::RefDefault()); |
| 4816 | TestCanvas canvas("SkParagraph_EmptyParagraphWithLineBreak.png"); |
Julia Lavrova | a40db42 | 2019-08-21 13:49:15 -0400 | [diff] [blame] | 4817 | |
| 4818 | ParagraphStyle paragraph_style; |
Julia Lavrova | a40db42 | 2019-08-21 13:49:15 -0400 | [diff] [blame] | 4819 | TextStyle text_style; |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4820 | text_style.setFontSize(16); |
| 4821 | text_style.setFontFamilies({SkString("Roboto")}); |
| 4822 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4823 | builder.addText("\n", 1); |
Julia Lavrova | a40db42 | 2019-08-21 13:49:15 -0400 | [diff] [blame] | 4824 | |
| 4825 | auto paragraph = builder.Build(); |
| 4826 | paragraph->layout(TestCanvasWidth); |
| 4827 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 4828 | auto result = paragraph->getRectsForPlaceholders(); |
Julia Lavrova | a40db42 | 2019-08-21 13:49:15 -0400 | [diff] [blame] | 4829 | } |
| 4830 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4831 | DEF_TEST(SkParagraph_NullInMiddleOfText, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4832 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4833 | if (!fontCollection->fontsFound()) return; |
| 4834 | fontCollection->setDefaultFontManager(SkFontMgr::RefDefault()); |
| 4835 | TestCanvas canvas("SkParagraph_NullInMiddleOfText.png"); |
| 4836 | |
| 4837 | const SkString text("null terminator ->\u0000<- on purpose did you see it?"); |
| 4838 | |
| 4839 | ParagraphStyle paragraph_style; |
| 4840 | TextStyle text_style; |
| 4841 | text_style.setFontSize(16); |
| 4842 | text_style.setFontFamilies({SkString("Roboto")}); |
| 4843 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4844 | builder.addText(text.c_str(), text.size()); |
| 4845 | |
| 4846 | auto paragraph = builder.Build(); |
| 4847 | paragraph->layout(TestCanvasWidth); |
| 4848 | paragraph->paint(canvas.get(), 0, 0); |
| 4849 | } |
| 4850 | |
Julia Lavrova | a40db42 | 2019-08-21 13:49:15 -0400 | [diff] [blame] | 4851 | DEF_TEST(SkParagraph_PlaceholderOnly, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4852 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | a40db42 | 2019-08-21 13:49:15 -0400 | [diff] [blame] | 4853 | if (!fontCollection->fontsFound()) return; |
| 4854 | TestCanvas canvas("SkParagraph_PlaceholderOnly.png"); |
| 4855 | |
| 4856 | ParagraphStyle paragraph_style; |
| 4857 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4858 | |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4859 | PlaceholderStyle placeholder(0, 0, PlaceholderAlignment::kBaseline, TextBaseline::kAlphabetic, 0); |
Julia Lavrova | a40db42 | 2019-08-21 13:49:15 -0400 | [diff] [blame] | 4860 | builder.addPlaceholder(placeholder); |
| 4861 | |
| 4862 | auto paragraph = builder.Build(); |
| 4863 | paragraph->layout(TestCanvasWidth); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 4864 | auto result = paragraph->getRectsForPlaceholders(); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4865 | paragraph->paint(canvas.get(), 0, 0); |
| 4866 | } |
| 4867 | |
| 4868 | DEF_TEST(SkParagraph_Fallbacks, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4869 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4870 | if (!fontCollection->fontsFound()) return; |
| 4871 | fontCollection->setDefaultFontManager(SkFontMgr::RefDefault(), "Arial"); |
| 4872 | TestCanvas canvas("SkParagraph_Fallbacks.png"); |
| 4873 | |
| 4874 | const char* multiScript = "A1!aÀàĀāƁƀḂⱠꜲꬰəͲἀἏЀЖԠꙐꙮՁخࡔࠇܦআਉઐଘஇఘಧൺඣᭆᯔᮯ᳇ꠈᜅᩌꪈ༇ꥄꡙꫤ᧰៘꧁꧂ᜰᨏᯤᢆᣭᗗꗃⵞ𐒎߷ጩꬤ𖠺‡₩℻Ⅷ↹⋇⏳ⓖ╋▒◛⚧⑆שׁ🅕㊼龜ポ䷤🂡\n"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4875 | const size_t len = strlen(multiScript); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4876 | |
| 4877 | const char* androidFonts[] = { |
| 4878 | "sans-serif", |
| 4879 | "sans-serif-condensed", |
| 4880 | "serif", |
| 4881 | "monospace", |
| 4882 | "serif-monospace", |
| 4883 | "casual", |
| 4884 | "cursive", |
| 4885 | "sans-serif-smallcaps", |
| 4886 | }; |
| 4887 | |
| 4888 | for (auto& font : androidFonts) { |
| 4889 | |
| 4890 | ParagraphStyle paragraph_style; |
| 4891 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4892 | |
| 4893 | TextStyle text_style; |
| 4894 | text_style.setColor(SK_ColorBLACK); |
| 4895 | text_style.setLocale(SkString("en_US")); |
| 4896 | text_style.setFontSize(20); |
| 4897 | |
| 4898 | text_style.setFontFamilies({ SkString(font) }); |
| 4899 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4900 | builder.addText(multiScript, len); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4901 | |
| 4902 | builder.pop(); |
| 4903 | |
| 4904 | auto paragraph = builder.Build(); |
| 4905 | paragraph->layout(TestCanvasWidth); |
| 4906 | paragraph->paint(canvas.get(), 0, 0); |
| 4907 | canvas.get()->translate(0, paragraph.get()->getHeight() + 10); |
| 4908 | } |
| 4909 | } |
| 4910 | |
| 4911 | DEF_TEST(SkParagraph_Bidi1, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4912 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4913 | if (!fontCollection->fontsFound()) return; |
| 4914 | fontCollection->setDefaultFontManager(SkFontMgr::RefDefault()); |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4915 | fontCollection->enableFontFallback(); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4916 | TestCanvas canvas("SkParagraph_Bidi1.png"); |
| 4917 | |
| 4918 | std::u16string abc = u"\u202Dabc"; |
| 4919 | std::u16string DEF = u"\u202EDEF"; |
| 4920 | std::u16string ghi = u"\u202Dghi"; |
| 4921 | std::u16string JKL = u"\u202EJKL"; |
| 4922 | std::u16string mno = u"\u202Dmno"; |
| 4923 | |
| 4924 | std::u16string abcDEFghiJKLmno = u"\u202Dabc\u202EDEF\u202Dghi\u202EJKL\u202Dmno"; |
| 4925 | |
| 4926 | ParagraphStyle paragraph_style; |
| 4927 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4928 | |
| 4929 | TextStyle text_style; |
| 4930 | text_style.setFontFamilies({ SkString("sans-serif")}); |
| 4931 | text_style.setFontSize(40); |
| 4932 | |
| 4933 | text_style.setColor(SK_ColorCYAN); |
| 4934 | text_style.setFontStyle(SkFontStyle(SkFontStyle::kThin_Weight, SkFontStyle::kNormal_Width, SkFontStyle::kUpright_Slant)); |
| 4935 | builder.pushStyle(text_style); |
| 4936 | builder.addText(abc); |
| 4937 | |
| 4938 | text_style.setColor(SK_ColorGREEN); |
| 4939 | text_style.setFontStyle(SkFontStyle(SkFontStyle::kLight_Weight, SkFontStyle::kNormal_Width, SkFontStyle::kUpright_Slant)); |
| 4940 | builder.pushStyle(text_style); |
| 4941 | builder.addText(DEF); |
| 4942 | |
| 4943 | text_style.setColor(SK_ColorYELLOW); |
| 4944 | text_style.setFontStyle(SkFontStyle(SkFontStyle::kNormal_Weight, SkFontStyle::kNormal_Width, SkFontStyle::kUpright_Slant)); |
| 4945 | builder.pushStyle(text_style); |
| 4946 | builder.addText(ghi); |
| 4947 | |
| 4948 | text_style.setColor(SK_ColorMAGENTA); |
| 4949 | text_style.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, SkFontStyle::kUpright_Slant)); |
| 4950 | builder.pushStyle(text_style); |
| 4951 | builder.addText(JKL); |
| 4952 | |
| 4953 | text_style.setColor(SK_ColorBLUE); |
| 4954 | text_style.setFontStyle(SkFontStyle(SkFontStyle::kBlack_Weight, SkFontStyle::kNormal_Width, SkFontStyle::kUpright_Slant)); |
| 4955 | builder.pushStyle(text_style); |
| 4956 | builder.addText(mno); |
| 4957 | |
| 4958 | auto paragraph = builder.Build(); |
| 4959 | paragraph->layout(400); |
| 4960 | paragraph->paint(canvas.get(), 0, 0); |
| 4961 | } |
| 4962 | |
| 4963 | DEF_TEST(SkParagraph_Bidi2, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4964 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4965 | if (!fontCollection->fontsFound()) return; |
| 4966 | fontCollection->setDefaultFontManager(SkFontMgr::RefDefault()); |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 4967 | fontCollection->enableFontFallback(); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4968 | TestCanvas canvas("SkParagraph_Bidi2.png"); |
| 4969 | |
| 4970 | std::u16string abcD = u"\u202Dabc\u202ED"; |
| 4971 | std::u16string EFgh = u"EF\u202Dgh"; |
| 4972 | std::u16string iJKLmno = u"i\u202EJKL\u202Dmno"; |
| 4973 | |
| 4974 | std::u16string abcDEFghiJKLmno = u"\u202Dabc\u202EDEF\u202Dghi\u202EJKL\u202Dmno"; |
| 4975 | |
| 4976 | ParagraphStyle paragraph_style; |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4977 | TextStyle text_style; |
| 4978 | text_style.setFontFamilies({ SkString("sans-serif")}); |
| 4979 | text_style.setFontSize(40); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4980 | |
Julia Lavrova | c88a3bc | 2020-01-23 10:16:26 -0500 | [diff] [blame] | 4981 | { |
| 4982 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4983 | builder.pushStyle(text_style); |
| 4984 | builder.addText(abcD); |
| 4985 | builder.pushStyle(text_style); |
| 4986 | builder.addText(EFgh); |
| 4987 | builder.pushStyle(text_style); |
| 4988 | builder.addText(iJKLmno); |
| 4989 | auto paragraph = builder.Build(); |
| 4990 | paragraph->layout(360); |
| 4991 | paragraph->paint(canvas.get(), 0, 0); |
| 4992 | } |
| 4993 | canvas.get()->translate(0, 400); |
| 4994 | { |
| 4995 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4996 | builder.pushStyle(text_style); |
| 4997 | builder.addText(abcDEFghiJKLmno); |
| 4998 | auto paragraph = builder.Build(); |
| 4999 | paragraph->layout(360); |
| 5000 | paragraph->paint(canvas.get(), 0, 0); |
| 5001 | } |
Julia Lavrova | a40db42 | 2019-08-21 13:49:15 -0400 | [diff] [blame] | 5002 | } |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 5003 | |
| 5004 | DEF_TEST(SkParagraph_NewlineOnly, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 5005 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 5006 | if (!fontCollection->fontsFound()) return; |
| 5007 | fontCollection->setDefaultFontManager(SkFontMgr::RefDefault()); |
| 5008 | TestCanvas canvas("SkParagraph_Newline.png"); |
| 5009 | |
| 5010 | TextStyle text_style; |
| 5011 | text_style.setFontFamilies({SkString("Ahem")}); |
| 5012 | text_style.setColor(SK_ColorBLACK); |
| 5013 | StrutStyle strut_style; |
| 5014 | strut_style.setStrutEnabled(false); |
| 5015 | ParagraphStyle paragraph_style; |
| 5016 | paragraph_style.setStrutStyle(strut_style); |
| 5017 | paragraph_style.setTextStyle(text_style); |
| 5018 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 5019 | builder.addText("\n"); |
| 5020 | auto paragraph = builder.Build(); |
| 5021 | paragraph->layout(1000); |
| 5022 | REPORTER_ASSERT(reporter, paragraph->getHeight() == 28); |
| 5023 | } |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 5024 | |
Julia Lavrova | b30d481 | 2019-11-08 14:51:16 -0500 | [diff] [blame] | 5025 | DEF_TEST_DISABLED(SkParagraph_FontResolutions, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 5026 | TestCanvas canvas("SkParagraph_FontResolutions.png"); |
| 5027 | |
| 5028 | sk_sp<TestFontCollection> fontCollection = |
| 5029 | sk_make_sp<TestFontCollection>(GetResourcePath("fonts").c_str(), false); |
| 5030 | if (!fontCollection->fontsFound()) return; |
| 5031 | |
| 5032 | if (!fontCollection->addFontFromFile("abc/abc.ttf", "abc")) { |
| 5033 | return; |
| 5034 | } |
| 5035 | if (!fontCollection->addFontFromFile("abc/abc+grave.ttf", "abc+grave")) { |
| 5036 | return; |
| 5037 | } |
| 5038 | if (!fontCollection->addFontFromFile("abc/abc_agrave.ttf", "abc_agrave")) { |
| 5039 | return; |
| 5040 | } |
| 5041 | |
| 5042 | TextStyle text_style; |
| 5043 | text_style.setFontFamilies({SkString("abc")}); |
| 5044 | text_style.setFontSize(50); |
| 5045 | |
| 5046 | ParagraphStyle paragraph_style; |
| 5047 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 5048 | |
| 5049 | text_style.setFontFamilies({SkString("abc"), SkString("abc+grave")}); |
| 5050 | text_style.setColor(SK_ColorBLUE); |
| 5051 | builder.pushStyle(text_style); |
| 5052 | builder.addText(u"a\u0300"); |
| 5053 | text_style.setColor(SK_ColorMAGENTA); |
| 5054 | builder.pushStyle(text_style); |
| 5055 | builder.addText(u"à"); |
| 5056 | |
| 5057 | text_style.setFontFamilies({SkString("abc"), SkString("abc_agrave")}); |
| 5058 | |
| 5059 | text_style.setColor(SK_ColorRED); |
| 5060 | builder.pushStyle(text_style); |
| 5061 | builder.addText(u"a\u0300"); |
| 5062 | text_style.setColor(SK_ColorGREEN); |
| 5063 | builder.pushStyle(text_style); |
| 5064 | builder.addText(u"à"); |
| 5065 | |
| 5066 | auto paragraph = builder.Build(); |
| 5067 | paragraph->layout(TestCanvasWidth); |
| 5068 | |
| 5069 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 5070 | REPORTER_ASSERT(reporter, impl->runs().size() == 2); |
| 5071 | |
| 5072 | REPORTER_ASSERT(reporter, impl->runs().front().size() == 4); |
| 5073 | REPORTER_ASSERT(reporter, impl->runs().front().glyphs()[0] == impl->runs().front().glyphs()[2]); |
| 5074 | REPORTER_ASSERT(reporter, impl->runs().front().glyphs()[1] == impl->runs().front().glyphs()[3]); |
| 5075 | |
| 5076 | REPORTER_ASSERT(reporter, impl->runs().back().size() == 2); |
| 5077 | REPORTER_ASSERT(reporter, impl->runs().back().glyphs()[0] == impl->runs().back().glyphs()[1]); |
| 5078 | |
| 5079 | paragraph->paint(canvas.get(), 100, 100); |
| 5080 | } |
| 5081 | |
Julia Lavrova | b30d481 | 2019-11-08 14:51:16 -0500 | [diff] [blame] | 5082 | DEF_TEST_DISABLED(SkParagraph_FontStyle, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 5083 | TestCanvas canvas("SkParagraph_FontStyle.png"); |
| 5084 | |
| 5085 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(GetResourcePath("fonts").c_str(), false, true); |
| 5086 | if (!fontCollection->fontsFound()) return; |
| 5087 | |
| 5088 | TextStyle text_style; |
| 5089 | text_style.setFontFamilies({SkString("Roboto")}); |
| 5090 | text_style.setColor(SK_ColorBLACK); |
| 5091 | text_style.setFontSize(20); |
| 5092 | SkFontStyle fs = SkFontStyle( |
| 5093 | SkFontStyle::Weight::kLight_Weight, |
| 5094 | SkFontStyle::Width::kNormal_Width, |
| 5095 | SkFontStyle::Slant::kUpright_Slant |
| 5096 | ); |
| 5097 | text_style.setFontStyle(fs); |
| 5098 | ParagraphStyle paragraph_style; |
| 5099 | paragraph_style.setTextStyle(text_style); |
| 5100 | TextStyle boldItalic; |
| 5101 | boldItalic.setFontFamilies({SkString("Roboto")}); |
| 5102 | boldItalic.setColor(SK_ColorRED); |
| 5103 | SkFontStyle bi = SkFontStyle( |
| 5104 | SkFontStyle::Weight::kBold_Weight, |
| 5105 | SkFontStyle::Width::kNormal_Width, |
| 5106 | SkFontStyle::Slant::kItalic_Slant |
| 5107 | ); |
| 5108 | boldItalic.setFontStyle(bi); |
| 5109 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 5110 | builder.addText("Default text\n"); |
| 5111 | builder.pushStyle(boldItalic); |
| 5112 | builder.addText("Bold and Italic\n"); |
| 5113 | builder.pop(); |
| 5114 | builder.addText("back to normal"); |
| 5115 | auto paragraph = builder.Build(); |
| 5116 | paragraph->layout(250); |
| 5117 | paragraph->paint(canvas.get(), 0, 0); |
| 5118 | } |
| 5119 | |
Julia Lavrova | b30d481 | 2019-11-08 14:51:16 -0500 | [diff] [blame] | 5120 | DEF_TEST_DISABLED(SkParagraph_Shaping, reporter) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 5121 | TestCanvas canvas("SkParagraph_Shaping.png"); |
| 5122 | |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 5123 | auto dir = "/usr/local/google/home/jlavrova/Sources/flutter/engine/src/out/host_debug_unopt_x86/gen/flutter/third_party/txt/assets"; |
| 5124 | sk_sp<TestFontCollection> fontCollection = |
| 5125 | sk_make_sp<TestFontCollection>(dir, /*GetResourcePath("fonts").c_str(), */ false); |
| 5126 | if (!fontCollection->fontsFound()) return; |
| 5127 | |
| 5128 | |
| 5129 | TextStyle text_style; |
| 5130 | text_style.setFontFamilies({SkString("Roboto")}); |
| 5131 | text_style.setColor(SK_ColorGRAY); |
| 5132 | text_style.setFontSize(14); |
| 5133 | SkFontStyle b = SkFontStyle( |
| 5134 | SkFontStyle::Weight::kNormal_Weight, |
| 5135 | SkFontStyle::Width::kNormal_Width, |
| 5136 | SkFontStyle::Slant::kUpright_Slant |
| 5137 | ); |
| 5138 | text_style.setFontStyle(b); |
| 5139 | ParagraphStyle paragraph_style; |
| 5140 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 5141 | builder.pushStyle(text_style); |
| 5142 | builder.addText("Eat0 apple0 pies0 | Eat1 apple1 pies1 | Eat2 apple2 pies2"); |
| 5143 | auto paragraph = builder.Build(); |
| 5144 | paragraph->layout(380); |
| 5145 | paragraph->paint(canvas.get(), 0, 0); |
| 5146 | } |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 5147 | |
| 5148 | DEF_TEST(SkParagraph_Ellipsis, reporter) { |
| 5149 | sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(); |
| 5150 | if (!fontCollection->fontsFound()) return; |
| 5151 | fontCollection->setDefaultFontManager(SkFontMgr::RefDefault()); |
| 5152 | TestCanvas canvas("SkParagraph_Ellipsis.png"); |
| 5153 | |
| 5154 | const char* text = "This\n" |
| 5155 | "is a wrapping test. It should wrap at manual newlines, and if softWrap is true, also at spaces."; |
| 5156 | TextStyle text_style; |
| 5157 | text_style.setFontFamilies({SkString("Ahem")}); |
| 5158 | text_style.setColor(SK_ColorBLACK); |
| 5159 | text_style.setFontSize(10); |
| 5160 | |
| 5161 | auto relayout = [&](size_t lines, bool ellipsis, |
| 5162 | SkScalar width, SkScalar height, SkScalar minWidth, SkScalar maxWidth, SkColor bg) { |
| 5163 | ParagraphStyle paragraph_style; |
| 5164 | SkPaint paint; |
| 5165 | paint.setColor(bg); |
| 5166 | text_style.setForegroundColor(paint); |
| 5167 | paragraph_style.setTextStyle(text_style); |
| 5168 | paragraph_style.setMaxLines(lines); |
| 5169 | if (ellipsis) { |
| 5170 | paragraph_style.setEllipsis(u"\u2026"); |
| 5171 | } |
| 5172 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 5173 | builder.addText(text); |
| 5174 | auto paragraph = builder.Build(); |
| 5175 | paragraph->layout(50); |
| 5176 | paragraph->paint(canvas.get(), 0, 0); |
| 5177 | canvas.get()->translate(50, paragraph->getHeight() + 10); |
| 5178 | auto result = paragraph->getRectsForRange(0, strlen(text), RectHeightStyle::kTight, RectWidthStyle::kTight); |
| 5179 | SkPaint background; |
| 5180 | background.setColor(SK_ColorRED); |
| 5181 | background.setStyle(SkPaint::kStroke_Style); |
| 5182 | background.setAntiAlias(true); |
| 5183 | background.setStrokeWidth(1); |
| 5184 | canvas.get()->drawRect(result.front().rect, background); |
| 5185 | |
| 5186 | SkASSERT(width == paragraph->getMaxWidth()); |
| 5187 | SkASSERT(height == paragraph->getHeight()); |
| 5188 | SkASSERT(minWidth == paragraph->getMinIntrinsicWidth()); |
| 5189 | SkASSERT(maxWidth == paragraph->getMaxIntrinsicWidth()); |
| 5190 | }; |
| 5191 | |
| 5192 | SkPaint paint; |
| 5193 | paint.setColor(SK_ColorLTGRAY); |
| 5194 | canvas.get()->drawRect(SkRect::MakeXYWH(0, 0, 50, 500), paint); |
| 5195 | |
| 5196 | relayout(1, false, 50, 10, 950, 950, SK_ColorRED); |
Julia Lavrova | 90bfd1c | 2019-12-04 11:43:32 -0500 | [diff] [blame] | 5197 | relayout(3, false, 50, 30, 90, 950, SK_ColorBLUE); |
| 5198 | 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] | 5199 | |
| 5200 | relayout(1, true, 50, 10, 950, 950, SK_ColorYELLOW); |
Julia Lavrova | 90bfd1c | 2019-12-04 11:43:32 -0500 | [diff] [blame] | 5201 | relayout(3, true, 50, 30, 90, 950, SK_ColorMAGENTA); |
Julia Lavrova | c028b42 | 2019-11-25 10:00:43 -0500 | [diff] [blame] | 5202 | relayout(std::numeric_limits<size_t>::max(), true, 50, 20, 950, 950, SK_ColorCYAN); |
| 5203 | |
| 5204 | relayout(1, false, 50, 10, 950, 950, SK_ColorRED); |
Julia Lavrova | 90bfd1c | 2019-12-04 11:43:32 -0500 | [diff] [blame] | 5205 | relayout(3, false, 50, 30, 90, 950, SK_ColorBLUE); |
| 5206 | 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] | 5207 | } |