lib/Graphics/Penplotter/GcodeXY.pm

filter: Statementstmt 15.5% 47 / 303 Branchbran 0.0% 0 / 92 Conditioncond 0.0% 0 / 46 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 34.7% 16 / 46 Podpod n/a totaltotal 12.2% 63 / 513 CC 71 SCAR 42.6
CC
71
Cov
11%
_flushPsegments
272.0
init
132.0
_genmove
90.0
CRAP 70.9
« lib/Graphics/Penplotter/GcodeXY/SVG.pm ↑ Summary lib/Graphics/Penplotter/GcodeXY/Anamorphic.pm »
1 package Graphics::Penplotter::GcodeXY v0.9.4;
2  
3 9 ▶use v5.38.2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use feature qw( signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶no warnings qw( experimental::signatures ); # required by List::Util and Term::ANSIcolor (perl testers matrix)
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use vars qw($VERSION @ISA @EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use POSIX qw( ceil );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use List::Util qw( max );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use Readonly qw( Readonly );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use Term::ANSIColor qw( RED RESET YELLOW );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use parent qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use Role::Tiny::With;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 with 'Graphics::Penplotter::GcodeXY::Geometry2D'; # geometry primitives and transforms
18 with 'Graphics::Penplotter::GcodeXY::Geometry3D';
19 with 'Graphics::Penplotter::GcodeXY::Hatch'; # needs: Geometry2D (gsave/grestore/_getsegintersect)
20 with 'Graphics::Penplotter::GcodeXY::Font'; # needs: Hatch (_dohatching), Geometry2D (gsave/grestore/translate)
21 with 'Graphics::Penplotter::GcodeXY::SVG'; # needs: Font (stroketext/setfont), Geometry2D (geometry methods)
22 with 'Graphics::Penplotter::GcodeXY::Postscript'; # no inter-role deps
23 with 'Graphics::Penplotter::GcodeXY::Split'; # no inter-role deps
24 with 'Graphics::Penplotter::GcodeXY::Vpype'; # no inter-role deps
25 with 'Graphics::Penplotter::GcodeXY::Optimize'; # no inter-role deps
26 with 'Graphics::Penplotter::GcodeXY::Anamorphic'; # no inter-role deps
27 with 'Graphics::Penplotter::GcodeXY::Swirl'; # no inter-role deps
28  
29 our @EXPORT_OK = qw(translate translateC stroketextfill stroketext strokefill stroke split
30 skewX skewY sethatchsep sethatchangle setfontsize setfont scale rotate
31 polygon penup pendown pageborder exportsvg exporteps output newsegpath
32 movetoR moveto lineR line initmatrix importsvg gsave grestore getsegpath
33 ellipse curveto curve currentpoint boxround boxR box arcto arc addtopage
34 addfontpath addcomment textwidth arrowhead polygonround vpype_linesort
35 polygon_clip polygon_clip_end polygonR polygonC
36 initmatrix3 translate3 translateC3 scale3 rotate3 rotate3_euler
37 compose_matrix invert_matrix currentpoint3 transform_point transform_points
38 moveto3 movetoR3 line3 lineR3 polygon3 polygon3C polygon3R project_to_svg
39 box3 cube axis_gizmo mesh prism sphere icosphere cylinder frustum cone capsule
40 plane torus disk pyramid quat_from_axis_angle quat_to_matrix quat_slerp
41 bbox3 compute_normals backface_cull occlusion_clip hidden_line_remove
42 flatten_to_2d draw_polylines mesh_to_obj mesh_from_obj mesh_to_stl mesh_from_stl
43 set_tolerance get_tolerance set_units set_coordinate_convention
44 anamorphic);
45  
46  
47 # ---------------------------------------------------------------------------
48 # SECTION: Data Structures and Constants
49 # ---------------------------------------------------------------------------
50  
51 # definition of page sizes in pt, should be Properly Cased
52 my %pspaper = (
53 '4A0' => [ 4768, 6741 ],
54 '2A0' => [ 3370, 4768 ],
55 A0 => [ 2384, 3370 ],
56 A1 => [ 1684, 2384 ],
57 A2 => [ 1191, 1684 ],
58 A3 => [ 841, 1190 ],
59 A4 => [ 595, 841 ],
60 # A5 => [ 420, 595 ],
61 # A6 => [ 297, 420 ],
62 # A7 => [ 210, 297 ],
63 # A8 => [ 148, 210 ],
64 # A9 => [ 105, 148 ],
65 # B0 => [ 2920, 4127 ],
66 # B1 => [ 2064, 2920 ],
67 # B2 => [ 1460, 2064 ],
68 # B3 => [ 1032, 1460 ],
69 # B4 => [ 729, 1032 ],
70 # B5 => [ 516, 729 ],
71 # B6 => [ 363, 516 ],
72 # B7 => [ 258, 363 ],
73 # B8 => [ 181, 258 ],
74 # B9 => [ 127, 181 ],
75 # B10 => [ 91, 127 ],
76 # Executive => [ 522, 756 ],
77 # Folio => [ 595, 935 ],
78 # 'Half-Letter' => [ 612, 397 ],
79 # Letter => [ 612, 792 ],
80 # 'US-Letter' => [ 612, 792 ],
81 # Legal => [ 612, 1008 ],
82 # 'US-Legal' => [ 612, 1008 ],
83 # Tabloid => [ 792, 1224 ],
84 # 'SuperB' => [ 843, 1227 ],
85 # Ledger => [ 1224, 792 ],
86 # 'Comm #10 Envelope' => [ 297, 684 ],
87 # 'Envelope-Monarch' => [ 280, 542 ],
88 # 'Envelope-DL' => [ 312, 624 ],
89 # 'Envelope-C5' => [ 461, 648 ],
90 # 'EuroPostcard' => [ 298, 420 ],
91 );
92  
93 # translate inch to/from other unit (multiplication factor)
94 # measuring units are two-letter acronyms:
95 # pt: postscript point (72 per inch)
96 # in: inch (72 postscript points)
97 # mm: millimetre (25.4 per inch)
98 # cm: centimetre (2.54 per inch)
99 # px: standard 96 ppi for a pen plotter, used for SVG
100 # pc: pica (6.0 per inch)
101 my %inches_to_unit = (
102 pt => 72.0,
103 in => 1.0,
104 mm => 25.4,
105 cm => 2.54,
106 px => 96.0,
107 pc => 6.0,
108 );
109  
110 my %unit_to_inches = (
111 px => 1.0/96.0, # 1 pixel = 1/96 inch
112 pt => 1.0/72.0, # 1 point = 1/72 inch
113 pc => 1.0/6.0, # 1 pica = 1/6 inch (12 points)
114 in => 1.0, # 1 inch = 1 inch
115 cm => 1.0/2.54, # 1 cm = 1/2.54 inch
116 mm => 1.0/25.4, # 1 mm = 1/25.4 inch
117 );
118  
119 # definition of constants
120 Readonly my $D2R => 0.01745329251; # degrees to radians scale factor
121 Readonly my $R2D => 57.2957795130; # radians to degrees scale factor
122 Readonly my $I2P => 72.0; # inches to points scale factor
123 Readonly my $EMPTY_STR => q{}; # ''
124 Readonly my $SPACE => q{ }; # ' '
125 Readonly my $EOL => qq{\n}; # "\n"
126 Readonly my $HALFPI => 1.57079632680; # 0.5*π
127 Readonly my $PI => 3.14159265359; # 1.0*π
128 Readonly my $THREEPI2 => 4.71238898038; # 1.5*π
129 Readonly my $TWOPI => 6.28318530718; # 2.0*π
130 Readonly my $PU => 1; # penup line opcode
131 Readonly my $PD => 2; # pendown line opcode
132 Readonly my $G00 => 3; # G00 line opcode
133 Readonly my $G01 => 4; # G01 line opcode
134 Readonly my $NOOP => 5; # "ignore this line" opcode
135 Readonly my $EPSILON => 0.00000001; # equality for floating point
136 Readonly my $BBMAX => 1_000_000.0; # huge page bounding box
137 Readonly my $IN => 0; # virtual pen is inside the sheet when cutting
138 Readonly my $OUT => 1; # virtual pen is outside the sheet when cutting
139 Readonly my $PENUP => 3; # physical pen is off the paper
140 Readonly my $PENDOWN => 4; # physical pen is on the paper
141  
142 # object allocation
143 0 ▶sub new ($class, %data) {
Subroutine new: not called CC=2 SCAR=17.9
144 0 my $self = {
145 # public:
146 papersize => undef, # paper size e.g. "A3"
147 xsize => undef, # bounding box x
148 ysize => undef, # bounding box y
149 units => 'in', # inches is used internally
150 header => "G20\nG90\nG17\nF 50\nG92 X 0 Y 0 Z 0\nG00 Z 0\n", # must end with penup
151 trailer => "G00 Z 0\nG00 X 0 Y 0\n",
152 penupcmd => "G00 Z 0",
153 pendowncmd => "G00 Z 0.2",
154 margin => 1.0, # margin as a PERCENTAGE
155 outfile => $EMPTY_STR,
156 curvepts => 50,
157 check => 0,
158 warn => 0, # out of page bounds warning
159 hatchsep => 0.012, # inches, equivalent to 0.3 mm (the tip of a BIC ballpoint pen)
160 hatchangle => 0, # degrees; 0 = horizontal hatch lines
161 id => $EMPTY_STR,
162 optimize => 1,
163 eps_linewidth => 0.3, # PostScript line width in points
164  
165 # private:
166 dscale => 1.0, # device scaling, used when units not "in"
167 opt_debug => 0, # what is the optimizer doing?
168 maxx => 0.0, # gcode design bounding box
169 maxy => 0.0,
170 minx => $BBMAX,
171 miny => $BBMAX,
172 penlocked => 0,
173 fontsize => 0, # default font size
174 fontname => "", # current font name
175 posx => 0, # current point, x
176 posy => 0, # current point, y
177 _g3_posx => 0, # 3D current point, x
178 _g3_posy => 0, # 3D current point, y
179 _g3_posz => 0, # 3D current point, z
180 _g3_tolerance => $EPSILON, # 3D fp tolerance
181 _g3_units => 'in',
182 _g3_handedness=> 'right',
183 _g3_euler_order => 'XYZ',
184 _g3_camera => undef,
185 pencount => 0, # number of times pen raised/lowered (raise+lower counts as 1)
186 slowdistcount => 0, # distance traveled on paper (in fact, its square)
187 fastdistcount => 0, # distance traveled above paper (in fact, its square)
188  
189 # plus others, set in init()
190 };
191 0 foreach ( keys %data ) {
192 0 $self->{$_} = $data{$_};
193 }
194 0 bless $self, $class;
195 0 $self->init();
196 0 return $self;
197 }
198  
199 0 ▶sub init ($self) {
Subroutine init: not called CC=11 SCAR=48.8
200 # Units
201 0 $self->{units} = lc $self->{units};
202 0 ▶ if ( defined( $unit_to_inches{ $self->{units} } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $unit_to_inches{$self->{'units'}}) { } 0 0 0
203 0 $self->{dscale} = $unit_to_inches{ $self->{units} };
204 }
205 else {
206 0 $self->_croak("unit '$self->{units}' not supported");
207 }
208  
209 # Paper size
210 0 ▶ if ( defined $self->{papersize} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"papersize"}) 0 0 0
211 0 $self->{papersize} = uc $self->{papersize};
212 0 ▶ if ( !defined $pspaper{ $self->{papersize} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pspaper{$self->{"papersize"}}) 0 0 0
213 0 $self->_croak("paper size '$self->{papersize}' not supported");
214 }
215 }
216 # warn and/or check
217 0 ▶ if ( defined $self->{papersize} && defined $pspaper{ $self->{papersize} } )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'papersize'} and defined $pspaper{$self->{'papersize'}}) { } 0 0 0
Condition0%0/3
defined $self->{"papersize"} and defined $pspaper{$self->{"papersize"}}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"papersize"} and defined $pspaper{$self->{"papersize"}}
defined $self->{"papersize"} defined $pspaper{$self->{"papersize"}}
Truth table0%0/3
A and B
A= defined $self->{"papersize"}B= defined $pspaper{$self->{"papersize"}}
ABresult
0X0
100
111
218 0 { ( $self->{xsize}, $self->{ysize} ) = @{ $pspaper{ $self->{papersize} } };
219 # If the unit is different from pt, adjust xsize and ysize.
220 0 ▶ if ($self->{units} ne 'pt') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"units"} ne "pt") 0 0 0
221 0 my $t = $inches_to_unit{ $self->{units} };
222 0 $self->{xsize} = $self->{xsize} * $t / $inches_to_unit{'pt'};
223 0 $self->{ysize} = $self->{ysize} * $t / $inches_to_unit{'pt'};
224 }
225 }
226 else { # paper size was not defined, check xsize and ysize
227 0 ▶ if ( $self->{warn} || $self->{check} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"warn"} or $self->{"check"}) 0 0 0
Condition0%0/3
$self->{"warn"} or $self->{"check"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"warn"} or $self->{"check"}
$self->{"warn"} $self->{"check"}
Truth table0%0/3
A or B
A= $self->{"warn"}B= $self->{"check"}
ABresult
1X1
011
000
228 0 ▶ if ( !defined $self->{xsize} || !defined $self->{ysize} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"xsize"} and defined $self->{"ysize"}) 0 0 0
Condition0%0/3
defined $self->{"xsize"} and defined $self->{"ysize"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"xsize"} and defined $self->{"ysize"}
defined $self->{"xsize"} defined $self->{"ysize"}
Truth table0%0/3
A and B
A= defined $self->{"xsize"}B= defined $self->{"ysize"}
ABresult
0X0
100
111
229 0 $self->_croak('cannot warn or check if page size undefined or unsupported');
230 }
231 }
232 }
233  
234 # page arrays
235 0 $self->{currentpage} = []; # list of generated gcode statements
236 0 $self->{psegments} = []; # path segments (needing stroking)
237 0 $self->{hsegments} = []; # list of segments needing hatching
238 0 $self->{gstate} = []; # graphics state
239 0 $self->{CTM} = [ [1,0,0], [0,1,0], [0,0,1] ]; # 2D current transformation matrix
240 0 $self->{_g3_CTM} = [ [1,0,0,0], [0,1,0,0], [0,0,1,0], [0,0,0,1] ]; # 3D CTM
241 0 $self->{_g3_gstate} = []; # 3D graphics state
242 0 $self->{_g3_camera} = undef; # camera (set via set_camera())
243 # add header to page
244 0 $self->_openpage();
245 0 return 1;
246 }
247  
248 # ---------------------------------------------------------------------------
249 # Section: Segments
250 # ---------------------------------------------------------------------------
251  
252 # Paths consist of a series of segments, where the end of one may be the start of another.
253 # These are then checked for intersection with a scanline if hatching is required. Hatching
254 # segments are stored in a separate array.
255  
256 #
257 # initialize the segment path
258 #
259 0 ▶sub newpath ($self) {
Subroutine newpath: not called CC=1 SCAR=6.9
260 0 @{ $self->{psegments} } = ();
261 0 @{ $self->{hsegments} } = ();
262 0 return 1;
263 }
264  
265 #
266 # close the segment path
267 #
268 0 ▶sub _closepath ($self) {
Subroutine _closepath: not called CC=1 SCAR=6.9
269 0 my ( $x, $y ) = $self->currentpoint();
270 0 my $tx = $self->{psegments}[0]{sx};
271 0 my $ty = $self->{psegments}[0]{sy};
272 0 my $k = $self->{psegments}[0]{key};
273 0 $self->_addpath( $k, $x, $y, $tx, $ty );
274 0 return 1;
275 }
276  
277 #
278 # add to the segment path
279 # this routine is the main way that line segments are added to the path,
280 # and is used by moveto, lineto, and closepath.
281 #
282 0 ▶sub _addpath ($self, $key, $sx, $sy, $dx, $dy) {
Subroutine _addpath: not called CC=2 SCAR=17.9
283 0 my $len = scalar @{ $self->{psegments} };
284 0 ▶ if (!defined $dy) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dy) 0 0 0
285 0 $self->_croak('need 5 parameters for addpath');
286 0 return 0;
287 }
288 # could insert LiangBarsky here to do cropping for svg viewbox
289 0 $self->{psegments}[$len] = { key => $key, sx => $sx, sy => $sy, dx => $dx, dy => $dy };
290 0 return 1;
291 }
292  
293 #
294 # add to the segment path
295 #
296 0 ▶sub addcomment ($self, $s) {
Subroutine addcomment: not called CC=2 SCAR=17.9
297 0 my $len = scalar @{ $self->{psegments} };
298 0 ▶ if ( !defined $s ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $s) 0 0 0
299 0 $self->_croak('need 1 parameter for addcomment');
300 0 return 0;
301 }
302 0 $self->{psegments}[$len] = { key => 'c', s => $s, sx => 0, sy => 0, dx => 0, dy => 0 };
303 0 return 1;
304 }
305  
306 # paint the current path, then clear it
307 0 ▶sub stroke ($self) {
Subroutine stroke: not called CC=1 SCAR=6.9
308 0 $self->_flushPsegments();
309 0 $self->newpath();
310 0 return 1;
311 }
312  
313 #
314 # translate the segment queue into gcode
315 #
316 0 ▶sub _flushPsegments ($self) {
Subroutine _flushPsegments: not called CC=16 SCAR=56.1
317 0 my $len = scalar @{ $self->{psegments} };
318 0 my ( $k, $d );
319 0 ▶ if ( !$len ) { return } # empty queue
Branch: 1 branch
BranchTrueFalseTotal
unless ($len) 0 0 0
320 0 $self->_optimize();
321 0 $len = scalar @{ $self->{psegments} }; # hopefully, lots have been removed
322 SEGMENT:
323 0 foreach my $i ( 0 .. $len - 1 ) {
324 # line segment length, for aggregation
325 0 $k = $self->{psegments}[$i]{key};
326 0 ▶ if (! defined $k) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $k) 0 0 0
327 0 next SEGMENT;
328 }
329 0 ▶ if ($k ne 'c') {
Branch: 1 branch
BranchTrueFalseTotal
if ($k ne "c") 0 0 0
330 $d = sqrt(
331 ( $self->{psegments}[$i]{sx} - $self->{psegments}[$i]{dx} ) *
332 ( $self->{psegments}[$i]{sx} - $self->{psegments}[$i]{dx} ) +
333 ( $self->{psegments}[$i]{sy} - $self->{psegments}[$i]{dy} ) *
334 0 ( $self->{psegments}[$i]{sy} - $self->{psegments}[$i]{dy} ) );
335 }
336 0 ▶ if ( $k eq 'l' ) { # lineto, the most frequent instruction
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq "l") 0 0 0
337 $self->_addtopage(
338 #sprintf "G01 X %.5f Y %.5f" . $EOL,
339 sprintf "G01 X %.5f Y %.5f",
340 $self->{psegments}[$i]{dx},
341 $self->{psegments}[$i]{dy}
342 0 );
343 0 $self->{slowdistcount} += $d;
344 0 next SEGMENT;
345 }
346 0 ▶ if ( $k eq 'm' ) { # moveto
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq "m") 0 0 0
347 $self->_addtopage(
348 #sprintf "G00 X %.5f Y %.5f" . $EOL,
349 sprintf "G00 X %.5f Y %.5f",
350 $self->{psegments}[$i]{dx},
351 $self->{psegments}[$i]{dy}
352 0 );
353 0 $self->{fastdistcount} += $d;
354 0 next SEGMENT;
355 }
356 0 ▶ if ( $k eq 'c' ) { # comment
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq "c") 0 0 0
357 $self->_addtopage(
358 #sprintf "(%s)" . $EOL,
359 sprintf "(%s)",
360 $self->{psegments}[$i]{s}
361 0 );
362 0 next SEGMENT;
363 }
364 0 ▶ if ( $k eq 'u' ) { # penup
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq "u") 0 0 0
365 0 ▶ if ( !$self->{penlocked} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"penlocked"}) 0 0 0
366 0 ▶ my $last = $self->{currentpage}[-1] // '';
Condition0%0/2
$self->{"currentpage"}[-1] // ""
l!l
00
MC/DC0%0/1
$self->{"currentpage"}[-1] // ""
$self->{"currentpage"}[-1]
Truth table0%0/2
A
A= $self->{"currentpage"}[-1]
Aresult
11
00
367 0 ▶ unless ( $last eq $self->{penupcmd} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($last eq $self->{"penupcmd"}) 0 0 0
368 0 $self->_addtopage( $self->{penupcmd} );
369 0 $self->{pencount}++;
370 }
371 }
372 0 next SEGMENT;
373 }
374 0 ▶ if ( $k eq 'd' ) { # pendown
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq "d") 0 0 0
375 0 ▶ if ( !$self->{penlocked} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"penlocked"}) 0 0 0
376 0 ▶ my $last = $self->{currentpage}[-1] // '';
Condition0%0/2
$self->{"currentpage"}[-1] // ""
l!l
00
MC/DC0%0/1
$self->{"currentpage"}[-1] // ""
$self->{"currentpage"}[-1]
Truth table0%0/2
A
A= $self->{"currentpage"}[-1]
Aresult
11
00
377 0 ▶ unless ( $last eq $self->{pendowncmd} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($last eq $self->{"pendowncmd"}) 0 0 0
378 0 $self->_addtopage( $self->{pendowncmd} );
379 }
380 }
381 0 next SEGMENT;
382 }
383 }
384 0 return 1;
385 }
386  
387 # ---------------------------------------------------------------------------
388 # Section: Gcode generation
389 # ---------------------------------------------------------------------------
390  
391 #
392 # Lift the pen
393 #
394 0 ▶sub penup ($self) {
Subroutine penup: not called CC=2 SCAR=17.9
395 0 ▶ if ( !$self->{penlocked} ) { $self->_addpath( 'u', -1, -1, -1, -1 ) }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"penlocked"}) 0 0 0
396 0 return 1;
397 }
398  
399 #
400 # Lower the pen
401 #
402 0 ▶sub pendown ($self) {
Subroutine pendown: not called CC=2 SCAR=17.9
403 0 ▶ if ( !$self->{penlocked} ) { $self->_addpath( 'd', -1, -1, -1, -1 ) }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"penlocked"}) 0 0 0
404 0 return 1;
405 }
406  
407 #
408 # Add a line of text to the output. No checking is done.
409 # All currently queued segments are flushed first.
410 #
411 0 ▶sub addtopage ($self, $data) {
Subroutine addtopage: not called CC=2 SCAR=17.9
412 0 ▶ if ( !defined $data ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 0 0 0
413 0 $self->_croak('addtopage: no data provided');
414 0 return 0;
415 }
416 0 $self->_flushPsegments();
417 0 $self->_addtopage($data);
418 0 return 1;
419 }
420  
421 #
422 # Create output file and report statistics
423 #
424 0 ▶sub output ($self, $file = undef) {
Subroutine output: not called CC=7 SCAR=40.3
425 0 ▶ $file ||= $self->{outfile};
Condition0%0/3
$file ||= $self->{"outfile"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$file ||= $self->{"outfile"}
$file $self->{"outfile"}
Truth table0%0/3
A ||= B
A= $fileB= $self->{"outfile"}
ABresult
1X1
011
000
426 0 ▶ if ( $file eq $EMPTY_STR ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq $EMPTY_STR) 0 0 0
427 0 $self->_croak('Must supply a filename for output');
428 }
429 0 my $out;
430 0 $self->_flushPsegments();
431 0 ▶ open $out, '>', $file or croak "Cannot write to file $file";
Branch: 1 branch
BranchTrueFalseTotal
open $out, ">", $file or croak("Cannot write to file $file") 0 0 0
432 0 my $count = scalar @{ $self->{currentpage} };
433 0 foreach my $i ( @{ $self->{currentpage} } ) {
434 0 print {$out} $i . $EOL;
435 }
436 0 $self->_closepage($out); # write the trailer to file, but not memory
437 0 close $out;
438 # report stats if asked for
439 0 ▶ if ( $self->{check} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"check"}) 0 0 0
440 0 $self->_stats();
441 }
442 0 return 1;
443 }
444  
445 #
446 # print statistics about the gcode program
447 #
448 0 ▶sub _stats ($self) {
Subroutine _stats: not called CC=1 SCAR=6.9
449 0 my $f = $self->{fastdistcount};
450 0 my $s = $self->{slowdistcount};
451 0 print STDOUT "=== Object \'" . $self->{id} . "\'===" . $EOL;
452 print STDOUT sprintf "Bounding box: (%.3f,%.3f) (%.3f,%.3f)" . $EOL,
453 0 $self->{minx}, $self->{miny}, $self->{maxx}, $self->{maxy};
454 0 $self->_checkp();
455 0 $self->_checkl();
456 0 print STDOUT 'Pen cycles: ' . $self->{pencount} . $EOL;
457 0 print STDOUT 'Distance above the paper: ';
458 0 print STDOUT sprintf "%.1f inches (%.1f cm, %.1f feet)" . $EOL, $f, $f * 2.54, $f * 0.0833;
459 0 print STDOUT 'Distance on the paper: ';
460 0 print STDOUT sprintf "%.1f inches (%.1f cm, %.1f feet)" . $EOL, $s, $s * 2.54, $s * 0.0833;
461 0 return 1;
462 }
463  
464 #
465 # set up a page, by adding the header
466 #
467 0 ▶sub _openpage ($self) {
Subroutine _openpage: not called CC=1 SCAR=6.9
468 0 $self->_addtopage( $self->{header} );
469 0 $self->_addtopage( $self->{penupcmd} ); # added just to be sure
470 0 return 1;
471 }
472  
473 #
474 # Close a page by adding a trailer, either by adding to currentpage
475 # or to an output file
476 #
477 0 ▶sub _closepage ($self, $out = undef) {
Subroutine _closepage: not called CC=3 SCAR=24.8
478 0 ▶ if ( defined $out ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $out) { } 0 0 0
479 0 print {$out} $self->{trailer} . $EOL;
480 }
481 else {
482 0 $self->_addtopage( $self->{trailer} );
483 }
484 0 return 1;
485 }
486  
487 #
488 # Add a line to the page
489 #
490 0 ▶sub _addtopage ($self, $data) {
Subroutine _addtopage: not called CC=1 SCAR=6.9
491 0 my $p = $self->{currentpage};
492 0 push @{$p}, $data;
493 0 return 1;
494 }
495  
496 0 ▶sub _penunlock ($self) {
Subroutine _penunlock: not called CC=1 SCAR=6.9
497 0 $self->{penlocked} = 0;
498 0 return 1;
499 }
500  
501 0 ▶sub _penlock ($self) {
Subroutine _penlock: not called CC=1 SCAR=6.9
502 0 $self->{penlocked} = 1;
503 0 return 1;
504 }
505  
506 0 ▶sub _penlocked ($self) {
Subroutine _penlocked: not called CC=1 SCAR=6.9
507 0 return $self->{penlocked};
508 }
509  
510 #
511 # Report a serious internal error and quit
512 #
513 0 ▶sub _error ($self, $msg) {
Subroutine _error: not called CC=1 SCAR=6.9
514 0 die RED $msg, RESET;
515 0 return 0;
516 }
517  
518 #
519 # Report a serious user error and quit
520 #
521 0 ▶sub _croak ($self, $msg) {
Subroutine _croak: not called CC=1 SCAR=6.9
522 0 croak YELLOW $msg, RESET;
523 0 return 0;
524 }
525  
526 #
527 # generate a pen move - from user coords
528 #
529 0 ▶sub _genmove ($self, $mode, $x, $y) {
Subroutine _genmove: not called CC=9 SCAR=45.0
530 0 my @point = ();
531 0 my @pointold = ();
532 0 my $opcode = 'm';
533 0 my ( $px, $py );
534 0 ▶ if ( $mode eq 'slow' ) { $opcode = 'l' }
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "slow") 0 0 0
535 # get the current point (user coords) - soon to be updated
536 0 my ( $cx, $cy ) = $self->currentpoint();
537 # get the paper coords of the current point and save them
538 0 @pointold = $self->_u_to_p( $cx, $cy );
539 #debugging
540 0 $px = $pointold[0];
541 0 $py = $pointold[1];
542 # get the device coords of the current point
543 0 ( $pointold[0], $pointold[1] ) = $self->_p_to_d(@pointold);
544 # set the new current point (the destination)
545 0 $self->currentpoint( $x, $y );
546 # get the paper coords of the new current point
547 0 @point = $self->_u_to_p( $x, $y );
548 0 $px = $point[0];
549 0 $py = $point[1];
550 # get the device coords of the new current point
551 0 ( $point[0], $point[1] ) = $self->_p_to_d(@point);
552 # add up the distance
553 # warn if out of device bounds for reporting and postscript generation
554 0 $self->_warn( $point[0], $point[1] );
555 0 ▶ if ( $self->{check} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"check"}) 0 0 0
556 0 ▶ if ( $point[0] > $self->{maxx} ) { $self->{maxx} = $point[0] }
Branch: 1 branch
BranchTrueFalseTotal
if ($point[0] > $self->{"maxx"}) 0 0 0
557 0 ▶ if ( $point[1] > $self->{maxy} ) { $self->{maxy} = $point[1] }
Branch: 1 branch
BranchTrueFalseTotal
if ($point[1] > $self->{"maxy"}) 0 0 0
558 0 ▶ if ( $point[0] < $self->{minx} && $point[0] > 0.0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($point[0] < $self->{"minx"} and $point[0] > 0) 0 0 0
Condition0%0/3
$point[0] < $self->{"minx"} and $point[0] > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$point[0] < $self->{"minx"} and $point[0] > 0
$point[0] < $self->{"minx"} $point[0] > 0
Truth table0%0/3
A and B
A= $point[0] < $self->{"minx"}B= $point[0] > 0
ABresult
0X0
100
111
559 0 $self->{minx} = $point[0];
560 }
561 0 ▶ if ( $point[1] < $self->{miny} && $point[1] > 0.0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($point[1] < $self->{"miny"} and $point[1] > 0) 0 0 0
Condition0%0/3
$point[1] < $self->{"miny"} and $point[1] > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$point[1] < $self->{"miny"} and $point[1] > 0
$point[1] < $self->{"miny"} $point[1] > 0
Truth table0%0/3
A and B
A= $point[1] < $self->{"miny"}B= $point[1] > 0
ABresult
0X0
100
111
562 0 $self->{miny} = $point[1];
563 }
564 }
565 # finally, generate the instruction
566 0 $self->_addpath( $opcode, $pointold[0], $pointold[1], $point[0], $point[1] );
567 0 return 1;
568 }
569  
570 #
571 # generate a slow move (pen on paper)
572 #
573 0 ▶sub _genslowmove ($self, $x, $y) {
Subroutine _genslowmove: not called CC=1 SCAR=6.9
574 0 $self->_genmove( 'slow', $x, $y );
575 0 return 1;
576 }
577  
578 #
579 # generate a fast move (pen off paper)
580 #
581 0 ▶sub _genfastmove ($self, $x, $y) {
Subroutine _genfastmove: not called CC=1 SCAR=6.9
582 0 $self->_genmove( 'fast', $x, $y );
583 0 return 1;
584 }
585  
586 #
587 # Warn if the pen ends up outside the page boundary
588 # We need DEVICE coordinates here for obvious reasons
589 #
590 0 ▶sub _warn ($self, $x, $y) {
Subroutine _warn: not called CC=5 SCAR=34.0
591 0 my ( $x0clip, $y0clip, $x1clip, $y1clip, $info );
592 0 ▶ if ( !$self->{warn} ) { return 0 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"warn"}) 0 0 0
593 # we check only the endpoint for now.
594 # just assume the line started at (0.1, 0.1)
595 0 ▶ if ( ( $x < 0 ) || ( $y < 0 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 0 or $y < 0) 0 0 0
Condition0%0/3
$x < 0 or $y < 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$x < 0 or $y < 0
$x < 0 $y < 0
Truth table0%0/3
A or B
A= $x < 0B= $y < 0
ABresult
1X1
011
000
596 0 print STDOUT "Out of bound: ($x,$y)" . $EOL;
597 0 return 0;
598 }
599 ( $x0clip, $y0clip, $x1clip, $y1clip, $info ) =
600 0 $self->_LiangBarsky( 0, 0, $self->{xsize}, $self->{ysize}, 0.1, 0.1, $x, $y );
601 0 ▶ if ( $info != 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info != 1) 0 0 0
602 0 print STDOUT "Out of bound: ($x,$y)" . $EOL;
603 }
604 0 return 1;
605 }
606  
607 # sizes in postscript units (pt)
608 my @a_sizes = (
609 { name => 'A4', width => 595, height => 842 },
610 { name => 'A3', width => 842, height => 1191 },
611 { name => 'A2', width => 1191, height => 1684 },
612 { name => 'A1', width => 1684, height => 2384 },
613 { name => 'A0', width => 2384, height => 3370 },
614 { name => '2A0', width => 3370, height => 4768 },
615 { name => '4A0', width => 4768, height => 6741 },
616 );
617  
618 # check if a design fits landscape
619 0 ▶sub _checkl ($self) {
Subroutine _checkl: not called CC=6 SCAR=37.4
620 0 my $y = $self->{maxx} * $I2P; # swap for landscape
621 0 my $x = $self->{maxy} * $I2P;
622 0 my $best_fit = 'size too big for 4A0 landscape!';
623 0 foreach my $size (@a_sizes) {
624 # find the smallest size that fits
625 0 ▶ if (($x <= $size->{width} && $y <= $size->{height}) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($x <= $size->{"width"} and $y <= $size->{"height"} or $y <= $size->{"width"} and $x <= $size->{"height"}) 0 0 0
Condition0%0/9
$x <= $size->{"width"} and $y <= $size->{"height"} or $y <= $size->{"width"} and $x <= $size->{"height"}
l!l&&r!l&&!r
000
$x <= $size->{"width"} and $y <= $size->{"height"}
!ll&&!rl&&r
000
$y <= $size->{"width"} and $x <= $size->{"height"}
!ll&&!rl&&r
000
MC/DC0%0/4
$x <= $size->{"width"} and $y <= $size->{"height"} or $y <= $size->{"width"} and $x <= $size->{"height"}
$x <= $size->{"width"} $y <= $size->{"height"} $y <= $size->{"width"} $x <= $size->{"height"}
Truth table0%0/7
A and B or C and D
A= $x <= $size->{"width"}B= $y <= $size->{"height"}C= $y <= $size->{"width"}D= $x <= $size->{"height"}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
626 ($y <= $size->{width} && $x <= $size->{height})) {
627 0 $best_fit = $size->{name};
628 0 last;
629 }
630 }
631 0 print STDOUT "best fit landscape: $best_fit" . $EOL;
632 0 return 1;
633 }
634  
635 # check if a design fits portrait
636 0 ▶sub _checkp ($self) {
Subroutine _checkp: not called CC=6 SCAR=37.4
637 0 my $x = $self->{maxx} * $I2P;
638 0 my $y = $self->{maxy} * $I2P;
639 0 my $best_fit = 'size too big for 4A0 portrait!';
640 0 foreach my $size (@a_sizes) {
641 # find the smallest size that fits
642 0 ▶ if (($x <= $size->{width} && $y <= $size->{height}) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($x <= $size->{"width"} and $y <= $size->{"height"} or $y <= $size->{"width"} and $x <= $size->{"height"}) 0 0 0
Condition0%0/9
$x <= $size->{"width"} and $y <= $size->{"height"} or $y <= $size->{"width"} and $x <= $size->{"height"}
l!l&&r!l&&!r
000
$x <= $size->{"width"} and $y <= $size->{"height"}
!ll&&!rl&&r
000
$y <= $size->{"width"} and $x <= $size->{"height"}
!ll&&!rl&&r
000
MC/DC0%0/4
$x <= $size->{"width"} and $y <= $size->{"height"} or $y <= $size->{"width"} and $x <= $size->{"height"}
$x <= $size->{"width"} $y <= $size->{"height"} $y <= $size->{"width"} $x <= $size->{"height"}
Truth table0%0/7
A and B or C and D
A= $x <= $size->{"width"}B= $y <= $size->{"height"}C= $y <= $size->{"width"}D= $x <= $size->{"height"}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
643 ($y <= $size->{width} && $x <= $size->{height})) {
644 0 $best_fit = $size->{name};
645 0 last;
646 }
647 }
648 0 print STDOUT "best fit portrait: $best_fit" . $EOL;
649 0 return 1;
650 }
651  
652 # parsing of instruction
653 0 ▶sub _parse ($self, $ss) {
Subroutine _parse: not called CC=8 SCAR=38.8
654 0 my ( $opp, $x, $xcoord, $y, $ycoord, $rest );
655 # some lines can be ignored
656 0 ▶ if ( $ss =~ m{\A\s*\z} ) { return ( $NOOP, 0, 0 ) } # ignore empty line
Branch: 1 branch
BranchTrueFalseTotal
if ($ss =~ /\A\s*\z/u) 0 0 0
657 0 ▶ if ( $ss =~ m{\A\s*\N{LEFT PARENTHESIS}} ) {
Subroutine BEGIN: called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
if ($ss =~ /\A\s*\N{U+28}/u) 0 0 0
658 0 return ( $NOOP, 0, 0 );
659 } # ignore comment line
660 # do some standardization, different tools have different gcode formats
661 0 ▶ $ss =~ s{X}{X\N{SPACE}}; # in case there is no space after X or Y or Z
Subroutine BEGIN: called CC=1 SCAR=0.2
662 0 $ss =~ s{Y}{Y\N{SPACE}};
663 0 $ss =~ s{Z0}{Z\N{SPACE}0};
664 0 $ss =~ s{G0\N{SPACE}}{G00\N{SPACE}}; # G0 equivalent to G00
665 0 $ss =~ s{G1\N{SPACE}}{G01\N{SPACE}}; # G1 equivalent to G01
666 0 $ss =~ s{\N{SPACE}\N{FULL STOP}000}{\N{SPACE}0\N{FULL STOP}000}g; # turns .0 into 0.0
667 0 ( $opp, $x, $xcoord, $y, $ycoord, $rest ) = split( / +/, $ss ); # split on multiple spaces
668 0 ▶ if ( $ss eq $self->{penupcmd} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ss eq $self->{"penupcmd"}) 0 0 0
669 0 return ( $PU, '0', '0' );
670 }
671 0 ▶ if ( $ss =~ m{G00\N{SPACE}Z\N{SPACE}} || $ss =~ m{G01\N{SPACE}Z\N{SPACE}} )
Branch: 1 branch
BranchTrueFalseTotal
if ($ss =~ /G00\N{U+20}Z\N{U+20}/u or $ss =~ /G01\N{U+20}Z\N{U+20}/u) 0 0 0
Condition0%0/3
$ss =~ /G00\N{U+20}Z\N{U+20}/u or $ss =~ /G01\N{U+20}Z\N{U+20}/u
l!l&&r!l&&!r
000
MC/DC0%0/2
$ss =~ /G00\N{U+20}Z\N{U+20}/u or $ss =~ /G01\N{U+20}Z\N{U+20}/u
$ss =~ /G00\N{U+20}Z\N{U+20}/u $ss =~ /G01\N{U+20}Z\N{U+20}/u
Truth table0%0/3
A or B
A= $ss =~ /G00\N{U+20}Z\N{U+20}/uB= $ss =~ /G01\N{U+20}Z\N{U+20}/u
ABresult
1X1
011
000
672 {
673 0 return ( $PD, '0', '0' );
674 }
675 0 ▶ if ( $opp eq 'G00' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opp eq "G00") 0 0 0
676 0 return ( $G00, $xcoord, $ycoord );
677 }
678 0 ▶ if ( $opp eq 'G01' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opp eq "G01") 0 0 0
679 0 return ( $G01, $xcoord, $ycoord );
680 }
681 # the user can insert own instruction, e.g. in header - we should not complain about that
682 # if ($self->{check}) {print STDOUT "parse: unknown instruction \"$ss\""}
683 0 return ( $NOOP, 0, 0 );
684 }
685  
686 #
687 # for convenience
688 #
689 0 ▶sub _fprintf ($info, @args) {
Subroutine _fprintf: not called CC=2 SCAR=17.9
690 0 print STDOUT "$info: ";
691 0 foreach (@args) {
692 0 printf STDOUT "%5.2f ", $_
693 }
694 0 print STDOUT $EOL;
695 0 return 1;
696 }
697  
698 0 ▶sub getsegpath ($self) {
Subroutine getsegpath: not called CC=2 SCAR=17.9
699 0 ▶ if (scalar @{ $self->{psegments} } == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"psegments"};} == 0) 0 0 0
700 0 return ();
701 }
702 0 return @{ $self->{psegments} };
703 }
704  
705 __END__
« lib/Graphics/Penplotter/GcodeXY/SVG.pm ↑ Summary lib/Graphics/Penplotter/GcodeXY/Anamorphic.pm »