2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "sk_tool_utils.h"
11 #include "Resources.h"
13 #include "SkGradientShader.h"
15 #include "SkTextBlob.h"
16 #include "SkTypeface.h"
20 static void draw_blob(SkCanvas* canvas, const SkTextBlob* blob, const SkPaint& skPaint,
21 const SkRect& clipRect) {
23 clipHairline.setColor(SK_ColorWHITE);
24 clipHairline.setStyle(SkPaint::kStroke_Style);
26 SkPaint paint(skPaint);
28 canvas->drawRect(clipRect, clipHairline);
30 canvas->drawTextBlob(blob, 0, 0, paint);
31 canvas->clipRect(clipRect);
33 canvas->drawTextBlob(blob, 0, 0, paint);
37 class MixedTextBlobsGM : public GM {
39 MixedTextBlobsGM() { }
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");
47 SkTextBlobBuilder builder;
50 // Text so large we draw as paths
52 paint.setTextSize(385);
53 const char* text = "O";
54 sk_tool_utils::set_portable_typeface(&paint);
57 paint.measureText(text, strlen(text), &bounds);
59 SkScalar yOffset = bounds.height();
60 sk_tool_utils::add_to_text_blob(&builder, text, paint, 10, yOffset);
61 SkScalar corruptedAx = bounds.width();
62 SkScalar corruptedAy = yOffset;
64 const SkScalar boundsHalfWidth = bounds.width() * SK_ScalarHalf;
65 const SkScalar boundsHalfHeight = bounds.height() * SK_ScalarHalf;
67 SkScalar xOffset = boundsHalfWidth;
68 yOffset = boundsHalfHeight;
71 paint.setTextSize(32);
73 paint.setAntiAlias(true);
74 paint.setSubpixelText(true);
75 paint.setLCDRenderText(true);
76 paint.measureText(text, strlen(text), &bounds);
77 sk_tool_utils::add_to_text_blob(&builder, text, paint, xOffset - bounds.width() * 0.25f,
78 yOffset - bounds.height() * 0.5f);
79 yOffset += bounds.height();
83 paint.setAntiAlias(false);
84 paint.setSubpixelText(false);
85 paint.setLCDRenderText(false);
86 paint.setTypeface(fEmojiTypeface);
88 paint.measureText(text, strlen(text), &bounds);
89 sk_tool_utils::add_to_text_blob(&builder, text, paint, xOffset - bounds.width() * 0.3f,
94 paint.setTextSize(12);
96 paint.setTypeface(fReallyBigATypeface);
97 sk_tool_utils::add_to_text_blob(&builder, text, paint, corruptedAx, corruptedAy);
98 fBlob = builder.make();
101 SkString onShortName() override {
102 SkString name("mixedtextblobs");
103 name.append(sk_tool_utils::platform_os_emoji());
107 SkISize onISize() override {
108 return SkISize::Make(kWidth, kHeight);
111 void onDraw(SkCanvas* canvas) override {
113 canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorGRAY));
117 // setup work needed to draw text with different clips
118 paint.setColor(SK_ColorBLACK);
119 canvas->translate(10, 40);
121 paint.setTextSize(40);
123 // compute the bounds of the text and setup some clips
124 SkRect bounds = fBlob->bounds();
126 const SkScalar boundsHalfWidth = bounds.width() * SK_ScalarHalf;
127 const SkScalar boundsHalfHeight = bounds.height() * SK_ScalarHalf;
128 const SkScalar boundsQuarterWidth = boundsHalfWidth * SK_ScalarHalf;
129 const SkScalar boundsQuarterHeight = boundsHalfHeight * SK_ScalarHalf;
131 SkRect upperLeftClip = SkRect::MakeXYWH(bounds.left(), bounds.top(),
132 boundsHalfWidth, boundsHalfHeight);
133 SkRect lowerRightClip = SkRect::MakeXYWH(bounds.centerX(), bounds.centerY(),
134 boundsHalfWidth, boundsHalfHeight);
135 SkRect interiorClip = bounds;
136 interiorClip.inset(boundsQuarterWidth, boundsQuarterHeight);
138 const SkRect clipRects[] = { bounds, upperLeftClip, lowerRightClip, interiorClip};
140 size_t count = sizeof(clipRects) / sizeof(SkRect);
141 for (size_t x = 0; x < count; ++x) {
142 draw_blob(canvas, fBlob.get(), paint, clipRects[x]);
143 if (x == (count >> 1) - 1) {
144 canvas->translate(SkScalarFloorToScalar(bounds.width() + SkIntToScalar(25)),
145 -(x * SkScalarFloorToScalar(bounds.height() +
146 SkIntToScalar(25))));
148 canvas->translate(0, SkScalarFloorToScalar(bounds.height() + SkIntToScalar(25)));
154 sk_sp<SkTypeface> fEmojiTypeface;
155 sk_sp<SkTypeface> fReallyBigATypeface;
156 const char* fEmojiText;
157 sk_sp<SkTextBlob> fBlob;
159 static constexpr int kWidth = 1250;
160 static constexpr int kHeight = 700;
162 typedef GM INHERITED;
165 //////////////////////////////////////////////////////////////////////////////
167 DEF_GM(return new MixedTextBlobsGM;)