| 1 |
/* |
| 2 |
* Copyright 2021 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/gm.h" |
| 9 |
#include "include/core/SkCanvas.h" |
| 10 |
#include "include/core/SkColor.h" |
| 11 |
#include "include/core/SkFont.h" |
| 12 |
#include "include/core/SkFontStyle.h" |
| 13 |
#include "include/core/SkFontTypes.h" |
| 14 |
#include "include/core/SkPaint.h" |
| 15 |
#include "include/core/SkPoint.h" |
| 16 |
#include "include/core/SkRect.h" |
| 17 |
#include "include/core/SkRefCnt.h" |
| 18 |
#include "include/core/SkScalar.h" |
| 19 |
#include "include/core/SkSize.h" |
| 20 |
#include "include/core/SkString.h" |
| 21 |
#include "include/core/SkTextBlob.h" |
| 22 |
#include "include/core/SkTypeface.h" |
| 23 |
#include "include/core/SkTypes.h" |
| 24 |
#include "include/private/base/SkTDArray.h" |
| 25 |
#include "include/private/chromium/Slug.h" |
| 26 |
#include "tools/ToolUtils.h" |
| 27 |
#include "tools/fonts/FontToolUtils.h" |
| 28 |
|
| 29 |
#if defined(SK_GRAPHITE) |
| 30 |
#include "include/gpu/graphite/ContextOptions.h" |
| 31 |
#endif |
| 32 |
|
| 33 |
#if defined(SK_GANESH) |
| 34 |
#include "include/gpu/ganesh/GrContextOptions.h" |
| 35 |
#endif |
| 36 |
|
| 37 |
#if defined(SK_GANESH) || defined(SK_GRAPHITE) |
| 38 |
|
| 39 |
class SlugGM : public skiagm::GM { |
| 40 |
public: |
| 41 |
SlugGM(const char* txt) : fText(txt) {} |
| 42 |
|
| 43 |
protected: |
| 44 |
#if defined(SK_GANESH) |
| 45 |
void modifyGrContextOptions(GrContextOptions* ctxOptions) override { |
| 46 |
ctxOptions->fSupportBilerpFromGlyphAtlas = true; |
| 47 |
} |
| 48 |
#endif |
| 49 |
|
| 50 |
#if defined(SK_GRAPHITE) |
| 51 |
void modifyGraphiteContextOptions(skgpu::graphite::ContextOptions* options) const override { |
| 52 |
options->fSupportBilerpFromGlyphAtlas = true; |
| 53 |
} |
| 54 |
#endif |
| 55 |
|
| 56 |
void onOnceBeforeDraw() override { |
| 57 |
fTypeface = ToolUtils::CreatePortableTypeface("serif", SkFontStyle()); |
| 58 |
SkFont font(fTypeface); |
| 59 |
size_t txtLen = strlen(fText); |
| 60 |
int glyphCount = font.countText(fText, txtLen, SkTextEncoding::kUTF8); |
| 61 |
|
| 62 |
fGlyphs.append(glyphCount); |
| 63 |
font.textToGlyphs(fText, txtLen, SkTextEncoding::kUTF8, fGlyphs); |
| 64 |
} |
| 65 |
|
| 66 |
SkString getName() const override { return SkString("slug"); } |
| 67 |
|
| 68 |
SkISize getISize() override { return SkISize::Make(1000, 480); } |
| 69 |
|
| 70 |
void onDraw(SkCanvas* canvas) override { |
| 71 |
sk_sp<SkTextBlob> blob(this->makeBlob()); |
| 72 |
SkPaint p; |
| 73 |
p.setAntiAlias(true); |
| 74 |
canvas->clipIRect(SkIRect::MakeSize(this->getISize()).makeInset(40, 50)); |
| 75 |
canvas->scale(1.3f, 1.3f); |
| 76 |
sk_sp<sktext::gpu::Slug> slug = sktext::gpu::Slug::ConvertBlob(canvas, *blob, {10, 10}, p); |
| 77 |
if (slug == nullptr) { |
| 78 |
return; |
| 79 |
} |
| 80 |
canvas->translate(0.5, 0.5); |
| 81 |
canvas->translate(30, 30); |
| 82 |
canvas->drawTextBlob(blob, 10, 10, p); |
| 83 |
canvas->translate(370, 0); |
| 84 |
slug->draw(canvas, p); |
| 85 |
for (float scale = 1.5; scale < 4; scale += 0.5) { |
| 86 |
canvas->translate(-370, 20 * scale); |
| 87 |
canvas->save(); |
| 88 |
canvas->scale(scale, scale); |
| 89 |
canvas->rotate(5); |
| 90 |
canvas->drawTextBlob(blob, 10, 10, p); |
| 91 |
canvas->restore(); |
| 92 |
canvas->translate(370, 0); |
| 93 |
canvas->save(); |
| 94 |
canvas->scale(scale, scale); |
| 95 |
canvas->rotate(5); |
| 96 |
|
| 97 |
slug->draw(canvas, p); |
| 98 |
canvas->restore(); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
private: |
| 103 |
sk_sp<SkTextBlob> makeBlob() { |
| 104 |
SkTextBlobBuilder builder; |
| 105 |
|
| 106 |
SkFont font; |
| 107 |
font.setSubpixel(true); |
| 108 |
font.setEdging(SkFont::Edging::kAntiAlias); |
| 109 |
font.setTypeface(fTypeface); |
| 110 |
font.setSize(16); |
| 111 |
|
| 112 |
const SkTextBlobBuilder::RunBuffer& buf = builder.allocRun(font, fGlyphs.size(), 0, 0); |
| 113 |
memcpy(buf.glyphs, fGlyphs.begin(), fGlyphs.size() * sizeof(uint16_t)); |
| 114 |
return builder.make(); |
| 115 |
} |
| 116 |
|
| 117 |
SkTDArray<SkGlyphID> fGlyphs; |
| 118 |
sk_sp<SkTypeface> fTypeface; |
| 119 |
const char* fText; |
| 120 |
using INHERITED = skiagm::GM; |
| 121 |
}; |
| 122 |
|
| 123 |
DEF_GM(return new SlugGM("hamburgefons");) |
| 124 |
#endif |