blob: 1d37b1989bf23f883974b175d19b590571f81c11 [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 */
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04007#include "Sample.h"
Mike Reed193d3a02018-02-08 15:45:51 -05008
reed@android.com0bb6d062010-05-17 14:50:04 +00009#include "SkBlurMaskFilter.h"
10#include "SkCanvas.h"
Mike Reed193d3a02018-02-08 15:45:51 -050011#include "SkColorFilter.h"
12#include "SkColorPriv.h"
reed@google.com021b4052011-09-28 15:24:00 +000013#include "SkColorShader.h"
reed@android.com0bb6d062010-05-17 14:50:04 +000014#include "SkGradientShader.h"
15#include "SkGraphics.h"
Mike Reed193d3a02018-02-08 15:45:51 -050016#include "SkOSFile.h"
reed@android.com0bb6d062010-05-17 14:50:04 +000017#include "SkPath.h"
18#include "SkRandom.h"
19#include "SkRegion.h"
20#include "SkShader.h"
Mike Reed193d3a02018-02-08 15:45:51 -050021#include "SkShaper.h"
22#include "SkStream.h"
23#include "SkTextBlob.h"
reed@android.com0bb6d062010-05-17 14:50:04 +000024#include "SkTime.h"
25#include "SkTypeface.h"
Hal Canaryea60b952018-08-21 11:45:46 -040026#include "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:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040041 bool onQuery(Sample::Event* evt) override {
42 if (Sample::TitleQ(*evt)) {
43 Sample::TitleR(evt, "TextBox");
reed@android.com0bb6d062010-05-17 14:50:04 +000044 return true;
45 }
46 return this->INHERITED::onQuery(evt);
47 }
reed@google.comd6f5a812011-03-02 14:34:11 +000048
reed@google.com021b4052011-09-28 15:24:00 +000049 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 Reed193d3a02018-02-08 15:45:51 -050054
rmistry@google.comae933ce2012-08-23 18:19:56 +000055 SkScalar margin = 20;
reed@android.com0bb6d062010-05-17 14:50:04 +000056
rmistry@google.comae933ce2012-08-23 18:19:56 +000057 SkPaint paint;
reed@google.com021b4052011-09-28 15:24:00 +000058 paint.setColor(fg);
reed@android.com0bb6d062010-05-17 14:50:04 +000059
rmistry@google.comae933ce2012-08-23 18:19:56 +000060 for (int i = 9; i < 24; i += 2) {
Ben Wagner454e5fb2019-02-08 17:46:38 -050061 SkTextBlobBuilderRunHandler builder(gText);
Hal Canary4484b8f2019-01-08 14:00:08 -050062 SkFont font(nullptr, SkIntToScalar(i));
63 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
64
Ben Wagnerb0591942019-02-15 14:46:18 -050065 SkPoint end = fShaper->shape(&builder, font, gText, strlen(gText), true,
66 { margin, margin }, w - margin);
Florin Malita9867f612018-12-12 10:54:49 -050067 canvas->drawTextBlob(builder.makeBlob(), 0, 0, paint);
Mike Reed193d3a02018-02-08 15:45:51 -050068
69 canvas->translate(0, end.y());
rmistry@google.comae933ce2012-08-23 18:19:56 +000070 }
reed@android.com0bb6d062010-05-17 14:50:04 +000071 }
reed@google.comd6f5a812011-03-02 14:34:11 +000072
Mike Reed193d3a02018-02-08 15:45:51 -050073 void onDrawContent(SkCanvas* canvas) override {
reed@google.com021b4052011-09-28 15:24:00 +000074 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
reed@android.com0bb6d062010-05-17 14:50:04 +000084private:
Ben Wagnerb0591942019-02-15 14:46:18 -050085 std::unique_ptr<SkShaper> fShaper;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040086 typedef Sample INHERITED;
reed@android.com0bb6d062010-05-17 14:50:04 +000087};
88
89//////////////////////////////////////////////////////////////////////////////
90
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040091DEF_SAMPLE( return new TextBoxView(); )