2012-07-20 11:20:32 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkPaint.h"
|
|
|
|
#include "SkRandom.h"
|
2012-07-20 11:34:36 +00:00
|
|
|
#include "SkTemplates.h"
|
2012-07-20 11:20:32 +00:00
|
|
|
|
|
|
|
class GetPosTextPathGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
GetPosTextPathGM() {}
|
|
|
|
|
|
|
|
protected:
|
2014-04-30 13:20:45 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2012-07-20 11:20:32 +00:00
|
|
|
return SkString("getpostextpath");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override { return SkISize::Make(480, 780); }
|
2012-07-20 11:20:32 +00:00
|
|
|
|
2012-08-07 15:53:00 +00:00
|
|
|
static void strokePath(SkCanvas* canvas, const SkPath& path) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
canvas->drawPath(path, paint);
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2012-08-07 15:53:00 +00:00
|
|
|
// explicitly add spaces, to test a prev. bug
|
|
|
|
const char* text = "Ham bur ge fons";
|
2014-01-27 13:42:58 +00:00
|
|
|
int len = SkToInt(strlen(text));
|
2012-08-07 15:53:00 +00:00
|
|
|
SkPath path;
|
2012-07-20 11:20:32 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2015-07-15 16:29:32 +00:00
|
|
|
sk_tool_utils::set_portable_typeface_always(&paint);
|
2012-07-20 11:20:32 +00:00
|
|
|
paint.setTextSize(SkIntToScalar(48));
|
2012-08-07 15:53:00 +00:00
|
|
|
|
|
|
|
canvas->translate(SkIntToScalar(10), SkIntToScalar(64));
|
|
|
|
|
|
|
|
canvas->drawText(text, len, 0, 0, paint);
|
|
|
|
paint.getTextPath(text, len, 0, 0, &path);
|
|
|
|
strokePath(canvas, path);
|
|
|
|
path.reset();
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-07-20 11:20:32 +00:00
|
|
|
SkAutoTArray<SkPoint> pos(len);
|
|
|
|
SkAutoTArray<SkScalar> widths(len);
|
|
|
|
paint.getTextWidths(text, len, &widths[0]);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2014-12-15 20:54:51 +00:00
|
|
|
SkRandom rand;
|
2012-07-20 11:20:32 +00:00
|
|
|
SkScalar x = SkIntToScalar(20);
|
|
|
|
SkScalar y = SkIntToScalar(100);
|
2014-01-27 13:42:58 +00:00
|
|
|
for (int i = 0; i < len; ++i) {
|
2012-07-20 11:20:32 +00:00
|
|
|
pos[i].set(x, y + rand.nextSScalar1() * 24);
|
|
|
|
x += widths[i];
|
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-08-07 15:53:00 +00:00
|
|
|
canvas->translate(0, SkIntToScalar(64));
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-08-07 15:53:00 +00:00
|
|
|
canvas->drawPosText(text, len, &pos[0], paint);
|
2012-07-20 11:20:32 +00:00
|
|
|
paint.getPosTextPath(text, len, &pos[0], &path);
|
2012-08-07 15:53:00 +00:00
|
|
|
strokePath(canvas, path);
|
2012-07-20 11:20:32 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static skiagm::GM* F(void*) { return new GetPosTextPathGM; }
|
|
|
|
static skiagm::GMRegistry gR(F);
|