blob: 2e6468a6fce3409419482e43bd444443999bc358 [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
8#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
joshualitt44c48512015-08-01 07:33:41 -070010
11#include "Resources.h"
12#include "SkCanvas.h"
13#include "SkGradientShader.h"
Mike Klein10d66cc2017-11-10 11:33:43 -050014#include "SkRandomScalerContext.h"
joshualitt44c48512015-08-01 07:33:41 -070015#include "SkStream.h"
16#include "SkSurface.h"
17#include "SkTextBlob.h"
18#include "SkTypeface.h"
joshualitt44c48512015-08-01 07:33:41 -070019
20#if SK_SUPPORT_GPU
21
22#include "GrContext.h"
23
24namespace skiagm {
25class TextBlobRandomFont : public GM {
26public:
27 // This gm tests that textblobs can be translated and scaled with a font that returns random
28 // but deterministic masks
29 TextBlobRandomFont() { }
30
31protected:
32 void onOnceBeforeDraw() override {
33 SkTextBlobBuilder builder;
34
35 const char* text = "The quick brown fox jumps over the lazy dog.";
36
37 // make textbloben
38 SkPaint paint;
39 paint.setTextSize(32);
40 paint.setLCDRenderText(true);
41
42 // Setup our random scaler context
mbocee6a9912016-05-31 11:42:36 -070043 sk_sp<SkTypeface> orig(sk_tool_utils::create_portable_typeface(
Ben Wagner71319502017-07-27 10:45:29 -040044 "sans-serif", SkFontStyle::Bold()));
halcanary96fcdcc2015-08-27 07:41:13 -070045 if (nullptr == orig) {
bungeman13b9c952016-05-12 10:09:30 -070046 orig = SkTypeface::MakeDefault();
joshualitt44c48512015-08-01 07:33:41 -070047 }
bungeman13b9c952016-05-12 10:09:30 -070048 paint.setTypeface(sk_make_sp<SkRandomTypeface>(orig, paint, false));
joshualitt44c48512015-08-01 07:33:41 -070049
50 SkRect bounds;
51 paint.measureText(text, strlen(text), &bounds);
joshualitt44c48512015-08-01 07:33:41 -070052 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, 0);
53
54 // A8
joshualittd45fb5a2015-08-01 10:33:40 -070055 const char* bigtext1 = "The quick brown fox";
56 const char* bigtext2 = "jumps over the lazy dog.";
57 paint.setTextSize(160);
joshualitt44c48512015-08-01 07:33:41 -070058 paint.setSubpixelText(false);
59 paint.setLCDRenderText(false);
joshualittd45fb5a2015-08-01 10:33:40 -070060 paint.measureText(bigtext1, strlen(bigtext1), &bounds);
61 SkScalar offset = bounds.height();
62 sk_tool_utils::add_to_text_blob(&builder, bigtext1, paint, 0, offset);
63
64 paint.measureText(bigtext2, strlen(bigtext2), &bounds);
65 offset += bounds.height();
66 sk_tool_utils::add_to_text_blob(&builder, bigtext2, paint, 0, offset);
67
68 // color emoji
Mike Kleinb251b722017-11-13 11:03:16 -050069 if (sk_sp<SkTypeface> origEmoji = sk_tool_utils::emoji_typeface()) {
joshualittd45fb5a2015-08-01 10:33:40 -070070 const char* emojiText = sk_tool_utils::emoji_sample_text();
71 paint.measureText(emojiText, strlen(emojiText), &bounds);
72 offset += bounds.height();
Mike Kleinb251b722017-11-13 11:03:16 -050073 paint.setTypeface(sk_make_sp<SkRandomTypeface>(origEmoji, paint, false));
joshualittd45fb5a2015-08-01 10:33:40 -070074 sk_tool_utils::add_to_text_blob(&builder, emojiText, paint, 0, offset);
75 }
joshualitt44c48512015-08-01 07:33:41 -070076
77 // build
fmalita37283c22016-09-13 10:00:23 -070078 fBlob = builder.make();
joshualitt44c48512015-08-01 07:33:41 -070079 }
80
81 SkString onShortName() override {
82 return SkString("textblobrandomfont");
83 }
84
85 SkISize onISize() override {
86 return SkISize::Make(kWidth, kHeight);
87 }
88
89 void onDraw(SkCanvas* canvas) override {
90 // This GM exists to test a specific feature of the GPU backend.
halcanary96fcdcc2015-08-27 07:41:13 -070091 if (nullptr == canvas->getGrContext()) {
halcanary2a243382015-09-09 08:16:41 -070092 skiagm::GM::DrawGpuOnlyMessage(canvas);
joshualitt44c48512015-08-01 07:33:41 -070093 return;
94 }
95
96 canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorWHITE));
97
Brian Osmanf9412802016-12-02 12:07:17 -050098 SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, canvas->imageInfo().colorType(),
99 kPremul_SkAlphaType,
Mike Reed693fdbd2017-01-12 10:13:40 -0500100 canvas->imageInfo().refColorSpace());
brianosman3a0dbde2016-07-26 11:36:05 -0700101 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
Mike Reed46596ae2018-01-02 15:40:29 -0500102 auto surface(sk_tool_utils::makeSurface(canvas, info, &props));
joshualitt44c48512015-08-01 07:33:41 -0700103 if (surface) {
104 SkPaint paint;
105 paint.setAntiAlias(true);
106
107 SkCanvas* c = surface->getCanvas();
108
joshualittd45fb5a2015-08-01 10:33:40 -0700109 int stride = SkScalarCeilToInt(fBlob->bounds().height());
110 int yOffset = stride / 8;
111 for (int i = 0; i < 1; i++) {
joshualitt44c48512015-08-01 07:33:41 -0700112 // fiddle the canvas to force regen of textblobs
113 canvas->rotate(i % 2 ? 0.0f : -0.05f);
114 canvas->drawTextBlob(fBlob, 10.0f, SkIntToScalar(yOffset), paint);
115 yOffset += stride;
116
117 // This will draw as black boxes
118 c->drawTextBlob(fBlob, 10, SkIntToScalar(yOffset), paint);
119 surface->draw(canvas, 0, 0, nullptr);
120
121 // free gpu resources and verify
122 yOffset += stride;
123 canvas->getGrContext()->freeGpuResources();
124 canvas->drawTextBlob(fBlob, 10, SkIntToScalar(yOffset), paint);
125
126 yOffset += stride;
127 }
128
129 } else {
130 const char* text = "This test requires a surface";
131 size_t len = strlen(text);
132 SkPaint paint;
133 canvas->drawText(text, len, 10, 100, paint);
134 }
135 }
136
137private:
fmalita37283c22016-09-13 10:00:23 -0700138 sk_sp<SkTextBlob> fBlob;
joshualitt44c48512015-08-01 07:33:41 -0700139
mtkleindbfd7ab2016-09-01 11:24:54 -0700140 static constexpr int kWidth = 2000;
141 static constexpr int kHeight = 1600;
joshualitt44c48512015-08-01 07:33:41 -0700142
143 typedef GM INHERITED;
144};
145
146//////////////////////////////////////////////////////////////////////////////
147
halcanary385fe4d2015-08-26 13:07:48 -0700148DEF_GM(return new TextBlobRandomFont;)
joshualitt44c48512015-08-01 07:33:41 -0700149}
150#endif