• 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(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 ((false)) {
142                 if (info->utf8Starts) {
143                     SkString str;
144                     for (int i = 0; i < info->count; ++i) {
145                         str.appendUnichar(gSpeach[info->utf8Starts[i]]);
146                     }
147                     SkDebugf("'%s'\n", str.c_str());
148                 }
149 
150                 // show position points
151                 for (int i = 0; i < info->count; ++i) {
152                     auto pos = info->positions[i];
153                     canvas->drawPoint(pos.fX + info->origin.fX, pos.fY + info->origin.fY, p2);
154                 }
155             }
156         });
157     }
158 
onDraw(SkCanvas * canvas,SkString * errorMsg)159     DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
160         if (nullptr == fPara) {
161             return DrawResult::kSkip;
162         }
163 
164         if (fFlags & kShowVisitor) {
165             canvas->clear(SK_ColorWHITE);
166             fPara->layout(400);
167             fPara->paint(canvas, 10, 10);
168             canvas->translate(400+10, 10);
169             this->drawFromVisitor(canvas, fPara.get());
170             return DrawResult::kOk;
171         }
172 
173         const int loop = (this->getMode() == kGM_Mode) ? 1 : 50;
174 
175         int parity = 0;
176         for (int i = 0; i < loop; ++i) {
177             SkAutoCanvasRestore acr(canvas, true);
178 
179             if (fFlags & kTimeLayout) {
180                 fPara->layout(400 + parity);
181                 parity = (parity + 1) & 1;
182             }
183             fPara->paint(canvas, 10, 10);
184         }
185         // clean up if we've been looping
186         if (loop > 1) {
187             canvas->clear(SK_ColorWHITE);
188             fPara->layout(400);
189             fPara->paint(canvas, 10, 10);
190         }
191 
192         if ((this->getMode() == kGM_Mode) && (fFlags & kTimeLayout)) {
193             return DrawResult::kSkip;
194         }
195         return DrawResult::kOk;
196     }
197 
runAsBench() const198     bool runAsBench() const override { return true; }
199 
onAnimate(double)200     bool onAnimate(double /*nanos*/) override {
201         return false;
202     }
203 
204 private:
205     using INHERITED = skiagm::GM;
206 };
207 DEF_GM(return new ParagraphGM(0);)
208 DEF_GM(return new ParagraphGM(kTimeLayout);)
209 DEF_GM(return new ParagraphGM(kUseUnderline);)
210 DEF_GM(return new ParagraphGM(kShowVisitor);)
211 DEF_GM(return new ParagraphGM(kShowVisitor | kUseUnderline);)
212