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 "config.h"
28 #include "SkANP.h"
29 #include "SkTypeface.h"
30
anp_newPaint()31 static ANPPaint* anp_newPaint() {
32 return new ANPPaint;
33 }
34
anp_deletePaint(ANPPaint * paint)35 static void anp_deletePaint(ANPPaint* paint) {
36 delete paint;
37 }
38
anp_getFlags(const ANPPaint * paint)39 static ANPPaintFlags anp_getFlags(const ANPPaint* paint) {
40 return paint->getFlags();
41 }
42
anp_setFlags(ANPPaint * paint,ANPPaintFlags flags)43 static void anp_setFlags(ANPPaint* paint, ANPPaintFlags flags) {
44 paint->setFlags(flags);
45 }
46
anp_getColor(const ANPPaint * paint)47 static ANPColor anp_getColor(const ANPPaint* paint) {
48 return paint->getColor();
49 }
50
anp_setColor(ANPPaint * paint,ANPColor color)51 static void anp_setColor(ANPPaint* paint, ANPColor color) {
52 paint->setColor(color);
53 }
54
anp_getStyle(const ANPPaint * paint)55 static ANPPaintStyle anp_getStyle(const ANPPaint* paint) {
56 return paint->getStyle();
57 }
58
anp_setStyle(ANPPaint * paint,ANPPaintStyle style)59 static void anp_setStyle(ANPPaint* paint, ANPPaintStyle style) {
60 paint->setStyle(static_cast<SkPaint::Style>(style));
61 }
62
anp_getStrokeWidth(const ANPPaint * paint)63 static float anp_getStrokeWidth(const ANPPaint* paint) {
64 return SkScalarToFloat(paint->getStrokeWidth());
65 }
66
anp_getStrokeMiter(const ANPPaint * paint)67 static float anp_getStrokeMiter(const ANPPaint* paint) {
68 return SkScalarToFloat(paint->getStrokeMiter());
69 }
70
anp_getStrokeCap(const ANPPaint * paint)71 static ANPPaintCap anp_getStrokeCap(const ANPPaint* paint) {
72 return paint->getStrokeCap();
73 }
74
anp_getStrokeJoin(const ANPPaint * paint)75 static ANPPaintJoin anp_getStrokeJoin(const ANPPaint* paint) {
76 return paint->getStrokeJoin();
77 }
78
anp_setStrokeWidth(ANPPaint * paint,float width)79 static void anp_setStrokeWidth(ANPPaint* paint, float width) {
80 paint->setStrokeWidth(SkFloatToScalar(width));
81 }
82
anp_setStrokeMiter(ANPPaint * paint,float miter)83 static void anp_setStrokeMiter(ANPPaint* paint, float miter) {
84 paint->setStrokeMiter(SkFloatToScalar(miter));
85 }
86
anp_setStrokeCap(ANPPaint * paint,ANPPaintCap cap)87 static void anp_setStrokeCap(ANPPaint* paint, ANPPaintCap cap) {
88 paint->setStrokeCap(static_cast<SkPaint::Cap>(cap));
89 }
90
anp_setStrokeJoin(ANPPaint * paint,ANPPaintJoin join)91 static void anp_setStrokeJoin(ANPPaint* paint, ANPPaintJoin join) {
92 paint->setStrokeJoin(static_cast<SkPaint::Join>(join));
93 }
94
anp_getTextEncoding(const ANPPaint * paint)95 static ANPTextEncoding anp_getTextEncoding(const ANPPaint* paint) {
96 return paint->getTextEncoding();
97 }
98
anp_getTextAlign(const ANPPaint * paint)99 static ANPPaintAlign anp_getTextAlign(const ANPPaint* paint) {
100 return paint->getTextAlign();
101 }
102
anp_getTextSize(const ANPPaint * paint)103 static float anp_getTextSize(const ANPPaint* paint) {
104 return SkScalarToFloat(paint->getTextSize());
105 }
106
anp_getTextScaleX(const ANPPaint * paint)107 static float anp_getTextScaleX(const ANPPaint* paint) {
108 return SkScalarToFloat(paint->getTextScaleX());
109 }
110
anp_getTextSkewX(const ANPPaint * paint)111 static float anp_getTextSkewX(const ANPPaint* paint) {
112 return SkScalarToFloat(paint->getTextSkewX());
113 }
114
anp_getTypeface(const ANPPaint * paint)115 static ANPTypeface* anp_getTypeface(const ANPPaint* paint) {
116 return reinterpret_cast<ANPTypeface*>(paint->getTypeface());
117 }
118
anp_setTextEncoding(ANPPaint * paint,ANPTextEncoding encoding)119 static void anp_setTextEncoding(ANPPaint* paint, ANPTextEncoding encoding) {
120 paint->setTextEncoding(static_cast<SkPaint::TextEncoding>(encoding));
121 }
122
anp_setTextAlign(ANPPaint * paint,ANPPaintAlign align)123 static void anp_setTextAlign(ANPPaint* paint, ANPPaintAlign align) {
124 paint->setTextAlign(static_cast<SkPaint::Align>(align));
125 }
126
anp_setTextSize(ANPPaint * paint,float textSize)127 static void anp_setTextSize(ANPPaint* paint, float textSize) {
128 paint->setTextSize(SkFloatToScalar(textSize));
129 }
130
anp_setTextScaleX(ANPPaint * paint,float scaleX)131 static void anp_setTextScaleX(ANPPaint* paint, float scaleX) {
132 paint->setTextScaleX(SkFloatToScalar(scaleX));
133 }
134
anp_setTextSkewX(ANPPaint * paint,float skewX)135 static void anp_setTextSkewX(ANPPaint* paint, float skewX) {
136 paint->setTextSkewX(SkFloatToScalar(skewX));
137 }
138
anp_setTypeface(ANPPaint * paint,ANPTypeface * tf)139 static void anp_setTypeface(ANPPaint* paint, ANPTypeface* tf) {
140 paint->setTypeface(tf);
141 }
142
anp_measureText(ANPPaint * paint,const void * text,uint32_t byteLength,ANPRectF * bounds)143 static float anp_measureText(ANPPaint* paint, const void* text,
144 uint32_t byteLength, ANPRectF* bounds) {
145 SkScalar w = paint->measureText(text, byteLength,
146 reinterpret_cast<SkRect*>(bounds));
147 return SkScalarToFloat(w);
148 }
149
150 /** Return the number of unichars specifed by the text.
151 If widths is not null, returns the array of advance widths for each
152 unichar.
153 If bounds is not null, returns the array of bounds for each unichar.
154 */
anp_getTextWidths(ANPPaint * paint,const void * text,uint32_t byteLength,float widths[],ANPRectF bounds[])155 static int anp_getTextWidths(ANPPaint* paint, const void* text,
156 uint32_t byteLength, float widths[], ANPRectF bounds[]) {
157 return paint->getTextWidths(text, byteLength, widths,
158 reinterpret_cast<SkRect*>(bounds));
159 }
160
anp_getFontMetrics(ANPPaint * paint,ANPFontMetrics * metrics)161 static float anp_getFontMetrics(ANPPaint* paint, ANPFontMetrics* metrics) {
162 SkPaint::FontMetrics fm;
163 SkScalar spacing = paint->getFontMetrics(&fm);
164 if (metrics) {
165 metrics->fTop = SkScalarToFloat(fm.fTop);
166 metrics->fAscent = SkScalarToFloat(fm.fAscent);
167 metrics->fDescent = SkScalarToFloat(fm.fDescent);
168 metrics->fBottom = SkScalarToFloat(fm.fBottom);
169 metrics->fLeading = SkScalarToFloat(fm.fLeading);
170 }
171 return SkScalarToFloat(spacing);
172 }
173
174 ///////////////////////////////////////////////////////////////////////////////
175
176 #define ASSIGN(obj, name) (obj)->name = anp_##name
177
ANPPaintInterfaceV0_Init(ANPInterface * value)178 void ANPPaintInterfaceV0_Init(ANPInterface* value) {
179 ANPPaintInterfaceV0* i = reinterpret_cast<ANPPaintInterfaceV0*>(value);
180
181 ASSIGN(i, newPaint);
182 ASSIGN(i, deletePaint);
183 ASSIGN(i, getFlags);
184 ASSIGN(i, setFlags);
185 ASSIGN(i, getColor);
186 ASSIGN(i, setColor);
187 ASSIGN(i, getStyle);
188 ASSIGN(i, setStyle);
189 ASSIGN(i, getStrokeWidth);
190 ASSIGN(i, getStrokeMiter);
191 ASSIGN(i, getStrokeCap);
192 ASSIGN(i, getStrokeJoin);
193 ASSIGN(i, setStrokeWidth);
194 ASSIGN(i, setStrokeMiter);
195 ASSIGN(i, setStrokeCap);
196 ASSIGN(i, setStrokeJoin);
197 ASSIGN(i, getTextEncoding);
198 ASSIGN(i, getTextAlign);
199 ASSIGN(i, getTextSize);
200 ASSIGN(i, getTextScaleX);
201 ASSIGN(i, getTextSkewX);
202 ASSIGN(i, getTypeface);
203 ASSIGN(i, setTextEncoding);
204 ASSIGN(i, setTextAlign);
205 ASSIGN(i, setTextSize);
206 ASSIGN(i, setTextScaleX);
207 ASSIGN(i, setTextSkewX);
208 ASSIGN(i, setTypeface);
209 ASSIGN(i, measureText);
210 ASSIGN(i, getTextWidths);
211 ASSIGN(i, getFontMetrics);
212 }
213