180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/*
380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Copyright 2011 Google Inc.
480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *
580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Use of this source code is governed by a BSD-style license that can be
680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * found in the LICENSE file.
780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "gm.h"
980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkCanvas.h"
1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkPaint.h"
1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkRandom.h"
1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querunamespace skiagm {
1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass EmptyPathGM : public GM {
1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querupublic:
1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    EmptyPathGM() {}
1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprotected:
2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkString onShortName() {
2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return SkString("emptypath");
2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkISize onISize() { return make_isize(600, 280); }
2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void drawEmpty(SkCanvas* canvas,
2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    SkColor color,
2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    const SkRect& clip,
2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    SkPaint::Style style,
3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    SkPath::FillType fill) {
3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkPath path;
3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        path.setFillType(fill);
3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkPaint paint;
3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        paint.setColor(color);
3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        paint.setStyle(style);
3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        canvas->save();
3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        canvas->clipRect(clip);
3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        canvas->drawPath(path, paint);
3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        canvas->restore();
4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual void onDraw(SkCanvas* canvas) {
4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        struct FillAndName {
4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            SkPath::FillType fFill;
4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            const char*      fName;
4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        };
4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        static const FillAndName gFills[] = {
4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            {SkPath::kWinding_FillType, "Winding"},
4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            {SkPath::kEvenOdd_FillType, "Even / Odd"},
5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            {SkPath::kInverseWinding_FillType, "Inverse Winding"},
5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            {SkPath::kInverseEvenOdd_FillType, "Inverse Even / Odd"},
5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        };
5380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        struct StyleAndName {
5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            SkPaint::Style fStyle;
5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            const char*    fName;
5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        };
5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        static const StyleAndName gStyles[] = {
5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            {SkPaint::kFill_Style, "Fill"},
5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            {SkPaint::kStroke_Style, "Stroke"},
6080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        };
6280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkPaint titlePaint;
6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        titlePaint.setColor(SK_ColorBLACK);
6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        titlePaint.setAntiAlias(true);
6680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        titlePaint.setLCDRenderText(true);
6780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        titlePaint.setTextSize(15 * SK_Scalar1);
6880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        const char title[] = "Empty Paths Drawn Into Rectangle Clips With "
6980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                             "Indicated Style and Fill";
7080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        canvas->drawText(title, strlen(title),
7180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                            20 * SK_Scalar1,
7280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                            20 * SK_Scalar1,
7380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                            titlePaint);
7480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
750a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger        SkLCGRandom rand;
7680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
7780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        int i = 0;
7880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        canvas->save();
7980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        canvas->translate(10 * SK_Scalar1, 0);
8080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        canvas->save();
8180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
8280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
8380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                if (0 == i % 4) {
8480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    canvas->restore();
8580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    canvas->translate(0, rect.height() + 40 * SK_Scalar1);
8680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    canvas->save();
8780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                } else {
8880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
8980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                }
9080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                ++i;
9180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                SkColor color = rand.nextU();
9480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                color = 0xff000000| color; // force solid
9580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                this->drawEmpty(canvas, color, rect,
9680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                gStyles[style].fStyle, gFills[fill].fFill);
9780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                SkPaint rectPaint;
9980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                rectPaint.setColor(SK_ColorBLACK);
10080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                rectPaint.setStyle(SkPaint::kStroke_Style);
10180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                rectPaint.setStrokeWidth(-1);
10280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                rectPaint.setAntiAlias(true);
10380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                canvas->drawRect(rect, rectPaint);
10480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
10580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                SkPaint labelPaint;
10680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                labelPaint.setColor(color);
10780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                labelPaint.setAntiAlias(true);
10880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                labelPaint.setLCDRenderText(true);
10980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                labelPaint.setTextSize(12 * SK_Scalar1);
11080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                canvas->drawText(gStyles[style].fName,
11180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                 strlen(gStyles[style].fName),
11280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                 0, rect.height() + 15 * SK_Scalar1,
11380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                 labelPaint);
11480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                canvas->drawText(gFills[fill].fName,
11580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                 strlen(gFills[fill].fName),
11680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                 0, rect.height() + 28 * SK_Scalar1,
11780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                 labelPaint);
11880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            }
11980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        }
12080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        canvas->restore();
12180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        canvas->restore();
12280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
12380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
12480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprivate:
12580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    typedef GM INHERITED;
12680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru};
12780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
12880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//////////////////////////////////////////////////////////////////////////////
12980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
13080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic GM* MyFactory(void*) { return new EmptyPathGM; }
13180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic GMRegistry reg(MyFactory);
13280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
13380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
134