forked from MapServer/MapServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maplabel.c
1458 lines (1266 loc) · 48.3 KB
/
maplabel.c
1
2
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
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
307
308
309
310
311
312
313
314
315
316
317
318
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
347
348
349
350
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
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
717
718
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
753
754
755
756
757
758
759
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
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
* $Id$
*
* Project: MapServer
* Purpose: Labeling Implementation.
* Author: Steve Lime and the MapServer team.
*
******************************************************************************
* Copyright (c) 1996-2005 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.
****************************************************************************/
/*
** maplabel.c: Routines to enable text drawing, BITMAP or TRUETYPE.
*/
#include "mapserver.h"
/**
* replace wrap characters with \n , respecting maximal line length.
*
* returns a pointer to the newly allocated text. memory is controlled
* inside this function, so the caller MUST use the pointer returned by
* the function:
* text = msWrapText(label,text);
*
* TODO/FIXME: function will produce erroneous/crashing? results
* if the wrap character is encoded with multiple bytes
*
* see http://mapserver.org/development/rfc/ms-rfc-40.html
* for a summary of how wrap/maxlength interact on the result
* of the text transformation
*/
char *msWrapText(labelObj *label, char *text)
{
char wrap;
int maxlength;
if(!text) /*not an error if no text*/
return text;
wrap = label->wrap;
maxlength = label->maxlength;
if(maxlength == 0) {
if(wrap!='\0') {
/* if maxlength = 0 *and* a wrap character was specified,
* replace all wrap characters by \n
* this is the traditional meaning of the wrap character
*/
msReplaceChar(text, wrap, '\n');
}
/* if neither maxlength, nor wrap were specified,
* don't transform this text */
return text;
} else if(maxlength>0) {
if(wrap!='\0') {
/* split input text at the wrap character, only if
* the current line length is over maxlength */
/* TODO: check if the wrap character is a valid byte
* inside a multibyte utf8 glyph. if so, the msCountChars
* will return an erroneous value */
int numwrapchars = msCountChars(text,wrap);
if(numwrapchars > 0) {
if(label->encoding) {
/* we have to use utf decoding functions here, so as not to
* split a text line on a multibyte character */
int num_cur_glyph_on_line = 0; /*count for the number of glyphs
on the current line*/
char *textptr = text;
char glyph[11]; /*storage for unicode fetching function*/
int glyphlen = 0; /*size of current glyph in bytes*/
while((glyphlen = msGetNextGlyph((const char**)&textptr,glyph))>0) {
num_cur_glyph_on_line++;
if(*glyph == wrap && num_cur_glyph_on_line>=(maxlength)) {
/*FIXME (if wrap becomes something other than char):*/
*(textptr-1)='\n'; /*replace wrap char with a \n*/
num_cur_glyph_on_line=0; /*reset count*/
}
}
} else {
int cur_char_on_line = 0;
char *textptr = text;
while(*textptr != 0) {
cur_char_on_line++;
if(*textptr == wrap && cur_char_on_line>=maxlength) {
*textptr='\n'; /*replace wrap char with a \n*/
cur_char_on_line=0; /*reset count*/
}
textptr++;
}
}
return text;
} else {
/*there are no characters available for wrapping*/
return text;
}
} else {
/* if no wrap character was specified, but a maxlength was,
* don't draw this label if it is longer than the specified maxlength*/
if(msGetNumGlyphs(text)>maxlength) {
free(text);
return NULL;
} else {
return text;
}
}
} else {
/* negative maxlength: we split lines unconditionally, i.e. without
loooking for a wrap character*/
int numglyphs,numlines;
maxlength = -maxlength; /* use a positive value*/
numglyphs = msGetNumGlyphs(text);
numlines = (numglyphs-1) / maxlength + 1; /*count total number of lines needed
after splitting*/
if(numlines>1) {
char *newtext = msSmallMalloc(strlen(text)+numlines+1);
char *newtextptr = newtext;
char *textptr = text;
int glyphlen = 0, num_cur_glyph = 0;
while((glyphlen = msGetNextGlyph((const char**)&textptr,newtextptr))>0) {
num_cur_glyph++;
newtextptr += glyphlen;
if(num_cur_glyph%maxlength == 0 && num_cur_glyph != numglyphs) {
/*we're at a split location, insert a newline*/
*newtextptr = '\n';
newtextptr++;
}
}
free(text);
return newtext;
} else {
/*no splitting needed, return the original*/
return text;
}
}
}
char *msAlignText(mapObj *map, labelObj *label, char *text)
{
double spacewidth=0.0; /*size of a single space, in fractional pixels*/
int numlines;
char **textlines,*newtext,*newtextptr;
int *textlinelengths,*numspacesforpadding;
int numspacestoadd,maxlinelength,i;
rectObj label_rect;
if(!msCountChars(text,'\n'))
return text; /*only one line*/
/*split text into individual lines
* TODO: check if splitting on \n is utf8 safe*/
textlines = msStringSplit(text,'\n',&numlines);
/*
* label->space_size_10 contains a cache for the horizontal size of a single
* 'space' character, at size 10 for the current label
* FIXME: in case of attribute binding for the FONT of the label, this cache will
* be wrong for labels where the attributed font is different than the first
* computed font. This shouldn't happen too often, and hopefully the size of a
* space character shouldn't vary too much between different fonts*/
if(label->space_size_10 == 0.0) {
/*if the cache hasn't been initialized yet, or with pixmap fonts*/
/* compute the size of 16 adjacent spaces. we can't do this for just one space,
* as the labelSize computing functions return integer bounding boxes. we assume
* that the integer rounding for such a number of spaces will be negligeable
* compared to the actual size of thoses spaces */
if(msGetLabelSize(map,label,". .",10.0,&label_rect,NULL) != MS_SUCCESS) {
/*error computing label size, we can't continue*/
/*free the previously allocated split text*/
while(numlines--)
free(textlines[numlines]);
free(textlines);
return text;
}
/* this is the size of a single space character. for truetype fonts,
* it's the size of a 10pt space. For pixmap fonts, it's the size
* for the current label */
spacewidth = (label_rect.maxx-label_rect.minx)/16.0;
if(label->type == MS_TRUETYPE) {
label->space_size_10=spacewidth; /*cache the computed size*/
/*size of a space for current label size*/
spacewidth = spacewidth * (double)label->size/10.0;
}
} else {
spacewidth = label->space_size_10 * (double)label->size/10.0;
}
spacewidth = MS_MAX(1,spacewidth);
/*length in pixels of each line*/
textlinelengths = (int*)msSmallMalloc(numlines*sizeof(int));
/*number of spaces that need to be added to each line*/
numspacesforpadding = (int*)msSmallMalloc(numlines*sizeof(int));
/*total number of spaces that need to be added*/
numspacestoadd=0;
/*length in pixels of the longest line*/
maxlinelength=0;
for(i=0; i<numlines; i++) {
if(MS_SUCCESS != msGetLabelSize(map,label,textlines[i],label->size, &label_rect,NULL)) {
msFreeCharArray(textlines,numlines);
msFree(textlinelengths);
msFree(numspacesforpadding);
return text;
}
textlinelengths[i] = label_rect.maxx-label_rect.minx;
if(maxlinelength<textlinelengths[i])
maxlinelength=textlinelengths[i];
}
for(i=0; i<numlines; i++) {
/* number of spaces to add so the current line is
* as long as the longest line */
double nfracspaces = (maxlinelength - textlinelengths[i])/spacewidth;
if(label->align == MS_ALIGN_CENTER) {
numspacesforpadding[i]=MS_NINT(nfracspaces/2.0);
} else {
if(label->align == MS_ALIGN_RIGHT) {
numspacesforpadding[i]=MS_NINT(nfracspaces);
}
}
numspacestoadd+=numspacesforpadding[i];
}
/*allocate new text with room for the additional spaces needed*/
newtext = (char*)msSmallMalloc(strlen(text)+1+numspacestoadd);
newtextptr=newtext;
for(i=0; i<numlines; i++) {
int j;
/*padd beginning of line with needed spaces*/
for(j=0; j<numspacesforpadding[i]; j++) {
*(newtextptr++)=' ';
}
/*copy original line*/
strcpy(newtextptr,textlines[i]);
/*place pointer at the char right after the current line*/
newtextptr+=strlen(textlines[i])+1;
if(i!=numlines-1) {
/*put the \n back in (was taken away by msStringSplit)*/
*(newtextptr-1)='\n';
}
}
/*free the original text*/
free(text);
for(i=0; i<numlines; i++) {
free(textlines[i]);
}
free(textlines);
free(textlinelengths);
free(numspacesforpadding);
/*return the aligned text. note that the terminating \0 was added by the last
* call to strcpy */
return newtext;
}
/*
* this function applies the label encoding and wrap parameters
* to the supplied text
* Note: it is the caller's responsibility to free the returned
* char array
*/
char *msTransformLabelText(mapObj *map, labelObj *label, char *text)
{
char *newtext = text;
if(label->encoding)
newtext = msGetEncodedString(text, label->encoding);
else
newtext=msStrdup(text);
if(newtext && (label->wrap!='\0' || label->maxlength!=0)) {
newtext = msWrapText(label, newtext);
}
if(newtext && label->align!=MS_ALIGN_LEFT ) {
newtext = msAlignText(map, label, newtext);
}
return newtext;
}
int msAddLabelGroup(mapObj *map, int layerindex, int classindex, shapeObj *shape, pointObj *point, double featuresize)
{
int i, priority, numactivelabels=0;
labelCacheSlotObj *cacheslot;
labelCacheMemberObj *cachePtr=NULL;
layerObj *layerPtr=NULL;
classObj *classPtr=NULL;
layerPtr = (GET_LAYER(map, layerindex)); /* set up a few pointers for clarity */
classPtr = GET_LAYER(map, layerindex)->class[classindex];
if(classPtr->numlabels == 0) return MS_SUCCESS; /* not an error just nothing to do */
for(i=0; i<classPtr->numlabels; i++) {
if(classPtr->labels[i]->status == MS_ON) {
numactivelabels++;
}
}
if(numactivelabels == 0) return MS_SUCCESS;
/* if the number of labels is 1 then call msAddLabel() accordingly */
if(numactivelabels == 1) {
for(i=0; i<classPtr->numlabels; i++) {
if(classPtr->labels[i]->status == MS_ON)
return msAddLabel(map, classPtr->labels[i], layerindex, classindex, shape, point, NULL, featuresize);
}
}
if (layerPtr->type == MS_LAYER_ANNOTATION && (cachePtr->numlabels > 1 || classPtr->leader.maxdistance)) {
msSetError(MS_MISCERR, "Multiple Labels and/or LEADERs are not supported with annotation layers", "msAddLabelGroup()");
return MS_FAILURE;
}
/* check that the label intersects the layer mask */
if(layerPtr->mask) {
int maskLayerIdx = msGetLayerIndex(map,layerPtr->mask);
layerObj *maskLayer = GET_LAYER(map,maskLayerIdx);
unsigned char *alphapixptr;
if(maskLayer->maskimage && MS_IMAGE_RENDERER(maskLayer->maskimage)->supports_pixel_buffer) {
rasterBufferObj rb;
int x,y;
memset(&rb,0,sizeof(rasterBufferObj));
MS_IMAGE_RENDERER(maskLayer->maskimage)->getRasterBufferHandle(maskLayer->maskimage,&rb);
x = MS_NINT(point->x);
y = MS_NINT(point->y);
#ifdef USE_GD
if(rb.type == MS_BUFFER_BYTE_RGBA) {
alphapixptr = rb.data.rgba.a+rb.data.rgba.row_step*y + rb.data.rgba.pixel_step*x;
if(!*alphapixptr) {
/* label point does not intersect mask */
return MS_SUCCESS;
}
} else {
if(!gdImageGetPixel(rb.data.gd_img,x,y))
return MS_SUCCESS;
}
#else
assert(rb.type == MS_BUFFER_BYTE_RGBA);
alphapixptr = rb.data.rgba.a+rb.data.rgba.row_step*y + rb.data.rgba.pixel_step*x;
if(!*alphapixptr) {
/* label point does not intersect mask */
return MS_SUCCESS;
}
#endif
} else {
msSetError(MS_MISCERR, "Layer (%s) references references a mask layer, but the selected renderer does not support them", "msAddLabelGroup()", layerPtr->name);
return (MS_FAILURE);
}
}
/* Validate label priority value and get ref on label cache for it */
priority = classPtr->labels[0]->priority; /* take priority from the first label */
if (priority < 1)
priority = 1;
else if (priority > MS_MAX_LABEL_PRIORITY)
priority = MS_MAX_LABEL_PRIORITY;
cacheslot = &(map->labelcache.slots[priority-1]);
if(cacheslot->numlabels == cacheslot->cachesize) { /* just add it to the end */
cacheslot->labels = (labelCacheMemberObj *) realloc(cacheslot->labels, sizeof(labelCacheMemberObj)*(cacheslot->cachesize+MS_LABELCACHEINCREMENT));
MS_CHECK_ALLOC(cacheslot->labels, sizeof(labelCacheMemberObj)*(cacheslot->cachesize+MS_LABELCACHEINCREMENT), MS_FAILURE);
cacheslot->cachesize += MS_LABELCACHEINCREMENT;
}
cachePtr = &(cacheslot->labels[cacheslot->numlabels]);
cachePtr->layerindex = layerindex; /* so we can get back to this *raw* data if necessary */
cachePtr->classindex = classindex;
if(shape) {
cachePtr->shapetype = shape->type;
} else {
cachePtr->shapetype = MS_SHAPE_POINT;
}
cachePtr->point = *point; /* the actual label point */
cachePtr->labelpath = NULL;
cachePtr->leaderline = NULL;
cachePtr->leaderbbox = NULL;
// cachePtr->text = msStrdup(string); /* the actual text */
/* TODO: perhaps we can get rid of this next section and just store a marker size? Why do we cache the styles for a point layer? */
/* copy the styles (only if there is an accompanying marker)
* We cannot simply keep refs because the rendering code might alters some members of the style objects
*/
cachePtr->styles = NULL;
cachePtr->numstyles = 0;
if(layerPtr->type == MS_LAYER_ANNOTATION && classPtr->numstyles > 0) {
cachePtr->numstyles = classPtr->numstyles;
cachePtr->styles = (styleObj *) msSmallMalloc(sizeof(styleObj)*classPtr->numstyles);
if (classPtr->numstyles > 0) {
for(i=0; i<classPtr->numstyles; i++) {
initStyle(&(cachePtr->styles[i]));
msCopyStyle(&(cachePtr->styles[i]), classPtr->styles[i]);
}
}
}
/*
** copy the labels (we are guaranteed to have more than one):
** we cannot simply keep refs because the rendering code alters some members of the style objects
*/
cachePtr->numlabels = 0;
cachePtr->labels = (labelObj *) msSmallMalloc(sizeof(labelObj)*numactivelabels);
for(i=0; i<classPtr->numlabels; i++) {
if(classPtr->labels[i]->status == MS_OFF) continue;
initLabel(&(cachePtr->labels[cachePtr->numlabels]));
msCopyLabel(&(cachePtr->labels[cachePtr->numlabels]), classPtr->labels[i]);
cachePtr->numlabels++;
}
assert(cachePtr->numlabels == numactivelabels);
cachePtr->markerid = -1;
cachePtr->featuresize = featuresize;
//cachePtr->poly = (shapeObj *) msSmallMalloc(sizeof(shapeObj));
//msInitShape(cachePtr->poly);
cachePtr->poly = NULL;
cachePtr->status = MS_FALSE;
if(layerPtr->type == MS_LAYER_POINT && classPtr->numstyles > 0) {
/* cache the marker placement, it's already on the map */
/* TO DO: at the moment only checks the bottom style, perhaps should check all of them */
/* #2347: after RFC-24 classPtr->styles could be NULL so we check it */
rectObj rect;
double w, h;
if(msGetMarkerSize(&map->symbolset, classPtr->styles[0], &w, &h, layerPtr->scalefactor) != MS_SUCCESS)
return(MS_FAILURE);
if(cacheslot->nummarkers == cacheslot->markercachesize) { /* just add it to the end */
cacheslot->markers = (markerCacheMemberObj *) realloc(cacheslot->markers, sizeof(markerCacheMemberObj)*(cacheslot->cachesize+MS_LABELCACHEINCREMENT));
MS_CHECK_ALLOC(cacheslot->markers, sizeof(markerCacheMemberObj)*(cacheslot->cachesize+MS_LABELCACHEINCREMENT), MS_FAILURE);
cacheslot->markercachesize+=MS_LABELCACHEINCREMENT;
}
i = cacheslot->nummarkers;
cacheslot->markers[i].poly = (shapeObj *) msSmallMalloc(sizeof(shapeObj));
msInitShape(cacheslot->markers[i].poly);
rect.minx = (point->x - .5 * w);
rect.miny = (point->y - .5 * h);
rect.maxx = rect.minx + (w-1);
rect.maxy = rect.miny + (h-1);
msRectToPolygon(rect, cacheslot->markers[i].poly);
cacheslot->markers[i].id = cacheslot->numlabels;
cachePtr->markerid = i;
cacheslot->nummarkers++;
}
cacheslot->numlabels++;
/* Maintain main labelCacheObj.numlabels only for backwards compatibility */
map->labelcache.numlabels++;
return(MS_SUCCESS);
}
int msAddLabel(mapObj *map, labelObj *label, int layerindex, int classindex, shapeObj *shape, pointObj *point, labelPathObj *labelpath, double featuresize)
{
int i;
labelCacheSlotObj *cacheslot;
labelCacheMemberObj *cachePtr=NULL;
layerObj *layerPtr=NULL;
classObj *classPtr=NULL;
if(!label) return(MS_FAILURE); // RFC 77 TODO: set a proper message
if(label->status == MS_OFF) return(MS_SUCCESS); /* not an error */
if(!label->annotext) {
/* check if we have a labelpnt style */
for(i=0; i<label->numstyles; i++) {
if(label->styles[i]->_geomtransform.type == MS_GEOMTRANSFORM_LABELPOINT)
break;
}
if(i==label->numstyles) {
/* label has no text or marker symbols */
return MS_SUCCESS;
}
}
layerPtr = (GET_LAYER(map, layerindex)); /* set up a few pointers for clarity */
classPtr = GET_LAYER(map, layerindex)->class[classindex];
if(classPtr->leader.maxdistance) {
if (layerPtr->type == MS_LAYER_ANNOTATION) {
msSetError(MS_MISCERR, "LEADERs are not supported on annotation layers", "msAddLabel()");
return MS_FAILURE;
}
if(labelpath) {
msSetError(MS_MISCERR, "LEADERs are not supported on ANGLE FOLLOW labels", "msAddLabel()");
return MS_FAILURE;
}
}
/* check that the label intersects the layer mask */
if (layerPtr->mask) {
int maskLayerIdx = msGetLayerIndex(map, layerPtr->mask);
layerObj *maskLayer = GET_LAYER(map, maskLayerIdx);
unsigned char *alphapixptr;
if (maskLayer->maskimage && MS_IMAGE_RENDERER(maskLayer->maskimage)->supports_pixel_buffer) {
rasterBufferObj rb;
memset(&rb, 0, sizeof (rasterBufferObj));
MS_IMAGE_RENDERER(maskLayer->maskimage)->getRasterBufferHandle(maskLayer->maskimage, &rb);
if (point) {
int x = MS_NINT(point->x);
int y = MS_NINT(point->y);
#ifdef USE_GD
if(rb.type == MS_BUFFER_BYTE_RGBA) {
alphapixptr = rb.data.rgba.a+rb.data.rgba.row_step*y + rb.data.rgba.pixel_step*x;
if(!*alphapixptr) {
/* label point does not intersect mask */
return MS_SUCCESS;
}
} else {
if(!gdImageGetPixel(rb.data.gd_img,x,y)) {
return MS_SUCCESS;
}
}
#else
assert(rb.type == MS_BUFFER_BYTE_RGBA);
alphapixptr = rb.data.rgba.a+rb.data.rgba.row_step*y + rb.data.rgba.pixel_step*x;
if(!*alphapixptr) {
/* label point does not intersect mask */
return MS_SUCCESS;
}
#endif
} else if (labelpath) {
int i = 0;
for (i = 0; i < labelpath->path.numpoints; i++) {
int x = MS_NINT(labelpath->path.point[i].x);
int y = MS_NINT(labelpath->path.point[i].y);
#ifdef USE_GD
if (rb.type == MS_BUFFER_BYTE_RGBA) {
alphapixptr = rb.data.rgba.a + rb.data.rgba.row_step * y + rb.data.rgba.pixel_step*x;
if (!*alphapixptr) {
/* label point does not intersect mask */
msFreeLabelPathObj(labelpath);
return MS_SUCCESS;
}
} else {
if (!gdImageGetPixel(rb.data.gd_img, x, y)) {
msFreeLabelPathObj(labelpath);
return MS_SUCCESS;
}
}
#else
assert(rb.type == MS_BUFFER_BYTE_RGBA);
alphapixptr = rb.data.rgba.a + rb.data.rgba.row_step * y + rb.data.rgba.pixel_step*x;
if (!*alphapixptr) {
/* label point does not intersect mask */
msFreeLabelPathObj(labelpath);
return MS_SUCCESS;
}
#endif
}
}
} else {
msSetError(MS_MISCERR, "Layer (%s) references references a mask layer, but the selected renderer does not support them", "msAddLabel()", layerPtr->name);
return (MS_FAILURE);
}
}
/* Validate label priority value and get ref on label cache for it */
if (label->priority < 1)
label->priority = 1;
else if (label->priority > MS_MAX_LABEL_PRIORITY)
label->priority = MS_MAX_LABEL_PRIORITY;
cacheslot = &(map->labelcache.slots[label->priority-1]);
if(cacheslot->numlabels == cacheslot->cachesize) { /* just add it to the end */
cacheslot->labels = (labelCacheMemberObj *) realloc(cacheslot->labels, sizeof(labelCacheMemberObj)*(cacheslot->cachesize+MS_LABELCACHEINCREMENT));
MS_CHECK_ALLOC(cacheslot->labels, sizeof(labelCacheMemberObj)*(cacheslot->cachesize+MS_LABELCACHEINCREMENT), MS_FAILURE);
cacheslot->cachesize += MS_LABELCACHEINCREMENT;
}
cachePtr = &(cacheslot->labels[cacheslot->numlabels]);
cachePtr->layerindex = layerindex; /* so we can get back to this *raw* data if necessary */
cachePtr->classindex = classindex;
if(shape) {
cachePtr->shapetype = shape->type;
} else {
cachePtr->shapetype = MS_SHAPE_POINT;
}
cachePtr->leaderline = NULL;
cachePtr->leaderbbox = NULL;
/* Store the label point or the label path (Bug #1620) */
if ( point ) {
cachePtr->point = *point; /* the actual label point */
cachePtr->labelpath = NULL;
} else {
assert(labelpath);
cachePtr->labelpath = labelpath;
/* Use the middle point of the labelpath for mindistance calculations */
cachePtr->point = labelpath->path.point[labelpath->path.numpoints / 2];
}
/* TODO: perhaps we can get rid of this next section and just store a marker size? Why do we cache the styles for a point layer? */
/* copy the styles (only if there is an accompanying marker)
* We cannot simply keeep refs because the rendering code alters some members of the style objects
*/
cachePtr->styles = NULL;
cachePtr->numstyles = 0;
if(layerPtr->type == MS_LAYER_ANNOTATION && classPtr->numstyles > 0) {
cachePtr->numstyles = classPtr->numstyles;
cachePtr->styles = (styleObj *) msSmallMalloc(sizeof(styleObj)*classPtr->numstyles);
if (classPtr->numstyles > 0) {
for(i=0; i<classPtr->numstyles; i++) {
initStyle(&(cachePtr->styles[i]));
msCopyStyle(&(cachePtr->styles[i]), classPtr->styles[i]);
}
}
}
/* copy the label */
cachePtr->numlabels = 1;
cachePtr->labels = (labelObj *) msSmallMalloc(sizeof(labelObj));
initLabel(cachePtr->labels);
msCopyLabel(cachePtr->labels, label);
cachePtr->markerid = -1;
cachePtr->featuresize = featuresize;
//cachePtr->poly = (shapeObj *) msSmallMalloc(sizeof(shapeObj));
//msInitShape(cachePtr->poly);
cachePtr->poly = NULL;
cachePtr->status = MS_FALSE;
if(layerPtr->type == MS_LAYER_POINT && classPtr->numstyles > 0) { /* cache the marker placement, it's already on the map */
rectObj rect;
double w, h;
if(cacheslot->nummarkers == cacheslot->markercachesize) { /* just add it to the end */
cacheslot->markers = (markerCacheMemberObj *) realloc(cacheslot->markers, sizeof(markerCacheMemberObj)*(cacheslot->cachesize+MS_LABELCACHEINCREMENT));
MS_CHECK_ALLOC(cacheslot->markers, sizeof(markerCacheMemberObj)*(cacheslot->cachesize+MS_LABELCACHEINCREMENT), MS_FAILURE);
cacheslot->markercachesize+=MS_LABELCACHEINCREMENT;
}
i = cacheslot->nummarkers;
cacheslot->markers[i].poly = (shapeObj *) msSmallMalloc(sizeof(shapeObj));
msInitShape(cacheslot->markers[i].poly);
/* TO DO: at the moment only checks the bottom style, perhaps should check all of them */
/* #2347: after RFC-24 classPtr->styles could be NULL so we check it */
if(classPtr->styles != NULL) {
if(msGetMarkerSize(&map->symbolset, classPtr->styles[0], &w, &h, layerPtr->scalefactor) != MS_SUCCESS)
return(MS_FAILURE);
rect.minx = point->x - .5 * w;
rect.miny = point->y - .5 * h;
rect.maxx = rect.minx + (w-1);
rect.maxy = rect.miny + (h-1);
msRectToPolygon(rect, cacheslot->markers[i].poly);
cacheslot->markers[i].id = cacheslot->numlabels;
cachePtr->markerid = i;
cacheslot->nummarkers++;
}
}
cacheslot->numlabels++;
/* Maintain main labelCacheObj.numlabels only for backwards compatibility */
map->labelcache.numlabels++;
return(MS_SUCCESS);
}
/*
** Is a label completely in the image, reserving a gutter (in pixels) inside
** image for no labels (effectively making image larger. The gutter can be
** negative in cases where a label has a buffer around it.
*/
static int labelInImage(int width, int height, shapeObj *lpoly, int gutter)
{
int i,j;
/* do a bbox test first */
if(lpoly->bounds.minx >= gutter &&
lpoly->bounds.miny >= gutter &&
lpoly->bounds.maxx < width-gutter &&
lpoly->bounds.maxy < height-gutter) {
return MS_TRUE;
}
for(i=0; i<lpoly->numlines; i++) {
for(j=1; j<lpoly->line[i].numpoints; j++) {
if(lpoly->line[i].point[j].x < gutter) return(MS_FALSE);
if(lpoly->line[i].point[j].x >= width-gutter) return(MS_FALSE);
if(lpoly->line[i].point[j].y < gutter) return(MS_FALSE);
if(lpoly->line[i].point[j].y >= height-gutter) return(MS_FALSE);
}
}
return(MS_TRUE);
}
/* msTestLabelCacheCollisions()
**
** Compares current label against labels already drawn and markers from cache and discards it
** by setting cachePtr->status=MS_FALSE if it is a duplicate, collides with another label,
** or collides with a marker.
**
** This function is used by the various msDrawLabelCacheXX() implementations.
int msTestLabelCacheCollisions(labelCacheObj *labelcache, labelObj *labelPtr,
int mapwidth, int mapheight, int buffer,
labelCacheMemberObj *cachePtr, int current_priority,
int current_label, int mindistance, double label_size);
*/
int msTestLabelCacheCollisions(mapObj *map, labelCacheMemberObj *cachePtr, shapeObj *poly,
int mindistance, int current_priority, int current_label)
{
labelCacheObj *labelcache = &(map->labelcache);
int i, p, ll, pp;
double label_width = 0;
labelCacheMemberObj *curCachePtr=NULL;
/*
* Check against image bounds first
*/
if(!cachePtr->labels[0].partials) {
if(labelInImage(map->width, map->height, poly, labelcache->gutter) == MS_FALSE) {
return MS_FALSE;
}
}
/* compute start index of first label to test: only test against rendered labels */
if(current_label>=0) {
i = current_label+1;
} else {
i = 0;
current_label = -current_label;
}
/* Compare against all rendered markers from this priority level and higher.
** Labels can overlap their own marker and markers from lower priority levels
*/
for (p=current_priority; p < MS_MAX_LABEL_PRIORITY; p++) {
labelCacheSlotObj *markerslot;
markerslot = &(labelcache->slots[p]);
for ( ll = 0; ll < markerslot->nummarkers; ll++ ) {
if ( !(p == current_priority && current_label == markerslot->markers[ll].id ) ) { /* labels can overlap their own marker */
if ( intersectLabelPolygons(markerslot->markers[ll].poly, poly ) == MS_TRUE ) {
return MS_FALSE;
}
}
}
}
if(mindistance > 0)
label_width = poly->bounds.maxx - poly->bounds.minx;
for(p=current_priority; p<MS_MAX_LABEL_PRIORITY; p++) {
labelCacheSlotObj *cacheslot;
cacheslot = &(labelcache->slots[p]);
for( ; i < cacheslot->numlabels; i++) {
curCachePtr = &(cacheslot->labels[i]);
if(curCachePtr->status == MS_TRUE) { /* compare bounding polygons and check for duplicates */
/* skip testing against ourself */
assert(p!=current_priority || i != current_label);
/*
** Note 1: We add the label_size to the mindistance value when comparing because we do want the mindistance
** value between the labels and not only from point to point.
**
** Note 2: We only check the first label (could be multiples (RFC 77)) since that is *by far* the most common
** use case. Could change in the future but it's not worth the overhead at this point.
*/
if(mindistance >0 &&
(cachePtr->layerindex == curCachePtr->layerindex) &&
(cachePtr->classindex == curCachePtr->classindex) &&
(cachePtr->labels[0].annotext && curCachePtr->labels[0].annotext &&
strcmp(cachePtr->labels[0].annotext, curCachePtr->labels[0].annotext) == 0) &&
(msDistancePointToPoint(&(cachePtr->point), &(curCachePtr->point)) <= (mindistance + label_width))) { /* label is a duplicate */
return MS_FALSE;
}
if(intersectLabelPolygons(curCachePtr->poly, poly) == MS_TRUE) { /* polys intersect */
return MS_FALSE;
}
if(curCachePtr->leaderline) {
/* our poly against rendered leader lines */
/* first do a bbox check */
if(msRectOverlap(curCachePtr->leaderbbox, &(poly->bounds))) {
/* look for intersecting line segments */
for(ll=0; ll<poly->numlines; ll++)
for(pp=1; pp<poly->line[ll].numpoints; pp++)
if(msIntersectSegments(
&(poly->line[ll].point[pp-1]),
&(poly->line[ll].point[pp]),
&(curCachePtr->leaderline->point[0]),
&(curCachePtr->leaderline->point[1])) == MS_TRUE) {
return(MS_FALSE);
}
}
}
if(cachePtr->leaderline) {
/* does our leader intersect current label */
/* first do a bbox check */
if(msRectOverlap(cachePtr->leaderbbox, &(curCachePtr->poly->bounds))) {
/* look for intersecting line segments */
for(ll=0; ll<curCachePtr->poly->numlines; ll++)
for(pp=1; pp<curCachePtr->poly->line[ll].numpoints; pp++)
if(msIntersectSegments(
&(curCachePtr->poly->line[ll].point[pp-1]),
&(curCachePtr->poly->line[ll].point[pp]),
&(cachePtr->leaderline->point[0]),
&(cachePtr->leaderline->point[1])) == MS_TRUE) {
return(MS_FALSE);
}
}
if(curCachePtr->leaderline) {
/* TODO: check intersection of leader lines, not only bbox test ? */
if(msRectOverlap(curCachePtr->leaderbbox, cachePtr->leaderbbox)) {
return MS_FALSE;
}
}
}
}
} /* i */
i = 0; /* Start over with 1st label of next slot */
} /* p */
return MS_TRUE;
}
/* msGetLabelCacheMember()
**
** Returns label cache members by index, making all members of the cache
** appear as if they were stored in a single array indexed from 0 to numlabels.
**
** Returns NULL if requested index is out of range.
*/
labelCacheMemberObj *msGetLabelCacheMember(labelCacheObj *labelcache, int i)
{
if(i >= 0 && i < labelcache->numlabels) {
int p;
for(p=0; p<MS_MAX_LABEL_PRIORITY; p++) {
if (i < labelcache->slots[p].numlabels )
return &(labelcache->slots[p].labels[i]); /* Found it */
else
i -= labelcache->slots[p].numlabels; /* Check next slots */
}
}
/* Out of range / not found */
return NULL;
}
int msInitFontSet(fontSetObj *fontset)
{
fontset->filename = NULL;
/* fontset->fonts = NULL; */
initHashTable(&(fontset->fonts));
fontset->numfonts = 0;
fontset->map = NULL;
return( 0 );
}
int msFreeFontSet(fontSetObj *fontset)
{
if (fontset->filename)
free(fontset->filename);
fontset->filename = NULL;
if (&(fontset->fonts))
msFreeHashItems(&(fontset->fonts));
/* fontset->fonts = NULL; */
fontset->numfonts = 0;
return( 0 );
}
int msLoadFontSet(fontSetObj *fontset, mapObj *map)
{
FILE *stream;
char buffer[MS_BUFFER_LENGTH];
char alias[64], file1[MS_PATH_LENGTH], file2[MS_PATH_LENGTH];
char *path;
char szPath[MS_MAXPATHLEN];
int i;
int bFullPath = 0;
if(fontset->numfonts != 0) /* already initialized */
return(0);
if(!fontset->filename)
return(0);
fontset->map = (mapObj *)map;
path = msGetPath(fontset->filename);
/* fontset->fonts = msCreateHashTable(); // create font hash */
/* if(!fontset->fonts) { */
/* msSetError(MS_HASHERR, "Error initializing font hash.", "msLoadFontSet()"); */
/* return(-1); */
/* } */
stream = fopen( msBuildPath(szPath, fontset->map->mappath, fontset->filename), "r");
if(!stream) {
msSetError(MS_IOERR, "Error opening fontset %s.", "msLoadFontset()",
fontset->filename);
return(-1);
}
i = 0;
while(fgets(buffer, MS_BUFFER_LENGTH, stream)) { /* while there's something to load */
if(buffer[0] == '#' || buffer[0] == '\n' || buffer[0] == '\r' || buffer[0] == ' ')
continue; /* skip comments and blank lines */
sscanf(buffer,"%s %s", alias, file1);
if (!(*file1) || !(*alias) || (strlen(file1) <= 0))
continue;
bFullPath = 0;
#if defined(_WIN32) && !defined(__CYGWIN__)
if (file1[0] == '\\' || (strlen(file1) > 1 && (file1[1] == ':')))
bFullPath = 1;
#else
if(file1[0] == '/')
bFullPath = 1;
#endif
if(bFullPath) { /* already full path */
msInsertHashTable(&(fontset->fonts), alias, file1);
} else {
snprintf(file2, sizeof(file2), "%s%s", path, file1);
/* msInsertHashTable(fontset->fonts, alias, file2); */
/*
** msBuildPath is use here, but if we have to save the fontset file
** the msBuildPath must be done everywhere the fonts are used and
** removed here.
*/
msInsertHashTable(&(fontset->fonts), alias,
msBuildPath(szPath, fontset->map->mappath, file2));
}