Create string tightly when retrive string from cbhm callback event
[framework/web/webkit-efl.git] / Source / WebKit2 / UIProcess / API / efl / ewk_text_style.cpp
1 /*
2    Copyright (C) 2011 Samsung Electronics
3
4     This library is free software; you can redistribute it and/or
5     modify it under the terms of the GNU Library General Public
6     License as published by the Free Software Foundation; either
7     version 2 of the License, or (at your option) any later version.
8
9     This library is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12     Library General Public License for more details.
13
14     You should have received a copy of the GNU Library General Public License
15     along with this library; see the file COPYING.LIB.  If not, write to
16     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17     Boston, MA 02110-1301, USA.
18 */
19
20 #include "config.h"
21 #include "ewk_text_style.h"
22
23 #include <WebCore/IntPoint.h>
24
25 struct _Ewk_Text_Style {
26     Ewk_Text_Style_State underlineState;
27     Ewk_Text_Style_State italicState;
28     Ewk_Text_Style_State boldState;
29
30     struct {
31         Evas_Point startPoint;
32         Evas_Point endPoint;
33     } position;
34
35     struct {
36         int r;
37         int g;
38         int b;
39         int a;
40     } bgColor;
41
42     struct {
43         int r;
44         int g;
45         int b;
46         int a;
47     } color;
48
49     const char* fontSize;
50     Eina_Bool hasComposition;
51 };
52
53 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
54 Ewk_Text_Style* ewkTextStyleCreate(const EditorState editorState, const WebCore::IntPoint& startPoint, const WebCore::IntPoint& endPoint)
55 {
56     Ewk_Text_Style* textStyle = new Ewk_Text_Style;
57
58     textStyle->underlineState = static_cast<Ewk_Text_Style_State>(editorState.underlineState);
59     textStyle->italicState = static_cast<Ewk_Text_Style_State>(editorState.italicState);
60     textStyle->boldState = static_cast<Ewk_Text_Style_State>(editorState.boldState);
61
62     textStyle->position.startPoint.x = startPoint.x();
63     textStyle->position.startPoint.y = startPoint.y();
64     textStyle->position.endPoint.x = endPoint.x();
65     textStyle->position.endPoint.y = endPoint.y();
66
67     // for example - rgba(255, 255, 255, 255)
68     // for example - rgb(255, 255, 255)
69     if (equalIgnoringCase(editorState.bgColor.left(3), "rgb")) {
70         size_t startPos = editorState.bgColor.find("(");
71         size_t endPos = editorState.bgColor.find(")");
72
73         String value = editorState.bgColor.substring(startPos + 1, endPos - startPos - 1);
74         Vector<String> colorValues;
75         value.split(",", colorValues);
76         textStyle->bgColor.r = colorValues[0].toInt();
77         textStyle->bgColor.g = colorValues[1].toInt();
78         textStyle->bgColor.b = colorValues[2].toInt();
79         if (equalIgnoringCase(editorState.bgColor.left(4), "rgba"))
80             textStyle->bgColor.a = colorValues[3].toInt();
81         else
82             textStyle->bgColor.a = 255;
83     }
84
85     if (equalIgnoringCase(editorState.color.left(3), "rgb")) {
86         size_t startPos = editorState.color.find("(");
87         size_t endPos = editorState.color.find(")");
88
89         String value = editorState.color.substring(startPos + 1, endPos - startPos - 1);
90         Vector<String> colorValues;
91         value.split(",", colorValues);
92         textStyle->color.r = colorValues[0].toInt();
93         textStyle->color.g = colorValues[1].toInt();
94         textStyle->color.b = colorValues[2].toInt();
95         if (equalIgnoringCase(editorState.color.left(4), "rgba"))
96             textStyle->color.a = colorValues[3].toInt();
97         else
98             textStyle->color.a = 255;
99     }
100
101     textStyle->fontSize = eina_stringshare_add(editorState.fontSize.utf8().data());
102     textStyle->hasComposition = editorState.hasComposition;
103
104     return textStyle;
105 }
106
107 void ewkTextStyleDelete(Ewk_Text_Style* textStyle)
108 {
109     EINA_SAFETY_ON_NULL_RETURN(textStyle);
110
111     if (textStyle->fontSize)
112         eina_stringshare_del(textStyle->fontSize);
113
114     delete textStyle;
115 }
116 #endif
117
118 Ewk_Text_Style_State ewk_text_style_underline_get(Ewk_Text_Style* textStyle)
119 {
120 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
121     EINA_SAFETY_ON_NULL_RETURN_VAL(textStyle, EWK_TEXT_STYLE_STATE_FALSE);
122
123     return textStyle->underlineState;
124 #else
125     return EWK_TEXT_STYLE_STATE_FALSE;
126 #endif
127 }
128
129 Ewk_Text_Style_State ewk_text_style_italic_get(Ewk_Text_Style* textStyle)
130 {
131 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
132     EINA_SAFETY_ON_NULL_RETURN_VAL(textStyle, EWK_TEXT_STYLE_STATE_FALSE);
133
134     return textStyle->italicState;
135 #else
136     return EWK_TEXT_STYLE_STATE_FALSE;
137 #endif
138 }
139
140 Ewk_Text_Style_State ewk_text_style_bold_get(Ewk_Text_Style* textStyle)
141 {
142 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
143     EINA_SAFETY_ON_NULL_RETURN_VAL(textStyle, EWK_TEXT_STYLE_STATE_FALSE);
144
145     return textStyle->boldState;
146 #else
147     return EWK_TEXT_STYLE_STATE_FALSE;
148 #endif
149 }
150
151 Eina_Bool ewk_text_style_position_get(Ewk_Text_Style* textStyle, Evas_Point* startPoint, Evas_Point* endPoint)
152 {
153 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
154     EINA_SAFETY_ON_NULL_RETURN_VAL(textStyle, false);
155
156     startPoint->x = textStyle->position.startPoint.x;
157     startPoint->y = textStyle->position.startPoint.y;
158
159     endPoint->x = textStyle->position.endPoint.x;
160     endPoint->y = textStyle->position.endPoint.y;
161
162     return true;
163 #else
164     return false;
165 #endif
166 }
167
168 Eina_Bool ewk_text_style_bg_color_get(Ewk_Text_Style* textStyle, int* r, int* g, int* b, int* a)
169 {
170 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
171     EINA_SAFETY_ON_NULL_RETURN_VAL(textStyle, false);
172
173     if (r)
174         *r = textStyle->bgColor.r;
175     if (g)
176         *g = textStyle->bgColor.g;
177     if (b)
178         *b = textStyle->bgColor.b;
179     if (a)
180         *a = textStyle->bgColor.a;
181
182    return true;
183 #else
184     return false;
185 #endif
186 }
187
188 Eina_Bool ewk_text_style_color_get(Ewk_Text_Style* textStyle, int* r, int* g, int* b, int* a)
189 {
190 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
191     EINA_SAFETY_ON_NULL_RETURN_VAL(textStyle, false);
192
193     if (r)
194         *r = textStyle->color.r;
195     if (g)
196         *g = textStyle->color.g;
197     if (b)
198         *b = textStyle->color.b;
199     if (a)
200         *a = textStyle->color.a;
201
202    return true;
203 #else
204     return false;
205 #endif
206 }
207
208 const char* ewk_text_style_font_size_get(Ewk_Text_Style* textStyle)
209 {
210     EINA_SAFETY_ON_NULL_RETURN_VAL(textStyle, 0);
211     return textStyle->fontSize;
212 }
213
214 Eina_Bool ewk_text_style_has_composition_get(Ewk_Text_Style* textStyle)
215 {
216     EINA_SAFETY_ON_NULL_RETURN_VAL(textStyle, false);
217     return textStyle->hasComposition;
218 }