1/*
2 * Copyright (C) 2006 Apple Computer, Inc.  All rights reserved.
3 * Copyright (C) 2012 Rik Cabanier (cabanier@adobe.com)
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "config.h"
28#include "platform/graphics/GraphicsTypes.h"
29
30#include "wtf/Assertions.h"
31#include "wtf/text/WTFString.h"
32
33namespace blink {
34
35static const char* const compositeOperatorNames[] = {
36    "clear",
37    "copy",
38    "source-over",
39    "source-in",
40    "source-out",
41    "source-atop",
42    "destination-over",
43    "destination-in",
44    "destination-out",
45    "destination-atop",
46    "xor",
47    "darker",
48    "lighter"
49};
50
51static const char* const blendOperatorNames[] = {
52    "multiply",
53    "screen",
54    "overlay",
55    "darken",
56    "lighten",
57    "color-dodge",
58    "color-burn",
59    "hard-light",
60    "soft-light",
61    "difference",
62    "exclusion",
63    "hue",
64    "saturation",
65    "color",
66    "luminosity"
67};
68const int numCompositeOperatorNames = WTF_ARRAY_LENGTH(compositeOperatorNames);
69const int numBlendOperatorNames = WTF_ARRAY_LENGTH(blendOperatorNames);
70
71bool parseCompositeAndBlendOperator(const String& s, CompositeOperator& op, WebBlendMode& blendOp)
72{
73    for (int i = 0; i < numCompositeOperatorNames; i++) {
74        if (s == compositeOperatorNames[i]) {
75            op = static_cast<CompositeOperator>(i);
76            blendOp = WebBlendModeNormal;
77            return true;
78        }
79    }
80
81    for (int i = 0; i < numBlendOperatorNames; i++) {
82        if (s == blendOperatorNames[i]) {
83            blendOp = static_cast<WebBlendMode>(i+1);
84            // For now, blending will always assume source-over. This will be fixed in the future
85            op = CompositeSourceOver;
86            return true;
87        }
88    }
89
90    return false;
91}
92
93// FIXME: when we support blend modes in combination with compositing other than source-over
94// this routine needs to be updated.
95String compositeOperatorName(CompositeOperator op, WebBlendMode blendOp)
96{
97    ASSERT(op >= 0);
98    ASSERT(op < numCompositeOperatorNames);
99    ASSERT(blendOp >= 0);
100    ASSERT(blendOp <= numBlendOperatorNames);
101    if (blendOp != WebBlendModeNormal)
102        return blendOperatorNames[blendOp-1];
103    return compositeOperatorNames[op];
104}
105
106bool parseLineCap(const String& s, LineCap& cap)
107{
108    if (s == "butt") {
109        cap = ButtCap;
110        return true;
111    }
112    if (s == "round") {
113        cap = RoundCap;
114        return true;
115    }
116    if (s == "square") {
117        cap = SquareCap;
118        return true;
119    }
120    return false;
121}
122
123String lineCapName(LineCap cap)
124{
125    ASSERT(cap >= 0);
126    ASSERT(cap < 3);
127    const char* const names[3] = { "butt", "round", "square" };
128    return names[cap];
129}
130
131bool parseLineJoin(const String& s, LineJoin& join)
132{
133    if (s == "miter") {
134        join = MiterJoin;
135        return true;
136    }
137    if (s == "round") {
138        join = RoundJoin;
139        return true;
140    }
141    if (s == "bevel") {
142        join = BevelJoin;
143        return true;
144    }
145    return false;
146}
147
148String lineJoinName(LineJoin join)
149{
150    ASSERT(join >= 0);
151    ASSERT(join < 3);
152    const char* const names[3] = { "miter", "round", "bevel" };
153    return names[join];
154}
155
156String textAlignName(TextAlign align)
157{
158    ASSERT(align >= 0);
159    ASSERT(align < 5);
160    const char* const names[5] = { "start", "end", "left", "center", "right" };
161    return names[align];
162}
163
164bool parseTextAlign(const String& s, TextAlign& align)
165{
166    if (s == "start") {
167        align = StartTextAlign;
168        return true;
169    }
170    if (s == "end") {
171        align = EndTextAlign;
172        return true;
173    }
174    if (s == "left") {
175        align = LeftTextAlign;
176        return true;
177    }
178    if (s == "center") {
179        align = CenterTextAlign;
180        return true;
181    }
182    if (s == "right") {
183        align = RightTextAlign;
184        return true;
185    }
186    return false;
187}
188
189String textBaselineName(TextBaseline baseline)
190{
191    ASSERT(baseline >= 0);
192    ASSERT(baseline < 6);
193    const char* const names[6] = { "alphabetic", "top", "middle", "bottom", "ideographic", "hanging" };
194    return names[baseline];
195}
196
197bool parseTextBaseline(const String& s, TextBaseline& baseline)
198{
199    if (s == "alphabetic") {
200        baseline = AlphabeticTextBaseline;
201        return true;
202    }
203    if (s == "top") {
204        baseline = TopTextBaseline;
205        return true;
206    }
207    if (s == "middle") {
208        baseline = MiddleTextBaseline;
209        return true;
210    }
211    if (s == "bottom") {
212        baseline = BottomTextBaseline;
213        return true;
214    }
215    if (s == "ideographic") {
216        baseline = IdeographicTextBaseline;
217        return true;
218    }
219    if (s == "hanging") {
220        baseline = HangingTextBaseline;
221        return true;
222    }
223    return false;
224}
225
226} // namespace blink
227