1/* 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 "sk_tool_utils.h" 10#include "SkCanvas.h" 11#include "SkPaint.h" 12#include "SkPath.h" 13#include "SkRandom.h" 14#include "SkTemplates.h" 15 16static void strokePath(SkCanvas* canvas, const SkPath& path) { 17 SkPaint paint; 18 paint.setAntiAlias(true); 19 paint.setColor(SK_ColorRED); 20 paint.setStyle(SkPaint::kStroke_Style); 21 canvas->drawPath(path, paint); 22} 23DEF_SIMPLE_GM(getpostextpath, canvas, 480, 780) { 24 // explicitly add spaces, to test a prev. bug 25 const char* text = "Ham bur ge fons"; 26 int len = SkToInt(strlen(text)); 27 SkPath path; 28 29 SkPaint paint; 30 paint.setAntiAlias(true); 31 sk_tool_utils::set_portable_typeface(&paint); 32 paint.setTextSize(SkIntToScalar(48)); 33 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(); 40 41 SkAutoTArray<SkPoint> pos(len); 42 SkAutoTArray<SkScalar> widths(len); 43 paint.getTextWidths(text, len, &widths[0]); 44 45 SkRandom rand; 46 SkScalar x = SkIntToScalar(20); 47 SkScalar y = SkIntToScalar(100); 48 for (int i = 0; i < len; ++i) { 49 pos[i].set(x, y + rand.nextSScalar1() * 24); 50 x += widths[i]; 51 } 52 53 canvas->translate(0, SkIntToScalar(64)); 54 55 canvas->drawPosText(text, len, &pos[0], paint); 56 paint.getPosTextPath(text, len, &pos[0], &path); 57 strokePath(canvas, path); 58} 59