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.
|
|
|
|
*/
|
2014-01-13 14:53:55 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SampleCode.h"
|
|
|
|
#include "SkView.h"
|
|
|
|
#include "SkCanvas.h"
|
2014-01-30 18:58:24 +00:00
|
|
|
#include "SkReadBuffer.h"
|
|
|
|
#include "SkWriteBuffer.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkGraphics.h"
|
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkRandom.h"
|
|
|
|
#include "SkRegion.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
#include "SkUtils.h"
|
|
|
|
#include "SkColorPriv.h"
|
|
|
|
#include "SkColorFilter.h"
|
|
|
|
#include "SkTime.h"
|
|
|
|
#include "SkTypeface.h"
|
|
|
|
#include "SkStream.h"
|
|
|
|
|
|
|
|
static const struct {
|
|
|
|
const char* fName;
|
|
|
|
uint32_t fFlags;
|
|
|
|
bool fFlushCache;
|
|
|
|
} gHints[] = {
|
|
|
|
{ "Linear", SkPaint::kLinearText_Flag, false },
|
|
|
|
{ "Normal", 0, true },
|
|
|
|
{ "Subpixel", SkPaint::kSubpixelText_Flag, true }
|
|
|
|
};
|
|
|
|
|
2014-05-12 13:35:55 +00:00
|
|
|
static void DrawTheText(SkCanvas* canvas, const char text[], size_t length, SkScalar x, SkScalar y,
|
|
|
|
const SkPaint& paint, SkScalar clickX) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint p(paint);
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
canvas->drawText(text, length, x, y, paint);
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
SkPoint pts[1000];
|
|
|
|
SkScalar xpos = x;
|
|
|
|
SkASSERT(length <= SK_ARRAY_COUNT(pts));
|
2011-05-18 12:17:53 +00:00
|
|
|
for (size_t i = 0; i < length; i++) {
|
2016-10-18 18:42:44 +00:00
|
|
|
pts[i].set(xpos, y);
|
|
|
|
xpos += paint.getTextSize();
|
2011-05-18 12:17:53 +00:00
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->drawPosText(text, length, pts, paint);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
p.setSubpixelText(true);
|
|
|
|
x += SkIntToScalar(180);
|
|
|
|
canvas->drawText(text, length, x, y, p);
|
|
|
|
|
|
|
|
#ifdef SK_DEBUG
|
2011-05-18 12:17:53 +00:00
|
|
|
if (true) {
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setSubpixelText(false);
|
|
|
|
p.setLinearText(true);
|
|
|
|
x += SkIntToScalar(180);
|
|
|
|
canvas->drawText(text, length, x, y, p);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-05-18 12:17:53 +00:00
|
|
|
class TextSpeedView : public SampleView {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2012-08-23 18:19:56 +00:00
|
|
|
TextSpeedView() {
|
2008-12-17 15:59:43 +00:00
|
|
|
fHints = 0;
|
|
|
|
fClickX = 0;
|
|
|
|
}
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
2015-03-26 01:17:31 +00:00
|
|
|
bool onQuery(SkEvent* evt) override {
|
2011-05-18 12:17:53 +00:00
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SampleCode::TitleR(evt, "Text");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2011-05-18 12:17:53 +00:00
|
|
|
static void make_textstrip(SkBitmap* bm) {
|
2014-02-17 02:55:57 +00:00
|
|
|
bm->allocPixels(SkImageInfo::Make(200, 18, kRGB_565_SkColorType,
|
|
|
|
kOpaque_SkAlphaType));
|
2008-12-17 15:59:43 +00:00
|
|
|
bm->eraseColor(SK_ColorWHITE);
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkCanvas canvas(*bm);
|
|
|
|
SkPaint paint;
|
|
|
|
const char* s = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit";
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2018-05-01 15:57:56 +00:00
|
|
|
paint.setFlags(paint.getFlags() | SkPaint::kAntiAlias_Flag);
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setTextSize(SkIntToScalar(14));
|
2017-04-28 19:35:12 +00:00
|
|
|
canvas.drawString(s, SkIntToScalar(8), SkIntToScalar(14), paint);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2013-09-09 20:09:12 +00:00
|
|
|
static void fill_pts(SkPoint pts[], size_t n, SkRandom* rand) {
|
2008-12-17 15:59:43 +00:00
|
|
|
for (size_t i = 0; i < n; i++)
|
|
|
|
pts[i].set(rand->nextUScalar1() * 640, rand->nextUScalar1() * 480);
|
|
|
|
}
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkAutoCanvasRestore restore(canvas, false);
|
|
|
|
{
|
|
|
|
SkRect r;
|
|
|
|
r.set(0, 0, SkIntToScalar(1000), SkIntToScalar(20));
|
2015-08-27 14:41:13 +00:00
|
|
|
// canvas->saveLayer(&r, nullptr, SkCanvas::kHasAlphaLayer_SaveFlag);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
// const uint16_t glyphs[] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19 };
|
|
|
|
int index = fHints % SK_ARRAY_COUNT(gHints);
|
|
|
|
index = 1;
|
|
|
|
// const char* style = gHints[index].fName;
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
// canvas->translate(0, SkIntToScalar(50));
|
|
|
|
|
2017-04-28 19:35:12 +00:00
|
|
|
// canvas->drawString(style, SkIntToScalar(20), SkIntToScalar(20), paint);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2016-05-12 17:09:30 +00:00
|
|
|
paint.setTypeface(SkTypeface::MakeFromFile("/skimages/samplefont.ttf"));
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setFlags(paint.getFlags() | gHints[index].fFlags);
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkRect clip;
|
|
|
|
clip.set(SkIntToScalar(25), SkIntToScalar(34), SkIntToScalar(88), SkIntToScalar(155));
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
const char* text = "Hamburgefons";
|
|
|
|
size_t length = strlen(text);
|
|
|
|
|
2011-05-02 17:33:45 +00:00
|
|
|
SkScalar y = SkIntToScalar(0);
|
|
|
|
for (int i = 9; i <= 24; i++) {
|
|
|
|
paint.setTextSize(SkIntToScalar(i) /*+ (gRand.nextU() & 0xFFFF)*/);
|
2011-05-18 12:17:53 +00:00
|
|
|
for (SkScalar dx = 0; dx <= SkIntToScalar(3)/4;
|
|
|
|
dx += SkIntToScalar(1) /* /4 */) {
|
2011-05-02 17:33:45 +00:00
|
|
|
y += paint.getFontSpacing();
|
2014-05-12 13:35:55 +00:00
|
|
|
DrawTheText(canvas, text, length, SkIntToScalar(20) + dx, y, paint, fClickX);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
}
|
2011-05-02 17:33:45 +00:00
|
|
|
if (gHints[index].fFlushCache) {
|
|
|
|
// SkGraphics::SetFontCacheUsed(0);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
}
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2013-01-08 16:17:50 +00:00
|
|
|
virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
|
2015-03-26 01:17:31 +00:00
|
|
|
unsigned modi) override {
|
2008-12-17 15:59:43 +00:00
|
|
|
fClickX = x;
|
2013-01-08 16:17:50 +00:00
|
|
|
return this->INHERITED::onFindClickHandler(x, y, modi);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
bool onClick(Click* click) override {
|
2008-12-17 15:59:43 +00:00
|
|
|
return this->INHERITED::onClick(click);
|
|
|
|
}
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
|
|
|
int fHints;
|
|
|
|
SkScalar fClickX;
|
|
|
|
|
2011-05-18 12:17:53 +00:00
|
|
|
typedef SampleView INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() { return new TextSpeedView; }
|
|
|
|
static SkViewRegister reg(MyFactory);
|