• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright 2012 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 
9 #include "gm.h"
10 #include "SkCanvas.h"
11 #include "SkString.h"
12 #include "SkTypeface.h"
13 #include "SkTypes.h"
14 
15 namespace skiagm {
16 
17 const char* gFaces[] = {
18     "Times Roman",
19     "Hiragino Maru Gothic Pro",
20     "Papyrus",
21     "Helvetica",
22     "Courier New"
23 };
24 
25 class TypefaceGM : public GM {
26 public:
TypefaceGM()27     TypefaceGM() {
28         fFaces = new SkTypeface*[SK_ARRAY_COUNT(gFaces)];
29         for (size_t i = 0; i < SK_ARRAY_COUNT(gFaces); i++) {
30             fFaces[i] = SkTypeface::CreateFromName(gFaces[i], SkTypeface::kNormal);
31         }
32     }
33 
~TypefaceGM()34     virtual ~TypefaceGM() {
35         for (size_t i = 0; i < SK_ARRAY_COUNT(gFaces); i++) {
36             fFaces[i]->unref();
37         }
38         delete [] fFaces;
39     }
40 
41 protected:
onShortName()42     virtual SkString onShortName() SK_OVERRIDE {
43         return SkString("typeface");
44     }
45 
onISize()46     virtual SkISize onISize() SK_OVERRIDE {
47         return SkISize::Make(640, 480);
48     }
49 
onDraw(SkCanvas * canvas)50     virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
51         SkString text("Typefaces are fun!");
52         SkScalar y = 0;
53 
54         SkPaint paint;
55         paint.setAntiAlias(true);
56         for (size_t i = 0; i < SK_ARRAY_COUNT(gFaces); i++) {
57             this->drawWithFace(text, i, y, paint, canvas);
58         }
59         // Now go backwards
60         for (int i = SK_ARRAY_COUNT(gFaces) - 1; i >= 0; i--) {
61             this->drawWithFace(text, i, y, paint, canvas);
62         }
63     }
64 
65 private:
drawWithFace(const SkString & text,int i,SkScalar & y,SkPaint & paint,SkCanvas * canvas)66     void drawWithFace(const SkString& text, int i, SkScalar& y, SkPaint& paint,
67                       SkCanvas* canvas) {
68         paint.setTypeface(fFaces[i]);
69         y += paint.getFontMetrics(NULL);
70         canvas->drawText(text.c_str(), text.size(), 0, y, paint);
71     }
72 
73     SkTypeface** fFaces;
74 
75     typedef GM INHERITED;
76 };
77 
78 ////////////////////////////////////////////////////////////////////////////////
79 
MyFactory(void *)80 static GM* MyFactory(void*) { return new TypefaceGM; }
81 static GMRegistry reg(MyFactory);
82 
83 }   // skiagm
84