/* * 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("