1 // Copyright 2021 Google LLC. 2 3 #include "experimental/sktext/include/Processor.h" 4 #include "include/core/SkCanvas.h" 5 #include "include/core/SkColorFilter.h" 6 #include "include/core/SkFontMgr.h" 7 #include "include/core/SkGraphics.h" 8 #include "include/core/SkPath.h" 9 #include "include/core/SkRegion.h" 10 #include "include/core/SkShader.h" 11 #include "include/core/SkStream.h" 12 #include "include/core/SkTime.h" 13 #include "samplecode/Sample.h" 14 #include "src/core/SkOSFile.h" 15 #include "src/shaders/SkColorShader.h" 16 #include "src/utils/SkOSPath.h" 17 #include "src/utils/SkUTF.h" 18 #include "tools/Resources.h" 19 #include "tools/flags/CommandLineFlags.h" 20 21 using namespace skia::text; 22 23 namespace { 24 class TextSample_HelloWorld : public Sample { 25 protected: name()26 SkString name() override { return SkString("TextSample_HelloWorld"); } 27 onDrawContent(SkCanvas * canvas)28 void onDrawContent(SkCanvas* canvas) override { 29 canvas->drawColor(SK_ColorWHITE); 30 Processor::drawText("Hello word", canvas, 0, 0); 31 } 32 33 private: 34 using INHERITED = Sample; 35 }; 36 37 class TextSample_Align_Dir : public Sample { 38 39 public: TextSample_Align_Dir()40 TextSample_Align_Dir() : fUnicode(SkUnicode::Make()) { } 41 protected: name()42 SkString name() override { return SkString("TextSample_Align_Dir"); } 43 drawLine(SkCanvas * canvas,SkScalar w,SkScalar h,const std::string & text,TextAlign align,TextDirection direction=TextDirection::kLtr)44 void drawLine(SkCanvas* canvas, SkScalar w, SkScalar h, 45 const std::string& text, 46 TextAlign align, 47 TextDirection direction = TextDirection::kLtr) { 48 SkColor background = SK_ColorGRAY; 49 const std::u16string& ellipsis = u"\u2026"; 50 SkScalar margin = 20; 51 52 SkAutoCanvasRestore acr(canvas, true); 53 54 canvas->clipRect(SkRect::MakeWH(w, h)); 55 canvas->drawColor(SK_ColorWHITE); 56 57 Processor::drawText(direction == TextDirection::kRtl ? mirror(text).c_str() : normal(text).c_str(), 58 canvas, 59 TextFormatStyle(align, direction), 60 SK_ColorBLACK, SK_ColorLTGRAY, 61 SkString("Roboto"), 12.0f, SkFontStyle::Normal(), 62 0, 0); 63 } 64 mirror(const std::string & text)65 SkString mirror(const std::string& text) { 66 std::u16string result; 67 result += u"\u202E"; 68 //for (auto i = text.size(); i > 0; --i) { 69 // result += text[i - 1]; 70 //} 71 for (auto ch : text) { 72 result += ch; 73 } 74 result += u"\u202C"; 75 return fUnicode->convertUtf16ToUtf8(result); 76 } 77 normal(const std::string & text)78 SkString normal(const std::string& text) { 79 std::u16string result; 80 result += u"\u202D"; 81 for (auto ch : text) { 82 result += ch; 83 } 84 result += u"\u202C"; 85 return fUnicode->convertUtf16ToUtf8(result); 86 } 87 onDrawContent(SkCanvas * canvas)88 void onDrawContent(SkCanvas* canvas) override { 89 90 canvas->drawColor(SK_ColorDKGRAY); 91 SkScalar width = this->width() / 4; 92 SkScalar height = this->height() / 2; 93 94 const std::string line = "One line of text"; 95 96 drawLine(canvas, width, height, line, TextAlign::kLeft, TextDirection::kLtr); 97 canvas->translate(width, 0); 98 drawLine(canvas, width, height, line, TextAlign::kRight, TextDirection::kLtr); 99 canvas->translate(width, 0); 100 drawLine(canvas, width, height, line, TextAlign::kCenter, TextDirection::kLtr); 101 canvas->translate(width, 0); 102 drawLine(canvas, width, height, line, TextAlign::kJustify, TextDirection::kLtr); 103 canvas->translate(-width * 3, height); 104 105 drawLine(canvas, width, height, line, TextAlign::kLeft, TextDirection::kRtl); 106 canvas->translate(width, 0); 107 drawLine(canvas, width, height, line, TextAlign::kRight, TextDirection::kRtl); 108 canvas->translate(width, 0); 109 drawLine(canvas, width, height, line, TextAlign::kCenter, TextDirection::kRtl); 110 canvas->translate(width, 0); 111 drawLine(canvas, width, height, line, TextAlign::kJustify, TextDirection::kRtl); 112 canvas->translate(width, 0); 113 } 114 115 private: 116 using INHERITED = Sample; 117 std::unique_ptr<SkUnicode> fUnicode; 118 }; 119 120 class TextSample_LongLTR : public Sample { 121 protected: name()122 SkString name() override { return SkString("TextSample_LongLTR"); } 123 onDrawContent(SkCanvas * canvas)124 void onDrawContent(SkCanvas* canvas) override { 125 canvas->drawColor(SK_ColorWHITE); 126 Processor::drawText("A very_very_very_very_very_very_very_very_very_very " 127 "very_very_very_very_very_very_very_very_very_very very very very very very very " 128 "very very very very very very very very very very very very very very very very " 129 "very very very very very very very very very very very very very long text", canvas, this->width()); 130 131 } 132 133 private: 134 using INHERITED = Sample; 135 std::unique_ptr<SkUnicode> fUnicode; 136 }; 137 138 class TextSample_LongRTL : public Sample { 139 protected: name()140 SkString name() override { return SkString("TextSample_LongRTL"); } 141 mirror(const std::string & text)142 SkString mirror(const std::string& text) { 143 std::u16string result; 144 result += u"\u202E"; 145 for (auto i = text.size(); i > 0; --i) { 146 result += text[i - 1]; 147 } 148 for (auto ch : text) { 149 result += ch; 150 } 151 result += u"\u202C"; 152 return fUnicode->convertUtf16ToUtf8(result); 153 } 154 onDrawContent(SkCanvas * canvas)155 void onDrawContent(SkCanvas* canvas) override { 156 canvas->drawColor(SK_ColorWHITE); 157 Processor::drawText("LONG MIRRORED TEXT SHOULD SHOW RIGHT TO LEFT (AS NORMAL)", canvas, 0, 0); 158 } 159 160 private: 161 using INHERITED = Sample; 162 std::unique_ptr<SkUnicode> fUnicode; 163 }; 164 } // namespace 165 166 DEF_SAMPLE(return new TextSample_HelloWorld();) 167 DEF_SAMPLE(return new TextSample_Align_Dir();) 168 DEF_SAMPLE(return new TextSample_LongLTR();) 169 DEF_SAMPLE(return new TextSample_LongRTL();) 170