aboutsummaryrefslogtreecommitdiff
path: root/Python/library/graphics.py
blob: 20f68c7dc7a894446dcb690e0eb2eb8b7f028a9e (plain)
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
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
# -*- coding: iso-8859-1 -*-
"""
#-------------------------------------------------------------------------------
#
#      Graphics.py
#      some graphic design functions
#
#-------------------------------------------------------------------------------
#  Functions to create complexe graphic component :
#  ------------------------------------------------
#      build_zinc_item          (realize a zinc item from description hash table
#                              management of enhanced graphics functions)
#
#      repeat_zinc_item         (duplication of given zinc item)
#
#  Function to compute complexe geometrical forms :
#  (text header of functions explain options for each form,
#  function return curve coords using control points of cubic curve)
#  -----------------------------------------------------------------
#     rounded_rectangle_coords (return curve coords of rounded rectangle)
#     hippodrome_coords       (return curve coords of circus form)
#     ellipse_coords          (return curve coords of ellipse form)
#     polygon_coords          (return curve coords of regular polygon)
#     roundedcurve_coords     (return curve coords of rounded curve)
#     polyline_coords         (return curve coords of polyline)
#     shiftpath_coords        (return curve coords of shifting path)
#     tabbox_coords           (return curve coords of tabbox's pages)
#     pathline_coords         (return triangles coords of pathline)
#
#  Function to compute 2D 1/2 relief and shadow :
#  function build zinc items (triangles and curve) to simulate this
#  -----------------------------------------------------------------
#     graphicitem_relief    (return triangle items simulate relief of given item)
#     polyline_relief_params (return triangle coords
#                           and lighting triangles color list)
#     graphicitem_shadow    (return triangles and curve items
#                           simulate shadow of given item))
#     polyline_shadow_params (return triangle and curve coords
#                           and shadow triangles color list))
#
#
#-------------------------------------------------------------------------------
#      Authors: Jean-Luc Vinot <vinot@cena.fr>
#      PM2PY: Guillaume Vidon <vidon@ath.cena.fr> 
#
# $Id$ 
#-------------------------------------------------------------------------------
"""
VERSION = "1.0"
__revision__ = "0.1"

import logging
import types
import re
from geometry import *
from pictorial import *
from math import pi, radians, atan2, sqrt, sin, cos

graphiclogger = logging.getLogger('Graphics')
debug = graphiclogger.debug
error = graphiclogger.error
exception = graphiclogger.exception
log = lambda msg : graphiclogger.log(logging.DEBUG, msg)

# constante facteur point directeur (conique -> quadratique)
const_ptd_factor = .5523

def transdic(**dict):
    newdic={}
    for key, val in dict.items():
        if (type(val) is types.ListType):
            newdic[key] = tuple(val)
        else:
            newdic[key] = val
    return newdic
    
def is_flat_list(apts):
    if reduce(lambda x, y : x and (type(y) in ( types.FloatType, types.IntType)),
              apts):
        if len(apts) % 2:
            raise ValueError("Not a valid Coords list")
        else :
            return True
    else :
        return False

def is_point(apoint):
    if (type(apoint) in ( types.TupleType, types.ListType)):
        if len(apoint) == 2 :
            if reduce(lambda x, y : x and (type(y) in ( types.FloatType, types.IntType)),
                      apts):
                return True
            else :
                return False
        elif reduce(lambda x, y : x and (type(y) in ( types.FloatType, types.IntType)),
                    apts[:-1])\
                    and type(apts[-1]) in ('c', 'n'):
            return True
        else :
            return False
    else :
        return False
                
# def is_tuple_list(apts):
#     if reduce(lambda x, y : x \
#               and is_point(x)),
#               apts):
                  
    
def lpts2coords(lpts):
    coords = []
    if (type(lpts) in ( types.TupleType, types.ListType )):
        for point in lpts :
            coords.append(tuple(point))
    return tuple(coords)
            
def coords2lpts(coords):
    lpts = []
    if (type(coords) in ( types.TupleType, types.ListType )):
        for point in coords :
            lpts.append(list(point))
    else :
        raise ValueError("Invalid Coords %s "%coords)
    return lpts

def build_zinc_item(widget, pgroup = 1, **options):
    """
    #---------------------------------------------------------------------------
    # Graphics::build_zinc_item
    # Création d'un objet Zinc de représentation
    #---------------------------------------------------------------------------
    # types d'items valides :
    # les items natifs zinc : group, rectangle, arc, curve, text, icon
    # les items ci-après permettent de spécifier des curves 'particulières' :
    # -roundedrectangle : rectangle à coin arrondi
    #       -hippodrome : hippodrome
    #          -ellipse : ellipse un centre 2 rayons
    #         -polygone : polygone régulier à n cotés (convexe ou en étoile)
    #     -roundedcurve : curve multicontours à coins arrondis (rayon unique)
    #         -polyline : curve multicontours à coins arrondis (le rayon pouvant
    #                     être défini 
    #                     spécifiquement pour chaque sommet)
    #         -pathline : création d'une ligne 'épaisse' avec l'item Zinc 
    #                     triangles décalage par rapport à un chemin donné 
    #                     (largeur et sens de décalage)
    #                     dégradé de couleurs de la ligne (linéaire, transversal
    #                     ou double)
    #---------------------------------------------------------------------------
    # paramètres :
    # widget : <widget> identifiant du widget Zinc
    # parentgroup : <tagOrId> identifiant du group parent
    #
    # options :
    #   -itemtype : type de l'item à construire (type zinc ou metatype)
    #     -coords : <coords|coordsList> coordonnées de l'item
    # -metacoords : <hastable> calcul de coordonnées par type 
    #               d'item différent de itemtype
    #   -contours : <contourList> paramètres multi-contours
    #     -params : <hastable> arguments spécifiques de l'item à passer
    #               au widget
    #    -addtags : [list of specific tags] to add to params -tags
    #    -texture : <imagefile> ajout d'une texture à l'item
    #    -pattern : <imagefile> ajout d'un pattern à l'item
    #     -relief : <hastable> création d'un relief à l'item invoque la fonction
    #               graphicitem_relief()
    #     -shadow : <hastable> création d'une ombre portée à l'item invoque
    #               la fonction graphicitem_shadow()
    #      -scale : <scale_factor|[xscale_factor,yscale_factor]> application
    #               d'une transformation zinc->scale à l'item
    #  -translate : <[delta_x,delta_y]> application d'un transformation zinc->translate
    #               à l'item.
    #     -rotate : <angle> application d'une transformation zinc->rotate
    #               (en degré) à l'item
    #       -name : <str> nom de l'item
    #               spécifiques item group :
    #       -clip : <coordList|hashtable> paramètres de clipping d'un item group
    #               (coords ou item)
    #      -items : <hashtable> appel récursif de la fonction permettant
    #               d'inclure des items au groupe
    #---------------------------------------------------------------------------
    #
    #---------------------------------------------------------------------------
    """

    if options.has_key('parentgroup'):
        parentgroup = options['parentgroup']
    else :
        parentgroup = pgroup
    try:
        itemtype = options['itemtype']
    except KeyError:
        raise ValueError("Must have itemtype option")
    try:
        coords = options['coords']
    except KeyError:
        try:
            coords = options['metacoords']
        except KeyError :
            raise ValueError("Must have coords or metacoords option")
        
    if options.has_key('params'):
        params = options['params']
    else:
        params = {}

    #return unless($widget and $itemtype
    #and ($coords or $options{'-metacoords'}))

    try:
        name = options['name']
    except KeyError:
        name = None

    item = None
    metatype = None
    items = []
    reliefs = []
    shadows = []
    tags = []

    #--------------------
    # GEOMETRIE DES ITEMS

    # gestion des types d'items particuliers et à raccords circulaires
    if (itemtype in ( 'roundedrectangle',
                      'hippodrome',
                      'polygone',
                      'ellipse',
                      'roundedcurve',
                      'polyline',
                      'curveline')):

        # par défaut la curve sera fermée -closed = 1
        if not params.has_key('closed'):
            params['closed'] = 1
        metatype = itemtype
        itemtype = 'curve'

        # possibilité de définir les coordonnées initiales par metatype
        if (options.has_key('metacoords')) :
            options['coords'] = meta_coords( **options['metacoords'])

    # création d'une pathline à partir d'item zinc triangles
    elif (itemtype == 'pathline') :
        itemtype = 'triangles'
        if (options.has_key('metacoords')) :
            coords = meta_coords( **options['metacoords'])
          
        if (options.has_key('graduate')) :
            numcolors = len(coords)
            lcolors = path_graduate(widget,
                                    numcolors,
                                    options['graduate'])
            params['colors'] = tuple(lcolors)

        if options.has_key('coords'):
            coords = pathline_coords(**options)
        else:
            coords = pathline_coords(coords, **options)

        # création d'une boite à onglet
    elif (itemtype == 'tabbox') :
        return build_tabboxitem(widget, parentgroup, **options)

    # calcul des coordonnées finales de la curve
    if (metatype is not None):
        coords = meta_coords(type = metatype, **options)


    # gestion du multi-contours (accessible pour tous les types
    # d'items géometriques)
    if (options.has_key('contours') and (metatype is not None)) :
        lcontours = options['contours']
        contours=[]
        numcontours = len(contours)
        for contour in lcontours:
            # radius et corners peuvent être défini
            # spécifiquement pour chaque contour
            (atype, way, addcoords,) = contour[:3]
            if len(contour) >= 4:
                radius = contour[3]
            else :
                radius = None
            if len(contour) >= 5:
                corners = contour[4]
            else:
                corners = None
            if len(contour) >= 6:
                corners_radius = contour[5]
            else :
                corners_radius = None
            if (radius is None):
                if options.has_key('radius'):
                    radius = options['radius']
                else :
                    raise ValueError("radius option requiered")

            newcoords = meta_coords(type = metatype,
                                    coords = addcoords,
                                    radius = radius,
                                    corners = corners,
                                    corners_radius = corners_radius
                                    )
            contours.append((atype, way, newcoords))

        options['contours'] = contours

    #----------------------
    # REALISATION DES ITEMS

    # ITEM GROUP
    # gestion des coordonnées et du clipping
    if (itemtype == 'group') :
        item = widget.add(itemtype,
                          parentgroup,
                          **params)
        widget.addtag_withtag(name, item)
        if coords:
            widget.coords(item, tuple(coords))

        # clipping du groupe par item ou par géometrie
        if (options.has_key('clip')) :
            clipbuilder = options['clip']
            clip = None

            # création d'un item de clipping
            if (type(clipbuilder) is types.DictType
                and clipbuilder.has_key('itemtype')):
                clip = build_zinc_item(widget, item, **clipbuilder)

            elif (type(clipbuilder) in (types.TupleType, types.ListType)
                  or widget.type(clipbuilder)) :
                clip = clipbuilder

            if (clip):
                widget.itemconfigure(item, clip = clip)

        # créations si besoin des items contenus dans le groupe
        if (options.has_key('items')
            and type(options['items']) is types.DictType) :
            for (itemname, itemstyle) in options['items'].items() :
                if not itemstyle.has_key('name'):
                    itemstyle['name'] = itemname
                build_zinc_item(widget, item, **itemstyle)


    # ITEM TEXT ou ICON
    elif (itemtype in ('text', 'icon')) :
        imagefile = None
        if (itemtype == 'icon') :
            imagefile = params['image']
            image = get_image(widget, imagefile)
            if (image) :
                params['image'] = image
            else:
                params['image'] = ""
    

        item = widget.add(itemtype,
                          parentgroup,
                          position = coords,
                          **params
                          )
        if imagefile:
            params['image'] = imagefile 


    # ITEMS GEOMETRIQUES -> CURVE
    else :
        nparams=params
        item = widget.add(itemtype,
                          parentgroup,
                          lpts2coords(coords),
                          **params
                          )

        if (itemtype == 'curve' and options.has_key('contours')) :
            for contour in options['contours'] :
                contour = list(contour)
                contour[2] = tuple(contour[2])
                widget.contour(item, *contour)
                             
        # gestion du mode norender
        if (options.has_key('texture')) :
            texture = get_texture(widget, options['texture'])
            if texture:
                widget.itemconfigure(item, tile = texture)

        if (options.has_key('pattern')) :
            bitmap = get_pattern(**options['pattern'])
            if bitmap:
                widget.itemconfigure(item, fillpattern = bitmap)

    # gestion des tags spécifiques
    if (options.has_key('addtags')) :
        tags = options['addtags']

        params_tags = params['tags']
        if params_tags:
            tags.extend(params_tags) 

        widget.itemconfigure(item, tags = tags)

    #-------------------------------
    # TRANSFORMATIONS ZINC DE L'ITEM

    # transformation scale de l'item si nécessaire
    if (options.has_key('scale')) :
        scale = options['scale']
        if (type(scale) is not types.TupleType) :
            scale = (scale, scale) 
        widget.scale(item, scale)
  

    # transformation rotate de l'item si nécessaire
    if (options.has_key('rotate')):
        widget.rotate(item, radians(options['rotate'])) 

    # transformation translate de l'item si nécessaire
    if (options.has_key('translate')):
        widget.translate(item, options['translate'])


    # répétition de l'item
    if (options.has_key('repeat')) :
        items.extend((item,
                      repeat_zinc_item(widget, item, **options['repeat'])))

    #-----------------------
    # RELIEF ET OMBRE PORTEE

    # gestion du relief
    if (options.has_key('relief')) :
        if (len(items)) :
            target = items
        else:
            target = item
        reliefs.extend(graphicitem_relief(widget,
                                          target, **options['relief']))

    # gestion de l'ombre portée
    if (options.has_key('shadow')) :
        if (len(items)) :
            target = items
        else:
            target = item
        shadows.extend(graphicitem_shadow(widget,
                                         target, **options['shadow']))
  

    if len(reliefs):
        items.extend(reliefs)
    if len(shadows):
        items.extend(shadows)

    if len(items):
        return items
    else:
        return item

def repeat_zinc_item(widget,
                     item,
                     num = 2,
                     dxy = (0,0),
                     angle = None,
                     params = None,
                     copytag = None) :
    """
    #---------------------------------------------------------------------------
    # Graphics::repeat_zinc_item
    # Duplication (clonage) d'un objet Zinc de représentation
    #---------------------------------------------------------------------------
    # paramètres :
    # widget : <widget> identifiant du widget zinc
    #   item : <tagOrId> identifiant de l'item source
    # options :
    #     -num : <n> nombre d'item total (par defaut 2)
    #     -dxy : <[delta_x, delta_y]> translation entre 2 duplications (par defaut [0,0])
    #   -angle : <angle> rotation entre 2 duplications
    # -copytag : <sting> ajout d'un tag indexé pour chaque copie
    #  -params : <hashtable> {clef => [value list]}> valeur de paramètre
    #            de chaque copie
    #---------------------------------------------------------------------------
    """
    clones = []
    delta_x, delta_y = dxy
    # duplication d'une liste d'items -> appel récursif
    if (type(item) in (types.TupleType, types.ListType)) :
        for part in item :
            clones.append(repeat_zinc_item(widget,
                                           part,
                                           dxy,
                                           angle,
                                           params,
                                           copytag))

        return clones

    tags = []

    if (copytag) :
        tags = widget.itemcget(item, 'tags')
        widget.itemconfigure(item, tags = tags + ("%s0"%copytag,))

    for i in xrange(1, num) :
        clone = None

        if (copytag) :
            clone = widget.clone(item, tags = tags + ("%s%s"%(copytag, i),))
        else :
            clone = widget.clone(item)

        clones.append(clone)
        widget.translate(clone, delta_x*i, delta_y*i)
        if angle :
            widget.rotate(clone, radians(angle*i))

        if (params is not None ) :
            widget.itemconfigure(clone, **params )
    return clones


#MUST BE TESTED
def meta_coords( type,
                 coords,
                 **options ):
    """
    #---------------------------------------------------------------------------
    # FONCTIONS GEOMETRIQUES
    #---------------------------------------------------------------------------
    
    #---------------------------------------------------------------------------
    # Graphics::meta_coords
    # retourne une liste de coordonnées en utilisant la fonction du type
    # d'item spécifié
    #---------------------------------------------------------------------------
    # paramètres : (passés par %options)
    #   -type : <string> type de primitive utilisée
    # -coords : <coordsList> coordonnées nécessitée par la fonction [type]_coords
    #
    # les autres options spécialisées au type seront passés
    # à la fonction [type]coords
    #---------------------------------------------------------------------------
    """
    pts = None

    if (type == 'roundedrectangle'):
        log('Coords for roundedrectangle')
        pts = rounded_rectangle_coords(coords, **options)

    elif (type == 'hippodrome') :
        log('Coords for hippodrome')
        pts = hippodrome_coords(coords, **options)

    elif (type == 'ellipse') :
        log('Coords for ellipse')
        pts = ellipse_coords(coords, **options)

    elif (type == 'roundedcurve') :
        log('Coords for roundedcurve')
        pts = roundedcurve_coords(coords, **options)

    elif (type == 'polygone') :
        log('Coords for polygone')
        pts = polygon_coords(coords, **options)

    elif (type == 'polyline') :
        log('Coords for polyline')
        pts = polyline_coords(coords, **options)
    
    elif (type == 'curveline') :
        log('Coords for curveline')
        pts = curveline_coords(coords, **options)

    return pts


def zincitem_2_curvecoords( widget, item,
                            linear = 0,
                            realcoords = 0,
                            adjust = 1,
                            ):
    """
    #--------------------------------------------------------------------------
    # Graphics::zincitem_2_curvecoords
    # retourne une liste des coordonnées 'Curve' d'un l'item Zinc
    # rectangle, arc ou curve
    #--------------------------------------------------------------------------
    # paramètres :
    # widget : <widget> identifiant du widget zinc
    #   item : <tagOrId> identifiant de l'item source
    # options :
    #     -linear : <boolean> réduction à des segments non curviligne
    #               (par défaut 0)
    # -realcoords : <boolean> coordonnées à transformer dans le groupe père
    #               (par défaut 0)
    #     -adjust : <boolean> ajustement de la courbe de bezier (par défaut 1)
    #--------------------------------------------------------------------------
    """
    itemtype = widget.type(item)

    if not itemtype :
        raise ValueError("Not a Valid Item %s" % item)

    itemcoords = widget.coords(item)
    coords = None
    multi = []

    if (itemtype == 'rectangle') :
        coords = rounded_rectangle_coords(itemcoords, radius = 0)

    elif (itemtype == 'arc') :
        coords = ellipse_coords(itemcoords)
        if linear :
            coords = curve2polyline_coords(coords, adjust) 

    elif (itemtype == 'curve') :
        numcontours = widget.contour(item)

        if (numcontours < 2) :
            if linear:
                coords = curve2polyline_coords(itemcoords, adjust)
        else :
            if (linear) :
                multi = curveitem2polyline_coords(widget, item)

            else :
                for contour in xrange(0, numcontours):
                    points = widget.coords(item, contour)
                    multi.extend(points)
            coords = multi

    if (realcoords) :
        parentgroup = widget.group(item)
        if (len(multi)) :
            newcoords = []
            for points in multi :
                transcoords = widget.transform(item, parentgroup, points)
                newcoords.extend(transcoords)
            coords = newcoords
        else :
            transcoords =  widget.transform(item, parentgroup, coords)
            coords = transcoords

    return coords

def rounded_rectangle_coords( coords, **options):
    """
    #---------------------------------------------------------------------------
    # Graphics::rounded_rectangle_coords
    # calcul des coords du rectangle à coins arrondis
    #---------------------------------------------------------------------------
    # paramètres :
    # coords : <coordsList> coordonnées bbox (haut-gauche et bas-droite)
    #          du rectangle
    # options :
    #  -radius : <dimension> rayon de raccord d'angle
    # -corners : <booleanList> liste des raccords de sommets
    #            [0 (aucun raccord)|1] par défaut [1,1,1,1]
    #---------------------------------------------------------------------------
    """
    (x_0, y_0, x_n, y_n) = (coords[0][0], coords[0][1],
                        coords[1][0], coords[1][1])

    if (options.has_key('radius')):
        radius = options['radius']
    else:
        radius = None
  
    if (options.has_key('corners')):
        corners = options['corners']
    else:
        corners = [1, 1, 1, 1]

    # attention aux formes 'négatives'
    if (x_n < x_0) :
        (x_0, x_n) = (x_n, x_0)
      
    if (y_n < y_0) :
        (y_0, y_n) = (y_n, y_0)

    height = min(x_n -x_0, y_n - y_0)
    #radius non defini dans les parametres
    if (radius is None) :
        radius = int(height/10)
        radius = max(radius, 3)
      
    #radius defini mais trop petit
    if ( radius < 2) :
        return ((x_0, y_0), (x_0, y_n), (x_n, y_n), (x_n, y_0))

    # correction de radius si necessaire
    max_rad = height
    #CODE BIZARRE
    #Comment corners ne peut être non défini
    #a ce niveau ?
    #  max_rad /= 2 if (!defined corners)
    if (corners is None):
        max_rad /= 2
    radius = min(max_rad, radius)

    # points remarquables
    ptd_delta = radius * const_ptd_factor
    (x_2, x_3) = (x_0 + radius, x_n - radius)
    (x_1, x_4) = (x_2 - ptd_delta, x_3 + ptd_delta)
    (y_2, y_3) = (y_0 + radius, y_n - radius)
    (y_1, y_4) = (y_2 - ptd_delta, y_3 + ptd_delta)

    # liste des 4 points sommet du rectangle : angles sans raccord circulaire
    angle_pts = ((x_0, y_0), (x_0, y_n), (x_n, y_n), (x_n, y_0))

    # liste des 4 segments quadratique : raccord d'angle = radius
    roundeds = [[(x_2, y_0), (x_1, y_0, 'c'), (x_0, y_1, 'c'), (x_0, y_2),],
                [(x_0, y_3), (x_0, y_4, 'c'), (x_1, y_n, 'c'), (x_2, y_n),],
                [(x_3, y_n), (x_4, y_n, 'c'), (x_n, y_4, 'c'), (x_n, y_3),],
                [(x_n, y_2), (x_n, y_1, 'c'), (x_4, y_0, 'c'), (x_3, y_0),]]

    pts = []
    previous = None
    for i  in xrange(0, 4):
        #BUGS ??
        if (corners[i]):
            if (previous is not None) :
                # on teste si non duplication de point
                (nx, ny) = roundeds[i][0]
                if (previous[0] == nx and previous[1] == ny) :
                    pts.pop()
            pts.extend(roundeds[i])
            previous = roundeds[i][3]
        else :
            pts.append(angle_pts[i])

    return pts

def ellipse_coords(coords, **options):
    """
    #---------------------------------------------------------------------------
    # Graphics::ellipse_coords
    # calcul des coords d'une ellipse
    #---------------------------------------------------------------------------
    # paramètres :
    # coords : <coordsList> coordonnées bbox du rectangle exinscrit
    # options :
    # -corners : <booleanList> liste des raccords de sommets
    # [0 (aucun raccord)|1] par défaut [1,1,1,1]
    #---------------------------------------------------------------------------
    """
    (x_0, y_0, x_n, y_n) = (coords[0][0], coords[0][1],
                        coords[1][0], coords[1][1])

    if options.has_key('corners') :
        corners = options.has_key('corners')
    else:
        corners = [1, 1, 1, 1]

    # attention aux formes 'négatives'
    if (x_n < x_0) :
        xs = x_0
        (x_0, x_n) = (x_n, xs)
    if (y_n < y_0) :
        ys = y_0
        (y_0, y_n) = (y_n, ys)

    # points remarquables
    delta_x = (x_n - x_0)/2 * const_ptd_factor
    delta_y = (y_n - y_0)/2 * const_ptd_factor
    (x_2, y_2) = ((x_0+x_n)/2, (y_0+y_n)/2)
    (x_1, x_3) = (x_2 - delta_x, x_2 + delta_x)
    (y_1, y_3) = (y_2 - delta_y, y_2 + delta_y)

    # liste des 4 points sommet de l'ellipse : angles sans raccord circulaire
    angle_pts = ((x_0, y_0), (x_0, y_n), (x_n, y_n), (x_n, y_0))

    # liste des 4 segments quadratique : raccord d'angle = arc d'ellipse
    roundeds = (((x_2, y_0), (x_1, y_0, 'c'), (x_0, y_1, 'c'), (x_0, y_2), ),
                ((x_0, y_2), (x_0, y_3, 'c'), (x_1, y_n, 'c'), (x_2, y_n), ),
                ((x_2, y_n), (x_3, y_n, 'c'), (x_n, y_3, 'c'), (x_n, y_2), ),
                ((x_n, y_2), (x_n, y_1, 'c'), (x_3, y_0, 'c'), (x_2, y_0), ))

    pts = []
    previous = None
    for i in xrange(0, 4):
        if (corners[i]) :
            if (previous) :
                # on teste si non duplication de point
                (nx, ny) = roundeds[i][0]
                if (previous[0] == nx and previous[1] == ny) :
                    pts.pop()
            
            
            pts.extend(roundeds[i])
            previous = roundeds[i][3]

        else :
            pts.append(angle_pts[i])

    return pts


def hippodrome_coords(coords, **options) :
    """
    #---------------------------------------------------------------------------
    # Graphics::hippodrome_coords
    # calcul des coords d'un hippodrome
    #---------------------------------------------------------------------------
    # paramètres :
    # coords : <coordsList> coordonnées bbox du rectangle exinscrit
    # options :
    # -orientation : orientation forcée de l'hippodrome [horizontal|vertical]
    #     -corners : liste des raccords de sommets [0|1] par défaut [1,1,1,1]
    #       -trunc : troncatures [left|right|top|bottom|both]
    #---------------------------------------------------------------------------
    """
    (x_0, y_0, x_n, y_n) = (coords[0][0],
                            coords[0][1],
                            coords[1][0],
                            coords[1][1])

    if (options.has_key('orientation')) :
        orientation = options['orientation']
    else:
        orientation = 'none'

    # orientation forcée de l'hippodrome
    # (sinon hippodrome sur le plus petit coté)
    if (orientation == 'horizontal') :
        height = abs(y_n - y_0)
    elif (orientation == 'vertical') :
        height = abs(x_n - x_0)
    else:
        height = min(abs(x_n - x_0), abs(y_n - y_0))
    radius = height/2
    corners = (1, 1, 1, 1)

    if  (options.has_key('corners')) :
        corners = options['corners']

    elif (options.has_key('trunc')) :
        trunc = options['trunc']
        if (trunc == 'both') :
            return ((x_0, y_0), (x_0, y_n), (x_n, y_n), (x_n, y_0))
        else :
            if (trunc == 'left'):
                corners = (0, 0, 1, 1)
            elif (trunc == 'right'):
                corners = (1, 1, 0, 0)
            elif (trunc == 'top'):
                corners = (0, 1, 1, 0)
            elif (trunc == 'bottom') :
                corners = (1, 0, 0, 1)
            else :
                corners = (1, 1, 1, 1)

    # l'hippodrome est un cas particulier de roundedRectangle
    # on retourne en passant la 'configuration' à la fonction
    # générique rounded_rectangle_coords
    return rounded_rectangle_coords(coords,
                                  radius = radius,
                                  corners = corners)

def polygon_coords(coords, **options):
    """
    #---------------------------------------------------------------------------
    # Graphics::polygon_coords
    # calcul des coords d'un polygone régulier
    #---------------------------------------------------------------------------
    # paramètres :
    # coords : <coords> point centre du polygone
    # options :
    #      -numsides : <integer> nombre de cotés
    #        -radius : <dimension> rayon de définition du polygone
    #                  (distance centre-sommets)
    #  -inner_radius : <dimension> rayon interne (polygone type étoile)
    #       -corners : <booleanList> liste des raccords de sommets [0|1]
    #                  par défaut [1,1,1,1]
    # -corner_radius : <dimension> rayon de raccord des cotés
    #    -startangle : <angle> angle de départ en degré du polygone
    #---------------------------------------------------------------------------
    """
    if options.has_key('numsides'):
        numsides = options['numsides']
    else :
        numsides = 0
    if options.has_key('radius'):
        radius = options['radius']
    else:
        radius = None
    if (numsides < 3 or not radius) :
        raise ValueError("Vous devez au moins spécifier "
                         +"un nombre de cotés >= 3 et un rayon...\n")

    if (coords is None):
        coords = (0, 0)
    if (options.has_key('startangle')) :
        startangle = options['startangle']
    else:
        startangle = 0
    anglestep = 360/numsides
    if options.has_key('inner_radius'):
        inner_radius = options['inner_radius']
    else:
        inner_radius = None
    pts = []

    # points du polygone
    for i in xrange(0, numsides):
        (xp, yp) = rad_point(coords, radius, startangle + (anglestep*i))
        pts.append((xp, yp))

        # polygones 'étoiles'
        if (inner_radius) :
            (xp, yp) = rad_point(coords, inner_radius,
                                 startangle + (anglestep*(i+ 0.5)))
            pts.append((xp, yp))
    
    pts.reverse()

    if (options.has_key('corner_radius')) :
        if options.has_key('corners'):
            corners = options['corners']
        else:
            corners = None
        return roundedcurve_coords(pts,
                                   radius = options['corner_radius'],
                                   corners = corners)
    else :
        return pts


def rounded_angle(widget, parentgroup, coords, radius) :
    """
    #---------------------------------------------------------------------------
    # Graphics::rounded_angle
    # THIS FUNCTION IS NO MORE USED, NEITHER EXPORTED
    # curve d'angle avec raccord circulaire
    #---------------------------------------------------------------------------
    # paramètres :
    # widget : identifiant du widget Zinc
    # parentgroup : <tagOrId> identifiant de l'item group parent
    # coords : <coordsList> les 3 points de l'angle
    # radius : <dimension> rayon de raccord
    #---------------------------------------------------------------------------
    """
    (pt0, pt1, pt2) = coords

    (corner_pts, center_pts) = rounded_angle_coords(coords, radius)
    (cx_0, cy_0) = center_pts

    if (parentgroup is None) :
        parentgroup = 1 

    pts = [pt0]
    pts.extend(corner_pts)
    pts.append(pt2)
    
    widget.add('curve',
               parentgroup,
               lpts2coords(pts),
               closed = 0,
               linewidth = 1,
               priority = 20,
               )


def rounded_angle_coords (coords, radius) :
    """
    #---------------------------------------------------------------------------
    # Graphics::rounded_angle_coords
    # calcul des coords d'un raccord d'angle circulaire
    #---------------------------------------------------------------------------
    # le raccord circulaire de 2 droites sécantes est traditionnellement
    # réalisé par un
    # arc (conique) du cercle inscrit de rayon radius tangent à ces 2 droites
    #
    # Quadratique :
    # une approche de cette courbe peut être réalisée simplement par le calcul
    # de 4 points
    # spécifiques qui définiront - quelle que soit la valeur de l'angle formé
    # par les 2
    # droites - le segment de raccord :
    # - les 2 points de tangence au cercle inscrit seront les points de début
    #   et de fin
    # du segment de raccord
    # - les 2 points de controle seront situés chacun sur le vecteur
    #   reliant le point de
    # tangence au sommet de l'angle (point secant des 2 droites)
    # leur position sur ce vecteur peut être simplifiée comme suit :
    # - à un facteur de 0.5523 de la distance au sommet pour
    #   un angle >= 90° et <= 270°
    # - à une 'réduction' de ce point vers le point de tangence
    #   pour les angles limites
    # de 90° vers 0° et de 270° vers 360°
    # ce facteur sera légérement modulé pour recouvrir plus précisement
    # l'arc correspondant
    #---------------------------------------------------------------------------
    # coords : <coordsList> les 3 points de l'angle
    # radius : <dimension> rayon de raccord
    #---------------------------------------------------------------------------
    """
    (pt0, pt1, pt2) = coords

    # valeur d'angle et angle formé par la bisectrice
    (angle, bisecangle)  = vertex_angle(pt0, pt1, pt2)

    # distance au centre du cercle inscrit : rayon/sinus demi-angle
    asin = sin(radians(angle/2))
    
    if (asin) :
        delta = abs(radius / asin)
    else:
        delta = radius

    # point centre du cercle inscrit de rayon $radius
    if (angle < 180) :
        refangle = bisecangle + 90
    else :
        refangle = bisecangle - 90
        
    (cx_0, cy_0) = rad_point(pt1, delta, refangle)

    # points de tangeance : pts perpendiculaires du centre aux 2 droites
    (px_1, py_1) = perpendicular_point((cx_0, cy_0), (pt0, pt1))
    (px_2, py_2) = perpendicular_point((cx_0, cy_0), (pt1, pt2))

    # point de controle de la quadratique
    # facteur de positionnement sur le vecteur pt.tangence, sommet
    ptd_factor =  const_ptd_factor
    if (angle < 90 or angle > 270) :
        if (angle < 90) :
            diffangle = angle
        else:
            diffangle =  360 - angle
        if (diffangle > 15) :
            ptd_factor -= (((90 - diffangle)/90) * (ptd_factor/4)) 
        ptd_factor = (diffangle/90) * (ptd_factor
                                       + ((1 - ptd_factor)
                                          * (90 - diffangle)/90))
    else :
        diffangle = abs(180 - angle)
        if (diffangle > 15) :
            ptd_factor += (((90 - diffangle)/90) * (ptd_factor/3))

    # delta xy aux pts de tangence
    (d1x, d1y) = ((pt1[0] - px_1) * ptd_factor, (pt1[1] - py_1) *  ptd_factor)
    (d2x, d2y) = ((pt1[0] - px_2) * ptd_factor, (pt1[1] - py_2) *  ptd_factor)

    # les 4 points de l'arc 'quadratique'
    corner_pts = [(px_1, py_1), (px_1+d1x, py_1+d1y, 'c'),
                  (px_2+d2x, py_2+d2y, 'c'), (px_2, py_2)]

    
    # retourne le segment de quadratique et le centre du cercle inscrit
    return (corner_pts, (cx_0, cy_0))


def roundedcurve_coords(coords, **options):
    """
    #---------------------------------------------------------------------------
    # Graphics::roundedcurve_coords
    # retourne les coordonnées d'une curve à coins arrondis
    #---------------------------------------------------------------------------
    # paramètres :
    # coords : <coordsList> liste de coordonnées des points de la curve
    # options :
    #  -radius : <dimension> rayon de raccord d'angle
    #  -corners : <booleanList> liste des raccords de sommets [0|1]
    # par défaut [1,1,1,1]
    #---------------------------------------------------------------------------
    """
    numfaces = len(coords)
    curve_pts = []

    if (options.has_key('radius')) :
        radius = options['radius']
    else:
        radius = 0
    corners = None
    if options.has_key('corners') :
        corners = options['corners']

    for index in xrange(numfaces):
        if (corners is not None) :
            if (index+1 > len(corners)) or not corners[index] :
                curve_pts.append(coords[index])
                continue

        if (index) :
            prev = index - 1
        else :
            prev = numfaces - 1
        if (index > numfaces - 2) :
            next = 0
        else :
            next = index + 1
        anglecoords = (coords[prev], coords[index], coords[next])
            
        quad_pts = rounded_angle_coords(anglecoords, radius)[0]
        curve_pts.extend(quad_pts)
    return curve_pts


def polyline_coords(coords, **options):
    """
    #---------------------------------------------------------------------------
    # Graphics::polyline_coords
    # retourne les coordonnées d'une polyline
    #---------------------------------------------------------------------------
    # paramètres :
    # coords : <coordsList> liste de coordonnées des sommets de la polyline
    # options :
    #  -radius : <dimension> rayon global de raccord d'angle
    # -corners : <booleanList> liste des raccords de sommets [0|1]
    #            par défaut [1,1,1,1],
    # -corners_radius : <dimensionList> liste des rayons de raccords de sommets
    #---------------------------------------------------------------------------
    """
    numfaces = len(coords)
    curve_pts = []

    if (options.has_key('radius')) :
        radius = options['radius']
    else:
        radius = 0
    if options.has_key('corners_radius'):
        corners_radius = options['corners_radius']
        corners = corners_radius
    else:
        corners_radius = None
        if options.has_key('corners'):
            corners = options['corners']
        else:
            corners = None

    for index in xrange(0, numfaces):
        if (corners is not None
            and (len(corners) - 1 < index
                 or not corners[index])):
            curve_pts.append(coords[index])
        else :
            if (index) :
                prev = index - 1
            else:
                prev = numfaces - 1
            if (index > numfaces - 2) :
                next = 0
            else:
                next = index + 1
            anglecoords = (coords[prev], coords[index], coords[next])

            if (corners_radius) :
                rad = corners_radius[index]
            else:
                rad = radius
            quad_pts = rounded_angle_coords(anglecoords, rad)[0]
            curve_pts.extend(quad_pts)

    return curve_pts

def pathline_coords (coords, **options):
    """
    #---------------------------------------------------------------------------
    # Graphics::pathline_coords
    # retourne les coordonnées d'une pathline
    #---------------------------------------------------------------------------
    # paramètres :
    # coords : <coordsList> liste de coordonnées des points du path
    # options :
    #    -closed : <boolean> ligne fermée
    #  -shifting : <out|center|in> sens de décalage du path (par défaut center)
    # -linewidth : <dimension> epaisseur de la ligne
    #---------------------------------------------------------------------------
    """
    numfaces = len(coords)
    pts = []

    if options.has_key('closed'):
        closed = options['closed']
    else:
        closed = None
    if (options.has_key('linewidth')) :
        linewidth = options['linewidth']
    else:
        linewidth = 2
          
    if (options.has_key('shifting')) :
        shifting = options['shifting']
    else:
        shifting = 'center'

    if ( not numfaces or linewidth < 2):
        raise ValueError("Invalid PathLine_coords")

    if (closed) :
        previous = coords[numfaces - 1]
    else:
        previous = None
      
    next = coords[1]
    if (shifting == 'center'):
        linewidth /= 2

    for i in xrange(0, numfaces):
        pt = coords[i]

        if (previous is None) :
            # extrémité de curve sans raccord -> angle plat
            previous = (pt[0] + (pt[0] - next[0]), pt[1] + (pt[1] - next[1]))

        (angle, bisecangle) = vertex_angle(previous, pt, next)

        # distance au centre du cercle inscrit : rayon/sinus demi-angle
        asin = sin(radians(angle/2))
        if (asin) :
            delta = abs(linewidth / asin)
        else:
            delta = linewidth

        if (shifting == 'out' or shifting == 'in') :
            if (shifting == 'out') :
                adding = -90
            else:
                adding = 90
            pts.append(rad_point(pt, delta, bisecangle + adding))
            pts.append(pt)

        else :
            pts.append(rad_point(pt, delta, bisecangle-90))
            pts.append(rad_point(pt, delta, bisecangle+90))

        if (i == numfaces - 2) :
            if (closed) :
                next = coords[0]
            else:
                next = (coords[i+1][0] + (coords[i+1][0] - pt[0]),
                        coords[i+1][1] + (coords[i+1][1] - pt[1]))
        elif (i == numfaces - 1):
            next = None
        else :
            next = coords[i+2]
    
        previous = coords[i]

    if (closed) :
        pts.extend((pts[0], pts[1], pts[2], pts[3]))

    return pts


def curveline_coords(coords, **options) :
    """
    #---------------------------------------------------------------------------
    # Graphics::curveline_coords
    # retourne les coordonnées d'une curveLine
    #---------------------------------------------------------------------------
    # paramètres :
    # coords : <coordsList> liste de coordonnées des points de la ligne
    # options :
    #    -closed : <boolean> ligne fermée
    #  -shifting : <out|center|in> sens de décalage du contour
    #              (par défaut center)
    # -linewidth : <dimension> epaisseur de la ligne
    #---------------------------------------------------------------------------
    """
    numfaces = len(coords)
    gopts = []
    backpts = []
    pts = []

    if options.has_key('closed'):
        closed = options['closed']
    else:
        closed = None
    if (options.has_key('linewidth')) :
        linewidth = options['linewidth']
    else:
        linewidth = 2
    if (options.has_key('shifting')) :
        shifting = options['shifting']
    else:
        shifting = 'center'

    if( not numfaces or linewidth < 2):
        raise ValueError("Bad coords %s or linewidth %s"%(numfaces, linewidth))

    if (closed) :
        previous = coords[numfaces - 1]
    else:
        previous = None
        
    next = coords[1]
    if (shifting == 'center'):
        linewidth /= 2 

    for i in xrange(0, numfaces):
        pt = coords[i]

        if ( previous is None ) :
            # extrémité de curve sans raccord -> angle plat
            previous = (pt[0] + (pt[0] - next[0]), pt[1] + (pt[1] - next[1]))
            

        (angle, bisecangle) = vertex_angle(previous, pt, next)

        # distance au centre du cercle inscrit : rayon/sinus demi-angle
        asin = sin(radians(angle/2))
        if (asin) :
            delta = abs(linewidth / asin)
        else:
            delta = linewidth

        if (shifting == 'out' or shifting == 'in') :
            if (shifting == 'out') :
                adding = -90
            else:
                adding = 90
            pts.append(rad_point(pt, delta, bisecangle + adding))
            pts.append(pt)

        else :
            pts = rad_point(pt, delta, bisecangle+90)
            gopts.append(pts)
            pts = rad_point(pt, delta, bisecangle-90)
            backpts.insert(0, pts)

        if (i == numfaces - 2) :
            if (closed) :
                next = coords[0]
            else:
                next = (coords[i+1][0] +
                        (coords[i+1][0] - pt[0]), coords[i+1][1]
                        + (coords[i+1][1] - pt[1]))
        else :
            next = coords[i+2]

        previous = coords[i]

    gopts.extend(backpts)

    if (closed) :
        gopts.extend ((gopts[0], gopts[1]))

    return gopts


def shiftpath_coords(coords, **options) :
    """
    #---------------------------------------------------------------------------
    # Graphics::shiftpath_coords
    # retourne les coordonnées d'un décalage de path
    #---------------------------------------------------------------------------
    # paramètres :
    # coords : <coordsList> liste de coordonnées des points du path
    # options :
    #   -closed : <boolean> ligne fermée
    # -shifting : <'out'|'in'> sens de décalage du path (par défaut out)
    #    -width : <dimension> largeur de décalage (par défaut 1)
    #---------------------------------------------------------------------------
    """
    numfaces = len(coords)

    if options.has_key('closed'):
        closed = options['closed']
    else:
        closed = None
    if (options.has_key('width')) :
        width = options['width']
    else:
        width = 1
    if (options.has_key('shifting')) :
        shifting = options['shifting']
    else:
        shifting = 'out'

    if (not numfaces or not width):
        return coords 

    pts = []
    
    if (closed) :
        previous = coords[numfaces - 1]
    else:
        previous = None
    next = coords[1]

    for i in xrange(0, numfaces):
        pt = coords[i]

        if ( previous is None ) :
            # extrémité de curve sans raccord -> angle plat
            previous = (pt[0] + (pt[0] - next[0]), pt[1] + (pt[1] - next[1]))
            

        (angle, bisecangle) = vertex_angle(previous, pt, next)

        # distance au centre du cercle inscrit : rayon/sinus demi-angle
        asin = sin(radians(angle/2))
        if (asin) :
            delta = abs(width / asin)
        else:
            delta = width

        if (shifting == 'out') :
            adding = -90
        else:
            adding = 90
        (x, y) = rad_point(pt, delta, bisecangle + adding)
        pts.append((x, y))


        if (i > numfaces - 3) :
            if (closed) :
                next = coords[0]
            else:
                next = (pt[0] + (pt[0] - previous[0]),
                        pt[1] + (pt[1] - previous[1]))

        else :
            next = coords[i+2]

        previous = coords[i]

    return pts




def curveitem2polyline_coords(widget, item, **options):
    """
    #---------------------------------------------------------------------------
    # Graphics::curveitem2polyline_coords
    # Conversion des coordonnées Znitem curve (multicontours)
    # en coordonnées polyline(s)
    #---------------------------------------------------------------------------
    # paramètres :
    # widget : <widget> identifiant du widget zinc
    #   item : <tagOrId> identifiant de l'item source
    # options :
    # -tunits : <integer> nombre pas de division des segments bezier
    #           (par défaut 20)
    # -adjust : <boolean> ajustement de la courbe de bezier (par défaut 1)
    #---------------------------------------------------------------------------
    """
    if (not widget.type(item)):
        raise ValueError("Item Not Found")
    coords = []
    numcontours = widget.contour(item)
    #parentgroup = widget.group(item)

    for contour in xrange(0, numcontours):
        points = widget.coords(item, contour)
        contourcoords = curve2polyline_coords(points, **options)

        coords.append(contourcoords)

    return coords

def curve2polyline_coords(points, **options):
    """
    #---------------------------------------------------------------------------
    # Graphics::curve2polyline_coords
    # Conversion curve -> polygone
    #---------------------------------------------------------------------------
    # paramètres :
    # points : <coordsList> liste des coordonnées curve à transformer
    # options :
    # -tunits : <integer> nombre pas de division des segments bezier
    #           (par défaut 20)
    # -adjust : <boolean> ajustement de la courbe de bezier (par défaut 1)
    #---------------------------------------------------------------------------
    """
    if (options.has_key('tunits')) :
        tunits = options['tunits']
    else:
        tunits = 20
    if (options.has_key('adjust')) :
        adjust = options['adjust']
    else:
        adjust = 1

    poly = []
    previous = None
    bseg = []
    numseg = 0
    #prevtype = None

    for point in points:
        if len(point) == 3:
            (x, y, c) = point
        elif len(point) == 2:
            (x, y, c) = (point, None)
        else:
            ValueError("Bad point")
        if (c == 'c') :
            if not len(bseg) and previous:
                bseg.append(previous)
            bseg.append(point)
            
        else :
            if (len (bseg)) :
                bseg.append(point)
                if (adjust) :
                    pts = bezier_compute(bseg, skipend = 1)
                    del pts[0]
                    del pts[0]
                    poly.extend(pts)
                     
                else :
                    pts = bezier_segment(bseg, tunits = tunits, skipend = 1)
                    del pts[0]
                    del pts[0]
                    poly.extend(pts)

                bseg = []
                numseg += 1
                #prevtype = 'bseg'

            else :
                poly.append((x, y))
                #prevtype = 'line'

        previous = point


    return poly


def build_tabboxitem(widget, parentgroup, **options):
    """
    #-------------------------------------------------------------------------------
    # Graphics::build_tabboxitem
    # construit les items de représentations Zinc d'une boite à onglets
    #-------------------------------------------------------------------------------
    # paramètres :
    #      widget : <widget> identifiant du widget zinc
    # parentgroup : <tagOrId> identifiant de l'item group parent
    #
    #    options :
    #     -coords : <coordsList> coordonnées haut-gauche et bas-droite
    #               du rectangle
    #               englobant du Tabbox
    #     -params : <hastable> arguments spécifiques des items curve
    #               à passer au widget
    #    -texture : <imagefile> ajout d'une texture aux items curve
    #  -tabtitles : <hashtable> table de hash de définition des titres onglets
    #  -pageitems : <hashtable> table de hash de définition des pages internes
    #     -relief : <hashtable> table de hash de définition du relief de forme
    #
    # (options de construction géometrique passées à tabbox_coords)
    #  -numpages : <integer> nombre de pages (onglets) de la boite
    #    -anchor : <'n'|'e'|'s'|'w'> ancrage (positionnement) de la ligne
    #              d'onglets
    # -alignment : <'left'|'center'|'right'> alignement des onglets sur le coté
    #              d'ancrage
    #  -tabwidth : <'auto'>|<dimension>|<dimensionList> : largeur des onglets
    #              'auto' largeur répartie, les largeurs sont auto-ajustée
    #              si besoin.
    # -tabheight : <'auto'>|<dimension> : hauteur des onglets
    #  -tabshift : <'auto'>|<dimension> offset de 'biseau' entre base et haut de
    #              l'onglet (défaut auto)
    #    -radius : <dimension> rayon des arrondis d'angle
    #   -overlap : <'auto'>|<dimension> offset de recouvrement/séparation entre
    #              onglets
    #   -corners : <booleanList> liste 'spécifique' des raccords de sommets [0|1]
    #---------------------------------------------------------------------------
    """
    if options.has_key('coords') :
        coords = options['coords']
    else:
        raise ValueError("Coords needed")
    if options.has_key('params'):
        params = options['params']
    else:
        params = {}
    if params.has_key('tags'):
        tags = params['tags']
    else :
        tags = []
    texture = None

    if (options.has_key('texture')) :
        texture = get_texture(widget,
                             options['texture'])
        

    if options.has_key('tabtitles'):
        titlestyle = options['tabtitles']
    else :
        titlestyle = None
    if (titlestyle) :
        titles = titlestyle['text']
    else:
        titles = None

    tabs = []
    (shapes, tcoords, invert) = tabbox_coords(**options)
    if (invert) :
        k = len(shapes)
    else:
        k = -1
    shapes.reverse()
    for shape in shapes :
        if (invert) :
            k -= 1
        else :
            k += +1
        group = widget.add('group', parentgroup)
        params['tags'] = tags
        params['tags'] += (k, 'intercalaire')
        form = widget.add('curve',
                          group,
                          lpts2coords(shape),
                          **params)
        if texture :
            widget.itemconfigure(form, tile = texture)

        if (options.has_key('relief')) :
            graphicitem_relief(widget, form, **options['relief'])
            

        if (options.has_key('page')) :
            build_zinc_item(widget, group, **options['page'])
    
        if (titles) :
            if (invert) :
                tindex = k
            else:
                tindex = len(shapes) - k
            titlestyle['itemtype'] = 'text'
            titlestyle['coords'] = tcoords[tindex]
            titlestyle['params']['text'] = titles[tindex]
            ltags = list(tags)
            ltags.append(tindex)
            ltags.append('titre')
            titlestyle['params']['tags'] = tuple(ltags)
            build_zinc_item(widget, group, **titlestyle)

    return tabs


def tabbox_coords(coords, **options):
    """
    #---------------------------------------------------------------------------
    # tabbox_coords
    # Calcul des shapes de boites à onglets
    #---------------------------------------------------------------------------
    # paramètres :
    # coords : <coordList> coordonnées haut-gauche bas-droite du rectangle
    #          englobant de la tabbox
    # options
    #  -numpages : <integer> nombre de pages (onglets) de la boite
    #    -anchor : <'n'|'e'|'s'|'w'> ancrage (positionnement) de la
    #              ligne d'onglets
    # -alignment : <'left'|'center'|'right'> alignement des onglets
    #              sur le coté d'ancrage
    #  -tabwidth : <'auto'>|<dimension>|<dimensionList> : largeur des onglets
    #              'auto' largeur répartie, les largeurs sont auto-ajustée
    #               si besoin.
    # -tabheight : <'auto'>|<dimension> : hauteur des onglets
    #  -tabshift : <'auto'>|<dimension> offset de 'biseau' entre base et haut
    #              de l'onglet (défaut auto)
    #    -radius : <dimension> rayon des arrondis d'angle
    #   -overlap : <'auto'>|<dimension> offset de recouvrement/séparation
    #              entre onglets
    #   -corners : <booleanList> liste 'spécifique' des raccords
    #              de sommets [0|1]
    #---------------------------------------------------------------------------
    """
    (x_0, y_0) = coords[0]
    (x_n, y_n) = coords[1]
    shapes, titles_coords = [], []
    inverse = None

    #loptions = options.keys()
    if options.has_key('numpages'):
        numpages = options['numpages']
    else:
        numpages = 0
    
    if (not x_0 or not y_0 or not x_n or not y_n or not numpages) :
        raise ValueError("Vous devez au minimum spécifier\
        le rectangle englobant et le nombre de pages")

    if (options.has_key('anchor')) :
        anchor = options['anchor']
    else:
        anchor = 'n'
    if (options.has_key('alignment')) :
        alignment = options['alignment']
    else:
        alignment ='left'


    if (options.has_key('tabwidth')) :
        nlen = options['tabwidth']
    else:
        nlen ='auto'
    if (options.has_key('tabheight')) :
        thick = options['tabheight']
    else:
        thick ='auto'
    if (options.has_key('tabshift')) :
        biso = options['tabshift']
    else:
        biso = 'auto'
    if (options.has_key('radius')) :
        radius = options['radius']
    else:
        radius = 0
    if (options.has_key('overlap')) :
        overlap = options['overlap']
    else:
        overlap = 0
    if (options.has_key('corners')):
        corners = options['corners']
    else:
        corners = None
    if (anchor in ( 'n', 's')) :
        orientation = 'horizontal'
    else:
        orientation = 'vertical'
    if (orientation == 'horizontal') :
        maxwidth = (x_n - x_0)
    else:
        maxwidth = (y_n - y_0)
    tabswidth = 0
    align = 1

    if (nlen == 'auto') :
        tabswidth = maxwidth
        nlen = float(tabswidth + (overlap * (numpages - 1)))/numpages
    else :
        if (type(nlen) in (types.TupleType, types.ListType )) :
            for w in nlen :
                tabswidth += (w - overlap)
      
            tabswidth += overlap
        else :
            tabswidth = (nlen * numpages) - (overlap * (numpages - 1))
    

        if (tabswidth > maxwidth) :
            tabswidth = maxwidth
            nlen = float(tabswidth + (overlap * (numpages - 1)))/numpages

        if (alignment == 'center' and ((maxwidth - tabswidth) > radius)):
            align = 0 


    if (thick == 'auto') :
        if (orientation == 'horizontal') :
            thick = int((y_n - y_0)/10)
        else:
            thick = int((x_n - y_0)/10)
        thick = max(10, thick) 
        thick = min(40, thick)

    if (biso == 'auto') :
        biso = int(thick/2)

    if ((alignment == 'right' and anchor != 'w') or
        (anchor == 'w' and alignment != 'right')) :

        if (type(nlen) in (types.TupleType, types.ListType)) :
            for p in xrange(0, numpages):
                nlen[p] *= -1
        else :
            nlen *= -1
        biso *= -1
        overlap *= -1

    if (alignment == 'center') :
        (biso1, biso2) = (biso/2, biso/2)
    else:
        (biso1, biso2) = (0, biso)

    cadre, tabdxy = [], []
    xref, yref = 0, 0
    if (orientation == 'vertical') :
        if (anchor == 'w'):
            thick *= -1 
        if (anchor == 'w') :
            (startx, endx) = (x_0, x_n)
        else:
            (startx, endx) = (x_n, x_0)
        if ((anchor == 'w' and alignment != 'right') or 
            (anchor == 'e' and alignment == 'right')) :
            (starty, endy) = (y_n, y_0)
        else:
            (starty, endy) = (y_0, y_n)

        xref = startx - thick
        yref = starty
        if  (alignment == 'center') :
            if (anchor == 'w') :
                ratio = -2
            else:
                ratio = 2
            yref += (float(maxwidth - tabswidth)/ratio)

        cadre = ((xref, endy), (endx, endy), (endx, starty), (xref, starty))

        # flag de retournement de la liste des pts de curve si nécessaire
        # -> sens anti-horaire
        inverse = (alignment == 'right')

    else :
        if (anchor == 's'):
            thick *= -1
            (starty, endy) = (y_n, y_0)
        else :
            (starty, endy) = (y_0, y_n)
            
        if (alignment == 'right') :
            (startx, endx) = (x_n, x_0)
        else:
            (startx, endx) = (x_0, x_n)


        yref = starty + thick
        if (alignment == 'center') :
            xref = x_0 + (float(maxwidth - tabswidth)/2)
        else :
            xref = startx

        cadre = ((endx, yref), (endx, endy), (startx, endy), (startx, yref))

        # flag de retournement de la liste des pts de curve si nécessaire
        # -> sens anti-horaire
        inverse = ((anchor == 'n' and alignment != 'right')
                   or (anchor == 's' and alignment == 'right'))
         
        
    for i in xrange(0, numpages):
        pts = []

        # décrochage onglet
        #push (pts, ([xref, yref])) if i > 0

        # cadre
        pts.extend(cadre)

        # points onglets
        if (i > 0 or not align) :
            pts.append((xref, yref)) 

        if (type(nlen) in (types.TupleType, types.ListType)) :
            tw = nlen[i]
        else:
            tw = nlen

        if (type(nlen) in (types.TupleType, types.ListType)) :
            slen = len(nlen)
        else:
            slen = nlen
        
        if (orientation == 'vertical') :
            tabdxy = ((thick, biso1), (thick, tw - biso2), (0, tw))
        else:
            tabdxy = ((biso1, -thick), (tw - biso2, -thick), (tw, 0))
        for delta_xy in tabdxy :
            pts.append((xref + delta_xy[0], yref + delta_xy[1]))
    

        if (radius) :
            if (not options.has_key('corners')) :
                if (i > 0 or not align) :
                    corners =  (0, 1, 1, 0, 0, 1, 1, 0)
                else :
                    corners = (0, 1, 1, 0, 1, 1, 0, 0, 0)
      
            curvepts = roundedcurve_coords(pts,
                                          radius = radius,
                                          corners = corners)
            lcurvepts = list(curvepts)
            if (inverse):
                lcurvepts.reverse()
            shapes.append(lcurvepts)
        else :
            if (inverse):
                pts.reverse()
            shapes.append(pts)

        if (orientation == 'horizontal') :
            titles_coords.append((float(xref) + (tw - (biso2 - biso1))/2,
                                  float(yref) - (thick/2)))
            xref += (tw - overlap)

        else :
            titles_coords.append( (float(xref) + (thick/2),
                                   yref + (slen - ((biso2 - biso1)/2))/2))
            yref += (slen - overlap)
            
    return (shapes, titles_coords, inverse)


def graphicitem_relief(widget, item, **options):
    """
    #---------------------------------------------------------------------------
    # Graphics::graphicitem_relief
    # construit un relief à l'item Zinc en utilisant des items Triangles
    #---------------------------------------------------------------------------
    # paramètres :
    #  widget : <widget> identifiant du widget zinc
    #    item : <tagOrId> identifiant de l'item zinc
    # options : <hash> table d'options
    #     -closed : <boolean> le relief assure la fermeture de forme (défaut 1)
    #     -profil : <'rounded'|'flat'> type de profil (defaut 'rounded')
    #     -relief : <'raised'|'sunken'> (défaut 'raised')
    #       -side : <'inside'|'outside'> relief interne ou externe à la forme
    #               (défaut 'inside')
    #      -color : <color> couleur du relief (défaut couleur de la forme)
    #   -smoothed : <boolean> facettes relief lissées ou non (défaut 1)
    # -lightangle : <angle> angle d'éclairage (défaut valeur générale widget)
    #      -width : <dimension> 'épaisseur' du relief en pixel
    #       -fine : <boolean> mode précision courbe de bezier
    #               (défaut 0 : auto-ajustée)
    #-------------------------------------------------------------------------------
    """
    items = []

    # relief d'une liste d'items -> appel récursif
    if (type(item) in (types.TupleType, types.ListType)) :
        for part in item :
            items.extend(graphicitem_relief(widget, part, **options))
    else :
        itemtype = widget.type(item)
        if not itemtype:
            raise ValueError("Bad Item")

        parentgroup = widget.group(item)
        if (options.has_key('priority')) :
            priority = options['priority']
        else :
            priority = widget.itemcget(item, 'priority')+1

        # coords transformés (polyline) de l'item
        adjust = not options['fine']
        for coords in zincitem_2_curvecoords(widget,
                                           item, linear = 1,
                                           realcoords = 1,
                                           adjust = adjust) :
            (pts, colors) = polyline_relief_params(widget,
                                                 item,
                                                 coords,
                                                 **options)

            items.append(widget.add('triangles',
                                    parentgroup,
                                    pts,
                                    priority = priority,
                                    colors = colors))


        # renforcement du contour
        if (widget.itemcget(item, 'linewidth')) :
            items.append(widget.clone(item,
                                      filled = 0,
                                      priority = priority+1))

    return items


def polyline_relief_params(widget, item, coords, **options):
    """
    #---------------------------------------------------------------------------
    # Graphics::polyline_relief_params
    # retourne la liste des points et des couleurs nécessaires à la construction
    # de l'item Triangles du relief
    #---------------------------------------------------------------------------
    # paramètres :
    #  widget : <widget> identifiant widget Zinc
    #    item : <tagOrId> identifiant item Zinc
    # options : <hash> table d'options
    #     -closed : <boolean> le relief assure la fermeture de forme (défaut 1)
    #     -profil : <'rounded'|'flat'> type de profil (defaut 'rounded')
    #     -relief : <'raised'|'sunken'> (défaut 'raised')
    #       -side : <'inside'|'outside'> relief interne ou externe à la forme
    #               (défaut 'inside')
    #      -color : <color> couleur du relief (défaut couleur de la forme)
    #   -smoothed : <boolean> facettes relief lissées ou non (défaut 1)
    # -lightangle : <angle> angle d'éclairage (défaut valeur générale widget)
    #      -width : <dimension> 'épaisseur' du relief en pixel
    #---------------------------------------------------------------------------
    """

    if (options.has_key('closed')) :
        closed = options['closed']
    else:
        closed = 1
    if (options.has_key('profil')) :
        profil = options['profil']
    else:
        profil = 'rounded'
    if (options.has_key('relief')) :
        relief = options['relief']
    else :
        relief = 'raised'
    if (options.has_key('side')) :
        side = options['side']
    else:
        side = 'inside'
    if (options.has_key('color')) :
        basiccolor = options['color']
    else:
        basiccolor = zincitem_predominantcolor(widget, item)
    if (options.has_key('smooth')) :
        smoothed = options['smooth']
    else:
        smoothed = 1
    if (options.has_key('lightangle')) :
        lightangle = options['lightangle']
    else:
        lightangle = widget.cget('lightangle')

    if options.has_key('width'):
        width = options['width']
    else:
        raise ValueError('Options must have width field')
    if ( width < 1) :
        (x_0, y_0, x_1, y_1) = widget.bbox(item)
        width = min(x_1 -x_0, y_1 - y_0)/10
        if (width < 2) :
            width = 2

    numfaces = len(coords)
    if (closed):
        previous = coords[numfaces - 1]
    else:
        previous = None
    next = coords[1]

    pts = []
    colors = []
    alpha = 100
    m = re.compile("^(?P<color>#[0-9a-fA-F]{6});(?P<alpha>\d{1,2})$")
    res = m.match(basiccolor)
    if (res is not None) :
        (basiccolor, alpha) = res.group('color'), res.group('alpha')

    if ( options.has_key('color')):
        color = options['color']
        res = m.match(color)
        if ((res is None) and (profil == 'flat')):
            alpha /= 2

    if (profil == 'rounded') :
        reliefalphas = [0, alpha]
    else:
        reliefalphas = [alpha, alpha]

    for i in xrange(0, numfaces):
        pt = coords[i]

        if (previous) :
            # extrémité de curve sans raccord -> angle plat
            previous = (pt[0] + (pt[0] - next[0]), pt[1] + (pt[1] - next[1]))
    

        (angle, bisecangle) = vertex_angle(previous, pt, next)

        # distance au centre du cercle inscrit : rayon/sinus demi-angle
        asin = sin(radians(angle/2))
        if (asin) :
            delta = abs(width / asin)
        else:
            delta = width
        if (side == 'outside') :
            decal = -90
        else:
            decal = 90

        shift_pt = rad_point(pt, delta, bisecangle+decal)
        pts.append(shift_pt)
        pts.append(pt)

        if (smoothed and i) :
            pts.append(shift_pt)
            pts.append(pt)
    

        faceangle = 360 -(linenormal(previous, next)+90)

        light = abs(lightangle - faceangle)
        if (light > 180):
            light = 360 - light
        if light < 1:
            light = 1 

        if (relief == 'sunken') :
            lumratio = (180-light)/180
        else:
            lumratio = light/180

        if ( not smoothed and i) :
            #A VOIR
            #OBSCURE
            colors.extend((colors[-2], colors[-1]))

        if (basiccolor) :
            # création des couleurs dérivées
            shade = lightingcolor(basiccolor, lumratio)
            color0 = "%s;%s"% (shade, reliefalphas[0])
            color1 = "%s;%s"% (shade, reliefalphas[1])
            colors.extend((color0, color1))

        else :
            c = (255*lumratio)
            color0 = hexargbcolor(c, c, c, reliefalphas[0])
            color1 = hexargbcolor(c, c, c, reliefalphas[1])
            colors.extend((color0, color1))
    

        if (i == (numfaces - 2)) :
            if (closed) :
                next = coords[0]
            else:
                next = (coords[i+1][0] + (coords[i+1][0] - pt[0]),
                        coords[i+1][1] + (coords[i+1][1] - pt[1]))
        else :
            next = coords[i+2]

        previous = coords[i]

    if (closed) :
        pts.extend((pts[0], pts[1], pts[2], pts[3]))
        colors.extend((colors[0], colors[1]))

        if (not smoothed) :
            pts.extend((pts[0], pts[1], pts[2], pts[3]))
            colors.extend((colors[0], colors[1]))
    
    return (pts, colors)


def graphicitem_shadow(widget, item, **options):
    """
    #---------------------------------------------------------------------------
    # Graphics::graphicitem_shadow
    # Création d'une ombre portée à l'item
    #---------------------------------------------------------------------------
    # paramètres :
    #  widget : <widget> identifiant widget Zinc
    #    item : <tagOrId> identifiant item Zinc
    # options : <hash> table d'options
    #    -opacity : <percent> opacité de l'ombre (défaut 50)
    #     -filled : <boolean> remplissage totale de l'ombre (hors bordure) (defaut 1)
    # -lightangle : <angle> angle d'éclairage (défaut valeur générale widget)
    #   -distance : <dimension> distance de projection de l'ombre en pixel
    #  -enlarging : <dimension> grossi de l'ombre portée en pixels (defaut 0)
    #      -width : <dimension> taille de diffusion/diffraction (défaut 4)
    #      -color : <color> couleur de l'ombre portée (défaut black)
    #---------------------------------------------------------------------------
    """
    items = []

    # relief d'une liste d'items -> appel récursif
    if (type(item) in (types.TupleType, types.ListType)) :
        for part in item :
            items.append(graphicitem_shadow(widget, part, **options))
        return items

    else :

        itemtype = widget.type(item)

        if not itemtype :
            raise ValueError("Not a valid Item Id %s"%item)

        # création d'un groupe à l'ombre portée
        if (options.has_key('parentgroup')) :
            parentgroup = options['parentgroup']
        else:
            parentgroup = widget.group(item)
        if (options.has_key('priority')) :
            priority = options['priority']
        else:
            priority = widget.itemcget(item, 'priority')-1
        priority = max(0, priority) 

        shadow = widget.add('group', parentgroup, priority = priority)
            
        if (itemtype == 'text') :
            if (options.has_key('opacity')) :
                opacity = options['opacity']
            else:
                opacity = 50
            if (options['color']) :
                color = options['color']
            else:
                color = '#000000'
            
            clone = widget.clone(item, color = "%s;%s"% (color, opacity))
            widget.chggroup(clone, shadow)

        else :

            # création des items (de dessin) de l'ombre
            if ( options.has_key('filled')) :
                filled = options['filled']
            else:
                filled = 1
      
            # coords transformés (polyline) de l'item
            for coords in zincitem_2_curvecoords(widget,
                                               item,
                                               linear = 1,
                                               realcoords = 1) :
                (t_pts, i_pts, colors) = polyline_shadow_params( coords,
                                                               **options)
                
                # option filled : remplissage hors bordure
                # de l'ombre portée (item curve)
                if (filled) :
                    if (len(items)) :
                        widget.contour(items[0], 'add', 0, i_pts)
	
                    else :
                        items.append( widget.add('curve', shadow, i_pts,
                                                  linewidth = 0,
                                                  filled = 1,
                                                  fillcolor = colors[0],
                                                  ))
                        
                # bordure de diffusion de l'ombre (item triangles)
                items.append( widget.add('triangles', shadow, t_pts,
                                          colors = colors))
      

        # positionnement de l'ombre portée
        if (options.has_key('distance')) :
            distance = options['distance']
        else:
            distance = 10
        if (options.has_key('lightangle')) :
            lightangle = options['lightangle']
        else:
            lightangle = widget.cget('lightangle')

        (delta_x, delta_y) = rad_point((0, 0),
                             distance,
                             lightangle+180)
        widget.translate(shadow, delta_x, -delta_y)

    return shadow


def polyline_shadow_params(coords, **options):
    """
    #---------------------------------------------------------------------------
    # Graphics::polyline_shadow_params
    # retourne les listes des points et de couleurs nécessaires à la 
    # construction des items triangles (bordure externe) et curve
    # (remplissage interne) de l'ombre portée
    #---------------------------------------------------------------------------
    # paramètres :
    #    coords : coordonnées
    # options : <hash> table d'options
    #    -opacity : <percent> opacité de l'ombre (défaut 50)
    # -lightangle : <angle> angle d'éclairage (défaut valeur générale widget)
    #   -distance : <dimension> distance de projection de l'ombre en pixel
    #               (défaut 10)
    #  -enlarging : <dimension> grossi de l'ombre portée en pixels (defaut 2)
    #      -width : <dimension> taille de diffusion/diffraction
    #               (défaut distance -2)
    #      -color : <color> couleur de l'ombre portée (défaut black)
    #---------------------------------------------------------------------------
    """
    if (options.has_key('distance')) :
        distance = options['distance']
    else:
        distance = 10
    if (options.has_key('width')) :
        width = options['width']
    else:
        width = distance-2
    if (options.has_key('opacity')) :
        opacity = options['opacity']
    else:
        opacity = 50
    if (options.has_key('color')) :
        color = options['color']
    else:
        color ='#000000'
    if (options.has_key('enlarging')) :
        enlarging = options['enlarging']
    else :
        enlarging = 2

    if (enlarging) :
        coords = shiftpath_coords(coords,
                                 width = enlarging,
                                 closed = 1,
                                 shifting = 'out')

    numfaces = len(coords)
    previous = coords[numfaces - 1]
    next = coords[1]

    t_pts = []
    i_pts = []
    colors = []
    (color0, color1) = ("%s;%s"% (color, opacity), "%s;0"% color)

    for i in xrange(0, numfaces):
        pt = coords[i]

        #A VOIR
        #Je ne vois pas quand cela peut arriver
        if (not previous) :
            # extrémité de curve sans raccord -> angle plat
            previous = (pt[0] + (pt[0] - next[0]), pt[1] + (pt[1] - next[1]))

        (angle, bisecangle) = vertex_angle(previous, pt, next)

        # distance au centre du cercle inscrit : rayon/sinus demi-angle
        asin = sin(radians(angle/2))
        if (asin) :
            delta = abs(width / asin)
        else :
            delta =  width
        decal = 90

        shift_pt = rad_point(pt, delta, bisecangle+decal)
        i_pts.append(shift_pt)
        t_pts.append(shift_pt)
        t_pts.append(pt)
        
        colors.append(color0)
        colors.append(color1)
        if (i == numfaces - 2) :
            next = coords[0]
        else :
            next = coords[i+2]

        previous = coords[i]

    # fermeture
    t_pts.extend((t_pts[0], t_pts[1], t_pts[2], t_pts[3]))
    i_pts.extend((t_pts[0], t_pts[1]))
    colors.extend((color0, color1, color0, color1))

    return (t_pts, i_pts, colors)





#Local Variables:
#mode : python
#tab-width: 4
#end: