[mapserver-commits] r7944 - trunk/mapserver
svn at osgeo.org
svn at osgeo.org
Fri Oct 3 11:21:25 EDT 2008
Author: tbonfort
Date: 2008-10-03 11:21:25 -0400 (Fri, 03 Oct 2008)
New Revision: 7944
Modified:
trunk/mapserver/mapagg.cpp
trunk/mapserver/mapgd.c
trunk/mapserver/mapgraticule.c
trunk/mapserver/maplabel.c
trunk/mapserver/mapscale.c
trunk/mapserver/mapserver.h
trunk/mapserver/mapsvg.c
Log:
refactor the label size computation functions (avoids duplicating the fontset handling, and the baseline adjustments) (#2390)
Modified: trunk/mapserver/mapagg.cpp
===================================================================
--- trunk/mapserver/mapagg.cpp 2008-10-03 13:37:46 UTC (rev 7943)
+++ trunk/mapserver/mapagg.cpp 2008-10-03 15:21:25 UTC (rev 7944)
@@ -716,10 +716,10 @@
fy += glyph->advance_y;
}
}
- rect->minx--;
+ /*rect->minx--;
rect->miny--;
rect->maxx++;
- rect->maxy++;
+ rect->maxy++;*/
return MS_SUCCESS;
}
@@ -2032,56 +2032,30 @@
}
-int msGetLabelSizeAGG(imageObj *img, char *string, labelObj *label,
- rectObj *rect, fontSetObj *fontset, double scalefactor,
- int adjustBaseline) {
- if (label->type==MS_TRUETYPE) {
+int msGetTruetypeTextBBoxAGG(imageObj *img, char *font, int size, char *string, rectObj *rect) {
AGGMapserverRenderer* ren = getAGGRenderer(img);
- int size;
- size = MS_NINT(label->size*scalefactor);
- size = MS_MAX(size, label->minsize);
- size = MS_MIN(size, label->maxsize);
- char * font = msLookupHashTable(&(fontset->fonts), label->font);
- if (!font) {
- msSetError(MS_TTFERR, "Requested font (%s) not found.", "msGetLabelSizeAGG()", label->font);
- return MS_FAILURE;
- }
- if (ren->getLabelSize(string, font, size, rect) != MS_SUCCESS)
- return MS_FAILURE;
- if(adjustBaseline) {
- int nNewlines = msCountChars(string,'\n');
- if(!nNewlines) {
- label->offsety += MS_NINT(((rect->miny+rect->maxy) + size) / 2);
- label->offsetx += MS_NINT(rect->minx / 2);
- }
- else {
- rectObj r;
- char* firstLine = msGetFirstLine(string);
- ren->getLabelSize(firstLine, font, size, &r);
- label->offsety += MS_NINT(((r.miny+r.maxy) + size) / 2);
- label->offsetx += MS_NINT(r.minx / 2);
- free(firstLine);
- }
- }
- } else {
- char **token=NULL;
- int t, num_tokens, max_token_length=0;
- if ((token = msStringSplit(string, '\n', &(num_tokens))) == NULL)
- return (0);
+ return ren->getLabelSize(string, font, size, rect);
+}
- for (t=0; t<num_tokens; t++)
- /* what's the longest token */
- max_token_length = MS_MAX(max_token_length, (int) strlen(token[t]));
+int msGetRasterTextBBoxAGG(imageObj *img, int size, char *string, rectObj *rect) {
+ char **token=NULL;
+ int t, num_tokens, max_token_length=0;
+ if ((token = msStringSplit(string, '\n', &(num_tokens))) == NULL)
+ return (0);
- rect->minx = 0;
- rect->miny = -(rasterfont_sizes[label->size].height * num_tokens);
- rect->maxx = rasterfont_sizes[label->size].width * max_token_length;
- rect->maxy = 0;
+ for (t=0; t<num_tokens; t++)
+ //what's the longest token
+ max_token_length = MS_MAX(max_token_length, (int) strlen(token[t]));
- msFreeCharArray(token, num_tokens);
- }
+ rect->minx = 0;
+ rect->miny = -(rasterfont_sizes[size].height * num_tokens);
+ rect->maxx = rasterfont_sizes[size].width * max_token_length;
+ rect->maxy = 0;
+
+ msFreeCharArray(token, num_tokens);
return MS_SUCCESS;
}
+
// ---------------------------------------------------------------------------
// Draw a label curved along a line
// ---------------------------------------------------------------------------
@@ -2366,7 +2340,7 @@
if (label.type == MS_TRUETYPE) label.size = height;
marker.x = dstX + MS_NINT(width / 2.0);
marker.y = dstY + MS_NINT(height / 2.0);
- if(msGetLabelSizeAGG(image, (char*)"Aa", &label, &label_rect, &map->fontset, 1.0, MS_FALSE) != -1)
+ if(msGetLabelSize(image, (char*)"Aa", &label, &label_rect, &map->fontset, 1.0, MS_FALSE) != -1)
{
pointObj label_point = get_metrics(&marker, MS_CC, label_rect, 0, 0, label.angle, 0, NULL);
msDrawTextAGG(image, label_point, (char*)"Aa", &label, &map->fontset, 1.0);
Modified: trunk/mapserver/mapgd.c
===================================================================
--- trunk/mapserver/mapgd.c 2008-10-03 13:37:46 UTC (rev 7943)
+++ trunk/mapserver/mapgd.c 2008-10-03 15:21:25 UTC (rev 7944)
@@ -3230,7 +3230,7 @@
if (label.type == MS_TRUETYPE) label.size = height;
marker.x = dstX + MS_NINT(width / 2.0);
marker.y = dstY + MS_NINT(height / 2.0);
- if(msGetLabelSizeGD("Aa", &label, &label_rect, &map->fontset, 1.0, MS_FALSE) != -1)
+ if(msGetLabelSize(NULL,"Aa", &label, &label_rect, &map->fontset, 1.0, MS_FALSE) != -1)
{
pointObj label_point = get_metrics(&marker, MS_CC, label_rect, 0, 0, label.angle, 0, NULL);
msDrawTextGD(img, label_point, "Aa", &label, &map->fontset, 1.0);
Modified: trunk/mapserver/mapgraticule.c
===================================================================
--- trunk/mapserver/mapgraticule.c 2008-10-03 13:37:46 UTC (rev 7943)
+++ trunk/mapserver/mapgraticule.c 2008-10-03 15:21:25 UTC (rev 7944)
@@ -630,7 +630,7 @@
if(pLayer->transform)
msTransformShapeToPixel(pShape, pLayer->map->extent, pLayer->map->cellsize);
- if(msGetLabelSizeGD( pShape->text, &pLayer->class[0]->label, &rectLabel, &pLayer->map->fontset, 1.0, MS_FALSE) != 0)
+ if(msGetLabelSize(NULL, pShape->text, &pLayer->class[0]->label, &rectLabel, &pLayer->map->fontset, 1.0, MS_FALSE) != 0)
return MS_FAILURE; /* msSetError already called */
switch( ePosition ) {
Modified: trunk/mapserver/maplabel.c
===================================================================
--- trunk/mapserver/maplabel.c 2008-10-03 13:37:46 UTC (rev 7943)
+++ trunk/mapserver/maplabel.c 2008-10-03 15:21:25 UTC (rev 7944)
@@ -642,6 +642,62 @@
#endif
}
+int msGetTruetypeTextBBox(imageObj *img, char *font, int size, char *string, rectObj *rect) {
+#ifdef USE_GD_FT
+#ifdef USE_AGG
+ if(img!=NULL && MS_RENDERER_AGG(img->format)) {
+ return msGetTruetypeTextBBoxAGG(img,font,size,string,rect);
+ } else
+#endif
+ {
+ int bbox[8];
+ char *error = gdImageStringFT(NULL, bbox, 0, font, size, 0, 0, 0, string);
+ if(error) {
+ msSetError(MS_TTFERR, error, "msGetTruetypeTextBBox()");
+ return(MS_FAILURE);
+ }
+
+ rect->minx = bbox[0];
+ rect->miny = bbox[5];
+ rect->maxx = bbox[2];
+ rect->maxy = bbox[1];
+ return MS_SUCCESS;
+ }
+#else
+ /*shouldn't happen*/
+ return(MS_FAILURE);
+#endif
+}
+
+int msGetRasterTextBBox(imageObj *img, int size, char *string, rectObj *rect) {
+#ifdef USE_AGG
+ if(img!=NULL && MS_RENDERER_AGG(img->format)) {
+ return msGetRasterTextBBoxAGG(img,size,string,rect);
+ } else
+#endif
+ {
+ gdFontPtr fontPtr;
+ char **token=NULL;
+ int t, num_tokens, max_token_length=0;
+ if((fontPtr = msGetBitmapFont(size)) == NULL)
+ return(-1);
+
+ if((token = msStringSplit(string, '\n', &(num_tokens))) == NULL)
+ return(0);
+
+ for(t=0; t<num_tokens; t++) /* what's the longest token */
+ max_token_length = MS_MAX(max_token_length, (int) strlen(token[t]));
+
+ rect->minx = 0;
+ rect->miny = -(fontPtr->h * num_tokens);
+ rect->maxx = fontPtr->w * max_token_length;
+ rect->maxy = 0;
+
+ msFreeCharArray(token, num_tokens);
+ return MS_SUCCESS;
+ }
+}
+
/*
** Note: All these routines assume a reference point at the LL corner of the text. GD's
** bitmapped fonts use UL and this is compensated for. Note the rect is relative to the
@@ -649,13 +705,12 @@
*/
/* assumes an angle of 0 regardless of what's in the label object */
-int msGetLabelSizeGD(char *string, labelObj *label, rectObj *rect, fontSetObj *fontset, double scalefactor, int adjustBaseline)
+int msGetLabelSize(imageObj *img, char *string, labelObj *label, rectObj *rect, fontSetObj *fontset, double scalefactor, int adjustBaseline)
{
int size;
if(label->type == MS_TRUETYPE) {
#ifdef USE_GD_FT
- int bbox[8];
- char *error=NULL, *font=NULL;
+ char *font=NULL;
size = MS_NINT(label->size*scalefactor);
size = MS_MAX(size, label->minsize);
@@ -670,71 +725,36 @@
return(-1);
}
+ if(msGetTruetypeTextBBox(img,font,size,string,rect)!=MS_SUCCESS)
+ return MS_FAILURE;
- error = gdImageStringFT(NULL, bbox, 0, font, size, 0, 0, 0, string);
- if(error) {
- msSetError(MS_TTFERR, error, "msGetLabelSize()");
- return(-1);
- }
-
- rect->minx = bbox[0];
- rect->miny = bbox[5];
- rect->maxx = bbox[2];
- rect->maxy = bbox[1];
-
/* bug 1449 fix (adjust baseline) */
if(adjustBaseline) {
int nNewlines = msCountChars(string,'\n');
if(!nNewlines) {
- label->offsety += MS_NINT(((bbox[5] + bbox[1]) + size) / 2);
- label->offsetx += MS_NINT(bbox[0] / 2);
+ label->offsety += MS_NINT(((rect->miny + rect->maxy) + size) / 2);
+ label->offsetx += MS_NINT(rect->minx / 2);
}
else {
+ rectObj rect2; /*bbox of first line only*/
char* firstLine = msGetFirstLine(string);
- gdImageStringFT(NULL, bbox, 0, font, size, 0, 0, 0, firstLine);
- label->offsety += MS_NINT(((bbox[5] + bbox[1]) + size) / 2);
- label->offsetx += MS_NINT(bbox[0] / 2);
+ msGetTruetypeTextBBox(img,font,size,firstLine,&rect2);
+ label->offsety += MS_NINT(((rect2.miny+rect2.maxy) + size) / 2);
+ label->offsetx += MS_NINT(rect2.minx / 2);
free(firstLine);
}
}
+ return MS_SUCCESS;
#else
msSetError(MS_TTFERR, "TrueType font support is not available.", "msGetLabelSize()");
return(-1);
#endif
} else { /* MS_BITMAP font */
- gdFontPtr fontPtr;
- char **token=NULL;
- int t, num_tokens, max_token_length=0;
- if((fontPtr = msGetBitmapFont(label->size)) == NULL)
- return(-1);
-
- if((token = msStringSplit(string, '\n', &(num_tokens))) == NULL)
- return(0);
-
- for(t=0; t<num_tokens; t++) /* what's the longest token */
- max_token_length = MS_MAX(max_token_length, (int) strlen(token[t]));
-
- rect->minx = 0;
- rect->miny = -(fontPtr->h * num_tokens);
- rect->maxx = fontPtr->w * max_token_length;
- rect->maxy = 0;
-
- msFreeCharArray(token, num_tokens);
-
- }
+ msGetRasterTextBBox(img,label->size,string,rect);
+ }
return(0);
}
-int msGetLabelSize(imageObj *img, char *string, labelObj *label, rectObj *rect, fontSetObj *fontset, double scalefactor, int adjustBaseline)
-{
-#ifdef USE_AGG
- if(img!= NULL && MS_RENDERER_AGG(img->format))
- return msGetLabelSizeAGG(img,string,label,rect,fontset,scalefactor,adjustBaseline);
- else
-#endif
- return msGetLabelSizeGD(string,label,rect,fontset,scalefactor,adjustBaseline);
-}
-
/*
* Return the size of the label, plus an array of individual character
* offsets, as calculated by gdImageStringFTEx(). The callee is
@@ -1068,7 +1088,7 @@
label.outlinecolor = style->outlinecolor;
label.antialias = symbol->antialias;
- if(msGetLabelSizeGD(symbol->character, &label, &label_rect, symbolset->fontset, scalefactor, MS_FALSE) == -1)
+ if(msGetLabelSize(NULL,symbol->character, &label, &label_rect, symbolset->fontset, scalefactor, MS_FALSE) == -1)
return(-1);
label_width = (int) label_rect.maxx - (int) label_rect.minx;
Modified: trunk/mapserver/mapscale.c
===================================================================
--- trunk/mapserver/mapscale.c 2008-10-03 13:37:46 UTC (rev 7943)
+++ trunk/mapserver/mapscale.c 2008-10-03 15:21:25 UTC (rev 7944)
@@ -310,7 +310,7 @@
map->scalebar.label.position = MS_CC;
p.x = ox + j*isx; /* + MS_NINT(fontPtr->w/2); */
p.y = oy + map->scalebar.height + MS_NINT(VSPACING*fontPtr->h);
- if(msGetLabelSizeGD(label,&(map->scalebar.label), &r,
+ if(msGetLabelSize(NULL,label,&(map->scalebar.label), &r,
&(map->fontset), 1, MS_FALSE) == -1) return(NULL);
p2 = get_metrics(&p, MS_CC, r, 0,0, 0, 0, NULL);
msDrawTextGD(image->img.gd, p2, label, &(map->scalebar.label), &(map->fontset), 1.0);
@@ -323,7 +323,7 @@
map->scalebar.label.position = MS_CR;
p.x = ox; /* + MS_NINT(fontPtr->w/2); */
p.y = oy + map->scalebar.height + MS_NINT(VSPACING*fontPtr->h);
- if(msGetLabelSizeGD(label,&(map->scalebar.label), &r,
+ if(msGetLabelSize(NULL,label,&(map->scalebar.label), &r,
&(map->fontset), 1, MS_FALSE) == -1) return(NULL);
p2 = get_metrics(&p, MS_CR, r, 0,0, 0, 0, NULL);
msDrawTextGD(image->img.gd, p2, label, &(map->scalebar.label), &(map->fontset), 1.0);
@@ -343,7 +343,7 @@
map->scalebar.label.position = MS_CC;
p.x = ox + j*isx; /* + MS_NINT(fontPtr->w/2); */
p.y = oy + map->scalebar.height + MS_NINT(VSPACING*fontPtr->h);
- if(msGetLabelSizeGD(label,&(map->scalebar.label), &r,
+ if(msGetLabelSize(NULL,label,&(map->scalebar.label), &r,
&(map->fontset), 1, MS_FALSE) == -1) return(NULL);
p2 = get_metrics(&p, MS_CC, r, 0,0, 0, 0, NULL);
msDrawTextGD(image->img.gd, p2, label, &(map->scalebar.label), &(map->fontset), 1.0);
@@ -359,7 +359,7 @@
map->scalebar.label.position = MS_CR;
p.x = ox; /* + MS_NINT(fontPtr->w/2); */
p.y = oy + map->scalebar.height + MS_NINT(VSPACING*fontPtr->h);
- if(msGetLabelSizeGD(label,&(map->scalebar.label), &r,
+ if(msGetLabelSize(NULL,label,&(map->scalebar.label), &r,
&(map->fontset), 1, MS_FALSE) == -1) return(NULL);
p2 = get_metrics(&p, MS_CR, r, 0,0, 0, 0, NULL);
msDrawTextGD(image->img.gd, p2, label, &(map->scalebar.label), &(map->fontset), 1.0);
Modified: trunk/mapserver/mapserver.h
===================================================================
--- trunk/mapserver/mapserver.h 2008-10-03 13:37:46 UTC (rev 7943)
+++ trunk/mapserver/mapserver.h 2008-10-03 15:21:25 UTC (rev 7944)
@@ -1702,8 +1702,6 @@
MS_DLL_EXPORT int msFreeFontSet(fontSetObj *fontset);
MS_DLL_EXPORT char *msTransformLabelText(mapObj *map, imageObj* image, labelObj *label, char *text);
MS_DLL_EXPORT int msGetLabelSize(imageObj *img, char *string, labelObj *label, rectObj *rect, fontSetObj *fontSet, double scalefactor, int adjustBaseline);
-MS_DLL_EXPORT int msGetLabelSizeGD(char *string, labelObj *label, rectObj *rect, fontSetObj *fontSet, double scalefactor, int adjustBaseline);
-
MS_DLL_EXPORT int msGetLabelSizeEx(char *string, labelObj *label, rectObj *rect, fontSetObj *fontSet, double scalefactor, int adjustBaseline, double **offsets);
MS_DLL_EXPORT int msAddLabel(mapObj *map, int layerindex, int classindex, int shapeindex, int tileindex, pointObj *point, labelPathObj *labelpath, char *string, double featuresize, labelObj *label);
MS_DLL_EXPORT void msTestLabelCacheCollisions(labelCacheObj *labelcache, labelObj *labelPtr, int mapwidth, int mapheight, int buffer, labelCacheMemberObj *cachePtr, int current_priority, int current_label);
@@ -1949,8 +1947,8 @@
MS_DLL_EXPORT void msDrawMarkerSymbolAGG(symbolSetObj *symbolset, imageObj *image, pointObj *p, styleObj *style, double scalefactor);
MS_DLL_EXPORT void msDrawLineSymbolAGG(symbolSetObj *symbolset, imageObj *image, shapeObj *p, styleObj *style, double scalefactor);
MS_DLL_EXPORT void msDrawShadeSymbolAGG(symbolSetObj *symbolset, imageObj *image,shapeObj *p, styleObj *style, double scalefactor);
-
-MS_DLL_EXPORT int msGetLabelSizeAGG(imageObj *img, char *string, labelObj *label, rectObj *rect, fontSetObj *fontSet, double scalefactor, int adjustBaseline);
+int msGetTruetypeTextBBoxAGG(imageObj *img, char *font, int size, char *string, rectObj *rect);
+int msGetRasterTextBBoxAGG(imageObj *img, int size, char *string, rectObj *rect);
MS_DLL_EXPORT int msDrawTextAGG(imageObj *imgage, pointObj labelPnt, char *string, labelObj *label, fontSetObj *fontset, double scalefactor);
MS_DLL_EXPORT int msDrawTextLineAGG(imageObj *image, char *string, labelObj *label, labelPathObj *labelpath, fontSetObj *fontset, double scalefactor);
Modified: trunk/mapserver/mapsvg.c
===================================================================
--- trunk/mapserver/mapsvg.c 2008-10-03 13:37:46 UTC (rev 7943)
+++ trunk/mapserver/mapsvg.c 2008-10-03 15:21:25 UTC (rev 7944)
@@ -1179,7 +1179,7 @@
if(!cachePtr->text || strlen(cachePtr->text) == 0)
continue; /* not an error, just don't want to do anything */
- if(msGetLabelSizeGD(cachePtr->text, labelPtr, &r, &(map->fontset), layerPtr->scalefactor, MS_TRUE) == -1)
+ if(msGetLabelSize(image, cachePtr->text, labelPtr, &r, &(map->fontset), layerPtr->scalefactor, MS_TRUE) == -1)
return(-1);
if(labelPtr->autominfeaturesize && ((r.maxx-r.minx) > cachePtr->featuresize))
More information about the mapserver-commits
mailing list