File Coverage

blib/lib/Text/Flow.pm
Criterion Covered Total %
statement 24 24 100.0
branch 2 2 100.0
condition n/a
subroutine 4 4 100.0
pod 1 1 100.0
total 31 31 100.0


line stmt bran cond sub pod time code
1              
2             package Text::Flow;
3 2     2   187510 use Moose;
  2         1617950  
  2         18  
4              
5 2     2   18572 use Text::Flow::Wrap;
  2         8  
  2         586  
6              
7             our $VERSION = '0.01';
8             our $AUTHORITY = 'cpan:STEVAN';
9              
10             has 'check_height' => (
11             is => 'rw',
12             isa => 'CodeRef',
13             required => 1,
14             );
15              
16             has 'wrapper' => (
17             is => 'rw',
18             isa => 'Text::Flow::Wrap',
19             required => 1,
20             );
21              
22             sub flow {
23 4     4 1 16536 my ($self, $text) = @_;
24            
25 4         1697 my $paragraphs = $self->wrapper->disassemble_paragraphs($text);
26            
27 4         19 my @sections = ([]);
28 4         14 foreach my $paragraph (@$paragraphs) {
29 8         12 push @{$sections[-1]} => [];
  8         23  
30 8         18 foreach my $line (@$paragraph) {
31 69 100       2832 unless ($self->check_height->($sections[-1])) {
32 5         50 push @sections => [[]];
33             }
34 69         561 push @{$sections[-1]->[-1]} => $line;
  69         205  
35             }
36             }
37            
38             #use Data::Dumper;
39             #warn Dumper \@sections;
40            
41 9         18 return map {
42 9         1370 chomp; $_;
  9         318  
43             } map {
44 4         13 $self->wrapper->reassemble_paragraphs($_);
45             } @sections;
46             }
47              
48 2     2   22 no Moose;
  2         3  
  2         49  
49              
50             1;
51              
52             __END__
53              
54             =pod
55              
56             =head1 NAME
57              
58             Text::Flow - Flexible text flowing and word wrapping for not just ASCII output.
59              
60             =head1 SYNOPSIS
61            
62             use Text::Flow;
63            
64             # use it on ASCII text ...
65             my $flow = Text::Flow->new(
66             check_height => sub {
67             my $paras = shift;
68             sum(map { scalar @$_ } @$paras) <= 10;
69             },
70             wrapper => Text::Flow::Wrap->new(
71             check_width => sub { length($_[0]) < 70 }
72             ),
73             );
74            
75             my @sections = $flow->flow($text);
76            
77             # @sections will now be an array of strings, each string
78             # will contain no more than 10 lines of text each of which
79             # will be no longer then 70 characters long
80            
81             # or use it on text in a PDF file ...
82             my $flow = Text::Flow->new(
83             check_height => sub {
84             my $paras = shift;
85             (sum(map { scalar @$_ } @$paras) * $pdf->get_font_height) < 200;
86             },
87             wrapper => Text::Flow::Wrap->new(
88             check_width => sub {
89             my $string = shift;
90             $pdf->get_string_width($string) < 100
91             },
92             )
93             );
94            
95             my @sections = $flow->flow($text);
96            
97             # @sections will now be an array of strings, each string
98             # will contain text which will fit within 200 pts and
99             # each line will be no longer then 100 pts wide
100              
101             =head1 DESCRIPTION
102              
103             This module provides a flexible way to wrap and flow text for both
104             ASCII and non-ASCII outputs.
105              
106             =head2 Another Text Wrap module!!?!?!
107              
108             The main purpose of this module is to provide text wrapping and flowing
109             features without being tied down to ASCII based output and fixed-width
110             fonts. My needs were for a more sophisticated text control in PDF and GIF
111             output formats in particular.
112              
113             =head1 METHODS
114              
115             =over 4
116              
117             =item B<new (%params)>
118              
119             This constructs the new Text::Flow instance, and accepts parameters for
120             the C<wrapper> and C<check_height> variables.
121              
122             =item B<wrapper (?$wrapper)>
123              
124             This is the accessor for the internally help Text::Flow::Wrapper instance
125             which is used by Text::Flow to wrap the individual lines.
126              
127             =item B<check_height>
128              
129             This is the accessor for the CODE ref which is used to check the height
130             of the current paragraph. It gets as an argument, an array-ref of paragraphs,
131             each of which is also an array-ref of text lines. The most common usage
132             is shown in the SYNOPSIS above, but you can really do anything you want
133             with them. It is expected to return a Boolean value, true if the height is
134             still okay, and false if the max height has been reached.
135              
136             =item B<flow ($text)>
137              
138             This method preforms the text flowing. It returns an array of strings which
139             can be treated as complete blocks of text. It will handle paragraph breaks
140             and line breaks for you.
141              
142             =back
143              
144             =head2 Introspection
145              
146             =over 4
147              
148             =item B<meta>
149              
150             Returns the Moose meta object associated with this class.
151              
152             =back
153              
154             =head1 TODO
155              
156             I wanted to write some tests for using this with GD modules as well. I suppose
157             we will have to wait until 0.02 for that.
158              
159             =head1 SIMILAR MODULES
160              
161             There are a bunch of other text wrapping and flowing modules out there, but
162             they are all meant for use in ASCII outout only. This just didn't work for
163             my needs, so I wrote this one. If you need to wrap ASCII text and want to
164             do it in a much simpler manner then this module provides, then I suggest
165             checking out those modules. This module is specifically made for when those
166             other modules are no longer powerful and flexible enough.
167              
168             =head1 BUGS
169              
170             All complex software has bugs lurking in it, and this module is no
171             exception. If you find a bug please either email me, or add the bug
172             to cpan-RT.
173              
174             =head1 AUTHOR
175              
176             Stevan Little E<lt>stevan@iinteractive.comE<gt>
177              
178             =head1 COPYRIGHT AND LICENSE
179              
180             Copyright 2007 by Infinity Interactive, Inc.
181              
182             L<http://www.iinteractive.com>
183              
184             This library is free software; you can redistribute it and/or modify
185             it under the same terms as Perl itself.
186              
187             =cut
188              
189