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 "SkCanvas.h" 10 #include "SkPaint.h" 11 #include "SkRandom.h" 12 #include "SkTemplates.h" 13 14 class GetPosTextPathGM : public skiagm::GM { 15 public: 16 GetPosTextPathGM() {} 17 18 protected: 19 virtual uint32_t onGetFlags() const SK_OVERRIDE { 20 return kSkipTiled_Flag; 21 } 22 23 SkString onShortName() { 24 return SkString("getpostextpath"); 25 } 26 27 SkISize onISize() { return SkISize::Make(480, 780); } 28 29 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 37 virtual void onDraw(SkCanvas* canvas) { 38 // explicitly add spaces, to test a prev. bug 39 const char* text = "Ham bur ge fons"; 40 int len = SkToInt(strlen(text)); 41 SkPath path; 42 43 SkPaint paint; 44 paint.setAntiAlias(true); 45 paint.setTextSize(SkIntToScalar(48)); 46 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(); 53 54 SkAutoTArray<SkPoint> pos(len); 55 SkAutoTArray<SkScalar> widths(len); 56 paint.getTextWidths(text, len, &widths[0]); 57 58 SkLCGRandom rand; 59 SkScalar x = SkIntToScalar(20); 60 SkScalar y = SkIntToScalar(100); 61 for (int i = 0; i < len; ++i) { 62 pos[i].set(x, y + rand.nextSScalar1() * 24); 63 x += widths[i]; 64 } 65 66 canvas->translate(0, SkIntToScalar(64)); 67 68 canvas->drawPosText(text, len, &pos[0], paint); 69 paint.getPosTextPath(text, len, &pos[0], &path); 70 strokePath(canvas, path); 71 } 72 }; 73 74 ////////////////////////////////////////////////////////////////////////////// 75 76 static skiagm::GM* F(void*) { return new GetPosTextPathGM; } 77 static skiagm::GMRegistry gR(F); 78