1/*
2 * Copyright 2017 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#if SK_SUPPORT_ATLAS_TEXT
11
12#include "SkAtlasTextContext.h"
13#include "SkAtlasTextFont.h"
14#include "SkAtlasTextTarget.h"
15#include "SkBitmap.h"
16#include "SkCanvas.h"
17#include "SkTypeface.h"
18#include "SkUtils.h"
19#include "gpu/TestContext.h"
20#include "gpu/atlastext/GLTestAtlasTextRenderer.h"
21#include "gpu/atlastext/TestAtlasTextRenderer.h"
22#include "sk_tool_utils.h"
23
24// GM that draws text using the Atlas Text interface offscreen and then blits that to the canvas.
25
26static SkScalar draw_string(SkAtlasTextTarget* target, const SkString& text, SkScalar x, SkScalar y,
27                            uint32_t color, sk_sp<SkTypeface> typeface, float size) {
28    if (!text.size()) {
29        return x;
30    }
31    auto font = SkAtlasTextFont::Make(typeface, size);
32    int cnt = SkUTF8_CountUnichars(text.c_str());
33    std::unique_ptr<SkGlyphID[]> glyphs(new SkGlyphID[cnt]);
34    typeface->charsToGlyphs(text.c_str(), SkTypeface::Encoding::kUTF8_Encoding, glyphs.get(), cnt);
35
36    // Using a paint to get the positions for each glyph.
37    SkPaint paint;
38    paint.setTextSize(size);
39    paint.setTypeface(std::move(typeface));
40    paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
41    std::unique_ptr<SkScalar[]> widths(new SkScalar[cnt]);
42    paint.getTextWidths(glyphs.get(), cnt * sizeof(SkGlyphID), widths.get(), nullptr);
43
44    std::unique_ptr<SkPoint[]> positions(new SkPoint[cnt]);
45    positions[0] = {x, y};
46    for (int i = 1; i < cnt; ++i) {
47        positions[i] = {positions[i - 1].fX + widths[i - 1], y};
48    }
49
50    target->drawText(glyphs.get(), positions.get(), cnt, color, *font);
51
52    // Return the width of the of draw.
53    return positions[cnt - 1].fX + widths[cnt - 1] - positions[0].fX;
54}
55
56class AtlasTextGM : public skiagm::GM {
57public:
58    AtlasTextGM() = default;
59
60protected:
61    SkString onShortName() override { return SkString("atlastext"); }
62
63    SkISize onISize() override { return SkISize::Make(kSize, kSize); }
64
65    void onOnceBeforeDraw() override {
66        fRenderer = sk_gpu_test::MakeGLTestAtlasTextRenderer();
67        if (!fRenderer) {
68            return;
69        }
70        fContext = SkAtlasTextContext::Make(fRenderer);
71        auto targetHandle = fRenderer->makeTargetHandle(kSize, kSize);
72        fTarget = SkAtlasTextTarget::Make(fContext, kSize, kSize, targetHandle);
73
74        fTypefaces[0] = sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Italic());
75        fTypefaces[1] =
76                sk_tool_utils::create_portable_typeface("sans-serif", SkFontStyle::Italic());
77        fTypefaces[2] = sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Normal());
78        fTypefaces[3] =
79                sk_tool_utils::create_portable_typeface("sans-serif", SkFontStyle::Normal());
80        fTypefaces[4] = sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Bold());
81        fTypefaces[5] = sk_tool_utils::create_portable_typeface("sans-serif", SkFontStyle::Bold());
82    }
83
84    void onDraw(SkCanvas* canvas) override {
85        if (!fRenderer) {
86            canvas->clear(SK_ColorRED);
87            return;
88        }
89        fRenderer->clearTarget(fTarget->handle(), 0xFF808080);
90        auto bmp = this->drawText();
91        SkPaint paint;
92        paint.setBlendMode(SkBlendMode::kSrc);
93        canvas->drawBitmap(bmp, 0, 0);
94    }
95
96private:
97    SkBitmap drawText() {
98        static const int kSizes[] = {8, 13, 18, 23, 30};
99
100        static const SkString kTexts[] = {SkString("ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
101                                          SkString("abcdefghijklmnopqrstuvwxyz"),
102                                          SkString("0123456789"),
103                                          SkString("!@#$%^&*()<>[]{}")};
104        SkScalar x = 0;
105        SkScalar y = 10;
106
107        SkRandom random;
108        do {
109            for (auto s : kSizes) {
110                auto size = 2 * s;
111                for (const auto& typeface : fTypefaces) {
112                    for (const auto& text : kTexts) {
113                        // Choose a random color but don't let alpha be too small to see.
114                        uint32_t color = random.nextU() | 0x40000000;
115                        fTarget->save();
116                        // Randomly add a little bit of perspective
117                        if (random.nextBool()) {
118                            SkMatrix persp;
119                            persp.reset();
120                            persp.setPerspY(0.0005f);
121                            persp.preTranslate(-x, -y + s);
122                            persp.postTranslate(x, y - s);
123                            fTarget->concat(persp);
124                        }
125                        // Randomly switch between positioning with a matrix vs x, y passed to draw.
126                        SkScalar drawX = x, drawY = y;
127                        if (random.nextBool()) {
128                            fTarget->translate(x, y);
129                            drawX = drawY = 0;
130                        }
131                        x += size +
132                             draw_string(fTarget.get(), text, drawX, drawY, color, typeface, size);
133                        x = SkScalarCeilToScalar(x);
134                        fTarget->restore();
135                        // Flush periodically to test continued drawing after a flush.
136                        if ((random.nextU() % 8) == 0) {
137                            fTarget->flush();
138                        }
139                        if (x + 100 > kSize) {
140                            x = 0;
141                            y += SkScalarCeilToScalar(size + 3);
142                            if (y > kSize) {
143                                fTarget->flush();
144                                return fRenderer->readTargetHandle(fTarget->handle());
145                            }
146                        }
147                    }
148                }
149            }
150        } while (true);
151    }
152
153    static constexpr int kSize = 1280;
154
155    sk_sp<SkTypeface> fTypefaces[6];
156    sk_sp<sk_gpu_test::TestAtlasTextRenderer> fRenderer;
157    std::unique_ptr<SkAtlasTextTarget> fTarget;
158    sk_sp<SkAtlasTextContext> fContext;
159
160    typedef GM INHERITED;
161};
162
163constexpr int AtlasTextGM::kSize;
164
165//////////////////////////////////////////////////////////////////////////////
166
167DEF_GM(return new AtlasTextGM;)
168
169#endif
170