1/*
2 * Copyright 2013 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#include "gm.h"
8#include "SkBitmapDevice.h"
9#include "SkTypeface.h"
10
11namespace skiagm {
12
13class DevicePropertiesGM : public GM {
14public:
15    DevicePropertiesGM() {
16        this->setBGColor(0xFFFFFFFF);
17    }
18
19    virtual ~DevicePropertiesGM() {
20    }
21
22protected:
23    virtual SkString onShortName() {
24        return SkString("deviceproperties");
25    }
26
27    virtual SkISize onISize() {
28        return SkISize::Make(1450, 750);
29    }
30
31    static void rotate_about(SkCanvas* canvas,
32                             SkScalar degrees,
33                             SkScalar px, SkScalar py) {
34        canvas->translate(px, py);
35        canvas->rotate(degrees);
36        canvas->translate(-px, -py);
37    }
38
39    virtual void onDraw(SkCanvas* originalCanvas) {
40        SkISize size = this->getISize();
41        SkBitmap bitmap;
42        bitmap.allocN32Pixels(size.width(), size.height());
43        SkDeviceProperties properties = SkDeviceProperties::Make(
44            SkDeviceProperties::Geometry::Make(SkDeviceProperties::Geometry::kVertical_Orientation,
45                                               SkDeviceProperties::Geometry::kBGR_Layout),
46            SK_Scalar1);
47        SkBitmapDevice device(bitmap, properties);
48        SkCanvas canvas(&device);
49        canvas.drawColor(SK_ColorWHITE);
50
51        SkPaint paint;
52
53        paint.setAntiAlias(true);
54        paint.setLCDRenderText(true);
55        //With freetype the default (normal hinting) can be really ugly.
56        //Most distros now set slight (vertical hinting only) in any event.
57        paint.setHinting(SkPaint::kSlight_Hinting);
58        SkSafeUnref(paint.setTypeface(SkTypeface::CreateFromName("Times Roman", SkTypeface::kNormal)));
59
60        const char* text = "Hamburgefons ooo mmm";
61        const size_t textLen = strlen(text);
62
63        for (int j = 0; j < 2; ++j) {
64            for (int i = 0; i < 6; ++i) {
65                SkScalar x = SkIntToScalar(10);
66                SkScalar y = SkIntToScalar(20);
67
68                SkAutoCanvasRestore acr(&canvas, true);
69                canvas.translate(SkIntToScalar(50 + i * 230),
70                                  SkIntToScalar(20));
71                rotate_about(&canvas, SkIntToScalar(i * 5), x, y * 10);
72
73                {
74                    SkPaint p;
75                    p.setAntiAlias(true);
76                    SkRect r;
77                    r.set(x - SkIntToScalar(3), SkIntToScalar(15),
78                          x - SkIntToScalar(1), SkIntToScalar(280));
79                    canvas.drawRect(r, p);
80                }
81
82                int index = 0;
83                for (int ps = 6; ps <= 22; ps++) {
84                    paint.setTextSize(SkIntToScalar(ps));
85                    canvas.drawText(text, textLen, x, y, paint);
86                    y += paint.getFontMetrics(NULL);
87                    index += 1;
88                }
89            }
90            canvas.translate(0, SkIntToScalar(360));
91            paint.setSubpixelText(true);
92        }
93        originalCanvas->drawBitmap(bitmap, 0, 0);
94    }
95
96#ifdef SK_BUILD_FOR_ANDROID
97    virtual uint32_t onGetFlags() const SK_OVERRIDE {
98        // On android, we fail due to bad gpu drivers (it seems) by adding too
99        // much to our text atlas (texture).
100        return kSkipGPU_Flag;
101    }
102#endif
103
104private:
105    typedef GM INHERITED;
106};
107
108//////////////////////////////////////////////////////////////////////////////
109
110static GM* MyFactory(void*) { return new DevicePropertiesGM; }
111static GMRegistry reg(MyFactory);
112
113}
114