blob: a2e46972b417a6629f6628dad7f0e2245abdcdc4 [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"
9
10#include "Resources.h"
11#include "SkCanvas.h"
12#include "SkGradientShader.h"
13#include "SkStream.h"
14#include "SkSurface.h"
15#include "SkTextBlob.h"
16#include "SkTypeface.h"
17#include "../src/fonts/SkRandomScalerContext.h"
18
19#if SK_SUPPORT_GPU
20
21#include "GrContext.h"
22
23namespace skiagm {
24class TextBlobRandomFont : public GM {
25public:
26 // This gm tests that textblobs can be translated and scaled with a font that returns random
27 // but deterministic masks
28 TextBlobRandomFont() { }
29
30protected:
31 void onOnceBeforeDraw() override {
32 SkTextBlobBuilder builder;
33
34 const char* text = "The quick brown fox jumps over the lazy dog.";
35
36 // make textbloben
37 SkPaint paint;
38 paint.setTextSize(32);
39 paint.setLCDRenderText(true);
40
41 // Setup our random scaler context
mbocee6a9912016-05-31 11:42:36 -070042 sk_sp<SkTypeface> orig(sk_tool_utils::create_portable_typeface(
43 "sans-serif", SkFontStyle::FromOldStyle(SkTypeface::kBold)));
halcanary96fcdcc2015-08-27 07:41:13 -070044 if (nullptr == orig) {
bungeman13b9c952016-05-12 10:09:30 -070045 orig = SkTypeface::MakeDefault();
joshualitt44c48512015-08-01 07:33:41 -070046 }
bungeman13b9c952016-05-12 10:09:30 -070047 paint.setTypeface(sk_make_sp<SkRandomTypeface>(orig, paint, false));
joshualitt44c48512015-08-01 07:33:41 -070048
49 SkRect bounds;
50 paint.measureText(text, strlen(text), &bounds);
joshualitt44c48512015-08-01 07:33:41 -070051 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, 0);
52
53 // A8
joshualittd45fb5a2015-08-01 10:33:40 -070054 const char* bigtext1 = "The quick brown fox";
55 const char* bigtext2 = "jumps over the lazy dog.";
56 paint.setTextSize(160);
joshualitt44c48512015-08-01 07:33:41 -070057 paint.setSubpixelText(false);
58 paint.setLCDRenderText(false);
joshualittd45fb5a2015-08-01 10:33:40 -070059 paint.measureText(bigtext1, strlen(bigtext1), &bounds);
60 SkScalar offset = bounds.height();
61 sk_tool_utils::add_to_text_blob(&builder, bigtext1, paint, 0, offset);
62
63 paint.measureText(bigtext2, strlen(bigtext2), &bounds);
64 offset += bounds.height();
65 sk_tool_utils::add_to_text_blob(&builder, bigtext2, paint, 0, offset);
66
67 // color emoji
bungeman13b9c952016-05-12 10:09:30 -070068 sk_sp<SkTypeface> origEmoji = sk_tool_utils::emoji_typeface();
joshualittd45fb5a2015-08-01 10:33:40 -070069 const char* osName = sk_tool_utils::platform_os_name();
70 // The mac emoji string will break us
71 if (origEmoji && (!strcmp(osName, "Android") || !strcmp(osName, "Ubuntu"))) {
72 const char* emojiText = sk_tool_utils::emoji_sample_text();
73 paint.measureText(emojiText, strlen(emojiText), &bounds);
74 offset += bounds.height();
bungeman13b9c952016-05-12 10:09:30 -070075 paint.setTypeface(sk_make_sp<SkRandomTypeface>(orig, paint, false));
joshualittd45fb5a2015-08-01 10:33:40 -070076 sk_tool_utils::add_to_text_blob(&builder, emojiText, paint, 0, offset);
77 }
joshualitt44c48512015-08-01 07:33:41 -070078
79 // build
fmalita37283c22016-09-13 10:00:23 -070080 fBlob = builder.make();
joshualitt44c48512015-08-01 07:33:41 -070081 }
82
83 SkString onShortName() override {
84 return SkString("textblobrandomfont");
85 }
86
87 SkISize onISize() override {
88 return SkISize::Make(kWidth, kHeight);
89 }
90
91 void onDraw(SkCanvas* canvas) override {
92 // This GM exists to test a specific feature of the GPU backend.
halcanary96fcdcc2015-08-27 07:41:13 -070093 if (nullptr == canvas->getGrContext()) {
halcanary2a243382015-09-09 08:16:41 -070094 skiagm::GM::DrawGpuOnlyMessage(canvas);
joshualitt44c48512015-08-01 07:33:41 -070095 return;
96 }
97
98 canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorWHITE));
99
brianosman52ede1d2016-06-20 08:25:02 -0700100 SkImageInfo info = SkImageInfo::MakeN32(kWidth, kHeight, kPremul_SkAlphaType,
101 sk_ref_sp(canvas->imageInfo().colorSpace()));
brianosman3a0dbde2016-07-26 11:36:05 -0700102 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
reede8f30622016-03-23 18:59:25 -0700103 auto surface(canvas->makeSurface(info, &props));
joshualitt44c48512015-08-01 07:33:41 -0700104 if (surface) {
105 SkPaint paint;
106 paint.setAntiAlias(true);
107
108 SkCanvas* c = surface->getCanvas();
109
joshualittd45fb5a2015-08-01 10:33:40 -0700110 int stride = SkScalarCeilToInt(fBlob->bounds().height());
111 int yOffset = stride / 8;
112 for (int i = 0; i < 1; i++) {
joshualitt44c48512015-08-01 07:33:41 -0700113 // fiddle the canvas to force regen of textblobs
114 canvas->rotate(i % 2 ? 0.0f : -0.05f);
115 canvas->drawTextBlob(fBlob, 10.0f, SkIntToScalar(yOffset), paint);
116 yOffset += stride;
117
118 // This will draw as black boxes
119 c->drawTextBlob(fBlob, 10, SkIntToScalar(yOffset), paint);
120 surface->draw(canvas, 0, 0, nullptr);
121
122 // free gpu resources and verify
123 yOffset += stride;
124 canvas->getGrContext()->freeGpuResources();
125 canvas->drawTextBlob(fBlob, 10, SkIntToScalar(yOffset), paint);
126
127 yOffset += stride;
128 }
129
130 } else {
131 const char* text = "This test requires a surface";
132 size_t len = strlen(text);
133 SkPaint paint;
134 canvas->drawText(text, len, 10, 100, paint);
135 }
136 }
137
138private:
fmalita37283c22016-09-13 10:00:23 -0700139 sk_sp<SkTextBlob> fBlob;
joshualitt44c48512015-08-01 07:33:41 -0700140
mtkleindbfd7ab2016-09-01 11:24:54 -0700141 static constexpr int kWidth = 2000;
142 static constexpr int kHeight = 1600;
joshualitt44c48512015-08-01 07:33:41 -0700143
144 typedef GM INHERITED;
145};
146
147//////////////////////////////////////////////////////////////////////////////
148
halcanary385fe4d2015-08-26 13:07:48 -0700149DEF_GM(return new TextBlobRandomFont;)
joshualitt44c48512015-08-01 07:33:41 -0700150}
151#endif