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.h"
9 #include "sk_tool_utils.h"
10
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
18 namespace skiagm {
19
draw_blob(SkCanvas * canvas,const SkTextBlob * blob,const SkPaint & skPaint,const SkRect & clipRect)20 static 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
37 class MixedTextBlobsGM : public GM {
38 public:
MixedTextBlobsGM()39 MixedTextBlobsGM() { }
40
41 protected:
onOnceBeforeDraw()42 void onOnceBeforeDraw() override {
43 fEmojiTypeface = sk_tool_utils::emoji_typeface();
44 fEmojiText = sk_tool_utils::emoji_sample_text();
45 fReallyBigATypeface = MakeResourceAsTypeface("fonts/ReallyBigA.ttf");
46
47 SkTextBlobBuilder builder;
48
49 // make textblob
50 // Text so large we draw as paths
51 SkFont font(sk_tool_utils::create_portable_typeface(), 385);
52 font.setEdging(SkFont::Edging::kAlias);
53 const char* text = "O";
54
55 SkRect bounds;
56 font.measureText(text, strlen(text), kUTF8_SkTextEncoding, &bounds);
57
58 SkScalar yOffset = bounds.height();
59 sk_tool_utils::add_to_text_blob(&builder, text, font, 10, yOffset);
60 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
70 font.setSize(32);
71 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
72 font.setSubpixel(true);
73 text = "LCD!!!!!";
74 font.measureText(text, strlen(text), kUTF8_SkTextEncoding, &bounds);
75 sk_tool_utils::add_to_text_blob(&builder, text, font, xOffset - bounds.width() * 0.25f,
76 yOffset - bounds.height() * 0.5f);
77 yOffset += bounds.height();
78
79 // color emoji
80 if (fEmojiTypeface) {
81 font.setEdging(SkFont::Edging::kAlias);
82 font.setSubpixel(false);
83 font.setTypeface(fEmojiTypeface);
84 text = fEmojiText;
85 font.measureText(text, strlen(text), kUTF8_SkTextEncoding, &bounds);
86 sk_tool_utils::add_to_text_blob(&builder, text, font, xOffset - bounds.width() * 0.3f,
87 yOffset);
88 }
89
90 // Corrupted font
91 font.setSize(12);
92 text = "aA";
93 font.setTypeface(fReallyBigATypeface);
94 sk_tool_utils::add_to_text_blob(&builder, text, font, corruptedAx, corruptedAy);
95 fBlob = builder.make();
96 }
97
onShortName()98 SkString onShortName() override {
99 return SkStringPrintf("mixedtextblobs%s",
100 sk_tool_utils::platform_font_manager());
101 }
102
onISize()103 SkISize onISize() override {
104 return SkISize::Make(kWidth, kHeight);
105 }
106
onDraw(SkCanvas * canvas)107 void onDraw(SkCanvas* canvas) override {
108
109 canvas->drawColor(SK_ColorGRAY);
110
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
117 // 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) {
136 draw_blob(canvas, fBlob.get(), paint, clipRects[x]);
137 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
147 private:
148 sk_sp<SkTypeface> fEmojiTypeface;
149 sk_sp<SkTypeface> fReallyBigATypeface;
150 const char* fEmojiText;
151 sk_sp<SkTextBlob> fBlob;
152
153 static constexpr int kWidth = 1250;
154 static constexpr int kHeight = 700;
155
156 typedef GM INHERITED;
157 };
158
159 //////////////////////////////////////////////////////////////////////////////
160
161 DEF_GM(return new MixedTextBlobsGM;)
162 }
163