1 /*
2  * Copyright 2008, The Android Open Source Project
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *  * Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  *  * Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 // must include config.h first for webkit to fiddle with new/delete
27 #include "SkANP.h"
28 #include "SkRect.h"
29 
anp_newCanvas(const ANPBitmap * bitmap)30 static ANPCanvas* anp_newCanvas(const ANPBitmap* bitmap) {
31     SkBitmap bm;
32     return new ANPCanvas(*SkANP::SetBitmap(&bm, *bitmap));
33 }
34 
anp_deleteCanvas(ANPCanvas * canvas)35 static void anp_deleteCanvas(ANPCanvas* canvas) {
36     delete canvas;
37 }
38 
anp_save(ANPCanvas * canvas)39 static void anp_save(ANPCanvas* canvas) {
40     canvas->skcanvas->save();
41 }
42 
anp_restore(ANPCanvas * canvas)43 static void anp_restore(ANPCanvas* canvas) {
44     canvas->skcanvas->restore();
45 }
46 
anp_translate(ANPCanvas * canvas,float tx,float ty)47 static void anp_translate(ANPCanvas* canvas, float tx, float ty) {
48     canvas->skcanvas->translate(SkFloatToScalar(tx), SkFloatToScalar(ty));
49 }
50 
anp_scale(ANPCanvas * canvas,float sx,float sy)51 static void anp_scale(ANPCanvas* canvas, float sx, float sy) {
52     canvas->skcanvas->scale(SkFloatToScalar(sx), SkFloatToScalar(sy));
53 }
54 
anp_rotate(ANPCanvas * canvas,float degrees)55 static void anp_rotate(ANPCanvas* canvas, float degrees) {
56     canvas->skcanvas->rotate(SkFloatToScalar(degrees));
57 }
58 
anp_skew(ANPCanvas * canvas,float kx,float ky)59 static void anp_skew(ANPCanvas* canvas, float kx, float ky) {
60     canvas->skcanvas->skew(SkFloatToScalar(kx), SkFloatToScalar(ky));
61 }
62 
anp_clipRect(ANPCanvas * canvas,const ANPRectF * rect)63 static void anp_clipRect(ANPCanvas* canvas, const ANPRectF* rect) {
64     SkRect r;
65     canvas->skcanvas->clipRect(*SkANP::SetRect(&r, *rect));
66 }
67 
anp_clipPath(ANPCanvas * canvas,const ANPPath * path)68 static void anp_clipPath(ANPCanvas* canvas, const ANPPath* path) {
69     canvas->skcanvas->clipPath(*path);
70 }
anp_concat(ANPCanvas * canvas,const ANPMatrix * matrix)71 static void anp_concat(ANPCanvas* canvas, const ANPMatrix* matrix) {
72     canvas->skcanvas->concat(*matrix);
73 }
74 
anp_getTotalMatrix(ANPCanvas * canvas,ANPMatrix * matrix)75 static void anp_getTotalMatrix(ANPCanvas* canvas, ANPMatrix* matrix) {
76     const SkMatrix& src = canvas->skcanvas->getTotalMatrix();
77     *matrix = *reinterpret_cast<const ANPMatrix*>(&src);
78 }
79 
anp_getLocalClipBounds(ANPCanvas * canvas,ANPRectF * r,bool antialias)80 static bool anp_getLocalClipBounds(ANPCanvas* canvas, ANPRectF* r,
81                                    bool antialias) {
82     SkRect bounds;
83     if (canvas->skcanvas->getClipBounds(&bounds)) {
84         SkANP::SetRect(r, bounds);
85         return true;
86     }
87     return false;
88 }
89 
anp_getDeviceClipBounds(ANPCanvas * canvas,ANPRectI * r)90 static bool anp_getDeviceClipBounds(ANPCanvas* canvas, ANPRectI* r) {
91     SkIRect bounds;
92     if (canvas->skcanvas->getClipDeviceBounds(&bounds)) {
93         SkANP::SetRect(r, bounds);
94         return true;
95     }
96     return false;
97 }
98 
anp_drawColor(ANPCanvas * canvas,ANPColor color)99 static void anp_drawColor(ANPCanvas* canvas, ANPColor color) {
100     canvas->skcanvas->drawColor(color);
101 }
102 
anp_drawPaint(ANPCanvas * canvas,const ANPPaint * paint)103 static void anp_drawPaint(ANPCanvas* canvas, const ANPPaint* paint) {
104     canvas->skcanvas->drawPaint(*paint);
105 }
106 
anp_drawLine(ANPCanvas * canvas,float x0,float y0,float x1,float y1,const ANPPaint * paint)107 static void anp_drawLine(ANPCanvas* canvas, float x0, float y0,
108                          float x1, float y1, const ANPPaint* paint) {
109     canvas->skcanvas->drawLine(SkFloatToScalar(x0), SkFloatToScalar(y0),
110                            SkFloatToScalar(x1), SkFloatToScalar(y1), *paint);
111 }
112 
anp_drawRect(ANPCanvas * canvas,const ANPRectF * rect,const ANPPaint * paint)113 static void anp_drawRect(ANPCanvas* canvas, const ANPRectF* rect,
114                          const ANPPaint* paint) {
115     SkRect  r;
116     canvas->skcanvas->drawRect(*SkANP::SetRect(&r, *rect), *paint);
117 }
118 
anp_drawOval(ANPCanvas * canvas,const ANPRectF * rect,const ANPPaint * paint)119 static void anp_drawOval(ANPCanvas* canvas, const ANPRectF* rect,
120                          const ANPPaint* paint) {
121     SkRect  r;
122     canvas->skcanvas->drawOval(*SkANP::SetRect(&r, *rect), *paint);
123 }
124 
anp_drawPath(ANPCanvas * canvas,const ANPPath * path,const ANPPaint * paint)125 static void anp_drawPath(ANPCanvas* canvas, const ANPPath* path,
126                          const ANPPaint* paint) {
127     canvas->skcanvas->drawPath(*path, *paint);
128 }
129 
anp_drawText(ANPCanvas * canvas,const void * text,uint32_t length,float x,float y,const ANPPaint * paint)130 static void anp_drawText(ANPCanvas* canvas, const void* text, uint32_t length,
131                          float x, float y, const ANPPaint* paint) {
132     canvas->skcanvas->drawText(text, length,
133                                SkFloatToScalar(x), SkFloatToScalar(y),
134                                *paint);
135 }
136 
anp_drawPosText(ANPCanvas * canvas,const void * text,uint32_t byteLength,const float xy[],const ANPPaint * paint)137 static void anp_drawPosText(ANPCanvas* canvas, const void* text,
138                 uint32_t byteLength, const float xy[], const ANPPaint* paint) {
139     canvas->skcanvas->drawPosText(text, byteLength,
140                                   reinterpret_cast<const SkPoint*>(xy), *paint);
141 }
142 
anp_drawBitmap(ANPCanvas * canvas,const ANPBitmap * bitmap,float x,float y,const ANPPaint * paint)143 static void anp_drawBitmap(ANPCanvas* canvas, const ANPBitmap* bitmap,
144                            float x, float y, const ANPPaint* paint) {
145     SkBitmap    bm;
146     canvas->skcanvas->drawBitmap(*SkANP::SetBitmap(&bm, *bitmap),
147                                  SkFloatToScalar(x), SkFloatToScalar(y),
148                                  paint);
149 }
150 
anp_drawBitmapRect(ANPCanvas * canvas,const ANPBitmap * bitmap,const ANPRectI * src,const ANPRectF * dst,const ANPPaint * paint)151 static void anp_drawBitmapRect(ANPCanvas* canvas, const ANPBitmap* bitmap,
152                               const ANPRectI* src, const ANPRectF* dst,
153                                const ANPPaint* paint) {
154     SkBitmap    bm;
155     SkRect      dstR;
156     SkIRect     srcR;
157 
158     if (src) {
159         canvas->skcanvas->drawBitmapRect(*SkANP::SetBitmap(&bm, *bitmap),
160                                          *SkANP::SetRect(&srcR, *src),
161                                          *SkANP::SetRect(&dstR, *dst),
162                                          paint);
163     } else {
164         canvas->skcanvas->drawBitmapRect(*SkANP::SetBitmap(&bm, *bitmap),
165                                          *SkANP::SetRect(&dstR, *dst),
166                                          paint);
167     }
168 }
169 
170 ///////////////////////////////////////////////////////////////////////////////
171 
172 #define ASSIGN(obj, name)   (obj)->name = anp_##name
173 
InitCanvasInterface(ANPCanvasInterfaceV0 * i)174 void InitCanvasInterface(ANPCanvasInterfaceV0* i) {
175     ASSIGN(i, newCanvas);
176     ASSIGN(i, deleteCanvas);
177     ASSIGN(i, save);
178     ASSIGN(i, restore);
179     ASSIGN(i, translate);
180     ASSIGN(i, scale);
181     ASSIGN(i, rotate);
182     ASSIGN(i, skew);
183     ASSIGN(i, clipRect);
184     ASSIGN(i, clipPath);
185     ASSIGN(i, concat);
186     ASSIGN(i, getTotalMatrix);
187     ASSIGN(i, getLocalClipBounds);
188     ASSIGN(i, getDeviceClipBounds);
189     ASSIGN(i, drawColor);
190     ASSIGN(i, drawPaint);
191     ASSIGN(i, drawLine);
192     ASSIGN(i, drawRect);
193     ASSIGN(i, drawOval);
194     ASSIGN(i, drawPath);
195     ASSIGN(i, drawText);
196     ASSIGN(i, drawPosText);
197     ASSIGN(i, drawBitmap);
198     ASSIGN(i, drawBitmapRect);
199 }
200