In this example, we illustrate how to use text objects in various manners.
We place, in the canvas, a text object along with a border image to delimit its geometry. After we instantiate the text object, we set lots of properties on it to the initial ones from a preset list, which has the following declaration:
The 's', 'o', 'w' and 'g' keys will make the text object to cycle to the preset values on colors for shadow, outline, glow and 'glow 2' effects, respectively. Naturally, they will only take effect on the text styles which resemble them.
The full example follows.
#ifdef HAVE_CONFIG_H
#include "config.h"
#else
#define PACKAGE_EXAMPLES_DIR "."
#endif
#include <Ecore.h>
#include <stdio.h>
#include <errno.h>
#define WIDTH (320)
#define HEIGHT (240)
#define GREY {190, 190, 190, 255}
#define BLACK {0, 0, 0, 255}
#define WHITE {255, 255, 255, 255}
#define RED {255, 0, 0, 255}
#define GREEN {0, 255, 0, 255}
#define BLUE {0, 0, 255, 255}
#define POINTER_CYCLE(_ptr, _array) \
do \
{ \
if ((unsigned int)(((unsigned char *)(_ptr)) - ((unsigned char *)(_array))) >= \
sizeof(_array)) \
_ptr = _array; \
} \
while(0)
static const char *commands = \
"commands are:\n"
"\tt - change text's current style\n"
"\tz - change text's font size\n"
"\tf - change text's font family\n"
"\tb - change text's base color\n"
"\ts - change text's \'shadow\' color\n"
"\to - change text's \'outline\' color\n"
"\tw - change text's \'glow\' color\n"
"\tg - change text's \'glow 2\' color\n"
"\th - print help\n";
static const char *border_img_path = PACKAGE_EXAMPLES_DIR "/red.png";
struct color_tuple
{
int r, g, b, a;
};
struct text_preset_data
{
const char **font_ptr;
const char *font[3];
struct color_tuple *text_ptr;
struct color_tuple text[6];
struct color_tuple *shadow_ptr;
struct color_tuple shadow[4];
struct color_tuple *outline_ptr;
struct color_tuple outline[4];
struct color_tuple *glow_ptr;
struct color_tuple glow[4];
struct color_tuple *glow2_ptr;
struct color_tuple glow2[4];
};
struct test_data
{
Ecore_Evas *ee;
struct text_preset_data t_data;
};
static struct test_data d = {0};
static void
{
}
static void
_canvas_resize_cb(Ecore_Evas *ee)
{
int w, h;
}
static const char *
{
switch (mode)
{
return "plain";
return "shadow";
return "outline";
return "soft outline";
return "glow";
return "outline shadow";
return "far shadow";
return "outline soft shadow";
return "soft shadow";
return "far soft shadow";
default:
return "invalid";
}
}
static void
void *einfo)
{
if (strcmp(ev->
key,
"h") == 0)
{
printf("%s", commands);
return;
}
if (strcmp(ev->
key,
"t") == 0)
{
type = (type + 1) % 10;
printf("Changing text's style to \'%s\'\n",
_text_style_type_to_str(type));
return;
}
if (strcmp(ev->
key,
"f") == 0)
{
int sz;
(d.t_data.font_ptr)++;
POINTER_CYCLE(d.t_data.font_ptr, d.t_data.font);
printf("Changing text's font to %s\n",
*d.t_data.font_ptr);
return;
}
if (strcmp(ev->
key,
"b") == 0)
{
(d.t_data.text_ptr)++;
POINTER_CYCLE(d.t_data.text_ptr, d.t_data.text);
d.text, d.t_data.text_ptr->r, d.t_data.text_ptr->g,
d.t_data.text_ptr->b, d.t_data.text_ptr->a);
printf("Changing base color for text to (%d, %d, %d, %d)\n",
d.t_data.text_ptr->r, d.t_data.text_ptr->g,
d.t_data.text_ptr->b, d.t_data.text_ptr->a);
return;
}
if (strcmp(ev->
key,
"g") == 0)
{
(d.t_data.glow2_ptr)++;
POINTER_CYCLE(d.t_data.glow2_ptr, d.t_data.glow2);
d.text, d.t_data.glow2_ptr->r, d.t_data.glow2_ptr->g,
d.t_data.glow2_ptr->b, d.t_data.glow2_ptr->a);
printf("Changing glow 2 color for text to (%d, %d, %d, %d)\n",
d.t_data.glow2_ptr->r, d.t_data.glow2_ptr->g,
d.t_data.glow2_ptr->b, d.t_data.glow2_ptr->a);
return;
}
if (strcmp(ev->
key,
"w") == 0)
{
(d.t_data.glow_ptr)++;
POINTER_CYCLE(d.t_data.glow_ptr, d.t_data.glow);
d.text, d.t_data.glow_ptr->r, d.t_data.glow_ptr->g,
d.t_data.glow_ptr->b, d.t_data.glow_ptr->a);
printf("Changing glow color for text to (%d, %d, %d, %d)\n",
d.t_data.glow_ptr->r, d.t_data.glow_ptr->g,
d.t_data.glow_ptr->b, d.t_data.glow_ptr->a);
return;
}
if (strcmp(ev->
key,
"o") == 0)
{
(d.t_data.outline_ptr)++;
POINTER_CYCLE(d.t_data.outline_ptr, d.t_data.outline);
d.text, d.t_data.outline_ptr->r, d.t_data.outline_ptr->g,
d.t_data.outline_ptr->b, d.t_data.outline_ptr->a);
printf("Changing outline color for text to (%d, %d, %d, %d)\n",
d.t_data.outline_ptr->r, d.t_data.outline_ptr->g,
d.t_data.outline_ptr->b, d.t_data.outline_ptr->a);
return;
}
if (strcmp(ev->
key,
"s") == 0)
{
(d.t_data.shadow_ptr)++;
POINTER_CYCLE(d.t_data.shadow_ptr, d.t_data.shadow);
d.text, d.t_data.shadow_ptr->r, d.t_data.shadow_ptr->g,
d.t_data.shadow_ptr->b, d.t_data.shadow_ptr->a);
printf("Changing shadow color for text to (%d, %d, %d, %d)\n",
d.t_data.shadow_ptr->r, d.t_data.shadow_ptr->g,
d.t_data.shadow_ptr->b, d.t_data.shadow_ptr->a);
return;
}
if (strcmp(ev->
key,
"z") == 0)
{
const char *font;
int size;
size = (size + 10) % 50;
if (!size) size = 10;
printf("Changing text's font size to %d\n", size);
return;
}
}
int
main(void)
{
int size;
const char *font;
return EXIT_FAILURE;
struct text_preset_data init_data =
{
.font = {"DejaVu", "Courier", "Utopia"},
.text = {BLACK, WHITE, GREY, RED, GREEN, BLUE},
.shadow = {WHITE, BLUE, GREEN, RED},
.outline = {WHITE, RED, GREEN, BLUE},
.glow = {WHITE, BLUE, GREEN, RED},
.glow2 = {WHITE, RED, BLUE, GREEN}
};
d.t_data = init_data;
d.t_data.font_ptr = d.t_data.font;
d.t_data.text_ptr = d.t_data.text;
d.t_data.glow_ptr = d.t_data.glow;
d.t_data.glow2_ptr = d.t_data.glow2;
d.t_data.outline_ptr = d.t_data.outline;
d.t_data.shadow_ptr = d.t_data.shadow;
if (!d.ee)
goto error;
d.text, d.t_data.text_ptr->r, d.t_data.text_ptr->g,
d.t_data.text_ptr->b, d.t_data.text_ptr->a);
d.text, d.t_data.glow_ptr->r, d.t_data.glow_ptr->g,
d.t_data.glow_ptr->b, d.t_data.glow_ptr->a);
d.text, d.t_data.glow2_ptr->r, d.t_data.glow2_ptr->g,
d.t_data.glow2_ptr->b, d.t_data.glow2_ptr->a);
d.text, d.t_data.outline_ptr->r, d.t_data.outline_ptr->g,
d.t_data.outline_ptr->b, d.t_data.outline_ptr->a);
d.text, d.t_data.shadow_ptr->r, d.t_data.shadow_ptr->g,
d.t_data.shadow_ptr->b, d.t_data.shadow_ptr->a);
printf("Adding text object with font %s, size %d\n", font, size);
printf("%s", commands);
return 0;
error:
fprintf(stderr, "error: Requires at least one Evas engine built and linked"
" to ecore-evas for this example to run properly.\n");
return -1;
}