blob: dbb82293660d23d0f226a9ef4a69a7bf5262a009 [file] [log] [blame]
joshualitteef5b3e2015-04-03 08:07:26 -07001/*
2 * Copyright 2013 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"
joshualitteef5b3e2015-04-03 08:07:26 -070010
11#include "Resources.h"
12#include "SkCanvas.h"
13#include "SkGradientShader.h"
14#include "SkStream.h"
15#include "SkTextBlob.h"
16#include "SkTypeface.h"
17
18namespace skiagm {
19
joshualitteef5b3e2015-04-03 08:07:26 -070020static void draw_blob(SkCanvas* canvas, const SkTextBlob* blob, const SkPaint& skPaint,
21 const SkRect& clipRect) {
22 SkPaint clipHairline;
23 clipHairline.setColor(SK_ColorWHITE);
24 clipHairline.setStyle(SkPaint::kStroke_Style);
25
26 SkPaint paint(skPaint);
27 canvas->save();
28 canvas->drawRect(clipRect, clipHairline);
29 paint.setAlpha(0x20);
30 canvas->drawTextBlob(blob, 0, 0, paint);
31 canvas->clipRect(clipRect);
32 paint.setAlpha(0xFF);
33 canvas->drawTextBlob(blob, 0, 0, paint);
34 canvas->restore();
35}
36
37class MixedTextBlobsGM : public GM {
38public:
39 MixedTextBlobsGM() { }
40
41protected:
42 void onOnceBeforeDraw() override {
bungeman13b9c952016-05-12 10:09:30 -070043 fEmojiTypeface = sk_tool_utils::emoji_typeface();
caryclarkc3dcb672015-07-21 12:27:36 -070044 fEmojiText = sk_tool_utils::emoji_sample_text();
bungeman13b9c952016-05-12 10:09:30 -070045 fReallyBigATypeface = MakeResourceAsTypeface("/fonts/ReallyBigA.ttf");
joshualitteef5b3e2015-04-03 08:07:26 -070046
47 SkTextBlobBuilder builder;
48
49 // make textblob
50 // Text so large we draw as paths
51 SkPaint paint;
jvanverth34d72882015-06-22 08:08:09 -070052 paint.setTextSize(385);
joshualitteef5b3e2015-04-03 08:07:26 -070053 const char* text = "O";
caryclark1818acb2015-07-24 12:09:25 -070054 sk_tool_utils::set_portable_typeface(&paint);
joshualitteef5b3e2015-04-03 08:07:26 -070055
56 SkRect bounds;
57 paint.measureText(text, strlen(text), &bounds);
58
59 SkScalar yOffset = bounds.height();
joshualitt9e36c1a2015-04-14 12:17:27 -070060 sk_tool_utils::add_to_text_blob(&builder, text, paint, 10, yOffset);
joshualitteef5b3e2015-04-03 08:07:26 -070061 SkScalar corruptedAx = bounds.width();
62 SkScalar corruptedAy = yOffset;
63
64 const SkScalar boundsHalfWidth = bounds.width() * SK_ScalarHalf;
65 const SkScalar boundsHalfHeight = bounds.height() * SK_ScalarHalf;
66
67 SkScalar xOffset = boundsHalfWidth;
68 yOffset = boundsHalfHeight;
69
70 // LCD
71 paint.setTextSize(32);
72 text = "LCD!!!!!";
joshualitt20dac882015-07-24 13:16:24 -070073 paint.setAntiAlias(true);
joshualitteef5b3e2015-04-03 08:07:26 -070074 paint.setSubpixelText(true);
75 paint.setLCDRenderText(true);
76 paint.measureText(text, strlen(text), &bounds);
joshualitt9e36c1a2015-04-14 12:17:27 -070077 sk_tool_utils::add_to_text_blob(&builder, text, paint, xOffset - bounds.width() * 0.25f,
78 yOffset - bounds.height() * 0.5f);
joshualitteef5b3e2015-04-03 08:07:26 -070079 yOffset += bounds.height();
80
81 // color emoji
caryclarkc3dcb672015-07-21 12:27:36 -070082 if (fEmojiTypeface) {
joshualitt20dac882015-07-24 13:16:24 -070083 paint.setAntiAlias(false);
caryclarkc3dcb672015-07-21 12:27:36 -070084 paint.setSubpixelText(false);
85 paint.setLCDRenderText(false);
86 paint.setTypeface(fEmojiTypeface);
87 text = fEmojiText;
88 paint.measureText(text, strlen(text), &bounds);
89 sk_tool_utils::add_to_text_blob(&builder, text, paint, xOffset - bounds.width() * 0.3f,
90 yOffset);
91 }
joshualitteef5b3e2015-04-03 08:07:26 -070092
93 // Corrupted font
94 paint.setTextSize(12);
95 text = "aA";
96 paint.setTypeface(fReallyBigATypeface);
joshualitt9e36c1a2015-04-14 12:17:27 -070097 sk_tool_utils::add_to_text_blob(&builder, text, paint, corruptedAx, corruptedAy);
fmalita37283c22016-09-13 10:00:23 -070098 fBlob = builder.make();
joshualitteef5b3e2015-04-03 08:07:26 -070099 }
100
101 SkString onShortName() override {
caryclarkc3dcb672015-07-21 12:27:36 -0700102 SkString name("mixedtextblobs");
103 name.append(sk_tool_utils::platform_os_emoji());
104 return name;
joshualitteef5b3e2015-04-03 08:07:26 -0700105 }
106
107 SkISize onISize() override {
108 return SkISize::Make(kWidth, kHeight);
109 }
110
111 void onDraw(SkCanvas* canvas) override {
112
caryclarkc3dcb672015-07-21 12:27:36 -0700113 canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorGRAY));
joshualitteef5b3e2015-04-03 08:07:26 -0700114
115 SkPaint paint;
116
117 // setup work needed to draw text with different clips
118 paint.setColor(SK_ColorBLACK);
119 canvas->translate(10, 40);
120
121 paint.setTextSize(40);
122
123 // compute the bounds of the text and setup some clips
124 SkRect bounds = fBlob->bounds();
125
126 const SkScalar boundsHalfWidth = bounds.width() * SK_ScalarHalf;
127 const SkScalar boundsHalfHeight = bounds.height() * SK_ScalarHalf;
128 const SkScalar boundsQuarterWidth = boundsHalfWidth * SK_ScalarHalf;
129 const SkScalar boundsQuarterHeight = boundsHalfHeight * SK_ScalarHalf;
130
131 SkRect upperLeftClip = SkRect::MakeXYWH(bounds.left(), bounds.top(),
132 boundsHalfWidth, boundsHalfHeight);
133 SkRect lowerRightClip = SkRect::MakeXYWH(bounds.centerX(), bounds.centerY(),
134 boundsHalfWidth, boundsHalfHeight);
135 SkRect interiorClip = bounds;
136 interiorClip.inset(boundsQuarterWidth, boundsQuarterHeight);
137
138 const SkRect clipRects[] = { bounds, upperLeftClip, lowerRightClip, interiorClip};
139
140 size_t count = sizeof(clipRects) / sizeof(SkRect);
141 for (size_t x = 0; x < count; ++x) {
fmalita37283c22016-09-13 10:00:23 -0700142 draw_blob(canvas, fBlob.get(), paint, clipRects[x]);
joshualitteef5b3e2015-04-03 08:07:26 -0700143 if (x == (count >> 1) - 1) {
144 canvas->translate(SkScalarFloorToScalar(bounds.width() + SkIntToScalar(25)),
145 -(x * SkScalarFloorToScalar(bounds.height() +
146 SkIntToScalar(25))));
147 } else {
148 canvas->translate(0, SkScalarFloorToScalar(bounds.height() + SkIntToScalar(25)));
149 }
150 }
151 }
152
153private:
bungeman13b9c952016-05-12 10:09:30 -0700154 sk_sp<SkTypeface> fEmojiTypeface;
155 sk_sp<SkTypeface> fReallyBigATypeface;
joshualitteef5b3e2015-04-03 08:07:26 -0700156 const char* fEmojiText;
fmalita37283c22016-09-13 10:00:23 -0700157 sk_sp<SkTextBlob> fBlob;
joshualitteef5b3e2015-04-03 08:07:26 -0700158
mtkleindbfd7ab2016-09-01 11:24:54 -0700159 static constexpr int kWidth = 1250;
160 static constexpr int kHeight = 700;
joshualitteef5b3e2015-04-03 08:07:26 -0700161
162 typedef GM INHERITED;
163};
164
165//////////////////////////////////////////////////////////////////////////////
166
halcanary385fe4d2015-08-26 13:07:48 -0700167DEF_GM(return new MixedTextBlobsGM;)
joshualitteef5b3e2015-04-03 08:07:26 -0700168}