1 /* 2 * Copyright 2020 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #include "gm/gm.h" 9 #include "include/core/SkCanvas.h" 10 #include "include/core/SkColor.h" 11 #include "include/core/SkFont.h" 12 #include "include/core/SkFontTypes.h" 13 #include "include/core/SkPaint.h" 14 #include "include/core/SkPoint.h" 15 #include "include/core/SkRect.h" 16 #include "include/core/SkScalar.h" 17 #include "include/core/SkShader.h" 18 #include "include/core/SkSize.h" 19 #include "include/core/SkString.h" 20 #include "include/core/SkTypeface.h" 21 #include "tools/ToolUtils.h" 22 23 #include "modules/skparagraph/include/Paragraph.h" 24 #include "modules/skparagraph/src/ParagraphBuilderImpl.h" 25 26 static const char* gSpeach = "Five score years ago, a great American, in whose symbolic shadow we stand today, signed the Emancipation Proclamation. This momentous decree came as a great beacon light of hope to millions of Negro slaves who had been seared in the flames of withering injustice. It came as a joyous daybreak to end the long night of their captivity."; 27 28 namespace { 29 enum ParaFlags { 30 kTimeLayout = 1 << 0, 31 kUseUnderline = 1 << 1, 32 kShowVisitor = 1 << 2, 33 }; 34 } // namespace 35 36 class ParagraphGM : public skiagm::GM { 37 std::unique_ptr<skia::textlayout::Paragraph> fPara; 38 const unsigned fFlags; 39 40 public: ParagraphGM(unsigned flags)41 ParagraphGM(unsigned flags) : fFlags(flags) {} 42 buildParagraph()43 void buildParagraph() { 44 skia::textlayout::TextStyle style; 45 style.setForegroundColor(SkPaint()); 46 style.setFontFamilies({SkString("sans-serif")}); 47 style.setFontSize(30); 48 49 if (fFlags & kUseUnderline) { 50 style.setDecoration(skia::textlayout::TextDecoration::kUnderline); 51 style.setDecorationMode(skia::textlayout::TextDecorationMode::kThrough); 52 style.setDecorationColor(SK_ColorBLACK); 53 style.setDecorationThicknessMultiplier(2); 54 } 55 56 skia::textlayout::ParagraphStyle paraStyle; 57 paraStyle.setTextStyle(style); 58 59 auto collection = sk_make_sp<skia::textlayout::FontCollection>(); 60 collection->setDefaultFontManager(SkFontMgr::RefDefault()); 61 auto builder = skia::textlayout::ParagraphBuilderImpl::make(paraStyle, collection); 62 if (nullptr == builder) { 63 fPara = nullptr; 64 return; 65 } 66 67 builder->addText(gSpeach, strlen(gSpeach)); 68 69 fPara = builder->Build(); 70 fPara->layout(400); 71 } 72 73 protected: onOnceBeforeDraw()74 void onOnceBeforeDraw() override { 75 this->buildParagraph(); 76 } 77 onShortName()78 SkString onShortName() override { 79 SkString name; 80 name.printf("paragraph%s_%s", 81 fFlags & kTimeLayout ? "_layout" : "", 82 fFlags & kUseUnderline ? "_underline" : ""); 83 if (fFlags & kShowVisitor) { 84 name.append("_visitor"); 85 } 86 return name; 87 } 88 onISize()89 SkISize onISize() override { 90 if (fFlags & kShowVisitor) { 91 return SkISize::Make(810, 420); 92 } 93 return SkISize::Make(412, 420); 94 } 95 drawFromVisitor(SkCanvas * canvas,skia::textlayout::Paragraph * para) const96 void drawFromVisitor(SkCanvas* canvas, skia::textlayout::Paragraph* para) const { 97 SkPaint p, p2; 98 p.setColor(0xFF0000FF); 99 p2.setColor(0xFFFF0000); 100 p2.setStrokeWidth(4); 101 p2.setStrokeCap(SkPaint::kSquare_Cap); 102 SkPaint underp; 103 underp.setStroke(true); 104 underp.setStrokeWidth(2); 105 underp.setAntiAlias(true); 106 underp.setColor(p.getColor()); 107 const SkScalar GAP = 2; 108 109 para->visit([&](int, const skia::textlayout::Paragraph::VisitorInfo* info) { 110 if (!info) { 111 return; 112 } 113 canvas->drawGlyphs(info->count, info->glyphs, info->positions, info->origin, 114 info->font, p); 115 116 if (fFlags & kUseUnderline) { 117 // Need to modify positions to roll-in the orign 118 std::vector<SkPoint> pos; 119 for (int i = 0; i < info->count; ++i) { 120 pos.push_back({info->origin.fX + info->positions[i].fX, 121 info->origin.fY + info->positions[i].fY}); 122 } 123 124 const SkScalar X0 = pos[0].fX; 125 const SkScalar X1 = X0 + info->advanceX; 126 const SkScalar Y = pos[0].fY; 127 auto sects = info->font.getIntercepts(info->glyphs, info->count, pos.data(), 128 Y+1, Y+3); 129 130 SkScalar x0 = X0; 131 for (size_t i = 0; i < sects.size(); i += 2) { 132 SkScalar x1 = sects[i] - GAP; 133 if (x0 < x1) { 134 canvas->drawLine(x0, Y+2, x1, Y+2, underp); 135 } 136 x0 = sects[i+1] + GAP; 137 } 138 canvas->drawLine(x0, Y+2, X1, Y+2, underp); 139 } 140 141 if (info->utf8Starts && false) { 142 SkString str; 143 for (int i = 0; i < info->count; ++i) { 144 str.appendUnichar(gSpeach[info->utf8Starts[i]]); 145 } 146 SkDebugf("'%s'\n", str.c_str()); 147 } 148 149 if (false) { // show position points 150 for (int i = 0; i < info->count; ++i) { 151 auto pos = info->positions[i]; 152 canvas->drawPoint(pos.fX + info->origin.fX, pos.fY + info->origin.fY, p2); 153 } 154 } 155 }); 156 } 157 onDraw(SkCanvas * canvas,SkString * errorMsg)158 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override { 159 if (nullptr == fPara) { 160 return DrawResult::kSkip; 161 } 162 163 if (fFlags & kShowVisitor) { 164 canvas->clear(SK_ColorWHITE); 165 fPara->layout(400); 166 fPara->paint(canvas, 10, 10); 167 canvas->translate(400+10, 10); 168 this->drawFromVisitor(canvas, fPara.get()); 169 return DrawResult::kOk; 170 } 171 172 const int loop = (this->getMode() == kGM_Mode) ? 1 : 50; 173 174 int parity = 0; 175 for (int i = 0; i < loop; ++i) { 176 SkAutoCanvasRestore acr(canvas, true); 177 178 if (fFlags & kTimeLayout) { 179 fPara->layout(400 + parity); 180 parity = (parity + 1) & 1; 181 } 182 fPara->paint(canvas, 10, 10); 183 } 184 // clean up if we've been looping 185 if (loop > 1) { 186 canvas->clear(SK_ColorWHITE); 187 fPara->layout(400); 188 fPara->paint(canvas, 10, 10); 189 } 190 191 if ((this->getMode() == kGM_Mode) && (fFlags & kTimeLayout)) { 192 return DrawResult::kSkip; 193 } 194 return DrawResult::kOk; 195 } 196 runAsBench() const197 bool runAsBench() const override { return true; } 198 onAnimate(double)199 bool onAnimate(double /*nanos*/) override { 200 return false; 201 } 202 203 private: 204 using INHERITED = skiagm::GM; 205 }; 206 DEF_GM(return new ParagraphGM(0);) 207 DEF_GM(return new ParagraphGM(kTimeLayout);) 208 DEF_GM(return new ParagraphGM(kUseUnderline);) 209 DEF_GM(return new ParagraphGM(kShowVisitor);) 210 DEF_GM(return new ParagraphGM(kShowVisitor | kUseUnderline);) 211