blob: 0f66a976b5353840395c01944734f5313be30e6c [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);
45 paint.setTextSize(SkIntToScalar(48));
reed@google.com7b4531f2012-08-07 15:53:00 +000046
47 canvas->translate(SkIntToScalar(10), SkIntToScalar(64));
48
49 canvas->drawText(text, len, 0, 0, paint);
50 paint.getTextPath(text, len, 0, 0, &path);
51 strokePath(canvas, path);
52 path.reset();
rmistry@google.comd6176b02012-08-23 18:14:13 +000053
reed@google.comca0062e2012-07-20 11:20:32 +000054 SkAutoTArray<SkPoint> pos(len);
55 SkAutoTArray<SkScalar> widths(len);
56 paint.getTextWidths(text, len, &widths[0]);
rmistry@google.comd6176b02012-08-23 18:14:13 +000057
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000058 SkLCGRandom rand;
reed@google.comca0062e2012-07-20 11:20:32 +000059 SkScalar x = SkIntToScalar(20);
60 SkScalar y = SkIntToScalar(100);
reed@google.com7fa2a652014-01-27 13:42:58 +000061 for (int i = 0; i < len; ++i) {
reed@google.comca0062e2012-07-20 11:20:32 +000062 pos[i].set(x, y + rand.nextSScalar1() * 24);
63 x += widths[i];
64 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000065
reed@google.com7b4531f2012-08-07 15:53:00 +000066 canvas->translate(0, SkIntToScalar(64));
rmistry@google.comd6176b02012-08-23 18:14:13 +000067
reed@google.com7b4531f2012-08-07 15:53:00 +000068 canvas->drawPosText(text, len, &pos[0], paint);
reed@google.comca0062e2012-07-20 11:20:32 +000069 paint.getPosTextPath(text, len, &pos[0], &path);
reed@google.com7b4531f2012-08-07 15:53:00 +000070 strokePath(canvas, path);
reed@google.comca0062e2012-07-20 11:20:32 +000071 }
72};
73
74//////////////////////////////////////////////////////////////////////////////
75
76static skiagm::GM* F(void*) { return new GetPosTextPathGM; }
reed@google.comca0062e2012-07-20 11:20:32 +000077static skiagm::GMRegistry gR(F);