blob: db509697440c71df239e8f4123091b619162ac74 [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
tfarinaaa458fb2015-01-05 17:18:51 -080020 SkString onShortName() SK_OVERRIDE {
reed@google.comca0062e2012-07-20 11:20:32 +000021 return SkString("getpostextpath");
22 }
23
tfarinaaa458fb2015-01-05 17:18:51 -080024 SkISize onISize() SK_OVERRIDE { return SkISize::Make(480, 780); }
reed@google.comca0062e2012-07-20 11:20:32 +000025
reed@google.com7b4531f2012-08-07 15:53:00 +000026 static void strokePath(SkCanvas* canvas, const SkPath& path) {
27 SkPaint paint;
28 paint.setAntiAlias(true);
29 paint.setColor(SK_ColorRED);
30 paint.setStyle(SkPaint::kStroke_Style);
31 canvas->drawPath(path, paint);
32 }
33
mtklein72c9faa2015-01-09 10:06:39 -080034 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
reed@google.com7b4531f2012-08-07 15:53:00 +000035 // explicitly add spaces, to test a prev. bug
36 const char* text = "Ham bur ge fons";
reed@google.com7fa2a652014-01-27 13:42:58 +000037 int len = SkToInt(strlen(text));
reed@google.com7b4531f2012-08-07 15:53:00 +000038 SkPath path;
reed@google.comca0062e2012-07-20 11:20:32 +000039
40 SkPaint paint;
41 paint.setAntiAlias(true);
Cary Clark992c7b02014-07-31 08:58:44 -040042 sk_tool_utils::set_portable_typeface(&paint);
reed@google.comca0062e2012-07-20 11:20:32 +000043 paint.setTextSize(SkIntToScalar(48));
reed@google.com7b4531f2012-08-07 15:53:00 +000044
45 canvas->translate(SkIntToScalar(10), SkIntToScalar(64));
46
47 canvas->drawText(text, len, 0, 0, paint);
48 paint.getTextPath(text, len, 0, 0, &path);
49 strokePath(canvas, path);
50 path.reset();
rmistry@google.comd6176b02012-08-23 18:14:13 +000051
reed@google.comca0062e2012-07-20 11:20:32 +000052 SkAutoTArray<SkPoint> pos(len);
53 SkAutoTArray<SkScalar> widths(len);
54 paint.getTextWidths(text, len, &widths[0]);
rmistry@google.comd6176b02012-08-23 18:14:13 +000055
scroggof9d61012014-12-15 12:54:51 -080056 SkRandom rand;
reed@google.comca0062e2012-07-20 11:20:32 +000057 SkScalar x = SkIntToScalar(20);
58 SkScalar y = SkIntToScalar(100);
reed@google.com7fa2a652014-01-27 13:42:58 +000059 for (int i = 0; i < len; ++i) {
reed@google.comca0062e2012-07-20 11:20:32 +000060 pos[i].set(x, y + rand.nextSScalar1() * 24);
61 x += widths[i];
62 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000063
reed@google.com7b4531f2012-08-07 15:53:00 +000064 canvas->translate(0, SkIntToScalar(64));
rmistry@google.comd6176b02012-08-23 18:14:13 +000065
reed@google.com7b4531f2012-08-07 15:53:00 +000066 canvas->drawPosText(text, len, &pos[0], paint);
reed@google.comca0062e2012-07-20 11:20:32 +000067 paint.getPosTextPath(text, len, &pos[0], &path);
reed@google.com7b4531f2012-08-07 15:53:00 +000068 strokePath(canvas, path);
reed@google.comca0062e2012-07-20 11:20:32 +000069 }
70};
71
72//////////////////////////////////////////////////////////////////////////////
73
74static skiagm::GM* F(void*) { return new GetPosTextPathGM; }
reed@google.comca0062e2012-07-20 11:20:32 +000075static skiagm::GMRegistry gR(F);