Factor code to rotate a canvas about a point.
[platform/upstream/libSkiaSharp.git] / gm / fontscaler.cpp
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 #include "gm.h"
8 #include "SkTypeface.h"
9
10 namespace skiagm {
11
12 class FontScalerGM : public GM {
13 public:
14     FontScalerGM() {
15         this->setBGColor(0xFFFFFFFF);
16     }
17
18 protected:
19
20     SkString onShortName() override {
21         SkString name("fontscaler");
22         name.append(sk_tool_utils::major_platform_os_name());
23         return name;
24     }
25
26     SkISize onISize() override {
27         return SkISize::Make(1450, 750);
28     }
29
30     void onDraw(SkCanvas* canvas) override {
31         SkPaint paint;
32
33         paint.setAntiAlias(true);
34         paint.setLCDRenderText(true);
35         //With freetype the default (normal hinting) can be really ugly.
36         //Most distros now set slight (vertical hinting only) in any event.
37         paint.setHinting(SkPaint::kSlight_Hinting);
38
39         const char* text = "Hamburgefons ooo mmm";
40         const size_t textLen = strlen(text);
41
42         for (int j = 0; j < 2; ++j) {
43             // This used to do 6 iterations but it causes the N4 to crash in the MSAA4 config.
44             for (int i = 0; i < 5; ++i) {
45                 SkScalar x = SkIntToScalar(10);
46                 SkScalar y = SkIntToScalar(20);
47
48                 SkAutoCanvasRestore acr(canvas, true);
49                 canvas->translate(SkIntToScalar(50 + i * 230),
50                                   SkIntToScalar(20));
51                 canvas->rotate(SkIntToScalar(i * 5), x, y * 10);
52
53                 {
54                     SkPaint p;
55                     p.setAntiAlias(true);
56                     SkRect r;
57                     r.set(x - SkIntToScalar(3), SkIntToScalar(15),
58                           x - SkIntToScalar(1), SkIntToScalar(280));
59                     canvas->drawRect(r, p);
60                 }
61
62                 for (int ps = 6; ps <= 22; ps++) {
63                     paint.setTextSize(SkIntToScalar(ps));
64                     canvas->drawText(text, textLen, x, y, paint);
65                     y += paint.getFontMetrics(nullptr);
66                 }
67             }
68             canvas->translate(0, SkIntToScalar(360));
69             paint.setSubpixelText(true);
70         }
71     }
72
73 private:
74     typedef GM INHERITED;
75 };
76
77 //////////////////////////////////////////////////////////////////////////////
78
79 static GM* MyFactory(void*) { return new FontScalerGM; }
80 static GMRegistry reg(MyFactory);
81
82 }