Added APIs to get font/background color of selected text.
[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
50 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
51 Ewk_Text_Style* ewkTextStyleCreate(int underlineState, int italicState, int boldState, const WebCore::IntPoint& startPoint, const WebCore::IntPoint& endPoint, const String& bgColor, const String& color)
52 {
53     Ewk_Text_Style* textStyle = new Ewk_Text_Style;
54
55     textStyle->underlineState = static_cast<Ewk_Text_Style_State>(underlineState);
56     textStyle->italicState = static_cast<Ewk_Text_Style_State>(italicState);
57     textStyle->boldState = static_cast<Ewk_Text_Style_State>(boldState);
58
59     textStyle->position.startPoint.x = startPoint.x();
60     textStyle->position.startPoint.y = startPoint.y();
61     textStyle->position.endPoint.x = endPoint.x();
62     textStyle->position.endPoint.y = endPoint.y();
63
64     // for example - rgba(255, 255, 255, 255)
65     // for example - rgb(255, 255, 255)
66     if (equalIgnoringCase(bgColor.left(3), "rgb")) {
67         size_t startPos = bgColor.find("(");
68         size_t endPos = bgColor.find(")");
69
70         String value = bgColor.substring(startPos + 1, endPos - startPos - 1);
71         Vector<String> colorValues;
72         value.split(",", colorValues);
73         textStyle->bgColor.r = colorValues[0].toInt();
74         textStyle->bgColor.g = colorValues[1].toInt();
75         textStyle->bgColor.b = colorValues[2].toInt();
76         if (equalIgnoringCase(bgColor.left(4), "rgba"))
77             textStyle->bgColor.a = colorValues[3].toInt();
78         else
79             textStyle->bgColor.a = 255;
80     }
81
82     if (equalIgnoringCase(color.left(3), "rgb")) {
83         size_t startPos = color.find("(");
84         size_t endPos = color.find(")");
85
86         String value = color.substring(startPos + 1, endPos - startPos - 1);
87         Vector<String> colorValues;
88         value.split(",", colorValues);
89         textStyle->color.r = colorValues[0].toInt();
90         textStyle->color.g = colorValues[1].toInt();
91         textStyle->color.b = colorValues[2].toInt();
92         if (equalIgnoringCase(color.left(4), "rgba"))
93             textStyle->color.a = colorValues[3].toInt();
94         else
95             textStyle->color.a = 255;
96     }
97
98     return textStyle;
99 }
100
101 void ewkTextStyleDelete(Ewk_Text_Style* textStyle)
102 {
103     EINA_SAFETY_ON_NULL_RETURN(textStyle);
104
105     delete textStyle;
106 }
107 #endif
108
109 Ewk_Text_Style_State ewk_text_style_underline_get(Ewk_Text_Style* textStyle)
110 {
111 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
112     EINA_SAFETY_ON_NULL_RETURN_VAL(textStyle, EWK_TEXT_STYLE_STATE_FALSE);
113
114     return textStyle->underlineState;
115 #else
116     return EWK_TEXT_STYLE_STATE_FALSE;
117 #endif
118 }
119
120 Ewk_Text_Style_State ewk_text_style_italic_get(Ewk_Text_Style* textStyle)
121 {
122 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
123     EINA_SAFETY_ON_NULL_RETURN_VAL(textStyle, EWK_TEXT_STYLE_STATE_FALSE);
124
125     return textStyle->italicState;
126 #else
127     return EWK_TEXT_STYLE_STATE_FALSE;
128 #endif
129 }
130
131 Ewk_Text_Style_State ewk_text_style_bold_get(Ewk_Text_Style* textStyle)
132 {
133 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
134     EINA_SAFETY_ON_NULL_RETURN_VAL(textStyle, EWK_TEXT_STYLE_STATE_FALSE);
135
136     return textStyle->boldState;
137 #else
138     return EWK_TEXT_STYLE_STATE_FALSE;
139 #endif
140 }
141
142 Eina_Bool ewk_text_style_position_get(Ewk_Text_Style* textStyle, Evas_Point* startPoint, Evas_Point* endPoint)
143 {
144 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
145     EINA_SAFETY_ON_NULL_RETURN_VAL(textStyle, false);
146
147     startPoint->x = textStyle->position.startPoint.x;
148     startPoint->y = textStyle->position.startPoint.y;
149
150     endPoint->x = textStyle->position.endPoint.x;
151     endPoint->y = textStyle->position.endPoint.y;
152
153     return true;
154 #else
155     return false;
156 #endif
157 }
158
159 Eina_Bool ewk_text_style_bg_color_get(Ewk_Text_Style* textStyle, int* r, int* g, int* b, int* a)
160 {
161 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
162     EINA_SAFETY_ON_NULL_RETURN_VAL(textStyle, false);
163
164     if (r)
165         *r = textStyle->bgColor.r;
166     if (g)
167         *g = textStyle->bgColor.g;
168     if (b)
169         *b = textStyle->bgColor.b;
170     if (a)
171         *a = textStyle->bgColor.a;
172
173    return true;
174 #else
175     return false;
176 #endif
177 }
178
179 Eina_Bool ewk_text_style_color_get(Ewk_Text_Style* textStyle, int* r, int* g, int* b, int* a)
180 {
181 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
182     EINA_SAFETY_ON_NULL_RETURN_VAL(textStyle, false);
183
184     if (r)
185         *r = textStyle->color.r;
186     if (g)
187         *g = textStyle->color.g;
188     if (b)
189         *b = textStyle->color.b;
190     if (a)
191         *a = textStyle->color.a;
192
193    return true;
194 #else
195     return false;
196 #endif
197 }