Merge branch 'master' of github.com:dpavlin/Printer-Zebra
[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),$/;
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) ) {
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]/ ) {
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         }
90
91         slurp_line;
92 }
93
94 __DATA__
95 ^A Scalable/Bitmapped Font
96 ^A@ Use Font Name to Call Font
97 ^B1 Code 11 Bar Code
98 ^B2 Interleaved 2 of 5 Bar Code
99 ^B3 Code 39 Bar Code
100 ^B4 Code 49 Bar Code
101 ^B5 Planet Code bar code
102 ^B7 PDF417 Bar Code
103 ^B8 EAN-8 Bar Code
104 ^B9 UPC-E Bar Code
105 ^BA Code 93 Bar Code
106 ^BB CODABLOCK Bar Code
107 ^BC Code 128 Bar Code (Subsets A, B, and C)
108 ^BD UPS MaxiCode Bar Code
109 ^BE EAN-13 Bar Code
110 ^BF Micro-PDF417 Bar Code
111 ^BI Industrial 2 of 5 Bar Codes
112 ^BJ Standard 2 of 5 Bar Code
113 ^BK ANSI Codabar Bar Code
114 ^BL LOGMARS Bar Code
115 ^BM MSI Bar Code
116 ^BO Aztec Bar Code Parameters
117 ^BP Plessey Bar Code
118 ^BQ QR Code Bar Code
119 ^BR RSS (Reduced Space Symbology) Bar Code
120 ^BS UPC/EAN Extensions
121 ^BT TLC39 bar code
122 ^BU UPC-A Bar Code
123 ^BX Data Matrix Bar Code
124 ^BY Bar Code Field Default
125 ^BZ POSTNET Bar Code
126 ^CC Change Carets
127 ~CC Change Carets
128 ^CD Change Delimiter
129 ~CD Change Delimiter
130 ^CF Change Alphanumeric Default Font
131 ^CI Change International Font
132 ^CM Change Memory Letter Designation
133 ^CO Cache On
134 ^CT Change Tilde
135 ~CT Change Tilde
136 ^CV Code Validation
137 ^CW Font Identifier
138 ~DB Download Bitmap Font
139 ~DE Download Encoding
140 ^DF Download Format
141 ~DG Download Graphics
142 ~DN Abort Download Graphic
143 ~DS Download Scalable Font
144 ~DT Download TrueType Font
145 ~DU Download Unbounded TrueType Font
146 ~DY Download Graphics
147 ~EG Erase Download Graphics
148 ^FB Field Block
149 ^FC Field Clock (for Real-Time Clock)
150 ^FD Field Data
151 ^FH Field Hexadecimal Indicator
152 ^FM Multiple Field Origin Locations
153 ^FN Field Number
154 ^FO Field Origin
155 ^FP Field Parameter
156 ^FR Field Reverse Print
157 ^FS Field Separator
158 ^FT Field Typeset
159 ^FV Field Variable
160 ^FW Field Orientation
161 ^FX Comment
162 ^GB Graphic Box
163 ^GC Graphic Circle
164 ^GD Graphic Diagonal Line
165 ^GE Graphic Ellipse
166 ^GF Graphic Field
167 ^GS Graphic Symbol
168 ~HB Battery Status
169 ~HD Head Temperature Information
170 ^HF Graphic Symbol
171 ^HG Host Graphic
172 ^HH Configuration Label Return
173 ~HI Host Identification
174 ~HM Host RAM Status
175 ~HS Host Status Return
176 ~HU Return ZebraNet Alert Configuration
177 ^HV Host Verification
178 ^HW Host Directory List
179 ^HY Upload Graphics
180 ^HZ Display Description Information
181 ^ID Object Delete
182 ^IL Image Load
183 ^IM Image Move
184 ^IS Image Save
185 ~JA Cancel All
186 ^JB Initialize Flash Memory
187 ~JB Reset Optional Memory
188 ~JC Set Media Sensor Calibration
189 ~JD Enable Communications Diagnostics
190 ~JE Disable Diagnostics
191 ~JF Set Battery Condition
192 ~JG Graphing Sensor Calibration
193 ^JJ Set Auxiliary Port
194 ~JL Set Label Length
195 ^JM Set Dots per Millimeter
196 ~JN Head Test Fatal
197 ~JO Head Test Non fatal
198 ~JP Pause and Cancel Format
199 ~JR Power On Reset
200 ^JS Sensor Select
201 ~JS Change Backfeed Sequence
202 ^JT Head Test Interval
203 ^JU Configuration Update
204 ^JW Set Ribbon Tension
205 ~JX Cancel Current Partially Input Format
206 ^JZ Reprint After Error
207 ~KB Kill Battery (Battery Discharge Mode)
208 ^KD Select Date and Time Format (for Real Time Clock)
209 ^KL Define Language
210 ^KN Define Printer Name
211 ^KP Define Password
212 ^LH Label Home
213 ^LL Label Length
214 ^LR Label Reverse Print
215 ^LS Label Shift
216 ^LT Label Top
217 ^MC Map Clear
218 ^MD Media Darkness
219 ^MF Media Feed
220 ^ML Maximum Label Length
221 ^MM Print Mode
222 ^MN Media Tracking
223 ^MP Mode Protection
224 ^MT Media Type
225 ^MU Set Units of Measurement
226 ^MW Modify Head Cold Warning
227 ~NC Network Connect
228 ^NI Network ID Number
229 ~NR Set All Network Printers Transparent
230 ^NS Change Networking Settings
231 ~NT Set Currently Connected Printer Transparent
232 ^PF Slew Given Number of Dot Rows
233 ^PH Slew to Home Position
234 ~PH Slew to Home Position
235 ^PM Printing Mirror Image of Label
236 ^PO Print Orientation
237 ^PP Programmable Pause
238 ~PP Programmable Pause
239 ^PQ Print Quantity
240 ^PR Print Rate
241 ~PR Applicator Reprint
242 ~PS Print Start
243 ^PW Print Width
244 ~RO Reset Advanced Counter
245 ^SC Set Serial Communications
246 ~SD Set Darkness
247 ^SE Select Encoding
248 ^SF Serialization Field (with a Standard ^FD String)
249 ^SL Set Mode and Language (for Real-Time Clock)
250 ^SN Serialization Data
251 ^SO Set Offset (for Real-Time Clock)
252 ^SP Start Print
253 ^SQ Halt ZebraNet Alert
254 ^SR Set Printhead Resistance
255 ^SS Set Media Sensors
256 ^ST Set Date and Time (for Real-Time Clock)
257 ^SX Set ZebraNet Alert
258 ^SZ Set ZPL
259 ~TA Tear-off Adjust Position
260 ^TO Transfer Object
261 ~WC Print Configuration Label
262 ^WD Print Directory Label
263 ^XA Start Format
264 ^XB Suppress Backfeed
265 ^XF Recall Format
266 ^XG Recall Graphic
267 ^XZ End Format
268 ^ZZ Printer Sleep