1 /*
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/gm.h"
9 #include "include/core/SkCanvas.h"
10 #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"
19 #include "include/core/SkTextBlob.h"
20 #include "include/core/SkTypeface.h"
21 #include "tools/Resources.h"
22 #include "tools/ToolUtils.h"
23
24 #include <string.h>
25
26 namespace skiagm {
27
draw_blob(SkCanvas * canvas,const SkTextBlob * blob,const SkPaint & skPaint,const SkRect & clipRect)28 static 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);
37 paint.setAlphaf(0.125f);
38 canvas->drawTextBlob(blob, 0, 0, paint);
39 canvas->clipRect(clipRect);
40 paint.setAlphaf(1.0f);
41 canvas->drawTextBlob(blob, 0, 0, paint);
42 canvas->restore();
43 }
44
45 class MixedTextBlobsGM : public GM {
46 public:
MixedTextBlobsGM()47 MixedTextBlobsGM() { }
48
49 protected:
onOnceBeforeDraw()50 void onOnceBeforeDraw() override {
51 fEmojiTypeface = ToolUtils::planet_typeface();
52 fEmojiText = "♁♃";
53 fReallyBigATypeface = MakeResourceAsTypeface("fonts/ReallyBigA.ttf");
54
55 SkTextBlobBuilder builder;
56
57 // make textblob
58 // Text so large we draw as paths
59 SkFont font(ToolUtils::create_portable_typeface(), 385);
60 font.setEdging(SkFont::Edging::kAlias);
61 const char* text = "O";
62
63 SkRect bounds;
64 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
65
66 SkScalar yOffset = bounds.height();
67 ToolUtils::add_to_text_blob(&builder, text, font, 10, yOffset);
68 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
78 font.setSize(32);
79 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
80 font.setSubpixel(true);
81 text = "LCD!!!!!";
82 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
83 ToolUtils::add_to_text_blob(&builder,
84 text,
85 font,
86 xOffset - bounds.width() * 0.25f,
87 yOffset - bounds.height() * 0.5f);
88
89 // color emoji font with large glyph
90 if (fEmojiTypeface) {
91 font.setEdging(SkFont::Edging::kAlias);
92 font.setSubpixel(false);
93 font.setTypeface(fEmojiTypeface);
94 font.measureText(fEmojiText, strlen(fEmojiText), SkTextEncoding::kUTF8, &bounds);
95 ToolUtils::add_to_text_blob(&builder, fEmojiText, font, xOffset, yOffset);
96 }
97
98 // outline font with large glyph
99 font.setSize(12);
100 text = "aA";
101 font.setTypeface(fReallyBigATypeface);
102 ToolUtils::add_to_text_blob(&builder, text, font, corruptedAx, corruptedAy);
103 fBlob = builder.make();
104 }
105
onShortName()106 SkString onShortName() override {
107 return SkString("mixedtextblobs");
108 }
109
onISize()110 SkISize onISize() override {
111 return SkISize::Make(kWidth, kHeight);
112 }
113
onDraw(SkCanvas * canvas)114 void onDraw(SkCanvas* canvas) override {
115
116 canvas->drawColor(SK_ColorGRAY);
117
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
124 // 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) {
143 draw_blob(canvas, fBlob.get(), paint, clipRects[x]);
144 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
154 private:
155 sk_sp<SkTypeface> fEmojiTypeface;
156 sk_sp<SkTypeface> fReallyBigATypeface;
157 const char* fEmojiText;
158 sk_sp<SkTextBlob> fBlob;
159
160 static constexpr int kWidth = 1250;
161 static constexpr int kHeight = 700;
162
163 typedef GM INHERITED;
164 };
165
166 //////////////////////////////////////////////////////////////////////////////
167
168 DEF_GM(return new MixedTextBlobsGM;)
169 }
170