blob: c1e754c290005d0aeffcd41fe20d054b8e816995 [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
42 SkAutoTUnref<SkTypeface> orig(sk_tool_utils::create_portable_typeface("sans-serif",
43 SkTypeface::kBold));
44 if (NULL == orig) {
45 orig.reset(SkTypeface::RefDefault());
46 }
47 SkAutoTUnref<SkTypeface> random(SkNEW_ARGS(SkRandomTypeface, (orig, paint, false)));
48 paint.setTypeface(random);
49
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
69 SkAutoTUnref<SkTypeface> origEmoji;
70 sk_tool_utils::emoji_typeface(&origEmoji);
71 const char* osName = sk_tool_utils::platform_os_name();
72 // The mac emoji string will break us
73 if (origEmoji && (!strcmp(osName, "Android") || !strcmp(osName, "Ubuntu"))) {
74 const char* emojiText = sk_tool_utils::emoji_sample_text();
75 paint.measureText(emojiText, strlen(emojiText), &bounds);
76 offset += bounds.height();
77 SkAutoTUnref<SkTypeface> randomEmoji(SkNEW_ARGS(SkRandomTypeface, (orig, paint,
78 false)));
79 paint.setTypeface(randomEmoji);
80 sk_tool_utils::add_to_text_blob(&builder, emojiText, paint, 0, offset);
81 }
joshualitt44c48512015-08-01 07:33:41 -070082
83 // build
84 fBlob.reset(builder.build());
85 }
86
87 SkString onShortName() override {
88 return SkString("textblobrandomfont");
89 }
90
91 SkISize onISize() override {
92 return SkISize::Make(kWidth, kHeight);
93 }
94
95 void onDraw(SkCanvas* canvas) override {
96 // This GM exists to test a specific feature of the GPU backend.
97 if (NULL == canvas->getGrContext()) {
98 this->drawGpuOnlyMessage(canvas);
99 return;
100 }
101
102 canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorWHITE));
103
104 SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight);
105 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
106 SkAutoTUnref<SkSurface> surface(canvas->newSurface(info, &props));
107 if (surface) {
108 SkPaint paint;
109 paint.setAntiAlias(true);
110
111 SkCanvas* c = surface->getCanvas();
112
joshualittd45fb5a2015-08-01 10:33:40 -0700113 int stride = SkScalarCeilToInt(fBlob->bounds().height());
114 int yOffset = stride / 8;
115 for (int i = 0; i < 1; i++) {
joshualitt44c48512015-08-01 07:33:41 -0700116 // fiddle the canvas to force regen of textblobs
117 canvas->rotate(i % 2 ? 0.0f : -0.05f);
118 canvas->drawTextBlob(fBlob, 10.0f, SkIntToScalar(yOffset), paint);
119 yOffset += stride;
120
121 // This will draw as black boxes
122 c->drawTextBlob(fBlob, 10, SkIntToScalar(yOffset), paint);
123 surface->draw(canvas, 0, 0, nullptr);
124
125 // free gpu resources and verify
126 yOffset += stride;
127 canvas->getGrContext()->freeGpuResources();
128 canvas->drawTextBlob(fBlob, 10, SkIntToScalar(yOffset), paint);
129
130 yOffset += stride;
131 }
132
133 } else {
134 const char* text = "This test requires a surface";
135 size_t len = strlen(text);
136 SkPaint paint;
137 canvas->drawText(text, len, 10, 100, paint);
138 }
139 }
140
141private:
142 SkAutoTUnref<const SkTextBlob> fBlob;
143
joshualittd45fb5a2015-08-01 10:33:40 -0700144 static const int kWidth = 2000;
145 static const int kHeight = 1600;
joshualitt44c48512015-08-01 07:33:41 -0700146
147 typedef GM INHERITED;
148};
149
150//////////////////////////////////////////////////////////////////////////////
151
152DEF_GM( return SkNEW(TextBlobRandomFont); )
153}
154#endif