Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1 | // Copyright 2019 Google LLC. |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2 | #include "include/core/SkCanvas.h" |
| 3 | #include "include/core/SkColorFilter.h" |
| 4 | #include "include/core/SkColorPriv.h" |
| 5 | #include "include/core/SkGraphics.h" |
| 6 | #include "include/core/SkPath.h" |
| 7 | #include "include/core/SkRegion.h" |
| 8 | #include "include/core/SkShader.h" |
| 9 | #include "include/core/SkStream.h" |
| 10 | #include "include/core/SkTextBlob.h" |
| 11 | #include "include/core/SkTime.h" |
| 12 | #include "include/core/SkTypeface.h" |
| 13 | #include "include/effects/SkBlurMaskFilter.h" |
| 14 | #include "include/effects/SkGradientShader.h" |
| 15 | #include "include/utils/SkRandom.h" |
| 16 | #include "modules/skparagraph/include/Paragraph.h" |
Julia Lavrova | 6e6333f | 2019-06-17 10:34:10 -0400 | [diff] [blame] | 17 | #include "modules/skparagraph/include/TypefaceFontProvider.h" |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 18 | #include "modules/skparagraph/src/ParagraphBuilderImpl.h" |
| 19 | #include "modules/skparagraph/src/ParagraphImpl.h" |
Julia Lavrova | 9af5cc4 | 2019-06-19 13:32:01 -0400 | [diff] [blame] | 20 | #include "modules/skparagraph/utils/TestFontCollection.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 21 | #include "samplecode/Sample.h" |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 22 | #include "src/core/SkOSFile.h" |
| 23 | #include "src/shaders/SkColorShader.h" |
| 24 | #include "src/utils/SkUTF.h" |
| 25 | #include "tools/Resources.h" |
| 26 | |
| 27 | using namespace skia::textlayout; |
| 28 | namespace { |
| 29 | |
| 30 | sk_sp<SkShader> setgrad(const SkRect& r, SkColor c0, SkColor c1) { |
| 31 | SkColor colors[] = {c0, c1}; |
| 32 | SkPoint pts[] = {{r.fLeft, r.fTop}, {r.fRight, r.fTop}}; |
| 33 | return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp); |
| 34 | } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 35 | } // namespace |
| 36 | |
| 37 | class ParagraphView1 : public Sample { |
| 38 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 39 | SkString name() override { return SkString("Paragraph1"); } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 40 | |
| 41 | void drawTest(SkCanvas* canvas, SkScalar w, SkScalar h, SkColor fg, SkColor bg) { |
| 42 | const std::vector< |
| 43 | std::tuple<std::string, bool, bool, int, SkColor, SkColor, bool, TextDecorationStyle>> |
| 44 | gParagraph = {{"monospace", true, false, 14, SK_ColorWHITE, SK_ColorRED, true, |
| 45 | TextDecorationStyle::kDashed}, |
| 46 | {"Assyrian", false, false, 20, SK_ColorWHITE, SK_ColorBLUE, false, |
| 47 | TextDecorationStyle::kDotted}, |
| 48 | {"serif", true, true, 10, SK_ColorWHITE, SK_ColorRED, true, |
| 49 | TextDecorationStyle::kDouble}, |
| 50 | {"Arial", false, true, 16, SK_ColorGRAY, SK_ColorGREEN, true, |
| 51 | TextDecorationStyle::kSolid}, |
| 52 | {"sans-serif", false, false, 8, SK_ColorWHITE, SK_ColorRED, false, |
| 53 | TextDecorationStyle::kWavy}}; |
| 54 | SkAutoCanvasRestore acr(canvas, true); |
| 55 | |
| 56 | canvas->clipRect(SkRect::MakeWH(w, h)); |
| 57 | canvas->drawColor(SK_ColorWHITE); |
| 58 | |
| 59 | SkScalar margin = 20; |
| 60 | |
| 61 | SkPaint paint; |
| 62 | paint.setAntiAlias(true); |
| 63 | paint.setColor(fg); |
| 64 | |
| 65 | SkPaint blue; |
| 66 | blue.setColor(SK_ColorBLUE); |
| 67 | |
| 68 | TextStyle defaultStyle; |
| 69 | defaultStyle.setBackgroundColor(blue); |
| 70 | defaultStyle.setForegroundColor(paint); |
| 71 | ParagraphStyle paraStyle; |
| 72 | |
| 73 | for (auto i = 1; i < 5; ++i) { |
| 74 | defaultStyle.setFontSize(24 * i); |
| 75 | paraStyle.setTextStyle(defaultStyle); |
| 76 | ParagraphBuilderImpl builder(paraStyle, sk_make_sp<FontCollection>()); |
| 77 | std::string name = "Paragraph: " + std::to_string(24 * i); |
| 78 | builder.addText(name.c_str()); |
| 79 | for (auto para : gParagraph) { |
| 80 | TextStyle style; |
| 81 | style.setFontFamilies({SkString(std::get<0>(para).c_str())}); |
| 82 | SkFontStyle fontStyle(std::get<1>(para) ? SkFontStyle::Weight::kBold_Weight |
| 83 | : SkFontStyle::Weight::kNormal_Weight, |
| 84 | SkFontStyle::Width::kNormal_Width, |
| 85 | std::get<2>(para) ? SkFontStyle::Slant::kItalic_Slant |
| 86 | : SkFontStyle::Slant::kUpright_Slant); |
| 87 | style.setFontStyle(fontStyle); |
| 88 | style.setFontSize(std::get<3>(para) * i); |
| 89 | SkPaint background; |
| 90 | background.setColor(std::get<4>(para)); |
| 91 | style.setBackgroundColor(background); |
| 92 | SkPaint foreground; |
| 93 | foreground.setColor(std::get<5>(para)); |
| 94 | foreground.setAntiAlias(true); |
| 95 | style.setForegroundColor(foreground); |
| 96 | if (std::get<6>(para)) { |
| 97 | style.addShadow(TextShadow(SK_ColorBLACK, SkPoint::Make(5, 5), 2)); |
| 98 | } |
| 99 | |
| 100 | auto decoration = (i % 4); |
| 101 | if (decoration == 3) { |
| 102 | decoration = 4; |
| 103 | } |
| 104 | |
| 105 | bool test = (TextDecoration)decoration != TextDecoration::kNoDecoration; |
| 106 | std::string deco = std::to_string((int)decoration); |
| 107 | if (test) { |
| 108 | style.setDecoration((TextDecoration)decoration); |
| 109 | style.setDecorationStyle(std::get<7>(para)); |
| 110 | style.setDecorationColor(std::get<5>(para)); |
| 111 | } |
| 112 | builder.pushStyle(style); |
| 113 | std::string name = " " + std::get<0>(para) + " " + |
| 114 | (std::get<1>(para) ? ", bold" : "") + |
| 115 | (std::get<2>(para) ? ", italic" : "") + " " + |
| 116 | std::to_string(std::get<3>(para) * i) + |
| 117 | (std::get<4>(para) != bg ? ", background" : "") + |
| 118 | (std::get<5>(para) != fg ? ", foreground" : "") + |
| 119 | (std::get<6>(para) ? ", shadow" : "") + |
| 120 | (test ? ", decorations " + deco : "") + ";"; |
| 121 | builder.addText(name.c_str()); |
| 122 | builder.pop(); |
| 123 | } |
| 124 | |
| 125 | auto paragraph = builder.Build(); |
| 126 | paragraph->layout(w - margin * 2); |
| 127 | |
| 128 | paragraph->paint(canvas, margin, margin); |
| 129 | |
| 130 | canvas->translate(0, paragraph->getHeight()); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | void onDrawContent(SkCanvas* canvas) override { |
| 135 | drawTest(canvas, this->width(), this->height(), SK_ColorRED, SK_ColorWHITE); |
| 136 | } |
| 137 | |
| 138 | private: |
| 139 | |
| 140 | typedef Sample INHERITED; |
| 141 | }; |
| 142 | |
| 143 | class ParagraphView2 : public Sample { |
| 144 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 145 | SkString name() override { return SkString("Paragraph2"); } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 146 | |
| 147 | void drawCode(SkCanvas* canvas, SkScalar w, SkScalar h) { |
| 148 | SkPaint comment; |
| 149 | comment.setColor(SK_ColorGRAY); |
| 150 | SkPaint constant; |
| 151 | constant.setColor(SK_ColorMAGENTA); |
| 152 | SkPaint null; |
| 153 | null.setColor(SK_ColorMAGENTA); |
| 154 | SkPaint literal; |
| 155 | literal.setColor(SK_ColorGREEN); |
| 156 | SkPaint code; |
| 157 | code.setColor(SK_ColorDKGRAY); |
| 158 | SkPaint number; |
| 159 | number.setColor(SK_ColorBLUE); |
| 160 | SkPaint name; |
| 161 | name.setColor(SK_ColorRED); |
| 162 | |
| 163 | SkPaint white; |
| 164 | white.setColor(SK_ColorWHITE); |
| 165 | |
| 166 | TextStyle defaultStyle; |
| 167 | defaultStyle.setBackgroundColor(white); |
| 168 | defaultStyle.setForegroundColor(code); |
| 169 | defaultStyle.setFontFamilies({SkString("monospace")}); |
| 170 | defaultStyle.setFontSize(30); |
| 171 | ParagraphStyle paraStyle; |
| 172 | paraStyle.setTextStyle(defaultStyle); |
| 173 | |
| 174 | ParagraphBuilderImpl builder(paraStyle, sk_make_sp<FontCollection>()); |
| 175 | |
| 176 | builder.pushStyle(style(name)); |
| 177 | builder.addText("RaisedButton"); |
| 178 | builder.pop(); |
| 179 | builder.addText("(\n"); |
| 180 | builder.addText(" child: "); |
| 181 | builder.pushStyle(style(constant)); |
| 182 | builder.addText("const"); |
| 183 | builder.pop(); |
| 184 | builder.addText(" "); |
| 185 | builder.pushStyle(style(name)); |
| 186 | builder.addText("Text"); |
| 187 | builder.pop(); |
| 188 | builder.addText("("); |
| 189 | builder.pushStyle(style(literal)); |
| 190 | builder.addText("'BUTTON TITLE'"); |
| 191 | builder.pop(); |
| 192 | builder.addText("),\n"); |
| 193 | |
| 194 | auto paragraph = builder.Build(); |
| 195 | paragraph->layout(w - 20); |
| 196 | |
| 197 | paragraph->paint(canvas, 20, 20); |
| 198 | } |
| 199 | |
| 200 | TextStyle style(SkPaint paint) { |
| 201 | TextStyle style; |
| 202 | paint.setAntiAlias(true); |
| 203 | style.setForegroundColor(paint); |
| 204 | style.setFontFamilies({SkString("monospace")}); |
| 205 | style.setFontSize(30); |
| 206 | |
| 207 | return style; |
| 208 | } |
| 209 | |
| 210 | void drawText(SkCanvas* canvas, SkScalar w, SkScalar h, std::vector<std::string>& text, |
| 211 | SkColor fg = SK_ColorDKGRAY, SkColor bg = SK_ColorWHITE, |
| 212 | const char* ff = "sans-serif", SkScalar fs = 24, |
| 213 | size_t lineLimit = 30, |
| 214 | const std::u16string& ellipsis = u"\u2026") { |
| 215 | SkAutoCanvasRestore acr(canvas, true); |
| 216 | |
| 217 | canvas->clipRect(SkRect::MakeWH(w, h)); |
| 218 | canvas->drawColor(bg); |
| 219 | |
| 220 | SkScalar margin = 20; |
| 221 | |
| 222 | SkPaint paint; |
| 223 | paint.setAntiAlias(true); |
| 224 | paint.setColor(fg); |
| 225 | |
| 226 | SkPaint blue; |
| 227 | blue.setColor(SK_ColorBLUE); |
| 228 | |
| 229 | SkPaint background; |
| 230 | background.setColor(bg); |
| 231 | |
| 232 | TextStyle style; |
| 233 | style.setBackgroundColor(blue); |
| 234 | style.setForegroundColor(paint); |
| 235 | style.setFontFamilies({SkString(ff)}); |
| 236 | style.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, |
| 237 | SkFontStyle::kNormal_Width, |
| 238 | SkFontStyle::kUpright_Slant)); |
| 239 | style.setFontSize(fs); |
| 240 | ParagraphStyle paraStyle; |
| 241 | paraStyle.setTextStyle(style); |
| 242 | paraStyle.setMaxLines(lineLimit); |
| 243 | |
| 244 | paraStyle.setEllipsis(ellipsis); |
| 245 | TextStyle defaultStyle; |
| 246 | defaultStyle.setFontSize(20); |
| 247 | paraStyle.setTextStyle(defaultStyle); |
Julia Lavrova | 9af5cc4 | 2019-06-19 13:32:01 -0400 | [diff] [blame] | 248 | auto fontCollection = sk_make_sp<TestFontCollection>(GetResourcePath("fonts").c_str()); |
| 249 | SkASSERT(fontCollection->fontsFound() != 0); |
| 250 | ParagraphBuilderImpl builder(paraStyle, fontCollection); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 251 | |
| 252 | SkPaint foreground; |
| 253 | foreground.setColor(fg); |
| 254 | style.setForegroundColor(foreground); |
| 255 | style.setBackgroundColor(background); |
| 256 | |
| 257 | for (auto& part : text) { |
| 258 | builder.pushStyle(style); |
| 259 | builder.addText(part.c_str()); |
| 260 | builder.pop(); |
| 261 | } |
| 262 | |
| 263 | auto paragraph = builder.Build(); |
| 264 | paragraph->layout(w - margin * 2); |
| 265 | paragraph->paint(canvas, margin, margin); |
| 266 | |
| 267 | canvas->translate(0, paragraph->getHeight() + margin); |
| 268 | } |
| 269 | |
| 270 | void drawLine(SkCanvas* canvas, SkScalar w, SkScalar h, const std::string& text, |
| 271 | TextAlign align) { |
| 272 | SkAutoCanvasRestore acr(canvas, true); |
| 273 | |
| 274 | canvas->clipRect(SkRect::MakeWH(w, h)); |
| 275 | canvas->drawColor(SK_ColorWHITE); |
| 276 | |
| 277 | SkScalar margin = 20; |
| 278 | |
| 279 | SkPaint paint; |
| 280 | paint.setAntiAlias(true); |
| 281 | paint.setColor(SK_ColorBLUE); |
| 282 | |
| 283 | SkPaint gray; |
| 284 | gray.setColor(SK_ColorLTGRAY); |
| 285 | |
| 286 | TextStyle style; |
| 287 | style.setBackgroundColor(gray); |
| 288 | style.setForegroundColor(paint); |
| 289 | style.setFontFamilies({SkString("Arial")}); |
| 290 | style.setFontSize(30); |
| 291 | ParagraphStyle paraStyle; |
| 292 | paraStyle.setTextStyle(style); |
| 293 | paraStyle.setTextAlign(align); |
| 294 | |
| 295 | ParagraphBuilderImpl builder(paraStyle, sk_make_sp<FontCollection>()); |
| 296 | builder.addText(text.c_str()); |
| 297 | |
| 298 | auto paragraph = builder.Build(); |
| 299 | paragraph->layout(w - margin * 2); |
| 300 | paragraph->layout(w - margin); |
| 301 | paragraph->paint(canvas, margin, margin); |
| 302 | |
| 303 | canvas->translate(0, paragraph->getHeight() + margin); |
| 304 | } |
| 305 | |
| 306 | void onDrawContent(SkCanvas* canvas) override { |
| 307 | std::vector<std::string> cupertino = { |
| 308 | "google_logogoogle_gsuper_g_logo 1 " |
| 309 | "google_logogoogle_gsuper_g_logo 12 " |
| 310 | "google_logogoogle_gsuper_g_logo 123 " |
| 311 | "google_logogoogle_gsuper_g_logo 1234 " |
| 312 | "google_logogoogle_gsuper_g_logo 12345 " |
| 313 | "google_logogoogle_gsuper_g_logo 123456 " |
| 314 | "google_logogoogle_gsuper_g_logo 1234567 " |
| 315 | "google_logogoogle_gsuper_g_logo 12345678 " |
| 316 | "google_logogoogle_gsuper_g_logo 123456789 " |
| 317 | "google_logogoogle_gsuper_g_logo 1234567890 " |
| 318 | "google_logogoogle_gsuper_g_logo 123456789 " |
| 319 | "google_logogoogle_gsuper_g_logo 12345678 " |
| 320 | "google_logogoogle_gsuper_g_logo 1234567 " |
| 321 | "google_logogoogle_gsuper_g_logo 123456 " |
| 322 | "google_logogoogle_gsuper_g_logo 12345 " |
| 323 | "google_logogoogle_gsuper_g_logo 1234 " |
| 324 | "google_logogoogle_gsuper_g_logo 123 " |
| 325 | "google_logogoogle_gsuper_g_logo 12 " |
| 326 | "google_logogoogle_gsuper_g_logo 1 " |
| 327 | "google_logogoogle_gsuper_g_logo " |
| 328 | "google_logogoogle_gsuper_g_logo " |
| 329 | "google_logogoogle_gsuper_g_logo " |
| 330 | "google_logogoogle_gsuper_g_logo " |
| 331 | "google_logogoogle_gsuper_g_logo " |
| 332 | "google_logogoogle_gsuper_g_logo"}; |
| 333 | std::vector<std::string> text = { |
| 334 | "My neighbor came over to say,\n" |
| 335 | "Although not in a neighborly way,\n\n" |
| 336 | "That he'd knock me around,\n\n\n" |
| 337 | "If I didn't stop the sound,\n\n\n\n" |
| 338 | "Of the classical music I play."}; |
| 339 | |
| 340 | std::vector<std::string> long_word = { |
| 341 | "A_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_" |
| 342 | "very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_" |
| 343 | "very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_" |
| 344 | "very_very_very_very_very_very_very_long_text"}; |
| 345 | |
| 346 | std::vector<std::string> very_long = { |
| 347 | "A very very very very very very very very very very very very very very very very " |
| 348 | "very very very very very very very very very very very very very very very very " |
| 349 | "very very very very very very very very very very very very very very very very " |
| 350 | "very very very very very very very long text"}; |
| 351 | |
| 352 | std::vector<std::string> very_word = { |
| 353 | "A very_very_very_very_very_very_very_very_very_very " |
| 354 | "very_very_very_very_very_very_very_very_very_very very very very very very very " |
| 355 | "very very very very very very very very very very very very very very very very " |
| 356 | "very very very very very very very very very very very very very long text"}; |
| 357 | |
| 358 | SkScalar width = this->width() / 5; |
| 359 | SkScalar height = this->height(); |
| 360 | drawText(canvas, width, height, long_word, SK_ColorBLACK, SK_ColorWHITE, "Google Sans", 30); |
| 361 | canvas->translate(width, 0); |
| 362 | drawText(canvas, width, height, very_long, SK_ColorBLACK, SK_ColorWHITE, "Google Sans", 30); |
| 363 | canvas->translate(width, 0); |
| 364 | drawText(canvas, width, height, very_word, SK_ColorBLACK, SK_ColorWHITE, "Google Sans", 30); |
| 365 | canvas->translate(width, 0); |
| 366 | |
| 367 | drawText(canvas, width, height / 2, text, SK_ColorBLACK, SK_ColorWHITE, "Roboto", 20, 100, |
| 368 | u"\u2026"); |
| 369 | canvas->translate(0, height / 2); |
| 370 | drawCode(canvas, width, height / 2); |
| 371 | canvas->translate(width, -height / 2); |
| 372 | |
| 373 | drawText(canvas, width, height, cupertino, SK_ColorBLACK, SK_ColorWHITE, "Google Sans", 30); |
| 374 | } |
| 375 | |
| 376 | private: |
| 377 | typedef Sample INHERITED; |
| 378 | }; |
| 379 | |
| 380 | class ParagraphView3 : public Sample { |
| 381 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 382 | SkString name() override { return SkString("Paragraph3"); } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 383 | |
| 384 | void drawLine(SkCanvas* canvas, SkScalar w, SkScalar h, const std::string& text, |
| 385 | TextAlign align, size_t lineLimit = std::numeric_limits<size_t>::max(), |
| 386 | bool RTL = false, SkColor background = SK_ColorGRAY, |
| 387 | const std::u16string& ellipsis = u"\u2026") { |
| 388 | SkAutoCanvasRestore acr(canvas, true); |
| 389 | |
| 390 | canvas->clipRect(SkRect::MakeWH(w, h)); |
| 391 | canvas->drawColor(SK_ColorWHITE); |
| 392 | |
| 393 | SkScalar margin = 20; |
| 394 | |
| 395 | SkPaint paint; |
| 396 | paint.setAntiAlias(true); |
| 397 | paint.setColor(SK_ColorBLACK); |
| 398 | |
| 399 | SkPaint gray; |
| 400 | gray.setColor(background); |
| 401 | |
| 402 | SkPaint yellow; |
| 403 | yellow.setColor(SK_ColorYELLOW); |
| 404 | |
| 405 | TextStyle style; |
| 406 | style.setBackgroundColor(gray); |
| 407 | style.setForegroundColor(paint); |
| 408 | style.setFontFamilies({SkString("sans-serif")}); |
| 409 | style.setFontSize(30); |
| 410 | ParagraphStyle paraStyle; |
| 411 | paraStyle.setTextStyle(style); |
| 412 | paraStyle.setTextAlign(align); |
| 413 | paraStyle.setMaxLines(lineLimit); |
| 414 | paraStyle.setEllipsis(ellipsis); |
| 415 | // paraStyle.setTextDirection(RTL ? SkTextDirection::rtl : SkTextDirection::ltr); |
| 416 | |
| 417 | ParagraphBuilderImpl builder(paraStyle, sk_make_sp<FontCollection>()); |
| 418 | if (RTL) { |
| 419 | builder.addText(mirror(text)); |
| 420 | } else { |
| 421 | builder.addText(normal(text)); |
| 422 | } |
| 423 | |
| 424 | canvas->drawRect(SkRect::MakeXYWH(margin, margin, w - margin * 2, h - margin * 2), yellow); |
| 425 | auto paragraph = builder.Build(); |
| 426 | paragraph->layout(w - margin * 2); |
| 427 | paragraph->paint(canvas, margin, margin); |
| 428 | } |
| 429 | |
| 430 | std::u16string mirror(const std::string& text) { |
| 431 | std::u16string result; |
| 432 | result += u"\u202E"; |
| 433 | // for (auto i = text.size(); i > 0; --i) { |
| 434 | // result += text[i - 1]; |
| 435 | //} |
| 436 | |
| 437 | for (auto i = text.size(); i > 0; --i) { |
| 438 | auto ch = text[i - 1]; |
| 439 | if (ch == ',') { |
| 440 | result += u"!"; |
| 441 | } else if (ch == '.') { |
| 442 | result += u"!"; |
| 443 | } else { |
| 444 | result += ch; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | result += u"\u202C"; |
| 449 | return result; |
| 450 | } |
| 451 | |
| 452 | std::u16string normal(const std::string& text) { |
| 453 | std::u16string result; |
| 454 | result += u"\u202D"; |
| 455 | for (auto ch : text) { |
| 456 | result += ch; |
| 457 | } |
| 458 | result += u"\u202C"; |
| 459 | return result; |
| 460 | } |
| 461 | |
| 462 | void onDrawContent(SkCanvas* canvas) override { |
| 463 | const std::string options = // { "open-source open-source open-source open-source" }; |
| 464 | {"Flutter is an open-source project to help developers " |
| 465 | "build high-performance, high-fidelity, mobile apps for " |
| 466 | "iOS and Android " |
| 467 | "from a single codebase. This design lab is a playground " |
| 468 | "and showcase of Flutter's many widgets, behaviors, " |
| 469 | "animations, layouts, and more."}; |
| 470 | |
| 471 | canvas->drawColor(SK_ColorDKGRAY); |
| 472 | SkScalar width = this->width() / 4; |
| 473 | SkScalar height = this->height() / 2; |
| 474 | |
| 475 | const std::string line = |
| 476 | "World domination is such an ugly phrase - I prefer to call it world optimisation"; |
| 477 | |
| 478 | drawLine(canvas, width, height, line, TextAlign::kLeft, 1, false, SK_ColorLTGRAY); |
| 479 | canvas->translate(width, 0); |
| 480 | drawLine(canvas, width, height, line, TextAlign::kRight, 2, false, SK_ColorLTGRAY); |
| 481 | canvas->translate(width, 0); |
| 482 | drawLine(canvas, width, height, line, TextAlign::kCenter, 3, false, SK_ColorLTGRAY); |
| 483 | canvas->translate(width, 0); |
| 484 | drawLine(canvas, width, height, line, TextAlign::kJustify, 4, false, SK_ColorLTGRAY); |
| 485 | canvas->translate(-width * 3, height); |
| 486 | |
| 487 | drawLine(canvas, width, height, line, TextAlign::kLeft, 1, true, SK_ColorLTGRAY); |
| 488 | canvas->translate(width, 0); |
| 489 | drawLine(canvas, width, height, line, TextAlign::kRight, 2, true, SK_ColorLTGRAY); |
| 490 | canvas->translate(width, 0); |
| 491 | drawLine(canvas, width, height, line, TextAlign::kCenter, 3, true, SK_ColorLTGRAY); |
| 492 | canvas->translate(width, 0); |
| 493 | drawLine(canvas, width, height, line, TextAlign::kJustify, 4, true, SK_ColorLTGRAY); |
| 494 | canvas->translate(width, 0); |
| 495 | } |
| 496 | |
| 497 | private: |
| 498 | typedef Sample INHERITED; |
| 499 | }; |
| 500 | |
| 501 | class ParagraphView4 : public Sample { |
| 502 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 503 | SkString name() override { return SkString("Paragraph4"); } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 504 | |
| 505 | void drawFlutter(SkCanvas* canvas, SkScalar w, SkScalar h, |
| 506 | const char* ff = "Google Sans", SkScalar fs = 30, |
| 507 | size_t lineLimit = std::numeric_limits<size_t>::max(), |
| 508 | const std::u16string& ellipsis = u"\u2026") { |
| 509 | SkAutoCanvasRestore acr(canvas, true); |
| 510 | |
| 511 | canvas->clipRect(SkRect::MakeWH(w, h)); |
| 512 | |
| 513 | SkScalar margin = 20; |
| 514 | |
| 515 | SkPaint black; |
| 516 | black.setAntiAlias(true); |
| 517 | black.setColor(SK_ColorBLACK); |
| 518 | |
| 519 | SkPaint blue; |
| 520 | blue.setAntiAlias(true); |
| 521 | blue.setColor(SK_ColorBLUE); |
| 522 | |
| 523 | SkPaint red; |
| 524 | red.setAntiAlias(true); |
| 525 | red.setColor(SK_ColorRED); |
| 526 | |
| 527 | SkPaint green; |
| 528 | green.setAntiAlias(true); |
| 529 | green.setColor(SK_ColorGREEN); |
| 530 | |
| 531 | SkPaint gray; |
| 532 | gray.setColor(SK_ColorLTGRAY); |
| 533 | |
| 534 | SkPaint yellow; |
| 535 | yellow.setColor(SK_ColorYELLOW); |
| 536 | |
| 537 | SkPaint magenta; |
| 538 | magenta.setAntiAlias(true); |
| 539 | magenta.setColor(SK_ColorMAGENTA); |
| 540 | |
| 541 | TextStyle style; |
| 542 | style.setFontFamilies({SkString(ff)}); |
| 543 | style.setFontSize(fs); |
| 544 | |
| 545 | TextStyle style0; |
| 546 | style0.setForegroundColor(black); |
| 547 | style0.setBackgroundColor(gray); |
| 548 | style0.setFontFamilies({SkString(ff)}); |
| 549 | style0.setFontSize(fs); |
| 550 | style0.setDecoration(TextDecoration::kUnderline); |
| 551 | style0.setDecorationStyle(TextDecorationStyle::kDouble); |
| 552 | style0.setDecorationColor(SK_ColorBLACK); |
| 553 | |
| 554 | TextStyle style1; |
| 555 | style1.setForegroundColor(blue); |
| 556 | style1.setBackgroundColor(yellow); |
| 557 | style1.setFontFamilies({SkString(ff)}); |
| 558 | style1.setFontSize(fs); |
| 559 | style1.setDecoration(TextDecoration::kOverline); |
| 560 | style1.setDecorationStyle(TextDecorationStyle::kWavy); |
| 561 | style1.setDecorationColor(SK_ColorBLACK); |
| 562 | |
| 563 | TextStyle style2; |
| 564 | style2.setForegroundColor(red); |
| 565 | style2.setFontFamilies({SkString(ff)}); |
| 566 | style2.setFontSize(fs); |
| 567 | |
| 568 | TextStyle style3; |
| 569 | style3.setForegroundColor(green); |
| 570 | style3.setFontFamilies({SkString(ff)}); |
| 571 | style3.setFontSize(fs); |
| 572 | |
| 573 | TextStyle style4; |
| 574 | style4.setForegroundColor(magenta); |
| 575 | style4.setFontFamilies({SkString(ff)}); |
| 576 | style4.setFontSize(fs); |
| 577 | |
| 578 | ParagraphStyle paraStyle; |
| 579 | paraStyle.setTextStyle(style); |
| 580 | paraStyle.setMaxLines(lineLimit); |
| 581 | |
| 582 | paraStyle.setEllipsis(ellipsis); |
| 583 | |
| 584 | const char* logo1 = "google_"; |
| 585 | const char* logo2 = "logo"; |
| 586 | const char* logo3 = "go"; |
| 587 | const char* logo4 = "ogle_logo"; |
| 588 | const char* logo5 = "google_lo"; |
| 589 | const char* logo6 = "go"; |
| 590 | { |
Julia Lavrova | 9af5cc4 | 2019-06-19 13:32:01 -0400 | [diff] [blame] | 591 | auto fontCollection = sk_make_sp<TestFontCollection>(GetResourcePath("fonts").c_str()); |
| 592 | SkASSERT(fontCollection->fontsFound() != 0); |
| 593 | ParagraphBuilderImpl builder(paraStyle, fontCollection); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 594 | |
| 595 | builder.pushStyle(style0); |
| 596 | builder.addText(logo1); |
| 597 | builder.pop(); |
| 598 | builder.pushStyle(style1); |
| 599 | builder.addText(logo2); |
| 600 | builder.pop(); |
| 601 | |
| 602 | builder.addText(" "); |
| 603 | |
| 604 | builder.pushStyle(style0); |
| 605 | builder.addText(logo3); |
| 606 | builder.pop(); |
| 607 | builder.pushStyle(style1); |
| 608 | builder.addText(logo4); |
| 609 | builder.pop(); |
| 610 | |
| 611 | builder.addText(" "); |
| 612 | |
| 613 | builder.pushStyle(style0); |
| 614 | builder.addText(logo5); |
| 615 | builder.pop(); |
| 616 | builder.pushStyle(style1); |
| 617 | builder.addText(logo6); |
| 618 | builder.pop(); |
| 619 | |
| 620 | auto paragraph = builder.Build(); |
| 621 | paragraph->layout(w - margin * 2); |
| 622 | paragraph->paint(canvas, margin, margin); |
| 623 | canvas->translate(0, h + margin); |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | void onDrawContent(SkCanvas* canvas) override { |
| 628 | canvas->drawColor(SK_ColorWHITE); |
| 629 | SkScalar width = this->width(); |
| 630 | SkScalar height = this->height(); |
| 631 | |
| 632 | drawFlutter(canvas, width, height / 2); |
| 633 | } |
| 634 | |
| 635 | private: |
| 636 | typedef Sample INHERITED; |
| 637 | }; |
| 638 | |
| 639 | class ParagraphView5 : public Sample { |
| 640 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 641 | SkString name() override { return SkString("Paragraph4"); } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 642 | |
| 643 | void bidi(SkCanvas* canvas, SkScalar w, SkScalar h, const std::u16string& text, |
| 644 | const std::u16string& expected, size_t lineLimit = std::numeric_limits<size_t>::max(), |
| 645 | const char* ff = "Roboto", SkScalar fs = 30, |
| 646 | const std::u16string& ellipsis = u"\u2026") { |
| 647 | SkAutoCanvasRestore acr(canvas, true); |
| 648 | |
| 649 | canvas->clipRect(SkRect::MakeWH(w, h)); |
| 650 | |
| 651 | SkScalar margin = 20; |
| 652 | |
| 653 | SkPaint black; |
| 654 | black.setColor(SK_ColorBLACK); |
| 655 | SkPaint gray; |
| 656 | gray.setColor(SK_ColorLTGRAY); |
| 657 | |
| 658 | TextStyle style; |
| 659 | style.setForegroundColor(black); |
| 660 | style.setFontFamilies({SkString(ff)}); |
| 661 | style.setFontSize(fs); |
| 662 | |
| 663 | TextStyle style0; |
| 664 | style0.setForegroundColor(black); |
| 665 | style0.setFontFamilies({SkString(ff)}); |
| 666 | style0.setFontSize(fs); |
| 667 | style0.setFontStyle(SkFontStyle(SkFontStyle::kNormal_Weight, SkFontStyle::kNormal_Width, |
| 668 | SkFontStyle::kItalic_Slant)); |
| 669 | |
| 670 | TextStyle style1; |
| 671 | style1.setForegroundColor(gray); |
| 672 | style1.setFontFamilies({SkString(ff)}); |
| 673 | style1.setFontSize(fs); |
| 674 | style1.setFontStyle(SkFontStyle(SkFontStyle::kBold_Weight, SkFontStyle::kNormal_Width, |
| 675 | SkFontStyle::kUpright_Slant)); |
| 676 | |
| 677 | ParagraphStyle paraStyle; |
| 678 | paraStyle.setTextStyle(style); |
| 679 | paraStyle.setMaxLines(lineLimit); |
| 680 | |
| 681 | paraStyle.setEllipsis(ellipsis); |
| 682 | |
Julia Lavrova | 9af5cc4 | 2019-06-19 13:32:01 -0400 | [diff] [blame] | 683 | auto fontCollection = sk_make_sp<TestFontCollection>(GetResourcePath("fonts").c_str()); |
| 684 | SkASSERT(fontCollection->fontsFound() != 0); |
| 685 | ParagraphBuilderImpl builder(paraStyle, fontCollection); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 686 | |
| 687 | if (text.empty()) { |
| 688 | const std::u16string text0 = u"\u202Dabc"; |
| 689 | const std::u16string text1 = u"\u202EFED"; |
| 690 | const std::u16string text2 = u"\u202Dghi"; |
| 691 | const std::u16string text3 = u"\u202ELKJ"; |
| 692 | const std::u16string text4 = u"\u202Dmno"; |
| 693 | builder.pushStyle(style0); |
| 694 | builder.addText(text0); |
| 695 | builder.pop(); |
| 696 | builder.pushStyle(style1); |
| 697 | builder.addText(text1); |
| 698 | builder.pop(); |
| 699 | builder.pushStyle(style0); |
| 700 | builder.addText(text2); |
| 701 | builder.pop(); |
| 702 | builder.pushStyle(style1); |
| 703 | builder.addText(text3); |
| 704 | builder.pop(); |
| 705 | builder.pushStyle(style0); |
| 706 | builder.addText(text4); |
| 707 | builder.pop(); |
| 708 | } else { |
| 709 | // icu::UnicodeString unicode((UChar*) text.data(), SkToS32(text.size())); |
| 710 | // std::string str; |
| 711 | // unicode.toUTF8String(str); |
| 712 | // SkDebugf("Text: %s\n", str.c_str()); |
| 713 | builder.addText(text + expected); |
| 714 | } |
| 715 | |
| 716 | auto paragraph = builder.Build(); |
| 717 | paragraph->layout(w - margin * 2); |
| 718 | paragraph->paint(canvas, margin, margin); |
| 719 | } |
| 720 | |
| 721 | void onDrawContent(SkCanvas* canvas) override { |
| 722 | canvas->drawColor(SK_ColorWHITE); |
| 723 | SkScalar width = this->width(); |
| 724 | SkScalar height = this->height() / 8; |
| 725 | |
| 726 | const std::u16string text1 = |
| 727 | u"A \u202ENAC\u202Cner, exceedingly \u202ENAC\u202Cny,\n" |
| 728 | "One morning remarked to his granny:\n" |
| 729 | "A \u202ENAC\u202Cner \u202ENAC\u202C \u202ENAC\u202C,\n" |
| 730 | "Anything that he \u202ENAC\u202C,\n" |
| 731 | "But a \u202ENAC\u202Cner \u202ENAC\u202C't \u202ENAC\u202C a \u202ENAC\u202C, " |
| 732 | "\u202ENAC\u202C he?"; |
| 733 | bidi(canvas, width, height * 3, text1, u"", 5); |
| 734 | canvas->translate(0, height * 3); |
| 735 | |
| 736 | bidi(canvas, width, height, u"\u2067DETALOSI\u2069", u""); |
| 737 | canvas->translate(0, height); |
| 738 | |
| 739 | bidi(canvas, width, height, u"\u202BDEDDEBME\u202C", u""); |
| 740 | canvas->translate(0, height); |
| 741 | |
| 742 | bidi(canvas, width, height, u"\u202EEDIRREVO\u202C", u""); |
| 743 | canvas->translate(0, height); |
| 744 | |
| 745 | bidi(canvas, width, height, u"\u200FTICILPMI\u200E", u""); |
| 746 | canvas->translate(0, height); |
| 747 | |
| 748 | bidi(canvas, width, height, u"123 456 7890 \u202EZYXWV UTS RQP ONM LKJ IHG FED CBA\u202C.", |
| 749 | u"", 2); |
| 750 | canvas->translate(0, height); |
| 751 | |
| 752 | // bidi(canvas, width, height, u"", u""); |
| 753 | // canvas->translate(0, height); |
| 754 | } |
| 755 | |
| 756 | private: |
| 757 | typedef Sample INHERITED; |
| 758 | }; |
| 759 | |
| 760 | class ParagraphView6 : public Sample { |
| 761 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 762 | SkString name() override { return SkString("Paragraph4"); } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 763 | |
| 764 | void hangingS(SkCanvas* canvas, SkScalar w, SkScalar h, SkScalar fs = 60.0) { |
| 765 | auto ff = "HangingS"; |
| 766 | |
| 767 | canvas->drawColor(SK_ColorLTGRAY); |
| 768 | |
| 769 | SkPaint black; |
| 770 | black.setAntiAlias(true); |
| 771 | black.setColor(SK_ColorBLACK); |
| 772 | |
| 773 | SkPaint blue; |
| 774 | blue.setAntiAlias(true); |
| 775 | blue.setColor(SK_ColorBLUE); |
| 776 | |
| 777 | SkPaint red; |
| 778 | red.setAntiAlias(true); |
| 779 | red.setColor(SK_ColorRED); |
| 780 | |
| 781 | SkPaint green; |
| 782 | green.setAntiAlias(true); |
| 783 | green.setColor(SK_ColorGREEN); |
| 784 | |
| 785 | SkPaint gray; |
| 786 | gray.setColor(SK_ColorCYAN); |
| 787 | |
| 788 | SkPaint yellow; |
| 789 | yellow.setColor(SK_ColorYELLOW); |
| 790 | |
| 791 | SkPaint magenta; |
| 792 | magenta.setAntiAlias(true); |
| 793 | magenta.setColor(SK_ColorMAGENTA); |
| 794 | |
| 795 | SkFontStyle fontStyle(SkFontStyle::kBold_Weight, SkFontStyle::kNormal_Width, |
| 796 | SkFontStyle::kItalic_Slant); |
| 797 | |
| 798 | TextStyle style; |
| 799 | style.setFontFamilies({SkString(ff)}); |
| 800 | style.setFontSize(fs); |
| 801 | style.setFontStyle(fontStyle); |
| 802 | |
| 803 | TextStyle style0; |
| 804 | style0.setForegroundColor(black); |
| 805 | style0.setBackgroundColor(gray); |
| 806 | style0.setFontFamilies({SkString(ff)}); |
| 807 | style0.setFontSize(fs); |
| 808 | style0.setFontStyle(fontStyle); |
| 809 | |
| 810 | TextStyle style1; |
| 811 | style1.setForegroundColor(blue); |
| 812 | style1.setBackgroundColor(yellow); |
| 813 | style1.setFontFamilies({SkString(ff)}); |
| 814 | style1.setFontSize(fs); |
| 815 | style1.setFontStyle(fontStyle); |
| 816 | |
| 817 | TextStyle style2; |
| 818 | style2.setForegroundColor(red); |
| 819 | style2.setFontFamilies({SkString(ff)}); |
| 820 | style2.setFontSize(fs); |
| 821 | style2.setFontStyle(fontStyle); |
| 822 | |
| 823 | TextStyle style3; |
| 824 | style3.setForegroundColor(green); |
| 825 | style3.setFontFamilies({SkString(ff)}); |
| 826 | style3.setFontSize(fs); |
| 827 | style3.setFontStyle(fontStyle); |
| 828 | |
| 829 | TextStyle style4; |
| 830 | style4.setForegroundColor(magenta); |
| 831 | style4.setFontFamilies({SkString(ff)}); |
| 832 | style4.setFontSize(fs); |
| 833 | style4.setFontStyle(fontStyle); |
| 834 | |
| 835 | ParagraphStyle paraStyle; |
| 836 | paraStyle.setTextStyle(style); |
| 837 | |
| 838 | const char* logo1 = "S"; |
| 839 | const char* logo2 = "kia"; |
| 840 | const char* logo3 = "Sk"; |
| 841 | const char* logo4 = "ia"; |
| 842 | const char* logo5 = "Ski"; |
| 843 | const char* logo6 = "a"; |
| 844 | { |
Julia Lavrova | 9af5cc4 | 2019-06-19 13:32:01 -0400 | [diff] [blame] | 845 | auto fontCollection = sk_make_sp<TestFontCollection>(GetResourcePath("fonts").c_str()); |
| 846 | SkASSERT(fontCollection->fontsFound() != 0); |
| 847 | ParagraphBuilderImpl builder(paraStyle, fontCollection); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 848 | |
| 849 | builder.pushStyle(style0); |
| 850 | builder.addText(logo1); |
| 851 | builder.pop(); |
| 852 | builder.pushStyle(style1); |
| 853 | builder.addText(logo2); |
| 854 | builder.pop(); |
| 855 | |
| 856 | builder.addText(" "); |
| 857 | |
| 858 | builder.pushStyle(style0); |
| 859 | builder.addText(logo3); |
| 860 | builder.pop(); |
| 861 | builder.pushStyle(style1); |
| 862 | builder.addText(logo4); |
| 863 | builder.pop(); |
| 864 | |
| 865 | builder.addText(" "); |
| 866 | |
| 867 | builder.pushStyle(style0); |
| 868 | builder.addText(logo5); |
| 869 | builder.pop(); |
| 870 | builder.pushStyle(style1); |
| 871 | builder.addText(logo6); |
| 872 | builder.pop(); |
| 873 | |
| 874 | auto paragraph = builder.Build(); |
| 875 | paragraph->layout(w); |
| 876 | paragraph->paint(canvas, 40, 40); |
| 877 | canvas->translate(0, h); |
| 878 | } |
| 879 | |
| 880 | const char* logo11 = "S"; |
| 881 | const char* logo12 = "S"; |
| 882 | const char* logo13 = "S"; |
| 883 | const char* logo14 = "S"; |
| 884 | const char* logo15 = "S"; |
| 885 | const char* logo16 = "S"; |
| 886 | { |
Julia Lavrova | 9af5cc4 | 2019-06-19 13:32:01 -0400 | [diff] [blame] | 887 | auto fontCollection = sk_make_sp<TestFontCollection>(GetResourcePath("fonts").c_str()); |
| 888 | SkASSERT(fontCollection->fontsFound() != 0); |
| 889 | ParagraphBuilderImpl builder(paraStyle, fontCollection); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 890 | |
| 891 | builder.pushStyle(style0); |
| 892 | builder.addText(logo11); |
| 893 | builder.pop(); |
| 894 | builder.pushStyle(style1); |
| 895 | builder.addText(logo12); |
| 896 | builder.pop(); |
| 897 | |
| 898 | builder.addText(" "); |
| 899 | |
| 900 | builder.pushStyle(style0); |
| 901 | builder.addText(logo13); |
| 902 | builder.pop(); |
| 903 | builder.pushStyle(style1); |
| 904 | builder.addText(logo14); |
| 905 | builder.pop(); |
| 906 | |
| 907 | builder.addText(" "); |
| 908 | |
| 909 | builder.pushStyle(style0); |
| 910 | builder.addText(logo15); |
| 911 | builder.pop(); |
| 912 | builder.pushStyle(style1); |
| 913 | builder.addText(logo16); |
| 914 | builder.pop(); |
| 915 | |
| 916 | auto paragraph = builder.Build(); |
| 917 | paragraph->layout(w); |
| 918 | paragraph->paint(canvas, 40, h); |
| 919 | canvas->translate(0, h); |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | void onDrawContent(SkCanvas* canvas) override { |
| 924 | canvas->drawColor(SK_ColorWHITE); |
| 925 | SkScalar width = this->width(); |
| 926 | SkScalar height = this->height() / 4; |
| 927 | |
| 928 | hangingS(canvas, width, height); |
| 929 | } |
| 930 | |
| 931 | private: |
| 932 | typedef Sample INHERITED; |
| 933 | }; |
| 934 | |
| 935 | class ParagraphView7 : public Sample { |
| 936 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 937 | SkString name() override { return SkString("Paragraph7"); } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 938 | |
| 939 | void drawText(SkCanvas* canvas, SkColor background, SkScalar letterSpace, SkScalar w, |
| 940 | SkScalar h) { |
| 941 | SkAutoCanvasRestore acr(canvas, true); |
| 942 | canvas->clipRect(SkRect::MakeWH(w, h)); |
| 943 | canvas->drawColor(background); |
| 944 | |
| 945 | const char* line = |
| 946 | "World domination is such an ugly phrase - I prefer to call it world optimisation"; |
| 947 | |
| 948 | ParagraphStyle paragraphStyle; |
| 949 | paragraphStyle.setTextAlign(TextAlign::kLeft); |
| 950 | paragraphStyle.setMaxLines(10); |
| 951 | paragraphStyle.turnHintingOff(); |
| 952 | TextStyle textStyle; |
| 953 | textStyle.setFontFamilies({SkString("Roboto")}); |
| 954 | textStyle.setFontSize(30); |
| 955 | textStyle.setLetterSpacing(letterSpace); |
| 956 | textStyle.setColor(SK_ColorBLACK); |
| 957 | textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 958 | SkFontStyle::kUpright_Slant)); |
| 959 | |
Julia Lavrova | 9af5cc4 | 2019-06-19 13:32:01 -0400 | [diff] [blame] | 960 | auto fontCollection = sk_make_sp<TestFontCollection>(GetResourcePath("fonts").c_str()); |
| 961 | SkASSERT(fontCollection->fontsFound() != 0); |
| 962 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 963 | builder.pushStyle(textStyle); |
| 964 | builder.addText(line); |
| 965 | builder.pop(); |
| 966 | |
| 967 | auto paragraph = builder.Build(); |
| 968 | paragraph->layout(w - 20); |
| 969 | paragraph->paint(canvas, 10, 10); |
| 970 | } |
| 971 | |
| 972 | void onDrawContent(SkCanvas* canvas) override { |
| 973 | canvas->drawColor(SK_ColorWHITE); |
| 974 | |
| 975 | auto h = this->height() / 4; |
| 976 | auto w = this->width() / 2; |
| 977 | |
| 978 | drawText(canvas, SK_ColorGRAY, 1, w, h); |
| 979 | canvas->translate(0, h); |
| 980 | |
| 981 | drawText(canvas, SK_ColorLTGRAY, 2, w, h); |
| 982 | canvas->translate(0, h); |
| 983 | |
| 984 | drawText(canvas, SK_ColorCYAN, 3, w, h); |
| 985 | canvas->translate(0, h); |
| 986 | |
| 987 | drawText(canvas, SK_ColorGRAY, 4, w, h); |
| 988 | canvas->translate(w, -3 * h); |
| 989 | |
| 990 | drawText(canvas, SK_ColorYELLOW, 5, w, h); |
| 991 | canvas->translate(0, h); |
| 992 | |
| 993 | drawText(canvas, SK_ColorGREEN, 10, w, h); |
| 994 | canvas->translate(0, h); |
| 995 | |
| 996 | drawText(canvas, SK_ColorRED, 15, w, h); |
| 997 | canvas->translate(0, h); |
| 998 | |
| 999 | drawText(canvas, SK_ColorBLUE, 20, w, h); |
| 1000 | canvas->translate(0, h); |
| 1001 | } |
| 1002 | |
| 1003 | private: |
| 1004 | typedef Sample INHERITED; |
| 1005 | }; |
| 1006 | |
| 1007 | class ParagraphView8 : public Sample { |
| 1008 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 1009 | SkString name() override { return SkString("Paragraph7"); } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1010 | |
| 1011 | void drawText(SkCanvas* canvas, SkColor background, SkScalar wordSpace, SkScalar w, |
| 1012 | SkScalar h) { |
| 1013 | SkAutoCanvasRestore acr(canvas, true); |
| 1014 | canvas->clipRect(SkRect::MakeWH(w, h)); |
| 1015 | canvas->drawColor(background); |
| 1016 | |
| 1017 | const char* line = |
| 1018 | "World domination is such an ugly phrase - I prefer to call it world optimisation"; |
| 1019 | |
| 1020 | ParagraphStyle paragraphStyle; |
| 1021 | paragraphStyle.setTextAlign(TextAlign::kLeft); |
| 1022 | paragraphStyle.setMaxLines(10); |
| 1023 | paragraphStyle.turnHintingOff(); |
| 1024 | TextStyle textStyle; |
| 1025 | textStyle.setFontFamilies({SkString("Roboto")}); |
| 1026 | textStyle.setFontSize(30); |
| 1027 | textStyle.setWordSpacing(wordSpace); |
| 1028 | textStyle.setColor(SK_ColorBLACK); |
| 1029 | textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 1030 | SkFontStyle::kUpright_Slant)); |
| 1031 | |
Julia Lavrova | 9af5cc4 | 2019-06-19 13:32:01 -0400 | [diff] [blame] | 1032 | auto fontCollection = sk_make_sp<TestFontCollection>(GetResourcePath("fonts").c_str()); |
| 1033 | SkASSERT(fontCollection->fontsFound() != 0); |
| 1034 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1035 | builder.pushStyle(textStyle); |
| 1036 | builder.addText(line); |
| 1037 | builder.pop(); |
| 1038 | |
| 1039 | auto paragraph = builder.Build(); |
| 1040 | paragraph->layout(w - 20); |
| 1041 | paragraph->paint(canvas, 10, 10); |
| 1042 | } |
| 1043 | |
| 1044 | void onDrawContent(SkCanvas* canvas) override { |
| 1045 | canvas->drawColor(SK_ColorWHITE); |
| 1046 | |
| 1047 | auto h = this->height() / 4; |
| 1048 | auto w = this->width() / 2; |
| 1049 | |
| 1050 | drawText(canvas, SK_ColorGRAY, 1, w, h); |
| 1051 | canvas->translate(0, h); |
| 1052 | |
| 1053 | drawText(canvas, SK_ColorLTGRAY, 2, w, h); |
| 1054 | canvas->translate(0, h); |
| 1055 | |
| 1056 | drawText(canvas, SK_ColorCYAN, 3, w, h); |
| 1057 | canvas->translate(0, h); |
| 1058 | |
| 1059 | drawText(canvas, SK_ColorGRAY, 4, w, h); |
| 1060 | canvas->translate(w, -3 * h); |
| 1061 | |
| 1062 | drawText(canvas, SK_ColorYELLOW, 5, w, h); |
| 1063 | canvas->translate(0, h); |
| 1064 | |
| 1065 | drawText(canvas, SK_ColorGREEN, 10, w, h); |
| 1066 | canvas->translate(0, h); |
| 1067 | |
| 1068 | drawText(canvas, SK_ColorRED, 15, w, h); |
| 1069 | canvas->translate(0, h); |
| 1070 | |
| 1071 | drawText(canvas, SK_ColorBLUE, 20, w, h); |
| 1072 | canvas->translate(0, h); |
| 1073 | } |
| 1074 | |
| 1075 | private: |
| 1076 | typedef Sample INHERITED; |
| 1077 | }; |
| 1078 | |
| 1079 | class ParagraphView9 : public Sample { |
| 1080 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 1081 | SkString name() override { return SkString("Paragraph9"); } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1082 | |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 1083 | bool onQuery(Sample::Event* evt) override { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1084 | SkUnichar uni; |
| 1085 | if (Sample::CharQ(*evt, &uni)) { |
| 1086 | switch (uni) { |
| 1087 | case 'w': |
| 1088 | ++wordSpacing; |
| 1089 | return true; |
| 1090 | case 'q': |
| 1091 | if (wordSpacing > 0) --wordSpacing; |
| 1092 | return true; |
| 1093 | case 'l': |
| 1094 | ++letterSpacing; |
| 1095 | return true; |
| 1096 | case 'k': |
| 1097 | if (letterSpacing > 0) --letterSpacing; |
| 1098 | return true; |
| 1099 | default: |
| 1100 | break; |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | return this->INHERITED::onQuery(evt); |
| 1105 | } |
| 1106 | |
| 1107 | void drawText(SkCanvas* canvas, SkColor background, SkScalar w, SkScalar h) { |
| 1108 | SkAutoCanvasRestore acr(canvas, true); |
| 1109 | canvas->clipRect(SkRect::MakeWH(w, h)); |
| 1110 | canvas->drawColor(background); |
| 1111 | |
| 1112 | const char* text = |
| 1113 | "( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)(" |
| 1114 | " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)(" |
| 1115 | " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)"; |
| 1116 | |
| 1117 | ParagraphStyle paragraphStyle; |
| 1118 | paragraphStyle.setTextAlign(TextAlign::kLeft); |
| 1119 | paragraphStyle.setMaxLines(10); |
| 1120 | paragraphStyle.turnHintingOff(); |
| 1121 | TextStyle textStyle; |
| 1122 | textStyle.setFontFamilies({SkString("Roboto")}); |
| 1123 | textStyle.setFontSize(50); |
| 1124 | textStyle.setHeight(1.3f); |
| 1125 | textStyle.setColor(SK_ColorBLACK); |
| 1126 | textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, |
| 1127 | SkFontStyle::kUpright_Slant)); |
| 1128 | |
Julia Lavrova | 9af5cc4 | 2019-06-19 13:32:01 -0400 | [diff] [blame] | 1129 | auto fontCollection = sk_make_sp<TestFontCollection>(GetResourcePath("fonts").c_str()); |
| 1130 | SkASSERT(fontCollection->fontsFound() != 0); |
| 1131 | ParagraphBuilderImpl builder(paragraphStyle, fontCollection); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1132 | builder.pushStyle(textStyle); |
| 1133 | builder.addText(text); |
| 1134 | builder.pop(); |
| 1135 | |
| 1136 | auto paragraph = builder.Build(); |
| 1137 | paragraph->layout(550); |
| 1138 | |
| 1139 | std::vector<size_t> sizes = {0, 1, 2, 8, 19, 21, 22, 30, 150}; |
| 1140 | |
| 1141 | std::vector<size_t> colors = {SK_ColorBLUE, SK_ColorCYAN, SK_ColorLTGRAY, SK_ColorGREEN, |
| 1142 | SK_ColorRED, SK_ColorWHITE, SK_ColorYELLOW, SK_ColorMAGENTA}; |
| 1143 | |
| 1144 | RectHeightStyle rect_height_style = RectHeightStyle::kTight; |
| 1145 | RectWidthStyle rect_width_style = RectWidthStyle::kTight; |
| 1146 | |
| 1147 | for (size_t i = 0; i < sizes.size() - 1; ++i) { |
| 1148 | size_t from = (i == 0 ? 0 : 1) + sizes[i]; |
| 1149 | size_t to = sizes[i + 1]; |
| 1150 | auto boxes = paragraph->getRectsForRange(from, to, rect_height_style, rect_width_style); |
| 1151 | if (boxes.empty()) { |
| 1152 | continue; |
| 1153 | } |
| 1154 | for (auto& box : boxes) { |
| 1155 | SkPaint paint; |
| 1156 | paint.setColor(colors[i % colors.size()]); |
| 1157 | paint.setShader(setgrad(box.rect, colors[i % colors.size()], SK_ColorWHITE)); |
| 1158 | canvas->drawRect(box.rect, paint); |
| 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | paragraph->paint(canvas, 0, 0); |
| 1163 | } |
| 1164 | |
| 1165 | void onDrawContent(SkCanvas* canvas) override { |
| 1166 | canvas->drawColor(SK_ColorWHITE); |
| 1167 | |
| 1168 | auto h = this->height(); |
| 1169 | auto w = this->width(); |
| 1170 | |
| 1171 | drawText(canvas, SK_ColorGRAY, w, h); |
| 1172 | } |
| 1173 | |
| 1174 | private: |
| 1175 | typedef Sample INHERITED; |
| 1176 | SkScalar letterSpacing; |
| 1177 | SkScalar wordSpacing; |
| 1178 | }; |
| 1179 | |
| 1180 | class ParagraphView10 : public Sample { |
| 1181 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 1182 | SkString name() override { return SkString("Paragraph10"); } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1183 | |
| 1184 | void onDrawContent(SkCanvas* canvas) override { |
| 1185 | canvas->drawColor(SK_ColorWHITE); |
| 1186 | |
| 1187 | const char* text = "English English 字典 字典 😀😃😄 😀😃😄"; |
| 1188 | ParagraphStyle paragraph_style; |
| 1189 | paragraph_style.turnHintingOff(); |
Julia Lavrova | 9af5cc4 | 2019-06-19 13:32:01 -0400 | [diff] [blame] | 1190 | auto fontCollection = sk_make_sp<TestFontCollection>(GetResourcePath("fonts").c_str()); |
| 1191 | SkASSERT(fontCollection->fontsFound() != 0); |
| 1192 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1193 | |
| 1194 | TextStyle text_style; |
| 1195 | text_style.setFontFamilies({SkString("Roboto"), SkString("Noto Color Emoji"), |
| 1196 | SkString("Source Han Serif CN")}); |
| 1197 | text_style.setColor(SK_ColorRED); |
| 1198 | text_style.setFontSize(60); |
| 1199 | text_style.setLetterSpacing(0); |
| 1200 | text_style.setWordSpacing(0); |
| 1201 | text_style.setColor(SK_ColorBLACK); |
| 1202 | text_style.setHeight(1); |
| 1203 | builder.pushStyle(text_style); |
| 1204 | builder.addText(text); |
| 1205 | builder.pop(); |
| 1206 | |
| 1207 | auto paragraph = builder.Build(); |
| 1208 | paragraph->layout(width()); |
| 1209 | |
| 1210 | paragraph->paint(canvas, 0, 0); |
| 1211 | SkDEBUGCODE(auto impl = reinterpret_cast<ParagraphImpl*>(paragraph.get())); |
| 1212 | SkASSERT(impl->runs().size() == 3); |
| 1213 | SkASSERT(impl->runs()[0].text().end() == impl->runs()[1].text().begin()); |
| 1214 | SkASSERT(impl->runs()[1].text().end() == impl->runs()[2].text().begin()); |
| 1215 | } |
| 1216 | |
| 1217 | private: |
| 1218 | typedef Sample INHERITED; |
| 1219 | }; |
| 1220 | |
| 1221 | class ParagraphView11 : public Sample { |
| 1222 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 1223 | SkString name() override { return SkString("Paragraph11"); } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1224 | |
| 1225 | void onDrawContent(SkCanvas* canvas) override { |
| 1226 | canvas->drawColor(SK_ColorWHITE); |
Julia Lavrova | 9af5cc4 | 2019-06-19 13:32:01 -0400 | [diff] [blame] | 1227 | /* |
Julia Lavrova | 6e6333f | 2019-06-17 10:34:10 -0400 | [diff] [blame] | 1228 | const char* text = |
| 1229 | "// Create a raised button.\n" |
| 1230 | "RaisedButton(\n" |
| 1231 | " child: const Text('BUTTON TITLE'),\n" |
| 1232 | " onPressed: () {\n" |
| 1233 | " // Perform some action\n" |
| 1234 | " },\n" |
| 1235 | ");\n" |
| 1236 | "\n" |
| 1237 | "// Create a disabled button.\n" |
| 1238 | "// Buttons are disabled when onPressed isn't\n" |
| 1239 | "// specified or is null.\n" |
| 1240 | "const RaisedButton(\n" |
| 1241 | " child: Text('BUTTON TITLE'),\n" |
| 1242 | " onPressed: null,\n" |
| 1243 | ");\n" |
| 1244 | "\n" |
| 1245 | "// Create a button with an icon and a\n" |
| 1246 | "// title.\n" |
| 1247 | "RaisedButton.icon(\n" |
| 1248 | " icon: const Icon(Icons.add, size: 18.0),\n" |
| 1249 | " label: const Text('BUTTON TITLE'),\n" |
| 1250 | " onPressed: () {\n" |
| 1251 | " // Perform some action\n" |
| 1252 | " },\n" |
| 1253 | ");"; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1254 | ParagraphStyle paragraph_style; |
| 1255 | paragraph_style.turnHintingOff(); |
| 1256 | ParagraphBuilderImpl builder(paragraph_style, sk_make_sp<FontCollection>()); |
| 1257 | |
| 1258 | TextStyle text_style; |
Julia Lavrova | 9af5cc4 | 2019-06-19 13:32:01 -0400 | [diff] [blame] | 1259 | text_style.setFontFamilies({}); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1260 | text_style.setColor(SK_ColorBLACK); |
Julia Lavrova | 9af5cc4 | 2019-06-19 13:32:01 -0400 | [diff] [blame] | 1261 | text_style.setFontSize(20); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1262 | builder.pushStyle(text_style); |
| 1263 | builder.addText(text); |
| 1264 | builder.pop(); |
| 1265 | |
| 1266 | auto paragraph = builder.Build(); |
Julia Lavrova | 9af5cc4 | 2019-06-19 13:32:01 -0400 | [diff] [blame] | 1267 | paragraph->layout(500); |
| 1268 | |
Julia Lavrova | f39124b | 2019-06-11 16:45:49 -0400 | [diff] [blame] | 1269 | auto result = |
| 1270 | paragraph->getRectsForRange(0, 1, RectHeightStyle::kTight, RectWidthStyle::kTight); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1271 | SkPaint paint; |
| 1272 | paint.setColor(SK_ColorLTGRAY); |
| 1273 | canvas->drawRect(result[0].rect, paint); |
| 1274 | paragraph->paint(canvas, 0, 0); |
Julia Lavrova | 9af5cc4 | 2019-06-19 13:32:01 -0400 | [diff] [blame] | 1275 | */ |
| 1276 | std::vector<uint16_t> text; |
| 1277 | for (uint16_t i = 0; i < 64; ++i) { |
| 1278 | text.push_back(i % 5 == 0 ? ' ' : i); |
| 1279 | } |
| 1280 | std::u16string u16_text(text.data(), text.data() + text.size()); |
| 1281 | TextStyle default_style; |
| 1282 | default_style.setFontFamilies({SkString("Roboto")}); |
| 1283 | ParagraphStyle paragraph_style; |
| 1284 | paragraph_style.setTextStyle(default_style); |
| 1285 | TextStyle text_style; |
| 1286 | text_style.setFontFamilies({SkString("Roboto")}); |
| 1287 | text_style.setColor(SK_ColorBLACK); |
| 1288 | auto fontCollection = sk_make_sp<TestFontCollection>(GetResourcePath("fonts").c_str()); |
| 1289 | SkASSERT(fontCollection->fontsFound() != 0); |
| 1290 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 1291 | builder.pushStyle(text_style); |
| 1292 | builder.addText(u16_text); |
| 1293 | builder.pop(); |
| 1294 | auto paragraph = builder.Build(); |
| 1295 | size_t count = 10; |
| 1296 | while (--count > 0) { |
| 1297 | paragraph->layout(300); |
| 1298 | paragraph->paint(canvas, 0, 0); |
| 1299 | } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | private: |
| 1303 | typedef Sample INHERITED; |
| 1304 | }; |
| 1305 | ////////////////////////////////////////////////////////////////////////////// |
| 1306 | |
| 1307 | DEF_SAMPLE(return new ParagraphView1();) |
| 1308 | DEF_SAMPLE(return new ParagraphView2();) |
| 1309 | DEF_SAMPLE(return new ParagraphView3();) |
| 1310 | DEF_SAMPLE(return new ParagraphView4();) |
| 1311 | DEF_SAMPLE(return new ParagraphView5();) |
| 1312 | DEF_SAMPLE(return new ParagraphView6();) |
| 1313 | DEF_SAMPLE(return new ParagraphView7();) |
| 1314 | DEF_SAMPLE(return new ParagraphView8();) |
| 1315 | DEF_SAMPLE(return new ParagraphView9();) |
| 1316 | DEF_SAMPLE(return new ParagraphView10();) |
| 1317 | DEF_SAMPLE(return new ParagraphView11();) |