SkParagraphTest.cpp 134 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946
  1. // Copyright 2019 Google LLC.
  2. #include <tools/ToolUtils.h>
  3. #include <sstream>
  4. #include "modules/skparagraph/include/TypefaceFontProvider.h"
  5. #include "modules/skparagraph/src/ParagraphBuilderImpl.h"
  6. #include "modules/skparagraph/src/ParagraphImpl.h"
  7. #include "src/core/SkOSFile.h"
  8. #include "src/utils/SkShaperJSONWriter.h"
  9. #include "tests/Test.h"
  10. #include "tools/Resources.h"
  11. #define VeryLongCanvasWidth 1000000
  12. #define TestCanvasWidth 1000
  13. #define TestCanvasHeight 600
  14. using namespace skia::textlayout;
  15. namespace {
  16. bool equal(const char* base, TextRange a, const char* b) {
  17. return std::strncmp(b, base + a.start, a.width()) == 0;
  18. }
  19. class TestFontCollection : public FontCollection {
  20. public:
  21. TestFontCollection()
  22. : fFontsFound(false)
  23. , fResolvedFonts(0)
  24. , fResourceDir(GetResourcePath("fonts").c_str())
  25. , fFontProvider(sk_make_sp<TypefaceFontProvider>()) {
  26. std::vector<SkString> fonts;
  27. SkOSFile::Iter iter(fResourceDir.c_str());
  28. SkString path;
  29. while (iter.next(&path)) {
  30. if (path.endsWith("Roboto-Italic.ttf")) {
  31. fFontsFound = true;
  32. }
  33. fonts.emplace_back(path);
  34. }
  35. if (!fFontsFound) {
  36. return;
  37. }
  38. // Only register fonts if we have to
  39. for (auto& font : fonts) {
  40. SkString file_path;
  41. file_path.printf("%s/%s", fResourceDir.c_str(), font.c_str());
  42. fFontProvider->registerTypeface(SkTypeface::MakeFromFile(file_path.c_str()));
  43. }
  44. this->setTestFontManager(std::move(fFontProvider));
  45. this->disableFontFallback();
  46. if (!fFontsFound) SkDebugf("Fonts not found, skipping all the tests\n");
  47. }
  48. ~TestFontCollection() = default;
  49. size_t resolvedFonts() const { return fResolvedFonts; }
  50. // TODO: temp solution until we check in fonts
  51. bool fontsFound() const { return fFontsFound; }
  52. private:
  53. bool fFontsFound;
  54. size_t fResolvedFonts;
  55. std::string fResourceDir;
  56. sk_sp<TypefaceFontProvider> fFontProvider;
  57. };
  58. } // namespace
  59. DEF_TEST(SkParagraph_SimpleParagraph, reporter) {
  60. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  61. if (!fontCollection->fontsFound()) return;
  62. const char* text = "Hello World Text Dialog";
  63. ParagraphStyle paragraph_style;
  64. paragraph_style.turnHintingOff();
  65. ParagraphBuilderImpl builder(paragraph_style, fontCollection);
  66. TextStyle text_style;
  67. text_style.setFontFamilies({SkString("Roboto")});
  68. text_style.setColor(SK_ColorBLACK);
  69. builder.pushStyle(text_style);
  70. builder.addText(text);
  71. builder.pop();
  72. auto paragraph = builder.Build();
  73. paragraph->layout(TestCanvasWidth);
  74. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  75. REPORTER_ASSERT(reporter, impl->runs().size() == 1);
  76. REPORTER_ASSERT(reporter, impl->styles().size() == 1); // paragraph style does not count
  77. REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style));
  78. size_t index = 0;
  79. for (auto& line : impl->lines()) {
  80. line.scanStyles(StyleType::kDecorations,
  81. [&index, reporter](TextRange text, TextStyle style, SkScalar) {
  82. REPORTER_ASSERT(reporter, index == 0);
  83. REPORTER_ASSERT(reporter, style.getColor() == SK_ColorBLACK);
  84. ++index;
  85. return true;
  86. });
  87. }
  88. }
  89. DEF_TEST(SkParagraph_SimpleRedParagraph, reporter) {
  90. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  91. if (!fontCollection->fontsFound()) return;
  92. const char* text = "I am RED";
  93. ParagraphStyle paragraph_style;
  94. paragraph_style.turnHintingOff();
  95. ParagraphBuilderImpl builder(paragraph_style, fontCollection);
  96. TextStyle text_style;
  97. text_style.setFontFamilies({SkString("Roboto")});
  98. text_style.setColor(SK_ColorRED);
  99. builder.pushStyle(text_style);
  100. builder.addText(text);
  101. builder.pop();
  102. auto paragraph = builder.Build();
  103. paragraph->layout(TestCanvasWidth);
  104. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  105. REPORTER_ASSERT(reporter, impl->runs().size() == 1);
  106. REPORTER_ASSERT(reporter, impl->styles().size() == 1); // paragraph style does not count
  107. REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style));
  108. size_t index = 0;
  109. for (auto& line : impl->lines()) {
  110. line.scanStyles(StyleType::kDecorations,
  111. [&index, reporter](TextRange text, TextStyle style, SkScalar) {
  112. REPORTER_ASSERT(reporter, index == 0);
  113. REPORTER_ASSERT(reporter, style.getColor() == SK_ColorRED);
  114. ++index;
  115. return true;
  116. });
  117. }
  118. }
  119. DEF_TEST(SkParagraph_RainbowParagraph, reporter) {
  120. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  121. if (!fontCollection->fontsFound()) return;
  122. const char* text1 = "Red Roboto";
  123. const char* text2 = "big Greeen Default";
  124. const char* text3 = "Defcolor Homemade Apple";
  125. const char* text4 = "Small Blue Roboto";
  126. const char* text5 =
  127. "Continue Last Style With lots of words to check if it overlaps "
  128. "properly or not";
  129. const char* text45 =
  130. "Small Blue Roboto"
  131. "Continue Last Style With lots of words to check if it overlaps "
  132. "properly or not";
  133. ParagraphStyle paragraph_style;
  134. paragraph_style.turnHintingOff();
  135. paragraph_style.setTextAlign(TextAlign::kLeft);
  136. paragraph_style.setMaxLines(1);
  137. ParagraphBuilderImpl builder(paragraph_style, fontCollection);
  138. TextStyle text_style1;
  139. text_style1.setFontFamilies({SkString("Roboto")});
  140. text_style1.setColor(SK_ColorRED);
  141. builder.pushStyle(text_style1);
  142. builder.addText(text1);
  143. TextStyle text_style2;
  144. text_style2.setFontFamilies({SkString("Roboto")});
  145. text_style2.setFontSize(50);
  146. text_style2.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
  147. SkFontStyle::kUpright_Slant));
  148. text_style2.setLetterSpacing(10);
  149. text_style2.setDecorationColor(SK_ColorBLACK);
  150. text_style2.setDecoration((TextDecoration)(
  151. TextDecoration::kUnderline | TextDecoration::kOverline | TextDecoration::kLineThrough));
  152. text_style2.setWordSpacing(30);
  153. text_style2.setColor(SK_ColorGREEN);
  154. builder.pushStyle(text_style2);
  155. builder.addText(text2);
  156. TextStyle text_style3;
  157. text_style3.setFontFamilies({SkString("Homemade Apple")});
  158. builder.pushStyle(text_style3);
  159. builder.addText(text3);
  160. TextStyle text_style4;
  161. text_style4.setFontFamilies({SkString("Roboto")});
  162. text_style4.setFontSize(14);
  163. text_style4.setDecorationColor(SK_ColorBLACK);
  164. text_style4.setDecoration((TextDecoration)(
  165. TextDecoration::kUnderline | TextDecoration::kOverline | TextDecoration::kLineThrough));
  166. text_style4.setColor(SK_ColorBLUE);
  167. builder.pushStyle(text_style4);
  168. builder.addText(text4);
  169. builder.addText(text5);
  170. builder.pop();
  171. auto paragraph = builder.Build();
  172. paragraph->layout(VeryLongCanvasWidth);
  173. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  174. REPORTER_ASSERT(reporter, impl->runs().size() == 4);
  175. REPORTER_ASSERT(reporter, impl->styles().size() == 4);
  176. REPORTER_ASSERT(reporter, impl->lines().size() == 1);
  177. size_t index = 0;
  178. impl->lines()[0].scanStyles(StyleType::kAllAttributes,
  179. [&](TextRange text, TextStyle style, SkScalar) {
  180. switch (index) {
  181. case 0:
  182. REPORTER_ASSERT(reporter, style.equals(text_style1));
  183. REPORTER_ASSERT(reporter, equal(impl->text().begin(), text, text1));
  184. break;
  185. case 1:
  186. REPORTER_ASSERT(reporter, style.equals(text_style2));
  187. REPORTER_ASSERT(reporter, equal(impl->text().begin(), text, text2));
  188. break;
  189. case 2:
  190. REPORTER_ASSERT(reporter, style.equals(text_style3));
  191. REPORTER_ASSERT(reporter, equal(impl->text().begin(), text, text3));
  192. break;
  193. case 3:
  194. REPORTER_ASSERT(reporter, style.equals(text_style4));
  195. REPORTER_ASSERT(reporter, equal(impl->text().begin(), text, text45));
  196. break;
  197. default:
  198. REPORTER_ASSERT(reporter, false);
  199. break;
  200. }
  201. ++index;
  202. return true;
  203. });
  204. REPORTER_ASSERT(reporter, index == 4);
  205. }
  206. DEF_TEST(SkParagraph_DefaultStyleParagraph, reporter) {
  207. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  208. if (!fontCollection->fontsFound()) return;
  209. const char* text = "No TextStyle! Uh Oh!";
  210. ParagraphStyle paragraph_style;
  211. TextStyle defaultStyle;
  212. defaultStyle.setFontFamilies({SkString("Roboto")});
  213. paragraph_style.setTextStyle(defaultStyle);
  214. paragraph_style.turnHintingOff();
  215. ParagraphBuilderImpl builder(paragraph_style, fontCollection);
  216. builder.addText(text);
  217. auto paragraph = builder.Build();
  218. paragraph->layout(VeryLongCanvasWidth);
  219. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  220. REPORTER_ASSERT(reporter, impl->runs().size() == 1);
  221. REPORTER_ASSERT(reporter, impl->styles().size() == 1);
  222. REPORTER_ASSERT(reporter, impl->lines().size() == 1);
  223. size_t index = 0;
  224. impl->lines()[0].scanStyles(
  225. StyleType::kAllAttributes, [&](TextRange text1, TextStyle style, SkScalar) {
  226. REPORTER_ASSERT(reporter, style.equals(paragraph_style.getTextStyle()));
  227. REPORTER_ASSERT(reporter, equal(impl->text().begin(), text1, text));
  228. ++index;
  229. return true;
  230. });
  231. REPORTER_ASSERT(reporter, index == 1);
  232. }
  233. DEF_TEST(SkParagraph_BoldParagraph, reporter) {
  234. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  235. if (!fontCollection->fontsFound()) return;
  236. const char* text = "This is Red max bold text!";
  237. ParagraphStyle paragraph_style;
  238. paragraph_style.turnHintingOff();
  239. ParagraphBuilderImpl builder(paragraph_style, fontCollection);
  240. TextStyle text_style;
  241. text_style.setFontFamilies({SkString("Roboto")});
  242. text_style.setColor(SK_ColorRED);
  243. text_style.setFontSize(60);
  244. text_style.setLetterSpacing(0);
  245. text_style.setFontStyle(SkFontStyle(SkFontStyle::kBlack_Weight, SkFontStyle::kNormal_Width,
  246. SkFontStyle::kUpright_Slant));
  247. builder.pushStyle(text_style);
  248. builder.addText(text);
  249. builder.pop();
  250. auto paragraph = builder.Build();
  251. paragraph->layout(VeryLongCanvasWidth);
  252. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  253. REPORTER_ASSERT(reporter, impl->runs().size() == 1);
  254. REPORTER_ASSERT(reporter, impl->styles().size() == 1);
  255. REPORTER_ASSERT(reporter, impl->lines().size() == 1);
  256. size_t index = 0;
  257. impl->lines()[0].scanStyles(StyleType::kAllAttributes,
  258. [&](TextRange text1, TextStyle style, SkScalar) {
  259. REPORTER_ASSERT(reporter, style.equals(text_style));
  260. REPORTER_ASSERT(reporter, equal(impl->text().begin(), text1, text));
  261. ++index;
  262. return true;
  263. });
  264. REPORTER_ASSERT(reporter, index == 1);
  265. }
  266. DEF_TEST(SkParagraph_LeftAlignParagraph, reporter) {
  267. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  268. if (!fontCollection->fontsFound()) return;
  269. const char* text =
  270. "This is a very long sentence to test if the text will properly wrap "
  271. "around and go to the next line. Sometimes, short sentence. Longer "
  272. "sentences are okay too because they are nessecary. Very short. "
  273. "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
  274. "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
  275. "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
  276. "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
  277. "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
  278. "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
  279. "mollit anim id est laborum. "
  280. "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
  281. "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
  282. "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
  283. "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
  284. "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
  285. "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
  286. "mollit anim id est laborum.";
  287. ParagraphStyle paragraph_style;
  288. paragraph_style.setMaxLines(14);
  289. paragraph_style.setTextAlign(TextAlign::kLeft);
  290. paragraph_style.turnHintingOff();
  291. ParagraphBuilderImpl builder(paragraph_style, fontCollection);
  292. TextStyle text_style;
  293. text_style.setFontFamilies({SkString("Roboto")});
  294. text_style.setFontSize(26);
  295. text_style.setLetterSpacing(1);
  296. text_style.setWordSpacing(5);
  297. text_style.setColor(SK_ColorBLACK);
  298. text_style.setHeight(1);
  299. text_style.setDecoration(TextDecoration::kUnderline);
  300. text_style.setDecorationColor(SK_ColorBLACK);
  301. builder.pushStyle(text_style);
  302. builder.addText(text);
  303. builder.pop();
  304. auto paragraph = builder.Build();
  305. paragraph->layout(TestCanvasWidth - 100);
  306. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  307. REPORTER_ASSERT(reporter, impl->text().size() == std::string{text}.length());
  308. REPORTER_ASSERT(reporter, impl->runs().size() == 1);
  309. REPORTER_ASSERT(reporter, impl->styles().size() == 1);
  310. REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style));
  311. REPORTER_ASSERT(reporter, impl->lines().size() == paragraph_style.getMaxLines());
  312. // Apparently, Minikin records start from the base line (24)
  313. double expected_y = 0;
  314. double epsilon = 0.01f;
  315. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].baseline(), 24.121f, epsilon));
  316. REPORTER_ASSERT(reporter,
  317. SkScalarNearlyEqual(impl->lines()[0].offset().fY, expected_y, epsilon));
  318. expected_y += 30;
  319. REPORTER_ASSERT(reporter,
  320. SkScalarNearlyEqual(impl->lines()[1].offset().fY, expected_y, epsilon));
  321. expected_y += 30;
  322. REPORTER_ASSERT(reporter,
  323. SkScalarNearlyEqual(impl->lines()[2].offset().fY, expected_y, epsilon));
  324. expected_y += 30;
  325. REPORTER_ASSERT(reporter,
  326. SkScalarNearlyEqual(impl->lines()[3].offset().fY, expected_y, epsilon));
  327. expected_y += 30 * 10;
  328. REPORTER_ASSERT(reporter,
  329. SkScalarNearlyEqual(impl->lines()[13].offset().fY, expected_y, epsilon));
  330. REPORTER_ASSERT(reporter,
  331. paragraph_style.getTextAlign() == impl->paragraphStyle().getTextAlign());
  332. // Tests for GetGlyphPositionAtCoordinate()
  333. REPORTER_ASSERT(reporter, impl->getGlyphPositionAtCoordinate(0, 0).position == 0);
  334. REPORTER_ASSERT(reporter, impl->getGlyphPositionAtCoordinate(1, 1).position == 0);
  335. REPORTER_ASSERT(reporter, impl->getGlyphPositionAtCoordinate(1, 35).position == 68);
  336. REPORTER_ASSERT(reporter, impl->getGlyphPositionAtCoordinate(1, 70).position == 134);
  337. REPORTER_ASSERT(reporter, impl->getGlyphPositionAtCoordinate(2000, 35).position == 134);
  338. }
  339. DEF_TEST(SkParagraph_RightAlignParagraph, reporter) {
  340. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  341. if (!fontCollection->fontsFound()) return;
  342. const char* text =
  343. "This is a very long sentence to test if the text will properly wrap "
  344. "around and go to the next line. Sometimes, short sentence. Longer "
  345. "sentences are okay too because they are nessecary. Very short. "
  346. "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
  347. "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
  348. "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
  349. "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
  350. "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
  351. "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
  352. "mollit anim id est laborum. "
  353. "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
  354. "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
  355. "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
  356. "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
  357. "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
  358. "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
  359. "mollit anim id est laborum.";
  360. ParagraphStyle paragraph_style;
  361. paragraph_style.setMaxLines(14);
  362. paragraph_style.setTextAlign(TextAlign::kRight);
  363. paragraph_style.turnHintingOff();
  364. ParagraphBuilderImpl builder(paragraph_style, fontCollection);
  365. TextStyle text_style;
  366. text_style.setFontFamilies({SkString("Roboto")});
  367. text_style.setFontSize(26);
  368. text_style.setLetterSpacing(1);
  369. text_style.setWordSpacing(5);
  370. text_style.setColor(SK_ColorBLACK);
  371. text_style.setHeight(1);
  372. text_style.setDecoration(TextDecoration::kUnderline);
  373. text_style.setDecorationColor(SK_ColorBLACK);
  374. builder.pushStyle(text_style);
  375. builder.addText(text);
  376. builder.pop();
  377. auto paragraph = builder.Build();
  378. paragraph->layout(TestCanvasWidth - 100);
  379. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  380. REPORTER_ASSERT(reporter, impl->runs().size() == 1);
  381. REPORTER_ASSERT(reporter, impl->styles().size() == 1);
  382. REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style));
  383. // Minikin has two records for each due to 'ghost' trailing whitespace run, SkParagraph - 1
  384. REPORTER_ASSERT(reporter, impl->lines().size() == paragraph_style.getMaxLines());
  385. // Apparently, Minikin records start from the base line (24)
  386. double expected_y = 0;
  387. double epsilon = 0.01f;
  388. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].baseline(), 24.121f, epsilon));
  389. REPORTER_ASSERT(reporter,
  390. SkScalarNearlyEqual(impl->lines()[0].offset().fY, expected_y, epsilon));
  391. expected_y += 30;
  392. REPORTER_ASSERT(reporter,
  393. SkScalarNearlyEqual(impl->lines()[1].offset().fY, expected_y, epsilon));
  394. expected_y += 30;
  395. REPORTER_ASSERT(reporter,
  396. SkScalarNearlyEqual(impl->lines()[2].offset().fY, expected_y, epsilon));
  397. expected_y += 30;
  398. REPORTER_ASSERT(reporter,
  399. SkScalarNearlyEqual(impl->lines()[3].offset().fY, expected_y, epsilon));
  400. expected_y += 30 * 10;
  401. REPORTER_ASSERT(reporter,
  402. SkScalarNearlyEqual(impl->lines()[13].offset().fY, expected_y, epsilon));
  403. auto calculate = [](const TextLine& line) -> SkScalar {
  404. return TestCanvasWidth - 100 - line.offset().fX - line.width();
  405. };
  406. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[0]), 0, epsilon));
  407. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[1]), 0, epsilon));
  408. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[2]), 0, epsilon));
  409. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[3]), 0, epsilon));
  410. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[13]), 0, epsilon));
  411. REPORTER_ASSERT(reporter,
  412. paragraph_style.getTextAlign() == impl->paragraphStyle().getTextAlign());
  413. }
  414. DEF_TEST(SkParagraph_CenterAlignParagraph, reporter) {
  415. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  416. if (!fontCollection->fontsFound()) return;
  417. const char* text =
  418. "This is a very long sentence to test if the text will properly wrap "
  419. "around and go to the next line. Sometimes, short sentence. Longer "
  420. "sentences are okay too because they are nessecary. Very short. "
  421. "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
  422. "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
  423. "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
  424. "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
  425. "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
  426. "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
  427. "mollit anim id est laborum. "
  428. "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
  429. "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
  430. "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
  431. "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
  432. "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
  433. "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
  434. "mollit anim id est laborum.";
  435. ParagraphStyle paragraph_style;
  436. paragraph_style.setMaxLines(14);
  437. paragraph_style.setTextAlign(TextAlign::kCenter);
  438. paragraph_style.turnHintingOff();
  439. ParagraphBuilderImpl builder(paragraph_style, fontCollection);
  440. TextStyle text_style;
  441. text_style.setFontFamilies({SkString("Roboto")});
  442. text_style.setFontSize(26);
  443. text_style.setLetterSpacing(1);
  444. text_style.setWordSpacing(5);
  445. text_style.setColor(SK_ColorBLACK);
  446. text_style.setHeight(1);
  447. text_style.setDecoration(TextDecoration::kUnderline);
  448. text_style.setDecorationColor(SK_ColorBLACK);
  449. builder.pushStyle(text_style);
  450. builder.addText(text);
  451. builder.pop();
  452. auto paragraph = builder.Build();
  453. paragraph->layout(TestCanvasWidth - 100);
  454. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  455. REPORTER_ASSERT(reporter, impl->text().size() == std::string{text}.length());
  456. REPORTER_ASSERT(reporter, impl->runs().size() == 1);
  457. REPORTER_ASSERT(reporter, impl->styles().size() == 1);
  458. REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style));
  459. // Minikin has two records for each due to 'ghost' trailing whitespace run, SkParagraph - 1
  460. REPORTER_ASSERT(reporter, impl->lines().size() == paragraph_style.getMaxLines());
  461. // Apparently, Minikin records start from the base line (24)
  462. double expected_y = 0;
  463. double epsilon = 0.01f;
  464. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].baseline(), 24.121f, epsilon));
  465. REPORTER_ASSERT(reporter,
  466. SkScalarNearlyEqual(impl->lines()[0].offset().fY, expected_y, epsilon));
  467. expected_y += 30;
  468. REPORTER_ASSERT(reporter,
  469. SkScalarNearlyEqual(impl->lines()[1].offset().fY, expected_y, epsilon));
  470. expected_y += 30;
  471. REPORTER_ASSERT(reporter,
  472. SkScalarNearlyEqual(impl->lines()[2].offset().fY, expected_y, epsilon));
  473. expected_y += 30;
  474. REPORTER_ASSERT(reporter,
  475. SkScalarNearlyEqual(impl->lines()[3].offset().fY, expected_y, epsilon));
  476. expected_y += 30 * 10;
  477. REPORTER_ASSERT(reporter,
  478. SkScalarNearlyEqual(impl->lines()[13].offset().fY, expected_y, epsilon));
  479. auto calculate = [](const TextLine& line) -> SkScalar {
  480. return TestCanvasWidth - 100 - (line.offset().fX * 2 + line.width());
  481. };
  482. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[0]), 0, epsilon));
  483. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[1]), 0, epsilon));
  484. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[2]), 0, epsilon));
  485. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[3]), 0, epsilon));
  486. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[13]), 0, epsilon));
  487. REPORTER_ASSERT(reporter,
  488. paragraph_style.getTextAlign() == impl->paragraphStyle().getTextAlign());
  489. }
  490. DEF_TEST(SkParagraph_JustifyAlignParagraph, reporter) {
  491. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  492. if (!fontCollection->fontsFound()) return;
  493. const char* text =
  494. "This is a very long sentence to test if the text will properly wrap "
  495. "around and go to the next line. Sometimes, short sentence. Longer "
  496. "sentences are okay too because they are nessecary. Very short. "
  497. "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
  498. "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
  499. "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
  500. "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
  501. "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
  502. "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
  503. "mollit anim id est laborum. "
  504. "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
  505. "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
  506. "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
  507. "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
  508. "velit esse cillum dolore eu fugiat.";
  509. ParagraphStyle paragraph_style;
  510. paragraph_style.setMaxLines(14);
  511. paragraph_style.setTextAlign(TextAlign::kJustify);
  512. paragraph_style.turnHintingOff();
  513. ParagraphBuilderImpl builder(paragraph_style, fontCollection);
  514. TextStyle text_style;
  515. text_style.setFontFamilies({SkString("Roboto")});
  516. text_style.setFontSize(26);
  517. text_style.setLetterSpacing(0);
  518. text_style.setWordSpacing(5);
  519. text_style.setColor(SK_ColorBLACK);
  520. text_style.setHeight(1);
  521. text_style.setDecoration(TextDecoration::kUnderline);
  522. text_style.setDecorationColor(SK_ColorBLACK);
  523. builder.pushStyle(text_style);
  524. builder.addText(text);
  525. builder.pop();
  526. auto paragraph = builder.Build();
  527. paragraph->layout(TestCanvasWidth - 100);
  528. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  529. REPORTER_ASSERT(reporter, impl->text().size() == std::string{text}.length());
  530. REPORTER_ASSERT(reporter, impl->runs().size() == 1);
  531. REPORTER_ASSERT(reporter, impl->styles().size() == 1);
  532. REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style));
  533. double expected_y = 0;
  534. double epsilon = 0.01f;
  535. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].baseline(), 24.121f, epsilon));
  536. REPORTER_ASSERT(reporter,
  537. SkScalarNearlyEqual(impl->lines()[0].offset().fY, expected_y, epsilon));
  538. expected_y += 30;
  539. REPORTER_ASSERT(reporter,
  540. SkScalarNearlyEqual(impl->lines()[1].offset().fY, expected_y, epsilon));
  541. expected_y += 30;
  542. REPORTER_ASSERT(reporter,
  543. SkScalarNearlyEqual(impl->lines()[2].offset().fY, expected_y, epsilon));
  544. expected_y += 30;
  545. REPORTER_ASSERT(reporter,
  546. SkScalarNearlyEqual(impl->lines()[3].offset().fY, expected_y, epsilon));
  547. expected_y += 30 * 9;
  548. REPORTER_ASSERT(reporter,
  549. SkScalarNearlyEqual(impl->lines()[12].offset().fY, expected_y, epsilon));
  550. auto calculate = [](const TextLine& line) -> SkScalar {
  551. return TestCanvasWidth - 100 - (line.offset().fX + line.width());
  552. };
  553. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[0]), 0, epsilon));
  554. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[1]), 0, epsilon));
  555. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[2]), 0, epsilon));
  556. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[3]), 0, epsilon));
  557. REPORTER_ASSERT(reporter, calculate(impl->lines()[12]) > 0);
  558. REPORTER_ASSERT(reporter,
  559. paragraph_style.getTextAlign() == impl->paragraphStyle().getTextAlign());
  560. }
  561. DEF_TEST(SkParagraph_JustifyRTL, reporter) {
  562. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  563. if (!fontCollection->fontsFound()) return;
  564. const char* text =
  565. "אאא בּבּבּבּ אאאא בּבּ אאא בּבּבּ אאאאא בּבּבּבּ אאאא בּבּבּבּבּ "
  566. "אאאאא בּבּבּבּבּ אאאבּבּבּבּבּבּאאאאא בּבּבּבּבּבּאאאאאבּבּבּבּבּבּ אאאאא בּבּבּבּבּ "
  567. "אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ";
  568. ParagraphStyle paragraph_style;
  569. paragraph_style.setMaxLines(14);
  570. paragraph_style.setTextAlign(TextAlign::kJustify);
  571. paragraph_style.turnHintingOff();
  572. ParagraphBuilderImpl builder(paragraph_style, fontCollection);
  573. TextStyle text_style;
  574. text_style.setFontFamilies({SkString("Ahem")});
  575. text_style.setFontSize(26);
  576. text_style.setColor(SK_ColorBLACK);
  577. text_style.setHeight(1);
  578. text_style.setDecoration(TextDecoration::kUnderline);
  579. text_style.setDecorationColor(SK_ColorBLACK);
  580. builder.pushStyle(text_style);
  581. builder.addText(text);
  582. builder.pop();
  583. auto paragraph = builder.Build();
  584. paragraph->layout(TestCanvasWidth - 100);
  585. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  586. auto calculate = [](const TextLine& line) -> SkScalar {
  587. return TestCanvasWidth - 100 - (line.offset().fX + line.width());
  588. };
  589. SkScalar epsilon = 0.1f;
  590. for (auto& line : impl->lines()) {
  591. if (&line == impl->lines().end() - 1) {
  592. REPORTER_ASSERT(reporter, calculate(line) > epsilon);
  593. } else {
  594. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(line), 0, epsilon));
  595. }
  596. }
  597. // Just make sure the the text is actually RTL
  598. for (auto& run : impl->runs()) {
  599. REPORTER_ASSERT(reporter, !run.leftToRight());
  600. }
  601. }
  602. DEF_TEST(SkParagraph_DecorationsParagraph, reporter) {
  603. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  604. if (!fontCollection->fontsFound()) return;
  605. ParagraphStyle paragraph_style;
  606. paragraph_style.setMaxLines(14);
  607. paragraph_style.setTextAlign(TextAlign::kLeft);
  608. paragraph_style.turnHintingOff();
  609. ParagraphBuilderImpl builder(paragraph_style, fontCollection);
  610. TextStyle text_style;
  611. text_style.setFontFamilies({SkString("Roboto")});
  612. text_style.setFontSize(26);
  613. text_style.setLetterSpacing(0);
  614. text_style.setWordSpacing(5);
  615. text_style.setColor(SK_ColorBLACK);
  616. text_style.setHeight(2);
  617. text_style.setDecoration(TextDecoration::kUnderline);
  618. text_style.setDecorationColor(SK_ColorBLACK);
  619. text_style.setDecoration((TextDecoration)(
  620. TextDecoration::kUnderline | TextDecoration::kOverline | TextDecoration::kLineThrough));
  621. text_style.setDecorationStyle(TextDecorationStyle::kSolid);
  622. text_style.setDecorationColor(SK_ColorBLACK);
  623. text_style.setDecorationThicknessMultiplier(2.0);
  624. builder.pushStyle(text_style);
  625. builder.addText("This text should be");
  626. text_style.setDecorationStyle(TextDecorationStyle::kDouble);
  627. text_style.setDecorationColor(SK_ColorBLUE);
  628. text_style.setDecorationThicknessMultiplier(1.0);
  629. builder.pushStyle(text_style);
  630. builder.addText(" decorated even when");
  631. text_style.setDecorationStyle(TextDecorationStyle::kDotted);
  632. text_style.setDecorationColor(SK_ColorBLACK);
  633. builder.pushStyle(text_style);
  634. builder.addText(" wrapped around to");
  635. text_style.setDecorationStyle(TextDecorationStyle::kDashed);
  636. text_style.setDecorationColor(SK_ColorBLACK);
  637. text_style.setDecorationThicknessMultiplier(3.0);
  638. builder.pushStyle(text_style);
  639. builder.addText(" the next line.");
  640. text_style.setDecorationStyle(TextDecorationStyle::kWavy);
  641. text_style.setDecorationColor(SK_ColorRED);
  642. text_style.setDecorationThicknessMultiplier(1.0);
  643. builder.pushStyle(text_style);
  644. builder.addText(" Otherwise, bad things happen.");
  645. builder.pop();
  646. auto paragraph = builder.Build();
  647. paragraph->layout(TestCanvasWidth - 100);
  648. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  649. size_t index = 0;
  650. for (auto& line : impl->lines()) {
  651. line.scanStyles(
  652. StyleType::kDecorations,
  653. [&index, reporter](TextRange, TextStyle style, SkScalar) {
  654. auto decoration = (TextDecoration)(TextDecoration::kUnderline |
  655. TextDecoration::kOverline |
  656. TextDecoration::kLineThrough);
  657. REPORTER_ASSERT(reporter, style.getDecorationType() == decoration);
  658. switch (index) {
  659. case 0:
  660. REPORTER_ASSERT(reporter, style.getDecorationStyle() ==
  661. TextDecorationStyle::kSolid);
  662. REPORTER_ASSERT(reporter, style.getDecorationColor() == SK_ColorBLACK);
  663. REPORTER_ASSERT(reporter,
  664. style.getDecorationThicknessMultiplier() == 2.0);
  665. break;
  666. case 1: // The style appears on 2 lines so it has 2 pieces
  667. REPORTER_ASSERT(reporter, style.getDecorationStyle() ==
  668. TextDecorationStyle::kDouble);
  669. REPORTER_ASSERT(reporter, style.getDecorationColor() == SK_ColorBLUE);
  670. REPORTER_ASSERT(reporter,
  671. style.getDecorationThicknessMultiplier() == 1.0);
  672. break;
  673. case 2:
  674. REPORTER_ASSERT(reporter, style.getDecorationStyle() ==
  675. TextDecorationStyle::kDotted);
  676. REPORTER_ASSERT(reporter, style.getDecorationColor() == SK_ColorBLACK);
  677. REPORTER_ASSERT(reporter,
  678. style.getDecorationThicknessMultiplier() == 1.0);
  679. break;
  680. case 3:
  681. case 4:
  682. REPORTER_ASSERT(reporter, style.getDecorationStyle() ==
  683. TextDecorationStyle::kDashed);
  684. REPORTER_ASSERT(reporter, style.getDecorationColor() == SK_ColorBLACK);
  685. REPORTER_ASSERT(reporter,
  686. style.getDecorationThicknessMultiplier() == 3.0);
  687. break;
  688. case 5:
  689. REPORTER_ASSERT(reporter, style.getDecorationStyle() ==
  690. TextDecorationStyle::kWavy);
  691. REPORTER_ASSERT(reporter, style.getDecorationColor() == SK_ColorRED);
  692. REPORTER_ASSERT(reporter,
  693. style.getDecorationThicknessMultiplier() == 1.0);
  694. break;
  695. default:
  696. REPORTER_ASSERT(reporter, false);
  697. break;
  698. }
  699. ++index;
  700. return true;
  701. });
  702. }
  703. }
  704. DEF_TEST(SkParagraph_ItalicsParagraph, reporter) {
  705. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  706. if (!fontCollection->fontsFound()) return;
  707. ParagraphStyle paragraph_style;
  708. paragraph_style.turnHintingOff();
  709. ParagraphBuilderImpl builder(paragraph_style, fontCollection);
  710. TextStyle text_style;
  711. text_style.setFontFamilies({SkString("Roboto")});
  712. text_style.setFontSize(10);
  713. text_style.setColor(SK_ColorRED);
  714. builder.pushStyle(text_style);
  715. builder.addText("No italic ");
  716. text_style.setFontStyle(SkFontStyle(SkFontStyle::kNormal_Weight, SkFontStyle::kNormal_Width,
  717. SkFontStyle::kItalic_Slant));
  718. builder.pushStyle(text_style);
  719. builder.addText("Yes Italic ");
  720. builder.pop();
  721. builder.addText("No Italic again.");
  722. auto paragraph = builder.Build();
  723. paragraph->layout(TestCanvasWidth);
  724. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  725. REPORTER_ASSERT(reporter, impl->runs().size() == 3);
  726. REPORTER_ASSERT(reporter, impl->styles().size() == 3);
  727. REPORTER_ASSERT(reporter, impl->lines().size() == 1);
  728. auto& line = impl->lines()[0];
  729. size_t index = 0;
  730. line.scanStyles(
  731. StyleType::kForeground,
  732. [&index, reporter](TextRange textRange, TextStyle style, SkScalar) {
  733. switch (index) {
  734. case 0:
  735. REPORTER_ASSERT(
  736. reporter,
  737. style.getFontStyle().slant() == SkFontStyle::kUpright_Slant);
  738. break;
  739. case 1:
  740. REPORTER_ASSERT(reporter,
  741. style.getFontStyle().slant() == SkFontStyle::kItalic_Slant);
  742. break;
  743. case 2:
  744. REPORTER_ASSERT(
  745. reporter,
  746. style.getFontStyle().slant() == SkFontStyle::kUpright_Slant);
  747. break;
  748. default:
  749. REPORTER_ASSERT(reporter, false);
  750. break;
  751. }
  752. ++index;
  753. return true;
  754. });
  755. }
  756. DEF_TEST(SkParagraph_ChineseParagraph, reporter) {
  757. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  758. if (!fontCollection->fontsFound()) return;
  759. const char* text =
  760. "左線読設重説切後碁給能上目秘使約。満毎冠行来昼本可必図将発確年。今属場育"
  761. "図情闘陰野高備込制詩西校客。審対江置講今固残必託地集済決維駆年策。立得庭"
  762. "際輝求佐抗蒼提夜合逃表。注統天言件自謙雅載報紙喪。作画稿愛器灯女書利変探"
  763. "訃第金線朝開化建。子戦年帝励害表月幕株漠新期刊人秘。図的海力生禁挙保天戦"
  764. "聞条年所在口。";
  765. ParagraphStyle paragraph_style;
  766. paragraph_style.setMaxLines(14);
  767. paragraph_style.setTextAlign(TextAlign::kJustify);
  768. paragraph_style.turnHintingOff();
  769. ParagraphBuilderImpl builder(paragraph_style, fontCollection);
  770. auto decoration = (TextDecoration)(TextDecoration::kUnderline | TextDecoration::kOverline |
  771. TextDecoration::kLineThrough);
  772. TextStyle text_style;
  773. text_style.setFontFamilies({SkString("Source Han Serif CN")});
  774. text_style.setFontSize(35);
  775. text_style.setColor(SK_ColorBLACK);
  776. text_style.setLetterSpacing(2);
  777. text_style.setHeight(1);
  778. text_style.setDecoration(decoration);
  779. text_style.setDecorationColor(SK_ColorBLACK);
  780. text_style.setDecorationStyle(TextDecorationStyle::kSolid);
  781. builder.pushStyle(text_style);
  782. builder.addText(text);
  783. builder.pop();
  784. auto paragraph = builder.Build();
  785. paragraph->layout(TestCanvasWidth - 100);
  786. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  787. REPORTER_ASSERT(reporter, impl->runs().size() == 1);
  788. REPORTER_ASSERT(reporter, impl->lines().size() == 7);
  789. REPORTER_ASSERT(reporter, impl->styles().size() == 1);
  790. REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style));
  791. }
  792. DEF_TEST(SkParagraph_ArabicParagraph, reporter) {
  793. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  794. if (!fontCollection->fontsFound()) return;
  795. const char* text =
  796. "من أسر وإعلان الخاصّة وهولندا،, عل قائمة الضغوط بالمطالبة تلك. الصفحة "
  797. "بمباركة التقليدية قام عن. تصفح";
  798. ParagraphStyle paragraph_style;
  799. paragraph_style.setMaxLines(14);
  800. paragraph_style.setTextAlign(TextAlign::kJustify);
  801. paragraph_style.turnHintingOff();
  802. ParagraphBuilderImpl builder(paragraph_style, fontCollection);
  803. auto decoration = (TextDecoration)(TextDecoration::kUnderline | TextDecoration::kOverline |
  804. TextDecoration::kLineThrough);
  805. TextStyle text_style;
  806. text_style.setFontFamilies({SkString("Katibeh")});
  807. text_style.setFontSize(35);
  808. text_style.setColor(SK_ColorBLACK);
  809. // TODO: turn off some font features for letter spacing (waiting on SkShaper)
  810. // text_style.setLetterSpacing(2);
  811. text_style.setDecoration(decoration);
  812. text_style.setDecorationColor(SK_ColorBLACK);
  813. text_style.setDecorationStyle(TextDecorationStyle::kSolid);
  814. builder.pushStyle(text_style);
  815. builder.addText(text);
  816. builder.pop();
  817. auto paragraph = builder.Build();
  818. paragraph->layout(TestCanvasWidth - 100);
  819. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  820. REPORTER_ASSERT(reporter, impl->runs().size() == 1);
  821. REPORTER_ASSERT(reporter, impl->lines().size() == 2);
  822. REPORTER_ASSERT(reporter, impl->styles().size() == 1);
  823. REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style));
  824. }
  825. DEF_TEST(SkParagraph_GetGlyphPositionAtCoordinateParagraph, reporter) {
  826. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  827. if (!fontCollection->fontsFound()) return;
  828. const char* text =
  829. "12345 67890 12345 67890 12345 67890 12345 67890 12345 67890 12345 "
  830. "67890 12345";
  831. ParagraphStyle paragraphStyle;
  832. paragraphStyle.setTextAlign(TextAlign::kLeft);
  833. paragraphStyle.setMaxLines(10);
  834. paragraphStyle.turnHintingOff();
  835. TextStyle textStyle;
  836. textStyle.setFontFamilies({SkString("Roboto")});
  837. textStyle.setFontSize(50);
  838. textStyle.setLetterSpacing(1);
  839. textStyle.setWordSpacing(5);
  840. textStyle.setHeight(1);
  841. textStyle.setColor(SK_ColorBLACK);
  842. ParagraphBuilderImpl builder(paragraphStyle, fontCollection);
  843. builder.pushStyle(textStyle);
  844. builder.addText(text);
  845. builder.pop();
  846. auto paragraph = builder.Build();
  847. paragraph->layout(550);
  848. // Tests for getGlyphPositionAtCoordinate()
  849. // NOTE: resulting values can be a few off from their respective positions in
  850. // the original text because the final trailing whitespaces are sometimes not
  851. // drawn (namely, when using "justify" alignment) and therefore are not active
  852. // glyphs.
  853. REPORTER_ASSERT(reporter,
  854. paragraph->getGlyphPositionAtCoordinate(-10000, -10000).position == 0);
  855. REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(-1, -1).position == 0);
  856. REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(0, 0).position == 0);
  857. REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(3, 3).position == 0);
  858. REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(35, 1).position == 1);
  859. REPORTER_ASSERT(reporter,
  860. paragraph->getGlyphPositionAtCoordinate(300, 2).position == 10); // !!! 11
  861. REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(301, 2.2f).position == 11);
  862. REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(302, 2.6f).position == 11);
  863. REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(301, 2.1f).position == 11);
  864. REPORTER_ASSERT(reporter,
  865. paragraph->getGlyphPositionAtCoordinate(100000, 20).position == 18);
  866. REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(450, 20).position == 16);
  867. REPORTER_ASSERT(reporter,
  868. paragraph->getGlyphPositionAtCoordinate(100000, 90).position == 36);
  869. REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(-100000, 90).position == 18);
  870. REPORTER_ASSERT(reporter,
  871. paragraph->getGlyphPositionAtCoordinate(20, -80).position == 0); // !!! 1
  872. REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(1, 90).position == 18);
  873. REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(1, 170).position == 36);
  874. REPORTER_ASSERT(reporter,
  875. paragraph->getGlyphPositionAtCoordinate(10000, 180).position == 72);
  876. REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(70, 180).position == 56);
  877. REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(1, 270).position == 72);
  878. REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(35, 90).position == 19);
  879. REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(10000, 10000).position == 77);
  880. REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(85, 10000).position == 75);
  881. }
  882. DEF_TEST(SkParagraph_GetRectsForRangeParagraph, reporter) {
  883. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  884. if (!fontCollection->fontsFound()) return;
  885. const char* text =
  886. "12345, \"67890\" 12345 67890 12345 67890 12345 67890 12345 67890 12345 "
  887. "67890 12345";
  888. ParagraphStyle paragraphStyle;
  889. paragraphStyle.setTextAlign(TextAlign::kLeft);
  890. paragraphStyle.setMaxLines(10);
  891. paragraphStyle.turnHintingOff();
  892. TextStyle textStyle;
  893. textStyle.setFontFamilies({SkString("Roboto")});
  894. textStyle.setFontSize(50);
  895. textStyle.setColor(SK_ColorBLACK);
  896. textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
  897. SkFontStyle::kUpright_Slant));
  898. ParagraphBuilderImpl builder(paragraphStyle, fontCollection);
  899. builder.pushStyle(textStyle);
  900. builder.addText(text);
  901. builder.pop();
  902. auto paragraph = builder.Build();
  903. paragraph->layout(550);
  904. RectHeightStyle heightStyle = RectHeightStyle::kMax;
  905. RectWidthStyle widthStyle = RectWidthStyle::kTight;
  906. SkScalar epsilon = 0.01f;
  907. {
  908. auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle);
  909. REPORTER_ASSERT(reporter, result.empty());
  910. }
  911. {
  912. auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle);
  913. REPORTER_ASSERT(reporter, result.size() == 1);
  914. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 0, epsilon));
  915. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon));
  916. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 28.417f, epsilon));
  917. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
  918. }
  919. {
  920. auto result = paragraph->getRectsForRange(2, 8, heightStyle, widthStyle);
  921. REPORTER_ASSERT(reporter, result.size() == 1);
  922. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 56.835f, epsilon));
  923. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon));
  924. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 177.97f, epsilon));
  925. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
  926. }
  927. {
  928. auto result = paragraph->getRectsForRange(8, 21, heightStyle, widthStyle);
  929. REPORTER_ASSERT(reporter, result.size() == 1);
  930. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 177.97f, epsilon));
  931. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon));
  932. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 507.031f, epsilon));
  933. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
  934. }
  935. {
  936. auto result = paragraph->getRectsForRange(30, 100, heightStyle, widthStyle);
  937. REPORTER_ASSERT(reporter, result.size() == 4);
  938. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 211.375f, epsilon));
  939. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 59.40625f, epsilon));
  940. // This number does not match: 463.617 & 451.171
  941. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 451.171f, epsilon));
  942. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 118, epsilon));
  943. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.left(), 0, epsilon));
  944. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.top(), 236.406f, epsilon));
  945. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.right(), 142.089f, epsilon));
  946. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.bottom(), 295, epsilon));
  947. }
  948. {
  949. auto result = paragraph->getRectsForRange(19, 22, heightStyle, widthStyle);
  950. REPORTER_ASSERT(reporter, result.size() == 1);
  951. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 450.1875f, epsilon));
  952. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon));
  953. // This number does not match 519.472 & 507.031
  954. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 507.031f, epsilon));
  955. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
  956. }
  957. {
  958. auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle);
  959. REPORTER_ASSERT(reporter, result.empty());
  960. }
  961. }
  962. DEF_TEST(SkParagraph_GetRectsForRangeTight, reporter) {
  963. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  964. if (!fontCollection->fontsFound()) return;
  965. const char* text =
  966. "( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)("
  967. " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)("
  968. " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)";
  969. ParagraphStyle paragraphStyle;
  970. paragraphStyle.setTextAlign(TextAlign::kLeft);
  971. paragraphStyle.setMaxLines(10);
  972. paragraphStyle.turnHintingOff();
  973. TextStyle textStyle;
  974. textStyle.setFontFamilies({ SkString("Noto Sans CJK JP")});
  975. textStyle.setFontSize(50);
  976. textStyle.setColor(SK_ColorBLACK);
  977. textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
  978. SkFontStyle::kUpright_Slant));
  979. ParagraphBuilderImpl builder(paragraphStyle, fontCollection);
  980. builder.pushStyle(textStyle);
  981. builder.addText(text);
  982. builder.pop();
  983. auto paragraph = builder.Build();
  984. paragraph->layout(550);
  985. RectHeightStyle heightStyle = RectHeightStyle::kTight;
  986. RectWidthStyle widthStyle = RectWidthStyle::kTight;
  987. SkScalar epsilon = 0.01f;
  988. {
  989. auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle);
  990. REPORTER_ASSERT(reporter, result.empty());
  991. }
  992. {
  993. auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle);
  994. REPORTER_ASSERT(reporter, result.size() == 1);
  995. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 0, epsilon));
  996. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0, epsilon));
  997. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 16.898f, epsilon));
  998. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 74, epsilon));
  999. }
  1000. {
  1001. auto result = paragraph->getRectsForRange(2, 8, heightStyle, widthStyle);
  1002. REPORTER_ASSERT(reporter, result.size() == 1);
  1003. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 66.899f, epsilon));
  1004. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0, epsilon));
  1005. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 264.099f, epsilon));
  1006. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 74, epsilon));
  1007. }
  1008. {
  1009. auto result = paragraph->getRectsForRange(8, 21, heightStyle, widthStyle);
  1010. REPORTER_ASSERT(reporter, result.size() == 2);
  1011. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 264.099f, epsilon));
  1012. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 528.199f, epsilon));
  1013. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.left(), 0, epsilon));
  1014. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.right(), 172.199f, epsilon));
  1015. }
  1016. }
  1017. DEF_TEST(SkParagraph_GetRectsForRangeIncludeLineSpacingMiddle, reporter) {
  1018. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  1019. if (!fontCollection->fontsFound()) return;
  1020. const char* text =
  1021. "( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)("
  1022. " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)("
  1023. " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)";
  1024. ParagraphStyle paragraphStyle;
  1025. paragraphStyle.setTextAlign(TextAlign::kLeft);
  1026. paragraphStyle.setMaxLines(10);
  1027. paragraphStyle.turnHintingOff();
  1028. TextStyle textStyle;
  1029. textStyle.setFontFamilies({SkString("Noto Sans CJK JP")});
  1030. textStyle.setFontSize(50);
  1031. textStyle.setHeight(1.3f);
  1032. textStyle.setColor(SK_ColorBLACK);
  1033. textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
  1034. SkFontStyle::kUpright_Slant));
  1035. ParagraphBuilderImpl builder(paragraphStyle, fontCollection);
  1036. builder.pushStyle(textStyle);
  1037. builder.addText(text);
  1038. builder.pop();
  1039. auto paragraph = builder.Build();
  1040. paragraph->layout(550);
  1041. RectHeightStyle heightStyle = RectHeightStyle::kIncludeLineSpacingMiddle;
  1042. RectWidthStyle widthStyle = RectWidthStyle::kTight;
  1043. SkScalar epsilon = 0.01f;
  1044. // 16 glyphs per line 160/16 = 10 lines
  1045. {
  1046. auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle);
  1047. REPORTER_ASSERT(reporter, result.empty());
  1048. }
  1049. {
  1050. auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle);
  1051. REPORTER_ASSERT(reporter, result.size() == 1);
  1052. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 0, epsilon));
  1053. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 8.60f, epsilon));
  1054. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 16.90f, epsilon));
  1055. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 93.60f, epsilon));
  1056. }
  1057. {
  1058. auto result = paragraph->getRectsForRange(2, 8, heightStyle, widthStyle);
  1059. REPORTER_ASSERT(reporter, result.size() == 1);
  1060. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 66.90f, epsilon));
  1061. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 8.60f, epsilon));
  1062. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 264.10f, epsilon));
  1063. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 93.60f, epsilon));
  1064. }
  1065. {
  1066. auto result = paragraph->getRectsForRange(8, 21, heightStyle, widthStyle);
  1067. REPORTER_ASSERT(reporter, result.size() == 2);
  1068. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 264.10f, epsilon));
  1069. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 8.60f, epsilon));
  1070. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 528.20f, epsilon));
  1071. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 93.60f, epsilon));
  1072. }
  1073. {
  1074. auto result = paragraph->getRectsForRange(30, 150, heightStyle, widthStyle);
  1075. REPORTER_ASSERT(reporter, result.size() == 9);
  1076. SkScalar offsetY = 104.60f;
  1077. for (auto& box : result) {
  1078. if (&box != &result.back()) {
  1079. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(box.rect.right(), 528.20f, epsilon));
  1080. }
  1081. if (&box != &result.front()) {
  1082. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(box.rect.left(), 0, epsilon));
  1083. }
  1084. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(box.rect.top(), offsetY, epsilon));
  1085. offsetY = box.rect.bottom() + 11;
  1086. }
  1087. }
  1088. {
  1089. auto result = paragraph->getRectsForRange(19, 22, heightStyle, widthStyle);
  1090. REPORTER_ASSERT(reporter, result.size() == 1);
  1091. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 97.20f, epsilon));
  1092. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 104.60f, epsilon));
  1093. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 197.20f, epsilon));
  1094. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 189.60f, epsilon));
  1095. }
  1096. {
  1097. auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle);
  1098. REPORTER_ASSERT(reporter, result.empty());
  1099. }
  1100. }
  1101. DEF_TEST(SkParagraph_GetRectsForRangeIncludeLineSpacingTop, reporter) {
  1102. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  1103. if (!fontCollection->fontsFound()) return;
  1104. const char* text =
  1105. "( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)("
  1106. " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)("
  1107. " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)";
  1108. ParagraphStyle paragraphStyle;
  1109. paragraphStyle.setTextAlign(TextAlign::kLeft);
  1110. paragraphStyle.setMaxLines(10);
  1111. paragraphStyle.turnHintingOff();
  1112. TextStyle textStyle;
  1113. textStyle.setFontFamilies({SkString("Noto Sans CJK JP")});
  1114. textStyle.setFontSize(50);
  1115. textStyle.setWordSpacing(0);
  1116. textStyle.setLetterSpacing(0);
  1117. textStyle.setHeight(1.3f);
  1118. textStyle.setColor(SK_ColorBLACK);
  1119. textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
  1120. SkFontStyle::kUpright_Slant));
  1121. ParagraphBuilderImpl builder(paragraphStyle, fontCollection);
  1122. builder.pushStyle(textStyle);
  1123. builder.addText(text);
  1124. builder.pop();
  1125. auto paragraph = builder.Build();
  1126. paragraph->layout(550);
  1127. RectHeightStyle heightStyle = RectHeightStyle::kIncludeLineSpacingTop;
  1128. RectWidthStyle widthStyle = RectWidthStyle::kTight;
  1129. SkScalar epsilon = 0.01f;
  1130. {
  1131. auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle);
  1132. REPORTER_ASSERT(reporter, result.empty());
  1133. }
  1134. {
  1135. auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle);
  1136. REPORTER_ASSERT(reporter, result.size() == 1);
  1137. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 0, epsilon));
  1138. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0, epsilon));
  1139. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 16.898f, epsilon));
  1140. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 91.199f, epsilon));
  1141. }
  1142. {
  1143. auto result = paragraph->getRectsForRange(2, 8, heightStyle, widthStyle);
  1144. REPORTER_ASSERT(reporter, result.size() == 1);
  1145. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 66.899f, epsilon));
  1146. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0, epsilon));
  1147. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 264.099f, epsilon));
  1148. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 91.199f, epsilon));
  1149. }
  1150. {
  1151. auto result = paragraph->getRectsForRange(8, 21, heightStyle, widthStyle);
  1152. REPORTER_ASSERT(reporter, result.size() == 2);
  1153. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 264.099f, epsilon));
  1154. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0, epsilon));
  1155. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 528.199f, epsilon));
  1156. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 91.199f, epsilon));
  1157. }
  1158. {
  1159. auto result = paragraph->getRectsForRange(30, 150, heightStyle, widthStyle);
  1160. REPORTER_ASSERT(reporter, result.size() == 9);
  1161. SkScalar level = 96;
  1162. for (auto& box : result) {
  1163. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(box.rect.top(), level, epsilon));
  1164. level += 96;
  1165. if (&box != &result.back()) {
  1166. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(box.rect.right(), 528.199f, epsilon));
  1167. }
  1168. if (&box != &result.front()) {
  1169. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(box.rect.left(), 0, epsilon));
  1170. }
  1171. }
  1172. }
  1173. {
  1174. auto result = paragraph->getRectsForRange(19, 22, heightStyle, widthStyle);
  1175. REPORTER_ASSERT(reporter, result.size() == 1);
  1176. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 97.199f, epsilon));
  1177. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 96, epsilon));
  1178. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 197.199f, epsilon));
  1179. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 187.199f, epsilon));
  1180. }
  1181. {
  1182. auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle);
  1183. REPORTER_ASSERT(reporter, result.empty());
  1184. }
  1185. }
  1186. DEF_TEST(SkParagraph_GetRectsForRangeIncludeLineSpacingBottom, reporter) {
  1187. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  1188. if (!fontCollection->fontsFound()) return;
  1189. const char* text =
  1190. "( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)("
  1191. " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)("
  1192. " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)";
  1193. ParagraphStyle paragraphStyle;
  1194. paragraphStyle.setTextAlign(TextAlign::kLeft);
  1195. paragraphStyle.setMaxLines(10);
  1196. paragraphStyle.turnHintingOff();
  1197. TextStyle textStyle;
  1198. textStyle.setFontFamilies({SkString("Noto Sans CJK JP")});
  1199. textStyle.setFontSize(50);
  1200. textStyle.setWordSpacing(0);
  1201. textStyle.setLetterSpacing(0);
  1202. textStyle.setHeight(1.3f);
  1203. textStyle.setColor(SK_ColorBLACK);
  1204. textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
  1205. SkFontStyle::kUpright_Slant));
  1206. ParagraphBuilderImpl builder(paragraphStyle, fontCollection);
  1207. builder.pushStyle(textStyle);
  1208. builder.addText(text);
  1209. builder.pop();
  1210. auto paragraph = builder.Build();
  1211. paragraph->layout(550);
  1212. RectHeightStyle heightStyle = RectHeightStyle::kIncludeLineSpacingBottom;
  1213. RectWidthStyle widthStyle = RectWidthStyle::kTight;
  1214. SkScalar epsilon = 0.01f;
  1215. {
  1216. auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle);
  1217. REPORTER_ASSERT(reporter, result.empty());
  1218. }
  1219. {
  1220. auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle);
  1221. REPORTER_ASSERT(reporter, result.size() == 1);
  1222. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 0, epsilon));
  1223. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 17.199f, epsilon));
  1224. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 16.898f, epsilon));
  1225. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 96, epsilon));
  1226. }
  1227. {
  1228. auto result = paragraph->getRectsForRange(2, 8, heightStyle, widthStyle);
  1229. REPORTER_ASSERT(reporter, result.size() == 1);
  1230. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 66.899f, epsilon));
  1231. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 17.199f, epsilon));
  1232. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 264.099f, epsilon));
  1233. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 96, epsilon));
  1234. }
  1235. {
  1236. auto result = paragraph->getRectsForRange(8, 21, heightStyle, widthStyle);
  1237. REPORTER_ASSERT(reporter, result.size() == 2);
  1238. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 264.099f, epsilon));
  1239. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 528.199f, epsilon));
  1240. // It seems that Minikin does not take in account like breaks, but we do.
  1241. // SkParagraph returns 528.199 instead
  1242. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.left(), 0, epsilon));
  1243. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.right(), 172.199f, epsilon));
  1244. }
  1245. {
  1246. auto result = paragraph->getRectsForRange(30, 150, heightStyle, widthStyle);
  1247. REPORTER_ASSERT(reporter, result.size() == 9);
  1248. SkScalar level = 17.199f + 96;
  1249. for (auto& box : result) {
  1250. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(box.rect.top(), level, epsilon));
  1251. level += 96;
  1252. if (&box != &result.back()) {
  1253. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(box.rect.right(), 528.199f, epsilon));
  1254. }
  1255. if (&box != &result.front()) {
  1256. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(box.rect.left(), 0, epsilon));
  1257. }
  1258. }
  1259. }
  1260. {
  1261. auto result = paragraph->getRectsForRange(19, 22, heightStyle, widthStyle);
  1262. REPORTER_ASSERT(reporter, result.size() == 1);
  1263. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 97.199f, epsilon));
  1264. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 113.199f, epsilon));
  1265. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 197.199f, epsilon));
  1266. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 192, epsilon));
  1267. }
  1268. {
  1269. auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle);
  1270. REPORTER_ASSERT(reporter, result.empty());
  1271. }
  1272. }
  1273. DEF_TEST(SkParagraph_GetRectsForRangeIncludeCombiningCharacter, reporter) {
  1274. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  1275. if (!fontCollection->fontsFound()) return;
  1276. const char* text = "ดีสวัสดีชาวโลกที่น่ารัก";
  1277. ParagraphStyle paragraphStyle;
  1278. paragraphStyle.setTextAlign(TextAlign::kCenter);
  1279. paragraphStyle.setMaxLines(10);
  1280. paragraphStyle.turnHintingOff();
  1281. ParagraphBuilderImpl builder(paragraphStyle, fontCollection);
  1282. TextStyle textStyle;
  1283. textStyle.setFontFamilies({SkString("Roboto")});
  1284. textStyle.setFontSize(50);
  1285. textStyle.setLetterSpacing(1);
  1286. textStyle.setWordSpacing(5);
  1287. textStyle.setHeight(1);
  1288. textStyle.setColor(SK_ColorBLACK);
  1289. builder.pushStyle(textStyle);
  1290. builder.addText(text);
  1291. builder.pop();
  1292. auto paragraph = builder.Build();
  1293. paragraph->layout(TestCanvasWidth - 100);
  1294. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  1295. REPORTER_ASSERT(reporter, impl->lines().size() == 1);
  1296. RectHeightStyle heightStyle = RectHeightStyle::kTight;
  1297. RectWidthStyle widthStyle = RectWidthStyle::kTight;
  1298. {
  1299. auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle);
  1300. REPORTER_ASSERT(reporter, result.empty());
  1301. }
  1302. {
  1303. auto first = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle);
  1304. auto second = paragraph->getRectsForRange(1, 2, heightStyle, widthStyle);
  1305. auto last = paragraph->getRectsForRange(0, 2, heightStyle, widthStyle);
  1306. REPORTER_ASSERT(reporter, first.size() == 1 && second.size() == 1 && last.size() == 1);
  1307. REPORTER_ASSERT(reporter,
  1308. last[0].rect.fLeft = SkTMin(first[0].rect.fLeft, second[0].rect.fLeft));
  1309. REPORTER_ASSERT(reporter,
  1310. last[0].rect.fRight = SkTMax(first[0].rect.fRight, second[0].rect.fRight));
  1311. }
  1312. {
  1313. auto first = paragraph->getRectsForRange(3, 4, heightStyle, widthStyle);
  1314. auto second = paragraph->getRectsForRange(4, 5, heightStyle, widthStyle);
  1315. auto last = paragraph->getRectsForRange(3, 5, heightStyle, widthStyle);
  1316. REPORTER_ASSERT(reporter, first.size() == 1 && second.size() == 1 && last.size() == 1);
  1317. REPORTER_ASSERT(reporter,
  1318. last[0].rect.fLeft = SkTMin(first[0].rect.fLeft, second[0].rect.fLeft));
  1319. REPORTER_ASSERT(reporter,
  1320. last[0].rect.fRight = SkTMax(first[0].rect.fRight, second[0].rect.fRight));
  1321. }
  1322. {
  1323. auto first = paragraph->getRectsForRange(14, 15, heightStyle, widthStyle);
  1324. auto second = paragraph->getRectsForRange(15, 16, heightStyle, widthStyle);
  1325. auto third = paragraph->getRectsForRange(16, 17, heightStyle, widthStyle);
  1326. auto last = paragraph->getRectsForRange(14, 17, heightStyle, widthStyle);
  1327. REPORTER_ASSERT(reporter, first.size() == 1 && second.size() == 1 && third.size() == 1 &&
  1328. last.size() == 1);
  1329. REPORTER_ASSERT(reporter,
  1330. last[0].rect.fLeft = SkTMin(first[0].rect.fLeft, third[0].rect.fLeft));
  1331. REPORTER_ASSERT(reporter,
  1332. last[0].rect.fRight = SkTMax(first[0].rect.fRight, third[0].rect.fRight));
  1333. }
  1334. }
  1335. DEF_TEST(SkParagraph_GetRectsForRangeCenterParagraph, reporter) {
  1336. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  1337. if (!fontCollection->fontsFound()) return;
  1338. // Minikin uses a hard coded list of unicode characters that he treats as invisible - as spaces.
  1339. // It's absolutely wrong - invisibility is a glyph attribute, not character/grapheme.
  1340. // Any attempt to substitute one for another leads to errors
  1341. // (for instance, some fonts can use these hard coded characters for something that is visible)
  1342. const char* text =
  1343. "01234 "; //"01234   "; // includes ideographic space and english space.
  1344. ParagraphStyle paragraphStyle;
  1345. paragraphStyle.setTextAlign(TextAlign::kCenter);
  1346. paragraphStyle.setMaxLines(10);
  1347. paragraphStyle.turnHintingOff();
  1348. ParagraphBuilderImpl builder(paragraphStyle, fontCollection);
  1349. TextStyle textStyle;
  1350. textStyle.setFontFamilies({SkString("Roboto")});
  1351. textStyle.setFontSize(50);
  1352. textStyle.setLetterSpacing(0);
  1353. textStyle.setWordSpacing(0);
  1354. textStyle.setHeight(1);
  1355. textStyle.setColor(SK_ColorBLACK);
  1356. textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
  1357. SkFontStyle::kUpright_Slant));
  1358. builder.pushStyle(textStyle);
  1359. builder.addText(text);
  1360. builder.pop();
  1361. auto paragraph = builder.Build();
  1362. paragraph->layout(550);
  1363. // Some of the formatting lazily done on paint
  1364. RectHeightStyle heightStyle = RectHeightStyle::kMax;
  1365. RectWidthStyle widthStyle = RectWidthStyle::kTight;
  1366. SkScalar epsilon = 0.01f;
  1367. {
  1368. auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle);
  1369. REPORTER_ASSERT(reporter, result.empty());
  1370. }
  1371. {
  1372. auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle);
  1373. REPORTER_ASSERT(reporter, result.size() == 1);
  1374. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 203.955f, epsilon));
  1375. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon));
  1376. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 232.373f, epsilon));
  1377. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
  1378. }
  1379. {
  1380. auto result = paragraph->getRectsForRange(2, 4, heightStyle, widthStyle);
  1381. REPORTER_ASSERT(reporter, result.size() == 1);
  1382. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 260.791f, epsilon));
  1383. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon));
  1384. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 317.626f, epsilon));
  1385. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
  1386. }
  1387. {
  1388. auto result = paragraph->getRectsForRange(4, 5, heightStyle, widthStyle);
  1389. REPORTER_ASSERT(reporter, result.size() == 1);
  1390. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 317.626f, epsilon));
  1391. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon));
  1392. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 346.044f, epsilon));
  1393. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
  1394. }
  1395. {
  1396. auto result = paragraph->getRectsForRange(4, 6, heightStyle, widthStyle);
  1397. REPORTER_ASSERT(reporter, result.size() == 1);
  1398. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 317.626f, epsilon));
  1399. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon));
  1400. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 346.044f, epsilon));
  1401. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
  1402. }
  1403. {
  1404. auto result = paragraph->getRectsForRange(5, 6, heightStyle, widthStyle);
  1405. REPORTER_ASSERT(reporter, result.size() == 1);
  1406. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 346.044f, epsilon));
  1407. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon));
  1408. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 346.044f, epsilon));
  1409. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
  1410. }
  1411. {
  1412. auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle);
  1413. REPORTER_ASSERT(reporter, result.empty());
  1414. }
  1415. }
  1416. DEF_TEST(SkParagraph_GetRectsForRangeCenterParagraphNewlineCentered, reporter) {
  1417. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  1418. if (!fontCollection->fontsFound()) return;
  1419. const char* text = "01234\n";
  1420. ParagraphStyle paragraphStyle;
  1421. paragraphStyle.setTextAlign(TextAlign::kCenter);
  1422. paragraphStyle.setMaxLines(10);
  1423. paragraphStyle.turnHintingOff();
  1424. ParagraphBuilderImpl builder(paragraphStyle, fontCollection);
  1425. TextStyle textStyle;
  1426. textStyle.setFontFamilies({SkString("Roboto")});
  1427. textStyle.setFontSize(50);
  1428. textStyle.setLetterSpacing(0);
  1429. textStyle.setWordSpacing(0);
  1430. textStyle.setHeight(1);
  1431. textStyle.setColor(SK_ColorBLACK);
  1432. textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
  1433. SkFontStyle::kUpright_Slant));
  1434. builder.pushStyle(textStyle);
  1435. builder.addText(text);
  1436. builder.pop();
  1437. auto paragraph = builder.Build();
  1438. paragraph->layout(550);
  1439. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  1440. REPORTER_ASSERT(reporter, impl->lines().size() == 2);
  1441. RectHeightStyle heightStyle = RectHeightStyle::kMax;
  1442. RectWidthStyle widthStyle = RectWidthStyle::kTight;
  1443. SkScalar epsilon = 0.01f;
  1444. {
  1445. auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle);
  1446. REPORTER_ASSERT(reporter, result.empty());
  1447. }
  1448. {
  1449. auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle);
  1450. REPORTER_ASSERT(reporter, result.size() == 1);
  1451. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 203.955f, epsilon));
  1452. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon));
  1453. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 232.373f, epsilon));
  1454. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
  1455. }
  1456. {
  1457. // Minikin has [6:7] not empty but I cannot imagine how and why
  1458. auto result = paragraph->getRectsForRange(5, 6, heightStyle, widthStyle);
  1459. REPORTER_ASSERT(reporter, result.size() == 1);
  1460. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 346.044f, epsilon));
  1461. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.406f, epsilon));
  1462. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 346.044f, epsilon));
  1463. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
  1464. }
  1465. }
  1466. DEF_TEST(SkParagraph_GetRectsForRangeCenterMultiLineParagraph, reporter) {
  1467. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  1468. if (!fontCollection->fontsFound()) return;
  1469. const char* text = "01234 \n0123 ";
  1470. // "01234   \n0123  "; // includes ideographic space and english space.
  1471. ParagraphStyle paragraphStyle;
  1472. paragraphStyle.setTextAlign(TextAlign::kCenter);
  1473. paragraphStyle.setMaxLines(10);
  1474. paragraphStyle.turnHintingOff();
  1475. ParagraphBuilderImpl builder(paragraphStyle, fontCollection);
  1476. TextStyle textStyle;
  1477. textStyle.setFontFamilies({SkString("Roboto")});
  1478. textStyle.setFontSize(50);
  1479. textStyle.setLetterSpacing(0);
  1480. textStyle.setWordSpacing(0);
  1481. textStyle.setHeight(1);
  1482. textStyle.setColor(SK_ColorBLACK);
  1483. textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
  1484. SkFontStyle::kUpright_Slant));
  1485. builder.pushStyle(textStyle);
  1486. builder.addText(text);
  1487. builder.pop();
  1488. auto paragraph = builder.Build();
  1489. paragraph->layout(550);
  1490. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  1491. REPORTER_ASSERT(reporter, impl->lines().size() == 2);
  1492. RectHeightStyle heightStyle = RectHeightStyle::kMax;
  1493. RectWidthStyle widthStyle = RectWidthStyle::kTight;
  1494. SkScalar epsilon = 0.01f;
  1495. {
  1496. auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle);
  1497. REPORTER_ASSERT(reporter, result.empty());
  1498. }
  1499. {
  1500. auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle);
  1501. REPORTER_ASSERT(reporter, result.size() == 1);
  1502. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 203.955f, epsilon));
  1503. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon));
  1504. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 232.373f, epsilon));
  1505. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
  1506. }
  1507. {
  1508. auto result = paragraph->getRectsForRange(2, 4, heightStyle, widthStyle);
  1509. REPORTER_ASSERT(reporter, result.size() == 1);
  1510. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 260.791f, epsilon));
  1511. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon));
  1512. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 317.626f, epsilon));
  1513. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
  1514. }
  1515. {
  1516. auto result = paragraph->getRectsForRange(4, 5, heightStyle, widthStyle);
  1517. REPORTER_ASSERT(reporter, result.size() == 1);
  1518. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 317.626f, epsilon));
  1519. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon));
  1520. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 346.044f, epsilon));
  1521. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
  1522. }
  1523. {
  1524. auto result = paragraph->getRectsForRange(4, 6, heightStyle, widthStyle);
  1525. REPORTER_ASSERT(reporter, result.size() == 1);
  1526. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 317.626f, epsilon));
  1527. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon));
  1528. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 346.044f, epsilon));
  1529. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
  1530. }
  1531. {
  1532. auto result = paragraph->getRectsForRange(5, 6, heightStyle, widthStyle);
  1533. REPORTER_ASSERT(reporter, result.size() == 1);
  1534. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 346.044f, epsilon));
  1535. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon));
  1536. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 346.044f, epsilon));
  1537. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
  1538. }
  1539. {
  1540. auto result = paragraph->getRectsForRange(10, 12, heightStyle, widthStyle);
  1541. REPORTER_ASSERT(reporter, result.size() == 1);
  1542. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 218.164f, epsilon));
  1543. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 59.40625f, epsilon));
  1544. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 275, epsilon));
  1545. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 118, epsilon));
  1546. }
  1547. {
  1548. // Minikin counts all spaces but they really don't appear on the screen
  1549. // and the text even centered without them
  1550. auto result = paragraph->getRectsForRange(14, 18, heightStyle, widthStyle);
  1551. REPORTER_ASSERT(reporter, result.size() == 1);
  1552. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 331.835f, epsilon));
  1553. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 59.40625f, epsilon));
  1554. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 331.835f, epsilon));
  1555. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 118, epsilon));
  1556. }
  1557. {
  1558. auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle);
  1559. REPORTER_ASSERT(reporter, result.empty());
  1560. }
  1561. }
  1562. DEF_TEST(SkParagraph_GetWordBoundaries, reporter) {
  1563. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  1564. if (!fontCollection->fontsFound()) return;
  1565. ParagraphStyle paragraphStyle;
  1566. paragraphStyle.setTextAlign(TextAlign::kLeft);
  1567. paragraphStyle.setMaxLines(10);
  1568. paragraphStyle.turnHintingOff();
  1569. TextStyle textStyle;
  1570. textStyle.setFontFamilies({SkString("Roboto")});
  1571. textStyle.setFontSize(52);
  1572. textStyle.setLetterSpacing(1.19039f);
  1573. textStyle.setWordSpacing(5);
  1574. textStyle.setHeight(1.5);
  1575. textStyle.setColor(SK_ColorBLACK);
  1576. textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
  1577. SkFontStyle::kUpright_Slant));
  1578. ParagraphBuilderImpl builder(paragraphStyle, fontCollection);
  1579. builder.pushStyle(textStyle);
  1580. builder.addText(
  1581. "12345 67890 12345 67890 12345 67890 12345 67890 12345 67890 12345 67890 12345");
  1582. builder.pop();
  1583. auto paragraph = builder.Build();
  1584. paragraph->layout(550);
  1585. REPORTER_ASSERT(reporter, paragraph->getWordBoundary(0) == SkRange<size_t>(0, 5));
  1586. REPORTER_ASSERT(reporter, paragraph->getWordBoundary(1) == SkRange<size_t>(0, 5));
  1587. REPORTER_ASSERT(reporter, paragraph->getWordBoundary(2) == SkRange<size_t>(0, 5));
  1588. REPORTER_ASSERT(reporter, paragraph->getWordBoundary(3) == SkRange<size_t>(0, 5));
  1589. REPORTER_ASSERT(reporter, paragraph->getWordBoundary(4) == SkRange<size_t>(0, 5));
  1590. REPORTER_ASSERT(reporter, paragraph->getWordBoundary(5) == SkRange<size_t>(5, 7));
  1591. REPORTER_ASSERT(reporter, paragraph->getWordBoundary(6) == SkRange<size_t>(5, 7));
  1592. REPORTER_ASSERT(reporter, paragraph->getWordBoundary(7) == SkRange<size_t>(7, 12));
  1593. REPORTER_ASSERT(reporter, paragraph->getWordBoundary(8) == SkRange<size_t>(7, 12));
  1594. REPORTER_ASSERT(reporter, paragraph->getWordBoundary(9) == SkRange<size_t>(7, 12));
  1595. REPORTER_ASSERT(reporter, paragraph->getWordBoundary(10) == SkRange<size_t>(7, 12));
  1596. REPORTER_ASSERT(reporter, paragraph->getWordBoundary(11) == SkRange<size_t>(7, 12));
  1597. REPORTER_ASSERT(reporter, paragraph->getWordBoundary(12) == SkRange<size_t>(12, 13));
  1598. REPORTER_ASSERT(reporter, paragraph->getWordBoundary(13) == SkRange<size_t>(13, 18));
  1599. REPORTER_ASSERT(reporter, paragraph->getWordBoundary(30) == SkRange<size_t>(30, 31));
  1600. auto len = static_cast<ParagraphImpl*>(paragraph.get())->text().size();
  1601. REPORTER_ASSERT(reporter, paragraph->getWordBoundary(len - 1) == SkRange<size_t>(len - 5, len));
  1602. }
  1603. DEF_TEST(SkParagraph_SpacingParagraph, reporter) {
  1604. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  1605. if (!fontCollection->fontsFound()) return;
  1606. ParagraphStyle paragraph_style;
  1607. paragraph_style.setMaxLines(10);
  1608. paragraph_style.setTextAlign(TextAlign::kLeft);
  1609. paragraph_style.turnHintingOff();
  1610. ParagraphBuilderImpl builder(paragraph_style, fontCollection);
  1611. TextStyle text_style;
  1612. text_style.setFontFamilies({SkString("Roboto")});
  1613. text_style.setFontSize(50);
  1614. text_style.setLetterSpacing(20);
  1615. text_style.setWordSpacing(0);
  1616. text_style.setColor(SK_ColorBLACK);
  1617. text_style.setHeight(1);
  1618. builder.pushStyle(text_style);
  1619. builder.addText("H");
  1620. builder.pop();
  1621. text_style.setLetterSpacing(10);
  1622. builder.pushStyle(text_style);
  1623. builder.addText("H");
  1624. builder.pop();
  1625. text_style.setLetterSpacing(20);
  1626. builder.pushStyle(text_style);
  1627. builder.addText("H");
  1628. builder.pop();
  1629. text_style.setLetterSpacing(0);
  1630. builder.pushStyle(text_style);
  1631. builder.addText("|");
  1632. builder.pop();
  1633. text_style.setLetterSpacing(0);
  1634. text_style.setWordSpacing(20);
  1635. builder.pushStyle(text_style);
  1636. builder.addText("H ");
  1637. builder.pop();
  1638. text_style.setLetterSpacing(0);
  1639. text_style.setWordSpacing(0);
  1640. builder.pushStyle(text_style);
  1641. builder.addText("H ");
  1642. builder.pop();
  1643. text_style.setLetterSpacing(0);
  1644. text_style.setWordSpacing(20);
  1645. builder.pushStyle(text_style);
  1646. builder.addText("H ");
  1647. builder.pop();
  1648. auto paragraph = builder.Build();
  1649. paragraph->layout(550);
  1650. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  1651. REPORTER_ASSERT(reporter, impl->lines().size() == 1);
  1652. size_t index = 0;
  1653. impl->lines().begin()->scanStyles(StyleType::kLetterSpacing,
  1654. [&index](TextRange text, TextStyle style, SkScalar) {
  1655. ++index;
  1656. return true;
  1657. });
  1658. REPORTER_ASSERT(reporter, index == 4);
  1659. index = 0;
  1660. impl->lines().begin()->scanStyles(StyleType::kWordSpacing,
  1661. [&index](TextRange text, TextStyle style, SkScalar) {
  1662. ++index;
  1663. return true;
  1664. });
  1665. REPORTER_ASSERT(reporter, index == 4);
  1666. }
  1667. DEF_TEST(SkParagraph_LongWordParagraph, reporter) {
  1668. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  1669. if (!fontCollection->fontsFound()) return;
  1670. const char* text =
  1671. "A "
  1672. "veryverylongwordtoseewherethiswillwraporifitwillatallandifitdoesthenthat"
  1673. "wouldbeagoodthingbecausethebreakingisworking.";
  1674. ParagraphStyle paragraph_style;
  1675. paragraph_style.turnHintingOff();
  1676. ParagraphBuilderImpl builder(paragraph_style, fontCollection);
  1677. TextStyle text_style;
  1678. text_style.setFontFamilies({SkString("Roboto")});
  1679. text_style.setColor(SK_ColorRED);
  1680. text_style.setFontSize(31);
  1681. text_style.setLetterSpacing(0);
  1682. text_style.setWordSpacing(0);
  1683. text_style.setColor(SK_ColorBLACK);
  1684. text_style.setHeight(1);
  1685. builder.pushStyle(text_style);
  1686. builder.addText(text);
  1687. builder.pop();
  1688. auto paragraph = builder.Build();
  1689. paragraph->layout(TestCanvasWidth / 2);
  1690. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  1691. REPORTER_ASSERT(reporter, impl->text().size() == std::string{text}.length());
  1692. REPORTER_ASSERT(reporter, impl->runs().size() == 1);
  1693. REPORTER_ASSERT(reporter, impl->styles().size() == 1);
  1694. REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style));
  1695. REPORTER_ASSERT(reporter, impl->lines().size() == 4);
  1696. REPORTER_ASSERT(reporter, impl->lines()[0].width() > TestCanvasWidth / 2 - 20);
  1697. REPORTER_ASSERT(reporter, impl->lines()[1].width() > TestCanvasWidth / 2 - 20);
  1698. REPORTER_ASSERT(reporter, impl->lines()[2].width() > TestCanvasWidth / 2 - 20);
  1699. }
  1700. DEF_TEST(SkParagraph_KernScaleParagraph, reporter) {
  1701. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  1702. if (!fontCollection->fontsFound()) return;
  1703. float scale = 3.0f;
  1704. ParagraphStyle paragraph_style;
  1705. ParagraphBuilderImpl builder(paragraph_style, fontCollection);
  1706. TextStyle text_style;
  1707. text_style.setFontFamilies({SkString("Droid Serif")});
  1708. text_style.setFontSize(100 / scale);
  1709. text_style.setWordSpacing(0);
  1710. text_style.setLetterSpacing(0);
  1711. text_style.setHeight(1);
  1712. text_style.setColor(SK_ColorBLACK);
  1713. builder.pushStyle(text_style);
  1714. builder.addText("AV00\nVA00\nA0V0");
  1715. builder.pop();
  1716. auto paragraph = builder.Build();
  1717. paragraph->layout(TestCanvasWidth / scale);
  1718. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  1719. // First and second lines must have the same width, the third one must be bigger
  1720. SkScalar epsilon = 0.01f;
  1721. REPORTER_ASSERT(reporter, impl->lines().size() == 3);
  1722. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].width(), 80.58f, epsilon));
  1723. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[1].width(), 80.58f, epsilon));
  1724. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[2].width(), 83.25f, epsilon));
  1725. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].height(), 39.00f, epsilon));
  1726. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[1].height(), 39.00f, epsilon));
  1727. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[2].height(), 39.00f, epsilon));
  1728. }
  1729. DEF_TEST(SkParagraph_NewlineParagraph, reporter) {
  1730. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  1731. if (!fontCollection->fontsFound()) return;
  1732. const char* text =
  1733. "line1\nline2 test1 test2 test3 test4 test5 test6 test7\nline3\n\nline4 "
  1734. "test1 test2 test3 test4";
  1735. ParagraphStyle paragraph_style;
  1736. paragraph_style.turnHintingOff();
  1737. ParagraphBuilderImpl builder(paragraph_style, fontCollection);
  1738. TextStyle text_style;
  1739. text_style.setFontFamilies({SkString("Roboto")});
  1740. text_style.setColor(SK_ColorRED);
  1741. text_style.setFontSize(60);
  1742. text_style.setLetterSpacing(0);
  1743. text_style.setWordSpacing(0);
  1744. text_style.setColor(SK_ColorBLACK);
  1745. text_style.setHeight(1);
  1746. builder.pushStyle(text_style);
  1747. builder.addText(text);
  1748. builder.pop();
  1749. auto paragraph = builder.Build();
  1750. paragraph->layout(TestCanvasWidth - 300);
  1751. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  1752. // Minikin does not count empty lines but SkParagraph does
  1753. REPORTER_ASSERT(reporter, impl->lines().size() == 7);
  1754. REPORTER_ASSERT(reporter, impl->lines()[0].offset().fY == 0);
  1755. REPORTER_ASSERT(reporter, impl->lines()[1].offset().fY == 70);
  1756. REPORTER_ASSERT(reporter, impl->lines()[2].offset().fY == 140);
  1757. REPORTER_ASSERT(reporter, impl->lines()[3].offset().fY == 210);
  1758. REPORTER_ASSERT(reporter, impl->lines()[4].offset().fY == 280); // Empty line
  1759. REPORTER_ASSERT(reporter, impl->lines()[5].offset().fY == 350);
  1760. REPORTER_ASSERT(reporter, impl->lines()[6].offset().fY == 420);
  1761. SkScalar epsilon = 0.1f;
  1762. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].width(), 130.31f, epsilon));
  1763. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[1].width(), 586.64f, epsilon));
  1764. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[2].width(), 593.49f, epsilon));
  1765. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[3].width(), 130.31f, epsilon));
  1766. REPORTER_ASSERT(reporter,
  1767. SkScalarNearlyEqual(impl->lines()[4].width(), 0, epsilon)); // Empty line
  1768. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[5].width(), 586.64f, epsilon));
  1769. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[6].width(), 137.16f, epsilon));
  1770. REPORTER_ASSERT(reporter, impl->lineShift(0) == 0);
  1771. }
  1772. DEF_TEST(SkParagraph_EmojiParagraph, reporter) {
  1773. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  1774. if (!fontCollection->fontsFound()) return;
  1775. const char* text =
  1776. "😀😃😄😁😆😅😂🤣☺😇🙂😍😡😟😢😻👽💩👍👎🙏👌👋👄👁👦👼👨‍🚀👨‍🚒🙋‍♂️👳👨‍👨‍👧"
  1777. "‍"
  1778. "👧"
  1779. "💼👡👠☂🐶🐰🐻🐼🐷🐒🐵🐔🐧🐦🐋🐟🐡🕸🐌🐴🐊🐄🐪🐘🌸🌏🔥🌟🌚🌝"
  1780. "💦"
  1781. "💧"
  1782. "❄🍕🍔🍟🥝🍱🕶🎩🏈⚽🚴‍♀️🎻🎼🎹🚨🚎🚐⚓🛳🚀🚁🏪🏢🖱⏰📱💾💉📉🛏"
  1783. "🔑"
  1784. "🔓"
  1785. "📁🗓📊❤💯🚫🔻♠♣🕓❗🏳🏁🏳️‍🌈🇮🇹🇱🇷🇺🇸🇬🇧🇨🇳"
  1786. "🇧"
  1787. "🇴";
  1788. ParagraphStyle paragraph_style;
  1789. paragraph_style.turnHintingOff();
  1790. ParagraphBuilderImpl builder(paragraph_style, fontCollection);
  1791. TextStyle text_style;
  1792. text_style.setFontFamilies({SkString("Noto Color Emoji")});
  1793. text_style.setFontSize(50);
  1794. text_style.setDecoration(TextDecoration::kUnderline);
  1795. text_style.setColor(SK_ColorBLACK);
  1796. builder.pushStyle(text_style);
  1797. builder.addText(text);
  1798. builder.pop();
  1799. auto paragraph = builder.Build();
  1800. paragraph->layout(TestCanvasWidth);
  1801. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  1802. REPORTER_ASSERT(reporter, impl->lines().size() == 8);
  1803. for (auto& line : impl->lines()) {
  1804. if (&line != impl->lines().end() - 1) {
  1805. REPORTER_ASSERT(reporter, line.width() == 998.25f);
  1806. } else {
  1807. REPORTER_ASSERT(reporter, line.width() < 998.25f);
  1808. }
  1809. REPORTER_ASSERT(reporter, line.height() == 59);
  1810. }
  1811. }
  1812. DEF_TEST(SkParagraph_RepeatLayoutParagraph, reporter) {
  1813. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  1814. if (!fontCollection->fontsFound()) return;
  1815. const char* text =
  1816. "Sentence to layout at diff widths to get diff line counts. short words "
  1817. "short words short words short words short words short words short words "
  1818. "short words short words short words short words short words short words "
  1819. "end";
  1820. ParagraphStyle paragraph_style;
  1821. paragraph_style.turnHintingOff();
  1822. ParagraphBuilderImpl builder(paragraph_style, fontCollection);
  1823. TextStyle text_style;
  1824. text_style.setFontFamilies({SkString("Roboto")});
  1825. text_style.setFontSize(31);
  1826. text_style.setLetterSpacing(0);
  1827. text_style.setWordSpacing(0);
  1828. text_style.setColor(SK_ColorBLACK);
  1829. text_style.setHeight(1);
  1830. text_style.setDecoration(TextDecoration::kUnderline);
  1831. text_style.setDecorationColor(SK_ColorBLACK);
  1832. builder.pushStyle(text_style);
  1833. builder.addText(text);
  1834. builder.pop();
  1835. auto paragraph = builder.Build();
  1836. paragraph->layout(300);
  1837. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  1838. // Some of the formatting lazily done on paint
  1839. REPORTER_ASSERT(reporter, impl->runs().size() == 1);
  1840. REPORTER_ASSERT(reporter, impl->styles().size() == 1);
  1841. REPORTER_ASSERT(reporter, impl->lines().size() == 12);
  1842. paragraph->layout(600);
  1843. REPORTER_ASSERT(reporter, impl->runs().size() == 1);
  1844. REPORTER_ASSERT(reporter, impl->styles().size() == 1);
  1845. REPORTER_ASSERT(reporter, impl->lines().size() == 6);
  1846. }
  1847. DEF_TEST(SkParagraph_Ellipsize, reporter) {
  1848. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  1849. if (!fontCollection->fontsFound()) return;
  1850. const char* text =
  1851. "This is a very long sentence to test if the text will properly wrap "
  1852. "around and go to the next line. Sometimes, short sentence. Longer "
  1853. "sentences are okay too because they are nessecary. Very short. ";
  1854. ParagraphStyle paragraph_style;
  1855. paragraph_style.setMaxLines(1);
  1856. paragraph_style.setEllipsis(u"\u2026");
  1857. paragraph_style.turnHintingOff();
  1858. ParagraphBuilderImpl builder(paragraph_style, fontCollection);
  1859. TextStyle text_style;
  1860. text_style.setFontFamilies({SkString("Roboto")});
  1861. text_style.setDecorationColor(SK_ColorBLACK);
  1862. builder.pushStyle(text_style);
  1863. builder.addText(text);
  1864. builder.pop();
  1865. auto paragraph = builder.Build();
  1866. paragraph->layout(TestCanvasWidth);
  1867. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  1868. // Check that the ellipsizer limited the text to one line and did not wrap to a second line.
  1869. REPORTER_ASSERT(reporter, impl->lines().size() == 1);
  1870. auto& line = impl->lines()[0];
  1871. REPORTER_ASSERT(reporter, line.ellipsis() != nullptr);
  1872. size_t index = 0;
  1873. line.scanRuns([&index, &line, reporter](Run* run, int32_t, size_t, SkRect, SkScalar, bool) {
  1874. ++index;
  1875. if (index == 2) {
  1876. REPORTER_ASSERT(reporter, run->textRange() == line.ellipsis()->textRange());
  1877. }
  1878. return true;
  1879. });
  1880. REPORTER_ASSERT(reporter, index == 2);
  1881. }
  1882. DEF_TEST(SkParagraph_UnderlineShiftParagraph, reporter) {
  1883. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  1884. if (!fontCollection->fontsFound()) return;
  1885. const char* text1 = "fluttser ";
  1886. const char* text2 = "mdje";
  1887. const char* text3 = "fluttser mdje";
  1888. ParagraphStyle paragraph_style;
  1889. paragraph_style.turnHintingOff();
  1890. paragraph_style.setTextAlign(TextAlign::kLeft);
  1891. paragraph_style.setMaxLines(2);
  1892. ParagraphBuilderImpl builder(paragraph_style, fontCollection);
  1893. TextStyle text_style;
  1894. text_style.setFontFamilies({SkString("Roboto")});
  1895. text_style.setColor(SK_ColorBLACK);
  1896. builder.pushStyle(text_style);
  1897. builder.addText(text1);
  1898. text_style.setDecoration(TextDecoration::kUnderline);
  1899. text_style.setDecorationColor(SK_ColorBLACK);
  1900. builder.pushStyle(text_style);
  1901. builder.addText(text2);
  1902. builder.pop();
  1903. auto paragraph = builder.Build();
  1904. paragraph->layout(TestCanvasWidth);
  1905. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  1906. ParagraphBuilderImpl builder1(paragraph_style, fontCollection);
  1907. text_style.setDecoration(TextDecoration::kNoDecoration);
  1908. builder1.pushStyle(text_style);
  1909. builder1.addText(text3);
  1910. builder1.pop();
  1911. auto paragraph1 = builder1.Build();
  1912. paragraph1->layout(TestCanvasWidth);
  1913. auto impl1 = static_cast<ParagraphImpl*>(paragraph1.get());
  1914. REPORTER_ASSERT(reporter, impl->lines().size() == 1);
  1915. REPORTER_ASSERT(reporter, impl1->lines().size() == 1);
  1916. {
  1917. auto& line = impl->lines()[0];
  1918. size_t index = 0;
  1919. line.scanStyles(
  1920. StyleType::kDecorations,
  1921. [&index, reporter](TextRange text, TextStyle style, SkScalar) {
  1922. switch (index) {
  1923. case 0:
  1924. REPORTER_ASSERT(reporter,
  1925. style.getDecorationType() == TextDecoration::kNoDecoration);
  1926. break;
  1927. case 1:
  1928. REPORTER_ASSERT(reporter,
  1929. style.getDecorationType() == TextDecoration::kUnderline);
  1930. break;
  1931. default:
  1932. REPORTER_ASSERT(reporter, false);
  1933. break;
  1934. }
  1935. ++index;
  1936. return true;
  1937. });
  1938. REPORTER_ASSERT(reporter, index == 2);
  1939. }
  1940. {
  1941. auto& line = impl1->lines()[0];
  1942. size_t index = 0;
  1943. line.scanStyles(StyleType::kDecorations,
  1944. [&index, reporter](TextRange text, TextStyle style, SkScalar) {
  1945. if (index == 0) {
  1946. REPORTER_ASSERT(reporter, style.getDecorationType() ==
  1947. TextDecoration::kNoDecoration);
  1948. } else {
  1949. REPORTER_ASSERT(reporter, false);
  1950. }
  1951. ++index;
  1952. return true;
  1953. });
  1954. REPORTER_ASSERT(reporter, index == 1);
  1955. }
  1956. auto rect = paragraph->getRectsForRange(0, 12, RectHeightStyle::kMax, RectWidthStyle::kTight)
  1957. .front()
  1958. .rect;
  1959. auto rect1 = paragraph1->getRectsForRange(0, 12, RectHeightStyle::kMax, RectWidthStyle::kTight)
  1960. .front()
  1961. .rect;
  1962. REPORTER_ASSERT(reporter, rect.fLeft == rect1.fLeft);
  1963. REPORTER_ASSERT(reporter, rect.fRight == rect1.fRight);
  1964. for (size_t i = 0; i < 12; ++i) {
  1965. auto r =
  1966. paragraph->getRectsForRange(i, i + 1, RectHeightStyle::kMax, RectWidthStyle::kTight)
  1967. .front()
  1968. .rect;
  1969. auto r1 =
  1970. paragraph1
  1971. ->getRectsForRange(i, i + 1, RectHeightStyle::kMax, RectWidthStyle::kTight)
  1972. .front()
  1973. .rect;
  1974. REPORTER_ASSERT(reporter, r.fLeft == r1.fLeft);
  1975. REPORTER_ASSERT(reporter, r.fRight == r1.fRight);
  1976. }
  1977. }
  1978. DEF_TEST(SkParagraph_SimpleShadow, reporter) {
  1979. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  1980. if (!fontCollection->fontsFound()) return;
  1981. const char* text = "Hello World Text Dialog";
  1982. ParagraphStyle paragraph_style;
  1983. paragraph_style.turnHintingOff();
  1984. ParagraphBuilderImpl builder(paragraph_style, fontCollection);
  1985. TextStyle text_style;
  1986. text_style.setFontFamilies({SkString("Roboto")});
  1987. text_style.setColor(SK_ColorBLACK);
  1988. text_style.addShadow(TextShadow(SK_ColorBLACK, SkPoint::Make(2.0f, 2.0f), 1.0));
  1989. builder.pushStyle(text_style);
  1990. builder.addText(text);
  1991. auto paragraph = builder.Build();
  1992. paragraph->layout(TestCanvasWidth);
  1993. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  1994. REPORTER_ASSERT(reporter, impl->runs().size() == 1);
  1995. REPORTER_ASSERT(reporter, impl->styles().size() == 1);
  1996. size_t index = 0;
  1997. for (auto& line : impl->lines()) {
  1998. line.scanStyles(
  1999. StyleType::kShadow,
  2000. [&index, text_style, reporter](TextRange text, TextStyle style, SkScalar) {
  2001. REPORTER_ASSERT(reporter, index == 0 && style.equals(text_style));
  2002. ++index;
  2003. return true;
  2004. });
  2005. }
  2006. }
  2007. DEF_TEST(SkParagraph_ComplexShadow, reporter) {
  2008. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  2009. if (!fontCollection->fontsFound()) return;
  2010. const char* text = "Text Chunk ";
  2011. ParagraphStyle paragraph_style;
  2012. paragraph_style.turnHintingOff();
  2013. ParagraphBuilderImpl builder(paragraph_style, fontCollection);
  2014. TextStyle text_style;
  2015. text_style.setFontFamilies({SkString("Roboto")});
  2016. text_style.setColor(SK_ColorBLACK);
  2017. text_style.addShadow(TextShadow(SK_ColorBLACK, SkPoint::Make(2.0f, 2.0f), 1.0f));
  2018. builder.pushStyle(text_style);
  2019. builder.addText(text);
  2020. text_style.addShadow(TextShadow(SK_ColorRED, SkPoint::Make(2.0f, 2.0f), 5.0f));
  2021. text_style.addShadow(TextShadow(SK_ColorGREEN, SkPoint::Make(10.0f, -5.0f), 3.0f));
  2022. builder.pushStyle(text_style);
  2023. builder.addText(text);
  2024. builder.pop();
  2025. builder.addText(text);
  2026. text_style.addShadow(TextShadow(SK_ColorRED, SkPoint::Make(0.0f, 1.0f), 0.0f));
  2027. builder.pushStyle(text_style);
  2028. builder.addText(text);
  2029. builder.pop();
  2030. builder.addText(text);
  2031. auto paragraph = builder.Build();
  2032. paragraph->layout(TestCanvasWidth);
  2033. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  2034. size_t index = 0;
  2035. for (auto& line : impl->lines()) {
  2036. line.scanStyles(
  2037. StyleType::kShadow,
  2038. [&index, text_style, reporter](TextRange text, TextStyle style, SkScalar) {
  2039. ++index;
  2040. switch (index) {
  2041. case 1:
  2042. REPORTER_ASSERT(reporter, style.getShadowNumber() == 1);
  2043. break;
  2044. case 2:
  2045. REPORTER_ASSERT(reporter, style.getShadowNumber() == 3);
  2046. break;
  2047. case 3:
  2048. REPORTER_ASSERT(reporter, style.getShadowNumber() == 1);
  2049. break;
  2050. case 4:
  2051. REPORTER_ASSERT(reporter, style.getShadowNumber() == 4);
  2052. REPORTER_ASSERT(reporter, style.equals(text_style));
  2053. break;
  2054. case 5:
  2055. REPORTER_ASSERT(reporter, style.getShadowNumber() == 1);
  2056. break;
  2057. default:
  2058. REPORTER_ASSERT(reporter, false);
  2059. }
  2060. return true;
  2061. });
  2062. }
  2063. }
  2064. DEF_TEST(SkParagraph_BaselineParagraph, reporter) {
  2065. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  2066. if (!fontCollection->fontsFound()) return;
  2067. const char* text =
  2068. "左線読設Byg後碁給能上目秘使約。満毎冠行来昼本可必図将発確年。今属場育"
  2069. "図情闘陰野高備込制詩西校客。審対江置講今固残必託地集済決維駆年策。立得";
  2070. ParagraphStyle paragraph_style;
  2071. paragraph_style.turnHintingOff();
  2072. paragraph_style.setMaxLines(14);
  2073. paragraph_style.setTextAlign(TextAlign::kJustify);
  2074. paragraph_style.setHeight(1.5);
  2075. ParagraphBuilderImpl builder(paragraph_style, fontCollection);
  2076. TextStyle text_style;
  2077. text_style.setFontFamilies({SkString("Source Han Serif CN")});
  2078. text_style.setColor(SK_ColorBLACK);
  2079. text_style.setFontSize(55);
  2080. text_style.setLetterSpacing(2);
  2081. text_style.setDecorationStyle(TextDecorationStyle::kSolid);
  2082. text_style.setDecorationColor(SK_ColorBLACK);
  2083. builder.pushStyle(text_style);
  2084. builder.addText(text);
  2085. builder.pop();
  2086. auto paragraph = builder.Build();
  2087. paragraph->layout(TestCanvasWidth - 100);
  2088. SkScalar epsilon = 0.01f;
  2089. REPORTER_ASSERT(reporter,
  2090. SkScalarNearlyEqual(paragraph->getIdeographicBaseline(), 79.035f, epsilon));
  2091. REPORTER_ASSERT(reporter,
  2092. SkScalarNearlyEqual(paragraph->getAlphabeticBaseline(), 63.305f, epsilon));
  2093. }
  2094. DEF_TEST(SkParagraph_FontFallbackParagraph, reporter) {
  2095. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  2096. if (!fontCollection->fontsFound()) return;
  2097. const char* text1 = "Roboto 字典 ";
  2098. const char* text2 = "Homemade Apple 字典";
  2099. const char* text3 = "Chinese 字典";
  2100. ParagraphStyle paragraph_style;
  2101. paragraph_style.turnHintingOff();
  2102. ParagraphBuilderImpl builder(paragraph_style, fontCollection);
  2103. TextStyle text_style;
  2104. text_style.setFontFamilies({
  2105. SkString("Not a real font"),
  2106. SkString("Also a fake font"),
  2107. SkString("So fake it is obvious"),
  2108. SkString("Next one should be a real font..."),
  2109. SkString("Roboto"),
  2110. SkString("another fake one in between"),
  2111. SkString("Homemade Apple"),
  2112. });
  2113. text_style.setColor(SK_ColorBLACK);
  2114. builder.pushStyle(text_style);
  2115. builder.addText(text1);
  2116. text_style.setFontFamilies({
  2117. SkString("Not a real font"),
  2118. SkString("Also a fake font"),
  2119. SkString("So fake it is obvious"),
  2120. SkString("Homemade Apple"),
  2121. SkString("Next one should be a real font..."),
  2122. SkString("Roboto"),
  2123. SkString("another fake one in between"),
  2124. SkString("Noto Sans CJK JP"),
  2125. SkString("Source Han Serif CN"),
  2126. });
  2127. builder.pushStyle(text_style);
  2128. builder.addText(text2);
  2129. text_style.setFontFamilies({
  2130. SkString("Not a real font"),
  2131. SkString("Also a fake font"),
  2132. SkString("So fake it is obvious"),
  2133. SkString("Homemade Apple"),
  2134. SkString("Next one should be a real font..."),
  2135. SkString("Roboto"),
  2136. SkString("another fake one in between"),
  2137. SkString("Source Han Serif CN"),
  2138. SkString("Noto Sans CJK JP"),
  2139. });
  2140. builder.pushStyle(text_style);
  2141. builder.addText(text3);
  2142. builder.pop();
  2143. auto paragraph = builder.Build();
  2144. paragraph->layout(TestCanvasWidth);
  2145. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  2146. // Font resolution in Skia produces 6 runs because 2 parts of "Roboto 字典 " have different
  2147. // script (Minikin merges the first 2 into one because of unresolved) [Apple + Unresolved ]
  2148. // [Apple + Noto] [Apple + Han]
  2149. REPORTER_ASSERT(reporter, impl->runs().size() == 6);
  2150. SkScalar epsilon = 0.01f;
  2151. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->runs()[0].advance().fX, 48.46f, epsilon));
  2152. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->runs()[1].advance().fX, 15.90f, epsilon));
  2153. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->runs()[2].advance().fX, 139.12f, epsilon));
  2154. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->runs()[3].advance().fX, 27.99f, epsilon));
  2155. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->runs()[4].advance().fX, 62.24f, epsilon));
  2156. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->runs()[5].advance().fX, 27.99f, epsilon));
  2157. // When a different font is resolved, then the metrics are different.
  2158. REPORTER_ASSERT(reporter, impl->runs()[1].ascent() != impl->runs()[3].ascent());
  2159. REPORTER_ASSERT(reporter, impl->runs()[1].descent() != impl->runs()[3].descent());
  2160. REPORTER_ASSERT(reporter, impl->runs()[3].ascent() != impl->runs()[5].ascent());
  2161. REPORTER_ASSERT(reporter, impl->runs()[3].descent() != impl->runs()[5].descent());
  2162. REPORTER_ASSERT(reporter, impl->runs()[1].ascent() != impl->runs()[5].ascent());
  2163. REPORTER_ASSERT(reporter, impl->runs()[1].descent() != impl->runs()[5].descent());
  2164. }
  2165. DEF_TEST(SkParagraph_StrutParagraph1, reporter) {
  2166. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  2167. if (!fontCollection->fontsFound()) return;
  2168. // The chinese extra height should be absorbed by the strut.
  2169. // const char* text = "01234満毎冠p来É本可\nabcd\n満毎É行p昼本可";
  2170. const char* text = "01234満毎冠行来昼本可\nabcd\n満毎冠行来昼本可";
  2171. ParagraphStyle paragraph_style;
  2172. paragraph_style.setMaxLines(10);
  2173. paragraph_style.setTextAlign(TextAlign::kLeft);
  2174. paragraph_style.turnHintingOff();
  2175. StrutStyle strut_style;
  2176. strut_style.setStrutEnabled(true);
  2177. strut_style.setFontFamilies({ SkString("BlahFake"), SkString("Ahem") });
  2178. strut_style.setFontSize(50);
  2179. strut_style.setHeight(1.8f);
  2180. strut_style.setLeading(0.1f);
  2181. strut_style.setForceStrutHeight(true);
  2182. paragraph_style.setStrutStyle(strut_style);
  2183. ParagraphBuilderImpl builder(paragraph_style, fontCollection);
  2184. TextStyle text_style;
  2185. text_style.setFontFamilies({SkString("Ahem")});
  2186. text_style.setFontSize(50);
  2187. // text_style.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
  2188. // SkFontStyle::kUpright_Slant));
  2189. text_style.setColor(SK_ColorBLACK);
  2190. text_style.setHeight(0.5f);
  2191. builder.pushStyle(text_style);
  2192. builder.addText(text);
  2193. builder.pop();
  2194. auto paragraph = builder.Build();
  2195. paragraph->layout(550);
  2196. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  2197. // Font is not resolved and the first line does not fit
  2198. REPORTER_ASSERT(reporter, impl->lines().size() == 4);
  2199. RectHeightStyle rect_height_style = RectHeightStyle::kTight;
  2200. RectHeightStyle rect_height_max_style = RectHeightStyle::kMax;
  2201. RectWidthStyle rect_width_style = RectWidthStyle::kTight;
  2202. SkScalar epsilon = 0.001f;
  2203. {
  2204. auto boxes = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style);
  2205. REPORTER_ASSERT(reporter, boxes.empty());
  2206. }
  2207. {
  2208. auto boxes = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style);
  2209. REPORTER_ASSERT(reporter, boxes.size() == 1);
  2210. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, epsilon));
  2211. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 34.5f, epsilon));
  2212. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, epsilon));
  2213. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 84.5f, epsilon));
  2214. }
  2215. {
  2216. auto boxes = paragraph->getRectsForRange(0, 1, rect_height_max_style, rect_width_style);
  2217. REPORTER_ASSERT(reporter, boxes.size() == 1);
  2218. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, epsilon));
  2219. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, epsilon));
  2220. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, epsilon));
  2221. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 95, epsilon));
  2222. }
  2223. {
  2224. auto boxes = paragraph->getRectsForRange(6, 10, rect_height_style, rect_width_style);
  2225. REPORTER_ASSERT(reporter, boxes.size() == 1);
  2226. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, epsilon));
  2227. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 34.5f, epsilon));
  2228. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, epsilon));
  2229. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 84.5f, epsilon));
  2230. }
  2231. {
  2232. auto boxes = paragraph->getRectsForRange(6, 10, rect_height_max_style, rect_width_style);
  2233. REPORTER_ASSERT(reporter, boxes.size() == 1);
  2234. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, epsilon));
  2235. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, epsilon));
  2236. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, epsilon));
  2237. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 95, epsilon));
  2238. }
  2239. {
  2240. auto boxes = paragraph->getRectsForRange(14, 16, rect_height_max_style, rect_width_style);
  2241. REPORTER_ASSERT(reporter, boxes.size() == 1);
  2242. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, epsilon));
  2243. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 190, epsilon));
  2244. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 100, epsilon));
  2245. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 285, epsilon));
  2246. }
  2247. {
  2248. auto boxes = paragraph->getRectsForRange(20, 25, rect_height_max_style, rect_width_style);
  2249. REPORTER_ASSERT(reporter, boxes.size() == 1);
  2250. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 50, epsilon));
  2251. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 285, epsilon));
  2252. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 300, epsilon));
  2253. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 380, epsilon));
  2254. }
  2255. }
  2256. DEF_TEST(SkParagraph_StrutParagraph2, reporter) {
  2257. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  2258. if (!fontCollection->fontsFound()) return;
  2259. // The chinese extra height should be absorbed by the strut.
  2260. const char* text = "01234ABCDEFGH\nabcd\nABCDEFGH";
  2261. ParagraphStyle paragraph_style;
  2262. paragraph_style.setMaxLines(10);
  2263. paragraph_style.setTextAlign(TextAlign::kLeft);
  2264. paragraph_style.turnHintingOff();
  2265. StrutStyle strut_style;
  2266. strut_style.setStrutEnabled(true);
  2267. strut_style.setFontFamilies({ SkString("Ahem") });
  2268. strut_style.setFontSize(50);
  2269. strut_style.setHeight(1.6f);
  2270. paragraph_style.setStrutStyle(strut_style);
  2271. ParagraphBuilderImpl builder(paragraph_style, fontCollection);
  2272. TextStyle text_style;
  2273. text_style.setFontFamilies({SkString("Ahem")});
  2274. text_style.setFontSize(50);
  2275. // text_style.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
  2276. // SkFontStyle::kUpright_Slant));
  2277. text_style.setColor(SK_ColorBLACK);
  2278. text_style.setHeight(1);
  2279. builder.pushStyle(text_style);
  2280. builder.addText(text);
  2281. builder.pop();
  2282. auto paragraph = builder.Build();
  2283. paragraph->layout(550);
  2284. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  2285. // Font is not resolved and the first line does not fit
  2286. REPORTER_ASSERT(reporter, impl->lines().size() == 4);
  2287. RectHeightStyle rect_height_style = RectHeightStyle::kTight;
  2288. RectHeightStyle rect_height_max_style = RectHeightStyle::kMax;
  2289. RectWidthStyle rect_width_style = RectWidthStyle::kTight;
  2290. SkScalar epsilon = 0.001f;
  2291. {
  2292. auto boxes = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style);
  2293. REPORTER_ASSERT(reporter, boxes.empty());
  2294. }
  2295. {
  2296. auto boxes = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style);
  2297. REPORTER_ASSERT(reporter, boxes.size() == 1);
  2298. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, epsilon));
  2299. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 24, epsilon));
  2300. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, epsilon));
  2301. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 74, epsilon));
  2302. }
  2303. {
  2304. auto boxes = paragraph->getRectsForRange(0, 1, rect_height_max_style, rect_width_style);
  2305. REPORTER_ASSERT(reporter, boxes.size() == 1);
  2306. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, epsilon));
  2307. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, epsilon));
  2308. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, epsilon));
  2309. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 80, epsilon));
  2310. }
  2311. {
  2312. auto boxes = paragraph->getRectsForRange(6, 10, rect_height_style, rect_width_style);
  2313. REPORTER_ASSERT(reporter, boxes.size() == 1);
  2314. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, epsilon));
  2315. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 24, epsilon));
  2316. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, epsilon));
  2317. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 74, epsilon));
  2318. }
  2319. {
  2320. auto boxes = paragraph->getRectsForRange(6, 10, rect_height_max_style, rect_width_style);
  2321. REPORTER_ASSERT(reporter, boxes.size() == 1);
  2322. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, epsilon));
  2323. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, epsilon));
  2324. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, epsilon));
  2325. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 80, epsilon));
  2326. }
  2327. {
  2328. auto boxes = paragraph->getRectsForRange(14, 16, rect_height_max_style, rect_width_style);
  2329. REPORTER_ASSERT(reporter, boxes.size() == 1);
  2330. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, epsilon));
  2331. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 160, epsilon));
  2332. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 100, epsilon));
  2333. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 240, epsilon));
  2334. }
  2335. {
  2336. auto boxes = paragraph->getRectsForRange(20, 25, rect_height_max_style, rect_width_style);
  2337. REPORTER_ASSERT(reporter, boxes.size() == 1);
  2338. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 50, epsilon));
  2339. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 240, epsilon));
  2340. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 300, epsilon));
  2341. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 320, epsilon));
  2342. }
  2343. }
  2344. DEF_TEST(SkParagraph_StrutParagraph3, reporter) {
  2345. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  2346. if (!fontCollection->fontsFound()) return;
  2347. // The chinese extra height should be absorbed by the strut.
  2348. // const char* text = "01234満毎p行来昼本可\nabcd\n満毎冠行来昼本可";
  2349. const char* text = "01234満毎冠行来昼本可\nabcd\n満毎冠行来昼本可";
  2350. ParagraphStyle paragraph_style;
  2351. paragraph_style.setMaxLines(10);
  2352. paragraph_style.setTextAlign(TextAlign::kLeft);
  2353. paragraph_style.turnHintingOff();
  2354. StrutStyle strut_style;
  2355. strut_style.setStrutEnabled(true);
  2356. strut_style.setFontFamilies({ SkString("Ahem") });
  2357. strut_style.setFontSize(50);
  2358. strut_style.setHeight(1.2f);
  2359. paragraph_style.setStrutStyle(strut_style);
  2360. ParagraphBuilderImpl builder(paragraph_style, fontCollection);
  2361. TextStyle text_style;
  2362. text_style.setFontFamilies({SkString("Ahem")});
  2363. text_style.setFontSize(50);
  2364. // text_style.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
  2365. // SkFontStyle::kUpright_Slant));
  2366. text_style.setColor(SK_ColorBLACK);
  2367. text_style.setHeight(1);
  2368. builder.pushStyle(text_style);
  2369. builder.addText(text);
  2370. builder.pop();
  2371. auto paragraph = builder.Build();
  2372. paragraph->layout(550);
  2373. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  2374. // Font is not resolved and the first line does not fit
  2375. REPORTER_ASSERT(reporter, impl->lines().size() == 4);
  2376. RectHeightStyle rect_height_style = RectHeightStyle::kTight;
  2377. RectHeightStyle rect_height_max_style = RectHeightStyle::kMax;
  2378. RectWidthStyle rect_width_style = RectWidthStyle::kTight;
  2379. SkScalar epsilon = 0.001f;
  2380. {
  2381. auto boxes = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style);
  2382. REPORTER_ASSERT(reporter, boxes.empty());
  2383. }
  2384. {
  2385. auto boxes = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style);
  2386. REPORTER_ASSERT(reporter, boxes.size() == 1);
  2387. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, epsilon));
  2388. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 8, epsilon));
  2389. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, epsilon));
  2390. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 58, epsilon));
  2391. }
  2392. {
  2393. auto boxes = paragraph->getRectsForRange(0, 1, rect_height_max_style, rect_width_style);
  2394. REPORTER_ASSERT(reporter, boxes.size() == 1);
  2395. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, epsilon));
  2396. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, epsilon));
  2397. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, epsilon));
  2398. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 60, epsilon));
  2399. }
  2400. {
  2401. auto boxes = paragraph->getRectsForRange(6, 10, rect_height_style, rect_width_style);
  2402. REPORTER_ASSERT(reporter, boxes.size() == 1);
  2403. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, epsilon));
  2404. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 8, epsilon));
  2405. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, epsilon));
  2406. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 58, epsilon));
  2407. }
  2408. {
  2409. auto boxes = paragraph->getRectsForRange(6, 10, rect_height_max_style, rect_width_style);
  2410. REPORTER_ASSERT(reporter, boxes.size() == 1);
  2411. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, epsilon));
  2412. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, epsilon));
  2413. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, epsilon));
  2414. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 60, epsilon));
  2415. }
  2416. {
  2417. auto boxes = paragraph->getRectsForRange(14, 16, rect_height_max_style, rect_width_style);
  2418. REPORTER_ASSERT(reporter, boxes.size() == 1);
  2419. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, epsilon));
  2420. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 120, epsilon));
  2421. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 100, epsilon));
  2422. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 180, epsilon));
  2423. }
  2424. {
  2425. auto boxes = paragraph->getRectsForRange(20, 25, rect_height_max_style, rect_width_style);
  2426. REPORTER_ASSERT(reporter, boxes.size() == 1);
  2427. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 50, epsilon));
  2428. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 180, epsilon));
  2429. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 300, epsilon));
  2430. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 240, epsilon));
  2431. }
  2432. }
  2433. DEF_TEST(SkParagraph_StrutForceParagraph, reporter) {
  2434. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  2435. if (!fontCollection->fontsFound()) return;
  2436. const char* text = "01234満毎冠行来昼本可\nabcd\n満毎冠行来昼本可";
  2437. ParagraphStyle paragraph_style;
  2438. paragraph_style.setMaxLines(10);
  2439. paragraph_style.setTextAlign(TextAlign::kLeft);
  2440. paragraph_style.turnHintingOff();
  2441. StrutStyle strut_style;
  2442. strut_style.setStrutEnabled(true);
  2443. strut_style.setFontFamilies({SkString("Ahem") });
  2444. strut_style.setFontSize(50);
  2445. strut_style.setHeight(1.5f);
  2446. strut_style.setLeading(0.1f);
  2447. strut_style.setForceStrutHeight(true);
  2448. paragraph_style.setStrutStyle(strut_style);
  2449. ParagraphBuilderImpl builder(paragraph_style, fontCollection);
  2450. TextStyle text_style;
  2451. text_style.setFontFamilies({SkString("Ahem")});
  2452. text_style.setFontSize(50);
  2453. text_style.setLetterSpacing(0);
  2454. text_style.setColor(SK_ColorBLACK);
  2455. text_style.setHeight(1);
  2456. builder.pushStyle(text_style);
  2457. builder.addText(text);
  2458. builder.pop();
  2459. auto paragraph = builder.Build();
  2460. paragraph->layout(550);
  2461. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  2462. // Font is not resolved and the first line does not fit
  2463. REPORTER_ASSERT(reporter, impl->lines().size() == 4);
  2464. RectHeightStyle rect_height_style = RectHeightStyle::kTight;
  2465. RectHeightStyle rect_height_max_style = RectHeightStyle::kMax;
  2466. RectWidthStyle rect_width_style = RectWidthStyle::kTight;
  2467. SkScalar epsilon = 0.001f;
  2468. auto boxes1 = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style);
  2469. REPORTER_ASSERT(reporter, boxes1.empty());
  2470. auto boxes2 = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style);
  2471. REPORTER_ASSERT(reporter, boxes2.size() == 1);
  2472. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes2[0].rect.left(), 0, epsilon));
  2473. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes2[0].rect.top(), 22.5f, epsilon));
  2474. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes2[0].rect.right(), 50, epsilon));
  2475. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes2[0].rect.bottom(), 72.5f, epsilon));
  2476. auto boxes3 = paragraph->getRectsForRange(0, 1, rect_height_max_style, rect_width_style);
  2477. REPORTER_ASSERT(reporter, boxes3.size() == 1);
  2478. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes3[0].rect.left(), 0, epsilon));
  2479. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes3[0].rect.top(), 0, epsilon));
  2480. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes3[0].rect.right(), 50, epsilon));
  2481. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes3[0].rect.bottom(), 80, epsilon));
  2482. auto boxes4 = paragraph->getRectsForRange(6, 10, rect_height_style, rect_width_style);
  2483. REPORTER_ASSERT(reporter, boxes4.size() == 1);
  2484. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes4[0].rect.left(), 300, epsilon));
  2485. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes4[0].rect.top(), 22.5f, epsilon));
  2486. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes4[0].rect.right(), 500, epsilon));
  2487. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes4[0].rect.bottom(), 72.5f, epsilon));
  2488. auto boxes5 = paragraph->getRectsForRange(6, 10, rect_height_max_style, rect_width_style);
  2489. REPORTER_ASSERT(reporter, boxes5.size() == 1);
  2490. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes5[0].rect.left(), 300, epsilon));
  2491. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes5[0].rect.top(), 0, epsilon));
  2492. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes5[0].rect.right(), 500, epsilon));
  2493. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes5[0].rect.bottom(), 80, epsilon));
  2494. auto boxes6 = paragraph->getRectsForRange(14, 16, rect_height_max_style, rect_width_style);
  2495. REPORTER_ASSERT(reporter, boxes6.size() == 1);
  2496. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes6[0].rect.left(), 0, epsilon));
  2497. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes6[0].rect.top(), 160, epsilon));
  2498. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes6[0].rect.right(), 100, epsilon));
  2499. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes6[0].rect.bottom(), 240, epsilon));
  2500. auto boxes7 = paragraph->getRectsForRange(20, 25, rect_height_max_style, rect_width_style);
  2501. REPORTER_ASSERT(reporter, boxes7.size() == 1);
  2502. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes7[0].rect.left(), 50, epsilon));
  2503. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes7[0].rect.top(), 240, epsilon));
  2504. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes7[0].rect.right(), 300, epsilon));
  2505. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes7[0].rect.bottom(), 320, epsilon));
  2506. }
  2507. // Not in Minikin
  2508. DEF_TEST(SkParagraph_WhitespacesInMultipleFonts, reporter) {
  2509. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  2510. if (!fontCollection->fontsFound()) return;
  2511. const char* text = "English English 字典 字典 😀😃😄 😀😃😄";
  2512. ParagraphStyle paragraph_style;
  2513. paragraph_style.turnHintingOff();
  2514. ParagraphBuilderImpl builder(paragraph_style, fontCollection);
  2515. TextStyle text_style;
  2516. text_style.setFontFamilies(
  2517. {SkString("Roboto"), SkString("Noto Color Emoji"), SkString("Source Han Serif CN")});
  2518. text_style.setFontSize(60);
  2519. builder.pushStyle(text_style);
  2520. builder.addText(text);
  2521. builder.pop();
  2522. auto paragraph = builder.Build();
  2523. paragraph->layout(TestCanvasWidth);
  2524. SkDEBUGCODE(auto impl = static_cast<ParagraphImpl*>(paragraph.get());)
  2525. SkASSERT(impl->runs().size() == 3);
  2526. SkASSERT(impl->runs()[0].textRange().end == impl->runs()[1].textRange().start);
  2527. SkASSERT(impl->runs()[1].textRange().end == impl->runs()[2].textRange().start);
  2528. }
  2529. // 4 to 1
  2530. DEF_TEST(SkParagraph_JSON1, reporter) {
  2531. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  2532. if (!fontCollection->fontsFound()) return;
  2533. const char* text = "👨‍👩‍👧‍👦";
  2534. ParagraphStyle paragraph_style;
  2535. paragraph_style.turnHintingOff();
  2536. ParagraphBuilderImpl builder(paragraph_style, fontCollection);
  2537. TextStyle text_style;
  2538. text_style.setFontFamilies({SkString("Noto Color Emoji")});
  2539. builder.pushStyle(text_style);
  2540. builder.addText(text);
  2541. builder.pop();
  2542. auto paragraph = builder.Build();
  2543. paragraph->layout(TestCanvasWidth);
  2544. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  2545. REPORTER_ASSERT(reporter, impl->runs().size() == 1);
  2546. auto run = impl->runs().front();
  2547. auto cluster = 0;
  2548. SkShaperJSONWriter::VisualizeClusters(
  2549. text, 0, std::strlen(text), run.glyphs(), run.clusterIndexes(),
  2550. [&](int codePointCount, SkSpan<const char> utf1to1, SkSpan<const SkGlyphID> glyph1to1) {
  2551. if (cluster == 0) {
  2552. std::string toCheckUtf8{utf1to1.data(), utf1to1.size()};
  2553. SkASSERT(std::strcmp(text, utf1to1.data()) == 0);
  2554. SkASSERT(glyph1to1.size() == 1);
  2555. SkASSERT(*glyph1to1.begin() == 1611);
  2556. }
  2557. ++cluster;
  2558. });
  2559. SkASSERT(cluster <= 2);
  2560. }
  2561. // 5 to 3
  2562. DEF_TEST(SkParagraph_JSON2, reporter) {
  2563. sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
  2564. if (!fontCollection->fontsFound()) return;
  2565. const char* text = "p〠q";
  2566. ParagraphStyle paragraph_style;
  2567. paragraph_style.turnHintingOff();
  2568. ParagraphBuilderImpl builder(paragraph_style, fontCollection);
  2569. TextStyle text_style;
  2570. text_style.setFontFamilies({SkString("Noto Sans CJK JP")});
  2571. text_style.setColor(SK_ColorBLACK);
  2572. text_style.setFontSize(50);
  2573. builder.pushStyle(text_style);
  2574. builder.addText(text);
  2575. builder.pop();
  2576. auto paragraph = builder.Build();
  2577. paragraph->layout(TestCanvasWidth);
  2578. auto impl = static_cast<ParagraphImpl*>(paragraph.get());
  2579. REPORTER_ASSERT(reporter, impl->runs().size() == 1);
  2580. auto run = impl->runs().front();
  2581. auto cluster = 0;
  2582. for (auto& run : impl->runs()) {
  2583. SkShaperJSONWriter::VisualizeClusters(
  2584. impl->text().begin() + run.textRange().start, 0, run.textRange().width(), run.glyphs(), run.clusterIndexes(),
  2585. [&](int codePointCount, SkSpan<const char> utf1to1,
  2586. SkSpan<const SkGlyphID> glyph1to1) {
  2587. if (cluster == 0) {
  2588. std::string toCheckUtf8{utf1to1.data(), utf1to1.size()};
  2589. SkASSERT(std::strcmp(text, utf1to1.data()) == 0);
  2590. SkASSERT(glyph1to1.size() == 3);
  2591. }
  2592. ++cluster;
  2593. });
  2594. }
  2595. SkASSERT(cluster <= 2);
  2596. }