blob: 8ae63dd69dbd6dd775a62de4a324663200c627da [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) {
Mike Reed92b33352019-08-24 19:39:13 -040051 SkScalar x = 10;
52 SkScalar y = 20;
bungeman@google.com0f0c2882011-11-04 15:47:41 +000053
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;
Mike Reed92b33352019-08-24 19:39:13 -040063 r.setLTRB(x - 3, 15, x - 1, 280);
bungeman@google.com0f0c2882011-11-04 15:47:41 +000064 canvas->drawRect(r, p);
65 }
66
bungeman@google.com0f0c2882011-11-04 15:47:41 +000067 for (int ps = 6; ps <= 22; ps++) {
Mike Reed2e6db182018-12-15 13:45:33 -050068 font.setSize(SkIntToScalar(ps));
Ben Wagner51e15a62019-05-07 15:38:46 -040069 canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, x, y, font, SkPaint());
Mike Reed2e6db182018-12-15 13:45:33 -050070 y += font.getMetrics(nullptr);
bungeman@google.com0f0c2882011-11-04 15:47:41 +000071 }
72 }
bungeman@google.coma682b932012-01-05 18:10:48 +000073 canvas->translate(0, SkIntToScalar(360));
Mike Reed2e6db182018-12-15 13:45:33 -050074 font.setSubpixel(true);
Ben Wagnerf460eee2019-04-18 16:37:45 -040075 font.setLinearMetrics(true);
Ben Wagner54aa8842019-08-27 16:20:39 -040076 font.setBaselineSnap(false);
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}