• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(
62                 paraStyle, collection, SkUnicode::Make());
63         if (nullptr == builder) {
64             fPara = nullptr;
65             return;
66         }
67 
68         builder->addText(gSpeach, strlen(gSpeach));
69 
70         fPara = builder->Build();
71         fPara->layout(400);
72     }
73 
74 protected:
onOnceBeforeDraw()75     void onOnceBeforeDraw() override {
76         this->buildParagraph();
77     }
78 
onShortName()79     SkString onShortName() override {
80         SkString name;
81         name.printf("paragraph%s_%s",
82                     fFlags & kTimeLayout   ? "_layout"    : "",
83                     fFlags & kUseUnderline ? "_underline" : "");
84         if (fFlags & kShowVisitor) {
85             name.append("_visitor");
86         }
87         return name;
88     }
89 
onISize()90     SkISize onISize() override {
91         if (fFlags & kShowVisitor) {
92             return SkISize::Make(810, 420);
93         }
94         return SkISize::Make(412, 420);
95     }
96 
drawFromVisitor(SkCanvas * canvas,skia::textlayout::Paragraph * para) const97     void drawFromVisitor(SkCanvas* canvas, skia::textlayout::Paragraph* para) const {
98         SkPaint p, p2;
99         p.setColor(0xFF0000FF);
100         p2.setColor(0xFFFF0000);
101         p2.setStrokeWidth(4);
102         p2.setStrokeCap(SkPaint::kSquare_Cap);
103         SkPaint underp;
104         underp.setStroke(true);
105         underp.setStrokeWidth(2);
106         underp.setAntiAlias(true);
107         underp.setColor(p.getColor());
108         const SkScalar GAP = 2;
109 
110         para->visit([&](int, const skia::textlayout::Paragraph::VisitorInfo* info) {
111             if (!info) {
112                 return;
113             }
114             canvas->drawGlyphs(info->count, info->glyphs, info->positions, info->origin,
115                                info->font, p);
116 
117             if (fFlags & kUseUnderline) {
118                 // Need to modify positions to roll-in the orign
119                 std::vector<SkPoint> pos;
120                 for (int i = 0; i < info->count; ++i) {
121                     pos.push_back({info->origin.fX + info->positions[i].fX,
122                                    info->origin.fY + info->positions[i].fY});
123                 }
124 
125                 const SkScalar X0 = pos[0].fX;
126                 const SkScalar X1 = X0 + info->advanceX;
127                 const SkScalar Y  = pos[0].fY;
128                 auto sects = info->font.getIntercepts(info->glyphs, info->count, pos.data(),
129                                                       Y+1, Y+3);
130 
131                 SkScalar x0 = X0;
132                 for (size_t i = 0; i < sects.size(); i += 2) {
133                     SkScalar x1 = sects[i] - GAP;
134                     if (x0 < x1) {
135                         canvas->drawLine(x0, Y+2, x1, Y+2, underp);
136                     }
137                     x0 = sects[i+1] + GAP;
138                 }
139                 canvas->drawLine(x0, Y+2, X1, Y+2, underp);
140             }
141 
142             if ((false)) {
143                 if (info->utf8Starts) {
144                     SkString str;
145                     for (int i = 0; i < info->count; ++i) {
146                         str.appendUnichar(gSpeach[info->utf8Starts[i]]);
147                     }
148                     SkDebugf("'%s'\n", str.c_str());
149                 }
150 
151                 // show position points
152                 for (int i = 0; i < info->count; ++i) {
153                     auto pos = info->positions[i];
154                     canvas->drawPoint(pos.fX + info->origin.fX, pos.fY + info->origin.fY, p2);
155                 }
156             }
157         });
158     }
159 
onDraw(SkCanvas * canvas,SkString * errorMsg)160     DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
161         if (nullptr == fPara) {
162             return DrawResult::kSkip;
163         }
164 
165         if (fFlags & kShowVisitor) {
166             canvas->clear(SK_ColorWHITE);
167             fPara->layout(400);
168             fPara->paint(canvas, 10, 10);
169             canvas->translate(400+10, 10);
170             this->drawFromVisitor(canvas, fPara.get());
171             return DrawResult::kOk;
172         }
173 
174         const int loop = (this->getMode() == kGM_Mode) ? 1 : 50;
175 
176         int parity = 0;
177         for (int i = 0; i < loop; ++i) {
178             SkAutoCanvasRestore acr(canvas, true);
179 
180             if (fFlags & kTimeLayout) {
181                 fPara->layout(400 + parity);
182                 parity = (parity + 1) & 1;
183             }
184             fPara->paint(canvas, 10, 10);
185         }
186         // clean up if we've been looping
187         if (loop > 1) {
188             canvas->clear(SK_ColorWHITE);
189             fPara->layout(400);
190             fPara->paint(canvas, 10, 10);
191         }
192 
193         if ((this->getMode() == kGM_Mode) && (fFlags & kTimeLayout)) {
194             return DrawResult::kSkip;
195         }
196         return DrawResult::kOk;
197     }
198 
runAsBench() const199     bool runAsBench() const override { return true; }
200 
onAnimate(double)201     bool onAnimate(double /*nanos*/) override {
202         return false;
203     }
204 
205 private:
206     using INHERITED = skiagm::GM;
207 };
208 DEF_GM(return new ParagraphGM(0);)
209 DEF_GM(return new ParagraphGM(kTimeLayout);)
210 DEF_GM(return new ParagraphGM(kUseUnderline);)
211 DEF_GM(return new ParagraphGM(kShowVisitor);)
212 DEF_GM(return new ParagraphGM(kShowVisitor | kUseUnderline);)
213