blob: 7aa159808bcd9c13c811a1754849d9dbd3fd1065 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05007#include "samplecode/Sample.h"
Mike Reed193d3a02018-02-08 15:45:51 -05008
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#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.com0bb6d062010-05-17 14:50:04 +000027
reed@google.comd6f5a812011-03-02 14:34:11 +000028static const char gText[] =
rmistry@google.comae933ce2012-08-23 18:19:56 +000029 "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.com0bb6d062010-05-17 14:50:04 +000035
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040036class TextBoxView : public Sample {
reed@google.comd6f5a812011-03-02 14:34:11 +000037public:
Ben Wagnerb0591942019-02-15 14:46:18 -050038 TextBoxView() : fShaper(SkShaper::Make()) {}
reed@google.comd6f5a812011-03-02 14:34:11 +000039
reed@android.com0bb6d062010-05-17 14:50:04 +000040protected:
Hal Canary8a027312019-07-03 10:55:44 -040041 SkString name() override { return SkString("TextBox"); }
reed@google.comd6f5a812011-03-02 14:34:11 +000042
reed@google.com021b4052011-09-28 15:24:00 +000043 void drawTest(SkCanvas* canvas, SkScalar w, SkScalar h, SkColor fg, SkColor bg) {
44 SkAutoCanvasRestore acr(canvas, true);
45
46 canvas->clipRect(SkRect::MakeWH(w, h));
47 canvas->drawColor(bg);
Mike Reed193d3a02018-02-08 15:45:51 -050048
rmistry@google.comae933ce2012-08-23 18:19:56 +000049 SkScalar margin = 20;
reed@android.com0bb6d062010-05-17 14:50:04 +000050
rmistry@google.comae933ce2012-08-23 18:19:56 +000051 SkPaint paint;
reed@google.com021b4052011-09-28 15:24:00 +000052 paint.setColor(fg);
reed@android.com0bb6d062010-05-17 14:50:04 +000053
rmistry@google.comae933ce2012-08-23 18:19:56 +000054 for (int i = 9; i < 24; i += 2) {
Ben Wagner3bdb69c2019-04-01 19:01:09 -040055 SkTextBlobBuilderRunHandler builder(gText, { margin, margin });
Ben Wagner1ca50522019-10-01 17:54:28 -040056 SkFont srcFont(nullptr, SkIntToScalar(i));
57 srcFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
Hal Canary4484b8f2019-01-08 14:00:08 -050058
Ben Wagner1ca50522019-10-01 17:54:28 -040059 const char* utf8 = gText;
60 size_t utf8Bytes = sizeof(gText) - 1;
61
62 std::unique_ptr<SkShaper::BiDiRunIterator> bidi(
63 SkShaper::MakeBiDiRunIterator(utf8, utf8Bytes, 0xfe));
64 if (!bidi) {
65 return;
66 }
67
68 std::unique_ptr<SkShaper::LanguageRunIterator> language(
69 SkShaper::MakeStdLanguageRunIterator(utf8, utf8Bytes));
70 if (!language) {
71 return;
72 }
73
74 SkFourByteTag undeterminedScript = SkSetFourByteTag('Z','y','y','y');
75 std::unique_ptr<SkShaper::ScriptRunIterator> script(
76 SkShaper::MakeScriptRunIterator(utf8, utf8Bytes, undeterminedScript));
77 if (!script) {
78 return;
79 }
80
81 std::unique_ptr<SkShaper::FontRunIterator> font(
82 SkShaper::MakeFontMgrRunIterator(utf8, utf8Bytes, srcFont, SkFontMgr::RefDefault(),
83 "Arial", SkFontStyle::Bold(), &*language));
84 if (!font) {
85 return;
86 }
87
88 fShaper->shape(utf8, utf8Bytes, *font, *bidi, *script, *language, w - margin, &builder);
Florin Malita9867f612018-12-12 10:54:49 -050089 canvas->drawTextBlob(builder.makeBlob(), 0, 0, paint);
Mike Reed193d3a02018-02-08 15:45:51 -050090
Ben Wagner3bdb69c2019-04-01 19:01:09 -040091 canvas->translate(0, builder.endPoint().y());
rmistry@google.comae933ce2012-08-23 18:19:56 +000092 }
reed@android.com0bb6d062010-05-17 14:50:04 +000093 }
reed@google.comd6f5a812011-03-02 14:34:11 +000094
Mike Reed193d3a02018-02-08 15:45:51 -050095 void onDrawContent(SkCanvas* canvas) override {
reed@google.com021b4052011-09-28 15:24:00 +000096 SkScalar width = this->width() / 3;
97 drawTest(canvas, width, this->height(), SK_ColorBLACK, SK_ColorWHITE);
98 canvas->translate(width, 0);
99 drawTest(canvas, width, this->height(), SK_ColorWHITE, SK_ColorBLACK);
100 canvas->translate(width, 0);
101 drawTest(canvas, width, this->height()/2, SK_ColorGRAY, SK_ColorWHITE);
102 canvas->translate(0, this->height()/2);
103 drawTest(canvas, width, this->height()/2, SK_ColorGRAY, SK_ColorBLACK);
104 }
105
reed@android.com0bb6d062010-05-17 14:50:04 +0000106private:
Ben Wagnerb0591942019-02-15 14:46:18 -0500107 std::unique_ptr<SkShaper> fShaper;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400108 typedef Sample INHERITED;
reed@android.com0bb6d062010-05-17 14:50:04 +0000109};
110
111//////////////////////////////////////////////////////////////////////////////
112
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400113DEF_SAMPLE( return new TextBoxView(); )