|
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 |
|
29 static ANPCanvas* anp_newCanvas(const ANPBitmap* bitmap) { |
|
30 SkBitmap bm; |
|
31 return new ANPCanvas(*SkANP::SetBitmap(&bm, *bitmap)); |
|
32 } |
|
33 |
|
34 static void anp_deleteCanvas(ANPCanvas* canvas) { |
|
35 delete canvas; |
|
36 } |
|
37 |
|
38 static void anp_save(ANPCanvas* canvas) { |
|
39 canvas->skcanvas->save(); |
|
40 } |
|
41 |
|
42 static void anp_restore(ANPCanvas* canvas) { |
|
43 canvas->skcanvas->restore(); |
|
44 } |
|
45 |
|
46 static void anp_translate(ANPCanvas* canvas, float tx, float ty) { |
|
47 canvas->skcanvas->translate(SkFloatToScalar(tx), SkFloatToScalar(ty)); |
|
48 } |
|
49 |
|
50 static void anp_scale(ANPCanvas* canvas, float sx, float sy) { |
|
51 canvas->skcanvas->scale(SkFloatToScalar(sx), SkFloatToScalar(sy)); |
|
52 } |
|
53 |
|
54 static void anp_rotate(ANPCanvas* canvas, float degrees) { |
|
55 canvas->skcanvas->rotate(SkFloatToScalar(degrees)); |
|
56 } |
|
57 |
|
58 static void anp_skew(ANPCanvas* canvas, float kx, float ky) { |
|
59 canvas->skcanvas->skew(SkFloatToScalar(kx), SkFloatToScalar(ky)); |
|
60 } |
|
61 |
|
62 static void anp_clipRect(ANPCanvas* canvas, const ANPRectF* rect) { |
|
63 SkRect r; |
|
64 canvas->skcanvas->clipRect(*SkANP::SetRect(&r, *rect)); |
|
65 } |
|
66 |
|
67 static void anp_clipPath(ANPCanvas* canvas, const ANPPath* path) { |
|
68 canvas->skcanvas->clipPath(*path); |
|
69 } |
|
70 static void anp_concat(ANPCanvas* canvas, const ANPMatrix* matrix) { |
|
71 canvas->skcanvas->concat(*matrix); |
|
72 } |
|
73 |
|
74 static void anp_getTotalMatrix(ANPCanvas* canvas, ANPMatrix* matrix) { |
|
75 const SkMatrix& src = canvas->skcanvas->getTotalMatrix(); |
|
76 *matrix = *reinterpret_cast<const ANPMatrix*>(&src); |
|
77 } |
|
78 |
|
79 static bool anp_getLocalClipBounds(ANPCanvas* canvas, ANPRectF* r, |
|
80 bool antialias) { |
|
81 SkRect bounds; |
|
82 if (canvas->skcanvas->getClipBounds(&bounds)) { |
|
83 SkANP::SetRect(r, bounds); |
|
84 return true; |
|
85 } |
|
86 return false; |
|
87 } |
|
88 |
|
89 static bool anp_getDeviceClipBounds(ANPCanvas* canvas, ANPRectI* r) { |
|
90 const SkRegion& clip = canvas->skcanvas->getTotalClip(); |
|
91 if (!clip.isEmpty()) { |
|
92 SkANP::SetRect(r, clip.getBounds()); |
|
93 return true; |
|
94 } |
|
95 return false; |
|
96 } |
|
97 |
|
98 static void anp_drawColor(ANPCanvas* canvas, ANPColor color) { |
|
99 canvas->skcanvas->drawColor(color); |
|
100 } |
|
101 |
|
102 static void anp_drawPaint(ANPCanvas* canvas, const ANPPaint* paint) { |
|
103 canvas->skcanvas->drawPaint(*paint); |
|
104 } |
|
105 |
|
106 static void anp_drawLine(ANPCanvas* canvas, float x0, float y0, |
|
107 float x1, float y1, const ANPPaint* paint) { |
|
108 canvas->skcanvas->drawLine(SkFloatToScalar(x0), SkFloatToScalar(y0), |
|
109 SkFloatToScalar(x1), SkFloatToScalar(y1), *paint); |
|
110 } |
|
111 |
|
112 static void anp_drawRect(ANPCanvas* canvas, const ANPRectF* rect, |
|
113 const ANPPaint* paint) { |
|
114 SkRect r; |
|
115 canvas->skcanvas->drawRect(*SkANP::SetRect(&r, *rect), *paint); |
|
116 } |
|
117 |
|
118 static void anp_drawOval(ANPCanvas* canvas, const ANPRectF* rect, |
|
119 const ANPPaint* paint) { |
|
120 SkRect r; |
|
121 canvas->skcanvas->drawOval(*SkANP::SetRect(&r, *rect), *paint); |
|
122 } |
|
123 |
|
124 static void anp_drawPath(ANPCanvas* canvas, const ANPPath* path, |
|
125 const ANPPaint* paint) { |
|
126 canvas->skcanvas->drawPath(*path, *paint); |
|
127 } |
|
128 |
|
129 static void anp_drawText(ANPCanvas* canvas, const void* text, uint32_t length, |
|
130 float x, float y, const ANPPaint* paint) { |
|
131 canvas->skcanvas->drawText(text, length, |
|
132 SkFloatToScalar(x), SkFloatToScalar(y), |
|
133 *paint); |
|
134 } |
|
135 |
|
136 static void anp_drawPosText(ANPCanvas* canvas, const void* text, |
|
137 uint32_t byteLength, const float xy[], const ANPPaint* paint) { |
|
138 canvas->skcanvas->drawPosText(text, byteLength, |
|
139 reinterpret_cast<const SkPoint*>(xy), *paint); |
|
140 } |
|
141 |
|
142 static void anp_drawBitmap(ANPCanvas* canvas, const ANPBitmap* bitmap, |
|
143 float x, float y, const ANPPaint* paint) { |
|
144 SkBitmap bm; |
|
145 canvas->skcanvas->drawBitmap(*SkANP::SetBitmap(&bm, *bitmap), |
|
146 SkFloatToScalar(x), SkFloatToScalar(y), |
|
147 paint); |
|
148 } |
|
149 |
|
150 static void anp_drawBitmapRect(ANPCanvas* canvas, const ANPBitmap* bitmap, |
|
151 const ANPRectI* src, const ANPRectF* dst, |
|
152 const ANPPaint* paint) { |
|
153 SkBitmap bm; |
|
154 SkRect dstR; |
|
155 SkIRect srcR, *srcPtr = NULL; |
|
156 |
|
157 if (src) { |
|
158 srcPtr = SkANP::SetRect(&srcR, *src); |
|
159 } |
|
160 canvas->skcanvas->drawBitmapRect(*SkANP::SetBitmap(&bm, *bitmap), srcPtr, |
|
161 *SkANP::SetRect(&dstR, *dst), paint); |
|
162 } |
|
163 |
|
164 /////////////////////////////////////////////////////////////////////////////// |
|
165 |
|
166 #define ASSIGN(obj, name) (obj)->name = anp_##name |
|
167 |
|
168 void InitCanvasInterface(ANPCanvasInterfaceV0* i) { |
|
169 ASSIGN(i, newCanvas); |
|
170 ASSIGN(i, deleteCanvas); |
|
171 ASSIGN(i, save); |
|
172 ASSIGN(i, restore); |
|
173 ASSIGN(i, translate); |
|
174 ASSIGN(i, scale); |
|
175 ASSIGN(i, rotate); |
|
176 ASSIGN(i, skew); |
|
177 ASSIGN(i, clipRect); |
|
178 ASSIGN(i, clipPath); |
|
179 ASSIGN(i, concat); |
|
180 ASSIGN(i, getTotalMatrix); |
|
181 ASSIGN(i, getLocalClipBounds); |
|
182 ASSIGN(i, getDeviceClipBounds); |
|
183 ASSIGN(i, drawColor); |
|
184 ASSIGN(i, drawPaint); |
|
185 ASSIGN(i, drawLine); |
|
186 ASSIGN(i, drawRect); |
|
187 ASSIGN(i, drawOval); |
|
188 ASSIGN(i, drawPath); |
|
189 ASSIGN(i, drawText); |
|
190 ASSIGN(i, drawPosText); |
|
191 ASSIGN(i, drawBitmap); |
|
192 ASSIGN(i, drawBitmapRect); |
|
193 } |