blob: faace242e02fbccb290d7e316d406a08b35d8a53 [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"
bungemand3ebb482015-08-05 13:57:49 -070011#include "SkPath.h"
reed@google.comca0062e2012-07-20 11:20:32 +000012#include "SkRandom.h"
reed@google.com00583242012-07-20 11:34:36 +000013#include "SkTemplates.h"
reed@google.comca0062e2012-07-20 11:20:32 +000014
15class GetPosTextPathGM : public skiagm::GM {
16public:
17 GetPosTextPathGM() {}
18
19protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000020
mtklein36352bf2015-03-25 18:17:31 -070021 SkString onShortName() override {
reed@google.comca0062e2012-07-20 11:20:32 +000022 return SkString("getpostextpath");
23 }
24
mtklein36352bf2015-03-25 18:17:31 -070025 SkISize onISize() override { return SkISize::Make(480, 780); }
reed@google.comca0062e2012-07-20 11:20:32 +000026
reed@google.com7b4531f2012-08-07 15:53:00 +000027 static void strokePath(SkCanvas* canvas, const SkPath& path) {
28 SkPaint paint;
29 paint.setAntiAlias(true);
30 paint.setColor(SK_ColorRED);
31 paint.setStyle(SkPaint::kStroke_Style);
32 canvas->drawPath(path, paint);
33 }
34
mtklein36352bf2015-03-25 18:17:31 -070035 void onDraw(SkCanvas* canvas) override {
reed@google.com7b4531f2012-08-07 15:53:00 +000036 // explicitly add spaces, to test a prev. bug
37 const char* text = "Ham bur ge fons";
reed@google.com7fa2a652014-01-27 13:42:58 +000038 int len = SkToInt(strlen(text));
reed@google.com7b4531f2012-08-07 15:53:00 +000039 SkPath path;
reed@google.comca0062e2012-07-20 11:20:32 +000040
41 SkPaint paint;
42 paint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070043 sk_tool_utils::set_portable_typeface(&paint);
reed@google.comca0062e2012-07-20 11:20:32 +000044 paint.setTextSize(SkIntToScalar(48));
reed@google.com7b4531f2012-08-07 15:53:00 +000045
46 canvas->translate(SkIntToScalar(10), SkIntToScalar(64));
47
48 canvas->drawText(text, len, 0, 0, paint);
49 paint.getTextPath(text, len, 0, 0, &path);
50 strokePath(canvas, path);
51 path.reset();
rmistry@google.comd6176b02012-08-23 18:14:13 +000052
reed@google.comca0062e2012-07-20 11:20:32 +000053 SkAutoTArray<SkPoint> pos(len);
54 SkAutoTArray<SkScalar> widths(len);
55 paint.getTextWidths(text, len, &widths[0]);
rmistry@google.comd6176b02012-08-23 18:14:13 +000056
scroggof9d61012014-12-15 12:54:51 -080057 SkRandom rand;
reed@google.comca0062e2012-07-20 11:20:32 +000058 SkScalar x = SkIntToScalar(20);
59 SkScalar y = SkIntToScalar(100);
reed@google.com7fa2a652014-01-27 13:42:58 +000060 for (int i = 0; i < len; ++i) {
reed@google.comca0062e2012-07-20 11:20:32 +000061 pos[i].set(x, y + rand.nextSScalar1() * 24);
62 x += widths[i];
63 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000064
reed@google.com7b4531f2012-08-07 15:53:00 +000065 canvas->translate(0, SkIntToScalar(64));
rmistry@google.comd6176b02012-08-23 18:14:13 +000066
reed@google.com7b4531f2012-08-07 15:53:00 +000067 canvas->drawPosText(text, len, &pos[0], paint);
reed@google.comca0062e2012-07-20 11:20:32 +000068 paint.getPosTextPath(text, len, &pos[0], &path);
reed@google.com7b4531f2012-08-07 15:53:00 +000069 strokePath(canvas, path);
reed@google.comca0062e2012-07-20 11:20:32 +000070 }
71};
72
73//////////////////////////////////////////////////////////////////////////////
74
75static skiagm::GM* F(void*) { return new GetPosTextPathGM; }
reed@google.comca0062e2012-07-20 11:20:32 +000076static skiagm::GMRegistry gR(F);