blob: 9e83b90daac0b707b3e1a0d382866ff5540ad0f6 [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();
Hal Canary53e5e7d2017-12-08 14:25:14 -050045 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
Mike Reed12a6d452018-12-21 22:22:31 -050051 SkFont font(sk_tool_utils::create_portable_typeface(), 385);
52 font.setEdging(SkFont::Edging::kAlias);
joshualitteef5b3e2015-04-03 08:07:26 -070053 const char* text = "O";
joshualitteef5b3e2015-04-03 08:07:26 -070054
55 SkRect bounds;
Mike Reed12a6d452018-12-21 22:22:31 -050056 font.measureText(text, strlen(text), kUTF8_SkTextEncoding, &bounds);
joshualitteef5b3e2015-04-03 08:07:26 -070057
58 SkScalar yOffset = bounds.height();
Mike Reed12a6d452018-12-21 22:22:31 -050059 sk_tool_utils::add_to_text_blob(&builder, text, font, 10, yOffset);
joshualitteef5b3e2015-04-03 08:07:26 -070060 SkScalar corruptedAx = bounds.width();
61 SkScalar corruptedAy = yOffset;
62
63 const SkScalar boundsHalfWidth = bounds.width() * SK_ScalarHalf;
64 const SkScalar boundsHalfHeight = bounds.height() * SK_ScalarHalf;
65
66 SkScalar xOffset = boundsHalfWidth;
67 yOffset = boundsHalfHeight;
68
69 // LCD
Mike Reed12a6d452018-12-21 22:22:31 -050070 font.setSize(32);
71 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
72 font.setSubpixel(true);
joshualitteef5b3e2015-04-03 08:07:26 -070073 text = "LCD!!!!!";
Mike Reed12a6d452018-12-21 22:22:31 -050074 font.measureText(text, strlen(text), kUTF8_SkTextEncoding, &bounds);
75 sk_tool_utils::add_to_text_blob(&builder, text, font, xOffset - bounds.width() * 0.25f,
joshualitt9e36c1a2015-04-14 12:17:27 -070076 yOffset - bounds.height() * 0.5f);
joshualitteef5b3e2015-04-03 08:07:26 -070077 yOffset += bounds.height();
78
79 // color emoji
caryclarkc3dcb672015-07-21 12:27:36 -070080 if (fEmojiTypeface) {
Mike Reed12a6d452018-12-21 22:22:31 -050081 font.setEdging(SkFont::Edging::kAlias);
82 font.setSubpixel(false);
83 font.setTypeface(fEmojiTypeface);
caryclarkc3dcb672015-07-21 12:27:36 -070084 text = fEmojiText;
Mike Reed12a6d452018-12-21 22:22:31 -050085 font.measureText(text, strlen(text), kUTF8_SkTextEncoding, &bounds);
86 sk_tool_utils::add_to_text_blob(&builder, text, font, xOffset - bounds.width() * 0.3f,
caryclarkc3dcb672015-07-21 12:27:36 -070087 yOffset);
88 }
joshualitteef5b3e2015-04-03 08:07:26 -070089
90 // Corrupted font
Mike Reed12a6d452018-12-21 22:22:31 -050091 font.setSize(12);
joshualitteef5b3e2015-04-03 08:07:26 -070092 text = "aA";
Mike Reed12a6d452018-12-21 22:22:31 -050093 font.setTypeface(fReallyBigATypeface);
94 sk_tool_utils::add_to_text_blob(&builder, text, font, corruptedAx, corruptedAy);
fmalita37283c22016-09-13 10:00:23 -070095 fBlob = builder.make();
joshualitteef5b3e2015-04-03 08:07:26 -070096 }
97
98 SkString onShortName() override {
Mike Kleinf436fbc2017-11-15 10:42:46 -050099 return SkStringPrintf("mixedtextblobs%s",
100 sk_tool_utils::platform_font_manager());
joshualitteef5b3e2015-04-03 08:07:26 -0700101 }
102
103 SkISize onISize() override {
104 return SkISize::Make(kWidth, kHeight);
105 }
106
107 void onDraw(SkCanvas* canvas) override {
108
Mike Kleind46dce32018-08-16 10:17:03 -0400109 canvas->drawColor(SK_ColorGRAY);
joshualitteef5b3e2015-04-03 08:07:26 -0700110
111 SkPaint paint;
112
113 // setup work needed to draw text with different clips
114 paint.setColor(SK_ColorBLACK);
115 canvas->translate(10, 40);
116
joshualitteef5b3e2015-04-03 08:07:26 -0700117 // compute the bounds of the text and setup some clips
118 SkRect bounds = fBlob->bounds();
119
120 const SkScalar boundsHalfWidth = bounds.width() * SK_ScalarHalf;
121 const SkScalar boundsHalfHeight = bounds.height() * SK_ScalarHalf;
122 const SkScalar boundsQuarterWidth = boundsHalfWidth * SK_ScalarHalf;
123 const SkScalar boundsQuarterHeight = boundsHalfHeight * SK_ScalarHalf;
124
125 SkRect upperLeftClip = SkRect::MakeXYWH(bounds.left(), bounds.top(),
126 boundsHalfWidth, boundsHalfHeight);
127 SkRect lowerRightClip = SkRect::MakeXYWH(bounds.centerX(), bounds.centerY(),
128 boundsHalfWidth, boundsHalfHeight);
129 SkRect interiorClip = bounds;
130 interiorClip.inset(boundsQuarterWidth, boundsQuarterHeight);
131
132 const SkRect clipRects[] = { bounds, upperLeftClip, lowerRightClip, interiorClip};
133
134 size_t count = sizeof(clipRects) / sizeof(SkRect);
135 for (size_t x = 0; x < count; ++x) {
fmalita37283c22016-09-13 10:00:23 -0700136 draw_blob(canvas, fBlob.get(), paint, clipRects[x]);
joshualitteef5b3e2015-04-03 08:07:26 -0700137 if (x == (count >> 1) - 1) {
138 canvas->translate(SkScalarFloorToScalar(bounds.width() + SkIntToScalar(25)),
139 -(x * SkScalarFloorToScalar(bounds.height() +
140 SkIntToScalar(25))));
141 } else {
142 canvas->translate(0, SkScalarFloorToScalar(bounds.height() + SkIntToScalar(25)));
143 }
144 }
145 }
146
147private:
bungeman13b9c952016-05-12 10:09:30 -0700148 sk_sp<SkTypeface> fEmojiTypeface;
149 sk_sp<SkTypeface> fReallyBigATypeface;
joshualitteef5b3e2015-04-03 08:07:26 -0700150 const char* fEmojiText;
fmalita37283c22016-09-13 10:00:23 -0700151 sk_sp<SkTextBlob> fBlob;
joshualitteef5b3e2015-04-03 08:07:26 -0700152
mtkleindbfd7ab2016-09-01 11:24:54 -0700153 static constexpr int kWidth = 1250;
154 static constexpr int kHeight = 700;
joshualitteef5b3e2015-04-03 08:07:26 -0700155
156 typedef GM INHERITED;
157};
158
159//////////////////////////////////////////////////////////////////////////////
160
halcanary385fe4d2015-08-26 13:07:48 -0700161DEF_GM(return new MixedTextBlobsGM;)
joshualitteef5b3e2015-04-03 08:07:26 -0700162}