blob: ce83a3c531453657ab3be470b4ab7106b92fe93e [file] [log] [blame]
bungeman@google.com0f0c2882011-11-04 15:47:41 +00001/*
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 */
Ben Wagner7fde8e12019-05-01 17:28:53 -04007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkCanvas.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkFont.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040011#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
18#include <string.h>
bungeman@google.com0f0c2882011-11-04 15:47:41 +000019
20namespace skiagm {
21
bungeman@google.com0f0c2882011-11-04 15:47:41 +000022class FontScalerGM : public GM {
23public:
24 FontScalerGM() {
25 this->setBGColor(0xFFFFFFFF);
bungeman@google.com0f0c2882011-11-04 15:47:41 +000026 }
27
bungeman@google.com0f0c2882011-11-04 15:47:41 +000028protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000029
mtklein36352bf2015-03-25 18:17:31 -070030 SkString onShortName() override {
Mike Kleinbea1f942019-03-08 11:11:55 -060031 return SkString("fontscaler");
bungeman@google.com0f0c2882011-11-04 15:47:41 +000032 }
33
mtklein36352bf2015-03-25 18:17:31 -070034 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070035 return SkISize::Make(1450, 750);
bungeman@google.com0f0c2882011-11-04 15:47:41 +000036 }
37
mtklein36352bf2015-03-25 18:17:31 -070038 void onDraw(SkCanvas* canvas) override {
Mike Reed2e6db182018-12-15 13:45:33 -050039 SkFont font;
40 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
bungeman@google.com0f0c2882011-11-04 15:47:41 +000041 //With freetype the default (normal hinting) can be really ugly.
42 //Most distros now set slight (vertical hinting only) in any event.
Ben Wagner5785e4a2019-05-07 16:50:29 -040043 font.setHinting(SkFontHinting::kSlight);
bungeman@google.com0f0c2882011-11-04 15:47:41 +000044
bungeman@google.com0f0c2882011-11-04 15:47:41 +000045 const char* text = "Hamburgefons ooo mmm";
46 const size_t textLen = strlen(text);
47
48 for (int j = 0; j < 2; ++j) {
bsalomon@google.com4121f952013-09-09 16:19:40 +000049 // This used to do 6 iterations but it causes the N4 to crash in the MSAA4 config.
50 for (int i = 0; i < 5; ++i) {
bungeman@google.com0f0c2882011-11-04 15:47:41 +000051 SkScalar x = SkIntToScalar(10);
52 SkScalar y = SkIntToScalar(20);
53
54 SkAutoCanvasRestore acr(canvas, true);
55 canvas->translate(SkIntToScalar(50 + i * 230),
56 SkIntToScalar(20));
bungeman7438bfc2016-07-12 15:01:19 -070057 canvas->rotate(SkIntToScalar(i * 5), x, y * 10);
bungeman@google.com0f0c2882011-11-04 15:47:41 +000058
59 {
60 SkPaint p;
61 p.setAntiAlias(true);
62 SkRect r;
bungeman@google.coma682b932012-01-05 18:10:48 +000063 r.set(x - SkIntToScalar(3), SkIntToScalar(15),
64 x - SkIntToScalar(1), SkIntToScalar(280));
bungeman@google.com0f0c2882011-11-04 15:47:41 +000065 canvas->drawRect(r, p);
66 }
67
bungeman@google.com0f0c2882011-11-04 15:47:41 +000068 for (int ps = 6; ps <= 22; ps++) {
Mike Reed2e6db182018-12-15 13:45:33 -050069 font.setSize(SkIntToScalar(ps));
Ben Wagner51e15a62019-05-07 15:38:46 -040070 canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, x, y, font, SkPaint());
Mike Reed2e6db182018-12-15 13:45:33 -050071 y += font.getMetrics(nullptr);
bungeman@google.com0f0c2882011-11-04 15:47:41 +000072 }
73 }
bungeman@google.coma682b932012-01-05 18:10:48 +000074 canvas->translate(0, SkIntToScalar(360));
Mike Reed2e6db182018-12-15 13:45:33 -050075 font.setSubpixel(true);
Ben Wagnerf460eee2019-04-18 16:37:45 -040076 font.setLinearMetrics(true);
bungeman@google.com0f0c2882011-11-04 15:47:41 +000077 }
78 }
79
80private:
bungeman@google.com0f0c2882011-11-04 15:47:41 +000081 typedef GM INHERITED;
82};
83
84//////////////////////////////////////////////////////////////////////////////
borenet@google.com0682ca62012-06-26 17:23:52 +000085
Hal Canarye964c182019-01-23 10:22:01 -050086DEF_GM( return new FontScalerGM; )
borenet@google.com0682ca62012-06-26 17:23:52 +000087
bungeman@google.com0f0c2882011-11-04 15:47:41 +000088}