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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
|
/***************************************************************************
* map_draw_cairo.c
*
* Copyright 2005 Ian McIntosh
* ian_mcintosh@linuxadvocate.org
****************************************************************************/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
//#define ENABLE_TIMING
#define RENDERING_THREAD_YIELD // do nothing
#define ACCEPTABLE_LINE_LABEL_OVERDRAW_IN_PIXELS_SQUARED (38*38)
#define HACK_AROUND_CAIRO_LINE_CAP_BUG // enable to ensure roads have rounded caps if the style dictates
#define ROAD_FONT "Bitstream Vera Sans"
#define AREA_FONT "Bitstream Vera Sans" // "Bitstream Charter"
#include <gdk/gdkx.h>
#include <cairo.h>
#include <gtk/gtk.h>
#include <math.h>
#include "gui.h"
#include "map.h"
#include "mainwindow.h"
#include "util.h"
#include "db.h"
#include "road.h"
#include "point.h"
#include "layers.h"
#include "location.h"
#include "locationset.h"
#include "scenemanager.h"
// Draw whole layers
static void map_draw_cairo_layer_polygons(map_t* pMap, cairo_t* pCairo, rendermetrics_t* pRenderMetrics, GPtrArray* pPointStringsArray, sublayerstyle_t* pSubLayerStyle, textlabelstyle_t* pLabelStyle);
static void map_draw_cairo_layer_lines(map_t* pMap, cairo_t* pCairo, rendermetrics_t* pRenderMetrics, GPtrArray* pPointStringsArray, sublayerstyle_t* pSubLayerStyle, textlabelstyle_t* pLabelStyle);
static void map_draw_cairo_layer_line_labels(map_t* pMap, cairo_t* pCairo, rendermetrics_t* pRenderMetrics, GPtrArray* pPointStringsArray, sublayerstyle_t* pSubLayerStyle, textlabelstyle_t* pLabelStyle);
static void map_draw_cairo_layer_polygon_labels(map_t* pMap, cairo_t* pCairo, rendermetrics_t* pRenderMetrics, GPtrArray* pPointStringsArray, sublayerstyle_t* pSubLayerStyle, textlabelstyle_t* pLabelStyle);
static void map_draw_cairo_locations(map_t* pMap, cairo_t* pCairo, rendermetrics_t* pRenderMetrics);
// Draw a single line/polygon/point
static void map_draw_cairo_background(map_t* pMap, cairo_t *pCairo);
static void map_draw_cairo_layer_points(map_t* pMap, cairo_t* pCairo, rendermetrics_t* pRenderMetrics, GPtrArray* pLocationsArray);
// Draw a single line/polygon label
static void map_draw_cairo_line_label(map_t* pMap, cairo_t *pCairo, textlabelstyle_t* pLabelStyle, rendermetrics_t* pRenderMetrics, pointstring_t* pPointString, gdouble fLineWidth, const gchar* pszLabel);
static void map_draw_cairo_polygon_label(map_t* pMap, cairo_t *pCairo, textlabelstyle_t* pLabelStyle, rendermetrics_t* pRenderMetrics, pointstring_t* pPointString, const gchar* pszLabel);
// static void map_draw_cairo_crosshair(map_t* pMap, cairo_t* pCairo, rendermetrics_t* pRenderMetrics);
void map_draw_cairo(map_t* pMap, rendermetrics_t* pRenderMetrics, GdkPixmap* pPixmap, gint nDrawFlags)
{
// 1. Set draw target to X Drawable
Display* dpy;
Drawable drawable;
dpy = gdk_x11_drawable_get_xdisplay(pPixmap);
drawable = gdk_x11_drawable_get_xid(pPixmap);
cairo_t* pCairo = cairo_create ();
cairo_set_target_drawable(pCairo, dpy, drawable);
// 2. Rendering
TIMER_BEGIN(maptimer, "BEGIN RENDER MAP (cairo)");
cairo_save(pCairo);
// 2.1. Settings for all rendering
cairo_set_fill_rule(pCairo, CAIRO_FILL_RULE_WINDING);
// 2.2. Draw Background
if(nDrawFlags & DRAWFLAG_GEOMETRY) {
map_draw_cairo_background(pMap, pCairo);
}
// 2.3. Render Layers
gint i;
for(i=0 ; i<NUM_ELEMS(layerdraworder) ; i++) {
gint nLayer = layerdraworder[i].nLayer;
gint nSubLayer = layerdraworder[i].nSubLayer;
gint nRenderType = layerdraworder[i].eSubLayerRenderType;
if(nRenderType == SUBLAYER_RENDERTYPE_LINES) {
if(nDrawFlags & DRAWFLAG_GEOMETRY) {
map_draw_cairo_layer_lines(pMap, pCairo,
pRenderMetrics,
/* geometry */ pMap->m_apLayerData[nLayer]->m_pPointStringsArray,
/* style */ &(g_aLayers[nLayer]->m_Style.m_aSubLayers[nSubLayer]),
&(g_aLayers[nLayer]->m_TextLabelStyle));
}
}
else if(nRenderType == SUBLAYER_RENDERTYPE_POLYGONS) {
if(nDrawFlags & DRAWFLAG_GEOMETRY) {
map_draw_cairo_layer_polygons(pMap, pCairo,
pRenderMetrics,
/* geometry */ pMap->m_apLayerData[nLayer]->m_pPointStringsArray,
/* style */ &(g_aLayers[nLayer]->m_Style.m_aSubLayers[nSubLayer]),
&(g_aLayers[nLayer]->m_TextLabelStyle));
}
}
else if(nRenderType == SUBLAYER_RENDERTYPE_LINE_LABELS) {
if(nDrawFlags & DRAWFLAG_LABELS) {
map_draw_cairo_layer_line_labels(pMap, pCairo,
pRenderMetrics,
/* geometry */ pMap->m_apLayerData[nLayer]->m_pPointStringsArray,
/* style */ &(g_aLayers[nLayer]->m_Style.m_aSubLayers[nSubLayer]),
&(g_aLayers[nLayer]->m_TextLabelStyle));
}
}
else if(nRenderType == SUBLAYER_RENDERTYPE_POLYGON_LABELS) {
if(nDrawFlags & DRAWFLAG_LABELS) {
map_draw_cairo_layer_polygon_labels(pMap, pCairo,
pRenderMetrics,
/* geometry */ pMap->m_apLayerData[nLayer]->m_pPointStringsArray,
/* style */ &(g_aLayers[nLayer]->m_Style.m_aSubLayers[nSubLayer]),
&(g_aLayers[nLayer]->m_TextLabelStyle));
}
}
}
// map_draw_cairo_locations(pMap, pCairo, pRenderMetrics);
// 4. Cleanup
cairo_restore(pCairo);
TIMER_END(maptimer, "END RENDER MAP (cairo)");
}
// ==============================================
// Begin map_draw_cairo_* functions
// ==============================================
// Background
static void map_draw_cairo_background(map_t* pMap, cairo_t *pCairo)
{
// XXX: Don't hardcode background color
cairo_save(pCairo);
cairo_set_rgb_color(pCairo, 247/255.0, 235/255.0, 230/255.0);
cairo_rectangle(pCairo, 0, 0, pMap->m_MapDimensions.m_uWidth, pMap->m_MapDimensions.m_uHeight);
cairo_fill(pCairo);
cairo_restore(pCairo);
}
//
// Draw a whole layer of line strings
//
void map_draw_cairo_layer_line_labels(map_t* pMap, cairo_t* pCairo, rendermetrics_t* pRenderMetrics, GPtrArray* pPointStringsArray, sublayerstyle_t* pSubLayerStyle, textlabelstyle_t* pLabelStyle)
{
gint i;
gdouble fLineWidth = pSubLayerStyle->m_afLineWidths[pRenderMetrics->m_nZoomLevel-1];
if(fLineWidth == 0) return;
gdouble fFontSize = pLabelStyle->m_afFontSizeAtZoomLevel[pRenderMetrics->m_nZoomLevel-1];
if(fFontSize == 0) return;
gchar* pszFontFamily = ROAD_FONT;
// set font for whole layer
cairo_save(pCairo);
cairo_select_font(pCairo, pszFontFamily, CAIRO_FONT_SLANT_NORMAL, pLabelStyle->m_abBoldAtZoomLevel[pRenderMetrics->m_nZoomLevel-1] ? CAIRO_FONT_WEIGHT_BOLD : CAIRO_FONT_WEIGHT_NORMAL);
cairo_scale_font(pCairo, fFontSize);
for(i=0 ; i<pPointStringsArray->len ; i++) {
pointstring_t* pPointString = g_ptr_array_index(pPointStringsArray, i);
if(pPointString->m_pszName[0] != '\0') {
map_draw_cairo_line_label(pMap, pCairo, pLabelStyle, pRenderMetrics, pPointString, fLineWidth, pPointString->m_pszName);
}
}
cairo_restore(pCairo);
}
//
// Draw a whole layer of polygons
//
void map_draw_cairo_layer_polygon_labels(map_t* pMap, cairo_t* pCairo, rendermetrics_t* pRenderMetrics, GPtrArray* pPointStringsArray, sublayerstyle_t* pSubLayerStyle, textlabelstyle_t* pLabelStyle)
{
gdouble fFontSize = pLabelStyle->m_afFontSizeAtZoomLevel[pRenderMetrics->m_nZoomLevel-1];
if(fFontSize == 0) return;
gchar* pszFontFamily = AREA_FONT;
// set font for whole layer
cairo_save(pCairo);
cairo_select_font(pCairo, pszFontFamily, CAIRO_FONT_SLANT_NORMAL, pLabelStyle->m_abBoldAtZoomLevel[pRenderMetrics->m_nZoomLevel-1] ? CAIRO_FONT_WEIGHT_BOLD : CAIRO_FONT_WEIGHT_NORMAL);
cairo_scale_font(pCairo, fFontSize);
gint i;
for(i=0 ; i<pPointStringsArray->len ; i++) {
pointstring_t* pPointString = g_ptr_array_index(pPointStringsArray, i);
if(pPointString->m_pszName[0] != '\0') {
map_draw_cairo_polygon_label(pMap, pCairo, pLabelStyle, pRenderMetrics, pPointString, pPointString->m_pszName);
}
}
cairo_restore(pCairo);
}
void map_draw_cairo_layer_lines(map_t* pMap, cairo_t* pCairo, rendermetrics_t* pRenderMetrics, GPtrArray* pPointStringsArray, sublayerstyle_t* pSubLayerStyle, textlabelstyle_t* pLabelStyle)
{
mappoint_t* pPoint;
pointstring_t* pPointString;
gint iString;
gint iPoint;
gdouble fLineWidth = pSubLayerStyle->m_afLineWidths[pRenderMetrics->m_nZoomLevel-1];
if(fLineWidth <= 0.0) return; // Don't draw invisible lines
if(pSubLayerStyle->m_clrColor.m_fAlpha == 0.0) return; // invisible?
cairo_save(pCairo);
// Raise the tolerance way up for thin lines
gint nCapStyle = pSubLayerStyle->m_nCapStyle;
// XXX: HACK
// nCapStyle = CAIRO_LINE_CAP_SQUARE;
gdouble fTolerance;
if(fLineWidth > 12.0) { // huge line, low tolerance
fTolerance = 0.40;
}
else if(fLineWidth >= 6.0) { // medium line, medium tolerance
fTolerance = 0.8;
}
else {
if(nCapStyle == CAIRO_LINE_CAP_ROUND) {
//g_print("forcing round->square cap style\n");
nCapStyle = CAIRO_LINE_CAP_SQUARE;
}
if(fLineWidth >= 3.0) {
fTolerance = 1.2;
}
else { // smaller...
fTolerance = 10;
}
}
cairo_set_tolerance(pCairo, fTolerance);
cairo_set_line_join(pCairo, pSubLayerStyle->m_nJoinStyle);
cairo_set_line_cap(pCairo, nCapStyle); /* CAIRO_LINE_CAP_BUTT, CAIRO_LINE_CAP_ROUND, CAIRO_LINE_CAP_CAP */
if(g_aDashStyles[pSubLayerStyle->m_nDashStyle].m_nDashCount > 1) {
cairo_set_dash(pCairo, g_aDashStyles[pSubLayerStyle->m_nDashStyle].m_pafDashList, g_aDashStyles[pSubLayerStyle->m_nDashStyle].m_nDashCount, 0.0);
}
// Set layer attributes
cairo_set_rgb_color(pCairo, pSubLayerStyle->m_clrColor.m_fRed, pSubLayerStyle->m_clrColor.m_fGreen, pSubLayerStyle->m_clrColor.m_fBlue);
cairo_set_alpha(pCairo, pSubLayerStyle->m_clrColor.m_fAlpha);
cairo_set_line_width(pCairo, fLineWidth);
for(iString=0 ; iString<pPointStringsArray->len ; iString++) {
RENDERING_THREAD_YIELD;
pPointString = g_ptr_array_index(pPointStringsArray, iString);
if(pPointString->m_pPointsArray->len >= 2) {
pPoint = g_ptr_array_index(pPointString->m_pPointsArray, 0);
// go to index 0
cairo_move_to(pCairo, SCALE_X(pRenderMetrics, pPoint->m_fLongitude), SCALE_Y(pRenderMetrics, pPoint->m_fLatitude));
// start at index 1 (0 was used above)
for(iPoint=1 ; iPoint<pPointString->m_pPointsArray->len ; iPoint++) {
pPoint = g_ptr_array_index(pPointString->m_pPointsArray, iPoint);//~ g_print(" point (%.05f,%.05f)\n", ScaleX(pPoint->m_fLongitude), ScaleY(pPoint->m_fLatitude));
cairo_line_to(pCairo, SCALE_X(pRenderMetrics, pPoint->m_fLongitude), SCALE_Y(pRenderMetrics, pPoint->m_fLatitude));
}
#ifdef HACK_AROUND_CAIRO_LINE_CAP_BUG
cairo_stroke(pCairo); // this is wrong place for it (see below)
#endif
}
}
#ifndef HACK_AROUND_CAIRO_LINE_CAP_BUG
// this is correct place to stroke, but we can't do this until Cairo fixes this bug:
// http://cairographics.org/samples/xxx_multi_segment_caps.html
cairo_stroke(pCairo);
#endif
cairo_restore(pCairo);
}
void map_draw_cairo_layer_polygons(map_t* pMap, cairo_t* pCairo, rendermetrics_t* pRenderMetrics, GPtrArray* pPointStringsArray, sublayerstyle_t* pSubLayerStyle, textlabelstyle_t* pLabelStyle)
{
mappoint_t* pPoint;
pointstring_t* pPointString;
gdouble fLineWidth = pSubLayerStyle->m_afLineWidths[pRenderMetrics->m_nZoomLevel-1];
if(fLineWidth == 0.0) return; // Don't both drawing with line width 0
if(pSubLayerStyle->m_clrColor.m_fAlpha == 0.0) return; // invisible?
// Set layer attributes
cairo_set_rgb_color(pCairo, pSubLayerStyle->m_clrColor.m_fRed, pSubLayerStyle->m_clrColor.m_fGreen, pSubLayerStyle->m_clrColor.m_fBlue);
cairo_set_alpha(pCairo, pSubLayerStyle->m_clrColor.m_fAlpha);
cairo_set_line_width(pCairo, fLineWidth);
cairo_set_fill_rule(pCairo, CAIRO_FILL_RULE_EVEN_ODD);
cairo_set_line_join(pCairo, pSubLayerStyle->m_nJoinStyle);
cairo_set_line_cap(pCairo, pSubLayerStyle->m_nCapStyle); /* CAIRO_LINE_CAP_BUTT, CAIRO_LINE_CAP_ROUND, CAIRO_LINE_CAP_CAP */
// cairo_set_dash(pCairo, g_aDashStyles[pSubLayerStyle->m_nDashStyle].m_pfList, g_aDashStyles[pSubLayerStyle->m_nDashStyle].m_nCount, 0.0);
gint iString;
for(iString=0 ; iString<pPointStringsArray->len ; iString++) {
RENDERING_THREAD_YIELD;
pPointString = g_ptr_array_index(pPointStringsArray, iString);
if(pPointString->m_pPointsArray->len >= 3) {
pPoint = g_ptr_array_index(pPointString->m_pPointsArray, 0);
// move to index 0
cairo_move_to(pCairo, SCALE_X(pRenderMetrics, pPoint->m_fLongitude), SCALE_Y(pRenderMetrics, pPoint->m_fLatitude));
// start at index 1 (0 was used above)
gint iPoint;
for(iPoint=1 ; iPoint<pPointString->m_pPointsArray->len ; iPoint++) {
pPoint = g_ptr_array_index(pPointString->m_pPointsArray, iPoint);
cairo_line_to(pCairo, SCALE_X(pRenderMetrics, pPoint->m_fLongitude), SCALE_Y(pRenderMetrics, pPoint->m_fLatitude));
}
//cairo_close_path(pCairo);
}
else {
// g_print("pPointString->m_pPointsArray->len = %d\n", pPointString->m_pPointsArray->len);
}
// TODO: this is debugging of polygons.
// cairo_save(pCairo);
// cairo_set_rgb_color(pCairo, 1,0,0);
//
// if(pPointString->m_pPointsArray->len >= 3) {
// gdouble fRadius = 2;
// // start at index 1 (0 was used above)
// for(iPoint=0 ; iPoint<pPointString->m_pPointsArray->len ; iPoint++) {
// pPoint = g_ptr_array_index(pPointString->m_pPointsArray, iPoint);
// cairo_move_to(pCairo, SCALE_X(pRenderMetrics, pPoint->m_fLongitude), SCALE_Y(pRenderMetrics, pPoint->m_fLatitude));
//
// color_t clr;
// util_random_color(&clr);
// cairo_set_rgb_color(pCairo, clr.m_fRed, clr.m_fGreen, clr.m_fBlue);
//
// gchar buf[20];
// g_snprintf(buf, 20, "%d", iPoint);
// //~ // cairo_select_font(pCairo, "Monospace", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
// cairo_text_path(pCairo, buf);
//
// cairo_arc(pCairo, SCALE_X(pRenderMetrics, pPoint->m_fLongitude), SCALE_Y(pRenderMetrics, pPoint->m_fLatitude), fRadius, 0, 360.0 * (M_PI/180.0));
// cairo_fill(pCairo);
// fRadius += 2;
// }
// }
// cairo_restore(pCairo);
}
cairo_fill(pCairo);
}
void map_draw_cairo_layer_points(map_t* pMap, cairo_t* pCairo, rendermetrics_t* pRenderMetrics, GPtrArray* pLocationsArray)
{
/* gdouble fRadius = map_degrees_to_pixels(pMap, 0.0007, map_get_zoomlevel(pMap));
gboolean bAddition = FALSE;
cairo_save(pCairo);
cairo_set_rgb_color(pCairo, 123.0/255.0, 48.0/255.0, 1.0);
cairo_set_alpha(pCairo, 0.3);
gint iLocation;
for(iLocation=0 ; iLocation<pLocationsArray->len ; iLocation++) {
location_t* pLocation = g_ptr_array_index(pLocationsArray, iLocation);
cairo_move_to(pCairo, SCALE_X(pRenderMetrics, pLocation->m_Coordinates.m_fLongitude), SCALE_Y(pRenderMetrics, pLocation->m_Coordinates.m_fLatitude));
cairo_arc(pCairo, SCALE_X(pRenderMetrics, pLocation->m_Coordinates.m_fLongitude), SCALE_Y(pRenderMetrics, pLocation->m_Coordinates.m_fLatitude), fRadius, 0, 2*M_PI);
if(bAddition) {
// fill each circle as we create them so they double-up on coverage
cairo_fill(pCairo);
}
}
cairo_set_fill_rule(pCairo, CAIRO_FILL_RULE_WINDING);
if(!bAddition) cairo_fill(pCairo);
// point centers
cairo_set_rgb_color(pCairo, 128.0/255.0, 128.0/255.0, 128.0/255.0);
cairo_set_alpha(pCairo, 1.0);
fRadius = 2;
for(iLocation=0 ; iLocation<pLocationsArray->len ; iLocation++) {
location_t* pLocation = g_ptr_array_index(pLocationsArray, iLocation);
cairo_move_to(pCairo, SCALE_X(pRenderMetrics, pLocation->m_Coordinates.m_fLongitude), SCALE_Y(pRenderMetrics, pLocation->m_Coordinates.m_fLatitude));
cairo_arc(pCairo, SCALE_X(pRenderMetrics, pLocation->m_Coordinates.m_fLongitude), SCALE_Y(pRenderMetrics, pLocation->m_Coordinates.m_fLatitude), fRadius, 0, 2*M_PI);
cairo_fill(pCairo);
}
cairo_set_fill_rule(pCairo, CAIRO_FILL_RULE_WINDING);
// cairo_fill(pCairo);
cairo_restore(pCairo);
*/
}
void map_draw_cairo_locations(map_t* pMap, cairo_t* pCairo, rendermetrics_t* pRenderMetrics)
{
/*
location_t loc;
location_t* pLoc = &loc;
// XXX: debug
pLoc->m_pszName = "2020 Massachusetts Ave., Cambridge, MA, 02140";
pLoc->m_Coordinates.m_fLongitude = 0;
pLoc->m_Coordinates.m_fLatitude = 0;
//
// Draw
//
cairo_save(pCairo);
gdouble fX = (gdouble)(gint)SCALE_X(pRenderMetrics, pLoc->m_Coordinates.m_fLongitude);
gdouble fY = (gdouble)(gint)SCALE_Y(pRenderMetrics, pLoc->m_Coordinates.m_fLatitude);
#define BIP (3)
#define BAP (1.5)
cairo_set_rgb_color(pCairo, 20/255.0, 20/255.0, 20/255.0);
cairo_set_line_width(pCairo, 2.5);
cairo_set_alpha(pCairo, 1.0);
cairo_rectangle(pCairo, fX - BIP, fY - BIP, BIP*2, BIP*2);
cairo_stroke(pCairo);
cairo_rectangle(pCairo, fX - BAP, fY - BAP, BAP*2, BAP*2);
cairo_fill(pCairo);
cairo_restore(pCairo);
*/
}
#define ROAD_MAX_SEGMENTS 100
#define DRAW_LABEL_BUFFER_LEN (200)
typedef struct labelposition {
guint m_nSegments;
guint m_iIndex;
gdouble m_fLength;
gdouble m_fRunTotal;
gdouble m_fMeanSlope;
gdouble m_fMeanAbsSlope;
gdouble m_fScore;
} labelposition_t;
//
// Draw a label along a 2-point line
//
static void map_draw_cairo_line_label_one_segment(map_t* pMap, cairo_t *pCairo, textlabelstyle_t* pLabelStyle, rendermetrics_t* pRenderMetrics, pointstring_t* pPointString, gdouble fLineWidth, const gchar* pszLabel)
{
// get permission to draw this label
if(FALSE == scenemanager_can_draw_label_at(pMap->m_pSceneManager, pszLabel, NULL)) {
return;
}
mappoint_t* pMapPoint1 = g_ptr_array_index(pPointString->m_pPointsArray, 0);
mappoint_t* pMapPoint2 = g_ptr_array_index(pPointString->m_pPointsArray, 1);
// swap first and second points such that the line goes left-to-right
if(pMapPoint2->m_fLongitude < pMapPoint1->m_fLongitude) {
mappoint_t* pTmp = pMapPoint1; pMapPoint1 = pMapPoint2; pMapPoint2 = pTmp;
}
// find extents
gdouble fMaxLat = max(pMapPoint1->m_fLatitude, pMapPoint2->m_fLatitude);
gdouble fMinLat = min(pMapPoint1->m_fLatitude, pMapPoint2->m_fLatitude);
gdouble fMaxLon = max(pMapPoint1->m_fLongitude, pMapPoint2->m_fLongitude);
gdouble fMinLon = min(pMapPoint1->m_fLongitude, pMapPoint2->m_fLongitude);
gdouble fX1 = SCALE_X(pRenderMetrics, pMapPoint1->m_fLongitude);
gdouble fY1 = SCALE_Y(pRenderMetrics, pMapPoint1->m_fLatitude);
gdouble fX2 = SCALE_X(pRenderMetrics, pMapPoint2->m_fLongitude);
gdouble fY2 = SCALE_Y(pRenderMetrics, pMapPoint2->m_fLatitude);
// rectangle overlap test
if(fMaxLat < pRenderMetrics->m_rWorldBoundingBox.m_A.m_fLatitude
|| fMaxLon < pRenderMetrics->m_rWorldBoundingBox.m_A.m_fLongitude
|| fMinLat > pRenderMetrics->m_rWorldBoundingBox.m_B.m_fLatitude
|| fMinLon > pRenderMetrics->m_rWorldBoundingBox.m_B.m_fLongitude)
{
return;
}
gdouble fRise = fY2 - fY1;
gdouble fRun = fX2 - fX1;
gdouble fLineLengthSquared = (fRun*fRun) + (fRise*fRise);
gchar* pszFontFamily = ROAD_FONT;
cairo_save(pCairo);
// get total width of string
cairo_text_extents_t extents;
cairo_text_extents(pCairo, pszLabel, &extents);
gdouble fLabelWidth = extents.width;
gdouble fFontHeight = extents.height;
// cairo_font_extents_t font_extents;
// cairo_current_font_extents(pCairo, &font_extents);
// gdouble fFontHeight = font_extents.ascent;
// text too big for line? XXX: This math is not right but good enough for now ;)
if((fLabelWidth * fLabelWidth) > (fLineLengthSquared + (ACCEPTABLE_LINE_LABEL_OVERDRAW_IN_PIXELS_SQUARED))) {
cairo_restore(pCairo);
return;
}
gdouble fLineLength = sqrt(fLineLengthSquared);
gdouble fTotalPadding = fLineLength - fLabelWidth;
// Normalize (make length = 1.0) by dividing by line length
// This makes a line with length 1 from the origin (0,0)
gdouble fNormalizedX = fRun / fLineLength;
gdouble fNormalizedY = fRise / fLineLength;
// NOTE: ***Swap the X and Y*** and normalize (make length = 1.0) by dividing by line length
// This makes a perpendicular line with length 1 from the origin (0,0)
gdouble fPerpendicularNormalizedX = fRise / fLineLength;
gdouble fPerpendicularNormalizedY = -(fRun / fLineLength);
// various places to try, in order
gdouble afPercentagesOfPadding[] = {0.5, 0.25, 0.75, 0.0, 1.0};
gint i;
for(i=0 ; i<NUM_ELEMS(afPercentagesOfPadding) ; i++) {
// try the next point along the line
gdouble fFrontPadding = fTotalPadding * afPercentagesOfPadding[i];
// align it on the line
// (front padding) |-text-| (back padding)
gdouble fDrawX = fX1 + (fNormalizedX * fFrontPadding);
gdouble fDrawY = fY1 + (fNormalizedY * fFrontPadding);
// center text vertically by shifting down by half of height
fDrawX -= (fPerpendicularNormalizedX * fFontHeight/2);
fDrawY -= (fPerpendicularNormalizedY * fFontHeight/2);
#define B (3) // a border around the text to keep things readable, in pixels
GdkPoint aBoundingPolygon[4];
// 0 is bottom left point
aBoundingPolygon[0].x = fDrawX - (fPerpendicularNormalizedX * B) - (fNormalizedX * B);
aBoundingPolygon[0].y = fDrawY - (fPerpendicularNormalizedX * B) - (fNormalizedX * B);
// 1 is upper left point
aBoundingPolygon[1].x = fDrawX + (fPerpendicularNormalizedX * (fFontHeight+B)) - (fNormalizedX * B); ;
aBoundingPolygon[1].y = fDrawY + (fPerpendicularNormalizedY * (fFontHeight+B)) - (fNormalizedY * B);;
// 2 is upper right point
aBoundingPolygon[2].x = aBoundingPolygon[1].x + (fNormalizedX * (fLabelWidth+B+B));
aBoundingPolygon[2].y = aBoundingPolygon[1].y + (fNormalizedY * (fLabelWidth+B+B));
// 3 is lower right point
aBoundingPolygon[3].x = fDrawX + (fNormalizedX * (fLabelWidth+B)) - (fPerpendicularNormalizedX * B);
aBoundingPolygon[3].y = fDrawY + (fNormalizedY * (fLabelWidth+B)) - (fPerpendicularNormalizedY * B);
// Ask whether we can draw here
if(FALSE == scenemanager_can_draw_polygon(pMap->m_pSceneManager, aBoundingPolygon, 4)) {
continue;
}
// do this after the padding calculation to possibly save some CPU cycles
gdouble fAngleInRadians = atan(fRise / fRun);
if(fRun < 0.0) fAngleInRadians += M_PI;
cairo_save(pCairo);
cairo_move_to(pCairo, fDrawX, fDrawY);
cairo_set_rgb_color(pCairo, 0.0,0.0,0.0);
cairo_set_alpha(pCairo, 1.0);
cairo_rotate(pCairo, fAngleInRadians);
gdouble fHaloSize = pLabelStyle->m_afHaloAtZoomLevel[pRenderMetrics->m_nZoomLevel-1];
if(fHaloSize >= 0) {
cairo_save(pCairo);
cairo_text_path(pCairo, pszLabel);
cairo_set_line_width(pCairo, fHaloSize);
cairo_set_rgb_color(pCairo, 1.0,1.0,1.0);
cairo_set_line_join(pCairo, CAIRO_LINE_JOIN_BEVEL);
//cairo_set_miter_limit(pCairo, 0.1);
cairo_stroke(pCairo);
cairo_restore(pCairo);
}
cairo_show_text(pCairo, pszLabel);
cairo_restore(pCairo);
// claim the space this took up and the label (so it won't be drawn twice)
scenemanager_claim_label(pMap->m_pSceneManager, pszLabel);
scenemanager_claim_polygon(pMap->m_pSceneManager, aBoundingPolygon, 4);
// success
break;
}
cairo_restore(pCairo);
}
//
// Draw a label along a multi-point line
//
static gint map_draw_cairo_line_label_position_sort(gconstpointer pA, gconstpointer pB)
{
labelposition_t *pPosA = *(labelposition_t **)pA;
labelposition_t *pPosB = *(labelposition_t **)pB;
if(pPosA->m_fScore > pPosB->m_fScore) return -1;
return 1;
}
static void map_draw_cairo_line_label(map_t* pMap, cairo_t *pCairo, textlabelstyle_t* pLabelStyle, rendermetrics_t* pRenderMetrics, pointstring_t* pPointString, gdouble fLineWidth, const gchar* pszLabel)
{
if(pPointString->m_pPointsArray->len < 2) return;
// pass off single segments to a specialized function
if(pPointString->m_pPointsArray->len == 2) {
map_draw_cairo_line_label_one_segment(pMap, pCairo, pLabelStyle, pRenderMetrics, pPointString, fLineWidth, pszLabel);
return;
}
// XXX
return;
if(pPointString->m_pPointsArray->len > ROAD_MAX_SEGMENTS) {
g_warning("not drawing label for road '%s' with > %d segments.\n", pszLabel, ROAD_MAX_SEGMENTS);
return;
}
// gdouble fFontSize = pLabelStyle->m_afFontSizeAtZoomLevel[pRenderMetrics->m_nZoomLevel-1];
// if(fFontSize == 0) return;
if(!scenemanager_can_draw_label_at(pMap->m_pSceneManager, pszLabel, NULL)) {
return;
}
gchar* pszFontFamily = ROAD_FONT;
cairo_save(pCairo);
// get total width of string
cairo_text_extents_t extents;
cairo_text_extents(pCairo, pszLabel, &extents);
// now find the ideal location
// place to store potential label positions
labelposition_t aPositions[ROAD_MAX_SEGMENTS];
gdouble aSlopes[ROAD_MAX_SEGMENTS];
mappoint_t* apPoints[ROAD_MAX_SEGMENTS];
gint nNumPoints = pPointString->m_pPointsArray->len;
mappoint_t* pMapPoint1;
mappoint_t* pMapPoint2;
// load point string into an array
gint iRead;
for(iRead=0 ; iRead<nNumPoints ; iRead++) {
apPoints[iRead] = g_ptr_array_index(pPointString->m_pPointsArray, iRead);
}
// measure total line length
gdouble fTotalLineLength = 0.0;
gint nPositions = 1;
gint iPoint, iPosition;
for(iPoint=1 ; iPoint<nNumPoints ; iPoint++) {
pMapPoint1 = apPoints[iPoint-1];
pMapPoint2 = apPoints[iPoint];
gdouble fX1 = SCALE_X(pRenderMetrics, pMapPoint1->m_fLongitude);
gdouble fY1 = SCALE_Y(pRenderMetrics, pMapPoint1->m_fLatitude);
gdouble fX2 = SCALE_X(pRenderMetrics, pMapPoint2->m_fLongitude);
gdouble fY2 = SCALE_Y(pRenderMetrics, pMapPoint2->m_fLatitude);
// determine slope of the line
gdouble fRise = fY2 - fY1;
gdouble fRun = fX2 - fX1;
gdouble fSlope = (fRun==0) ? G_MAXDOUBLE : (fRise/fRun);
gdouble fLineLength = sqrt((fRun*fRun) + (fRise*fRise));
aSlopes[iPoint] = fSlope;
aPositions[iPoint].m_nSegments = 0;
aPositions[iPoint].m_iIndex = iPoint;
aPositions[iPoint].m_fLength = 0.0;
aPositions[iPoint].m_fRunTotal = 0.0;
aPositions[iPoint].m_fMeanSlope = 0.0;
aPositions[iPoint].m_fMeanAbsSlope = 0.0;
for(iPosition = nPositions ; iPosition <= iPoint ; iPosition++) {
aPositions[iPosition].m_fLength += fLineLength;
aPositions[iPosition].m_fRunTotal += fRun;
aPositions[iPosition].m_nSegments++;
aPositions[iPosition].m_fMeanSlope += fSlope;
aPositions[iPosition].m_fMeanAbsSlope += (fSlope<0) ? -fSlope : fSlope;
if(aPositions[iPosition].m_fLength >= extents.width) nPositions++;
}
fTotalLineLength += fLineLength;
}
// if label is longer than entire line, we're out of luck
if(extents.width > fTotalLineLength) {
cairo_restore(pCairo);
return;
}
//GPtrArray *pPositionsPtrArray = g_ptr_array_new();
gdouble fMaxScore = 0;
gint iBestPosition;
for(iPosition = 1 ; iPosition < nPositions ; iPosition++) {
// finish calculating mean slope
aPositions[iPosition].m_fMeanSlope /= aPositions[iPosition].m_nSegments;
aPositions[iPosition].m_fMeanAbsSlope /= aPositions[iPosition].m_nSegments;
// calculating std dev of slope
gint iEndPoint = iPosition + aPositions[iPosition].m_nSegments;
gdouble fDiffSquaredSum = 0.0;
for(iPoint = iPosition ; iPoint < iEndPoint ; iPoint++) {
gdouble fDiff = aSlopes[iPoint] - aPositions[iPosition].m_fMeanSlope;
fDiffSquaredSum += (fDiff*fDiff);
}
gdouble fStdDevSlope = sqrt(fDiffSquaredSum / aPositions[iPosition].m_nSegments);
// calculate a score between 0 (worst) and 1 (best), we want to minimize both the mean and std dev of slope
aPositions[iPosition].m_fScore = 1.0/((aPositions[iPosition].m_fMeanAbsSlope+1.0)*(fStdDevSlope+1.0));
// find max only
if(aPositions[iPosition].m_fScore > fMaxScore) {
fMaxScore = aPositions[iPosition].m_fScore;
iBestPosition = iPosition;
}
// add position to the ptr array
//g_ptr_array_add(pPositionsPtrArray, &(aPositions[iPosition]));
/*
g_print("%s: [%d] segments = %d, slope = %2.2f, stddev = %2.2f, score = %2.2f\n", pszLabel, iPosition,
aPositions[iPosition].m_nSegments,
aPositions[iPosition].m_fMeanAbsSlope,
fStdDevSlope,
aPositions[iPosition].m_fScore);
*/
}
// sort postions by score
//if(nPositions > 1)
// g_ptr_array_sort(pPositionsPtrArray, map_draw_cairo_line_label_position_sort);
/*
for(iPosition = 1 ; iPosition < nPositions ; iPosition++) {
labelposition_t *pPos = g_ptr_array_index(pPositionsPtrArray, iPosition - 1);
if(pPos->m_nSegments > 1)
g_print("%s: [%d] segments = %d, slope = %2.2f, score = %2.2f\n", pszLabel, iPosition,
pPos->m_nSegments,
pPos->m_fMeanAbsSlope,
pPos->m_fScore);
}
*/
cairo_font_extents_t font_extents;
cairo_current_font_extents(pCairo, &font_extents);
gint nTotalStringLength = strlen(pszLabel);
/*
for(iPosition = 1 ; iPosition < nPositions ; iPosition++) {
labelposition_t *pPos = g_ptr_array_index(pPositionsPtrArray, iPosition - 1);
gint iBestPosition = pPos->m_iIndex;
*/
//g_print("trying position %d with score %2.2f\n", iBestPosition, pPos->m_fScore);
gdouble fFrontPadding = 0.0;
gdouble fFrontPaddingNext = (aPositions[iBestPosition].m_fLength - extents.width) / 2;
gint iStartPoint;
iStartPoint = iBestPosition;
if(aPositions[iBestPosition].m_fRunTotal > 0) {
iStartPoint = iBestPosition;
}
else {
// road runs backwards, reverse everything
iStartPoint = nNumPoints - iBestPosition - aPositions[iBestPosition].m_nSegments + 1;
// reverse the array
gint iRead,iWrite;
for(iWrite=0, iRead=nNumPoints-1 ; iRead>= 0 ; iWrite++, iRead--) {
apPoints[iWrite] = g_ptr_array_index(pPointString->m_pPointsArray, iRead);
}
}
gint iEndPoint = iStartPoint + aPositions[iBestPosition].m_nSegments;
gint nStringStartIndex = 0;
#if 0
gboolean bGood = TRUE;
for(iPoint = iStartPoint ; iPoint < iEndPoint ; iPoint++) {
RENDERING_THREAD_YIELD;
if(nTotalStringLength == nStringStartIndex) break; // done
pMapPoint1 = apPoints[iPoint-1];
pMapPoint2 = apPoints[iPoint];
gdouble fX1 = SCALE_X(pRenderMetrics, pMapPoint1->m_fLongitude);
gdouble fY1 = SCALE_Y(pRenderMetrics, pMapPoint1->m_fLatitude);
gdouble fX2 = SCALE_X(pRenderMetrics, pMapPoint2->m_fLongitude);
gdouble fY2 = SCALE_Y(pRenderMetrics, pMapPoint2->m_fLatitude);
// determine slope of the line
gdouble fRise = fY2 - fY1;
gdouble fRun = fX2 - fX1;
gdouble fLineLength = sqrt((fRun*fRun) + (fRise*fRise));
fFrontPadding = fFrontPaddingNext;
fFrontPaddingNext = 0.0;
// this is probably not needed now that we only loop over line segments that will contain some label
if(fFrontPadding > fLineLength) {
fFrontPaddingNext = fFrontPadding - fLineLength;
continue;
}
// do this after the padding calculation to possibly save some CPU cycles
gdouble fAngleInRadians = atan(fRise / fRun);
if(fRun < 0.0) fAngleInRadians += M_PI;
//g_print("(fRise(%f) / fRun(%f)) = %f, atan(fRise / fRun) = %f: ", fRise, fRun, fRise / fRun, fAngleInRadians);
//g_print("=== NEW SEGMENT, pixel (deltaY=%f, deltaX=%f), line len=%f, (%f,%f)->(%f,%f)\n",fRise, fRun, fLineLength, pMapPoint1->m_fLatitude,pMapPoint1->m_fLongitude,pMapPoint2->m_fLatitude,pMapPoint2->m_fLongitude);
//g_print(" has screen coords (%f,%f)->(%f,%f)\n", fX1,fY1,fX2,fY2);
gchar azLabelSegment[DRAW_LABEL_BUFFER_LEN];
// Figure out how much of the string we can put in this line segment
gboolean bFoundWorkableStringLength = FALSE;
gint nWorkableStringLength;
if(iPoint == (nNumPoints-1)) {
// last segment, use all of string
nWorkableStringLength = (nTotalStringLength - nStringStartIndex);
// copy it in to the temp buffer
memcpy(azLabelSegment, &pszLabel[nStringStartIndex], nWorkableStringLength);
azLabelSegment[nWorkableStringLength] = '\0';
}
else {
g_assert((nTotalStringLength - nStringStartIndex) > 0);
for(nWorkableStringLength = (nTotalStringLength - nStringStartIndex) ; nWorkableStringLength >= 1 ; nWorkableStringLength--) {
//g_print("trying nWorkableStringLength = %d\n", nWorkableStringLength);
if(nWorkableStringLength >= DRAW_LABEL_BUFFER_LEN) break;
// copy it in to the temp buffer
memcpy(azLabelSegment, &pszLabel[nStringStartIndex], nWorkableStringLength);
azLabelSegment[nWorkableStringLength] = '\0';
//g_print("azLabelSegment = %s\n", azLabelSegment);
// measure the label
cairo_text_extents(pCairo, azLabelSegment, &extents);
// if we're skipping ahead some (frontpadding), effective line length is smaller, so subtract padding
if(extents.width <= (fLineLength - fFrontPadding)) {
//g_print("found length %d for %s\n", nWorkableStringLength, azLabelSegment);
bFoundWorkableStringLength = TRUE;
// if we have 3 pixels, and we use 2, this should be NEGATIVE 1
// TODO: we should only really do this if the next segment doesn't take a huge bend
fFrontPaddingNext = extents.width - (fLineLength - fFrontPadding);
fFrontPaddingNext /= 2; // no good explanation for this
break;
}
}
if(!bFoundWorkableStringLength) {
// write one character and set some frontpadding for the next piece
g_assert(nWorkableStringLength == 0);
nWorkableStringLength = 1;
// give the next segment some padding if we went over on this segment
fFrontPaddingNext = extents.width - (fLineLength - fFrontPadding);
//g_print("forcing a character (%s) on small segment, giving next segment front-padding: %f\n", azLabelSegment, fFrontPaddingNext);
}
}
// move the current index up
nStringStartIndex += nWorkableStringLength;
// Normalize (make length = 1.0) by dividing by line length
// This makes a line with length 1 from the origin (0,0)
gdouble fNormalizedX = fRun / fLineLength;
gdouble fNormalizedY = fRise / fLineLength;
// CENTER IT on the line ?
// (buffer space) |-text-| (buffer space)
// ======================================
//gdouble fHalfBufferSpace = ((fLineLength - extents.width)/2);
gdouble fDrawX = fX1 + (fNormalizedX * fFrontPadding);
gdouble fDrawY = fY1 + (fNormalizedY * fFrontPadding);
// NOTE: ***Swap the X and Y*** and normalize (make length = 1.0) by dividing by line length
// This makes a perpendicular line with length 1 from the origin (0,0)
gdouble fPerpendicularNormalizedX = fRise / fLineLength;
gdouble fPerpendicularNormalizedY = -(fRun / fLineLength);
// we want the normal pointing towards the top of the screen. that's the negative Y direction.
//if(fPerpendicularNormalizedY > 0) fPerpendicularNormalizedY = -fPerpendicularNormalizedY;
fDrawX -= (fPerpendicularNormalizedX * font_extents.ascent/2);
fDrawY -= (fPerpendicularNormalizedY * font_extents.ascent/2);
GdkPoint aBoundingPolygon[4];
// 0 is bottom left point
aBoundingPolygon[0].x = fDrawX;
aBoundingPolygon[0].y = fDrawY;
// 1 is upper left point
aBoundingPolygon[1].x = fDrawX + (fPerpendicularNormalizedX * font_extents.ascent);
aBoundingPolygon[1].y = fDrawY + (fPerpendicularNormalizedY * font_extents.ascent);
// 2 is upper right point
aBoundingPolygon[2].x = aBoundingPolygon[1].x + (fNormalizedX * extents.width);
aBoundingPolygon[2].y = aBoundingPolygon[1].y + (fNormalizedY * extents.width);
// 2 is lower right point
aBoundingPolygon[3].x = fDrawX + (fNormalizedX * extents.width);
aBoundingPolygon[3].y = fDrawY + (fNormalizedY * extents.width);
if(FALSE == scenemanager_can_draw_polygon(pMap->m_pSceneManager, aBoundingPolygon, 4)) {
bGood = FALSE;
break;
}
}
// try next position
if(bGood == FALSE)
continue;
// reset these
fFrontPadding = 0.0;
fFrontPaddingNext = (aPositions[iBestPosition].m_fLength - extents.width) / 2;
nStringStartIndex = 0;
#endif
// draw it
for(iPoint = iStartPoint ; iPoint < iEndPoint ; iPoint++) {
RENDERING_THREAD_YIELD;
if(nTotalStringLength == nStringStartIndex) break; // done
pMapPoint1 = apPoints[iPoint-1];
pMapPoint2 = apPoints[iPoint];
gdouble fX1 = SCALE_X(pRenderMetrics, pMapPoint1->m_fLongitude);
gdouble fY1 = SCALE_Y(pRenderMetrics, pMapPoint1->m_fLatitude);
gdouble fX2 = SCALE_X(pRenderMetrics, pMapPoint2->m_fLongitude);
gdouble fY2 = SCALE_Y(pRenderMetrics, pMapPoint2->m_fLatitude);
// determine slope of the line
gdouble fRise = fY2 - fY1;
gdouble fRun = fX2 - fX1;
gdouble fLineLength = sqrt((fRun*fRun) + (fRise*fRise));
fFrontPadding = fFrontPaddingNext;
fFrontPaddingNext = 0.0;
// do this after the padding calculation to possibly save some CPU cycles
gdouble fAngleInRadians = atan(fRise / fRun);
if(fRun < 0.0) fAngleInRadians += M_PI;
//g_print("(fRise(%f) / fRun(%f)) = %f, atan(fRise / fRun) = %f: ", fRise, fRun, fRise / fRun, fAngleInRadians);
//g_print("=== NEW SEGMENT, pixel (deltaY=%f, deltaX=%f), line len=%f, (%f,%f)->(%f,%f)\n",fRise, fRun, fLineLength, pMapPoint1->m_fLatitude,pMapPoint1->m_fLongitude,pMapPoint2->m_fLatitude,pMapPoint2->m_fLongitude);
//g_print(" has screen coords (%f,%f)->(%f,%f)\n", fX1,fY1,fX2,fY2);
gchar azLabelSegment[DRAW_LABEL_BUFFER_LEN];
// Figure out how much of the string we can put in this line segment
gboolean bFoundWorkableStringLength = FALSE;
gint nWorkableStringLength;
if(iPoint == (nNumPoints-1)) {
// last segment, use all of string
nWorkableStringLength = (nTotalStringLength - nStringStartIndex);
// copy it in to the temp buffer
memcpy(azLabelSegment, &pszLabel[nStringStartIndex], nWorkableStringLength);
azLabelSegment[nWorkableStringLength] = '\0';
}
else {
g_assert((nTotalStringLength - nStringStartIndex) > 0);
for(nWorkableStringLength = (nTotalStringLength - nStringStartIndex) ; nWorkableStringLength >= 1 ; nWorkableStringLength--) {
//g_print("trying nWorkableStringLength = %d\n", nWorkableStringLength);
if(nWorkableStringLength >= DRAW_LABEL_BUFFER_LEN) break;
// copy it in to the temp buffer
memcpy(azLabelSegment, &pszLabel[nStringStartIndex], nWorkableStringLength);
azLabelSegment[nWorkableStringLength] = '\0';
//g_print("azLabelSegment = %s\n", azLabelSegment);
// measure the label
cairo_text_extents(pCairo, azLabelSegment, &extents);
// if we're skipping ahead some (frontpadding), effective line length is smaller, so subtract padding
if(extents.width <= (fLineLength - fFrontPadding)) {
//g_print("found length %d for %s\n", nWorkableStringLength, azLabelSegment);
bFoundWorkableStringLength = TRUE;
// if we have 3 pixels, and we use 2, this should be NEGATIVE 1
// TODO: we should only really do this if the next segment doesn't take a huge bend
fFrontPaddingNext = extents.width - (fLineLength - fFrontPadding);
fFrontPaddingNext /= 2; // no good explanation for this
break;
}
}
if(!bFoundWorkableStringLength) {
// write one character and set some frontpadding for the next piece
g_assert(nWorkableStringLength == 0);
nWorkableStringLength = 1;
// give the next segment some padding if we went over on this segment
fFrontPaddingNext = extents.width - (fLineLength - fFrontPadding);
//g_print("forcing a character (%s) on small segment, giving next segment front-padding: %f\n", azLabelSegment, fFrontPaddingNext);
}
}
// move the current index up
nStringStartIndex += nWorkableStringLength;
// Normalize (make length = 1.0) by dividing by line length
// This makes a line with length 1 from the origin (0,0)
gdouble fNormalizedX = fRun / fLineLength;
gdouble fNormalizedY = fRise / fLineLength;
// CENTER IT on the line ?
// (buffer space) |-text-| (buffer space)
// ======================================
//gdouble fHalfBufferSpace = ((fLineLength - extents.width)/2);
gdouble fDrawX = fX1 + (fNormalizedX * fFrontPadding);
gdouble fDrawY = fY1 + (fNormalizedY * fFrontPadding);
// NOTE: ***Swap the X and Y*** and normalize (make length = 1.0) by dividing by line length
// This makes a perpendicular line with length 1 from the origin (0,0)
gdouble fPerpendicularNormalizedX = fRise / fLineLength;
gdouble fPerpendicularNormalizedY = -(fRun / fLineLength);
// we want the normal pointing towards the top of the screen. that's the negative Y direction.
//if(fPerpendicularNormalizedY > 0) fPerpendicularNormalizedY = -fPerpendicularNormalizedY;
fDrawX -= (fPerpendicularNormalizedX * font_extents.ascent/2);
fDrawY -= (fPerpendicularNormalizedY * font_extents.ascent/2);
GdkPoint aBoundingPolygon[4];
// 0 is bottom left point
aBoundingPolygon[0].x = fDrawX;
aBoundingPolygon[0].y = fDrawY;
// 1 is upper left point
aBoundingPolygon[1].x = fDrawX + (fPerpendicularNormalizedX * font_extents.ascent);
aBoundingPolygon[1].y = fDrawY + (fPerpendicularNormalizedY * font_extents.ascent);
// 2 is upper right point
aBoundingPolygon[2].x = aBoundingPolygon[1].x + (fNormalizedX * extents.width);
aBoundingPolygon[2].y = aBoundingPolygon[1].y + (fNormalizedY * extents.width);
// 3 is lower right point
aBoundingPolygon[3].x = fDrawX + (fNormalizedX * extents.width);
aBoundingPolygon[3].y = fDrawY + (fNormalizedY * extents.width);
cairo_save(pCairo);
cairo_move_to(pCairo, fDrawX, fDrawY);
cairo_set_rgb_color(pCairo, 0.0,0.0,0.0);
cairo_set_alpha(pCairo, 1.0);
cairo_rotate(pCairo, fAngleInRadians);
gdouble fHaloSize = pLabelStyle->m_afHaloAtZoomLevel[pRenderMetrics->m_nZoomLevel-1];
if(fHaloSize >= 0) {
cairo_save(pCairo);
cairo_text_path(pCairo, azLabelSegment);
cairo_set_line_width(pCairo, fHaloSize);
cairo_set_rgb_color(pCairo, 1.0,1.0,1.0);
cairo_set_line_join(pCairo, CAIRO_LINE_JOIN_BEVEL);
//cairo_set_miter_limit(pCairo, 0.1);
cairo_stroke(pCairo);
cairo_restore(pCairo);
}
cairo_show_text(pCairo, azLabelSegment);
//cairo_fill(pCairo);
cairo_restore(pCairo);
// claim the space this took up
scenemanager_claim_polygon(pMap->m_pSceneManager, aBoundingPolygon, 4);
}
cairo_restore(pCairo);
// claim the label (so it won't be drawn twice)
scenemanager_claim_label(pMap->m_pSceneManager, pszLabel);
/*
// success
break;
}
g_ptr_array_free(pPositionsPtrArray, FALSE);
*/
}
//
// Draw a single polygon label
//
void map_draw_cairo_polygon_label(map_t* pMap, cairo_t *pCairo, textlabelstyle_t* pLabelStyle, rendermetrics_t* pRenderMetrics, pointstring_t* pPointString, const gchar* pszLabel)
{
if(pPointString->m_pPointsArray->len < 3) return;
if(FALSE == scenemanager_can_draw_label_at(pMap->m_pSceneManager, pszLabel, NULL)) {
return; // duplicate label or whatever other rule scenemanager uses
}
gdouble fTotalX = 0.0;
gdouble fTotalY = 0.0;
gdouble fMaxLat = MIN_LATITUDE; // init to the worst possible value so first point will override
gdouble fMinLat = MAX_LATITUDE;
gdouble fMaxLon = MIN_LONGITUDE;
gdouble fMinLon = MAX_LONGITUDE;
mappoint_t* pMapPoint;
gdouble fX;
gdouble fY;
gint i;
for(i=0 ; i<pPointString->m_pPointsArray->len ; i++) {
pMapPoint = g_ptr_array_index(pPointString->m_pPointsArray, i);
// find extents
fMaxLat = max(pMapPoint->m_fLatitude,fMaxLat);
fMinLat = min(pMapPoint->m_fLatitude,fMinLat);
fMaxLon = max(pMapPoint->m_fLongitude,fMaxLon);
fMinLon = min(pMapPoint->m_fLongitude,fMinLon);
fX = SCALE_X(pRenderMetrics, pMapPoint->m_fLongitude);
fY = SCALE_Y(pRenderMetrics, pMapPoint->m_fLatitude);
// sum up Xs and Ys (we'll take an average later)
fTotalX += fX;
fTotalY += fY;
}
// rectangle overlap test
if(fMaxLat < pRenderMetrics->m_rWorldBoundingBox.m_A.m_fLatitude
|| fMaxLon < pRenderMetrics->m_rWorldBoundingBox.m_A.m_fLongitude
|| fMinLat > pRenderMetrics->m_rWorldBoundingBox.m_B.m_fLatitude
|| fMinLon > pRenderMetrics->m_rWorldBoundingBox.m_B.m_fLongitude)
{
return; // not visible
}
gdouble fDrawX = SCALE_X(pRenderMetrics, (fMinLon + fMaxLon) / 2); //fMinX + fPolygonWidth/2; //fTotalX / pPointString->m_pPointsArray->len;
gdouble fDrawY = SCALE_Y(pRenderMetrics, (fMinLat + fMaxLat) / 2); //fMinY + fPolygonHeight/2; //fTotalY / pPointString->m_pPointsArray->len;
cairo_save(pCairo);
// Get total width of string
cairo_text_extents_t extents;
cairo_text_extents(pCairo, pszLabel, &extents);
// is the text too big for the polygon?
// if((extents.width > (fPolygonWidth * 1.5)) || (extents.height > (fPolygonHeight * 1.5))) {
// cairo_restore(pCairo);
// return;
// }
fDrawX -= (extents.width / 2);
fDrawY += (extents.height / 2);
// check permission with scenemanager
GdkRectangle rcLabelOutline;
rcLabelOutline.x = (gint)fDrawX;
rcLabelOutline.width = extents.width;
rcLabelOutline.y = ((gint)fDrawY) - extents.height;
rcLabelOutline.height = extents.height;
if(FALSE == scenemanager_can_draw_rectangle(pMap->m_pSceneManager, &rcLabelOutline)) {
cairo_restore(pCairo);
return;
}
// claim it! Now no one else will draw text here.
scenemanager_claim_rectangle(pMap->m_pSceneManager, &rcLabelOutline);
cairo_move_to(pCairo, fDrawX, fDrawY);
gdouble fHaloSize = pLabelStyle->m_afHaloAtZoomLevel[pRenderMetrics->m_nZoomLevel-1];
if(fHaloSize >= 0) {
cairo_save(pCairo);
cairo_text_path(pCairo, pszLabel);
cairo_set_line_width(pCairo, fHaloSize);
cairo_set_rgb_color(pCairo, 1.0,1.0,1.0);
cairo_set_line_join(pCairo, CAIRO_LINE_JOIN_BEVEL);
// cairo_set_miter_limit(pCairo, 0.1);
cairo_stroke(pCairo);
cairo_restore(pCairo);
}
cairo_show_text(pCairo, pszLabel);
cairo_restore(pCairo);
// Tell scenemanager that we've drawn this label
scenemanager_claim_label(pMap->m_pSceneManager, pszLabel);
}
#ifdef ROADSTER_DEAD_CODE
/*
//
// Draw a crosshair
//
#define CROSSHAIR_LINE_RELIEF (6)
#define CROSSHAIR_LINE_LENGTH (12)
#define CROSSHAIR_CIRCLE_RADIUS (12)
static void map_draw_cairo_crosshair(map_t* pMap, cairo_t* pCairo, rendermetrics_t* pRenderMetrics)
{
cairo_save(pCairo);
cairo_set_line_width(pCairo, 1.0);
cairo_set_rgb_color(pCairo, 0.1, 0.1, 0.1);
cairo_set_alpha(pCairo, 1.0);
// left line
cairo_move_to(pCairo, (pRenderMetrics->m_nWindowWidth/2) - (CROSSHAIR_LINE_RELIEF + CROSSHAIR_LINE_LENGTH), (pRenderMetrics->m_nWindowHeight/2));
cairo_line_to(pCairo, (pRenderMetrics->m_nWindowWidth/2) - (CROSSHAIR_LINE_RELIEF), (pRenderMetrics->m_nWindowHeight/2));
// right line
cairo_move_to(pCairo, (pRenderMetrics->m_nWindowWidth/2) + (CROSSHAIR_LINE_RELIEF + CROSSHAIR_LINE_LENGTH), (pRenderMetrics->m_nWindowHeight/2));
cairo_line_to(pCairo, (pRenderMetrics->m_nWindowWidth/2) + (CROSSHAIR_LINE_RELIEF), (pRenderMetrics->m_nWindowHeight/2));
// top line
cairo_move_to(pCairo, (pRenderMetrics->m_nWindowWidth/2), (pRenderMetrics->m_nWindowHeight/2) - (CROSSHAIR_LINE_RELIEF + CROSSHAIR_LINE_LENGTH));
cairo_line_to(pCairo, (pRenderMetrics->m_nWindowWidth/2), (pRenderMetrics->m_nWindowHeight/2) - (CROSSHAIR_LINE_RELIEF));
// bottom line
cairo_move_to(pCairo, (pRenderMetrics->m_nWindowWidth/2), (pRenderMetrics->m_nWindowHeight/2) + (CROSSHAIR_LINE_RELIEF + CROSSHAIR_LINE_LENGTH));
cairo_line_to(pCairo, (pRenderMetrics->m_nWindowWidth/2), (pRenderMetrics->m_nWindowHeight/2) + (CROSSHAIR_LINE_RELIEF));
cairo_stroke(pCairo);
cairo_arc(pCairo, pRenderMetrics->m_nWindowWidth/2, pRenderMetrics->m_nWindowHeight/2, CROSSHAIR_CIRCLE_RADIUS, 0, 2*M_PI);
cairo_stroke(pCairo);
cairo_restore(pCairo);
}
void map_draw_cairo_locations(map_t* pMap, cairo_t* pCairo, rendermetrics_t* pRenderMetrics)
{
location_t loc;
location_t* pLoc = &loc;
// XXX: debug
pLoc->m_pszName = "some address., Cambridge, MA, 02140";
pLoc->m_Coordinates.m_fLongitude = 0;
pLoc->m_Coordinates.m_fLatitude = 0;
gchar* pszFontFamily = ROAD_FONT;
gdouble fFontSize = 14.0;
//
// Draw
//
cairo_save(pCairo);
cairo_select_font(pCairo, pszFontFamily, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
cairo_scale_font(pCairo, fFontSize);
// measure the label
cairo_text_extents_t extents;
cairo_text_extents(pCairo, pLoc->m_pszName, &extents);
gdouble fLabelWidth = extents.width;
gdouble fLabelHeight = extents.height;
gdouble fX = SCALE_X(pRenderMetrics, pLoc->m_Coordinates.m_fLongitude);
gdouble fY = SCALE_Y(pRenderMetrics, pLoc->m_Coordinates.m_fLatitude);
#define LOCATION_LABEL_CELL_PADDING (4)
cairo_rectangle(pCairo, fX - LOCATION_LABEL_CELL_PADDING, fY-(fLabelHeight/2)-(LOCATION_LABEL_CELL_PADDING), fLabelWidth + (LOCATION_LABEL_CELL_PADDING*2), fLabelHeight + (LOCATION_LABEL_CELL_PADDING*2));
cairo_save(pCairo);
cairo_set_rgb_color(pCairo, 247/255.0, 247/255.0, 247/255.0);
cairo_set_alpha(pCairo, 0.9);
cairo_fill(pCairo);
cairo_restore(pCairo);
cairo_set_rgb_color(pCairo, 232/255.0, 166/255.0, 0/255.0);
cairo_set_alpha(pCairo, 1.0);
cairo_set_line_width(pCairo, 0.9);
cairo_stroke(pCairo);
cairo_set_rgb_color(pCairo, 0/255.0, 0/255.0, 0/255.0);
cairo_move_to(pCairo, fX, fY+(fLabelHeight/2));
cairo_show_text(pCairo, pLoc->m_pszName);
cairo_restore(pCairo);
}
void map_draw_cairo_gps_trail(map_t* pMap, cairo_t* pCairo, pointstring_t* pPointString)
{
rendermetrics_t renderMetrics = {0};
map_get_render_metrics(pMap, &renderMetrics);
rendermetrics_t* pRenderMetrics = &renderMetrics;
if(pPointString->m_pPointsArray->len > 2) {
mappoint_t* pPoint = g_ptr_array_index(pPointString->m_pPointsArray, 0);
// move to index 0
cairo_move_to(pCairo, SCALE_X(pRenderMetrics, pPoint->m_fLongitude), SCALE_Y(pRenderMetrics, pPoint->m_fLatitude));
gint i;
for(i=1 ; i<pPointString->m_pPointsArray->len ; i++) {
RENDERING_THREAD_YIELD;
pPoint = g_ptr_array_index(pPointString->m_pPointsArray, i);
cairo_line_to(pCairo, SCALE_X(pRenderMetrics, pPoint->m_fLongitude), SCALE_Y(pRenderMetrics, pPoint->m_fLatitude));
}
cairo_set_rgb_color(pCairo, 0.0, 0.0, 0.7);
cairo_set_alpha(pCairo, 0.6);
cairo_set_line_width(pCairo, 6);
cairo_set_line_cap(pCairo, CAIRO_LINE_CAP_ROUND);
cairo_set_line_join(pCairo, CAIRO_LINE_JOIN_ROUND);
cairo_set_miter_limit(pCairo, 5);
cairo_stroke(pCairo);
}
}
*/
#endif
|