File: mapsvg.c

package info (click to toggle)
mapserver 4.10.0-5%2Betch1
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 11,960 kB
  • ctags: 15,165
  • sloc: ansic: 164,837; python: 5,770; java: 5,169; perl: 2,388; cpp: 1,603; makefile: 735; lex: 507; sh: 375; yacc: 317; tcl: 158; cs: 85; ruby: 53
file content (1931 lines) | stat: -rw-r--r-- 73,702 bytes parent folder | download | duplicates (2)
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
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
/******************************************************************************
 * $Id: mapsvg.c,v 1.22 2006/01/16 20:21:18 sdlime Exp $
 *
 * Name:     mapsvg.c
 * Project:  MapServer
 * Language: C
 * Purpose:  SVG output
 * Author:  DM Solutions Group (assefa@dmsolutions.ca)
 *
 *
 ******************************************************************************
 * 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.
 ******************************************************************************
 *
 * $Log: mapsvg.c,v $
 * Revision 1.22  2006/01/16 20:21:18  sdlime
 * Fixed error with image legends (shifted text) introduced by the 1449 bug fix. (bug 1607)
 *
 * Revision 1.21  2005/10/26 17:53:57  frank
 * Avoid warning about unused function.
 *
 * Revision 1.20  2005/09/26 19:47:14  assefa
 * Correct typo bugs when outputing symbols (Bug 1407)
 *
 * Revision 1.19  2005/07/05 14:36:33  assefa
 * Correct a bug : using msFree on a wrong pointer : Bug 1406.
 *
 * Revision 1.18  2005/06/22 15:16:25  assefa
 * Correct problems with multipolygons (Bug 1390).
 * Remove unecessary spaces when generating the SVG.
 * Contribution to these 2 issues by Makoto Niwa niwa@orkney.co.jp.
 *
 * Revision 1.17  2005/06/14 16:03:34  dan
 * Updated copyright date to 2005
 *
 * Revision 1.16  2005/05/19 04:09:34  sdlime
 * Removed the LINE_VERT_THRESHOLD test (bug 564) from PDF/SWF/SVG/imagemap drivers.
 *
 * Revision 1.15  2005/04/21 04:34:03  dan
 * Fixed old problem with labels occasionally drawn upside down (bug 564)
 *
 * Revision 1.14  2005/04/13 15:55:55  dan
 * Added missing include stdarg.h (bug 1315)
 *
 * Revision 1.13  2005/04/07 21:49:24  frank
 * use MS_PI instead of M_PI, available everywhere
 *
 * Revision 1.12  2005/04/07 17:23:16  assefa
 * Remove #ifdef USE_SVG. It was added during development.
 *
 * Revision 1.11  2005/03/07 15:58:10  assefa
 * Use gzip function to output svgz.
 *
 * Revision 1.10  2005/03/02 04:23:36  assefa
 * Partial support of symbols (vector and ellipse).
 *
 * Revision 1.9  2005/02/21 22:22:32  assefa
 * Add code for GOSVG_LATLONG_TRANSFORMATION.
 *
 * Revision 1.8  2005/02/18 18:14:11  dan
 * New patch to fix url strings broken by previous patch to bug 1238
 *
 * Revision 1.7  2005/02/18 03:06:47  dan
 * Turned all C++ (//) comments into C comments (bug 1238)
 *
 * Revision 1.6  2005/02/11 22:14:08  frank
 * Fixed imagePolyline declaration.
 *
 * Revision 1.5  2005/02/09 20:43:49  assefa
 * Add SVG utility function msSaveImagetoFpSVG.
 *
 * Revision 1.4  2005/02/09 18:30:50  assefa
 * goSVG specific metadata added.
 *
 * Revision 1.3  2005/02/05 00:07:14  assefa
 * Add support for lables with encoding.
 *
 * Revision 1.2  2005/02/03 00:07:39  assefa
 * Add partial text and raster support.
 *
 * Revision 1.1  2005/02/01 23:18:54  assefa
 * Initial version : svg output.
 *
 *
 */


#include <stdarg.h>

#include <assert.h>
#ifdef USE_ZLIB
#include "zlib.h"
#endif
#include "map.h"

MS_CVSID("$Id: mapsvg.c,v 1.22 2006/01/16 20:21:18 sdlime Exp $")


static int msIO_fprintfgz(FILE *fp, int bCompressed, const char *format, ... )

{
    va_list args;
    int     return_val;
    char workBuf[8000];

    va_start( args, format );
#if defined(HAVE_VSNPRINTF)
    return_val = vsnprintf( workBuf, sizeof(workBuf), format, args );
#else
    return_val = vsprintf( workBuf, format, args);
#endif

    va_end( args );

    if( return_val < 0 || return_val >= sizeof(workBuf) )
        return -1;

    if (bCompressed)
    {

#ifdef USE_ZLIB
        return gzwrite( fp, workBuf, return_val);
#else
        return fwrite( workBuf, 1, return_val, fp );
#endif
    }
    else
      return fwrite( workBuf, 1, return_val, fp );
    
}

/************************************************************************/
/*                             msImageCreateSVG                         */
/*                                                                      */
/*      Create SVG image (called from prepareimage).                    */
/*                                                                      */
/*      Creates all necessary fileds to be able to output svg file.     */
/************************************************************************/
MS_DLL_EXPORT imageObj *msImageCreateSVG(int width, int height, 
                                         outputFormatObj *format, 
                                         char *imagepath, 
                                         char *imageurl, mapObj *map)
{
    double      a, b , c, d, e, f;
    imageObj    *image = NULL;
    static char epsgCode[20] ="";
    static char *value;
    int         nZoomInTH, nZoomOutTH, nScrollTH;


    assert( strcasecmp(format->driver,"svg") == 0 );
    image = (imageObj *)calloc(1,sizeof(imageObj));

    image->format = format;
    format->refcount++;

    image->width = width;
    image->height = height;
    image->imagepath = NULL;
    image->imageurl = NULL;
    
    if (imagepath)
      image->imagepath = strdup(imagepath);

    if (imageurl)
        image->imageurl = strdup(imageurl);

    image->img.svg = (SVGObj *)malloc(sizeof(SVGObj));
    image->img.svg->filename = NULL;
    image->img.svg->map = map;
    image->img.svg->streamclosed = 0;

    if (! map->web.imagepath)
    {
        msSetError(MS_MISCERR, "web image path need to be set.",
                   "msImageCreateSVG");
        return NULL;
    }

#ifdef USE_ZLIB
    if (strcasecmp(msGetOutputFormatOption(image->format, 
                                           "COMPRESSED_OUTPUT",""),
                   "TRUE") == 0)
    {
        if( map != NULL && map->web.imagepath != NULL )
          image->img.svg->filename = msTmpFile(map->mappath,
                                                 map->web.imagepath,"svgz");
    }
    else
      if( map != NULL && map->web.imagepath != NULL )
          image->img.svg->filename = msTmpFile(map->mappath,
                                               map->web.imagepath,"svg"); 
#else
    if( map != NULL && map->web.imagepath != NULL )
          image->img.svg->filename = msTmpFile(map->mappath,
                                               map->web.imagepath,"svg"); 
#endif
    
    
    if (!image->img.svg->filename)
    {
        msSetError(MS_IOERR, "Failed to create temporary svg file.",
                    "msImageCreateSVG()" );
        return NULL;
    }

    if (strcasecmp(msGetOutputFormatOption(image->format, "COMPRESSED_OUTPUT",""),
                   "TRUE") == 0)
    {
#ifdef USE_ZLIB
        image->img.svg->stream = gzopen(image->img.svg->filename, "wb" );
        image->img.svg->compressed = 1;
#else
        image->img.svg->stream = fopen(image->img.svg->filename, "w" );
        image->img.svg->compressed = 0;
#endif
    }
    else
    {
        image->img.svg->stream = fopen(image->img.svg->filename, "w" );
        image->img.svg->compressed = 0;
    }
    

    if (!image->img.svg->stream)
    {
        msSetError(MS_IOERR, "Failed to open temporary svg file.",
                    "msImageCreateSVG()" );
        return NULL;
    }
    
    /* initialize it with the svg header */

    msIO_fprintfgz(image->img.svg->stream, image->img.svg->compressed,
                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    /* TODO : seems to have a problem of dtd validation using IE (http://www.24help.info/printthread.php?t=52147) */
    /* msIO_fprintfgz(image->img.svg->stream, image->img.svg->compressed,  "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n"); */
    /* msIO_fprintfgz(image->img.svg->stream, image->img.svg->compressed,  "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-flat.dtd\">\n"); */


/* -------------------------------------------------------------------- */
/*      Special output for goSVG support.                               */
/* -------------------------------------------------------------------- */
    if (strcasecmp(msGetOutputFormatOption(image->format, "GOSVG",""),
                   "TRUE") == 0)
    {
        /* transformation from geographical coordinates to pixel coordinates */
        a = width/(map->extent.maxx-map->extent.minx);
        b = 0;
        c = 0;
        d = -height/(map->extent.maxy - map->extent.miny);
        e = -width/(map->extent.maxx-map->extent.minx) *map->extent.minx;
        f = height/(map->extent.maxy-map->extent.miny)*map->extent.maxy;

        msIO_fprintfgz(image->img.svg->stream, image->img.svg->compressed,  "<svg version=\"1.1\" width=\"%d\" height=\"%d\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns:au=\"http://www.svgmobile.jp/2004/kddip\" au:boundingBox=\"0 0 %d %d\">\n",  width, height, width, height);
        msIO_fprintfgz(image->img.svg->stream, image->img.svg->compressed,  "<title>%s</title>\n", map->name);
        msIO_fprintfgz(image->img.svg->stream, image->img.svg->compressed,  "<metadata>\n");
        msIO_fprintfgz(image->img.svg->stream, image->img.svg->compressed, "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n");
        msIO_fprintfgz(image->img.svg->stream, image->img.svg->compressed,  "xmlns:crs=\"http://www.ogc.org/crs\" xmlns:svg=\"http://wwww.w3.org/2000/svg\">\n");
        msIO_fprintfgz(image->img.svg->stream, image->img.svg->compressed,  "<rdf:Description>\n");

        if (map->units == MS_DD && 
            (strcasecmp(msGetOutputFormatOption(image->format, 
                                               "GOSVG_LATLONG_TRANSFORMATION",""), 
                       "TRUE") == 0))
        {
            
            double plat = ((map->extent.maxy-map->extent.miny)/2) + map->extent.miny;
            double plon =(( map->extent.maxx-map->extent.minx)/2) + map->extent.minx;
            double wp = width;
            double hp = height;
            double r = ((map->extent.maxy-map->extent.miny)/2)*111320;/*1degree of long in meteres at latitude 0 */

            double circle=40000000;
            double res=hp;
            double aspect=width/height;/*hp/wp;*/
            double aspect_d;
            double wd;
            double hd;
            double dx;
            double dy;
            double x1;
            double y1;
            double x2;
            double y2;
            double a1;
            double d1;
            double e1;
            double f1;

            aspect_d = (hp)/(wp/cos(plat*(MS_PI/180.0)));
	    
	    if (aspect <= 1) {
		hd=(360*2*r)/circle;
		wd=hd/aspect_d;
	    } else {
		wd=(360*2*r)/(cos(plat*(MS_PI/180.0))*circle);
		hd=aspect_d*wd;
	    }
	    
	    x1=plon-wd/2;
	    y1=plat-hd/2;
	    x2=plon+wd/2;
	    y2=plat+hd/2;
	    
	    dy=y2-y1;
	    dx=x2-x1;
	    d1=-(res/dy);
	    f1=-d1*y1+hp;
	    
	    a1=cos(((y1+y2)/2)*(MS_PI/180.0))*(res/dy);
	    e1= -a1*x1;

            msIO_fprintfgz(image->img.svg->stream, image->img.svg->compressed,  "\n<!-- transform parameters using catersian transformation : svg:transform=\"matrix(%f,%f,%f,%f,%f,%f)\"  -->\n", a, b, c, d, e, f);
            msIO_fprintfgz(image->img.svg->stream, image->img.svg->compressed,  "<crs:CoordinateReferenceSystem svg:transform=\"matrix(%f,%f,%f,%f,%f,%f)\"\n", a1, b, c, d1, e1, f1);
            

        }
        else
          msIO_fprintfgz(image->img.svg->stream, image->img.svg->compressed,  "<crs:CoordinateReferenceSystem svg:transform=\"matrix(%f,%f,%f,%f,%f,%f)\"\n", a, b, c, d, e, f);

        
        /* get epsg code: TODO : test what if espg code is not set ? */
        if (map->projection.numargs > 0 && 
            (value = strstr(map->projection.args[0], "init=epsg:")) != NULL && 
            strlen(value) < 20) 
        {
            sprintf(epsgCode, "%s", value+10);
            msIO_fprintfgz(image->img.svg->stream, image->img.svg->compressed,  "rdf:resource=\"http://www.opengis.net/gml/srs/epsg.xml#%s\"/>\n",epsgCode);
        }
        else
          msIO_fprintfgz(image->img.svg->stream, image->img.svg->compressed, "/> \n");

        msIO_fprintfgz(image->img.svg->stream, image->img.svg->compressed,  "</rdf:Description>\n</rdf:RDF>\n"); 
        
        nZoomInTH = 70;
        nZoomOutTH = 100;
        nScrollTH = 10;
        
        nZoomInTH = atoi(msGetOutputFormatOption(image->format, "GOSVG_ZoomInTH","70"));
        nZoomOutTH = atoi(msGetOutputFormatOption(image->format, "GOSVG_ZoomOutTH","100"));
        nScrollTH = atoi(msGetOutputFormatOption(image->format, "GOSVG_ScrollTH","10"));

        msIO_fprintfgz(image->img.svg->stream, image->img.svg->compressed,  "<au:lbs protocol=\"maprequest\">\n");
        msIO_fprintfgz(image->img.svg->stream, image->img.svg->compressed,  
                     "<au:zoomin th=\"%d\" xlink:href=\".\"/>\n", nZoomInTH);
        msIO_fprintfgz(image->img.svg->stream, image->img.svg->compressed,  
                     "<au:zoomout th=\"%d\" xlink:href=\".\"/>\n", nZoomOutTH);
        msIO_fprintfgz(image->img.svg->stream, image->img.svg->compressed,  
                     "<au:scroll th=\"%d\" xlink:href=\".\"/>\n", nScrollTH);
        msIO_fprintfgz(image->img.svg->stream, image->img.svg->compressed,  "</au:lbs>\n");

        msIO_fprintfgz(image->img.svg->stream, image->img.svg->compressed, "</metadata>\n");
    }
    else
    {
        msIO_fprintfgz(image->img.svg->stream, image->img.svg->compressed,  "<svg version=\"1.1\" width=\"%d\" height=\"%d\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",  width, height);
    }
    
    return image;
}


/************************************************************************/
/*                              imagePolyline                           */
/*                                                                      */
/*      This function is similar to the imagePolyline function found    */
/*      in mapgd.c.                                                     */
/*                                                                      */
/*      Draws an svg line element.                                      */
/************************************************************************/
static void imagePolyline(FILE *fp, int bCompressed, shapeObj *p, 
                          colorObj *color, int size,
                          int symbolstylelength, int *symbolstyle)
{
    int i, j, k;
    char *pszDashArray = NULL;
    char szTmp[100];

    if (!fp || !p || !color || size <0 )
      return;

    /* the default size is 1. */
    if (size <= 0)
      size = 1;


    for (i = 0; i < p->numlines; i++)
    {
        
        if (symbolstylelength > 0 && symbolstyle)
        {
            for (k=0; k<symbolstylelength; k++)
            {
                if (k < symbolstylelength-1)
                  sprintf(szTmp, "%d, ", symbolstyle[k]);
                else
                  sprintf(szTmp, "%d", symbolstyle[k]);
                
                pszDashArray = strcatalloc(pszDashArray, szTmp);
            }
            
            msIO_fprintfgz(fp, bCompressed, "<polyline fill=\"none\" stroke=\"#%02x%02x%02x\" stroke-width=\"%d\" stroke-dasharray=\"%s\" points=\"",color->red, color->green, 
                           color->blue,size, pszDashArray);
        }
        else
          msIO_fprintfgz(fp, bCompressed,"<polyline fill=\"none\" stroke=\"#%02x%02x%02x\" stroke-width=\"%d\" points=\"",color->red, color->green, color->blue,size);
        
            
        msIO_fprintfgz(fp, bCompressed, "%d,%d", (int)p->line[i].point[0].x, (int)p->line[i].point[0].y);
        for(j=1; j<p->line[i].numpoints; j++)
        {
            msIO_fprintfgz(fp, bCompressed, " %d,%d", (int)p->line[i].point[j].x, (int)p->line[i].point[j].y);
        }
        
        
        msIO_fprintfgz(fp, bCompressed,"\"/>\n");
    }
    
}




/************************************************************************/
/*                           msTransformShapeSVG                        */
/*                                                                      */
/*      Transform geographic coordinated to output coordinates.         */
/*                                                                      */
/*      This function uses output format options :                      */
/*        - if FULL_RESOLUTION is set to TRUE, there won't be any       */
/*      filtering done onthe shape coordinates. Default value is TRUE.  */
/*        - if USE_GEOGRAPHICAL_COORDINATES is set to TRUE, the         */
/*      shape is not transformed output coordinates (pixels) but        */
/*      written as is (geographical coordinates). The default value     */
/*      is FALSE.                                                       */
/*                                                                      */
/*      So of non of this parameters are specified, the defalut will    */
/*      output full resolution pixel coordinates.                       */
/************************************************************************/
void msTransformShapeSVG(shapeObj *shape, rectObj extent, double cellsize,
                         imageObj *image)
{
    int i,j; 
    int bFullRes, bUseGeoCoord;
    if (!image || !MS_DRIVER_SVG(image->format) )
      return;
    
    if(shape->numlines == 0) return; 



    bFullRes = 0;
    if (strcasecmp(msGetOutputFormatOption(image->format, "FULL_RESOLUTION",""), 
                   "TRUE") == 0)
      bFullRes = 1;

    bUseGeoCoord= 0;
    
    /* if (strcasecmp(msGetOutputFormatOption(image->format, 
                                           "USE_GEOGRAPHICAL_COORDINATES",""),
                                           "TRUE") == 0) */
    /* bUseGeoCoord = 1;*/

    /* if it is full resolution and we want geographical output */
    /* just return, no transformations needed. */
     if (bFullRes && bUseGeoCoord)
      return;

    /* low resolution and pixel output */
    if (!bFullRes && !bUseGeoCoord)
    {
        msTransformShapeToPixel(shape, extent, cellsize);
        return;
    }

    /* full resolution, pixel output */
    if (bFullRes && !bUseGeoCoord)
    {
        if(shape->type == MS_SHAPE_LINE || shape->type == MS_SHAPE_POLYGON) 
        { 
            for(i=0; i<shape->numlines; i++) 
            {
                for(j=0; j < shape->line[i].numpoints; j++ ) 
                {
                    /*
                    shape->line[i].point[j].x = 
                      (shape->line[i].point[j].x - extent.minx)/cellsize;
                    shape->line[i].point[j].y = 
                      (extent.maxy - shape->line[i].point[j].y)/cellsize;
                    
                    */
                    shape->line[i].point[j].x = MS_MAP2IMAGE_X(shape->line[i].point[j].x,
                                                               extent.minx, cellsize);
                    shape->line[i].point[j].y = MS_MAP2IMAGE_Y( shape->line[i].point[j].y,
                                                                extent.maxy,
                                                                cellsize);
                                                                
                    
                }
            }      
            return;
        }
    }

    /* low resolution, geographical output.  */
    /* Function comes from mapprimitive.c msTransformPixelToShape */
    /* TODO : adapat this function. For now just return fullres with geo coordinates */
    if (!bFullRes && bUseGeoCoord)
    {
        return;
        
        /*
        if(shape->type == MS_SHAPE_LINE || shape->type == MS_SHAPE_POLYGON) { // remove co-linear vertices
  
            for(i=0; i<shape->numlines; i++) { // for each part
      
                shape->line[i].point[0].x = MS_MAP2IMAGE_X(shape->line[i].point[0].x, extent.minx, cellsize);
                shape->line[i].point[0].y = MS_MAP2IMAGE_Y(shape->line[i].point[0].y, extent.maxy, cellsize);
      
                for(j=1, k=1; j < shape->line[i].numpoints; j++ ) {
	
                    shape->line[i].point[k].x = MS_MAP2IMAGE_X(shape->line[i].point[j].x, extent.minx, cellsize);
                    shape->line[i].point[k].y = MS_MAP2IMAGE_Y(shape->line[i].point[j].y, extent.maxy, cellsize);

                    if(k == 1) {
                        if((shape->line[i].point[0].x != shape->line[i].point[1].x) || (shape->line[i].point[0].y != shape->line[i].point[1].y))
                          k++;
                    } else {
                        if((shape->line[i].point[k-1].x != shape->line[i].point[k].x) || (shape->line[i].point[k-1].y != shape->line[i].point[k].y)) {
                            if(((shape->line[i].point[k-2].y - shape->line[i].point[k-1].y)*(shape->line[i].point[k-1].x - shape->line[i].point[k].x)) == ((shape->line[i].point[k-2].x - shape->line[i].point[k-1].x)*(shape->line[i].point[k-1].y - shape->line[i].point[k].y))) {	    
                                shape->line[i].point[k-1].x = shape->line[i].point[k].x;
                                shape->line[i].point[k-1].y = shape->line[i].point[k].y;	
                            } else {
                                k++;
                            }
                        }
                    }
                }
                shape->line[i].numpoints = k; // save actual number kept
            }
        } else { // points or untyped shapes
            for(i=0; i<shape->numlines; i++) { // for each part
                for(j=1; j < shape->line[i].numpoints; j++ ) {
                    shape->line[i].point[j].x = MS_MAP2IMAGE_X(shape->line[i].point[j].x, extent.minx, cellsize);
                    shape->line[i].point[j].y = MS_MAP2IMAGE_Y(shape->line[i].point[j].y, extent.maxy, cellsize);
                }
            }
        }
        */
    }
}

/************************************************************************/
/*                           msDrawLineSymbolSVG                        */
/*                                                                      */
/*      Draw line layers.                                               */
/************************************************************************/
MS_DLL_EXPORT void msDrawLineSymbolSVG(symbolSetObj *symbolset, 
                                       imageObj *image, 
                                       shapeObj *p, styleObj *style, 
                                       double scalefactor)
{
    /* int styleDashed[100]; */
    int ox, oy;
    double size;
    int width;
    /* gdPoint points[MS_MAXVECTORPOINTS]; */
    symbolObj *symbol;


/* -------------------------------------------------------------------- */
/*      if not SVG, return.                                             */
/* -------------------------------------------------------------------- */
    if (!image || !MS_DRIVER_SVG(image->format) )
        return;

    if(!p) return;
    if(p->numlines <= 0) return;

    if(style->size == -1) {
      size = (int)msSymbolGetDefaultSize( &( symbolset->symbol[style->symbol] ) );
    }
    else
      size = style->size;

    /* TODO: Don't get this modification, is it needed elsewhere? */
    if(size*scalefactor > style->maxsize) scalefactor = (float)style->maxsize/(float)size;
    if(size*scalefactor < style->minsize) scalefactor = (float)style->minsize/(float)size;
    size = MS_NINT(size*scalefactor);
    size = MS_MAX(size, style->minsize);
    size = MS_MIN(size, style->maxsize);

    width = MS_NINT(style->width*scalefactor);
    width = MS_MAX(width, style->minwidth);
    width = MS_MIN(width, style->maxwidth);

    if(style->symbol > symbolset->numsymbols || style->symbol < 0) return; /* no such symbol, 0 is OK */

     if (!MS_VALID_COLOR( style->color))
      return;
    
    if(size < 1) return; /* size too small */

    /* TODO : do we need offset ?? */
    ox = MS_NINT(style->offsetx*scalefactor);
    oy = (style->offsety == -99) ? -99 : (int)(style->offsety*scalefactor);

    symbol = &(symbolset->symbol[style->symbol]);


    /* TODO : all lines are draw as filled lines.  */
    
    /* if default symbol 0 is used, use the style's width parameter */
    /* else use the symbol size. */
    /* TODO : size needs to be double ? */
    if(style->symbol == 0) 
      imagePolyline(image->img.svg->stream, image->img.svg->compressed, 
                    p, &style->color, width,
                    symbol->stylelength,  symbol->style);
    else
      imagePolyline(image->img.svg->stream, image->img.svg->compressed, 
                    p, &style->color, (int)size,
                    symbol->stylelength,  symbol->style);
        return;
    

}

/************************************************************************/
/*                           msImageStartLayerSVG                       */
/************************************************************************/
void msImageStartLayerSVG(mapObj *map, layerObj *layer, imageObj *image)
{
    if (image && MS_DRIVER_SVG(image->format) && layer && map)/* && filename) */
    {
        if (strcasecmp(msGetOutputFormatOption(image->format, "GOSVG",""),
                       "TRUE") != 0)
          msIO_fprintfgz(image->img.svg->stream, image->img.svg->compressed,  "\n<!-- START LAYER %s -->\n", layer->name); 
    }
}



static void imageFillPolygon(FILE *fp, int bCompressed, 
                             shapeObj *p, colorObj *psFillColor, 
                             colorObj *psOutlineColor, int size,
                             int symbolstylelength, int *symbolstyle)
{
    int i, j, k,max;
    char *pszDashArray = NULL;
    char szTmp[100];


    if (!fp || !p || (psFillColor==NULL && psOutlineColor==NULL) || size <0 )
      return;

    /* the default size is 1. */
    if (size <= 0)
      size = 1;

    max=0;
    for (i = 0; i < p->numlines; i++)
    {
      if(p->line[i].numpoints > max)
        max=p->line[i].numpoints;
    }

    for (i = 0; i < p->numlines; i++)
    {
        if (i == 0)
        {
            pszDashArray = strcatalloc(pszDashArray,"");
            if (symbolstylelength > 0 && symbolstyle)
            {
                sprintf(szTmp, "stroke-dasharray=\"");
                pszDashArray = strcatalloc(pszDashArray, szTmp);
                for (k=0; k<symbolstylelength; k++)
                {
                    if (k < symbolstylelength-1)
                      sprintf(szTmp, "%d, ", symbolstyle[k]);
                    else
                      sprintf(szTmp, "%d\"", symbolstyle[k]);

                    pszDashArray = strcatalloc(pszDashArray, szTmp);
                }
            }
            if (max > 2)
            {
              if (psOutlineColor && psFillColor)
              /*msIO_fprintfgz(fp, bCompressed, "<polygon fill=\"#%02x%02x%02x\" stroke=\"#%02x%02x%02x\"  stroke-width=\"%d\" %s points=\"",
                           psFillColor->red, psFillColor->green, 
                           psFillColor->blue,
                           psOutlineColor->red, psOutlineColor->green, 
                           psOutlineColor->blue,
                           size, pszDashArray);*/
                msIO_fprintfgz(fp, bCompressed, "<path fill=\"#%02x%02x%02x\" stroke=\"#%02x%02x%02x\" stroke-width=\"%d\"%s d=\"",
                           psFillColor->red, psFillColor->green, 
                           psFillColor->blue,
                           psOutlineColor->red, psOutlineColor->green, 
                           psOutlineColor->blue,
                           size, pszDashArray);
            
              else if (psOutlineColor)
              {
                /*msIO_fprintfgz(fp,  bCompressed, "<polygon  stroke=\"#%02x%02x%02x\"  stroke-width=\"%d\" %s points=\"",
                           psOutlineColor->red, psOutlineColor->green, 
                           psOutlineColor->blue,
                           size, pszDashArray);*/
                msIO_fprintfgz(fp,  bCompressed, "<path stroke=\"#%02x%02x%02x\" stroke-width=\"%d\"%s d=\"",
                           psOutlineColor->red, psOutlineColor->green, 
                           psOutlineColor->blue,
                           size, pszDashArray);
              }
              else /* fill color valid  */
              {
                /*msIO_fprintfgz(fp,  bCompressed, "<polygon fill=\"#%02x%02x%02x\" points=\"",
                             psFillColor->red, psFillColor->green, 
                             psFillColor->blue);*/
                msIO_fprintfgz(fp,  bCompressed, "<path fill=\"#%02x%02x%02x\" d=\"",
                             psFillColor->red, psFillColor->green, 
                             psFillColor->blue);
                           
              }
            }
        }

        if(p->line[i].numpoints > 2)
        {
          msIO_fprintfgz(fp,  bCompressed, "M %d %d ", (int)p->line[i].point[0].x, (int)p->line[i].point[0].y);
        for(j=1; j<p->line[i].numpoints; j++)
          msIO_fprintfgz(fp,  bCompressed, "L %d %d ", (int)p->line[i].point[j].x, (int)p->line[i].point[j].y);

            /*msIO_fprintfgz(fp,  bCompressed, " %d,%d", (int)p->line[i].point[j].x, (int)p->line[i].point[j].y);*/
        
        }
        if (i == (p->numlines -1) && max > 2)
          msIO_fprintfgz(fp,  bCompressed,"z\"/>\n");
    }

}


/************************************************************************/
/*                           msDrawShadeSymbolSVG                       */
/*                                                                      */
/*      Drawing of POLYGON layers.                                      */
/************************************************************************/
void msDrawShadeSymbolSVG(symbolSetObj *symbolset, imageObj *image, 
                          shapeObj *p, styleObj *style, double scalefactor)
{
    colorObj    *psFillColor = NULL;
    colorObj    *psOutlineColor = NULL;
    colorObj    sFc;
    colorObj    sOc;
    symbolObj   *symbol;
    int         size;
/* -------------------------------------------------------------------- */
/*      if not svg, return.                                             */
/* -------------------------------------------------------------------- */
    if (image == NULL || !MS_DRIVER_SVG(image->format) )
      return;
    
    if(p == NULL || p->numlines <= 0)
      return;


    symbol = &(symbolset->symbol[style->symbol]);

    if(style->size == -1) 
    {
        size = (int)msSymbolGetDefaultSize( &( symbolset->symbol[style->symbol] ) );
        size = MS_NINT(size*scalefactor);
    }
    else
      size = MS_NINT(style->size*scalefactor);

    size = MS_MAX(size, style->minsize);
    size = MS_MIN(size, style->maxsize);

    if(style->symbol > symbolset->numsymbols || style->symbol < 0) /* no such symbol, 0 is OK */
        return;

    if(size < 1) /* size too small */
        return;

    sFc.red = style->color.red;
    sFc.green = style->color.green;
    sFc.blue = style->color.blue;

    sOc.red = style->outlinecolor.red;
    sOc.green = style->outlinecolor.green;
    sOc.blue = style->outlinecolor.blue;

    if (MS_VALID_COLOR(sFc))
      psFillColor = &sFc;
    if (MS_VALID_COLOR(sOc))
      psOutlineColor = &sOc;

    imageFillPolygon(image->img.svg->stream, image->img.svg->compressed ,p, psFillColor, psOutlineColor, size,
                     symbol->stylelength,  symbol->style);

}

#define Tcl_UniChar int
#define TCL_UTF_MAX 3
/*comes from gd source code (ggdft.c).*/

#ifdef notdef /* disabled since not currently used */
static int gdTcl_UtfToUniChar (char *str, Tcl_UniChar * chPtr)
{
  int byte;
  
  /*
   * Unroll 1 to 3 byte UTF-8 sequences, use loop to handle longer ones.
   */

  byte = *((unsigned char *) str);

  if (byte < 0xC0)
    {
      /*
       * Handles properly formed UTF-8 characters between
       * 0x01 and 0x7F.  Also treats \0 and naked trail
       * bytes 0x80 to 0xBF as valid characters representing
       * themselves.
       */

      *chPtr = (Tcl_UniChar) byte;
      return 1;
    }
  else if (byte < 0xE0)
    {
      if ((str[1] & 0xC0) == 0x80)
	{
	  /*
	   * Two-byte-character lead-byte followed
	   * by a trail-byte.
	   */

	  *chPtr = (Tcl_UniChar) (((byte & 0x1F) << 6) | (str[1] & 0x3F));
	  return 2;
	}
      /*
       * A two-byte-character lead-byte not followed by trail-byte
       * represents itself.
       */

      *chPtr = (Tcl_UniChar) byte;
      return 1;
    }
  else if (byte < 0xF0)
    {
      if (((str[1] & 0xC0) == 0x80) && ((str[2] & 0xC0) == 0x80))
	{
	  /*
	   * Three-byte-character lead byte followed by
	   * two trail bytes.
	   */

	  *chPtr = (Tcl_UniChar) (((byte & 0x0F) << 12)
				  | ((str[1] & 0x3F) << 6) | (str[2] & 0x3F));
	  return 3;
	}
      /*
       * A three-byte-character lead-byte not followed by
       * two trail-bytes represents itself.
       */

      *chPtr = (Tcl_UniChar) byte;
      return 1;
    }
#if TCL_UTF_MAX > 3
  else
    {
      int ch, total, trail;

      total = totalBytes[byte];
      trail = total - 1;
      if (trail > 0)
	{
	  ch = byte & (0x3F >> trail);
	  do
	    {
	      str++;
	      if ((*str & 0xC0) != 0x80)
		{
		  *chPtr = byte;
		  return 1;
		}
	      ch <<= 6;
	      ch |= (*str & 0x3F);
	      trail--;
	    }
	  while (trail > 0);
	  *chPtr = ch;
	  return total;
	}
    }
#endif

  *chPtr = (Tcl_UniChar) byte;
  return 1;
}
#endif /* def notdef */

static void drawSVGText(FILE *fp, int bCompressed, int x, int y, 
                        char *string, double size, 
                        colorObj *psColor, colorObj *psOutlineColor,
                        char *pszFontFamilily,  char *pszFontStyle,
                        char *pszFontWeight, int nAnchorPosition,
                        double dfAngle, int bEncoding)
{
    char *pszFontStyleString = NULL;
    char *pszFontWeightString = NULL;
    char *pszFillString = NULL, *pszStrokeString = NULL;
    char *pszAngleString=NULL, *pszAngleAnchorString=NULL;
    char *pszFinalString=NULL;
    /*    char *pszTmpStr = NULL;*/
    char szTmp[100];
    /*    char *next; */
    /*    int len, i, ch; */

    pszFontStyleString = strcatalloc(pszFontStyleString, "");
    pszFontWeightString = strcatalloc(pszFontWeightString, "");
    pszAngleString = strcatalloc(pszAngleString, "");
    pszAngleAnchorString = strcatalloc(pszAngleAnchorString, "");

    if (pszFontStyle)
    {
        sprintf(szTmp, " font-style=\"%s\"", pszFontStyle);
        pszFontStyleString = strcatalloc(pszFontStyleString, szTmp);
    }

    if (pszFontWeight)
    {
        sprintf(szTmp, " font-weight=\"%s\"", pszFontWeight);
        pszFontWeightString = strcatalloc(pszFontWeightString, szTmp);
    }
 
    pszFillString =  strcatalloc(pszFillString, "");
    if (psColor)
    {
        if (MS_VALID_COLOR(*psColor))
          sprintf(szTmp, " fill=\"#%02x%02x%02x\"",
                  psColor->red, psColor->green  , psColor->blue);
        else
          sprintf(szTmp," fill=\"none\"");

        pszFillString = strcatalloc(pszFillString, szTmp);
    }

    pszStrokeString =  strcatalloc(pszStrokeString, "");
    if (psOutlineColor && MS_VALID_COLOR(*psOutlineColor))
    {
        sprintf(szTmp, " stroke=\"#%02x%02x%02x\" stroke-width=\"0.5\"",
                psOutlineColor->red, psOutlineColor->green  , psOutlineColor->blue);
        pszStrokeString = strcatalloc(pszStrokeString, szTmp);
    }

    /* angle */
    if (dfAngle > 0.0)
    {
        sprintf(szTmp, " transform=\"rotate(%f %d %d)\"",
                -dfAngle, x, y);
        pszAngleString = strcatalloc(pszAngleString, szTmp);
    }

    /* anchor point */
    if (nAnchorPosition == MS_UL || nAnchorPosition == MS_CL ||
        nAnchorPosition == MS_LL)
    {
        sprintf(szTmp, " text-anchor=\"end\"");
        pszAngleAnchorString = strcatalloc(pszAngleAnchorString, szTmp);
    }
    else if (nAnchorPosition == MS_UC || nAnchorPosition == MS_CC ||
             nAnchorPosition == MS_LC)
    {
        sprintf(szTmp, " text-anchor=\"middle\"");
        pszAngleAnchorString = strcatalloc(pszAngleAnchorString, szTmp);
    }
    else if (nAnchorPosition == MS_UR || nAnchorPosition == MS_CR ||
             nAnchorPosition == MS_LR)
    {
        sprintf(szTmp, " text-anchor=\"start\"");
        pszAngleAnchorString = strcatalloc(pszAngleAnchorString, szTmp);
    }

/*    if (bEncoding)
    {
        pszTmpStr = strcatalloc(pszTmpStr, string);
        next = pszTmpStr;
        for (i=0; *next; i++)
        {
            ch = *next;
            
            
            len = gdTcl_UtfToUniChar (next, &ch);
            next += len;
        
            sprintf(szTmp, "&#x%x;",ch);
            pszFinalString = strcatalloc(pszFinalString, szTmp);
        }
    }
    else*/
      pszFinalString = string;

    /* TODO : font size set to unit pt */
    msIO_fprintfgz(fp, bCompressed,"<text x=\"%d\" y=\"%d\" font-family=\"%s\" font-size=\"%dpt\"%s%s%s%s%s%s>%s</text>\n",
                 x, y, pszFontFamilily, (int)size, 
                 pszFontStyleString, pszFontWeightString, 
                 pszFillString, pszStrokeString, pszAngleString, 
                 pszAngleAnchorString, pszFinalString);

    if (pszFontStyleString)
      msFree(pszFontStyleString);
    if (pszFontWeightString)
      msFree(pszFontWeightString);
    if (pszFillString)
      msFree(pszFillString);
    if (pszStrokeString)
      msFree(pszStrokeString);
    if (pszAngleString)
      msFree(pszAngleString);
    if (pszAngleAnchorString)
      msFree(pszAngleAnchorString);
    if (bEncoding && pszFinalString)
      msFree(pszFinalString);
    
}


/************************************************************************/
/*                              msDrawTextSVG                           */
/************************************************************************/
int msDrawTextSVG(imageObj *image, pointObj labelPnt, char *string, 
                 labelObj *label, fontSetObj *fontset, double scalefactor)
{
    char  *font=NULL;
    double size;
    colorObj sColor, sOutlineColor;
    char **aszFontsParts = NULL;
    int nFontParts= 0;
    char *pszFontFamily = NULL, *pszFontStyle=NULL, *pszFontWeight=NULL;
    int x, y;
    int bEncoding = 0;

/* -------------------------------------------------------------------- */
/*      if not svg or invaid inputs, return.                            */
/* -------------------------------------------------------------------- */
    if (!image || !string || (strlen(string) == 0) || !label || !fontset || 
        !MS_DRIVER_SVG(image->format) )
      return (0);

    if(label->encoding != NULL ) 
    { /* converting the label encoding */
        
        string = msGetEncodedString(string, label->encoding);
        if(string == NULL) return(-1);
        bEncoding = 1;
    }

    /* TODO : not transform it to integer is layer transform is false */
    x = MS_NINT(labelPnt.x);
    y = MS_NINT(labelPnt.y);


#ifndef USE_GD_FT
    msSetError(MS_TTFERR, "TrueType font support is not available.", "msDrawTextSVG()");
    /* if(label->encoding != NULL) msFree(string); */
    return(-1); 
#endif

 
    if(label->type == MS_TRUETYPE) 
    {
        sColor.red = -1;
        sColor.green = -1;
        sColor.blue = -1;

        sOutlineColor.red = -1;
        sOutlineColor.green = -1;
        sOutlineColor.blue = -1;

        /* TODO : check support for all parameters  */
        /* color, outlinecolor, shadowcolor, backgroundcolor, backgroundshadowcolor */
        /* position, offset, angle, buffer, antialias, wrap, encoding */

        size = label->size*scalefactor;
        size = MS_MAX(size, label->minsize);
        size = MS_MIN(size, label->maxsize);

        if(!fontset) {
            msSetError(MS_TTFERR, "No fontset defined.", "msDrawTextSVG()");
            if(label->encoding != NULL) msFree(string);
            return(-1);
        }
        
        if(!label->font) {
            msSetError(MS_TTFERR, "No Trueype font defined.", "msDrawTextGD()");
            if(label->encoding != NULL) msFree(string);
            return(-1);
        }
        
        font = msLookupHashTable(&(fontset->fonts), label->font);
        if(!font) {
            msSetError(MS_TTFERR, "Requested font (%s) not found.", "msDrawTextSVg()", label->font);
            if(label->encoding != NULL) msFree(string);
            return(-1);
        }
    
        if (MS_VALID_COLOR(label->color))
        {
            sColor.red = label->color.red;
            sColor.green = label->color.green;
            sColor.blue = label->color.blue;
        }
        if (MS_VALID_COLOR(label->outlinecolor))
        {
            sOutlineColor.red = label->outlinecolor.red;
            sOutlineColor.green = label->outlinecolor.green;
            sOutlineColor.blue = label->outlinecolor.blue;
        }

        if (!MS_VALID_COLOR(label->color) && !MS_VALID_COLOR(label->outlinecolor))
        {
            msSetError(MS_TTFERR, "Invalid color", "drawSVGText()");
            return(-1);
        }
        
/* -------------------------------------------------------------------- */
/*      parse font string :                                             */
/*      There are 3 parts to the name separated with - (example         */
/*      arial-italic-bold):                                             */
/*        - font-family,                                                */
/*        - font-style (italic, oblique, nomal)                         */
/*        - font-weight (normal | bold | bolder | lighter | 100 | 200 ..*/
/* -------------------------------------------------------------------- */
        /* parse font string :  */
        aszFontsParts = split(label->font, '_', &nFontParts);
 
        pszFontFamily = aszFontsParts[0];
        if (nFontParts == 3)
        {
            pszFontStyle = aszFontsParts[1];
            pszFontWeight = aszFontsParts[2];
        }
        else if (nFontParts == 2)
        {
            if (strcasecmp(aszFontsParts[1], "italic") == 0 ||
                strcasecmp(aszFontsParts[1], "oblique") == 0 ||
                strcasecmp(aszFontsParts[1], "normal") == 0)
              pszFontStyle = aszFontsParts[1];
            else
              pszFontWeight = aszFontsParts[1];
        }

        drawSVGText(image->img.svg->stream, image->img.svg->compressed, x, y, 
                    string, size, 
                    &sColor, &sOutlineColor,
                    pszFontFamily,  pszFontStyle, pszFontWeight,
                    label->position, label->angle, bEncoding);

        /* msFreeCharArray(aszFontsParts, nFontParts); */

        return 0;
    }

    return -1;
}


/************************************************************************/
/*                           msDrawLabelCacheSVG                        */
/*                                                                      */
/*      This function comes from mapgd.c (function                      */
/*      msDrawLabelCacheGD) with minor adjustments.                     */
/************************************************************************/
int msDrawLabelCacheSVG(imageObj *image, mapObj *map)
{
  pointObj p;
  int i, j, l;
  rectObj r;
  
  labelCacheMemberObj *cachePtr=NULL;
  layerObj *layerPtr=NULL;
  labelObj *labelPtr=NULL;

  int marker_width, marker_height;
  int marker_offset_x, marker_offset_y;
  rectObj marker_rect;

/* -------------------------------------------------------------------- */
/*      if not svg or invaid inputs, return.                            */
/* -------------------------------------------------------------------- */
    if (!image || !map || !MS_DRIVER_SVG(image->format) )
      return (0);

  
  for(l=map->labelcache.numlabels-1; l>=0; l--) {

    cachePtr = &(map->labelcache.labels[l]); /* point to right spot in the label cache */

    layerPtr = &(map->layers[cachePtr->layerindex]); /* set a couple of other pointers, avoids nasty references */
    labelPtr = &(cachePtr->label);

    if(!cachePtr->text || strlen(cachePtr->text) == 0)
      continue; /* not an error, just don't want to do anything */

    if(msGetLabelSize(cachePtr->text, labelPtr, &r, &(map->fontset), layerPtr->scalefactor, MS_TRUE) == -1)
      return(-1);

    if(labelPtr->autominfeaturesize && ((r.maxx-r.minx) > cachePtr->featuresize))
      continue; /* label too large relative to the feature */

    marker_offset_x = marker_offset_y = 0; /* assume no marker */
    if((layerPtr->type == MS_LAYER_ANNOTATION && cachePtr->numstyles > 0) || layerPtr->type == MS_LAYER_POINT) { /* there *is* a marker       */

      /* TO DO: at the moment only checks the bottom style, perhaps should check all of them */
      if(msGetMarkerSize(&map->symbolset, &(cachePtr->styles[0]), &marker_width, &marker_height, layerPtr->scalefactor) != MS_SUCCESS)
	return(-1);

      marker_offset_x = MS_NINT(marker_width/2.0);
      marker_offset_y = MS_NINT(marker_height/2.0);      

      marker_rect.minx = MS_NINT(cachePtr->point.x - .5 * marker_width);
      marker_rect.miny = MS_NINT(cachePtr->point.y - .5 * marker_height);
      marker_rect.maxx = marker_rect.minx + (marker_width-1);
      marker_rect.maxy = marker_rect.miny + (marker_height-1); 
    }

    if(labelPtr->position == MS_AUTO) {

      if(layerPtr->type == MS_LAYER_LINE) {
	int position = MS_UC;

	for(j=0; j<2; j++) { /* Two angles or two positions, depending on angle. Steep angles will use the angle approach, otherwise we'll rotate between UC and LC. */

	  msFreeShape(cachePtr->poly);
	  cachePtr->status = MS_TRUE; /* assume label *can* be drawn */

	  p = get_metrics(&(cachePtr->point), position, r, (marker_offset_x + labelPtr->offsetx), (marker_offset_y + labelPtr->offsety), labelPtr->angle, labelPtr->buffer, cachePtr->poly);

	  if(layerPtr->type == MS_LAYER_ANNOTATION && cachePtr->numstyles > 0)
	    msRectToPolygon(marker_rect, cachePtr->poly); /* save marker bounding polygon */

	  if(!labelPtr->partials) { /* check against image first */
	    if(labelInImage(map->width, map->height, cachePtr->poly, labelPtr->buffer) == MS_FALSE) {
	      cachePtr->status = MS_FALSE;
	      continue; /* next angle */
	    }
	  }

	  for(i=0; i<map->labelcache.nummarkers; i++) { /* compare against points already drawn */
	    if(l != map->labelcache.markers[i].id) { /* labels can overlap their own marker */
	      if(intersectLabelPolygons(map->labelcache.markers[i].poly, cachePtr->poly) == MS_TRUE) { /* polys intersect */
		cachePtr->status = MS_FALSE;
		break;
	      }
	    }
	  }

	  if(!cachePtr->status)
	    continue; /* next angle */

	  for(i=l+1; i<map->labelcache.numlabels; i++) { /* compare against rendered labels */
	    if(map->labelcache.labels[i].status == MS_TRUE) { /* compare bounding polygons and check for duplicates */

	      if((labelPtr->mindistance != -1) && (cachePtr->classindex == map->labelcache.labels[i].classindex) && (strcmp(cachePtr->text,map->labelcache.labels[i].text) == 0) && (msDistancePointToPoint(&(cachePtr->point), &(map->labelcache.labels[i].point)) <= labelPtr->mindistance)) { /* label is a duplicate */
		cachePtr->status = MS_FALSE;
		break;
	      }

	      if(intersectLabelPolygons(map->labelcache.labels[i].poly, cachePtr->poly) == MS_TRUE) { /* polys intersect */
		cachePtr->status = MS_FALSE;
		break;
	      }
	    }
	  }

	  if(cachePtr->status) /* found a suitable place for this label */
	    break;

	} /* next angle */

      } else {
	for(j=0; j<=7; j++) { /* loop through the outer label positions */

	  msFreeShape(cachePtr->poly);
	  cachePtr->status = MS_TRUE; /* assume label *can* be drawn */

	  p = get_metrics(&(cachePtr->point), j, r, (marker_offset_x + labelPtr->offsetx), (marker_offset_y + labelPtr->offsety), labelPtr->angle, labelPtr->buffer, cachePtr->poly);

	  if(layerPtr->type == MS_LAYER_ANNOTATION && cachePtr->numstyles > 0)
	    msRectToPolygon(marker_rect, cachePtr->poly); /* save marker bounding polygon */

	  if(!labelPtr->partials) { /* check against image first */
	    if(labelInImage(map->width, map->height, cachePtr->poly, labelPtr->buffer) == MS_FALSE) {
	      cachePtr->status = MS_FALSE;
	      continue; /* next position */
	    }
	  }

	  for(i=0; i<map->labelcache.nummarkers; i++) { /* compare against points already drawn */
	    if(l != map->labelcache.markers[i].id) { /* labels can overlap their own marker */
	      if(intersectLabelPolygons(map->labelcache.markers[i].poly, cachePtr->poly) == MS_TRUE) { /* polys intersect */
		cachePtr->status = MS_FALSE;
		break;
	      }
	    }
	  }

	  if(!cachePtr->status)
	    continue; /* next position */

	  for(i=l+1; i<map->labelcache.numlabels; i++) { /* compare against rendered labels */
	    if(map->labelcache.labels[i].status == MS_TRUE) { /* compare bounding polygons and check for duplicates */

	      if((labelPtr->mindistance != -1) && (cachePtr->classindex == map->labelcache.labels[i].classindex) && (strcmp(cachePtr->text,map->labelcache.labels[i].text) == 0) && (msDistancePointToPoint(&(cachePtr->point), &(map->labelcache.labels[i].point)) <= labelPtr->mindistance)) { /* label is a duplicate */
		cachePtr->status = MS_FALSE;
		break;
	      }

	      if(intersectLabelPolygons(map->labelcache.labels[i].poly, cachePtr->poly) == MS_TRUE) { /* polys intersect */
		cachePtr->status = MS_FALSE;
		break;
	      }
	    }
	  }

	  if(cachePtr->status) /* found a suitable place for this label */
	    break;
	} /* next position */
      }

      if(labelPtr->force) cachePtr->status = MS_TRUE; /* draw in spite of collisions based on last position, need a *best* position */

    } else {
      cachePtr->status = MS_TRUE; /* assume label *can* be drawn */

      if(labelPtr->position == MS_CC) /* don't need the marker_offset */
        p = get_metrics(&(cachePtr->point), labelPtr->position, r, labelPtr->offsetx, labelPtr->offsety, labelPtr->angle, labelPtr->buffer, cachePtr->poly);
      else
        p = get_metrics(&(cachePtr->point), labelPtr->position, r, (marker_offset_x + labelPtr->offsetx), (marker_offset_y + labelPtr->offsety), labelPtr->angle, labelPtr->buffer, cachePtr->poly);

      if(layerPtr->type == MS_LAYER_ANNOTATION && cachePtr->numstyles > 0)
	msRectToPolygon(marker_rect, cachePtr->poly); /* save marker bounding polygon, part of overlap tests */

      if(!labelPtr->force) { /* no need to check anything else */

	if(!labelPtr->partials) {
	  if(labelInImage(map->width, map->height, cachePtr->poly, labelPtr->buffer) == MS_FALSE)
	    cachePtr->status = MS_FALSE;
	}

	if(!cachePtr->status)
	  continue; /* next label */

	for(i=0; i<map->labelcache.nummarkers; i++) { /* compare against points already drawn */
	  if(l != map->labelcache.markers[i].id) { /* labels can overlap their own marker */
	    if(intersectLabelPolygons(map->labelcache.markers[i].poly, cachePtr->poly) == MS_TRUE) { /* polys intersect */
	      cachePtr->status = MS_FALSE;
	      break;
	    }
	  }
	}

	if(!cachePtr->status)
	  continue; /* next label */

	for(i=l+1; i<map->labelcache.numlabels; i++) { /* compare against rendered label */
	  if(map->labelcache.labels[i].status == MS_TRUE) { /* compare bounding polygons and check for duplicates */
	    if((labelPtr->mindistance != -1) && (cachePtr->classindex == map->labelcache.labels[i].classindex) && (strcmp(cachePtr->text, map->labelcache.labels[i].text) == 0) && (msDistancePointToPoint(&(cachePtr->point), &(map->labelcache.labels[i].point)) <= labelPtr->mindistance)) { /* label is a duplicate */
	      cachePtr->status = MS_FALSE;
	      break;
	    }

	    if(intersectLabelPolygons(map->labelcache.labels[i].poly, cachePtr->poly) == MS_TRUE) { /* polys intersect */          
	      cachePtr->status = MS_FALSE;
	      break;
	    }
	  }
	}
      }
    } /* end position if-then-else */

    /* imagePolyline(img, cachePtr->poly, 1, 0, 0); */

    if(!cachePtr->status)
      continue; /* next label */

    if(layerPtr->type == MS_LAYER_ANNOTATION && cachePtr->numstyles > 0) { /* need to draw a marker */
      for(i=0; i<cachePtr->numstyles; i++)
        msDrawMarkerSymbolSVG(&map->symbolset, image, &(cachePtr->point), &(cachePtr->styles[i]), layerPtr->scalefactor);
    }

    /* background not supported */
    /* if(MS_VALID_COLOR(labelPtr->backgroundcolor)) billboardGD(img, cachePtr->poly, labelPtr); */
    msDrawTextSVG(image, p, cachePtr->text, labelPtr, &(map->fontset), layerPtr->scalefactor); /* actually draw the label */

  } /* next label */

  
  return(0);
}

/************************************************************************/
/*                          msDrawMarkerSymbolSVG                       */
/*                                                                      */
/*      Draw symbols.                                                   */
/************************************************************************/
void msDrawMarkerSymbolSVG(symbolSetObj *symbolset, imageObj *image, 
                          pointObj *p, styleObj *style, double scalefactor)
{
    char szTmp[100];
    symbolObj *symbol=NULL;
    double size,d;
    int width;
    int x,y,rx,ry;
    char *pszFill = NULL, *pszStroke=NULL;
    int bFillSetToNone = 0;
    int i, j, k;
    pointObj mPoints[MS_MAXVECTORPOINTS];
    pointObj oldpnt,newpnt;
    int offset_x, offset_y;
    char *pszFontFamily = NULL, *pszFontStyle=NULL, *pszFontWeight=NULL;
    char **aszFontsParts = NULL;
    int nFontParts= 0;
    char *font=NULL;
    rectObj rect;

/* -------------------------------------------------------------------- */
/*      if not svg or invaid inputs, return.                            */
/* -------------------------------------------------------------------- */
    if (!image || !p || !style || !MS_DRIVER_SVG(image->format) )
      return;
   
    if(!p) return;

    symbol = &(symbolset->symbol[style->symbol]);

 
    if(style->size == -1) {
    size = msSymbolGetDefaultSize( &( symbolset->symbol[style->symbol] ) );
    size = MS_NINT(size*scalefactor);
    } else
      size = MS_NINT(style->size*scalefactor);
    size = MS_MAX(size, style->minsize);
    size = MS_MIN(size, style->maxsize);

    width = MS_NINT(style->width*scalefactor);
    width = MS_MAX(width, style->minwidth);
    width = MS_MIN(width, style->maxwidth);

    if(style->symbol > symbolset->numsymbols || style->symbol < 0) return; /* no such symbol, 0 is OK */
    
    if (!MS_VALID_COLOR(style->color) && !MS_VALID_COLOR(style->outlinecolor) && 
        symbol->type != MS_SYMBOL_PIXMAP)
      return;

     if(size < 1) return; /* size too small */

     if(style->symbol == 0 )/*&& fc >= 0) {*/ /* simply draw a single pixel of the specified color */
     {
         /*gdImageSetPixel(img, (int)(p->x + ox), (int)(p->y + oy), fc);*/
         return;
     }

      switch(symbol->type) 
      { 
          case(MS_SYMBOL_TRUETYPE): 
#ifdef USE_GD_FT
            font = msLookupHashTable(&(symbolset->fontset->fonts), symbol->font);
            if(!font) return;

            if(msGetCharacterSize(symbol->character, (int)size, font, &rect) 
               != MS_SUCCESS) 
              return;

            x = (int)(p->x +  style->offsetx - (rect.maxx - rect.minx)/2 - rect.minx);
            y = (int)(p->y + style->offsety - rect.maxy + (rect.maxy - rect.miny)/2);

/* -------------------------------------------------------------------- */
/*      parse font string :                                             */
/*      There are 3 parts to the name separated with - (example         */
/*      arial-italic-bold):                                             */
/*        - font-family,                                                */
/*        - font-style (italic, oblique, nomal)                         */
/*        - font-weight (normal | bold | bolder | lighter | 100 | 200 ..*/
/* -------------------------------------------------------------------- */
            /* parse font string :  */
            aszFontsParts = split(symbol->font, '_', &nFontParts);
 
            pszFontFamily = aszFontsParts[0];
            if (nFontParts == 3)
            {
                pszFontStyle = aszFontsParts[1];
                pszFontWeight = aszFontsParts[2];
            }
            else if (nFontParts == 2)
            {
                if (strcasecmp(aszFontsParts[1], "italic") == 0 ||
                    strcasecmp(aszFontsParts[1], "oblique") == 0 ||
                    strcasecmp(aszFontsParts[1], "normal") == 0)
                  pszFontStyle = aszFontsParts[1];
                else
                  pszFontWeight = aszFontsParts[1];
            }

            drawSVGText(image->img.svg->stream, image->img.svg->compressed, 
                        x, y, symbol->character, size, 
                        &style->color, &style->outlinecolor,
                        pszFontFamily,  pszFontStyle, pszFontWeight,
                        -1, -1,0);
            /* msFreeCharArray(aszFontsParts, nFontParts); */
           
#endif            
            break;

          case(MS_SYMBOL_PIXMAP):
            break;
            
          case(MS_SYMBOL_ELLIPSE):
            d = size/symbol->sizey;
            rx = MS_NINT((symbol->sizex*d)/2);
            ry = MS_NINT((symbol->sizey*d)/2);
            
            x = MS_NINT(p->x  + style->offsetx);
            y = MS_NINT(p->y  + style->offsety);

            /*TODO : style->angle */
            pszFill = strcatalloc(pszFill,"");
            if (MS_VALID_COLOR(style->color) && symbol->filled)
            {
                sprintf(szTmp, "fill=\"#%02x%02x%02x\"",style->color.red, 
                        style->color.green,
                        style->color.blue);
                pszFill = strcatalloc(pszFill, szTmp);
            }
            else
            {
                pszFill = strcatalloc(pszFill,"fill=\"none\"");
                bFillSetToNone =1;
            }
            pszStroke = strcatalloc(pszStroke, "");
            if (MS_VALID_COLOR(style->outlinecolor))
            {
                sprintf(szTmp, "stroke=\"#%02x%02x%02x\"",style->outlinecolor.red, 
                        style->outlinecolor.green,
                        style->outlinecolor.blue);
                pszStroke = strcatalloc(pszStroke, szTmp);
            } 
            else if (bFillSetToNone)
            {
                /*if the fill color is not setc (or the symbol is not filled) and 
                the outline color is not set, set the stroke to black. 
                This is the way the gd outputs reacts to this case */
                sprintf(szTmp, "stroke=\"#%02x%02x%02x\"",0,0,0);
                pszStroke = strcatalloc(pszStroke, szTmp);
            }
            

            msIO_fprintfgz(image->img.svg->stream, image->img.svg->compressed,
                           "<ellipse cx=\"%d\" cy=\"%d\" rx=\"%d\" ry=\"%d\" %s %s />\n", x, y, rx, ry, pszFill, pszStroke);
            msFree(pszFill);
            msFree(pszStroke);
            
            break;

          case(MS_SYMBOL_VECTOR):
            d = size/symbol->sizey;
            offset_x = MS_NINT(p->x - d*.5*symbol->sizex +  style->offsetx);
            offset_y = MS_NINT(p->y - d*.5*symbol->sizey +  style->offsety);

            pszFill = strcatalloc(pszFill,"");
            if (MS_VALID_COLOR(style->color) && symbol->filled)
            {
                sprintf(szTmp, "fill=\"#%02x%02x%02x\"",
                        style->color.red, 
                        style->color.green,
                        style->color.blue);
                pszFill = strcatalloc(pszFill, szTmp);
            }
            else
            {
                bFillSetToNone = 1;
                pszFill = strcatalloc(pszFill,"fill=\"none\"");
            }
            pszStroke = strcatalloc(pszStroke, "");
            if (MS_VALID_COLOR(style->outlinecolor))
            {
                sprintf(szTmp, "stroke=\"#%02x%02x%02x\"",
                        style->outlinecolor.red, 
                        style->outlinecolor.green,
                        style->outlinecolor.blue);
                pszStroke = strcatalloc(pszStroke, szTmp);
            }
            else if (bFillSetToNone)
            {
                /*if the fill color is not setc (or the symbol is not filled) and 
                the outline color is not set, set the stroke to black. 
                This is the way the gd outputs reacts to this case */
                sprintf(szTmp, "stroke=\"#%02x%02x%02x\"",0,0,0);
                pszStroke = strcatalloc(pszStroke, szTmp);
            }
            if (width <= 0)
              width = 1;

            if(symbol->filled) 
            { /* if filled */
                k = 0; /* point counter */
                for(j=0;j < symbol->numpoints;j++) 
                {
                    if((symbol->points[j].x < 0) && (symbol->points[j].y < 0)) 
                    { /* new polygon (PENUP) */
                        if(k>2) 
                        {
                            
                            msIO_fprintfgz(image->img.svg->stream, 
                                           image->img.svg->compressed,  
                                         "<polygon %s %s stroke-width=\"%d\" points=\"", 
                                         pszFill, pszStroke, width);
                            
                            for (i=0; i<k;i++)
                            {
                                 msIO_fprintfgz(image->img.svg->stream, 
                                                image->img.svg->compressed,  
                                                " %d,%d", 
                                                (int)mPoints[i].x, 
                                                (int)mPoints[i].y);
                            }
                            msIO_fprintfgz(image->img.svg->stream, 
                                           image->img.svg->compressed,  
                                           "\"/>\n");
                        }
                        k = 0; /* reset point counter */
                    } 
                    else 
                    {
                        mPoints[k].x = MS_NINT(d*symbol->points[j].x + offset_x);
                        mPoints[k].y = MS_NINT(d*symbol->points[j].y + offset_y); 
                        k++;
                    }
                }

                msIO_fprintfgz(image->img.svg->stream, 
                               image->img.svg->compressed,  
                             "<polygon %s %s stroke-width=\"%d\" points=\"", 
                             pszFill, pszStroke, width);
                            
                for (i=0; i<k;i++)
                {
                    msIO_fprintfgz(image->img.svg->stream, 
                                   image->img.svg->compressed, " %d,%d", 
                                 (int)mPoints[i].x, (int)mPoints[i].y);
                }
                msIO_fprintfgz(image->img.svg->stream, 
                               image->img.svg->compressed,  "\"/>\n");
                
            }
            else
            { /* NOT filled */     
                
                 

                oldpnt.x = MS_NINT(d*symbol->points[0].x + offset_x); /* convert first point in marker s */
                oldpnt.y = MS_NINT(d*symbol->points[0].y + offset_y);
      
                for(j=1;j < symbol->numpoints;j++) { /* step through the marker s */
                    if((symbol->points[j].x < 0) && (symbol->points[j].y < 0)) {
                        oldpnt.x = MS_NINT(d*symbol->points[j].x + offset_x);
                        oldpnt.y = MS_NINT(d*symbol->points[j].y + offset_y);
                    } else {
                        if((symbol->points[j-1].x < 0) && (symbol->points[j-1].y < 0)) { /* Last point was PENUP, now a new beginning */
                            oldpnt.x = MS_NINT(d*symbol->points[j].x + offset_x);
                            oldpnt.y = MS_NINT(d*symbol->points[j].y + offset_y);
                        } else {
                            newpnt.x = MS_NINT(d*symbol->points[j].x + offset_x);
                            newpnt.y = MS_NINT(d*symbol->points[j].y + offset_y);
                            msIO_fprintfgz(image->img.svg->stream, 
                                           image->img.svg->compressed,  
                              "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" %s %s stroke-width=\"%d\" stroke-linecap=\"round\"/>\n",
                                         (int)oldpnt.x, (int)oldpnt.y, (int)newpnt.x, (int)newpnt.y,
                                         pszFill, pszStroke, width);
                            oldpnt = newpnt;
                        }
                    }
                } /* end for loop */   

            }
            break;
           
          default:
            break;
      }  
}



/************************************************************************/
/*                            msSaveImagetoFpSVG                        */
/*                                                                      */
/*      Save SVG to file pointer. It is an utility function and is      */
/*      now just used from php_mapscript.c                              */
/************************************************************************/
MS_DLL_EXPORT int msSaveImagetoFpSVG(imageObj *image, FILE *stream)
{
    unsigned char block[4000];
    int bytes_read;
    FILE *fp = NULL;

    if (image && MS_DRIVER_SVG(image->format) && stream)
    {
        if (!image->img.svg->streamclosed)
        {
            msIO_fprintfgz(image->img.svg->stream, image->img.svg->compressed,  "</svg>\n"); 
            if (image->img.svg->compressed)
            {
#ifdef USE_ZLIB
                gzclose(image->img.svg->stream);
#else
                fclose(image->img.svg->stream);
#endif
            }
            else
              fclose(image->img.svg->stream);
           
            image->img.svg->streamclosed = 1;
        }
        fp = fopen(image->img.svg->filename, "rb" );
        if( fp == NULL )
        {
            msSetError( MS_MISCERR, 
                        "Failed to open %s for streaming to stdout.",
                        "msSaveImagetoFpSVG()", image->img.svg->filename);
            return MS_FAILURE;
        }
            
        while( (bytes_read = fread(block, 1, sizeof(block), fp)) > 0 )
          msIO_fwrite( block, 1, bytes_read, stream );

        fclose( fp );

        return MS_SUCCESS;
    }	
    
    return MS_FAILURE;
}

      
        
/************************************************************************/
/*                              msSaveImageSVG                          */
/*                                                                      */
/*      Save SVG to file or send to standard output.                    */
/************************************************************************/
MS_DLL_EXPORT int msSaveImageSVG(imageObj *image, char *filename)
{
    FILE *fp = NULL, *stream=NULL;
    unsigned char block[4000];
    int bytes_read;

    if (image && MS_DRIVER_SVG(image->format))/* && filename) */
    {
        if (!image->img.svg->streamclosed)
        {
            msIO_fprintfgz(image->img.svg->stream, image->img.svg->compressed,  "</svg>\n"); 
            if (image->img.svg->compressed)
            {
#ifdef USE_ZLIB
                gzclose(image->img.svg->stream);
#else
                fclose(image->img.svg->stream);
#endif
            }
            else
              fclose(image->img.svg->stream);
            image->img.svg->streamclosed = 1;
        }
        
        if (!filename)
        {
            if (image->img.svg->compressed)
            {
                if( msIO_needBinaryStdout() == MS_FAILURE ) 
                  return MS_FAILURE; 
            }

            fp = fopen(image->img.svg->filename, "rb" );
            if( fp == NULL )
            {
                msSetError( MS_MISCERR, 
                            "Failed to open %s for streaming to stdout.",
                            "msSaveImageSVG()", image->img.svg->filename);
                return MS_FAILURE;
            }
            
            while( (bytes_read = fread(block, 1, sizeof(block), fp)) > 0 )
              msIO_fwrite( block, 1, bytes_read, stdout );

            fclose( fp );
        }
        else
        {
            stream = fopen(filename, "wb");
            if (!stream)
            {
                msSetError(MS_IOERR, "Unable to open file %s for writing",
                           "msSaveImageSVG()", filename);
                return MS_FAILURE;
            }
            fp = fopen(image->img.svg->filename, "rb" );
            if( fp == NULL )
            {
                msSetError( MS_MISCERR, 
                            "Failed to open temporaray svg file %s",
                            "msSaveImageSVG()", image->img.svg->filename);
                return MS_FAILURE;
            }

            while( (bytes_read = fread(block, 1, sizeof(block), fp)) > 0 )
              msIO_fwrite( block, 1, bytes_read, stream );

            fclose( fp );
            fclose(stream);
        }
        return MS_SUCCESS;
    }
    return MS_FAILURE;
   
}



/************************************************************************/
/*                           msDrawRasterLayerSVG                       */
/*                                                                      */
/*      Draw raster layers.                                             */
/************************************************************************/
int msDrawRasterLayerSVG(mapObj *map, layerObj *layer, imageObj *image)
{
    outputFormatObj *format = NULL;
    imageObj    *imagetmp = NULL;
    char        *pszTmpfile = NULL;
    char        *pszURL = NULL;

    if (!image || !map || !MS_DRIVER_SVG(image->format) || 
        image->width <= 0 ||image->height <= 0)
      return MS_FAILURE;

    if (!map->web.imagepath || !map->web.imageurl)
    {
        msSetError(MS_MISCERR, "web image path and imageurl need to be set.",
                   "msDrawRasterLayerSVG");
        return MS_FAILURE;
    }
/* -------------------------------------------------------------------- */
/*      create a temprary GD image and render in it.                    */
/* -------------------------------------------------------------------- */
    format = msCreateDefaultOutputFormat( NULL, "GD/PNG24" );
    if (!format)
      format = msCreateDefaultOutputFormat( NULL, "GD/JPEG" );

    if (!format)
    {
        msSetError(MS_MISCERR, "Unable to crete temporary GD image format (PNG or JPEG)",
                   "msDrawRasterLayerSVG");
        return MS_FAILURE;
    }

    imagetmp = msImageCreate(image->width, image->height, format, 
                               NULL, NULL, map );

    /* TODO : msDrawRasterLayerLow returns 0 (ms_success) in some cases */
    /* without drawing anything (ex : if it does not fit in scale) */
    if (msDrawRasterLayerLow(map, layer, imagetmp) != MS_FAILURE)
    {
        pszTmpfile = msTmpFile(map->mappath,map->web.imagepath,format->extension);
        if (!pszTmpfile)
        {
            msSetError(MS_IOERR, "Failed to create temporary svg file.",
                    "msImageCreateSVG()" );
            return MS_FAILURE;
        }
        msSaveImageGD(imagetmp->img.gd, pszTmpfile, format);
        pszURL = (char *)malloc(sizeof(char)*(strlen(map->web.imageurl)+
                                              strlen(pszTmpfile)+
                                              strlen(format->extension)+2));
        sprintf(pszURL, "%s%s.%s", map->web.imageurl, msGetBasename(pszTmpfile), 
                format->extension);
        msIO_fprintfgz(image->img.svg->stream, image->img.svg->compressed,  "\n<image xlink:href=\"%s\" x=\"0\" y=\"0\" width=\"%d\" height=\"%d\"/>\n", pszURL, map->width, map->height);

         msFreeImage(imagetmp);
         msFree(pszTmpfile);
         msFree(pszURL);

         /* TODO : should we keep track of the file and delete it ? */
         return MS_SUCCESS;
    }

    return MS_FAILURE;
         
 
}

/************************************************************************/
/*                              msFreeImageSVG                          */
/*                                                                      */
/*      TODO                                                            */
/************************************************************************/
MS_DLL_EXPORT void msFreeImageSVG(imageObj *image)
{
}