123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946 |
- // Copyright 2019 Google LLC.
- #include <tools/ToolUtils.h>
- #include <sstream>
- #include "modules/skparagraph/include/TypefaceFontProvider.h"
- #include "modules/skparagraph/src/ParagraphBuilderImpl.h"
- #include "modules/skparagraph/src/ParagraphImpl.h"
- #include "src/core/SkOSFile.h"
- #include "src/utils/SkShaperJSONWriter.h"
- #include "tests/Test.h"
- #include "tools/Resources.h"
- #define VeryLongCanvasWidth 1000000
- #define TestCanvasWidth 1000
- #define TestCanvasHeight 600
- using namespace skia::textlayout;
- namespace {
- bool equal(const char* base, TextRange a, const char* b) {
- return std::strncmp(b, base + a.start, a.width()) == 0;
- }
- class TestFontCollection : public FontCollection {
- public:
- TestFontCollection()
- : fFontsFound(false)
- , fResolvedFonts(0)
- , fResourceDir(GetResourcePath("fonts").c_str())
- , fFontProvider(sk_make_sp<TypefaceFontProvider>()) {
- std::vector<SkString> fonts;
- SkOSFile::Iter iter(fResourceDir.c_str());
- SkString path;
- while (iter.next(&path)) {
- if (path.endsWith("Roboto-Italic.ttf")) {
- fFontsFound = true;
- }
- fonts.emplace_back(path);
- }
- if (!fFontsFound) {
- return;
- }
- // Only register fonts if we have to
- for (auto& font : fonts) {
- SkString file_path;
- file_path.printf("%s/%s", fResourceDir.c_str(), font.c_str());
- fFontProvider->registerTypeface(SkTypeface::MakeFromFile(file_path.c_str()));
- }
- this->setTestFontManager(std::move(fFontProvider));
- this->disableFontFallback();
- if (!fFontsFound) SkDebugf("Fonts not found, skipping all the tests\n");
- }
- ~TestFontCollection() = default;
- size_t resolvedFonts() const { return fResolvedFonts; }
- // TODO: temp solution until we check in fonts
- bool fontsFound() const { return fFontsFound; }
- private:
- bool fFontsFound;
- size_t fResolvedFonts;
- std::string fResourceDir;
- sk_sp<TypefaceFontProvider> fFontProvider;
- };
- } // namespace
- DEF_TEST(SkParagraph_SimpleParagraph, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text = "Hello World Text Dialog";
- ParagraphStyle paragraph_style;
- paragraph_style.turnHintingOff();
- ParagraphBuilderImpl builder(paragraph_style, fontCollection);
- TextStyle text_style;
- text_style.setFontFamilies({SkString("Roboto")});
- text_style.setColor(SK_ColorBLACK);
- builder.pushStyle(text_style);
- builder.addText(text);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(TestCanvasWidth);
- auto impl = static_cast<ParagraphImpl*>(paragraph.get());
- REPORTER_ASSERT(reporter, impl->runs().size() == 1);
- REPORTER_ASSERT(reporter, impl->styles().size() == 1); // paragraph style does not count
- REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style));
- size_t index = 0;
- for (auto& line : impl->lines()) {
- line.scanStyles(StyleType::kDecorations,
- [&index, reporter](TextRange text, TextStyle style, SkScalar) {
- REPORTER_ASSERT(reporter, index == 0);
- REPORTER_ASSERT(reporter, style.getColor() == SK_ColorBLACK);
- ++index;
- return true;
- });
- }
- }
- DEF_TEST(SkParagraph_SimpleRedParagraph, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text = "I am RED";
- ParagraphStyle paragraph_style;
- paragraph_style.turnHintingOff();
- ParagraphBuilderImpl builder(paragraph_style, fontCollection);
- TextStyle text_style;
- text_style.setFontFamilies({SkString("Roboto")});
- text_style.setColor(SK_ColorRED);
- builder.pushStyle(text_style);
- builder.addText(text);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(TestCanvasWidth);
- auto impl = static_cast<ParagraphImpl*>(paragraph.get());
- REPORTER_ASSERT(reporter, impl->runs().size() == 1);
- REPORTER_ASSERT(reporter, impl->styles().size() == 1); // paragraph style does not count
- REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style));
- size_t index = 0;
- for (auto& line : impl->lines()) {
- line.scanStyles(StyleType::kDecorations,
- [&index, reporter](TextRange text, TextStyle style, SkScalar) {
- REPORTER_ASSERT(reporter, index == 0);
- REPORTER_ASSERT(reporter, style.getColor() == SK_ColorRED);
- ++index;
- return true;
- });
- }
- }
- DEF_TEST(SkParagraph_RainbowParagraph, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text1 = "Red Roboto";
- const char* text2 = "big Greeen Default";
- const char* text3 = "Defcolor Homemade Apple";
- const char* text4 = "Small Blue Roboto";
- const char* text5 =
- "Continue Last Style With lots of words to check if it overlaps "
- "properly or not";
- const char* text45 =
- "Small Blue Roboto"
- "Continue Last Style With lots of words to check if it overlaps "
- "properly or not";
- ParagraphStyle paragraph_style;
- paragraph_style.turnHintingOff();
- paragraph_style.setTextAlign(TextAlign::kLeft);
- paragraph_style.setMaxLines(1);
- ParagraphBuilderImpl builder(paragraph_style, fontCollection);
- TextStyle text_style1;
- text_style1.setFontFamilies({SkString("Roboto")});
- text_style1.setColor(SK_ColorRED);
- builder.pushStyle(text_style1);
- builder.addText(text1);
- TextStyle text_style2;
- text_style2.setFontFamilies({SkString("Roboto")});
- text_style2.setFontSize(50);
- text_style2.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
- SkFontStyle::kUpright_Slant));
- text_style2.setLetterSpacing(10);
- text_style2.setDecorationColor(SK_ColorBLACK);
- text_style2.setDecoration((TextDecoration)(
- TextDecoration::kUnderline | TextDecoration::kOverline | TextDecoration::kLineThrough));
- text_style2.setWordSpacing(30);
- text_style2.setColor(SK_ColorGREEN);
- builder.pushStyle(text_style2);
- builder.addText(text2);
- TextStyle text_style3;
- text_style3.setFontFamilies({SkString("Homemade Apple")});
- builder.pushStyle(text_style3);
- builder.addText(text3);
- TextStyle text_style4;
- text_style4.setFontFamilies({SkString("Roboto")});
- text_style4.setFontSize(14);
- text_style4.setDecorationColor(SK_ColorBLACK);
- text_style4.setDecoration((TextDecoration)(
- TextDecoration::kUnderline | TextDecoration::kOverline | TextDecoration::kLineThrough));
- text_style4.setColor(SK_ColorBLUE);
- builder.pushStyle(text_style4);
- builder.addText(text4);
- builder.addText(text5);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(VeryLongCanvasWidth);
- auto impl = static_cast<ParagraphImpl*>(paragraph.get());
- REPORTER_ASSERT(reporter, impl->runs().size() == 4);
- REPORTER_ASSERT(reporter, impl->styles().size() == 4);
- REPORTER_ASSERT(reporter, impl->lines().size() == 1);
- size_t index = 0;
- impl->lines()[0].scanStyles(StyleType::kAllAttributes,
- [&](TextRange text, TextStyle style, SkScalar) {
- switch (index) {
- case 0:
- REPORTER_ASSERT(reporter, style.equals(text_style1));
- REPORTER_ASSERT(reporter, equal(impl->text().begin(), text, text1));
- break;
- case 1:
- REPORTER_ASSERT(reporter, style.equals(text_style2));
- REPORTER_ASSERT(reporter, equal(impl->text().begin(), text, text2));
- break;
- case 2:
- REPORTER_ASSERT(reporter, style.equals(text_style3));
- REPORTER_ASSERT(reporter, equal(impl->text().begin(), text, text3));
- break;
- case 3:
- REPORTER_ASSERT(reporter, style.equals(text_style4));
- REPORTER_ASSERT(reporter, equal(impl->text().begin(), text, text45));
- break;
- default:
- REPORTER_ASSERT(reporter, false);
- break;
- }
- ++index;
- return true;
- });
- REPORTER_ASSERT(reporter, index == 4);
- }
- DEF_TEST(SkParagraph_DefaultStyleParagraph, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text = "No TextStyle! Uh Oh!";
- ParagraphStyle paragraph_style;
- TextStyle defaultStyle;
- defaultStyle.setFontFamilies({SkString("Roboto")});
- paragraph_style.setTextStyle(defaultStyle);
- paragraph_style.turnHintingOff();
- ParagraphBuilderImpl builder(paragraph_style, fontCollection);
- builder.addText(text);
- auto paragraph = builder.Build();
- paragraph->layout(VeryLongCanvasWidth);
- auto impl = static_cast<ParagraphImpl*>(paragraph.get());
- REPORTER_ASSERT(reporter, impl->runs().size() == 1);
- REPORTER_ASSERT(reporter, impl->styles().size() == 1);
- REPORTER_ASSERT(reporter, impl->lines().size() == 1);
- size_t index = 0;
- impl->lines()[0].scanStyles(
- StyleType::kAllAttributes, [&](TextRange text1, TextStyle style, SkScalar) {
- REPORTER_ASSERT(reporter, style.equals(paragraph_style.getTextStyle()));
- REPORTER_ASSERT(reporter, equal(impl->text().begin(), text1, text));
- ++index;
- return true;
- });
- REPORTER_ASSERT(reporter, index == 1);
- }
- DEF_TEST(SkParagraph_BoldParagraph, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text = "This is Red max bold text!";
- ParagraphStyle paragraph_style;
- paragraph_style.turnHintingOff();
- ParagraphBuilderImpl builder(paragraph_style, fontCollection);
- TextStyle text_style;
- text_style.setFontFamilies({SkString("Roboto")});
- text_style.setColor(SK_ColorRED);
- text_style.setFontSize(60);
- text_style.setLetterSpacing(0);
- text_style.setFontStyle(SkFontStyle(SkFontStyle::kBlack_Weight, SkFontStyle::kNormal_Width,
- SkFontStyle::kUpright_Slant));
- builder.pushStyle(text_style);
- builder.addText(text);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(VeryLongCanvasWidth);
- auto impl = static_cast<ParagraphImpl*>(paragraph.get());
- REPORTER_ASSERT(reporter, impl->runs().size() == 1);
- REPORTER_ASSERT(reporter, impl->styles().size() == 1);
- REPORTER_ASSERT(reporter, impl->lines().size() == 1);
- size_t index = 0;
- impl->lines()[0].scanStyles(StyleType::kAllAttributes,
- [&](TextRange text1, TextStyle style, SkScalar) {
- REPORTER_ASSERT(reporter, style.equals(text_style));
- REPORTER_ASSERT(reporter, equal(impl->text().begin(), text1, text));
- ++index;
- return true;
- });
- REPORTER_ASSERT(reporter, index == 1);
- }
- DEF_TEST(SkParagraph_LeftAlignParagraph, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text =
- "This is a very long sentence to test if the text will properly wrap "
- "around and go to the next line. Sometimes, short sentence. Longer "
- "sentences are okay too because they are nessecary. Very short. "
- "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
- "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
- "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
- "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
- "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
- "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
- "mollit anim id est laborum. "
- "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
- "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
- "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
- "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
- "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
- "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
- "mollit anim id est laborum.";
- ParagraphStyle paragraph_style;
- paragraph_style.setMaxLines(14);
- paragraph_style.setTextAlign(TextAlign::kLeft);
- paragraph_style.turnHintingOff();
- ParagraphBuilderImpl builder(paragraph_style, fontCollection);
- TextStyle text_style;
- text_style.setFontFamilies({SkString("Roboto")});
- text_style.setFontSize(26);
- text_style.setLetterSpacing(1);
- text_style.setWordSpacing(5);
- text_style.setColor(SK_ColorBLACK);
- text_style.setHeight(1);
- text_style.setDecoration(TextDecoration::kUnderline);
- text_style.setDecorationColor(SK_ColorBLACK);
- builder.pushStyle(text_style);
- builder.addText(text);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(TestCanvasWidth - 100);
- auto impl = static_cast<ParagraphImpl*>(paragraph.get());
- REPORTER_ASSERT(reporter, impl->text().size() == std::string{text}.length());
- REPORTER_ASSERT(reporter, impl->runs().size() == 1);
- REPORTER_ASSERT(reporter, impl->styles().size() == 1);
- REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style));
- REPORTER_ASSERT(reporter, impl->lines().size() == paragraph_style.getMaxLines());
- // Apparently, Minikin records start from the base line (24)
- double expected_y = 0;
- double epsilon = 0.01f;
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].baseline(), 24.121f, epsilon));
- REPORTER_ASSERT(reporter,
- SkScalarNearlyEqual(impl->lines()[0].offset().fY, expected_y, epsilon));
- expected_y += 30;
- REPORTER_ASSERT(reporter,
- SkScalarNearlyEqual(impl->lines()[1].offset().fY, expected_y, epsilon));
- expected_y += 30;
- REPORTER_ASSERT(reporter,
- SkScalarNearlyEqual(impl->lines()[2].offset().fY, expected_y, epsilon));
- expected_y += 30;
- REPORTER_ASSERT(reporter,
- SkScalarNearlyEqual(impl->lines()[3].offset().fY, expected_y, epsilon));
- expected_y += 30 * 10;
- REPORTER_ASSERT(reporter,
- SkScalarNearlyEqual(impl->lines()[13].offset().fY, expected_y, epsilon));
- REPORTER_ASSERT(reporter,
- paragraph_style.getTextAlign() == impl->paragraphStyle().getTextAlign());
- // Tests for GetGlyphPositionAtCoordinate()
- REPORTER_ASSERT(reporter, impl->getGlyphPositionAtCoordinate(0, 0).position == 0);
- REPORTER_ASSERT(reporter, impl->getGlyphPositionAtCoordinate(1, 1).position == 0);
- REPORTER_ASSERT(reporter, impl->getGlyphPositionAtCoordinate(1, 35).position == 68);
- REPORTER_ASSERT(reporter, impl->getGlyphPositionAtCoordinate(1, 70).position == 134);
- REPORTER_ASSERT(reporter, impl->getGlyphPositionAtCoordinate(2000, 35).position == 134);
- }
- DEF_TEST(SkParagraph_RightAlignParagraph, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text =
- "This is a very long sentence to test if the text will properly wrap "
- "around and go to the next line. Sometimes, short sentence. Longer "
- "sentences are okay too because they are nessecary. Very short. "
- "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
- "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
- "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
- "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
- "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
- "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
- "mollit anim id est laborum. "
- "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
- "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
- "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
- "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
- "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
- "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
- "mollit anim id est laborum.";
- ParagraphStyle paragraph_style;
- paragraph_style.setMaxLines(14);
- paragraph_style.setTextAlign(TextAlign::kRight);
- paragraph_style.turnHintingOff();
- ParagraphBuilderImpl builder(paragraph_style, fontCollection);
- TextStyle text_style;
- text_style.setFontFamilies({SkString("Roboto")});
- text_style.setFontSize(26);
- text_style.setLetterSpacing(1);
- text_style.setWordSpacing(5);
- text_style.setColor(SK_ColorBLACK);
- text_style.setHeight(1);
- text_style.setDecoration(TextDecoration::kUnderline);
- text_style.setDecorationColor(SK_ColorBLACK);
- builder.pushStyle(text_style);
- builder.addText(text);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(TestCanvasWidth - 100);
- auto impl = static_cast<ParagraphImpl*>(paragraph.get());
- REPORTER_ASSERT(reporter, impl->runs().size() == 1);
- REPORTER_ASSERT(reporter, impl->styles().size() == 1);
- REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style));
- // Minikin has two records for each due to 'ghost' trailing whitespace run, SkParagraph - 1
- REPORTER_ASSERT(reporter, impl->lines().size() == paragraph_style.getMaxLines());
- // Apparently, Minikin records start from the base line (24)
- double expected_y = 0;
- double epsilon = 0.01f;
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].baseline(), 24.121f, epsilon));
- REPORTER_ASSERT(reporter,
- SkScalarNearlyEqual(impl->lines()[0].offset().fY, expected_y, epsilon));
- expected_y += 30;
- REPORTER_ASSERT(reporter,
- SkScalarNearlyEqual(impl->lines()[1].offset().fY, expected_y, epsilon));
- expected_y += 30;
- REPORTER_ASSERT(reporter,
- SkScalarNearlyEqual(impl->lines()[2].offset().fY, expected_y, epsilon));
- expected_y += 30;
- REPORTER_ASSERT(reporter,
- SkScalarNearlyEqual(impl->lines()[3].offset().fY, expected_y, epsilon));
- expected_y += 30 * 10;
- REPORTER_ASSERT(reporter,
- SkScalarNearlyEqual(impl->lines()[13].offset().fY, expected_y, epsilon));
- auto calculate = [](const TextLine& line) -> SkScalar {
- return TestCanvasWidth - 100 - line.offset().fX - line.width();
- };
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[0]), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[1]), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[2]), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[3]), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[13]), 0, epsilon));
- REPORTER_ASSERT(reporter,
- paragraph_style.getTextAlign() == impl->paragraphStyle().getTextAlign());
- }
- DEF_TEST(SkParagraph_CenterAlignParagraph, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text =
- "This is a very long sentence to test if the text will properly wrap "
- "around and go to the next line. Sometimes, short sentence. Longer "
- "sentences are okay too because they are nessecary. Very short. "
- "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
- "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
- "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
- "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
- "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
- "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
- "mollit anim id est laborum. "
- "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
- "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
- "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
- "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
- "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
- "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
- "mollit anim id est laborum.";
- ParagraphStyle paragraph_style;
- paragraph_style.setMaxLines(14);
- paragraph_style.setTextAlign(TextAlign::kCenter);
- paragraph_style.turnHintingOff();
- ParagraphBuilderImpl builder(paragraph_style, fontCollection);
- TextStyle text_style;
- text_style.setFontFamilies({SkString("Roboto")});
- text_style.setFontSize(26);
- text_style.setLetterSpacing(1);
- text_style.setWordSpacing(5);
- text_style.setColor(SK_ColorBLACK);
- text_style.setHeight(1);
- text_style.setDecoration(TextDecoration::kUnderline);
- text_style.setDecorationColor(SK_ColorBLACK);
- builder.pushStyle(text_style);
- builder.addText(text);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(TestCanvasWidth - 100);
- auto impl = static_cast<ParagraphImpl*>(paragraph.get());
- REPORTER_ASSERT(reporter, impl->text().size() == std::string{text}.length());
- REPORTER_ASSERT(reporter, impl->runs().size() == 1);
- REPORTER_ASSERT(reporter, impl->styles().size() == 1);
- REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style));
- // Minikin has two records for each due to 'ghost' trailing whitespace run, SkParagraph - 1
- REPORTER_ASSERT(reporter, impl->lines().size() == paragraph_style.getMaxLines());
- // Apparently, Minikin records start from the base line (24)
- double expected_y = 0;
- double epsilon = 0.01f;
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].baseline(), 24.121f, epsilon));
- REPORTER_ASSERT(reporter,
- SkScalarNearlyEqual(impl->lines()[0].offset().fY, expected_y, epsilon));
- expected_y += 30;
- REPORTER_ASSERT(reporter,
- SkScalarNearlyEqual(impl->lines()[1].offset().fY, expected_y, epsilon));
- expected_y += 30;
- REPORTER_ASSERT(reporter,
- SkScalarNearlyEqual(impl->lines()[2].offset().fY, expected_y, epsilon));
- expected_y += 30;
- REPORTER_ASSERT(reporter,
- SkScalarNearlyEqual(impl->lines()[3].offset().fY, expected_y, epsilon));
- expected_y += 30 * 10;
- REPORTER_ASSERT(reporter,
- SkScalarNearlyEqual(impl->lines()[13].offset().fY, expected_y, epsilon));
- auto calculate = [](const TextLine& line) -> SkScalar {
- return TestCanvasWidth - 100 - (line.offset().fX * 2 + line.width());
- };
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[0]), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[1]), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[2]), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[3]), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[13]), 0, epsilon));
- REPORTER_ASSERT(reporter,
- paragraph_style.getTextAlign() == impl->paragraphStyle().getTextAlign());
- }
- DEF_TEST(SkParagraph_JustifyAlignParagraph, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text =
- "This is a very long sentence to test if the text will properly wrap "
- "around and go to the next line. Sometimes, short sentence. Longer "
- "sentences are okay too because they are nessecary. Very short. "
- "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
- "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
- "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
- "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
- "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
- "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
- "mollit anim id est laborum. "
- "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
- "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
- "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
- "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
- "velit esse cillum dolore eu fugiat.";
- ParagraphStyle paragraph_style;
- paragraph_style.setMaxLines(14);
- paragraph_style.setTextAlign(TextAlign::kJustify);
- paragraph_style.turnHintingOff();
- ParagraphBuilderImpl builder(paragraph_style, fontCollection);
- TextStyle text_style;
- text_style.setFontFamilies({SkString("Roboto")});
- text_style.setFontSize(26);
- text_style.setLetterSpacing(0);
- text_style.setWordSpacing(5);
- text_style.setColor(SK_ColorBLACK);
- text_style.setHeight(1);
- text_style.setDecoration(TextDecoration::kUnderline);
- text_style.setDecorationColor(SK_ColorBLACK);
- builder.pushStyle(text_style);
- builder.addText(text);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(TestCanvasWidth - 100);
- auto impl = static_cast<ParagraphImpl*>(paragraph.get());
- REPORTER_ASSERT(reporter, impl->text().size() == std::string{text}.length());
- REPORTER_ASSERT(reporter, impl->runs().size() == 1);
- REPORTER_ASSERT(reporter, impl->styles().size() == 1);
- REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style));
- double expected_y = 0;
- double epsilon = 0.01f;
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].baseline(), 24.121f, epsilon));
- REPORTER_ASSERT(reporter,
- SkScalarNearlyEqual(impl->lines()[0].offset().fY, expected_y, epsilon));
- expected_y += 30;
- REPORTER_ASSERT(reporter,
- SkScalarNearlyEqual(impl->lines()[1].offset().fY, expected_y, epsilon));
- expected_y += 30;
- REPORTER_ASSERT(reporter,
- SkScalarNearlyEqual(impl->lines()[2].offset().fY, expected_y, epsilon));
- expected_y += 30;
- REPORTER_ASSERT(reporter,
- SkScalarNearlyEqual(impl->lines()[3].offset().fY, expected_y, epsilon));
- expected_y += 30 * 9;
- REPORTER_ASSERT(reporter,
- SkScalarNearlyEqual(impl->lines()[12].offset().fY, expected_y, epsilon));
- auto calculate = [](const TextLine& line) -> SkScalar {
- return TestCanvasWidth - 100 - (line.offset().fX + line.width());
- };
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[0]), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[1]), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[2]), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[3]), 0, epsilon));
- REPORTER_ASSERT(reporter, calculate(impl->lines()[12]) > 0);
- REPORTER_ASSERT(reporter,
- paragraph_style.getTextAlign() == impl->paragraphStyle().getTextAlign());
- }
- DEF_TEST(SkParagraph_JustifyRTL, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text =
- "אאא בּבּבּבּ אאאא בּבּ אאא בּבּבּ אאאאא בּבּבּבּ אאאא בּבּבּבּבּ "
- "אאאאא בּבּבּבּבּ אאאבּבּבּבּבּבּאאאאא בּבּבּבּבּבּאאאאאבּבּבּבּבּבּ אאאאא בּבּבּבּבּ "
- "אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ";
- ParagraphStyle paragraph_style;
- paragraph_style.setMaxLines(14);
- paragraph_style.setTextAlign(TextAlign::kJustify);
- paragraph_style.turnHintingOff();
- ParagraphBuilderImpl builder(paragraph_style, fontCollection);
- TextStyle text_style;
- text_style.setFontFamilies({SkString("Ahem")});
- text_style.setFontSize(26);
- text_style.setColor(SK_ColorBLACK);
- text_style.setHeight(1);
- text_style.setDecoration(TextDecoration::kUnderline);
- text_style.setDecorationColor(SK_ColorBLACK);
- builder.pushStyle(text_style);
- builder.addText(text);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(TestCanvasWidth - 100);
- auto impl = static_cast<ParagraphImpl*>(paragraph.get());
- auto calculate = [](const TextLine& line) -> SkScalar {
- return TestCanvasWidth - 100 - (line.offset().fX + line.width());
- };
- SkScalar epsilon = 0.1f;
- for (auto& line : impl->lines()) {
- if (&line == impl->lines().end() - 1) {
- REPORTER_ASSERT(reporter, calculate(line) > epsilon);
- } else {
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(line), 0, epsilon));
- }
- }
- // Just make sure the the text is actually RTL
- for (auto& run : impl->runs()) {
- REPORTER_ASSERT(reporter, !run.leftToRight());
- }
- }
- DEF_TEST(SkParagraph_DecorationsParagraph, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- ParagraphStyle paragraph_style;
- paragraph_style.setMaxLines(14);
- paragraph_style.setTextAlign(TextAlign::kLeft);
- paragraph_style.turnHintingOff();
- ParagraphBuilderImpl builder(paragraph_style, fontCollection);
- TextStyle text_style;
- text_style.setFontFamilies({SkString("Roboto")});
- text_style.setFontSize(26);
- text_style.setLetterSpacing(0);
- text_style.setWordSpacing(5);
- text_style.setColor(SK_ColorBLACK);
- text_style.setHeight(2);
- text_style.setDecoration(TextDecoration::kUnderline);
- text_style.setDecorationColor(SK_ColorBLACK);
- text_style.setDecoration((TextDecoration)(
- TextDecoration::kUnderline | TextDecoration::kOverline | TextDecoration::kLineThrough));
- text_style.setDecorationStyle(TextDecorationStyle::kSolid);
- text_style.setDecorationColor(SK_ColorBLACK);
- text_style.setDecorationThicknessMultiplier(2.0);
- builder.pushStyle(text_style);
- builder.addText("This text should be");
- text_style.setDecorationStyle(TextDecorationStyle::kDouble);
- text_style.setDecorationColor(SK_ColorBLUE);
- text_style.setDecorationThicknessMultiplier(1.0);
- builder.pushStyle(text_style);
- builder.addText(" decorated even when");
- text_style.setDecorationStyle(TextDecorationStyle::kDotted);
- text_style.setDecorationColor(SK_ColorBLACK);
- builder.pushStyle(text_style);
- builder.addText(" wrapped around to");
- text_style.setDecorationStyle(TextDecorationStyle::kDashed);
- text_style.setDecorationColor(SK_ColorBLACK);
- text_style.setDecorationThicknessMultiplier(3.0);
- builder.pushStyle(text_style);
- builder.addText(" the next line.");
- text_style.setDecorationStyle(TextDecorationStyle::kWavy);
- text_style.setDecorationColor(SK_ColorRED);
- text_style.setDecorationThicknessMultiplier(1.0);
- builder.pushStyle(text_style);
- builder.addText(" Otherwise, bad things happen.");
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(TestCanvasWidth - 100);
- auto impl = static_cast<ParagraphImpl*>(paragraph.get());
- size_t index = 0;
- for (auto& line : impl->lines()) {
- line.scanStyles(
- StyleType::kDecorations,
- [&index, reporter](TextRange, TextStyle style, SkScalar) {
- auto decoration = (TextDecoration)(TextDecoration::kUnderline |
- TextDecoration::kOverline |
- TextDecoration::kLineThrough);
- REPORTER_ASSERT(reporter, style.getDecorationType() == decoration);
- switch (index) {
- case 0:
- REPORTER_ASSERT(reporter, style.getDecorationStyle() ==
- TextDecorationStyle::kSolid);
- REPORTER_ASSERT(reporter, style.getDecorationColor() == SK_ColorBLACK);
- REPORTER_ASSERT(reporter,
- style.getDecorationThicknessMultiplier() == 2.0);
- break;
- case 1: // The style appears on 2 lines so it has 2 pieces
- REPORTER_ASSERT(reporter, style.getDecorationStyle() ==
- TextDecorationStyle::kDouble);
- REPORTER_ASSERT(reporter, style.getDecorationColor() == SK_ColorBLUE);
- REPORTER_ASSERT(reporter,
- style.getDecorationThicknessMultiplier() == 1.0);
- break;
- case 2:
- REPORTER_ASSERT(reporter, style.getDecorationStyle() ==
- TextDecorationStyle::kDotted);
- REPORTER_ASSERT(reporter, style.getDecorationColor() == SK_ColorBLACK);
- REPORTER_ASSERT(reporter,
- style.getDecorationThicknessMultiplier() == 1.0);
- break;
- case 3:
- case 4:
- REPORTER_ASSERT(reporter, style.getDecorationStyle() ==
- TextDecorationStyle::kDashed);
- REPORTER_ASSERT(reporter, style.getDecorationColor() == SK_ColorBLACK);
- REPORTER_ASSERT(reporter,
- style.getDecorationThicknessMultiplier() == 3.0);
- break;
- case 5:
- REPORTER_ASSERT(reporter, style.getDecorationStyle() ==
- TextDecorationStyle::kWavy);
- REPORTER_ASSERT(reporter, style.getDecorationColor() == SK_ColorRED);
- REPORTER_ASSERT(reporter,
- style.getDecorationThicknessMultiplier() == 1.0);
- break;
- default:
- REPORTER_ASSERT(reporter, false);
- break;
- }
- ++index;
- return true;
- });
- }
- }
- DEF_TEST(SkParagraph_ItalicsParagraph, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- ParagraphStyle paragraph_style;
- paragraph_style.turnHintingOff();
- ParagraphBuilderImpl builder(paragraph_style, fontCollection);
- TextStyle text_style;
- text_style.setFontFamilies({SkString("Roboto")});
- text_style.setFontSize(10);
- text_style.setColor(SK_ColorRED);
- builder.pushStyle(text_style);
- builder.addText("No italic ");
- text_style.setFontStyle(SkFontStyle(SkFontStyle::kNormal_Weight, SkFontStyle::kNormal_Width,
- SkFontStyle::kItalic_Slant));
- builder.pushStyle(text_style);
- builder.addText("Yes Italic ");
- builder.pop();
- builder.addText("No Italic again.");
- auto paragraph = builder.Build();
- paragraph->layout(TestCanvasWidth);
- auto impl = static_cast<ParagraphImpl*>(paragraph.get());
- REPORTER_ASSERT(reporter, impl->runs().size() == 3);
- REPORTER_ASSERT(reporter, impl->styles().size() == 3);
- REPORTER_ASSERT(reporter, impl->lines().size() == 1);
- auto& line = impl->lines()[0];
- size_t index = 0;
- line.scanStyles(
- StyleType::kForeground,
- [&index, reporter](TextRange textRange, TextStyle style, SkScalar) {
- switch (index) {
- case 0:
- REPORTER_ASSERT(
- reporter,
- style.getFontStyle().slant() == SkFontStyle::kUpright_Slant);
- break;
- case 1:
- REPORTER_ASSERT(reporter,
- style.getFontStyle().slant() == SkFontStyle::kItalic_Slant);
- break;
- case 2:
- REPORTER_ASSERT(
- reporter,
- style.getFontStyle().slant() == SkFontStyle::kUpright_Slant);
- break;
- default:
- REPORTER_ASSERT(reporter, false);
- break;
- }
- ++index;
- return true;
- });
- }
- DEF_TEST(SkParagraph_ChineseParagraph, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text =
- "左線読設重説切後碁給能上目秘使約。満毎冠行来昼本可必図将発確年。今属場育"
- "図情闘陰野高備込制詩西校客。審対江置講今固残必託地集済決維駆年策。立得庭"
- "際輝求佐抗蒼提夜合逃表。注統天言件自謙雅載報紙喪。作画稿愛器灯女書利変探"
- "訃第金線朝開化建。子戦年帝励害表月幕株漠新期刊人秘。図的海力生禁挙保天戦"
- "聞条年所在口。";
- ParagraphStyle paragraph_style;
- paragraph_style.setMaxLines(14);
- paragraph_style.setTextAlign(TextAlign::kJustify);
- paragraph_style.turnHintingOff();
- ParagraphBuilderImpl builder(paragraph_style, fontCollection);
- auto decoration = (TextDecoration)(TextDecoration::kUnderline | TextDecoration::kOverline |
- TextDecoration::kLineThrough);
- TextStyle text_style;
- text_style.setFontFamilies({SkString("Source Han Serif CN")});
- text_style.setFontSize(35);
- text_style.setColor(SK_ColorBLACK);
- text_style.setLetterSpacing(2);
- text_style.setHeight(1);
- text_style.setDecoration(decoration);
- text_style.setDecorationColor(SK_ColorBLACK);
- text_style.setDecorationStyle(TextDecorationStyle::kSolid);
- builder.pushStyle(text_style);
- builder.addText(text);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(TestCanvasWidth - 100);
- auto impl = static_cast<ParagraphImpl*>(paragraph.get());
- REPORTER_ASSERT(reporter, impl->runs().size() == 1);
- REPORTER_ASSERT(reporter, impl->lines().size() == 7);
- REPORTER_ASSERT(reporter, impl->styles().size() == 1);
- REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style));
- }
- DEF_TEST(SkParagraph_ArabicParagraph, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text =
- "من أسر وإعلان الخاصّة وهولندا،, عل قائمة الضغوط بالمطالبة تلك. الصفحة "
- "بمباركة التقليدية قام عن. تصفح";
- ParagraphStyle paragraph_style;
- paragraph_style.setMaxLines(14);
- paragraph_style.setTextAlign(TextAlign::kJustify);
- paragraph_style.turnHintingOff();
- ParagraphBuilderImpl builder(paragraph_style, fontCollection);
- auto decoration = (TextDecoration)(TextDecoration::kUnderline | TextDecoration::kOverline |
- TextDecoration::kLineThrough);
- TextStyle text_style;
- text_style.setFontFamilies({SkString("Katibeh")});
- text_style.setFontSize(35);
- text_style.setColor(SK_ColorBLACK);
- // TODO: turn off some font features for letter spacing (waiting on SkShaper)
- // text_style.setLetterSpacing(2);
- text_style.setDecoration(decoration);
- text_style.setDecorationColor(SK_ColorBLACK);
- text_style.setDecorationStyle(TextDecorationStyle::kSolid);
- builder.pushStyle(text_style);
- builder.addText(text);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(TestCanvasWidth - 100);
- auto impl = static_cast<ParagraphImpl*>(paragraph.get());
- REPORTER_ASSERT(reporter, impl->runs().size() == 1);
- REPORTER_ASSERT(reporter, impl->lines().size() == 2);
- REPORTER_ASSERT(reporter, impl->styles().size() == 1);
- REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style));
- }
- DEF_TEST(SkParagraph_GetGlyphPositionAtCoordinateParagraph, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text =
- "12345 67890 12345 67890 12345 67890 12345 67890 12345 67890 12345 "
- "67890 12345";
- ParagraphStyle paragraphStyle;
- paragraphStyle.setTextAlign(TextAlign::kLeft);
- paragraphStyle.setMaxLines(10);
- paragraphStyle.turnHintingOff();
- TextStyle textStyle;
- textStyle.setFontFamilies({SkString("Roboto")});
- textStyle.setFontSize(50);
- textStyle.setLetterSpacing(1);
- textStyle.setWordSpacing(5);
- textStyle.setHeight(1);
- textStyle.setColor(SK_ColorBLACK);
- ParagraphBuilderImpl builder(paragraphStyle, fontCollection);
- builder.pushStyle(textStyle);
- builder.addText(text);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(550);
- // Tests for getGlyphPositionAtCoordinate()
- // NOTE: resulting values can be a few off from their respective positions in
- // the original text because the final trailing whitespaces are sometimes not
- // drawn (namely, when using "justify" alignment) and therefore are not active
- // glyphs.
- REPORTER_ASSERT(reporter,
- paragraph->getGlyphPositionAtCoordinate(-10000, -10000).position == 0);
- REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(-1, -1).position == 0);
- REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(0, 0).position == 0);
- REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(3, 3).position == 0);
- REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(35, 1).position == 1);
- REPORTER_ASSERT(reporter,
- paragraph->getGlyphPositionAtCoordinate(300, 2).position == 10); // !!! 11
- REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(301, 2.2f).position == 11);
- REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(302, 2.6f).position == 11);
- REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(301, 2.1f).position == 11);
- REPORTER_ASSERT(reporter,
- paragraph->getGlyphPositionAtCoordinate(100000, 20).position == 18);
- REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(450, 20).position == 16);
- REPORTER_ASSERT(reporter,
- paragraph->getGlyphPositionAtCoordinate(100000, 90).position == 36);
- REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(-100000, 90).position == 18);
- REPORTER_ASSERT(reporter,
- paragraph->getGlyphPositionAtCoordinate(20, -80).position == 0); // !!! 1
- REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(1, 90).position == 18);
- REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(1, 170).position == 36);
- REPORTER_ASSERT(reporter,
- paragraph->getGlyphPositionAtCoordinate(10000, 180).position == 72);
- REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(70, 180).position == 56);
- REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(1, 270).position == 72);
- REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(35, 90).position == 19);
- REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(10000, 10000).position == 77);
- REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(85, 10000).position == 75);
- }
- DEF_TEST(SkParagraph_GetRectsForRangeParagraph, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text =
- "12345, \"67890\" 12345 67890 12345 67890 12345 67890 12345 67890 12345 "
- "67890 12345";
- ParagraphStyle paragraphStyle;
- paragraphStyle.setTextAlign(TextAlign::kLeft);
- paragraphStyle.setMaxLines(10);
- paragraphStyle.turnHintingOff();
- TextStyle textStyle;
- textStyle.setFontFamilies({SkString("Roboto")});
- textStyle.setFontSize(50);
- textStyle.setColor(SK_ColorBLACK);
- textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
- SkFontStyle::kUpright_Slant));
- ParagraphBuilderImpl builder(paragraphStyle, fontCollection);
- builder.pushStyle(textStyle);
- builder.addText(text);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(550);
- RectHeightStyle heightStyle = RectHeightStyle::kMax;
- RectWidthStyle widthStyle = RectWidthStyle::kTight;
- SkScalar epsilon = 0.01f;
- {
- auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.empty());
- }
- {
- auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 28.417f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
- }
- {
- auto result = paragraph->getRectsForRange(2, 8, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 56.835f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 177.97f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
- }
- {
- auto result = paragraph->getRectsForRange(8, 21, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 177.97f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 507.031f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
- }
- {
- auto result = paragraph->getRectsForRange(30, 100, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 4);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 211.375f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 59.40625f, epsilon));
- // This number does not match: 463.617 & 451.171
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 451.171f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 118, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.left(), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.top(), 236.406f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.right(), 142.089f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.bottom(), 295, epsilon));
- }
- {
- auto result = paragraph->getRectsForRange(19, 22, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 450.1875f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon));
- // This number does not match 519.472 & 507.031
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 507.031f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
- }
- {
- auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.empty());
- }
- }
- DEF_TEST(SkParagraph_GetRectsForRangeTight, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text =
- "( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)("
- " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)("
- " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)";
- ParagraphStyle paragraphStyle;
- paragraphStyle.setTextAlign(TextAlign::kLeft);
- paragraphStyle.setMaxLines(10);
- paragraphStyle.turnHintingOff();
- TextStyle textStyle;
- textStyle.setFontFamilies({ SkString("Noto Sans CJK JP")});
- textStyle.setFontSize(50);
- textStyle.setColor(SK_ColorBLACK);
- textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
- SkFontStyle::kUpright_Slant));
- ParagraphBuilderImpl builder(paragraphStyle, fontCollection);
- builder.pushStyle(textStyle);
- builder.addText(text);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(550);
- RectHeightStyle heightStyle = RectHeightStyle::kTight;
- RectWidthStyle widthStyle = RectWidthStyle::kTight;
- SkScalar epsilon = 0.01f;
- {
- auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.empty());
- }
- {
- auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 16.898f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 74, epsilon));
- }
- {
- auto result = paragraph->getRectsForRange(2, 8, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 66.899f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 264.099f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 74, epsilon));
- }
- {
- auto result = paragraph->getRectsForRange(8, 21, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 2);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 264.099f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 528.199f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.left(), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.right(), 172.199f, epsilon));
- }
- }
- DEF_TEST(SkParagraph_GetRectsForRangeIncludeLineSpacingMiddle, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text =
- "( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)("
- " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)("
- " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)";
- ParagraphStyle paragraphStyle;
- paragraphStyle.setTextAlign(TextAlign::kLeft);
- paragraphStyle.setMaxLines(10);
- paragraphStyle.turnHintingOff();
- TextStyle textStyle;
- textStyle.setFontFamilies({SkString("Noto Sans CJK JP")});
- textStyle.setFontSize(50);
- textStyle.setHeight(1.3f);
- textStyle.setColor(SK_ColorBLACK);
- textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
- SkFontStyle::kUpright_Slant));
- ParagraphBuilderImpl builder(paragraphStyle, fontCollection);
- builder.pushStyle(textStyle);
- builder.addText(text);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(550);
- RectHeightStyle heightStyle = RectHeightStyle::kIncludeLineSpacingMiddle;
- RectWidthStyle widthStyle = RectWidthStyle::kTight;
- SkScalar epsilon = 0.01f;
- // 16 glyphs per line 160/16 = 10 lines
- {
- auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.empty());
- }
- {
- auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 8.60f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 16.90f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 93.60f, epsilon));
- }
- {
- auto result = paragraph->getRectsForRange(2, 8, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 66.90f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 8.60f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 264.10f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 93.60f, epsilon));
- }
- {
- auto result = paragraph->getRectsForRange(8, 21, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 2);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 264.10f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 8.60f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 528.20f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 93.60f, epsilon));
- }
- {
- auto result = paragraph->getRectsForRange(30, 150, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 9);
- SkScalar offsetY = 104.60f;
- for (auto& box : result) {
- if (&box != &result.back()) {
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(box.rect.right(), 528.20f, epsilon));
- }
- if (&box != &result.front()) {
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(box.rect.left(), 0, epsilon));
- }
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(box.rect.top(), offsetY, epsilon));
- offsetY = box.rect.bottom() + 11;
- }
- }
- {
- auto result = paragraph->getRectsForRange(19, 22, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 97.20f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 104.60f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 197.20f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 189.60f, epsilon));
- }
- {
- auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.empty());
- }
- }
- DEF_TEST(SkParagraph_GetRectsForRangeIncludeLineSpacingTop, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text =
- "( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)("
- " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)("
- " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)";
- ParagraphStyle paragraphStyle;
- paragraphStyle.setTextAlign(TextAlign::kLeft);
- paragraphStyle.setMaxLines(10);
- paragraphStyle.turnHintingOff();
- TextStyle textStyle;
- textStyle.setFontFamilies({SkString("Noto Sans CJK JP")});
- textStyle.setFontSize(50);
- textStyle.setWordSpacing(0);
- textStyle.setLetterSpacing(0);
- textStyle.setHeight(1.3f);
- textStyle.setColor(SK_ColorBLACK);
- textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
- SkFontStyle::kUpright_Slant));
- ParagraphBuilderImpl builder(paragraphStyle, fontCollection);
- builder.pushStyle(textStyle);
- builder.addText(text);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(550);
- RectHeightStyle heightStyle = RectHeightStyle::kIncludeLineSpacingTop;
- RectWidthStyle widthStyle = RectWidthStyle::kTight;
- SkScalar epsilon = 0.01f;
- {
- auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.empty());
- }
- {
- auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 16.898f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 91.199f, epsilon));
- }
- {
- auto result = paragraph->getRectsForRange(2, 8, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 66.899f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 264.099f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 91.199f, epsilon));
- }
- {
- auto result = paragraph->getRectsForRange(8, 21, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 2);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 264.099f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 528.199f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 91.199f, epsilon));
- }
- {
- auto result = paragraph->getRectsForRange(30, 150, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 9);
- SkScalar level = 96;
- for (auto& box : result) {
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(box.rect.top(), level, epsilon));
- level += 96;
- if (&box != &result.back()) {
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(box.rect.right(), 528.199f, epsilon));
- }
- if (&box != &result.front()) {
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(box.rect.left(), 0, epsilon));
- }
- }
- }
- {
- auto result = paragraph->getRectsForRange(19, 22, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 97.199f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 96, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 197.199f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 187.199f, epsilon));
- }
- {
- auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.empty());
- }
- }
- DEF_TEST(SkParagraph_GetRectsForRangeIncludeLineSpacingBottom, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text =
- "( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)("
- " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)("
- " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)";
- ParagraphStyle paragraphStyle;
- paragraphStyle.setTextAlign(TextAlign::kLeft);
- paragraphStyle.setMaxLines(10);
- paragraphStyle.turnHintingOff();
- TextStyle textStyle;
- textStyle.setFontFamilies({SkString("Noto Sans CJK JP")});
- textStyle.setFontSize(50);
- textStyle.setWordSpacing(0);
- textStyle.setLetterSpacing(0);
- textStyle.setHeight(1.3f);
- textStyle.setColor(SK_ColorBLACK);
- textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
- SkFontStyle::kUpright_Slant));
- ParagraphBuilderImpl builder(paragraphStyle, fontCollection);
- builder.pushStyle(textStyle);
- builder.addText(text);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(550);
- RectHeightStyle heightStyle = RectHeightStyle::kIncludeLineSpacingBottom;
- RectWidthStyle widthStyle = RectWidthStyle::kTight;
- SkScalar epsilon = 0.01f;
- {
- auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.empty());
- }
- {
- auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 17.199f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 16.898f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 96, epsilon));
- }
- {
- auto result = paragraph->getRectsForRange(2, 8, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 66.899f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 17.199f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 264.099f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 96, epsilon));
- }
- {
- auto result = paragraph->getRectsForRange(8, 21, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 2);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 264.099f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 528.199f, epsilon));
- // It seems that Minikin does not take in account like breaks, but we do.
- // SkParagraph returns 528.199 instead
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.left(), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.right(), 172.199f, epsilon));
- }
- {
- auto result = paragraph->getRectsForRange(30, 150, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 9);
- SkScalar level = 17.199f + 96;
- for (auto& box : result) {
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(box.rect.top(), level, epsilon));
- level += 96;
- if (&box != &result.back()) {
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(box.rect.right(), 528.199f, epsilon));
- }
- if (&box != &result.front()) {
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(box.rect.left(), 0, epsilon));
- }
- }
- }
- {
- auto result = paragraph->getRectsForRange(19, 22, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 97.199f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 113.199f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 197.199f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 192, epsilon));
- }
- {
- auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.empty());
- }
- }
- DEF_TEST(SkParagraph_GetRectsForRangeIncludeCombiningCharacter, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text = "ดีสวัสดีชาวโลกที่น่ารัก";
- ParagraphStyle paragraphStyle;
- paragraphStyle.setTextAlign(TextAlign::kCenter);
- paragraphStyle.setMaxLines(10);
- paragraphStyle.turnHintingOff();
- ParagraphBuilderImpl builder(paragraphStyle, fontCollection);
- TextStyle textStyle;
- textStyle.setFontFamilies({SkString("Roboto")});
- textStyle.setFontSize(50);
- textStyle.setLetterSpacing(1);
- textStyle.setWordSpacing(5);
- textStyle.setHeight(1);
- textStyle.setColor(SK_ColorBLACK);
- builder.pushStyle(textStyle);
- builder.addText(text);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(TestCanvasWidth - 100);
- auto impl = static_cast<ParagraphImpl*>(paragraph.get());
- REPORTER_ASSERT(reporter, impl->lines().size() == 1);
- RectHeightStyle heightStyle = RectHeightStyle::kTight;
- RectWidthStyle widthStyle = RectWidthStyle::kTight;
- {
- auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.empty());
- }
- {
- auto first = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle);
- auto second = paragraph->getRectsForRange(1, 2, heightStyle, widthStyle);
- auto last = paragraph->getRectsForRange(0, 2, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, first.size() == 1 && second.size() == 1 && last.size() == 1);
- REPORTER_ASSERT(reporter,
- last[0].rect.fLeft = SkTMin(first[0].rect.fLeft, second[0].rect.fLeft));
- REPORTER_ASSERT(reporter,
- last[0].rect.fRight = SkTMax(first[0].rect.fRight, second[0].rect.fRight));
- }
- {
- auto first = paragraph->getRectsForRange(3, 4, heightStyle, widthStyle);
- auto second = paragraph->getRectsForRange(4, 5, heightStyle, widthStyle);
- auto last = paragraph->getRectsForRange(3, 5, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, first.size() == 1 && second.size() == 1 && last.size() == 1);
- REPORTER_ASSERT(reporter,
- last[0].rect.fLeft = SkTMin(first[0].rect.fLeft, second[0].rect.fLeft));
- REPORTER_ASSERT(reporter,
- last[0].rect.fRight = SkTMax(first[0].rect.fRight, second[0].rect.fRight));
- }
- {
- auto first = paragraph->getRectsForRange(14, 15, heightStyle, widthStyle);
- auto second = paragraph->getRectsForRange(15, 16, heightStyle, widthStyle);
- auto third = paragraph->getRectsForRange(16, 17, heightStyle, widthStyle);
- auto last = paragraph->getRectsForRange(14, 17, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, first.size() == 1 && second.size() == 1 && third.size() == 1 &&
- last.size() == 1);
- REPORTER_ASSERT(reporter,
- last[0].rect.fLeft = SkTMin(first[0].rect.fLeft, third[0].rect.fLeft));
- REPORTER_ASSERT(reporter,
- last[0].rect.fRight = SkTMax(first[0].rect.fRight, third[0].rect.fRight));
- }
- }
- DEF_TEST(SkParagraph_GetRectsForRangeCenterParagraph, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- // Minikin uses a hard coded list of unicode characters that he treats as invisible - as spaces.
- // It's absolutely wrong - invisibility is a glyph attribute, not character/grapheme.
- // Any attempt to substitute one for another leads to errors
- // (for instance, some fonts can use these hard coded characters for something that is visible)
- const char* text =
- "01234 "; //"01234 "; // includes ideographic space and english space.
- ParagraphStyle paragraphStyle;
- paragraphStyle.setTextAlign(TextAlign::kCenter);
- paragraphStyle.setMaxLines(10);
- paragraphStyle.turnHintingOff();
- ParagraphBuilderImpl builder(paragraphStyle, fontCollection);
- TextStyle textStyle;
- textStyle.setFontFamilies({SkString("Roboto")});
- textStyle.setFontSize(50);
- textStyle.setLetterSpacing(0);
- textStyle.setWordSpacing(0);
- textStyle.setHeight(1);
- textStyle.setColor(SK_ColorBLACK);
- textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
- SkFontStyle::kUpright_Slant));
- builder.pushStyle(textStyle);
- builder.addText(text);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(550);
- // Some of the formatting lazily done on paint
- RectHeightStyle heightStyle = RectHeightStyle::kMax;
- RectWidthStyle widthStyle = RectWidthStyle::kTight;
- SkScalar epsilon = 0.01f;
- {
- auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.empty());
- }
- {
- auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 203.955f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 232.373f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
- }
- {
- auto result = paragraph->getRectsForRange(2, 4, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 260.791f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 317.626f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
- }
- {
- auto result = paragraph->getRectsForRange(4, 5, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 317.626f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 346.044f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
- }
- {
- auto result = paragraph->getRectsForRange(4, 6, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 317.626f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 346.044f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
- }
- {
- auto result = paragraph->getRectsForRange(5, 6, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 346.044f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 346.044f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
- }
- {
- auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.empty());
- }
- }
- DEF_TEST(SkParagraph_GetRectsForRangeCenterParagraphNewlineCentered, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text = "01234\n";
- ParagraphStyle paragraphStyle;
- paragraphStyle.setTextAlign(TextAlign::kCenter);
- paragraphStyle.setMaxLines(10);
- paragraphStyle.turnHintingOff();
- ParagraphBuilderImpl builder(paragraphStyle, fontCollection);
- TextStyle textStyle;
- textStyle.setFontFamilies({SkString("Roboto")});
- textStyle.setFontSize(50);
- textStyle.setLetterSpacing(0);
- textStyle.setWordSpacing(0);
- textStyle.setHeight(1);
- textStyle.setColor(SK_ColorBLACK);
- textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
- SkFontStyle::kUpright_Slant));
- builder.pushStyle(textStyle);
- builder.addText(text);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(550);
- auto impl = static_cast<ParagraphImpl*>(paragraph.get());
- REPORTER_ASSERT(reporter, impl->lines().size() == 2);
- RectHeightStyle heightStyle = RectHeightStyle::kMax;
- RectWidthStyle widthStyle = RectWidthStyle::kTight;
- SkScalar epsilon = 0.01f;
- {
- auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.empty());
- }
- {
- auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 203.955f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 232.373f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
- }
- {
- // Minikin has [6:7] not empty but I cannot imagine how and why
- auto result = paragraph->getRectsForRange(5, 6, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 346.044f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.406f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 346.044f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
- }
- }
- DEF_TEST(SkParagraph_GetRectsForRangeCenterMultiLineParagraph, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text = "01234 \n0123 ";
- // "01234 \n0123 "; // includes ideographic space and english space.
- ParagraphStyle paragraphStyle;
- paragraphStyle.setTextAlign(TextAlign::kCenter);
- paragraphStyle.setMaxLines(10);
- paragraphStyle.turnHintingOff();
- ParagraphBuilderImpl builder(paragraphStyle, fontCollection);
- TextStyle textStyle;
- textStyle.setFontFamilies({SkString("Roboto")});
- textStyle.setFontSize(50);
- textStyle.setLetterSpacing(0);
- textStyle.setWordSpacing(0);
- textStyle.setHeight(1);
- textStyle.setColor(SK_ColorBLACK);
- textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
- SkFontStyle::kUpright_Slant));
- builder.pushStyle(textStyle);
- builder.addText(text);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(550);
- auto impl = static_cast<ParagraphImpl*>(paragraph.get());
- REPORTER_ASSERT(reporter, impl->lines().size() == 2);
- RectHeightStyle heightStyle = RectHeightStyle::kMax;
- RectWidthStyle widthStyle = RectWidthStyle::kTight;
- SkScalar epsilon = 0.01f;
- {
- auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.empty());
- }
- {
- auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 203.955f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 232.373f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
- }
- {
- auto result = paragraph->getRectsForRange(2, 4, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 260.791f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 317.626f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
- }
- {
- auto result = paragraph->getRectsForRange(4, 5, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 317.626f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 346.044f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
- }
- {
- auto result = paragraph->getRectsForRange(4, 6, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 317.626f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 346.044f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
- }
- {
- auto result = paragraph->getRectsForRange(5, 6, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 346.044f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 346.044f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
- }
- {
- auto result = paragraph->getRectsForRange(10, 12, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 218.164f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 59.40625f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 275, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 118, epsilon));
- }
- {
- // Minikin counts all spaces but they really don't appear on the screen
- // and the text even centered without them
- auto result = paragraph->getRectsForRange(14, 18, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 331.835f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 59.40625f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 331.835f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 118, epsilon));
- }
- {
- auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle);
- REPORTER_ASSERT(reporter, result.empty());
- }
- }
- DEF_TEST(SkParagraph_GetWordBoundaries, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- ParagraphStyle paragraphStyle;
- paragraphStyle.setTextAlign(TextAlign::kLeft);
- paragraphStyle.setMaxLines(10);
- paragraphStyle.turnHintingOff();
- TextStyle textStyle;
- textStyle.setFontFamilies({SkString("Roboto")});
- textStyle.setFontSize(52);
- textStyle.setLetterSpacing(1.19039f);
- textStyle.setWordSpacing(5);
- textStyle.setHeight(1.5);
- textStyle.setColor(SK_ColorBLACK);
- textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
- SkFontStyle::kUpright_Slant));
- ParagraphBuilderImpl builder(paragraphStyle, fontCollection);
- builder.pushStyle(textStyle);
- builder.addText(
- "12345 67890 12345 67890 12345 67890 12345 67890 12345 67890 12345 67890 12345");
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(550);
- REPORTER_ASSERT(reporter, paragraph->getWordBoundary(0) == SkRange<size_t>(0, 5));
- REPORTER_ASSERT(reporter, paragraph->getWordBoundary(1) == SkRange<size_t>(0, 5));
- REPORTER_ASSERT(reporter, paragraph->getWordBoundary(2) == SkRange<size_t>(0, 5));
- REPORTER_ASSERT(reporter, paragraph->getWordBoundary(3) == SkRange<size_t>(0, 5));
- REPORTER_ASSERT(reporter, paragraph->getWordBoundary(4) == SkRange<size_t>(0, 5));
- REPORTER_ASSERT(reporter, paragraph->getWordBoundary(5) == SkRange<size_t>(5, 7));
- REPORTER_ASSERT(reporter, paragraph->getWordBoundary(6) == SkRange<size_t>(5, 7));
- REPORTER_ASSERT(reporter, paragraph->getWordBoundary(7) == SkRange<size_t>(7, 12));
- REPORTER_ASSERT(reporter, paragraph->getWordBoundary(8) == SkRange<size_t>(7, 12));
- REPORTER_ASSERT(reporter, paragraph->getWordBoundary(9) == SkRange<size_t>(7, 12));
- REPORTER_ASSERT(reporter, paragraph->getWordBoundary(10) == SkRange<size_t>(7, 12));
- REPORTER_ASSERT(reporter, paragraph->getWordBoundary(11) == SkRange<size_t>(7, 12));
- REPORTER_ASSERT(reporter, paragraph->getWordBoundary(12) == SkRange<size_t>(12, 13));
- REPORTER_ASSERT(reporter, paragraph->getWordBoundary(13) == SkRange<size_t>(13, 18));
- REPORTER_ASSERT(reporter, paragraph->getWordBoundary(30) == SkRange<size_t>(30, 31));
- auto len = static_cast<ParagraphImpl*>(paragraph.get())->text().size();
- REPORTER_ASSERT(reporter, paragraph->getWordBoundary(len - 1) == SkRange<size_t>(len - 5, len));
- }
- DEF_TEST(SkParagraph_SpacingParagraph, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- ParagraphStyle paragraph_style;
- paragraph_style.setMaxLines(10);
- paragraph_style.setTextAlign(TextAlign::kLeft);
- paragraph_style.turnHintingOff();
- ParagraphBuilderImpl builder(paragraph_style, fontCollection);
- TextStyle text_style;
- text_style.setFontFamilies({SkString("Roboto")});
- text_style.setFontSize(50);
- text_style.setLetterSpacing(20);
- text_style.setWordSpacing(0);
- text_style.setColor(SK_ColorBLACK);
- text_style.setHeight(1);
- builder.pushStyle(text_style);
- builder.addText("H");
- builder.pop();
- text_style.setLetterSpacing(10);
- builder.pushStyle(text_style);
- builder.addText("H");
- builder.pop();
- text_style.setLetterSpacing(20);
- builder.pushStyle(text_style);
- builder.addText("H");
- builder.pop();
- text_style.setLetterSpacing(0);
- builder.pushStyle(text_style);
- builder.addText("|");
- builder.pop();
- text_style.setLetterSpacing(0);
- text_style.setWordSpacing(20);
- builder.pushStyle(text_style);
- builder.addText("H ");
- builder.pop();
- text_style.setLetterSpacing(0);
- text_style.setWordSpacing(0);
- builder.pushStyle(text_style);
- builder.addText("H ");
- builder.pop();
- text_style.setLetterSpacing(0);
- text_style.setWordSpacing(20);
- builder.pushStyle(text_style);
- builder.addText("H ");
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(550);
- auto impl = static_cast<ParagraphImpl*>(paragraph.get());
- REPORTER_ASSERT(reporter, impl->lines().size() == 1);
- size_t index = 0;
- impl->lines().begin()->scanStyles(StyleType::kLetterSpacing,
- [&index](TextRange text, TextStyle style, SkScalar) {
- ++index;
- return true;
- });
- REPORTER_ASSERT(reporter, index == 4);
- index = 0;
- impl->lines().begin()->scanStyles(StyleType::kWordSpacing,
- [&index](TextRange text, TextStyle style, SkScalar) {
- ++index;
- return true;
- });
- REPORTER_ASSERT(reporter, index == 4);
- }
- DEF_TEST(SkParagraph_LongWordParagraph, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text =
- "A "
- "veryverylongwordtoseewherethiswillwraporifitwillatallandifitdoesthenthat"
- "wouldbeagoodthingbecausethebreakingisworking.";
- ParagraphStyle paragraph_style;
- paragraph_style.turnHintingOff();
- ParagraphBuilderImpl builder(paragraph_style, fontCollection);
- TextStyle text_style;
- text_style.setFontFamilies({SkString("Roboto")});
- text_style.setColor(SK_ColorRED);
- text_style.setFontSize(31);
- text_style.setLetterSpacing(0);
- text_style.setWordSpacing(0);
- text_style.setColor(SK_ColorBLACK);
- text_style.setHeight(1);
- builder.pushStyle(text_style);
- builder.addText(text);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(TestCanvasWidth / 2);
- auto impl = static_cast<ParagraphImpl*>(paragraph.get());
- REPORTER_ASSERT(reporter, impl->text().size() == std::string{text}.length());
- REPORTER_ASSERT(reporter, impl->runs().size() == 1);
- REPORTER_ASSERT(reporter, impl->styles().size() == 1);
- REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style));
- REPORTER_ASSERT(reporter, impl->lines().size() == 4);
- REPORTER_ASSERT(reporter, impl->lines()[0].width() > TestCanvasWidth / 2 - 20);
- REPORTER_ASSERT(reporter, impl->lines()[1].width() > TestCanvasWidth / 2 - 20);
- REPORTER_ASSERT(reporter, impl->lines()[2].width() > TestCanvasWidth / 2 - 20);
- }
- DEF_TEST(SkParagraph_KernScaleParagraph, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- float scale = 3.0f;
- ParagraphStyle paragraph_style;
- ParagraphBuilderImpl builder(paragraph_style, fontCollection);
- TextStyle text_style;
- text_style.setFontFamilies({SkString("Droid Serif")});
- text_style.setFontSize(100 / scale);
- text_style.setWordSpacing(0);
- text_style.setLetterSpacing(0);
- text_style.setHeight(1);
- text_style.setColor(SK_ColorBLACK);
- builder.pushStyle(text_style);
- builder.addText("AV00\nVA00\nA0V0");
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(TestCanvasWidth / scale);
- auto impl = static_cast<ParagraphImpl*>(paragraph.get());
- // First and second lines must have the same width, the third one must be bigger
- SkScalar epsilon = 0.01f;
- REPORTER_ASSERT(reporter, impl->lines().size() == 3);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].width(), 80.58f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[1].width(), 80.58f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[2].width(), 83.25f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].height(), 39.00f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[1].height(), 39.00f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[2].height(), 39.00f, epsilon));
- }
- DEF_TEST(SkParagraph_NewlineParagraph, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text =
- "line1\nline2 test1 test2 test3 test4 test5 test6 test7\nline3\n\nline4 "
- "test1 test2 test3 test4";
- ParagraphStyle paragraph_style;
- paragraph_style.turnHintingOff();
- ParagraphBuilderImpl builder(paragraph_style, fontCollection);
- TextStyle text_style;
- text_style.setFontFamilies({SkString("Roboto")});
- text_style.setColor(SK_ColorRED);
- text_style.setFontSize(60);
- text_style.setLetterSpacing(0);
- text_style.setWordSpacing(0);
- text_style.setColor(SK_ColorBLACK);
- text_style.setHeight(1);
- builder.pushStyle(text_style);
- builder.addText(text);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(TestCanvasWidth - 300);
- auto impl = static_cast<ParagraphImpl*>(paragraph.get());
- // Minikin does not count empty lines but SkParagraph does
- REPORTER_ASSERT(reporter, impl->lines().size() == 7);
- REPORTER_ASSERT(reporter, impl->lines()[0].offset().fY == 0);
- REPORTER_ASSERT(reporter, impl->lines()[1].offset().fY == 70);
- REPORTER_ASSERT(reporter, impl->lines()[2].offset().fY == 140);
- REPORTER_ASSERT(reporter, impl->lines()[3].offset().fY == 210);
- REPORTER_ASSERT(reporter, impl->lines()[4].offset().fY == 280); // Empty line
- REPORTER_ASSERT(reporter, impl->lines()[5].offset().fY == 350);
- REPORTER_ASSERT(reporter, impl->lines()[6].offset().fY == 420);
- SkScalar epsilon = 0.1f;
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].width(), 130.31f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[1].width(), 586.64f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[2].width(), 593.49f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[3].width(), 130.31f, epsilon));
- REPORTER_ASSERT(reporter,
- SkScalarNearlyEqual(impl->lines()[4].width(), 0, epsilon)); // Empty line
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[5].width(), 586.64f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[6].width(), 137.16f, epsilon));
- REPORTER_ASSERT(reporter, impl->lineShift(0) == 0);
- }
- DEF_TEST(SkParagraph_EmojiParagraph, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text =
- "😀😃😄😁😆😅😂🤣☺😇🙂😍😡😟😢😻👽💩👍👎🙏👌👋👄👁👦👼👨🚀👨🚒🙋♂️👳👨👨👧"
- ""
- "👧"
- "💼👡👠☂🐶🐰🐻🐼🐷🐒🐵🐔🐧🐦🐋🐟🐡🕸🐌🐴🐊🐄🐪🐘🌸🌏🔥🌟🌚🌝"
- "💦"
- "💧"
- "❄🍕🍔🍟🥝🍱🕶🎩🏈⚽🚴♀️🎻🎼🎹🚨🚎🚐⚓🛳🚀🚁🏪🏢🖱⏰📱💾💉📉🛏"
- "🔑"
- "🔓"
- "📁🗓📊❤💯🚫🔻♠♣🕓❗🏳🏁🏳️🌈🇮🇹🇱🇷🇺🇸🇬🇧🇨🇳"
- "🇧"
- "🇴";
- ParagraphStyle paragraph_style;
- paragraph_style.turnHintingOff();
- ParagraphBuilderImpl builder(paragraph_style, fontCollection);
- TextStyle text_style;
- text_style.setFontFamilies({SkString("Noto Color Emoji")});
- text_style.setFontSize(50);
- text_style.setDecoration(TextDecoration::kUnderline);
- text_style.setColor(SK_ColorBLACK);
- builder.pushStyle(text_style);
- builder.addText(text);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(TestCanvasWidth);
- auto impl = static_cast<ParagraphImpl*>(paragraph.get());
- REPORTER_ASSERT(reporter, impl->lines().size() == 8);
- for (auto& line : impl->lines()) {
- if (&line != impl->lines().end() - 1) {
- REPORTER_ASSERT(reporter, line.width() == 998.25f);
- } else {
- REPORTER_ASSERT(reporter, line.width() < 998.25f);
- }
- REPORTER_ASSERT(reporter, line.height() == 59);
- }
- }
- DEF_TEST(SkParagraph_RepeatLayoutParagraph, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text =
- "Sentence to layout at diff widths to get diff line counts. short words "
- "short words short words short words short words short words short words "
- "short words short words short words short words short words short words "
- "end";
- ParagraphStyle paragraph_style;
- paragraph_style.turnHintingOff();
- ParagraphBuilderImpl builder(paragraph_style, fontCollection);
- TextStyle text_style;
- text_style.setFontFamilies({SkString("Roboto")});
- text_style.setFontSize(31);
- text_style.setLetterSpacing(0);
- text_style.setWordSpacing(0);
- text_style.setColor(SK_ColorBLACK);
- text_style.setHeight(1);
- text_style.setDecoration(TextDecoration::kUnderline);
- text_style.setDecorationColor(SK_ColorBLACK);
- builder.pushStyle(text_style);
- builder.addText(text);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(300);
- auto impl = static_cast<ParagraphImpl*>(paragraph.get());
- // Some of the formatting lazily done on paint
- REPORTER_ASSERT(reporter, impl->runs().size() == 1);
- REPORTER_ASSERT(reporter, impl->styles().size() == 1);
- REPORTER_ASSERT(reporter, impl->lines().size() == 12);
- paragraph->layout(600);
- REPORTER_ASSERT(reporter, impl->runs().size() == 1);
- REPORTER_ASSERT(reporter, impl->styles().size() == 1);
- REPORTER_ASSERT(reporter, impl->lines().size() == 6);
- }
- DEF_TEST(SkParagraph_Ellipsize, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text =
- "This is a very long sentence to test if the text will properly wrap "
- "around and go to the next line. Sometimes, short sentence. Longer "
- "sentences are okay too because they are nessecary. Very short. ";
- ParagraphStyle paragraph_style;
- paragraph_style.setMaxLines(1);
- paragraph_style.setEllipsis(u"\u2026");
- paragraph_style.turnHintingOff();
- ParagraphBuilderImpl builder(paragraph_style, fontCollection);
- TextStyle text_style;
- text_style.setFontFamilies({SkString("Roboto")});
- text_style.setDecorationColor(SK_ColorBLACK);
- builder.pushStyle(text_style);
- builder.addText(text);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(TestCanvasWidth);
- auto impl = static_cast<ParagraphImpl*>(paragraph.get());
- // Check that the ellipsizer limited the text to one line and did not wrap to a second line.
- REPORTER_ASSERT(reporter, impl->lines().size() == 1);
- auto& line = impl->lines()[0];
- REPORTER_ASSERT(reporter, line.ellipsis() != nullptr);
- size_t index = 0;
- line.scanRuns([&index, &line, reporter](Run* run, int32_t, size_t, SkRect, SkScalar, bool) {
- ++index;
- if (index == 2) {
- REPORTER_ASSERT(reporter, run->textRange() == line.ellipsis()->textRange());
- }
- return true;
- });
- REPORTER_ASSERT(reporter, index == 2);
- }
- DEF_TEST(SkParagraph_UnderlineShiftParagraph, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text1 = "fluttser ";
- const char* text2 = "mdje";
- const char* text3 = "fluttser mdje";
- ParagraphStyle paragraph_style;
- paragraph_style.turnHintingOff();
- paragraph_style.setTextAlign(TextAlign::kLeft);
- paragraph_style.setMaxLines(2);
- ParagraphBuilderImpl builder(paragraph_style, fontCollection);
- TextStyle text_style;
- text_style.setFontFamilies({SkString("Roboto")});
- text_style.setColor(SK_ColorBLACK);
- builder.pushStyle(text_style);
- builder.addText(text1);
- text_style.setDecoration(TextDecoration::kUnderline);
- text_style.setDecorationColor(SK_ColorBLACK);
- builder.pushStyle(text_style);
- builder.addText(text2);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(TestCanvasWidth);
- auto impl = static_cast<ParagraphImpl*>(paragraph.get());
- ParagraphBuilderImpl builder1(paragraph_style, fontCollection);
- text_style.setDecoration(TextDecoration::kNoDecoration);
- builder1.pushStyle(text_style);
- builder1.addText(text3);
- builder1.pop();
- auto paragraph1 = builder1.Build();
- paragraph1->layout(TestCanvasWidth);
- auto impl1 = static_cast<ParagraphImpl*>(paragraph1.get());
- REPORTER_ASSERT(reporter, impl->lines().size() == 1);
- REPORTER_ASSERT(reporter, impl1->lines().size() == 1);
- {
- auto& line = impl->lines()[0];
- size_t index = 0;
- line.scanStyles(
- StyleType::kDecorations,
- [&index, reporter](TextRange text, TextStyle style, SkScalar) {
- switch (index) {
- case 0:
- REPORTER_ASSERT(reporter,
- style.getDecorationType() == TextDecoration::kNoDecoration);
- break;
- case 1:
- REPORTER_ASSERT(reporter,
- style.getDecorationType() == TextDecoration::kUnderline);
- break;
- default:
- REPORTER_ASSERT(reporter, false);
- break;
- }
- ++index;
- return true;
- });
- REPORTER_ASSERT(reporter, index == 2);
- }
- {
- auto& line = impl1->lines()[0];
- size_t index = 0;
- line.scanStyles(StyleType::kDecorations,
- [&index, reporter](TextRange text, TextStyle style, SkScalar) {
- if (index == 0) {
- REPORTER_ASSERT(reporter, style.getDecorationType() ==
- TextDecoration::kNoDecoration);
- } else {
- REPORTER_ASSERT(reporter, false);
- }
- ++index;
- return true;
- });
- REPORTER_ASSERT(reporter, index == 1);
- }
- auto rect = paragraph->getRectsForRange(0, 12, RectHeightStyle::kMax, RectWidthStyle::kTight)
- .front()
- .rect;
- auto rect1 = paragraph1->getRectsForRange(0, 12, RectHeightStyle::kMax, RectWidthStyle::kTight)
- .front()
- .rect;
- REPORTER_ASSERT(reporter, rect.fLeft == rect1.fLeft);
- REPORTER_ASSERT(reporter, rect.fRight == rect1.fRight);
- for (size_t i = 0; i < 12; ++i) {
- auto r =
- paragraph->getRectsForRange(i, i + 1, RectHeightStyle::kMax, RectWidthStyle::kTight)
- .front()
- .rect;
- auto r1 =
- paragraph1
- ->getRectsForRange(i, i + 1, RectHeightStyle::kMax, RectWidthStyle::kTight)
- .front()
- .rect;
- REPORTER_ASSERT(reporter, r.fLeft == r1.fLeft);
- REPORTER_ASSERT(reporter, r.fRight == r1.fRight);
- }
- }
- DEF_TEST(SkParagraph_SimpleShadow, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text = "Hello World Text Dialog";
- ParagraphStyle paragraph_style;
- paragraph_style.turnHintingOff();
- ParagraphBuilderImpl builder(paragraph_style, fontCollection);
- TextStyle text_style;
- text_style.setFontFamilies({SkString("Roboto")});
- text_style.setColor(SK_ColorBLACK);
- text_style.addShadow(TextShadow(SK_ColorBLACK, SkPoint::Make(2.0f, 2.0f), 1.0));
- builder.pushStyle(text_style);
- builder.addText(text);
- auto paragraph = builder.Build();
- paragraph->layout(TestCanvasWidth);
- auto impl = static_cast<ParagraphImpl*>(paragraph.get());
- REPORTER_ASSERT(reporter, impl->runs().size() == 1);
- REPORTER_ASSERT(reporter, impl->styles().size() == 1);
- size_t index = 0;
- for (auto& line : impl->lines()) {
- line.scanStyles(
- StyleType::kShadow,
- [&index, text_style, reporter](TextRange text, TextStyle style, SkScalar) {
- REPORTER_ASSERT(reporter, index == 0 && style.equals(text_style));
- ++index;
- return true;
- });
- }
- }
- DEF_TEST(SkParagraph_ComplexShadow, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text = "Text Chunk ";
- ParagraphStyle paragraph_style;
- paragraph_style.turnHintingOff();
- ParagraphBuilderImpl builder(paragraph_style, fontCollection);
- TextStyle text_style;
- text_style.setFontFamilies({SkString("Roboto")});
- text_style.setColor(SK_ColorBLACK);
- text_style.addShadow(TextShadow(SK_ColorBLACK, SkPoint::Make(2.0f, 2.0f), 1.0f));
- builder.pushStyle(text_style);
- builder.addText(text);
- text_style.addShadow(TextShadow(SK_ColorRED, SkPoint::Make(2.0f, 2.0f), 5.0f));
- text_style.addShadow(TextShadow(SK_ColorGREEN, SkPoint::Make(10.0f, -5.0f), 3.0f));
- builder.pushStyle(text_style);
- builder.addText(text);
- builder.pop();
- builder.addText(text);
- text_style.addShadow(TextShadow(SK_ColorRED, SkPoint::Make(0.0f, 1.0f), 0.0f));
- builder.pushStyle(text_style);
- builder.addText(text);
- builder.pop();
- builder.addText(text);
- auto paragraph = builder.Build();
- paragraph->layout(TestCanvasWidth);
- auto impl = static_cast<ParagraphImpl*>(paragraph.get());
- size_t index = 0;
- for (auto& line : impl->lines()) {
- line.scanStyles(
- StyleType::kShadow,
- [&index, text_style, reporter](TextRange text, TextStyle style, SkScalar) {
- ++index;
- switch (index) {
- case 1:
- REPORTER_ASSERT(reporter, style.getShadowNumber() == 1);
- break;
- case 2:
- REPORTER_ASSERT(reporter, style.getShadowNumber() == 3);
- break;
- case 3:
- REPORTER_ASSERT(reporter, style.getShadowNumber() == 1);
- break;
- case 4:
- REPORTER_ASSERT(reporter, style.getShadowNumber() == 4);
- REPORTER_ASSERT(reporter, style.equals(text_style));
- break;
- case 5:
- REPORTER_ASSERT(reporter, style.getShadowNumber() == 1);
- break;
- default:
- REPORTER_ASSERT(reporter, false);
- }
- return true;
- });
- }
- }
- DEF_TEST(SkParagraph_BaselineParagraph, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text =
- "左線読設Byg後碁給能上目秘使約。満毎冠行来昼本可必図将発確年。今属場育"
- "図情闘陰野高備込制詩西校客。審対江置講今固残必託地集済決維駆年策。立得";
- ParagraphStyle paragraph_style;
- paragraph_style.turnHintingOff();
- paragraph_style.setMaxLines(14);
- paragraph_style.setTextAlign(TextAlign::kJustify);
- paragraph_style.setHeight(1.5);
- ParagraphBuilderImpl builder(paragraph_style, fontCollection);
- TextStyle text_style;
- text_style.setFontFamilies({SkString("Source Han Serif CN")});
- text_style.setColor(SK_ColorBLACK);
- text_style.setFontSize(55);
- text_style.setLetterSpacing(2);
- text_style.setDecorationStyle(TextDecorationStyle::kSolid);
- text_style.setDecorationColor(SK_ColorBLACK);
- builder.pushStyle(text_style);
- builder.addText(text);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(TestCanvasWidth - 100);
- SkScalar epsilon = 0.01f;
- REPORTER_ASSERT(reporter,
- SkScalarNearlyEqual(paragraph->getIdeographicBaseline(), 79.035f, epsilon));
- REPORTER_ASSERT(reporter,
- SkScalarNearlyEqual(paragraph->getAlphabeticBaseline(), 63.305f, epsilon));
- }
- DEF_TEST(SkParagraph_FontFallbackParagraph, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text1 = "Roboto 字典 ";
- const char* text2 = "Homemade Apple 字典";
- const char* text3 = "Chinese 字典";
- ParagraphStyle paragraph_style;
- paragraph_style.turnHintingOff();
- ParagraphBuilderImpl builder(paragraph_style, fontCollection);
- TextStyle text_style;
- text_style.setFontFamilies({
- SkString("Not a real font"),
- SkString("Also a fake font"),
- SkString("So fake it is obvious"),
- SkString("Next one should be a real font..."),
- SkString("Roboto"),
- SkString("another fake one in between"),
- SkString("Homemade Apple"),
- });
- text_style.setColor(SK_ColorBLACK);
- builder.pushStyle(text_style);
- builder.addText(text1);
- text_style.setFontFamilies({
- SkString("Not a real font"),
- SkString("Also a fake font"),
- SkString("So fake it is obvious"),
- SkString("Homemade Apple"),
- SkString("Next one should be a real font..."),
- SkString("Roboto"),
- SkString("another fake one in between"),
- SkString("Noto Sans CJK JP"),
- SkString("Source Han Serif CN"),
- });
- builder.pushStyle(text_style);
- builder.addText(text2);
- text_style.setFontFamilies({
- SkString("Not a real font"),
- SkString("Also a fake font"),
- SkString("So fake it is obvious"),
- SkString("Homemade Apple"),
- SkString("Next one should be a real font..."),
- SkString("Roboto"),
- SkString("another fake one in between"),
- SkString("Source Han Serif CN"),
- SkString("Noto Sans CJK JP"),
- });
- builder.pushStyle(text_style);
- builder.addText(text3);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(TestCanvasWidth);
- auto impl = static_cast<ParagraphImpl*>(paragraph.get());
- // Font resolution in Skia produces 6 runs because 2 parts of "Roboto 字典 " have different
- // script (Minikin merges the first 2 into one because of unresolved) [Apple + Unresolved ]
- // [Apple + Noto] [Apple + Han]
- REPORTER_ASSERT(reporter, impl->runs().size() == 6);
- SkScalar epsilon = 0.01f;
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->runs()[0].advance().fX, 48.46f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->runs()[1].advance().fX, 15.90f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->runs()[2].advance().fX, 139.12f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->runs()[3].advance().fX, 27.99f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->runs()[4].advance().fX, 62.24f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->runs()[5].advance().fX, 27.99f, epsilon));
- // When a different font is resolved, then the metrics are different.
- REPORTER_ASSERT(reporter, impl->runs()[1].ascent() != impl->runs()[3].ascent());
- REPORTER_ASSERT(reporter, impl->runs()[1].descent() != impl->runs()[3].descent());
- REPORTER_ASSERT(reporter, impl->runs()[3].ascent() != impl->runs()[5].ascent());
- REPORTER_ASSERT(reporter, impl->runs()[3].descent() != impl->runs()[5].descent());
- REPORTER_ASSERT(reporter, impl->runs()[1].ascent() != impl->runs()[5].ascent());
- REPORTER_ASSERT(reporter, impl->runs()[1].descent() != impl->runs()[5].descent());
- }
- DEF_TEST(SkParagraph_StrutParagraph1, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- // The chinese extra height should be absorbed by the strut.
- // const char* text = "01234満毎冠p来É本可\nabcd\n満毎É行p昼本可";
- const char* text = "01234満毎冠行来昼本可\nabcd\n満毎冠行来昼本可";
- ParagraphStyle paragraph_style;
- paragraph_style.setMaxLines(10);
- paragraph_style.setTextAlign(TextAlign::kLeft);
- paragraph_style.turnHintingOff();
- StrutStyle strut_style;
- strut_style.setStrutEnabled(true);
- strut_style.setFontFamilies({ SkString("BlahFake"), SkString("Ahem") });
- strut_style.setFontSize(50);
- strut_style.setHeight(1.8f);
- strut_style.setLeading(0.1f);
- strut_style.setForceStrutHeight(true);
- paragraph_style.setStrutStyle(strut_style);
- ParagraphBuilderImpl builder(paragraph_style, fontCollection);
- TextStyle text_style;
- text_style.setFontFamilies({SkString("Ahem")});
- text_style.setFontSize(50);
- // text_style.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
- // SkFontStyle::kUpright_Slant));
- text_style.setColor(SK_ColorBLACK);
- text_style.setHeight(0.5f);
- builder.pushStyle(text_style);
- builder.addText(text);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(550);
- auto impl = static_cast<ParagraphImpl*>(paragraph.get());
- // Font is not resolved and the first line does not fit
- REPORTER_ASSERT(reporter, impl->lines().size() == 4);
- RectHeightStyle rect_height_style = RectHeightStyle::kTight;
- RectHeightStyle rect_height_max_style = RectHeightStyle::kMax;
- RectWidthStyle rect_width_style = RectWidthStyle::kTight;
- SkScalar epsilon = 0.001f;
- {
- auto boxes = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style);
- REPORTER_ASSERT(reporter, boxes.empty());
- }
- {
- auto boxes = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style);
- REPORTER_ASSERT(reporter, boxes.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 34.5f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 84.5f, epsilon));
- }
- {
- auto boxes = paragraph->getRectsForRange(0, 1, rect_height_max_style, rect_width_style);
- REPORTER_ASSERT(reporter, boxes.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 95, epsilon));
- }
- {
- auto boxes = paragraph->getRectsForRange(6, 10, rect_height_style, rect_width_style);
- REPORTER_ASSERT(reporter, boxes.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 34.5f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 84.5f, epsilon));
- }
- {
- auto boxes = paragraph->getRectsForRange(6, 10, rect_height_max_style, rect_width_style);
- REPORTER_ASSERT(reporter, boxes.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 95, epsilon));
- }
- {
- auto boxes = paragraph->getRectsForRange(14, 16, rect_height_max_style, rect_width_style);
- REPORTER_ASSERT(reporter, boxes.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 190, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 100, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 285, epsilon));
- }
- {
- auto boxes = paragraph->getRectsForRange(20, 25, rect_height_max_style, rect_width_style);
- REPORTER_ASSERT(reporter, boxes.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 50, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 285, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 300, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 380, epsilon));
- }
- }
- DEF_TEST(SkParagraph_StrutParagraph2, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- // The chinese extra height should be absorbed by the strut.
- const char* text = "01234ABCDEFGH\nabcd\nABCDEFGH";
- ParagraphStyle paragraph_style;
- paragraph_style.setMaxLines(10);
- paragraph_style.setTextAlign(TextAlign::kLeft);
- paragraph_style.turnHintingOff();
- StrutStyle strut_style;
- strut_style.setStrutEnabled(true);
- strut_style.setFontFamilies({ SkString("Ahem") });
- strut_style.setFontSize(50);
- strut_style.setHeight(1.6f);
- paragraph_style.setStrutStyle(strut_style);
- ParagraphBuilderImpl builder(paragraph_style, fontCollection);
- TextStyle text_style;
- text_style.setFontFamilies({SkString("Ahem")});
- text_style.setFontSize(50);
- // text_style.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
- // SkFontStyle::kUpright_Slant));
- text_style.setColor(SK_ColorBLACK);
- text_style.setHeight(1);
- builder.pushStyle(text_style);
- builder.addText(text);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(550);
- auto impl = static_cast<ParagraphImpl*>(paragraph.get());
- // Font is not resolved and the first line does not fit
- REPORTER_ASSERT(reporter, impl->lines().size() == 4);
- RectHeightStyle rect_height_style = RectHeightStyle::kTight;
- RectHeightStyle rect_height_max_style = RectHeightStyle::kMax;
- RectWidthStyle rect_width_style = RectWidthStyle::kTight;
- SkScalar epsilon = 0.001f;
- {
- auto boxes = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style);
- REPORTER_ASSERT(reporter, boxes.empty());
- }
- {
- auto boxes = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style);
- REPORTER_ASSERT(reporter, boxes.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 24, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 74, epsilon));
- }
- {
- auto boxes = paragraph->getRectsForRange(0, 1, rect_height_max_style, rect_width_style);
- REPORTER_ASSERT(reporter, boxes.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 80, epsilon));
- }
- {
- auto boxes = paragraph->getRectsForRange(6, 10, rect_height_style, rect_width_style);
- REPORTER_ASSERT(reporter, boxes.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 24, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 74, epsilon));
- }
- {
- auto boxes = paragraph->getRectsForRange(6, 10, rect_height_max_style, rect_width_style);
- REPORTER_ASSERT(reporter, boxes.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 80, epsilon));
- }
- {
- auto boxes = paragraph->getRectsForRange(14, 16, rect_height_max_style, rect_width_style);
- REPORTER_ASSERT(reporter, boxes.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 160, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 100, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 240, epsilon));
- }
- {
- auto boxes = paragraph->getRectsForRange(20, 25, rect_height_max_style, rect_width_style);
- REPORTER_ASSERT(reporter, boxes.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 50, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 240, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 300, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 320, epsilon));
- }
- }
- DEF_TEST(SkParagraph_StrutParagraph3, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- // The chinese extra height should be absorbed by the strut.
- // const char* text = "01234満毎p行来昼本可\nabcd\n満毎冠行来昼本可";
- const char* text = "01234満毎冠行来昼本可\nabcd\n満毎冠行来昼本可";
- ParagraphStyle paragraph_style;
- paragraph_style.setMaxLines(10);
- paragraph_style.setTextAlign(TextAlign::kLeft);
- paragraph_style.turnHintingOff();
- StrutStyle strut_style;
- strut_style.setStrutEnabled(true);
- strut_style.setFontFamilies({ SkString("Ahem") });
- strut_style.setFontSize(50);
- strut_style.setHeight(1.2f);
- paragraph_style.setStrutStyle(strut_style);
- ParagraphBuilderImpl builder(paragraph_style, fontCollection);
- TextStyle text_style;
- text_style.setFontFamilies({SkString("Ahem")});
- text_style.setFontSize(50);
- // text_style.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
- // SkFontStyle::kUpright_Slant));
- text_style.setColor(SK_ColorBLACK);
- text_style.setHeight(1);
- builder.pushStyle(text_style);
- builder.addText(text);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(550);
- auto impl = static_cast<ParagraphImpl*>(paragraph.get());
- // Font is not resolved and the first line does not fit
- REPORTER_ASSERT(reporter, impl->lines().size() == 4);
- RectHeightStyle rect_height_style = RectHeightStyle::kTight;
- RectHeightStyle rect_height_max_style = RectHeightStyle::kMax;
- RectWidthStyle rect_width_style = RectWidthStyle::kTight;
- SkScalar epsilon = 0.001f;
- {
- auto boxes = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style);
- REPORTER_ASSERT(reporter, boxes.empty());
- }
- {
- auto boxes = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style);
- REPORTER_ASSERT(reporter, boxes.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 8, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 58, epsilon));
- }
- {
- auto boxes = paragraph->getRectsForRange(0, 1, rect_height_max_style, rect_width_style);
- REPORTER_ASSERT(reporter, boxes.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 60, epsilon));
- }
- {
- auto boxes = paragraph->getRectsForRange(6, 10, rect_height_style, rect_width_style);
- REPORTER_ASSERT(reporter, boxes.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 8, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 58, epsilon));
- }
- {
- auto boxes = paragraph->getRectsForRange(6, 10, rect_height_max_style, rect_width_style);
- REPORTER_ASSERT(reporter, boxes.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 60, epsilon));
- }
- {
- auto boxes = paragraph->getRectsForRange(14, 16, rect_height_max_style, rect_width_style);
- REPORTER_ASSERT(reporter, boxes.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 120, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 100, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 180, epsilon));
- }
- {
- auto boxes = paragraph->getRectsForRange(20, 25, rect_height_max_style, rect_width_style);
- REPORTER_ASSERT(reporter, boxes.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 50, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 180, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 300, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 240, epsilon));
- }
- }
- DEF_TEST(SkParagraph_StrutForceParagraph, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text = "01234満毎冠行来昼本可\nabcd\n満毎冠行来昼本可";
- ParagraphStyle paragraph_style;
- paragraph_style.setMaxLines(10);
- paragraph_style.setTextAlign(TextAlign::kLeft);
- paragraph_style.turnHintingOff();
- StrutStyle strut_style;
- strut_style.setStrutEnabled(true);
- strut_style.setFontFamilies({SkString("Ahem") });
- strut_style.setFontSize(50);
- strut_style.setHeight(1.5f);
- strut_style.setLeading(0.1f);
- strut_style.setForceStrutHeight(true);
- paragraph_style.setStrutStyle(strut_style);
- ParagraphBuilderImpl builder(paragraph_style, fontCollection);
- TextStyle text_style;
- text_style.setFontFamilies({SkString("Ahem")});
- text_style.setFontSize(50);
- text_style.setLetterSpacing(0);
- text_style.setColor(SK_ColorBLACK);
- text_style.setHeight(1);
- builder.pushStyle(text_style);
- builder.addText(text);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(550);
- auto impl = static_cast<ParagraphImpl*>(paragraph.get());
- // Font is not resolved and the first line does not fit
- REPORTER_ASSERT(reporter, impl->lines().size() == 4);
- RectHeightStyle rect_height_style = RectHeightStyle::kTight;
- RectHeightStyle rect_height_max_style = RectHeightStyle::kMax;
- RectWidthStyle rect_width_style = RectWidthStyle::kTight;
- SkScalar epsilon = 0.001f;
- auto boxes1 = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style);
- REPORTER_ASSERT(reporter, boxes1.empty());
- auto boxes2 = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style);
- REPORTER_ASSERT(reporter, boxes2.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes2[0].rect.left(), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes2[0].rect.top(), 22.5f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes2[0].rect.right(), 50, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes2[0].rect.bottom(), 72.5f, epsilon));
- auto boxes3 = paragraph->getRectsForRange(0, 1, rect_height_max_style, rect_width_style);
- REPORTER_ASSERT(reporter, boxes3.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes3[0].rect.left(), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes3[0].rect.top(), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes3[0].rect.right(), 50, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes3[0].rect.bottom(), 80, epsilon));
- auto boxes4 = paragraph->getRectsForRange(6, 10, rect_height_style, rect_width_style);
- REPORTER_ASSERT(reporter, boxes4.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes4[0].rect.left(), 300, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes4[0].rect.top(), 22.5f, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes4[0].rect.right(), 500, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes4[0].rect.bottom(), 72.5f, epsilon));
- auto boxes5 = paragraph->getRectsForRange(6, 10, rect_height_max_style, rect_width_style);
- REPORTER_ASSERT(reporter, boxes5.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes5[0].rect.left(), 300, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes5[0].rect.top(), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes5[0].rect.right(), 500, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes5[0].rect.bottom(), 80, epsilon));
- auto boxes6 = paragraph->getRectsForRange(14, 16, rect_height_max_style, rect_width_style);
- REPORTER_ASSERT(reporter, boxes6.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes6[0].rect.left(), 0, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes6[0].rect.top(), 160, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes6[0].rect.right(), 100, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes6[0].rect.bottom(), 240, epsilon));
- auto boxes7 = paragraph->getRectsForRange(20, 25, rect_height_max_style, rect_width_style);
- REPORTER_ASSERT(reporter, boxes7.size() == 1);
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes7[0].rect.left(), 50, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes7[0].rect.top(), 240, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes7[0].rect.right(), 300, epsilon));
- REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes7[0].rect.bottom(), 320, epsilon));
- }
- // Not in Minikin
- DEF_TEST(SkParagraph_WhitespacesInMultipleFonts, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text = "English English 字典 字典 😀😃😄 😀😃😄";
- ParagraphStyle paragraph_style;
- paragraph_style.turnHintingOff();
- ParagraphBuilderImpl builder(paragraph_style, fontCollection);
- TextStyle text_style;
- text_style.setFontFamilies(
- {SkString("Roboto"), SkString("Noto Color Emoji"), SkString("Source Han Serif CN")});
- text_style.setFontSize(60);
- builder.pushStyle(text_style);
- builder.addText(text);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(TestCanvasWidth);
- SkDEBUGCODE(auto impl = static_cast<ParagraphImpl*>(paragraph.get());)
- SkASSERT(impl->runs().size() == 3);
- SkASSERT(impl->runs()[0].textRange().end == impl->runs()[1].textRange().start);
- SkASSERT(impl->runs()[1].textRange().end == impl->runs()[2].textRange().start);
- }
- // 4 to 1
- DEF_TEST(SkParagraph_JSON1, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text = "👨👩👧👦";
- ParagraphStyle paragraph_style;
- paragraph_style.turnHintingOff();
- ParagraphBuilderImpl builder(paragraph_style, fontCollection);
- TextStyle text_style;
- text_style.setFontFamilies({SkString("Noto Color Emoji")});
- builder.pushStyle(text_style);
- builder.addText(text);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(TestCanvasWidth);
- auto impl = static_cast<ParagraphImpl*>(paragraph.get());
- REPORTER_ASSERT(reporter, impl->runs().size() == 1);
- auto run = impl->runs().front();
- auto cluster = 0;
- SkShaperJSONWriter::VisualizeClusters(
- text, 0, std::strlen(text), run.glyphs(), run.clusterIndexes(),
- [&](int codePointCount, SkSpan<const char> utf1to1, SkSpan<const SkGlyphID> glyph1to1) {
- if (cluster == 0) {
- std::string toCheckUtf8{utf1to1.data(), utf1to1.size()};
- SkASSERT(std::strcmp(text, utf1to1.data()) == 0);
- SkASSERT(glyph1to1.size() == 1);
- SkASSERT(*glyph1to1.begin() == 1611);
- }
- ++cluster;
- });
- SkASSERT(cluster <= 2);
- }
- // 5 to 3
- DEF_TEST(SkParagraph_JSON2, reporter) {
- sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>();
- if (!fontCollection->fontsFound()) return;
- const char* text = "p〠q";
- ParagraphStyle paragraph_style;
- paragraph_style.turnHintingOff();
- ParagraphBuilderImpl builder(paragraph_style, fontCollection);
- TextStyle text_style;
- text_style.setFontFamilies({SkString("Noto Sans CJK JP")});
- text_style.setColor(SK_ColorBLACK);
- text_style.setFontSize(50);
- builder.pushStyle(text_style);
- builder.addText(text);
- builder.pop();
- auto paragraph = builder.Build();
- paragraph->layout(TestCanvasWidth);
- auto impl = static_cast<ParagraphImpl*>(paragraph.get());
- REPORTER_ASSERT(reporter, impl->runs().size() == 1);
- auto run = impl->runs().front();
- auto cluster = 0;
- for (auto& run : impl->runs()) {
- SkShaperJSONWriter::VisualizeClusters(
- impl->text().begin() + run.textRange().start, 0, run.textRange().width(), run.glyphs(), run.clusterIndexes(),
- [&](int codePointCount, SkSpan<const char> utf1to1,
- SkSpan<const SkGlyphID> glyph1to1) {
- if (cluster == 0) {
- std::string toCheckUtf8{utf1to1.data(), utf1to1.size()};
- SkASSERT(std::strcmp(text, utf1to1.data()) == 0);
- SkASSERT(glyph1to1.size() == 3);
- }
- ++cluster;
- });
- }
- SkASSERT(cluster <= 2);
- }
|