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
31static ANPPaint* anp_newPaint() {
32    return new ANPPaint;
33}
34
35static void anp_deletePaint(ANPPaint* paint) {
36    delete paint;
37}
38
39static ANPPaintFlags anp_getFlags(const ANPPaint* paint) {
40    return paint->getFlags();
41}
42
43static void anp_setFlags(ANPPaint* paint, ANPPaintFlags flags) {
44    paint->setFlags(flags);
45}
46
47static ANPColor anp_getColor(const ANPPaint* paint) {
48    return paint->getColor();
49}
50
51static void anp_setColor(ANPPaint* paint, ANPColor color) {
52    paint->setColor(color);
53}
54
55static ANPPaintStyle anp_getStyle(const ANPPaint* paint) {
56    return paint->getStyle();
57}
58
59static void anp_setStyle(ANPPaint* paint, ANPPaintStyle style) {
60    paint->setStyle(static_cast<SkPaint::Style>(style));
61}
62
63static float anp_getStrokeWidth(const ANPPaint* paint) {
64    return SkScalarToFloat(paint->getStrokeWidth());
65}
66
67static float anp_getStrokeMiter(const ANPPaint* paint) {
68    return SkScalarToFloat(paint->getStrokeMiter());
69}
70
71static ANPPaintCap anp_getStrokeCap(const ANPPaint* paint) {
72    return paint->getStrokeCap();
73}
74
75static ANPPaintJoin anp_getStrokeJoin(const ANPPaint* paint) {
76    return paint->getStrokeJoin();
77}
78
79static void anp_setStrokeWidth(ANPPaint* paint, float width) {
80    paint->setStrokeWidth(SkFloatToScalar(width));
81}
82
83static void anp_setStrokeMiter(ANPPaint* paint, float miter) {
84    paint->setStrokeMiter(SkFloatToScalar(miter));
85}
86
87static void anp_setStrokeCap(ANPPaint* paint, ANPPaintCap cap) {
88    paint->setStrokeCap(static_cast<SkPaint::Cap>(cap));
89}
90
91static void anp_setStrokeJoin(ANPPaint* paint, ANPPaintJoin join) {
92    paint->setStrokeJoin(static_cast<SkPaint::Join>(join));
93}
94
95static ANPTextEncoding anp_getTextEncoding(const ANPPaint* paint) {
96    return paint->getTextEncoding();
97}
98
99static ANPPaintAlign anp_getTextAlign(const ANPPaint* paint) {
100    return paint->getTextAlign();
101}
102
103static float anp_getTextSize(const ANPPaint* paint) {
104    return SkScalarToFloat(paint->getTextSize());
105}
106
107static float anp_getTextScaleX(const ANPPaint* paint) {
108    return SkScalarToFloat(paint->getTextScaleX());
109}
110
111static float anp_getTextSkewX(const ANPPaint* paint) {
112    return SkScalarToFloat(paint->getTextSkewX());
113}
114
115static ANPTypeface* anp_getTypeface(const ANPPaint* paint) {
116    return reinterpret_cast<ANPTypeface*>(paint->getTypeface());
117}
118
119static void anp_setTextEncoding(ANPPaint* paint, ANPTextEncoding encoding) {
120    paint->setTextEncoding(static_cast<SkPaint::TextEncoding>(encoding));
121}
122
123static void anp_setTextAlign(ANPPaint* paint, ANPPaintAlign align) {
124    paint->setTextAlign(static_cast<SkPaint::Align>(align));
125}
126
127static void anp_setTextSize(ANPPaint* paint, float textSize) {
128    paint->setTextSize(SkFloatToScalar(textSize));
129}
130
131static void anp_setTextScaleX(ANPPaint* paint, float scaleX) {
132    paint->setTextScaleX(SkFloatToScalar(scaleX));
133}
134
135static void anp_setTextSkewX(ANPPaint* paint, float skewX) {
136    paint->setTextSkewX(SkFloatToScalar(skewX));
137}
138
139static void anp_setTypeface(ANPPaint* paint, ANPTypeface* tf) {
140    paint->setTypeface(tf);
141}
142
143static 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 */
155static 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
161static 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
178void 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