Writer.pm

filter: Statementstmt 69.7% 196 / 281 Branchbran 44.6% 83 / 186 Conditioncond 30.4% 39 / 128 MC/DCmcdc 6.0% 4 / 66 Subroutinesub 55.3% 26 / 47 Podpod 63.6% 21 / 33 totaltotal 49.8% 369 / 741 CC 143 SCAR 45.2
CC
143
Cov
53%
prolog
291.7
table
132.0
printf
90.0
CRAP 91.5
↑ Summary Writer/TableRowDecl.pm »
1  
2 6 require 5.005; # we need m/...\z/
3 package RTF::Writer;
4 6 ▶use strict; # Time-stamp: "2003-11-04 02:13:08 AST"
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶BEGIN { eval {require utf8}; $INC{"utf8.pm"} = "dummy_value" if $@ }
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 6 6
7 # hack to allow "use utf8" under old Perls
8 6 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 6 die sprintf "%s can't work (yet) in a non-ASCII world", __PACKAGE__
11 unless chr(65) eq 'A';
12  
13 6 ▶use vars qw($VERSION @ISA @EXPORT_OK
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 $AUTOLOAD $AUTO_NL $WRAP @Escape);
15  
16 6 ▶$AUTO_NL = 1 unless defined $AUTO_NL; # TODO: document
Branch: 1 branch
BranchTrueFalseTotal
unless defined $RTF::Writer::AUTO_NL 6 0 6
17 6 ▶$WRAP = 1 unless defined $WRAP; # TODO: document
Branch: 1 branch
BranchTrueFalseTotal
unless defined $RTF::Writer::WRAP 6 0 6
18  
19 6 require Exporter;
20 6 @ISA = ('Exporter');
21 6 $VERSION = '1.11';
22 6 @EXPORT_OK = qw( inch inches in point points pt cm rtfesc );
23  
24 sub DEBUG () {0}
25 6 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 6 ▶use RTF::Writer::TableRowDecl ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 #**************************************************************************
29  
30 0 ▶sub CHARSET_LATIN1 {
Subroutine CHARSET_LATIN1: not called CC=1 SCAR=6.9
Pod undocumented
31 0 $Escape[0xA0] = "\\~";
32 0 $Escape[0xAD] = "\\-";
33 0 return;
34 }
35  
36 0 ▶sub CHARSET_UNICODE {
Subroutine CHARSET_UNICODE: not called CC=1 SCAR=6.9
Pod undocumented
37 0 $Escape[0xA0] = "\\~";
38 0 $Escape[0xAD] = "\\-";
39 0 return;
40 }
41  
42 0 ▶sub CHARSET_OTHER {
Subroutine CHARSET_OTHER: not called CC=1 SCAR=6.9
Pod undocumented
43 0 $Escape[0xA0] = "\\'a0";
44 0 $Escape[0xAD] = "\\'ad";
45 0 return;
46 }
47  
48 #--------------------------------------------------------------------------
49 # Init:
50  
51 # Using an array for this avoids some problems with nasty UTF8 bugs in
52 # hash lookup algorithms.
53  
54 6 @Escape = map sprintf("\\'%02x", $_), 0x00 .. 0xFF;
55 6 foreach my $i ( 0x20 .. 0x7E ) { $Escape[$i] = chr($i) }
56  
57 {
58 6 my @refinements = (
59 "\\" => "\\'5c",
60 "{" => "\\'7b",
61 "}" => "\\'7d",
62
63 "\cm" => '',
64 "\cj" => '',
65 "\n" => "\n\\line ",
66 # This bit of voodoo means that whichever of \cm | \cj isn't synonymous
67 # with \n, is aliased to empty-string, and whichever of them IS "\n",
68 # turns into the "\n\\line ".
69
70 "\t" => "\\tab ", # Tabs (altho theoretically raw \t's might be okay)
71 "\f" => "\n\\page\n", # Formfeed
72 "-" => "\\_", # Turn plaintext '-' into a non-breaking hyphen
73 # I /think/ that's for the best.
74 "\xA0" => "\\~", # \xA0 is Latin-1/Unicode non-breaking space
75 "\xAD" => "\\-", # \xAD is Latin-1/Unicode soft (optional) hyphen
76 '.' => "\\'2e",
77 'F' => "\\'46",
78 );
79 6 my($char, $esc);
80 6 ▶ while(@refinements) {
Branch: 1 branch
BranchTrueFalseTotal
if (@refinements) 78 6 84
81 78 ($char, $esc) = splice @refinements,0,2;
82 78 $Escape[ord $char] = $esc;
83 }
84 }
85  
86 #--------------------------------------------------------------------------
87  
88 # The conversion functions, for export:
89 0 ▶sub inch { int(.5 + $_[0] * 1440) }
Subroutine inch: not called CC=1 SCAR=6.9
Pod documented
90 0 ▶sub inches { int(.5 + $_[0] * 1440) }
Subroutine inches: not called CC=1 SCAR=6.9
Pod documented
91 0 ▶sub in { int(.5 + $_[0] * 1440) }
Subroutine in: not called CC=1 SCAR=6.9
Pod documented
92 0 ▶sub points { int(.5 + $_[0] * 20) }
Subroutine points: not called CC=1 SCAR=6.9
Pod documented
93 0 ▶sub point { int(.5 + $_[0] * 20) }
Subroutine point: not called CC=1 SCAR=6.9
Pod documented
94 0 ▶sub pt { int(.5 + $_[0] * 20) }
Subroutine pt: not called CC=1 SCAR=6.9
Pod documented
95 0 ▶sub cm { int(.5 + $_[0] * (1440 / 2.54) ) } # approx 567
Subroutine cm: not called CC=1 SCAR=6.9
Pod documented
96  
97 1 ▶sub rtfesc {
Subroutine rtfesc: called CC=12 SCAR=40.0
Pod documented
98 # Note that this doesn't apply our wrapping algorithm, because
99 # I don't forsee this being used for many-line things.
100
101 1 ▶ shift if @_ and ref($_[0] || '') and UNIVERSAL::isa($_[0], __PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if @_ and ref($_[0] || "") and UNIVERSAL::isa($_[0], __PACKAGE__) 0 1 1
Condition37%3/8
@_ and ref($_[0] || "") and UNIVERSAL::isa($_[0], __PACKAGE__)
!ll&&!rl&&r
100
@_ and ref($_[0] || "")
!ll&&!rl&&r
010
$_[0] || ''
l!l
10
MC/DC0%0/3
@_ and ref($_[0] || "") and UNIVERSAL::isa($_[0], __PACKAGE__)
@_ $_[0] UNIVERSAL::isa($_[0], __PACKAGE__)
Truth table0%0/4
A and B and C
A= @_B= $_[0]C= UNIVERSAL::isa($_[0], __PACKAGE__)
ABCresult
0XX0
10X0
1100
1111
102 # that's so we can double as a method
103
104 1 my $x; # scratch
105 1 ▶ if(!defined wantarray) { # void context: alter in-place!
Branch: 2 branches
BranchTrueFalseTotal
if (not defined wantarray) { } 0 1 1
elsif (wantarray) { } 0 1 1
106 0 for(@_) {
107 0 s/([F\.\x00-\x1F\-\\\{\}\x7F-\xFF])/$Escape[ord$1]/g; # ESCAPER
108 0 ▶ s/([^\x00-\xFF])/'\\uc1\\u'.((ord($1)<32768)?ord($1):(ord($1)-65536)).'?'/eg;
Branch: 1 branch
BranchTrueFalseTotal
ord $1 < 32768 ? : 0 0 0
109 # We escape F and . because when they're line-initial (or alone
110 # on a line), some mailers eat them or freak out.
111 }
112 0 return;
113 } elsif(wantarray) { # return an array
114 0 return map {;
115 0 ($x = $_) =~
116 s/([F\.\x00-\x1F\-\\\{\}\x7F-\xFF])/$Escape[ord$1]/g; # ESCAPER
117 0 $x =~
118 0 ▶ s/([^\x00-\xFF])/'\\uc1\\u'.((ord($1)<32768)?ord($1):(ord($1)-65536)).'?'/eg;
Branch: 1 branch
BranchTrueFalseTotal
ord $1 < 32768 ? : 0 0 0
119 0 $x;
120 } @_;
121 } else { # return a single scalar
122 1 ▶ ($x = ((@_ == 1) ? $_[0] : join '', @_)
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 1 0 1
123 ) =~
124 s/([F\.\x00-\x1F\-\\\{\}\x7F-\xFF])/$Escape[ord$1]/g; # ESCAPER
125 # Escape \, {, }, -, control chars, and 7f-ff.
126 1 $x =~
127 0 ▶ s/([^\x00-\xFF])/'\\uc1\\u'.((ord($1)<32768)?ord($1):(ord($1)-65536)).'?'/eg;
Branch: 1 branch
BranchTrueFalseTotal
ord $1 < 32768 ? : 0 0 0
128  
129 1 return $x;
130 }
131 }
132  
133  
134 #**************************************************************************
135  
136 6 ▶sub new_to_file {
Subroutine new_to_file: called CC=4 SCAR=14.2
Pod documented
137 # just a wrapper around new_to_fh
138 6 my $class = shift;
139 6 ▶ defined $_[0] or Carp::croak "undef isn't a good filename for new_to_file";
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] or Carp::croak("undef isn't a good filename for new_to_file") 6 0 6
140 6 ▶ length $_[0] or Carp::croak "\"\" isn't a good filename for new_to_file";
Branch: 1 branch
BranchTrueFalseTotal
length $_[0] or Carp::croak("\"\" isn't a good filename for new_to_file") 6 0 6
141 6 local(*FH);
142 6 ▶ open(FH, ">$_[0]") or Carp::croak "Can't write-open $_[0]: $!";
Branch: 1 branch
BranchTrueFalseTotal
open FH, ">$_[0]" or Carp::croak("Can't write-open $_[0]: $!") 6 0 6
143 6 DEBUG and print "Opened-file $_[0] -> ", *FH{IO}, "\n";
144 6 my $new = $class->new_to_fh(*FH{IO});
145 6 return $new;
146 }
147  
148 0 ▶sub new_to_filehandle { shift->new_to_handle(@_) }
Subroutine new_to_filehandle: not called CC=1 SCAR=6.9
Pod undocumented
149 0 ▶sub new_to_handle { shift->new_to_fh( @_) }
Subroutine new_to_handle: not called CC=1 SCAR=6.9
Pod documented
150  
151 6 ▶sub new_to_fh { # legacy
Subroutine new_to_fh: called CC=4 SCAR=15.2
Pod undocumented
152 6 ▶ Carp::croak "Open to what filehandle?"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] and length $_[1] 0 6 6
Condition33%1/3
defined $_[1] and length $_[1]
!ll&&!rl&&r
006
MC/DC0%0/2
defined $_[1] and length $_[1]
defined $_[1] length $_[1]
Truth table33%1/3
A and B
A= defined $_[1]B= length $_[1]
ABresult
0X0
100
111
153 unless defined $_[1] and length $_[1];
154 6 my $fh = $_[1];
155 6 DEBUG and print "Opened-fh $fh\n";
156  
157 6 my $class = shift;
158 6 my $last_was_command = 0;
159 6 ▶ my $new = bless [
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
060
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
160 _make_emitter_closure($fh),
161 '', # things to be printed, on closing
162 $fh,
163 ], ref($class) || $class;
164 6 return $new;
165 }
166  
167 1 ▶sub new_to_string {
Subroutine new_to_string: called CC=4 SCAR=15.9
Pod documented
168 1 ▶ Carp::croak "Open to what scalar-ref?"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] and ref $_[1] eq "SCALAR" 0 1 1
Condition33%1/3
defined $_[1] and ref $_[1] eq "SCALAR"
!ll&&!rl&&r
001
MC/DC0%0/2
defined $_[1] and ref $_[1] eq "SCALAR"
defined $_[1] ref $_[1] eq "SCALAR"
Truth table33%1/3
A and B
A= defined $_[1]B= ref $_[1] eq "SCALAR"
ABresult
0X0
100
111
169 unless defined $_[1] and ref($_[1]) eq 'SCALAR';
170 1 my($class, $sr) = @_;
171 1 DEBUG and print "Opened-sr $sr\n";
172  
173 1 ▶ my $new = bless [
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
174 _make_emitter_closure(undef,$sr),
175 '', # things to be printed, on closing
176 undef,
177 ], ref($class) || $class;
178 1 return $new;
179 }
180  
181 #**************************************************************************
182 # Think twice before outright overriding this method:
183  
184 520 ▶sub print {
Subroutine print: called CC=3 SCAR=11.4
Pod documented
185 520 ▶ ref $_[0] or Carp::croak(__PACKAGE__ .
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] or Carp::croak("RTF::Writer's print(...) is supposed to be an object method!") 520 0 520
186 "'s print(...) is supposed to be an object method!");
187 520 DEBUG > 1 and print "Calling $_[0][0]\n";
188 goto &{
189 520 ▶ $_[0][0] || # call the closure
Condition50%1/2
$_[0][0] || Carp::croak('That RTF::Writer object has been closed!?')
l!l
5200
MC/DC0%0/1
$_[0][0] || Carp::croak('That RTF::Writer object has been closed!?')
$_[0][0]
Truth table50%1/2
A
A= $_[0][0]
Aresult
11
00
190 Carp::croak("That " . __PACKAGE__ . " object has been closed!?")
191 };
192 }
193  
194 #**************************************************************************
195 0 ▶sub printf {
Subroutine printf: not called CC=9 SCAR=45.0
Pod documented
196 0 ▶ ref $_[0] or Carp::croak(__PACKAGE__ .
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] or Carp::croak("RTF::Writer's printf(...) is supposed to be an object method!") 0 0 0
197 "'s printf(...) is supposed to be an object method!");
198 0 my($it,$format) = splice(@_,0,2);
199 0 ▶ $format = '' unless defined $format;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $format 0 0 0
200
201 0 ▶ if(ref($format) ne 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $format ne 'SCALAR') { } 0 0 0
202 # Example: $it->printf("%04d: %s\n", @stuff)
203 0 DEBUG and print "Nonescaped format <$format> on <@_>\n";
204 0 my $x = sprintf($format, @_);
205 0 DEBUG and print "Formatted (not yet esc): $x\n";
206 0 $it->print( $x );
207 # And, in escaping, this will be wrapped.
208 } else {
209 # Example: $it->printf(\'{\f30\b %s:} {\i %d}', @stuff)
210 0 DEBUG and print "Escaped format <", $$format, "> on <@_>\n";
211  
212 0 my $str; # scratch
213
214 # Escape anything non-numeric:
215 0 ▶ for(my $i = 0; $i < @_; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 0 0 0
216 0 ▶ next if !defined($_[$i]) or !length($_[$i]) or
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[$i] && length $_[$i] or $_[$i] =~ /^[+-]?(?=\d|\.\d)\d*(?:\.\d*)?(?:[Ee](?:[+-]?\d+))?\z/s 0 0 0
Condition0%0/6
not defined $_[$i] && length $_[$i] or $_[$i] =~ /^[+-]?(?=\d|\.\d)\d*(?:\.\d*)?(?:[Ee](?:[+-]?\d+))?\z/s
l!l&&r!l&&!r
000
defined $_[$i] && length $_[$i]
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $_[$i] && length $_[$i] or $_[$i] =~ /^[+-]?(?=\d|\.\d)\d*(?:\.\d*)?(?:[Ee](?:[+-]?\d+))?\z/s
defined $_[$i] length $_[$i] $_[$i] =~ /^[+-]?(?=\d|\.\d)\d*(?:\.\d*)?(?:[Ee](?:[+-]?\d+))?\z/s
Truth table0%0/4
not(A && B) or C
A= defined $_[$i]B= length $_[$i]C= $_[$i] =~ /^[+-]?(?=\d|\.\d)\d*(?:\.\d*)?(?:[Ee](?:[+-]?\d+))?\z/s
ABCresult
0XX1
10X1
1111
1100
217 $_[$i] =~ m/^[+-]?(?=\d|\.\d)\d*(?:\.\d*)?(?:[Ee](?:[+-]?\d+))?\z/s;
218
219 0 ($str = $_[$i]) =~
220 s/([F\.\x00-\x1F\-\\\{\}\x7F-\xFF])/$Escape[ord$1]/g; # ESCAPER
221 0 $str =~
222 0 ▶ s/([^\x00-\xFF])/'\\uc1\\u'.((ord($1)<32768)?ord($1):(ord($1)-65536)).'?'/eg;
Branch: 1 branch
BranchTrueFalseTotal
ord $1 < 32768 ? : 0 0 0
223  
224 # Don't bother applying wrapping, I guess.
225
226 0 DEBUG > 2 and print "Escaping <$_[$i]> to <$str>\n";
227 0 splice @_, $i, 1, $str;
228 # MAGIC! makes it so we don't alter the original.
229 }
230  
231 0 my $x = sprintf $$format, @_;
232 0 DEBUG and print "Formatted (esc): $x\n";
233 0 $it->print( \$x ); # No wrapping applied.
234  
235 # We mustn't escape things that we might intend, in the sprintf
236 # format, to treat as numbers, since escaping would turn '-'
237 # to '\_', and that would turn something numeric like "-14"
238 # or "1.5E-9" into something non-numeric like "\_14"
239 # or "1.5E\_9". So we use this regexp.
240 # The solution here /could/ fail to apply the escaping of
241 # "-" -> "\_", to number-seeming things we were really going
242 # to use as strings, but that seems relatively harmless.
243 # The only completely correct way to do that would be to
244 # completely reimplement sprintf in pure Perl, or at least
245 # enough of it that we parse the format -- so not only could we
246 # tell what items from @_ were to be treated as numbers and
247 # which as strings, but also so we could take the output of
248 # formatting numbers, and /then/ apply the '-' -> '\_'
249 # escaping.
250 # However, the /only/ benefit of this would be to get the
251 # '-' -> '\_' escaping to apply. And in practice, this could
252 # be a problem only in two cases: a leading minus-sign, as
253 # in '-53.3', which presumably won't occur in a context
254 # where a word-processor would hyphenate; and after an "E",
255 # as in "1.5E-9". While it's more likely that a word-precessor
256 # might hyphenate there, I that think scientific-notation
257 # numbers are in practive relatively rare. So there.
258 }
259 }
260  
261 #--------------------------------------------------------------------------
262 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=5 SCAR=34.0
263 0 DEBUG and print "**** $_[0] hits autoload for $AUTOLOAD\n";
264 0 ▶ if(ref($_[0]) and $AUTOLOAD =~ m<::([A-Z][a-z]*(?:_?[0-9]+)?)$>s) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] and $RTF::Writer::AUTOLOAD =~ /::([A-Z][a-z]*(?:_?[0-9]+)?)$/s) { } 0 0 0
Condition0%0/3
ref $_[0] and $RTF::Writer::AUTOLOAD =~ /::([A-Z][a-z]*(?:_?[0-9]+)?)$/s
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and $RTF::Writer::AUTOLOAD =~ /::([A-Z][a-z]*(?:_?[0-9]+)?)$/s
ref $_[0] $RTF::Writer::AUTOLOAD =~ /::([A-Z][a-z]*(?:_?[0-9]+)?)$/s
Truth table0%0/3
A and B
A= ref $_[0]B= $RTF::Writer::AUTOLOAD =~ /::([A-Z][a-z]*(?:_?[0-9]+)?)$/s
ABresult
0X0
100
111
265 0 my $cmd = "\\" . lc($1);
266 0 $cmd =~ tr<_><->; # So: $x->fi_180 -> $x->print(\'\fs-180')
267 0 my $it = shift;
268 0 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
269 0 return $it->print(\'{', \$cmd, @_, \'}');
270 # So: $it->Lang1234(...) -> $it->print([\'\lang123', ... ]);
271 # (Well, the { ... } is just an incidental optimization.)
272 } else {
273 0 return $it->print(\$cmd);
274 # So: $it->Lang1234() -> $it->print(\'\lang123');
275 }
276 } else {
277 0 ▶ Carp::croak "Can't locate object method \"$AUTOLOAD\" via package \""
Condition0%0/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table0%0/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
278 . (ref($_[0]) || $_[0]) . '"';
279 }
280 }
281  
282 #--------------------------------------------------------------------------
283 7 ▶sub close {
Subroutine close: called CC=3 SCAR=11.0
Pod documented
284 7 ▶ return unless $_[0][0]; # Already closed?!
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0][0] 0 7 7
285 7 DEBUG > 1 and print "Closing $_[0]\n";
286 7 ▶ $_[0]->print(\$_[0][1]) if length $_[0][1];
Branch: 1 branch
BranchTrueFalseTotal
if length $_[0][1] 6 1 7
287 7 undef $_[0][0]; # ...presumably clausing any FH to close and destroy.
288 7 $_[0][1] = '';
289 7 return;
290 }
291  
292 #--------------------------------------------------------------------------
293 7 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=14.2
294 # just a rudimentary version of $fh->close()
295 7 ▶ $_[0]->print(\$_[0][1]) if $_[0][0] and $_[0][1];
Branch: 1 branch
BranchTrueFalseTotal
if $_[0][0] and $_[0][1] 0 7 7
Condition33%1/3
$_[0][0] and $_[0][1]
!ll&&!rl&&r
700
MC/DC0%0/2
$_[0][0] and $_[0][1]
$_[0][0] $_[0][1]
Truth table33%1/3
A and B
A= $_[0][0]B= $_[0][1]
ABresult
0X0
100
111
296 }
297  
298 #**************************************************************************
299 6 ▶use UNIVERSAL ();
Subroutine BEGIN: called CC=1 SCAR=0.0
300  
301 0 ▶sub table {
Subroutine table: not called CC=11 SCAR=48.8
Pod documented
302 # Wrapper around row().
303 0 my $it = shift;
304 0 ▶ Carp::croak "table isn't a class method" unless ref $it;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $it 0 0 0
305 0 ▶ my $decl = shift
Branch: 1 branch
BranchTrueFalseTotal
if @_ and defined $_[0] and ref $_[0] and UNIVERSAL::isa($_[0], "RTF::Writer::TableRowDecl") 0 0 0
Condition0%0/9
@_ and defined $_[0] and ref $_[0] and UNIVERSAL::isa($_[0], "RTF::Writer::TableRowDecl")
!ll&&!rl&&r
000
@_ and defined $_[0] and ref $_[0]
!ll&&!rl&&r
000
@_ and defined $_[0]
!ll&&!rl&&r
000
MC/DC0%0/4
@_ and defined $_[0] and ref $_[0] and UNIVERSAL::isa($_[0], "RTF::Writer::TableRowDecl")
@_ defined $_[0] ref $_[0] UNIVERSAL::isa($_[0], "RTF::Writer::TableRowDecl")
Truth table0%0/5
A and B and C and D
A= @_B= defined $_[0]C= ref $_[0]D= UNIVERSAL::isa($_[0], "RTF::Writer::TableRowDecl")
ABCDresult
0XXX0
10XX0
110X0
11100
11111
306 if @_ and defined $_[0] and ref($_[0])
307 and UNIVERSAL::isa($_[0], __PACKAGE__ . '::TableRowDecl');
308 # Remaining items are row-arrayrefs.
309  
310 0 ▶ push @_, [''] unless @_; # avoid table with no rows!
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
311  
312 0 ▶ $decl ||= RTF::Writer::TableRowDecl->new_auto_for_rows(@_);
Condition0%0/3
$decl ||= "RTF::Writer::TableRowDecl"->new_auto_for_rows(@_)
l!l&&r!l&&!r
000
MC/DC0%0/2
$decl ||= "RTF::Writer::TableRowDecl"->new_auto_for_rows(@_)
$decl "RTF::Writer::TableRowDecl"->new_auto_for_rows(@_)
Truth table0%0/3
A ||= B
A= $declB= "RTF::Writer::TableRowDecl"->new_auto_for_rows(@_)
ABresult
1X1
011
000
313  
314 0 $it->print(\'\par\pard');
315 # Because ill things happen unless the paragraph
316 # that the table starts in, is virgin.
317 0 foreach my $row_content (@_) {
318 0 ▶ Carp::croak "table's row-parameters have to be arrayrefs"
Branch: 1 branch
BranchTrueFalseTotal
unless ref($row_content || "") eq "ARRAY" 0 0 0
Condition0%0/2
$row_content || ''
l!l
00
MC/DC0%0/1
$row_content || ''
$row_content
Truth table0%0/2
A
A= $row_content
Aresult
11
00
319 unless ref($row_content || '') eq 'ARRAY';
320 0 $it->row($decl, @$row_content);
321 }
322 0 return scalar @_;
323 }
324  
325 #--------------------------------------------------------------------------
326  
327 5 ▶sub row {
Subroutine row: called CC=14 SCAR=31.8
Pod documented
328 # Generate a table row.
329 5 my $it = shift;
330 5 ▶ Carp::croak "row isn't a class method" unless ref $it;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $it 0 5 5
331 5 ▶ Carp::croak "row's first parameter has to be a table row declaration"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ and defined $_[0] and ref $_[0] and UNIVERSAL::isa($_[0], "RTF::Writer::TableRowDecl") 0 5 5
Condition33%3/9
@_ and defined $_[0] and ref $_[0] and UNIVERSAL::isa($_[0], "RTF::Writer::TableRowDecl")
!ll&&!rl&&r
005
@_ and defined $_[0] and ref $_[0]
!ll&&!rl&&r
005
@_ and defined $_[0]
!ll&&!rl&&r
005
MC/DC0%0/4
@_ and defined $_[0] and ref $_[0] and UNIVERSAL::isa($_[0], "RTF::Writer::TableRowDecl")
@_ defined $_[0] ref $_[0] UNIVERSAL::isa($_[0], "RTF::Writer::TableRowDecl")
Truth table20%1/5
A and B and C and D
A= @_B= defined $_[0]C= ref $_[0]D= UNIVERSAL::isa($_[0], "RTF::Writer::TableRowDecl")
ABCDresult
0XXX0
10XX0
110X0
11100
11111
332 unless @_ and defined $_[0] and ref($_[0])
333 and UNIVERSAL::isa($_[0], __PACKAGE__ . '::TableRowDecl');
334 5 my $decl = shift;
335  
336 # Pad with blank cells, if need be:
337 0 ▶ push @_, (\'') x scalar(@{$decl->[0]} - @_) if @{$decl->[0]} > @_;
Branch: 1 branch
BranchTrueFalseTotal
if @{$decl->[0];} > @_ 0 5 5
338 # We have to avoid having a cell-less row:
339 5 ▶ push @_, \'' unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 5 5
340
341 5 my $cell_count = @_;
342
343
344 5 my @inits = $decl->cell_content_init;
345
346 5 ▶ unshift @_,
Condition100%2/2
shift @inits || ''
l!l
32
MC/DC100%1/1
shift @inits || ''
shift @inits
Truth table100%2/2
A
A= shift @inits
Aresult
11
00
347 \(
348 '\pard\intbl' . ( shift(@inits) || '' )
349 );
350 5 ▶ for(my $i = 1; $i < @_; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 20 5 25
351 20 ▶ if(defined($_) and ref($_) eq '' and -1 != index($_[$i], "\f")) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_ and ref $_ eq "" and index($_[$i], "\f") != -1) 0 20 20
Condition33%2/6
defined $_ and ref $_ eq "" and index($_[$i], "\f") != -1
!ll&&!rl&&r
2000
defined $_ and ref $_ eq ""
!ll&&!rl&&r
2000
MC/DC0%0/3
defined $_ and ref $_ eq "" and index($_[$i], "\f") != -1
defined $_ ref $_ eq "" index($_[$i], "\f") != -1
Truth table25%1/4
A and B and C
A= defined $_B= ref $_ eq ""C= index($_[$i], "\f") != -1
ABCresult
0XX0
10X0
1100
1111
352 # The one case where we need to mess with things: if there's a
353 # formfeed in this plaintext.
354 0 my $x = $_[$i];
355 0 $x =~ tr/\f/\n/;
356 0 splice @_, $i, 1, $x; # Swap in the copy, not touching the original.
357 }
358 20 ▶ splice(@_, $i + 1, 0, \(
Condition100%2/2
shift @inits || ''
l!l
911
MC/DC100%1/1
shift @inits || ''
shift @inits
Truth table100%2/2
A
A= shift @inits
Aresult
11
00
359 '\cell\pard\intbl' . (shift(@inits) || '')
360 ));
361 }
362 5 $_[-1] = \'\cell\row\pard';
363  
364 5 $it->print(
365 \'{',
366 $decl->decl_code($cell_count),
367 @_,
368 \'}',
369 );
370 5 return $cell_count; # Might as well return somehting.
371 }
372  
373 #--------------------------------------------------------------------------
374  
375 4 ▶sub number_pages {
Subroutine number_pages: called CC=1 SCAR=0.0
Pod documented
376 4 my $r = shift;
377 4 $r->print(
378 \"\n{\\header \\pard\\qr\\plain\\f0",
379 @_,
380 \"\\chpgn\\par}\n\n"
381 );
382 # This is actually a section attribute. To reset, \'\sect\sectd'
383 # to start a new section.
384 }
385  
386 #**************************************************************************
387  
388 10 ▶sub paragraph {
Subroutine paragraph: called CC=1 SCAR=0.0
Pod documented
389 10 my $r = shift;
390 10 $r->print(\"{\\pard\n", @_, \"\n\\par}\n\n");
391 }
392  
393 #**************************************************************************
394  
395 2 ▶sub image_paragraph {
Subroutine image_paragraph: called CC=3 SCAR=11.3
Pod documented
396 2 my $r = shift;
397 2 my($filename, $declcode) = $r->_image_params(@_);
398 2 ▶ return unless $r->print( \"{\\pard\\qc\n{\\pict\n", \$declcode);
Branch: 1 branch
BranchTrueFalseTotal
unless $r->print(\"{\\pard\\qc\n{\\pict\n", \$declcode) 0 2 2
399 2 ▶ $r->_image_data($filename) or return;
Branch: 1 branch
BranchTrueFalseTotal
$r->_image_data($filename) or return 2 0 2
400 2 $r->print( \"}\n\\par}\n\n" );
401 }
402  
403 0 ▶sub paragraph_image { shift->image_paragraph(@_) }
Subroutine paragraph_image: not called CC=1 SCAR=6.9
Pod undocumented
404 0 ▶sub paragraph_picture { shift->image_paragraph(@_) }
Subroutine paragraph_picture: not called CC=1 SCAR=6.9
Pod undocumented
405 0 ▶sub picture_paragraph { shift->image_paragraph(@_) }
Subroutine picture_paragraph: not called CC=1 SCAR=6.9
Pod undocumented
406  
407 0 ▶sub pict { shift->image(@_) }
Subroutine pict: not called CC=1 SCAR=6.9
Pod undocumented
408  
409 2 ▶sub image {
Subroutine image: called CC=2 SCAR=7.0
Pod documented
410 2 ▶ Carp::croak "Don't call \$rtf->image(...) in void context!"
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 2 2
411 unless defined wantarray;
412 2 my $r = shift;
413 2 my($filename, $declcode) = $r->_image_params(@_);
414 2 my $out = "{\\pict\n$declcode";
415 2 $r->_image_data($filename, \$out );
416 2 $out .= "}\n";
417 2 return \$out;
418 }
419  
420 #--------------------------------------------------------------------------
421  
422 4 ▶sub _image_params {
Subroutine _image_params: called CC=10 SCAR=26.9
423 4 my $self = shift;
424
425 4 my %o = @_;
426 4 my $decl;
427 4 ▶ my $filespec = $o{'filename'} || Carp::croak "What filename?";
Condition50%1/2
$o{'filename'} || Carp::croak('What filename?')
l!l
40
MC/DC0%0/1
$o{'filename'} || Carp::croak('What filename?')
$o{'filename'}
Truth table50%1/2
A
A= $o{'filename'}
Aresult
11
00
428 4 ▶ Carp::croak "No such file as $filespec"
Branch: 1 branch
BranchTrueFalseTotal
unless $filespec and -e $filespec 0 4 4
Condition33%1/3
$filespec and -e $filespec
!ll&&!rl&&r
004
MC/DC0%0/2
$filespec and -e $filespec
$filespec -e $filespec
Truth table33%1/3
A and B
A= $filespecB= -e $filespec
ABresult
0X0
100
111
429 unless $filespec and -e $filespec;
430  
431 4 ▶ if(defined $o{'picspecs'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $o{'picspecs'}) { } 0 4 4
432 0 $decl = $o{'picspecs'};
433 0 ▶ $decl = $$decl if ref $decl;
Branch: 1 branch
BranchTrueFalseTotal
if ref $decl 0 0 0
434 } else {
435 4 require Image::Size;
436 4 my($h,$w, $type) = Image::Size::imgsize( $filespec );
437 4 ▶ Carp::croak "$filespec - $type" unless $h and $w;
Branch: 1 branch
BranchTrueFalseTotal
unless $h and $w 0 4 4
Condition33%1/3
$h and $w
!ll&&!rl&&r
004
MC/DC0%0/2
$h and $w
$h $w
Truth table33%1/3
A and B
A= $hB= $w
ABresult
0X0
100
111
438  
439 4 ▶ my $tag =
Branch: 2 branches
BranchTrueFalseTotal
$type eq 'PNG' ? : 2 2 4
$type eq 'JPG' ? : 2 0 2
440 ($type eq 'PNG') ? '\pngblip'
441 : ($type eq 'JPG') ? '\jpegblip'
442 : Carp::croak("I can't handle images of type $type like $filespec");
443 ;
444 4 $decl = "$tag\\picw$w\\pich$h\n";
445  
446 # Now glom on any extra parameters specified:
447 $decl .= join '',
448 map sprintf("\\pic%s%s", $_, int $o{$_}),
449 4 grep defined($o{$_}),
450 qw<wgoal hgoal scalex scaley cropt cropb cropr cropl>
451 ;
452 }
453 4 $decl .= "\n"; # So it doesn't run together with the image data.
454  
455 4 return( $filespec, $decl );
456 }
457  
458  
459 4 ▶sub _image_data {
Subroutine _image_data: called CC=5 SCAR=16.1
460 4 my($r, $filename, $to) = @_;
461  
462 4 my $buffer;
463 my $in;
464 {
465 4 local(*IMAGE);
466 4 ▶ open(IMAGE, $filename) or Carp::croak "Can't read-open $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
open IMAGE, $filename or Carp::croak("Can't read-open ${filename}: $!") 4 0 4
467 4 $in = *IMAGE;
468 }
469 4 binmode($in);
470 4 ▶ while( read($in, $buffer, 32) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $in, $buffer, 32) 968 4 972
471 968 ▶ if($to) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to) { } 484 484 968
472 484 $$to .= unpack("H*", $buffer) . "\n" ;
473 } else {
474 484 ▶ $r->print( \( unpack("H*", $buffer) . "\n" ) ) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$r->print(\(unpack("H*", $buffer) . "\n")) or return 0 484 0 484
475 }
476 # Turn 32 bytes into 64 hex characters, and then add a newline.
477 # (If the last chunk of data is under 32 bytes, then the unpack()
478 # does the right thing.)
479 }
480 4 CORE::close($in);
481 4 return 1;
482 }
483  
484 #**************************************************************************
485  
486 # two tolerated variant forms:
487 0 ▶sub prologue { shift->prolog(@_) }
Subroutine prologue: not called CC=1 SCAR=6.9
Pod undocumented
488 0 ▶sub premable { shift->prolog(@_) }
Subroutine premable: not called CC=1 SCAR=6.9
Pod undocumented
489  
490 6 ▶sub prolog {
Subroutine prolog: called CC=45 SCAR=56.8
Pod documented
491 # Emit prolog with given parameters
492 6 DEBUG and print "Prolog args: <@_>\n";
493 6 my($it, %h) = (@_);
494  
495 6 my $x; #scratch
496 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
497  
498 6 ▶ $h{'revtim' } = time unless exists $h{'revtim'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $h{"revtim"} 6 0 6
499 6 ▶ $h{'creatim'} = time unless exists $h{'creatim'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $h{"creatim"} 6 0 6
500 $h{'doccomm'} =
501 escape_broadly(sprintf 'written by %s [Perl %s v%s]',
502 $0, ref($it), $it->VERSION())
503 6 ▶ unless exists $h{'doccomm'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $h{"doccomm"} 6 0 6
504 # So you can set each to undef if you want it suppressed.
505  
506 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
507 6 ▶ my $fonts = $h{'fonts'} || $h{'font_table'} || $h{'fonttable'} || [];
Condition37%3/8
$h{'fonts'} || $h{'font_table'} || $h{'fonttable'} || []
l!l
06
$h{"fonts"} or $h{"font_table"} or $h{"fonttable"}
l!l&&r!l&&!r
006
$h{"fonts"} or $h{"font_table"}
l!l&&r!l&&!r
006
MC/DC0%0/3
$h{'fonts'} || $h{'font_table'} || $h{'fonttable'} || []
$h{"fonts"} $h{"font_table"} $h{"fonttable"}
Truth table25%1/4
A or B or C
A= $h{"fonts"}B= $h{"font_table"}C= $h{"fonttable"}
ABCresult
1XX1
01X1
0011
0000
508 6 ▶ $fonts = [$fonts] unless ref $fonts;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fonts 0 6 6
509 6 ▶ push @$fonts, \'\froman Times New Roman'
Branch: 1 branch
BranchTrueFalseTotal
if ref $fonts eq "ARRAY" and not @$fonts 6 0 6
Condition33%1/3
ref $fonts eq "ARRAY" and not @$fonts
!ll&&!rl&&r
006
MC/DC0%0/2
ref $fonts eq "ARRAY" and not @$fonts
ref $fonts eq "ARRAY" not @$fonts
Truth table33%1/3
A and not(B)
A= ref $fonts eq "ARRAY"B= not @$fonts
ABresult
0X0
100
111
510 if ref($fonts) eq 'ARRAY' and ! @$fonts; # avoid having an empty font table
511  
512 6 my $font_count = -1;
513 6 ▶ $fonts = \join '',
Branch: 2 branches
BranchTrueFalseTotal
if ref $fonts eq "ARRAY" 6 0 6
ref $_ ? : 6 0 6
514 # '{' \fonttbl (<fontinfo> | ('{' <fontinfo> '}'))+ '}'
515 "{\\fonttbl\n",
516 map( ref($_)
517 ? ("{\\f", ++$font_count, ' ', $$_, ";}\n")
518 : ("{\\f", ++$font_count, '\fnil ', escape_broadly($_), ";}\n"),
519 @$fonts
520 # <fontnum> <fontfamily>
521 # <fcharset>? <fprq>? <panose>? <nontaggedname>? <fontemb>? <codepage>?
522 # <fontname> <fontaltname>? ';'
523 ), "}\n"
524 if ref $fonts eq 'ARRAY'
525 ;
526
527 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
528 my $info = join '',
529 # And the info group:
530 "\n{\\info \n",
531  
532 # \version? & \vern? & \edmins? & \nofpages? & \nofwords? \nofchars?
533 # & \id?
534 # & <title>? & <subject>? & <author>? & <manager>? & <company>?
535 # & <operator>? & <category>? & <keywords>? & <comment>? & <doccomm>?
536  
537 # Time things, all optional:
538 map(
539 (!defined($x = $h{$_})) ? () : (
540 "{\\$_ ", (
541 ref($x) eq 'SCALAR' ? $$x :
542 ref($x) eq 'ARRAY' ? _time_to_rtf(@$x) :
543 $x =~ m<^\d+$> ? _time_to_rtf( $x) :
544 $x, # dubious, but let it thru
545 ),
546 "}\n"
547 ),
548 qw(creatim revtim printim buptim)
549 ),
550  
551 map( # Optional integer things:
552 (!defined($x = $h{$_})) ? () :
553 $x =~ m<^[0-9]+$> ? "\\$_$x\n" :
554 Carp::croak("value for \"$_\" must be an integer, not \"$_\""),
555  
556 qw(version vern edmins nofpages nofwords nofchars nofcharsws id)
557 ),
558  
559 # Optional non-time non-integer things:
560 map(
561 (!defined($x = $h{$_})) ? () : (
562 "{\\$_ ",
563 (ref($x) eq 'SCALAR') ? $$x : $x,
564 "}\n"
565 ),
566 qw(title subject author manager company operator category
567 keywords comment doccomm hlinkbase)
568 ),
569  
570 ref( $h{'more_info'} || '' ) eq 'SCALAR'
571 0 ▶ ? ${ $h{'more_info'} } : ( $h{'more_info'} || '' ),
Branch: 9 branches
BranchTrueFalseTotal
!defined($x = $h{$_}) ? : 12 12 24
ref $x eq 'SCALAR' ? : 0 12 12
ref $x eq 'ARRAY' ? : 0 12 12
$x =~ /^\d+$/ ? : 12 0 12
!defined($x = $h{$_}) ? : 48 0 48
$x =~ /^[0-9]+$/ ? : 0 0 0
!defined($x = $h{$_}) ? : 56 10 66
ref $x eq 'SCALAR' ? : 0 10 10
ref($h{'more_info'} || '') eq 'SCALAR' ? : 0 6 6
Condition50%2/4
$h{'more_info'} || ''
l!l
06
$h{"more_info"} or ""
l!l
06
MC/DC0%0/1
$h{'more_info'} || ''
$h{'more_info'}
MC/DC0%0/1
$h{"more_info"} or ""
$h{"more_info"}
Truth table50%1/2
A
A= $h{'more_info'}
Aresult
11
00
Truth table50%1/2
A
A= $h{"more_info"}
Aresult
11
00
572  
573 "}\n\n", # end of info group
574 ;
575  
576 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
577 # Cook up the color table.
578 #
579 # Note that you might want to feed this a null 0th entry:
580 # as in: [ undef, [255,0,0], [0,0,255], ... ]
581
582 my $color_table = ($h{'colors'} || $h{'color_table'}
583 6 ▶ || $h{'colortable'} || $h{'colortbl'} || '');
Condition36%4/11
$h{'colors'} || $h{'color_table'} || $h{'colortable'} || $h{'colortbl'} || ''
l!l
06
$h{"colors"} or $h{"color_table"} or $h{"colortable"} or $h{"colortbl"}
l!l&&r!l&&!r
006
$h{"colors"} or $h{"color_table"} or $h{"colortable"}
l!l&&r!l&&!r
006
$h{"colors"} or $h{"color_table"}
l!l&&r!l&&!r
006
MC/DC0%0/4
$h{'colors'} || $h{'color_table'} || $h{'colortable'} || $h{'colortbl'} || ''
$h{"colors"} $h{"color_table"} $h{"colortable"} $h{"colortbl"}
Truth table20%1/5
A or B or C or D
A= $h{"colors"}B= $h{"color_table"}C= $h{"colortable"}D= $h{"colortbl"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
584 6 ▶ if(ref($color_table) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $color_table eq 'ARRAY') { } 0 6 6
elsif (ref $color_table eq 'SCALAR') { } 0 6 6
585 #print "R ", ref($color_table), "<", @$color_table, "> =$color_table\n";
586 0 ▶ $color_table = \join '',
Branch: 3 branches
BranchTrueFalseTotal
ref($_ || '') eq 'ARRAY' ? : 0 0 0
ref($_ || '') eq 'SCALAR' ? : 0 0 0
$$_ =~ /;[\cm\cj\n]*\z/s ? : 0 0 0
Condition0%0/10
$_ || ''
l!l
00
$_->[0] || 0
l!l
00
$_->[1] || 0
l!l
00
$_->[2] || 0
l!l
00
$_ || ''
l!l
00
MC/DC0%0/1
$_ || ''
$_
MC/DC0%0/1
$_->[0] || 0
$_->[0]
MC/DC0%0/1
$_->[1] || 0
$_->[1]
MC/DC0%0/1
$_->[2] || 0
$_->[2]
MC/DC0%0/1
$_ || ''
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
Truth table0%0/2
A
A= $_->[0]
Aresult
11
00
Truth table0%0/2
A
A= $_->[1]
Aresult
11
00
Truth table0%0/2
A
A= $_->[2]
Aresult
11
00
Truth table0%0/2
A
A= $_
Aresult
11
00
587 '{\colortbl ',
588 map(
589 (ref($_ || '') eq 'ARRAY' ) ? sprintf('\red%d\green%d\blue%d;',
590 $_->[0] || 0,
591 $_->[1] || 0,
592 $_->[2] || 0,
593 )
594 : (ref($_ || '') eq 'SCALAR') ? (
595 ($$_ =~ m/;[\cm\cj\n]*\z/s) ? $$_ : ($$_ . ';') )
596 # Make sure it ends with a semicolon
597 : ';', # null entry
598 @$color_table
599 ),
600 '}'
601 ;
602 } elsif(ref($color_table) eq 'SCALAR') {
603 # pass it thru
604 } else {
605 6 $color_table = \'{\colortbl;\red255\green0\blue0;\red0\green0\blue255;}';
606 }
607
608 6 $h{'colortbl'} = $color_table;
609 #print "Color table: <", ${$h{'colortbl'}}, ">\n";
610  
611 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
612 #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
613 #
614 # Now emit the table:
615 #
616 # \rtf <charset> \deff? <fonttbl> <filetbl>? <colortbl>? <stylesheet>?
617 # <listtables>? <revtbl>?
618  
619 $it->print( \join '',
620 '{\rtf' ,
621 defined($h{'rtf_version'}) ? $h{'rtf_version'} : '1',
622  
623 "\\" . ($h{'charset'} || 'ansi'),
624 "\\deff" . int($h{'deff'} || 0),
625  
626 (!defined($x = $h{'more_default'})) ? '' # place to sneak in more stuff
627 : ref($x) eq 'SCALAR' ? $$x
628 : $x,
629
630 $$fonts,
631  
632 map( ref( $h{$_} || '' ) eq 'SCALAR'
633 6 ▶ ? ${ $h{$_} } : ( $h{$_} || '' ),
Branch: 4 branches
BranchTrueFalseTotal
defined $h{'rtf_version'} ? : 0 6 6
!defined($x = $h{'more_default'}) ? : 6 0 6
ref $x eq 'SCALAR' ? : 0 0 0
ref($h{$_} || '') eq 'SCALAR' ? : 6 24 30
Condition62%5/8
$h{'charset'} || 'ansi'
l!l
06
$h{'deff'} || 0
l!l
06
$h{$_} || ''
l!l
624
$h{$_} or ""
l!l
024
MC/DC0%0/1
$h{'charset'} || 'ansi'
$h{'charset'}
MC/DC0%0/1
$h{'deff'} || 0
$h{'deff'}
MC/DC100%1/1
$h{$_} || ''
$h{$_}
MC/DC0%0/1
$h{$_} or ""
$h{$_}
Truth table50%1/2
A
A= $h{'charset'}
Aresult
11
00
Truth table50%1/2
A
A= $h{'deff'}
Aresult
11
00
Truth table100%2/2
A
A= $h{$_}
Aresult
11
00
Truth table50%1/2
A
A= $h{$_}
Aresult
11
00
634 qw( filetbl colortbl stylesheet listtables revtbl )
635 ),
636  
637 $info,
638  
639 );
640 #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
641 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
642  
643 6 $it->[1] .= '}';
644 6 DEBUG > 2 and print "Setting $it\'s out-buffer to <$it->[1]>\n";
645 # to close the group that this document opened in its first char
646 6 return 1;
647 }
648  
649  
650 # Two subs used in the "prolog" method:
651  
652 6 ▶sub escape_broadly {
Subroutine escape_broadly: called CC=2 SCAR=7.9
Pod undocumented
653 # Non-destructively quote anything fishy.
654 6 my $scratch = $_[0];
655 6 $scratch =~
656 18 s/([F\.\x00-\x1F\\\{\}\x7F-\xFF])/"\\'".(unpack("H2",$1))/eg; # ESCAPER
657 6 $scratch =~
658 0 ▶ s/([^\x00-\xFF])/'\\uc1\\u'.((ord($1)<32768)?ord($1):(ord($1)-65536)).'?'/eg;
Branch: 1 branch
BranchTrueFalseTotal
ord $1 < 32768 ? : 0 0 0
659 6 return $scratch;
660 }
661  
662 12 ▶sub _time_to_rtf {
Subroutine _time_to_rtf: called CC=3 SCAR=11.2
663 # accepts no-params (meaning now), an epoch time, or a timelist
664 12 ▶ push @_, time() unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 12 12
665 12 ▶ if(@_ == 1) { # normal case
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 12 0 12
666 12 @_ = (localtime(shift @_))[5,4,3,2,1,0];
667 12 $_[0] += 1900; # RTF counts 2023 as 2023, not 123.
668 12 $_[1]++; # RTF counts January as 1, not 0.
669 }
670 12 return sprintf '\yr%d\mo%d\dy%d\hr%d\min%d\sec%d', @_;
671 }
672  
673 #**************************************************************************
674 #
675 # The following makes the scary scary emitter-closure:
676 #
677  
678 6 my $counter = 0; # for debug purposes
679  
680 7 ▶sub _make_emitter_closure {
Subroutine _make_emitter_closure: called CC=1 SCAR=0.2
681 7 my($fh, $sr) = @_;
682 # sr should either be undef, or a scalar-ref
683 7 my $scratch;
684  
685 # A closure on $fh or $sr, for printing to it.
686
687 523 ▶ sub {
Subroutine __ANON__: called CC=18 SCAR=31.7
688 523 my $this = shift;
689 523 DEBUG > 1 and print "Writing (@_) to ", $sr ? "S_$sr\n" : "F_$fh\n";
690  
691 523 foreach my $x (@_) {
692 615 ▶ next unless defined $x;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x 0 615 615
693 615 ▶ if(ref($x) eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $x eq 'ARRAY') { } 3 612 615
elsif (ref $x eq 'SCALAR') { } 579 33 612
elsif (length $x) { } 33 0 33
694 3 ▶ next if @$x == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$x == 0 0 3 3
695 3 ▶ $sr ? ( $$sr .= '{' ) : print $fh '{';
Branch: 1 branch
BranchTrueFalseTotal
$sr ? : 0 3 3
696 3 DEBUG > 1 and print " $counter: wrote {\n";
697 3 $this->[0]->($this, @$x); # recurse!
698 3 ▶ $sr ? ( $$sr .= '}' ) : print $fh '}';
Branch: 1 branch
BranchTrueFalseTotal
$sr ? : 0 3 3
699 3 DEBUG > 2 and print " wrote }\n";
700 } elsif(ref($x) eq 'SCALAR') {
701 579 ▶ if(!defined($$x) or !length($$x)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $$x && length $$x) { } 0 579 579
elsif (not $RTF::Writer::AUTO_NL && $$x =~ /[a-zA-Z0-9]\z/s) { } 543 36 579
Condition33%1/3
defined $$x && length $$x
!ll&&!rl&&r
00579
MC/DC0%0/2
defined $$x && length $$x
defined $$x length $$x
Truth table33%1/3
A && B
A= defined $$xB= length $$x
ABresult
0X0
100
111
702 # no-op
703 0 DEBUG > 2 and print " $counter: skipping null sr\n";
704 ▶ } elsif( not( $AUTO_NL and $$x =~ m<[a-zA-Z0-9]\z>s )) {
Condition66%2/3
$RTF::Writer::AUTO_NL && $$x =~ /[a-zA-Z0-9]\z/s
!ll&&!rl&&r
054336
MC/DC50%1/2
$RTF::Writer::AUTO_NL && $$x =~ /[a-zA-Z0-9]\z/s
$RTF::Writer::AUTO_NL $$x =~ /[a-zA-Z0-9]\z/s
Truth table66%2/3
A && B
A= $RTF::Writer::AUTO_NLB= $$x =~ /[a-zA-Z0-9]\z/s
ABresult
0X0
100
111
705 543 ▶ $sr ? ( $$sr .= $$x ) : print $fh $$x;
Branch: 1 branch
BranchTrueFalseTotal
$sr ? : 0 543 543
706 543 DEBUG > 2 and print " $counter: wrote sr $$x\n";
707 } else {
708 # $AUTO_NL is true, and $$x's last char is in [a-zA-Z0-9]
709 36 ▶ $sr ? ( $$sr .= $$x . "\n" ) : print $fh $$x, "\n";
Branch: 1 branch
BranchTrueFalseTotal
$sr ? : 0 36 36
710 36 DEBUG > 2 and print " $counter: wrote sr $$x +nl\n";
711  
712 # Why emit a newline? Because that string might end in a
713 # command, and we want to do the Right Thing in the case of:
714 # $r->print(\'\i', 'donuts')
715 # i.e., printing "\i[newline]donuts", not "\idonuts"
716 #
717 # And why not emit "\i[space]donuts"? because we if we emit a
718 # space and the thing we emitted WASN'T a control word, then
719 # we did a bad thing! Spaces are tricky -- sometimes they're
720 # meaningless, and sometimes they mean a literal space.
721 # But newlines are always ignored -- well, unless preceded
722 # by an escaping backslash, but to get that, the user would
723 # have to have the previous group end in an unmatched backslash,
724 # as in $h->print(\"\\foo\\", ...) So don't do that!
725 }
726
727 } elsif(length $x) { # It's plaintext
728 33 ($scratch = $x) =~
729 29 s/([F\.\x00-\x1F\-\\\{\}\x7F-\xFF])/$Escape[ord$1]/eg; # ESCAPER
730 33 $scratch =~
731 0 ▶ s/([^\x00-\xFF])/'\\uc1\\u'.((ord($1)<32768)?ord($1):(ord($1)-65536)).'?'/eg;
Branch: 1 branch
BranchTrueFalseTotal
ord $1 < 32768 ? : 0 0 0
732  
733 # Escape \, {, }, -, control chars, and 7f-ff, and Unicode.
734  
735 # And now: a not terribly clever algorithm for inserting newlines
736 # at a guaranteed harmless place: after a block of whitespace
737 # after the 65th column.
738 # Why not before the block of whitespace? Consider:
739 # q<\foo bar> If we break that into q<\foo>+NL+q< bar>, then
740 # suddenly the space after the newline is significant, instead
741 # of just being the dummy space that ends the \foo command token.
742 33 ▶ $scratch =~
Branch: 1 branch
BranchTrueFalseTotal
if $RTF::Writer::WRAP 33 0 33
743 s/(
744 [^\cm\cj\n]{65} # Snare 65 characters from a line
745 [^\cm\cj\n\x20]{0,50} # and finish any current word
746 )
747 (\x20{1,10})(?![\cm\cj\n]) # capture some spaces not at line-end
748 /$1$2\n/gx # and put a NL after those spaces
749 if $WRAP;
750 # This may wrap at well past the 65th column, but not past the 120th.
751 33 ▶ $sr ? ( $$sr .= $scratch ) : print $fh $scratch;
Branch: 1 branch
BranchTrueFalseTotal
$sr ? : 1 32 33
752 33 DEBUG > 2 and print " $counter: wrote scalar <$scratch>\n";
753 33 $scratch = '';
754 }
755 # otherwise it's 0-length plaintext, so ignore.
756 }
757 523 DEBUG > 3 and print $fh "{\\v $^T/", ++$counter, "}\n";
758 523 return 1;
759 7 };
760 }
761  
762 #--------------------------------------------------------------------------
763 6 1;
764  
765  
↑ Summary Writer/TableRowDecl.pm »