diff options
Diffstat (limited to 'libcore/cairoimage.c')
-rw-r--r-- | libcore/cairoimage.c | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/libcore/cairoimage.c b/libcore/cairoimage.c new file mode 100644 index 0000000..a8b56c3 --- /dev/null +++ b/libcore/cairoimage.c @@ -0,0 +1,92 @@ +#include "cairoimage.h" + +cCairoImage::cCairoImage(void) { + surface = NULL; + cr = NULL; +} + +cCairoImage::~cCairoImage() { + if (cr) + cairo_destroy (cr); + if (surface) + cairo_surface_destroy (surface); +} + +void cCairoImage::InitCairoImage(int width, int height) { + this->width = width; + this->height = height; + + surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height); + cr = cairo_create(surface); + cairo_set_antialias(cr, CAIRO_ANTIALIAS_BEST); +} + +void cCairoImage::DrawTextVertical(string text, tColor color, string font, int size) { + + int imgHeight = GetTextWidth(text, font, size); + InitCairoImage(size * 1.2, imgHeight); + + SetColor(color); + cairo_move_to (cr, size, imgHeight); + cairo_font_weight_t fontWeight = CAIRO_FONT_WEIGHT_NORMAL; + cairo_font_slant_t fontSlant = CAIRO_FONT_SLANT_NORMAL; + cairo_select_font_face (cr, font.c_str(), fontSlant, fontWeight); + cairo_set_font_size (cr, size); + cairo_rotate(cr, 3*M_PI/2); + cairo_show_text (cr, text.c_str()); +} + +cImage *cCairoImage::GetImage(void) { + if (!cr || !surface) + return NULL; + + unsigned char *data = cairo_image_surface_get_data(surface); + cImage *image = new cImage(cSize(width, height), (tColor*)data); + return image; +} + +/********************************************************************************** +* Private Functions +**********************************************************************************/ + +int cCairoImage::GetTextWidth(string text, string font, int size) { + cairo_surface_t *tmpSurface; + cairo_t *tmpCr; + + double width = 300; + double height = (double)size *1.3; + + cairo_font_weight_t fontWeight = CAIRO_FONT_WEIGHT_NORMAL; + cairo_font_slant_t fontSlant = CAIRO_FONT_SLANT_NORMAL; + + tmpSurface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height); + tmpCr = cairo_create (tmpSurface); + + cairo_select_font_face (tmpCr, font.c_str(), fontSlant, fontWeight); + cairo_set_font_size (tmpCr, size); + + cairo_text_extents_t te; + cairo_text_extents (tmpCr, text.c_str(), &te); + int textWidth = te.width; + + cairo_destroy (tmpCr); + cairo_surface_destroy (tmpSurface); + + return (double)textWidth * 1.1; +} + +void cCairoImage::SetColor(tColor color) { + if (!cr || !surface) + return; + tIndex tAlpha = (color & 0xFF000000) >> 24; + tIndex tRed = (color & 0x00FF0000) >> 16; + tIndex tGreen = (color & 0x0000FF00) >> 8; + tIndex tBlue = (color & 0x000000FF); + + double a = (int)tAlpha / (double)255; + double r = (int)tRed / (double)255; + double g = (int)tGreen / (double)255; + double b = (int)tBlue / (double)255; + + cairo_set_source_rgba(cr, r, g, b, a); +}
\ No newline at end of file |