aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/SimpleCocoaApp/SimpleApp.mm
blob: 95684c1683cb252ab28220226b05e32a75dbc2be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 * Copyright 2011 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#include "SkCanvas.h"
#include "SkCGUtils.h"
#include "SkGraphics.h"
#include "SkImageDecoder.h"
#include "SkOSFile.h"
#include "SkPaint.h"
#include "SkPicture.h"
#include "SkStream.h"
#include "SkWindow.h"

static void make_filepath(SkString* path, const char* dir, const SkString& name) {
    size_t len = strlen(dir);
    path->set(dir);
    if (len > 0 && dir[len - 1] != '/') {
        path->append("/");
    }
    path->append(name);
}

static SkPicture* LoadPicture(const char path[]) {
    SkPicture* pic = NULL;

    SkBitmap bm;
    if (SkImageDecoder::DecodeFile(path, &bm)) {
        bm.setImmutable();
        pic = new SkPicture;
        SkCanvas* can = pic->beginRecording(bm.width(), bm.height());
        can->drawBitmap(bm, 0, 0, NULL);
        pic->endRecording();
    } else {
        SkFILEStream stream(path);
        if (stream.isValid()) {
            pic = new SkPicture(&stream, NULL, &SkImageDecoder::DecodeStream);
        }

        if (false) { // re-record
            SkPicture p2;
            pic->draw(p2.beginRecording(pic->width(), pic->height()));
            p2.endRecording();

            SkString path2(path);
            path2.append(".new.skp");
            SkFILEWStream writer(path2.c_str());
            p2.serialize(&writer);
        }
    }
    return pic;
}

class SkSampleView : public SkView {
public:
    SkSampleView() {
        this->setVisibleP(true);
        this->setClipToBounds(false);
    };
protected:
    virtual void onDraw(SkCanvas* canvas) {
        canvas->drawColor(0xFFFFFFFF);
        SkPaint p;
        p.setTextSize(20);
        p.setAntiAlias(true);
        canvas->drawText("Hello World!", 13, 50, 30, p);
     //   SkRect r = {50, 50, 80, 80};
        p.setColor(0xAA11EEAA);
   //     canvas->drawRect(r, p);

        SkRect result;
        SkPath path;
        path.moveTo(0, 0);
        path.lineTo(1, 1);
        path.lineTo(1, 8);
        path.lineTo(0, 9);
        SkASSERT(path.hasRectangularInterior(&result));

        path.reset();
        path.addRect(10, 10, 100, 100, SkPath::kCW_Direction);
        path.addRect(20, 20, 50, 50, SkPath::kCW_Direction);
        path.addRect(50, 50, 90, 90, SkPath::kCCW_Direction);
        p.setColor(0xAA335577);
        canvas->drawPath(path, p);
        SkASSERT(!path.hasRectangularInterior(NULL));
        path.reset();
        path.addRect(10, 10, 100, 100, SkPath::kCW_Direction);
        path.addRect(20, 20, 80, 80, SkPath::kCW_Direction);
        SkRect expected = {20, 20, 80, 80};
        SkASSERT(path.hasRectangularInterior(&result));
        SkASSERT(result == expected);

    }
private:
    typedef SkView INHERITED;
};

void application_init();
void application_term();

static int showPathContour(SkPath::Iter& iter) {
    uint8_t verb;
    SkPoint pts[4];
    int moves = 0;
    bool waitForClose = false;
    while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
        switch (verb) {
            case SkPath::kMove_Verb:
                if (!waitForClose) {
                    ++moves;
                    waitForClose = true;
                }
                SkDebugf("path.moveTo(%1.9g, %1.9g);\n", pts[0].fX, pts[0].fY);
                break;
            case SkPath::kLine_Verb:
                SkDebugf("path.lineTo(%1.9g, %1.9g);\n", pts[1].fX, pts[1].fY);
                break;
            case SkPath::kQuad_Verb:
                SkDebugf("path.quadTo(%1.9g, %1.9g, %1.9g, %1.9g);\n",
                    pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY);
                break;
            case SkPath::kCubic_Verb:
                SkDebugf("path.cubicTo(%1.9g, %1.9g, %1.9g, %1.9g, %1.9g, %1.9g);\n",
                    pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY,
                    pts[3].fX, pts[3].fY);
                break;
            case SkPath::kClose_Verb:
                waitForClose = false;
                SkDebugf("path.close();\n");
                break;
            default:
                SkDEBUGFAIL("bad verb");
                SkASSERT(0);
                return 0;
        }
    }
    return moves;
}

class PathCanvas : public SkCanvas {
    virtual void drawPath(const SkPath& path, const SkPaint& paint) {
        if (nameonly) {
            SkDebugf("    %s%d,\n", filename.c_str(), ++count);
            return;
        }
        SkPath::Iter iter(path, true);
        SkDebugf("<div id=\"%s%d\">\n", filename.c_str(), ++count);
        SkASSERT(path.getFillType() < SkPath::kInverseWinding_FillType);
        SkDebugf("path.setFillType(SkPath::k%s_FillType);\n",
            path.getFillType() == SkPath::kWinding_FillType ? "Winding" : "EvenOdd");
        int contours = showPathContour(iter);
        SkRect r;
        SkRect copy = r;
        bool hasOne = path.hasRectangularInterior(&r);
        bool expected = (path.getFillType() == SkPath::kWinding_FillType && contours == 1)
            || (path.getFillType() == SkPath::kEvenOdd_FillType && contours == 2);
        if (!expected) {
            SkDebugf("suspect contours=%d\n", contours);
        }
        int verbs = path.countVerbs();
        int points = path.countPoints();
        if (hasOne) {
            if (rectVerbsMin > verbs) {
                rectVerbsMin = verbs;
            }
            if (rectVerbsMax < verbs) {
                rectVerbsMax = verbs;
            }
            if (rectPointsMin > points) {
                rectPointsMin = points;
            }
            if (rectPointsMax < points) {
                rectPointsMax = points;
            }
            SkDebugf("path.addRect(%1.9g, %1.9g, %1.9g, %1.9g);\n",
                    r.fLeft, r.fTop, r.fRight, r.fBottom);
        } else {
            if (verbsMin > verbs) {
                verbsMin = verbs;
            }
            if (verbsMax < verbs) {
                verbsMax = verbs;
            }
            if (pointsMin > points) {
                pointsMin = points;
            }
            if (pointsMax < points) {
                pointsMax = points;
            }
            SkDebugf("no interior bounds\n");
        }
        path.hasRectangularInterior(&copy);
        SkDebugf("</div>\n\n");
    }

    virtual void drawPosTextH(const void* text, size_t byteLength,
                              const SkScalar xpos[], SkScalar constY,
                              const SkPaint& paint) {
    }

public:
    void divName(const SkString& str, bool only) {
        filename = str;
        char* chars = filename.writable_str();
        while (*chars) {
            if (*chars == '.' || *chars == '-') *chars = '_';
            chars++;
        }
        count = 0;
        nameonly = only;
    }

    void init() {
        pointsMin = verbsMin = SK_MaxS32;
        pointsMax = verbsMax = SK_MinS32;
        rectPointsMin = rectVerbsMin = SK_MaxS32;
        rectPointsMax = rectVerbsMax = SK_MinS32;
    }

    SkString filename;
    int count;
    bool nameonly;
    int pointsMin;
    int pointsMax;
    int verbsMin;
    int verbsMax;
    int rectPointsMin;
    int rectPointsMax;
    int rectVerbsMin;
    int rectVerbsMax;
};

bool runone = false;

void application_init() {
    SkGraphics::Init();
    SkEvent::Init();
    if (runone) {
        return;
    }
    const char pictDir[] = "/Volumes/chrome/nih/skia/skp/skp";
    SkOSFile::Iter iter(pictDir, "skp");
    SkString filename;
    PathCanvas canvas;
    canvas.init();
    while (iter.next(&filename)) {
        SkString path;
     //   if (true) filename.set("tabl_www_sahadan_com.skp");
        make_filepath(&path, pictDir, filename);
        canvas.divName(filename, false);
        SkPicture* pic = LoadPicture(path.c_str());
        pic->draw(&canvas);
        delete pic;
    }
    SkDebugf("\n</div>\n\n");

    SkDebugf("<script type=\"text/javascript\">\n\n");
    SkDebugf("var testDivs = [\n");

    iter.reset(pictDir, "skp");
    while (iter.next(&filename)) {
        SkString path;
        make_filepath(&path, pictDir, filename);
        canvas.divName(filename, true);
        SkPicture* pic = LoadPicture(path.c_str());
        pic->draw(&canvas);
        delete pic;
    }
    SkDebugf("];\n\n");

    SkDebugf("points min=%d max=%d verbs min=%d max=%d\n", canvas.pointsMin, canvas.pointsMax,
            canvas.verbsMin, canvas.verbsMax);
    SkDebugf("rect points min=%d max=%d verbs min=%d max=%d\n", canvas.rectPointsMin, canvas.rectPointsMax,
            canvas.rectVerbsMin, canvas.rectVerbsMax);

    SkDebugf("\n");
}

void application_term() {
    SkEvent::Term();
}

class FillLayout : public SkView::Layout {
protected:
    virtual void onLayoutChildren(SkView* parent) {
        SkView* view = SkView::F2BIter(parent).next();
        view->setSize(parent->width(), parent->height());
    }
};

#import "SimpleApp.h"
@implementation SimpleNSView

- (id)initWithDefaults {
    if ((self = [super initWithDefaults])) {
        fWind = new SkOSWindow(self);
        fWind->setLayout(new FillLayout, false);
        fWind->attachChildToFront(new SkSampleView)->unref();
    }
    return self;
}

- (void)drawRect:(NSRect)dirtyRect {
    CGContextRef ctx = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
    SkCGDrawBitmap(ctx, fWind->getBitmap(), 0, 0);
}

@end