xref: /aosp_15_r20/external/skia/gm/fontscaler.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkFont.h"
11 #include "include/core/SkFontTypes.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkRect.h"
14 #include "include/core/SkScalar.h"
15 #include "include/core/SkSize.h"
16 #include "include/core/SkString.h"
17 #include "tools/fonts/FontToolUtils.h"
18 
19 #include <string.h>
20 
21 namespace skiagm {
22 
23 class FontScalerGM : public GM {
24 public:
FontScalerGM()25     FontScalerGM() {
26         this->setBGColor(0xFFFFFFFF);
27     }
28 
29 protected:
getName() const30     SkString getName() const override { return SkString("fontscaler"); }
31 
getISize()32     SkISize getISize() override { return SkISize::Make(1450, 750); }
33 
onDraw(SkCanvas * canvas)34     void onDraw(SkCanvas* canvas) override {
35         SkFont font = ToolUtils::DefaultPortableFont();
36         font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
37         //With freetype the default (normal hinting) can be really ugly.
38         //Most distros now set slight (vertical hinting only) in any event.
39         font.setHinting(SkFontHinting::kSlight);
40 
41         const char* text = "Hamburgefons ooo mmm";
42         const size_t textLen = strlen(text);
43 
44         for (int j = 0; j < 2; ++j) {
45             // This used to do 6 iterations but it causes the N4 to crash in the MSAA4 config.
46             for (int i = 0; i < 5; ++i) {
47                 SkScalar x = 10;
48                 SkScalar y = 20;
49 
50                 SkAutoCanvasRestore acr(canvas, true);
51                 canvas->translate(SkIntToScalar(50 + i * 230),
52                                   SkIntToScalar(20));
53                 canvas->rotate(SkIntToScalar(i * 5), x, y * 10);
54 
55                 {
56                     SkPaint p;
57                     p.setAntiAlias(true);
58                     SkRect r;
59                     r.setLTRB(x - 3, 15, x - 1, 280);
60                     canvas->drawRect(r, p);
61                 }
62 
63                 for (int ps = 6; ps <= 22; ps++) {
64                     font.setSize(SkIntToScalar(ps));
65                     canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, x, y, font, SkPaint());
66                     y += font.getMetrics(nullptr);
67                 }
68             }
69             canvas->translate(0, SkIntToScalar(360));
70             font.setSubpixel(true);
71             font.setLinearMetrics(true);
72             font.setBaselineSnap(false);
73         }
74     }
75 
76 private:
77     using INHERITED = GM;
78 };
79 
80 //////////////////////////////////////////////////////////////////////////////
81 
82 DEF_GM( return new FontScalerGM; )
83 
84 }  // namespace skiagm
85