Home | History | Annotate | Download | only in samplecode
      1 /*
      2  * Copyright 2011 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 #include "SampleCode.h"
      8 #include "SkBlurMask.h"
      9 #include "SkBlurDrawLooper.h"
     10 #include "SkCanvas.h"
     11 #include "SkPath.h"
     12 #include "SkPathMeasure.h"
     13 
     14 #define REPEAT_COUNT    1
     15 
     16 static void textStrokePath(SkCanvas* canvas) {
     17     SkPaint paint;
     18     SkPath  path;
     19     SkRect  rect;
     20 
     21     canvas->save();
     22     canvas->scale(SkIntToScalar(250),SkIntToScalar(250));
     23 
     24     rect.set(0.0f,  0.21f,
     25              0.78f, 0.99f);
     26 
     27     path.addArc(rect, SkIntToScalar(280), SkIntToScalar(350));
     28 
     29     paint.setAntiAlias(true);
     30     paint.setStyle(SkPaint::kStroke_Style);
     31     paint.setColor(0xFFFF0000);
     32     paint.setTextSize(0.085f);
     33     paint.setStrokeWidth(.005f);
     34 
     35     canvas->drawPath(path, paint);
     36 
     37     paint.setLooper(SkBlurDrawLooper::Make(SK_ColorBLACK, SkBlurMask::ConvertRadiusToSigma(0.002f),
     38                                            0.0f, 0.0f));
     39 
     40     const char* text = "DRAWING STROKED TEXT WITH A BLUR ON A PATH";
     41     size_t      len = strlen(text);
     42 
     43     canvas->drawTextOnPathHV(text, len, path, 0,
     44                              -0.025f, paint);
     45     canvas->restore();
     46 }
     47 
     48 static void textPathMatrix(SkCanvas* canvas) {
     49     SkPaint paint;
     50     SkPath  path;
     51     SkMatrix matrix;
     52 
     53     path.moveTo(SkIntToScalar(050), SkIntToScalar(200));
     54     path.quadTo(SkIntToScalar(250), SkIntToScalar(000),
     55                 SkIntToScalar(450), SkIntToScalar(200));
     56 
     57     paint.setAntiAlias(true);
     58 
     59     paint.setStyle(SkPaint::kStroke_Style);
     60     canvas->drawPath(path, paint);
     61     paint.setStyle(SkPaint::kFill_Style);
     62     paint.setTextSize(SkIntToScalar(48));
     63     paint.setTextAlign(SkPaint::kRight_Align);
     64 
     65     const char* text = "Reflection";
     66     size_t      len = strlen(text);
     67 
     68     SkPathMeasure   meas(path, false);
     69     SkScalar pathLen = meas.getLength();
     70 
     71     canvas->drawTextOnPath(text, len, path, nullptr, paint);
     72 
     73     paint.setColor(SK_ColorRED);
     74     matrix.setScale(-SK_Scalar1, SK_Scalar1);
     75     matrix.postTranslate(pathLen, 0);
     76     canvas->drawTextOnPath(text, len, path, &matrix, paint);
     77 
     78     paint.setColor(SK_ColorBLUE);
     79     matrix.setScale(SK_Scalar1, -SK_Scalar1);
     80     canvas->drawTextOnPath(text, len, path, &matrix, paint);
     81 
     82     paint.setColor(SK_ColorGREEN);
     83     matrix.setScale(-SK_Scalar1, -SK_Scalar1);
     84     matrix.postTranslate(pathLen, 0);
     85     canvas->drawTextOnPath(text, len, path, &matrix, paint);
     86 }
     87 
     88 class TextOnPathView : public SampleView {
     89 public:
     90     SkPath      fPath;
     91     SkScalar    fHOffset;
     92 
     93 protected:
     94     void onOnceBeforeDraw() override {
     95         SkRect r;
     96         r.set(SkIntToScalar(100), SkIntToScalar(100),
     97               SkIntToScalar(300), SkIntToScalar(300));
     98         fPath.addOval(r);
     99         fPath.offset(SkIntToScalar(-50), SkIntToScalar(-50));
    100 
    101         fHOffset = SkIntToScalar(50);
    102     }
    103 
    104     // overrides from SkEventSink
    105     bool onQuery(SkEvent* evt) override {
    106         if (SampleCode::TitleQ(*evt)) {
    107             SampleCode::TitleR(evt, "Text On Path");
    108             return true;
    109         }
    110         return this->INHERITED::onQuery(evt);
    111     }
    112 
    113     void onDrawContent(SkCanvas* canvas) override {
    114         SkPaint paint;
    115         paint.setAntiAlias(true);
    116         paint.setTextSize(SkIntToScalar(48));
    117 
    118         const char* text = "Hamburgefons";
    119         size_t      len = strlen(text);
    120 
    121         for (int j = 0; j < REPEAT_COUNT; j++) {
    122             SkScalar x = fHOffset;
    123 
    124             paint.setColor(SK_ColorBLACK);
    125             canvas->drawTextOnPathHV(text, len, fPath,
    126                                      x, paint.getTextSize()/2, paint);
    127 
    128             paint.setColor(SK_ColorRED);
    129             canvas->drawTextOnPathHV(text, len, fPath,
    130                                      x + SkIntToScalar(50), 0, paint);
    131 
    132             paint.setColor(SK_ColorBLUE);
    133             canvas->drawTextOnPathHV(text, len, fPath,
    134                          x + SkIntToScalar(100), -paint.getTextSize()/2, paint);
    135         }
    136 
    137         paint.setColor(SK_ColorGREEN);
    138         paint.setStyle(SkPaint::kStroke_Style);
    139         canvas->drawPath(fPath, paint);
    140 
    141         canvas->translate(SkIntToScalar(275), 0);
    142         textStrokePath(canvas);
    143 
    144         canvas->translate(SkIntToScalar(-275), SkIntToScalar(250));
    145         textPathMatrix(canvas);
    146     }
    147 
    148     SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
    149         fHints += 1;
    150         return this->INHERITED::onFindClickHandler(x, y, modi);
    151     }
    152 
    153 private:
    154     int fHints;
    155     typedef SampleView INHERITED;
    156 };
    157 
    158 //////////////////////////////////////////////////////////////////////////////
    159 
    160 static SkView* MyFactory() {
    161     return new TextOnPathView;
    162 }
    163 
    164 static SkViewRegister reg(MyFactory);
    165