SampleParagraph.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  1. // Copyright 2019 Google LLC.
  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"
  17. #include "modules/skparagraph/include/TypefaceFontProvider.h"
  18. #include "modules/skparagraph/src/ParagraphBuilderImpl.h"
  19. #include "modules/skparagraph/src/ParagraphImpl.h"
  20. #include "modules/skparagraph/utils/TestFontCollection.h"
  21. #include "samplecode/Sample.h"
  22. #include "src/core/SkOSFile.h"
  23. #include "src/shaders/SkColorShader.h"
  24. #include "src/utils/SkUTF.h"
  25. #include "tools/Resources.h"
  26. using namespace skia::textlayout;
  27. namespace {
  28. class ParagraphView_Base : public Sample {
  29. protected:
  30. sk_sp<TestFontCollection> fFC;
  31. void onOnceBeforeDraw() override {
  32. fFC = sk_make_sp<TestFontCollection>(GetResourcePath("fonts").c_str());
  33. }
  34. };
  35. sk_sp<SkShader> setgrad(const SkRect& r, SkColor c0, SkColor c1) {
  36. SkColor colors[] = {c0, c1};
  37. SkPoint pts[] = {{r.fLeft, r.fTop}, {r.fRight, r.fTop}};
  38. return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
  39. }
  40. const char* gText =
  41. "This is a very long sentence to test if the text will properly wrap "
  42. "around and go to the next line. Sometimes, short sentence. Longer "
  43. "sentences are okay too because they are nessecary. Very short. "
  44. "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
  45. "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
  46. "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
  47. "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
  48. "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
  49. "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
  50. "mollit anim id est laborum. "
  51. "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
  52. "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
  53. "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
  54. "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
  55. "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
  56. "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
  57. "mollit anim id est laborum.";
  58. } // namespace
  59. class ParagraphView1 : public ParagraphView_Base {
  60. protected:
  61. SkString name() override { return SkString("Paragraph1"); }
  62. void drawTest(SkCanvas* canvas, SkScalar w, SkScalar h, SkColor fg, SkColor bg) {
  63. const std::vector<
  64. std::tuple<std::string, bool, bool, int, SkColor, SkColor, bool, TextDecorationStyle>>
  65. gParagraph = {{"monospace", true, false, 14, SK_ColorWHITE, SK_ColorRED, true,
  66. TextDecorationStyle::kDashed},
  67. {"Assyrian", false, false, 20, SK_ColorWHITE, SK_ColorBLUE, false,
  68. TextDecorationStyle::kDotted},
  69. {"serif", true, true, 10, SK_ColorWHITE, SK_ColorRED, true,
  70. TextDecorationStyle::kDouble},
  71. {"Arial", false, true, 16, SK_ColorGRAY, SK_ColorGREEN, true,
  72. TextDecorationStyle::kSolid},
  73. {"sans-serif", false, false, 8, SK_ColorWHITE, SK_ColorRED, false,
  74. TextDecorationStyle::kWavy}};
  75. SkAutoCanvasRestore acr(canvas, true);
  76. canvas->clipRect(SkRect::MakeWH(w, h));
  77. canvas->drawColor(SK_ColorWHITE);
  78. SkScalar margin = 20;
  79. SkPaint paint;
  80. paint.setAntiAlias(true);
  81. paint.setColor(fg);
  82. SkPaint blue;
  83. blue.setColor(SK_ColorBLUE);
  84. TextStyle defaultStyle;
  85. defaultStyle.setBackgroundColor(blue);
  86. defaultStyle.setForegroundColor(paint);
  87. ParagraphStyle paraStyle;
  88. for (auto i = 1; i < 5; ++i) {
  89. defaultStyle.setFontSize(24 * i);
  90. paraStyle.setTextStyle(defaultStyle);
  91. auto fontCollection = sk_make_sp<FontCollection>();
  92. fontCollection->setDefaultFontManager(SkFontMgr::RefDefault());
  93. ParagraphBuilderImpl builder(paraStyle, fontCollection);
  94. std::string name = "Paragraph: " + std::to_string(24 * i);
  95. builder.addText(name.c_str());
  96. for (auto para : gParagraph) {
  97. TextStyle style;
  98. style.setFontFamilies({SkString(std::get<0>(para).c_str())});
  99. SkFontStyle fontStyle(std::get<1>(para) ? SkFontStyle::Weight::kBold_Weight
  100. : SkFontStyle::Weight::kNormal_Weight,
  101. SkFontStyle::Width::kNormal_Width,
  102. std::get<2>(para) ? SkFontStyle::Slant::kItalic_Slant
  103. : SkFontStyle::Slant::kUpright_Slant);
  104. style.setFontStyle(fontStyle);
  105. style.setFontSize(std::get<3>(para) * i);
  106. SkPaint background;
  107. background.setColor(std::get<4>(para));
  108. style.setBackgroundColor(background);
  109. SkPaint foreground;
  110. foreground.setColor(std::get<5>(para));
  111. foreground.setAntiAlias(true);
  112. style.setForegroundColor(foreground);
  113. if (std::get<6>(para)) {
  114. style.addShadow(TextShadow(SK_ColorBLACK, SkPoint::Make(5, 5), 2));
  115. }
  116. auto decoration = (i % 4);
  117. if (decoration == 3) {
  118. decoration = 4;
  119. }
  120. bool test = (TextDecoration)decoration != TextDecoration::kNoDecoration;
  121. std::string deco = std::to_string((int)decoration);
  122. if (test) {
  123. style.setDecoration((TextDecoration)decoration);
  124. style.setDecorationStyle(std::get<7>(para));
  125. style.setDecorationColor(std::get<5>(para));
  126. }
  127. builder.pushStyle(style);
  128. std::string name = " " + std::get<0>(para) + " " +
  129. (std::get<1>(para) ? ", bold" : "") +
  130. (std::get<2>(para) ? ", italic" : "") + " " +
  131. std::to_string(std::get<3>(para) * i) +
  132. (std::get<4>(para) != bg ? ", background" : "") +
  133. (std::get<5>(para) != fg ? ", foreground" : "") +
  134. (std::get<6>(para) ? ", shadow" : "") +
  135. (test ? ", decorations " + deco : "") + ";";
  136. builder.addText(name.c_str());
  137. builder.pop();
  138. }
  139. auto paragraph = builder.Build();
  140. paragraph->layout(w - margin * 2);
  141. paragraph->paint(canvas, margin, margin);
  142. canvas->translate(0, paragraph->getHeight());
  143. }
  144. }
  145. void onDrawContent(SkCanvas* canvas) override {
  146. drawTest(canvas, this->width(), this->height(), SK_ColorRED, SK_ColorWHITE);
  147. }
  148. private:
  149. typedef Sample INHERITED;
  150. };
  151. class ParagraphView2 : public ParagraphView_Base {
  152. protected:
  153. SkString name() override { return SkString("Paragraph2"); }
  154. void drawCode(SkCanvas* canvas, SkScalar w, SkScalar h) {
  155. SkPaint comment;
  156. comment.setColor(SK_ColorGRAY);
  157. SkPaint constant;
  158. constant.setColor(SK_ColorMAGENTA);
  159. SkPaint null;
  160. null.setColor(SK_ColorMAGENTA);
  161. SkPaint literal;
  162. literal.setColor(SK_ColorGREEN);
  163. SkPaint code;
  164. code.setColor(SK_ColorDKGRAY);
  165. SkPaint number;
  166. number.setColor(SK_ColorBLUE);
  167. SkPaint name;
  168. name.setColor(SK_ColorRED);
  169. SkPaint white;
  170. white.setColor(SK_ColorWHITE);
  171. TextStyle defaultStyle;
  172. defaultStyle.setBackgroundColor(white);
  173. defaultStyle.setForegroundColor(code);
  174. defaultStyle.setFontFamilies({SkString("monospace")});
  175. defaultStyle.setFontSize(30);
  176. ParagraphStyle paraStyle;
  177. paraStyle.setTextStyle(defaultStyle);
  178. auto fontCollection = sk_make_sp<FontCollection>();
  179. fontCollection->setDefaultFontManager(SkFontMgr::RefDefault());
  180. ParagraphBuilderImpl builder(paraStyle, fontCollection);
  181. builder.pushStyle(style(name));
  182. builder.addText("RaisedButton");
  183. builder.pop();
  184. builder.addText("(\n");
  185. builder.addText(" child: ");
  186. builder.pushStyle(style(constant));
  187. builder.addText("const");
  188. builder.pop();
  189. builder.addText(" ");
  190. builder.pushStyle(style(name));
  191. builder.addText("Text");
  192. builder.pop();
  193. builder.addText("(");
  194. builder.pushStyle(style(literal));
  195. builder.addText("'BUTTON TITLE'");
  196. builder.pop();
  197. builder.addText("),\n");
  198. auto paragraph = builder.Build();
  199. paragraph->layout(w - 20);
  200. paragraph->paint(canvas, 20, 20);
  201. }
  202. TextStyle style(SkPaint paint) {
  203. TextStyle style;
  204. paint.setAntiAlias(true);
  205. style.setForegroundColor(paint);
  206. style.setFontFamilies({SkString("monospace")});
  207. style.setFontSize(30);
  208. return style;
  209. }
  210. void drawText(SkCanvas* canvas, SkScalar w, SkScalar h, std::vector<const char*>& 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. canvas->clipRect(SkRect::MakeWH(w, h));
  217. canvas->drawColor(bg);
  218. SkScalar margin = 20;
  219. SkPaint paint;
  220. paint.setAntiAlias(true);
  221. paint.setColor(fg);
  222. SkPaint blue;
  223. blue.setColor(SK_ColorBLUE);
  224. SkPaint background;
  225. background.setColor(bg);
  226. TextStyle style;
  227. style.setBackgroundColor(blue);
  228. style.setForegroundColor(paint);
  229. style.setFontFamilies({SkString(ff)});
  230. style.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight,
  231. SkFontStyle::kNormal_Width,
  232. SkFontStyle::kUpright_Slant));
  233. style.setFontSize(fs);
  234. ParagraphStyle paraStyle;
  235. paraStyle.setTextStyle(style);
  236. paraStyle.setMaxLines(lineLimit);
  237. paraStyle.setEllipsis(ellipsis);
  238. TextStyle defaultStyle;
  239. defaultStyle.setFontSize(20);
  240. paraStyle.setTextStyle(defaultStyle);
  241. ParagraphBuilderImpl builder(paraStyle, fFC);
  242. SkPaint foreground;
  243. foreground.setColor(fg);
  244. style.setForegroundColor(foreground);
  245. style.setBackgroundColor(background);
  246. for (auto& part : text) {
  247. builder.pushStyle(style);
  248. builder.addText(part);
  249. builder.pop();
  250. }
  251. auto paragraph = builder.Build();
  252. paragraph->layout(w - margin * 2);
  253. paragraph->paint(canvas, margin, margin);
  254. canvas->translate(0, paragraph->getHeight() + margin);
  255. }
  256. void drawLine(SkCanvas* canvas, SkScalar w, SkScalar h, const std::string& text,
  257. TextAlign align) {
  258. SkAutoCanvasRestore acr(canvas, true);
  259. canvas->clipRect(SkRect::MakeWH(w, h));
  260. canvas->drawColor(SK_ColorWHITE);
  261. SkScalar margin = 20;
  262. SkPaint paint;
  263. paint.setAntiAlias(true);
  264. paint.setColor(SK_ColorBLUE);
  265. SkPaint gray;
  266. gray.setColor(SK_ColorLTGRAY);
  267. TextStyle style;
  268. style.setBackgroundColor(gray);
  269. style.setForegroundColor(paint);
  270. style.setFontFamilies({SkString("Arial")});
  271. style.setFontSize(30);
  272. ParagraphStyle paraStyle;
  273. paraStyle.setTextStyle(style);
  274. paraStyle.setTextAlign(align);
  275. auto fontCollection = sk_make_sp<FontCollection>();
  276. fontCollection->setDefaultFontManager(SkFontMgr::RefDefault());
  277. ParagraphBuilderImpl builder(paraStyle, fontCollection);
  278. builder.addText(text.c_str());
  279. auto paragraph = builder.Build();
  280. paragraph->layout(w - margin * 2);
  281. paragraph->layout(w - margin);
  282. paragraph->paint(canvas, margin, margin);
  283. canvas->translate(0, paragraph->getHeight() + margin);
  284. }
  285. void onDrawContent(SkCanvas* canvas) override {
  286. std::vector<const char*> cupertino = {
  287. "google_logogoogle_gsuper_g_logo 1 "
  288. "google_logogoogle_gsuper_g_logo 12 "
  289. "google_logogoogle_gsuper_g_logo 123 "
  290. "google_logogoogle_gsuper_g_logo 1234 "
  291. "google_logogoogle_gsuper_g_logo 12345 "
  292. "google_logogoogle_gsuper_g_logo 123456 "
  293. "google_logogoogle_gsuper_g_logo 1234567 "
  294. "google_logogoogle_gsuper_g_logo 12345678 "
  295. "google_logogoogle_gsuper_g_logo 123456789 "
  296. "google_logogoogle_gsuper_g_logo 1234567890 "
  297. "google_logogoogle_gsuper_g_logo 123456789 "
  298. "google_logogoogle_gsuper_g_logo 12345678 "
  299. "google_logogoogle_gsuper_g_logo 1234567 "
  300. "google_logogoogle_gsuper_g_logo 123456 "
  301. "google_logogoogle_gsuper_g_logo 12345 "
  302. "google_logogoogle_gsuper_g_logo 1234 "
  303. "google_logogoogle_gsuper_g_logo 123 "
  304. "google_logogoogle_gsuper_g_logo 12 "
  305. "google_logogoogle_gsuper_g_logo 1 "
  306. "google_logogoogle_gsuper_g_logo "
  307. "google_logogoogle_gsuper_g_logo "
  308. "google_logogoogle_gsuper_g_logo "
  309. "google_logogoogle_gsuper_g_logo "
  310. "google_logogoogle_gsuper_g_logo "
  311. "google_logogoogle_gsuper_g_logo"};
  312. std::vector<const char*> text = {
  313. "My neighbor came over to say,\n"
  314. "Although not in a neighborly way,\n\n"
  315. "That he'd knock me around,\n\n\n"
  316. "If I didn't stop the sound,\n\n\n\n"
  317. "Of the classical music I play."};
  318. std::vector<const char*> long_word = {
  319. "A_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_"
  320. "very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_"
  321. "very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_"
  322. "very_very_very_very_very_very_very_long_text"};
  323. std::vector<const char*> very_long = {
  324. "A very very very very very very very very very very very very very very very very "
  325. "very very very very very very very very very very very very very very very very "
  326. "very very very very very very very very very very very very very very very very "
  327. "very very very very very very very long text"};
  328. std::vector<const char*> very_word = {
  329. "A very_very_very_very_very_very_very_very_very_very "
  330. "very_very_very_very_very_very_very_very_very_very very very very very very very "
  331. "very very very very very very very very very very very very very very very very "
  332. "very very very very very very very very very very very very very long text"};
  333. SkScalar width = this->width() / 5;
  334. SkScalar height = this->height();
  335. drawText(canvas, width, height, long_word, SK_ColorBLACK, SK_ColorWHITE, "Google Sans", 30);
  336. canvas->translate(width, 0);
  337. drawText(canvas, width, height, very_long, SK_ColorBLACK, SK_ColorWHITE, "Google Sans", 30);
  338. canvas->translate(width, 0);
  339. drawText(canvas, width, height, very_word, SK_ColorBLACK, SK_ColorWHITE, "Google Sans", 30);
  340. canvas->translate(width, 0);
  341. drawText(canvas, width, height / 2, text, SK_ColorBLACK, SK_ColorWHITE, "Roboto", 20, 100,
  342. u"\u2026");
  343. canvas->translate(0, height / 2);
  344. drawCode(canvas, width, height / 2);
  345. canvas->translate(width, -height / 2);
  346. drawText(canvas, width, height, cupertino, SK_ColorBLACK, SK_ColorWHITE, "Google Sans", 30);
  347. }
  348. private:
  349. typedef Sample INHERITED;
  350. };
  351. class ParagraphView3 : public ParagraphView_Base {
  352. protected:
  353. SkString name() override { return SkString("Paragraph3"); }
  354. void drawLine(SkCanvas* canvas, SkScalar w, SkScalar h, const std::string& text,
  355. TextAlign align, size_t lineLimit = std::numeric_limits<size_t>::max(),
  356. bool RTL = false, SkColor background = SK_ColorGRAY,
  357. const std::u16string& ellipsis = u"\u2026") {
  358. SkAutoCanvasRestore acr(canvas, true);
  359. canvas->clipRect(SkRect::MakeWH(w, h));
  360. canvas->drawColor(SK_ColorWHITE);
  361. SkScalar margin = 20;
  362. SkPaint paint;
  363. paint.setAntiAlias(true);
  364. paint.setColor(SK_ColorBLACK);
  365. SkPaint gray;
  366. gray.setColor(background);
  367. SkPaint yellow;
  368. yellow.setColor(SK_ColorYELLOW);
  369. TextStyle style;
  370. style.setBackgroundColor(gray);
  371. style.setForegroundColor(paint);
  372. style.setFontFamilies({SkString("sans-serif")});
  373. style.setFontSize(30);
  374. ParagraphStyle paraStyle;
  375. paraStyle.setTextStyle(style);
  376. paraStyle.setTextAlign(align);
  377. paraStyle.setMaxLines(lineLimit);
  378. paraStyle.setEllipsis(ellipsis);
  379. // paraStyle.setTextDirection(RTL ? SkTextDirection::rtl : SkTextDirection::ltr);
  380. auto fontCollection = sk_make_sp<FontCollection>();
  381. fontCollection->setDefaultFontManager(SkFontMgr::RefDefault());
  382. ParagraphBuilderImpl builder(paraStyle, fontCollection);
  383. if (RTL) {
  384. builder.addText(mirror(text));
  385. } else {
  386. builder.addText(normal(text));
  387. }
  388. canvas->drawRect(SkRect::MakeXYWH(margin, margin, w - margin * 2, h - margin * 2), yellow);
  389. auto paragraph = builder.Build();
  390. paragraph->layout(w - margin * 2);
  391. paragraph->paint(canvas, margin, margin);
  392. }
  393. std::u16string mirror(const std::string& text) {
  394. std::u16string result;
  395. result += u"\u202E";
  396. // for (auto i = text.size(); i > 0; --i) {
  397. // result += text[i - 1];
  398. //}
  399. for (auto i = text.size(); i > 0; --i) {
  400. auto ch = text[i - 1];
  401. if (ch == ',') {
  402. result += u"!";
  403. } else if (ch == '.') {
  404. result += u"!";
  405. } else {
  406. result += ch;
  407. }
  408. }
  409. result += u"\u202C";
  410. return result;
  411. }
  412. std::u16string normal(const std::string& text) {
  413. std::u16string result;
  414. result += u"\u202D";
  415. for (auto ch : text) {
  416. result += ch;
  417. }
  418. result += u"\u202C";
  419. return result;
  420. }
  421. void onDrawContent(SkCanvas* canvas) override {
  422. const std::string options = // { "open-source open-source open-source open-source" };
  423. {"Flutter is an open-source project to help developers "
  424. "build high-performance, high-fidelity, mobile apps for "
  425. "iOS and Android "
  426. "from a single codebase. This design lab is a playground "
  427. "and showcase of Flutter's many widgets, behaviors, "
  428. "animations, layouts, and more."};
  429. canvas->drawColor(SK_ColorDKGRAY);
  430. SkScalar width = this->width() / 4;
  431. SkScalar height = this->height() / 2;
  432. const std::string line =
  433. "World domination is such an ugly phrase - I prefer to call it world optimisation";
  434. drawLine(canvas, width, height, line, TextAlign::kLeft, 1, false, SK_ColorLTGRAY);
  435. canvas->translate(width, 0);
  436. drawLine(canvas, width, height, line, TextAlign::kRight, 2, false, SK_ColorLTGRAY);
  437. canvas->translate(width, 0);
  438. drawLine(canvas, width, height, line, TextAlign::kCenter, 3, false, SK_ColorLTGRAY);
  439. canvas->translate(width, 0);
  440. drawLine(canvas, width, height, line, TextAlign::kJustify, 4, false, SK_ColorLTGRAY);
  441. canvas->translate(-width * 3, height);
  442. drawLine(canvas, width, height, line, TextAlign::kLeft, 1, true, SK_ColorLTGRAY);
  443. canvas->translate(width, 0);
  444. drawLine(canvas, width, height, line, TextAlign::kRight, 2, true, SK_ColorLTGRAY);
  445. canvas->translate(width, 0);
  446. drawLine(canvas, width, height, line, TextAlign::kCenter, 3, true, SK_ColorLTGRAY);
  447. canvas->translate(width, 0);
  448. drawLine(canvas, width, height, line, TextAlign::kJustify, 4, true, SK_ColorLTGRAY);
  449. canvas->translate(width, 0);
  450. }
  451. private:
  452. typedef Sample INHERITED;
  453. };
  454. class ParagraphView4 : public ParagraphView_Base {
  455. protected:
  456. SkString name() override { return SkString("Paragraph4"); }
  457. void drawFlutter(SkCanvas* canvas, SkScalar w, SkScalar h,
  458. const char* ff = "Google Sans", SkScalar fs = 30,
  459. size_t lineLimit = std::numeric_limits<size_t>::max(),
  460. const std::u16string& ellipsis = u"\u2026") {
  461. SkAutoCanvasRestore acr(canvas, true);
  462. canvas->clipRect(SkRect::MakeWH(w, h));
  463. SkScalar margin = 20;
  464. SkPaint black;
  465. black.setAntiAlias(true);
  466. black.setColor(SK_ColorBLACK);
  467. SkPaint blue;
  468. blue.setAntiAlias(true);
  469. blue.setColor(SK_ColorBLUE);
  470. SkPaint red;
  471. red.setAntiAlias(true);
  472. red.setColor(SK_ColorRED);
  473. SkPaint green;
  474. green.setAntiAlias(true);
  475. green.setColor(SK_ColorGREEN);
  476. SkPaint gray;
  477. gray.setColor(SK_ColorLTGRAY);
  478. SkPaint yellow;
  479. yellow.setColor(SK_ColorYELLOW);
  480. SkPaint magenta;
  481. magenta.setAntiAlias(true);
  482. magenta.setColor(SK_ColorMAGENTA);
  483. TextStyle style;
  484. style.setFontFamilies({SkString(ff)});
  485. style.setFontSize(fs);
  486. TextStyle style0;
  487. style0.setForegroundColor(black);
  488. style0.setBackgroundColor(gray);
  489. style0.setFontFamilies({SkString(ff)});
  490. style0.setFontSize(fs);
  491. style0.setDecoration(TextDecoration::kUnderline);
  492. style0.setDecorationStyle(TextDecorationStyle::kDouble);
  493. style0.setDecorationColor(SK_ColorBLACK);
  494. TextStyle style1;
  495. style1.setForegroundColor(blue);
  496. style1.setBackgroundColor(yellow);
  497. style1.setFontFamilies({SkString(ff)});
  498. style1.setFontSize(fs);
  499. style1.setDecoration(TextDecoration::kOverline);
  500. style1.setDecorationStyle(TextDecorationStyle::kWavy);
  501. style1.setDecorationColor(SK_ColorBLACK);
  502. TextStyle style2;
  503. style2.setForegroundColor(red);
  504. style2.setFontFamilies({SkString(ff)});
  505. style2.setFontSize(fs);
  506. TextStyle style3;
  507. style3.setForegroundColor(green);
  508. style3.setFontFamilies({SkString(ff)});
  509. style3.setFontSize(fs);
  510. TextStyle style4;
  511. style4.setForegroundColor(magenta);
  512. style4.setFontFamilies({SkString(ff)});
  513. style4.setFontSize(fs);
  514. ParagraphStyle paraStyle;
  515. paraStyle.setTextStyle(style);
  516. paraStyle.setMaxLines(lineLimit);
  517. paraStyle.setEllipsis(ellipsis);
  518. const char* logo1 = "google_";
  519. const char* logo2 = "logo";
  520. const char* logo3 = "go";
  521. const char* logo4 = "ogle_logo";
  522. const char* logo5 = "google_lo";
  523. const char* logo6 = "go";
  524. {
  525. ParagraphBuilderImpl builder(paraStyle, fFC);
  526. builder.pushStyle(style0);
  527. builder.addText(logo1);
  528. builder.pop();
  529. builder.pushStyle(style1);
  530. builder.addText(logo2);
  531. builder.pop();
  532. builder.addText(" ");
  533. builder.pushStyle(style0);
  534. builder.addText(logo3);
  535. builder.pop();
  536. builder.pushStyle(style1);
  537. builder.addText(logo4);
  538. builder.pop();
  539. builder.addText(" ");
  540. builder.pushStyle(style0);
  541. builder.addText(logo5);
  542. builder.pop();
  543. builder.pushStyle(style1);
  544. builder.addText(logo6);
  545. builder.pop();
  546. auto paragraph = builder.Build();
  547. paragraph->layout(w - margin * 2);
  548. paragraph->paint(canvas, margin, margin);
  549. canvas->translate(0, h + margin);
  550. }
  551. }
  552. void onDrawContent(SkCanvas* canvas) override {
  553. canvas->drawColor(SK_ColorWHITE);
  554. SkScalar width = this->width();
  555. SkScalar height = this->height();
  556. drawFlutter(canvas, width, height / 2);
  557. }
  558. private:
  559. typedef Sample INHERITED;
  560. };
  561. class ParagraphView5 : public ParagraphView_Base {
  562. protected:
  563. SkString name() override { return SkString("Paragraph5"); }
  564. void bidi(SkCanvas* canvas, SkScalar w, SkScalar h, const std::u16string& text,
  565. const std::u16string& expected, size_t lineLimit = std::numeric_limits<size_t>::max(),
  566. const char* ff = "Roboto", SkScalar fs = 30,
  567. const std::u16string& ellipsis = u"\u2026") {
  568. SkAutoCanvasRestore acr(canvas, true);
  569. canvas->clipRect(SkRect::MakeWH(w, h));
  570. SkScalar margin = 20;
  571. SkPaint black;
  572. black.setColor(SK_ColorBLACK);
  573. SkPaint gray;
  574. gray.setColor(SK_ColorLTGRAY);
  575. TextStyle style;
  576. style.setForegroundColor(black);
  577. style.setFontFamilies({SkString(ff)});
  578. style.setFontSize(fs);
  579. TextStyle style0;
  580. style0.setForegroundColor(black);
  581. style0.setFontFamilies({SkString(ff)});
  582. style0.setFontSize(fs);
  583. style0.setFontStyle(SkFontStyle(SkFontStyle::kNormal_Weight, SkFontStyle::kNormal_Width,
  584. SkFontStyle::kItalic_Slant));
  585. TextStyle style1;
  586. style1.setForegroundColor(gray);
  587. style1.setFontFamilies({SkString(ff)});
  588. style1.setFontSize(fs);
  589. style1.setFontStyle(SkFontStyle(SkFontStyle::kBold_Weight, SkFontStyle::kNormal_Width,
  590. SkFontStyle::kUpright_Slant));
  591. ParagraphStyle paraStyle;
  592. paraStyle.setTextStyle(style);
  593. paraStyle.setMaxLines(lineLimit);
  594. paraStyle.setEllipsis(ellipsis);
  595. ParagraphBuilderImpl builder(paraStyle, fFC);
  596. if (text.empty()) {
  597. const std::u16string text0 = u"\u202Dabc";
  598. const std::u16string text1 = u"\u202EFED";
  599. const std::u16string text2 = u"\u202Dghi";
  600. const std::u16string text3 = u"\u202ELKJ";
  601. const std::u16string text4 = u"\u202Dmno";
  602. builder.pushStyle(style0);
  603. builder.addText(text0);
  604. builder.pop();
  605. builder.pushStyle(style1);
  606. builder.addText(text1);
  607. builder.pop();
  608. builder.pushStyle(style0);
  609. builder.addText(text2);
  610. builder.pop();
  611. builder.pushStyle(style1);
  612. builder.addText(text3);
  613. builder.pop();
  614. builder.pushStyle(style0);
  615. builder.addText(text4);
  616. builder.pop();
  617. } else {
  618. // icu::UnicodeString unicode((UChar*) text.data(), SkToS32(text.size()));
  619. // std::string str;
  620. // unicode.toUTF8String(str);
  621. // SkDebugf("Text: %s\n", str.c_str());
  622. builder.addText(text + expected);
  623. }
  624. auto paragraph = builder.Build();
  625. paragraph->layout(w - margin * 2);
  626. paragraph->paint(canvas, margin, margin);
  627. }
  628. void onDrawContent(SkCanvas* canvas) override {
  629. canvas->drawColor(SK_ColorWHITE);
  630. SkScalar width = this->width();
  631. SkScalar height = this->height() / 8;
  632. const std::u16string text1 =
  633. u"A \u202ENAC\u202Cner, exceedingly \u202ENAC\u202Cny,\n"
  634. "One morning remarked to his granny:\n"
  635. "A \u202ENAC\u202Cner \u202ENAC\u202C \u202ENAC\u202C,\n"
  636. "Anything that he \u202ENAC\u202C,\n"
  637. "But a \u202ENAC\u202Cner \u202ENAC\u202C't \u202ENAC\u202C a \u202ENAC\u202C, "
  638. "\u202ENAC\u202C he?";
  639. bidi(canvas, width, height * 3, text1, u"", 5);
  640. canvas->translate(0, height * 3);
  641. bidi(canvas, width, height, u"\u2067DETALOSI\u2069", u"");
  642. canvas->translate(0, height);
  643. bidi(canvas, width, height, u"\u202BDEDDEBME\u202C", u"");
  644. canvas->translate(0, height);
  645. bidi(canvas, width, height, u"\u202EEDIRREVO\u202C", u"");
  646. canvas->translate(0, height);
  647. bidi(canvas, width, height, u"\u200FTICILPMI\u200E", u"");
  648. canvas->translate(0, height);
  649. bidi(canvas, width, height, u"123 456 7890 \u202EZYXWV UTS RQP ONM LKJ IHG FED CBA\u202C.",
  650. u"", 2);
  651. canvas->translate(0, height);
  652. // bidi(canvas, width, height, u"", u"");
  653. // canvas->translate(0, height);
  654. }
  655. private:
  656. typedef Sample INHERITED;
  657. };
  658. class ParagraphView6 : public ParagraphView_Base {
  659. protected:
  660. SkString name() override { return SkString("Paragraph6"); }
  661. void hangingS(SkCanvas* canvas, SkScalar w, SkScalar h, SkScalar fs = 60.0) {
  662. auto ff = "HangingS";
  663. canvas->drawColor(SK_ColorLTGRAY);
  664. SkPaint black;
  665. black.setAntiAlias(true);
  666. black.setColor(SK_ColorBLACK);
  667. SkPaint blue;
  668. blue.setAntiAlias(true);
  669. blue.setColor(SK_ColorBLUE);
  670. SkPaint red;
  671. red.setAntiAlias(true);
  672. red.setColor(SK_ColorRED);
  673. SkPaint green;
  674. green.setAntiAlias(true);
  675. green.setColor(SK_ColorGREEN);
  676. SkPaint gray;
  677. gray.setColor(SK_ColorCYAN);
  678. SkPaint yellow;
  679. yellow.setColor(SK_ColorYELLOW);
  680. SkPaint magenta;
  681. magenta.setAntiAlias(true);
  682. magenta.setColor(SK_ColorMAGENTA);
  683. SkFontStyle fontStyle(SkFontStyle::kBold_Weight, SkFontStyle::kNormal_Width,
  684. SkFontStyle::kItalic_Slant);
  685. TextStyle style;
  686. style.setFontFamilies({SkString(ff)});
  687. style.setFontSize(fs);
  688. style.setFontStyle(fontStyle);
  689. TextStyle style0;
  690. style0.setForegroundColor(black);
  691. style0.setBackgroundColor(gray);
  692. style0.setFontFamilies({SkString(ff)});
  693. style0.setFontSize(fs);
  694. style0.setFontStyle(fontStyle);
  695. TextStyle style1;
  696. style1.setForegroundColor(blue);
  697. style1.setBackgroundColor(yellow);
  698. style1.setFontFamilies({SkString(ff)});
  699. style1.setFontSize(fs);
  700. style1.setFontStyle(fontStyle);
  701. TextStyle style2;
  702. style2.setForegroundColor(red);
  703. style2.setFontFamilies({SkString(ff)});
  704. style2.setFontSize(fs);
  705. style2.setFontStyle(fontStyle);
  706. TextStyle style3;
  707. style3.setForegroundColor(green);
  708. style3.setFontFamilies({SkString(ff)});
  709. style3.setFontSize(fs);
  710. style3.setFontStyle(fontStyle);
  711. TextStyle style4;
  712. style4.setForegroundColor(magenta);
  713. style4.setFontFamilies({SkString(ff)});
  714. style4.setFontSize(fs);
  715. style4.setFontStyle(fontStyle);
  716. ParagraphStyle paraStyle;
  717. paraStyle.setTextStyle(style);
  718. const char* logo1 = "S";
  719. const char* logo2 = "kia";
  720. const char* logo3 = "Sk";
  721. const char* logo4 = "ia";
  722. const char* logo5 = "Ski";
  723. const char* logo6 = "a";
  724. {
  725. ParagraphBuilderImpl builder(paraStyle, fFC);
  726. builder.pushStyle(style0);
  727. builder.addText(logo1);
  728. builder.pop();
  729. builder.pushStyle(style1);
  730. builder.addText(logo2);
  731. builder.pop();
  732. builder.addText(" ");
  733. builder.pushStyle(style0);
  734. builder.addText(logo3);
  735. builder.pop();
  736. builder.pushStyle(style1);
  737. builder.addText(logo4);
  738. builder.pop();
  739. builder.addText(" ");
  740. builder.pushStyle(style0);
  741. builder.addText(logo5);
  742. builder.pop();
  743. builder.pushStyle(style1);
  744. builder.addText(logo6);
  745. builder.pop();
  746. auto paragraph = builder.Build();
  747. paragraph->layout(w);
  748. paragraph->paint(canvas, 40, 40);
  749. canvas->translate(0, h);
  750. }
  751. const char* logo11 = "S";
  752. const char* logo12 = "S";
  753. const char* logo13 = "S";
  754. const char* logo14 = "S";
  755. const char* logo15 = "S";
  756. const char* logo16 = "S";
  757. {
  758. ParagraphBuilderImpl builder(paraStyle, fFC);
  759. builder.pushStyle(style0);
  760. builder.addText(logo11);
  761. builder.pop();
  762. builder.pushStyle(style1);
  763. builder.addText(logo12);
  764. builder.pop();
  765. builder.addText(" ");
  766. builder.pushStyle(style0);
  767. builder.addText(logo13);
  768. builder.pop();
  769. builder.pushStyle(style1);
  770. builder.addText(logo14);
  771. builder.pop();
  772. builder.addText(" ");
  773. builder.pushStyle(style0);
  774. builder.addText(logo15);
  775. builder.pop();
  776. builder.pushStyle(style1);
  777. builder.addText(logo16);
  778. builder.pop();
  779. auto paragraph = builder.Build();
  780. paragraph->layout(w);
  781. paragraph->paint(canvas, 40, h);
  782. canvas->translate(0, h);
  783. }
  784. }
  785. void onDrawContent(SkCanvas* canvas) override {
  786. canvas->drawColor(SK_ColorWHITE);
  787. SkScalar width = this->width();
  788. SkScalar height = this->height() / 4;
  789. hangingS(canvas, width, height);
  790. }
  791. private:
  792. typedef Sample INHERITED;
  793. };
  794. class ParagraphView7 : public ParagraphView_Base {
  795. protected:
  796. SkString name() override { return SkString("Paragraph7"); }
  797. void drawText(SkCanvas* canvas, SkColor background, SkScalar letterSpace, SkScalar w,
  798. SkScalar h) {
  799. SkAutoCanvasRestore acr(canvas, true);
  800. canvas->clipRect(SkRect::MakeWH(w, h));
  801. canvas->drawColor(background);
  802. const char* line =
  803. "World domination is such an ugly phrase - I prefer to call it world optimisation";
  804. ParagraphStyle paragraphStyle;
  805. paragraphStyle.setTextAlign(TextAlign::kLeft);
  806. paragraphStyle.setMaxLines(10);
  807. paragraphStyle.turnHintingOff();
  808. TextStyle textStyle;
  809. textStyle.setFontFamilies({SkString("Roboto")});
  810. textStyle.setFontSize(30);
  811. textStyle.setLetterSpacing(letterSpace);
  812. textStyle.setColor(SK_ColorBLACK);
  813. textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
  814. SkFontStyle::kUpright_Slant));
  815. ParagraphBuilderImpl builder(paragraphStyle, fFC);
  816. builder.pushStyle(textStyle);
  817. builder.addText(line);
  818. builder.pop();
  819. auto paragraph = builder.Build();
  820. paragraph->layout(w - 20);
  821. paragraph->paint(canvas, 10, 10);
  822. }
  823. void onDrawContent(SkCanvas* canvas) override {
  824. canvas->drawColor(SK_ColorWHITE);
  825. auto h = this->height() / 4;
  826. auto w = this->width() / 2;
  827. drawText(canvas, SK_ColorGRAY, 1, w, h);
  828. canvas->translate(0, h);
  829. drawText(canvas, SK_ColorLTGRAY, 2, w, h);
  830. canvas->translate(0, h);
  831. drawText(canvas, SK_ColorCYAN, 3, w, h);
  832. canvas->translate(0, h);
  833. drawText(canvas, SK_ColorGRAY, 4, w, h);
  834. canvas->translate(w, -3 * h);
  835. drawText(canvas, SK_ColorYELLOW, 5, w, h);
  836. canvas->translate(0, h);
  837. drawText(canvas, SK_ColorGREEN, 10, w, h);
  838. canvas->translate(0, h);
  839. drawText(canvas, SK_ColorRED, 15, w, h);
  840. canvas->translate(0, h);
  841. drawText(canvas, SK_ColorBLUE, 20, w, h);
  842. canvas->translate(0, h);
  843. }
  844. private:
  845. typedef Sample INHERITED;
  846. };
  847. class ParagraphView8 : public ParagraphView_Base {
  848. protected:
  849. SkString name() override { return SkString("Paragraph7"); }
  850. void drawText(SkCanvas* canvas, SkColor background, SkScalar wordSpace, SkScalar w,
  851. SkScalar h) {
  852. SkAutoCanvasRestore acr(canvas, true);
  853. canvas->clipRect(SkRect::MakeWH(w, h));
  854. canvas->drawColor(background);
  855. const char* line =
  856. "World domination is such an ugly phrase - I prefer to call it world optimisation";
  857. ParagraphStyle paragraphStyle;
  858. paragraphStyle.setTextAlign(TextAlign::kLeft);
  859. paragraphStyle.setMaxLines(10);
  860. paragraphStyle.turnHintingOff();
  861. TextStyle textStyle;
  862. textStyle.setFontFamilies({SkString("Roboto")});
  863. textStyle.setFontSize(30);
  864. textStyle.setWordSpacing(wordSpace);
  865. textStyle.setColor(SK_ColorBLACK);
  866. textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
  867. SkFontStyle::kUpright_Slant));
  868. ParagraphBuilderImpl builder(paragraphStyle, fFC);
  869. builder.pushStyle(textStyle);
  870. builder.addText(line);
  871. builder.pop();
  872. auto paragraph = builder.Build();
  873. paragraph->layout(w - 20);
  874. paragraph->paint(canvas, 10, 10);
  875. }
  876. void onDrawContent(SkCanvas* canvas) override {
  877. canvas->drawColor(SK_ColorWHITE);
  878. auto h = this->height() / 4;
  879. auto w = this->width() / 2;
  880. drawText(canvas, SK_ColorGRAY, 1, w, h);
  881. canvas->translate(0, h);
  882. drawText(canvas, SK_ColorLTGRAY, 2, w, h);
  883. canvas->translate(0, h);
  884. drawText(canvas, SK_ColorCYAN, 3, w, h);
  885. canvas->translate(0, h);
  886. drawText(canvas, SK_ColorGRAY, 4, w, h);
  887. canvas->translate(w, -3 * h);
  888. drawText(canvas, SK_ColorYELLOW, 5, w, h);
  889. canvas->translate(0, h);
  890. drawText(canvas, SK_ColorGREEN, 10, w, h);
  891. canvas->translate(0, h);
  892. drawText(canvas, SK_ColorRED, 15, w, h);
  893. canvas->translate(0, h);
  894. drawText(canvas, SK_ColorBLUE, 20, w, h);
  895. canvas->translate(0, h);
  896. }
  897. private:
  898. typedef Sample INHERITED;
  899. };
  900. class ParagraphView9 : public ParagraphView_Base {
  901. protected:
  902. SkString name() override { return SkString("Paragraph9"); }
  903. bool onChar(SkUnichar uni) override {
  904. switch (uni) {
  905. case 'w':
  906. ++wordSpacing;
  907. return true;
  908. case 'q':
  909. if (wordSpacing > 0) --wordSpacing;
  910. return true;
  911. case 'l':
  912. ++letterSpacing;
  913. return true;
  914. case 'k':
  915. if (letterSpacing > 0) --letterSpacing;
  916. return true;
  917. default:
  918. break;
  919. }
  920. return false;
  921. }
  922. void drawText(SkCanvas* canvas, SkColor background, SkScalar w, SkScalar h) {
  923. SkAutoCanvasRestore acr(canvas, true);
  924. canvas->clipRect(SkRect::MakeWH(w, h));
  925. canvas->drawColor(background);
  926. const char* text =
  927. "( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)("
  928. " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)("
  929. " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)";
  930. ParagraphStyle paragraphStyle;
  931. paragraphStyle.setTextAlign(TextAlign::kLeft);
  932. paragraphStyle.setMaxLines(10);
  933. paragraphStyle.turnHintingOff();
  934. TextStyle textStyle;
  935. textStyle.setFontFamilies({SkString("Roboto")});
  936. textStyle.setFontSize(50);
  937. textStyle.setHeight(1.3f);
  938. textStyle.setColor(SK_ColorBLACK);
  939. textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
  940. SkFontStyle::kUpright_Slant));
  941. ParagraphBuilderImpl builder(paragraphStyle, fFC);
  942. builder.pushStyle(textStyle);
  943. builder.addText(text);
  944. builder.pop();
  945. auto paragraph = builder.Build();
  946. paragraph->layout(550);
  947. std::vector<size_t> sizes = {0, 1, 2, 8, 19, 21, 22, 30, 150};
  948. std::vector<size_t> colors = {SK_ColorBLUE, SK_ColorCYAN, SK_ColorLTGRAY, SK_ColorGREEN,
  949. SK_ColorRED, SK_ColorWHITE, SK_ColorYELLOW, SK_ColorMAGENTA};
  950. RectHeightStyle rect_height_style = RectHeightStyle::kTight;
  951. RectWidthStyle rect_width_style = RectWidthStyle::kTight;
  952. for (size_t i = 0; i < sizes.size() - 1; ++i) {
  953. size_t from = (i == 0 ? 0 : 1) + sizes[i];
  954. size_t to = sizes[i + 1];
  955. auto boxes = paragraph->getRectsForRange(from, to, rect_height_style, rect_width_style);
  956. if (boxes.empty()) {
  957. continue;
  958. }
  959. for (auto& box : boxes) {
  960. SkPaint paint;
  961. paint.setColor(colors[i % colors.size()]);
  962. paint.setShader(setgrad(box.rect, colors[i % colors.size()], SK_ColorWHITE));
  963. canvas->drawRect(box.rect, paint);
  964. }
  965. }
  966. paragraph->paint(canvas, 0, 0);
  967. }
  968. void onDrawContent(SkCanvas* canvas) override {
  969. canvas->drawColor(SK_ColorWHITE);
  970. auto h = this->height();
  971. auto w = this->width();
  972. drawText(canvas, SK_ColorGRAY, w, h);
  973. }
  974. private:
  975. typedef Sample INHERITED;
  976. SkScalar letterSpacing;
  977. SkScalar wordSpacing;
  978. };
  979. class ParagraphView10 : public ParagraphView_Base {
  980. protected:
  981. SkString name() override { return SkString("Paragraph10"); }
  982. void onDrawContent(SkCanvas* canvas) override {
  983. canvas->drawColor(SK_ColorWHITE);
  984. const char* text = "English English 字典 字典 😀😃😄 😀😃😄";
  985. ParagraphStyle paragraph_style;
  986. paragraph_style.turnHintingOff();
  987. ParagraphBuilderImpl builder(paragraph_style, fFC);
  988. TextStyle text_style;
  989. text_style.setFontFamilies({SkString("Roboto"),
  990. SkString("Noto Color Emoji"),
  991. SkString("Source Han Serif CN")});
  992. text_style.setColor(SK_ColorRED);
  993. text_style.setFontSize(60);
  994. text_style.setLetterSpacing(0);
  995. text_style.setWordSpacing(0);
  996. text_style.setColor(SK_ColorBLACK);
  997. text_style.setHeight(1);
  998. builder.pushStyle(text_style);
  999. builder.addText(text);
  1000. builder.pop();
  1001. auto paragraph = builder.Build();
  1002. paragraph->layout(width());
  1003. paragraph->paint(canvas, 0, 0);
  1004. SkDEBUGCODE(auto impl = reinterpret_cast<ParagraphImpl*>(paragraph.get()));
  1005. SkASSERT(impl->runs().size() == 3);
  1006. SkASSERT(impl->runs()[0].textRange().end == impl->runs()[1].textRange().start);
  1007. SkASSERT(impl->runs()[1].textRange().end == impl->runs()[2].textRange().start);
  1008. }
  1009. private:
  1010. typedef Sample INHERITED;
  1011. };
  1012. class ParagraphView11 : public ParagraphView_Base {
  1013. protected:
  1014. SkString name() override { return SkString("Paragraph11"); }
  1015. void onDrawContent(SkCanvas* canvas) override {
  1016. canvas->drawColor(SK_ColorWHITE);
  1017. const char* text = "The same text many times";
  1018. for (size_t i = 0; i < 10; i++) {
  1019. ParagraphStyle paragraph_style;
  1020. ParagraphBuilderImpl builder(paragraph_style, fFC);
  1021. TextStyle text_style;
  1022. text_style.setFontFamilies({SkString("Roboto")});
  1023. text_style.setColor(SK_ColorBLACK);
  1024. text_style.setFontSize(10 + 2 * (i % 10));
  1025. builder.pushStyle(text_style);
  1026. builder.addText(text);
  1027. builder.pop();
  1028. auto paragraph = builder.Build();
  1029. paragraph->layout(500);
  1030. paragraph->paint(canvas, 0, 40 * (i % 10));
  1031. }
  1032. }
  1033. private:
  1034. typedef Sample INHERITED;
  1035. };
  1036. // Measure different stages of layout/paint
  1037. class ParagraphView12 : public ParagraphView_Base {
  1038. protected:
  1039. SkString name() override { return SkString("Paragraph12"); }
  1040. void onDrawContent(SkCanvas* canvas) override {
  1041. ParagraphStyle paragraph_style;
  1042. paragraph_style.setMaxLines(14);
  1043. paragraph_style.setTextAlign(TextAlign::kLeft);
  1044. paragraph_style.turnHintingOff();
  1045. ParagraphBuilderImpl builder(paragraph_style, fFC);
  1046. TextStyle text_style;
  1047. text_style.setFontFamilies({SkString("Roboto")});
  1048. text_style.setFontSize(26);
  1049. text_style.setColor(SK_ColorBLACK);
  1050. text_style.setHeight(1);
  1051. text_style.setDecoration(TextDecoration::kUnderline);
  1052. text_style.setDecorationColor(SK_ColorBLACK);
  1053. builder.pushStyle(text_style);
  1054. builder.addText(gText);
  1055. builder.pop();
  1056. auto paragraph = builder.Build();
  1057. auto impl = reinterpret_cast<ParagraphImpl*>(paragraph.get());
  1058. impl->turnOnCache(false);
  1059. for (auto i = 0; i < 1000; ++i) {
  1060. impl->setState(kUnknown);
  1061. impl->shapeTextIntoEndlessLine();
  1062. impl->setState(kShaped);
  1063. }
  1064. for (auto i = 0; i < 1000; ++i) {
  1065. impl->setState(kShaped);
  1066. impl->buildClusterTable();
  1067. impl->markLineBreaks();
  1068. impl->setState(kMarked);
  1069. }
  1070. for (auto i = 0; i < 1000; ++i) {
  1071. impl->setState(kMarked);
  1072. impl->breakShapedTextIntoLines(1000);
  1073. impl->setState(kLineBroken);
  1074. }
  1075. for (auto i = 0; i < 1000; ++i) {
  1076. impl->setState(kLineBroken);
  1077. impl->formatLines(1000);
  1078. impl->setState(kFormatted);
  1079. }
  1080. for (auto i = 0; i < 1000; ++i) {
  1081. impl->setState(kFormatted);
  1082. impl->paintLinesIntoPicture();
  1083. impl->setState(kDrawn);
  1084. }
  1085. auto picture = impl->getPicture();
  1086. SkMatrix matrix = SkMatrix::MakeTrans(0, 0);
  1087. for (auto i = 0; i < 1000; ++i) {
  1088. canvas->drawPicture(picture, &matrix, nullptr);
  1089. }
  1090. }
  1091. private:
  1092. typedef Sample INHERITED;
  1093. };
  1094. //////////////////////////////////////////////////////////////////////////////
  1095. DEF_SAMPLE(return new ParagraphView1();)
  1096. DEF_SAMPLE(return new ParagraphView2();)
  1097. DEF_SAMPLE(return new ParagraphView3();)
  1098. DEF_SAMPLE(return new ParagraphView4();)
  1099. DEF_SAMPLE(return new ParagraphView5();)
  1100. DEF_SAMPLE(return new ParagraphView6();)
  1101. DEF_SAMPLE(return new ParagraphView7();)
  1102. DEF_SAMPLE(return new ParagraphView8();)
  1103. DEF_SAMPLE(return new ParagraphView9();)
  1104. DEF_SAMPLE(return new ParagraphView10();)
  1105. DEF_SAMPLE(return new ParagraphView11();)
  1106. DEF_SAMPLE(return new ParagraphView12();)