-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhpgl.js
2852 lines (2408 loc) · 91.9 KB
/
hpgl.js
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
/*
hpgl v0.8.7-10
A Node.js library to communicate with HPGL-compatible devices such as plotters and printers.
https://github.com/djipco/hpgl
The MIT License (MIT)
Copyright (c) 2016-2018, Jean-Philippe Côté
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
'use strict';
const EventEmitter = require('events').EventEmitter;
const fs = require("fs-extra");
const util = require('util');
module.exports = {};
/**
* Array of valid paper orientations.
* @private
*/
const ORIENTATIONS = ["portrait", "landscape"];
// End of text (ETX) character that acts as a label terminator
const LABEL_TERMINATOR = String.fromCharCode(3);
const PAPER_SIZES = {
A: {short: 21.59, long: 27.94},
B: {short: 27.94, long: 43.18},
C: {short: 43.18, long: 55.88},
D: {short: 55.88, long: 86.36},
E: {short: 86.36, long: 111.76},
A4: {short: 21, long: 29.7},
A3: {short: 29.7, long: 42},
A2: {short: 42, long: 59.4},
A1: {short: 59.4, long: 84.1},
A0: {short: 84.1, long: 118.9}
};
const CHARACTERS = {
// Character Set 0 (ANSI/ASCII)
" ": {code: 32, charset: 0},
"!": {code: 33, charset: 0},
'"': {code: 34, charset: 0},
"#": {code: 35, charset: 0},
"$": {code: 36, charset: 0},
"%": {code: 37, charset: 0},
"&": {code: 38, charset: 0},
"'": {code: 39, charset: 0},
"(": {code: 40, charset: 0},
")": {code: 41, charset: 0},
"*": {code: 42, charset: 0},
"+": {code: 43, charset: 0},
",": {code: 44, charset: 0},
"-": {code: 45, charset: 0},
".": {code: 46, charset: 0},
"/": {code: 47, charset: 0},
"0": {code: 48, charset: 0},
"1": {code: 49, charset: 0},
"2": {code: 50, charset: 0},
"3": {code: 51, charset: 0},
"4": {code: 52, charset: 0},
"5": {code: 53, charset: 0},
"6": {code: 54, charset: 0},
"7": {code: 55, charset: 0},
"8": {code: 56, charset: 0},
"9": {code: 57, charset: 0},
":": {code: 58, charset: 0},
";": {code: 59, charset: 0},
"<": {code: 60, charset: 0},
"=": {code: 61, charset: 0},
">": {code: 62, charset: 0},
"?": {code: 63, charset: 0},
"@": {code: 64, charset: 0},
"A": {code: 65, charset: 0},
"B": {code: 66, charset: 0},
"C": {code: 67, charset: 0},
"D": {code: 68, charset: 0},
"E": {code: 69, charset: 0},
"F": {code: 70, charset: 0},
"G": {code: 71, charset: 0},
"H": {code: 72, charset: 0},
"I": {code: 73, charset: 0},
"J": {code: 74, charset: 0},
"K": {code: 75, charset: 0},
"L": {code: 76, charset: 0},
"M": {code: 77, charset: 0},
"N": {code: 78, charset: 0},
"O": {code: 79, charset: 0},
"P": {code: 80, charset: 0},
"Q": {code: 81, charset: 0},
"R": {code: 82, charset: 0},
"S": {code: 83, charset: 0},
"T": {code: 84, charset: 0},
"U": {code: 85, charset: 0},
"V": {code: 86, charset: 0},
"W": {code: 87, charset: 0},
"X": {code: 88, charset: 0},
"Y": {code: 89, charset: 0},
"Z": {code: 90, charset: 0},
"[": {code: 91, charset: 0},
"\\": {code: 92, charset: 0},
"]": {code: 93, charset: 0},
"^": {code: 94, charset: 0},
"_": {code: 95, charset: 0},
"`": {code: 96, charset: 0},
"a": {code: 97, charset: 0},
"b": {code: 98, charset: 0},
"c": {code: 99, charset: 0},
"d": {code: 100, charset: 0},
"e": {code: 101, charset: 0},
"f": {code: 102, charset: 0},
"g": {code: 103, charset: 0},
"h": {code: 104, charset: 0},
"i": {code: 105, charset: 0},
"j": {code: 106, charset: 0},
"k": {code: 107, charset: 0},
"l": {code: 108, charset: 0},
"m": {code: 109, charset: 0},
"n": {code: 110, charset: 0},
"o": {code: 111, charset: 0},
"p": {code: 112, charset: 0},
"q": {code: 113, charset: 0},
"r": {code: 114, charset: 0},
"s": {code: 115, charset: 0},
"t": {code: 116, charset: 0},
"u": {code: 117, charset: 0},
"v": {code: 118, charset: 0},
"w": {code: 119, charset: 0},
"x": {code: 120, charset: 0},
"y": {code: 121, charset: 0},
"z": {code: 122, charset: 0},
"{": {code: 123, charset: 0},
"|": {code: 124, charset: 0},
"}": {code: 125, charset: 0},
"~": {code: 126, charset: 0},
// Character Set 7 (Roman Extensions)
"À": {code: 33, charset: 7},
"Â": {code: 34, charset: 7},
"È": {code: 35, charset: 7},
"Ê": {code: 36, charset: 7},
"Ë": {code: 37, charset: 7},
"Î": {code: 38, charset: 7},
"Ï": {code: 39, charset: 7},
"´": {code: 40, charset: 7},
// 41 is duplicate
"ˆ": {code: 42, charset: 7},
"¨": {code: 43, charset: 7},
"˜": {code: 44, charset: 7},
"Ù": {code: 45, charset: 7},
"Û": {code: 46, charset: 7},
"£": {code: 47, charset: 7},
"¯": {code: 48, charset: 7},
// 49 is unused
// 50 is unused
"˚": {code: 51, charset: 7},
"Ç": {code: 52, charset: 7},
"ç": {code: 53, charset: 7},
"Ñ": {code: 54, charset: 7},
"ñ": {code: 55, charset: 7},
"¡": {code: 56, charset: 7},
"¿": {code: 57, charset: 7},
"¤": {code: 58, charset: 7},
// 59 is duplicate of 47
"¥": {code: 60, charset: 7},
"§": {code: 61, charset: 7},
// 62: not sure... ƒ
"¢": {code: 63, charset: 7},
"â": {code: 64, charset: 7},
"ê": {code: 65, charset: 7},
"ô": {code: 66, charset: 7},
"û": {code: 67, charset: 7},
"á": {code: 68, charset: 7},
"é": {code: 69, charset: 7},
"ó": {code: 70, charset: 7},
"ú": {code: 71, charset: 7},
"à": {code: 72, charset: 7},
"è": {code: 73, charset: 7},
"ò": {code: 74, charset: 7},
"ù": {code: 75, charset: 7},
"ä": {code: 76, charset: 7},
"ë": {code: 77, charset: 7},
"ö": {code: 78, charset: 7},
"ü": {code: 79, charset: 7},
"Á": {code: 80, charset: 7},
// "?": 81,
// "?": 82,
"Æ": {code: 83, charset: 7},
// "?": 84,
"í": {code: 85, charset: 7},
// "?": 86,
// "?": 87,
// "?": 88,
"ì": {code: 89, charset: 7},
// "?": 90,
"Ü": {code: 91, charset: 7},
"É": {code: 92, charset: 7},
// "": 93,
// "": 94,
"Ô": {code: 95, charset: 7},
// same as 80
"Ã": {code: 97, charset: 7},
"ã": {code: 98, charset: 7},
// "?": 99,
// "?": 100,
"Í": {code: 101, charset: 7},
// "?": 102,
"Ó": {code: 103, charset: 7},
"Ò": {code: 104, charset: 7},
"Õ": {code: 105, charset: 7},
"õ": {code: 106, charset: 7},
"Š": {code: 107, charset: 7},
"š": {code: 108, charset: 7},
"Ú": {code: 109, charset: 7},
// "?": 110,
// "?": 111,
"þ": {code: 112, charset: 7},
"Þ": {code: 113, charset: 7},
// 114 is unused,
// 115 is unused,
// 116 is unused,
// 117 is unused,
// "?": 118,
"¼": {code: 119, charset: 7},
"½": {code: 120, charset: 7},
// "?": 121,
// "?": 122,
// "?": 123,
// "": 124,
// "?": 125,
"±": {code: 126, charset: 7}
// "µ": {code: 96, charset: 34}
// // circumflex (we send "a", then "backspace" and then the circumflex accent)
// "â": [97, 8, 94],
// "ê": [101, 8, 94],
// "ô": [111, 8, 94],
// "û": [117, 8, 94],
//
// // diaresis (we send "a", then "backspace" and then the diaresis mark)
// "ä": [97, 8, 126],
// "ë": [101, 8, 126],
// "ö": [111, 8, 126],
// "ü": [117, 8, 126]
};
/**
* A rectangle object with position (x, y) and dimensions (width, height).
*
* @typedef {object} Rectangle
* @property x {Number} - Position of the rectangle's top-left corner along the **x** axis.
* @property y {Number} - Position of the rectangle's topl-left corner along the **y** axis.
* @property width {Number} - Width of the rectangle.
* @property height {Number} - Height of the rectangle.
*/
let Rectangle = function (x = 0, y = 0, width = 0, height = 0) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
};
/**
* The `Models` class is basically an enumeration class that provides information about all the
* devices (only a few plotters for now) that are supported by the library.
*
* >Note: the only plotter that was tested so far is the **HP 7475A**. We are assuming the others
* >are going to work based on the documentation we found for them.
*
* If you have a plotter that is not listed here, [contact the author](https://twitter.com/jpcote)
* to see if we can add support for your device. Adding support for a new model simply involves
* retrieving the information such as the one found in this class for other devices.
*
* @class
*/
let Models = {
/**
* Characteristics of the plotter
*
* @typedef {object} PlotterCharacteristics
* @property {string} brand - Name of the manufacturer of the device.
* @property {number} buffer - Size of the device's buffer in bytes (characters).
* @property {string[]} instructions - An array of all the 2-letter HPGL instruction codes
* supported by the device.
* @property {string} model - Model of the device. The library attempts to retrieve that
* information from the device itself.
* @property {Object} papers - Supported paper formats
* @property {string[]} papers.list - Array of all paper formats supported by the device.
* @property {number} papers.~format~ - Information about a specific paper format. Substitute
* `~format~` with the actual format from the `papers.list` array: **A3**, **A4**, **A**, **B**,
* **C**, etc.
* @property {number} papers.~format~.long - The length of the long side of the plottable are.
* @property {number} papers.~format~.short - The length of the short side of the plottable are.
* @property {number} papers.~format~.psCode - The paper size (**PS**) code for that paper (not
* @property {number} papers.~format~.margins - The margins for that paper.
* @property {number} papers.~format~.margins.landscape - Margins in **landscape** orientation.
* @property {number} papers.~format~.margins.landscape.top - Top margin.
* @property {number} papers.~format~.margins.landscape.right - Right margin.
* @property {number} papers.~format~.margins.landscape.bottom - Bottom margin.
* @property {number} papers.~format~.margins.landscape.left - Left margin.
* @property {number} papers.~format~.margins.portrait - Margins in **portrait** orientation.
* @property {number} papers.~format~.margins.portrait.top - Top margin.
* @property {number} papers.~format~.margins.portrait.right - Right margin.
* @property {number} papers.~format~.margins.portrait.bottom - Bottom margin.
* @property {number} papers.~format~.margins.portrait.left - Left margin.
* necessary on most devices).
*/
/** @type {PlotterCharacteristics} */
"GENERIC": {
brand: "Unknown",
model: "GENERIC",
buffer: 60,
papers: {
list: ["A", "B", "A4", "A3"],
A4: {long: 10870, short: 7600},
A3: {long: 15970, short: 10870},
A: {long: 10170, short: 7840},
B: {long: 16450, short: 10170}
},
resolution: {
x: 40,
y: 40
},
instructions: [
"AA", "AP", "AR", "AS", "BF", "BL", "CA", "CI", "CM", "CP", "CS", "CT", "CV", "DC", "DF",
"DI", "DL", "DP", "DR", "DS", "DT", "EA", "EP", "ER", "ES", "EW", "FP", "FS", "FT", "GC",
"GM", "IM", "IN", "IP", "IV", "IW", "KY", "LB", "LO", "LT", "NR", "OA", "OC", "OD", "OE",
"OF", "OG", "OH", "OI", "OK", "OL", "OO", "OP", "OS", "OT", "OW", "PA", "PB", "PD", "PG",
"PM", "PR", "PT", "PU", "RA", "RO", "RP", "RR", "SA", "SC", "SI", "SL", "SM", "SP", "SR",
"SS", "TL", "UC", "UF", "VS", "WD", "WG", "XT", "YT"
]
},
/**
* Characteristics for the **HP 7440A** plotter. This model is also known as the *Color Pro
* Graphics* Plotter. It is an ANSI A and ISO A4 plotter with an 8-pen carousel. Note that the
* paper size cannot be changed programmatically. It has to be changed by flipping a hardware
* switch on the back of the device.
*
* **Important**: this model can be fitted with an optional *Graphics Enhancement Cartridge* which
* bumps its buffer memory to 1024 (instead of 60) bytes. The cartridge also adds support for
* various plotting instructions which are not supported by default. For example, without the
* cartridge, this model cannot draw rectangles, circles or arcs, it cannot set the pen thickness,
* etc.
*
* @type {PlotterCharacteristics}
*/
"7440A": {
brand: "HP",
model: "7440A",
buffer: 60,
papers: {
list: ["A", "A4"],
A4: { // 210x297
long: 10880,
short: 7640,
margins: {
portrait: {top: 440, right: 400, bottom: 560, left: 360}, // plotting range x: 10900 (272.5mm)
landscape: {top: 360, right: 420, bottom: 400, left: 560} // plotting range y: 7650 (191.25mm)
},
scalingPoints: {
p1: { x: 250, y: 279 },
p2: { x: 10250, y: 7479 }
}
},
A: {
long: 10280,
short: 7640,
margins: {
portrait: {top: 240, right: 360, bottom: 640, left: 640}, // plotting range x : 10300 (257.5mm)
landscape: {top: 640, right: 240, bottom: 360, left: 640} // plotting range y : 7650 (191.25mm)
},
scalingPoints: {
p1: { x: 250, y: 279 },
p2: { x: 10250, y: 7479 }
}
},
},
resolution: { x: 40, y: 40 },
instructions: [
"CA", "CP", "CS", "DC", "DF", "DI", "DP", "DR", "IM", "IN", "IP", "IW", "LB", "LT", "OA",
"OC", "OD", "OE", "OF", "OH", "OI", "OO", "OP", "OS", "OW", "PA", "PD", "PR", "PU", "RO",
"SA", "SC", "SI", "SL", "SM", "SP", "SR", "SS", "TL", "UC", "VS", "XT", "YT"
]
},
/**
* Characteristics for the **HP 7470A** plotter.
*
* @type {PlotterCharacteristics}
*/
"7470A": {
brand: "HP",
model: "7470A",
buffer: 255,
papers: {
list: ["A", "A4"],
A4: {long: 10900, short: 7650},
A: {long: 10300, short: 7650} // labeled as "US" on this model
},
resolution: {
x: 40,
y: 40
},
instructions: [
"AA", "AR", "CA", "CI", "CP", "CS", "DC", "DF", "DI", "DP", "DR", "DT", "IM", "IN", "IP",
"IW", "LB", "LT", "OA", "OC", "OD", "OE", "OF", "OI", "OO", "OP", "OS", "OW", "PA", "PD",
"PR", "PU", "SA", "SC", "SI", "SL", "SM", "SP", "SR", "SS", "TL", "UC", "VS", "XT", "YT"
]
},
/**
* Characteristics for the **HP 7475A** plotter. It is a A/A4 and B/A3 size plotter with a 6-pen
* carousel.
*
* @type {PlotterCharacteristics}
*/
"7475A": {
brand: "HP",
model: "7475A",
buffer: 1024,
papers: {
list: ["A", "B", "A4", "A3"],
A: {
long: 10365,
short: 7962,
psCode: 4,
margins: {
landscape: {top: 562, right: 463, bottom: 112, left: 348},
portrait: {top: 348, right: 562, bottom: 463, left: 112}
},
scalingPoints: {
p1: { x: 250, y: 596 },
p2: { x: 10250, y: 7796 }
}
},
B: {
long: 16640,
short: 10365,
psCode: 0,
margins: {
landscape: {top: 463, right: 112, bottom: 348, left: 562},
portrait: {top: 112, right: 348, bottom: 562, left: 463}
},
scalingPoints: {
p1: { x: 522, y: 259 },
p2: { x: 15722, y: 10259 }
}
},
A4: {
long: 11040,
short: 7721,
psCode: 4,
margins: {
landscape: {},
portrait: {}
},
scalingPoints: {
p1: { x: 603, y: 521 },
p2: { x: 10603, y: 7721 }
}
},
A3: {
long: 16158,
short: 11040,
psCode: 0,
margins: {
landscape: {},
portrait: {}
},
scalingPoints: {
p1: { x: 170, y: 602 },
p2: { x: 15370, y: 10602 }
}
}
},
resolution: {
x: 40,
y: 40
},
instructions: [
"AA", "AR", "CA", "CI", "CP", "CS", "DC", "DF", "DI", "DP", "DR", "DT", "EA", "ER", "EW",
"FT", "IM", "IN", "IP", "IW", "LB", "LT", "OA", "OC", "OD", "OE", "OF", "OH", "OI", "OO",
"OP", "OS", "OW", "PA", "PD", "PR", "PS", "PT", "PU", "RA", "RO", "RR", "SA", "SC", "SI",
"SL", "SM", "SP", "SR", "SS", "TL", "UC", "VS", "WG", "XT", "YT"
]
},
/**
* Characteristics for the **HP 7550A** plotter.
*
* @type {PlotterCharacteristics}
*/
"7550A": {
brand: "HP",
model: "7550A",
buffer: 12800,
papers: {
list: ["A", "B", "A4", "A3"],
A4: {long: 10870, short: 7600},
A3: {long: 15970, short: 10870},
A: {long: 10170, short: 7840},
B: {long: 16450, short: 10170}
},
resolution: {
x: 40,
y: 40
},
instructions: [
"AA", "AP", "AR", "AS", "BF", "BL", "CA", "CI", "CM", "CP", "CS", "CT", "CV", "DC", "DF",
"DI", "DL", "DP", "DR", "DS", "DT", "EA", "EP", "ER", "ES", "EW", "FP", "FS", "FT", "GC",
"GM", "IM", "IN", "IP", "IV", "IW", "KY", "LB", "LO", "LT", "NR", "OA", "OC", "OD", "OE",
"OF", "OG", "OH", "OI", "OK", "OL", "OO", "OP", "OS", "OT", "OW", "PA", "PB", "PD", "PG",
"PM", "PR", "PT", "PU", "RA", "RO", "RP", "RR", "SA", "SC", "SI", "SL", "SM", "SP", "SR",
"SS", "TL", "UC", "UF", "VS", "WD", "WG", "XT", "YT"
]
},
// "7550B": {},
// "7550Plus": {},
// LARGE:
/**
* Characteristics for the **HP 7580A** plotter.
*
* @type {PlotterCharacteristics}
*
*/
// * A and A4 sizes are loaded with the longer axis horizontal.
// * All other sizes have long axis vertical.
// * LES MARGES SONT À LA PAGE 43 DU MANUEL. ÇA PERMETTRA DE CALCULER LES GROSSEURS
// * LA PAGE 53 DIT COMMENT CHARGER LE PAPIER (DANS QUEL SENS)
"7580A": {
brand: "HP",
model: "7580A",
buffer: 1024,
// papers: {
// list: ["A", "B", "C", "D", "A4", "A3", "A2", "A1"],
// A: {long: 10170, short: 7840},
// B: {long: 16450, short: 10170},
// C: {long: ?, short: ?},
// D: {long: ?, short: ?},
// A4: {long: 10870, short: 7600},
// A3: {long: 15970, short: 10870},
// A2: {long: ?, short: ?},
// A1: {long: ?, short: ?}
// },
resolution: {
x: 40,
y: 40
},
instructions: [
"AA", "AP", "AR", "AS", "BL", "CA", "CC", "CI", "CM", "CP", "CS", "CT", "DC", "DF", "DI",
"DL", "DP", "DR", "DS", "DT", "EA", "EP", "ER", "ES", "EW", "FP", "FS", "FT", "GP", "IM",
"IN", "IP", "IV", "IW", "LB", "LO", "LT", "NR", "OA", "OC", "OD", "OE", "OF", "OH", "OI",
"OL", "OO", "OP", "OS", "OT", "OW", "PA", "PB", "PD", "PM", "PR", "PT", "PU", "RA", "RO",
"RR", "SA", "SC", "SG", "SI", "SL", "SM", "SP", "SR", "SS", "TL", "UC", "UF", "VS", "WG",
"XT", "YT"
]
},
/**
* Characteristics for the **HP 7585A** plotter.
*
* @type {PlotterCharacteristics}
*
*/
// * A, C, A4, and A2 sizes are loaded with the longer axis horizontal.
// * All other sizes have long axis vertical.
"7585A": {
brand: "HP",
model: "7585A",
buffer: 1024,
// papers: {
// list: ["A", "B", "C", "D", "E", "A4", "A3", "A2", "A1", "A0"],
// A: {long: 10170, short: 7840},
// B: {long: 16450, short: 10170},
// C: {long: ?, short: ?},
// D: {long: ?, short: ?},
// E: {long: ?, short: ?},
// A4: {long: 10870, short: 7600},
// A3: {long: 15970, short: 10870},
// A2: {long: ?, short: ?},
// A1: {long: ?, short: ?}
// A0: {long: ?, short: ?}
// },
resolution: {
x: 40,
y: 40
},
instructions: [
"AA", "AP", "AR", "AS", "BL", "CA", "CC", "CI", "CM", "CP", "CS", "CT", "DC", "DF", "DI",
"DL", "DP", "DR", "DS", "DT", "EA", "EP", "ER", "ES", "EW", "FP", "FS", "FT", "GP", "IM",
"IN", "IP", "IV", "IW", "LB", "LO", "LT", "NR", "OA", "OC", "OD", "OE", "OF", "OH", "OI",
"OL", "OO", "OP", "OS", "OT", "OW", "PA", "PB", "PD", "PM", "PR", "PT", "PU", "RA", "RO",
"RR", "SA", "SC", "SG", "SI", "SL", "SM", "SP", "SR", "SS", "TL", "UC", "UF", "VS", "WG",
"XT", "YT"
]
},
// "7585B": {},
/**
* Characteristics for the **HP 7586B** plotter.
*
* @type {PlotterCharacteristics}
*/
"7586B": {
brand: "HP",
model: "7586B",
buffer: 1024,
// papers: {
// list: ["A", "B", "C", "D", "E", "A4", "A3", "A2", "A1", "A0"],
// A: {long: 10170, short: 7840},
// B: {long: 16450, short: 10170},
// C: {long: ?, short: ?},
// D: {long: ?, short: ?},
// E: {long: ?, short: ?},
// A4: {long: 10870, short: 7600},
// A3: {long: 15970, short: 10870},
// A2: {long: ?, short: ?},
// A1: {long: ?, short: ?}
// A0: {long: ?, short: ?}
// },
resolution: {
x: 40,
y: 40
},
instructions: [
"AA", "AF", "AH", "AP", "AR", "AS", "BL", "CA", "CC", "CI", "CM", "CP", "CS", "CT", "DC",
"DF", "DI", "DL", "DP", "DR", "DS", "DT", "EA", "EC", "EP", "ER", "ES", "EW", "FP", "FR",
"FS", "FT", "GP", "IM", "IN", "IP", "IV", "IW", "LB", "LO", "LT", "NR", "OA", "OC", "OD",
"OE", "OF", "OH", "OI", "OL", "OO", "OP", "OS", "OT", "OW", "PA", "PB", "PD", "PG", "PM",
"PR", "PT", "PU", "RA", "RO", "RR", "SA", "SC", "SG", "SI", "SL", "SM", "SP", "SR", "SS",
"TL", "UC", "UF", "VS", "WG", "XT", "YT"
]
}
};
/*
b. "Automatic" is available only for the following models, and
allows continuous plotting on rollfeed or automatically-fed media:
7550, 7586B, DraftMasterII (7596A)
*/
/*
HPGL Pen Plotters (http://www.winline.com/outdevs.html)
HP 7220C
HP ColorPro, HP 7470, HP 7475A, HP 7550A
HP DraftPro (7570A), HP DraftPro DXL (7575A), HP DraftPro EXL (7576A)
HP 7580A, HP 7580B, HP 7585A, HP 7585B, HP 7586B
HP DraftMaster I (7595A), HP DraftMaster II (7596A)
IOLINE LP 3700, IOLINE LP 4000
Generic HPGL plotter driver supports Hewlett Packard, Océ, Calcomp, Mutoh, Graphtec, Summagraphics, IOLINE, ENCAD, Benson, Schlumberger, Aristo, Zünd and most other HPGL devices.
*/
/*
Cannot use HP-IB plotters such as:
- 7225B
- 9872A
-
*/
/**
* The `Plotter` class provides methods to interact with an HPGL-compatible plotter such as those
* made by HP. Various other makers also use or support the HPGL protocol (Calcomp, for example).
*
* #### Event Handling
*
* This object extends Node's core [EventEmitter](https://nodejs.org/api/events.html) object. This
* means you can use methods such as:
* [on()](https://nodejs.org/api/events.html#events_emitter_on_eventname_listener),
* [once()](https://nodejs.org/api/events.html#events_emitter_once_eventname_listener),
* [removeListener()](https://nodejs.org/api/events.html#events_emitter_removelistener_eventname_listener),
* etc.
*
* #### Usage examples
*
* Here is how you can use the `Plotter` object in a Node.js-compatible project:
*
* ```
* const SerialPort = require("serialport");
* let transport = new SerialPort("/dev/tty.usbserial", { autoOpen: false });
*
* const Plotter = require("hpgl").Plotter;
* let plotter = new Plotter();
*
* plotter.connect(transport, {orientation: "portrait"}, function(error) {
*
* if (error) {
* console.log(error);
* return;
* }
*
* this
* .moveTo(2, 2)
* .drawText("Hello, World!")
* .moveTo(5, 5)
* .drawRectangle(4, 3)
*
* });
* ```
*
* If you are using NW.js, you need to change the first three lines of code to this:
*
* ```
* var SerialPort = nw.require("browser-serialport").SerialPort;
* var transport = new SerialPort("/dev/tty.usbserial", {}, false);
*
* const Plotter = nw.require("hpgl").Plotter;
* ```
*
* @todo Use OO to identify plotters with potential extended capabilities (such as 7440A)
* @todo Create a getter that returns the size of the plottable area.
* @todo Instructions queued with waitForResponse should timeout if the response does not come`
* @todo The queue() function should validate if the instruction(s) is actually valid.
* @todo Implement penThickness.
* @todo ?? The buffer must be flushed during connect because some devices (7440A) will keep commands in the buffer
*
* @class
* @fires Plotter#connected
* @fires Plotter#data
* @fires Plotter#error
* @fires Plotter#ready
* @fires Plotter#fileplotted
*/
let Plotter = function() {
/**
* The number of milliseconds to wait for responses to RS-232-C instructions. This value is a
* lowest common denominator. As an example, the HP7440A's response time is around 260ms.
*
* @member {Number}
* @name Plotter#DEVICE_RS232_DELAY
* @constant
* @default 375
* @private
*/
Object.defineProperty(this, "DEVICE_RS232_DELAY", {
enumerable: true,
writable: false,
value: 375
});
/**
* The delay to wait for between calls to process the queue.
*
* @member {Number}
* @name Plotter#QUEUE_DELAY
* @constant
* @default 100
* @private
*/
Object.defineProperty(this, "QUEUE_DELAY", {
enumerable: true,
writable: false,
value: 100
});
/**
* Prefix for the RS-232 instructions. It is typically made up of the `escape` character followed
* by a period.
*
* @member {String}
* @name Plotter#RS232_PREFIX
* @constant
* @private
*/
Object.defineProperty(this, "RS232_PREFIX", {
enumerable: true,
writable: false,
value: String.fromCharCode(27) + "."
});
/**
* Queue of command objects that will be sent (one by one) to the plotter when the device's buffer
* has enough space.
*
* @private
* @member {Array}
*/
this._queue = [];
/**
* ID of the timeout used to periodically process the queue.
*
* @private
* @member {Number}
*/
this._queueTimeOutId = 0;
/**
* ID of the timeout used to reattempte communication (when lost)
*
* @private
* @member {Number}
*/
this._retryTimeoutId = 0;
/**
* Serial input buffer
*
* @private
* @member {String}
*/
this._buffer = "";
/**
* Path to a file were hpgl commands should be savec.
*
* @private
* @member {String}
*/
this._outputFile = undefined;
/**
* Plotter pen's nib size
*
* @private
* @member {Number}
*/
this._penThickness = 0.3;
/**
* The thickness of the drawing pen's nib in millimiters. The value must be between 0.1 and 5.
* Specifying an invalid value will set the thickness to the default value of 0.3.
*
* Specifying the pen's thickness is particularly important when trying to shade shapes.
*
* @member {Number} Plotter#penThickness
*/
Object.defineProperty(this, 'penThickness', {
get: () => { return this._penThickness; },
set: (value) => {
if (value >= 0.1 && value <= 5) {
this._penThickness = value;
} else {
this._penThickness = 0.3
}
}
});
/**
* The paper orientation currently selected (portrait or landscape). Paper orientation is assigned
* during the connection to the device (with the [connect()]{@link Plotter#connect} function) or
* when saving to file (with the [connect()]{@link Plotter#startCapturingToFile} function).
*
* @type {String}
* @default "landscape"
* @readonly
*/
this.orientation = "landscape";
/**
* Whether the drawing should be flipped along the x-axis.
*
* @type {Boolean}
* @default false
* @readonly
*/
this.flipX = false;
/**
* Whether the drawing should be flipped along the y-axis.
*
* @type {Boolean}
* @default false
* @readonly
*/
this.flipY = false;
/**
* Indicates whether a successful serial connection has been established or not. This does not
* necessarily mean the device is ready to receive commands.
*
* To know when the device is ready to receive commands, you can check out the `ready` property or
* listen to the [ready]{@link Plotter#event:ready} event
*
* @member {Number} Plotter#connected
* @readOnly
*/
Object.defineProperty(this, 'connected', {
get: () => {
if (this.transport && typeof this.transport.isOpen === 'function') {
return this.transport.isOpen(); // browser-serialport
} else if (this.transport) {
return this.transport.isOpen; // node-serialport
} else {
return false;
}
}
});
/**
* The format of paper currently selected (A4, letter, B, etc.). Paper format is assigned during
* the connection to the device (with the [connect()]{@link Plotter#connect} function). Currently,
* it cannot be changed on the fly.
*
* @type {String}
* @default "A"
* @readonly
*/
this.paper = "A";
/**
* @type {PlotterCharacteristics}
* @readonly
*/
this.characteristics = undefined;
/**
* The object that is used for serial communication. This object must adhere to the
* [serialport](https://www.npmjs.com/package/serialport) module interface. Typically, it is one
* of: [serialport](https://www.npmjs.com/package/serialport),
* [browser-serialport](https://www.npmjs.com/package/browser-serialport) or
* [virtual-serialport](https://www.npmjs.com/package/virtual-serialport)
*
* @member {Object}
* @readOnly
*/
this.transport = undefined;
/**
* Indicates whether the device is ready to receive commands or not. The device is ready only
* after having been successfully connected by using the [Plotter.connect()]{@link Plotter#connect}
* function. Instructions should not be sent to the device prior to it being ready.
*
* The [Plotter]{@link Plotter} object triggers the [ready]{@link Plotter#event:ready} event when
* its ready.
*
* @member {Boolean}
* @readOnly
*/
this.ready = false;
/**
* This is a an empty object inside which user's of this library are invited to add whatever
* property they might find useful.
*
* @member {Object}
*/
this.userData = {};
};
util.inherits(Plotter, EventEmitter);
/**
* Opens a serial connection to the device using the specified serial transport layer. The following
* serial modules are supported: [serialport](https://www.npmjs.com/package/serialport) and
* [browser-serialport](https://www.npmjs.com/package/browser-serialport).
*
* Important: calling `connect()` will terminate any ongoing file capture. If you want to both
* plot and save to file at the same time, call
* [startCapturingToFile()]{@link Plotter#startCapturingToFile} only after the plotter is ready.