mapcairo.c 29.2 KB
Newer Older
1
/******************************************************************************
2
 * $Id: mapcairo.c 11591 2011-04-18 10:13:01Z tbonfort $
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
 *
 * Project:  MapServer
 * Purpose:  Cairo Rendering functions
 * Author:   Thomas Bonfort
 *
 ******************************************************************************
 * Copyright (c) 1996-2009 Regents of the University of Minnesota.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of this Software or works derived from this Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *****************************************************************************/
#include "mapserver.h"

#ifdef USE_CAIRO

#include <cairo.h>
#if defined(_WIN32) && !defined(__CYGWIN__)
#include <cairo-pdf.h>
#include <cairo-svg.h>
#else
#include <cairo/cairo-pdf.h>
#include <cairo/cairo-svg.h>
#endif

# include <cairo-ft.h>
/*
#include <pango/pangocairo.h>
#include <glib.h>
*/
#include <string.h>
/*
#include <ft2build.h>
#include FT_FREETYPE_H
*/

53 54
typedef struct facecacheObj faceCacheObj;
struct facecacheObj {
55 56 57 58
    cairo_font_face_t *face;
    FT_Face ftface;
    char *path;
    faceCacheObj *next;
59
    cairo_user_data_key_t facekey;
60 61 62
};

int freeFaceCache(faceCacheObj *fc) {
63 64 65
    //printf("***\nface has %d references\n***\n",cairo_font_face_get_reference_count(fc->face));
    cairo_font_face_destroy(fc->face);
    free(fc->path);
66 67 68 69 70
    return MS_SUCCESS;
}

typedef struct {
    faceCacheObj *facecache;
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    FT_Library library;
    /* dummy surface and context */
    unsigned char dummydata[4];
    cairo_surface_t *dummysurface;
    cairo_t *dummycr;
} cairoCacheData;

void initializeCache(void **vcache) {
   cairoCacheData *cache = (cairoCacheData*)malloc(sizeof(cairoCacheData));
   *vcache = cache;
       
    cache->facecache = NULL;
    FT_Init_FreeType(&(cache->library));
    /* dummy surface and context */
    cache->dummysurface = cairo_image_surface_create_for_data(cache->dummydata, CAIRO_FORMAT_ARGB32, 1,1,4);
    cache->dummycr = cairo_create(cache->dummysurface);
}

int cleanupCairo(void *cache) {
    cairoCacheData *ccache = (cairoCacheData*)cache;
    
    if(ccache->dummycr) {
        cairo_destroy(ccache->dummycr);
    }
    if(ccache->dummysurface) {
        cairo_surface_destroy(ccache->dummysurface);
    }
    if(ccache->facecache) {
99
        faceCacheObj *next,*cur;
100
        cur = ccache->facecache;
101 102
        do {
            next = cur->next;
103
            freeFaceCache(cur);
104 105 106
            free(cur);
            cur=next;
        } while(cur);
107 108 109 110
    }
    if(ccache->library)
        FT_Done_FreeType(ccache->library);
    free(ccache);
111 112 113
    return MS_SUCCESS;
}

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130




typedef struct {
    cairo_surface_t *surface;
    cairo_t *cr;
    bufferObj *outputStream;
    int use_alpha;
} cairo_renderer;


#define CAIRO_RENDERER(im) ((cairo_renderer*)(im->img.plugin))


int freeImageCairo(imageObj *img) {
    cairo_renderer *r = CAIRO_RENDERER(img);
131 132
    if(r) {
        cairo_destroy(r->cr);
133 134
        cairo_surface_finish(r->surface);
        cairo_surface_destroy(r->surface);
135 136 137 138
        if(r->outputStream) {
            msBufferFree(r->outputStream);
            free(r->outputStream);
        }
139
        free(r);
140
    }
141
    return MS_SUCCESS;
142 143 144
}


145
faceCacheObj *getFontFace(cairoCacheData *cache, char *font) {
146
    faceCacheObj *newface = NULL;
147
    faceCacheObj *cur=cache->facecache;
148
    while(cur) {
149 150
        if(!strcmp(cur->path,font))
            return cur;
151 152 153 154
        cur = cur->next;
    }
    newface = malloc(sizeof(faceCacheObj));
    
155 156 157 158 159 160 161
    if(FT_New_Face(cache->library, font, 0, &(newface->ftface))) {
        msSetError(MS_RENDERERERR,"Freetype failed to open font %s","getFontFace()",font);
        free(newface);
        return NULL;
    }
    newface->next = cache->facecache;
    cache->facecache = newface;
162 163
    newface->face = cairo_ft_font_face_create_for_ft_face(newface->ftface, 0);

164
    cairo_font_face_set_user_data (newface->face, &newface->facekey,
165 166
            &(newface->ftface), (cairo_destroy_func_t) FT_Done_Face);

167 168
    newface->path = msStrdup(font);
    return newface;
169 170
}

171
#define msCairoSetSourceColor(cr, c) cairo_set_source_rgba((cr),(c)->red/255.0,(c)->green/255.0,(c)->blue/255.0,(c)->alpha/255.0);
172

173 174 175 176
int renderLineCairo(imageObj *img, shapeObj *p, strokeStyleObj *stroke) {
    int i,j;
    cairo_renderer *r = CAIRO_RENDERER(img);
    assert(stroke->color);
177
    cairo_new_path(r->cr);
178 179 180 181 182 183 184 185 186 187 188
    msCairoSetSourceColor(r->cr,stroke->color);
    for(i=0;i<p->numlines;i++) {
        lineObj *l = &(p->line[i]);
        cairo_move_to(r->cr,l->point[0].x,l->point[0].y);
        for(j=1;j<l->numpoints;j++) {
            cairo_line_to(r->cr,l->point[j].x,l->point[j].y);
        }
    }
    if(stroke->patternlength>0) {
        cairo_set_dash(r->cr,stroke->pattern,stroke->patternlength,0);
    }
189 190 191 192 193 194 195 196 197 198 199 200
    switch(stroke->linecap) {
        case MS_CJC_BUTT:
            cairo_set_line_cap(r->cr,CAIRO_LINE_CAP_BUTT);
            break;
        case MS_CJC_SQUARE:
            cairo_set_line_cap(r->cr,CAIRO_LINE_CAP_SQUARE);
            break;
        case MS_CJC_ROUND:
        case MS_CJC_NONE:
        default:
            cairo_set_line_cap(r->cr,CAIRO_LINE_CAP_ROUND);
    }
201 202 203 204 205 206
    cairo_set_line_width (r->cr, stroke->width);
    cairo_stroke (r->cr);
    if(stroke->patternlength>0) {
        cairo_set_dash(r->cr,stroke->pattern,0,0);
    }
    return MS_SUCCESS;
207 208
}

209 210 211
int renderPolygonCairo(imageObj *img, shapeObj *p, colorObj *c) {
    cairo_renderer *r = CAIRO_RENDERER(img);
    int i,j;
212 213
    cairo_new_path(r->cr);
    cairo_set_fill_rule(r->cr,CAIRO_FILL_RULE_EVEN_ODD);
214 215 216 217 218 219 220 221 222 223 224
    msCairoSetSourceColor(r->cr,c);
    for(i=0;i<p->numlines;i++) {
        lineObj *l = &(p->line[i]);
        cairo_move_to(r->cr,l->point[0].x,l->point[0].y);
        for(j=1;j<l->numpoints;j++) {
            cairo_line_to(r->cr,l->point[j].x,l->point[j].y);
        }
        cairo_close_path(r->cr);
    }
    cairo_fill(r->cr);
    return MS_SUCCESS;
225 226
}

227
int renderPolygonTiledCairo(imageObj *img, shapeObj *p,  imageObj *tile) {
228
    int i,j;
229 230 231
    cairo_renderer *r = CAIRO_RENDERER(img);
    cairo_renderer *tileRenderer = CAIRO_RENDERER(tile);
    cairo_pattern_t *pattern = cairo_pattern_create_for_surface(tileRenderer->surface);
232
    cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
233
    
234 235 236 237 238 239 240 241 242 243
    cairo_set_source(r->cr, pattern);
    for (i = 0; i < p->numlines; i++) {
      lineObj *l = &(p->line[i]);
      cairo_move_to(r->cr, l->point[0].x, l->point[0].y);
      for (j = 1; j < l->numpoints; j++) {
        cairo_line_to(r->cr, l->point[j].x, l->point[j].y);
      }
      //cairo_close_path(r->cr);
    }
    cairo_fill(r->cr);
244 245
    cairo_pattern_destroy(pattern);
    return MS_SUCCESS;
246 247 248
}

cairo_surface_t *createSurfaceFromBuffer(rasterBufferObj *b) {
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
    assert(b->type == MS_BUFFER_BYTE_RGBA);
    return cairo_image_surface_create_for_data (b->data.rgba.pixels,
                                                CAIRO_FORMAT_ARGB32,
                                                b->width,
                                                b->height,
                                                b->data.rgba.row_step);
}


int renderPixmapSymbolCairo(imageObj *img, double x, double y,symbolObj *symbol,
                            symbolStyleObj *style) {
   cairo_renderer *r = CAIRO_RENDERER(img);
   cairo_surface_t *im;
   rasterBufferObj *b = symbol->pixmap_buffer;
   assert(b);
   if(!symbol->renderer_cache) {
      symbol->renderer_cache = (void*)createSurfaceFromBuffer(b);
   }
   assert(symbol->renderer_cache);
   im=(cairo_surface_t*)symbol->renderer_cache;
   cairo_save(r->cr);
   if(style->rotation != 0 || style-> scale != 1) {
      cairo_translate (r->cr, x,y);
      cairo_rotate (r->cr, -style->rotation);
      cairo_scale  (r->cr, style->scale,style->scale);
      cairo_translate (r->cr, -0.5*b->width, -0.5*b->height);
   } else {
       cairo_translate (r->cr, MS_NINT(x-0.5*b->width),MS_NINT(y-0.5*b->height));
   }
   cairo_set_source_surface (r->cr, im, 0, 0);
   cairo_paint (r->cr);
   cairo_restore(r->cr);
   return MS_SUCCESS;
}

int renderVectorSymbolCairo(imageObj *img, double x, double y, symbolObj *symbol,
                            symbolStyleObj *style) {
    cairo_renderer *r = CAIRO_RENDERER(img);
    double ox=symbol->sizex*0.5,oy=symbol->sizey*0.5;
    int is_new = 1,i;
    cairo_new_path(r->cr);
    cairo_save(r->cr);
    cairo_translate(r->cr,x,y);
    cairo_scale(r->cr,style->scale,style->scale);
    cairo_rotate(r->cr,-style->rotation);
    cairo_translate(r->cr,-ox,-oy);
    for (i = 0; i < symbol->numpoints; i++) {
        if ((symbol->points[i].x == -99) && (symbol->points[i].y == -99)) { // (PENUP)
            is_new = 1;
        } else {
            if (is_new) {
                cairo_move_to(r->cr,symbol->points[i].x,symbol->points[i].y);
                is_new = 0;
            } else {
                cairo_line_to(r->cr,symbol->points[i].x,symbol->points[i].y);
            }
        }
    }
307
    cairo_restore(r->cr);
308 309 310 311 312 313 314 315 316 317 318
    if(style->color) {
        msCairoSetSourceColor(r->cr,style->color);
        cairo_fill_preserve(r->cr);
    }
    if(style->outlinewidth>0) {
        msCairoSetSourceColor(r->cr,style->outlinecolor);
        cairo_set_line_width (r->cr, style->outlinewidth);
        cairo_stroke_preserve(r->cr);
    }
    cairo_new_path(r->cr);
    return MS_SUCCESS;
319 320
}

321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
int renderTruetypeSymbolCairo(imageObj *img, double x, double y, symbolObj *symbol,
                              symbolStyleObj *s) {
   int unicode;
   cairo_glyph_t glyph;
   cairo_text_extents_t extents;

   cairo_matrix_t trans;
   double ox,oy;
   cairoCacheData *cache = MS_IMAGE_RENDERER_CACHE(img);
   cairo_renderer *r = CAIRO_RENDERER(img);
   faceCacheObj *face = getFontFace(cache,symbol->full_font_path);

   if(!face) return MS_FAILURE;

   cairo_save(r->cr); 
   cairo_set_font_face(r->cr,face->face);
   cairo_set_font_size(r->cr,s->scale*96/72.0);


   msUTF8ToUniChar(symbol->character, &unicode);
   glyph.index = FT_Get_Char_Index(face->ftface, unicode);
   glyph.x=0;
   glyph.y=0;
   cairo_glyph_extents(r->cr,&glyph,1,&extents);
   ox=extents.x_bearing+extents.width/2.;
   oy=extents.y_bearing+extents.height/2.;
347 348 349



350
   cairo_matrix_init_rotate(&trans,-s->rotation);
351

352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
   cairo_matrix_transform_point(&trans,&ox,&oy);
   //cairo_translate(cr,-extents.width/2,-extents.height/2);

   cairo_translate(r->cr,x-ox,y-oy);
   cairo_rotate(r->cr, -s->rotation);

   cairo_glyph_path(r->cr,&glyph,1);

   if (s->outlinewidth) {
      msCairoSetSourceColor(r->cr, s->outlinecolor);
      cairo_set_line_width(r->cr, s->outlinewidth + 1);
      cairo_stroke_preserve(r->cr);
   }
   if(s->color) {
       msCairoSetSourceColor(r->cr, s->color);
       cairo_fill_preserve(r->cr);
   }
   cairo_new_path(r->cr);
   cairo_restore(r->cr);
   return MS_SUCCESS;
}

int renderTileCairo(imageObj *img, imageObj *tile, double x, double y) {
    cairo_renderer *r = CAIRO_RENDERER(img);
    cairo_surface_t *im = CAIRO_RENDERER(tile)->surface;
    int w = cairo_image_surface_get_width (im);
    int h = cairo_image_surface_get_height (im);
    cairo_save(r->cr);
    cairo_translate(r->cr, MS_NINT(x-0.5 * w), MS_NINT(y -0.5 * h));
    cairo_set_source_surface(r->cr, im, 0, 0);
    cairo_pattern_set_filter (cairo_get_source (r->cr), CAIRO_FILTER_NEAREST);
    cairo_paint(r->cr);
    cairo_restore(r->cr);
    return MS_SUCCESS;
}

#define CAIROLINESPACE 1.33

int getTruetypeTextBBoxCairo(rendererVTableObj *renderer, char *font, double size, char *text, rectObj *rect, double **advances) {
    
    
    cairoCacheData *cache = MS_RENDERER_CACHE(renderer);
    faceCacheObj *face = getFontFace(cache,font);
 
396 397 398 399 400 401 402 403
    char *utfptr=text;
    int i,has_kerning,unicode;
    unsigned long previdx=0;
    int numglyphs = msGetNumGlyphs(text);
    cairo_glyph_t glyph;
    cairo_text_extents_t extents;
    double px=0,py=0;

404 405 406 407 408 409
    if(face == NULL) {
        return MS_FAILURE;
    }

    cairo_set_font_face(cache->dummycr,face->face);
    cairo_set_font_size(cache->dummycr,size*96/72.0);
410

411
    has_kerning = FT_HAS_KERNING((face->ftface));
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426

    if(advances != NULL) {
        *advances = (double*)malloc(numglyphs*sizeof(double));
    }

    for(i=0;i<numglyphs;i++) {
        utfptr+=msUTF8ToUniChar(utfptr, &unicode);
        glyph.x=px;
        glyph.y=py;
        if(unicode=='\n') {
            py += ceil(size*CAIROLINESPACE);
            px = 0;
            previdx=0;
            continue;
        }
427
        glyph.index = FT_Get_Char_Index(face->ftface, unicode);
428 429
        if( has_kerning && previdx ) {
            FT_Vector delta;
430
            FT_Get_Kerning( face->ftface, previdx, glyph.index, FT_KERNING_DEFAULT, &delta );
431 432
            px += delta.x / 64.;
        }
433
        cairo_glyph_extents(cache->dummycr,&glyph,1,&extents);
434
        
435 436 437 438 439 440 441 442 443 444 445
        if(i==0) {
            rect->minx = px+extents.x_bearing;
            rect->miny = py+extents.y_bearing;
            rect->maxx = px+extents.x_bearing+extents.width;
            rect->maxy = py+extents.y_bearing+extents.height;
        } else {
            rect->minx = MS_MIN(rect->minx,px+extents.x_bearing);
            rect->miny = MS_MIN(rect->miny,py+extents.y_bearing);
            rect->maxy = MS_MAX(rect->maxy,py+extents.y_bearing+extents.height);
            rect->maxx = MS_MAX(rect->maxx,px+extents.x_bearing+extents.width);
        }
446 447 448 449 450
        if(advances!=NULL)
            (*advances)[i]=extents.x_advance;
        px += extents.x_advance;
        previdx=glyph.index;
    }
451 452 453 454 455 456 457
    /*
    rect->minx = 0;
    rect->miny = 0;
    rect->maxx = 1;
    rect->maxy = 1;
    */
    return MS_SUCCESS;
458 459
}

460 461 462 463
int renderGlyphsCairo(imageObj *img,double x, double y, labelStyleObj *style, char *text) {
    cairo_renderer *r = CAIRO_RENDERER(img);
    cairoCacheData *cache = MS_IMAGE_RENDERER_CACHE(img);
    faceCacheObj *face = getFontFace(cache,style->font);
464 465 466 467 468 469 470 471 472

    char *utfptr=text;
    int i,has_kerning,unicode;
    unsigned long previdx=0;
    int numglyphs = msGetNumGlyphs(text);
    cairo_glyph_t glyph;
    cairo_text_extents_t extents;
    double px=0,py=0;

473 474 475 476 477
    if(face == NULL) {
        return MS_FAILURE;
    }

    cairo_set_font_face(r->cr,face->face);
478 479 480
    cairo_set_font_size(r->cr,style->size*96/72.0);

    cairo_save(r->cr);
481 482 483
    cairo_translate(r->cr,MS_NINT(x),MS_NINT(y));
    if(style->rotation != 0.0)
       cairo_rotate(r->cr, -style->rotation);
484

485
    has_kerning = FT_HAS_KERNING((face->ftface));
486 487 488 489 490 491 492 493 494 495
    for(i=0;i<numglyphs;i++) {
        utfptr+=msUTF8ToUniChar(utfptr, &unicode);
        glyph.x=px;
        glyph.y=py;
        if(unicode=='\n') {
            py += ceil(style->size*CAIROLINESPACE);
            px = 0;
            previdx=0;
            continue;
        }
496
        glyph.index = FT_Get_Char_Index(face->ftface, unicode);
497 498
        if( has_kerning && previdx ) {
            FT_Vector delta;
499
            FT_Get_Kerning( face->ftface, previdx, glyph.index, FT_KERNING_DEFAULT, &delta );
500 501 502 503 504 505 506 507 508 509
            px += delta.x / 64.;
        }
        cairo_glyph_extents(r->cr,&glyph,1,&extents);
        cairo_glyph_path(r->cr,&glyph,1);
        px += extents.x_advance;
        previdx=glyph.index;
    }

    if (style->outlinewidth > 0) {
        cairo_save(r->cr);
510 511
        msCairoSetSourceColor(r->cr, style->outlinecolor);
        cairo_set_line_width(r->cr, style->outlinewidth + 1);
512 513 514
        cairo_stroke_preserve(r->cr);
        cairo_restore(r->cr);
    }
515 516 517
    if(style->color) {
        msCairoSetSourceColor(r->cr, style->color);
        cairo_fill(r->cr);
518
    }
519 520 521
    cairo_new_path(r->cr);
    cairo_restore(r->cr);
    return MS_SUCCESS;
522 523 524 525 526 527 528 529 530
}


cairo_status_t _stream_write_fn(void *b, const unsigned char *data, unsigned int length) {
    msBufferAppend((bufferObj*)b,(void*)data,length);
    return CAIRO_STATUS_SUCCESS;
}

imageObj* createImageCairo(int width, int height, outputFormatObj *format,colorObj* bg) {
531 532 533 534 535 536 537 538 539 540 541
    imageObj *image = NULL;
    cairo_renderer *r=NULL;
    if (format->imagemode != MS_IMAGEMODE_RGB && format->imagemode!= MS_IMAGEMODE_RGBA) {
        msSetError(MS_MISCERR,
                   "Cairo driver only supports RGB or RGBA pixel models.","msImageCreateCairo()");
        return image;
    }
    if (width > 0 && height > 0) {
        image = (imageObj *) calloc(1, sizeof(imageObj));
        r = (cairo_renderer*)calloc(1,sizeof(cairo_renderer));
        if(!strcasecmp(format->driver,"cairo/pdf")) {
542 543
            r->outputStream = (bufferObj*)malloc(sizeof(bufferObj));
            msBufferInit(r->outputStream);
544
            r->surface = cairo_pdf_surface_create_for_stream(
545 546 547
                    _stream_write_fn,
                    r->outputStream,
                    width,height);
548 549
        } else if(!strcasecmp(format->driver,"cairo/svg")) {
            r->outputStream = (bufferObj*)malloc(sizeof(bufferObj));
550 551 552 553 554
            msBufferInit(r->outputStream);
            r->surface = cairo_svg_surface_create_for_stream(
                    _stream_write_fn,
                    r->outputStream,
                    width,height);
555 556 557 558 559 560 561 562 563 564
        } else if(!strcasecmp(format->driver,"cairo/winGDI") && format->device) {
#if CAIRO_HAS_WIN32_SURFACE
            r->outputStream = NULL;
            r->surface = cairo_win32_surface_create(format->device);
#else
            msSetError(MS_RENDERERERR, "Cannot create cairo image. Cairo was not compiled with support for the win32 backend.",
             "msImageCreateCairo()");
#endif
        } else if(!strcasecmp(format->driver,"cairo/winGDIPrint") && format->device) {
#if CAIRO_HAS_WIN32_SURFACE
565
            r->outputStream = NULL;
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
            r->surface = cairo_win32_printing_surface_create(format->device);
#else
            msSetError(MS_RENDERERERR, "Cannot create cairo image. Cairo was not compiled with support for the win32 backend.",
             "msImageCreateCairo()");
#endif
        } else {
            r->outputStream = NULL;
            r->surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
        }
        r->cr = cairo_create(r->surface);
        if(format->transparent || !bg || !MS_VALID_COLOR(*bg)) {
           r->use_alpha = 1;
           cairo_set_source_rgba (r->cr, 0,0,0,0);
        } else {
           r->use_alpha = 0;
           msCairoSetSourceColor(r->cr,bg);
        }
        cairo_save (r->cr);
        cairo_set_operator (r->cr, CAIRO_OPERATOR_SOURCE);
        cairo_paint (r->cr);
        cairo_restore (r->cr);

        cairo_set_line_cap (r->cr,CAIRO_LINE_CAP_ROUND);
        cairo_set_line_join(r->cr,CAIRO_LINE_JOIN_ROUND);
        image->img.plugin = (void*)r;
    } else {
       msSetError(MS_RENDERERERR, "Cannot create cairo image of size %dx%d.",
                  "msImageCreateCairo()", width, height);
    }
    return image;
596 597 598
}

int saveImageCairo(imageObj *img, FILE *fp, outputFormatObj *format) {
599 600
    cairo_renderer *r = CAIRO_RENDERER(img);
    if(!strcasecmp(img->format->driver,"cairo/pdf") || !strcasecmp(img->format->driver,"cairo/svg")) {
601 602
        cairo_surface_finish (r->surface);
        fwrite(r->outputStream->data,r->outputStream->size,1,fp);
603
    } else {
604
        // not supported
605 606
    }
    return MS_SUCCESS;
607 608 609
}

void *msCreateTileEllipseCairo(double width, double height, double angle,
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
                               colorObj *c, colorObj *bc, colorObj *oc, double ow) {
    double s = (MS_MAX(width,height)+ow)*1.5;
    cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
                                                           s,s);
    cairo_t *cr = cairo_create(surface);
    //cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
    //cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
    if (bc && MS_VALID_COLOR(*bc)) {
        msCairoSetSourceColor(cr, bc);
        cairo_paint(cr);
    }
    cairo_save(cr);
    cairo_translate(cr,s/2,s/2);
    cairo_rotate(cr, -angle);
    cairo_scale(cr, width/2,height/2);
    cairo_arc(cr, 0, 0, 1, 0, 2 * MS_PI);
    cairo_restore(cr);
    if (c != NULL && MS_VALID_COLOR(*c)) {
        msCairoSetSourceColor(cr, c);
        cairo_fill_preserve(cr);
    }
    if (oc != NULL && MS_VALID_COLOR(*oc)) {
        cairo_set_line_width(cr, ow);
        msCairoSetSourceColor(cr, oc);
        cairo_stroke_preserve(cr);
    }
    cairo_new_path(cr);
    cairo_destroy(cr);
    return surface;

}

int renderEllipseSymbolCairo(imageObj *img, double x, double y, symbolObj *symbol,
                             symbolStyleObj *style) {
    cairo_renderer *r = CAIRO_RENDERER(img);
    
    cairo_save(r->cr);
    cairo_set_line_cap(r->cr, CAIRO_LINE_CAP_BUTT);
    cairo_set_line_join(r->cr, CAIRO_LINE_JOIN_MITER);
    cairo_translate(r->cr,x,y);
    cairo_rotate(r->cr,-style->rotation);
    cairo_scale(r->cr,symbol->sizex*style->scale/2,symbol->sizey*style->scale/2);
    cairo_arc (r->cr, 0,0,1, 0, 2 * MS_PI);
    cairo_restore(r->cr);
    if(style->color) {
        msCairoSetSourceColor(r->cr, style->color);
        cairo_fill_preserve(r->cr);
    }
    if(style->outlinewidth > 0) {
        cairo_set_line_width (r->cr, style->outlinewidth);
        msCairoSetSourceColor(r->cr, style->outlinecolor);
        cairo_stroke_preserve(r->cr);
    }
    cairo_new_path(r->cr);
    return MS_SUCCESS;
}



int startLayerVectorCairo(imageObj *img, mapObj *map, layerObj *layer) {
   cairo_renderer *r = CAIRO_RENDERER(img);
   cairo_push_group (r->cr);
   return MS_SUCCESS;
}

int closeLayerVectorCairo(imageObj *img, mapObj *map, layerObj *layer) {
   cairo_renderer *r = CAIRO_RENDERER(img);
   cairo_pop_group_to_source (r->cr);
   cairo_paint_with_alpha (r->cr, layer->opacity*0.01);
   return MS_SUCCESS;
}

int startLayerRasterCairo(imageObj *img, mapObj *map, layerObj *layer) {
    return MS_SUCCESS;
}

int closeLayerRasterCairo(imageObj *img, mapObj *map, layerObj *layer) {
    return MS_SUCCESS;
}


int getRasterBufferHandleCairo(imageObj *img, rasterBufferObj *rb) {
  unsigned char *pb;
    cairo_renderer *r = CAIRO_RENDERER(img);
    rb->type = MS_BUFFER_BYTE_RGBA;
    pb = cairo_image_surface_get_data(r->surface);
    rb->data.rgba.pixels = pb;
    rb->data.rgba.row_step = cairo_image_surface_get_stride(r->surface);
    rb->data.rgba.pixel_step=4;
    rb->width = cairo_image_surface_get_width(r->surface);
    rb->height = cairo_image_surface_get_height(r->surface);
    rb->data.rgba.r = &(pb[2]);
    rb->data.rgba.g = &(pb[1]);
    rb->data.rgba.b = &(pb[0]);
    if(r->use_alpha)
       rb->data.rgba.a = &(pb[3]);
    else
       rb->data.rgba.a = NULL;
    return MS_SUCCESS;
}

int getRasterBufferCopyCairo(imageObj *img, rasterBufferObj *rb) {
    cairo_renderer *r = CAIRO_RENDERER(img);
    unsigned char *pb;
    rb->type = MS_BUFFER_BYTE_RGBA;
    rb->data.rgba.row_step = cairo_image_surface_get_stride(r->surface);
    rb->data.rgba.pixel_step=4;
717 718
    rb->width = cairo_image_surface_get_width(r->surface);
    rb->height = cairo_image_surface_get_height(r->surface);
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
    pb = (unsigned char*)malloc(rb->height * rb->data.rgba.row_step * sizeof(unsigned char*));
    memcpy(pb,cairo_image_surface_get_data(r->surface),rb->height * rb->data.rgba.row_step);
    rb->data.rgba.pixels = pb;
    rb->data.rgba.r = &(pb[2]);
    rb->data.rgba.g = &(pb[1]);
    rb->data.rgba.b = &(pb[0]);
    if(r->use_alpha)
       rb->data.rgba.a = &(pb[3]);
    else
       rb->data.rgba.a = NULL;
    return MS_SUCCESS;
}



int mergeRasterBufferCairo(imageObj *img, rasterBufferObj *rb, double opacity,
                           int srcX, int srcY, int dstX, int dstY, 
                           int width, int height) {
    cairo_surface_t *src;
    cairo_renderer *r;
    //not implemented for src,dst,width and height;
    if(!rb->type == MS_BUFFER_BYTE_RGBA) {
        return MS_FAILURE;
    }
    r = CAIRO_RENDERER(img);

    src = cairo_image_surface_create_for_data(rb->data.rgba.pixels,CAIRO_FORMAT_ARGB32,
                                              rb->width,rb->height,
                                              rb->data.rgba.row_step);
    
    if(dstX||dstY||srcX||srcY||width!=img->width||height!=img->height) {
        cairo_set_source_surface (r->cr, src, dstX - srcX, dstY - srcY);
        cairo_rectangle (r->cr, dstX, dstY, width, height);
        cairo_fill (r->cr);
753
    } else {
754 755
        cairo_set_source_surface (r->cr,src,0,0);
        cairo_paint_with_alpha(r->cr,opacity);
756
    }
757 758 759
    cairo_surface_finish(src);
    cairo_surface_destroy(src);
    return MS_SUCCESS;
760 761
}

762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
int freeSymbolCairo(symbolObj *s) {
    if(!s->renderer_cache)
        return MS_SUCCESS;
    switch(s->type) {
    case MS_SYMBOL_VECTOR:
        cairo_path_destroy(s->renderer_cache);
        break;
    case MS_SYMBOL_PIXMAP:
        cairo_surface_finish(s->renderer_cache);
        cairo_surface_destroy(s->renderer_cache);
        break;
    }
    s->renderer_cache=NULL;
    return MS_SUCCESS;
}

int initializeRasterBufferCairo(rasterBufferObj *rb, int width, int height, int mode) {
    rb->type = MS_BUFFER_BYTE_RGBA;
    rb->width = width;
    rb->height = height;
    rb->data.rgba.pixel_step = 4;
    rb->data.rgba.row_step = width * 4;
    rb->data.rgba.pixels = (unsigned char*)calloc(width*height*4,sizeof(unsigned char));
    rb->data.rgba.r = &(rb->data.rgba.pixels[2]);
    rb->data.rgba.g = &(rb->data.rgba.pixels[1]);
    rb->data.rgba.b = &(rb->data.rgba.pixels[0]);
    rb->data.rgba.a = &(rb->data.rgba.pixels[3]);
   return MS_SUCCESS;
790 791 792 793 794 795 796 797
}

#endif /*USE_CAIRO*/


int msPopulateRendererVTableCairoRaster( rendererVTableObj *renderer ) {
#ifdef USE_CAIRO
    renderer->supports_pixel_buffer=1;
798 799 800 801 802 803
    renderer->supports_transparent_layers = 0;
    renderer->default_transform_mode = MS_TRANSFORM_SIMPLIFY;
    initializeCache(&MS_RENDERER_CACHE(renderer));
    renderer->startLayer = startLayerRasterCairo;
    renderer->endLayer = closeLayerRasterCairo;
    renderer->renderLineTiled = NULL;
804 805 806
    renderer->renderLine=&renderLineCairo;
    renderer->createImage=&createImageCairo;
    renderer->saveImage=&saveImageCairo;
807 808
    renderer->getRasterBufferHandle=&getRasterBufferHandleCairo;
    renderer->getRasterBufferCopy=&getRasterBufferCopyCairo;
809 810 811 812 813 814 815 816 817 818
    renderer->renderPolygon=&renderPolygonCairo;
    renderer->renderGlyphs=&renderGlyphsCairo;
    renderer->freeImage=&freeImageCairo;
    renderer->renderEllipseSymbol = &renderEllipseSymbolCairo;
    renderer->renderVectorSymbol = &renderVectorSymbolCairo;
    renderer->renderTruetypeSymbol = &renderTruetypeSymbolCairo;
    renderer->renderPixmapSymbol = &renderPixmapSymbolCairo;
    renderer->mergeRasterBuffer = &mergeRasterBufferCairo;
    renderer->getTruetypeTextBBox = &getTruetypeTextBBoxCairo;
    renderer->renderTile = &renderTileCairo;
819
    renderer->loadImageFromFile = &msLoadMSRasterBufferFromFile;
820 821
    renderer->renderPolygonTiled = &renderPolygonTiledCairo;
    renderer->freeSymbol = &freeSymbolCairo;
822
    renderer->cleanup = &cleanupCairo;
823 824 825 826 827 828 829 830 831 832
    return MS_SUCCESS;
#else
    msSetError(MS_MISCERR, "Cairo Driver requested but is not built in", 
            "msPopulateRendererVTableCairoRaster()");
    return MS_FAILURE;
#endif
}

inline int populateRendererVTableCairoVector( rendererVTableObj *renderer ) {
#ifdef USE_CAIRO
833
    renderer->use_imagecache=0;
834 835
    renderer->supports_pixel_buffer=0;
    renderer->supports_transparent_layers = 1;
836 837 838 839
    renderer->default_transform_mode = MS_TRANSFORM_SIMPLIFY;
    initializeCache(&MS_RENDERER_CACHE(renderer));
    renderer->startLayer = startLayerVectorCairo;
    renderer->endLayer = closeLayerVectorCairo;
840
    renderer->renderLine=&renderLineCairo;
841
    renderer->renderLineTiled = NULL;
842 843
    renderer->createImage=&createImageCairo;
    renderer->saveImage=&saveImageCairo;
844
    renderer->getRasterBufferHandle=&getRasterBufferHandleCairo;
845 846 847 848 849 850 851
    renderer->renderPolygon=&renderPolygonCairo;
    renderer->renderGlyphs=&renderGlyphsCairo;
    renderer->freeImage=&freeImageCairo;
    renderer->renderEllipseSymbol = &renderEllipseSymbolCairo;
    renderer->renderVectorSymbol = &renderVectorSymbolCairo;
    renderer->renderTruetypeSymbol = &renderTruetypeSymbolCairo;
    renderer->renderPixmapSymbol = &renderPixmapSymbolCairo;
852
    renderer->loadImageFromFile = &msLoadMSRasterBufferFromFile;
853
    renderer->mergeRasterBuffer = &mergeRasterBufferCairo;
854
    renderer->initializeRasterBuffer = initializeRasterBufferCairo;
855 856 857 858
    renderer->getTruetypeTextBBox = &getTruetypeTextBBoxCairo;
    renderer->renderTile = &renderTileCairo;
    renderer->renderPolygonTiled = &renderPolygonTiledCairo;
    renderer->freeSymbol = &freeSymbolCairo;
859
    renderer->cleanup = &cleanupCairo;
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
    return MS_SUCCESS;
#else
    msSetError(MS_MISCERR, "Cairo Driver requested but is not built in", 
            "msPopulateRendererVTableCairoRaster()");
    return MS_FAILURE;
#endif
}

int msPopulateRendererVTableCairoSVG( rendererVTableObj *renderer ) {
    return populateRendererVTableCairoVector(renderer);
}
int msPopulateRendererVTableCairoPDF( rendererVTableObj *renderer ) {
    return populateRendererVTableCairoVector(renderer);
}