Author: dmeyer
Date: Sat Jun 17 15:26:17 2006
New Revision: 1544

Modified:
   trunk/imlib2/src/font.py
   trunk/imlib2/src/image.c
   trunk/imlib2/src/image.py

Log:
add evas text effects as imlib2 feature

Modified: trunk/imlib2/src/font.py
==============================================================================
--- trunk/imlib2/src/font.py    (original)
+++ trunk/imlib2/src/font.py    Sat Jun 17 15:26:17 2006
@@ -54,7 +54,8 @@
         self.fontname = fontdesc[:sep]
         self.size = fontdesc[sep + 1:]
         self.set_color(color)
-
+        self.style = 0 # TEXT_STYLE_PLAIN
+        
 
     def get_text_size(self, text):
         """
@@ -86,6 +87,41 @@
             self.color = color
 
 
+    def set_style(self, style, shadow=(0,0,0,0), outline=(0,0,0,0),
+                  glow=(0,0,0,0), glow2=(0,0,0,0)):
+        """
+        Set a text style. Based on the style different color parameter
+        need to be set.
+
+        Arguments:
+            style:   the style to use (disable with TEXT_STYLE_PLAIN)
+            shadow:  shadow color for TEXT_STYLE_SHADOW, 
TEXT_STYLE_OUTLINE_SHADOW,
+                     TEXT_STYLE_FAR_SHADOW, TEXT_STYLE_OUTLINE_SOFT_SHADOW,
+                     TEXT_STYLE_SOFT_SHADOW and TEXT_STYLE_FAR_SOFT_SHADOW
+            outline: outline color for TEXT_STYLE_OUTLINE, 
TEXT_STYLE_SOFT_OUTLINE, 
+                     TEXT_STYLE_OUTLINE_SHADOW and 
TEXT_STYLE_OUTLINE_SOFT_SHADOW
+            glow:    glow color 1 for TEXT_STYLE_GLOW
+            glow2:   glow color 2 for TEXT_STYLE_GLOW
+        """
+        self.style = style
+        if len(shadow) == 3:
+            self.shadow = tuple(shadow) + (255,)
+        else:
+            self.shadow = shadow
+        if len(outline) == 3:
+            self.outline = tuple(outline) + (255,)
+        else:
+            self.outline = outline
+        if len(glow) == 3:
+            self.glow = tuple(glow) + (255,)
+        else:
+            self.glow = glow
+        if len(glow2) == 3:
+            self.glow2 = tuple(glow2) + (255,)
+        else:
+            self.glow2 = glow2
+
+        
     def __getattr__(self, attr):
         """
         These attributes are available:

Modified: trunk/imlib2/src/image.c
==============================================================================
--- trunk/imlib2/src/image.c    (original)
+++ trunk/imlib2/src/image.c    Sat Jun 17 15:26:17 2006
@@ -76,6 +76,23 @@
     "Imlib2 Image Object"      /* tp_doc */
 };
 
+typedef enum _Text_Style_Type {
+    TEXT_STYLE_PLAIN,
+    TEXT_STYLE_SHADOW,
+    TEXT_STYLE_OUTLINE,
+    TEXT_STYLE_SOFT_OUTLINE,
+    TEXT_STYLE_GLOW,
+    TEXT_STYLE_OUTLINE_SHADOW,
+    TEXT_STYLE_FAR_SHADOW,
+    TEXT_STYLE_OUTLINE_SOFT_SHADOW,
+    TEXT_STYLE_SOFT_SHADOW,
+    TEXT_STYLE_FAR_SOFT_SHADOW
+} Text_Style_Type;
+
+typedef struct _Color {
+    int r,g,b,a;
+} Color;
+
 // Exported _C_API function
 Imlib_Image *imlib_image_from_pyobject(Image_PyObject *pyimg)
 {
@@ -398,9 +415,9 @@
     int x, y, w, h, advance_w, advance_h, r, g, b, a;
     char *text;
     Font_PyObject *font;
-
+    
     if (!PyArg_ParseTuple(args, "O!iis(iiii)", &Font_PyObject_Type, &font, &x,
-              &y, &text, &r, &g, &b, &a))
+                         &y, &text, &r, &g, &b, &a))
         return NULL;
 
     imlib_context_set_image(((Image_PyObject *)self)->image);
@@ -408,11 +425,130 @@
 
     imlib_context_set_color(r, g, b, a);
     imlib_text_draw_with_return_metrics(x, y, text, &w, &h, &advance_w,
-                    &advance_h);
+                                       &advance_h);
     return Py_BuildValue("(llll)", w, h, advance_w, advance_h);
 }
 
 
+#define DRAW_TEXT(divx,divy) imlib_text_draw(x+divx, y+divy, text);
+#define COLOR_SET_AMUL(col, amul) \
+    imlib_context_set_color(col.r, col.g, col.b, (col.a * amul) / 255);
+#define COLOR_SET(col) imlib_context_set_color(col.r, col.g, col.b, col.a);
+
+PyObject *Image_PyObject__draw_text_with_style(PyObject *self, PyObject *args)
+{
+    Color color, shadow, outline, glow, glow2;
+    int x, y, w, h, advance_w, advance_h, i, j;
+    Text_Style_Type style;
+    char *text;
+    Font_PyObject *font;
+    
+    const char vals[5][5] = {
+       {0, 1, 2, 1, 0},
+       {1, 3, 4, 3, 1},
+       {2, 4, 5, 4, 2},
+       {1, 3, 4, 3, 1},
+       {0, 1, 2, 1, 0}
+    };
+
+    if (!PyArg_ParseTuple(args, "O!iisi(iiii)(iiii)(iiii)(iiii)(iiii)", 
+                         &Font_PyObject_Type, &font, &x, &y, &text, &style,
+                         &color.r, &color.g, &color.b, &color.a, 
+                         &shadow.r, &shadow.g, &shadow.b, &shadow.a,
+                         &outline.r, &outline.g, &outline.b, &outline.a,
+                         &glow.r, &glow.g, &glow.b, &glow.a,
+                         &glow2.r, &glow2.g, &glow2.b, &glow2.a))
+        return NULL;
+       
+    imlib_context_set_image(((Image_PyObject *)self)->image);
+    imlib_context_set_font(((Font_PyObject *)font)->font);
+    
+    /* FIXME: change x,y based on effect */
+
+    /* The following code is more or less copied from evas_object_text */
+
+    /* shadows */
+    if (style == TEXT_STYLE_SHADOW) {
+       COLOR_SET(shadow);
+       DRAW_TEXT(1, 1);
+    }
+    
+    else if ((style == TEXT_STYLE_OUTLINE_SHADOW) ||
+            (style == TEXT_STYLE_FAR_SHADOW)) {
+       COLOR_SET(shadow);
+       DRAW_TEXT(2, 2);
+    }
+    
+    else if ((style == TEXT_STYLE_OUTLINE_SOFT_SHADOW) ||
+            (style == TEXT_STYLE_FAR_SOFT_SHADOW)) {
+       for (j = 0; j < 5; j++) {
+           for (i = 0; i < 5; i++) {
+               if (vals[i][j] != 0) {
+                   COLOR_SET_AMUL(shadow, vals[i][j] * 50);
+                   DRAW_TEXT(i, j);
+               }
+           }
+       }
+    }
+    else if (style == TEXT_STYLE_SOFT_SHADOW) {
+       for (j = 0; j < 5; j++) {
+           for (i = 0; i < 5; i++) {
+               if (vals[i][j] != 0) {
+                   COLOR_SET_AMUL(shadow, vals[i][j] * 50);
+                   DRAW_TEXT(i - 1, j - 1);
+               }
+           }
+       }
+    }
+                         
+    /* glows */
+    if (style == TEXT_STYLE_GLOW) {
+       for (j = 0; j < 5; j++) {
+           for (i = 0; i < 5; i++) {
+               if (vals[i][j] != 0) {
+                   COLOR_SET_AMUL(glow, vals[i][j] * 50);
+                   DRAW_TEXT(i - 2, j - 2);
+               }
+           }
+       }
+       COLOR_SET(glow2);
+       DRAW_TEXT(-1, 0);
+       DRAW_TEXT(1, 0);
+       DRAW_TEXT(0, -1);
+       DRAW_TEXT(0, 1);
+    }
+    
+
+    /* outlines */
+    if ((style == TEXT_STYLE_OUTLINE) ||
+       (style == TEXT_STYLE_OUTLINE_SHADOW) ||
+       (style == TEXT_STYLE_OUTLINE_SOFT_SHADOW)) {
+       COLOR_SET(outline);
+       DRAW_TEXT(-1, 0);
+       DRAW_TEXT(1, 0);
+       DRAW_TEXT(0, -1);
+       DRAW_TEXT(0, 1);
+     }
+    else if (style == TEXT_STYLE_SOFT_OUTLINE) {
+       for (j = 0; j < 5; j++) {
+           for (i = 0; i < 5; i++) {
+               if (((i != 2) || (j != 2)) && (vals[i][j] != 0)) {
+                   COLOR_SET_AMUL(outline, vals[i][j] * 50);
+                   DRAW_TEXT(i - 2, j - 2);
+               }
+           }
+       }
+    }
+
+    COLOR_SET(color);
+    imlib_text_draw_with_return_metrics(x, y, text, &w, &h, &advance_w, 
&advance_h);
+
+    /* FIXME: add effect to advance_w and advance_h */
+    return Py_BuildValue("(llll)", w, h, advance_w, advance_h);
+}
+
+
+
 PyObject *Image_PyObject__draw_rectangle(PyObject *self, PyObject *args)
 {
     int x, y, w, h, r, g, b, a, fill = 0;
@@ -578,6 +714,7 @@
     { "draw_rectangle", Image_PyObject__draw_rectangle, METH_VARARGS },
     { "draw_ellipse", Image_PyObject__draw_ellipse, METH_VARARGS },
     { "draw_text", Image_PyObject__draw_text, METH_VARARGS },
+    { "draw_text_with_style", Image_PyObject__draw_text_with_style, 
METH_VARARGS },
     { "draw_mask", Image_PyObject__draw_mask, METH_VARARGS },
     { "clear", Image_PyObject__clear, METH_VARARGS },
     { "copy_rect", Image_PyObject__copy_rect, METH_VARARGS },

Modified: trunk/imlib2/src/image.py
==============================================================================
--- trunk/imlib2/src/image.py   (original)
+++ trunk/imlib2/src/image.py   Sat Jun 17 15:26:17 2006
@@ -36,6 +36,17 @@
 from kaa.notifier import Signal
 from font import Font
 
+TEXT_STYLE_PLAIN, \
+TEXT_STYLE_SHADOW, \
+TEXT_STYLE_OUTLINE, \
+TEXT_STYLE_SOFT_OUTLINE, \
+TEXT_STYLE_GLOW, \
+TEXT_STYLE_OUTLINE_SHADOW, \
+TEXT_STYLE_FAR_SHADOW, \
+TEXT_STYLE_OUTLINE_SOFT_SHADOW, \
+TEXT_STYLE_SOFT_SHADOW, \
+TEXT_STYLE_FAR_SOFT_SHADOW = range(10)
+
 class Image(object):
     """
     Imlib2 Image class.  The constructor can be called directly, or a new
@@ -426,7 +437,9 @@
         return self.font
 
 
-    def draw_text(self, (x, y), text, color = None, font_or_fontname = None):
+    def draw_text(self, (x, y), text, color = None, font_or_fontname = None,
+                  style = None, shadow = None, outline = None, glow = None,
+                  glow2 = None):
         """
         Draws text on the image.
 
@@ -444,6 +457,8 @@
                             form "Fontname/Size" such as "Arial/16".  If this
                             parameter is none, the font context is used, as
                             specified by set_font().
+          style:            The style to use. Id style is None, the style from
+                            the font object will be used.
 
         Returns: a 4-tuple representing the width, height, horizontal advance,
                  and vertical advance of the rendered text.
@@ -457,13 +472,39 @@
 
         if not color:
             color = font.color
-        if len(color) == 3:
+        elif len(color) == 3:
             color = tuple(color) + (255,)
 
-        metrics = self._image.draw_text(font._font, int(x), int(y),
-                                        utf8(text), color)
+        if style == TEXT_STYLE_PLAIN or \
+               (style == None and font.style == TEXT_STYLE_PLAIN):
+            metrics = self._image.draw_text(font._font, int(x), int(y),
+                                            utf8(text), color)
+        else:
+            if not style:
+                style = font.style
+            if not shadow:
+                shadow = font.shadow
+            elif len(shadow) == 3:
+                shadow = tuple(shadow) + (255,)
+            if not outline:
+                outline = font.outline
+            elif len(outline) == 3:
+                outline = tuple(outline) + (255,)
+            if not glow:
+                glow = font.glow
+            elif len(glow) == 3:
+                glow = tuple(glow) + (255,)
+            if not glow2:
+                glow2 = font.glow2
+            elif len(glow2) == 3:
+                glow2 = tuple(glow2) + (255,)
+                
+            metrics = self._image.draw_text_with_style(font._font, int(x), 
int(y),
+                                                       utf8(text), style, 
color,
+                                                       shadow, outline, glow, 
glow2)
         self._changed()
         return metrics
+        
 
 
     def draw_rectangle(self, (x, y), (w, h), color, fill = True):


_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog

Reply via email to