2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "samplecode/Sample.h"
|
2018-02-08 20:45:51 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColorFilter.h"
|
|
|
|
#include "include/core/SkColorPriv.h"
|
|
|
|
#include "include/core/SkGraphics.h"
|
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
#include "include/core/SkRegion.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkStream.h"
|
|
|
|
#include "include/core/SkTextBlob.h"
|
|
|
|
#include "include/core/SkTime.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/effects/SkBlurMaskFilter.h"
|
|
|
|
#include "include/effects/SkGradientShader.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
|
|
|
#include "modules/skshaper/include/SkShaper.h"
|
|
|
|
#include "src/core/SkOSFile.h"
|
|
|
|
#include "src/shaders/SkColorShader.h"
|
|
|
|
#include "src/utils/SkUTF.h"
|
2010-05-17 14:50:04 +00:00
|
|
|
|
2011-03-02 14:34:11 +00:00
|
|
|
static const char gText[] =
|
2012-08-23 18:19:56 +00:00
|
|
|
"When in the Course of human events it becomes necessary for one people "
|
|
|
|
"to dissolve the political bands which have connected them with another "
|
|
|
|
"and to assume among the powers of the earth, the separate and equal "
|
|
|
|
"station to which the Laws of Nature and of Nature's God entitle them, "
|
|
|
|
"a decent respect to the opinions of mankind requires that they should "
|
|
|
|
"declare the causes which impel them to the separation.";
|
2010-05-17 14:50:04 +00:00
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class TextBoxView : public Sample {
|
2011-03-02 14:34:11 +00:00
|
|
|
public:
|
2019-02-15 19:46:18 +00:00
|
|
|
TextBoxView() : fShaper(SkShaper::Make()) {}
|
2011-03-02 14:34:11 +00:00
|
|
|
|
2010-05-17 14:50:04 +00:00
|
|
|
protected:
|
2018-08-08 15:36:17 +00:00
|
|
|
bool onQuery(Sample::Event* evt) override {
|
|
|
|
if (Sample::TitleQ(*evt)) {
|
|
|
|
Sample::TitleR(evt, "TextBox");
|
2010-05-17 14:50:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
2011-03-02 14:34:11 +00:00
|
|
|
|
2011-09-28 15:24:00 +00:00
|
|
|
void drawTest(SkCanvas* canvas, SkScalar w, SkScalar h, SkColor fg, SkColor bg) {
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
|
|
|
|
canvas->clipRect(SkRect::MakeWH(w, h));
|
|
|
|
canvas->drawColor(bg);
|
2018-02-08 20:45:51 +00:00
|
|
|
|
2012-08-23 18:19:56 +00:00
|
|
|
SkScalar margin = 20;
|
2010-05-17 14:50:04 +00:00
|
|
|
|
2012-08-23 18:19:56 +00:00
|
|
|
SkPaint paint;
|
2011-09-28 15:24:00 +00:00
|
|
|
paint.setColor(fg);
|
2010-05-17 14:50:04 +00:00
|
|
|
|
2012-08-23 18:19:56 +00:00
|
|
|
for (int i = 9; i < 24; i += 2) {
|
2019-04-01 23:01:09 +00:00
|
|
|
SkTextBlobBuilderRunHandler builder(gText, { margin, margin });
|
2019-01-08 19:00:08 +00:00
|
|
|
SkFont font(nullptr, SkIntToScalar(i));
|
|
|
|
font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
|
|
|
|
|
2019-04-01 23:01:09 +00:00
|
|
|
fShaper->shape(gText, strlen(gText), font, true, w - margin, &builder);
|
2018-12-12 15:54:49 +00:00
|
|
|
canvas->drawTextBlob(builder.makeBlob(), 0, 0, paint);
|
2018-02-08 20:45:51 +00:00
|
|
|
|
2019-04-01 23:01:09 +00:00
|
|
|
canvas->translate(0, builder.endPoint().y());
|
2012-08-23 18:19:56 +00:00
|
|
|
}
|
2010-05-17 14:50:04 +00:00
|
|
|
}
|
2011-03-02 14:34:11 +00:00
|
|
|
|
2018-02-08 20:45:51 +00:00
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
2011-09-28 15:24:00 +00:00
|
|
|
SkScalar width = this->width() / 3;
|
|
|
|
drawTest(canvas, width, this->height(), SK_ColorBLACK, SK_ColorWHITE);
|
|
|
|
canvas->translate(width, 0);
|
|
|
|
drawTest(canvas, width, this->height(), SK_ColorWHITE, SK_ColorBLACK);
|
|
|
|
canvas->translate(width, 0);
|
|
|
|
drawTest(canvas, width, this->height()/2, SK_ColorGRAY, SK_ColorWHITE);
|
|
|
|
canvas->translate(0, this->height()/2);
|
|
|
|
drawTest(canvas, width, this->height()/2, SK_ColorGRAY, SK_ColorBLACK);
|
|
|
|
}
|
|
|
|
|
2010-05-17 14:50:04 +00:00
|
|
|
private:
|
2019-02-15 19:46:18 +00:00
|
|
|
std::unique_ptr<SkShaper> fShaper;
|
2018-08-08 15:36:17 +00:00
|
|
|
typedef Sample INHERITED;
|
2010-05-17 14:50:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
DEF_SAMPLE( return new TextBoxView(); )
|