forked from MapServer/MapServer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
maplabel.c
1264 lines (1112 loc) · 41 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 <float.h>
#include "mapserver.h"
#include "fontcache.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(char *text, char wrap, int maxlength)
{
if(!text) /*not an error if no text*/
return text;
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) {
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*/
}
}
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;
}
}
}
void initTextPath(textPathObj *ts) {
memset(ts,0,sizeof(*ts));
}
int WARN_UNUSED msLayoutTextSymbol(mapObj *map, textSymbolObj *ts, textPathObj *tgret);
#if defined(USE_EXTENDED_DEBUG) && 0
static void msDebugTextPath(textSymbolObj *ts) {
int i;
msDebug("text: %s\n",ts->annotext);
if(ts->textpath) {
for(i=0;i<ts->textpath->numglyphs; i++) {
glyphObj *g = &ts->textpath->glyphs[i];
msDebug("glyph %d: pos: %f %f\n",g->glyph->key.codepoint,g->pnt.x,g->pnt.y);
}
} else {
msDebug("no glyphs\n");
}
msDebug("\n=========================================\n");
}
#endif
int msComputeTextPath(mapObj *map, textSymbolObj *ts) {
textPathObj *tgret = msSmallMalloc(sizeof(textPathObj));
assert(ts->annotext && *ts->annotext);
initTextPath(tgret);
ts->textpath = tgret;
tgret->absolute = 0;
tgret->glyph_size = ts->label->size * ts->scalefactor;
tgret->glyph_size = MS_MAX(tgret->glyph_size, ts->label->minsize * ts->resolutionfactor);
tgret->glyph_size = MS_NINT(MS_MIN(tgret->glyph_size, ts->label->maxsize * ts->resolutionfactor));
tgret->line_height = ceil(tgret->glyph_size * 1.33);
return msLayoutTextSymbol(map,ts,tgret);
}
void initTextSymbol(textSymbolObj *ts) {
memset(ts,0,sizeof(*ts));
}
void freeTextPath(textPathObj *tp) {
free(tp->glyphs);
if(tp->bounds.poly) {
free(tp->bounds.poly->point);
free(tp->bounds.poly);
}
}
void freeTextSymbol(textSymbolObj *ts) {
if(ts->textpath) {
freeTextPath(ts->textpath);
free(ts->textpath);
}
if(ts->label->numstyles) {
if(ts->style_bounds) {
int i;
for(i=0;i<ts->label->numstyles; i++) {
if(ts->style_bounds[i]) {
if(ts->style_bounds[i]->poly) {
free(ts->style_bounds[i]->poly->point);
free(ts->style_bounds[i]->poly);
}
free(ts->style_bounds[i]);
}
}
free(ts->style_bounds);
}
}
free(ts->annotext);
if(freeLabel(ts->label) == MS_SUCCESS) {
free(ts->label);
}
}
void msCopyTextPath(textPathObj *dst, textPathObj *src) {
int i;
*dst = *src;
if(src->bounds.poly) {
dst->bounds.poly = msSmallMalloc(sizeof(lineObj));
dst->bounds.poly->numpoints = src->bounds.poly->numpoints;
for(i=0; i<src->bounds.poly->numpoints; i++) {
dst->bounds.poly->point[i] = src->bounds.poly->point[i];
}
} else {
dst->bounds.poly = NULL;
}
if(dst->numglyphs > 0) {
dst->glyphs = msSmallMalloc(dst->numglyphs * sizeof(glyphObj));
for(i=0; i<dst->numglyphs; i++)
dst->glyphs[i] = src->glyphs[i];
}
}
void msCopyTextSymbol(textSymbolObj *dst, textSymbolObj *src) {
*dst = *src;
MS_REFCNT_INCR(src->label);
dst->annotext = msStrdup(src->annotext);
if(dst->textpath) {
dst->textpath = msSmallMalloc(sizeof(textPathObj));
msCopyTextPath(dst->textpath,src->textpath);
}
}
static int labelNeedsDeepCopy(labelObj *label) {
int i;
if(label->numbindings > 0) return MS_TRUE;
for(i=0; i<label->numstyles; i++) {
if(label->styles[i]->numbindings>0) {
return MS_TRUE;
}
}
return MS_FALSE;
}
void msPopulateTextSymbolForLabelAndString(textSymbolObj *ts, labelObj *l, char *string, double scalefactor, double resolutionfactor, label_cache_mode cache) {
if(cache == duplicate_always) {
ts->label = msSmallMalloc(sizeof(labelObj));
initLabel(ts->label);
msCopyLabel(ts->label,l);
} else if(cache == duplicate_never) {
ts->label = l;
MS_REFCNT_INCR(l);
} else if(cache == duplicate_if_needed && labelNeedsDeepCopy(l)) {
ts->label = msSmallMalloc(sizeof(labelObj));
initLabel(ts->label);
msCopyLabel(ts->label,l);
} else {
ts->label = l;
MS_REFCNT_INCR(l);
}
ts->resolutionfactor = resolutionfactor;
ts->scalefactor = scalefactor;
ts->annotext = string; /* we take the ownership of the annotation text */
ts->rotation = l->angle * MS_DEG_TO_RAD;
}
int msAddLabelGroup(mapObj *map, imageObj *image, int layerindex, int classindex, shapeObj *shape, pointObj *point, double featuresize)
{
int l,s, priority;
labelCacheSlotObj *cacheslot;
labelCacheMemberObj *cachePtr=NULL;
layerObj *layerPtr=NULL;
classObj *classPtr=NULL;
int numtextsymbols = 0;
textSymbolObj **textsymbols, *ts;
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 */
/* 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));
if(UNLIKELY(MS_FAILURE == MS_IMAGE_RENDERER(maskLayer->maskimage)->getRasterBufferHandle(maskLayer->maskimage,&rb))) {
return MS_FAILURE;
}
x = MS_NINT(point->x);
y = MS_NINT(point->y);
/* Using label repeatdistance, we might have a point with x/y below 0. See #4764 */
if (x >= 0 && x < rb.width && y >= 0 && y < rb.height) {
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;
}
}
} 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);
}
}
textsymbols = msSmallMalloc(classPtr->numlabels * sizeof(textSymbolObj*));
for(l=0; l<classPtr->numlabels; l++) {
labelObj *lbl = classPtr->labels[l];
char *annotext;
if(msGetLabelStatus(map,layerPtr,shape,lbl) == MS_OFF) {
continue;
}
annotext = msShapeGetLabelAnnotation(layerPtr,shape,lbl);
if(!annotext) {
for(s=0;s<lbl->numstyles;l++) {
if(lbl->styles[s]->_geomtransform.type == MS_GEOMTRANSFORM_LABELPOINT)
break; /* we have a "symbol only label, so we shouldn't skip this label */
}
if(s == lbl->numstyles) {
continue; /* no anno text, and no label symbols */
}
}
ts = msSmallMalloc(sizeof(textSymbolObj));
initTextSymbol(ts);
msPopulateTextSymbolForLabelAndString(ts,lbl,annotext,layerPtr->scalefactor,image->resolutionfactor, 1);
if(annotext && *annotext && lbl->autominfeaturesize && featuresize > 0) {
if(UNLIKELY(MS_FAILURE == msComputeTextPath(map,ts)))
return MS_FAILURE;
if(featuresize < (ts->textpath->bounds.bbox.maxx - ts->textpath->bounds.bbox.minx)) {
/* feature is too big to be drawn, skip it */
freeTextSymbol(ts);
free(ts);
continue;
}
}
textsymbols[numtextsymbols] = ts;
numtextsymbols++;
}
if(numtextsymbols == 0) {
free(textsymbols);
return MS_SUCCESS;
}
/* 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;
cachePtr->point = *point; /* the actual label point */
cachePtr->leaderline = NULL;
cachePtr->leaderbbox = NULL;
cachePtr->markerid = -1;
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 */
double w, h;
if(msGetMarkerSize(map, 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;
}
cacheslot->markers[cacheslot->nummarkers].bounds.minx = (point->x - .5 * w);
cacheslot->markers[cacheslot->nummarkers].bounds.miny = (point->y - .5 * h);
cacheslot->markers[cacheslot->nummarkers].bounds.maxx = cacheslot->markers[cacheslot->nummarkers].bounds.minx + (w-1);
cacheslot->markers[cacheslot->nummarkers].bounds.maxy = cacheslot->markers[cacheslot->nummarkers].bounds.miny + (h-1);
cacheslot->markers[cacheslot->nummarkers].id = cacheslot->numlabels;
cachePtr->markerid = cacheslot->nummarkers;
cacheslot->nummarkers++;
}
cachePtr->textsymbols = textsymbols;
cachePtr->numtextsymbols = numtextsymbols;
cacheslot->numlabels++;
return(MS_SUCCESS);
}
int msAddLabel(mapObj *map, imageObj *image, labelObj *label, int layerindex, int classindex,
shapeObj *shape, pointObj *point, double featuresize, textSymbolObj *ts)
{
int i;
labelCacheSlotObj *cacheslot;
labelCacheMemberObj *cachePtr=NULL;
char *annotext = NULL;
layerObj *layerPtr;
classObj *classPtr;
layerPtr=GET_LAYER(map,layerindex);
assert(layerPtr);
assert(classindex < layerPtr->numclasses);
classPtr = layerPtr->class[classindex];
assert(label);
if(ts)
annotext = ts->annotext;
else if(shape)
annotext = msShapeGetLabelAnnotation(layerPtr,shape,label);
if(!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 */
if(ts) {
freeTextSymbol(ts);
free(ts);
}
return MS_SUCCESS;
}
}
if(classPtr->leader) {
if(ts && ts->textpath && ts->textpath->absolute) {
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));
if(UNLIKELY(MS_FAILURE == MS_IMAGE_RENDERER(maskLayer->maskimage)->getRasterBufferHandle(maskLayer->maskimage, &rb))) {
return MS_FAILURE;
}
assert(rb.type == MS_BUFFER_BYTE_RGBA);
if (point) {
int x = MS_NINT(point->x);
int y = MS_NINT(point->y);
/* Using label repeatdistance, we might have a point with x/y below 0. See #4764 */
if (x >= 0 && x < rb.width && y >= 0 && y < rb.height) {
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 */
if(ts) {
freeTextSymbol(ts);
free(ts);
}
return MS_SUCCESS;
}
}
} else if (ts && ts->textpath) {
int i = 0;
for (i = 0; i < ts->textpath->numglyphs; i++) {
int x = MS_NINT(ts->textpath->glyphs[i].pnt.x);
int y = MS_NINT(ts->textpath->glyphs[i].pnt.y);
if (x >= 0 && x < rb.width && y >= 0 && y < rb.height) {
alphapixptr = rb.data.rgba.a + rb.data.rgba.row_step * y + rb.data.rgba.pixel_step*x;
if (!*alphapixptr) {
freeTextSymbol(ts);
free(ts);
return MS_SUCCESS;
}
}
}
}
} 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);
}
}
if(!ts) {
ts = msSmallMalloc(sizeof(textSymbolObj));
initTextSymbol(ts);
msPopulateTextSymbolForLabelAndString(ts,label,annotext,layerPtr->scalefactor,image->resolutionfactor, 1);
}
if(annotext && label->autominfeaturesize && featuresize > 0) {
if(!ts->textpath) {
if(UNLIKELY(MS_FAILURE == msComputeTextPath(map,ts)))
return MS_FAILURE;
}
if(featuresize > (ts->textpath->bounds.bbox.maxx - ts->textpath->bounds.bbox.minx)) {
/* feature is too big to be drawn, skip it */
freeTextSymbol(ts);
free(ts);
return MS_SUCCESS;
}
}
/* 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;
#ifdef include_deprecated
if(shape) {
cachePtr->shapetype = shape->type;
} else {
cachePtr->shapetype = MS_SHAPE_POINT;
}
#endif
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 */
} else {
assert(ts && ts->textpath && ts->textpath->absolute && ts->textpath->numglyphs>0);
/* Use the middle point of the labelpath for mindistance calculations */
cachePtr->point = ts->textpath->glyphs[ts->textpath->numglyphs/2].pnt;
}
/* copy the label */
cachePtr->numtextsymbols = 1;
cachePtr->textsymbols = (textSymbolObj **) msSmallMalloc(sizeof(textSymbolObj*));
cachePtr->textsymbols[0] = ts;
cachePtr->markerid = -1;
cachePtr->status = MS_FALSE;
if(layerPtr->type == MS_LAYER_POINT && classPtr->numstyles > 0) { /* cache the marker placement, it's already on the map */
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;
/* 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, classPtr->styles[0], &w, &h, layerPtr->scalefactor) != MS_SUCCESS)
return(MS_FAILURE);
cacheslot->markers[cacheslot->nummarkers].bounds.minx = (point->x - .5 * w);
cacheslot->markers[cacheslot->nummarkers].bounds.miny = (point->y - .5 * h);
cacheslot->markers[cacheslot->nummarkers].bounds.maxx = cacheslot->markers[cacheslot->nummarkers].bounds.minx + (w-1);
cacheslot->markers[cacheslot->nummarkers].bounds.maxy = cacheslot->markers[cacheslot->nummarkers].bounds.miny + (h-1);
cacheslot->markers[i].id = cacheslot->numlabels;
cachePtr->markerid = i;
cacheslot->nummarkers++;
}
}
cacheslot->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, lineObj *lpoly, rectObj *bounds, int gutter)
{
int j;
/* do a bbox test first */
if(bounds->minx >= gutter &&
bounds->miny >= gutter &&
bounds->maxx < width-gutter &&
bounds->maxy < height-gutter) {
return MS_TRUE;
}
if(lpoly) {
for(j=1; j<lpoly->numpoints; j++) {
if(lpoly->point[j].x < gutter) return(MS_FALSE);
if(lpoly->point[j].x >= width-gutter) return(MS_FALSE);
if(lpoly->point[j].y < gutter) return(MS_FALSE);
if(lpoly->point[j].y >= height-gutter) return(MS_FALSE);
}
} else {
/* if no poly, then return false as the boundong box intersected */
return MS_FALSE;
}
return(MS_TRUE);
}
void insertRenderedLabelMember(mapObj *map, labelCacheMemberObj *cachePtr) {
if(map->labelcache.num_rendered_members == map->labelcache.num_allocated_rendered_members) {
if(map->labelcache.num_rendered_members == 0) {
map->labelcache.num_allocated_rendered_members = 50;
} else {
map->labelcache.num_allocated_rendered_members *= 2;
}
map->labelcache.rendered_text_symbols = msSmallRealloc(map->labelcache.rendered_text_symbols,
map->labelcache.num_allocated_rendered_members * sizeof(labelCacheMemberObj*));
}
map->labelcache.rendered_text_symbols[map->labelcache.num_rendered_members++] = cachePtr;
}
static inline int testSegmentLabelBBoxIntersection(const rectObj *leaderbbox, const pointObj *lp1,
const pointObj *lp2, const label_bounds *test) {
if(msRectOverlap(leaderbbox, &test->bbox)) {
if(test->poly) {
int pp;
for(pp=1; pp<test->poly->numpoints; pp++) {
if(msIntersectSegments(
&(test->poly->point[pp-1]),
&(test->poly->point[pp]),
lp1,lp2) == MS_TRUE) {
return(MS_FALSE);
}
}
} else {
pointObj tp1,tp2;
tp1.x = test->bbox.minx;
tp1.y = test->bbox.miny;
tp2.x = test->bbox.minx;
tp2.y = test->bbox.maxy;
if(msIntersectSegments(lp1,lp2,&tp1,&tp2))
return MS_FALSE;
tp2.x = test->bbox.maxx;
tp2.y = test->bbox.miny;
if(msIntersectSegments(lp1,lp2,&tp1,&tp2))
return MS_FALSE;
tp1.x = test->bbox.maxx;
tp1.y = test->bbox.maxy;
tp2.x = test->bbox.minx;
tp2.y = test->bbox.maxy;
if(msIntersectSegments(lp1,lp2,&tp1,&tp2))
return MS_FALSE;
tp2.x = test->bbox.maxx;
tp2.y = test->bbox.miny;
if(msIntersectSegments(lp1,lp2,&tp1,&tp2))
return MS_FALSE;
}
}
return MS_TRUE;
}
int msTestLabelCacheLeaderCollision(mapObj *map, pointObj *lp1, pointObj *lp2) {
int p;
rectObj leaderbbox;
leaderbbox.minx = MS_MIN(lp1->x,lp2->x);
leaderbbox.maxx = MS_MAX(lp1->x,lp2->x);
leaderbbox.miny = MS_MIN(lp1->y,lp2->y);
leaderbbox.maxy = MS_MAX(lp1->y,lp2->y);
for(p=0; p<map->labelcache.num_rendered_members; p++) {
labelCacheMemberObj *curCachePtr= map->labelcache.rendered_text_symbols[p];
if(msRectOverlap(&leaderbbox, &(curCachePtr->bbox))) {
/* leaderbbox interesects with the curCachePtr's global bbox */
int t;
for(t=0; t<curCachePtr->numtextsymbols; t++) {
int s;
textSymbolObj *ts = curCachePtr->textsymbols[t];
/* check for intersect with textpath */
if(ts->textpath && testSegmentLabelBBoxIntersection(&leaderbbox, lp1, lp2, &ts->textpath->bounds) == MS_FALSE) {
return MS_FALSE;
}
/* check for intersect with label's labelpnt styles */
if(ts->style_bounds) {
for(s=0; s<ts->label->numstyles; s++) {
if(ts->label->styles[s]->_geomtransform.type == MS_GEOMTRANSFORM_LABELPOINT) {
if(testSegmentLabelBBoxIntersection(&leaderbbox, lp1,lp2, ts->style_bounds[s]) == MS_FALSE) {
return MS_FALSE;
}
}
}
}
}
if(curCachePtr->leaderbbox) {
if(msIntersectSegments(lp1,lp2,&(curCachePtr->leaderline->point[0]), &(curCachePtr->leaderline->point[1])) == MS_TRUE) {
return MS_FALSE;
}
}
}
}
return MS_TRUE;
}
/* msTestLabelCacheCollisions()
**
** Compares label bounds (in *bounds) against labels already drawn and markers from cache and
** returns MS_FALSE if it collides with another label, or collides with a marker.
**
** This function is used by the various msDrawLabelCacheXX() implementations.
int msTestLabelCacheCollisions(mapObj *map, labelCacheMemberObj *cachePtr, label_bounds *bounds,
int current_priority, int current_label);
*/
int msTestLabelCacheCollisions(mapObj *map, labelCacheMemberObj *cachePtr, label_bounds *lb,
int current_priority, int current_label)
{
labelCacheObj *labelcache = &(map->labelcache);
int i, p, ll;
/*
* Check against image bounds first
*/
if(!cachePtr->textsymbols[0]->label->partials) {
if(labelInImage(map->width, map->height, lb->poly, &lb->bbox, labelcache->gutter) == MS_FALSE) {
return MS_FALSE;
}
}
/* 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(NULL, &markerslot->markers[ll].bounds, lb->poly, &lb->bbox ) == MS_TRUE ) {
return MS_FALSE;
}
}
}
}
for(p=0; p<labelcache->num_rendered_members; p++) {
labelCacheMemberObj *curCachePtr= labelcache->rendered_text_symbols[p];
if(msRectOverlap(&curCachePtr->bbox,&lb->bbox)) {
for(i=0; i<curCachePtr->numtextsymbols; i++) {
int j;
textSymbolObj *ts = curCachePtr->textsymbols[i];
if(ts->textpath && intersectLabelPolygons(ts->textpath->bounds.poly, &ts->textpath->bounds.bbox, lb->poly, &lb->bbox) == MS_TRUE ) {
return MS_FALSE;
}
if(ts->style_bounds) {
for(j=0;j<ts->label->numstyles;j++) {
if(ts->style_bounds[j] && ts->label->styles[j]->_geomtransform.type == MS_GEOMTRANSFORM_LABELPOINT) {
if(intersectLabelPolygons(ts->style_bounds[j]->poly, &ts->style_bounds[j]->bbox,
lb->poly, &lb->bbox)) {
return MS_FALSE;
}
}
}
}
}
}
if(curCachePtr->leaderline) {
if(testSegmentLabelBBoxIntersection(curCachePtr->leaderbbox, &curCachePtr->leaderline->point[0],
&curCachePtr->leaderline->point[1], lb) == MS_FALSE) {
return MS_FALSE;
}
}
}
return MS_TRUE;
}
/* utility function to get the rect of a string outside of a rendering loop, i.e.
without going through textpath layouts */
int msGetStringSize(mapObj *map, labelObj *label, int size, char *string, rectObj *r) {
textSymbolObj ts;
double lsize = label->size;
initTextSymbol(&ts);
label->size = size;
msPopulateTextSymbolForLabelAndString(&ts,label,msStrdup(string),1,1,0);
if(UNLIKELY(MS_FAILURE == msGetTextSymbolSize(map,&ts,r)))
return MS_FAILURE;
label->size = lsize;
freeTextSymbol(&ts);
return MS_SUCCESS;
}
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));
}
i++;
}
fontset->numfonts = i;
fclose(stream); /* close the file */
free(path);
return(0);
}
int msGetTextSymbolSize(mapObj *map, textSymbolObj *ts, rectObj *r) {
if(!ts->textpath) {
if(UNLIKELY(MS_FAILURE == msComputeTextPath(map,ts)))
return MS_FAILURE;
}
*r = ts->textpath->bounds.bbox;
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
** LL corner of the text to be rendered, this is first line for TrueType fonts.
*/
#define MARKER_SLOP 2
pointObj get_metrics(pointObj *p, int position, textPathObj *tp, int ox, int oy, double rotation, int buffer, label_bounds *bounds)
{
pointObj q;
double x1=0, y1=0, x2=0, y2=0;
double sin_a,cos_a;
double w, h, x, y;
w = tp->bounds.bbox.maxx - tp->bounds.bbox.minx;
h = tp->bounds.bbox.maxy - tp->bounds.bbox.miny;
switch(position) {
case MS_UL:
x1 = -w - ox;
y1 = -oy;
break;
case MS_UC:
x1 = -(w/2.0);
y1 = -oy - MARKER_SLOP;
break;
case MS_UR:
x1 = ox;
y1 = -oy;
break;
case MS_CL:
x1 = -w - ox - MARKER_SLOP;
if(oy > 0 && tp->numlines == 1)
y1 = oy;
else
y1 = (h/2.0);
break;
case MS_CC:
x1 = -(w/2.0) + ox;
y1 = (h/2.0) + oy;
break;
case MS_CR:
x1 = ox + MARKER_SLOP;
if(oy > 0 && tp->numlines == 1)
y1 = oy;
else
y1 = (h/2.0);
break;
case MS_LL:
x1 = -w - ox;
y1 = h + oy;
break;
case MS_LC:
x1 = -(w/2.0);
y1 = h + oy + MARKER_SLOP;
break;
case MS_LR:
x1 = ox;