Elements.pm untested

filter: Statementstmt 0.0% 0 / 41 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 66 CC 7 SCAR 30.5
CC
7
Cov
0%
Write
42.0
addType
6.0
new
2.0
CRAP 21.1
1 # -*- Perl -*-
2 # PostScript::Elements.pm
3 # An object for representing lines, circles, boxes, and images
4 # such that they can be easily output as PostScript code.
5 #
6 # You may distribute this under the same terms as Perl
7 # itself.
8 #
9  
10 PostScript::Elements::VERSION = '0.06';
11  
12 package PostScript::Elements;
13 use strict;
14  
15 # @paramnames is the list of valid parameters,
16 # %defaults is the hash of default values for those parameters.
17 #
18 my @paramnames = qw( type points linewidth linetint filltint );
19 my %defaults = (
20 linewidth => 1, # 1 pt
21 filltint => -1, # -1 represents transparent
22 linetint => 0, # 0 == black, 1 = white
23 );
24  
25 sub new {
26 # The constructor method.
27 # An Element object can be one or more lines, circles,
28 # boxes or images.
29 #
30 my $class = shift;
31 my $self = [];
32
33 bless($self,$class);
34 return $self;
35 }
36  
37  
38 sub addType {
39 # A general routine for adding lines, circle, box, or
40 # image objects to an Element.
41 #
42 my $self = shift;
43 my %params = @_;
44
45 # If a parameter is not specified, use the default...
46 #
47 foreach (@paramnames) {
48 $params{$_} = $defaults{$_} unless (defined($params{$_}));
49 };
50
51 # Add the object to the current element
52 #
53 push @$self, { type => $params{'type'},
54 points => $params{'points'},
55 linewidth => $params{'linewidth'},
56 filltint => $params{'filltint'},
57 linetint => $params{'linetint'},
58 };
59 }
60  
61 sub addArc {
62 # Add an arc to the Element.
63 # A points parameter is required, consisting of a reference to
64 # list specifying the center coordinate, the radius
65 # of the arc, and two numbers specifying the starting angle
66 # and the ending angle describing the sweep of the arc. E.g.:
67 # addArc(points=>[50,50,25,0,360])
68 # would add a complete circle centered at 50,50 with a radius of 25.
69 #
70 my $self = shift;
71 $self->addType(type => 'arc',
72 @_);
73 }
74  
75  
76 sub addBox {
77 # Add a Box to the Element.
78 # The points parameter should consist of the upper left coordinate
79 # of the box, its width, and its height.
80 #
81 my $self = shift;
82 $self->addType(type => 'box',
83 @_);
84 }
85  
86  
87 sub addLine {
88 # Add a Line to the Element.
89 # The points parameter should contain the starting coordinate and
90 # the end coordinate.
91 #
92 my $self = shift;
93 $self->addType(type => 'line',
94 @_);
95 }
96  
97  
98 sub Write() {
99 # A method to create the PostScript code that will render the
100 # objects in the Element.
101 #
102 my $self = shift;
103 my $returnval = "";
104 my ($width, $height);
105  
106  
107 foreach my $element (@$self) {
108
109 # Generate the appropriate PostScript based on the
110 # type attribute.
111 # Arc:
112 if ($element->{type} =~ /arc/i) {
113 # First save the current path (if any),
114 # then create the path of the arc
115 #
116 $returnval .= "gsave\n".
117 $element->{'points'}->[0]." ". # x value
118 $element->{'points'}->[1]." ". # y value
119 $element->{'points'}->[2]." ". # radius
120 $element->{'points'}->[3]." ". # start angle
121 $element->{'points'}->[4]." ". # end angle
122 "arc\n";
123 # Don't fill the arc if filltint attribute is -1
124 # otherwise fill the current path with the tint specified by
125 # the filltint attribute. Save the path so it can be restored
126 # after the fill and it can be used by the stroke function.
127 #
128 if ($element->{'filltint'} >= 0 ) {
129 $returnval .= $element->{'filltint'}." setgray\n".
130 "gsave \nfill \ngrestore\n";
131 }
132
133 # Stroke the arc with the width indicated by linewidth
134 # and a greyscale percentage specified by linetint
135 #
136 $returnval .= $element->{'linetint'}." setgray\n".
137 $element->{'linewidth'}." setlinewidth\n".
138 "stroke\n".
139 "1 setgray\n".
140 $defaults{'linewidth'}." setlinewidth\n".
141 "grestore\n";
142
143 # Box:
144 #
145 } elsif ($element->{type} =~ /box/i) {
146 # A box is described by the upper left corner, a width
147 # and a height
148 #
149 $width = $element->{'points'}->[2];
150 $height = $element->{'points'}->[3];
151  
152 $returnval .= "gsave\n";
153 $returnval .= $element->{'points'}->[0]." ". # x value
154 $element->{'points'}->[1]." ". # y value
155 "moveto\n".
156 # now draw it clockwise...
157 #
158 "$width 0 rlineto\n".
159 "0 ".(0-$height)." rlineto\n".
160 (0-$width)." 0 rlineto\n".
161 "0 $height rlineto\n".
162 "closepath\n";
163 # Don't fill the box if filltint attribute is -1
164 #
165 if ($element->{'filltint'} >= 0 ) {
166 $returnval .= $element->{'filltint'}." setgray\n".
167 "gsave \nfill \ngrestore\n";
168 }
169 $returnval .= $element->{'linetint'}." setgray\n".
170 $element->{'linewidth'}." setlinewidth\n".
171 "stroke\n".
172 "1 setgray\n".
173 $defaults{'linewidth'}." setlinewidth\n".
174 "grestore\n";
175  
176 # Line:
177 #
178 } elsif ($element->{type} =~ /line/i) {
179  
180 $returnval .= "gsave\n";
181 $returnval .= $element->{'points'}->[0]." ". # start x
182 $element->{'points'}->[1]." ". # start y
183 "moveto\n".
184 $element->{'points'}->[2]." ". # end x
185 $element->{'points'}->[3]." ". # end y
186 "lineto\n".
187 $element->{'linetint'}." setgray\n".
188 $element->{'linewidth'}." setlinewidth\n".
189 "stroke\n".
190 "1 setgray\n".
191 $defaults{'linewidth'}." setlinewidth\n".
192 "grestore\n";
193  
194  
195 } else {
196 # Do nothing
197 }
198 }
199 return ($returnval);
200 }
201  
202  
203  
204 1;
205  
206 __END__