epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 7 | #include "samplecode/Sample.h" |
Mike Reed | 193d3a0 | 2018-02-08 15:45:51 -0500 | [diff] [blame] | 8 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkColorFilter.h" |
| 11 | #include "include/core/SkColorPriv.h" |
| 12 | #include "include/core/SkGraphics.h" |
| 13 | #include "include/core/SkPath.h" |
| 14 | #include "include/core/SkRegion.h" |
| 15 | #include "include/core/SkShader.h" |
| 16 | #include "include/core/SkStream.h" |
| 17 | #include "include/core/SkTextBlob.h" |
| 18 | #include "include/core/SkTime.h" |
| 19 | #include "include/core/SkTypeface.h" |
| 20 | #include "include/effects/SkBlurMaskFilter.h" |
| 21 | #include "include/effects/SkGradientShader.h" |
| 22 | #include "include/utils/SkRandom.h" |
| 23 | #include "modules/skshaper/include/SkShaper.h" |
| 24 | #include "src/core/SkOSFile.h" |
| 25 | #include "src/shaders/SkColorShader.h" |
| 26 | #include "src/utils/SkUTF.h" |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 27 | |
reed@google.com | d6f5a81 | 2011-03-02 14:34:11 +0000 | [diff] [blame] | 28 | static const char gText[] = |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 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."; |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 35 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 36 | class TextBoxView : public Sample { |
reed@google.com | d6f5a81 | 2011-03-02 14:34:11 +0000 | [diff] [blame] | 37 | public: |
Ben Wagner | b059194 | 2019-02-15 14:46:18 -0500 | [diff] [blame] | 38 | TextBoxView() : fShaper(SkShaper::Make()) {} |
reed@google.com | d6f5a81 | 2011-03-02 14:34:11 +0000 | [diff] [blame] | 39 | |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 40 | protected: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 41 | bool onQuery(Sample::Event* evt) override { |
| 42 | if (Sample::TitleQ(*evt)) { |
| 43 | Sample::TitleR(evt, "TextBox"); |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 44 | return true; |
| 45 | } |
| 46 | return this->INHERITED::onQuery(evt); |
| 47 | } |
reed@google.com | d6f5a81 | 2011-03-02 14:34:11 +0000 | [diff] [blame] | 48 | |
reed@google.com | 021b405 | 2011-09-28 15:24:00 +0000 | [diff] [blame] | 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); |
Mike Reed | 193d3a0 | 2018-02-08 15:45:51 -0500 | [diff] [blame] | 54 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 55 | SkScalar margin = 20; |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 56 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 57 | SkPaint paint; |
reed@google.com | 021b405 | 2011-09-28 15:24:00 +0000 | [diff] [blame] | 58 | paint.setColor(fg); |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 59 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 60 | for (int i = 9; i < 24; i += 2) { |
Ben Wagner | 3bdb69c | 2019-04-01 19:01:09 -0400 | [diff] [blame] | 61 | SkTextBlobBuilderRunHandler builder(gText, { margin, margin }); |
Hal Canary | 4484b8f | 2019-01-08 14:00:08 -0500 | [diff] [blame] | 62 | SkFont font(nullptr, SkIntToScalar(i)); |
| 63 | font.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
| 64 | |
Ben Wagner | 3bdb69c | 2019-04-01 19:01:09 -0400 | [diff] [blame] | 65 | fShaper->shape(gText, strlen(gText), font, true, w - margin, &builder); |
Florin Malita | 9867f61 | 2018-12-12 10:54:49 -0500 | [diff] [blame] | 66 | canvas->drawTextBlob(builder.makeBlob(), 0, 0, paint); |
Mike Reed | 193d3a0 | 2018-02-08 15:45:51 -0500 | [diff] [blame] | 67 | |
Ben Wagner | 3bdb69c | 2019-04-01 19:01:09 -0400 | [diff] [blame] | 68 | canvas->translate(0, builder.endPoint().y()); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 69 | } |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 70 | } |
reed@google.com | d6f5a81 | 2011-03-02 14:34:11 +0000 | [diff] [blame] | 71 | |
Mike Reed | 193d3a0 | 2018-02-08 15:45:51 -0500 | [diff] [blame] | 72 | void onDrawContent(SkCanvas* canvas) override { |
reed@google.com | 021b405 | 2011-09-28 15:24:00 +0000 | [diff] [blame] | 73 | SkScalar width = this->width() / 3; |
| 74 | drawTest(canvas, width, this->height(), SK_ColorBLACK, SK_ColorWHITE); |
| 75 | canvas->translate(width, 0); |
| 76 | drawTest(canvas, width, this->height(), SK_ColorWHITE, SK_ColorBLACK); |
| 77 | canvas->translate(width, 0); |
| 78 | drawTest(canvas, width, this->height()/2, SK_ColorGRAY, SK_ColorWHITE); |
| 79 | canvas->translate(0, this->height()/2); |
| 80 | drawTest(canvas, width, this->height()/2, SK_ColorGRAY, SK_ColorBLACK); |
| 81 | } |
| 82 | |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 83 | private: |
Ben Wagner | b059194 | 2019-02-15 14:46:18 -0500 | [diff] [blame] | 84 | std::unique_ptr<SkShaper> fShaper; |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 85 | typedef Sample INHERITED; |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | ////////////////////////////////////////////////////////////////////////////// |
| 89 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 90 | DEF_SAMPLE( return new TextBoxView(); ) |