File: gdevcgml.c

package info (click to toggle)
ghostscript 8.71~dfsg2-9+squeeze1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 79,896 kB
  • ctags: 80,654
  • sloc: ansic: 501,432; sh: 25,689; python: 4,853; cpp: 3,633; perl: 3,597; tcl: 1,480; makefile: 1,187; lisp: 407; asm: 284; xml: 263; awk: 66; csh: 17; yacc: 15
file content (1152 lines) | stat: -rw-r--r-- 28,461 bytes parent folder | download | duplicates (3)
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
/* Copyright (C) 2001-2006 Artifex Software, Inc.
   All Rights Reserved.
  
   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied, modified
   or distributed except as expressly authorized under the terms of that
   license.  Refer to licensing information at http://www.artifex.com/
   or contact Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134,
   San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
*/
/* $Id: gdevcgml.c 8250 2007-09-25 13:31:24Z giles $ */
/* CGM-writing library */
#include "memory_.h"
#include "stdio_.h"
#include "gdevcgmx.h"

/* Forward references to command-writing procedures */
static void begin_command(cgm_state *, cgm_op_index);

#define OP(op) begin_command(st, op)
static cgm_result end_command(cgm_state *);

#define END_OP (void)end_command(st)
#define DONE return end_command(st)
/* Parameters */
static void put_int(cgm_state *, cgm_int, int);

#define CI(ci) put_int(st, ci, st->metafile.color_index_precision)
#define I(i) put_int(st, i, st->metafile.integer_precision)
#define IX(ix) put_int(st, ix, st->metafile.index_precision)
#define E(e) put_int(st, (int)(e), 16)
static void put_real(cgm_state *, cgm_real, const cgm_precision *);

#define R(r) put_real(st, r, &st->metafile.real_precision)
static void put_vdc(cgm_state *, const cgm_vdc *);

#define VDC(vdc) put_vdc(st, vdc)
#define VDC2(vdc1, vdc2) VDC(vdc1); VDC(vdc2)
#define VDC4(vdc1, vdc2, vdc3, vdc4) VDC2(vdc1, vdc2); VDC2(vdc3, vdc4)
static void put_vdc_r(cgm_state *, const cgm_line_marker_extent *, cgm_line_marker_specification_mode);

#define VDC_R(vdcr, mode) put_vdc_r(st, vdcr, mode)
static void put_point(cgm_state *, const cgm_point *);

#define P(p) put_point(st, p)
static void put_points(cgm_state *, const cgm_point *, int);

#define nP(p, n) put_points(st, p, n)
static void put_string(cgm_state *, const char *, uint);

#define S(s, l) put_string(st, s, l)
static void put_color(cgm_state *, const cgm_color *);

#define CO(co) put_color(st, co)
static void put_rgb(cgm_state *, const cgm_rgb *);

#define CD(cd) put_rgb(st, cd)
/* Other data types */
#define put_byte(st, b)\
  if ( st->command_count == command_max_count ) write_command(st, false);\
  st->command[st->command_count++] = (byte)(b)
static void put_bytes(cgm_state *, const byte *, uint);
static void write_command(cgm_state *, bool);
static void put_real_precision(cgm_state *, const cgm_precision *);

/* ================ Public routines ================ */

/* ---------------- Initialize/terminate ---------------- */

/* Initialize a CGM writer. */
cgm_state *
cgm_initialize(FILE * file, const cgm_allocator * cal)
{
    cgm_state *st = (*cal->alloc) (cal->private_data, sizeof(cgm_state));

    if (st == 0)
	return 0;
    st->file = file;
    st->allocator = *cal;
    /* Initialize metafile elements. */
    st->metafile.vdc_type = cgm_vdc_integer;
    st->metafile.integer_precision = 16;
    st->metafile.real_precision.representation = cgm_representation_fixed;
    st->metafile.real_precision.exponent_or_whole_width = 16;
    st->metafile.real_precision.fraction_width = 16;
    st->metafile.index_precision = 16;
    st->metafile.color_precision = 8;
    st->metafile.color_index_precision = 8;
    st->metafile.maximum_color_index = 63;
    /* color_value_extent */
    /*st->metafile.character_coding_announcer = 0; */
    /* Initialize picture elements. */
    st->picture.scaling_mode = cgm_scaling_abstract;
    st->picture.color_selection_mode = cgm_color_selection_indexed;
    st->picture.line_width_specification_mode = cgm_line_marker_absolute;
    st->picture.marker_size_specification_mode = cgm_line_marker_absolute;
    st->picture.edge_width_specification_mode = cgm_line_marker_absolute;
    /* vdc_extent */
    /* background_color */
    /* Initialize control elements. */
    st->vdc_integer_precision = st->metafile.integer_precision;
    st->vdc_real_precision = st->metafile.real_precision;
    st->transparency = cgm_transparency_on;
    /* clip_rectangle */
    st->clip_indicator = cgm_clip_on;
    /* Initialize other state elements. */
    st->line_bundle_index = 1;
    st->line_type = cgm_line_solid;
    /* line_width */
    /* line_color */
    st->marker_bundle_index = 1;
    st->marker_type = cgm_marker_asterisk;
    /* marker_size */
    /* marker_color */
    st->text_bundle_index = 1;
    st->text_font_index = 1;
    st->text_precision = cgm_text_precision_string;
    st->character_expansion_factor = 1.0;
    st->character_spacing = 0.0;
    /* text_color */
    /* character_height */
    /* character_orientation */
    st->text_path = cgm_text_path_right;
    /* text_alignment */
    st->character_set_index = 1;
    st->alternate_character_set_index = 1;
    st->fill_bundle_index = 1;
    st->interior_style = cgm_interior_style_hollow;
    st->hatch_index = cgm_hatch_horizontal;
    st->pattern_index = 1;
    st->edge_bundle_index = 1;
    st->edge_type = cgm_edge_solid;
    /* edge_width */
    st->edge_visibility = false;
    /* fill_reference_point */
    /* pattern_table */
    /* pattern_size */
    /* color_table */
    memset(st->source_flags, (byte) cgm_aspect_source_individual,
	   sizeof(st->source_flags));
    return st;
}

/* Terminate a CGM writer. */
cgm_result
cgm_terminate(cgm_state * st)
{
    (*st->allocator.free) (st->allocator.private_data, st);
    return cgm_result_ok;
}

/* ---------------- Metafile elements ---------------- */

cgm_result
cgm_BEGIN_METAFILE(cgm_state * st, const char *str, uint len)
{
    OP(BEGIN_METAFILE);
    S(str, len);
    DONE;
}

cgm_result
cgm_set_metafile_elements(cgm_state * st, const cgm_metafile_elements * meta, long mask)
{
    if ((mask & cgm_set_METAFILE_VERSION)) {
	OP(METAFILE_VERSION);
	I(meta->metafile_version);
	END_OP;
	st->metafile.metafile_version = meta->metafile_version;
    }
    if ((mask & cgm_set_METAFILE_DESCRIPTION)) {
	OP(METAFILE_DESCRIPTION);
	S(meta->metafile_description.chars, meta->metafile_description.length);
	END_OP;
	st->metafile.metafile_description = meta->metafile_description;
    }
    if ((mask & cgm_set_VDC_TYPE)) {
	OP(VDC_TYPE);
	E(meta->vdc_type);
	END_OP;
	st->metafile.vdc_type = meta->vdc_type;
    }
    if ((mask & cgm_set_INTEGER_PRECISION)) {
	OP(INTEGER_PRECISION);
	I(meta->integer_precision);
	END_OP;
	st->metafile.integer_precision = meta->integer_precision;
    }
    if ((mask & cgm_set_REAL_PRECISION)) {
	OP(REAL_PRECISION);
	put_real_precision(st, &meta->real_precision);
	END_OP;
	st->metafile.real_precision = meta->real_precision;
    }
    if ((mask & cgm_set_INDEX_PRECISION)) {
	OP(INDEX_PRECISION);
	I(meta->index_precision);
	END_OP;
	st->metafile.index_precision = meta->index_precision;
    }
    if ((mask & cgm_set_COLOR_PRECISION)) {
	OP(COLOR_PRECISION);
	I(meta->color_precision);
	END_OP;
	st->metafile.color_index_precision = meta->color_index_precision;
    }
    if ((mask & cgm_set_COLOR_INDEX_PRECISION)) {
	OP(COLOR_INDEX_PRECISION);
	I(meta->color_index_precision);
	END_OP;
	st->metafile.color_index_precision = meta->color_index_precision;
    }
    if ((mask & cgm_set_MAXIMUM_COLOR_INDEX)) {
	OP(MAXIMUM_COLOR_INDEX);
	CI(meta->maximum_color_index);
	END_OP;
	st->metafile.maximum_color_index = meta->maximum_color_index;
    }
    if ((mask & cgm_set_METAFILE_ELEMENT_LIST)) {
	int i;
	const int *p;

	OP(METAFILE_ELEMENT_LIST);
	for (i = 0, p = meta->metafile_element_list;
	     i < meta->metafile_element_list_count;
	     i++, p += 2
	    ) {
	    I(p[0]);
	    I(p[1]);
	}
	END_OP;
	st->metafile.metafile_element_list =
	    meta->metafile_element_list;
	st->metafile.metafile_element_list_count =
	    meta->metafile_element_list_count;
    }
    /* element list */
    if ((mask & cgm_set_FONT_LIST)) {
	int i;

	OP(FONT_LIST);
	for (i = 0; i < meta->font_list_count; ++i)
	    S(meta->font_list[i].chars, meta->font_list[i].length);
	END_OP;
	st->metafile.font_list = meta->font_list;
	st->metafile.font_list_count = meta->font_list_count;
    }
    /* character set list */
    /* character coding announcer */
    return st->result;
}

cgm_result
cgm_END_METAFILE(cgm_state * st)
{
    OP(END_METAFILE);
    DONE;
}

/* ---------------- Picture elements ---------------- */

cgm_result
cgm_BEGIN_PICTURE(cgm_state * st, const char *str, uint len)
{
    OP(BEGIN_PICTURE);
    S(str, len);
    DONE;
}

cgm_result
cgm_set_picture_elements(cgm_state * st, const cgm_picture_elements * pic, long mask)
{
    if ((mask & cgm_set_SCALING_MODE)) {
	OP(SCALING_MODE);
	E(pic->scaling_mode);
	R(pic->scale_factor);
	st->picture.scaling_mode = pic->scaling_mode;
	st->picture.scale_factor = pic->scale_factor;
	END_OP;
    }
    if ((mask & cgm_set_COLOR_SELECTION_MODE)) {
	OP(COLOR_SELECTION_MODE);
	E(pic->color_selection_mode);
	END_OP;
	st->picture.color_selection_mode = pic->color_selection_mode;
    }
    if ((mask & cgm_set_LINE_WIDTH_SPECIFICATION_MODE)) {
	OP(LINE_WIDTH_SPECIFICATION_MODE);
	E(pic->line_width_specification_mode);
	END_OP;
	st->picture.line_width_specification_mode = pic->line_width_specification_mode;
    }
    if ((mask & cgm_set_MARKER_SIZE_SPECIFICATION_MODE)) {
	OP(MARKER_SIZE_SPECIFICATION_MODE);
	E(pic->marker_size_specification_mode);
	END_OP;
	st->picture.marker_size_specification_mode = pic->marker_size_specification_mode;
    }
    if ((mask & cgm_set_EDGE_WIDTH_SPECIFICATION_MODE)) {
	OP(EDGE_WIDTH_SPECIFICATION_MODE);
	E(pic->edge_width_specification_mode);
	END_OP;
	st->picture.edge_width_specification_mode = pic->edge_width_specification_mode;
    }
    if ((mask & cgm_set_VDC_EXTENT)) {
	OP(VDC_EXTENT);
	P(&pic->vdc_extent[0]);
	P(&pic->vdc_extent[1]);
	END_OP;
	st->picture.vdc_extent[0] = pic->vdc_extent[0];
	st->picture.vdc_extent[1] = pic->vdc_extent[1];
    }
    if ((mask & cgm_set_BACKGROUND_COLOR)) {
	OP(BACKGROUND_COLOR);
	CD(&pic->background_color.rgb);
	DONE;
	st->picture.background_color = pic->background_color;
    }
    return st->result;
}

cgm_result
cgm_BEGIN_PICTURE_BODY(cgm_state * st)
{
    OP(BEGIN_PICTURE_BODY);
    DONE;
}

cgm_result
cgm_END_PICTURE(cgm_state * st)
{
    OP(END_PICTURE);
    DONE;
}

/* ---------------- Control elements ---------------- */

cgm_result
cgm_VDC_INTEGER_PRECISION(cgm_state * st, int precision)
{
    if (st->vdc_integer_precision != precision) {
	OP(VDC_INTEGER_PRECISION);
	I(precision);
	st->vdc_integer_precision = precision;
	DONE;
    } else
	return cgm_result_ok;
}

cgm_result
cgm_VDC_REAL_PRECISION(cgm_state * st, const cgm_precision * precision)
{
    OP(VDC_REAL_PRECISION);
    put_real_precision(st, precision);
    st->vdc_real_precision = *precision;
    DONE;
}

cgm_result
cgm_AUXILIARY_COLOR(cgm_state * st, const cgm_color * color)
{
    OP(AUXILIARY_COLOR);
    CO(color);
    st->auxiliary_color = *color;
    DONE;
}

cgm_result
cgm_TRANSPARENCY(cgm_state * st, cgm_transparency transparency)
{
    OP(TRANSPARENCY);
    E(transparency);
    st->transparency = transparency;
    DONE;
}

cgm_result
cgm_CLIP_RECTANGLE(cgm_state * st, const cgm_point rectangle[2])
{
    OP(CLIP_RECTANGLE);
    P(&rectangle[0]);
    st->clip_rectangle[0] = rectangle[0];
    P(&rectangle[1]);
    st->clip_rectangle[1] = rectangle[1];
    DONE;
}

cgm_result
cgm_CLIP_INDICATOR(cgm_state * st, cgm_clip_indicator clip)
{
    OP(CLIP_INDICATOR);
    E(clip);
    st->clip_indicator = clip;
    DONE;
}

/* ---------------- Graphical primitive elements ---------------- */

cgm_result
cgm_POLYLINE(cgm_state * st, const cgm_point * vertices, int count)
{
    OP(POLYLINE);
    nP(vertices, count);
    DONE;
}

cgm_result
cgm_DISJOINT_POLYLINE(cgm_state * st, const cgm_point * endpoints, int count)
{
    OP(DISJOINT_POLYLINE);
    nP(endpoints, count);
    DONE;
}

cgm_result
cgm_POLYMARKER(cgm_state * st, const cgm_point * positions, int count)
{
    OP(POLYMARKER);
    nP(positions, count);
    DONE;
}

cgm_result
cgm_TEXT(cgm_state * st, const cgm_point * position, bool final, const char *str, uint len)
{
    OP(TEXT);
    P(position);
    E(final);
    S(str, len);
    DONE;
}

cgm_result
cgm_RESTRICTED_TEXT(cgm_state * st, const cgm_vdc * delta_width, const cgm_vdc * delta_height, const cgm_point * position, bool final, const char *str, uint len)
{
    OP(RESTRICTED_TEXT);
    VDC2(delta_width, delta_height);
    P(position);
    E(final);
    S(str, len);
    DONE;
}

cgm_result
cgm_APPEND_TEXT(cgm_state * st, bool final, const char *str, uint len)
{
    OP(APPEND_TEXT);
    E(final);
    S(str, len);
    DONE;
}

cgm_result
cgm_POLYGON(cgm_state * st, const cgm_point * vertices, int count)
{
    OP(POLYGON);
    nP(vertices, count);
    DONE;
}

cgm_result
cgm_POLYGON_SET(cgm_state * st, const cgm_polygon_edge * vertices, int count)
{
    int i;

    OP(POLYGON);
    for (i = 0; i < count; ++i) {
	P(&vertices[i].vertex);
	E(vertices[i].edge_out);
    }
    DONE;
}

cgm_result
cgm_CELL_ARRAY(cgm_state * st, const cgm_point * pqr /*[3] */ , cgm_int nx, cgm_int ny, cgm_int local_color_precision, cgm_cell_representation_mode mode, const byte * values, uint source_bit, uint raster)
{
    int precision = local_color_precision;
    int bits_per_pixel;
    uint row_bytes;
    const byte *row = values + (source_bit >> 3);
    int bit = source_bit & 7;
    int y;

    /* Currently we ignore the cell representation_mode, and always */
    /* produce cell arrays in 'packed' format. */
    mode = cgm_cell_mode_packed;
    OP(CELL_ARRAY);
    nP(pqr, 3);
    I(nx);
    I(ny);
    I(local_color_precision);
    E(mode);
    if (precision == 0)
	precision = (st->picture.color_selection_mode ==
		     cgm_color_selection_indexed ?
		     st->metafile.color_index_precision :
		     st->metafile.color_precision);
    bits_per_pixel =
	(st->picture.color_selection_mode == cgm_color_selection_indexed ?
	 precision : precision * 3);
    row_bytes = (bits_per_pixel * nx + 7) >> 3;
    for (y = 0; y < ny; y++, row += raster) {
	if (bit == 0)
	    put_bytes(st, row, row_bytes);
	else {
	    uint i;

	    for (i = 0; i < row_bytes; i++) {
		byte b = (row[i] << bit) +
		(row[i + 1] >> (8 - bit));

		put_byte(st, b);
	    }
	}
	if ((row_bytes & 1)) {
	    put_byte(st, 0);
	}
    }
    DONE;
}

cgm_result
cgm_RECTANGLE(cgm_state * st, const cgm_point * corner1, const cgm_point * corner2)
{
    OP(RECTANGLE);
    P(corner1);
    P(corner2);
    DONE;
}

cgm_result
cgm_CIRCLE(cgm_state * st, const cgm_point * center, const cgm_vdc * radius)
{
    OP(CIRCLE);
    P(center);
    VDC(radius);
    DONE;
}

cgm_result
cgm_CIRCULAR_ARC_3_POINT(cgm_state * st, const cgm_point * start, const cgm_point * intermediate, const cgm_point * end)
{
    OP(CIRCULAR_ARC_3_POINT);
    P(start);
    P(intermediate);
    P(end);
    DONE;
}

cgm_result
cgm_CIRCULAR_ARC_3_POINT_CLOSE(cgm_state * st, const cgm_point * start, const cgm_point * intermediate, const cgm_point * end, cgm_arc_closure closure)
{
    OP(CIRCULAR_ARC_3_POINT_CLOSE);
    P(start);
    P(intermediate);
    P(end);
    E(closure);
    DONE;
}

cgm_result
cgm_CIRCULAR_ARC_CENTER(cgm_state * st, const cgm_point * center, const cgm_vdc * dx_start, const cgm_vdc * dy_start, const cgm_vdc * dx_end, const cgm_vdc * dy_end, const cgm_vdc * radius)
{
    OP(CIRCULAR_ARC_CENTER);
    P(center);
    VDC4(dx_start, dy_start, dx_end, dy_end);
    VDC(radius);
    DONE;
}

cgm_result
cgm_CIRCULAR_ARC_CENTER_CLOSE(cgm_state * st, const cgm_point * center, const cgm_vdc * dx_start, const cgm_vdc * dy_start, const cgm_vdc * dx_end, const cgm_vdc * dy_end, const cgm_vdc * radius, cgm_arc_closure closure)
{
    OP(CIRCULAR_ARC_CENTER_CLOSE);
    P(center);
    VDC4(dx_start, dy_start, dx_end, dy_end);
    VDC(radius);
    E(closure);
    DONE;
}

cgm_result
cgm_ELLIPSE(cgm_state * st, const cgm_point * center, const cgm_point * cd1_end, const cgm_point * cd2_end)
{
    OP(ELLIPSE);
    P(center);
    P(cd1_end);
    P(cd2_end);
    DONE;
}

cgm_result
cgm_ELLIPTICAL_ARC(cgm_state * st, const cgm_point * center, const cgm_point * cd1_end, const cgm_point * cd2_end, const cgm_vdc * dx_start, const cgm_vdc * dy_start, const cgm_vdc * dx_end, const cgm_vdc * dy_end)
{
    OP(ELLIPTICAL_ARC);
    P(center);
    P(cd1_end);
    P(cd2_end);
    VDC4(dx_start, dy_start, dx_end, dy_end);
    DONE;
}

cgm_result
cgm_ELLIPTICAL_ARC_CLOSE(cgm_state * st, const cgm_point * center, const cgm_point * cd1_end, const cgm_point * cd2_end, const cgm_vdc * dx_start, const cgm_vdc * dy_start, const cgm_vdc * dx_end, const cgm_vdc * dy_end, cgm_arc_closure closure)
{
    OP(ELLIPTICAL_ARC_CLOSE);
    P(center);
    P(cd1_end);
    P(cd2_end);
    VDC4(dx_start, dy_start, dx_end, dy_end);
    E(closure);
    DONE;
}

/* ---------------- Attribute elements ---------------- */

cgm_result
cgm_LINE_BUNDLE_INDEX(cgm_state * st, cgm_int index)
{
    OP(LINE_BUNDLE_INDEX);
    IX(index);
    st->line_bundle_index = index;
    DONE;
}

cgm_result
cgm_LINE_TYPE(cgm_state * st, cgm_line_type line_type)
{
    OP(LINE_TYPE);
    IX((int)line_type);
    st->line_type = line_type;
    DONE;
}

cgm_result
cgm_LINE_WIDTH(cgm_state * st, const cgm_line_width * line_width)
{
    OP(LINE_WIDTH);
    VDC_R(line_width, st->picture.line_width_specification_mode);
    st->line_width = *line_width;
    DONE;
}

cgm_result
cgm_LINE_COLOR(cgm_state * st, const cgm_color * color)
{
    OP(LINE_COLOR);
    CO(color);
    st->line_color = *color;
    DONE;
}

cgm_result
cgm_MARKER_BUNDLE_INDEX(cgm_state * st, cgm_int index)
{
    OP(MARKER_BUNDLE_INDEX);
    IX(index);
    st->marker_bundle_index = index;
    DONE;
}

cgm_result
cgm_MARKER_TYPE(cgm_state * st, cgm_marker_type marker_type)
{
    OP(MARKER_TYPE);
    IX((int)marker_type);
    st->marker_type = marker_type;
    DONE;
}

cgm_result
cgm_MARKER_SIZE(cgm_state * st, const cgm_marker_size * marker_size)
{
    OP(MARKER_SIZE);
    VDC_R(marker_size, st->picture.marker_size_specification_mode);
    st->marker_size = *marker_size;
    DONE;
}

cgm_result
cgm_MARKER_COLOR(cgm_state * st, const cgm_color * color)
{
    OP(MARKER_COLOR);
    CO(color);
    st->marker_color = *color;
    DONE;
}

cgm_result
cgm_TEXT_BUNDLE_INDEX(cgm_state * st, cgm_int index)
{
    OP(TEXT_BUNDLE_INDEX);
    IX(index);
    st->text_bundle_index = index;
    DONE;
}

cgm_result
cgm_TEXT_FONT_INDEX(cgm_state * st, cgm_int index)
{
    OP(TEXT_FONT_INDEX);
    IX(index);
    st->text_font_index = index;
    DONE;
}

cgm_result
cgm_TEXT_PRECISION(cgm_state * st, cgm_text_precision precision)
{
    OP(TEXT_PRECISION);
    E(precision);
    st->text_precision = precision;
    DONE;
}

cgm_result
cgm_CHARACTER_EXPANSION_FACTOR(cgm_state * st, cgm_real factor)
{
    OP(CHARACTER_EXPANSION_FACTOR);
    R(factor);
    st->character_expansion_factor = factor;
    DONE;
}

cgm_result
cgm_CHARACTER_SPACING(cgm_state * st, cgm_real spacing)
{
    OP(CHARACTER_SPACING);
    R(spacing);
    st->character_spacing = spacing;
    DONE;
}

cgm_result
cgm_TEXT_COLOR(cgm_state * st, const cgm_color * color)
{
    OP(TEXT_COLOR);
    CO(color);
    st->text_color = *color;
    DONE;
}

cgm_result
cgm_CHARACTER_HEIGHT(cgm_state * st, const cgm_vdc * height)
{
    OP(CHARACTER_HEIGHT);
    VDC(height);
    st->character_height = *height;
    DONE;
}

cgm_result
cgm_CHARACTER_ORIENTATION(cgm_state * st, const cgm_vdc * x_up, const cgm_vdc * y_up, const cgm_vdc * x_base, const cgm_vdc * y_base)
{
    OP(CHARACTER_ORIENTATION);
    VDC4(x_up, y_up, x_base, y_base);
    st->character_orientation[0] = *x_up;
    st->character_orientation[1] = *y_up;
    st->character_orientation[2] = *x_base;
    st->character_orientation[3] = *y_base;
    DONE;
}

cgm_result
cgm_TEXT_PATH(cgm_state * st, cgm_text_path text_path)
{
    OP(TEXT_PATH);
    E(text_path);
    st->text_path = text_path;
    DONE;
}

cgm_result
cgm_TEXT_ALIGNMENT(cgm_state * st, cgm_text_alignment_horizontal align_h, cgm_text_alignment_vertical align_v, cgm_real align_cont_h, cgm_real align_cont_v)
{
    OP(TEXT_ALIGNMENT);
    E(align_h);
    E(align_v);
    R(align_cont_h);
    R(align_cont_v);
    DONE;
}

cgm_result
cgm_CHARACTER_SET_INDEX(cgm_state * st, cgm_int index)
{
    OP(CHARACTER_SET_INDEX);
    IX(index);
    st->character_set_index = index;
    DONE;
}

/* See gdevcgml.c for why this isn't named cgm_ALTERNATE_.... */
cgm_result
cgm_ALT_CHARACTER_SET_INDEX(cgm_state * st, cgm_int index)
{
    OP(ALTERNATE_CHARACTER_SET_INDEX);
    IX(index);
    st->alternate_character_set_index = index;
    DONE;
}

cgm_result
cgm_FILL_BUNDLE_INDEX(cgm_state * st, cgm_int index)
{
    OP(FILL_BUNDLE_INDEX);
    IX(index);
    st->fill_bundle_index = index;
    DONE;
}

cgm_result
cgm_INTERIOR_STYLE(cgm_state * st, cgm_interior_style interior_style)
{
    OP(INTERIOR_STYLE);
    E(interior_style);
    st->interior_style = interior_style;
    DONE;
}

cgm_result
cgm_FILL_COLOR(cgm_state * st, const cgm_color * color)
{
    OP(FILL_COLOR);
    CO(color);
    st->fill_color = *color;
    DONE;
}

cgm_result
cgm_HATCH_INDEX(cgm_state * st, cgm_hatch_index hatch_index)
{
    OP(HATCH_INDEX);
    IX((int)hatch_index);
    st->hatch_index = hatch_index;
    DONE;
}

cgm_result
cgm_PATTERN_INDEX(cgm_state * st, cgm_int index)
{
    OP(PATTERN_INDEX);
    IX(index);
    st->pattern_index = index;
    DONE;
}

cgm_result
cgm_EDGE_BUNDLE_INDEX(cgm_state * st, cgm_int index)
{
    OP(EDGE_BUNDLE_INDEX);
    IX(index);
    st->edge_bundle_index = index;
    DONE;
}

cgm_result
cgm_EDGE_TYPE(cgm_state * st, cgm_edge_type edge_type)
{
    OP(EDGE_TYPE);
    IX((int)edge_type);
    st->edge_type = edge_type;
    DONE;
}

cgm_result
cgm_EDGE_WIDTH(cgm_state * st, const cgm_edge_width * edge_width)
{
    OP(EDGE_WIDTH);
    VDC_R(edge_width, st->picture.edge_width_specification_mode);
    st->edge_width = *edge_width;
    DONE;
}

cgm_result
cgm_EDGE_COLOR(cgm_state * st, const cgm_color * color)
{
    OP(EDGE_COLOR);
    CO(color);
    DONE;
}

cgm_result
cgm_EDGE_VISIBILITY(cgm_state * st, bool visibility)
{
    OP(EDGE_VISIBILITY);
    E(visibility);
    st->edge_visibility = visibility;
    DONE;
}

cgm_result
cgm_FILL_REFERENCE_POINT(cgm_state * st, const cgm_point * reference_point)
{
    OP(FILL_REFERENCE_POINT);
    P(reference_point);
    st->fill_reference_point = *reference_point;
    DONE;
}

/* PATTERN_TABLE */

cgm_result
cgm_PATTERN_SIZE(cgm_state * st, const cgm_vdc * x_height, const cgm_vdc * y_height, const cgm_vdc * x_width, const cgm_vdc * y_width)
{
    OP(PATTERN_SIZE);
    VDC4(x_height, y_height, x_width, y_width);
    st->pattern_size[0] = *x_height;
    st->pattern_size[1] = *y_height;
    st->pattern_size[2] = *x_width;
    st->pattern_size[3] = *y_width;
    DONE;
}

cgm_result
cgm_COLOR_TABLE(cgm_state * st, cgm_int index, const cgm_color * values, int count)
{
    int i;

    OP(COLOR_TABLE);
    CI(index);
    for (i = 0; i < count; ++i)
	CD(&values[i].rgb);
    DONE;
}

cgm_result 
cgm_ASPECT_SOURCE_FLAGS(cgm_state * st, const cgm_aspect_source_flag * flags, int count)
{
    int i;

    OP(ASPECT_SOURCE_FLAGS);
    for (i = 0; i < count; ++i) {
	E(flags[i].type);
	E(flags[i].source);
	st->source_flags[flags[i].type] = (byte) flags[i].source;
    }
    DONE;
}

/* ================ Internal routines ================ */

/* Begin a command. */
static void
begin_command(cgm_state * st, cgm_op_index op)
{
    uint op_word = (uint) op << cgm_op_id_shift;

    st->command[0] = (byte) (op_word >> 8);
    st->command[1] = (byte) (op_word);
    st->command_count = 4;	/* leave room for extension */
    st->command_first = true;
    st->result = cgm_result_ok;
}

/* Write the buffer for a partial command. */
/* Note that we always write an even number of bytes. */
static void
write_command(cgm_state * st, bool last)
{
    byte *command = st->command;
    int count = st->command_count;

    if (st->command_first) {
	if (count <= 34) {
	    command[2] = command[0];
	    command[3] = command[1] + count - 4;
	    command += 2, count -= 2;
	} else {
	    int pcount = count - 4;

	    command[1] |= 31;
	    command[2] = (byte) (pcount >> 8);
	    if (!last)
		command[2] |= 0x80;
	    command[3] = (byte) pcount;
	}
	st->command_first = false;
    } else {
	int pcount = count - 2;

	command[0] = (byte) (pcount >> 8);
	if (!last)
	    command[0] |= 0x80;
	command[1] = (byte) pcount;
    }
    fwrite(command, sizeof(byte), count + (count & 1), st->file);
    st->command_count = 2;	/* leave room for extension header */
    if (ferror(st->file))
	st->result = cgm_result_io_error;
}

/* End a command. */
static cgm_result
end_command(cgm_state * st)
{
    write_command(st, true);
    return st->result;
}

/* Put an integer value. */
static void
put_int(cgm_state * st, cgm_int value, int precision)
{
    switch (precision) {
	case 32:
	    put_byte(st, value >> 24);
	case 24:
	    put_byte(st, value >> 16);
	case 16:
	    put_byte(st, value >> 8);
	case 8:
	    put_byte(st, value);
    }
}

/* Put a real value. */
static void
put_real(cgm_state * st, cgm_real value, const cgm_precision * pr)
{
    if (pr->representation == cgm_representation_floating) {
    } else {			/* Casting to integer simply discards the fraction, */
	/* so we need to be careful with negative values. */
	long whole = (long)value;
	double fpart;

	if (value < whole)
	    --whole;
	fpart = value - whole;
	put_int(st, whole, pr->exponent_or_whole_width);
	if (pr->fraction_width == 16) {
	    uint fraction = (uint) (fpart * (1.0 * 0x10000));

	    put_int(st, fraction, 16);
	} else {		/* pr->fraction_width == 32 */
	    ulong fraction =
	    (ulong) (fpart * (1.0 * 0x10000 * 0x10000));

	    put_int(st, fraction, 32);
	}
    }
}

/* Put a real precision. */
static void
put_real_precision(cgm_state * st, const cgm_precision * precision)
{
    I((int)precision->representation);
    I(precision->exponent_or_whole_width);
    I(precision->fraction_width);
}

/* Put a VDC. */
static void
put_vdc(cgm_state * st, const cgm_vdc * pvdc)
{
    if (st->metafile.vdc_type == cgm_vdc_integer)
	put_int(st, pvdc->integer, st->vdc_integer_precision);
    else
	put_real(st, pvdc->real, &st->vdc_real_precision);
}

/* Put a VDC or a real. */
static void
put_vdc_r(cgm_state * st, const cgm_line_marker_extent * extent,
	  cgm_line_marker_specification_mode mode)
{
    if (mode == cgm_line_marker_absolute)
	VDC(&extent->absolute);
    else
	R(extent->scaled);
}

/* Put a point (pair of VDCs). */
static void
put_point(cgm_state * st, const cgm_point * ppt)
{
    if (st->metafile.vdc_type == cgm_vdc_integer) {
	put_int(st, ppt->integer.x, st->vdc_integer_precision);
	put_int(st, ppt->integer.y, st->vdc_integer_precision);
    } else {
	put_real(st, ppt->real.x, &st->vdc_real_precision);
	put_real(st, ppt->real.y, &st->vdc_real_precision);
    }
}

/* Put a list of points. */
static void
put_points(cgm_state * st, const cgm_point * ppt, int count)
{
    int i;

    for (i = 0; i < count; i++)
	P(ppt + i);
}

/* Put bytes. */
static void
put_bytes(cgm_state * st, const byte * data, uint length)
{
    int count;

    while (length > (count = command_max_count - st->command_count)) {
	memcpy(st->command + st->command_count, data, count);
	st->command_count += count;
	write_command(st, false);
	data += count;
	length -= count;
    }
    memcpy(st->command + st->command_count, data, length);
    st->command_count += length;
}

/* Put a string. */
static void
put_string(cgm_state * st, const char *data, uint length)
{				/* The CGM specification seems to imply that the continuation */
    /* mechanism for commands and the mechanism for strings */
    /* are orthogonal; we take this interpretation. */
    if (length >= 255) {
	put_byte(st, 255);
	while (length > 32767) {
	    put_int(st, 65535, 2);
	    put_bytes(st, (const byte *)data, 32767);
	    data += 32767;
	    length -= 32767;
	}
    }
    put_byte(st, length);
    put_bytes(st, (const byte *)data, length);
}

/* Put a color. */
static void
put_color(cgm_state * st, const cgm_color * color)
{
    if (st->picture.color_selection_mode == cgm_color_selection_indexed)
	CI(color->index);
    else
	CD(&color->rgb);
}

/* Put an RGB value. */
static void
put_rgb(cgm_state * st, const cgm_rgb * rgb)
{
    put_int(st, rgb->r, st->metafile.color_precision);
    put_int(st, rgb->g, st->metafile.color_precision);
    put_int(st, rgb->b, st->metafile.color_precision);
}