blob: 1fcce5fca50973a2b1d772e685af66511b3eb3aa [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"
Hal Canaryc640d0d2018-06-13 09:59:02 -040010
reed@google.comca0062e2012-07-20 11:20:32 +000011#include "SkCanvas.h"
12#include "SkPaint.h"
bungemand3ebb482015-08-05 13:57:49 -070013#include "SkPath.h"
reed@google.comca0062e2012-07-20 11:20:32 +000014#include "SkRandom.h"
reed@google.com00583242012-07-20 11:34:36 +000015#include "SkTemplates.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040016#include "SkTo.h"
reed@google.comca0062e2012-07-20 11:20:32 +000017
halcanary2a243382015-09-09 08:16:41 -070018static void strokePath(SkCanvas* canvas, const SkPath& path) {
reed@google.com7b4531f2012-08-07 15:53:00 +000019 SkPaint paint;
20 paint.setAntiAlias(true);
21 paint.setColor(SK_ColorRED);
22 paint.setStyle(SkPaint::kStroke_Style);
23 canvas->drawPath(path, paint);
halcanary2a243382015-09-09 08:16:41 -070024}
25DEF_SIMPLE_GM(getpostextpath, canvas, 480, 780) {
reed@google.com7b4531f2012-08-07 15:53:00 +000026 // explicitly add spaces, to test a prev. bug
27 const char* text = "Ham bur ge fons";
reed@google.com7fa2a652014-01-27 13:42:58 +000028 int len = SkToInt(strlen(text));
reed@google.com7b4531f2012-08-07 15:53:00 +000029 SkPath path;
reed@google.comca0062e2012-07-20 11:20:32 +000030
31 SkPaint paint;
32 paint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070033 sk_tool_utils::set_portable_typeface(&paint);
reed@google.comca0062e2012-07-20 11:20:32 +000034 paint.setTextSize(SkIntToScalar(48));
reed@google.com7b4531f2012-08-07 15:53:00 +000035
36 canvas->translate(SkIntToScalar(10), SkIntToScalar(64));
37
38 canvas->drawText(text, len, 0, 0, paint);
39 paint.getTextPath(text, len, 0, 0, &path);
40 strokePath(canvas, path);
41 path.reset();
rmistry@google.comd6176b02012-08-23 18:14:13 +000042
reed@google.comca0062e2012-07-20 11:20:32 +000043 SkAutoTArray<SkPoint> pos(len);
44 SkAutoTArray<SkScalar> widths(len);
45 paint.getTextWidths(text, len, &widths[0]);
rmistry@google.comd6176b02012-08-23 18:14:13 +000046
scroggof9d61012014-12-15 12:54:51 -080047 SkRandom rand;
reed@google.comca0062e2012-07-20 11:20:32 +000048 SkScalar x = SkIntToScalar(20);
49 SkScalar y = SkIntToScalar(100);
reed@google.com7fa2a652014-01-27 13:42:58 +000050 for (int i = 0; i < len; ++i) {
reed@google.comca0062e2012-07-20 11:20:32 +000051 pos[i].set(x, y + rand.nextSScalar1() * 24);
52 x += widths[i];
53 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000054
reed@google.com7b4531f2012-08-07 15:53:00 +000055 canvas->translate(0, SkIntToScalar(64));
rmistry@google.comd6176b02012-08-23 18:14:13 +000056
reed@google.com7b4531f2012-08-07 15:53:00 +000057 canvas->drawPosText(text, len, &pos[0], paint);
reed@google.comca0062e2012-07-20 11:20:32 +000058 paint.getPosTextPath(text, len, &pos[0], &path);
reed@google.com7b4531f2012-08-07 15:53:00 +000059 strokePath(canvas, path);
halcanary2a243382015-09-09 08:16:41 -070060}