blob: e7b6828464e71ffeeb90b7b5512f83ba92ace48f [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkColor.h"
11#include "include/core/SkFont.h"
12#include "include/core/SkFontTypes.h"
13#include "include/core/SkPaint.h"
14#include "include/core/SkRect.h"
15#include "include/core/SkRefCnt.h"
16#include "include/core/SkScalar.h"
17#include "include/core/SkSize.h"
18#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/core/SkTextBlob.h"
20#include "include/core/SkTypeface.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "tools/Resources.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040022#include "tools/ToolUtils.h"
23
24#include <string.h>
joshualitteef5b3e2015-04-03 08:07:26 -070025
26namespace skiagm {
27
joshualitteef5b3e2015-04-03 08:07:26 -070028static void draw_blob(SkCanvas* canvas, const SkTextBlob* blob, const SkPaint& skPaint,
29 const SkRect& clipRect) {
30 SkPaint clipHairline;
31 clipHairline.setColor(SK_ColorWHITE);
32 clipHairline.setStyle(SkPaint::kStroke_Style);
33
34 SkPaint paint(skPaint);
35 canvas->save();
36 canvas->drawRect(clipRect, clipHairline);
Mike Reed9407e242019-02-15 16:13:57 -050037 paint.setAlphaf(0.125f);
joshualitteef5b3e2015-04-03 08:07:26 -070038 canvas->drawTextBlob(blob, 0, 0, paint);
39 canvas->clipRect(clipRect);
Mike Reed9407e242019-02-15 16:13:57 -050040 paint.setAlphaf(1.0f);
joshualitteef5b3e2015-04-03 08:07:26 -070041 canvas->drawTextBlob(blob, 0, 0, paint);
42 canvas->restore();
43}
44
45class MixedTextBlobsGM : public GM {
46public:
47 MixedTextBlobsGM() { }
48
49protected:
50 void onOnceBeforeDraw() override {
Mike Kleinea3f0142019-03-20 11:12:10 -050051 fEmojiTypeface = ToolUtils::planet_typeface();
Ben Wagner8dce0542019-03-08 18:05:47 -050052 fEmojiText = "♁♃";
Hal Canary53e5e7d2017-12-08 14:25:14 -050053 fReallyBigATypeface = MakeResourceAsTypeface("fonts/ReallyBigA.ttf");
joshualitteef5b3e2015-04-03 08:07:26 -070054
55 SkTextBlobBuilder builder;
56
57 // make textblob
58 // Text so large we draw as paths
Mike Kleinea3f0142019-03-20 11:12:10 -050059 SkFont font(ToolUtils::create_portable_typeface(), 385);
Mike Reed12a6d452018-12-21 22:22:31 -050060 font.setEdging(SkFont::Edging::kAlias);
joshualitteef5b3e2015-04-03 08:07:26 -070061 const char* text = "O";
joshualitteef5b3e2015-04-03 08:07:26 -070062
63 SkRect bounds;
Ben Wagner51e15a62019-05-07 15:38:46 -040064 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
joshualitteef5b3e2015-04-03 08:07:26 -070065
66 SkScalar yOffset = bounds.height();
Mike Kleinea3f0142019-03-20 11:12:10 -050067 ToolUtils::add_to_text_blob(&builder, text, font, 10, yOffset);
joshualitteef5b3e2015-04-03 08:07:26 -070068 SkScalar corruptedAx = bounds.width();
69 SkScalar corruptedAy = yOffset;
70
71 const SkScalar boundsHalfWidth = bounds.width() * SK_ScalarHalf;
72 const SkScalar boundsHalfHeight = bounds.height() * SK_ScalarHalf;
73
74 SkScalar xOffset = boundsHalfWidth;
75 yOffset = boundsHalfHeight;
76
77 // LCD
Mike Reed12a6d452018-12-21 22:22:31 -050078 font.setSize(32);
79 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
80 font.setSubpixel(true);
joshualitteef5b3e2015-04-03 08:07:26 -070081 text = "LCD!!!!!";
Ben Wagner51e15a62019-05-07 15:38:46 -040082 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
Mike Kleinea3f0142019-03-20 11:12:10 -050083 ToolUtils::add_to_text_blob(&builder,
84 text,
85 font,
86 xOffset - bounds.width() * 0.25f,
87 yOffset - bounds.height() * 0.5f);
joshualitteef5b3e2015-04-03 08:07:26 -070088
Ben Wagner8dce0542019-03-08 18:05:47 -050089 // color emoji font with large glyph
caryclarkc3dcb672015-07-21 12:27:36 -070090 if (fEmojiTypeface) {
Mike Reed12a6d452018-12-21 22:22:31 -050091 font.setEdging(SkFont::Edging::kAlias);
92 font.setSubpixel(false);
93 font.setTypeface(fEmojiTypeface);
Ben Wagner51e15a62019-05-07 15:38:46 -040094 font.measureText(fEmojiText, strlen(fEmojiText), SkTextEncoding::kUTF8, &bounds);
Mike Kleinea3f0142019-03-20 11:12:10 -050095 ToolUtils::add_to_text_blob(&builder, fEmojiText, font, xOffset, yOffset);
caryclarkc3dcb672015-07-21 12:27:36 -070096 }
joshualitteef5b3e2015-04-03 08:07:26 -070097
Ben Wagner8dce0542019-03-08 18:05:47 -050098 // outline font with large glyph
Mike Reed12a6d452018-12-21 22:22:31 -050099 font.setSize(12);
joshualitteef5b3e2015-04-03 08:07:26 -0700100 text = "aA";
Mike Reed12a6d452018-12-21 22:22:31 -0500101 font.setTypeface(fReallyBigATypeface);
Mike Kleinea3f0142019-03-20 11:12:10 -0500102 ToolUtils::add_to_text_blob(&builder, text, font, corruptedAx, corruptedAy);
fmalita37283c22016-09-13 10:00:23 -0700103 fBlob = builder.make();
joshualitteef5b3e2015-04-03 08:07:26 -0700104 }
105
106 SkString onShortName() override {
Mike Kleinbea1f942019-03-08 11:11:55 -0600107 return SkString("mixedtextblobs");
joshualitteef5b3e2015-04-03 08:07:26 -0700108 }
109
110 SkISize onISize() override {
111 return SkISize::Make(kWidth, kHeight);
112 }
113
114 void onDraw(SkCanvas* canvas) override {
115
Mike Kleind46dce32018-08-16 10:17:03 -0400116 canvas->drawColor(SK_ColorGRAY);
joshualitteef5b3e2015-04-03 08:07:26 -0700117
118 SkPaint paint;
119
120 // setup work needed to draw text with different clips
121 paint.setColor(SK_ColorBLACK);
122 canvas->translate(10, 40);
123
joshualitteef5b3e2015-04-03 08:07:26 -0700124 // compute the bounds of the text and setup some clips
125 SkRect bounds = fBlob->bounds();
126
127 const SkScalar boundsHalfWidth = bounds.width() * SK_ScalarHalf;
128 const SkScalar boundsHalfHeight = bounds.height() * SK_ScalarHalf;
129 const SkScalar boundsQuarterWidth = boundsHalfWidth * SK_ScalarHalf;
130 const SkScalar boundsQuarterHeight = boundsHalfHeight * SK_ScalarHalf;
131
132 SkRect upperLeftClip = SkRect::MakeXYWH(bounds.left(), bounds.top(),
133 boundsHalfWidth, boundsHalfHeight);
134 SkRect lowerRightClip = SkRect::MakeXYWH(bounds.centerX(), bounds.centerY(),
135 boundsHalfWidth, boundsHalfHeight);
136 SkRect interiorClip = bounds;
137 interiorClip.inset(boundsQuarterWidth, boundsQuarterHeight);
138
139 const SkRect clipRects[] = { bounds, upperLeftClip, lowerRightClip, interiorClip};
140
141 size_t count = sizeof(clipRects) / sizeof(SkRect);
142 for (size_t x = 0; x < count; ++x) {
fmalita37283c22016-09-13 10:00:23 -0700143 draw_blob(canvas, fBlob.get(), paint, clipRects[x]);
joshualitteef5b3e2015-04-03 08:07:26 -0700144 if (x == (count >> 1) - 1) {
145 canvas->translate(SkScalarFloorToScalar(bounds.width() + SkIntToScalar(25)),
146 -(x * SkScalarFloorToScalar(bounds.height() +
147 SkIntToScalar(25))));
148 } else {
149 canvas->translate(0, SkScalarFloorToScalar(bounds.height() + SkIntToScalar(25)));
150 }
151 }
152 }
153
154private:
bungeman13b9c952016-05-12 10:09:30 -0700155 sk_sp<SkTypeface> fEmojiTypeface;
156 sk_sp<SkTypeface> fReallyBigATypeface;
joshualitteef5b3e2015-04-03 08:07:26 -0700157 const char* fEmojiText;
fmalita37283c22016-09-13 10:00:23 -0700158 sk_sp<SkTextBlob> fBlob;
joshualitteef5b3e2015-04-03 08:07:26 -0700159
mtkleindbfd7ab2016-09-01 11:24:54 -0700160 static constexpr int kWidth = 1250;
161 static constexpr int kHeight = 700;
joshualitteef5b3e2015-04-03 08:07:26 -0700162
John Stiles7571f9e2020-09-02 22:42:33 -0400163 using INHERITED = GM;
joshualitteef5b3e2015-04-03 08:07:26 -0700164};
165
166//////////////////////////////////////////////////////////////////////////////
167
halcanary385fe4d2015-08-26 13:07:48 -0700168DEF_GM(return new MixedTextBlobsGM;)
John Stilesa6841be2020-08-06 14:11:56 -0400169} // namespace skiagm