lib/Graphics/Penplotter/GcodeXY/Postscript.pm untested

filter: Statementstmt 0.0% 0 / 159 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 223 CC 22 SCAR 58.9
CC
22
Cov
0%
exporteps
506.0
BEGIN
2.0
BEGIN
2.0
CRAP 359.7
1 package Graphics::Penplotter::GcodeXY::Postscript v0.9.4;
2  
3 use v5.38.2;
4 use feature qw( signatures );
5 no warnings qw( experimental::signatures );
6 use strict;
7 use warnings;
8 use Role::Tiny;
9 use Carp qw( croak );
10 use POSIX qw( floor ceil );
11 use Readonly qw( Readonly );
12  
13 # ---------------------------------------------------------------------------
14 # Graphics::Penplotter::GcodeXY::Postscript
15 # Role providing PostScript / EPS export for GcodeXY.
16 # ---------------------------------------------------------------------------
17  
18 requires qw( _parse _flushPsegments _checkp _checkl);
19  
20 # ---------------------------------------------------------------------------
21 # Constants -- private copies of the values defined in GcodeXY.pm.
22 # These must remain identical to their counterparts in the main module.
23 # ---------------------------------------------------------------------------
24  
25 Readonly my $I2P => 72.0; # inches to PostScript points
26 Readonly my $BBMAX => 1_000_000.0; # sentinel for bounding-box minimum
27 Readonly my $EOL => qq{\n};
28  
29 # _parse() return opcodes -- must match the values in GcodeXY.pm exactly
30 Readonly my $PU => 1; # penup
31 Readonly my $PD => 2; # pendown
32 Readonly my $G00 => 3; # fast move
33 Readonly my $G01 => 4; # draw move
34 Readonly my $NOOP => 5; # ignored line
35  
36 # ---------------------------------------------------------------------------
37 # Paper sizes in points [width, height] -- copied from GcodeXY.pm.
38 # ---------------------------------------------------------------------------
39  
40 my %pspaper = (
41 '4A0' => [ 4768, 6741 ],
42 '2A0' => [ 3370, 4768 ],
43 A0 => [ 2384, 3370 ],
44 A1 => [ 1684, 2384 ],
45 A2 => [ 1191, 1684 ],
46 A3 => [ 841, 1190 ],
47 A4 => [ 595, 841 ],
48 );
49  
50 # Sorted smallest-first for best-fit reporting -- mirrors @a_sizes in GcodeXY.pm
51 my @a_sizes = (
52 { name => 'A4', width => 595, height => 842 },
53 { name => 'A3', width => 842, height => 1191 },
54 { name => 'A2', width => 1191, height => 1684 },
55 { name => 'A1', width => 1684, height => 2384 },
56 { name => 'A0', width => 2384, height => 3370 },
57 { name => '2A0', width => 3370, height => 4768 },
58 { name => '4A0', width => 4768, height => 6741 },
59 );
60  
61 # ---------------------------------------------------------------------------
62 # exporteps($filename)
63 #
64 # Export the current drawing as a DSC 3.0 compliant Encapsulated PostScript
65 # file. See eps.pl for a full description of every design decision.
66 #
67 # Returns a four-element list: (llx_pt, lly_pt, urx_pt, ury_pt) as floats,
68 # matching the return convention of exportsvg().
69 # ---------------------------------------------------------------------------
70  
71 sub exporteps ($self, $gcout = undef) {
72  
73 croak 'exporteps: filename not provided' unless $gcout;
74  
75 # Flush any pending segments so currentpage is complete.
76 $self->_flushPsegments();
77  
78 my $limit = scalar @{ $self->{currentpage} };
79 croak "exporteps: $gcout: empty queue. Aborting." unless $limit;
80  
81 # ------------------------------------------------------------------
82 # Pass 1: scan currentpage for the true bounding box.
83 # We skip the gcode header (everything up to and including the first
84 # penupcmd sentinel written by _openpage()).
85 # ------------------------------------------------------------------
86 my $maxx = 0.0;
87 my $maxy = 0.0;
88 my $minx = $BBMAX;
89 my $miny = $BBMAX;
90  
91 my $linecount = 0;
92 HEADER_SCAN:
93 while ($linecount < $limit) {
94 $linecount++;
95 last HEADER_SCAN
96 if $self->{currentpage}[$linecount - 1] eq $self->{penupcmd};
97 }
98  
99 for my $i ($linecount .. $limit - 1) {
100 my ($op, $xn, $yn) = $self->_parse( $self->{currentpage}[$i] );
101 next unless $op eq $G00 || $op eq $G01;
102 my $x = $xn + 0.0;
103 my $y = $yn + 0.0;
104 $maxx = $x if $x > $maxx;
105 $maxy = $y if $y > $maxy;
106 $minx = $x if $x < $minx;
107 $miny = $y if $y < $miny;
108 }
109  
110 # Convert to points
111 croak "exporteps: $gcout: no drawing found (no G00/G01 coordinates in page)"
112 if $minx >= $BBMAX;
113  
114 my $bbllx_f = $minx * $I2P;
115 my $bblly_f = $miny * $I2P;
116 my $bburx_f = $maxx * $I2P;
117 my $bbuly_f = $maxy * $I2P;
118  
119 # DSC requires integer bounding box values.
120 # floor the minimums, ceil the maximums (conservative outer bound).
121 my $bbllx = floor($bbllx_f);
122 my $bblly = floor($bblly_f);
123 my $bburx = ceil($bburx_f);
124 my $bbuly = ceil($bbuly_f);
125  
126 # ------------------------------------------------------------------
127 # Paper media comment (informational only -- no device operators).
128 # ------------------------------------------------------------------
129 my $media_comment = '';
130 if ( defined $self->{papersize} ) {
131 my $ps = uc $self->{papersize};
132 if ( defined $pspaper{$ps} ) {
133 my ($pw, $ph) = @{ $pspaper{$ps} };
134 $media_comment = "%%PageMedia: $ps $pw $ph 80 () ()\n";
135 }
136 }
137  
138 # ------------------------------------------------------------------
139 # Pass 2: write the EPS file.
140 # ------------------------------------------------------------------
141 open( my $out, '>', $gcout )
142 or croak "exporteps: cannot open '$gcout': $!";
143  
144 # --- DSC header ---------------------------------------------------
145 # The magic comment must be on the very first line, no leading space.
146 print {$out} "%!PS-Adobe-3.0 EPSF-3.0\n";
147 print {$out} "%%BoundingBox: $bbllx $bblly $bburx $bbuly\n";
148 printf {$out} "%%%%HiResBoundingBox: %.4f %.4f %.4f %.4f\n",
149 $bbllx_f, $bblly_f, $bburx_f, $bbuly_f;
150  
151 my $creator = 'Graphics::Penplotter::GcodeXY';
152 $creator .= " id=$self->{id}" if $self->{id};
153 print {$out} "%%Creator: ($creator)\n";
154 print {$out} "%%Title: ($gcout)\n";
155  
156 {
157 my @t = localtime;
158 printf {$out} "%%%%CreationDate: %04d-%02d-%02d %02d:%02d:%02d\n",
159 $t[5]+1900, $t[4]+1, $t[3], $t[2], $t[1], $t[0];
160 }
161  
162 print {$out} "%%LanguageLevel: 1\n";
163 print {$out} "%%Pages: 1\n";
164 print {$out} $media_comment if $media_comment;
165 print {$out} "%%EndComments\n";
166 print {$out} "%%BeginProlog\n";
167 print {$out} "%%EndProlog\n";
168 print {$out} "%%Page: 1 1\n";
169  
170 # --- Graphics state -----------------------------------------------
171 my $lw = $self->{eps_linewidth} // 0.3;
172 printf {$out} "%.4f setlinewidth\n", $lw;
173 print {$out} "1 setlinecap\n"; # round
174 print {$out} "1 setlinejoin\n"; # round
175  
176 # --- Body ---------------------------------------------------------
177 my $path_open = 0;
178  
179 $linecount = 0;
180 HEADER_BODY:
181 while ($linecount < $limit) {
182 $linecount++;
183 last HEADER_BODY
184 if $self->{currentpage}[$linecount - 1] eq $self->{penupcmd};
185 }
186  
187 while ($linecount < $limit) {
188 $linecount++;
189 my $line = $self->{currentpage}[$linecount - 1];
190 my ($op, $xn, $yn) = $self->_parse($line);
191  
192 if ($op eq $PU) {
193 if ($path_open) {
194 print {$out} "stroke\n";
195 $path_open = 0;
196 }
197 }
198 elsif ($op eq $G00) {
199 if ($path_open) {
200 print {$out} "stroke\n";
201 }
202 printf {$out} "newpath %.4f %.4f moveto\n",
203 $xn * $I2P, $yn * $I2P;
204 $path_open = 1;
205 }
206 elsif ($op eq $G01) {
207 printf {$out} "%.4f %.4f lineto\n",
208 $xn * $I2P, $yn * $I2P;
209 }
210 # $PD and $NOOP: nothing to emit
211 }
212  
213 # Close any still-open path; no showpage in EPS.
214 if ($path_open) {
215 print {$out} "stroke\n";
216 }
217  
218 print {$out} "%%Trailer\n";
219 print {$out} "%%EOF\n";
220  
221 close $out;
222  
223 # --- Diagnostics --------------------------------------------------
224 if ( $self->{check} ) {
225 printf STDOUT "exporteps: %s: bounding box = (%.3f,%.3f) (%.3f,%.3f) pt\n",
226 $gcout, $bbllx_f, $bblly_f, $bburx_f, $bbuly_f;
227 $self->_checkp();
228 $self->_checkl();
229 }
230  
231 return ($bbllx_f, $bblly_f, $bburx_f, $bbuly_f);
232 }
233  
234 1;
235  
236 __END__