Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(430)

Side by Side Diff: gm/textblobshader.cpp

Issue 586743002: SkTextBlob shader space workaround. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | gyp/gmslides.gypi » ('j') | src/core/SkDevice.cpp » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2014 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 "SkCanvas.h"
11 #include "SkGradientShader.h"
12 #include "SkPoint.h"
13 #include "SkShader.h"
14 #include "SkTextBlob.h"
15 #include "SkTDArray.h"
16 #include "SkTypeface.h"
17
robertphillips 2014/09/19 17:20:26 // This GM exercises ... ?
f(malita) 2014/09/19 17:55:53 Done.
18 class TextBlobShaderGM : public skiagm::GM {
19 public:
20 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.
21 SkAutoTUnref<SkTypeface> tface(
22 sk_tool_utils::create_portable_typeface("Times", SkTypeface::kBold)) ;
23 SkPaint p;
24 p.setAntiAlias(true);
25 p.setSubpixelText(true);
26 p.setTypeface(tface);
27 p.setTextSize(30);
28
29 SkTDArray<uint16_t> glyphs;
30 size_t txtLen = strlen(txt);
31 int glyphCount = p.textToGlyphs(txt, txtLen, NULL);
32 glyphs.append(glyphCount);
33 p.textToGlyphs(txt, txtLen, glyphs.begin());
34
35 SkTextBlobBuilder builder;
36 const SkTextBlobBuilder::RunBuffer* run;
37 p.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
38 run = &builder.allocRun(p, glyphCount, 10, 10, NULL);
39 memcpy(run->glyphs, glyphs.begin(), glyphCount * sizeof(uint16_t));
40
41 run = &builder.allocRunPosH(p, glyphCount, 80, NULL);
42 memcpy(run->glyphs, glyphs.begin(), glyphCount * sizeof(uint16_t));
43 for (int i = 0; i < glyphCount; ++i) {
44 run->pos[i] = p.getTextSize() * i * .75f;
45 }
46
47 run = &builder.allocRunPos(p, glyphCount, NULL);
48 memcpy(run->glyphs, glyphs.begin(), glyphCount * sizeof(uint16_t));
49 for (int i = 0; i < glyphCount; ++i) {
50 run->pos[i * 2] = p.getTextSize() * i * .75f;
51 run->pos[i * 2 + 1] = 150 + 5 * sinf((float)i * 8 / glyphCount);
52 }
53
54 fBlob.reset(builder.build());
55
56 SkColor colors[2];
57 colors[0] = SK_ColorRED;
58 colors[1] = SK_ColorGREEN;
59
60 SkScalar pos[SK_ARRAY_COUNT(colors)];
61 for (unsigned i = 0; i < SK_ARRAY_COUNT(pos); ++i) {
62 pos[i] = (float)i / (SK_ARRAY_COUNT(pos) - 1);
63 }
64
65 SkISize sz = this->onISize();
66 fShader.reset(SkGradientShader::CreateRadial(SkPoint::Make(sz.width() / 2, sz.height() / 2),
67 sz.width() * .66f, colors, pos,
68 SK_ARRAY_COUNT(colors),
69 SkShader::kRepeat_TileMode) );
70 }
71
72 virtual SkString onShortName() SK_OVERRIDE {
73 return SkString("textblobshader");
74 }
75
76 virtual SkISize onISize() SK_OVERRIDE {
77 return SkISize::Make(640, 480);
78 }
79
80 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
81 SkPaint p;
82 p.setStyle(SkPaint::kFill_Style);
83 p.setShader(fShader);
84
85 SkISize sz = this->onISize();
86 static const int kXCount = 4;
87 static const int kYCount = 3;
88 for (int i = 0; i < kXCount; ++i) {
89 for (int j = 0; j < kYCount; ++j) {
90 canvas->drawTextBlob(fBlob,
91 i * sz.width() / kXCount,
92 j * sz.height() / kYCount,
93 p);
94 }
95 }
96 }
97
98 private:
99 SkAutoTUnref<const SkTextBlob> fBlob;
100 SkAutoTUnref<SkShader> fShader;
101
102 typedef skiagm::GM INHERITED;
103 };
104
105 DEF_GM( return SkNEW_ARGS(TextBlobShaderGM, ("Blobber")); )
OLDNEW
« no previous file with comments | « no previous file | gyp/gmslides.gypi » ('j') | src/core/SkDevice.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698