Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / graphics / GraphicsTypes.cpp
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
33 namespace blink {
34
35 static 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
51 static const char* const blendOperatorNames[] = {
52     "normal",
53     "multiply",
54     "screen",
55     "overlay",
56     "darken",
57     "lighten",
58     "color-dodge",
59     "color-burn",
60     "hard-light",
61     "soft-light",
62     "difference",
63     "exclusion",
64     "hue",
65     "saturation",
66     "color",
67     "luminosity"
68 };
69 const int numCompositeOperatorNames = WTF_ARRAY_LENGTH(compositeOperatorNames);
70 const int numBlendOperatorNames = WTF_ARRAY_LENGTH(blendOperatorNames);
71
72 bool parseCompositeAndBlendOperator(const String& s, CompositeOperator& op, WebBlendMode& blendOp)
73 {
74     for (int i = 0; i < numCompositeOperatorNames; i++) {
75         if (s == compositeOperatorNames[i]) {
76             op = static_cast<CompositeOperator>(i);
77             blendOp = WebBlendModeNormal;
78             return true;
79         }
80     }
81
82     for (int i = 0; i < numBlendOperatorNames; i++) {
83         if (s == blendOperatorNames[i]) {
84             blendOp = static_cast<WebBlendMode>(i);
85             op = CompositeSourceOver;
86             return true;
87         }
88     }
89
90     return false;
91 }
92
93 String compositeOperatorName(CompositeOperator op, WebBlendMode blendOp)
94 {
95     ASSERT(op >= 0);
96     ASSERT(op < numCompositeOperatorNames);
97     ASSERT(blendOp >= 0);
98     if (blendOp != WebBlendModeNormal)
99         return blendOperatorNames[blendOp];
100     return compositeOperatorNames[op];
101 }
102
103 bool parseLineCap(const String& s, LineCap& cap)
104 {
105     if (s == "butt") {
106         cap = ButtCap;
107         return true;
108     }
109     if (s == "round") {
110         cap = RoundCap;
111         return true;
112     }
113     if (s == "square") {
114         cap = SquareCap;
115         return true;
116     }
117     return false;
118 }
119
120 String lineCapName(LineCap cap)
121 {
122     ASSERT(cap >= 0);
123     ASSERT(cap < 3);
124     const char* const names[3] = { "butt", "round", "square" };
125     return names[cap];
126 }
127
128 bool parseLineJoin(const String& s, LineJoin& join)
129 {
130     if (s == "miter") {
131         join = MiterJoin;
132         return true;
133     }
134     if (s == "round") {
135         join = RoundJoin;
136         return true;
137     }
138     if (s == "bevel") {
139         join = BevelJoin;
140         return true;
141     }
142     return false;
143 }
144
145 String lineJoinName(LineJoin join)
146 {
147     ASSERT(join >= 0);
148     ASSERT(join < 3);
149     const char* const names[3] = { "miter", "round", "bevel" };
150     return names[join];
151 }
152
153 String textAlignName(TextAlign align)
154 {
155     ASSERT(align >= 0);
156     ASSERT(align < 5);
157     const char* const names[5] = { "start", "end", "left", "center", "right" };
158     return names[align];
159 }
160
161 bool parseTextAlign(const String& s, TextAlign& align)
162 {
163     if (s == "start") {
164         align = StartTextAlign;
165         return true;
166     }
167     if (s == "end") {
168         align = EndTextAlign;
169         return true;
170     }
171     if (s == "left") {
172         align = LeftTextAlign;
173         return true;
174     }
175     if (s == "center") {
176         align = CenterTextAlign;
177         return true;
178     }
179     if (s == "right") {
180         align = RightTextAlign;
181         return true;
182     }
183     return false;
184 }
185
186 String textBaselineName(TextBaseline baseline)
187 {
188     ASSERT(baseline >= 0);
189     ASSERT(baseline < 6);
190     const char* const names[6] = { "alphabetic", "top", "middle", "bottom", "ideographic", "hanging" };
191     return names[baseline];
192 }
193
194 bool parseTextBaseline(const String& s, TextBaseline& baseline)
195 {
196     if (s == "alphabetic") {
197         baseline = AlphabeticTextBaseline;
198         return true;
199     }
200     if (s == "top") {
201         baseline = TopTextBaseline;
202         return true;
203     }
204     if (s == "middle") {
205         baseline = MiddleTextBaseline;
206         return true;
207     }
208     if (s == "bottom") {
209         baseline = BottomTextBaseline;
210         return true;
211     }
212     if (s == "ideographic") {
213         baseline = IdeographicTextBaseline;
214         return true;
215     }
216     if (s == "hanging") {
217         baseline = HangingTextBaseline;
218         return true;
219     }
220     return false;
221 }
222
223 } // namespace blink