860f28b0ca6a05d158fda873853ba3597e63bc35
[Printer-Zebra.git] / ZPL2pbm.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use autodie;
5 use Data::Dump qw(dump);
6
7 # convert Zebra label printer ZPL to pbm image
8
9 my $command;
10 while(<DATA>) {
11         chomp;
12         my ( $cmd, $desc ) = split(/\s/,$_,2);
13         $command->{$cmd} = $desc;
14 }
15
16 my $file = shift @ARGV || die "usage: $0 dump.zpl > dump.pbm";
17
18 open(my $in, '<', $file);
19
20 our $line = '';
21 sub slurp_line {
22         $line .= <$in> unless length $line > 3;
23 }
24 slurp_line;
25
26 while( $line ) {
27         $line =~ s/[\r\n]+$// && warn "## removed CR/LF\n";
28         warn "# line ",dump($line),$/ if length($line) < 80 or $ENV{DEBUG};
29         if ( $line =~ s/~DG(\w+:)?(.+)// ) {
30                 my ( $name, $t,$w ) = split(/,/,$2,4);
31
32                 warn "# ~DG$1 => [$name] t=$t w=$w\n";
33
34                 my $data;
35                 read $in, $data, $t;
36
37                 my $out;
38                 # ZPL decompress
39                 my $repeat = 1;
40                 foreach my $p ( 0 .. length($data) - 1 ) {
41                         my $c = substr($data,$p,1);
42                         if ( $c eq ',' ) {
43                                 my $l = ( $w * 2 ) - length($out) % ( $w * 2 );
44                                 $l = $w * 2 if $l == 0;
45                                 warn "# $p ZERO-to-EOL $c [$l]\n";
46                                 $out .= "0" x $l;
47                         } elsif ( $c eq ':' ) {
48                                 $out .= length($out) > $w ? substr($out,-$w*2) : "00" x $w;
49                                 warn "# $p :\n";
50                         } elsif ( $c eq 'z' ) {
51                                 $repeat += 400;
52                         } elsif ( $c ge 'g' && $c le 'y' ) {
53                                 $repeat += 20 * ( ord($c) - ord('f') );
54                         } elsif ( $c ge 'G' && $c le 'Y' ) {
55                                 $repeat += ord($c) - ord('F');
56                         } elsif ( $c =~ m/[0-9A-F]/i ) {
57                                 if ( $repeat ) {
58                                         warn "# $p $repeat $c\n";
59                                         $out .= $c x $repeat;
60                                         $repeat = 0;
61                                 } else {
62                                         warn "# $p hex $c\n";
63                                         $out .= $c;
64                                 }
65                         } else {
66                                 warn "ABORT: offset $p data [$c]";
67                                 $line = $c . substr($data,$p);
68                                 slurp_line;
69                                 last;
70                         }
71
72                         warn "## $repeat [$c] out = ",length($out),$/;
73                 }
74
75                 my $bitmap = pack('H*', $out);
76                 warn "# graphics of ",length($data)," bytes ZPL decompressed to ",length($out)," hex and ", length($bitmap), " bytes bitmap\n";
77                 my $pw = $w * 8;
78                 my $ph = int(length($bitmap) / $w);
79                 print "P4\n$pw $ph\n", substr($bitmap,0,$ph*$w);
80
81         } elsif ( $line =~ s/^([~\^][^~\^\r\n]*)// ) {
82                 my $cmd = substr($1,0,3);
83                 if ( my $desc = $command->{$cmd} ) {
84                         warn "ZPL: $1\t$desc\n";
85                 } else {
86                         warn "UNKNOWN: ",dump($1),$/;
87                 }
88                 $line =~ s/^[\r\n]+// && warn "## removed CR/LF\n";
89         } else {
90                 my $unknown = $1 if $line =~ s/^(.)//; # printer seems to ignore invalid chars
91                 warn "IGNORE: ",dump($unknown);
92         }
93
94         slurp_line;
95 }
96
97 __DATA__
98 ^A Scalable/Bitmapped Font
99 ^A@ Use Font Name to Call Font
100 ^B1 Code 11 Bar Code
101 ^B2 Interleaved 2 of 5 Bar Code
102 ^B3 Code 39 Bar Code
103 ^B4 Code 49 Bar Code
104 ^B5 Planet Code bar code
105 ^B7 PDF417 Bar Code
106 ^B8 EAN-8 Bar Code
107 ^B9 UPC-E Bar Code
108 ^BA Code 93 Bar Code
109 ^BB CODABLOCK Bar Code
110 ^BC Code 128 Bar Code (Subsets A, B, and C)
111 ^BD UPS MaxiCode Bar Code
112 ^BE EAN-13 Bar Code
113 ^BF Micro-PDF417 Bar Code
114 ^BI Industrial 2 of 5 Bar Codes
115 ^BJ Standard 2 of 5 Bar Code
116 ^BK ANSI Codabar Bar Code
117 ^BL LOGMARS Bar Code
118 ^BM MSI Bar Code
119 ^BO Aztec Bar Code Parameters
120 ^BP Plessey Bar Code
121 ^BQ QR Code Bar Code
122 ^BR RSS (Reduced Space Symbology) Bar Code
123 ^BS UPC/EAN Extensions
124 ^BT TLC39 bar code
125 ^BU UPC-A Bar Code
126 ^BX Data Matrix Bar Code
127 ^BY Bar Code Field Default
128 ^BZ POSTNET Bar Code
129 ^CC Change Carets
130 ~CC Change Carets
131 ^CD Change Delimiter
132 ~CD Change Delimiter
133 ^CF Change Alphanumeric Default Font
134 ^CI Change International Font
135 ^CM Change Memory Letter Designation
136 ^CO Cache On
137 ^CT Change Tilde
138 ~CT Change Tilde
139 ^CV Code Validation
140 ^CW Font Identifier
141 ~DB Download Bitmap Font
142 ~DE Download Encoding
143 ^DF Download Format
144 ~DG Download Graphics
145 ~DN Abort Download Graphic
146 ~DS Download Scalable Font
147 ~DT Download TrueType Font
148 ~DU Download Unbounded TrueType Font
149 ~DY Download Graphics
150 ~EG Erase Download Graphics
151 ^FB Field Block
152 ^FC Field Clock (for Real-Time Clock)
153 ^FD Field Data
154 ^FH Field Hexadecimal Indicator
155 ^FM Multiple Field Origin Locations
156 ^FN Field Number
157 ^FO Field Origin
158 ^FP Field Parameter
159 ^FR Field Reverse Print
160 ^FS Field Separator
161 ^FT Field Typeset
162 ^FV Field Variable
163 ^FW Field Orientation
164 ^FX Comment
165 ^GB Graphic Box
166 ^GC Graphic Circle
167 ^GD Graphic Diagonal Line
168 ^GE Graphic Ellipse
169 ^GF Graphic Field
170 ^GS Graphic Symbol
171 ~HB Battery Status
172 ~HD Head Temperature Information
173 ^HF Graphic Symbol
174 ^HG Host Graphic
175 ^HH Configuration Label Return
176 ~HI Host Identification
177 ~HM Host RAM Status
178 ~HS Host Status Return
179 ~HU Return ZebraNet Alert Configuration
180 ^HV Host Verification
181 ^HW Host Directory List
182 ^HY Upload Graphics
183 ^HZ Display Description Information
184 ^ID Object Delete
185 ^IL Image Load
186 ^IM Image Move
187 ^IS Image Save
188 ~JA Cancel All
189 ^JB Initialize Flash Memory
190 ~JB Reset Optional Memory
191 ~JC Set Media Sensor Calibration
192 ~JD Enable Communications Diagnostics
193 ~JE Disable Diagnostics
194 ~JF Set Battery Condition
195 ~JG Graphing Sensor Calibration
196 ^JJ Set Auxiliary Port
197 ~JL Set Label Length
198 ^JM Set Dots per Millimeter
199 ~JN Head Test Fatal
200 ~JO Head Test Non fatal
201 ~JP Pause and Cancel Format
202 ~JR Power On Reset
203 ^JS Sensor Select
204 ~JS Change Backfeed Sequence
205 ^JT Head Test Interval
206 ^JU Configuration Update
207 ^JW Set Ribbon Tension
208 ~JX Cancel Current Partially Input Format
209 ^JZ Reprint After Error
210 ~KB Kill Battery (Battery Discharge Mode)
211 ^KD Select Date and Time Format (for Real Time Clock)
212 ^KL Define Language
213 ^KN Define Printer Name
214 ^KP Define Password
215 ^LH Label Home
216 ^LL Label Length
217 ^LR Label Reverse Print
218 ^LS Label Shift
219 ^LT Label Top
220 ^MC Map Clear
221 ^MD Media Darkness
222 ^MF Media Feed
223 ^ML Maximum Label Length
224 ^MM Print Mode
225 ^MN Media Tracking
226 ^MP Mode Protection
227 ^MT Media Type
228 ^MU Set Units of Measurement
229 ^MW Modify Head Cold Warning
230 ~NC Network Connect
231 ^NI Network ID Number
232 ~NR Set All Network Printers Transparent
233 ^NS Change Networking Settings
234 ~NT Set Currently Connected Printer Transparent
235 ^PF Slew Given Number of Dot Rows
236 ^PH Slew to Home Position
237 ~PH Slew to Home Position
238 ^PM Printing Mirror Image of Label
239 ^PO Print Orientation
240 ^PP Programmable Pause
241 ~PP Programmable Pause
242 ^PQ Print Quantity
243 ^PR Print Rate
244 ~PR Applicator Reprint
245 ~PS Print Start
246 ^PW Print Width
247 ~RO Reset Advanced Counter
248 ^SC Set Serial Communications
249 ~SD Set Darkness
250 ^SE Select Encoding
251 ^SF Serialization Field (with a Standard ^FD String)
252 ^SL Set Mode and Language (for Real-Time Clock)
253 ^SN Serialization Data
254 ^SO Set Offset (for Real-Time Clock)
255 ^SP Start Print
256 ^SQ Halt ZebraNet Alert
257 ^SR Set Printhead Resistance
258 ^SS Set Media Sensors
259 ^ST Set Date and Time (for Real-Time Clock)
260 ^SX Set ZebraNet Alert
261 ^SZ Set ZPL
262 ~TA Tear-off Adjust Position
263 ^TO Transfer Object
264 ~WC Print Configuration Label
265 ^WD Print Directory Label
266 ^XA Start Format
267 ^XB Suppress Backfeed
268 ^XF Recall Format
269 ^XG Recall Graphic
270 ^XZ End Format
271 ^ZZ Printer Sleep