Added Evas#font_hinting=.
[ruby-evas.git] / src / rb_text.c
1 /*
2  * Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 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  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include <ruby.h>
20
21 #include <Evas.h>
22
23 #include "rb_evas_main.h"
24 #include "rb_evas.h"
25 #include "rb_evas_object.h"
26
27 #define GET_COLOR_METHOD(name) \
28         int r = 0, g = 0, b = 0, a = 0; \
29 \
30         GET_OBJ (self, RbEvasObject, e); \
31 \
32         evas_object_text_##name##_color_get (e->real, &r, &g, &b, &a); \
33 \
34         return rb_ary_new3 (4, \
35                             INT2FIX (r), INT2FIX (g), \
36                             INT2FIX (b), INT2FIX (a));
37
38 #define SET_COLOR_METHOD(name) \
39         GET_OBJ (self, RbEvasObject, e); \
40 \
41         Check_Type (r, T_FIXNUM); \
42         Check_Type (g, T_FIXNUM); \
43         Check_Type (b, T_FIXNUM); \
44         Check_Type (a, T_FIXNUM); \
45 \
46         evas_object_text_##name##_color_set (e->real, \
47                                              FIX2INT (r), FIX2INT (g), \
48                                              FIX2INT (b), FIX2INT (a)); \
49 \
50         return Qnil;
51
52 /*
53  * call-seq:
54  *  Evas::Text.new(evas) => text
55  *
56  * Creates an Evas::Text object.
57  */
58 static VALUE c_init (VALUE self, VALUE evas)
59 {
60         CHECK_CLASS (evas, cEvas);
61         GET_OBJ (evas, RbEvas, e);
62         GET_OBJ (self, RbEvasObject, text);
63
64         text->real = evas_object_text_add (e->real);
65
66         rb_call_super (1, &evas);
67
68         return self;
69 }
70
71 /*
72  * call-seq:
73  *  text.font_source => string or nil
74  *
75  * Returns the font source of <i>text</i>.
76  */
77 static VALUE c_font_source_get (VALUE self)
78 {
79         const char *tmp;
80
81         GET_OBJ (self, RbEvasObject, e);
82
83         if (!(tmp = evas_object_text_font_source_get (e->real)))
84                 return Qnil;
85         else
86                 return rb_str_new2 (tmp);
87 }
88
89 /*
90  * call-seq:
91  *  text.font_source(source)
92  *
93  * Sets the font source of <i>text</i>.
94  */
95 static VALUE c_font_source_set (VALUE self, VALUE val)
96 {
97         GET_OBJ (self, RbEvasObject, e);
98
99         evas_object_text_font_source_set (e->real, StringValuePtr (val));
100
101         return Qnil;
102 }
103
104 /*
105  * call-seq:
106  *  text.get_font => array
107  *
108  * Returns the font name and font size of <i>text</i>.
109  *
110  *  text.set_font("vera", 10) #=> nil
111  *  text_get_font             #=> ["vera", 10]
112  */
113 static VALUE c_get_font (VALUE self)
114 {
115         const char *font = NULL;
116         Evas_Font_Size size = 0;
117
118         GET_OBJ (self, RbEvasObject, e);
119
120         evas_object_text_font_get (e->real, &font, &size);
121
122         return rb_ary_new3 (2, font ? rb_str_new2 (font) : Qnil,
123                             INT2FIX (size));
124 }
125
126 /*
127  * call-seq:
128  *  text.set_font(font, size) => nil
129  *
130  * Sets the font name and font size of <i>text</i>.
131  *
132  *  text.set_font("vera", 10) #=> nil
133  */
134 static VALUE c_set_font (VALUE self, VALUE font, VALUE size)
135 {
136         GET_OBJ (self, RbEvasObject, e);
137
138         Check_Type (size, T_FIXNUM);
139
140         evas_object_text_font_set (e->real, StringValuePtr (font),
141                                    FIX2INT (size));
142
143         return Qnil;
144 }
145
146 /*
147  * call-seq:
148  *  text.text => string
149  *
150  * Returns the text of <i>text</i>.
151  */
152 static VALUE c_text_get (VALUE self)
153 {
154         const char *tmp;
155
156         GET_OBJ (self, RbEvasObject, e);
157
158         if (!(tmp = evas_object_text_text_get (e->real)))
159                 return Qnil;
160         else
161                 return rb_str_new2 (tmp);
162 }
163
164 /*
165  * call-seq:
166  *  text.text(string)
167  *
168  * Sets the text of <i>text</i>.
169  */
170 static VALUE c_text_set (VALUE self, VALUE val)
171 {
172         GET_OBJ (self, RbEvasObject, e);
173
174         evas_object_text_text_set (e->real, StringValuePtr (val));
175
176         return Qnil;
177 }
178
179 /*
180  * call-seq:
181  *  text.style => integer
182  *
183  * Returns the style of <i>text</i>.
184  */
185 static VALUE c_style_get (VALUE self)
186 {
187         GET_OBJ (self, RbEvasObject, e);
188
189         return INT2FIX (evas_object_text_style_get (e->real));
190 }
191
192 /*
193  * call-seq:
194  *  text.style = integer
195  *
196  * Sets the style of <i>text</i>.
197  */
198 static VALUE c_style_set (VALUE self, VALUE val)
199 {
200         GET_OBJ (self, RbEvasObject, e);
201
202         Check_Type (val, T_FIXNUM);
203
204         evas_object_text_style_set (e->real, FIX2INT (val));
205
206         return Qnil;
207 }
208
209 static VALUE c_style_pad_get (VALUE self)
210 {
211         int l = 0, r = 0, t = 0, b = 0;
212
213         GET_OBJ (self, RbEvasObject, e);
214
215         evas_object_text_style_pad_get (e->real, &l, &r, &t, &b);
216
217         return rb_ary_new3 (4,
218                             INT2FIX (l), INT2FIX (r),
219                             INT2FIX (r), INT2FIX (b));
220 }
221
222 /*
223  * call-seq:
224  *  e.get_shadow_color => array
225  *
226  * Returns the shadow color of <i>e</i>.
227  *
228  *  e.set_shadow_color(128, 128, 128, 0) #=> nil
229  *  e.get_shadow_color                   #=> [128, 128, 128, 0]
230  */
231 static VALUE c_get_shadow_color (VALUE self)
232 {
233         GET_COLOR_METHOD (shadow);
234 }
235
236 /*
237  * call-seq:
238  *  e.set_shadow_color(r, g, b, a) => nil
239  *
240  * Sets the shadow color of <i>e</i>.
241  *
242  *  e.set_shadow_color(128, 128, 128, 0) #=> nil
243  */
244 static VALUE c_set_shadow_color (VALUE self,
245                                  VALUE r, VALUE g, VALUE b, VALUE a)
246 {
247         SET_COLOR_METHOD (shadow);
248 }
249
250 /*
251  * call-seq:
252  *  e.get_glow_color => array
253  *
254  * Returns the glow color of <i>e</i>.
255  *
256  *  e.set_glow_color(128, 128, 128, 0) #=> nil
257  *  e.get_glow_color                   #=> [128, 128, 128, 0]
258  */
259 static VALUE c_get_glow_color (VALUE self)
260 {
261         GET_COLOR_METHOD (glow);
262 }
263
264 /*
265  * call-seq:
266  *  e.set_glow_color(r, g, b, a) => nil
267  *
268  * Sets the glow color of <i>e</i>.
269  *
270  *  e.set_glow_color(128, 128, 128, 0) #=> nil
271  */
272 static VALUE c_set_glow_color (VALUE self,
273                                  VALUE r, VALUE g, VALUE b, VALUE a)
274 {
275         SET_COLOR_METHOD (glow);
276 }
277
278 /*
279  * call-seq:
280  *  e.get_glow2_color => array
281  *
282  * Returns the glow2 color of <i>e</i>.
283  *
284  *  e.set_glow2_color(128, 128, 128, 0) #=> nil
285  *  e.get_glow2_color                   #=> [128, 128, 128, 0]
286  */
287 static VALUE c_get_glow2_color (VALUE self)
288 {
289         GET_COLOR_METHOD (glow2);
290 }
291
292 /*
293  * call-seq:
294  *  e.set_glow2_color(r, g, b, a) => nil
295  *
296  * Sets the glow2 color of <i>e</i>.
297  *
298  *  e.set_glow2_color(128, 128, 128, 0) #=> nil
299  */
300 static VALUE c_set_glow2_color (VALUE self,
301                                VALUE r, VALUE g, VALUE b, VALUE a)
302 {
303         SET_COLOR_METHOD (glow2);
304 }
305
306 /*
307  * call-seq:
308  *  e.get_outline_color => array
309  *
310  * Returns the outline color of <i>e</i>.
311  *
312  *  e.set_outline_color(128, 128, 128, 0) #=> nil
313  *  e.get_outline_color                   #=> [128, 128, 128, 0]
314  */
315 static VALUE c_get_outline_color (VALUE self)
316 {
317         GET_COLOR_METHOD (outline);
318 }
319
320 /*
321  * call-seq:
322  *  e.set_outline_color(r, g, b, a) => nil
323  *
324  * Sets the outline color of <i>e</i>.
325  *
326  *  e.set_outline_color(128, 128, 128, 0) #=> nil
327  */
328 static VALUE c_set_outline_color (VALUE self,
329                                   VALUE r, VALUE g, VALUE b, VALUE a)
330 {
331         SET_COLOR_METHOD (outline);
332 }
333
334 static VALUE c_ascent_get (VALUE self)
335 {
336         GET_OBJ (self, RbEvasObject, e);
337
338         return INT2FIX ((int) evas_object_text_ascent_get (e->real));
339 }
340
341 static VALUE c_descent_get (VALUE self)
342 {
343         GET_OBJ (self, RbEvasObject, e);
344
345         return INT2FIX ((int) evas_object_text_descent_get (e->real));
346 }
347
348 static VALUE c_max_ascent_get (VALUE self)
349 {
350         GET_OBJ (self, RbEvasObject, e);
351
352         return INT2FIX ((int) evas_object_text_max_ascent_get (e->real));
353 }
354
355 static VALUE c_max_descent_get (VALUE self)
356 {
357         GET_OBJ (self, RbEvasObject, e);
358
359         return INT2FIX ((int) evas_object_text_max_descent_get (e->real));
360 }
361
362 static VALUE c_advance_get (VALUE self)
363 {
364         int h = 0, v = 0;
365
366         GET_OBJ (self, RbEvasObject, e);
367
368         h = (int) evas_object_text_horiz_advance_get (e->real);
369         v = (int) evas_object_text_vert_advance_get (e->real);
370
371         return rb_ary_new3 (2, INT2FIX (h), INT2FIX (v));
372 }
373
374 static VALUE c_inset_get (VALUE self)
375 {
376         GET_OBJ (self, RbEvasObject, e);
377
378         return INT2FIX ((int) evas_object_text_inset_get (e->real));
379 }
380
381 void Init_Text (void)
382 {
383         VALUE c, c2;
384
385         c = rb_define_class_under (mEvas, "Text", cEvasObject);
386
387         rb_define_method (c, "initialize", c_init, 1);
388         rb_define_method (c, "font_source", c_font_source_get, 0);
389         rb_define_method (c, "font_source=", c_font_source_set, 1);
390         rb_define_method (c, "get_font", c_get_font, 0);
391         rb_define_method (c, "set_font", c_set_font, 2);
392         rb_define_method (c, "text", c_text_get, 0);
393         rb_define_method (c, "text=", c_text_set, 1);
394         rb_define_method (c, "style", c_style_get, 0);
395         rb_define_method (c, "style=", c_style_set, 1);
396         rb_define_method (c, "style_pad", c_style_pad_get, 0);
397         rb_define_method (c, "get_shadow_color", c_get_shadow_color, 0);
398         rb_define_method (c, "set_shadow_color", c_set_shadow_color, 4);
399         rb_define_method (c, "get_glow_color", c_get_glow_color, 0);
400         rb_define_method (c, "set_glow_color", c_set_glow_color, 4);
401         rb_define_method (c, "get_glow2_color", c_get_glow2_color, 0);
402         rb_define_method (c, "set_glow2_color", c_set_glow2_color, 4);
403         rb_define_method (c, "get_outline_color", c_get_outline_color, 0);
404         rb_define_method (c, "set_outline_color", c_set_outline_color, 4);
405
406         rb_define_method (c, "ascent", c_ascent_get, 0);
407         rb_define_method (c, "descent", c_descent_get, 0);
408         rb_define_method (c, "max_ascent", c_max_ascent_get, 0);
409         rb_define_method (c, "max_descent", c_max_descent_get, 0);
410         rb_define_method (c, "advance", c_advance_get, 0);
411         rb_define_method (c, "inset", c_inset_get, 0);
412
413         c2 = rb_define_class_under (mEvas, "Style", c);
414
415         DEF_CONST (c2, EVAS_TEXT_STYLE_, PLAIN);
416         DEF_CONST (c2, EVAS_TEXT_STYLE_, SHADOW);
417         DEF_CONST (c2, EVAS_TEXT_STYLE_, OUTLINE);
418         DEF_CONST (c2, EVAS_TEXT_STYLE_, SOFT_OUTLINE);
419         DEF_CONST (c2, EVAS_TEXT_STYLE_, GLOW);
420         DEF_CONST (c2, EVAS_TEXT_STYLE_, OUTLINE_SHADOW);
421         DEF_CONST (c2, EVAS_TEXT_STYLE_, FAR_SHADOW);
422         DEF_CONST (c2, EVAS_TEXT_STYLE_, OUTLINE_SOFT_SHADOW);
423         DEF_CONST (c2, EVAS_TEXT_STYLE_, SOFT_SHADOW);
424         DEF_CONST (c2, EVAS_TEXT_STYLE_, FAR_SOFT_SHADOW);
425 }