summaryrefslogtreecommitdiff
path: root/cd/src/intcgm/intcgm2.c
blob: b6be7ee3d7405e623a5b6010d15ab2b55abe207b (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
#include <stdio.h>      /* FILE, ftell, fseek, fputc, fopen, fclose, fputs, fprintf */
#include <string.h>     /* strlen */
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include "cd.h"
#include "cdcgm.h"
#include "list.h"
#include "types.h"
#include "bparse.h"
#include "tparse.h"
#include "intcgm.h"
#include "intcgm6.h"
#include "tparse.h"

typedef struct {
                 const char *nome;
				 int (*func) (void);
               } comando;

/**************************************************
***************************************************
**                                               **
**                  FUNCOES CGM                  **
**                                               **
***************************************************
**************************************************/

/************************************************
*                                               *
*            Dados para nao-binario             *
*                                               *
************************************************/

/* delimiter elements */

comando _cgmt_NULL             = { "", NULL };
comando _cgmt_BEGMF            = { "begmf", &cgmt_begmtf };
comando _cgmt_ENDMF            = { "endmf", &cgmt_endmtf };
comando _cgmt_BEG_PIC          = { "begpic", &cgmt_begpic };
comando _cgmt_BEG_PIC_BODY     = { "begpicbody", &cgmt_begpib };
comando _cgmt_END_PIC          = { "endpic", &cgmt_endpic };

/* metafile descriptor elements */

comando _cgmt_MF_VERSION       = { "mfversion", cgmt_mtfver };
comando _cgmt_MF_DESC          = { "mfdesc", cgmt_mtfdsc };
comando _cgmt_VDC_TYPE         = { "vdctype", cgmt_vdctyp };
comando _cgmt_INTEGER_PREC     = { "integerprec", cgmt_intpre };
comando _cgmt_REAL_PREC        = { "realprec", cgmt_realpr };
comando _cgmt_INDEX_PREC       = { "indexprec", cgmt_indpre };
comando _cgmt_COLR_PREC        = { "colrprec", cgmt_colpre };
comando _cgmt_COLR_INDEX_PREC  = { "colrindexprec", cgmt_colipr };
comando _cgmt_MAX_COLR_INDEX   = { "maxcolrindex", cgmt_maxcoi };
comando _cgmt_COLR_VALUE_EXT   = { "colrvalueext", cgmt_covaex };
comando _cgmt_MF_ELEM_LIST     = { "mfelemlist", cgmt_mtfell };
comando _cgmt_BEG_MF_DEFAULTS  = { "begmfdefaults", cgmt_bmtfdf };
comando _cgmt_END_MF_DEFAULTS  = { "endmfdefaults", cgmt_emtfdf };
comando _cgmt_FONT_LIST        = { "fontlist", cgmt_fntlst };
comando _cgmt_CHAR_SET_LIST    = { "charsetlist", cgmt_chslst };
comando _cgmt_CHAR_CODING      = { "charcoding", cgmt_chcdac };

/* picture descriptor elements */

comando _cgmt_SCALE_MODE       = { "scalemode", cgmt_sclmde };
comando _cgmt_COLR_MODE        = { "colrmode", cgmt_clslmd };
comando _cgmt_LINE_WIDTH_MODE  = { "linewidthmode", cgmt_lnwdmd };
comando _cgmt_MARKER_SIZE_MODE = { "markersizemode", cgmt_mkszmd };
comando _cgmt_EDGE_WIDTH_MODE  = { "edgewidthmode", cgmt_edwdmd };
comando _cgmt_VDC_EXTENT       = { "vdcext", cgmt_vdcext };
comando _cgmt_BACK_COLR        = { "backcolr", cgmt_bckcol };

/* control elements */

comando _cgmt_VDC_INTEGER_PREC = { "vdcintegerprec", cgmt_vdcipr };
comando _cgmt_VDC_REAL_PREC    = { "vdcrealprec", cgmt_vdcrpr };
comando _cgmt_AUX_COLR         = { "auxcolr", cgmt_auxcol };
comando _cgmt_TRANSPARENCY     = { "transparency", cgmt_transp };
comando _cgmt_CLIP_RECT        = { "cliprect", cgmt_clprec };
comando _cgmt_CLIP             = { "clip", cgmt_clpind };

/* primitive elements */

comando _cgmt_LINE             = { "line", cgmt_polyln };
comando _cgmt_INCR_LINE        = { "incrline", cgmt_incply };
comando _cgmt_DISJT_LINE       = { "disjtline", cgmt_djtply };
comando _cgmt_INCR_DISJT_LINE  = { "incrdisjt_line", cgmt_indjpl };
comando _cgmt_MARKER           = { "marker", cgmt_polymk };
comando _cgmt_INCR_MARKER      = { "incrmarker", cgmt_incplm };
comando _cgmt_TEXT             = { "text", cgmt_text };
comando _cgmt_RESTR_TEXT       = { "restrtext", cgmt_rsttxt };
comando _cgmt_APND_TEXT        = { "apndtext", cgmt_apdtxt };
comando _cgmt_POLYGON          = { "polygon", cgmt_polygn };
comando _cgmt_INCR_POLYGON     = { "incrpolygon", cgmt_incplg };
comando _cgmt_POLYGON_SET      = { "polygonset", cgmt_plgset };
comando _cgmt_INCR_POLYGON_SET = { "incrpolygonset", cgmt_inpgst };
comando _cgmt_CELL_ARRAY       = { "cellarray", cgmt_cellar };
comando _cgmt_GDP              = { "gdp", cgmt_gdp };
comando _cgmt_RECT             = { "rect", cgmt_rect };
comando _cgmt_CIRCLE           = { "circle", cgmt_circle };
comando _cgmt_ARC_3_PT         = { "arc3pt", cgmt_circ3p };
comando _cgmt_ARC_3_PT_CLOSE   = { "arc3ptclose", cgmt_cir3pc };
comando _cgmt_ARC_CTR          = { "arcctr", cgmt_circnt };
comando _cgmt_ARC_CTR_CLOSE    = { "arcctr_close", cgmt_ccntcl };
comando _cgmt_ELLIPSE          = { "ellipse", cgmt_ellips };
comando _cgmt_ELLIP_ARC        = { "elliparc", cgmt_ellarc };
comando _cgmt_ELLIP_ARC_CLOSE  = { "elliparcclose", cgmt_ellacl };

/* attribute elements */

comando _cgmt_LINE_INDEX       = { "lineindex", cgmt_lnbdin };
comando _cgmt_LINE_TYPE        = { "linetype", cgmt_lntype };
comando _cgmt_LINE_WIDTH       = { "linewidth", cgmt_lnwidt };
comando _cgmt_LINE_COLR        = { "linecolr", cgmt_lncolr };
comando _cgmt_MARKER_INDEX     = { "markerindex", cgmt_mkbdin };
comando _cgmt_MARKER_TYPE      = { "markertype", cgmt_mktype };
comando _cgmt_MARKER_WIDTH     = { "markersize", cgmt_mksize };
comando _cgmt_MARKER_COLR      = { "markercolr", cgmt_mkcolr };
comando _cgmt_TEXT_INDEX       = { "textindex", cgmt_txbdin };
comando _cgmt_TEXT_FONT_INDEX  = { "textfontindex", cgmt_txftin };
comando _cgmt_TEXT_PREC        = { "textprec", cgmt_txtprc };
comando _cgmt_CHAR_EXPAN       = { "charexpan", cgmt_chrexp };
comando _cgmt_CHAR_SPACE       = { "charspace", cgmt_chrspc };
comando _cgmt_TEXT_COLR        = { "textcolr", cgmt_txtclr };
comando _cgmt_CHAR_HEIGHT      = { "charheight", cgmt_chrhgt };
comando _cgmt_CHAR_ORI         = { "charori", cgmt_chrori };
comando _cgmt_TEXT_PATH        = { "textpath", cgmt_txtpat };
comando _cgmt_TEXT_ALIGN       = { "textalign", cgmt_txtali };
comando _cgmt_CHAR_SET_INDEX   = { "charsetindex", cgmt_chseti };
comando _cgmt_ALT_CHAR_SET     = { "altcharsetindex", cgmt_achsti };
comando _cgmt_FILL_INDEX       = { "fillindex", cgmt_fillin };
comando _cgmt_INT_STYLE        = { "intstyle", cgmt_intsty };
comando _cgmt_FILL_COLR        = { "fillcolr", cgmt_fillco };
comando _cgmt_HATCH_INDEX      = { "hatchindex", cgmt_hatind };
comando _cgmt_PAT_INDEX        = { "patindex", cgmt_patind };
comando _cgmt_EDGE_INDEX       = { "edgeindex", cgmt_edgind };
comando _cgmt_EDGE_TYPE        = { "edgetype", cgmt_edgtyp };
comando _cgmt_EDGE_WIDTH       = { "edgewidth", cgmt_edgwid };
comando _cgmt_EDGE_COLR        = { "edgecolr", cgmt_edgcol };
comando _cgmt_EDGE_VIS         = { "edgevis", cgmt_edgvis };
comando _cgmt_FILL_REF_PT      = { "fillrefpt", cgmt_fillrf };
comando _cgmt_PAT_TABLE        = { "pattable", cgmt_pattab };
comando _cgmt_PAT_SIZE         = { "patsize", cgmt_patsiz };
comando _cgmt_COLR_TABLE       = { "colrtable", cgmt_coltab };
comando _cgmt_ASF              = { "asf", cgmt_asf };

/* escape elements */

comando _cgmt_ESCAPE           = { "escape", cgmt_escape };
comando _cgmt_DOMAIN_RING      = { "domainring", NULL };

/* external elements */

comando _cgmt_MESSAGE          = { "message", cgmt_messag };
comando _cgmt_APPL_DATA        = { "appldata", cgmt_appdta };

comando *_cgmt_delimiter[] = {
      &_cgmt_NULL,
      &_cgmt_BEGMF,
      &_cgmt_ENDMF,
      &_cgmt_BEG_PIC,
      &_cgmt_BEG_PIC_BODY,
      &_cgmt_END_PIC,
      NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
      NULL, NULL, NULL, NULL, NULL, NULL, NULL };

comando *_cgmt_metafile[] = {
      &_cgmt_END_MF_DEFAULTS,
      &_cgmt_MF_VERSION,
      &_cgmt_MF_DESC,
      &_cgmt_VDC_TYPE,
      &_cgmt_INTEGER_PREC,
      &_cgmt_REAL_PREC,
      &_cgmt_INDEX_PREC,
      &_cgmt_COLR_PREC,
      &_cgmt_COLR_INDEX_PREC,
      &_cgmt_MAX_COLR_INDEX,
      &_cgmt_COLR_VALUE_EXT,
      &_cgmt_MF_ELEM_LIST,
      &_cgmt_BEG_MF_DEFAULTS,
      &_cgmt_FONT_LIST,
      &_cgmt_CHAR_SET_LIST,
      &_cgmt_CHAR_CODING,
      NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };

comando *_cgmt_picture[] = {
      &_cgmt_NULL,
      &_cgmt_SCALE_MODE,
      &_cgmt_COLR_MODE,
      &_cgmt_LINE_WIDTH_MODE,
      &_cgmt_MARKER_SIZE_MODE,
      &_cgmt_EDGE_WIDTH_MODE,
      &_cgmt_VDC_EXTENT,
      &_cgmt_BACK_COLR,
      NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
      NULL, NULL, NULL, NULL };

comando *_cgmt_control[] = {
      &_cgmt_NULL,
      &_cgmt_VDC_INTEGER_PREC,
      &_cgmt_VDC_REAL_PREC,
      &_cgmt_AUX_COLR,
      &_cgmt_TRANSPARENCY,
      &_cgmt_CLIP_RECT,
      &_cgmt_CLIP,
      NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
      NULL, NULL, NULL, NULL, NULL, NULL };

comando *_cgmt_primitive[] = {
      &_cgmt_NULL,
      &_cgmt_LINE,
      &_cgmt_INCR_LINE,
      &_cgmt_DISJT_LINE,
      &_cgmt_INCR_DISJT_LINE,
      &_cgmt_MARKER,
      &_cgmt_INCR_MARKER,
      &_cgmt_TEXT,
      &_cgmt_RESTR_TEXT,
      &_cgmt_APND_TEXT,
      &_cgmt_POLYGON,
      &_cgmt_INCR_POLYGON,
      &_cgmt_POLYGON_SET,
      &_cgmt_INCR_POLYGON_SET,
      &_cgmt_CELL_ARRAY,
      &_cgmt_GDP,
      &_cgmt_RECT,
      &_cgmt_CIRCLE,
      &_cgmt_ARC_3_PT,
      &_cgmt_ARC_3_PT_CLOSE,
      &_cgmt_ARC_CTR,
      &_cgmt_ARC_CTR_CLOSE,
      &_cgmt_ELLIPSE,
      &_cgmt_ELLIP_ARC,
      &_cgmt_ELLIP_ARC_CLOSE,
      NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };

comando *_cgmt_attributes[] = {
      &_cgmt_NULL,
      &_cgmt_LINE_INDEX,
      &_cgmt_LINE_TYPE,
      &_cgmt_LINE_WIDTH,
      &_cgmt_LINE_COLR,
      &_cgmt_MARKER_INDEX,
      &_cgmt_MARKER_TYPE,
      &_cgmt_MARKER_WIDTH,
      &_cgmt_MARKER_COLR,
      &_cgmt_TEXT_INDEX,
      &_cgmt_TEXT_FONT_INDEX,
      &_cgmt_TEXT_PREC,
      &_cgmt_CHAR_EXPAN,
      &_cgmt_CHAR_SPACE,
      &_cgmt_TEXT_COLR,
      &_cgmt_CHAR_HEIGHT,
      &_cgmt_CHAR_ORI,
      &_cgmt_TEXT_PATH,
      &_cgmt_TEXT_ALIGN,
      &_cgmt_CHAR_SET_INDEX,
      &_cgmt_ALT_CHAR_SET,
      &_cgmt_FILL_INDEX,
      &_cgmt_INT_STYLE,
      &_cgmt_FILL_COLR,
      &_cgmt_HATCH_INDEX,
      &_cgmt_PAT_INDEX,
      &_cgmt_EDGE_INDEX,
      &_cgmt_EDGE_TYPE,
      &_cgmt_EDGE_WIDTH,
      &_cgmt_EDGE_COLR,
      &_cgmt_EDGE_VIS,
      &_cgmt_FILL_REF_PT,
      &_cgmt_PAT_TABLE,
      &_cgmt_PAT_SIZE,
      &_cgmt_COLR_TABLE,
      &_cgmt_ASF,
      NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
      NULL, NULL, NULL, NULL };

comando *_cgmt_escape[] = {
      &_cgmt_NULL,
      &_cgmt_ESCAPE,
      &_cgmt_DOMAIN_RING,
      NULL};

comando *_cgmt_external[] = {
      &_cgmt_NULL,
      &_cgmt_MESSAGE,
      &_cgmt_APPL_DATA,
      NULL };

comando *_cgmt_segment[] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };

comando *_cgmt_NULL_NULL[] = {
      &_cgmt_NULL,
	  NULL };
		
comando **_cgmt_comandos[] = {
       _cgmt_NULL_NULL,
       _cgmt_delimiter,
       _cgmt_metafile,
       _cgmt_picture,
       _cgmt_control,
       _cgmt_primitive,
       _cgmt_attributes,
       _cgmt_escape,
       _cgmt_external,
       _cgmt_segment,
      NULL };

/************************************************
*                                               *
*            Dados para binario                 *
*                                               *
************************************************/

/* delimiter elements */

CGM_FUNC _cgmb_NULL            = NULL;

CGM_FUNC _cgmb_NOOP            = &cgmb_noop;
CGM_FUNC _cgmb_BEGMF           = &cgmb_begmtf;
CGM_FUNC _cgmb_ENDMF           = &cgmb_endmtf;
CGM_FUNC _cgmb_BEG_PIC         = &cgmb_begpic;
CGM_FUNC _cgmb_BEG_PIC_BODY    = &cgmb_begpib;
CGM_FUNC _cgmb_END_PIC         = &cgmb_endpic;

/* metafile descriptor elements */

CGM_FUNC _cgmb_MF_VERSION       = &cgmb_mtfver;
CGM_FUNC _cgmb_MF_DESC          = &cgmb_mtfdsc;
CGM_FUNC _cgmb_VDC_TYPE         = &cgmb_vdctyp;
CGM_FUNC _cgmb_INTEGER_PREC     = &cgmb_intpre;
CGM_FUNC _cgmb_REAL_PREC        = &cgmb_realpr;
CGM_FUNC _cgmb_INDEX_PREC       = &cgmb_indpre;
CGM_FUNC _cgmb_COLR_PREC        = &cgmb_colpre;
CGM_FUNC _cgmb_COLR_INDEX_PREC  = &cgmb_colipr;
CGM_FUNC _cgmb_MAX_COLR_INDEX   = &cgmb_maxcoi;
CGM_FUNC _cgmb_COLR_VALUE_EXT   = &cgmb_covaex;
CGM_FUNC _cgmb_MF_ELEM_LIST     = &cgmb_mtfell;
CGM_FUNC _cgmb_MF_DEFAULTS_RPL  = &cgmb_bmtfdf;
CGM_FUNC _cgmb_FONT_LIST        = &cgmb_fntlst;
CGM_FUNC _cgmb_CHAR_SET_LIST    = &cgmb_chslst;
CGM_FUNC _cgmb_CHAR_CODING      = &cgmb_chcdac;

/* picture descriptor elements */

CGM_FUNC _cgmb_SCALE_MODE       = &cgmb_sclmde;
CGM_FUNC _cgmb_COLR_MODE        = &cgmb_clslmd;
CGM_FUNC _cgmb_LINE_WIDTH_MODE  = &cgmb_lnwdmd;
CGM_FUNC _cgmb_MARKER_SIZE_MODE = &cgmb_mkszmd;
CGM_FUNC _cgmb_EDGE_WIDTH_MODE  = &cgmb_edwdmd;
CGM_FUNC _cgmb_VDC_EXTENT       = &cgmb_vdcext;
CGM_FUNC _cgmb_BACK_COLR        = &cgmb_bckcol;

/* control elements */

CGM_FUNC _cgmb_VDC_INTEGER_PREC = &cgmb_vdcipr;
CGM_FUNC _cgmb_VDC_REAL_PREC    = &cgmb_vdcrpr;
CGM_FUNC _cgmb_AUX_COLR         = &cgmb_auxcol;
CGM_FUNC _cgmb_TRANSPARENCY     = &cgmb_transp;
CGM_FUNC _cgmb_CLIP_RECT        = &cgmb_clprec;
CGM_FUNC _cgmb_CLIP             = &cgmb_clpind;

/* primitive elements */

CGM_FUNC _cgmb_LINE             = &cgmb_polyln;
CGM_FUNC _cgmb_DISJT_LINE       = &cgmb_djtply;
CGM_FUNC _cgmb_MARKER           = &cgmb_polymk;
CGM_FUNC _cgmb_TEXT             = &cgmb_text;
CGM_FUNC _cgmb_RESTR_TEXT       = &cgmb_rsttxt;
CGM_FUNC _cgmb_APND_TEXT        = &cgmb_apdtxt;
CGM_FUNC _cgmb_POLYGON          = &cgmb_polygn;
CGM_FUNC _cgmb_POLYGON_SET      = &cgmb_plgset;
CGM_FUNC _cgmb_CELL_ARRAY       = &cgmb_cellar;
CGM_FUNC _cgmb_GDP              = &cgmb_gdp;
CGM_FUNC _cgmb_RECT             = &cgmb_rect;
CGM_FUNC _cgmb_CIRCLE           = &cgmb_circle;
CGM_FUNC _cgmb_ARC_3_PT         = &cgmb_circ3p;
CGM_FUNC _cgmb_ARC_3_PT_CLOSE   = &cgmb_cir3pc;
CGM_FUNC _cgmb_ARC_CTR          = &cgmb_circnt;
CGM_FUNC _cgmb_ARC_CTR_CLOSE    = &cgmb_ccntcl;
CGM_FUNC _cgmb_ELLIPSE          = &cgmb_ellips;
CGM_FUNC _cgmb_ELLIP_ARC        = &cgmb_ellarc;
CGM_FUNC _cgmb_ELLIP_ARC_CLOSE  = &cgmb_ellacl;

/* attribute elements */

CGM_FUNC _cgmb_LINE_INDEX       = &cgmb_lnbdin;
CGM_FUNC _cgmb_LINE_TYPE        = &cgmb_lntype;
CGM_FUNC _cgmb_LINE_WIDTH       = &cgmb_lnwidt;
CGM_FUNC _cgmb_LINE_COLR        = &cgmb_lncolr;
CGM_FUNC _cgmb_MARKER_INDEX     = &cgmb_mkbdin;
CGM_FUNC _cgmb_MARKER_TYPE      = &cgmb_mktype;
CGM_FUNC _cgmb_MARKER_WIDTH     = &cgmb_mksize;
CGM_FUNC _cgmb_MARKER_COLR      = &cgmb_mkcolr;
CGM_FUNC _cgmb_TEXT_INDEX       = &cgmb_txbdin;
CGM_FUNC _cgmb_TEXT_FONT_INDEX  = &cgmb_txftin;
CGM_FUNC _cgmb_TEXT_PREC        = &cgmb_txtprc;
CGM_FUNC _cgmb_CHAR_EXPAN       = &cgmb_chrexp;
CGM_FUNC _cgmb_CHAR_SPACE       = &cgmb_chrspc;
CGM_FUNC _cgmb_TEXT_COLR        = &cgmb_txtclr;
CGM_FUNC _cgmb_CHAR_HEIGHT      = &cgmb_chrhgt;
CGM_FUNC _cgmb_CHAR_ORI         = &cgmb_chrori;
CGM_FUNC _cgmb_TEXT_PATH        = &cgmb_txtpat;
CGM_FUNC _cgmb_TEXT_ALIGN       = &cgmb_txtali;
CGM_FUNC _cgmb_CHAR_SET_INDEX   = &cgmb_chseti;
CGM_FUNC _cgmb_ALT_CHAR_SET     = &cgmb_achsti;
CGM_FUNC _cgmb_FILL_INDEX       = &cgmb_fillin;
CGM_FUNC _cgmb_INT_STYLE        = &cgmb_intsty;
CGM_FUNC _cgmb_FILL_COLR        = &cgmb_fillco;
CGM_FUNC _cgmb_HATCH_INDEX      = &cgmb_hatind;
CGM_FUNC _cgmb_PAT_INDEX        = &cgmb_patind;
CGM_FUNC _cgmb_EDGE_INDEX       = &cgmb_edgind;
CGM_FUNC _cgmb_EDGE_TYPE        = &cgmb_edgtyp;
CGM_FUNC _cgmb_EDGE_WIDTH       = &cgmb_edgwid;
CGM_FUNC _cgmb_EDGE_COLR        = &cgmb_edgcol;
CGM_FUNC _cgmb_EDGE_VIS         = &cgmb_edgvis;
CGM_FUNC _cgmb_FILL_REF_PT      = &cgmb_fillrf;
CGM_FUNC _cgmb_PAT_TABLE        = &cgmb_pattab;
CGM_FUNC _cgmb_PAT_SIZE         = &cgmb_patsiz;
CGM_FUNC _cgmb_COLR_TABLE       = &cgmb_coltab;
CGM_FUNC _cgmb_ASF              = &cgmb_asf;

/* escape elements */

CGM_FUNC _cgmb_ESCAPE           = &cgmb_escape;

/* external elements */

CGM_FUNC _cgmb_MESSAGE          = &cgmb_messag;
CGM_FUNC _cgmb_APPL_DATA        = &cgmb_appdta;

CGM_FUNC *_cgmb_delimiter[] = {
      &_cgmb_NOOP,
      &_cgmb_BEGMF,
      &_cgmb_ENDMF,
      &_cgmb_BEG_PIC,
      &_cgmb_BEG_PIC_BODY,
      &_cgmb_END_PIC,
      NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
      NULL, NULL, NULL, NULL, NULL, NULL, NULL };

CGM_FUNC *_cgmb_metafile[] = {
      &_cgmb_NULL,
      &_cgmb_MF_VERSION,
      &_cgmb_MF_DESC,
      &_cgmb_VDC_TYPE,
      &_cgmb_INTEGER_PREC,
      &_cgmb_REAL_PREC,
      &_cgmb_INDEX_PREC,
      &_cgmb_COLR_PREC,
      &_cgmb_COLR_INDEX_PREC,
      &_cgmb_MAX_COLR_INDEX,
      &_cgmb_COLR_VALUE_EXT,
      &_cgmb_MF_ELEM_LIST,
      &_cgmb_MF_DEFAULTS_RPL,
      &_cgmb_FONT_LIST,
      &_cgmb_CHAR_SET_LIST,
      &_cgmb_CHAR_CODING,
      NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };

CGM_FUNC *_cgmb_picture[] = {
      &_cgmb_NULL,
      &_cgmb_SCALE_MODE,
      &_cgmb_COLR_MODE,
      &_cgmb_LINE_WIDTH_MODE,
      &_cgmb_MARKER_SIZE_MODE,
      &_cgmb_EDGE_WIDTH_MODE,
      &_cgmb_VDC_EXTENT,
      &_cgmb_BACK_COLR,
      NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
      NULL, NULL, NULL, NULL };

CGM_FUNC *_cgmb_control[] = {
      &_cgmb_NULL,
      &_cgmb_VDC_INTEGER_PREC,
      &_cgmb_VDC_REAL_PREC,
      &_cgmb_AUX_COLR,
      &_cgmb_TRANSPARENCY,
      &_cgmb_CLIP_RECT,
      &_cgmb_CLIP,
      NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
      NULL, NULL, NULL, NULL, NULL, NULL };

CGM_FUNC *_cgmb_primitive[] = {
      &_cgmb_NULL,
      &_cgmb_LINE,
      &_cgmb_DISJT_LINE,
      &_cgmb_MARKER,
      &_cgmb_TEXT,
      &_cgmb_RESTR_TEXT,
      &_cgmb_APND_TEXT,
      &_cgmb_POLYGON,
      &_cgmb_POLYGON_SET,
      &_cgmb_CELL_ARRAY,
      &_cgmb_GDP,
      &_cgmb_RECT,
      &_cgmb_CIRCLE,
      &_cgmb_ARC_3_PT,
      &_cgmb_ARC_3_PT_CLOSE,
      &_cgmb_ARC_CTR,
      &_cgmb_ARC_CTR_CLOSE,
      &_cgmb_ELLIPSE,
      &_cgmb_ELLIP_ARC,
      &_cgmb_ELLIP_ARC_CLOSE,
      NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };

CGM_FUNC *_cgmb_attributes[] = {
      &_cgmb_NULL,
      &_cgmb_LINE_INDEX,
      &_cgmb_LINE_TYPE,
      &_cgmb_LINE_WIDTH,
      &_cgmb_LINE_COLR,
      &_cgmb_MARKER_INDEX,
      &_cgmb_MARKER_TYPE,
      &_cgmb_MARKER_WIDTH,
      &_cgmb_MARKER_COLR,
      &_cgmb_TEXT_INDEX,
      &_cgmb_TEXT_FONT_INDEX,
      &_cgmb_TEXT_PREC,
      &_cgmb_CHAR_EXPAN,
      &_cgmb_CHAR_SPACE,
      &_cgmb_TEXT_COLR,
      &_cgmb_CHAR_HEIGHT,
      &_cgmb_CHAR_ORI,
      &_cgmb_TEXT_PATH,
      &_cgmb_TEXT_ALIGN,
      &_cgmb_CHAR_SET_INDEX,
      &_cgmb_ALT_CHAR_SET,
      &_cgmb_FILL_INDEX,
      &_cgmb_INT_STYLE,
      &_cgmb_FILL_COLR,
      &_cgmb_HATCH_INDEX,
      &_cgmb_PAT_INDEX,
      &_cgmb_EDGE_INDEX,
      &_cgmb_EDGE_TYPE,
      &_cgmb_EDGE_WIDTH,
      &_cgmb_EDGE_COLR,
      &_cgmb_EDGE_VIS,
      &_cgmb_FILL_REF_PT,
      &_cgmb_PAT_TABLE,
      &_cgmb_PAT_SIZE,
      &_cgmb_COLR_TABLE,
      &_cgmb_ASF,
      NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
      NULL, NULL, NULL, NULL };

CGM_FUNC *_cgmb_escape[] = {
      &_cgmb_NULL,
      &_cgmb_ESCAPE,
      NULL};

CGM_FUNC *_cgmb_external[] = {
      &_cgmb_NULL,
      &_cgmb_MESSAGE,
      &_cgmb_APPL_DATA,
      NULL };

CGM_FUNC *_cgmb_segment[] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };

CGM_FUNC **_cgmb_comandos[] = {
       _cgmb_delimiter,
       _cgmb_metafile,
       _cgmb_picture,
       _cgmb_control,
       _cgmb_primitive,
       _cgmb_attributes,
       _cgmb_escape,
       _cgmb_external,
       _cgmb_segment,
       NULL };

/************************************************
*                                               *
*             Funcoes para binario              *
*                                               *
************************************************/

int cgm_getfilepos ( void )
{
 if (cdcgmcountercb)
  return cdcgmcountercb ( intcgm_canvas, (ftell ( intcgm_cgm.fp )*100.)/intcgm_cgm.file_size );
  
 return 0; 
}

#define cgmb_getw cgmb_getu16
#define cgmb_getb(b) cgmb_getc((unsigned char *)(b))

static void cgmb_donothing ( void )
{
 intcgm_cgm.bc=intcgm_cgm.len;
}

int cgmb_exec_comand ( int classe, int id )
{
 int err;

 if ( _cgmb_comandos[classe][id]==NULL )
  {
   cgmb_donothing();
   return 0;
  }

 err = (*_cgmb_comandos[classe][id])();

 if ( err == -1 )
  return 1;
 else if ( err )
  {
   cgmb_donothing();
   return 0;
  }

 return 0;
}

int cgmb_getbit ( unsigned char *b )
{
 static unsigned char b1;

 if ( intcgm_cgm.pc==0 || intcgm_cgm.pc==8 )
  {
   b1 = intcgm_cgm.buff.dados[intcgm_cgm.bc];
   intcgm_cgm.bc++;

   if ( intcgm_cgm.bc > intcgm_cgm.len )
   {
    return 1;
   }

   intcgm_cgm.pc=0;
  }

 *b = b1;

 switch ( intcgm_cgm.pc )
  {
   case 0:
    *b = ( *b | 0x0080 ) >> 7;
    break;
   case 1:
    *b = ( *b | 0x0040 ) >> 6;
    break;
   case 2:
    *b = ( *b | 0x0020 ) >> 5;
    break;
   case 3:
    *b = ( *b | 0x0010 ) >> 4;
    break;
   case 4:
    *b = ( *b | 0x0008 ) >> 3;
    break;
   case 5:
    *b = ( *b | 0x0004 ) >> 2;
    break;
   case 6:
    *b = ( *b | 0x0002 ) >> 1;
    break;
   case 7:
    *b = ( *b | 0x0001 );
    break;
  }

 intcgm_cgm.pc++;

 return 0;
}

int cgmb_get2bit ( unsigned char *b )
{
 static unsigned char b1;

 if ( intcgm_cgm.pc==0 || intcgm_cgm.pc==8 )
  {
   b1 = intcgm_cgm.buff.dados[intcgm_cgm.bc];
   intcgm_cgm.bc++;

   if ( intcgm_cgm.bc > intcgm_cgm.len ) return 1;

   intcgm_cgm.pc=0;
  }

 *b = b1;

 switch ( intcgm_cgm.pc )
  {
   case 0:
    *b = ( *b | 0x00C0 ) >> 6;
    break;
   case 2:
    *b = ( *b | 0x0030 ) >> 4;
    break;
   case 4:
    *b = ( *b | 0x000C ) >> 2;
    break;
   case 6:
    *b = ( *b | 0x0003 );
    break;
  }

 intcgm_cgm.pc += 2;

 return 0;
}


int cgmb_get4bit ( unsigned char *b )
{
 static unsigned char b1;

 if ( intcgm_cgm.pc==0 || intcgm_cgm.pc==8 )
  {
   b1 = intcgm_cgm.buff.dados[intcgm_cgm.bc];
   intcgm_cgm.bc++;

   if ( intcgm_cgm.bc > intcgm_cgm.len ) return 1;

   intcgm_cgm.pc=0;
  }

 *b = b1;

 switch ( intcgm_cgm.pc )
  {
   case 0:
    *b = ( *b | 0x00F0 ) >> 4;
    break;
   case 4:
    *b = ( *b | 0x000F );
    break;
  }

 intcgm_cgm.pc += 4;

 return 0;
}

int cgmb_getw ( unsigned short * );

int cgmb_getc ( unsigned char *b )
{
 *b = intcgm_cgm.buff.dados[intcgm_cgm.bc];
 intcgm_cgm.bc++;

 if ( intcgm_cgm.bc > intcgm_cgm.len ) return 1;
 
 return 0;   
}

int cgmb_geti8  ( signed char *b )
{
 unsigned char b1;

 if ( cgmb_getb ( &b1 ) ) return 1;

 *b = (signed char) b1;
 
 return 0;
}

int cgmb_geti16 ( short *b )
{
 unsigned char b1, b2;

 if ( cgmb_getb ( &b1 ) ) return 1;
 if ( cgmb_getb ( &b2 ) ) return 1;

 *b = ( b1<<8 ) | b2;

 return 0;
}

int cgmb_geti24 ( long *b )
{
 unsigned char b1, b2, b3;

 if ( cgmb_getb ( &b1 ) ) return 1;
 if ( cgmb_getb ( &b2 ) ) return 1;
 if ( cgmb_getb ( &b3 ) ) return 1;

 *b = ( b1<<16 ) | ( b2<<8 ) | b3;

 return 0;
}

int cgmb_geti32 ( long *b )
{
 unsigned char b1, b2, b3, b4;

 if ( cgmb_getb ( &b1 ) ) return 1;
 if ( cgmb_getb ( &b2 ) ) return 1;
 if ( cgmb_getb ( &b3 ) ) return 1;
 if ( cgmb_getb ( &b4 ) ) return 1;

 *b = ( b1<<24 ) | ( b2<<16 ) | ( b3<<8 ) | b4;

 return 0;
}

int cgmb_getu8  ( unsigned char *b )
{
 if ( cgmb_getb ( b ) ) return 1;

 return 0;
}

int cgmb_getu16 ( unsigned short *b )
{
 unsigned char b1, b2;

 if ( cgmb_getb ( &b1 ) ) return 1;
 if ( cgmb_getb ( &b2 ) ) return 1;

 *b = ( b1<<8 ) | b2;

 return 0;
}

int cgmb_getu24 ( unsigned long *b )
{
 unsigned char b1, b2, b3;

 if ( cgmb_getb ( &b1 ) ) return 1;
 if ( cgmb_getb ( &b2 ) ) return 1;
 if ( cgmb_getb ( &b3 ) ) return 1;

 *b = ( b1<<16 ) | ( b2<<8 ) | b3;

 return 0;
}

int cgmb_getu32 ( unsigned long *b )
{
 unsigned char b1, b2, b3, b4;

 if ( cgmb_getb ( &b1 ) ) return 1;
 if ( cgmb_getb ( &b2 ) ) return 1;
 if ( cgmb_getb ( &b3 ) ) return 1;
 if ( cgmb_getb ( &b4 ) ) return 1;

 *b = ( b1<<24 ) | ( b2<<16 ) | ( b3<<8 ) | b4;

 return 0;
}

int cgmb_getfl32 ( float *b )
{
 unsigned char b1, b2, b3, b4;
 union {
        float f;
        long  l;
       } r;

 if ( cgmb_getb ( &b1 ) ) return 1;
 if ( cgmb_getb ( &b2 ) ) return 1;
 if ( cgmb_getb ( &b3 ) ) return 1;
 if ( cgmb_getb ( &b4 ) ) return 1;

 r.l = ( b1<<24 ) | ( b2<<16 ) | ( b3<<8 ) | b4;
 *b = r.f;

 return 0;
}

int cgmb_getfl64 ( double *b )
{
 unsigned char b1, b2, b3, b4, b5, b6, b7, b8;
 union {
        double d;
        long   l[2];
       } r;

 if ( cgmb_getb ( &b1 ) ) return 1;
 if ( cgmb_getb ( &b2 ) ) return 1;
 if ( cgmb_getb ( &b3 ) ) return 1;
 if ( cgmb_getb ( &b4 ) ) return 1;
 if ( cgmb_getb ( &b5 ) ) return 1;
 if ( cgmb_getb ( &b6 ) ) return 1;
 if ( cgmb_getb ( &b7 ) ) return 1;
 if ( cgmb_getb ( &b8 ) ) return 1;

 r.l[1] = ( b1<<24 ) | ( b2<<16 ) | ( b3<<8 ) | b4;
 r.l[0] = ( b5<<24 ) | ( b6<<16 ) | ( b7<<8 ) | b8;
 *b = r.d;

 return 0;
}

int cgmb_getfx32 ( float *b )
{
 short si;
 unsigned short ui;

 if ( cgmb_geti16 ( &si ) ) return 1;
 if ( cgmb_getu16 ( &ui ) ) return 1;

 *b = (float) ( si + ( ui / 65536.0 ) );

 return 0;
}

int cgmb_getfx64 ( double *b )
{
 long si, ui;

 if ( cgmb_geti32 ( &si ) ) return 1;
 if ( cgmb_geti32 ( &ui ) ) return 1;

 *b = si + ( (unsigned short) ui / ( 65536.0 * 65536.0 ) );

 return 0;
}

int cgmb_ter ( void )
{
 return 0;
}

int cgmb_rch ( void )
{
 int c, id, len, cont, i; 
 unsigned char ch[2], dummy;
 unsigned short b;
 int erro;

 if ( intcgm_cgm.bc!=intcgm_cgm.len )
  {
   return 1;
  }

 intcgm_cgm.bc = 0;

 erro = fread ( ch, 1, 2, intcgm_cgm.fp );
 if ( erro<2 ) return 1;

 intcgm_cgm.bl += 2;

 b = (ch[0] << 8) + ch[1];

 len = b & 0x001F;

 id = ( b & 0x0FE0 ) >> 5;

 c = ( b & 0xF000 ) >> 12;

 cont = 0;

 if ( len > 30 )
  {
   erro = fread ( ch, 1, 2, intcgm_cgm.fp );
   if ( erro<2 ) return 1;

   intcgm_cgm.bl += 2;

   b = (ch[0] << 8) + ch[1];

   len = b & 0x7FFF;
   cont = ( b & 0x8000 );
  }

 intcgm_cgm.len = len;

 if ( intcgm_cgm.len )
  {
   if ( intcgm_cgm.len>intcgm_cgm.buff.size )
     intcgm_cgm.buff.dados = (char *) realloc ( intcgm_cgm.buff.dados, sizeof(char) * intcgm_cgm.len );

   erro = fread ( intcgm_cgm.buff.dados, 1, intcgm_cgm.len, intcgm_cgm.fp );
   if ( erro<intcgm_cgm.len ) return 1;

   intcgm_cgm.bl += intcgm_cgm.len;

   if ( len & 1 )
    {
     erro = fread ( &dummy, 1, 1, intcgm_cgm.fp );
     if ( erro<1 ) return 1;

     intcgm_cgm.bl += 1;
    }

   while ( cont )
    {
     unsigned char ch[2];
     unsigned short b;
     int old_len = intcgm_cgm.len;

     erro = fread ( ch, 1, 2, intcgm_cgm.fp );
     if ( erro<2 ) return 1;

     intcgm_cgm.bl += 2;

     b = (ch[0] << 8) + ch[1];

     cont = ( b & 0x8000 );

     len = b & 0x7fff;

     intcgm_cgm.len += len;

     if ( intcgm_cgm.len>intcgm_cgm.buff.size )
       intcgm_cgm.buff.dados = (char *) realloc ( (char *)intcgm_cgm.buff.dados, sizeof(char) * intcgm_cgm.len );

     erro = fread ( &intcgm_cgm.buff.dados[old_len], 1, len, intcgm_cgm.fp );
     if ( erro<len ) return 1;

     if ( len & 1 )
      {
       erro = fread ( &dummy, 1, 1, intcgm_cgm.fp );
       if ( erro<1 ) return 1;
       intcgm_cgm.bl += 1;
      }
    }
  }

 cgm_getfilepos ();

 if ( cgmb_exec_comand ( c, id ) ) return 1;

 for ( i=0; i<intcgm_cgm.len-intcgm_cgm.bc; i++ )
  {
   unsigned char dummy;
   if ( cgmb_getb ( &dummy ) ) return 1;
  }

 return 0;
}

int cgmb_ci ( unsigned long *ci )
{
 unsigned char c;
 unsigned short i;

 switch ( intcgm_cgm.cix_prec )
  {
   case 0: if ( cgmb_getu8  ( &c ) ) return 1; 
           *ci = (unsigned long) c;
           break;
   case 1: if ( cgmb_getu16 ( &i ) ) return 1;
           *ci = (unsigned long) i;
           break;
   case 2: if ( cgmb_getu24 ( ci ) ) return 1;
           break;
   case 3: if ( cgmb_getu32 ( ci ) ) return 1;
           break;
  }

 return 0;
}

int cgmb_cd ( unsigned long *cd )
{
 unsigned char c;
 unsigned short i;

 switch ( intcgm_cgm.cd_prec )
  {
   case 0: if ( cgmb_getu8  ( &c ) ) return 1;
           *cd = (unsigned long) c;
           break;
   case 1: if ( cgmb_getu16 ( &i ) ) return 1;
           *cd = (unsigned long) i;
           break;
   case 2: if ( cgmb_getu24 ( cd ) ) return 1;
           break;
   case 3: if ( cgmb_getu32 ( cd ) ) return 1;
           break;
  }

 return 0;
}

int cgmb_rgb ( unsigned long *r, unsigned long *g, unsigned long *b )
{
 if ( cgmb_cd ( r ) ) return 1;
 if ( cgmb_cd ( g ) ) return 1;
 if ( cgmb_cd ( b ) ) return 1;

 return 0;
}

int cgmb_ix ( long *ix )
{
 signed char c;
 short i;

 switch ( intcgm_cgm.ix_prec.b_prec )
  {
   case 0: if ( cgmb_geti8  ( &c ) ) return 1;
           *ix = (long) c;
           break;
   case 1: if ( cgmb_geti16 ( &i ) ) return 1;
           *ix = (long) i;
           break;
   case 2: if ( cgmb_geti24 ( ix ) ) return 1;
           break;
   case 3: if ( cgmb_geti32 ( ix ) ) return 1;
           break;
  }

 return 0;
}

int cgmb_e ( short *e )
{
 return cgmb_geti16 ( e );
}

int cgmb_i ( long *li )
{
 signed char c;
 short i;

 switch ( intcgm_cgm.int_prec.b_prec )
  {
   case 0: if ( cgmb_geti8  ( &c ) ) return 1;
           *li = (long) c;
           break;
   case 1: if ( cgmb_geti16 ( &i ) ) return 1;
           *li = (long) i;
           break;
   case 2: if ( cgmb_geti24 ( li ) ) return 1;
           break;
   case 3: if ( cgmb_geti32 ( li ) ) return 1;
           break;
  }

 return 0;
}

int cgmb_u ( unsigned long *ui )
{
 unsigned char c;
 unsigned short i;

 switch ( intcgm_cgm.int_prec.b_prec )
  {
   case 0: if ( cgmb_getu8  ( &c ) ) return 1;
           *ui = (unsigned long) c;
           break;
   case 1: if ( cgmb_getu16 ( &i ) ) return 1;
           *ui = (unsigned long) i;
           break;
   case 2: if ( cgmb_getu24 ( ui ) ) return 1;
           break;
   case 3: if ( cgmb_getu32 ( ui ) ) return 1;
           break;
  }

 return 0;
}

int cgmb_r ( double *d )
{
 float f;

 switch ( intcgm_cgm.real_prec.b_prec )
  {
   case 0: if ( cgmb_getfl32 ( &f ) ) return 1;
           *d = (double) f;
           break;
   case 1: if ( cgmb_getfl64 ( d ) ) return 1;
           break;
   case 2: if ( cgmb_getfx32 ( &f ) ) return 1;
           *d = (double) f;
           break;
   case 3: if ( cgmb_getfx64 ( d ) ) return 1;
           break;
  }

 return 0;
}

int cgmb_s ( char **str )
{
 register unsigned i = 0;
 unsigned char l;
 unsigned short l1;
 unsigned short cont;
 char *s = NULL;

 cont = 1;

 if ( cgmb_getu8 ( &l ) ) return 1;

 l1 = l;

 while ( cont )
  {
   if ( l > 254 )
    {
     if ( cgmb_getu16 ( &l1 ) ) return 1;
     cont = ( l1 & 0x8000);
     l1 &= 0x7fff;
    }
   else
    cont = 0;

   s = (char *)realloc ( (unsigned char *)s, (sizeof ( char ) * l1) + 1 );
 
   for ( i=0; i<l1; i++ )
    {
     unsigned char k;
     if ( cgmb_getb ( &k ) ) return 1;
     s[i] = (char) k;
    }

  }
 s[i] = '\0';

 *str = s;

 return 0;
}

int cgmb_vdc ( double *vdc )
{
 signed char c;
 short i;
 long l;
 float f;

 if ( intcgm_cgm.vdc_type == 0 )
  switch ( intcgm_cgm.vdc_int.b_prec )
   {
    case 0: if ( cgmb_geti8  ( &c ) ) return 1;
            *vdc = (double) c;
            break;
    case 1: if ( cgmb_geti16 (  &i ) ) return 1;
            *vdc = (double) i;
            break;
    case 2: if ( cgmb_geti24 ( &l ) ) return 1;
            *vdc = (double) l;
            break;
    case 3: if ( cgmb_geti32 ( &l ) ) return 1;
            *vdc = (double) l;
            break;
   }
 else
   switch ( intcgm_cgm.vdc_real.b_prec )
    {
     case 0: if ( cgmb_getfl32 ( &f ) ) return 1;
             *vdc = (double) f;
             break;
     case 1: if ( cgmb_getfl64 ( vdc ) ) return 1;
             break;
     case 2: if ( cgmb_getfx32 ( &f ) ) return 1;
             *vdc = (double) f;
             break;
     case 3: if ( cgmb_getfx64 ( vdc ) ) return 1;
             break;
    }

 return 0;
}

int cgmb_p ( double *x, double *y )
{
 if ( cgmb_vdc ( x ) ) return 1;
 if ( cgmb_vdc ( y ) ) return 1;
 
 return 0;
}

int cgmb_co ( void *co )
{
 if ( intcgm_cgm.clrsm == 0 ) /* indexed */
  {
   unsigned long *ci = (unsigned long *) co;
   if ( cgmb_ci ( ci ) ) return 1;
  }
 else
  {
   unsigned long *rgb = (unsigned long *) co;
   if ( cgmb_rgb ( &rgb[0], &rgb[1], &rgb[2] ) ) return 1;
  }

 return 0;
}

int cgmb_pixeli ( unsigned long *ci, int localp )
{
 unsigned char c;
 unsigned short i;

 if ( localp==0 )
  {
   if ( intcgm_cgm.cix_prec==0 ) localp = 8;
   else if ( intcgm_cgm.cix_prec==1 ) localp = 16;
   else if ( intcgm_cgm.cix_prec==2 ) localp = 24;
   else if ( intcgm_cgm.cix_prec==3 ) localp = 32;
  }

 switch ( localp )
  {
   case 1: if ( cgmb_getbit ( &c ) ) return 1;
           *ci = (unsigned long) c;
           break;
   case 2: if ( cgmb_get2bit ( &c ) )
           *ci = (unsigned long) c;
           return 1;
           break;
   case 4: if ( cgmb_get4bit ( &c ) )
           *ci = (unsigned long) c;
           return 1;
           break;
   case 8: if ( cgmb_getu8  ( &c ) ) return 1; 
           *ci = (unsigned long) c;
           break;
   case 16: if ( cgmb_getu16 ( &i ) ) return 1;
           *ci = (unsigned long) i;
           break;
   case 24: if ( cgmb_getu24 ( ci ) ) return 1;
           break;
   case 32: if ( cgmb_getu32 ( ci ) ) return 1;
           break;
  }

 return 0;
}

int cgmb_pixeld ( unsigned long *cd, int localp )
{
 unsigned char c;
 unsigned short i;

 if ( localp==0 )
  {
   if ( intcgm_cgm.cd_prec==0 ) localp = 8;
   else if ( intcgm_cgm.cd_prec==1 ) localp = 16;
   else if ( intcgm_cgm.cd_prec==2 ) localp = 24;
   else if ( intcgm_cgm.cd_prec==3 ) localp = 32;
  }

 switch ( localp )
  {
   case  1: if ( cgmb_getbit  ( &c ) ) return 1;
            *cd = (unsigned long) c;
            break;
   case  2: if ( cgmb_get2bit ( &c ) ) return 1;
            *cd = (unsigned long) c;
            break;
   case  4: if ( cgmb_get4bit ( &c ) )
            *cd = (unsigned long) c;
            return 1;
            break;
   case  8: if ( cgmb_getu8  ( &c ) ) return 1;
            *cd = (unsigned long) c;
            break;
   case 16: if ( cgmb_getu16 ( &i ) ) return 1;
            *cd = (unsigned long) i;
            break;
   case 24: if ( cgmb_getu24 ( cd ) ) return 1;
            break;
   case 32: if ( cgmb_getu32 ( cd ) ) return 1;
            break;
  }

 return 0;
}

int cgmb_pixelrgb ( unsigned long *r, unsigned long *g, unsigned long *b, int localp )
{
 if ( cgmb_pixeld ( r, localp ) ) return 1;
 if ( cgmb_pixeld ( g, localp ) ) return 1;
 if ( cgmb_pixeld ( b, localp ) ) return 1;

 return 0;
}

int cgmb_getpixel ( void *co, int localp )
{
 if ( intcgm_cgm.clrsm == 0 ) /* indexed */
  {
   unsigned long *ci = (unsigned long *) co;
   if ( cgmb_pixeli ( ci, localp ) ) return 1;
  }
 else
  {
   unsigned long *rgb = (unsigned long *) co;
   if ( cgmb_pixelrgb ( &rgb[0], &rgb[1], &rgb[2], localp ) ) return 1;
  }

 return 0;
}

/************************************************
*                                               *
*            Funcoes para clear text            *
*                                               *
************************************************/

void strlower ( char *string )
{
 int i;
 for ( i=0; string[i]!='\0'; i++ )
  string[i] = tolower ( string[i] );
}

char *cgmt_getsep (void)
{
 static char ch[256];

 fscanf ( intcgm_cgm.fp, "%[ \r\n\t\v\f,]", ch );

 return ch;
}

void cgmt_getcom (void)
{
 char chr[256], c;

 while ( (c = fgetc( intcgm_cgm.fp )) == '%' )
  {
   fscanf ( intcgm_cgm.fp, "%[^%]%%", chr );

   cgmt_getsep();
  }

 ungetc( c, intcgm_cgm.fp );
}

char *cgmt_getparentheses (void)
{
 static char ch[256];

 cgmt_getsep();

 fscanf ( intcgm_cgm.fp, "%[()]", ch );

 return ch;
}

int cgmt_ter ( void )
{
 char c;

 cgmt_getcom();

 cgmt_getsep();

 fscanf ( intcgm_cgm.fp, "%c", &c );

 if ( c=='/' || c==';' ) return 0;

 ungetc ( c, intcgm_cgm.fp );

 return 1;
}

int cgmt_rch ( void )
{
 char chr[256];
 char *pt;
 int i, j;

 cgmt_getsep();

 cgmt_getcom();

/* addcounter();*/

 fscanf ( intcgm_cgm.fp, "%[^ \r\n\t\v\f,/;%\"()]", chr );

 pt = strtok(chr,"_$");

 while ( (pt = strtok ( NULL, "_$" )) )
  strcat ( chr, pt );

 strlower(chr);

 for ( i=0; _cgmt_comandos[i]!=NULL; i++ )
  {
   for ( j=0; _cgmt_comandos[i][j]!=NULL; j++ )
    {
     if ( strcmp( chr, _cgmt_comandos[i][j]->nome )==0 )
      {
       int r = (*_cgmt_comandos[i][j]->func)();
	   cgm_getfilepos ();
	   return r;
	  }
    }
  }


 return 0;
}

int cgmt_i ( long *i )
{
 cgmt_getsep();

 cgmt_getcom();

 if ( fscanf( intcgm_cgm.fp, "%ld", i ) ) return 0;

 return 1;
}

int cgmt_ci ( unsigned long *ci )
{
 return cgmt_i ( (long*)ci );
}

int cgmt_cd ( unsigned long *cd )
{
 return cgmt_i ( (long *) cd );
}

int cgmt_rgb ( unsigned long *r, unsigned long *g, unsigned long *b )
{
 if ( cgmt_cd ( r ) ) return 1;

 if ( cgmt_cd ( g ) ) return 1;

 if ( cgmt_cd ( b ) ) return 1;
 
 return 0;
}

int cgmt_ix ( long *ix )
{
 return cgmt_i ( (long *) ix );
}

int cgmt_e ( short *e, const char **el )
{
 char chr[256];
 int i;
 char *pt;

 cgmt_getsep();

 cgmt_getcom();

 fscanf ( intcgm_cgm.fp, "%[^ \r\n\t\v\f,/;%\"\']", chr );

 strlower(chr);

 pt = strtok(chr,"_$");

 while ( (pt = strtok ( NULL, "_$" )) )
  strcat ( chr, pt );

 for ( i=0; el[i]!=NULL; i++ )
  if ( strcmp( chr, el[i] ) == 0 )
   {
    *e = i;
    return 0;
   };
   
 return 1;
}

int cgmt_r ( double *f )
{
 cgmt_getsep();

 cgmt_getcom();

 if ( fscanf( intcgm_cgm.fp, "%lg", f ) ) return 0;

 return 1;
}

int cgmt_s ( char **str )
{
 char c, delim;
 int intcgm_block = 80;
 int i = 0;

 *str = (char *) malloc ( intcgm_block*sizeof(char) );

 strcpy ( *str, "" );

 cgmt_getsep();

 cgmt_getcom();

 delim = fgetc ( intcgm_cgm.fp );

 if ( delim != '"' && delim != '\'' ) return 1;

 do
  {
   if ( (c=fgetc(intcgm_cgm.fp))==delim )
    if ( (c=fgetc(intcgm_cgm.fp))==delim )
     (*str)[i++] = c;
    else 
     {
      ungetc(c,intcgm_cgm.fp);
      break;
     }
   else
    (*str)[i++] = c;

   if ( (i+1)==intcgm_block )
    {
     intcgm_block *= 2;

     *str = (char *) realloc ( *str, intcgm_block*sizeof(char) );
    }
  } while ( 1 );

 (*str)[i] = '\0';

 /* addcounter();*/

 return 0;
}

int cgmt_vdc ( double *vdc )
{
 long l;

 if ( intcgm_cgm.vdc_type==0 )
  {
   if ( cgmt_i ( &l ) ) return 1;
   *vdc = (double) l;
   return 0;
  }
 else
  return cgmt_r ( vdc  );
}

int cgmt_p ( double *x, double *y )
{
 cgmt_getparentheses();

 if ( cgmt_vdc ( x ) ) return 1;

 if ( cgmt_vdc ( y ) ) return 1;

 cgmt_getparentheses();

 return 0;
}

int cgmt_co ( void *co )
{
 if ( intcgm_cgm.clrsm == 0 ) /* indexed */
  {
   unsigned long *ci = (unsigned long*)co;
   if ( cgmt_ci ( ci ) ) return 1;
  }
 else
  {
   unsigned long *cb = (unsigned long *) co;
   if ( cgmt_rgb ( &cb[0], &cb[1], &cb[2] ) ) return 1;
  }
  
 return 0; 
}