File: cgm_parse.py

package info (click to toggle)
inkscape 1.4.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 402,900 kB
  • sloc: cpp: 547,256; python: 72,677; ansic: 63,355; javascript: 3,864; xml: 2,345; sh: 1,667; makefile: 824; perl: 614
file content (1293 lines) | stat: -rw-r--r-- 35,006 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
#!/usr/bin/env python3
# coding=utf-8
#
# Copyright (c) 2024 jonathan.neuhauser@outlook.com
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
"""
Parse binary CGM files
"""

from dataclasses import dataclass, fields, is_dataclass
from io import BufferedIOBase, BytesIO
import struct
from typing import (
    ClassVar,
    Dict,
    List,
    Optional,
    Tuple,
    Type,
    Union,
    get_type_hints,
    get_origin,
    get_args,
)
import warnings
import cgm_enums
import inkex


class FunctionRegistry(dict):
    def register(self, key):
        def decorator(func):
            self[key] = func
            return func

        return decorator


class BinaryCGMCommandParser:
    type_registry: Dict[Tuple[int, int], Type] = {}
    unsigned_readers = FunctionRegistry()
    signed_readers = FunctionRegistry()
    real_readers = FunctionRegistry()
    readers = FunctionRegistry()
    sizers = FunctionRegistry()

    def __init__(self, data: Union[bytes, BufferedIOBase]):
        if isinstance(data, bytes):
            data = BytesIO(data)
        # The stream we currently operate on. For long form commands its
        # easier to temporarily exchange this reference
        self.stream = data
        # The main stream of the file
        self.outer_stream = data
        # Reader functions for basic data types, where precision can be changed
        # in the file.
        self.integer_precision = 16
        self.index_precision = 16
        self.real_type = cgm_enums.RealTypeEnum.FIXED_POINT
        self.real_precision = (16, 16)
        self.vdc_integer_precision = 16
        self.vdc_real_type = cgm_enums.RealTypeEnum.FIXED_POINT
        self.vdc_real_precision = (16, 16)
        self.float_precision = (9, 23)
        self.colour_component_precision = 8
        self.colour_index_precision = 8
        self.vdc_type = cgm_enums.VDCTypeEnum.INTEGER
        self.vdc_real_type = cgm_enums.RealTypeEnum.FIXED_POINT
        self.vdc_real_precision = (16, 16)
        self.colour_model = cgm_enums.ColourModelEnum.RGB
        self.colour_selection_mode = cgm_enums.ColourSelectionModeEnum.INDEXED
        self.line_width_specification_mode = cgm_enums.WidthSpecificationModeEnum.SCALED
        self.marker_size_specification_mode = (
            cgm_enums.WidthSpecificationModeEnum.SCALED
        )
        self.edge_width_specification_mode = cgm_enums.WidthSpecificationModeEnum.SCALED
        self.interior_style_specification_mode = (
            cgm_enums.WidthSpecificationModeEnum.ABSOLUTE
        )

    @staticmethod
    def register_target_datatype(cls):
        if cls.id is not None:
            BinaryCGMCommandParser.type_registry[cls.id] = cls

    def read(self, bytes):
        res = self.stream.read(bytes)
        if len(res) != bytes:
            raise ValueError("EOF")
        return res

    def read_padded(self, length):
        # An uneven length of a command is padded with a null byte
        if length % 2 == 1:
            arg_bytes = self.read(length + 1)
            assert arg_bytes[-1] == 0
            return arg_bytes[:-1]
        return self.read(length)

    @unsigned_readers.register(8)
    def read_u8(self) -> int:
        return int.from_bytes(self.read(1), byteorder="big")

    @unsigned_readers.register(16)
    def read_u16(self) -> int:
        return int.from_bytes(self.read(2), byteorder="big")

    @unsigned_readers.register(24)
    def read_u24(self) -> int:
        return int.from_bytes(self.read(3), byteorder="big")

    @unsigned_readers.register(32)
    def read_u32(self) -> int:
        return int.from_bytes(self.read(4), byteorder="big")

    @signed_readers.register(8)
    def read_i8(self) -> int:
        return int.from_bytes(self.read(1), byteorder="big", signed=True)

    @signed_readers.register(16)
    @readers.register("E")
    def read_i16(self) -> int:
        return int.from_bytes(self.read(2), byteorder="big", signed=True)

    @signed_readers.register(24)
    def read_i24(self) -> int:
        return int.from_bytes(self.read(3), byteorder="big", signed=True)

    @signed_readers.register(32)
    def read_i32(self) -> int:
        return int.from_bytes(self.read(4), byteorder="big", signed=True)

    @real_readers.register((cgm_enums.RealTypeEnum.FLOATING_POINT, 9, 23))
    def read_float(self) -> float:
        return struct.unpack(">f", self.read(4))[0]

    @real_readers.register((cgm_enums.RealTypeEnum.FLOATING_POINT, 12, 52))
    def read_double(self) -> float:
        return struct.unpack(">d", self.read(8))[0]

    @real_readers.register((cgm_enums.RealTypeEnum.FIXED_POINT, 16, 16))
    def read_fixed_single(self) -> float:
        # Some sample files appear to not be using
        # two's complement for signed integers if they occur in a flot.
        # IMO that's incorrect according to the specification.
        return self.read_i16() + self.read_u16() / 2**16

    @real_readers.register((cgm_enums.RealTypeEnum.FIXED_POINT, 32, 32))
    def read_fixed_double(self) -> float:
        return self.read_i32() + self.read_u32() / 2**32

    @readers.register("S")
    @readers.register("D")
    @readers.register("SF")
    def read_str(self) -> str:
        """The first byte indicates the size.
        If the size is 255, the first two bytes indicate the size.
        From the new size, the first bit is a continuation flag
        and the remaining 15 bits are the size"""
        data: bytes
        size = self.read_u8()
        if size != 0xFF:
            data = self.read(size)
        else:
            # Long-form string
            self.stream.seek(0)
            _data = bytearray()
            while True:
                cur_size = self.read_u16()
                length = cur_size & 0b0111111111111111
                _data.extend(self.read(length))
                if not cur_size & 0b1000000000000000:
                    break
            data = bytes(_data)
        # TODO implement proper string encoding
        return data.decode("latin-1")

    def get_single_value(self, type_str: Union[str, Tuple[str]]):
        if type_str in self.readers:
            return BinaryCGMCommandParser.readers[type_str](self)
        else:
            raise ValueError(f"Unknown data type {type_str}")

    def get_value(
        self,
        type_str: Union[str, Tuple[str, ...]],
        target_types: Union[type, Tuple[type]],
        length: Optional[int] = None,
    ):
        """This function reads a value from the stream.
        The result can either be:
         - a primitive type (e.g. "I", <type int> -> int)
         - a tuple of primitive types
           (e.g. ("I", "P"), (<type int>, <type Point>) -> (int, Point))
         - a dataclass
           (e.g. ("P", "E"), <type PolygonLineSetEntry> -> PolygonLineSetEntry)
           in this case the type_str applies to the fields of the dataclass,
           and the target types of those fields are inferred from the
           type hint of the dataclass field
         - a list of any of the previous
           (e.g. ("I", "R"), typing.List[<type int>, <type float>]
           -> List[Tuple[int, float]])
           length must be defined for lists
           This just calls get_value with the argument of the List typehint,
           and collects the result until we've read length bytes
        """
        res = None
        initial_pos = self.stream.tell()

        if isinstance(type_str, str) and isinstance(target_types, type):
            if target_types == str and length == 0:
                return ""
            res = self.get_single_value(type_str)
            if type_str not in ("P", "CO", "CD", "CI"):
                res = target_types(res)

        elif isinstance(type_str, tuple):
            if get_origin(target_types) == tuple:
                target_types = get_args(target_types)
            if isinstance(target_types, tuple):
                assert len(target_types) == len(type_str)
                res = tuple(
                    self.get_value(i, t) for t, i in zip(target_types, type_str)
                )

            elif is_dataclass(target_types):
                res = target_types(
                    *self.get_value(
                        type_str,
                        tuple(
                            get_type_hints(target_types)[t.name]
                            for t in fields(target_types)
                        ),
                    )
                )
        if res is None and get_origin(target_types) == list:
            assert length is not None
            target_type = get_args(target_types)[0]
            try:
                field_size = self.get_size(type_str)
                # Assert that the remaining bytes fit into the list without residue
                assert length % field_size == 0
            except ValueError:
                # just an assertion here, continue
                pass
            res = []
            while True:
                if (self.stream.tell() - initial_pos) >= length:
                    break
                res.append(self.get_value(type_str, target_type))

        assert res is not None, "invalid specification for get_value"
        # assert isinstance(res, target_types), "bad return type"
        return res

    @readers.register("IX")
    def read_index(self):
        return BinaryCGMCommandParser.signed_readers[self.index_precision](self)

    @readers.register("SI")
    @readers.register("I")
    def read_signed(self):
        return BinaryCGMCommandParser.signed_readers[self.integer_precision](self)

    @readers.register("VDC")
    def read_vdc(self):
        return (
            BinaryCGMCommandParser.signed_readers[self.vdc_integer_precision](self)
            if self.vdc_type == cgm_enums.VDCTypeEnum.INTEGER
            else BinaryCGMCommandParser.real_readers[
                (self.vdc_real_type, *self.vdc_real_precision)
            ](self)
        )

    @readers.register("R")
    def read_real(self):
        return BinaryCGMCommandParser.real_readers[
            (self.real_type, *self.real_precision)
        ](self)

    @readers.register("F")
    def read_floating(self):
        return BinaryCGMCommandParser.real_readers[
            (cgm_enums.RealTypeEnum.FLOATING_POINT, *self.float_precision)
        ](self)

    @readers.register("CI")
    def read_indexed_colour(self):
        return cgm_enums.IndexColour(
            self.signed_readers[self.colour_index_precision](self)
        )

    @readers.register("CCO")
    def read_colour_component(self):
        return BinaryCGMCommandParser.unsigned_readers[self.colour_component_precision](
            self
        )

    @readers.register("CD")
    def read_direct_colour(self):
        result = cgm_enums.DirectColour(
            *(
                self.read_colour_component()
                for _ in range(
                    4 if self.colour_model == cgm_enums.ColourModelEnum.CMYK else 3
                )
            )
        )
        result.colour_model = self.colour_model
        return result

    @readers.register("CO")
    def read_colour(self):
        if self.colour_selection_mode == cgm_enums.ColourSelectionModeEnum.DIRECT:
            return self.read_direct_colour()
        else:
            return self.read_indexed_colour()

    @readers.register("P")
    def read_point(self):
        return cgm_enums.Point(self.read_vdc(), self.read_vdc())

    def read_size(self, mode: cgm_enums.WidthSpecificationModeEnum):
        if mode == cgm_enums.WidthSpecificationModeEnum.ABSOLUTE:
            return self.read_vdc()
        return self.read_real()

    @readers.register("LSS")
    def read_line_size(self):
        return self.read_size(self.line_width_specification_mode)

    @readers.register("MSS")
    def read_marker_size(self):
        return self.read_size(self.marker_size_specification_mode)

    @readers.register("ESS")
    def read_edge_size(self):
        return self.read_size(self.edge_width_specification_mode)

    @readers.register("ISS")
    def read_interior_style_size(self):
        return self.read_size(self.interior_style_specification_mode)

    # SIZING FUNCTIONS

    def get_size(self, type_str: Union[str, Tuple[str, ...]]):
        if type_str in BinaryCGMCommandParser.sizers:
            return BinaryCGMCommandParser.sizers[type_str](self)
        if isinstance(type_str, tuple):
            return sum(self.get_size(i) for i in type_str)
        else:
            raise ValueError(f"No size know for type {type_str}")

    @sizers.register("CI")
    def ci_size(self):
        return self.colour_index_precision // 8

    @sizers.register("CCO")
    def cco_size(self):
        return self.colour_component_precision // 8

    @sizers.register("CD")
    def cd_size(self):
        return self.cco_size() * (
            4 if self.colour_model == cgm_enums.ColourModelEnum.CMYK else 3
        )

    @sizers.register("IX")
    def ix_size(self):
        return self.index_precision // 8

    @sizers.register("E")
    def e_size(self):
        return 2

    @sizers.register("I")
    def i_size(self):
        return self.integer_precision // 8

    @sizers.register("R")
    def r_size(self):
        return (self.real_precision[0] + self.real_precision[1]) // 8

    @sizers.register("VDC")
    def vdc_size(self):
        return (
            self.vdc_integer_precision // 8
            if self.vdc_type == cgm_enums.VDCTypeEnum.INTEGER
            else (self.vdc_real_precision[0] + self.vdc_real_precision[1]) // 8
        )

    @sizers.register("P")
    def p_size(self):
        return 2 * self.vdc_size()

    @sizers.register("CO")
    def co_size(self):
        if self.colour_selection_mode == cgm_enums.ColourSelectionModeEnum.DIRECT:
            return self.cd_size()
        else:
            return self.ci_size()

    def ss_size(self, mode: cgm_enums.WidthSpecificationModeEnum):
        if mode == cgm_enums.WidthSpecificationModeEnum.ABSOLUTE:
            return self.vdc_size()
        return self.r_size()

    @sizers.register("LSS")
    def lss_size(self):
        return self.ss_size(self.line_width_specification_mode)

    @sizers.register("MSS")
    def marker_size(self):
        return self.ss_size(self.marker_size_specification_mode)

    @sizers.register("ESS")
    def edge_size(self):
        return self.ss_size(self.edge_width_specification_mode)

    @sizers.register("ISS")
    def interior_style_size(self):
        return self.ss_size(self.interior_style_specification_mode)

    def parse(self):
        while True:
            try:
                header = self.read_u16()
            except ValueError:
                return
            clss = header >> 12
            obj_id = (header & 0b0000111111100000) >> 5
            total_length = header & 0x1F

            if total_length == 31:
                # Long-form header
                args = bytearray()
                total_length = 0
                while True:
                    data = self.read_u16()
                    length = data & 0b0111111111111111
                    total_length += length
                    args.extend(self.read_padded(length))
                    if not data & 0b1000000000000000:
                        break
                self.stream = BytesIO(bytes(args))

            old_pos = self.stream.tell()
            interpreted = self.command_factory(clss, obj_id, total_length)
            new_pos = self.stream.tell()
            if (
                new_pos - old_pos not in [total_length] + [total_length - 1]
                if total_length % 2 == 0
                else []
            ):
                warnings.warn(
                    f"{interpreted.__class__.__name__}: Expected to read {total_length} bytes, but read {new_pos - old_pos}"
                )
            new_pos = old_pos + total_length
            self.stream.seek(new_pos)
            if isinstance(interpreted, ParserModifierCommand):
                self.update_readers(interpreted)

            # Prepare the stream for the next entry
            if self.stream == self.outer_stream:
                if new_pos % 2 != 0:
                    # Fields are always aligned with 2-byte words
                    self.read(1)
            else:
                self.stream = self.outer_stream

            yield interpreted

            if isinstance(interpreted, EndMetafile):
                # Sometimes there are trailing characters afterwards, return
                return

    def parse_data(self, data: BytesIO):
        """Temporarily exchanges the stream by data to read primitives from it,
        using the same settings as the current stream"""
        outer_stream = self.outer_stream
        stream = self.stream

        self.outer_stream = data
        self.stream = data
        result = tuple(self.parse())

        self.outer_stream = outer_stream
        self.stream = stream

        return result

    def update_readers(self, interpreted):
        if isinstance(interpreted, VdcType):
            self.vdc_type = interpreted.vdc_type
        elif isinstance(interpreted, IntegerPrecision):
            self.integer_precision = interpreted.integer_precision
        elif isinstance(interpreted, RealPrecision):
            self.real_type = interpreted.representation
            self.real_precision = (
                interpreted.exponent_whole_size,
                interpreted.fraction_size,
            )
            if interpreted.representation == cgm_enums.RealTypeEnum.FLOATING_POINT:
                self.float_precision = (
                    interpreted.exponent_whole_size,
                    interpreted.fraction_size,
                )
        elif isinstance(interpreted, IndexPrecision):
            self.index_precision = interpreted.index_precision
        elif isinstance(interpreted, ColourIndexPrecision):
            self.colour_index_precision = interpreted.colour_index_precision
        elif isinstance(interpreted, ColourPrecision):
            self.colour_component_precision = interpreted.colour_precision
        elif isinstance(interpreted, ColourSelectionMode):
            self.colour_selection_mode = interpreted.colour_selection_mode
        elif isinstance(interpreted, ColourModel):
            self.colour_model = interpreted.colour_model
        elif isinstance(interpreted, VDCIntegerPrecision):
            self.vdc_integer_precision = interpreted.integer_precision
        elif isinstance(interpreted, VDCRealPrecision):
            self.vdc_real_type = interpreted.representation
            self.vdc_real_precision = (
                interpreted.exponent_whole_size,
                interpreted.fraction_size,
            )
        elif isinstance(interpreted, LineWidthSpecificationMode):
            self.line_width_specification_mode = interpreted.size_specification
        elif isinstance(interpreted, EdgeWidthSpecificationMode):
            self.edge_width_specification_mode = interpreted.size_specification
        elif isinstance(interpreted, MarkerSizeSpecificationMode):
            self.marker_size_specification_mode = interpreted.size_specification
        elif isinstance(interpreted, InteriorStyleSpecificationMode):
            self.interior_style_specification_mode = interpreted.size_specification
        else:
            inkex.errormsg(
                f"Parser modifier command {interpreted} without associated action"
            )

    def command_factory(self, group, command, length):
        cls = self.type_registry.get((group, command), UnknownCommand)
        result = cls.parse_binary(self, length)
        if isinstance(result, UnknownCommand):
            result.id = (group, command)
        return result


@dataclass
class CGMCommand:
    id: ClassVar[Tuple[int, int]] = (0, 0)
    binary_params: ClassVar[Tuple[Union[str, Tuple[str, ...]], ...]] = tuple()

    def __init_subclass__(cls, **kwargs):
        super().__init_subclass__(**kwargs)
        BinaryCGMCommandParser.register_target_datatype(cls)

    @classmethod
    def get_binary_args(
        cls,
        stream: BinaryCGMCommandParser,
        length: int,
    ):
        initial_pos = stream.stream.tell()
        for i, (field, types) in enumerate(zip(fields(cls), cls.binary_params)):
            outer_type = get_type_hints(cls)[field.name]
            yield stream.get_value(
                types, outer_type, length - (stream.stream.tell() - initial_pos)
            )

    @classmethod
    def parse_binary(cls, stream: BinaryCGMCommandParser, length):
        args = list(cls.get_binary_args(stream, length))
        return cls(*args)


6


@dataclass
class NoOp(CGMCommand):
    id = (0, 0)
    data: bytes

    @classmethod
    def parse_binary(cls, stream: BinaryCGMCommandParser, length):
        return cls(stream.read(length))


@dataclass
class UnknownCommand(CGMCommand):
    data: bytes

    @classmethod
    def parse_binary(cls, stream: BinaryCGMCommandParser, length):
        return cls(stream.read(length))


@dataclass
class BeginMetafile(CGMCommand):
    id = (0, 1)
    binary_params = ("SF",)
    name: str


@dataclass
class EndMetafile(CGMCommand):
    id = (0, 2)


@dataclass
class BeginPicture(CGMCommand):
    id = (0, 3)
    binary_params = ("SF",)
    name: str


@dataclass
class BeginPictureBody(CGMCommand):
    id = (0, 4)


@dataclass
class EndPicture(CGMCommand):
    id = (0, 5)


# Metafile descriptor elements


@dataclass
class MetafileVersion(CGMCommand):
    id = (1, 1)
    binary_params = ("I",)
    version: int


@dataclass
class MetafileDescription(CGMCommand):
    id = (1, 2)
    binary_params = ("SF",)
    description: str


class ParserModifierCommand(CGMCommand):
    pass


@dataclass
class VdcType(ParserModifierCommand):
    id = (1, 3)
    binary_params = ("E",)
    vdc_type: cgm_enums.VDCTypeEnum


@dataclass
class IntegerPrecision(ParserModifierCommand):
    id = (1, 4)
    binary_params = ("I",)
    integer_precision: int


@dataclass
class RealPrecision(ParserModifierCommand):
    id = (1, 5)
    binary_params = "E", "I", "I"
    representation: cgm_enums.RealTypeEnum
    exponent_whole_size: int
    fraction_size: int


@dataclass
class IndexPrecision(ParserModifierCommand):
    id = (1, 6)
    binary_params = ("I",)
    index_precision: int


@dataclass
class ColourPrecision(ParserModifierCommand):
    id = (1, 7)
    binary_params = ("I",)
    colour_precision: int


@dataclass
class ColourIndexPrecision(ParserModifierCommand):
    id = (1, 8)
    binary_params = ("I",)
    colour_index_precision: int


@dataclass
class MaximumColourIndex(CGMCommand):
    id = (1, 9)
    binary_params = ("CI",)
    maximum_colour_index: cgm_enums.IndexColour


@dataclass
class ColourValueExtent(CGMCommand):
    id = (1, 10)

    minimum: Optional[int]
    maximum: Optional[int]

    first_component: Optional[Tuple[float, float]]
    second_component: Optional[Tuple[float, float]]
    third_component: Optional[Tuple[float, float]]

    @classmethod
    def get_binary_args(cls, stream: BinaryCGMCommandParser, length):
        if stream.colour_model in (
            cgm_enums.ColourModelEnum.CMYK,
            cgm_enums.ColourModelEnum.RGB,
        ):
            return stream.read_direct_colour(), stream.read_direct_colour(), 0, 0, 0
        else:
            return 0, 0, stream.read_real(), stream.read_real(), stream.read_real()


@dataclass
class MetafileElementList(CGMCommand):
    id = (1, 11)
    elements: List[Tuple[int, int]]

    @classmethod
    def get_binary_args(cls, stream: BinaryCGMCommandParser, length: int):
        num = stream.read_signed()
        yield [(stream.read_index(), stream.read_index()) for _ in range(num)]


@dataclass
class MetafileDefaultsReplacement(CGMCommand):
    id = (1, 12)
    data: List[CGMCommand]

    @classmethod
    def get_binary_args(cls, stream: BinaryCGMCommandParser, length: int):
        data = stream.read(length)

        yield stream.parse_data(BytesIO(data))


@dataclass
class FontList(CGMCommand):
    id = (1, 13)
    binary_params = ("SF",)

    fonts: List[str]


@dataclass
class CharacterSetList(CGMCommand):
    id = (1, 14)
    binary_params = (("E", "SF"),)

    specification: List[Tuple[cgm_enums.CharacterSetTypeEnum, str]]


@dataclass
class CharacterCodingAnnouncer(CGMCommand):
    id = (1, 15)
    binary_params = ("E",)

    encoding: cgm_enums.CharacterCodingEnum


@dataclass
class MaximumVDCExtent(CGMCommand):
    id = (1, 17)
    binary_params = "P", "P"

    first_corner: cgm_enums.Point
    second_corner: cgm_enums.Point


@dataclass
class ColourModel(ParserModifierCommand):
    id = (1, 19)
    binary_params = ("IX",)

    colour_model: cgm_enums.ColourModelEnum


@dataclass
class ScalingMode(CGMCommand):
    id = (2, 1)
    binary_params = "E", "F"

    scaling_mode: cgm_enums.ScalingModeEnum
    metric_scaling_factor: float


@dataclass
class ColourSelectionMode(ParserModifierCommand):
    id = (2, 2)
    binary_params = ("E",)

    colour_selection_mode: cgm_enums.ColourSelectionModeEnum


@dataclass
class LineWidthSpecificationMode(ParserModifierCommand):
    id = (2, 3)
    binary_params = ("E",)

    size_specification: cgm_enums.WidthSpecificationModeEnum


@dataclass
class MarkerSizeSpecificationMode(ParserModifierCommand):
    id = (2, 4)

    binary_params = ("E",)
    size_specification: cgm_enums.WidthSpecificationModeEnum


@dataclass
class EdgeWidthSpecificationMode(ParserModifierCommand):
    id = (2, 5)

    binary_params = ("E",)
    size_specification: cgm_enums.WidthSpecificationModeEnum


@dataclass
class VDCExtent(CGMCommand):
    id = (2, 6)
    binary_params = "P", "P"

    first_corner: cgm_enums.Point
    second_corner: cgm_enums.Point


@dataclass
class BackgroundColour(CGMCommand):
    id = (2, 7)
    binary_params = ("CD",)

    colour: cgm_enums.DirectColour


@dataclass
class InteriorStyleSpecificationMode(ParserModifierCommand):
    id = (2, 16)

    binary_params = ("E",)
    size_specification: cgm_enums.WidthSpecificationModeEnum


@dataclass
class VDCIntegerPrecision(ParserModifierCommand):
    id = (3, 1)
    binary_params = ("I",)
    integer_precision: int


@dataclass
class VDCRealPrecision(ParserModifierCommand):
    id = (3, 2)
    binary_params = "E", "I", "I"
    representation: cgm_enums.RealTypeEnum
    exponent_whole_size: int
    fraction_size: int


@dataclass
class Transparency(CGMCommand):
    id = (3, 4)
    binary_params = ("E",)
    indicator: cgm_enums.TransparencyEnum


@dataclass
class ClipRectangle(CGMCommand):
    id = (3, 5)
    binary_params = ("P", "P")
    p1: cgm_enums.Point
    p2: cgm_enums.Point


@dataclass
class ClipIndicator(CGMCommand):
    id = (3, 6)
    binary_params = ("E",)
    clip: cgm_enums.ClipIndicatorEnum


@dataclass
class Polyline(CGMCommand):
    id = (4, 1)
    binary_params = ("P",)
    points: List[cgm_enums.Point]


@dataclass
class DisjointPolyline(CGMCommand):
    id = (4, 2)
    binary_params = ("P",)
    points: List[cgm_enums.Point]


@dataclass
class Polymarker(CGMCommand):
    id = (4, 3)
    binary_params = ("P",)
    points: List[cgm_enums.Point]


@dataclass
class Text(CGMCommand):
    id = (4, 4)
    binary_params = ("P", "E", "S")
    point: cgm_enums.Point
    flag: cgm_enums.TextFinalFlag
    string: str


@dataclass
class RestrictedText(CGMCommand):
    id = (4, 5)
    binary_params = ("VDC", "VDC", "P", "E", "S")
    width: float
    height: float
    point: cgm_enums.Point
    flag: cgm_enums.TextFinalFlag
    string: str


@dataclass
class AppendText(CGMCommand):
    id = (4, 6)
    binary_params = ("E", "S")
    flag: cgm_enums.TextFinalFlag
    string: str


@dataclass
class Polygon(CGMCommand):
    id = (4, 7)
    binary_params = ("P",)
    points: List[cgm_enums.Point]


@dataclass
class PolygonSet(CGMCommand):
    id = (4, 8)
    points: List[cgm_enums.PolygonSetEntry]
    binary_params = (("P", "E"),)


# TODO: Cell array (complicated parsing with sub-byte values)


@dataclass
class GeneralizedDrawingPrimitive(CGMCommand):
    id = (4, 10)
    identifier: int
    points: List[cgm_enums.Point]
    data: bytes

    @classmethod
    def get_binary_args(cls, stream: BinaryCGMCommandParser, length: int):
        yield stream.read_signed()
        num = stream.read_signed()
        points = [stream.read_point() for _ in range(num)]
        yield points
        read = stream.i_size() * 2 + num * stream.p_size()
        yield stream.read(length - read)


@dataclass
class Rectangle(CGMCommand):
    id = (4, 11)
    binary_params = ("P", "P")
    p1: cgm_enums.Point
    p2: cgm_enums.Point


@dataclass
class Circle(CGMCommand):
    id = (4, 12)
    binary_params = ("P", "VDC")
    center: cgm_enums.Point
    radius: float


@dataclass
class CircularArc3Point(CGMCommand):
    id = (4, 13)
    binary_params = ("P", "P", "P")
    p1: cgm_enums.Point
    p2: cgm_enums.Point
    p3: cgm_enums.Point


@dataclass
class CircularArc3PointClose(CircularArc3Point):
    id = (4, 14)
    binary_params = ("P", "P", "P", "E")  # type: ignore
    closure: cgm_enums.ArcClosureEnum


@dataclass
class CircularArcCentre(CGMCommand):
    id = (4, 15)
    binary_params = ("P", "VDC", "VDC", "VDC", "VDC", "VDC")  # type: ignore
    centre: cgm_enums.Point
    delta_x_start: float
    delta_y_start: float
    delta_x_end: float
    delta_y_end: float
    radius: float


@dataclass
class CircularArcCentreClose(CircularArcCentre):
    id = (4, 16)
    binary_params = ("P", "VDC", "VDC", "VDC", "VDC", "VDC", "E")  # type: ignore
    closure: cgm_enums.ArcClosureEnum


@dataclass
class Ellipse(CGMCommand):
    id = (4, 17)
    binary_params = ("P", "P", "P")
    center: cgm_enums.Point
    point_1: cgm_enums.Point
    point_2: cgm_enums.Point


@dataclass
class EllipticalArc(Ellipse):
    id = (4, 18)
    binary_params = ("P", "P", "P", "VDC", "VDC", "VDC", "VDC")  # type: ignore
    delta_x_start: float
    delta_y_start: float
    delta_x_end: float
    delta_y_end: float


@dataclass
class EllipticalArcClose(EllipticalArc):
    id = (4, 19)
    binary_params = ("P", "P", "P", "VDC", "VDC", "VDC", "VDC", "E")  # type: ignore
    closure: cgm_enums.ArcClosureEnum


@dataclass
class Polybezier(CGMCommand):
    id = (4, 26)
    binary_params = ("E", "P")
    continuous: cgm_enums.PolybezierContinuityEnum
    points: List[cgm_enums.Point]


@dataclass
class LineType(CGMCommand):
    id = (5, 2)
    binary_params = ("IX",)
    type: cgm_enums.LineTypeEnum


@dataclass
class LineWidth(CGMCommand):
    id = (5, 3)
    binary_params = ("LSS",)
    width: float


@dataclass
class LineColour(CGMCommand):
    id = (5, 4)
    binary_params = ("CO",)
    colour: cgm_enums.Colour


@dataclass
class MarkerType(CGMCommand):
    id = (5, 6)
    binary_params = ("E",)
    type: cgm_enums.MarkerTypeEnum


@dataclass
class MarkerSize(CGMCommand):
    id = (5, 7)
    binary_params = ("MSS",)
    size: float


@dataclass
class MarkerColour(CGMCommand):
    id = (5, 8)
    binary_params = ("CO",)
    colour: cgm_enums.Colour


@dataclass
class TextFontIndex(CGMCommand):
    id = (5, 10)
    binary_params = ("IX",)
    index: int


@dataclass
class TextPrecision(CGMCommand):
    id = (5, 11)
    binary_params = ("E",)
    text_precision: cgm_enums.TextPrecisionEnum


@dataclass
class CharacterExpansionFactor(CGMCommand):
    id = (5, 12)
    binary_params = ("R",)
    factor: float


@dataclass
class CharacterSpacing(CGMCommand):
    id = (5, 13)
    binary_params = ("R",)
    spacing: float


@dataclass
class TextColour(CGMCommand):
    id = (5, 14)
    binary_params = ("CO",)
    colour: cgm_enums.Colour


@dataclass
class CharacterHeight(CGMCommand):
    id = (5, 15)
    binary_params = ("VDC",)
    height: float


@dataclass
class CharacterOrientation(CGMCommand):
    id = (5, 16)
    binary_params = ("VDC", "VDC", "VDC", "VDC")
    x_character_up: float
    y_character_up: float
    x_character_base: float
    y_character_base: float


@dataclass
class TextPath(CGMCommand):
    id = (5, 17)
    binary_params = ("E",)
    path: cgm_enums.TextPathEnum


@dataclass
class TextAlignment(CGMCommand):
    id = (5, 18)
    binary_params = ("E", "E", "R", "R")

    horizontal_alignment: cgm_enums.TextHorizontalAlignmentEnum
    vertical_alignment: cgm_enums.TextVerticalAlignmentEnum
    continous_horizontal_alignment: float
    continous_vertical_alignment: float


@dataclass
class CharacterSetIndex(CGMCommand):
    id = (5, 19)
    binary_params = ("IX",)

    index: int


@dataclass
class AlternateCharacterSetIndex(CGMCommand):
    id = (5, 20)
    binary_params = ("IX",)

    index: int


@dataclass
class InteriorStyle(CGMCommand):
    id = (5, 22)
    binary_params = ("E",)

    interior_style: cgm_enums.InteriorStyleEnum


@dataclass
class FillColour(CGMCommand):
    id = (5, 23)
    binary_params = ("CO",)

    fill: cgm_enums.Colour


@dataclass
class HatchIndex(CGMCommand):
    id = (5, 24)
    binary_params = ("IX",)
    index: int


@dataclass
class PatternIndex(CGMCommand):
    id = (5, 25)
    binary_params = ("IX",)
    index: int


@dataclass
class EdgeType(CGMCommand):
    id = (5, 27)
    binary_params = ("IX",)
    type: cgm_enums.LineTypeEnum


@dataclass
class EdgeWidth(CGMCommand):
    id = (5, 28)
    binary_params = ("ESS",)
    width: float


@dataclass
class EdgeColour(CGMCommand):
    id = (5, 29)
    binary_params = ("CO",)
    colour: cgm_enums.Colour


@dataclass
class EdgeVisibility(CGMCommand):
    id = (5, 30)
    binary_params = ("E",)
    visibility: cgm_enums.EdgeVisibilityEnum


@dataclass
class ColourTable(CGMCommand):
    id = (5, 34)
    binary_params = ("CI", "CD")

    colour_index: cgm_enums.IndexColour
    colours: List[cgm_enums.DirectColour]


@dataclass
class AspectSourceFlags(CGMCommand):
    id = (5, 35)
    binary_params = (("E", "E"),)
    flags: List[Tuple[cgm_enums.ASFTypeEnum, cgm_enums.ASFValue]]


@dataclass
class LineCap(CGMCommand):
    id = (5, 37)
    binary_params = ("E", "E")
    cap: cgm_enums.CapIndicatorEnum
    dash_cap: cgm_enums.DashCapIndicatorEnum


@dataclass
class RestrictedTextType(CGMCommand):
    id = (5, 42)
    binary_params = ("E",)
    type: cgm_enums.RestrictionTypeEnum


@dataclass
class ApplicationData(CGMCommand):
    id = (7, 2)
    binary_params = ("I", "SF")

    identifier: int
    data: str