Index: gm/textblobshader.cpp |
diff --git a/gm/textblobshader.cpp b/gm/textblobshader.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..164ef6e48f01c95b6631b49c29c833deb472f647 |
--- /dev/null |
+++ b/gm/textblobshader.cpp |
@@ -0,0 +1,105 @@ |
+/* |
+ * Copyright 2014 Google Inc. |
+ * |
+ * Use of this source code is governed by a BSD-style license that can be |
+ * found in the LICENSE file. |
+ */ |
+ |
+#include "gm.h" |
+ |
+#include "SkCanvas.h" |
+#include "SkGradientShader.h" |
+#include "SkPoint.h" |
+#include "SkShader.h" |
+#include "SkTextBlob.h" |
+#include "SkTDArray.h" |
+#include "SkTypeface.h" |
+ |
robertphillips
2014/09/19 17:20:26
// This GM exercises ... ?
f(malita)
2014/09/19 17:55:53
Done.
|
+class TextBlobShaderGM : public skiagm::GM { |
+public: |
+ TextBlobShaderGM(const char* txt) { |
robertphillips
2014/09/19 17:20:26
Would it make sense to do any or all of this in on
f(malita)
2014/09/19 17:55:52
Done.
|
+ SkAutoTUnref<SkTypeface> tface( |
+ sk_tool_utils::create_portable_typeface("Times", SkTypeface::kBold)); |
+ SkPaint p; |
+ p.setAntiAlias(true); |
+ p.setSubpixelText(true); |
+ p.setTypeface(tface); |
+ p.setTextSize(30); |
+ |
+ SkTDArray<uint16_t> glyphs; |
+ size_t txtLen = strlen(txt); |
+ int glyphCount = p.textToGlyphs(txt, txtLen, NULL); |
+ glyphs.append(glyphCount); |
+ p.textToGlyphs(txt, txtLen, glyphs.begin()); |
+ |
+ SkTextBlobBuilder builder; |
+ const SkTextBlobBuilder::RunBuffer* run; |
+ p.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
+ run = &builder.allocRun(p, glyphCount, 10, 10, NULL); |
+ memcpy(run->glyphs, glyphs.begin(), glyphCount * sizeof(uint16_t)); |
+ |
+ run = &builder.allocRunPosH(p, glyphCount, 80, NULL); |
+ memcpy(run->glyphs, glyphs.begin(), glyphCount * sizeof(uint16_t)); |
+ for (int i = 0; i < glyphCount; ++i) { |
+ run->pos[i] = p.getTextSize() * i * .75f; |
+ } |
+ |
+ run = &builder.allocRunPos(p, glyphCount, NULL); |
+ memcpy(run->glyphs, glyphs.begin(), glyphCount * sizeof(uint16_t)); |
+ for (int i = 0; i < glyphCount; ++i) { |
+ run->pos[i * 2] = p.getTextSize() * i * .75f; |
+ run->pos[i * 2 + 1] = 150 + 5 * sinf((float)i * 8 / glyphCount); |
+ } |
+ |
+ fBlob.reset(builder.build()); |
+ |
+ SkColor colors[2]; |
+ colors[0] = SK_ColorRED; |
+ colors[1] = SK_ColorGREEN; |
+ |
+ SkScalar pos[SK_ARRAY_COUNT(colors)]; |
+ for (unsigned i = 0; i < SK_ARRAY_COUNT(pos); ++i) { |
+ pos[i] = (float)i / (SK_ARRAY_COUNT(pos) - 1); |
+ } |
+ |
+ SkISize sz = this->onISize(); |
+ fShader.reset(SkGradientShader::CreateRadial(SkPoint::Make(sz.width() / 2, sz.height() / 2), |
+ sz.width() * .66f, colors, pos, |
+ SK_ARRAY_COUNT(colors), |
+ SkShader::kRepeat_TileMode)); |
+ } |
+ |
+ virtual SkString onShortName() SK_OVERRIDE { |
+ return SkString("textblobshader"); |
+ } |
+ |
+ virtual SkISize onISize() SK_OVERRIDE { |
+ return SkISize::Make(640, 480); |
+ } |
+ |
+ virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
+ SkPaint p; |
+ p.setStyle(SkPaint::kFill_Style); |
+ p.setShader(fShader); |
+ |
+ SkISize sz = this->onISize(); |
+ static const int kXCount = 4; |
+ static const int kYCount = 3; |
+ for (int i = 0; i < kXCount; ++i) { |
+ for (int j = 0; j < kYCount; ++j) { |
+ canvas->drawTextBlob(fBlob, |
+ i * sz.width() / kXCount, |
+ j * sz.height() / kYCount, |
+ p); |
+ } |
+ } |
+ } |
+ |
+private: |
+ SkAutoTUnref<const SkTextBlob> fBlob; |
+ SkAutoTUnref<SkShader> fShader; |
+ |
+ typedef skiagm::GM INHERITED; |
+}; |
+ |
+DEF_GM( return SkNEW_ARGS(TextBlobShaderGM, ("Blobber")); ) |