Home | History | Annotate | Download | only in gm
      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 #include "sk_tool_utils.h"
     10 
     11 #include "SkCanvas.h"
     12 #include "SkGradientShader.h"
     13 #include "SkPoint.h"
     14 #include "SkShader.h"
     15 #include "SkTextBlob.h"
     16 #include "SkTDArray.h"
     17 #include "SkTypeface.h"
     18 
     19 // This GM exercises drawTextBlob offset vs. shader space behavior.
     20 class TextBlobShaderGM : public skiagm::GM {
     21 public:
     22     TextBlobShaderGM(const char* txt) {
     23         SkPaint p;
     24         sk_tool_utils::set_portable_typeface(&p);
     25         size_t txtLen = strlen(txt);
     26         fGlyphs.append(p.textToGlyphs(txt, txtLen, nullptr));
     27         p.textToGlyphs(txt, txtLen, fGlyphs.begin());
     28     }
     29 
     30 protected:
     31 
     32     void onOnceBeforeDraw() override {
     33         SkPaint p;
     34         p.setAntiAlias(true);
     35         p.setSubpixelText(true);
     36         p.setTextSize(30);
     37         p.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
     38         sk_tool_utils::set_portable_typeface(&p);
     39 
     40         SkTextBlobBuilder builder;
     41         int glyphCount = fGlyphs.count();
     42         const SkTextBlobBuilder::RunBuffer* run;
     43 
     44         run = &builder.allocRun(p, glyphCount, 10, 10, nullptr);
     45         memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t));
     46 
     47         run = &builder.allocRunPosH(p, glyphCount,  80, nullptr);
     48         memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t));
     49         for (int i = 0; i < glyphCount; ++i) {
     50             run->pos[i] = p.getTextSize() * i * .75f;
     51         }
     52 
     53         run = &builder.allocRunPos(p, glyphCount, nullptr);
     54         memcpy(run->glyphs, fGlyphs.begin(), glyphCount * sizeof(uint16_t));
     55         for (int i = 0; i < glyphCount; ++i) {
     56             run->pos[i * 2] = p.getTextSize() * i * .75f;
     57             run->pos[i * 2 + 1] = 150 + 5 * sinf((float)i * 8 / glyphCount);
     58         }
     59 
     60         fBlob = builder.make();
     61 
     62         SkColor  colors[2];
     63         colors[0] = SK_ColorRED;
     64         colors[1] = SK_ColorGREEN;
     65 
     66         SkScalar pos[SK_ARRAY_COUNT(colors)];
     67         for (unsigned i = 0; i < SK_ARRAY_COUNT(pos); ++i) {
     68             pos[i] = (float)i / (SK_ARRAY_COUNT(pos) - 1);
     69         }
     70 
     71         SkISize sz = this->onISize();
     72         fShader = SkGradientShader::MakeRadial(SkPoint::Make(SkIntToScalar(sz.width() / 2),
     73                                                SkIntToScalar(sz.height() / 2)),
     74                                                sz.width() * .66f, colors, pos,
     75                                                SK_ARRAY_COUNT(colors),
     76                                                SkShader::kRepeat_TileMode);
     77     }
     78 
     79     SkString onShortName() override {
     80         return SkString("textblobshader");
     81     }
     82 
     83     SkISize onISize() override {
     84         return SkISize::Make(640, 480);
     85     }
     86 
     87     void onDraw(SkCanvas* canvas) override {
     88         SkPaint p;
     89         p.setStyle(SkPaint::kFill_Style);
     90         p.setShader(fShader);
     91 
     92         SkISize sz = this->onISize();
     93         constexpr int kXCount = 4;
     94         constexpr int kYCount = 3;
     95         for (int i = 0; i < kXCount; ++i) {
     96             for (int j = 0; j < kYCount; ++j) {
     97                 canvas->drawTextBlob(fBlob,
     98                                      SkIntToScalar(i * sz.width() / kXCount),
     99                                      SkIntToScalar(j * sz.height() / kYCount),
    100                                      p);
    101             }
    102         }
    103     }
    104 
    105 private:
    106     SkTDArray<uint16_t> fGlyphs;
    107     sk_sp<SkTextBlob>   fBlob;
    108     sk_sp<SkShader>     fShader;
    109 
    110     typedef skiagm::GM INHERITED;
    111 };
    112 
    113 DEF_GM(return new TextBlobShaderGM("Blobber");)
    114