blob: dc38c6b1987354fd51160ff26d07b224ac32744d [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"
Kevin Lubicke836c3a2023-10-20 06:55:35 -040023#include "tools/fonts/FontToolUtils.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040024
25#include <string.h>
joshualitteef5b3e2015-04-03 08:07:26 -070026
27namespace skiagm {
28
joshualitteef5b3e2015-04-03 08:07:26 -070029static void draw_blob(SkCanvas* canvas, const SkTextBlob* blob, const SkPaint& skPaint,
30 const SkRect& clipRect) {
31 SkPaint clipHairline;
32 clipHairline.setColor(SK_ColorWHITE);
33 clipHairline.setStyle(SkPaint::kStroke_Style);
34
35 SkPaint paint(skPaint);
36 canvas->save();
37 canvas->drawRect(clipRect, clipHairline);
Mike Reed9407e242019-02-15 16:13:57 -050038 paint.setAlphaf(0.125f);
joshualitteef5b3e2015-04-03 08:07:26 -070039 canvas->drawTextBlob(blob, 0, 0, paint);
40 canvas->clipRect(clipRect);
Mike Reed9407e242019-02-15 16:13:57 -050041 paint.setAlphaf(1.0f);
joshualitteef5b3e2015-04-03 08:07:26 -070042 canvas->drawTextBlob(blob, 0, 0, paint);
43 canvas->restore();
44}
45
46class MixedTextBlobsGM : public GM {
47public:
48 MixedTextBlobsGM() { }
49
50protected:
51 void onOnceBeforeDraw() override {
Kevin Lubicke836c3a2023-10-20 06:55:35 -040052 fEmojiTypeface = ToolUtils::PlanetTypeface();
Ben Wagner8dce0542019-03-08 18:05:47 -050053 fEmojiText = "♁♃";
Kevin Lubick1e971192023-11-10 16:14:44 -050054 fReallyBigATypeface = ToolUtils::CreateTypefaceFromResource("fonts/ReallyBigA.ttf");
Kevin Lubickbca43ec2023-10-30 10:11:22 -040055 if (!fReallyBigATypeface) {
56 fReallyBigATypeface = ToolUtils::DefaultPortableTypeface();
57 }
joshualitteef5b3e2015-04-03 08:07:26 -070058
59 SkTextBlobBuilder builder;
60
61 // make textblob
62 // Text so large we draw as paths
Kevin Lubicke836c3a2023-10-20 06:55:35 -040063 SkFont font(ToolUtils::DefaultPortableTypeface(), 385);
Mike Reed12a6d452018-12-21 22:22:31 -050064 font.setEdging(SkFont::Edging::kAlias);
joshualitteef5b3e2015-04-03 08:07:26 -070065 const char* text = "O";
joshualitteef5b3e2015-04-03 08:07:26 -070066
67 SkRect bounds;
Ben Wagner51e15a62019-05-07 15:38:46 -040068 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
joshualitteef5b3e2015-04-03 08:07:26 -070069
70 SkScalar yOffset = bounds.height();
Mike Kleinea3f0142019-03-20 11:12:10 -050071 ToolUtils::add_to_text_blob(&builder, text, font, 10, yOffset);
joshualitteef5b3e2015-04-03 08:07:26 -070072 SkScalar corruptedAx = bounds.width();
73 SkScalar corruptedAy = yOffset;
74
75 const SkScalar boundsHalfWidth = bounds.width() * SK_ScalarHalf;
76 const SkScalar boundsHalfHeight = bounds.height() * SK_ScalarHalf;
77
78 SkScalar xOffset = boundsHalfWidth;
79 yOffset = boundsHalfHeight;
80
81 // LCD
Mike Reed12a6d452018-12-21 22:22:31 -050082 font.setSize(32);
83 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
84 font.setSubpixel(true);
joshualitteef5b3e2015-04-03 08:07:26 -070085 text = "LCD!!!!!";
Ben Wagner51e15a62019-05-07 15:38:46 -040086 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
Mike Kleinea3f0142019-03-20 11:12:10 -050087 ToolUtils::add_to_text_blob(&builder,
88 text,
89 font,
90 xOffset - bounds.width() * 0.25f,
91 yOffset - bounds.height() * 0.5f);
joshualitteef5b3e2015-04-03 08:07:26 -070092
Ben Wagner8dce0542019-03-08 18:05:47 -050093 // color emoji font with large glyph
caryclarkc3dcb672015-07-21 12:27:36 -070094 if (fEmojiTypeface) {
Mike Reed12a6d452018-12-21 22:22:31 -050095 font.setEdging(SkFont::Edging::kAlias);
96 font.setSubpixel(false);
97 font.setTypeface(fEmojiTypeface);
Ben Wagner51e15a62019-05-07 15:38:46 -040098 font.measureText(fEmojiText, strlen(fEmojiText), SkTextEncoding::kUTF8, &bounds);
Mike Kleinea3f0142019-03-20 11:12:10 -050099 ToolUtils::add_to_text_blob(&builder, fEmojiText, font, xOffset, yOffset);
caryclarkc3dcb672015-07-21 12:27:36 -0700100 }
joshualitteef5b3e2015-04-03 08:07:26 -0700101
Ben Wagner8dce0542019-03-08 18:05:47 -0500102 // outline font with large glyph
Mike Reed12a6d452018-12-21 22:22:31 -0500103 font.setSize(12);
joshualitteef5b3e2015-04-03 08:07:26 -0700104 text = "aA";
Mike Reed12a6d452018-12-21 22:22:31 -0500105 font.setTypeface(fReallyBigATypeface);
Mike Kleinea3f0142019-03-20 11:12:10 -0500106 ToolUtils::add_to_text_blob(&builder, text, font, corruptedAx, corruptedAy);
fmalita37283c22016-09-13 10:00:23 -0700107 fBlob = builder.make();
joshualitteef5b3e2015-04-03 08:07:26 -0700108 }
109
Leandro Lovisolo24fa2112023-08-15 19:05:17 +0000110 SkString getName() const override { return SkString("mixedtextblobs"); }
joshualitteef5b3e2015-04-03 08:07:26 -0700111
Leandro Lovisolo8f023882023-08-15 21:13:52 +0000112 SkISize getISize() override { return SkISize::Make(kWidth, kHeight); }
joshualitteef5b3e2015-04-03 08:07:26 -0700113
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
Brian Salomon9fa47cc2021-10-08 18:48:26 -0400160 inline static constexpr int kWidth = 1250;
161 inline 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