1 /* 2 * Copyright 2011 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 "Resources.h" 11 #include "SkFontMetrics.h" 12 #include "SkPath.h" 13 #include "SkTextUtils.h" 14 #include "SkTypeface.h" 15 16 class Poly2PolyGM : public skiagm::GM { 17 public: Poly2PolyGM()18 Poly2PolyGM() {} 19 20 protected: 21 onShortName()22 SkString onShortName() override { 23 return SkString("poly2poly"); 24 } 25 onISize()26 SkISize onISize() override { 27 return SkISize::Make(835, 840); 28 } 29 doDraw(SkCanvas * canvas,const SkFont & font,SkPaint * paint,const int isrc[],const int idst[],int count)30 static void doDraw(SkCanvas* canvas, const SkFont& font, SkPaint* paint, const int isrc[], 31 const int idst[], int count) { 32 SkMatrix matrix; 33 SkPoint src[4], dst[4]; 34 35 for (int i = 0; i < count; i++) { 36 src[i].set(SkIntToScalar(isrc[2*i+0]), SkIntToScalar(isrc[2*i+1])); 37 dst[i].set(SkIntToScalar(idst[2*i+0]), SkIntToScalar(idst[2*i+1])); 38 } 39 40 canvas->save(); 41 matrix.setPolyToPoly(src, dst, count); 42 canvas->concat(matrix); 43 44 paint->setColor(SK_ColorGRAY); 45 paint->setStyle(SkPaint::kStroke_Style); 46 const SkScalar D = 64; 47 canvas->drawRect(SkRect::MakeWH(D, D), *paint); 48 canvas->drawLine(0, 0, D, D, *paint); 49 canvas->drawLine(0, D, D, 0, *paint); 50 51 SkFontMetrics fm; 52 font.getMetrics(&fm); 53 paint->setColor(SK_ColorRED); 54 paint->setStyle(SkPaint::kFill_Style); 55 SkScalar x = D/2; 56 SkScalar y = D/2 - (fm.fAscent + fm.fDescent)/2; 57 uint16_t glyphID = 3; // X 58 SkTextUtils::Draw(canvas, &glyphID, sizeof(glyphID), kGlyphID_SkTextEncoding, x, y, 59 font, *paint, SkTextUtils::kCenter_Align); 60 canvas->restore(); 61 } 62 onOnceBeforeDraw()63 void onOnceBeforeDraw() override { 64 fEmFace = MakeResourceAsTypeface("fonts/Em.ttf"); 65 } 66 onDraw(SkCanvas * canvas)67 void onDraw(SkCanvas* canvas) override { 68 SkPaint paint; 69 paint.setAntiAlias(true); 70 paint.setStrokeWidth(SkIntToScalar(4)); 71 SkFont font(fEmFace, 40); 72 73 canvas->save(); 74 canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); 75 // translate (1 point) 76 const int src1[] = { 0, 0 }; 77 const int dst1[] = { 5, 5 }; 78 doDraw(canvas, font, &paint, src1, dst1, 1); 79 canvas->restore(); 80 81 canvas->save(); 82 canvas->translate(SkIntToScalar(160), SkIntToScalar(10)); 83 // rotate/uniform-scale (2 points) 84 const int src2[] = { 32, 32, 64, 32 }; 85 const int dst2[] = { 32, 32, 64, 48 }; 86 doDraw(canvas, font, &paint, src2, dst2, 2); 87 canvas->restore(); 88 89 canvas->save(); 90 canvas->translate(SkIntToScalar(10), SkIntToScalar(110)); 91 // rotate/skew (3 points) 92 const int src3[] = { 0, 0, 64, 0, 0, 64 }; 93 const int dst3[] = { 0, 0, 96, 0, 24, 64 }; 94 doDraw(canvas, font, &paint, src3, dst3, 3); 95 canvas->restore(); 96 97 canvas->save(); 98 canvas->translate(SkIntToScalar(160), SkIntToScalar(110)); 99 // perspective (4 points) 100 const int src4[] = { 0, 0, 64, 0, 64, 64, 0, 64 }; 101 const int dst4[] = { 0, 0, 96, 0, 64, 96, 0, 64 }; 102 doDraw(canvas, font, &paint, src4, dst4, 4); 103 canvas->restore(); 104 } 105 106 private: 107 typedef skiagm::GM INHERITED; 108 sk_sp<SkTypeface> fEmFace; 109 }; 110 111 ////////////////////////////////////////////////////////////////////////////// 112 113 DEF_GM( return new Poly2PolyGM; ) 114