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.setAlphaf(0.125f);
30 canvas->drawTextBlob(blob, 0, 0, paint);
31 canvas->clipRect(clipRect);
32 paint.setAlphaf(1.0f);
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 SkString("mixedtextblobs");
100 }
101
onISize()102 SkISize onISize() override {
103 return SkISize::Make(kWidth, kHeight);
104 }
105
onDraw(SkCanvas * canvas)106 void onDraw(SkCanvas* canvas) override {
107
108 canvas->drawColor(SK_ColorGRAY);
109
110 SkPaint paint;
111
112 // setup work needed to draw text with different clips
113 paint.setColor(SK_ColorBLACK);
114 canvas->translate(10, 40);
115
116 // compute the bounds of the text and setup some clips
117 SkRect bounds = fBlob->bounds();
118
119 const SkScalar boundsHalfWidth = bounds.width() * SK_ScalarHalf;
120 const SkScalar boundsHalfHeight = bounds.height() * SK_ScalarHalf;
121 const SkScalar boundsQuarterWidth = boundsHalfWidth * SK_ScalarHalf;
122 const SkScalar boundsQuarterHeight = boundsHalfHeight * SK_ScalarHalf;
123
124 SkRect upperLeftClip = SkRect::MakeXYWH(bounds.left(), bounds.top(),
125 boundsHalfWidth, boundsHalfHeight);
126 SkRect lowerRightClip = SkRect::MakeXYWH(bounds.centerX(), bounds.centerY(),
127 boundsHalfWidth, boundsHalfHeight);
128 SkRect interiorClip = bounds;
129 interiorClip.inset(boundsQuarterWidth, boundsQuarterHeight);
130
131 const SkRect clipRects[] = { bounds, upperLeftClip, lowerRightClip, interiorClip};
132
133 size_t count = sizeof(clipRects) / sizeof(SkRect);
134 for (size_t x = 0; x < count; ++x) {
135 draw_blob(canvas, fBlob.get(), paint, clipRects[x]);
136 if (x == (count >> 1) - 1) {
137 canvas->translate(SkScalarFloorToScalar(bounds.width() + SkIntToScalar(25)),
138 -(x * SkScalarFloorToScalar(bounds.height() +
139 SkIntToScalar(25))));
140 } else {
141 canvas->translate(0, SkScalarFloorToScalar(bounds.height() + SkIntToScalar(25)));
142 }
143 }
144 }
145
146 private:
147 sk_sp<SkTypeface> fEmojiTypeface;
148 sk_sp<SkTypeface> fReallyBigATypeface;
149 const char* fEmojiText;
150 sk_sp<SkTextBlob> fBlob;
151
152 static constexpr int kWidth = 1250;
153 static constexpr int kHeight = 700;
154
155 typedef GM INHERITED;
156 };
157
158 //////////////////////////////////////////////////////////////////////////////
159
160 DEF_GM(return new MixedTextBlobsGM;)
161 }
162