• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2011 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 #include "Sample.h"
8 
9 #include "SkBlurMaskFilter.h"
10 #include "SkCanvas.h"
11 #include "SkColorFilter.h"
12 #include "SkColorPriv.h"
13 #include "SkColorShader.h"
14 #include "SkGradientShader.h"
15 #include "SkGraphics.h"
16 #include "SkOSFile.h"
17 #include "SkPath.h"
18 #include "SkRandom.h"
19 #include "SkRegion.h"
20 #include "SkShader.h"
21 #include "SkShaper.h"
22 #include "SkStream.h"
23 #include "SkTextBlob.h"
24 #include "SkTime.h"
25 #include "SkTypeface.h"
26 #include "SkUTF.h"
27 
28 static const char gText[] =
29     "When in the Course of human events it becomes necessary for one people "
30     "to dissolve the political bands which have connected them with another "
31     "and to assume among the powers of the earth, the separate and equal "
32     "station to which the Laws of Nature and of Nature's God entitle them, "
33     "a decent respect to the opinions of mankind requires that they should "
34     "declare the causes which impel them to the separation.";
35 
36 class TextBoxView : public Sample {
37 public:
TextBoxView()38     TextBoxView() : fShaper(SkShaper::Make()) {}
39 
40 protected:
onQuery(Sample::Event * evt)41     bool onQuery(Sample::Event* evt) override {
42         if (Sample::TitleQ(*evt)) {
43             Sample::TitleR(evt, "TextBox");
44             return true;
45         }
46         return this->INHERITED::onQuery(evt);
47     }
48 
drawTest(SkCanvas * canvas,SkScalar w,SkScalar h,SkColor fg,SkColor bg)49     void drawTest(SkCanvas* canvas, SkScalar w, SkScalar h, SkColor fg, SkColor bg) {
50         SkAutoCanvasRestore acr(canvas, true);
51 
52         canvas->clipRect(SkRect::MakeWH(w, h));
53         canvas->drawColor(bg);
54 
55         SkScalar margin = 20;
56 
57         SkPaint paint;
58         paint.setColor(fg);
59 
60         for (int i = 9; i < 24; i += 2) {
61             SkTextBlobBuilderRunHandler builder(gText);
62             SkFont font(nullptr, SkIntToScalar(i));
63             font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
64 
65             SkPoint end = fShaper->shape(&builder, font, gText, strlen(gText), true,
66                                          { margin, margin }, w - margin);
67             canvas->drawTextBlob(builder.makeBlob(), 0, 0, paint);
68 
69             canvas->translate(0, end.y());
70         }
71     }
72 
onDrawContent(SkCanvas * canvas)73     void onDrawContent(SkCanvas* canvas) override {
74         SkScalar width = this->width() / 3;
75         drawTest(canvas, width, this->height(), SK_ColorBLACK, SK_ColorWHITE);
76         canvas->translate(width, 0);
77         drawTest(canvas, width, this->height(), SK_ColorWHITE, SK_ColorBLACK);
78         canvas->translate(width, 0);
79         drawTest(canvas, width, this->height()/2, SK_ColorGRAY, SK_ColorWHITE);
80         canvas->translate(0, this->height()/2);
81         drawTest(canvas, width, this->height()/2, SK_ColorGRAY, SK_ColorBLACK);
82     }
83 
84 private:
85     std::unique_ptr<SkShaper> fShaper;
86     typedef Sample INHERITED;
87 };
88 
89 //////////////////////////////////////////////////////////////////////////////
90 
91 DEF_SAMPLE( return new TextBoxView(); )
92