Home | History | Annotate | Download | only in gm
      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 "gm.h"
      8 #include "SkCanvas.h"
      9 #include "SkGradientShader.h"
     10 #include "SkPath.h"
     11 
     12 static void makebm(SkBitmap* bm, int w, int h) {
     13     bm->allocN32Pixels(w, h);
     14     bm->eraseColor(SK_ColorTRANSPARENT);
     15 
     16     SkCanvas    canvas(*bm);
     17     SkScalar    s = SkIntToScalar(SkMin32(w, h));
     18     static const SkPoint     kPts0[] = { { 0, 0 }, { s, s } };
     19     static const SkPoint     kPts1[] = { { s, 0 }, { 0, s } };
     20     static const SkScalar    kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
     21     static const SkColor kColors0[] = {0x40FF00FF, 0xF0FFFF00, 0x4000FFFF };
     22     static const SkColor kColors1[] = {0xF0FF00FF, 0x80FFFF00, 0xF000FFFF };
     23 
     24 
     25     SkPaint     paint;
     26 
     27     paint.setShader(SkGradientShader::CreateLinear(kPts0, kColors0, kPos,
     28                     SK_ARRAY_COUNT(kColors0), SkShader::kClamp_TileMode))->unref();
     29     canvas.drawPaint(paint);
     30     paint.setShader(SkGradientShader::CreateLinear(kPts1, kColors1, kPos,
     31                     SK_ARRAY_COUNT(kColors1), SkShader::kClamp_TileMode))->unref();
     32     canvas.drawPaint(paint);
     33 }
     34 
     35 ///////////////////////////////////////////////////////////////////////////////
     36 
     37 struct LabeledMatrix {
     38     SkMatrix    fMatrix;
     39     const char* fLabel;
     40 };
     41 
     42 DEF_SIMPLE_GM_BG(shadertext2, canvas, 1800, 900,
     43                  sk_tool_utils::color_to_565(0xFFDDDDDD)) {
     44         static const char kText[] = "SKIA";
     45         static const int kTextLen = SK_ARRAY_COUNT(kText) - 1;
     46         static const int kPointSize = 55;
     47 
     48         SkTDArray<LabeledMatrix> matrices;
     49         matrices.append()->fMatrix.reset();
     50         matrices.top().fLabel = "Identity";
     51         matrices.append()->fMatrix.setScale(1.2f, 0.8f);
     52         matrices.top().fLabel = "Scale";
     53         matrices.append()->fMatrix.setRotate(10.f);
     54         matrices.top().fLabel = "Rotate";
     55         matrices.append()->fMatrix.reset();
     56         matrices.top().fMatrix.setPerspX(-0.0015f);
     57         matrices.top().fMatrix.setPerspY(+0.0015f);
     58         matrices.top().fLabel = "Persp";
     59 
     60         SkTDArray<LabeledMatrix> localMatrices;
     61         localMatrices.append()->fMatrix.reset();
     62         localMatrices.top().fLabel = "Identity";
     63         localMatrices.append()->fMatrix.setScale(2.5f, 0.2f);
     64         localMatrices.top().fLabel = "Scale";
     65         localMatrices.append()->fMatrix.setRotate(45.f);
     66         localMatrices.top().fLabel = "Rotate";
     67         localMatrices.append()->fMatrix.reset();
     68         localMatrices.top().fMatrix.setPerspX(-0.007f);
     69         localMatrices.top().fMatrix.setPerspY(+0.008f);
     70         localMatrices.top().fLabel = "Persp";
     71 
     72         static SkBitmap bmp;
     73         if (bmp.isNull()) {
     74             makebm(&bmp, kPointSize / 2, kPointSize / 2);
     75         }
     76 
     77         SkPaint fillPaint;
     78         fillPaint.setAntiAlias(true);
     79         sk_tool_utils::set_portable_typeface(&fillPaint);
     80         fillPaint.setTextSize(SkIntToScalar(kPointSize));
     81         fillPaint.setFilterQuality(kLow_SkFilterQuality);
     82 
     83         SkPaint outlinePaint;
     84         outlinePaint.setAntiAlias(true);
     85         sk_tool_utils::set_portable_typeface(&outlinePaint);
     86         outlinePaint.setTextSize(SkIntToScalar(kPointSize));
     87         outlinePaint.setStyle(SkPaint::kStroke_Style);
     88         outlinePaint.setStrokeWidth(0.f);
     89 
     90         SkScalar w = fillPaint.measureText(kText, kTextLen);
     91         static SkScalar kPadY = 0.5f * kPointSize;
     92         static SkScalar kPadX = 1.5f * kPointSize;
     93 
     94         SkPaint strokePaint(fillPaint);
     95         strokePaint.setStyle(SkPaint::kStroke_Style);
     96         strokePaint.setStrokeWidth(kPointSize * 0.1f);
     97 
     98         SkPaint labelPaint;
     99         labelPaint.setColor(0xff000000);
    100         labelPaint.setAntiAlias(true);
    101         sk_tool_utils::set_portable_typeface(&labelPaint);
    102         labelPaint.setTextSize(12.f);
    103 
    104         canvas->translate(15.f, 15.f);
    105         canvas->drawBitmap(bmp, 0, 0);
    106         canvas->translate(0, bmp.height() + labelPaint.getTextSize() + 15.f);
    107 
    108         static const char kLabelLabel[] = "localM / canvasM";
    109         canvas->drawText(kLabelLabel, strlen(kLabelLabel), 0, 0, labelPaint);
    110         canvas->translate(0, 15.f);
    111 
    112         canvas->save();
    113         SkScalar maxLabelW = 0;
    114         canvas->translate(0, kPadY / 2 + kPointSize);
    115         for (int lm = 0; lm < localMatrices.count(); ++lm) {
    116             canvas->drawText(matrices[lm].fLabel, strlen(matrices[lm].fLabel),
    117                              0, labelPaint.getTextSize() - 1, labelPaint);
    118             SkScalar labelW = labelPaint.measureText(matrices[lm].fLabel,
    119                                                      strlen(matrices[lm].fLabel));
    120             maxLabelW = SkMaxScalar(maxLabelW, labelW);
    121             canvas->translate(0.f, 2 * kPointSize + 2.5f * kPadY);
    122         }
    123         canvas->restore();
    124 
    125         canvas->translate(maxLabelW + kPadX / 2.f, 0.f);
    126 
    127         for (int s = 0; s < 2; ++s) {
    128             SkPaint& paint = s ? strokePaint : fillPaint;
    129 
    130             SkScalar columnH = 0;
    131             for (int m = 0; m < matrices.count(); ++m) {
    132                 columnH = 0;
    133                 canvas->save();
    134                 canvas->drawText(matrices[m].fLabel, strlen(matrices[m].fLabel),
    135                                  0, labelPaint.getTextSize() - 1, labelPaint);
    136                 canvas->translate(0, kPadY / 2 + kPointSize);
    137                 columnH += kPadY / 2 + kPointSize;
    138                 for (int lm = 0; lm < localMatrices.count(); ++lm) {
    139                     paint.setShader(
    140                             SkShader::CreateBitmapShader(bmp,
    141                                                          SkShader::kMirror_TileMode,
    142                                                          SkShader::kRepeat_TileMode,
    143                                                          &localMatrices[lm].fMatrix))->unref();
    144 
    145                     canvas->save();
    146                         canvas->concat(matrices[m].fMatrix);
    147                         canvas->drawText(kText, kTextLen, 0, 0, paint);
    148                         canvas->drawText(kText, kTextLen, 0, 0, outlinePaint);
    149                     canvas->restore();
    150 
    151                     SkPath path;
    152                     path.arcTo(SkRect::MakeXYWH(-0.1f * w, 0.f,
    153                                                 1.2f * w, 2.f * kPointSize),
    154                                                 225.f, 359.f,
    155                                                 false);
    156                     path.close();
    157 
    158                     canvas->translate(0.f, kPointSize + kPadY);
    159                     columnH += kPointSize + kPadY;
    160 
    161                     canvas->save();
    162                         canvas->concat(matrices[m].fMatrix);
    163                         canvas->drawTextOnPath(kText, kTextLen, path, nullptr, paint);
    164                         canvas->drawTextOnPath(kText, kTextLen, path, nullptr, outlinePaint);
    165                     canvas->restore();
    166                     SkPaint stroke;
    167                     stroke.setStyle(SkPaint::kStroke_Style);
    168                     canvas->translate(0.f, kPointSize + kPadY);
    169                     columnH += kPointSize + kPadY;
    170                 }
    171                 canvas->restore();
    172                 canvas->translate(w + kPadX, 0.f);
    173             }
    174             if (0 == s) {
    175                 canvas->drawLine(0.f, -kPadY, 0.f, columnH + kPadY, outlinePaint);
    176                 canvas->translate(kPadX / 2, 0.f);
    177                 static const char kFillLabel[] = "Filled";
    178                 static const char kStrokeLabel[] = "Stroked";
    179                 SkScalar y = columnH + kPadY / 2;
    180                 SkScalar fillX = -outlinePaint.measureText(kFillLabel, strlen(kFillLabel)) - kPadX;
    181                 SkScalar strokeX = kPadX;
    182                 canvas->drawText(kFillLabel, strlen(kFillLabel), fillX, y, labelPaint);
    183                 canvas->drawText(kStrokeLabel, strlen(kStrokeLabel), strokeX, y, labelPaint);
    184             }
    185         }
    186 }
    187