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 if (!targetHandle) { 73 return; 74 } 75 fTarget = SkAtlasTextTarget::Make(fContext, kSize, kSize, targetHandle); 76 77 fTypefaces[0] = sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Italic()); 78 fTypefaces[1] = 79 sk_tool_utils::create_portable_typeface("sans-serif", SkFontStyle::Italic()); 80 fTypefaces[2] = sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Normal()); 81 fTypefaces[3] = 82 sk_tool_utils::create_portable_typeface("sans-serif", SkFontStyle::Normal()); 83 fTypefaces[4] = sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Bold()); 84 fTypefaces[5] = sk_tool_utils::create_portable_typeface("sans-serif", SkFontStyle::Bold()); 85 } 86 87 void onDraw(SkCanvas* canvas) override { 88 if (!fRenderer || !fTarget || !fTarget->handle()) { 89 canvas->clear(SK_ColorRED); 90 return; 91 } 92 fRenderer->clearTarget(fTarget->handle(), 0xFF808080); 93 auto bmp = this->drawText(); 94 SkPaint paint; 95 paint.setBlendMode(SkBlendMode::kSrc); 96 canvas->drawBitmap(bmp, 0, 0); 97 } 98 99private: 100 SkBitmap drawText() { 101 static const int kSizes[] = {8, 13, 18, 23, 30}; 102 103 static const SkString kTexts[] = {SkString("ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 104 SkString("abcdefghijklmnopqrstuvwxyz"), 105 SkString("0123456789"), 106 SkString("!@#$%^&*()<>[]{}")}; 107 SkScalar x = 0; 108 SkScalar y = 10; 109 110 SkRandom random; 111 do { 112 for (auto s : kSizes) { 113 auto size = 2 * s; 114 for (const auto& typeface : fTypefaces) { 115 for (const auto& text : kTexts) { 116 // Choose a random color but don't let alpha be too small to see. 117 uint32_t color = random.nextU() | 0x40000000; 118 fTarget->save(); 119 // Randomly add a little bit of perspective 120 if (random.nextBool()) { 121 SkMatrix persp; 122 persp.reset(); 123 persp.setPerspY(0.0005f); 124 persp.preTranslate(-x, -y + s); 125 persp.postTranslate(x, y - s); 126 fTarget->concat(persp); 127 } 128 // Randomly switch between positioning with a matrix vs x, y passed to draw. 129 SkScalar drawX = x, drawY = y; 130 if (random.nextBool()) { 131 fTarget->translate(x, y); 132 drawX = drawY = 0; 133 } 134 x += size + 135 draw_string(fTarget.get(), text, drawX, drawY, color, typeface, size); 136 x = SkScalarCeilToScalar(x); 137 fTarget->restore(); 138 // Flush periodically to test continued drawing after a flush. 139 if ((random.nextU() % 8) == 0) { 140 fTarget->flush(); 141 } 142 if (x + 100 > kSize) { 143 x = 0; 144 y += SkScalarCeilToScalar(size + 3); 145 if (y > kSize) { 146 fTarget->flush(); 147 return fRenderer->readTargetHandle(fTarget->handle()); 148 } 149 } 150 } 151 } 152 } 153 } while (true); 154 } 155 156 static constexpr int kSize = 1280; 157 158 sk_sp<SkTypeface> fTypefaces[6]; 159 sk_sp<sk_gpu_test::TestAtlasTextRenderer> fRenderer; 160 std::unique_ptr<SkAtlasTextTarget> fTarget; 161 sk_sp<SkAtlasTextContext> fContext; 162 163 typedef GM INHERITED; 164}; 165 166constexpr int AtlasTextGM::kSize; 167 168////////////////////////////////////////////////////////////////////////////// 169 170DEF_GM(return new AtlasTextGM;) 171 172#endif 173