Skip to content

Commit

Permalink
Implement SVG legend and scalebar rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
szekerest committed Apr 6, 2015
1 parent f97f607 commit 9ecc2b0
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 35 deletions.
48 changes: 35 additions & 13 deletions maplegend.c
Expand Up @@ -679,7 +679,8 @@ int msEmbedLegend(mapObj *map, imageObj *img)
map->symbolset.numsymbols++;
initSymbol(legendSymbol);

if(!MS_RENDERER_PLUGIN(map->outputformat) || !MS_MAP_RENDERER(map)->supports_pixel_buffer) {
if(!MS_RENDERER_PLUGIN(map->outputformat) || !(MS_MAP_RENDERER(map)->supports_pixel_buffer
|| MS_MAP_RENDERER(map)->supports_svg)) {
imageType = msStrdup(map->imagetype); /* save format */
if MS_DRIVER_CAIRO(map->outputformat)
map->outputformat = msSelectOutputFormat( map, "cairopng" );
Expand All @@ -700,23 +701,44 @@ int msEmbedLegend(mapObj *map, imageObj *img)
}

/* copy renderered legend image into symbol */
legendSymbol->pixmap_buffer = calloc(1,sizeof(rasterBufferObj));
MS_CHECK_ALLOC(legendSymbol->pixmap_buffer, sizeof(rasterBufferObj), MS_FAILURE);
if (MS_MAP_RENDERER(map)->supports_svg) {
int size;
char* svg_text;
legendSymbol->type = MS_SYMBOL_SVG;
svg_text = msSaveImageBuffer(image, &size, map->outputformat);
msFreeImage( image );
if (!svg_text)
return MS_FAILURE;
legendSymbol->svg_text = msSmallMalloc(size + 1);
memcpy(legendSymbol->svg_text, svg_text, size);
legendSymbol->svg_text[size] = 0;
msFree(svg_text);
if(MS_SUCCESS != msPreloadSVGSymbol(legendSymbol))
return MS_FAILURE;
}
else {
legendSymbol->pixmap_buffer = calloc(1,sizeof(rasterBufferObj));
MS_CHECK_ALLOC(legendSymbol->pixmap_buffer, sizeof(rasterBufferObj), MS_FAILURE);

if(MS_SUCCESS != renderer->getRasterBufferCopy(image,legendSymbol->pixmap_buffer))
return MS_FAILURE;
legendSymbol->renderer = renderer;
legendSymbol->renderer_free_func = renderer->freeSymbol;
if(MS_SUCCESS != renderer->getRasterBufferCopy(image,legendSymbol->pixmap_buffer)) {
msFreeImage( image );
return MS_FAILURE;
}

msFreeImage( image );
msFreeImage( image );

if(!legendSymbol->pixmap_buffer) return(-1); /* something went wrong creating scalebar */
if(!legendSymbol->pixmap_buffer) return(-1); /* something went wrong creating scalebar */

legendSymbol->type = MS_SYMBOL_PIXMAP; /* intialize a few things */
legendSymbol->name = msStrdup("legend");
legendSymbol->sizex = legendSymbol->pixmap_buffer->width;
legendSymbol->sizey = legendSymbol->pixmap_buffer->height;
legendSymbol->type = MS_SYMBOL_PIXMAP; /* intialize a few things */
legendSymbol->sizex = legendSymbol->pixmap_buffer->width;
legendSymbol->sizey = legendSymbol->pixmap_buffer->height;
}

legendSymbol->renderer = renderer;
legendSymbol->renderer_free_func = renderer->freeSymbol;

legendSymbol->name = msStrdup("legend");

/* I'm not too sure this test is sufficient ... NFW. */
/* if(map->legend.transparent == MS_ON) */
/* gdImageColorTransparent(legendSymbol->img_deprecated, 0); */
Expand Down
64 changes: 42 additions & 22 deletions mapscale.c
Expand Up @@ -188,7 +188,7 @@ imageObj *msDrawScalebar(mapObj *map)
}

renderer = MS_MAP_RENDERER(map);
if(!renderer || !MS_MAP_RENDERER(map)->supports_pixel_buffer) {
if(!renderer || !(MS_MAP_RENDERER(map)->supports_pixel_buffer || MS_MAP_RENDERER(map)->supports_svg)) {
msSetError(MS_MISCERR, "Outputformat not supported for scalebar", "msDrawScalebar()");
return(NULL);
}
Expand Down Expand Up @@ -385,7 +385,8 @@ int msEmbedScalebar(mapObj *map, imageObj *img)
s = map->symbolset.numsymbols;
map->symbolset.numsymbols++;

if(!MS_RENDERER_PLUGIN(map->outputformat) || !MS_MAP_RENDERER(map)->supports_pixel_buffer) {
if(!MS_RENDERER_PLUGIN(map->outputformat) || !(MS_MAP_RENDERER(map)->supports_pixel_buffer
|| MS_MAP_RENDERER(map)->supports_svg)) {
imageType = msStrdup(map->imagetype); /* save format */
if MS_DRIVER_CAIRO(map->outputformat)
map->outputformat = msSelectOutputFormat( map, "cairopng" );
Expand All @@ -406,46 +407,67 @@ int msEmbedScalebar(mapObj *map, imageObj *img)
if(!image) {
return MS_FAILURE;
}
embededSymbol->pixmap_buffer = calloc(1,sizeof(rasterBufferObj));
MS_CHECK_ALLOC(embededSymbol->pixmap_buffer, sizeof(rasterBufferObj), MS_FAILURE);

if(MS_SUCCESS != renderer->getRasterBufferCopy(image,embededSymbol->pixmap_buffer)) {
return MS_FAILURE;
/* intialize a few things */
embededSymbol->name = msStrdup("scalebar");

if (MS_MAP_RENDERER(map)->supports_svg) {
int size;
char* svg_text;
embededSymbol->type = MS_SYMBOL_SVG;
svg_text = msSaveImageBuffer(image, &size, map->outputformat);
msFreeImage( image );
if (!svg_text)
return MS_FAILURE;
embededSymbol->svg_text = msSmallMalloc(size + 1);
memcpy(embededSymbol->svg_text, svg_text, size);
embededSymbol->svg_text[size] = 0;
msFree(svg_text);
if(MS_SUCCESS != msPreloadSVGSymbol(embededSymbol))
return MS_FAILURE;
}
else {
embededSymbol->pixmap_buffer = calloc(1,sizeof(rasterBufferObj));
MS_CHECK_ALLOC(embededSymbol->pixmap_buffer, sizeof(rasterBufferObj), MS_FAILURE);

embededSymbol->type = MS_SYMBOL_PIXMAP; /* intialize a few things */
embededSymbol->name = msStrdup("scalebar");
embededSymbol->sizex = embededSymbol->pixmap_buffer->width;
embededSymbol->sizey = embededSymbol->pixmap_buffer->height;
if(MS_SUCCESS != renderer->getRasterBufferCopy(image,embededSymbol->pixmap_buffer)) {
msFreeImage( image );
return MS_FAILURE;
}
msFreeImage( image );
embededSymbol->type = MS_SYMBOL_PIXMAP;
embededSymbol->sizex = embededSymbol->pixmap_buffer->width;
embededSymbol->sizey = embededSymbol->pixmap_buffer->height;
}
if(map->scalebar.transparent) {
embededSymbol->transparent = MS_TRUE;
embededSymbol->transparentcolor = 0;
}

switch(map->scalebar.position) {
case(MS_LL):
point.x = MS_NINT(embededSymbol->pixmap_buffer->width/2.0);
point.y = map->height - MS_NINT(embededSymbol->pixmap_buffer->height/2.0);
point.x = MS_NINT(embededSymbol->sizex/2.0);
point.y = map->height - MS_NINT(embededSymbol->sizey/2.0);
break;
case(MS_LR):
point.x = map->width - MS_NINT(embededSymbol->pixmap_buffer->width/2.0);
point.y = map->height - MS_NINT(embededSymbol->pixmap_buffer->height/2.0);
point.x = map->width - MS_NINT(embededSymbol->sizex/2.0);
point.y = map->height - MS_NINT(embededSymbol->sizey/2.0);
break;
case(MS_LC):
point.x = MS_NINT(map->width/2.0);
point.y = map->height - MS_NINT(embededSymbol->pixmap_buffer->height/2.0);
point.y = map->height - MS_NINT(embededSymbol->sizey/2.0);
break;
case(MS_UR):
point.x = map->width - MS_NINT(embededSymbol->pixmap_buffer->width/2.0);
point.y = MS_NINT(embededSymbol->pixmap_buffer->height/2.0);
point.x = map->width - MS_NINT(embededSymbol->sizex/2.0);
point.y = MS_NINT(embededSymbol->sizey/2.0);
break;
case(MS_UL):
point.x = MS_NINT(embededSymbol->pixmap_buffer->width/2.0);
point.y = MS_NINT(embededSymbol->pixmap_buffer->height/2.0);
point.x = MS_NINT(embededSymbol->sizex/2.0);
point.y = MS_NINT(embededSymbol->sizey/2.0);
break;
case(MS_UC):
point.x = MS_NINT(map->width/2.0);
point.y = MS_NINT(embededSymbol->pixmap_buffer->height/2.0);
point.y = MS_NINT(embededSymbol->sizey/2.0);
break;
}

Expand Down Expand Up @@ -500,8 +522,6 @@ int msEmbedScalebar(mapObj *map, imageObj *img)
/* Mark layer as deleted so that it doesn't interfere with html legends or with saving maps */
GET_LAYER(map, l)->status = MS_DELETE;


msFreeImage( image );
return(0);
}

Expand Down

0 comments on commit 9ecc2b0

Please sign in to comment.