blob: eac570ac58890a3260b45e112be38c618282b216 [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"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
reed@google.comca0062e2012-07-20 11:20:32 +000010#include "SkCanvas.h"
11#include "SkPaint.h"
bungemand3ebb482015-08-05 13:57:49 -070012#include "SkPath.h"
reed@google.comca0062e2012-07-20 11:20:32 +000013#include "SkRandom.h"
reed@google.com00583242012-07-20 11:34:36 +000014#include "SkTemplates.h"
reed@google.comca0062e2012-07-20 11:20:32 +000015
halcanary2a243382015-09-09 08:16:41 -070016static void strokePath(SkCanvas* canvas, const SkPath& path) {
reed@google.com7b4531f2012-08-07 15:53:00 +000017 SkPaint paint;
18 paint.setAntiAlias(true);
19 paint.setColor(SK_ColorRED);
20 paint.setStyle(SkPaint::kStroke_Style);
21 canvas->drawPath(path, paint);
halcanary2a243382015-09-09 08:16:41 -070022}
23DEF_SIMPLE_GM(getpostextpath, canvas, 480, 780) {
reed@google.com7b4531f2012-08-07 15:53:00 +000024 // explicitly add spaces, to test a prev. bug
25 const char* text = "Ham bur ge fons";
reed@google.com7fa2a652014-01-27 13:42:58 +000026 int len = SkToInt(strlen(text));
reed@google.com7b4531f2012-08-07 15:53:00 +000027 SkPath path;
reed@google.comca0062e2012-07-20 11:20:32 +000028
29 SkPaint paint;
30 paint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070031 sk_tool_utils::set_portable_typeface(&paint);
reed@google.comca0062e2012-07-20 11:20:32 +000032 paint.setTextSize(SkIntToScalar(48));
reed@google.com7b4531f2012-08-07 15:53:00 +000033
34 canvas->translate(SkIntToScalar(10), SkIntToScalar(64));
35
36 canvas->drawText(text, len, 0, 0, paint);
37 paint.getTextPath(text, len, 0, 0, &path);
38 strokePath(canvas, path);
39 path.reset();
rmistry@google.comd6176b02012-08-23 18:14:13 +000040
reed@google.comca0062e2012-07-20 11:20:32 +000041 SkAutoTArray<SkPoint> pos(len);
42 SkAutoTArray<SkScalar> widths(len);
43 paint.getTextWidths(text, len, &widths[0]);
rmistry@google.comd6176b02012-08-23 18:14:13 +000044
scroggof9d61012014-12-15 12:54:51 -080045 SkRandom rand;
reed@google.comca0062e2012-07-20 11:20:32 +000046 SkScalar x = SkIntToScalar(20);
47 SkScalar y = SkIntToScalar(100);
reed@google.com7fa2a652014-01-27 13:42:58 +000048 for (int i = 0; i < len; ++i) {
reed@google.comca0062e2012-07-20 11:20:32 +000049 pos[i].set(x, y + rand.nextSScalar1() * 24);
50 x += widths[i];
51 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000052
reed@google.com7b4531f2012-08-07 15:53:00 +000053 canvas->translate(0, SkIntToScalar(64));
rmistry@google.comd6176b02012-08-23 18:14:13 +000054
reed@google.com7b4531f2012-08-07 15:53:00 +000055 canvas->drawPosText(text, len, &pos[0], paint);
reed@google.comca0062e2012-07-20 11:20:32 +000056 paint.getPosTextPath(text, len, &pos[0], &path);
reed@google.com7b4531f2012-08-07 15:53:00 +000057 strokePath(canvas, path);
halcanary2a243382015-09-09 08:16:41 -070058}