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

filter: Statementstmt 0.0% 0 / 49 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 86 CC 9 SCAR 38.3
CC
9
Cov
0%
vpype_linesort
90.0
BEGIN
2.0
BEGIN
2.0
CRAP 46.0
1 package Graphics::Penplotter::GcodeXY::Vpype 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 File::Temp qw( tempfile );
11  
12 # ---------------------------------------------------------------------------
13 # Graphics::Penplotter::GcodeXY::Vpype
14 # Role providing the vpype line-sort interface for GcodeXY.
15 # ---------------------------------------------------------------------------
16  
17 requires qw(exportsvg importsvg _croak);
18  
19 # ---------------------------------------------------------------------------
20 # Readonly my $EMPTY_STR -- private copy of the constant in GcodeXY.pm.
21 # ---------------------------------------------------------------------------
22  
23 use Readonly qw( Readonly );
24 Readonly my $EMPTY_STR => q{};
25  
26  
27 # ===========================================================================
28 # PUBLIC METHOD
29 # ===========================================================================
30  
31 # Run vpype's linemerge+linesort pipeline on the current drawing and return
32 # a new GcodeXY object containing the optimised path.
33 #
34 # Requires vpype to be installed and on $PATH.
35 # Returns a new object of the same class.
36 sub vpype_linesort ($self) {
37 # Create two temporary SVG files: one for input, one for output.
38 my ($fh_in, $vin) = tempfile( SUFFIX => '.svg' );
39 my ($fh_out, $vout) = tempfile( SUFFIX => '.svg' );
40 close $fh_in if defined $fh_in;
41 close $fh_out if defined $fh_out;
42  
43 # Export the current drawing to the input temp file.
44 $self->exportsvg($vin);
45  
46 # Build the vpype command.
47 my @cmd = (
48 'vpype',
49 'read', $vin,
50 'linemerge', '-t', '0.01mm',
51 'linesort',
52 'write', '--format', 'svg', $vout,
53 );
54 if ( defined $self->{papersize} && $self->{papersize} ne $EMPTY_STR ) {
55 splice @cmd, -2, 0, '--page-size', lc $self->{papersize};
56 }
57  
58 # Run vpype.
59 my $rc = system(@cmd);
60 if ( $rc != 0 ) {
61 unlink $vin if -e $vin;
62 unlink $vout if -e $vout;
63 $self->_croak("vpype_linesort: vpype command failed (exit=$rc)");
64 }
65  
66 # Instantiate a new object of the same class and import vpype's output.
67 my $class = ref($self) || $self;
68 my $v = $class->new(
69 papersize => $self->{papersize},
70 xsize => $self->{xsize},
71 ysize => $self->{ysize},
72 units => $self->{units},
73 header => $self->{header},
74 trailer => $self->{trailer},
75 penupcmd => $self->{penupcmd},
76 pendowncmd => $self->{pendowncmd},
77 margin => $self->{margin},
78 curvepts => $self->{curvepts},
79 check => $self->{check},
80 warn => $self->{warn},
81 hatchsep => $self->{hatchsep},
82 id => 'vpype-linesort',
83 optimize => $self->{optimize},
84 dscale => $self->{dscale},
85 opt_debug => $self->{opt_debug},
86 fontsize => $self->{fontsize},
87 fontname => $self->{fontname},
88 );
89 $v->importsvg($vout);
90  
91 # Clean up temp files.
92 unlink $vin if -e $vin;
93 unlink $vout if -e $vout;
94  
95 return $v;
96 }
97  
98  
99 1;
100  
101 __END__