michael@0: /* michael@0: * Copyright 2008, The Android Open Source Project michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * * Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * * Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY michael@0: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE michael@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR michael@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR michael@0: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, michael@0: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, michael@0: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR michael@0: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY michael@0: * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: michael@0: // must include config.h first for webkit to fiddle with new/delete michael@0: #include "SkANP.h" michael@0: michael@0: static ANPCanvas* anp_newCanvas(const ANPBitmap* bitmap) { michael@0: SkBitmap bm; michael@0: return new ANPCanvas(*SkANP::SetBitmap(&bm, *bitmap)); michael@0: } michael@0: michael@0: static void anp_deleteCanvas(ANPCanvas* canvas) { michael@0: delete canvas; michael@0: } michael@0: michael@0: static void anp_save(ANPCanvas* canvas) { michael@0: canvas->skcanvas->save(); michael@0: } michael@0: michael@0: static void anp_restore(ANPCanvas* canvas) { michael@0: canvas->skcanvas->restore(); michael@0: } michael@0: michael@0: static void anp_translate(ANPCanvas* canvas, float tx, float ty) { michael@0: canvas->skcanvas->translate(SkFloatToScalar(tx), SkFloatToScalar(ty)); michael@0: } michael@0: michael@0: static void anp_scale(ANPCanvas* canvas, float sx, float sy) { michael@0: canvas->skcanvas->scale(SkFloatToScalar(sx), SkFloatToScalar(sy)); michael@0: } michael@0: michael@0: static void anp_rotate(ANPCanvas* canvas, float degrees) { michael@0: canvas->skcanvas->rotate(SkFloatToScalar(degrees)); michael@0: } michael@0: michael@0: static void anp_skew(ANPCanvas* canvas, float kx, float ky) { michael@0: canvas->skcanvas->skew(SkFloatToScalar(kx), SkFloatToScalar(ky)); michael@0: } michael@0: michael@0: static void anp_clipRect(ANPCanvas* canvas, const ANPRectF* rect) { michael@0: SkRect r; michael@0: canvas->skcanvas->clipRect(*SkANP::SetRect(&r, *rect)); michael@0: } michael@0: michael@0: static void anp_clipPath(ANPCanvas* canvas, const ANPPath* path) { michael@0: canvas->skcanvas->clipPath(*path); michael@0: } michael@0: static void anp_concat(ANPCanvas* canvas, const ANPMatrix* matrix) { michael@0: canvas->skcanvas->concat(*matrix); michael@0: } michael@0: michael@0: static void anp_getTotalMatrix(ANPCanvas* canvas, ANPMatrix* matrix) { michael@0: const SkMatrix& src = canvas->skcanvas->getTotalMatrix(); michael@0: *matrix = *reinterpret_cast(&src); michael@0: } michael@0: michael@0: static bool anp_getLocalClipBounds(ANPCanvas* canvas, ANPRectF* r, michael@0: bool antialias) { michael@0: SkRect bounds; michael@0: if (canvas->skcanvas->getClipBounds(&bounds)) { michael@0: SkANP::SetRect(r, bounds); michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: static bool anp_getDeviceClipBounds(ANPCanvas* canvas, ANPRectI* r) { michael@0: const SkRegion& clip = canvas->skcanvas->getTotalClip(); michael@0: if (!clip.isEmpty()) { michael@0: SkANP::SetRect(r, clip.getBounds()); michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: static void anp_drawColor(ANPCanvas* canvas, ANPColor color) { michael@0: canvas->skcanvas->drawColor(color); michael@0: } michael@0: michael@0: static void anp_drawPaint(ANPCanvas* canvas, const ANPPaint* paint) { michael@0: canvas->skcanvas->drawPaint(*paint); michael@0: } michael@0: michael@0: static void anp_drawLine(ANPCanvas* canvas, float x0, float y0, michael@0: float x1, float y1, const ANPPaint* paint) { michael@0: canvas->skcanvas->drawLine(SkFloatToScalar(x0), SkFloatToScalar(y0), michael@0: SkFloatToScalar(x1), SkFloatToScalar(y1), *paint); michael@0: } michael@0: michael@0: static void anp_drawRect(ANPCanvas* canvas, const ANPRectF* rect, michael@0: const ANPPaint* paint) { michael@0: SkRect r; michael@0: canvas->skcanvas->drawRect(*SkANP::SetRect(&r, *rect), *paint); michael@0: } michael@0: michael@0: static void anp_drawOval(ANPCanvas* canvas, const ANPRectF* rect, michael@0: const ANPPaint* paint) { michael@0: SkRect r; michael@0: canvas->skcanvas->drawOval(*SkANP::SetRect(&r, *rect), *paint); michael@0: } michael@0: michael@0: static void anp_drawPath(ANPCanvas* canvas, const ANPPath* path, michael@0: const ANPPaint* paint) { michael@0: canvas->skcanvas->drawPath(*path, *paint); michael@0: } michael@0: michael@0: static void anp_drawText(ANPCanvas* canvas, const void* text, uint32_t length, michael@0: float x, float y, const ANPPaint* paint) { michael@0: canvas->skcanvas->drawText(text, length, michael@0: SkFloatToScalar(x), SkFloatToScalar(y), michael@0: *paint); michael@0: } michael@0: michael@0: static void anp_drawPosText(ANPCanvas* canvas, const void* text, michael@0: uint32_t byteLength, const float xy[], const ANPPaint* paint) { michael@0: canvas->skcanvas->drawPosText(text, byteLength, michael@0: reinterpret_cast(xy), *paint); michael@0: } michael@0: michael@0: static void anp_drawBitmap(ANPCanvas* canvas, const ANPBitmap* bitmap, michael@0: float x, float y, const ANPPaint* paint) { michael@0: SkBitmap bm; michael@0: canvas->skcanvas->drawBitmap(*SkANP::SetBitmap(&bm, *bitmap), michael@0: SkFloatToScalar(x), SkFloatToScalar(y), michael@0: paint); michael@0: } michael@0: michael@0: static void anp_drawBitmapRect(ANPCanvas* canvas, const ANPBitmap* bitmap, michael@0: const ANPRectI* src, const ANPRectF* dst, michael@0: const ANPPaint* paint) { michael@0: SkBitmap bm; michael@0: SkRect dstR; michael@0: SkIRect srcR, *srcPtr = NULL; michael@0: michael@0: if (src) { michael@0: srcPtr = SkANP::SetRect(&srcR, *src); michael@0: } michael@0: canvas->skcanvas->drawBitmapRect(*SkANP::SetBitmap(&bm, *bitmap), srcPtr, michael@0: *SkANP::SetRect(&dstR, *dst), paint); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #define ASSIGN(obj, name) (obj)->name = anp_##name michael@0: michael@0: void InitCanvasInterface(ANPCanvasInterfaceV0* i) { michael@0: ASSIGN(i, newCanvas); michael@0: ASSIGN(i, deleteCanvas); michael@0: ASSIGN(i, save); michael@0: ASSIGN(i, restore); michael@0: ASSIGN(i, translate); michael@0: ASSIGN(i, scale); michael@0: ASSIGN(i, rotate); michael@0: ASSIGN(i, skew); michael@0: ASSIGN(i, clipRect); michael@0: ASSIGN(i, clipPath); michael@0: ASSIGN(i, concat); michael@0: ASSIGN(i, getTotalMatrix); michael@0: ASSIGN(i, getLocalClipBounds); michael@0: ASSIGN(i, getDeviceClipBounds); michael@0: ASSIGN(i, drawColor); michael@0: ASSIGN(i, drawPaint); michael@0: ASSIGN(i, drawLine); michael@0: ASSIGN(i, drawRect); michael@0: ASSIGN(i, drawOval); michael@0: ASSIGN(i, drawPath); michael@0: ASSIGN(i, drawText); michael@0: ASSIGN(i, drawPosText); michael@0: ASSIGN(i, drawBitmap); michael@0: ASSIGN(i, drawBitmapRect); michael@0: }