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
10#include "Resources.h"
11#include "SkCanvas.h"
12#include "SkGradientShader.h"
13#include "SkStream.h"
14#include "SkTextBlob.h"
15#include "SkTypeface.h"
16
17namespace skiagm {
18
19static void draw_blob(SkCanvas* canvas, const SkTextBlob* blob, const SkPaint& skPaint,
20                      const SkRect& clipRect) {
21    SkPaint clipHairline;
22    clipHairline.setColor(SK_ColorWHITE);
23    clipHairline.setStyle(SkPaint::kStroke_Style);
24
25    SkPaint paint(skPaint);
26    canvas->save();
27    canvas->drawRect(clipRect, clipHairline);
28    paint.setAlpha(0x20);
29    canvas->drawTextBlob(blob, 0, 0, paint);
30    canvas->clipRect(clipRect);
31    paint.setAlpha(0xFF);
32    canvas->drawTextBlob(blob, 0, 0, paint);
33    canvas->restore();
34}
35
36class MixedTextBlobsGM : public GM {
37public:
38    MixedTextBlobsGM() { }
39
40protected:
41    void onOnceBeforeDraw() override {
42#ifndef SK_BUILD_FOR_MAC
43        fEmojiTypeface.reset(GetResourceAsTypeface("/fonts/Funkster.ttf"));
44        fEmojiText = "Emoji!!!";
45#else
46        fEmojiTypeface.reset(SkTypeface::CreateFromName("Apple Color Emoji", SkTypeface::kNormal));
47        fEmojiText = "\xF0\x9F\x92\xB0" "\xF0\x9F\x8F\xA1" "\xF0\x9F\x8E\x85" // ������
48                     "\xF0\x9F\x8D\xAA" "\xF0\x9F\x8D\x95" "\xF0\x9F\x9A\x80"; // ������
49#endif
50        fReallyBigATypeface.reset(GetResourceAsTypeface("/fonts/ReallyBigA.ttf"));
51
52        SkTextBlobBuilder builder;
53
54        // make textblob
55        // Text so large we draw as paths
56        SkPaint paint;
57        paint.setTextSize(384);
58        const char* text = "O";
59        sk_tool_utils::set_portable_typeface(&paint);
60
61        SkRect bounds;
62        paint.measureText(text, strlen(text), &bounds);
63
64        SkScalar yOffset = bounds.height();
65        sk_tool_utils::add_to_text_blob(&builder, text, paint, 10, yOffset);
66        SkScalar corruptedAx = bounds.width();
67        SkScalar corruptedAy = yOffset;
68
69        const SkScalar boundsHalfWidth = bounds.width() * SK_ScalarHalf;
70        const SkScalar boundsHalfHeight = bounds.height() * SK_ScalarHalf;
71
72        SkScalar xOffset = boundsHalfWidth;
73        yOffset = boundsHalfHeight;
74
75        // LCD
76        paint.setTextSize(32);
77        text = "LCD!!!!!";
78        paint.setSubpixelText(true);
79        paint.setLCDRenderText(true);
80        paint.measureText(text, strlen(text), &bounds);
81        sk_tool_utils::add_to_text_blob(&builder, text, paint, xOffset - bounds.width() * 0.25f,
82                                        yOffset - bounds.height() * 0.5f);
83        yOffset += bounds.height();
84
85        // color emoji
86        paint.setSubpixelText(false);
87        paint.setLCDRenderText(false);
88        paint.setTypeface(fEmojiTypeface);
89        text = fEmojiText;
90        paint.measureText(text, strlen(text), &bounds);
91        sk_tool_utils::add_to_text_blob(&builder, text, paint, xOffset - bounds.width() * 0.3f,
92                                        yOffset);
93
94        // Corrupted font
95        paint.setTextSize(12);
96        text = "aA";
97        paint.setTypeface(fReallyBigATypeface);
98        sk_tool_utils::add_to_text_blob(&builder, text, paint, corruptedAx, corruptedAy);
99        fBlob.reset(builder.build());
100    }
101
102    SkString onShortName() override {
103        return SkString("mixedtextblobs");
104    }
105
106    SkISize onISize() override {
107        return SkISize::Make(kWidth, kHeight);
108    }
109
110    void onDraw(SkCanvas* canvas) override {
111
112        canvas->drawColor(SK_ColorGRAY);
113
114        SkPaint paint;
115
116        // setup work needed to draw text with different clips
117        paint.setColor(SK_ColorBLACK);
118        canvas->translate(10, 40);
119
120        paint.setTextSize(40);
121
122        // compute the bounds of the text and setup some clips
123        SkRect bounds = fBlob->bounds();
124
125        const SkScalar boundsHalfWidth = bounds.width() * SK_ScalarHalf;
126        const SkScalar boundsHalfHeight = bounds.height() * SK_ScalarHalf;
127        const SkScalar boundsQuarterWidth = boundsHalfWidth * SK_ScalarHalf;
128        const SkScalar boundsQuarterHeight = boundsHalfHeight * SK_ScalarHalf;
129
130        SkRect upperLeftClip = SkRect::MakeXYWH(bounds.left(), bounds.top(),
131                                                boundsHalfWidth, boundsHalfHeight);
132        SkRect lowerRightClip = SkRect::MakeXYWH(bounds.centerX(), bounds.centerY(),
133                                                 boundsHalfWidth, boundsHalfHeight);
134        SkRect interiorClip = bounds;
135        interiorClip.inset(boundsQuarterWidth, boundsQuarterHeight);
136
137        const SkRect clipRects[] = { bounds, upperLeftClip, lowerRightClip, interiorClip};
138
139        size_t count = sizeof(clipRects) / sizeof(SkRect);
140        for (size_t x = 0; x < count; ++x) {
141            draw_blob(canvas, fBlob, paint, clipRects[x]);
142            if (x == (count >> 1) - 1) {
143                canvas->translate(SkScalarFloorToScalar(bounds.width() + SkIntToScalar(25)),
144                                  -(x * SkScalarFloorToScalar(bounds.height() +
145                                    SkIntToScalar(25))));
146            } else {
147                canvas->translate(0, SkScalarFloorToScalar(bounds.height() + SkIntToScalar(25)));
148            }
149        }
150    }
151
152private:
153    SkAutoTUnref<SkTypeface> fEmojiTypeface;
154    SkAutoTUnref<SkTypeface> fReallyBigATypeface;
155    const char* fEmojiText;
156    SkAutoTUnref<const SkTextBlob> fBlob;
157
158    static const int kWidth = 1250;
159    static const int kHeight = 700;
160
161    typedef GM INHERITED;
162};
163
164//////////////////////////////////////////////////////////////////////////////
165
166DEF_GM( return SkNEW(MixedTextBlobsGM); )
167}
168