Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1 | // Copyright 2019 Google LLC. |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2 | #include "src/utils/SkOSPath.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 | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 7 | #include "src/core/SkOSFile.h" |
| 8 | #include "src/utils/SkShaperJSONWriter.h" |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 9 | #include "tests/CodecPriv.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 10 | #include "tests/Test.h" |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 11 | #include "tools/Resources.h" |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 12 | #include "tools/ToolUtils.h" |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 13 | |
| 14 | #define VeryLongCanvasWidth 1000000 |
| 15 | #define TestCanvasWidth 1000 |
| 16 | #define TestCanvasHeight 600 |
| 17 | |
| 18 | using namespace skia::textlayout; |
| 19 | namespace { |
| 20 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 21 | SkScalar EPSILON100 = 0.01f; |
| 22 | SkScalar EPSILON50 = 0.02f; |
| 23 | SkScalar EPSILON20 = 0.05f; |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 24 | SkScalar EPSILON10 = 0.1f; |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 25 | SkScalar EPSILON5 = 0.20f; |
| 26 | SkScalar EPSILON2 = 0.50f; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 27 | |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 28 | bool equal(const char* base, TextRange a, const char* b) { |
| 29 | return std::strncmp(b, base + a.start, a.width()) == 0; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 30 | } |
| 31 | class TestFontCollection : public FontCollection { |
| 32 | public: |
| 33 | TestFontCollection() |
| 34 | : fFontsFound(false) |
| 35 | , fResolvedFonts(0) |
| 36 | , fResourceDir(GetResourcePath("fonts").c_str()) |
| 37 | , fFontProvider(sk_make_sp<TypefaceFontProvider>()) { |
| 38 | std::vector<SkString> fonts; |
| 39 | SkOSFile::Iter iter(fResourceDir.c_str()); |
| 40 | SkString path; |
| 41 | while (iter.next(&path)) { |
| 42 | if (path.endsWith("Roboto-Italic.ttf")) { |
| 43 | fFontsFound = true; |
| 44 | } |
| 45 | fonts.emplace_back(path); |
| 46 | } |
| 47 | |
| 48 | if (!fFontsFound) { |
| 49 | return; |
| 50 | } |
| 51 | // Only register fonts if we have to |
| 52 | for (auto& font : fonts) { |
| 53 | SkString file_path; |
| 54 | file_path.printf("%s/%s", fResourceDir.c_str(), font.c_str()); |
| 55 | fFontProvider->registerTypeface(SkTypeface::MakeFromFile(file_path.c_str())); |
| 56 | } |
| 57 | |
| 58 | this->setTestFontManager(std::move(fFontProvider)); |
| 59 | this->disableFontFallback(); |
| 60 | |
| 61 | if (!fFontsFound) SkDebugf("Fonts not found, skipping all the tests\n"); |
| 62 | } |
| 63 | |
| 64 | ~TestFontCollection() = default; |
| 65 | |
| 66 | size_t resolvedFonts() const { return fResolvedFonts; } |
| 67 | |
| 68 | // TODO: temp solution until we check in fonts |
| 69 | bool fontsFound() const { return fFontsFound; } |
| 70 | |
| 71 | private: |
| 72 | bool fFontsFound; |
| 73 | size_t fResolvedFonts; |
| 74 | std::string fResourceDir; |
| 75 | sk_sp<TypefaceFontProvider> fFontProvider; |
| 76 | }; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 77 | |
| 78 | class TestCanvas { |
| 79 | public: |
| 80 | TestCanvas(const char* testName) : name(testName) { |
| 81 | bits.allocN32Pixels(TestCanvasWidth, TestCanvasHeight); |
| 82 | canvas = new SkCanvas(bits); |
| 83 | canvas->clear(SK_ColorWHITE); |
| 84 | } |
| 85 | |
| 86 | ~TestCanvas() { |
Julia Lavrova | aae845f | 2019-08-16 16:44:40 -0400 | [diff] [blame] | 87 | SkString tmpDir = skiatest::GetTmpDir(); |
| 88 | if (!tmpDir.isEmpty()) { |
| 89 | SkString path = SkOSPath::Join(tmpDir.c_str(), name); |
| 90 | SkFILEWStream file(path.c_str()); |
| 91 | if (!SkEncodeImage(&file, bits, SkEncodedImageFormat::kPNG, 100)) { |
| 92 | SkDebugf("Cannot write a picture %s\n", name); |
| 93 | } |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 94 | } |
| 95 | delete canvas; |
| 96 | } |
| 97 | |
| 98 | void drawRects(SkColor color, std::vector<TextBox>& result, bool fill = false) { |
| 99 | |
| 100 | SkPaint paint; |
| 101 | if (!fill) { |
| 102 | paint.setStyle(SkPaint::kStroke_Style); |
| 103 | paint.setAntiAlias(true); |
| 104 | paint.setStrokeWidth(1); |
| 105 | } |
| 106 | paint.setColor(color); |
| 107 | for (auto& r : result) { |
| 108 | canvas->drawRect(r.rect, paint); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | void drawLine(SkColor color, SkRect rect, bool vertical = true) { |
| 113 | |
| 114 | SkPaint paint; |
| 115 | paint.setStyle(SkPaint::kStroke_Style); |
| 116 | paint.setAntiAlias(true); |
| 117 | paint.setStrokeWidth(1); |
| 118 | paint.setColor(color); |
| 119 | if (vertical) { |
| 120 | canvas->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint); |
| 121 | } else { |
| 122 | canvas->drawLine(rect.fLeft, rect.fTop, rect.fRight, rect.fTop, paint); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | void drawLines(SkColor color, std::vector<TextBox>& result) { |
| 127 | |
| 128 | for (auto& r : result) { |
| 129 | drawLine(color, r.rect); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | SkCanvas* get() { return canvas; } |
| 134 | private: |
| 135 | SkBitmap bits; |
| 136 | SkCanvas* canvas; |
| 137 | const char* name; |
| 138 | }; |
| 139 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 140 | } // namespace |
| 141 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 142 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 143 | DEF_TEST(SkParagraph_SimpleParagraph, reporter) { |
| 144 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 145 | if (!fontCollection->fontsFound()) return; |
| 146 | const char* text = "Hello World Text Dialog"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 147 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 148 | |
| 149 | ParagraphStyle paragraph_style; |
| 150 | paragraph_style.turnHintingOff(); |
| 151 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 152 | |
| 153 | TextStyle text_style; |
| 154 | text_style.setFontFamilies({SkString("Roboto")}); |
| 155 | text_style.setColor(SK_ColorBLACK); |
| 156 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 157 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 158 | builder.pop(); |
| 159 | |
| 160 | auto paragraph = builder.Build(); |
| 161 | paragraph->layout(TestCanvasWidth); |
| 162 | |
| 163 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 164 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 165 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); // paragraph style does not count |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 166 | REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 167 | |
| 168 | size_t index = 0; |
| 169 | for (auto& line : impl->lines()) { |
| 170 | line.scanStyles(StyleType::kDecorations, |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 171 | [&index, reporter] |
| 172 | (TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 173 | REPORTER_ASSERT(reporter, index == 0); |
| 174 | REPORTER_ASSERT(reporter, style.getColor() == SK_ColorBLACK); |
| 175 | ++index; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 176 | }); |
| 177 | } |
| 178 | } |
| 179 | |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 180 | // Checked: DIFF+ (half of the letter spacing before the text???) |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 181 | DEF_TEST(SkParagraph_InlinePlaceholderParagraph, reporter) { |
| 182 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 183 | TestCanvas canvas("SkParagraph_InlinePlaceholderParagraph.png"); |
| 184 | if (!fontCollection->fontsFound()) return; |
| 185 | |
| 186 | const char* text = "012 34"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 187 | const size_t len = strlen(text); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 188 | |
| 189 | ParagraphStyle paragraph_style; |
| 190 | paragraph_style.turnHintingOff(); |
| 191 | paragraph_style.setMaxLines(14); |
| 192 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 193 | |
| 194 | TextStyle text_style; |
| 195 | text_style.setFontFamilies({SkString("Roboto")}); |
| 196 | text_style.setColor(SK_ColorBLACK); |
| 197 | text_style.setFontSize(26); |
| 198 | text_style.setWordSpacing(5); |
| 199 | text_style.setLetterSpacing(1); |
| 200 | text_style.setDecoration(TextDecoration::kUnderline); |
| 201 | text_style.setDecorationColor(SK_ColorBLACK); |
| 202 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 203 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 204 | |
| 205 | PlaceholderStyle placeholder1(50, 50, PlaceholderAlignment::kBaseline, TextBaseline::kAlphabetic, 0); |
| 206 | builder.addPlaceholder(placeholder1); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 207 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 208 | builder.addPlaceholder(placeholder1); |
| 209 | |
| 210 | PlaceholderStyle placeholder2(5, 50, PlaceholderAlignment::kBaseline, TextBaseline::kAlphabetic, 50); |
| 211 | builder.addPlaceholder(placeholder2); |
| 212 | builder.addPlaceholder(placeholder1); |
| 213 | builder.addPlaceholder(placeholder2); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 214 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 215 | builder.addPlaceholder(placeholder2); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 216 | builder.addText(text, len); |
| 217 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 218 | builder.addPlaceholder(placeholder2); |
| 219 | builder.addPlaceholder(placeholder2); |
| 220 | builder.addPlaceholder(placeholder2); |
| 221 | builder.addPlaceholder(placeholder2); |
| 222 | builder.addPlaceholder(placeholder2); |
| 223 | builder.addPlaceholder(placeholder1); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 224 | builder.addText(text, len); |
| 225 | builder.addText(text, len); |
| 226 | builder.addText(text, len); |
| 227 | builder.addText(text, len); |
| 228 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 229 | builder.addPlaceholder(placeholder2); |
| 230 | builder.addPlaceholder(placeholder1); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 231 | builder.addText(text, len); |
| 232 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 233 | |
| 234 | builder.pop(); |
| 235 | |
| 236 | auto paragraph = builder.Build(); |
| 237 | paragraph->layout(TestCanvasWidth); |
| 238 | paragraph->paint(canvas.get(), 0, 0); |
| 239 | |
| 240 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 241 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 242 | |
| 243 | auto boxes = paragraph->getRectsForRange(0, 3, rect_height_style, rect_width_style); |
| 244 | canvas.drawRects(SK_ColorRED, boxes); |
| 245 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 246 | |
| 247 | boxes = paragraph->getRectsForRange(0, 3, rect_height_style, rect_width_style); |
| 248 | canvas.drawRects(SK_ColorGREEN, boxes); |
| 249 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 250 | |
| 251 | boxes = paragraph->GetRectsForPlaceholders(); |
| 252 | canvas.drawRects(SK_ColorRED, boxes); |
| 253 | |
| 254 | boxes = paragraph->getRectsForRange(4, 17, rect_height_style, rect_width_style); |
| 255 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 256 | |
| 257 | REPORTER_ASSERT(reporter, boxes.size() == 7); |
| 258 | |
| 259 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.left(), 90.921f - 0.5f, EPSILON2)); |
| 260 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.top(), 50, EPSILON100)); |
| 261 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.right(), 90.921f + 50 - 0.5f, EPSILON2)); |
| 262 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.bottom(), 100, EPSILON100)); |
| 263 | |
| 264 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[3].rect.left(), 231.343f - 0.5f, EPSILON2)); |
| 265 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[3].rect.top(), 50, EPSILON100)); |
| 266 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[3].rect.right(), 231.343f + 50 - 0.5f, EPSILON2)); |
| 267 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[3].rect.bottom(), 100, EPSILON100)); |
| 268 | |
| 269 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[4].rect.left(), 281.343f - 0.5f, EPSILON2)); |
| 270 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[4].rect.top(), 0, EPSILON100)); |
| 271 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[4].rect.right(), 281.343f + 5 - 0.5f, EPSILON2)); |
| 272 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[4].rect.bottom(), 50, EPSILON100)); |
| 273 | |
| 274 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[6].rect.left(), 336.343f - 0.5f, EPSILON2)); |
| 275 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[6].rect.top(), 0, EPSILON100)); |
| 276 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[6].rect.right(), 336.343f + 5 - 0.5f, EPSILON2)); |
| 277 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[6].rect.bottom(), 50, EPSILON100)); |
| 278 | } |
| 279 | |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 280 | // Checked: DIFF+ (half of the letter spacing before the text???) |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 281 | DEF_TEST(SkParagraph_InlinePlaceholderBaselineParagraph, reporter) { |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 282 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 283 | TestCanvas canvas("SkParagraph_InlinePlaceholderBaselineParagraph.png"); |
| 284 | if (!fontCollection->fontsFound()) return; |
| 285 | |
| 286 | const char* text = "012 34"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 287 | const size_t len = strlen(text); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 288 | |
| 289 | ParagraphStyle paragraph_style; |
| 290 | paragraph_style.turnHintingOff(); |
| 291 | paragraph_style.setMaxLines(14); |
| 292 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 293 | |
| 294 | TextStyle text_style; |
| 295 | text_style.setFontFamilies({SkString("Roboto")}); |
| 296 | text_style.setColor(SK_ColorBLACK); |
| 297 | text_style.setFontSize(26); |
| 298 | text_style.setWordSpacing(5); |
| 299 | text_style.setLetterSpacing(1); |
| 300 | text_style.setDecoration(TextDecoration::kUnderline); |
| 301 | text_style.setDecorationColor(SK_ColorBLACK); |
| 302 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 303 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 304 | |
| 305 | PlaceholderStyle placeholder(55, 50, PlaceholderAlignment::kBaseline, TextBaseline::kAlphabetic, 38.347f); |
| 306 | builder.addPlaceholder(placeholder); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 307 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 308 | |
| 309 | builder.pop(); |
| 310 | |
| 311 | auto paragraph = builder.Build(); |
| 312 | paragraph->layout(TestCanvasWidth); |
| 313 | paragraph->paint(canvas.get(), 0, 0); |
| 314 | |
| 315 | auto boxes = paragraph->GetRectsForPlaceholders(); |
| 316 | canvas.drawRects(SK_ColorRED, boxes); |
| 317 | |
| 318 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 319 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 90.921f - 0.5f, EPSILON2)); |
| 320 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100)); |
| 321 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f + 55 - 0.5f, EPSILON2)); |
| 322 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 50, EPSILON100)); |
| 323 | |
| 324 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 325 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 326 | |
| 327 | boxes = paragraph->getRectsForRange(5, 6, rect_height_style, rect_width_style); |
| 328 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 329 | |
| 330 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 331 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 75.324f - 0.5f, EPSILON2)); |
| 332 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 14.226f, EPSILON100)); |
| 333 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f - 0.5f, EPSILON2)); |
| 334 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 44.694f, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 335 | } |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 336 | |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 337 | // Checked: DIFF+ (half of the letter spacing before the text???) |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 338 | DEF_TEST(SkParagraph_InlinePlaceholderAboveBaselineParagraph, reporter) { |
| 339 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 340 | TestCanvas canvas("SkParagraph_InlinePlaceholderAboveBaselineParagraph.png"); |
| 341 | if (!fontCollection->fontsFound()) return; |
| 342 | |
| 343 | const char* text = "012 34"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 344 | const size_t len = strlen(text); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 345 | |
| 346 | ParagraphStyle paragraph_style; |
| 347 | paragraph_style.turnHintingOff(); |
| 348 | paragraph_style.setMaxLines(14); |
| 349 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 350 | |
| 351 | TextStyle text_style; |
| 352 | text_style.setFontFamilies({SkString("Roboto")}); |
| 353 | text_style.setColor(SK_ColorBLACK); |
| 354 | text_style.setFontSize(26); |
| 355 | text_style.setWordSpacing(5); |
| 356 | text_style.setLetterSpacing(1); |
| 357 | text_style.setDecoration(TextDecoration::kUnderline); |
| 358 | text_style.setDecorationColor(SK_ColorBLACK); |
| 359 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 360 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 361 | |
| 362 | PlaceholderStyle placeholder(55, 50, PlaceholderAlignment::kAboveBaseline, TextBaseline::kAlphabetic, 903129.129308f); |
| 363 | builder.addPlaceholder(placeholder); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 364 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 365 | |
| 366 | builder.pop(); |
| 367 | |
| 368 | auto paragraph = builder.Build(); |
| 369 | paragraph->layout(TestCanvasWidth); |
| 370 | paragraph->paint(canvas.get(), 0, 0); |
| 371 | |
| 372 | auto boxes = paragraph->GetRectsForPlaceholders(); |
| 373 | canvas.drawRects(SK_ColorRED, boxes); |
| 374 | |
| 375 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 376 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 90.921f - 0.5f, EPSILON2)); |
| 377 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), -0.347f, EPSILON100)); |
| 378 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f + 55 - 0.5f, EPSILON2)); |
| 379 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 49.652f, EPSILON100)); |
| 380 | |
| 381 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 382 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 383 | |
| 384 | boxes = paragraph->getRectsForRange(5, 6, rect_height_style, rect_width_style); |
| 385 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 386 | |
| 387 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 388 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 75.324f - 0.5f, EPSILON2)); |
| 389 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 25.531f, EPSILON100)); |
| 390 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f - 0.5f, EPSILON2)); |
| 391 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 56, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 392 | } |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 393 | |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 394 | // Checked: DIFF+ (half of the letter spacing before the text???) |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 395 | DEF_TEST(SkParagraph_InlinePlaceholderBelowBaselineParagraph, reporter) { |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 396 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 397 | TestCanvas canvas("SkParagraph_InlinePlaceholderBelowBaselineParagraph.png"); |
| 398 | if (!fontCollection->fontsFound()) return; |
| 399 | |
| 400 | const char* text = "012 34"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 401 | const size_t len = strlen(text); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 402 | |
| 403 | ParagraphStyle paragraph_style; |
| 404 | paragraph_style.turnHintingOff(); |
| 405 | paragraph_style.setMaxLines(14); |
| 406 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 407 | |
| 408 | TextStyle text_style; |
| 409 | text_style.setFontFamilies({SkString("Roboto")}); |
| 410 | text_style.setColor(SK_ColorBLACK); |
| 411 | text_style.setFontSize(26); |
| 412 | text_style.setWordSpacing(5); |
| 413 | text_style.setLetterSpacing(1); |
| 414 | text_style.setDecoration(TextDecoration::kUnderline); |
| 415 | text_style.setDecorationColor(SK_ColorBLACK); |
| 416 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 417 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 418 | |
| 419 | PlaceholderStyle placeholder(55, 50, PlaceholderAlignment::kBelowBaseline, TextBaseline::kAlphabetic, 903129.129308f); |
| 420 | builder.addPlaceholder(placeholder); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 421 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 422 | |
| 423 | builder.pop(); |
| 424 | |
| 425 | auto paragraph = builder.Build(); |
| 426 | paragraph->layout(TestCanvasWidth); |
| 427 | paragraph->paint(canvas.get(), 0, 0); |
| 428 | |
| 429 | auto boxes = paragraph->GetRectsForPlaceholders(); |
| 430 | canvas.drawRects(SK_ColorRED, boxes); |
| 431 | |
| 432 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 433 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 90.921f - 0.5f, EPSILON2)); |
| 434 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 24, EPSILON100)); |
| 435 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f + 55 - 0.5f, EPSILON2)); |
| 436 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 74, EPSILON100)); |
| 437 | |
| 438 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 439 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 440 | |
| 441 | boxes = paragraph->getRectsForRange(5, 6, rect_height_style, rect_width_style); |
| 442 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 443 | |
| 444 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 445 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 75.324f - 0.5f, EPSILON2)); |
| 446 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), -0.121f, EPSILON100)); |
| 447 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f - 0.5f, EPSILON2)); |
| 448 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 30.347f, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 449 | } |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 450 | |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 451 | // Checked: DIFF+ (half of the letter spacing before the text???) |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 452 | DEF_TEST(SkParagraph_InlinePlaceholderBottomParagraph, reporter) { |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 453 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 454 | TestCanvas canvas("SkParagraph_InlinePlaceholderBottomParagraph.png"); |
| 455 | if (!fontCollection->fontsFound()) return; |
| 456 | |
| 457 | const char* text = "012 34"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 458 | const size_t len = strlen(text); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 459 | |
| 460 | ParagraphStyle paragraph_style; |
| 461 | paragraph_style.turnHintingOff(); |
| 462 | paragraph_style.setMaxLines(14); |
| 463 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 464 | |
| 465 | TextStyle text_style; |
| 466 | text_style.setFontFamilies({SkString("Roboto")}); |
| 467 | text_style.setColor(SK_ColorBLACK); |
| 468 | text_style.setFontSize(26); |
| 469 | text_style.setWordSpacing(5); |
| 470 | text_style.setLetterSpacing(1); |
| 471 | text_style.setDecoration(TextDecoration::kUnderline); |
| 472 | text_style.setDecorationColor(SK_ColorBLACK); |
| 473 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 474 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 475 | |
| 476 | PlaceholderStyle placeholder(55, 50, PlaceholderAlignment::kBottom, TextBaseline::kAlphabetic, 0); |
| 477 | builder.addPlaceholder(placeholder); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 478 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 479 | |
| 480 | builder.pop(); |
| 481 | |
| 482 | auto paragraph = builder.Build(); |
| 483 | paragraph->layout(TestCanvasWidth); |
| 484 | paragraph->paint(canvas.get(), 0, 0); |
| 485 | |
| 486 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 487 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 488 | |
| 489 | auto boxes = paragraph->GetRectsForPlaceholders(); |
| 490 | canvas.drawRects(SK_ColorRED, boxes); |
| 491 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 492 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 90.921f - 0.5f, EPSILON50)); |
| 493 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100)); |
| 494 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f + 55 - 0.5f, EPSILON50)); |
| 495 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 50, EPSILON100)); |
| 496 | |
| 497 | boxes = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style); |
| 498 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 499 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 500 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0.5f - 0.5f, EPSILON50)); |
| 501 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 19.531f, EPSILON100)); |
| 502 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 16.097f - 0.5f, EPSILON50)); |
| 503 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 50, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 504 | } |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 505 | |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 506 | // Checked: DIFF+ (half of the letter spacing before the text???) |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 507 | DEF_TEST(SkParagraph_InlinePlaceholderTopParagraph, reporter) { |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 508 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 509 | TestCanvas canvas("SkParagraph_InlinePlaceholderTopParagraph.png"); |
| 510 | if (!fontCollection->fontsFound()) return; |
| 511 | |
| 512 | const char* text = "012 34"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 513 | const size_t len = strlen(text); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 514 | |
| 515 | ParagraphStyle paragraph_style; |
| 516 | paragraph_style.turnHintingOff(); |
| 517 | paragraph_style.setMaxLines(14); |
| 518 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 519 | |
| 520 | TextStyle text_style; |
| 521 | text_style.setFontFamilies({SkString("Roboto")}); |
| 522 | text_style.setColor(SK_ColorBLACK); |
| 523 | text_style.setFontSize(26); |
| 524 | text_style.setWordSpacing(5); |
| 525 | text_style.setLetterSpacing(1); |
| 526 | text_style.setDecoration(TextDecoration::kUnderline); |
| 527 | text_style.setDecorationColor(SK_ColorBLACK); |
| 528 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 529 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 530 | |
| 531 | PlaceholderStyle placeholder(55, 50, PlaceholderAlignment::kTop, TextBaseline::kAlphabetic, 0); |
| 532 | builder.addPlaceholder(placeholder); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 533 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 534 | |
| 535 | builder.pop(); |
| 536 | |
| 537 | auto paragraph = builder.Build(); |
| 538 | paragraph->layout(TestCanvasWidth); |
| 539 | paragraph->paint(canvas.get(), 0, 0); |
| 540 | |
| 541 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 542 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 543 | |
| 544 | auto boxes = paragraph->GetRectsForPlaceholders(); |
| 545 | canvas.drawRects(SK_ColorRED, boxes); |
| 546 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 547 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 90.921f - 0.5f, EPSILON50)); |
| 548 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100)); |
| 549 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f + 55 - 0.5f, EPSILON50)); |
| 550 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 50, EPSILON100)); |
| 551 | |
| 552 | boxes = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style); |
| 553 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 554 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 555 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0.5f - 0.5f, EPSILON50)); |
| 556 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100)); |
| 557 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 16.097f - 0.5f, EPSILON50)); |
| 558 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 30.468f, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 559 | } |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 560 | |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 561 | // Checked: DIFF+ (half of the letter spacing before the text???) |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 562 | DEF_TEST(SkParagraph_InlinePlaceholderMiddleParagraph, reporter) { |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 563 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 564 | TestCanvas canvas("SkParagraph_InlinePlaceholderMiddleParagraph.png"); |
| 565 | if (!fontCollection->fontsFound()) return; |
| 566 | |
| 567 | const char* text = "012 34"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 568 | const size_t len = strlen(text); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 569 | |
| 570 | ParagraphStyle paragraph_style; |
| 571 | paragraph_style.turnHintingOff(); |
| 572 | paragraph_style.setMaxLines(14); |
| 573 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 574 | |
| 575 | TextStyle text_style; |
| 576 | text_style.setFontFamilies({SkString("Roboto")}); |
| 577 | text_style.setColor(SK_ColorBLACK); |
| 578 | text_style.setFontSize(26); |
| 579 | text_style.setWordSpacing(5); |
| 580 | text_style.setLetterSpacing(1); |
| 581 | text_style.setDecoration(TextDecoration::kUnderline); |
| 582 | text_style.setDecorationColor(SK_ColorBLACK); |
| 583 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 584 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 585 | |
| 586 | PlaceholderStyle placeholder(55, 50, PlaceholderAlignment::kMiddle, TextBaseline::kAlphabetic, 0); |
| 587 | builder.addPlaceholder(placeholder); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 588 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 589 | |
| 590 | builder.pop(); |
| 591 | |
| 592 | auto paragraph = builder.Build(); |
| 593 | paragraph->layout(TestCanvasWidth); |
| 594 | paragraph->paint(canvas.get(), 0, 0); |
| 595 | |
| 596 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 597 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 598 | |
| 599 | auto boxes = paragraph->GetRectsForPlaceholders(); |
| 600 | canvas.drawRects(SK_ColorRED, boxes); |
| 601 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 602 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 90.921f - 0.5f, EPSILON50)); |
| 603 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100)); |
| 604 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f + 55 - 0.5f, EPSILON50)); |
| 605 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 50, EPSILON100)); |
| 606 | |
| 607 | boxes = paragraph->getRectsForRange(5, 6, rect_height_style, rect_width_style); |
| 608 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 609 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 610 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 75.324f - 0.5f, EPSILON50)); |
| 611 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 9.765f, EPSILON100)); |
| 612 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f - 0.5f, EPSILON50)); |
| 613 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 40.234f, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 614 | } |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 615 | |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 616 | // Checked: DIFF+ (half of the letter spacing before the text???) |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 617 | DEF_TEST(SkParagraph_InlinePlaceholderIdeographicBaselineParagraph, reporter) { |
| 618 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 619 | TestCanvas canvas("SkParagraph_InlinePlaceholderIdeographicBaselineParagraph.png"); |
| 620 | if (!fontCollection->fontsFound()) return; |
| 621 | |
| 622 | const char* text = "給能上目秘使"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 623 | const size_t len = strlen(text); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 624 | |
| 625 | ParagraphStyle paragraph_style; |
| 626 | paragraph_style.turnHintingOff(); |
| 627 | paragraph_style.setMaxLines(14); |
| 628 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 629 | |
| 630 | TextStyle text_style; |
| 631 | text_style.setFontFamilies({SkString("Source Han Serif CN")}); |
| 632 | text_style.setColor(SK_ColorBLACK); |
| 633 | text_style.setFontSize(26); |
| 634 | text_style.setWordSpacing(5); |
| 635 | text_style.setLetterSpacing(1); |
| 636 | text_style.setDecoration(TextDecoration::kUnderline); |
| 637 | text_style.setDecorationColor(SK_ColorBLACK); |
| 638 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 639 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 640 | PlaceholderStyle placeholder(55, 50, PlaceholderAlignment::kBaseline, TextBaseline::kIdeographic, 38.347f); |
| 641 | builder.addPlaceholder(placeholder); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 642 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 643 | |
| 644 | builder.pop(); |
| 645 | |
| 646 | auto paragraph = builder.Build(); |
| 647 | paragraph->layout(TestCanvasWidth); |
| 648 | paragraph->paint(canvas.get(), 0, 0); |
| 649 | |
| 650 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 651 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 652 | |
| 653 | auto boxes = paragraph->GetRectsForPlaceholders(); |
| 654 | canvas.drawRects(SK_ColorRED, boxes); |
| 655 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 656 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 162.5f - 0.5f, EPSILON50)); |
| 657 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100)); |
| 658 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 162.5f + 55 - 0.5f, EPSILON50)); |
| 659 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 50, EPSILON100)); |
| 660 | |
| 661 | boxes = paragraph->getRectsForRange(5, 6, rect_height_style, rect_width_style); |
| 662 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 663 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 664 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 135.5f - 0.5f, EPSILON50)); |
| 665 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 4.703f, EPSILON100)); |
| 666 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 162.5f - 0.5f, EPSILON50)); |
| 667 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 42.065f, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 668 | } |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 669 | |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 670 | // Checked: DIFF+ (half of the letter spacing before the text???) |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 671 | DEF_TEST(SkParagraph_InlinePlaceholderBreakParagraph, reporter) { |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 672 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 673 | TestCanvas canvas("SkParagraph_InlinePlaceholderBreakParagraph.png"); |
| 674 | if (!fontCollection->fontsFound()) return; |
| 675 | |
| 676 | const char* text = "012 34"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 677 | const size_t len = strlen(text); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 678 | |
| 679 | ParagraphStyle paragraph_style; |
| 680 | paragraph_style.turnHintingOff(); |
| 681 | paragraph_style.setMaxLines(14); |
| 682 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 683 | |
| 684 | TextStyle text_style; |
| 685 | text_style.setFontFamilies({SkString("Roboto")}); |
| 686 | text_style.setColor(SK_ColorBLACK); |
| 687 | text_style.setFontSize(26); |
| 688 | text_style.setWordSpacing(5); |
| 689 | text_style.setLetterSpacing(1); |
| 690 | text_style.setDecoration(TextDecoration::kUnderline); |
| 691 | text_style.setDecorationColor(SK_ColorBLACK); |
| 692 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 693 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 694 | |
| 695 | PlaceholderStyle placeholder1(50, 50, PlaceholderAlignment::kBaseline, TextBaseline::kAlphabetic, 50); |
| 696 | PlaceholderStyle placeholder2(25, 25, PlaceholderAlignment::kBaseline, TextBaseline::kAlphabetic, 12.5f); |
| 697 | |
| 698 | builder.addPlaceholder(placeholder1); |
| 699 | builder.addPlaceholder(placeholder1); |
| 700 | builder.addPlaceholder(placeholder1); |
| 701 | builder.addPlaceholder(placeholder2); |
| 702 | builder.addPlaceholder(placeholder1); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 703 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 704 | |
| 705 | builder.addPlaceholder(placeholder1); |
| 706 | builder.addPlaceholder(placeholder1); |
| 707 | builder.addPlaceholder(placeholder1); |
| 708 | builder.addPlaceholder(placeholder1); |
| 709 | builder.addPlaceholder(placeholder2); // 4 + 1 |
| 710 | builder.addPlaceholder(placeholder1); |
| 711 | builder.addPlaceholder(placeholder1); |
| 712 | builder.addPlaceholder(placeholder1); |
| 713 | builder.addPlaceholder(placeholder1); |
| 714 | builder.addPlaceholder(placeholder1); |
| 715 | builder.addPlaceholder(placeholder1); |
| 716 | builder.addPlaceholder(placeholder2); // 6 + 1 |
| 717 | builder.addPlaceholder(placeholder1); |
| 718 | builder.addPlaceholder(placeholder1); |
| 719 | builder.addPlaceholder(placeholder1); |
| 720 | builder.addPlaceholder(placeholder1); |
| 721 | builder.addPlaceholder(placeholder1); |
| 722 | builder.addPlaceholder(placeholder1); |
| 723 | builder.addPlaceholder(placeholder1); |
| 724 | builder.addPlaceholder(placeholder2); // 7 + 1 |
| 725 | |
| 726 | builder.addPlaceholder(placeholder1); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 727 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 728 | builder.addPlaceholder(placeholder1); |
| 729 | builder.addPlaceholder(placeholder2); |
| 730 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 731 | builder.addText(text, len); |
| 732 | builder.addText(text, len); |
| 733 | builder.addText(text, len); |
| 734 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 735 | |
| 736 | builder.addPlaceholder(placeholder2); |
| 737 | builder.addPlaceholder(placeholder1); |
| 738 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 739 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 740 | |
| 741 | builder.addPlaceholder(placeholder2); |
| 742 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 743 | builder.addText(text, len); |
| 744 | builder.addText(text, len); |
| 745 | builder.addText(text, len); |
| 746 | builder.addText(text, len); |
| 747 | builder.addText(text, len); |
| 748 | builder.addText(text, len); |
| 749 | builder.addText(text, len); |
| 750 | builder.addText(text, len); |
| 751 | builder.addText(text, len); |
| 752 | builder.addText(text, len); |
| 753 | builder.addText(text, len); |
| 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); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 762 | |
| 763 | builder.pop(); |
| 764 | |
| 765 | auto paragraph = builder.Build(); |
| 766 | paragraph->layout(TestCanvasWidth - 100); |
| 767 | paragraph->paint(canvas.get(), 0, 0); |
| 768 | |
| 769 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 770 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 771 | |
| 772 | auto boxes = paragraph->getRectsForRange(0, 3, rect_height_style, rect_width_style); |
| 773 | canvas.drawRects(SK_ColorRED, boxes); |
| 774 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 775 | |
| 776 | boxes = paragraph->getRectsForRange(175, 176, rect_height_style, rect_width_style); |
| 777 | canvas.drawRects(SK_ColorGREEN, boxes); |
| 778 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 779 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 31.695f - 0.5f, EPSILON50)); |
| 780 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 218.531f, EPSILON100)); |
| 781 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 47.292f - 0.5f, EPSILON50)); |
| 782 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 249, EPSILON100)); |
| 783 | |
| 784 | boxes = paragraph->GetRectsForPlaceholders(); |
| 785 | canvas.drawRects(SK_ColorRED, boxes); |
| 786 | |
| 787 | boxes = paragraph->getRectsForRange(4, 45, rect_height_style, rect_width_style); |
| 788 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 789 | REPORTER_ASSERT(reporter, boxes.size() == 30); |
| 790 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 59.726f - 0.5f, EPSILON50)); |
| 791 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 26.378f, EPSILON100)); |
| 792 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f - 0.5f, EPSILON50)); |
| 793 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 56.847f, EPSILON100)); |
| 794 | |
| 795 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[11].rect.left(), 606.343f - 0.5f, EPSILON20)); |
| 796 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[11].rect.top(), 38, EPSILON100)); |
| 797 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[11].rect.right(), 631.343f - 0.5f, EPSILON20)); |
| 798 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[11].rect.bottom(), 63, EPSILON100)); |
| 799 | |
| 800 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[17].rect.left(), 0.5f - 0.5f, EPSILON50)); |
| 801 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[17].rect.top(), 63.5f, EPSILON100)); |
| 802 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[17].rect.right(), 50.5f - 0.5f, EPSILON50)); |
| 803 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[17].rect.bottom(), 113.5f, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 804 | } |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 805 | |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 806 | // Checked: DIFF+ (half of the letter spacing before the text???) |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 807 | DEF_TEST(SkParagraph_InlinePlaceholderGetRectsParagraph, reporter) { |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 808 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 809 | TestCanvas canvas("SkParagraph_InlinePlaceholderGetRectsParagraph.png"); |
| 810 | if (!fontCollection->fontsFound()) return; |
| 811 | |
| 812 | const char* text = "012 34"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 813 | const size_t len = strlen(text); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 814 | |
| 815 | ParagraphStyle paragraph_style; |
| 816 | paragraph_style.turnHintingOff(); |
| 817 | paragraph_style.setMaxLines(14); |
| 818 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 819 | |
| 820 | TextStyle text_style; |
| 821 | text_style.setFontFamilies({SkString("Roboto")}); |
| 822 | text_style.setColor(SK_ColorBLACK); |
| 823 | text_style.setFontSize(26); |
| 824 | text_style.setWordSpacing(5); |
| 825 | text_style.setLetterSpacing(1); |
| 826 | text_style.setDecoration(TextDecoration::kUnderline); |
| 827 | text_style.setDecorationColor(SK_ColorBLACK); |
| 828 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 829 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 830 | |
| 831 | PlaceholderStyle placeholder1(50, 50, PlaceholderAlignment::kBaseline, TextBaseline::kAlphabetic, 50); |
| 832 | PlaceholderStyle placeholder2(5, 20, PlaceholderAlignment::kBaseline, TextBaseline::kAlphabetic, 10); |
| 833 | |
| 834 | builder.addPlaceholder(placeholder1); |
| 835 | builder.addPlaceholder(placeholder1); |
| 836 | builder.addPlaceholder(placeholder1); |
| 837 | builder.addPlaceholder(placeholder1); |
| 838 | builder.addPlaceholder(placeholder1); |
| 839 | builder.addPlaceholder(placeholder1); |
| 840 | builder.addPlaceholder(placeholder1); |
| 841 | builder.addPlaceholder(placeholder1); |
| 842 | builder.addPlaceholder(placeholder2); // 8 + 1 |
| 843 | builder.addPlaceholder(placeholder1); |
| 844 | builder.addPlaceholder(placeholder1); |
| 845 | builder.addPlaceholder(placeholder1); |
| 846 | builder.addPlaceholder(placeholder1); |
| 847 | builder.addPlaceholder(placeholder1); |
| 848 | builder.addPlaceholder(placeholder2); // 5 + 1 |
| 849 | builder.addPlaceholder(placeholder1); |
| 850 | builder.addPlaceholder(placeholder1); |
| 851 | builder.addPlaceholder(placeholder1); |
| 852 | builder.addPlaceholder(placeholder1); |
| 853 | builder.addPlaceholder(placeholder1); |
| 854 | builder.addPlaceholder(placeholder1); |
| 855 | builder.addPlaceholder(placeholder1); |
| 856 | builder.addPlaceholder(placeholder1); // 8 + 0 |
| 857 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 858 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 859 | |
| 860 | builder.addPlaceholder(placeholder1); |
| 861 | builder.addPlaceholder(placeholder2); |
| 862 | builder.addPlaceholder(placeholder2); // 1 + 2 |
| 863 | builder.addPlaceholder(placeholder1); |
| 864 | builder.addPlaceholder(placeholder2); |
| 865 | builder.addPlaceholder(placeholder2); // 1 + 2 |
| 866 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 867 | builder.addText(text, len); |
| 868 | builder.addText(text, len); |
| 869 | builder.addText(text, len); |
| 870 | builder.addText(text, len); |
| 871 | builder.addText(text, len); |
| 872 | builder.addText(text, len); |
| 873 | builder.addText(text, len); |
| 874 | builder.addText(text, len); |
| 875 | builder.addText(text, len); |
| 876 | builder.addText(text, len); |
| 877 | builder.addText(text, len); // 11 |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 878 | |
| 879 | builder.addPlaceholder(placeholder2); |
| 880 | builder.addPlaceholder(placeholder1); |
| 881 | builder.addPlaceholder(placeholder2); |
| 882 | builder.addPlaceholder(placeholder1); |
| 883 | builder.addPlaceholder(placeholder2); |
| 884 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 885 | builder.addText(text, len); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 886 | |
| 887 | builder.pop(); |
| 888 | |
| 889 | auto paragraph = builder.Build(); |
| 890 | paragraph->layout(TestCanvasWidth); |
| 891 | paragraph->paint(canvas.get(), 0, 0); |
| 892 | |
| 893 | RectHeightStyle rect_height_style = RectHeightStyle::kMax; |
| 894 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 895 | |
| 896 | auto boxes = paragraph->GetRectsForPlaceholders(); |
| 897 | canvas.drawRects(SK_ColorRED, boxes); |
| 898 | |
| 899 | REPORTER_ASSERT(reporter, boxes.size() == 34); |
| 900 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 90.921f - 0.5f, EPSILON50)); |
| 901 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100)); |
| 902 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 140.921f - 0.5f, EPSILON50)); |
| 903 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 50, EPSILON100)); |
| 904 | |
| 905 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[16].rect.left(), 800.921f - 0.5f, EPSILON20)); |
| 906 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[16].rect.top(), 0, EPSILON100)); |
| 907 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[16].rect.right(), 850.921f - 0.5f, EPSILON20)); |
| 908 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[16].rect.bottom(), 50, EPSILON100)); |
| 909 | |
| 910 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[33].rect.left(), 503.382f - 0.5f, EPSILON10)); |
| 911 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[33].rect.top(), 160, EPSILON100)); |
| 912 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[33].rect.right(), 508.382f - 0.5f, EPSILON10)); |
| 913 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[33].rect.bottom(), 180, EPSILON100)); |
| 914 | |
| 915 | boxes = paragraph->getRectsForRange(30, 50, rect_height_style, rect_width_style); |
| 916 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 917 | |
| 918 | REPORTER_ASSERT(reporter, boxes.size() == 8); |
| 919 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 216.097f - 0.5f, EPSILON50)); |
| 920 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 60, EPSILON100)); |
| 921 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 290.921f - 0.5f, EPSILON50)); |
| 922 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 120, EPSILON100)); |
| 923 | |
| 924 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.left(), 290.921f - 0.5f, EPSILON20)); |
| 925 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.top(), 60, EPSILON100)); |
| 926 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.right(), 340.921f - 0.5f, EPSILON20)); |
| 927 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.bottom(), 120, EPSILON100)); |
| 928 | |
| 929 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[2].rect.left(), 340.921f - 0.5f, EPSILON50)); |
| 930 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[2].rect.top(), 60, EPSILON100)); |
| 931 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[2].rect.right(), 345.921f - 0.5f, EPSILON50)); |
| 932 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[2].rect.bottom(), 120, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 936 | DEF_TEST(SkParagraph_SimpleRedParagraph, reporter) { |
| 937 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 938 | if (!fontCollection->fontsFound()) return; |
| 939 | const char* text = "I am RED"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 940 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 941 | |
| 942 | ParagraphStyle paragraph_style; |
| 943 | paragraph_style.turnHintingOff(); |
| 944 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 945 | |
| 946 | TextStyle text_style; |
| 947 | text_style.setFontFamilies({SkString("Roboto")}); |
| 948 | text_style.setColor(SK_ColorRED); |
| 949 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 950 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 951 | builder.pop(); |
| 952 | |
| 953 | auto paragraph = builder.Build(); |
| 954 | paragraph->layout(TestCanvasWidth); |
| 955 | |
| 956 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 957 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 958 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); // paragraph style does not count |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 959 | REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 960 | |
| 961 | size_t index = 0; |
| 962 | for (auto& line : impl->lines()) { |
| 963 | line.scanStyles(StyleType::kDecorations, |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 964 | [reporter, &index](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
| 965 | REPORTER_ASSERT(reporter, index == 0); |
| 966 | REPORTER_ASSERT(reporter, style.getColor() == SK_ColorRED); |
| 967 | ++index; |
| 968 | return true; |
| 969 | }); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 970 | } |
| 971 | } |
| 972 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 973 | // Checked: DIFF+ (Space between 1 & 2 style blocks) |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 974 | DEF_TEST(SkParagraph_RainbowParagraph, reporter) { |
| 975 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 976 | TestCanvas canvas("SkParagraph_RainbowParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 977 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 978 | const char* text1 = "Red Roboto"; // [0:10) |
| 979 | const char* text2 = "big Greeen Default"; // [10:28) |
| 980 | const char* text3 = "Defcolor Homemade Apple"; // [28:51) |
| 981 | const char* text4 = "Small Blue Roboto"; // [51:68) |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 982 | const char* text41 = "Small Blue "; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 983 | const char* text5 = |
| 984 | "Continue Last Style With lots of words to check if it overlaps " |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 985 | "properly or not"; // [68:) |
| 986 | const char* text42 = |
| 987 | "Roboto" |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 988 | "Continue Last Style With lots of words to check if it overlaps " |
| 989 | "properly or not"; |
| 990 | |
| 991 | ParagraphStyle paragraph_style; |
| 992 | paragraph_style.turnHintingOff(); |
| 993 | paragraph_style.setTextAlign(TextAlign::kLeft); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 994 | paragraph_style.setMaxLines(2); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 995 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 996 | |
| 997 | TextStyle text_style1; |
| 998 | text_style1.setFontFamilies({SkString("Roboto")}); |
| 999 | |
| 1000 | text_style1.setColor(SK_ColorRED); |
| 1001 | builder.pushStyle(text_style1); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1002 | builder.addText(text1, strlen(text1)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1003 | |
| 1004 | TextStyle text_style2; |
| 1005 | text_style2.setFontFamilies({SkString("Roboto")}); |
| 1006 | text_style2.setFontSize(50); |
| 1007 | text_style2.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 1008 | SkFontStyle::kUpright_Slant)); |
| 1009 | text_style2.setLetterSpacing(10); |
| 1010 | text_style2.setDecorationColor(SK_ColorBLACK); |
| 1011 | text_style2.setDecoration((TextDecoration)( |
| 1012 | TextDecoration::kUnderline | TextDecoration::kOverline | TextDecoration::kLineThrough)); |
| 1013 | text_style2.setWordSpacing(30); |
| 1014 | text_style2.setColor(SK_ColorGREEN); |
| 1015 | builder.pushStyle(text_style2); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1016 | builder.addText(text2, strlen(text2)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1017 | |
| 1018 | TextStyle text_style3; |
| 1019 | text_style3.setFontFamilies({SkString("Homemade Apple")}); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1020 | text_style3.setColor(SK_ColorBLACK); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1021 | builder.pushStyle(text_style3); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1022 | builder.addText(text3, strlen(text3)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1023 | |
| 1024 | TextStyle text_style4; |
| 1025 | text_style4.setFontFamilies({SkString("Roboto")}); |
| 1026 | text_style4.setFontSize(14); |
| 1027 | text_style4.setDecorationColor(SK_ColorBLACK); |
| 1028 | text_style4.setDecoration((TextDecoration)( |
| 1029 | TextDecoration::kUnderline | TextDecoration::kOverline | TextDecoration::kLineThrough)); |
| 1030 | text_style4.setColor(SK_ColorBLUE); |
| 1031 | builder.pushStyle(text_style4); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1032 | builder.addText(text4, strlen(text4)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1033 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1034 | builder.addText(text5, strlen(text5)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1035 | builder.pop(); |
| 1036 | |
| 1037 | auto paragraph = builder.Build(); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1038 | paragraph->layout(1000); |
| 1039 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1040 | |
| 1041 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 1042 | REPORTER_ASSERT(reporter, impl->runs().size() == 4); |
| 1043 | REPORTER_ASSERT(reporter, impl->styles().size() == 4); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1044 | REPORTER_ASSERT(reporter, impl->lines().size() == 2); |
| 1045 | |
| 1046 | auto rects = paragraph->getRectsForRange(0, impl->text().size(), RectHeightStyle::kMax, RectWidthStyle::kTight); |
| 1047 | canvas.drawRects(SK_ColorMAGENTA, rects); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1048 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1049 | size_t index = 0; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1050 | impl->lines()[0].scanStyles( |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1051 | StyleType::kAllAttributes, |
| 1052 | [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1053 | switch (index) { |
| 1054 | case 0: |
| 1055 | REPORTER_ASSERT(reporter, style.equals(text_style1)); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1056 | REPORTER_ASSERT(reporter, equal(impl->text().begin(), textRange, text1)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1057 | break; |
| 1058 | case 1: |
| 1059 | REPORTER_ASSERT(reporter, style.equals(text_style2)); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1060 | REPORTER_ASSERT(reporter, equal(impl->text().begin(), textRange, text2)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1061 | break; |
| 1062 | case 2: |
| 1063 | REPORTER_ASSERT(reporter, style.equals(text_style3)); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1064 | REPORTER_ASSERT(reporter, equal(impl->text().begin(), textRange, text3)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1065 | break; |
| 1066 | case 3: |
| 1067 | REPORTER_ASSERT(reporter, style.equals(text_style4)); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1068 | REPORTER_ASSERT(reporter, equal(impl->text().begin(), textRange, text41)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1069 | break; |
| 1070 | default: |
| 1071 | REPORTER_ASSERT(reporter, false); |
| 1072 | break; |
| 1073 | } |
| 1074 | ++index; |
| 1075 | return true; |
| 1076 | }); |
| 1077 | impl->lines()[1].scanStyles( |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1078 | StyleType::kAllAttributes, |
| 1079 | [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1080 | switch (index) { |
| 1081 | case 4: |
| 1082 | REPORTER_ASSERT(reporter, style.equals(text_style4)); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1083 | REPORTER_ASSERT(reporter, equal(impl->text().begin(), textRange, text42)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1084 | break; |
| 1085 | default: |
| 1086 | REPORTER_ASSERT(reporter, false); |
| 1087 | break; |
| 1088 | } |
| 1089 | ++index; |
| 1090 | return true; |
| 1091 | }); |
| 1092 | REPORTER_ASSERT(reporter, index == 5); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1093 | } |
| 1094 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1095 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1096 | DEF_TEST(SkParagraph_DefaultStyleParagraph, reporter) { |
| 1097 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 1098 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1099 | TestCanvas canvas("SkParagraph_DefaultStyleParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1100 | const char* text = "No TextStyle! Uh Oh!"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1101 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1102 | |
| 1103 | ParagraphStyle paragraph_style; |
| 1104 | TextStyle defaultStyle; |
| 1105 | defaultStyle.setFontFamilies({SkString("Roboto")}); |
| 1106 | paragraph_style.setTextStyle(defaultStyle); |
| 1107 | paragraph_style.turnHintingOff(); |
| 1108 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1109 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1110 | |
| 1111 | auto paragraph = builder.Build(); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1112 | paragraph->layout(TestCanvasWidth); |
| 1113 | paragraph->paint(canvas.get(), 10.0, 15.0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1114 | |
| 1115 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 1116 | |
| 1117 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 1118 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
| 1119 | REPORTER_ASSERT(reporter, impl->lines().size() == 1); |
| 1120 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1121 | size_t index = 0; |
| 1122 | impl->lines()[0].scanStyles( |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1123 | StyleType::kAllAttributes, |
| 1124 | [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1125 | REPORTER_ASSERT(reporter, style.equals(paragraph_style.getTextStyle())); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1126 | REPORTER_ASSERT(reporter, equal(impl->text().begin(), textRange, text)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1127 | ++index; |
| 1128 | return true; |
| 1129 | }); |
| 1130 | REPORTER_ASSERT(reporter, index == 1); |
| 1131 | } |
| 1132 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1133 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1134 | DEF_TEST(SkParagraph_BoldParagraph, reporter) { |
| 1135 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 1136 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1137 | TestCanvas canvas("SkParagraph_BoldParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1138 | const char* text = "This is Red max bold text!"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1139 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1140 | |
| 1141 | ParagraphStyle paragraph_style; |
| 1142 | paragraph_style.turnHintingOff(); |
| 1143 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1144 | |
| 1145 | TextStyle text_style; |
| 1146 | text_style.setFontFamilies({SkString("Roboto")}); |
| 1147 | text_style.setColor(SK_ColorRED); |
| 1148 | text_style.setFontSize(60); |
| 1149 | text_style.setLetterSpacing(0); |
| 1150 | text_style.setFontStyle(SkFontStyle(SkFontStyle::kBlack_Weight, SkFontStyle::kNormal_Width, |
| 1151 | SkFontStyle::kUpright_Slant)); |
| 1152 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1153 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1154 | builder.pop(); |
| 1155 | |
| 1156 | auto paragraph = builder.Build(); |
| 1157 | paragraph->layout(VeryLongCanvasWidth); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1158 | paragraph->paint(canvas.get(), 10.0, 60.0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1159 | |
| 1160 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 1161 | |
| 1162 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 1163 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
| 1164 | REPORTER_ASSERT(reporter, impl->lines().size() == 1); |
| 1165 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1166 | size_t index = 0; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1167 | impl->lines()[0].scanStyles( |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1168 | StyleType::kAllAttributes, |
| 1169 | [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1170 | REPORTER_ASSERT(reporter, style.equals(text_style)); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1171 | REPORTER_ASSERT(reporter, equal(impl->text().begin(), textRange, text)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1172 | ++index; |
| 1173 | return true; |
| 1174 | }); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1175 | REPORTER_ASSERT(reporter, index == 1); |
| 1176 | } |
| 1177 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1178 | // Checked: NO DIFF (line height rounding error) |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1179 | DEF_TEST(SkParagraph_HeightOverrideParagraph, reporter) { |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1180 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 1181 | if (!fontCollection->fontsFound()) return; |
| 1182 | TestCanvas canvas("SkParagraph_HeightOverrideParagraph.png"); |
| 1183 | const char* text = "01234満毎冠行来昼本可\nabcd\n満毎冠行来昼本可"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1184 | const size_t len = strlen(text); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1185 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1186 | ParagraphStyle paragraph_style; |
| 1187 | paragraph_style.turnHintingOff(); |
| 1188 | paragraph_style.setMaxLines(10); |
| 1189 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1190 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1191 | TextStyle text_style; |
| 1192 | text_style.setFontFamilies({SkString("Roboto")}); |
| 1193 | text_style.setFontSize(20); |
| 1194 | text_style.setColor(SK_ColorBLACK); |
| 1195 | text_style.setHeight(3.6345f); |
| 1196 | text_style.setHeightOverride(true); |
| 1197 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1198 | builder.addText(text, len); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1199 | builder.pop(); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1200 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1201 | auto paragraph = builder.Build(); |
| 1202 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1203 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1204 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 1205 | REPORTER_ASSERT(reporter, impl->runs().size() == 3); |
| 1206 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); // paragraph style does not count |
| 1207 | REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1208 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1209 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1210 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1211 | SkPaint paint; |
| 1212 | paint.setStyle(SkPaint::kStroke_Style); |
| 1213 | paint.setAntiAlias(true); |
| 1214 | paint.setStrokeWidth(1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1215 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1216 | // Tests for GetRectsForRange() |
| 1217 | RectHeightStyle rect_height_style = RectHeightStyle::kIncludeLineSpacingMiddle; |
| 1218 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 1219 | paint.setColor(SK_ColorRED); |
| 1220 | std::vector<TextBox> boxes = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style); |
| 1221 | canvas.drawRects(SK_ColorRED, boxes); |
| 1222 | REPORTER_ASSERT(reporter, boxes.size() == 0ull); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1223 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1224 | boxes = paragraph->getRectsForRange(0, 40, rect_height_style, rect_width_style); |
| 1225 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 1226 | REPORTER_ASSERT(reporter, boxes.size() == 3ull); |
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 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.left(), 0, EPSILON100)); |
| 1229 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.top(), 92.805f, EPSILON5)); |
| 1230 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.right(), 43.843f, EPSILON100)); |
| 1231 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.bottom(), 165.495f, EPSILON5)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1232 | } |
| 1233 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1234 | // Checked: DIFF+ |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1235 | DEF_TEST(SkParagraph_LeftAlignParagraph, reporter) { |
| 1236 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 1237 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1238 | TestCanvas canvas("SkParagraph_LeftAlignParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1239 | const char* text = |
| 1240 | "This is a very long sentence to test if the text will properly wrap " |
| 1241 | "around and go to the next line. Sometimes, short sentence. Longer " |
| 1242 | "sentences are okay too because they are nessecary. Very short. " |
| 1243 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod " |
| 1244 | "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim " |
| 1245 | "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea " |
| 1246 | "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate " |
| 1247 | "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint " |
| 1248 | "occaecat cupidatat non proident, sunt in culpa qui officia deserunt " |
| 1249 | "mollit anim id est laborum. " |
| 1250 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod " |
| 1251 | "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim " |
| 1252 | "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea " |
| 1253 | "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate " |
| 1254 | "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint " |
| 1255 | "occaecat cupidatat non proident, sunt in culpa qui officia deserunt " |
| 1256 | "mollit anim id est laborum."; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1257 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1258 | |
| 1259 | ParagraphStyle paragraph_style; |
| 1260 | paragraph_style.setMaxLines(14); |
| 1261 | paragraph_style.setTextAlign(TextAlign::kLeft); |
| 1262 | paragraph_style.turnHintingOff(); |
| 1263 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1264 | |
| 1265 | TextStyle text_style; |
| 1266 | text_style.setFontFamilies({SkString("Roboto")}); |
| 1267 | text_style.setFontSize(26); |
| 1268 | text_style.setLetterSpacing(1); |
| 1269 | text_style.setWordSpacing(5); |
| 1270 | text_style.setColor(SK_ColorBLACK); |
| 1271 | text_style.setHeight(1); |
| 1272 | text_style.setDecoration(TextDecoration::kUnderline); |
| 1273 | text_style.setDecorationColor(SK_ColorBLACK); |
| 1274 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1275 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1276 | builder.pop(); |
| 1277 | |
| 1278 | auto paragraph = builder.Build(); |
| 1279 | paragraph->layout(TestCanvasWidth - 100); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1280 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1281 | |
| 1282 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1283 | |
| 1284 | REPORTER_ASSERT(reporter, impl->text().size() == std::string{text}.length()); |
| 1285 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 1286 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 1287 | REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1288 | REPORTER_ASSERT(reporter, impl->lines().size() == paragraph_style.getMaxLines()); |
| 1289 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1290 | double expected_y = 0; |
| 1291 | double epsilon = 0.01f; |
| 1292 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].baseline(), 24.121f, epsilon)); |
| 1293 | REPORTER_ASSERT(reporter, |
| 1294 | SkScalarNearlyEqual(impl->lines()[0].offset().fY, expected_y, epsilon)); |
| 1295 | expected_y += 30; |
| 1296 | REPORTER_ASSERT(reporter, |
| 1297 | SkScalarNearlyEqual(impl->lines()[1].offset().fY, expected_y, epsilon)); |
| 1298 | expected_y += 30; |
| 1299 | REPORTER_ASSERT(reporter, |
| 1300 | SkScalarNearlyEqual(impl->lines()[2].offset().fY, expected_y, epsilon)); |
| 1301 | expected_y += 30; |
| 1302 | REPORTER_ASSERT(reporter, |
| 1303 | SkScalarNearlyEqual(impl->lines()[3].offset().fY, expected_y, epsilon)); |
| 1304 | expected_y += 30 * 10; |
| 1305 | REPORTER_ASSERT(reporter, |
| 1306 | SkScalarNearlyEqual(impl->lines()[13].offset().fY, expected_y, epsilon)); |
| 1307 | |
| 1308 | REPORTER_ASSERT(reporter, |
| 1309 | paragraph_style.getTextAlign() == impl->paragraphStyle().getTextAlign()); |
| 1310 | |
| 1311 | // Tests for GetGlyphPositionAtCoordinate() |
| 1312 | REPORTER_ASSERT(reporter, impl->getGlyphPositionAtCoordinate(0, 0).position == 0); |
| 1313 | REPORTER_ASSERT(reporter, impl->getGlyphPositionAtCoordinate(1, 1).position == 0); |
| 1314 | REPORTER_ASSERT(reporter, impl->getGlyphPositionAtCoordinate(1, 35).position == 68); |
| 1315 | REPORTER_ASSERT(reporter, impl->getGlyphPositionAtCoordinate(1, 70).position == 134); |
| 1316 | REPORTER_ASSERT(reporter, impl->getGlyphPositionAtCoordinate(2000, 35).position == 134); |
| 1317 | } |
| 1318 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1319 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1320 | DEF_TEST(SkParagraph_RightAlignParagraph, reporter) { |
| 1321 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 1322 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1323 | TestCanvas canvas("SkParagraph_RightAlignParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1324 | const char* text = |
| 1325 | "This is a very long sentence to test if the text will properly wrap " |
| 1326 | "around and go to the next line. Sometimes, short sentence. Longer " |
| 1327 | "sentences are okay too because they are nessecary. Very short. " |
| 1328 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod " |
| 1329 | "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim " |
| 1330 | "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea " |
| 1331 | "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate " |
| 1332 | "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint " |
| 1333 | "occaecat cupidatat non proident, sunt in culpa qui officia deserunt " |
| 1334 | "mollit anim id est laborum. " |
| 1335 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod " |
| 1336 | "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim " |
| 1337 | "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea " |
| 1338 | "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate " |
| 1339 | "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint " |
| 1340 | "occaecat cupidatat non proident, sunt in culpa qui officia deserunt " |
| 1341 | "mollit anim id est laborum."; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1342 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1343 | |
| 1344 | ParagraphStyle paragraph_style; |
| 1345 | paragraph_style.setMaxLines(14); |
| 1346 | paragraph_style.setTextAlign(TextAlign::kRight); |
| 1347 | paragraph_style.turnHintingOff(); |
| 1348 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1349 | |
| 1350 | TextStyle text_style; |
| 1351 | text_style.setFontFamilies({SkString("Roboto")}); |
| 1352 | text_style.setFontSize(26); |
| 1353 | text_style.setLetterSpacing(1); |
| 1354 | text_style.setWordSpacing(5); |
| 1355 | text_style.setColor(SK_ColorBLACK); |
| 1356 | text_style.setHeight(1); |
| 1357 | text_style.setDecoration(TextDecoration::kUnderline); |
| 1358 | text_style.setDecorationColor(SK_ColorBLACK); |
| 1359 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1360 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1361 | builder.pop(); |
| 1362 | |
| 1363 | auto paragraph = builder.Build(); |
| 1364 | paragraph->layout(TestCanvasWidth - 100); |
| 1365 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1366 | paragraph->paint(canvas.get(), 0, 0); |
| 1367 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1368 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1369 | |
| 1370 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 1371 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 1372 | REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1373 | REPORTER_ASSERT(reporter, impl->lines().size() == paragraph_style.getMaxLines()); |
| 1374 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1375 | double expected_y = 0; |
| 1376 | double epsilon = 0.01f; |
| 1377 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].baseline(), 24.121f, epsilon)); |
| 1378 | REPORTER_ASSERT(reporter, |
| 1379 | SkScalarNearlyEqual(impl->lines()[0].offset().fY, expected_y, epsilon)); |
| 1380 | expected_y += 30; |
| 1381 | REPORTER_ASSERT(reporter, |
| 1382 | SkScalarNearlyEqual(impl->lines()[1].offset().fY, expected_y, epsilon)); |
| 1383 | expected_y += 30; |
| 1384 | REPORTER_ASSERT(reporter, |
| 1385 | SkScalarNearlyEqual(impl->lines()[2].offset().fY, expected_y, epsilon)); |
| 1386 | expected_y += 30; |
| 1387 | REPORTER_ASSERT(reporter, |
| 1388 | SkScalarNearlyEqual(impl->lines()[3].offset().fY, expected_y, epsilon)); |
| 1389 | expected_y += 30 * 10; |
| 1390 | REPORTER_ASSERT(reporter, |
| 1391 | SkScalarNearlyEqual(impl->lines()[13].offset().fY, expected_y, epsilon)); |
| 1392 | |
| 1393 | auto calculate = [](const TextLine& line) -> SkScalar { |
| 1394 | return TestCanvasWidth - 100 - line.offset().fX - line.width(); |
| 1395 | }; |
| 1396 | |
| 1397 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[0]), 0, epsilon)); |
| 1398 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[1]), 0, epsilon)); |
| 1399 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[2]), 0, epsilon)); |
| 1400 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[3]), 0, epsilon)); |
| 1401 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[13]), 0, epsilon)); |
| 1402 | |
| 1403 | REPORTER_ASSERT(reporter, |
| 1404 | paragraph_style.getTextAlign() == impl->paragraphStyle().getTextAlign()); |
| 1405 | } |
| 1406 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1407 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1408 | DEF_TEST(SkParagraph_CenterAlignParagraph, reporter) { |
| 1409 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 1410 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1411 | TestCanvas canvas("SkParagraph_CenterAlignParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1412 | const char* text = |
| 1413 | "This is a very long sentence to test if the text will properly wrap " |
| 1414 | "around and go to the next line. Sometimes, short sentence. Longer " |
| 1415 | "sentences are okay too because they are nessecary. Very short. " |
| 1416 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod " |
| 1417 | "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim " |
| 1418 | "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea " |
| 1419 | "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate " |
| 1420 | "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint " |
| 1421 | "occaecat cupidatat non proident, sunt in culpa qui officia deserunt " |
| 1422 | "mollit anim id est laborum. " |
| 1423 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod " |
| 1424 | "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim " |
| 1425 | "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea " |
| 1426 | "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate " |
| 1427 | "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint " |
| 1428 | "occaecat cupidatat non proident, sunt in culpa qui officia deserunt " |
| 1429 | "mollit anim id est laborum."; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1430 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1431 | |
| 1432 | ParagraphStyle paragraph_style; |
| 1433 | paragraph_style.setMaxLines(14); |
| 1434 | paragraph_style.setTextAlign(TextAlign::kCenter); |
| 1435 | paragraph_style.turnHintingOff(); |
| 1436 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1437 | |
| 1438 | TextStyle text_style; |
| 1439 | text_style.setFontFamilies({SkString("Roboto")}); |
| 1440 | text_style.setFontSize(26); |
| 1441 | text_style.setLetterSpacing(1); |
| 1442 | text_style.setWordSpacing(5); |
| 1443 | text_style.setColor(SK_ColorBLACK); |
| 1444 | text_style.setHeight(1); |
| 1445 | text_style.setDecoration(TextDecoration::kUnderline); |
| 1446 | text_style.setDecorationColor(SK_ColorBLACK); |
| 1447 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1448 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1449 | builder.pop(); |
| 1450 | |
| 1451 | auto paragraph = builder.Build(); |
| 1452 | paragraph->layout(TestCanvasWidth - 100); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1453 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1454 | |
| 1455 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1456 | |
| 1457 | REPORTER_ASSERT(reporter, impl->text().size() == std::string{text}.length()); |
| 1458 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 1459 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 1460 | REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1461 | REPORTER_ASSERT(reporter, impl->lines().size() == paragraph_style.getMaxLines()); |
| 1462 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1463 | double expected_y = 0; |
| 1464 | double epsilon = 0.01f; |
| 1465 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].baseline(), 24.121f, epsilon)); |
| 1466 | REPORTER_ASSERT(reporter, |
| 1467 | SkScalarNearlyEqual(impl->lines()[0].offset().fY, expected_y, epsilon)); |
| 1468 | expected_y += 30; |
| 1469 | REPORTER_ASSERT(reporter, |
| 1470 | SkScalarNearlyEqual(impl->lines()[1].offset().fY, expected_y, epsilon)); |
| 1471 | expected_y += 30; |
| 1472 | REPORTER_ASSERT(reporter, |
| 1473 | SkScalarNearlyEqual(impl->lines()[2].offset().fY, expected_y, epsilon)); |
| 1474 | expected_y += 30; |
| 1475 | REPORTER_ASSERT(reporter, |
| 1476 | SkScalarNearlyEqual(impl->lines()[3].offset().fY, expected_y, epsilon)); |
| 1477 | expected_y += 30 * 10; |
| 1478 | REPORTER_ASSERT(reporter, |
| 1479 | SkScalarNearlyEqual(impl->lines()[13].offset().fY, expected_y, epsilon)); |
| 1480 | |
| 1481 | auto calculate = [](const TextLine& line) -> SkScalar { |
| 1482 | return TestCanvasWidth - 100 - (line.offset().fX * 2 + line.width()); |
| 1483 | }; |
| 1484 | |
| 1485 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[0]), 0, epsilon)); |
| 1486 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[1]), 0, epsilon)); |
| 1487 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[2]), 0, epsilon)); |
| 1488 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[3]), 0, epsilon)); |
| 1489 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[13]), 0, epsilon)); |
| 1490 | |
| 1491 | REPORTER_ASSERT(reporter, |
| 1492 | paragraph_style.getTextAlign() == impl->paragraphStyle().getTextAlign()); |
| 1493 | } |
| 1494 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1495 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1496 | DEF_TEST(SkParagraph_JustifyAlignParagraph, reporter) { |
| 1497 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 1498 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1499 | TestCanvas canvas("SkParagraph_JustifyAlignParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1500 | const char* text = |
| 1501 | "This is a very long sentence to test if the text will properly wrap " |
| 1502 | "around and go to the next line. Sometimes, short sentence. Longer " |
| 1503 | "sentences are okay too because they are nessecary. Very short. " |
| 1504 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod " |
| 1505 | "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim " |
| 1506 | "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea " |
| 1507 | "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate " |
| 1508 | "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint " |
| 1509 | "occaecat cupidatat non proident, sunt in culpa qui officia deserunt " |
| 1510 | "mollit anim id est laborum. " |
| 1511 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod " |
| 1512 | "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim " |
| 1513 | "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea " |
| 1514 | "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate " |
| 1515 | "velit esse cillum dolore eu fugiat."; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1516 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1517 | |
| 1518 | ParagraphStyle paragraph_style; |
| 1519 | paragraph_style.setMaxLines(14); |
| 1520 | paragraph_style.setTextAlign(TextAlign::kJustify); |
| 1521 | paragraph_style.turnHintingOff(); |
| 1522 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1523 | |
| 1524 | TextStyle text_style; |
| 1525 | text_style.setFontFamilies({SkString("Roboto")}); |
| 1526 | text_style.setFontSize(26); |
| 1527 | text_style.setLetterSpacing(0); |
| 1528 | text_style.setWordSpacing(5); |
| 1529 | text_style.setColor(SK_ColorBLACK); |
| 1530 | text_style.setHeight(1); |
| 1531 | text_style.setDecoration(TextDecoration::kUnderline); |
| 1532 | text_style.setDecorationColor(SK_ColorBLACK); |
| 1533 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1534 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1535 | builder.pop(); |
| 1536 | |
| 1537 | auto paragraph = builder.Build(); |
| 1538 | paragraph->layout(TestCanvasWidth - 100); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1539 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1540 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1541 | RectHeightStyle rect_height_style = RectHeightStyle::kMax; |
| 1542 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 1543 | auto boxes = paragraph->getRectsForRange(0, 100, rect_height_style, rect_width_style); |
| 1544 | canvas.drawRects(SK_ColorRED, boxes); |
| 1545 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1546 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1547 | |
| 1548 | REPORTER_ASSERT(reporter, impl->text().size() == std::string{text}.length()); |
| 1549 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 1550 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 1551 | REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1552 | |
| 1553 | double expected_y = 0; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1554 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].baseline(), 24.121f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1555 | REPORTER_ASSERT(reporter, |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1556 | SkScalarNearlyEqual(impl->lines()[0].offset().fY, expected_y, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1557 | expected_y += 30; |
| 1558 | REPORTER_ASSERT(reporter, |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1559 | SkScalarNearlyEqual(impl->lines()[1].offset().fY, expected_y, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1560 | expected_y += 30; |
| 1561 | REPORTER_ASSERT(reporter, |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1562 | SkScalarNearlyEqual(impl->lines()[2].offset().fY, expected_y, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1563 | expected_y += 30; |
| 1564 | REPORTER_ASSERT(reporter, |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1565 | SkScalarNearlyEqual(impl->lines()[3].offset().fY, expected_y, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1566 | expected_y += 30 * 9; |
| 1567 | REPORTER_ASSERT(reporter, |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1568 | SkScalarNearlyEqual(impl->lines()[12].offset().fY, expected_y, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1569 | |
| 1570 | auto calculate = [](const TextLine& line) -> SkScalar { |
| 1571 | return TestCanvasWidth - 100 - (line.offset().fX + line.width()); |
| 1572 | }; |
| 1573 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1574 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[0]), 0, EPSILON100)); |
| 1575 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[1]), 0, EPSILON100)); |
| 1576 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[2]), 0, EPSILON100)); |
| 1577 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[3]), 0, EPSILON100)); |
| 1578 | REPORTER_ASSERT(reporter, calculate(impl->lines()[13]) > 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1579 | |
| 1580 | REPORTER_ASSERT(reporter, |
| 1581 | paragraph_style.getTextAlign() == impl->paragraphStyle().getTextAlign()); |
| 1582 | } |
| 1583 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1584 | // Checked: DIFF (ghost spaces as a separate box in TxtLib) |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1585 | DEF_TEST(SkParagraph_JustifyRTL, reporter) { |
| 1586 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 1587 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1588 | TestCanvas canvas("SkParagraph_JustifyRTL.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1589 | const char* text = |
| 1590 | "אאא בּבּבּבּ אאאא בּבּ אאא בּבּבּ אאאאא בּבּבּבּ אאאא בּבּבּבּבּ " |
| 1591 | "אאאאא בּבּבּבּבּ אאאבּבּבּבּבּבּאאאאא בּבּבּבּבּבּאאאאאבּבּבּבּבּבּ אאאאא בּבּבּבּבּ " |
| 1592 | "אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1593 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1594 | |
| 1595 | ParagraphStyle paragraph_style; |
| 1596 | paragraph_style.setMaxLines(14); |
| 1597 | paragraph_style.setTextAlign(TextAlign::kJustify); |
| 1598 | paragraph_style.turnHintingOff(); |
| 1599 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1600 | |
| 1601 | TextStyle text_style; |
| 1602 | text_style.setFontFamilies({SkString("Ahem")}); |
| 1603 | text_style.setFontSize(26); |
| 1604 | text_style.setColor(SK_ColorBLACK); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1605 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1606 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1607 | builder.pop(); |
| 1608 | |
| 1609 | auto paragraph = builder.Build(); |
| 1610 | paragraph->layout(TestCanvasWidth - 100); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1611 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1612 | |
| 1613 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1614 | |
| 1615 | auto calculate = [](const TextLine& line) -> SkScalar { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1616 | return TestCanvasWidth - 100 - line.width(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1617 | }; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1618 | for (auto& line : impl->lines()) { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1619 | if (&line == &impl->lines().back()) { |
| 1620 | REPORTER_ASSERT(reporter, calculate(line) > EPSILON100); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1621 | } else { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1622 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(line), 0, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1623 | } |
| 1624 | } |
| 1625 | |
| 1626 | // Just make sure the the text is actually RTL |
| 1627 | for (auto& run : impl->runs()) { |
| 1628 | REPORTER_ASSERT(reporter, !run.leftToRight()); |
| 1629 | } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1630 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1631 | // Tests for GetRectsForRange() |
| 1632 | RectHeightStyle rect_height_style = RectHeightStyle::kMax; |
| 1633 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 1634 | auto boxes = paragraph->getRectsForRange(0, 100, rect_height_style, rect_width_style); |
| 1635 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1636 | REPORTER_ASSERT(reporter, boxes.size() == 3); // DIFF |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1637 | |
| 1638 | boxes = paragraph->getRectsForRange(240, 250, rect_height_style, rect_width_style); |
| 1639 | canvas.drawRects(SK_ColorBLUE, boxes); |
| 1640 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 1641 | |
| 1642 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 588, EPSILON100)); |
| 1643 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 130, EPSILON100)); |
| 1644 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 640, EPSILON100)); |
| 1645 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 156, EPSILON100)); |
| 1646 | } |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1647 | |
| 1648 | // Checked: NO DIFF (some minor decoration differences, probably) |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1649 | DEF_TEST(SkParagraph_DecorationsParagraph, reporter) { |
| 1650 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 1651 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1652 | TestCanvas canvas("SkParagraph_DecorationsParagraph.png"); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1653 | const char* text1 = "This text should be"; |
| 1654 | const char* text2 = " decorated even when"; |
| 1655 | const char* text3 = " wrapped around to"; |
| 1656 | const char* text4 = " the next line."; |
| 1657 | const char* text5 = " Otherwise, bad things happen."; |
| 1658 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1659 | ParagraphStyle paragraph_style; |
| 1660 | paragraph_style.setMaxLines(14); |
| 1661 | paragraph_style.setTextAlign(TextAlign::kLeft); |
| 1662 | paragraph_style.turnHintingOff(); |
| 1663 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1664 | |
| 1665 | TextStyle text_style; |
| 1666 | text_style.setFontFamilies({SkString("Roboto")}); |
| 1667 | text_style.setFontSize(26); |
| 1668 | text_style.setLetterSpacing(0); |
| 1669 | text_style.setWordSpacing(5); |
| 1670 | text_style.setColor(SK_ColorBLACK); |
| 1671 | text_style.setHeight(2); |
| 1672 | text_style.setDecoration(TextDecoration::kUnderline); |
| 1673 | text_style.setDecorationColor(SK_ColorBLACK); |
| 1674 | text_style.setDecoration((TextDecoration)( |
| 1675 | TextDecoration::kUnderline | TextDecoration::kOverline | TextDecoration::kLineThrough)); |
| 1676 | text_style.setDecorationStyle(TextDecorationStyle::kSolid); |
| 1677 | text_style.setDecorationColor(SK_ColorBLACK); |
| 1678 | text_style.setDecorationThicknessMultiplier(2.0); |
| 1679 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1680 | builder.addText(text1, strlen(text1)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1681 | |
| 1682 | text_style.setDecorationStyle(TextDecorationStyle::kDouble); |
| 1683 | text_style.setDecorationColor(SK_ColorBLUE); |
| 1684 | text_style.setDecorationThicknessMultiplier(1.0); |
| 1685 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1686 | builder.addText(text2, strlen(text2)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1687 | |
| 1688 | text_style.setDecorationStyle(TextDecorationStyle::kDotted); |
| 1689 | text_style.setDecorationColor(SK_ColorBLACK); |
| 1690 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1691 | builder.addText(text3, strlen(text3)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1692 | |
| 1693 | text_style.setDecorationStyle(TextDecorationStyle::kDashed); |
| 1694 | text_style.setDecorationColor(SK_ColorBLACK); |
| 1695 | text_style.setDecorationThicknessMultiplier(3.0); |
| 1696 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1697 | builder.addText(text4, strlen(text4)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1698 | |
| 1699 | text_style.setDecorationStyle(TextDecorationStyle::kWavy); |
| 1700 | text_style.setDecorationColor(SK_ColorRED); |
| 1701 | text_style.setDecorationThicknessMultiplier(1.0); |
| 1702 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1703 | builder.addText(text5, strlen(text5)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1704 | builder.pop(); |
| 1705 | |
| 1706 | auto paragraph = builder.Build(); |
| 1707 | paragraph->layout(TestCanvasWidth - 100); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1708 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1709 | |
| 1710 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1711 | |
| 1712 | size_t index = 0; |
| 1713 | for (auto& line : impl->lines()) { |
| 1714 | line.scanStyles( |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1715 | StyleType::kDecorations, |
| 1716 | [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1717 | auto decoration = (TextDecoration)(TextDecoration::kUnderline | |
| 1718 | TextDecoration::kOverline | |
| 1719 | TextDecoration::kLineThrough); |
| 1720 | REPORTER_ASSERT(reporter, style.getDecorationType() == decoration); |
| 1721 | switch (index) { |
| 1722 | case 0: |
| 1723 | REPORTER_ASSERT(reporter, style.getDecorationStyle() == |
| 1724 | TextDecorationStyle::kSolid); |
| 1725 | REPORTER_ASSERT(reporter, style.getDecorationColor() == SK_ColorBLACK); |
| 1726 | REPORTER_ASSERT(reporter, |
| 1727 | style.getDecorationThicknessMultiplier() == 2.0); |
| 1728 | break; |
| 1729 | case 1: // The style appears on 2 lines so it has 2 pieces |
| 1730 | REPORTER_ASSERT(reporter, style.getDecorationStyle() == |
| 1731 | TextDecorationStyle::kDouble); |
| 1732 | REPORTER_ASSERT(reporter, style.getDecorationColor() == SK_ColorBLUE); |
| 1733 | REPORTER_ASSERT(reporter, |
| 1734 | style.getDecorationThicknessMultiplier() == 1.0); |
| 1735 | break; |
| 1736 | case 2: |
| 1737 | REPORTER_ASSERT(reporter, style.getDecorationStyle() == |
| 1738 | TextDecorationStyle::kDotted); |
| 1739 | REPORTER_ASSERT(reporter, style.getDecorationColor() == SK_ColorBLACK); |
| 1740 | REPORTER_ASSERT(reporter, |
| 1741 | style.getDecorationThicknessMultiplier() == 1.0); |
| 1742 | break; |
| 1743 | case 3: |
| 1744 | case 4: |
| 1745 | REPORTER_ASSERT(reporter, style.getDecorationStyle() == |
| 1746 | TextDecorationStyle::kDashed); |
| 1747 | REPORTER_ASSERT(reporter, style.getDecorationColor() == SK_ColorBLACK); |
| 1748 | REPORTER_ASSERT(reporter, |
| 1749 | style.getDecorationThicknessMultiplier() == 3.0); |
| 1750 | break; |
| 1751 | case 5: |
| 1752 | REPORTER_ASSERT(reporter, style.getDecorationStyle() == |
| 1753 | TextDecorationStyle::kWavy); |
| 1754 | REPORTER_ASSERT(reporter, style.getDecorationColor() == SK_ColorRED); |
| 1755 | REPORTER_ASSERT(reporter, |
| 1756 | style.getDecorationThicknessMultiplier() == 1.0); |
| 1757 | break; |
| 1758 | default: |
| 1759 | REPORTER_ASSERT(reporter, false); |
| 1760 | break; |
| 1761 | } |
| 1762 | ++index; |
| 1763 | return true; |
| 1764 | }); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1765 | } |
| 1766 | } |
| 1767 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1768 | DEF_TEST(SkParagraph_WavyDecorationParagraph, reporter) { |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1769 | SkDebugf("TODO: Fix decorations\n"); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1770 | } |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1771 | |
| 1772 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1773 | DEF_TEST(SkParagraph_ItalicsParagraph, reporter) { |
| 1774 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 1775 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1776 | TestCanvas canvas("SkParagraph_ItalicsParagraph.png"); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1777 | const char* text1 = "No italic "; |
| 1778 | const char* text2 = "Yes Italic "; |
| 1779 | const char* text3 = "No Italic again."; |
| 1780 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1781 | ParagraphStyle paragraph_style; |
| 1782 | paragraph_style.turnHintingOff(); |
| 1783 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1784 | |
| 1785 | TextStyle text_style; |
| 1786 | text_style.setFontFamilies({SkString("Roboto")}); |
| 1787 | text_style.setFontSize(10); |
| 1788 | text_style.setColor(SK_ColorRED); |
| 1789 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1790 | builder.addText(text1, strlen(text1)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1791 | |
| 1792 | text_style.setFontStyle(SkFontStyle(SkFontStyle::kNormal_Weight, SkFontStyle::kNormal_Width, |
| 1793 | SkFontStyle::kItalic_Slant)); |
| 1794 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1795 | builder.addText(text2, strlen(text2)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1796 | builder.pop(); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1797 | builder.addText(text3, strlen(text3)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1798 | |
| 1799 | auto paragraph = builder.Build(); |
| 1800 | paragraph->layout(TestCanvasWidth); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1801 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1802 | |
| 1803 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1804 | |
| 1805 | REPORTER_ASSERT(reporter, impl->runs().size() == 3); |
| 1806 | REPORTER_ASSERT(reporter, impl->styles().size() == 3); |
| 1807 | REPORTER_ASSERT(reporter, impl->lines().size() == 1); |
| 1808 | auto& line = impl->lines()[0]; |
| 1809 | size_t index = 0; |
| 1810 | line.scanStyles( |
| 1811 | StyleType::kForeground, |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 1812 | [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1813 | switch (index) { |
| 1814 | case 0: |
| 1815 | REPORTER_ASSERT( |
| 1816 | reporter, |
| 1817 | style.getFontStyle().slant() == SkFontStyle::kUpright_Slant); |
| 1818 | break; |
| 1819 | case 1: |
| 1820 | REPORTER_ASSERT(reporter, |
| 1821 | style.getFontStyle().slant() == SkFontStyle::kItalic_Slant); |
| 1822 | break; |
| 1823 | case 2: |
| 1824 | REPORTER_ASSERT( |
| 1825 | reporter, |
| 1826 | style.getFontStyle().slant() == SkFontStyle::kUpright_Slant); |
| 1827 | break; |
| 1828 | default: |
| 1829 | REPORTER_ASSERT(reporter, false); |
| 1830 | break; |
| 1831 | } |
| 1832 | ++index; |
| 1833 | return true; |
| 1834 | }); |
| 1835 | } |
| 1836 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1837 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1838 | DEF_TEST(SkParagraph_ChineseParagraph, reporter) { |
| 1839 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 1840 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1841 | TestCanvas canvas("SkParagraph_ChineseParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1842 | const char* text = |
| 1843 | "左線読設重説切後碁給能上目秘使約。満毎冠行来昼本可必図将発確年。今属場育" |
| 1844 | "図情闘陰野高備込制詩西校客。審対江置講今固残必託地集済決維駆年策。立得庭" |
| 1845 | "際輝求佐抗蒼提夜合逃表。注統天言件自謙雅載報紙喪。作画稿愛器灯女書利変探" |
| 1846 | "訃第金線朝開化建。子戦年帝励害表月幕株漠新期刊人秘。図的海力生禁挙保天戦" |
| 1847 | "聞条年所在口。"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1848 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1849 | |
| 1850 | ParagraphStyle paragraph_style; |
| 1851 | paragraph_style.setMaxLines(14); |
| 1852 | paragraph_style.setTextAlign(TextAlign::kJustify); |
| 1853 | paragraph_style.turnHintingOff(); |
| 1854 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1855 | |
| 1856 | auto decoration = (TextDecoration)(TextDecoration::kUnderline | TextDecoration::kOverline | |
| 1857 | TextDecoration::kLineThrough); |
| 1858 | |
| 1859 | TextStyle text_style; |
| 1860 | text_style.setFontFamilies({SkString("Source Han Serif CN")}); |
| 1861 | text_style.setFontSize(35); |
| 1862 | text_style.setColor(SK_ColorBLACK); |
| 1863 | text_style.setLetterSpacing(2); |
| 1864 | text_style.setHeight(1); |
| 1865 | text_style.setDecoration(decoration); |
| 1866 | text_style.setDecorationColor(SK_ColorBLACK); |
| 1867 | text_style.setDecorationStyle(TextDecorationStyle::kSolid); |
| 1868 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1869 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1870 | builder.pop(); |
| 1871 | |
| 1872 | auto paragraph = builder.Build(); |
| 1873 | paragraph->layout(TestCanvasWidth - 100); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1874 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1875 | |
| 1876 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1877 | |
| 1878 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 1879 | REPORTER_ASSERT(reporter, impl->lines().size() == 7); |
| 1880 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 1881 | REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1882 | } |
| 1883 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1884 | // Checked: NO DIFF (disabled) |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1885 | DEF_TEST(SkParagraph_ArabicParagraph, reporter) { |
| 1886 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 1887 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1888 | TestCanvas canvas("SkParagraph_ArabicParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1889 | const char* text = |
| 1890 | "من أسر وإعلان الخاصّة وهولندا،, عل قائمة الضغوط بالمطالبة تلك. الصفحة " |
| 1891 | "بمباركة التقليدية قام عن. تصفح"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1892 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1893 | |
| 1894 | ParagraphStyle paragraph_style; |
| 1895 | paragraph_style.setMaxLines(14); |
| 1896 | paragraph_style.setTextAlign(TextAlign::kJustify); |
| 1897 | paragraph_style.turnHintingOff(); |
| 1898 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1899 | |
| 1900 | auto decoration = (TextDecoration)(TextDecoration::kUnderline | TextDecoration::kOverline | |
| 1901 | TextDecoration::kLineThrough); |
| 1902 | |
| 1903 | TextStyle text_style; |
| 1904 | text_style.setFontFamilies({SkString("Katibeh")}); |
| 1905 | text_style.setFontSize(35); |
| 1906 | text_style.setColor(SK_ColorBLACK); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1907 | text_style.setLetterSpacing(2); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1908 | text_style.setDecoration(decoration); |
| 1909 | text_style.setDecorationColor(SK_ColorBLACK); |
| 1910 | text_style.setDecorationStyle(TextDecorationStyle::kSolid); |
| 1911 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1912 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1913 | builder.pop(); |
| 1914 | |
| 1915 | auto paragraph = builder.Build(); |
| 1916 | paragraph->layout(TestCanvasWidth - 100); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1917 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1918 | |
| 1919 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1920 | |
| 1921 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 1922 | REPORTER_ASSERT(reporter, impl->lines().size() == 2); |
| 1923 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 1924 | REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1925 | } |
| 1926 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1927 | // Checked: DIFF (2 boxes and each space is a word) |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1928 | DEF_TEST(SkParagraph_ArabicRectsParagraph, reporter) { |
| 1929 | |
| 1930 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 1931 | if (!fontCollection->fontsFound()) return; |
| 1932 | TestCanvas canvas("SkParagraph_ArabicRectsParagraph.png"); |
| 1933 | const char* text = "بمباركة التقليدية قام عن. تصفح يد "; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1934 | const size_t len = strlen(text); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1935 | |
| 1936 | ParagraphStyle paragraph_style; |
| 1937 | paragraph_style.turnHintingOff(); |
| 1938 | paragraph_style.setMaxLines(14); |
| 1939 | paragraph_style.setTextAlign(TextAlign::kRight); |
| 1940 | paragraph_style.setTextDirection(TextDirection::kRtl); |
| 1941 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1942 | |
| 1943 | TextStyle text_style; |
| 1944 | text_style.setFontFamilies({SkString("Noto Naskh Arabic")}); |
| 1945 | text_style.setFontSize(26); |
| 1946 | text_style.setWordSpacing(5); |
| 1947 | text_style.setColor(SK_ColorBLACK); |
| 1948 | text_style.setDecoration(TextDecoration::kUnderline); |
| 1949 | text_style.setDecorationColor(SK_ColorBLACK); |
| 1950 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1951 | builder.addText(text, len); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1952 | builder.pop(); |
| 1953 | |
| 1954 | auto paragraph = builder.Build(); |
| 1955 | paragraph->layout(TestCanvasWidth - 100); |
| 1956 | |
| 1957 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 1958 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 1959 | |
| 1960 | paragraph->paint(canvas.get(), 0, 0); |
| 1961 | |
| 1962 | RectHeightStyle rect_height_style = RectHeightStyle::kMax; |
| 1963 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 1964 | std::vector<TextBox> boxes = paragraph->getRectsForRange(0, 100, rect_height_style, rect_width_style); |
| 1965 | canvas.drawRects(SK_ColorRED, boxes); |
| 1966 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1967 | REPORTER_ASSERT(reporter, boxes.size() == 1ull); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1968 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 1969 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 538.548f, EPSILON100)); // DIFF: 510.09375 |
| 1970 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), -0.268f, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1971 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 900, EPSILON100)); |
| 1972 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 44, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1973 | } |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1974 | |
| 1975 | // Checked DIFF+ |
| 1976 | DEF_TEST(SkParagraph_ArabicRectsLTRLeftAlignParagraph, reporter) { |
| 1977 | |
| 1978 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 1979 | if (!fontCollection->fontsFound()) return; |
| 1980 | TestCanvas canvas("SkParagraph_ArabicRectsLTRLeftAlignParagraph.png"); |
| 1981 | const char* text = "Helloبمباركة التقليدية قام عن. تصفح يد "; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1982 | const size_t len = strlen(text); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 1983 | |
| 1984 | ParagraphStyle paragraph_style; |
| 1985 | paragraph_style.turnHintingOff(); |
| 1986 | paragraph_style.setMaxLines(14); |
| 1987 | paragraph_style.setTextAlign(TextAlign::kLeft); |
| 1988 | paragraph_style.setTextDirection(TextDirection::kLtr); |
| 1989 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1990 | |
| 1991 | TextStyle text_style; |
| 1992 | text_style.setFontFamilies({SkString("Noto Naskh Arabic")}); |
| 1993 | text_style.setFontSize(26); |
| 1994 | text_style.setWordSpacing(5); |
| 1995 | text_style.setColor(SK_ColorBLACK); |
| 1996 | text_style.setDecoration(TextDecoration::kUnderline); |
| 1997 | text_style.setDecorationColor(SK_ColorBLACK); |
| 1998 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 1999 | builder.addText(text, len); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2000 | builder.pop(); |
| 2001 | |
| 2002 | auto paragraph = builder.Build(); |
| 2003 | paragraph->layout(TestCanvasWidth - 100); |
| 2004 | |
| 2005 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 2006 | REPORTER_ASSERT(reporter, impl->runs().size() == 3); |
| 2007 | |
| 2008 | paragraph->paint(canvas.get(), 0, 0); |
| 2009 | |
| 2010 | RectHeightStyle rect_height_style = RectHeightStyle::kMax; |
| 2011 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 2012 | std::vector<TextBox> boxes = paragraph->getRectsForRange(36, 40, rect_height_style, rect_width_style); |
| 2013 | canvas.drawRects(SK_ColorRED, boxes); |
| 2014 | |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 2015 | REPORTER_ASSERT(reporter, boxes.size() == 1ull); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2016 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 83.916f, EPSILON100)); // DIFF: 89.40625 |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2017 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), -0.268f, EPSILON100)); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 2018 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 115.893f, EPSILON100)); // DIFF: 121.87891 |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2019 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 44, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2020 | } |
| 2021 | |
| 2022 | // Checked DIFF+ |
| 2023 | DEF_TEST(SkParagraph_ArabicRectsLTRRightAlignParagraph, reporter) { |
| 2024 | |
| 2025 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 2026 | if (!fontCollection->fontsFound()) return; |
| 2027 | TestCanvas canvas("SkParagraph_ArabicRectsLTRRightAlignParagraph.png"); |
| 2028 | const char* text = "Helloبمباركة التقليدية قام عن. تصفح يد "; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2029 | const size_t len = strlen(text); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2030 | |
| 2031 | ParagraphStyle paragraph_style; |
| 2032 | paragraph_style.turnHintingOff(); |
| 2033 | paragraph_style.setMaxLines(14); |
| 2034 | paragraph_style.setTextAlign(TextAlign::kRight); |
| 2035 | paragraph_style.setTextDirection(TextDirection::kLtr); |
| 2036 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 2037 | |
| 2038 | TextStyle text_style; |
| 2039 | text_style.setFontFamilies({SkString("Noto Naskh Arabic")}); |
| 2040 | text_style.setFontSize(26); |
| 2041 | text_style.setWordSpacing(5); |
| 2042 | text_style.setColor(SK_ColorBLACK); |
| 2043 | text_style.setDecoration(TextDecoration::kUnderline); |
| 2044 | text_style.setDecorationColor(SK_ColorBLACK); |
| 2045 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2046 | builder.addText(text, len); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2047 | builder.pop(); |
| 2048 | |
| 2049 | auto paragraph = builder.Build(); |
| 2050 | paragraph->layout(TestCanvasWidth - 100); |
| 2051 | |
| 2052 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 2053 | REPORTER_ASSERT(reporter, impl->runs().size() == 3); |
| 2054 | |
| 2055 | paragraph->paint(canvas.get(), 0, 0); |
| 2056 | |
| 2057 | RectHeightStyle rect_height_style = RectHeightStyle::kMax; |
| 2058 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 2059 | std::vector<TextBox> boxes = |
| 2060 | paragraph->getRectsForRange(36, 40, rect_height_style, rect_width_style); |
| 2061 | canvas.drawRects(SK_ColorRED, boxes); |
| 2062 | |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 2063 | REPORTER_ASSERT(reporter, boxes.size() == 1ull); // DIFF |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2064 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 561.501f, EPSILON100)); // DIFF |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2065 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), -0.268f, EPSILON100)); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 2066 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 593.479f, EPSILON100)); // DIFF |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2067 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 44, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2068 | } |
| 2069 | |
| 2070 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2071 | DEF_TEST(SkParagraph_GetGlyphPositionAtCoordinateParagraph, reporter) { |
| 2072 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 2073 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2074 | TestCanvas canvas("SkParagraph_GetGlyphPositionAtCoordinateParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2075 | const char* text = |
| 2076 | "12345 67890 12345 67890 12345 67890 12345 67890 12345 67890 12345 " |
| 2077 | "67890 12345"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2078 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2079 | |
| 2080 | ParagraphStyle paragraphStyle; |
| 2081 | paragraphStyle.setTextAlign(TextAlign::kLeft); |
| 2082 | paragraphStyle.setMaxLines(10); |
| 2083 | paragraphStyle.turnHintingOff(); |
| 2084 | TextStyle textStyle; |
| 2085 | textStyle.setFontFamilies({SkString("Roboto")}); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2086 | textStyle.setFontStyle(SkFontStyle(SkFontStyle::kNormal_Weight, SkFontStyle::kNormal_Width, |
| 2087 | SkFontStyle::kUpright_Slant)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2088 | textStyle.setFontSize(50); |
| 2089 | textStyle.setLetterSpacing(1); |
| 2090 | textStyle.setWordSpacing(5); |
| 2091 | textStyle.setHeight(1); |
| 2092 | textStyle.setColor(SK_ColorBLACK); |
| 2093 | |
| 2094 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 2095 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2096 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2097 | builder.pop(); |
| 2098 | |
| 2099 | auto paragraph = builder.Build(); |
| 2100 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2101 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2102 | |
| 2103 | // Tests for getGlyphPositionAtCoordinate() |
| 2104 | // NOTE: resulting values can be a few off from their respective positions in |
| 2105 | // the original text because the final trailing whitespaces are sometimes not |
| 2106 | // drawn (namely, when using "justify" alignment) and therefore are not active |
| 2107 | // glyphs. |
| 2108 | REPORTER_ASSERT(reporter, |
| 2109 | paragraph->getGlyphPositionAtCoordinate(-10000, -10000).position == 0); |
| 2110 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(-1, -1).position == 0); |
| 2111 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(0, 0).position == 0); |
| 2112 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(3, 3).position == 0); |
| 2113 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(35, 1).position == 1); |
| 2114 | REPORTER_ASSERT(reporter, |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2115 | paragraph->getGlyphPositionAtCoordinate(300, 2).position == 11); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2116 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(301, 2.2f).position == 11); |
| 2117 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(302, 2.6f).position == 11); |
| 2118 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(301, 2.1f).position == 11); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2119 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(100000, 20).position == 18); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2120 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(450, 20).position == 16); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2121 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(100000, 90).position == 36); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2122 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(-100000, 90).position == 18); |
| 2123 | REPORTER_ASSERT(reporter, |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2124 | paragraph->getGlyphPositionAtCoordinate(20, -80).position == 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2125 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(1, 90).position == 18); |
| 2126 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(1, 170).position == 36); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2127 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(10000, 180).position == 72); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2128 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(70, 180).position == 56); |
| 2129 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(1, 270).position == 72); |
| 2130 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(35, 90).position == 19); |
| 2131 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(10000, 10000).position == 77); |
| 2132 | REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(85, 10000).position == 75); |
| 2133 | } |
| 2134 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2135 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2136 | DEF_TEST(SkParagraph_GetRectsForRangeParagraph, reporter) { |
| 2137 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 2138 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2139 | TestCanvas canvas("SkParagraph_GetRectsForRangeParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2140 | const char* text = |
| 2141 | "12345, \"67890\" 12345 67890 12345 67890 12345 67890 12345 67890 12345 " |
| 2142 | "67890 12345"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2143 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2144 | |
| 2145 | ParagraphStyle paragraphStyle; |
| 2146 | paragraphStyle.setTextAlign(TextAlign::kLeft); |
| 2147 | paragraphStyle.setMaxLines(10); |
| 2148 | paragraphStyle.turnHintingOff(); |
| 2149 | TextStyle textStyle; |
| 2150 | textStyle.setFontFamilies({SkString("Roboto")}); |
| 2151 | textStyle.setFontSize(50); |
| 2152 | textStyle.setColor(SK_ColorBLACK); |
| 2153 | textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 2154 | SkFontStyle::kUpright_Slant)); |
| 2155 | |
| 2156 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 2157 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2158 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2159 | builder.pop(); |
| 2160 | |
| 2161 | auto paragraph = builder.Build(); |
| 2162 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2163 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2164 | |
| 2165 | RectHeightStyle heightStyle = RectHeightStyle::kMax; |
| 2166 | RectWidthStyle widthStyle = RectWidthStyle::kTight; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2167 | |
| 2168 | SkPaint paint; |
| 2169 | paint.setStyle(SkPaint::kStroke_Style); |
| 2170 | paint.setAntiAlias(true); |
| 2171 | paint.setStrokeWidth(1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2172 | |
| 2173 | { |
| 2174 | auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle); |
| 2175 | REPORTER_ASSERT(reporter, result.empty()); |
| 2176 | } |
| 2177 | { |
| 2178 | auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2179 | canvas.drawRects(SK_ColorRED, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2180 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2181 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 0, EPSILON100)); |
| 2182 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100)); |
| 2183 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 28.417f, EPSILON100)); |
| 2184 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2185 | } |
| 2186 | { |
| 2187 | auto result = paragraph->getRectsForRange(2, 8, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2188 | canvas.drawRects(SK_ColorBLUE, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2189 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2190 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 56.835f, EPSILON100)); |
| 2191 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100)); |
| 2192 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 177.97f, EPSILON100)); |
| 2193 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2194 | } |
| 2195 | { |
| 2196 | auto result = paragraph->getRectsForRange(8, 21, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2197 | canvas.drawRects(SK_ColorGREEN, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2198 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2199 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 177.97f, EPSILON100)); |
| 2200 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100)); |
| 2201 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 507.031f, EPSILON100)); |
| 2202 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2203 | } |
| 2204 | { |
| 2205 | auto result = paragraph->getRectsForRange(30, 100, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2206 | canvas.drawRects(SK_ColorRED, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2207 | REPORTER_ASSERT(reporter, result.size() == 4); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2208 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 211.375f, EPSILON100)); |
| 2209 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 59.40625f, EPSILON100)); |
| 2210 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 463.623f, EPSILON100)); |
| 2211 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 118, EPSILON100)); |
| 2212 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.left(), 0, EPSILON100)); |
| 2213 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.top(), 236.406f, EPSILON100)); |
| 2214 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.right(), 142.089f, EPSILON100)); |
| 2215 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.bottom(), 295, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2216 | } |
| 2217 | { |
| 2218 | auto result = paragraph->getRectsForRange(19, 22, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2219 | canvas.drawRects(SK_ColorBLUE, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2220 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2221 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 450.1875f, EPSILON100)); |
| 2222 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100)); |
| 2223 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 519.47266f, EPSILON100)); |
| 2224 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2225 | } |
| 2226 | { |
| 2227 | auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle); |
| 2228 | REPORTER_ASSERT(reporter, result.empty()); |
| 2229 | } |
| 2230 | } |
| 2231 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2232 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2233 | DEF_TEST(SkParagraph_GetRectsForRangeTight, reporter) { |
| 2234 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 2235 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2236 | TestCanvas canvas("SkParagraph_GetRectsForRangeTight.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2237 | const char* text = |
| 2238 | "( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)(" |
| 2239 | " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)(" |
| 2240 | " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2241 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2242 | |
| 2243 | ParagraphStyle paragraphStyle; |
| 2244 | paragraphStyle.setTextAlign(TextAlign::kLeft); |
| 2245 | paragraphStyle.setMaxLines(10); |
| 2246 | paragraphStyle.turnHintingOff(); |
| 2247 | TextStyle textStyle; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2248 | textStyle.setFontFamilies({SkString("Noto Sans CJK JP")}); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2249 | textStyle.setFontSize(50); |
| 2250 | textStyle.setColor(SK_ColorBLACK); |
| 2251 | textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 2252 | SkFontStyle::kUpright_Slant)); |
| 2253 | |
| 2254 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 2255 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2256 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2257 | builder.pop(); |
| 2258 | |
| 2259 | auto paragraph = builder.Build(); |
| 2260 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2261 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2262 | |
| 2263 | RectHeightStyle heightStyle = RectHeightStyle::kTight; |
| 2264 | RectWidthStyle widthStyle = RectWidthStyle::kTight; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2265 | { |
| 2266 | auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle); |
| 2267 | REPORTER_ASSERT(reporter, result.empty()); |
| 2268 | } |
| 2269 | { |
| 2270 | auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2271 | canvas.drawRects(SK_ColorRED, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2272 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2273 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 0, EPSILON100)); |
| 2274 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0, EPSILON100)); |
| 2275 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 16.898f, EPSILON100)); |
| 2276 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 74, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2277 | } |
| 2278 | { |
| 2279 | auto result = paragraph->getRectsForRange(2, 8, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2280 | canvas.drawRects(SK_ColorBLUE, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2281 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2282 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 66.899f, EPSILON100)); |
| 2283 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0, EPSILON100)); |
| 2284 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 264.099f, EPSILON100)); |
| 2285 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 74, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2286 | } |
| 2287 | { |
| 2288 | auto result = paragraph->getRectsForRange(8, 21, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2289 | canvas.drawRects(SK_ColorGREEN, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2290 | REPORTER_ASSERT(reporter, result.size() == 2); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2291 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 264.099f, EPSILON100)); |
| 2292 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0, EPSILON100)); |
| 2293 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 595.085f, EPSILON50)); |
| 2294 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 74, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2295 | } |
| 2296 | } |
| 2297 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2298 | // Checked: DIFF+ |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2299 | DEF_TEST(SkParagraph_GetRectsForRangeIncludeLineSpacingMiddle, reporter) { |
| 2300 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 2301 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2302 | TestCanvas canvas("SkParagraph_GetRectsForRangeIncludeLineSpacingMiddle.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2303 | const char* text = |
| 2304 | "( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)(" |
| 2305 | " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)(" |
| 2306 | " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2307 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2308 | |
| 2309 | ParagraphStyle paragraphStyle; |
| 2310 | paragraphStyle.setTextAlign(TextAlign::kLeft); |
| 2311 | paragraphStyle.setMaxLines(10); |
| 2312 | paragraphStyle.turnHintingOff(); |
| 2313 | TextStyle textStyle; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2314 | textStyle.setFontFamilies({SkString("Roboto")}); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2315 | textStyle.setFontSize(50); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2316 | textStyle.setHeight(1.6f); |
| 2317 | textStyle.setHeightOverride(true); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2318 | textStyle.setColor(SK_ColorBLACK); |
| 2319 | textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 2320 | SkFontStyle::kUpright_Slant)); |
| 2321 | |
| 2322 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 2323 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2324 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2325 | builder.pop(); |
| 2326 | |
| 2327 | auto paragraph = builder.Build(); |
| 2328 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2329 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2330 | |
| 2331 | RectHeightStyle heightStyle = RectHeightStyle::kIncludeLineSpacingMiddle; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2332 | RectWidthStyle widthStyle = RectWidthStyle::kMax; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2333 | { |
| 2334 | auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle); |
| 2335 | REPORTER_ASSERT(reporter, result.empty()); |
| 2336 | } |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2337 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2338 | { |
| 2339 | auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2340 | canvas.drawRects(SK_ColorRED, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2341 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2342 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 0, EPSILON100)); |
| 2343 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946615f, EPSILON100)); |
| 2344 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 17.4296889f, EPSILON100)); |
| 2345 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 88.473305f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2346 | } |
| 2347 | { |
| 2348 | auto result = paragraph->getRectsForRange(2, 8, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2349 | canvas.drawRects(SK_ColorBLUE, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2350 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2351 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 67.429688f, EPSILON100)); |
| 2352 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946615f, EPSILON100)); |
| 2353 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 190.00781f, EPSILON100)); |
| 2354 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 88.473305f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2355 | } |
| 2356 | { |
| 2357 | auto result = paragraph->getRectsForRange(8, 21, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2358 | canvas.drawRects(SK_ColorGREEN, result); |
| 2359 | REPORTER_ASSERT(reporter, result.size() == 1); |
| 2360 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 190.00781f, EPSILON100)); |
| 2361 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946615f, EPSILON100)); |
| 2362 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 508.0625f, EPSILON50)); |
| 2363 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 88.473305f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2364 | } |
| 2365 | { |
| 2366 | auto result = paragraph->getRectsForRange(30, 150, heightStyle, widthStyle); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2367 | canvas.drawRects(SK_ColorRED, result); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2368 | REPORTER_ASSERT(reporter, result.size() == 8); |
| 2369 | |
| 2370 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 190.00781f, EPSILON100)); |
| 2371 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 88.473305f, EPSILON100)); |
| 2372 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 525.687f, EPSILON20)); |
| 2373 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 168.47331f, EPSILON100)); |
| 2374 | |
| 2375 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.left(), 525.687f, EPSILON20)); |
| 2376 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.top(), 88.473305f, EPSILON100)); |
| 2377 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.right(), 570.02344f, EPSILON20)); |
| 2378 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.bottom(), 168.47331f, EPSILON100)); |
| 2379 | |
| 2380 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.left(), 0, EPSILON100)); |
| 2381 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.top(), 168.47331f, EPSILON100)); |
| 2382 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.right(), 531.574f, EPSILON50)); |
| 2383 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.bottom(), 248.47331f, EPSILON100)); |
| 2384 | |
| 2385 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.left(), 531.574f, EPSILON50)); |
| 2386 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.top(), 168.47331f, EPSILON100)); |
| 2387 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.right(), 570.02344f, EPSILON20)); |
| 2388 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.bottom(), 248.47331f, EPSILON100)); |
| 2389 | |
| 2390 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.left(), 0, EPSILON100)); |
| 2391 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.top(), 248.47331f, EPSILON100)); |
| 2392 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.right(), 570.02344f, EPSILON20)); |
| 2393 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.bottom(), 328.47333f, EPSILON100)); |
| 2394 | |
| 2395 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.left(), 0, EPSILON100)); |
| 2396 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.top(), 328.47333f, EPSILON100)); |
| 2397 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.right(), 570.02344f, EPSILON20)); |
| 2398 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.bottom(), 408.4733f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2399 | } |
| 2400 | { |
| 2401 | auto result = paragraph->getRectsForRange(19, 22, heightStyle, widthStyle); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2402 | canvas.drawRects(SK_ColorBLUE, result); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2403 | REPORTER_ASSERT(reporter, result.size() == 2); // DIFF |
| 2404 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 463.72656f, EPSILON50)); |
| 2405 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946615f, EPSILON100)); |
| 2406 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 530.23047f, EPSILON50)); |
| 2407 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 88.473305f, EPSILON100)); |
| 2408 | |
| 2409 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.left(), 530.23047f, EPSILON50)); |
| 2410 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.top(), 16.946615f, EPSILON100)); |
| 2411 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.right(), 570.02344f, EPSILON20)); |
| 2412 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.bottom(), 88.473305f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2413 | } |
| 2414 | { |
| 2415 | auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle); |
| 2416 | REPORTER_ASSERT(reporter, result.empty()); |
| 2417 | } |
| 2418 | } |
| 2419 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2420 | // Checked: NO DIFF+ |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2421 | DEF_TEST(SkParagraph_GetRectsForRangeIncludeLineSpacingTop, reporter) { |
| 2422 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 2423 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2424 | TestCanvas canvas("SkParagraph_GetRectsForRangeIncludeLineSpacingTop.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2425 | const char* text = |
| 2426 | "( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)(" |
| 2427 | " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)(" |
| 2428 | " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2429 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2430 | |
| 2431 | ParagraphStyle paragraphStyle; |
| 2432 | paragraphStyle.setTextAlign(TextAlign::kLeft); |
| 2433 | paragraphStyle.setMaxLines(10); |
| 2434 | paragraphStyle.turnHintingOff(); |
| 2435 | TextStyle textStyle; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2436 | textStyle.setFontFamilies({SkString("Roboto")}); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2437 | textStyle.setFontSize(50); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2438 | textStyle.setHeight(1.6f); |
| 2439 | textStyle.setHeightOverride(true); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2440 | textStyle.setColor(SK_ColorBLACK); |
| 2441 | textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 2442 | SkFontStyle::kUpright_Slant)); |
| 2443 | |
| 2444 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 2445 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2446 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2447 | builder.pop(); |
| 2448 | |
| 2449 | auto paragraph = builder.Build(); |
| 2450 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2451 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2452 | |
| 2453 | RectHeightStyle heightStyle = RectHeightStyle::kIncludeLineSpacingTop; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2454 | RectWidthStyle widthStyle = RectWidthStyle::kMax; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2455 | { |
| 2456 | auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle); |
| 2457 | REPORTER_ASSERT(reporter, result.empty()); |
| 2458 | } |
| 2459 | |
| 2460 | { |
| 2461 | auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2462 | canvas.drawRects(SK_ColorRED, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2463 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2464 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 0, EPSILON100)); |
| 2465 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946615f, EPSILON100)); |
| 2466 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 17.4296889f, EPSILON100)); |
| 2467 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 80, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2468 | } |
| 2469 | { |
| 2470 | auto result = paragraph->getRectsForRange(2, 8, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2471 | canvas.drawRects(SK_ColorBLUE, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2472 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2473 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 67.429688f, EPSILON100)); |
| 2474 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946615f, EPSILON100)); |
| 2475 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 190.00781f, EPSILON100)); |
| 2476 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 80, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2477 | } |
| 2478 | { |
| 2479 | auto result = paragraph->getRectsForRange(8, 21, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2480 | canvas.drawRects(SK_ColorGREEN, result); |
| 2481 | REPORTER_ASSERT(reporter, result.size() == 1); |
| 2482 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 190.00781f, EPSILON100)); |
| 2483 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946615f, EPSILON100)); |
| 2484 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 508.0625f, EPSILON50)); |
| 2485 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 80, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2486 | } |
| 2487 | { |
| 2488 | auto result = paragraph->getRectsForRange(30, 150, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2489 | canvas.drawRects(SK_ColorMAGENTA, result); |
| 2490 | REPORTER_ASSERT(reporter, result.size() == 8); |
| 2491 | |
| 2492 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 190.00781f, EPSILON100)); |
| 2493 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 80, EPSILON100)); |
| 2494 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 525.687f, EPSILON20)); |
| 2495 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 160, EPSILON100)); |
| 2496 | |
| 2497 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.left(), 525.687f, EPSILON20)); |
| 2498 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.top(), 80, EPSILON100)); |
| 2499 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.right(), 570.02344f, EPSILON20)); |
| 2500 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.bottom(), 160, EPSILON100)); |
| 2501 | |
| 2502 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.left(), 0, EPSILON100)); |
| 2503 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.top(), 160, EPSILON100)); |
| 2504 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.right(), 531.574f, EPSILON20)); |
| 2505 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.bottom(), 240, EPSILON100)); |
| 2506 | |
| 2507 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.left(), 531.574f, EPSILON20)); |
| 2508 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.top(), 160, EPSILON100)); |
| 2509 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.right(), 570.02344f, EPSILON20)); |
| 2510 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.bottom(), 240, EPSILON100)); |
| 2511 | |
| 2512 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.left(), 0, EPSILON100)); |
| 2513 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.top(), 240, EPSILON100)); |
| 2514 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.right(), 570.02344f, EPSILON20)); |
| 2515 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.bottom(), 320, EPSILON100)); |
| 2516 | |
| 2517 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.left(), 0, EPSILON100)); |
| 2518 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.top(), 320, EPSILON100)); |
| 2519 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.right(), 570.02344f, EPSILON20)); |
| 2520 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.bottom(), 400, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2521 | } |
| 2522 | { |
| 2523 | auto result = paragraph->getRectsForRange(19, 22, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2524 | canvas.drawRects(SK_ColorBLACK, result); |
| 2525 | REPORTER_ASSERT(reporter, result.size() == 2); // DIFF |
| 2526 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 463.72656f, EPSILON50)); |
| 2527 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946615f, EPSILON100)); |
| 2528 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 530.23047f, EPSILON50)); |
| 2529 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 80, EPSILON100)); |
| 2530 | |
| 2531 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.left(), 530.23047f, EPSILON50)); |
| 2532 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.top(), 16.946615f, EPSILON100)); |
| 2533 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.right(), 570.02344f, EPSILON20)); |
| 2534 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.bottom(), 80, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2535 | } |
| 2536 | { |
| 2537 | auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle); |
| 2538 | REPORTER_ASSERT(reporter, result.empty()); |
| 2539 | } |
| 2540 | } |
| 2541 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2542 | // Checked: NO DIFF+ |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2543 | DEF_TEST(SkParagraph_GetRectsForRangeIncludeLineSpacingBottom, reporter) { |
| 2544 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 2545 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2546 | TestCanvas canvas("SkParagraph_GetRectsForRangeIncludeLineSpacingBottom.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2547 | const char* text = |
| 2548 | "( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)(" |
| 2549 | " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)(" |
| 2550 | " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2551 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2552 | |
| 2553 | ParagraphStyle paragraphStyle; |
| 2554 | paragraphStyle.setTextAlign(TextAlign::kLeft); |
| 2555 | paragraphStyle.setMaxLines(10); |
| 2556 | paragraphStyle.turnHintingOff(); |
| 2557 | TextStyle textStyle; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2558 | textStyle.setFontFamilies({SkString("Roboto")}); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2559 | textStyle.setFontSize(50); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2560 | textStyle.setHeight(1.6f); |
| 2561 | textStyle.setHeightOverride(true); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2562 | textStyle.setColor(SK_ColorBLACK); |
| 2563 | textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 2564 | SkFontStyle::kUpright_Slant)); |
| 2565 | |
| 2566 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 2567 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2568 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2569 | builder.pop(); |
| 2570 | |
| 2571 | auto paragraph = builder.Build(); |
| 2572 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2573 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2574 | |
| 2575 | RectHeightStyle heightStyle = RectHeightStyle::kIncludeLineSpacingBottom; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2576 | RectWidthStyle widthStyle = RectWidthStyle::kMax; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2577 | { |
| 2578 | auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle); |
| 2579 | REPORTER_ASSERT(reporter, result.empty()); |
| 2580 | } |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2581 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2582 | { |
| 2583 | auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2584 | canvas.drawRects(SK_ColorRED, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2585 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2586 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 0, EPSILON100)); |
| 2587 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946f, EPSILON100)); |
| 2588 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 17.429f, EPSILON100)); |
| 2589 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 96.946f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2590 | } |
| 2591 | { |
| 2592 | auto result = paragraph->getRectsForRange(2, 8, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2593 | canvas.drawRects(SK_ColorBLUE, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2594 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2595 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 67.4298f, EPSILON100)); |
| 2596 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946f, EPSILON100)); |
| 2597 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 190.007f, EPSILON100)); |
| 2598 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 96.946f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2599 | } |
| 2600 | { |
| 2601 | auto result = paragraph->getRectsForRange(8, 21, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2602 | canvas.drawRects(SK_ColorGREEN, result); |
| 2603 | REPORTER_ASSERT(reporter, result.size() == 1); |
| 2604 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 190.007f, EPSILON100)); |
| 2605 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946f, EPSILON100)); |
| 2606 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 508.062f, EPSILON50)); |
| 2607 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 96.946f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2608 | } |
| 2609 | { |
| 2610 | auto result = paragraph->getRectsForRange(30, 150, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2611 | canvas.drawRects(SK_ColorMAGENTA, result); |
| 2612 | REPORTER_ASSERT(reporter, result.size() == 8); |
| 2613 | |
| 2614 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 190.007f, EPSILON100)); |
| 2615 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 96.946f, EPSILON100)); |
| 2616 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 525.687f, EPSILON20)); |
| 2617 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 176.946f, EPSILON100)); |
| 2618 | |
| 2619 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.left(), 525.687f, EPSILON20)); |
| 2620 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.top(), 96.946f, EPSILON100)); |
| 2621 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.right(), 570.023f, EPSILON20)); |
| 2622 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.bottom(), 176.946f, EPSILON100)); |
| 2623 | |
| 2624 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.left(), 0, EPSILON100)); |
| 2625 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.top(), 176.946f, EPSILON100)); |
| 2626 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.right(), 531.574f, EPSILON20)); |
| 2627 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.bottom(), 256.946f, EPSILON100)); |
| 2628 | |
| 2629 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.left(), 531.574f, EPSILON20)); |
| 2630 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.top(), 176.946f, EPSILON100)); |
| 2631 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.right(), 570.023f, EPSILON20)); |
| 2632 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.bottom(), 256.946f, EPSILON100)); |
| 2633 | |
| 2634 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.left(), 0, EPSILON100)); |
| 2635 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.top(), 256.946f, EPSILON100)); |
| 2636 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.right(), 570.023f, EPSILON20)); |
| 2637 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.bottom(), 336.946f, EPSILON100)); |
| 2638 | |
| 2639 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.left(), 0, EPSILON100)); |
| 2640 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.top(), 336.946f, EPSILON100)); |
| 2641 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.right(), 570.023f, EPSILON20)); |
| 2642 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.bottom(), 416.946f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2643 | } |
| 2644 | { |
| 2645 | auto result = paragraph->getRectsForRange(19, 22, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2646 | canvas.drawRects(SK_ColorBLACK, result); |
| 2647 | REPORTER_ASSERT(reporter, result.size() == 2); // DIFF |
| 2648 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 463.726f, EPSILON50)); |
| 2649 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946f, EPSILON100)); |
| 2650 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 530.230f, EPSILON50)); |
| 2651 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 96.946f, EPSILON100)); |
| 2652 | |
| 2653 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.left(), 530.230f, EPSILON50)); |
| 2654 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.top(), 16.946f, EPSILON100)); |
| 2655 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.right(), 570.023f, EPSILON20)); |
| 2656 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.bottom(), 96.946f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2657 | } |
| 2658 | { |
| 2659 | auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle); |
| 2660 | REPORTER_ASSERT(reporter, result.empty()); |
| 2661 | } |
| 2662 | } |
| 2663 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2664 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2665 | DEF_TEST(SkParagraph_GetRectsForRangeIncludeCombiningCharacter, reporter) { |
| 2666 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 2667 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2668 | TestCanvas canvas("SkParagraph_GetRectsForRangeIncludeCombiningCharacter.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2669 | const char* text = "ดีสวัสดีชาวโลกที่น่ารัก"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2670 | const size_t len = strlen(text); |
| 2671 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2672 | ParagraphStyle paragraphStyle; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2673 | paragraphStyle.setTextAlign(TextAlign::kLeft); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2674 | paragraphStyle.setMaxLines(10); |
| 2675 | paragraphStyle.turnHintingOff(); |
| 2676 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 2677 | |
| 2678 | TextStyle textStyle; |
| 2679 | textStyle.setFontFamilies({SkString("Roboto")}); |
| 2680 | textStyle.setFontSize(50); |
| 2681 | textStyle.setLetterSpacing(1); |
| 2682 | textStyle.setWordSpacing(5); |
| 2683 | textStyle.setHeight(1); |
| 2684 | textStyle.setColor(SK_ColorBLACK); |
| 2685 | |
| 2686 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2687 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2688 | builder.pop(); |
| 2689 | |
| 2690 | auto paragraph = builder.Build(); |
| 2691 | paragraph->layout(TestCanvasWidth - 100); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2692 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2693 | |
| 2694 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2695 | REPORTER_ASSERT(reporter, impl->lines().size() == 1); |
| 2696 | |
| 2697 | RectHeightStyle heightStyle = RectHeightStyle::kTight; |
| 2698 | RectWidthStyle widthStyle = RectWidthStyle::kTight; |
| 2699 | { |
| 2700 | auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle); |
| 2701 | REPORTER_ASSERT(reporter, result.empty()); |
| 2702 | } |
| 2703 | { |
| 2704 | auto first = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle); |
| 2705 | auto second = paragraph->getRectsForRange(1, 2, heightStyle, widthStyle); |
| 2706 | auto last = paragraph->getRectsForRange(0, 2, heightStyle, widthStyle); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2707 | REPORTER_ASSERT(reporter, first.size() == 0 && second.size() == 1 && last.size() == 1); |
| 2708 | REPORTER_ASSERT(reporter, second[0].rect == last[0].rect); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2709 | } |
| 2710 | { |
| 2711 | auto first = paragraph->getRectsForRange(3, 4, heightStyle, widthStyle); |
| 2712 | auto second = paragraph->getRectsForRange(4, 5, heightStyle, widthStyle); |
| 2713 | auto last = paragraph->getRectsForRange(3, 5, heightStyle, widthStyle); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2714 | REPORTER_ASSERT(reporter, first.size() == 0 && second.size() == 1 && last.size() == 1); |
| 2715 | REPORTER_ASSERT(reporter, second[0].rect == last[0].rect); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2716 | } |
| 2717 | { |
| 2718 | auto first = paragraph->getRectsForRange(14, 15, heightStyle, widthStyle); |
| 2719 | auto second = paragraph->getRectsForRange(15, 16, heightStyle, widthStyle); |
| 2720 | auto third = paragraph->getRectsForRange(16, 17, heightStyle, widthStyle); |
| 2721 | auto last = paragraph->getRectsForRange(14, 17, heightStyle, widthStyle); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2722 | REPORTER_ASSERT(reporter, first.size() == 0 && second.size() == 0 && |
| 2723 | third.size() == 1 && last.size() == 1); |
| 2724 | REPORTER_ASSERT(reporter, third[0].rect == last[0].rect); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2725 | } |
| 2726 | } |
| 2727 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2728 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2729 | DEF_TEST(SkParagraph_GetRectsForRangeCenterParagraph, reporter) { |
| 2730 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 2731 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2732 | TestCanvas canvas("SkParagraph_GetRectsForRangeCenterParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2733 | // Minikin uses a hard coded list of unicode characters that he treats as invisible - as spaces. |
| 2734 | // It's absolutely wrong - invisibility is a glyph attribute, not character/grapheme. |
| 2735 | // Any attempt to substitute one for another leads to errors |
| 2736 | // (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] | 2737 | const char* text = "01234 "; // includes ideographic space and english space. |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2738 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2739 | |
| 2740 | ParagraphStyle paragraphStyle; |
| 2741 | paragraphStyle.setTextAlign(TextAlign::kCenter); |
| 2742 | paragraphStyle.setMaxLines(10); |
| 2743 | paragraphStyle.turnHintingOff(); |
| 2744 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 2745 | |
| 2746 | TextStyle textStyle; |
| 2747 | textStyle.setFontFamilies({SkString("Roboto")}); |
| 2748 | textStyle.setFontSize(50); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2749 | textStyle.setHeight(1); |
| 2750 | textStyle.setColor(SK_ColorBLACK); |
| 2751 | textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 2752 | SkFontStyle::kUpright_Slant)); |
| 2753 | |
| 2754 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2755 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2756 | builder.pop(); |
| 2757 | |
| 2758 | auto paragraph = builder.Build(); |
| 2759 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2760 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2761 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2762 | // Some of the formatting lazily done on paint |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2763 | RectHeightStyle heightStyle = RectHeightStyle::kMax; |
| 2764 | RectWidthStyle widthStyle = RectWidthStyle::kTight; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2765 | { |
| 2766 | auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle); |
| 2767 | REPORTER_ASSERT(reporter, result.empty()); |
| 2768 | } |
| 2769 | |
| 2770 | { |
| 2771 | auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2772 | canvas.drawRects(SK_ColorRED, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2773 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2774 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 203.955f, EPSILON100)); |
| 2775 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100)); |
| 2776 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 232.373f, EPSILON100)); |
| 2777 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2778 | } |
| 2779 | |
| 2780 | { |
| 2781 | auto result = paragraph->getRectsForRange(2, 4, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2782 | canvas.drawRects(SK_ColorBLUE, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2783 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2784 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 260.791f, EPSILON100)); |
| 2785 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100)); |
| 2786 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 317.626f, EPSILON100)); |
| 2787 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2788 | } |
| 2789 | |
| 2790 | { |
| 2791 | auto result = paragraph->getRectsForRange(4, 5, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2792 | canvas.drawRects(SK_ColorGREEN, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2793 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2794 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 317.626f, EPSILON100)); |
| 2795 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100)); |
| 2796 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 346.044f, EPSILON100)); |
| 2797 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2798 | } |
| 2799 | |
| 2800 | { |
| 2801 | auto result = paragraph->getRectsForRange(4, 6, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2802 | canvas.drawRects(SK_ColorBLACK, result); |
| 2803 | REPORTER_ASSERT(reporter, result.size() == 1); // DIFF |
| 2804 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 317.626f, EPSILON100)); |
| 2805 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100)); |
| 2806 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 358.494f, EPSILON100)); |
| 2807 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2808 | } |
| 2809 | |
| 2810 | { |
| 2811 | auto result = paragraph->getRectsForRange(5, 6, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2812 | canvas.drawRects(SK_ColorRED, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2813 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2814 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 346.044f, EPSILON100)); |
| 2815 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100)); |
| 2816 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 358.494f, EPSILON100)); |
| 2817 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2818 | } |
| 2819 | |
| 2820 | { |
| 2821 | auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle); |
| 2822 | REPORTER_ASSERT(reporter, result.empty()); |
| 2823 | } |
| 2824 | } |
| 2825 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2826 | // Checked DIFF+ |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2827 | DEF_TEST(SkParagraph_GetRectsForRangeCenterParagraphNewlineCentered, reporter) { |
| 2828 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 2829 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2830 | TestCanvas canvas("SkParagraph_GetRectsForRangeCenterParagraphNewlineCentered.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2831 | const char* text = "01234\n"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2832 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2833 | |
| 2834 | ParagraphStyle paragraphStyle; |
| 2835 | paragraphStyle.setTextAlign(TextAlign::kCenter); |
| 2836 | paragraphStyle.setMaxLines(10); |
| 2837 | paragraphStyle.turnHintingOff(); |
| 2838 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 2839 | |
| 2840 | TextStyle textStyle; |
| 2841 | textStyle.setFontFamilies({SkString("Roboto")}); |
| 2842 | textStyle.setFontSize(50); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2843 | textStyle.setHeight(1); |
| 2844 | textStyle.setColor(SK_ColorBLACK); |
| 2845 | textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 2846 | SkFontStyle::kUpright_Slant)); |
| 2847 | |
| 2848 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2849 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2850 | builder.pop(); |
| 2851 | |
| 2852 | auto paragraph = builder.Build(); |
| 2853 | paragraph->layout(550); |
| 2854 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2855 | paragraph->paint(canvas.get(), 0, 0); |
| 2856 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2857 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2858 | REPORTER_ASSERT(reporter, impl->lines().size() == 2); |
| 2859 | |
| 2860 | RectHeightStyle heightStyle = RectHeightStyle::kMax; |
| 2861 | RectWidthStyle widthStyle = RectWidthStyle::kTight; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2862 | { |
| 2863 | auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle); |
| 2864 | REPORTER_ASSERT(reporter, result.empty()); |
| 2865 | } |
| 2866 | |
| 2867 | { |
| 2868 | auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 2869 | canvas.drawRects(SK_ColorRED, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2870 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2871 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 203.955f, EPSILON100)); |
| 2872 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100)); |
| 2873 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 232.373f, EPSILON100)); |
| 2874 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2875 | } |
| 2876 | |
| 2877 | { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2878 | auto result = paragraph->getRectsForRange(6, 7, heightStyle, widthStyle); |
| 2879 | canvas.drawRects(SK_ColorBLUE, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2880 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2881 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 275.0f, EPSILON100)); |
| 2882 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 59.406f, EPSILON100)); |
| 2883 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 275.0f, EPSILON100)); |
| 2884 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 118, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2885 | } |
| 2886 | } |
| 2887 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2888 | // Checked NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2889 | DEF_TEST(SkParagraph_GetRectsForRangeCenterMultiLineParagraph, reporter) { |
| 2890 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 2891 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2892 | TestCanvas canvas("SkParagraph_GetRectsForRangeCenterMultiLineParagraph.png"); |
| 2893 | const char* text = "01234 \n0123 "; // includes ideographic space and english space. |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2894 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2895 | |
| 2896 | ParagraphStyle paragraphStyle; |
| 2897 | paragraphStyle.setTextAlign(TextAlign::kCenter); |
| 2898 | paragraphStyle.setMaxLines(10); |
| 2899 | paragraphStyle.turnHintingOff(); |
| 2900 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 2901 | |
| 2902 | TextStyle textStyle; |
| 2903 | textStyle.setFontFamilies({SkString("Roboto")}); |
| 2904 | textStyle.setFontSize(50); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2905 | textStyle.setHeight(1); |
| 2906 | textStyle.setColor(SK_ColorBLACK); |
| 2907 | textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 2908 | SkFontStyle::kUpright_Slant)); |
| 2909 | |
| 2910 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2911 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2912 | builder.pop(); |
| 2913 | |
| 2914 | auto paragraph = builder.Build(); |
| 2915 | paragraph->layout(550); |
| 2916 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2917 | paragraph->paint(canvas.get(), 0, 0); |
| 2918 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2919 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2920 | |
| 2921 | REPORTER_ASSERT(reporter, impl->lines().size() == 2); |
| 2922 | |
| 2923 | RectHeightStyle heightStyle = RectHeightStyle::kMax; |
| 2924 | RectWidthStyle widthStyle = RectWidthStyle::kTight; |
| 2925 | SkScalar epsilon = 0.01f; |
| 2926 | { |
| 2927 | auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle); |
| 2928 | REPORTER_ASSERT(reporter, result.empty()); |
| 2929 | } |
| 2930 | { |
| 2931 | auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2932 | canvas.drawRects(SK_ColorRED, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2933 | REPORTER_ASSERT(reporter, result.size() == 1); |
| 2934 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 203.955f, epsilon)); |
| 2935 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon)); |
| 2936 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 232.373f, epsilon)); |
| 2937 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon)); |
| 2938 | } |
| 2939 | { |
| 2940 | auto result = paragraph->getRectsForRange(2, 4, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2941 | canvas.drawRects(SK_ColorBLUE, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2942 | REPORTER_ASSERT(reporter, result.size() == 1); |
| 2943 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 260.791f, epsilon)); |
| 2944 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon)); |
| 2945 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 317.626f, epsilon)); |
| 2946 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon)); |
| 2947 | } |
| 2948 | { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2949 | auto result = paragraph->getRectsForRange(4, 6, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2950 | canvas.drawRects(SK_ColorGREEN, result); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2951 | REPORTER_ASSERT(reporter, result.size() == 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2952 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 317.626f, epsilon)); |
| 2953 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon)); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2954 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 358.494f, epsilon)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2955 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon)); |
| 2956 | } |
| 2957 | { |
| 2958 | auto result = paragraph->getRectsForRange(5, 6, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2959 | canvas.drawRects(SK_ColorYELLOW, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2960 | REPORTER_ASSERT(reporter, result.size() == 1); |
| 2961 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 346.044f, epsilon)); |
| 2962 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2963 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 358.494f, epsilon)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2964 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon)); |
| 2965 | } |
| 2966 | { |
| 2967 | auto result = paragraph->getRectsForRange(10, 12, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2968 | canvas.drawRects(SK_ColorCYAN, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2969 | REPORTER_ASSERT(reporter, result.size() == 1); |
| 2970 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 218.164f, epsilon)); |
| 2971 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 59.40625f, epsilon)); |
| 2972 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 275, epsilon)); |
| 2973 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 118, epsilon)); |
| 2974 | } |
| 2975 | { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2976 | auto result = paragraph->getRectsForRange(14, 18, heightStyle, widthStyle); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2977 | canvas.drawRects(SK_ColorBLACK, result); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2978 | REPORTER_ASSERT(reporter, result.size() == 1); |
| 2979 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 331.835f, epsilon)); |
| 2980 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 59.40625f, epsilon)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 2981 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 419.189f, epsilon)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2982 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 118, epsilon)); |
| 2983 | } |
| 2984 | { |
| 2985 | auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle); |
| 2986 | REPORTER_ASSERT(reporter, result.empty()); |
| 2987 | } |
| 2988 | } |
| 2989 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2990 | // Checked: DIFF (line height rounding error) |
| 2991 | DEF_TEST(SkParagraph_GetRectsForRangeStrut, reporter) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2992 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 2993 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2994 | TestCanvas canvas("SkParagraph_GetRectsForRangeStrut.png"); |
| 2995 | const char* text = "Chinese 字典"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 2996 | const size_t len = strlen(text); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 2997 | |
| 2998 | StrutStyle strutStyle; |
| 2999 | strutStyle.setStrutEnabled(true); |
| 3000 | strutStyle.setFontFamilies({SkString("Roboto")}); |
| 3001 | strutStyle.setFontSize(14.0); |
| 3002 | |
| 3003 | ParagraphStyle paragraphStyle; |
| 3004 | paragraphStyle.setStrutStyle(strutStyle); |
| 3005 | |
| 3006 | TextStyle textStyle; |
| 3007 | textStyle.setFontFamilies({SkString("Noto Sans CJK JP")}); |
| 3008 | textStyle.setFontSize(20); |
| 3009 | textStyle.setColor(SK_ColorBLACK); |
| 3010 | |
| 3011 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 3012 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3013 | builder.addText(text, len); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3014 | builder.pop(); |
| 3015 | |
| 3016 | auto paragraph = builder.Build(); |
| 3017 | paragraph->layout(550); |
| 3018 | paragraph->paint(canvas.get(), 0, 0); |
| 3019 | |
| 3020 | { |
| 3021 | auto result = paragraph->getRectsForRange(0, 10, RectHeightStyle::kTight, RectWidthStyle::kMax); |
| 3022 | canvas.drawRects(SK_ColorGREEN, result); |
| 3023 | REPORTER_ASSERT(reporter, result.size() == 1); |
| 3024 | } |
| 3025 | |
| 3026 | { |
| 3027 | auto result = paragraph->getRectsForRange(0, 10, RectHeightStyle::kStrut, RectWidthStyle::kMax); |
| 3028 | canvas.drawRects(SK_ColorRED, result); |
| 3029 | REPORTER_ASSERT(reporter, result.size() == 1); |
| 3030 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 0, EPSILON100)); |
| 3031 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 10.611f, EPSILON2)); |
| 3032 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 118.605f, EPSILON50)); |
| 3033 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 27.017f, EPSILON2)); |
| 3034 | } |
| 3035 | } |
| 3036 | |
| 3037 | // Checked: NO DIFF |
| 3038 | DEF_TEST(SkParagraph_GetRectsForRangeStrutFallback, reporter) { |
| 3039 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 3040 | if (!fontCollection->fontsFound()) return; |
| 3041 | TestCanvas canvas("SkParagraph_GetRectsForRangeStrutFallback.png"); |
| 3042 | const char* text = "Chinese 字典"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3043 | const size_t len = strlen(text); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3044 | |
| 3045 | StrutStyle strutStyle; |
| 3046 | strutStyle.setStrutEnabled(false); |
| 3047 | |
| 3048 | ParagraphStyle paragraphStyle; |
| 3049 | paragraphStyle.setStrutStyle(strutStyle); |
| 3050 | |
| 3051 | TextStyle textStyle; |
| 3052 | textStyle.setFontFamilies({SkString("Noto Sans CJK JP")}); |
| 3053 | textStyle.setFontSize(20); |
| 3054 | textStyle.setColor(SK_ColorBLACK); |
| 3055 | |
| 3056 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 3057 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3058 | builder.addText(text, len); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3059 | builder.pop(); |
| 3060 | |
| 3061 | auto paragraph = builder.Build(); |
| 3062 | paragraph->layout(550); |
| 3063 | paragraph->paint(canvas.get(), 0, 0); |
| 3064 | |
| 3065 | |
| 3066 | auto result1 = paragraph->getRectsForRange(0, 10, RectHeightStyle::kTight, RectWidthStyle::kMax); |
| 3067 | canvas.drawRects(SK_ColorGREEN, result1); |
| 3068 | REPORTER_ASSERT(reporter, result1.size() == 1); |
| 3069 | |
| 3070 | auto result2 = paragraph->getRectsForRange(0, 10, RectHeightStyle::kStrut, RectWidthStyle::kMax); |
| 3071 | canvas.drawRects(SK_ColorRED, result2); |
| 3072 | REPORTER_ASSERT(reporter, result2.size() == 1); |
| 3073 | |
| 3074 | REPORTER_ASSERT(reporter, result1[0].rect == result2[0].rect); |
| 3075 | } |
| 3076 | |
| 3077 | // Checked: DIFF (small in numbers) |
| 3078 | DEF_TEST(SkParagraph_GetWordBoundaryParagraph, reporter) { |
| 3079 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 3080 | if (!fontCollection->fontsFound()) return; |
| 3081 | TestCanvas canvas("SkParagraph_GetWordBoundaryParagraph.png"); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3082 | const char* text = "12345 67890 12345 67890 12345 67890 12345 " |
| 3083 | "67890 12345 67890 12345 67890 12345"; |
| 3084 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3085 | ParagraphStyle paragraphStyle; |
| 3086 | paragraphStyle.setTextAlign(TextAlign::kLeft); |
| 3087 | paragraphStyle.setMaxLines(10); |
| 3088 | paragraphStyle.turnHintingOff(); |
| 3089 | TextStyle textStyle; |
| 3090 | textStyle.setFontFamilies({SkString("Roboto")}); |
| 3091 | textStyle.setFontSize(52); |
| 3092 | textStyle.setLetterSpacing(1.19039f); |
| 3093 | textStyle.setWordSpacing(5); |
| 3094 | textStyle.setHeight(1.5); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3095 | textStyle.setHeightOverride(true); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3096 | textStyle.setColor(SK_ColorBLACK); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3097 | |
| 3098 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
| 3099 | builder.pushStyle(textStyle); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3100 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3101 | builder.pop(); |
| 3102 | |
| 3103 | auto paragraph = builder.Build(); |
| 3104 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3105 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3106 | |
| 3107 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(0) == SkRange<size_t>(0, 5)); |
| 3108 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(1) == SkRange<size_t>(0, 5)); |
| 3109 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(2) == SkRange<size_t>(0, 5)); |
| 3110 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(3) == SkRange<size_t>(0, 5)); |
| 3111 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(4) == SkRange<size_t>(0, 5)); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3112 | auto boxes = paragraph->getRectsForRange(5, 6, RectHeightStyle::kMax, RectWidthStyle::kTight); |
| 3113 | canvas.drawLines(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3114 | |
| 3115 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(5) == SkRange<size_t>(5, 7)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3116 | boxes = paragraph->getRectsForRange(6, 7, RectHeightStyle::kMax, RectWidthStyle::kTight); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3117 | canvas.drawLines(SK_ColorRED, boxes); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3118 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3119 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(6) == SkRange<size_t>(5, 7)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3120 | boxes = paragraph->getRectsForRange(7, 8, RectHeightStyle::kMax, RectWidthStyle::kTight); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3121 | canvas.drawLines(SK_ColorRED, boxes); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3122 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3123 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(7) == SkRange<size_t>(7, 12)); |
| 3124 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(8) == SkRange<size_t>(7, 12)); |
| 3125 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(9) == SkRange<size_t>(7, 12)); |
| 3126 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(10) == SkRange<size_t>(7, 12)); |
| 3127 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(11) == SkRange<size_t>(7, 12)); |
| 3128 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(12) == SkRange<size_t>(12, 13)); |
| 3129 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(13) == SkRange<size_t>(13, 18)); |
| 3130 | REPORTER_ASSERT(reporter, paragraph->getWordBoundary(30) == SkRange<size_t>(30, 31)); |
| 3131 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3132 | boxes = paragraph->getRectsForRange(12, 13, RectHeightStyle::kMax, RectWidthStyle::kTight); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3133 | canvas.drawLines(SK_ColorRED, boxes); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3134 | boxes = paragraph->getRectsForRange(13, 14, RectHeightStyle::kMax, RectWidthStyle::kTight); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3135 | canvas.drawLines(SK_ColorRED, boxes); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3136 | boxes = paragraph->getRectsForRange(18, 19, RectHeightStyle::kMax, RectWidthStyle::kTight); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3137 | canvas.drawLines(SK_ColorRED, boxes); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3138 | boxes = paragraph->getRectsForRange(19, 20, RectHeightStyle::kMax, RectWidthStyle::kTight); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3139 | canvas.drawLines(SK_ColorRED, boxes); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3140 | boxes = paragraph->getRectsForRange(24, 25, RectHeightStyle::kMax, RectWidthStyle::kTight); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3141 | canvas.drawLines(SK_ColorRED, boxes); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3142 | boxes = paragraph->getRectsForRange(25, 26, RectHeightStyle::kMax, RectWidthStyle::kTight); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3143 | canvas.drawLines(SK_ColorRED, boxes); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3144 | boxes = paragraph->getRectsForRange(30, 31, RectHeightStyle::kMax, RectWidthStyle::kTight); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3145 | canvas.drawLines(SK_ColorRED, boxes); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3146 | boxes = paragraph->getRectsForRange(31, 32, RectHeightStyle::kMax, RectWidthStyle::kTight); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3147 | canvas.drawLines(SK_ColorRED, boxes); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3148 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3149 | auto outLen = static_cast<ParagraphImpl*>(paragraph.get())->text().size(); |
| 3150 | 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] | 3151 | } |
| 3152 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3153 | // Checked: DIFF (unclear) |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3154 | DEF_TEST(SkParagraph_SpacingParagraph, reporter) { |
| 3155 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 3156 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3157 | TestCanvas canvas("SkParagraph_SpacingParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3158 | ParagraphStyle paragraph_style; |
| 3159 | paragraph_style.setMaxLines(10); |
| 3160 | paragraph_style.setTextAlign(TextAlign::kLeft); |
| 3161 | paragraph_style.turnHintingOff(); |
| 3162 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3163 | |
| 3164 | TextStyle text_style; |
| 3165 | text_style.setFontFamilies({SkString("Roboto")}); |
| 3166 | text_style.setFontSize(50); |
| 3167 | text_style.setLetterSpacing(20); |
| 3168 | text_style.setWordSpacing(0); |
| 3169 | text_style.setColor(SK_ColorBLACK); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3170 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3171 | builder.addText("H", 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3172 | builder.pop(); |
| 3173 | |
| 3174 | text_style.setLetterSpacing(10); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3175 | text_style.setWordSpacing(0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3176 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3177 | builder.addText("H", 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3178 | builder.pop(); |
| 3179 | |
| 3180 | text_style.setLetterSpacing(20); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3181 | text_style.setWordSpacing(0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3182 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3183 | builder.addText("H", 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3184 | builder.pop(); |
| 3185 | |
| 3186 | text_style.setLetterSpacing(0); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3187 | text_style.setWordSpacing(0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3188 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3189 | builder.addText("|", 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3190 | builder.pop(); |
| 3191 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3192 | const char* hSpace = "H "; |
| 3193 | const size_t len = strlen(hSpace); |
| 3194 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3195 | text_style.setLetterSpacing(0); |
| 3196 | text_style.setWordSpacing(20); |
| 3197 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3198 | builder.addText(hSpace, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3199 | builder.pop(); |
| 3200 | |
| 3201 | text_style.setLetterSpacing(0); |
| 3202 | text_style.setWordSpacing(0); |
| 3203 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3204 | builder.addText(hSpace, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3205 | builder.pop(); |
| 3206 | |
| 3207 | text_style.setLetterSpacing(0); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3208 | text_style.setLetterSpacing(0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3209 | text_style.setWordSpacing(20); |
| 3210 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3211 | builder.addText(hSpace, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3212 | builder.pop(); |
| 3213 | |
| 3214 | auto paragraph = builder.Build(); |
| 3215 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3216 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3217 | |
| 3218 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 3219 | REPORTER_ASSERT(reporter, impl->lines().size() == 1); |
| 3220 | size_t index = 0; |
| 3221 | impl->lines().begin()->scanStyles(StyleType::kLetterSpacing, |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 3222 | [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
| 3223 | ++index; |
| 3224 | return true; |
| 3225 | }); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3226 | REPORTER_ASSERT(reporter, index == 4); |
| 3227 | index = 0; |
| 3228 | impl->lines().begin()->scanStyles(StyleType::kWordSpacing, |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 3229 | [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
| 3230 | ++index; |
| 3231 | return true; |
| 3232 | }); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3233 | REPORTER_ASSERT(reporter, index == 4); |
| 3234 | } |
| 3235 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3236 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3237 | DEF_TEST(SkParagraph_LongWordParagraph, reporter) { |
| 3238 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 3239 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3240 | TestCanvas canvas("SkParagraph_LongWordParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3241 | const char* text = |
| 3242 | "A " |
| 3243 | "veryverylongwordtoseewherethiswillwraporifitwillatallandifitdoesthenthat" |
| 3244 | "wouldbeagoodthingbecausethebreakingisworking."; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3245 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3246 | |
| 3247 | ParagraphStyle paragraph_style; |
| 3248 | paragraph_style.turnHintingOff(); |
| 3249 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3250 | |
| 3251 | TextStyle text_style; |
| 3252 | text_style.setFontFamilies({SkString("Roboto")}); |
| 3253 | text_style.setColor(SK_ColorRED); |
| 3254 | text_style.setFontSize(31); |
| 3255 | text_style.setLetterSpacing(0); |
| 3256 | text_style.setWordSpacing(0); |
| 3257 | text_style.setColor(SK_ColorBLACK); |
| 3258 | text_style.setHeight(1); |
| 3259 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3260 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3261 | builder.pop(); |
| 3262 | |
| 3263 | auto paragraph = builder.Build(); |
| 3264 | paragraph->layout(TestCanvasWidth / 2); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3265 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3266 | |
| 3267 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 3268 | REPORTER_ASSERT(reporter, impl->text().size() == std::string{text}.length()); |
| 3269 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 3270 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 3271 | REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3272 | REPORTER_ASSERT(reporter, impl->lines().size() == 4); |
| 3273 | |
| 3274 | REPORTER_ASSERT(reporter, impl->lines()[0].width() > TestCanvasWidth / 2 - 20); |
| 3275 | REPORTER_ASSERT(reporter, impl->lines()[1].width() > TestCanvasWidth / 2 - 20); |
| 3276 | REPORTER_ASSERT(reporter, impl->lines()[2].width() > TestCanvasWidth / 2 - 20); |
| 3277 | } |
| 3278 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3279 | // Checked: DIFF? |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3280 | DEF_TEST(SkParagraph_KernScaleParagraph, reporter) { |
| 3281 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 3282 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3283 | TestCanvas canvas("SkParagraph_KernScaleParagraph.png"); |
| 3284 | |
| 3285 | const char* text1 = "AVAVAWAH A0 V0 VA To The Lo"; |
| 3286 | const char* text2 = " Dialog Text List lots of words to see " |
| 3287 | "if kerning works on a bigger set of characters AVAVAW"; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3288 | float scale = 3.0f; |
| 3289 | ParagraphStyle paragraph_style; |
| 3290 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3291 | TextStyle text_style; |
| 3292 | text_style.setFontFamilies({SkString("Droid Serif")}); |
| 3293 | text_style.setFontSize(100 / scale); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3294 | text_style.setColor(SK_ColorBLACK); |
| 3295 | |
| 3296 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3297 | builder.addText(text1, strlen(text1)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3298 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3299 | builder.addText("A", 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3300 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3301 | builder.addText("V", 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3302 | text_style.setFontSize(14 / scale); |
| 3303 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3304 | builder.addText(text2, strlen(text2)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3305 | builder.pop(); |
| 3306 | |
| 3307 | auto paragraph = builder.Build(); |
| 3308 | paragraph->layout(TestCanvasWidth / scale); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3309 | canvas.get()->scale(scale, scale); |
| 3310 | paragraph->paint(canvas.get(), 0, 0); |
| 3311 | canvas.get()->scale(1, 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3312 | |
| 3313 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3314 | |
Julia Lavrova | 6e6333f | 2019-06-17 10:34:10 -0400 | [diff] [blame] | 3315 | // 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] | 3316 | REPORTER_ASSERT(reporter, impl->lines().size() == 3); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3317 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].width(), 285.858f, EPSILON100)); |
| 3318 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[1].width(), 329.709f, EPSILON100)); |
| 3319 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[2].width(), 120.619f, EPSILON100)); |
| 3320 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].height(), 39.00f, EPSILON100)); |
| 3321 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[1].height(), 39.00f, EPSILON100)); |
| 3322 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[2].height(), 05.00f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3323 | } |
| 3324 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3325 | // Checked: DIFF+ |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3326 | DEF_TEST(SkParagraph_NewlineParagraph, reporter) { |
| 3327 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 3328 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3329 | TestCanvas canvas("SkParagraph_NewlineParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3330 | const char* text = |
| 3331 | "line1\nline2 test1 test2 test3 test4 test5 test6 test7\nline3\n\nline4 " |
| 3332 | "test1 test2 test3 test4"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3333 | const size_t len = strlen(text); |
| 3334 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3335 | ParagraphStyle paragraph_style; |
| 3336 | paragraph_style.turnHintingOff(); |
| 3337 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3338 | |
| 3339 | TextStyle text_style; |
| 3340 | text_style.setFontFamilies({SkString("Roboto")}); |
| 3341 | text_style.setColor(SK_ColorRED); |
| 3342 | text_style.setFontSize(60); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3343 | text_style.setColor(SK_ColorBLACK); |
| 3344 | text_style.setHeight(1); |
| 3345 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3346 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3347 | builder.pop(); |
| 3348 | |
| 3349 | auto paragraph = builder.Build(); |
| 3350 | paragraph->layout(TestCanvasWidth - 300); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3351 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3352 | |
| 3353 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 3354 | // Minikin does not count empty lines but SkParagraph does |
| 3355 | REPORTER_ASSERT(reporter, impl->lines().size() == 7); |
| 3356 | |
| 3357 | REPORTER_ASSERT(reporter, impl->lines()[0].offset().fY == 0); |
| 3358 | REPORTER_ASSERT(reporter, impl->lines()[1].offset().fY == 70); |
| 3359 | REPORTER_ASSERT(reporter, impl->lines()[2].offset().fY == 140); |
| 3360 | REPORTER_ASSERT(reporter, impl->lines()[3].offset().fY == 210); |
| 3361 | REPORTER_ASSERT(reporter, impl->lines()[4].offset().fY == 280); // Empty line |
| 3362 | REPORTER_ASSERT(reporter, impl->lines()[5].offset().fY == 350); |
| 3363 | REPORTER_ASSERT(reporter, impl->lines()[6].offset().fY == 420); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3364 | } |
| 3365 | |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 3366 | // TODO: Fix underline |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3367 | DEF_TEST(SkParagraph_EmojiParagraph, reporter) { |
| 3368 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 3369 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3370 | TestCanvas canvas("SkParagraph_EmojiParagraph.png"); |
| 3371 | const char* text = |
| 3372 | "😀😃😄😁😆😅😂🤣☺😇🙂😍😡😟😢😻👽💩👍👎🙏👌👋👄👁👦👼👨🚀👨🚒🙋♂️👳👨👨👧👧\ |
| 3373 | 💼👡👠☂🐶🐰🐻🐼🐷🐒🐵🐔🐧🐦🐋🐟🐡🕸🐌🐴🐊🐄🐪🐘🌸🌏🔥🌟🌚🌝💦💧\ |
| 3374 | ❄🍕🍔🍟🥝🍱🕶🎩🏈⚽🚴♀️🎻🎼🎹🚨🚎🚐⚓🛳🚀🚁🏪🏢🖱⏰📱💾💉📉🛏🔑🔓\ |
| 3375 | 📁🗓📊❤💯🚫🔻♠♣🕓❗🏳🏁🏳️🌈🇮🇹🇱🇷🇺🇸🇬🇧🇨🇳🇧🇴"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3376 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3377 | |
| 3378 | ParagraphStyle paragraph_style; |
| 3379 | paragraph_style.turnHintingOff(); |
| 3380 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3381 | |
| 3382 | TextStyle text_style; |
| 3383 | text_style.setFontFamilies({SkString("Noto Color Emoji")}); |
| 3384 | text_style.setFontSize(50); |
| 3385 | text_style.setDecoration(TextDecoration::kUnderline); |
| 3386 | text_style.setColor(SK_ColorBLACK); |
| 3387 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3388 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3389 | builder.pop(); |
| 3390 | |
| 3391 | auto paragraph = builder.Build(); |
| 3392 | paragraph->layout(TestCanvasWidth); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3393 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3394 | |
| 3395 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3396 | |
| 3397 | REPORTER_ASSERT(reporter, impl->lines().size() == 8); |
| 3398 | for (auto& line : impl->lines()) { |
| 3399 | if (&line != impl->lines().end() - 1) { |
| 3400 | REPORTER_ASSERT(reporter, line.width() == 998.25f); |
| 3401 | } else { |
| 3402 | REPORTER_ASSERT(reporter, line.width() < 998.25f); |
| 3403 | } |
| 3404 | REPORTER_ASSERT(reporter, line.height() == 59); |
| 3405 | } |
| 3406 | } |
| 3407 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3408 | // Checked: DIFF+ |
| 3409 | DEF_TEST(SkParagraph_EmojiMultiLineRectsParagraph, reporter) { |
| 3410 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 3411 | if (!fontCollection->fontsFound()) return; |
| 3412 | TestCanvas canvas("SkParagraph_EmojiMultiLineRectsParagraph.png"); |
| 3413 | const char* text = |
| 3414 | "👩👩👦👩👩👧👧🇺🇸👩👩👦👩👩👧👧i🇺🇸👩👩👦👩👩👧👧🇺🇸👩👩👦👩👩👧👧🇺🇸" |
| 3415 | "👩👩👦👩👩👧👧🇺🇸👩👩👦👩👩👧👧🇺🇸👩👩👦👩👩👧👧🇺🇸👩👩👦👩👩👧👧🇺🇸" |
| 3416 | "👩👩👦👩👩👧👧🇺🇸👩👩👦👩👩👧👧🇺🇸👩👩👦👩👩👧👧🇺🇸👩👩👦👩👩👧👧🇺🇸" |
| 3417 | "👩👩👦👩👩👧👧🇺🇸👩👩👦👩👩👧👧🇺🇸👩👩👦👩👩👧👧🇺🇸👩👩👦👩👩👧👧🇺🇸" |
| 3418 | "❄🍕🍔🍟🥝🍱🕶🎩🏈⚽🚴♀️🎻🎼🎹🚨🚎🚐⚓🛳🚀🚁🏪🏢🖱⏰📱💾💉📉🛏🔑🔓" |
| 3419 | "📁🗓📊❤💯🚫🔻♠♣🕓❗🏳🏁🏳️🌈🇮🇹🇱🇷🇺🇸🇬🇧🇨🇳🇧🇴"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3420 | const size_t len = strlen(text); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3421 | |
| 3422 | ParagraphStyle paragraph_style; |
| 3423 | paragraph_style.turnHintingOff(); |
| 3424 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3425 | |
| 3426 | TextStyle text_style; |
| 3427 | text_style.setFontFamilies({SkString("Noto Color Emoji")}); |
| 3428 | text_style.setFontSize(50); |
| 3429 | text_style.setColor(SK_ColorBLACK); |
| 3430 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3431 | builder.addText(text, len); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3432 | builder.pop(); |
| 3433 | |
| 3434 | auto paragraph = builder.Build(); |
| 3435 | paragraph->layout(TestCanvasWidth - 300); |
| 3436 | paragraph->paint(canvas.get(), 0, 0); |
| 3437 | |
| 3438 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 3439 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 3440 | |
| 3441 | auto result = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style); |
| 3442 | REPORTER_ASSERT(reporter, result.size() == 0); |
| 3443 | |
| 3444 | result = paragraph->getRectsForRange(0, 119, rect_height_style, rect_width_style); |
| 3445 | REPORTER_ASSERT(reporter, result.size() == 2); |
| 3446 | canvas.drawRects(SK_ColorRED, result); |
| 3447 | |
| 3448 | result = paragraph->getRectsForRange(122, 132, rect_height_style, rect_width_style); |
| 3449 | REPORTER_ASSERT(reporter, result.size() == 1); |
| 3450 | canvas.drawRects(SK_ColorBLUE, result); |
| 3451 | |
| 3452 | auto pos = paragraph->getGlyphPositionAtCoordinate(610, 100).position; |
| 3453 | result = paragraph->getRectsForRange(0, pos, rect_height_style, rect_width_style); |
| 3454 | REPORTER_ASSERT(reporter, result.size() == 2); |
| 3455 | canvas.drawRects(SK_ColorGREEN, result); |
| 3456 | |
| 3457 | pos = paragraph->getGlyphPositionAtCoordinate(580, 100).position; |
| 3458 | result = paragraph->getRectsForRange(0, pos, rect_height_style, rect_width_style); |
| 3459 | REPORTER_ASSERT(reporter, result.size() == 2); |
| 3460 | canvas.drawRects(SK_ColorGREEN, result); |
| 3461 | |
| 3462 | pos = paragraph->getGlyphPositionAtCoordinate(560, 100).position; |
| 3463 | result = paragraph->getRectsForRange(0, pos, rect_height_style, rect_width_style); |
| 3464 | REPORTER_ASSERT(reporter, result.size() == 2); |
| 3465 | canvas.drawRects(SK_ColorGREEN, result); |
| 3466 | } |
| 3467 | |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3468 | DEF_TEST(SkParagraph_HyphenBreakParagraph, reporter) { |
| 3469 | SkDebugf("Hyphens are not implemented, and will not be implemented soon.\n"); |
| 3470 | } |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3471 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3472 | // Checked: DIFF (line breaking) |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3473 | DEF_TEST(SkParagraph_RepeatLayoutParagraph, reporter) { |
| 3474 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 3475 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3476 | TestCanvas canvas("SkParagraph_RepeatLayoutParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3477 | const char* text = |
| 3478 | "Sentence to layout at diff widths to get diff line counts. short words " |
| 3479 | "short words short words short words short words short words short words " |
| 3480 | "short words short words short words short words short words short words " |
| 3481 | "end"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3482 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3483 | |
| 3484 | ParagraphStyle paragraph_style; |
| 3485 | paragraph_style.turnHintingOff(); |
| 3486 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3487 | |
| 3488 | TextStyle text_style; |
| 3489 | text_style.setFontFamilies({SkString("Roboto")}); |
| 3490 | text_style.setFontSize(31); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3491 | text_style.setColor(SK_ColorBLACK); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3492 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3493 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3494 | builder.pop(); |
| 3495 | |
| 3496 | auto paragraph = builder.Build(); |
| 3497 | paragraph->layout(300); |
| 3498 | |
| 3499 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 3500 | // Some of the formatting lazily done on paint |
| 3501 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 3502 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
| 3503 | REPORTER_ASSERT(reporter, impl->lines().size() == 12); |
| 3504 | |
| 3505 | paragraph->layout(600); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3506 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3507 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 3508 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
| 3509 | REPORTER_ASSERT(reporter, impl->lines().size() == 6); |
| 3510 | } |
| 3511 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3512 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3513 | DEF_TEST(SkParagraph_Ellipsize, reporter) { |
| 3514 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 3515 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3516 | TestCanvas canvas("SkParagraph_Ellipsize.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3517 | const char* text = |
| 3518 | "This is a very long sentence to test if the text will properly wrap " |
| 3519 | "around and go to the next line. Sometimes, short sentence. Longer " |
| 3520 | "sentences are okay too because they are nessecary. Very short. "; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3521 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3522 | |
| 3523 | ParagraphStyle paragraph_style; |
| 3524 | paragraph_style.setMaxLines(1); |
| 3525 | paragraph_style.setEllipsis(u"\u2026"); |
| 3526 | paragraph_style.turnHintingOff(); |
| 3527 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3528 | |
| 3529 | TextStyle text_style; |
| 3530 | text_style.setFontFamilies({SkString("Roboto")}); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3531 | text_style.setColor(SK_ColorBLACK); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3532 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3533 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3534 | builder.pop(); |
| 3535 | |
| 3536 | auto paragraph = builder.Build(); |
| 3537 | paragraph->layout(TestCanvasWidth); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3538 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3539 | |
| 3540 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3541 | |
| 3542 | // Check that the ellipsizer limited the text to one line and did not wrap to a second line. |
| 3543 | REPORTER_ASSERT(reporter, impl->lines().size() == 1); |
| 3544 | |
| 3545 | auto& line = impl->lines()[0]; |
| 3546 | REPORTER_ASSERT(reporter, line.ellipsis() != nullptr); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 3547 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3548 | } |
| 3549 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3550 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3551 | DEF_TEST(SkParagraph_UnderlineShiftParagraph, reporter) { |
| 3552 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 3553 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3554 | TestCanvas canvas("SkParagraph_UnderlineShiftParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3555 | const char* text1 = "fluttser "; |
| 3556 | const char* text2 = "mdje"; |
| 3557 | const char* text3 = "fluttser mdje"; |
| 3558 | |
| 3559 | ParagraphStyle paragraph_style; |
| 3560 | paragraph_style.turnHintingOff(); |
| 3561 | paragraph_style.setTextAlign(TextAlign::kLeft); |
| 3562 | paragraph_style.setMaxLines(2); |
| 3563 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3564 | |
| 3565 | TextStyle text_style; |
| 3566 | text_style.setFontFamilies({SkString("Roboto")}); |
| 3567 | text_style.setColor(SK_ColorBLACK); |
| 3568 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3569 | builder.addText(text1, strlen(text1)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3570 | text_style.setDecoration(TextDecoration::kUnderline); |
| 3571 | text_style.setDecorationColor(SK_ColorBLACK); |
| 3572 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3573 | builder.addText(text2, strlen(text2)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3574 | builder.pop(); |
| 3575 | |
| 3576 | auto paragraph = builder.Build(); |
| 3577 | paragraph->layout(TestCanvasWidth); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3578 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3579 | |
| 3580 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 3581 | |
| 3582 | ParagraphBuilderImpl builder1(paragraph_style, fontCollection); |
| 3583 | text_style.setDecoration(TextDecoration::kNoDecoration); |
| 3584 | builder1.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3585 | builder1.addText(text3, strlen(text3)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3586 | builder1.pop(); |
| 3587 | |
| 3588 | auto paragraph1 = builder1.Build(); |
| 3589 | paragraph1->layout(TestCanvasWidth); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3590 | paragraph1->paint(canvas.get(), 0, 25); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3591 | |
| 3592 | auto impl1 = static_cast<ParagraphImpl*>(paragraph1.get()); |
| 3593 | |
| 3594 | REPORTER_ASSERT(reporter, impl->lines().size() == 1); |
| 3595 | REPORTER_ASSERT(reporter, impl1->lines().size() == 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3596 | |
| 3597 | auto rect = paragraph->getRectsForRange(0, 12, RectHeightStyle::kMax, RectWidthStyle::kTight) |
| 3598 | .front() |
| 3599 | .rect; |
| 3600 | auto rect1 = paragraph1->getRectsForRange(0, 12, RectHeightStyle::kMax, RectWidthStyle::kTight) |
| 3601 | .front() |
| 3602 | .rect; |
| 3603 | REPORTER_ASSERT(reporter, rect.fLeft == rect1.fLeft); |
| 3604 | REPORTER_ASSERT(reporter, rect.fRight == rect1.fRight); |
| 3605 | |
| 3606 | for (size_t i = 0; i < 12; ++i) { |
| 3607 | auto r = |
| 3608 | paragraph->getRectsForRange(i, i + 1, RectHeightStyle::kMax, RectWidthStyle::kTight) |
| 3609 | .front() |
| 3610 | .rect; |
| 3611 | auto r1 = |
| 3612 | paragraph1 |
| 3613 | ->getRectsForRange(i, i + 1, RectHeightStyle::kMax, RectWidthStyle::kTight) |
| 3614 | .front() |
| 3615 | .rect; |
| 3616 | |
| 3617 | REPORTER_ASSERT(reporter, r.fLeft == r1.fLeft); |
| 3618 | REPORTER_ASSERT(reporter, r.fRight == r1.fRight); |
| 3619 | } |
| 3620 | } |
| 3621 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3622 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3623 | DEF_TEST(SkParagraph_SimpleShadow, reporter) { |
| 3624 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 3625 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3626 | TestCanvas canvas("SkParagraph_SimpleShadow.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3627 | const char* text = "Hello World Text Dialog"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3628 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3629 | |
| 3630 | ParagraphStyle paragraph_style; |
| 3631 | paragraph_style.turnHintingOff(); |
| 3632 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3633 | |
| 3634 | TextStyle text_style; |
| 3635 | text_style.setFontFamilies({SkString("Roboto")}); |
| 3636 | text_style.setColor(SK_ColorBLACK); |
| 3637 | text_style.addShadow(TextShadow(SK_ColorBLACK, SkPoint::Make(2.0f, 2.0f), 1.0)); |
| 3638 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3639 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3640 | |
| 3641 | auto paragraph = builder.Build(); |
| 3642 | paragraph->layout(TestCanvasWidth); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3643 | paragraph->paint(canvas.get(), 10.0, 15.0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3644 | |
| 3645 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 3646 | |
| 3647 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 3648 | REPORTER_ASSERT(reporter, impl->styles().size() == 1); |
| 3649 | size_t index = 0; |
| 3650 | for (auto& line : impl->lines()) { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3651 | line.scanStyles(StyleType::kShadow, |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 3652 | [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3653 | REPORTER_ASSERT(reporter, index == 0 && style.equals(text_style)); |
| 3654 | ++index; |
| 3655 | return true; |
| 3656 | }); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3657 | } |
| 3658 | } |
| 3659 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3660 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3661 | DEF_TEST(SkParagraph_ComplexShadow, reporter) { |
| 3662 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 3663 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3664 | TestCanvas canvas("SkParagraph_ComplexShadow.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3665 | const char* text = "Text Chunk "; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3666 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3667 | |
| 3668 | ParagraphStyle paragraph_style; |
| 3669 | paragraph_style.turnHintingOff(); |
| 3670 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3671 | |
| 3672 | TextStyle text_style; |
| 3673 | text_style.setFontFamilies({SkString("Roboto")}); |
| 3674 | text_style.setColor(SK_ColorBLACK); |
| 3675 | text_style.addShadow(TextShadow(SK_ColorBLACK, SkPoint::Make(2.0f, 2.0f), 1.0f)); |
| 3676 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3677 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3678 | |
| 3679 | text_style.addShadow(TextShadow(SK_ColorRED, SkPoint::Make(2.0f, 2.0f), 5.0f)); |
| 3680 | text_style.addShadow(TextShadow(SK_ColorGREEN, SkPoint::Make(10.0f, -5.0f), 3.0f)); |
| 3681 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3682 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3683 | builder.pop(); |
| 3684 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3685 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3686 | |
| 3687 | text_style.addShadow(TextShadow(SK_ColorRED, SkPoint::Make(0.0f, 1.0f), 0.0f)); |
| 3688 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3689 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3690 | builder.pop(); |
| 3691 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3692 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3693 | |
| 3694 | auto paragraph = builder.Build(); |
| 3695 | paragraph->layout(TestCanvasWidth); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3696 | paragraph->paint(canvas.get(), 10.0, 15.0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3697 | |
| 3698 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 3699 | |
| 3700 | size_t index = 0; |
| 3701 | for (auto& line : impl->lines()) { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3702 | line.scanStyles(StyleType::kShadow, |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 3703 | [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3704 | ++index; |
| 3705 | switch (index) { |
| 3706 | case 1: |
| 3707 | REPORTER_ASSERT(reporter, style.getShadowNumber() == 1); |
| 3708 | break; |
| 3709 | case 2: |
| 3710 | REPORTER_ASSERT(reporter, style.getShadowNumber() == 3); |
| 3711 | break; |
| 3712 | case 3: |
| 3713 | REPORTER_ASSERT(reporter, style.getShadowNumber() == 1); |
| 3714 | break; |
| 3715 | case 4: |
| 3716 | REPORTER_ASSERT(reporter, style.getShadowNumber() == 4); |
| 3717 | REPORTER_ASSERT(reporter, style.equals(text_style)); |
| 3718 | break; |
| 3719 | case 5: |
| 3720 | REPORTER_ASSERT(reporter, style.getShadowNumber() == 1); |
| 3721 | break; |
| 3722 | default: |
| 3723 | REPORTER_ASSERT(reporter, false); |
| 3724 | } |
| 3725 | return true; |
| 3726 | }); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3727 | } |
| 3728 | } |
| 3729 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3730 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3731 | DEF_TEST(SkParagraph_BaselineParagraph, reporter) { |
| 3732 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 3733 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3734 | TestCanvas canvas("SkParagraph_BaselineParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3735 | const char* text = |
| 3736 | "左線読設Byg後碁給能上目秘使約。満毎冠行来昼本可必図将発確年。今属場育" |
| 3737 | "図情闘陰野高備込制詩西校客。審対江置講今固残必託地集済決維駆年策。立得"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3738 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3739 | |
| 3740 | ParagraphStyle paragraph_style; |
| 3741 | paragraph_style.turnHintingOff(); |
| 3742 | paragraph_style.setMaxLines(14); |
| 3743 | paragraph_style.setTextAlign(TextAlign::kJustify); |
| 3744 | paragraph_style.setHeight(1.5); |
| 3745 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3746 | |
| 3747 | TextStyle text_style; |
| 3748 | text_style.setFontFamilies({SkString("Source Han Serif CN")}); |
| 3749 | text_style.setColor(SK_ColorBLACK); |
| 3750 | text_style.setFontSize(55); |
| 3751 | text_style.setLetterSpacing(2); |
| 3752 | text_style.setDecorationStyle(TextDecorationStyle::kSolid); |
| 3753 | text_style.setDecorationColor(SK_ColorBLACK); |
| 3754 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3755 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3756 | builder.pop(); |
| 3757 | |
| 3758 | auto paragraph = builder.Build(); |
| 3759 | paragraph->layout(TestCanvasWidth - 100); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3760 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3761 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3762 | SkRect rect1 = SkRect::MakeXYWH(0, paragraph->getIdeographicBaseline(), |
| 3763 | paragraph->getMaxWidth(), |
| 3764 | paragraph->getIdeographicBaseline()); |
| 3765 | SkRect rect2 = SkRect::MakeXYWH(0, paragraph->getAlphabeticBaseline(), |
| 3766 | paragraph->getMaxWidth(), |
| 3767 | paragraph->getAlphabeticBaseline()); |
| 3768 | canvas.drawLine(SK_ColorRED, rect1, false); |
| 3769 | canvas.drawLine(SK_ColorGREEN, rect2, false); |
| 3770 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3771 | REPORTER_ASSERT(reporter, |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3772 | SkScalarNearlyEqual(paragraph->getIdeographicBaseline(), 79.035f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3773 | REPORTER_ASSERT(reporter, |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3774 | SkScalarNearlyEqual(paragraph->getAlphabeticBaseline(), 63.305f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3775 | } |
| 3776 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3777 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3778 | DEF_TEST(SkParagraph_FontFallbackParagraph, reporter) { |
| 3779 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 3780 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3781 | TestCanvas canvas("SkParagraph_FontFallbackParagraph.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3782 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3783 | const char* text1 = "Roboto 字典 "; // Roboto + unresolved |
| 3784 | const char* text2 = "Homemade Apple 字典"; // Homemade Apple + Noto Sans... |
| 3785 | const char* text3 = "Chinese 字典"; // Homemade Apple + Source Han |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3786 | |
| 3787 | ParagraphStyle paragraph_style; |
| 3788 | paragraph_style.turnHintingOff(); |
| 3789 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3790 | |
| 3791 | TextStyle text_style; |
| 3792 | text_style.setFontFamilies({ |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3793 | SkString("Not a real font"), |
| 3794 | SkString("Also a fake font"), |
| 3795 | SkString("So fake it is obvious"), |
| 3796 | SkString("Next one should be a real font..."), |
| 3797 | SkString("Roboto"), |
| 3798 | SkString("another fake one in between"), |
| 3799 | SkString("Homemade Apple"), |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3800 | }); |
| 3801 | text_style.setColor(SK_ColorBLACK); |
| 3802 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3803 | builder.addText(text1, strlen(text1)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3804 | |
| 3805 | text_style.setFontFamilies({ |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3806 | SkString("Not a real font"), |
| 3807 | SkString("Also a fake font"), |
| 3808 | SkString("So fake it is obvious"), |
| 3809 | SkString("Homemade Apple"), |
| 3810 | SkString("Next one should be a real font..."), |
| 3811 | SkString("Roboto"), |
| 3812 | SkString("another fake one in between"), |
| 3813 | SkString("Noto Sans CJK JP"), |
| 3814 | SkString("Source Han Serif CN"), |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3815 | }); |
| 3816 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3817 | builder.addText(text2, strlen(text2)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3818 | |
| 3819 | text_style.setFontFamilies({ |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3820 | SkString("Not a real font"), |
| 3821 | SkString("Also a fake font"), |
| 3822 | SkString("So fake it is obvious"), |
| 3823 | SkString("Homemade Apple"), |
| 3824 | SkString("Next one should be a real font..."), |
| 3825 | SkString("Roboto"), |
| 3826 | SkString("another fake one in between"), |
| 3827 | SkString("Source Han Serif CN"), |
| 3828 | SkString("Noto Sans CJK JP"), |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3829 | }); |
| 3830 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3831 | builder.addText(text3, strlen(text3)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3832 | |
| 3833 | builder.pop(); |
| 3834 | |
| 3835 | auto paragraph = builder.Build(); |
| 3836 | paragraph->layout(TestCanvasWidth); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3837 | paragraph->paint(canvas.get(), 10.0, 15.0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3838 | |
| 3839 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 3840 | |
| 3841 | // Font resolution in Skia produces 6 runs because 2 parts of "Roboto 字典 " have different |
| 3842 | // script (Minikin merges the first 2 into one because of unresolved) [Apple + Unresolved ] |
| 3843 | // [Apple + Noto] [Apple + Han] |
| 3844 | REPORTER_ASSERT(reporter, impl->runs().size() == 6); |
| 3845 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3846 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->runs()[0].advance().fX, 48.330f, EPSILON100)); |
| 3847 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->runs()[1].advance().fX, 15.879f, EPSILON100)); |
| 3848 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->runs()[2].advance().fX, 139.125f, EPSILON100)); |
| 3849 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->runs()[3].advance().fX, 27.999f, EPSILON100)); |
| 3850 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->runs()[4].advance().fX, 62.248f, EPSILON100)); |
| 3851 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->runs()[5].advance().fX, 27.999f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3852 | |
| 3853 | // When a different font is resolved, then the metrics are different. |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3854 | REPORTER_ASSERT(reporter, impl->runs()[1].correctAscent() != impl->runs()[3].correctAscent()); |
| 3855 | REPORTER_ASSERT(reporter, impl->runs()[1].correctDescent() != impl->runs()[3].correctDescent()); |
| 3856 | REPORTER_ASSERT(reporter, impl->runs()[3].correctAscent() != impl->runs()[5].correctAscent()); |
| 3857 | REPORTER_ASSERT(reporter, impl->runs()[3].correctDescent() != impl->runs()[5].correctDescent()); |
| 3858 | REPORTER_ASSERT(reporter, impl->runs()[1].correctAscent() != impl->runs()[5].correctAscent()); |
| 3859 | REPORTER_ASSERT(reporter, impl->runs()[1].correctDescent() != impl->runs()[5].correctDescent()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3860 | } |
| 3861 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3862 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3863 | DEF_TEST(SkParagraph_StrutParagraph1, reporter) { |
| 3864 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 3865 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3866 | TestCanvas canvas("SkParagraph_StrutParagraph1.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3867 | // The chinese extra height should be absorbed by the strut. |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3868 | const char* text = "01234満毎冠p来É本可\nabcd\n満毎É行p昼本可"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3869 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3870 | |
| 3871 | ParagraphStyle paragraph_style; |
| 3872 | paragraph_style.setMaxLines(10); |
| 3873 | paragraph_style.setTextAlign(TextAlign::kLeft); |
| 3874 | paragraph_style.turnHintingOff(); |
| 3875 | |
| 3876 | StrutStyle strut_style; |
| 3877 | strut_style.setStrutEnabled(true); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3878 | strut_style.setFontFamilies({SkString("BlahFake"), SkString("Ahem")}); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3879 | strut_style.setFontSize(50); |
| 3880 | strut_style.setHeight(1.8f); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3881 | strut_style.setHeightOverride(true); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3882 | strut_style.setLeading(0.1f); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3883 | paragraph_style.setStrutStyle(strut_style); |
| 3884 | |
| 3885 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3886 | |
| 3887 | TextStyle text_style; |
| 3888 | text_style.setFontFamilies({SkString("Ahem")}); |
| 3889 | text_style.setFontSize(50); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3890 | 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] | 3891 | text_style.setColor(SK_ColorBLACK); |
| 3892 | text_style.setHeight(0.5f); |
| 3893 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3894 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3895 | builder.pop(); |
| 3896 | |
| 3897 | auto paragraph = builder.Build(); |
| 3898 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3899 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3900 | |
| 3901 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3902 | REPORTER_ASSERT(reporter, impl->lines().size() == 4); |
| 3903 | |
| 3904 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 3905 | RectHeightStyle rect_height_max_style = RectHeightStyle::kMax; |
| 3906 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3907 | { |
| 3908 | auto boxes = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style); |
| 3909 | REPORTER_ASSERT(reporter, boxes.empty()); |
| 3910 | } |
| 3911 | { |
| 3912 | auto boxes = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3913 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3914 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3915 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, EPSILON100)); |
| 3916 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 34.5f, EPSILON100)); |
| 3917 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, EPSILON100)); |
| 3918 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 84.5f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3919 | } |
| 3920 | { |
| 3921 | 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] | 3922 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3923 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3924 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, EPSILON100)); |
| 3925 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 34.5f, EPSILON100)); |
| 3926 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, EPSILON100)); |
| 3927 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 95, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3928 | } |
| 3929 | { |
| 3930 | auto boxes = paragraph->getRectsForRange(6, 10, rect_height_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3931 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3932 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3933 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, EPSILON100)); |
| 3934 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 34.5f, EPSILON100)); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3935 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3936 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 84.5f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3937 | } |
| 3938 | { |
| 3939 | 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] | 3940 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3941 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3942 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, EPSILON100)); |
| 3943 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 34.5f, EPSILON100)); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3944 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3945 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 95, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3946 | } |
| 3947 | { |
| 3948 | 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] | 3949 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3950 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3951 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, EPSILON100)); |
| 3952 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 224.5f, EPSILON100)); |
| 3953 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 100, EPSILON100)); |
| 3954 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 285, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3955 | } |
| 3956 | { |
| 3957 | 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] | 3958 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3959 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3960 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 50, EPSILON100)); |
| 3961 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 319.5f, EPSILON100)); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 3962 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 300, EPSILON100)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3963 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 380, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3964 | } |
| 3965 | } |
| 3966 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3967 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3968 | DEF_TEST(SkParagraph_StrutParagraph2, reporter) { |
| 3969 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 3970 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 3971 | TestCanvas canvas("SkParagraph_StrutParagraph2.png"); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3972 | // The chinese extra height should be absorbed by the strut. |
| 3973 | const char* text = "01234ABCDEFGH\nabcd\nABCDEFGH"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 3974 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3975 | |
| 3976 | ParagraphStyle paragraph_style; |
| 3977 | paragraph_style.setMaxLines(10); |
| 3978 | paragraph_style.setTextAlign(TextAlign::kLeft); |
| 3979 | paragraph_style.turnHintingOff(); |
| 3980 | |
| 3981 | StrutStyle strut_style; |
| 3982 | |
| 3983 | strut_style.setStrutEnabled(true); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3984 | strut_style.setFontFamilies({SkString("Ahem")}); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3985 | strut_style.setFontSize(50); |
| 3986 | strut_style.setHeight(1.6f); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3987 | strut_style.setHeightOverride(true); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3988 | paragraph_style.setStrutStyle(strut_style); |
| 3989 | |
| 3990 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 3991 | |
| 3992 | TextStyle text_style; |
| 3993 | text_style.setFontFamilies({SkString("Ahem")}); |
| 3994 | text_style.setFontSize(50); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 3995 | text_style.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 3996 | SkFontStyle::kUpright_Slant)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 3997 | text_style.setColor(SK_ColorBLACK); |
| 3998 | text_style.setHeight(1); |
| 3999 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4000 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4001 | builder.pop(); |
| 4002 | |
| 4003 | auto paragraph = builder.Build(); |
| 4004 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4005 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4006 | |
| 4007 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 4008 | // Font is not resolved and the first line does not fit |
| 4009 | REPORTER_ASSERT(reporter, impl->lines().size() == 4); |
| 4010 | |
| 4011 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 4012 | RectHeightStyle rect_height_max_style = RectHeightStyle::kMax; |
| 4013 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4014 | { |
| 4015 | auto boxes = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style); |
| 4016 | REPORTER_ASSERT(reporter, boxes.empty()); |
| 4017 | } |
| 4018 | { |
| 4019 | auto boxes = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4020 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4021 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4022 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, EPSILON100)); |
| 4023 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 24, EPSILON100)); |
| 4024 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, EPSILON100)); |
| 4025 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 74, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4026 | } |
| 4027 | { |
| 4028 | 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] | 4029 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4030 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4031 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, EPSILON100)); |
| 4032 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 24, EPSILON100)); |
| 4033 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, EPSILON100)); |
| 4034 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 80, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4035 | } |
| 4036 | { |
| 4037 | auto boxes = paragraph->getRectsForRange(6, 10, rect_height_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4038 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4039 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4040 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, EPSILON100)); |
| 4041 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 24, EPSILON100)); |
| 4042 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, EPSILON100)); |
| 4043 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 74, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4044 | } |
| 4045 | { |
| 4046 | 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] | 4047 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4048 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4049 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, EPSILON100)); |
| 4050 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 24, EPSILON100)); |
| 4051 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, EPSILON100)); |
| 4052 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 80, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4053 | } |
| 4054 | { |
| 4055 | 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] | 4056 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4057 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4058 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, EPSILON100)); |
| 4059 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 184, EPSILON100)); |
| 4060 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 100, EPSILON100)); |
| 4061 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 240, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4062 | } |
| 4063 | { |
| 4064 | 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] | 4065 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4066 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4067 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 50, EPSILON100)); |
| 4068 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 264, EPSILON100)); |
| 4069 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 300, EPSILON100)); |
| 4070 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 320, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4071 | } |
| 4072 | } |
| 4073 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4074 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4075 | DEF_TEST(SkParagraph_StrutParagraph3, reporter) { |
| 4076 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 4077 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4078 | TestCanvas canvas("SkParagraph_StrutParagraph3.png"); |
| 4079 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4080 | // The chinese extra height should be absorbed by the strut. |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4081 | const char* text = "01234満毎p行来昼本可\nabcd\n満毎冠行来昼本可"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4082 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4083 | |
| 4084 | ParagraphStyle paragraph_style; |
| 4085 | paragraph_style.setMaxLines(10); |
| 4086 | paragraph_style.setTextAlign(TextAlign::kLeft); |
| 4087 | paragraph_style.turnHintingOff(); |
| 4088 | |
| 4089 | StrutStyle strut_style; |
| 4090 | strut_style.setStrutEnabled(true); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4091 | strut_style.setFontFamilies({SkString("Ahem")}); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4092 | strut_style.setFontSize(50); |
| 4093 | strut_style.setHeight(1.2f); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4094 | strut_style.setHeightOverride(true); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4095 | paragraph_style.setStrutStyle(strut_style); |
| 4096 | |
| 4097 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4098 | |
| 4099 | TextStyle text_style; |
| 4100 | text_style.setFontFamilies({SkString("Ahem")}); |
| 4101 | text_style.setFontSize(50); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4102 | text_style.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 4103 | SkFontStyle::kUpright_Slant)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4104 | text_style.setColor(SK_ColorBLACK); |
| 4105 | text_style.setHeight(1); |
| 4106 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4107 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4108 | builder.pop(); |
| 4109 | |
| 4110 | auto paragraph = builder.Build(); |
| 4111 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4112 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4113 | |
| 4114 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 4115 | // Font is not resolved and the first line does not fit |
| 4116 | REPORTER_ASSERT(reporter, impl->lines().size() == 4); |
| 4117 | |
| 4118 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 4119 | RectHeightStyle rect_height_max_style = RectHeightStyle::kMax; |
| 4120 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 4121 | SkScalar epsilon = 0.001f; |
| 4122 | { |
| 4123 | auto boxes = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style); |
| 4124 | REPORTER_ASSERT(reporter, boxes.empty()); |
| 4125 | } |
| 4126 | { |
| 4127 | auto boxes = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4128 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4129 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 4130 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, epsilon)); |
| 4131 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 8, epsilon)); |
| 4132 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, epsilon)); |
| 4133 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 58, epsilon)); |
| 4134 | } |
| 4135 | { |
| 4136 | 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] | 4137 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4138 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 4139 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, epsilon)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4140 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 8, epsilon)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4141 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, epsilon)); |
| 4142 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 60, epsilon)); |
| 4143 | } |
| 4144 | { |
| 4145 | auto boxes = paragraph->getRectsForRange(6, 10, rect_height_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4146 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4147 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4148 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, epsilon)); |
| 4149 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 8, epsilon)); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4150 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, epsilon)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4151 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 58, epsilon)); |
| 4152 | } |
| 4153 | { |
| 4154 | 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] | 4155 | canvas.drawRects(SK_ColorRED, boxes); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4156 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4157 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, epsilon)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4158 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 8, epsilon)); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4159 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, epsilon)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4160 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 60, epsilon)); |
| 4161 | } |
| 4162 | { |
| 4163 | 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] | 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); |
| 4166 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, epsilon)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4167 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 128, epsilon)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4168 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 100, epsilon)); |
| 4169 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 180, epsilon)); |
| 4170 | } |
| 4171 | { |
| 4172 | 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] | 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); |
| 4175 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 50, epsilon)); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4176 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 188, epsilon)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4177 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 300, epsilon)); |
| 4178 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 240, epsilon)); |
| 4179 | } |
| 4180 | } |
| 4181 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4182 | // Checked: NO DIFF |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4183 | DEF_TEST(SkParagraph_StrutForceParagraph, reporter) { |
| 4184 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 4185 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4186 | TestCanvas canvas("SkParagraph_StrutForceParagraph.png"); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4187 | const char* text = "01234満毎冠行来昼本可\nabcd\n満毎冠行来昼本可"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4188 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4189 | |
| 4190 | ParagraphStyle paragraph_style; |
| 4191 | paragraph_style.setMaxLines(10); |
| 4192 | paragraph_style.setTextAlign(TextAlign::kLeft); |
| 4193 | paragraph_style.turnHintingOff(); |
| 4194 | |
| 4195 | StrutStyle strut_style; |
| 4196 | strut_style.setStrutEnabled(true); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4197 | strut_style.setFontFamilies({SkString("Ahem")}); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4198 | strut_style.setFontSize(50); |
| 4199 | strut_style.setHeight(1.5f); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4200 | strut_style.setHeightOverride(true); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4201 | strut_style.setLeading(0.1f); |
| 4202 | strut_style.setForceStrutHeight(true); |
| 4203 | paragraph_style.setStrutStyle(strut_style); |
| 4204 | |
| 4205 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4206 | |
| 4207 | TextStyle text_style; |
| 4208 | text_style.setFontFamilies({SkString("Ahem")}); |
| 4209 | text_style.setFontSize(50); |
| 4210 | text_style.setLetterSpacing(0); |
| 4211 | text_style.setColor(SK_ColorBLACK); |
| 4212 | text_style.setHeight(1); |
| 4213 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4214 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4215 | builder.pop(); |
| 4216 | |
| 4217 | auto paragraph = builder.Build(); |
| 4218 | paragraph->layout(550); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4219 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4220 | |
| 4221 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 4222 | // Font is not resolved and the first line does not fit |
| 4223 | REPORTER_ASSERT(reporter, impl->lines().size() == 4); |
| 4224 | |
| 4225 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 4226 | RectHeightStyle rect_height_max_style = RectHeightStyle::kMax; |
| 4227 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4228 | |
| 4229 | auto boxes1 = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style); |
| 4230 | REPORTER_ASSERT(reporter, boxes1.empty()); |
| 4231 | |
| 4232 | auto boxes2 = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4233 | canvas.drawRects(SK_ColorRED, boxes2); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4234 | REPORTER_ASSERT(reporter, boxes2.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4235 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes2[0].rect.left(), 0, EPSILON100)); |
| 4236 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes2[0].rect.top(), 22.5f, EPSILON100)); |
| 4237 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes2[0].rect.right(), 50, EPSILON100)); |
| 4238 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes2[0].rect.bottom(), 72.5f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4239 | |
| 4240 | 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] | 4241 | canvas.drawRects(SK_ColorRED, boxes3); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4242 | REPORTER_ASSERT(reporter, boxes3.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4243 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes3[0].rect.left(), 0, EPSILON100)); |
| 4244 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes3[0].rect.top(), 22.5f, EPSILON100)); |
| 4245 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes3[0].rect.right(), 50, EPSILON100)); |
| 4246 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes3[0].rect.bottom(), 80, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4247 | |
| 4248 | auto boxes4 = paragraph->getRectsForRange(6, 10, rect_height_style, rect_width_style); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4249 | canvas.drawRects(SK_ColorRED, boxes4); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4250 | REPORTER_ASSERT(reporter, boxes4.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4251 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes4[0].rect.left(), 300, EPSILON100)); |
| 4252 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes4[0].rect.top(), 22.5f, EPSILON100)); |
| 4253 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes4[0].rect.right(), 500, EPSILON100)); |
| 4254 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes4[0].rect.bottom(), 72.5f, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4255 | |
| 4256 | 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] | 4257 | canvas.drawRects(SK_ColorRED, boxes5); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4258 | REPORTER_ASSERT(reporter, boxes5.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4259 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes5[0].rect.left(), 300, EPSILON100)); |
| 4260 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes5[0].rect.top(), 22.5f, EPSILON100)); |
| 4261 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes5[0].rect.right(), 500, EPSILON100)); |
| 4262 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes5[0].rect.bottom(), 80, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4263 | |
| 4264 | 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] | 4265 | canvas.drawRects(SK_ColorRED, boxes6); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4266 | REPORTER_ASSERT(reporter, boxes6.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4267 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes6[0].rect.left(), 0, EPSILON100)); |
| 4268 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes6[0].rect.top(), 182.5f, EPSILON100)); |
| 4269 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes6[0].rect.right(), 100, EPSILON100)); |
| 4270 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes6[0].rect.bottom(), 240, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4271 | |
| 4272 | 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] | 4273 | canvas.drawRects(SK_ColorRED, boxes7); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4274 | REPORTER_ASSERT(reporter, boxes7.size() == 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4275 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes7[0].rect.left(), 50, EPSILON100)); |
| 4276 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes7[0].rect.top(), 262.5f, EPSILON100)); |
| 4277 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes7[0].rect.right(), 300, EPSILON100)); |
| 4278 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes7[0].rect.bottom(), 320, EPSILON100)); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4279 | } |
| 4280 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4281 | // Checked: NO DIFF |
| 4282 | DEF_TEST(SkParagraph_StrutDefaultParagraph, reporter) { |
| 4283 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 4284 | if (!fontCollection->fontsFound()) return; |
| 4285 | TestCanvas canvas("SkParagraph_StrutDefaultParagraph.png"); |
| 4286 | |
| 4287 | const char* text = "01234満毎冠行来昼本可\nabcd\n満毎冠行来昼本可"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4288 | const size_t len = strlen(text); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4289 | |
| 4290 | ParagraphStyle paragraph_style; |
| 4291 | paragraph_style.setMaxLines(10); |
| 4292 | paragraph_style.setTextAlign(TextAlign::kLeft); |
| 4293 | paragraph_style.turnHintingOff(); |
| 4294 | |
| 4295 | StrutStyle strut_style; |
| 4296 | strut_style.setStrutEnabled(true); |
| 4297 | strut_style.setFontFamilies({SkString("Ahem")}); |
| 4298 | strut_style.setFontSize(50); |
| 4299 | strut_style.setHeight(1.5f); |
| 4300 | strut_style.setLeading(0.1f); |
| 4301 | strut_style.setForceStrutHeight(false); |
| 4302 | paragraph_style.setStrutStyle(strut_style); |
| 4303 | |
| 4304 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4305 | |
| 4306 | TextStyle text_style; |
| 4307 | text_style.setFontFamilies({SkString("Ahem")}); |
| 4308 | text_style.setFontSize(20); |
| 4309 | text_style.setColor(SK_ColorBLACK); |
| 4310 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4311 | builder.addText(text, len); |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4312 | builder.pop(); |
| 4313 | |
| 4314 | auto paragraph = builder.Build(); |
| 4315 | paragraph->layout(550); |
| 4316 | paragraph->paint(canvas.get(), 0, 0); |
| 4317 | |
| 4318 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 4319 | RectHeightStyle rect_height_strut_style = RectHeightStyle::kStrut; |
| 4320 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 4321 | { |
| 4322 | auto boxes = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style); |
| 4323 | REPORTER_ASSERT(reporter, boxes.empty()); |
| 4324 | } |
| 4325 | { |
| 4326 | auto boxes = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style); |
| 4327 | canvas.drawRects(SK_ColorRED, boxes); |
| 4328 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 4329 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, EPSILON100)); |
| 4330 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 26.5f, EPSILON100)); |
| 4331 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 20, EPSILON100)); |
| 4332 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 46.5f, EPSILON100)); |
| 4333 | } |
| 4334 | { |
| 4335 | auto boxes = paragraph->getRectsForRange(0, 2, rect_height_strut_style, rect_width_style); |
| 4336 | canvas.drawRects(SK_ColorRED, boxes); |
| 4337 | REPORTER_ASSERT(reporter, boxes.size() == 1); |
| 4338 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, EPSILON100)); |
| 4339 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 2.5f, EPSILON100)); |
| 4340 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 40, EPSILON100)); |
| 4341 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 52.5f, EPSILON100)); |
| 4342 | } |
| 4343 | } |
| 4344 | |
| 4345 | // TODO: Implement font features |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 4346 | DEF_TEST(SkParagraph_FontFeaturesParagraph, reporter) { |
| 4347 | SkDebugf("TODO: Font features\n"); |
| 4348 | } |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4349 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4350 | // Not in Minikin |
| 4351 | DEF_TEST(SkParagraph_WhitespacesInMultipleFonts, reporter) { |
| 4352 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 4353 | if (!fontCollection->fontsFound()) return; |
| 4354 | const char* text = "English English 字典 字典 😀😃😄 😀😃😄"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4355 | const size_t len = strlen(text); |
| 4356 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4357 | ParagraphStyle paragraph_style; |
| 4358 | paragraph_style.turnHintingOff(); |
| 4359 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4360 | |
| 4361 | TextStyle text_style; |
| 4362 | text_style.setFontFamilies( |
| 4363 | {SkString("Roboto"), SkString("Noto Color Emoji"), SkString("Source Han Serif CN")}); |
| 4364 | text_style.setFontSize(60); |
| 4365 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4366 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4367 | builder.pop(); |
| 4368 | |
| 4369 | auto paragraph = builder.Build(); |
| 4370 | paragraph->layout(TestCanvasWidth); |
| 4371 | |
| 4372 | SkDEBUGCODE(auto impl = static_cast<ParagraphImpl*>(paragraph.get());) |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4373 | SkASSERT(impl->runs().size() == 3); |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 4374 | SkASSERT(impl->runs()[0].textRange().end == impl->runs()[1].textRange().start); |
| 4375 | SkASSERT(impl->runs()[1].textRange().end == impl->runs()[2].textRange().start); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4376 | } |
| 4377 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4378 | DEF_TEST(SkParagraph_JSON1, reporter) { |
| 4379 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 4380 | if (!fontCollection->fontsFound()) return; |
| 4381 | const char* text = "👨👩👧👦"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4382 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4383 | |
| 4384 | ParagraphStyle paragraph_style; |
| 4385 | paragraph_style.turnHintingOff(); |
| 4386 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4387 | |
| 4388 | TextStyle text_style; |
| 4389 | text_style.setFontFamilies({SkString("Noto Color Emoji")}); |
| 4390 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4391 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4392 | builder.pop(); |
| 4393 | |
| 4394 | auto paragraph = builder.Build(); |
| 4395 | paragraph->layout(TestCanvasWidth); |
| 4396 | |
| 4397 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 4398 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 4399 | auto run = impl->runs().front(); |
| 4400 | |
| 4401 | auto cluster = 0; |
| 4402 | SkShaperJSONWriter::VisualizeClusters( |
| 4403 | text, 0, std::strlen(text), run.glyphs(), run.clusterIndexes(), |
| 4404 | [&](int codePointCount, SkSpan<const char> utf1to1, SkSpan<const SkGlyphID> glyph1to1) { |
| 4405 | if (cluster == 0) { |
| 4406 | std::string toCheckUtf8{utf1to1.data(), utf1to1.size()}; |
| 4407 | SkASSERT(std::strcmp(text, utf1to1.data()) == 0); |
| 4408 | SkASSERT(glyph1to1.size() == 1); |
| 4409 | SkASSERT(*glyph1to1.begin() == 1611); |
| 4410 | } |
| 4411 | ++cluster; |
| 4412 | }); |
| 4413 | SkASSERT(cluster <= 2); |
| 4414 | } |
| 4415 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4416 | DEF_TEST(SkParagraph_JSON2, reporter) { |
| 4417 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 4418 | if (!fontCollection->fontsFound()) return; |
| 4419 | const char* text = "p〠q"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4420 | const size_t len = strlen(text); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4421 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4422 | ParagraphStyle paragraph_style; |
| 4423 | paragraph_style.turnHintingOff(); |
| 4424 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4425 | |
| 4426 | TextStyle text_style; |
| 4427 | text_style.setFontFamilies({SkString("Noto Sans CJK JP")}); |
| 4428 | text_style.setColor(SK_ColorBLACK); |
| 4429 | text_style.setFontSize(50); |
| 4430 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4431 | builder.addText(text, len); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4432 | builder.pop(); |
| 4433 | |
| 4434 | auto paragraph = builder.Build(); |
| 4435 | paragraph->layout(TestCanvasWidth); |
| 4436 | |
| 4437 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 4438 | REPORTER_ASSERT(reporter, impl->runs().size() == 1); |
| 4439 | auto run = impl->runs().front(); |
| 4440 | |
| 4441 | auto cluster = 0; |
| 4442 | for (auto& run : impl->runs()) { |
| 4443 | SkShaperJSONWriter::VisualizeClusters( |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 4444 | impl->text().begin() + run.textRange().start, 0, run.textRange().width(), |
| 4445 | run.glyphs(), run.clusterIndexes(), |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4446 | [&](int codePointCount, SkSpan<const char> utf1to1, |
| 4447 | SkSpan<const SkGlyphID> glyph1to1) { |
| 4448 | if (cluster == 0) { |
| 4449 | std::string toCheckUtf8{utf1to1.data(), utf1to1.size()}; |
| 4450 | SkASSERT(std::strcmp(text, utf1to1.data()) == 0); |
| 4451 | SkASSERT(glyph1to1.size() == 3); |
| 4452 | } |
| 4453 | ++cluster; |
| 4454 | }); |
| 4455 | } |
| 4456 | |
| 4457 | SkASSERT(cluster <= 2); |
| 4458 | } |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4459 | |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4460 | DEF_TEST(SkParagraph_CacheText, reporter) { |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4461 | ParagraphCache cache; |
| 4462 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 4463 | if (!fontCollection->fontsFound()) return; |
| 4464 | |
| 4465 | ParagraphStyle paragraph_style; |
| 4466 | paragraph_style.turnHintingOff(); |
| 4467 | |
| 4468 | TextStyle text_style; |
| 4469 | text_style.setFontFamilies({SkString("Roboto")}); |
| 4470 | text_style.setColor(SK_ColorBLACK); |
| 4471 | |
| 4472 | auto test = [&](const char* text, int count, bool expectedToBeFound) { |
| 4473 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4474 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4475 | builder.addText(text, strlen(text)); |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4476 | builder.pop(); |
| 4477 | auto paragraph = builder.Build(); |
| 4478 | |
| 4479 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 4480 | REPORTER_ASSERT(reporter, count == cache.count()); |
| 4481 | auto found = cache.findParagraph(impl); |
| 4482 | REPORTER_ASSERT(reporter, found == expectedToBeFound); |
| 4483 | auto added = cache.updateParagraph(impl); |
| 4484 | REPORTER_ASSERT(reporter, added != expectedToBeFound); |
| 4485 | }; |
| 4486 | |
| 4487 | test("text1", 0, false); |
| 4488 | test("text1", 1, true); |
| 4489 | test("text2", 1, false); |
| 4490 | test("text2", 2, true); |
| 4491 | test("text3", 2, false); |
| 4492 | } |
| 4493 | |
| 4494 | DEF_TEST(SkParagraph_CacheFonts, reporter) { |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4495 | ParagraphCache cache; |
| 4496 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 4497 | if (!fontCollection->fontsFound()) return; |
| 4498 | |
| 4499 | ParagraphStyle paragraph_style; |
| 4500 | paragraph_style.turnHintingOff(); |
| 4501 | |
| 4502 | TextStyle text_style; |
| 4503 | text_style.setColor(SK_ColorBLACK); |
| 4504 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4505 | const char* text = "text"; |
| 4506 | const size_t len = strlen(text); |
| 4507 | |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4508 | auto test = [&](int count, bool expectedToBeFound) { |
| 4509 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4510 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4511 | builder.addText(text, len); |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4512 | builder.pop(); |
| 4513 | auto paragraph = builder.Build(); |
| 4514 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 4515 | |
| 4516 | impl->getResolver().findAllFontsForAllStyledBlocks(impl); |
| 4517 | |
| 4518 | REPORTER_ASSERT(reporter, count == cache.count()); |
| 4519 | auto found = cache.findParagraph(impl); |
| 4520 | REPORTER_ASSERT(reporter, found == expectedToBeFound); |
| 4521 | auto added = cache.updateParagraph(impl); |
| 4522 | REPORTER_ASSERT(reporter, added != expectedToBeFound); |
| 4523 | }; |
| 4524 | |
| 4525 | text_style.setFontFamilies({SkString("Roboto")}); |
| 4526 | test(0, false); |
| 4527 | test(1, true); |
| 4528 | text_style.setFontFamilies({SkString("Homemade Apple")}); |
| 4529 | test(1, false); |
| 4530 | test(2, true); |
| 4531 | text_style.setFontFamilies({SkString("Noto Color Emoji")}); |
| 4532 | test(2, false); |
| 4533 | } |
| 4534 | |
| 4535 | DEF_TEST(SkParagraph_CacheFontRanges, reporter) { |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4536 | ParagraphCache cache; |
| 4537 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 4538 | if (!fontCollection->fontsFound()) return; |
| 4539 | |
| 4540 | ParagraphStyle paragraph_style; |
| 4541 | paragraph_style.turnHintingOff(); |
| 4542 | |
| 4543 | TextStyle text_style; |
| 4544 | text_style.setFontFamilies({SkString("Roboto")}); |
| 4545 | text_style.setColor(SK_ColorBLACK); |
| 4546 | |
| 4547 | auto test = [&](const char* text1, |
| 4548 | const char* text2, |
| 4549 | const char* font1, |
| 4550 | const char* font2, |
| 4551 | int count, |
| 4552 | bool expectedToBeFound) { |
| 4553 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4554 | text_style.setFontFamilies({SkString(font1)}); |
| 4555 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4556 | builder.addText(text1, strlen(text1)); |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4557 | builder.pop(); |
| 4558 | text_style.setFontFamilies({SkString(font2)}); |
| 4559 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4560 | builder.addText(text2, strlen(text2)); |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4561 | builder.pop(); |
| 4562 | auto paragraph = builder.Build(); |
| 4563 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 4564 | |
| 4565 | impl->getResolver().findAllFontsForAllStyledBlocks(impl); |
| 4566 | |
| 4567 | REPORTER_ASSERT(reporter, count == cache.count()); |
| 4568 | auto found = cache.findParagraph(impl); |
| 4569 | REPORTER_ASSERT(reporter, found == expectedToBeFound); |
| 4570 | auto added = cache.updateParagraph(impl); |
| 4571 | REPORTER_ASSERT(reporter, added != expectedToBeFound); |
| 4572 | }; |
| 4573 | |
| 4574 | test("text", "", "Roboto", "Homemade Apple", 0, false); |
| 4575 | test("t", "ext", "Roboto", "Homemade Apple", 1, false); |
| 4576 | test("te", "xt", "Roboto", "Homemade Apple", 2, false); |
| 4577 | test("tex", "t", "Roboto", "Homemade Apple", 3, false); |
| 4578 | test("text", "", "Roboto", "Homemade Apple", 4, true); |
| 4579 | } |
| 4580 | |
| 4581 | DEF_TEST(SkParagraph_CacheStyles, reporter) { |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4582 | ParagraphCache cache; |
| 4583 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 4584 | if (!fontCollection->fontsFound()) return; |
| 4585 | |
| 4586 | ParagraphStyle paragraph_style; |
| 4587 | paragraph_style.turnHintingOff(); |
| 4588 | |
| 4589 | TextStyle text_style; |
| 4590 | text_style.setFontFamilies({SkString("Roboto")}); |
| 4591 | text_style.setColor(SK_ColorBLACK); |
| 4592 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4593 | const char* text = "text"; |
| 4594 | const size_t len = strlen(text); |
| 4595 | |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4596 | auto test = [&](int count, bool expectedToBeFound) { |
| 4597 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4598 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4599 | builder.addText(text, len); |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4600 | builder.pop(); |
| 4601 | auto paragraph = builder.Build(); |
| 4602 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 4603 | |
| 4604 | impl->getResolver().findAllFontsForAllStyledBlocks(impl); |
| 4605 | |
| 4606 | REPORTER_ASSERT(reporter, count == cache.count()); |
| 4607 | auto found = cache.findParagraph(impl); |
| 4608 | REPORTER_ASSERT(reporter, found == expectedToBeFound); |
| 4609 | auto added = cache.updateParagraph(impl); |
| 4610 | REPORTER_ASSERT(reporter, added != expectedToBeFound); |
| 4611 | }; |
| 4612 | |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 4613 | test(0, false); |
| 4614 | test(1, true); |
| 4615 | text_style.setLetterSpacing(10); |
| 4616 | test(1, false); |
| 4617 | test(2, true); |
| 4618 | text_style.setWordSpacing(10); |
| 4619 | test(2, false); |
| 4620 | } |
Julia Lavrova | a40db42 | 2019-08-21 13:49:15 -0400 | [diff] [blame] | 4621 | |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4622 | DEF_TEST(SkParagraph_EmptyParagraphWithLineBreak, reporter) { |
Julia Lavrova | a40db42 | 2019-08-21 13:49:15 -0400 | [diff] [blame] | 4623 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 4624 | if (!fontCollection->fontsFound()) return; |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4625 | fontCollection->setDefaultFontManager(SkFontMgr::RefDefault()); |
| 4626 | TestCanvas canvas("SkParagraph_EmptyParagraphWithLineBreak.png"); |
Julia Lavrova | a40db42 | 2019-08-21 13:49:15 -0400 | [diff] [blame] | 4627 | |
| 4628 | ParagraphStyle paragraph_style; |
Julia Lavrova | a40db42 | 2019-08-21 13:49:15 -0400 | [diff] [blame] | 4629 | TextStyle text_style; |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4630 | text_style.setFontSize(16); |
| 4631 | text_style.setFontFamilies({SkString("Roboto")}); |
| 4632 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4633 | builder.addText("\n", 1); |
Julia Lavrova | a40db42 | 2019-08-21 13:49:15 -0400 | [diff] [blame] | 4634 | |
| 4635 | auto paragraph = builder.Build(); |
| 4636 | paragraph->layout(TestCanvasWidth); |
| 4637 | paragraph->paint(canvas.get(), 0, 0); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4638 | auto result = paragraph->GetRectsForPlaceholders(); |
Julia Lavrova | a40db42 | 2019-08-21 13:49:15 -0400 | [diff] [blame] | 4639 | } |
| 4640 | |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4641 | DEF_TEST(SkParagraph_NullInMiddleOfText, reporter) { |
| 4642 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 4643 | if (!fontCollection->fontsFound()) return; |
| 4644 | fontCollection->setDefaultFontManager(SkFontMgr::RefDefault()); |
| 4645 | TestCanvas canvas("SkParagraph_NullInMiddleOfText.png"); |
| 4646 | |
| 4647 | const SkString text("null terminator ->\u0000<- on purpose did you see it?"); |
| 4648 | |
| 4649 | ParagraphStyle paragraph_style; |
| 4650 | TextStyle text_style; |
| 4651 | text_style.setFontSize(16); |
| 4652 | text_style.setFontFamilies({SkString("Roboto")}); |
| 4653 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4654 | builder.addText(text.c_str(), text.size()); |
| 4655 | |
| 4656 | auto paragraph = builder.Build(); |
| 4657 | paragraph->layout(TestCanvasWidth); |
| 4658 | paragraph->paint(canvas.get(), 0, 0); |
| 4659 | } |
| 4660 | |
Julia Lavrova | a40db42 | 2019-08-21 13:49:15 -0400 | [diff] [blame] | 4661 | DEF_TEST(SkParagraph_PlaceholderOnly, reporter) { |
| 4662 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 4663 | if (!fontCollection->fontsFound()) return; |
| 4664 | TestCanvas canvas("SkParagraph_PlaceholderOnly.png"); |
| 4665 | |
| 4666 | ParagraphStyle paragraph_style; |
| 4667 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4668 | |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4669 | PlaceholderStyle placeholder(0, 0, PlaceholderAlignment::kBaseline, TextBaseline::kAlphabetic, 0); |
Julia Lavrova | a40db42 | 2019-08-21 13:49:15 -0400 | [diff] [blame] | 4670 | builder.addPlaceholder(placeholder); |
| 4671 | |
| 4672 | auto paragraph = builder.Build(); |
| 4673 | paragraph->layout(TestCanvasWidth); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4674 | auto result = paragraph->GetRectsForPlaceholders(); |
| 4675 | paragraph->paint(canvas.get(), 0, 0); |
| 4676 | } |
| 4677 | |
| 4678 | DEF_TEST(SkParagraph_Fallbacks, reporter) { |
| 4679 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 4680 | if (!fontCollection->fontsFound()) return; |
| 4681 | fontCollection->setDefaultFontManager(SkFontMgr::RefDefault(), "Arial"); |
| 4682 | TestCanvas canvas("SkParagraph_Fallbacks.png"); |
| 4683 | |
| 4684 | const char* multiScript = "A1!aÀàĀāƁƀḂⱠꜲꬰəͲἀἏЀЖԠꙐꙮՁخࡔࠇܦআਉઐଘஇఘಧൺඣᭆᯔᮯ᳇ꠈᜅᩌꪈ༇ꥄꡙꫤ᧰៘꧁꧂ᜰᨏᯤᢆᣭᗗꗃⵞ𐒎߷ጩꬤ𖠺‡₩℻Ⅷ↹⋇⏳ⓖ╋▒◛⚧⑆שׁ🅕㊼龜ポ䷤🂡\n"; |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4685 | const size_t len = strlen(multiScript); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4686 | |
| 4687 | const char* androidFonts[] = { |
| 4688 | "sans-serif", |
| 4689 | "sans-serif-condensed", |
| 4690 | "serif", |
| 4691 | "monospace", |
| 4692 | "serif-monospace", |
| 4693 | "casual", |
| 4694 | "cursive", |
| 4695 | "sans-serif-smallcaps", |
| 4696 | }; |
| 4697 | |
| 4698 | for (auto& font : androidFonts) { |
| 4699 | |
| 4700 | ParagraphStyle paragraph_style; |
| 4701 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4702 | |
| 4703 | TextStyle text_style; |
| 4704 | text_style.setColor(SK_ColorBLACK); |
| 4705 | text_style.setLocale(SkString("en_US")); |
| 4706 | text_style.setFontSize(20); |
| 4707 | |
| 4708 | text_style.setFontFamilies({ SkString(font) }); |
| 4709 | builder.pushStyle(text_style); |
Kevin Lubick | 7aeabcf | 2019-09-27 15:16:13 -0400 | [diff] [blame] | 4710 | builder.addText(multiScript, len); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 4711 | |
| 4712 | builder.pop(); |
| 4713 | |
| 4714 | auto paragraph = builder.Build(); |
| 4715 | paragraph->layout(TestCanvasWidth); |
| 4716 | paragraph->paint(canvas.get(), 0, 0); |
| 4717 | canvas.get()->translate(0, paragraph.get()->getHeight() + 10); |
| 4718 | } |
| 4719 | } |
| 4720 | |
| 4721 | DEF_TEST(SkParagraph_Bidi1, reporter) { |
| 4722 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 4723 | if (!fontCollection->fontsFound()) return; |
| 4724 | fontCollection->setDefaultFontManager(SkFontMgr::RefDefault()); |
| 4725 | TestCanvas canvas("SkParagraph_Bidi1.png"); |
| 4726 | |
| 4727 | std::u16string abc = u"\u202Dabc"; |
| 4728 | std::u16string DEF = u"\u202EDEF"; |
| 4729 | std::u16string ghi = u"\u202Dghi"; |
| 4730 | std::u16string JKL = u"\u202EJKL"; |
| 4731 | std::u16string mno = u"\u202Dmno"; |
| 4732 | |
| 4733 | std::u16string abcDEFghiJKLmno = u"\u202Dabc\u202EDEF\u202Dghi\u202EJKL\u202Dmno"; |
| 4734 | |
| 4735 | ParagraphStyle paragraph_style; |
| 4736 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4737 | |
| 4738 | TextStyle text_style; |
| 4739 | text_style.setFontFamilies({ SkString("sans-serif")}); |
| 4740 | text_style.setFontSize(40); |
| 4741 | |
| 4742 | text_style.setColor(SK_ColorCYAN); |
| 4743 | text_style.setFontStyle(SkFontStyle(SkFontStyle::kThin_Weight, SkFontStyle::kNormal_Width, SkFontStyle::kUpright_Slant)); |
| 4744 | builder.pushStyle(text_style); |
| 4745 | builder.addText(abc); |
| 4746 | |
| 4747 | text_style.setColor(SK_ColorGREEN); |
| 4748 | text_style.setFontStyle(SkFontStyle(SkFontStyle::kLight_Weight, SkFontStyle::kNormal_Width, SkFontStyle::kUpright_Slant)); |
| 4749 | builder.pushStyle(text_style); |
| 4750 | builder.addText(DEF); |
| 4751 | |
| 4752 | text_style.setColor(SK_ColorYELLOW); |
| 4753 | text_style.setFontStyle(SkFontStyle(SkFontStyle::kNormal_Weight, SkFontStyle::kNormal_Width, SkFontStyle::kUpright_Slant)); |
| 4754 | builder.pushStyle(text_style); |
| 4755 | builder.addText(ghi); |
| 4756 | |
| 4757 | text_style.setColor(SK_ColorMAGENTA); |
| 4758 | text_style.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, SkFontStyle::kUpright_Slant)); |
| 4759 | builder.pushStyle(text_style); |
| 4760 | builder.addText(JKL); |
| 4761 | |
| 4762 | text_style.setColor(SK_ColorBLUE); |
| 4763 | text_style.setFontStyle(SkFontStyle(SkFontStyle::kBlack_Weight, SkFontStyle::kNormal_Width, SkFontStyle::kUpright_Slant)); |
| 4764 | builder.pushStyle(text_style); |
| 4765 | builder.addText(mno); |
| 4766 | |
| 4767 | auto paragraph = builder.Build(); |
| 4768 | paragraph->layout(400); |
| 4769 | paragraph->paint(canvas.get(), 0, 0); |
| 4770 | } |
| 4771 | |
| 4772 | DEF_TEST(SkParagraph_Bidi2, reporter) { |
| 4773 | sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(); |
| 4774 | if (!fontCollection->fontsFound()) return; |
| 4775 | fontCollection->setDefaultFontManager(SkFontMgr::RefDefault()); |
| 4776 | TestCanvas canvas("SkParagraph_Bidi2.png"); |
| 4777 | |
| 4778 | std::u16string abcD = u"\u202Dabc\u202ED"; |
| 4779 | std::u16string EFgh = u"EF\u202Dgh"; |
| 4780 | std::u16string iJKLmno = u"i\u202EJKL\u202Dmno"; |
| 4781 | |
| 4782 | std::u16string abcDEFghiJKLmno = u"\u202Dabc\u202EDEF\u202Dghi\u202EJKL\u202Dmno"; |
| 4783 | |
| 4784 | ParagraphStyle paragraph_style; |
| 4785 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 4786 | |
| 4787 | TextStyle text_style; |
| 4788 | text_style.setFontFamilies({ SkString("sans-serif")}); |
| 4789 | text_style.setFontSize(40); |
| 4790 | text_style.setColor(SK_ColorBLACK); |
| 4791 | |
| 4792 | text_style.setColor(SK_ColorYELLOW); |
| 4793 | text_style.setFontSize(40); |
| 4794 | text_style.setFontStyle(SkFontStyle(SkFontStyle::kThin_Weight, SkFontStyle::kNormal_Width, SkFontStyle::kUpright_Slant)); |
| 4795 | builder.pushStyle(text_style); |
| 4796 | builder.addText(abcD); |
| 4797 | |
| 4798 | text_style.setColor(SK_ColorRED); |
| 4799 | text_style.setFontSize(50); |
| 4800 | text_style.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, SkFontStyle::kUpright_Slant)); |
| 4801 | builder.pushStyle(text_style); |
| 4802 | builder.addText(EFgh); |
| 4803 | |
| 4804 | text_style.setColor(SK_ColorMAGENTA); |
| 4805 | text_style.setFontSize(60); |
| 4806 | text_style.setFontStyle(SkFontStyle(SkFontStyle::kExtraBold_Weight, SkFontStyle::kNormal_Width, SkFontStyle::kUpright_Slant)); |
| 4807 | builder.pushStyle(text_style); |
| 4808 | builder.addText(iJKLmno); |
| 4809 | |
| 4810 | auto paragraph = builder.Build(); |
| 4811 | paragraph->layout(360); |
Julia Lavrova | a40db42 | 2019-08-21 13:49:15 -0400 | [diff] [blame] | 4812 | paragraph->paint(canvas.get(), 0, 0); |
| 4813 | } |