blob: 0160a3eb4c15562d245a91012317b297850dd863 [file] [log] [blame]
joshualitt44c48512015-08-01 07:33:41 -07001/*
2 * Copyright 2015 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkColor.h"
11#include "include/core/SkColorSpace.h"
12#include "include/core/SkFont.h"
13#include "include/core/SkFontStyle.h"
14#include "include/core/SkFontTypes.h"
15#include "include/core/SkImageInfo.h"
16#include "include/core/SkPaint.h"
17#include "include/core/SkRect.h"
18#include "include/core/SkRefCnt.h"
19#include "include/core/SkScalar.h"
20#include "include/core/SkSize.h"
21#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040023#include "include/core/SkSurfaceProps.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "include/core/SkTextBlob.h"
25#include "include/core/SkTypeface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040026#include "include/gpu/GrContext.h"
27#include "tools/ToolUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "tools/fonts/RandomScalerContext.h"
joshualitt44c48512015-08-01 07:33:41 -070029
Ben Wagner7fde8e12019-05-01 17:28:53 -040030#include <string.h>
31#include <utility>
32
33class GrRenderTargetContext;
joshualitt44c48512015-08-01 07:33:41 -070034
35namespace skiagm {
Chris Dalton3a778372019-02-07 15:23:36 -070036class TextBlobRandomFont : public GpuGM {
joshualitt44c48512015-08-01 07:33:41 -070037public:
38 // This gm tests that textblobs can be translated and scaled with a font that returns random
39 // but deterministic masks
40 TextBlobRandomFont() { }
41
42protected:
43 void onOnceBeforeDraw() override {
44 SkTextBlobBuilder builder;
45
46 const char* text = "The quick brown fox jumps over the lazy dog.";
47
joshualitt44c48512015-08-01 07:33:41 -070048 SkPaint paint;
Ben Wagner20fa1e92018-04-30 15:39:15 -040049 paint.setAntiAlias(true);
Mike Reedea8900e2018-12-22 17:37:30 -050050 paint.setColor(SK_ColorMAGENTA);
51
52 // make textbloben
53 SkFont font;
54 font.setSize(32);
55 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
joshualitt44c48512015-08-01 07:33:41 -070056
57 // Setup our random scaler context
Mike Kleinea3f0142019-03-20 11:12:10 -050058 auto typeface = ToolUtils::create_portable_typeface("sans-serif", SkFontStyle::Bold());
Ben Wagner20fa1e92018-04-30 15:39:15 -040059 if (!typeface) {
60 typeface = SkTypeface::MakeDefault();
joshualitt44c48512015-08-01 07:33:41 -070061 }
Mike Reedea8900e2018-12-22 17:37:30 -050062 font.setTypeface(sk_make_sp<SkRandomTypeface>(std::move(typeface), paint, false));
joshualitt44c48512015-08-01 07:33:41 -070063
Ben Wagner20fa1e92018-04-30 15:39:15 -040064 SkScalar y = 0;
joshualitt44c48512015-08-01 07:33:41 -070065 SkRect bounds;
Ben Wagner51e15a62019-05-07 15:38:46 -040066 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
Ben Wagner20fa1e92018-04-30 15:39:15 -040067 y -= bounds.fTop;
Mike Kleinea3f0142019-03-20 11:12:10 -050068 ToolUtils::add_to_text_blob(&builder, text, font, 0, y);
Ben Wagner20fa1e92018-04-30 15:39:15 -040069 y += bounds.fBottom;
joshualitt44c48512015-08-01 07:33:41 -070070
71 // A8
joshualittd45fb5a2015-08-01 10:33:40 -070072 const char* bigtext1 = "The quick brown fox";
73 const char* bigtext2 = "jumps over the lazy dog.";
Mike Reedea8900e2018-12-22 17:37:30 -050074 font.setSize(160);
75 font.setSubpixel(false);
76 font.setEdging(SkFont::Edging::kAntiAlias);
Ben Wagner51e15a62019-05-07 15:38:46 -040077 font.measureText(bigtext1, strlen(bigtext1), SkTextEncoding::kUTF8, &bounds);
Ben Wagner20fa1e92018-04-30 15:39:15 -040078 y -= bounds.fTop;
Mike Kleinea3f0142019-03-20 11:12:10 -050079 ToolUtils::add_to_text_blob(&builder, bigtext1, font, 0, y);
Ben Wagner20fa1e92018-04-30 15:39:15 -040080 y += bounds.fBottom;
joshualittd45fb5a2015-08-01 10:33:40 -070081
Ben Wagner51e15a62019-05-07 15:38:46 -040082 font.measureText(bigtext2, strlen(bigtext2), SkTextEncoding::kUTF8, &bounds);
Ben Wagner20fa1e92018-04-30 15:39:15 -040083 y -= bounds.fTop;
Mike Kleinea3f0142019-03-20 11:12:10 -050084 ToolUtils::add_to_text_blob(&builder, bigtext2, font, 0, y);
Ben Wagner20fa1e92018-04-30 15:39:15 -040085 y += bounds.fBottom;
joshualittd45fb5a2015-08-01 10:33:40 -070086
87 // color emoji
Mike Kleinea3f0142019-03-20 11:12:10 -050088 if (sk_sp<SkTypeface> origEmoji = ToolUtils::emoji_typeface()) {
Mike Reedea8900e2018-12-22 17:37:30 -050089 font.setTypeface(sk_make_sp<SkRandomTypeface>(origEmoji, paint, false));
Mike Kleinea3f0142019-03-20 11:12:10 -050090 const char* emojiText = ToolUtils::emoji_sample_text();
Ben Wagner51e15a62019-05-07 15:38:46 -040091 font.measureText(emojiText, strlen(emojiText), SkTextEncoding::kUTF8, &bounds);
Ben Wagner20fa1e92018-04-30 15:39:15 -040092 y -= bounds.fTop;
Mike Kleinea3f0142019-03-20 11:12:10 -050093 ToolUtils::add_to_text_blob(&builder, emojiText, font, 0, y);
Ben Wagner20fa1e92018-04-30 15:39:15 -040094 y += bounds.fBottom;
joshualittd45fb5a2015-08-01 10:33:40 -070095 }
joshualitt44c48512015-08-01 07:33:41 -070096
97 // build
fmalita37283c22016-09-13 10:00:23 -070098 fBlob = builder.make();
joshualitt44c48512015-08-01 07:33:41 -070099 }
100
101 SkString onShortName() override {
102 return SkString("textblobrandomfont");
103 }
104
105 SkISize onISize() override {
106 return SkISize::Make(kWidth, kHeight);
107 }
108
Chris Dalton50e24d72019-02-07 16:20:09 -0700109 DrawResult onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas,
110 SkString* errorMsg) override {
joshualitt44c48512015-08-01 07:33:41 -0700111 // This GM exists to test a specific feature of the GPU backend.
Mike Kleinea3f0142019-03-20 11:12:10 -0500112 // This GM uses ToolUtils::makeSurface which doesn't work well with vias.
Ben Wagner20fa1e92018-04-30 15:39:15 -0400113 // This GM uses SkRandomTypeface which doesn't work well with serialization.
Mike Kleind46dce32018-08-16 10:17:03 -0400114 canvas->drawColor(SK_ColorWHITE);
joshualitt44c48512015-08-01 07:33:41 -0700115
Brian Osmanf9412802016-12-02 12:07:17 -0500116 SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, canvas->imageInfo().colorType(),
117 kPremul_SkAlphaType,
Mike Reed693fdbd2017-01-12 10:13:40 -0500118 canvas->imageInfo().refColorSpace());
brianosman3a0dbde2016-07-26 11:36:05 -0700119 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
Mike Kleinea3f0142019-03-20 11:12:10 -0500120 auto surface(ToolUtils::makeSurface(canvas, info, &props));
Ben Wagner20fa1e92018-04-30 15:39:15 -0400121 if (!surface) {
Chris Dalton50e24d72019-02-07 16:20:09 -0700122 *errorMsg = "This test requires a surface";
123 return DrawResult::kFail;
Ben Wagner20fa1e92018-04-30 15:39:15 -0400124 }
125
126 SkPaint paint;
127 paint.setAntiAlias(true);
128
Jim Van Verth30f64e42018-05-24 14:32:26 -0400129 SkCanvas* surfaceCanvas = surface->getCanvas();
Ben Wagner20fa1e92018-04-30 15:39:15 -0400130
131 SkScalar stride = SkScalarCeilToScalar(fBlob->bounds().height());
132 SkScalar yOffset = 5;
Ben Wagner20fa1e92018-04-30 15:39:15 -0400133
Jim Van Verth30f64e42018-05-24 14:32:26 -0400134 canvas->save();
135 // Originally we would alternate between rotating and not to force blob regeneration,
136 // but that code seems to have rotted. Keeping the rotate to match the old GM as
137 // much as possible, and it seems like a reasonable stress test for transformed
138 // color emoji.
139 canvas->rotate(-0.05f);
140 canvas->drawTextBlob(fBlob, 10, yOffset, paint);
141 yOffset += stride;
142 canvas->restore();
Ben Wagner20fa1e92018-04-30 15:39:15 -0400143
Chris Dalton3a778372019-02-07 15:23:36 -0700144 // Rotate in the surface canvas, not the final canvas, to avoid aliasing
145 surfaceCanvas->rotate(-0.05f);
146 surfaceCanvas->drawTextBlob(fBlob, 10, yOffset, paint);
147 surface->draw(canvas, 0, 0, nullptr);
Jim Van Verth30f64e42018-05-24 14:32:26 -0400148 yOffset += stride;
149
150 // free gpu resources and verify
Chris Dalton3a778372019-02-07 15:23:36 -0700151 context->freeGpuResources();
Jim Van Verth30f64e42018-05-24 14:32:26 -0400152
153 canvas->rotate(-0.05f);
154 canvas->drawTextBlob(fBlob, 10, yOffset, paint);
155 yOffset += stride;
Chris Dalton50e24d72019-02-07 16:20:09 -0700156 return DrawResult::kOk;
joshualitt44c48512015-08-01 07:33:41 -0700157 }
158
159private:
fmalita37283c22016-09-13 10:00:23 -0700160 sk_sp<SkTextBlob> fBlob;
joshualitt44c48512015-08-01 07:33:41 -0700161
mtkleindbfd7ab2016-09-01 11:24:54 -0700162 static constexpr int kWidth = 2000;
163 static constexpr int kHeight = 1600;
joshualitt44c48512015-08-01 07:33:41 -0700164
165 typedef GM INHERITED;
166};
167
168//////////////////////////////////////////////////////////////////////////////
169
halcanary385fe4d2015-08-26 13:07:48 -0700170DEF_GM(return new TextBlobRandomFont;)
joshualitt44c48512015-08-01 07:33:41 -0700171}