blob: a4115202588324342d6f153e634e32f5118d2ef3 [file] [log] [blame]
reed@google.comca0062e2012-07-20 11:20:32 +00001/*
2 * Copyright 2012 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 */
7
8#include "gm.h"
9#include "SkCanvas.h"
10#include "SkPaint.h"
11#include "SkRandom.h"
reed@google.com00583242012-07-20 11:34:36 +000012#include "SkTemplates.h"
reed@google.comca0062e2012-07-20 11:20:32 +000013
14class GetPosTextPathGM : public skiagm::GM {
15public:
16 GetPosTextPathGM() {}
17
18protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000019 virtual uint32_t onGetFlags() const SK_OVERRIDE {
20 return kSkipTiled_Flag;
21 }
22
reed@google.comca0062e2012-07-20 11:20:32 +000023 SkString onShortName() {
24 return SkString("getpostextpath");
25 }
26
tfarinaf5393182014-06-09 23:59:03 -070027 SkISize onISize() { return SkISize::Make(480, 780); }
reed@google.comca0062e2012-07-20 11:20:32 +000028
reed@google.com7b4531f2012-08-07 15:53:00 +000029 static void strokePath(SkCanvas* canvas, const SkPath& path) {
30 SkPaint paint;
31 paint.setAntiAlias(true);
32 paint.setColor(SK_ColorRED);
33 paint.setStyle(SkPaint::kStroke_Style);
34 canvas->drawPath(path, paint);
35 }
36
reed@google.comca0062e2012-07-20 11:20:32 +000037 virtual void onDraw(SkCanvas* canvas) {
reed@google.com7b4531f2012-08-07 15:53:00 +000038 // explicitly add spaces, to test a prev. bug
39 const char* text = "Ham bur ge fons";
reed@google.com7fa2a652014-01-27 13:42:58 +000040 int len = SkToInt(strlen(text));
reed@google.com7b4531f2012-08-07 15:53:00 +000041 SkPath path;
reed@google.comca0062e2012-07-20 11:20:32 +000042
43 SkPaint paint;
44 paint.setAntiAlias(true);
Cary Clark992c7b02014-07-31 08:58:44 -040045 sk_tool_utils::set_portable_typeface(&paint);
reed@google.comca0062e2012-07-20 11:20:32 +000046 paint.setTextSize(SkIntToScalar(48));
reed@google.com7b4531f2012-08-07 15:53:00 +000047
48 canvas->translate(SkIntToScalar(10), SkIntToScalar(64));
49
50 canvas->drawText(text, len, 0, 0, paint);
51 paint.getTextPath(text, len, 0, 0, &path);
52 strokePath(canvas, path);
53 path.reset();
rmistry@google.comd6176b02012-08-23 18:14:13 +000054
reed@google.comca0062e2012-07-20 11:20:32 +000055 SkAutoTArray<SkPoint> pos(len);
56 SkAutoTArray<SkScalar> widths(len);
57 paint.getTextWidths(text, len, &widths[0]);
rmistry@google.comd6176b02012-08-23 18:14:13 +000058
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000059 SkLCGRandom rand;
reed@google.comca0062e2012-07-20 11:20:32 +000060 SkScalar x = SkIntToScalar(20);
61 SkScalar y = SkIntToScalar(100);
reed@google.com7fa2a652014-01-27 13:42:58 +000062 for (int i = 0; i < len; ++i) {
reed@google.comca0062e2012-07-20 11:20:32 +000063 pos[i].set(x, y + rand.nextSScalar1() * 24);
64 x += widths[i];
65 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000066
reed@google.com7b4531f2012-08-07 15:53:00 +000067 canvas->translate(0, SkIntToScalar(64));
rmistry@google.comd6176b02012-08-23 18:14:13 +000068
reed@google.com7b4531f2012-08-07 15:53:00 +000069 canvas->drawPosText(text, len, &pos[0], paint);
reed@google.comca0062e2012-07-20 11:20:32 +000070 paint.getPosTextPath(text, len, &pos[0], &path);
reed@google.com7b4531f2012-08-07 15:53:00 +000071 strokePath(canvas, path);
reed@google.comca0062e2012-07-20 11:20:32 +000072 }
73};
74
75//////////////////////////////////////////////////////////////////////////////
76
77static skiagm::GM* F(void*) { return new GetPosTextPathGM; }
reed@google.comca0062e2012-07-20 11:20:32 +000078static skiagm::GMRegistry gR(F);