blib/lib/Text/Format/Screenplay.pm

filter: Statementstmt 45.9% 73 / 159 Branchbran 37.1% 29 / 78 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 73.3% 11 / 15 Podpod 100% 2 / 2 totaltotal 41.5% 115 / 277 CC 47 SCAR 60.7
CC
47
Cov
40%
_create_outfile
870.5
_parse_infile
17.0
_put
6.0
CRAP 433.3
↑ Summary
1 package Text::Format::Screenplay;
2  
3 1 ▶use 5.008009;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use File::Slurp qw(read_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use PDF::Create;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Text::Wrap;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = '0.01';
11  
12 1 my $DOUBLE_DIALOG_LINES = 0;
13 1 my $SHOW_SCENE_NUMBERS = 0;
14  
15 1 my $CHAR_WIDTH = 7;
16 1 my $CHAR_HEIGHT = 12;
17 1 my $PAGE_WIDTH = 612;
18 1 my $PAGE_HEIGHT = 792;
19  
20 # parse infile
21 1 my $TITLE = '';
22 1 my @AUTHOR = ();
23 1 my @CONTACT = ();
24 1 my $DRAFT = '';
25 1 my $DATE = '';
26  
27 sub new
28 1 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
29 1 my ($class, @args) = @_;
30 1 my $self = bless {}, $class;
31 1 return $self->_init();
32 }
33  
34 sub _init
35 1 ▶{
Subroutine _init: called CC=1 SCAR=0.0
36 1 my ($self, %opts) = @_;
37 1 return $self;
38 }
39  
40 sub pdf
41 1 ▶{
Subroutine pdf: called CC=1 SCAR=0.0
Pod documented
42 1 my ($self, $filename, $outfile) = @_;
43  
44 # structure of $scenes: each element: [ 'type', 'content' ]
45 1 my $scenes = $self->_parse_infile( read_file($filename) );
46
47 1 $self->_create_outfile( $scenes, $outfile );
48 }
49  
50 sub _parse_infile
51 1 ▶{
Subroutine _parse_infile: called CC=17 SCAR=28.3
52 1 my ($self, @lines) = @_;
53 1 my @sp;
54
55 1 my $i; my $j;
56 1 ▶ for ($i=0; $i<scalar(@lines); $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @lines) 17 1 18
57 {
58 17 ▶ next if $lines[$i] =~ /^[\s\t\n\r]*$/; # ignore blank lines
Branch: 1 branch
BranchTrueFalseTotal
if $lines[$i] =~ /^[\s\t\n\r]*$/ 0 17 17
59 17 ▶ next if $lines[$i] =~ /^[\s\t\n\r]*\#/; # ignore comment lines
Branch: 1 branch
BranchTrueFalseTotal
if $lines[$i] =~ /^[\s\t\n\r]*\#/ 0 17 17
60
61 # parse next paragraph
62 17 my $para = '';
63 17 ▶ for ($j=$i; $j<scalar(@lines); $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < scalar @lines) 52 1 53
64 52 chomp($lines[$j]);
65 52 ▶ next if $lines[$j] =~ /^[\s\t\n\r]*\#/; # ignore comment lines
Branch: 1 branch
BranchTrueFalseTotal
if $lines[$j] =~ /^[\s\t\n\r]*\#/ 0 52 52
66 52 ▶ last if $lines[$j] =~ /^[\s\t\n\r]*$/; # ignore blank lines
Branch: 1 branch
BranchTrueFalseTotal
if $lines[$j] =~ /^[\s\t\n\r]*$/ 16 36 52
67 36 $para .= $lines[$j];
68 }
69 17 $i = $j;
70
71 # split into pre-colon and post-colon parts
72 17 my ($pre, $post) = $para =~ /^([^\:\~\>]*[\:\~\>]?)(.*)$/;
73 17 ▶ $post = '' unless $post;
Branch: 1 branch
BranchTrueFalseTotal
unless $post 4 13 17
74
75 17 ▶ if ($pre eq 'title:') {
Branch: 9 branches
BranchTrueFalseTotal
if ($pre eq 'title:') { } 1 16 17
elsif ($pre eq 'author:') { } 1 15 16
elsif ($pre eq 'contact:') { } 1 14 15
elsif ($pre eq 'draft:') { } 1 13 14
elsif ($pre eq 'date:') { } 1 12 13
elsif ($pre eq ':') { } 3 9 12
elsif ($pre eq '>') { } 1 8 9
elsif ($pre eq '~') { } 1 7 8
elsif ($pre =~ /^.{1,20}\:$/) { } 2 5 7
76 1 ($TITLE) = uc($post) =~ /^[\s\t]*(.*)[\s\t\r\n]*$/;
77 } elsif ($pre eq 'author:') {
78 1 @AUTHOR = map { s/^[\s\t]*(.*)[\s\t\r\n]*$/$1/; $_ } split /;/, $post;
79 } elsif ($pre eq 'contact:') {
80 1 @CONTACT = map { s/^[\s\t]*(.*)[\s\t\r\n]*$/$1/; $_ } split /;/, $post;
81 } elsif ($pre eq 'draft:') {
82 1 $DRAFT = $post;
83 } elsif ($pre eq 'date:') {
84 1 $DATE = $post;
85 } elsif ($pre eq ':') {
86 3 push @sp, ['s', $post];
87 } elsif ($pre eq '>') {
88 1 push @sp, ['t', $post];
89 } elsif ($pre eq '~') {
90 1 push @sp, ['c', $post];
91 } elsif ($pre =~ /^.{1,20}\:$/) {
92 2 $pre =~ s/\:$//;
93 2 push @sp, [$pre, $post];
94 } else {
95 5 push @sp, ['d', $pre];
96 }
97 }
98
99 1 return \@sp;
100 }
101  
102 sub _create_outfile
103 1 ▶{
Subroutine _create_outfile: called CC=30 SCAR=67.7
104 1 my ($self, $sp, $outfile) = @_;
105  
106 1 my $pdf = new PDF::Create
107 ('filename' => $outfile,
108 'Version' => 1.2,
109 'PageMode' => 'UseOutlines',
110 'Author' => join(', ', @AUTHOR),
111 'Title' => $TITLE,
112 );
113 1 my $root = $pdf->new_page('Media-Box' => [0, $PAGE_WIDTH, 0, $PAGE_HEIGHT]);
114 0 my $font = $pdf->font
115 ('Subtype' => 'Type1',
116 'Encoding' => 'WinAnsiEncoding',
117 'BaseFont' => 'CourierNew');
118  
119 0 my $mid = sprintf "%.0f", ($PAGE_WIDTH / $CHAR_WIDTH / 2);
120  
121 #
122 # title page
123 #
124 0 my $page = $root->new_page; # inherit from root
125  
126 0 my $line = 25;
127 0 $line = _put($page, $font, $TITLE, $line, _mid($mid,$TITLE));
128 0 $line += 4;
129 0 $line = _put($page, $font, "written by", $line, _mid($mid,"written by"));
130 0 $line++;
131 0 foreach (@AUTHOR) { $line = _put($page, $font, $_, $line, _mid($mid,$_)) }
132 0 _put($page, $font, join("\n",@CONTACT), 61-scalar(@CONTACT), 15);
133 0 _put($page, $font, $DRAFT, 56, 62);
134 0 _put($page, $font, $DATE, 60, 62);
135  
136 #
137 # script pages
138 #
139 0 my $pnum = 1; # page counter
140 0 my $snum = 1; # scene counter
141
142 0 $page = $root->new_page;
143 0 _put($page, $font, sprintf("%10s", "$pnum."), 4, 62);
144 0 $line = 6;
145 0 ▶ for (my $e=0; $e<@{$sp}; $e++)
Branch: 1 branch
BranchTrueFalseTotal
if ($e < @{$sp;}) 0 0 0
146 {
147 0 my $type = $sp->[$e]->[0];
148 0 my $text = $sp->[$e]->[1];
149
150 0 ▶ if ($type !~ /^[stcd]$/) { $text =~ s/\(/\n\(/g; $text =~ s/\)/)\n/g }
Branch: 1 branch
BranchTrueFalseTotal
unless ($type =~ /^[stcd]$/) 0 0 0
151  
152 # check if current page is full
153 0 my $till = 0; # till what line the next element needs space
154 0 ▶ if ($type eq 's') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 's') { } 0 0 0
elsif ($type eq 't' or $type eq 'c') { } 0 0 0
elsif ($type eq 'd') { } 0 0 0
155 # own + 1 line space + space for next part
156 0 $till = $line + _sizeof($text,1000);
157 0 $till += 10;
158 ▶ } elsif ($type eq 't' || $type eq 'c') {
Condition0%0/3
$type eq "t" or $type eq "c"
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "t" or $type eq "c"
$type eq "t" $type eq "c"
Truth table0%0/3
A or B
A= $type eq "t"B= $type eq "c"
ABresult
1X1
011
000
159 # one line (+ 2 empty lines of not last line)
160 0 $till = $line + 1;
161 0 ▶ $till += 2 unless $till == 60;
Branch: 1 branch
BranchTrueFalseTotal
unless $till == 60 0 0 0
162 } elsif ($type eq 'd') {
163 # own size (+ 1 empty line if not on last line)
164 0 $till = $line + _sizeof($text,54);
165 0 ▶ $till++ unless $till == 60;
Branch: 1 branch
BranchTrueFalseTotal
unless $till == 60 0 0 0
166 } else {
167 # name line + dialog lines
168 0 $till = $line + 1 + _sizeof($text,32);
169 0 ▶ $till += _sizeof($text,32) if $DOUBLE_DIALOG_LINES;
Branch: 1 branch
BranchTrueFalseTotal
if $DOUBLE_DIALOG_LINES 0 0 0
170 }
171
172 # create new page
173 0 ▶ if ($till > 60) {
Branch: 1 branch
BranchTrueFalseTotal
if ($till > 60) 0 0 0
174 0 $pnum++;
175 0 $page = $root->new_page;
176 0 _put($page, $font, sprintf("%10s", "$pnum."), 4, 62);
177 0 $line = 6;
178 0 next;
179 }
180  
181 # add current element + space to page
182 0 ▶ if ($type eq 's') {
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq 's') { } 0 0 0
elsif ($type eq 't') { } 0 0 0
elsif ($type eq 'c') { } 0 0 0
elsif ($type eq 'd') { } 0 0 0
183 0 ▶ if ($SHOW_SCENE_NUMBERS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($SHOW_SCENE_NUMBERS) 0 0 0
184 0 _put($page, $font, sprintf("%5s",$snum), $line, 13);
185 0 _put($page, $font, $snum, $line, 74);
186 0 $snum++;
187 }
188 0 $line = _put($page, $font, $text, $line, 19);
189 0 $line++;
190 } elsif ($type eq 't') {
191 0 $line = _put($page, $font, $text, $line, 62);
192 0 ▶ $line += 2 unless $line == 60;
Branch: 1 branch
BranchTrueFalseTotal
unless $line == 60 0 0 0
193 } elsif ($type eq 'c') {
194 0 $line = _put($page, $font, $text, $line, _mid($mid,$text));
195 0 ▶ $line += 2 unless $line == 60;
Branch: 1 branch
BranchTrueFalseTotal
unless $line == 60 0 0 0
196 } elsif ($type eq 'd') {
197 0 $line = _put($page, $font, _wrapit($text,54), $line, 19);
198 0 ▶ if (($e+1 < scalar(@{$sp})) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($e + 1 < scalar @{$sp;} and $$sp[$e + 1][0] eq 't' || $$sp[$e + 1][0] eq 'c' || $$sp[$e + 1][0] eq 's') { } 0 0 0
Condition0%0/9
$e + 1 < scalar @{$sp;} and $$sp[$e + 1][0] eq "t" || $$sp[$e + 1][0] eq "c" || $$sp[$e + 1][0] eq "s"
!ll&&!rl&&r
000
$$sp[$e + 1][0] eq "t" or $$sp[$e + 1][0] eq "c" or $$sp[$e + 1][0] eq "s"
l!l&&r!l&&!r
000
$$sp[$e + 1][0] eq "t" or $$sp[$e + 1][0] eq "c"
l!l&&r!l&&!r
000
MC/DC0%0/4
$e + 1 < scalar @{$sp;} and $$sp[$e + 1][0] eq "t" || $$sp[$e + 1][0] eq "c" || $$sp[$e + 1][0] eq "s"
$e + 1 < scalar @{$sp;} $$sp[$e + 1][0] eq "t" $$sp[$e + 1][0] eq "c" $$sp[$e + 1][0] eq "s"
Truth table0%0/5
A and B or C or D
A= $e + 1 < scalar @{$sp;}B= $$sp[$e + 1][0] eq "t"C= $$sp[$e + 1][0] eq "c"D= $$sp[$e + 1][0] eq "s"
ABCDresult
0XXX0
10000
11XX1
101X1
10011
199 ($sp->[$e+1]->[0] eq 't' || $sp->[$e+1]->[0] eq 'c' ||
200 $sp->[$e+1]->[0] eq 's')) {
201 0 ▶ $line += 2 unless $line == 60;
Branch: 1 branch
BranchTrueFalseTotal
unless $line == 60 0 0 0
202 } else {
203 0 ▶ $line++ unless $line == 60;
Branch: 1 branch
BranchTrueFalseTotal
unless $line == 60 0 0 0
204 }
205 } else {
206 0 $line = _put($page, $font, $type, $line, 43);
207 0 $text = _wrapit($text,32); $text =~ s/\(/ \(/g;
208 0 ▶ if ($DOUBLE_DIALOG_LINES) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DOUBLE_DIALOG_LINES) 0 0 0
209 0 $text =~ s/\n/\n\n/g; $text = "\n$text" }
210 0 $line = _put($page, $font, $text, $line, 29);
211 0 ▶ if (($e+1 < scalar(@{$sp})) && ($sp->[$e+1]->[0] !~ /^stcd$/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e + 1 < scalar @{$sp;} and not $$sp[$e + 1][0] =~ /^stcd$/) { } 0 0 0
Condition0%0/3
$e + 1 < scalar @{$sp;} and not $$sp[$e + 1][0] =~ /^stcd$/
!ll&&!rl&&r
000
MC/DC0%0/2
$e + 1 < scalar @{$sp;} and not $$sp[$e + 1][0] =~ /^stcd$/
$e + 1 < scalar @{$sp;} not $$sp[$e + 1][0] =~ /^stcd$/
Truth table0%0/3
A and not(B)
A= $e + 1 < scalar @{$sp;}B= not $$sp[$e + 1][0] =~ /^stcd$/
ABresult
0X0
100
111
212 0 ▶ $line++ unless $line == 60;
Branch: 1 branch
BranchTrueFalseTotal
unless $line == 60 0 0 0
213 } else {
214 0 ▶ $line += 2 unless $line == 60;
Branch: 1 branch
BranchTrueFalseTotal
unless $line == 60 0 0 0
215 }
216 }
217 }
218  
219 0 $pdf->close;
220 }
221  
222 sub _sizeof
223 0 ▶{
Subroutine _sizeof: not called CC=1 SCAR=6.9
224 0 my ($text, $width) = @_;
225 0 $text = _wrapit($text, $width);
226 0 my @lines = split /\n/, $text;
227 0 return scalar(@lines);
228 }
229  
230 sub _wrapit
231 0 ▶{
Subroutine _wrapit: not called CC=1 SCAR=6.9
232 0 my ($text, $width) = @_;
233 0 $Text::Wrap::columns = $width;
234 0 $text = Text::Wrap::wrap("", "", $text);
235 0 $text =~ s/\s*\n\s*/\n/g;
236 0 $text =~ s/^\s*//g;
237 0 return $text;
238 }
239  
240 sub _mid
241 0 ▶{
Subroutine _mid: not called CC=1 SCAR=6.9
242 0 my ($mid, $str) = @_;
243 0 return sprintf("%.0f", $mid - (length($str) / 2));
244 }
245  
246 # places a text block onto a page, starting at a certain position
247 sub _put
248 0 ▶{
Subroutine _put: not called CC=2 SCAR=17.9
249 0 my ($page, $font, $text, $line, $column) = @_;
250 0 foreach (split /\n/, $text) {
251 0 $page->stringl($font, 10,
252 ($column * $CHAR_WIDTH),
253 ($PAGE_HEIGHT - ($line * $CHAR_HEIGHT)), $_);
254 0 $line++;
255 }
256 0 return $line;
257 }
258  
259 1 1;
260 __END__
↑ Summary