WE_Frontend/TextLayouter.pm

filter: Statementstmt 95.2% 81 / 85 Branchbran 81.2% 26 / 32 Conditioncond 58.3% 7 / 12 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 90.0% 9 / 10 Podpod 25.0% 1 / 4 totaltotal 84.0% 126 / 150 CC 25 SCAR 18.8
CC
25
Cov
88%
break_text
12.0
add_y
6.0
get_bounds
5.1
CRAP 6.6
« WE_Content/PerlDD.pm ↑ Summary WE/Util/Escape.pm »
1 # -*- perl -*-
2  
3 #
4 # $Id: TextLayouter.pm,v 1.4 2003/12/16 15:21:23 eserte Exp $
5 # Author: Slaven Rezic
6 #
7 # Copyright (C) 2001 Online Office Berlin. All rights reserved.
8 # Copyright (C) 2002 Slaven Rezic.
9 # This is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License, see the file COPYING.
11  
12 #
13 # Mail: slaven@rezic.de
14 # WWW: http://we-framework.sourceforge.net
15 #
16  
17 package WE_Frontend::TextLayouter;
18  
19 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use vars qw($VERSION %fontinfo @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 $VERSION = sprintf("%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/);
22  
23 2 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 @EXPORT_OK = qw(break_text continue_text_with_nl combine_fontinfo);
25  
26 2 ▶sub break_text {
Subroutine break_text: called CC=12 SCAR=24.9
Pod documented
27 2 my($text, $fontinfo, $boxwidth, $boxheight, %args) = @_;
28  
29 2 my @out;
30 2 ▶ my $box = $args{'-box'}; $box = "" if !defined $box;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $box 1 1 2
31 2 ▶ my $x = $args{'-x'} || 0;
Condition50%1/2
$args{'-x'} || 0
l!l
02
MC/DC0%0/1
$args{'-x'} || 0
$args{'-x'}
Truth table50%1/2
A
A= $args{'-x'}
Aresult
11
00
32 2 ▶ my $y = $args{'-y'} || 0;
Condition100%2/2
$args{'-y'} || 0
l!l
11
MC/DC100%1/1
$args{'-y'} || 0
$args{'-y'}
Truth table100%2/2
A
A= $args{'-y'}
Aresult
11
00
33 2 ▶ my $maxlineheight = $args{'-maxlineheight'} || 0;
Condition50%1/2
$args{'-maxlineheight'} || 0
l!l
02
MC/DC0%0/1
$args{'-maxlineheight'} || 0
$args{'-maxlineheight'}
Truth table50%1/2
A
A= $args{'-maxlineheight'}
Aresult
11
00
34 2 my $lastboxref = $args{'-lastbox'};
35  
36 2 my $space_width = (get_bounds(" ", $fontinfo))[0];
37 2 my $line_height = (get_bounds("A", $fontinfo))[1]; # XXX maybe better character than "A"?
38  
39 2 foreach my $line (split /\n/, $text) {
40  
41 14 my $push_word;
42 14 my $word_height = 0;
43  
44 26 ▶ my $next_line_or_box = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
45 26 ▶ if ($y+$word_height+$maxlineheight >= $boxheight) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y + $word_height + $maxlineheight >= $boxheight) { } 3 23 26
46 # next box
47 3 push @out, $box;
48 3 $box = "";
49 3 $x = 0;
50 3 $y = 0;
51 3 $maxlineheight = 0;
52 3 ▶ $push_word->() if $push_word;
Branch: 1 branch
BranchTrueFalseTotal
if $push_word 2 1 3
53 } else {
54 # next line
55 23 $box .= "\n";
56 23 $x = 0;
57 23 $y += $maxlineheight;
58 23 $maxlineheight = 0;
59 23 ▶ $push_word->() if $push_word;
Branch: 1 branch
BranchTrueFalseTotal
if $push_word 10 13 23
60 }
61 14 };
62  
63 14 foreach my $word (split /\s+/, $line) {
64 106 my $word_width;
65 106 ($word_width, $word_height) = get_bounds($word, $fontinfo);
66  
67 212 ▶ my $is_beginning_of_line = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
68 212 ▶ $box eq '' || $box =~ /\n\Z/s;
Branch: 1 branch
BranchTrueFalseTotal
$box eq "" or $box =~ /\n\Z/s 6 206 212
69 106 };
70  
71 106 ▶ $push_word = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
72 106 ▶ if (!$is_beginning_of_line->()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&$is_beginning_of_line()) 80 26 106
73 80 $box .= " ";
74 80 $x += $space_width;
75 }
76 106 $box .= $word;
77 106 $x += $word_width;
78 106 ▶ if ($word_height > $maxlineheight) {
Branch: 1 branch
BranchTrueFalseTotal
if ($word_height > $maxlineheight) 26 80 106
79 26 $maxlineheight = $word_height;
80 }
81 106 };
82  
83 106 ▶ my $this_space_width = (!$is_beginning_of_line->() ? $space_width : 0);
Branch: 1 branch
BranchTrueFalseTotal
!&$is_beginning_of_line() ? : 92 14 106
84  
85 106 ▶ if ($x+$word_width+$this_space_width < $boxwidth &&
Branch: 1 branch
BranchTrueFalseTotal
if ($x + $word_width + $this_space_width < $boxwidth and $y + $word_height < $boxheight) { } 94 12 106
Condition66%2/3
$x + $word_width + $this_space_width < $boxwidth and $y + $word_height < $boxheight
!ll&&!rl&&r
12094
MC/DC50%1/2
$x + $word_width + $this_space_width < $boxwidth and $y + $word_height < $boxheight
$x + $word_width + $this_space_width < $boxwidth $y + $word_height < $boxheight
Truth table66%2/3
A and B
A= $x + $word_width + $this_space_width < $boxwidthB= $y + $word_height < $boxheight
ABresult
0X0
100
111
86 $y+$word_height < $boxheight) {
87 # fits into this line
88 94 $push_word->();
89 } else {
90 12 $next_line_or_box->();
91 }
92 }
93  
94 14 undef $push_word;
95 14 $maxlineheight = $line_height;
96 14 $next_line_or_box->();
97 }
98  
99 2 ▶ push @out, $box if $box ne "";
Branch: 1 branch
BranchTrueFalseTotal
if $box ne "" 2 0 2
100  
101 2 ▶ if ($lastboxref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lastboxref) 1 1 2
102 1 $lastboxref->{'-box'} = $box;
103 1 $lastboxref->{'-x'} = $x;
104 1 $lastboxref->{'-y'} = $y;
105 1 $lastboxref->{'-maxlineheight'} = $maxlineheight;
106 }
107  
108 2 @out;
109 }
110  
111 #XXX
112 # sub add_br {
113 # my($fontinfo, $boxwidth, $boxheight, %args) = @_;
114 # if (!$args{'-prevlastbox'}) {
115 # die "The -prevlastbox option is missing";
116 # }
117 # my $prevlastbox = delete $args{'-prevlastbox'};
118 # my %new_args = (-box => $prevlastbox->{'-box'}."<br>\n",
119 # -x => 0,
120 # -y => $prevlastbox->{'-maxlineheight'}+$prevlastbox->{'-y'});
121 # break_text("", $fontinfo, $boxwidth, $boxheight, %args, %new_args);
122 # }
123  
124 ###XXX hmmm...????
125 1 ▶sub continue_text_with_nl {
Subroutine continue_text_with_nl: called CC=3 SCAR=11.8
Pod undocumented
126 1 my($text, $fontinfo, $boxwidth, $boxheight, %args) = @_;
127 1 ▶ if (!$args{'-prevlastbox'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"-prevlastbox"}) 0 1 1
128 0 die "The -prevlastbox option is missing";
129 }
130 1 my $prevlastbox = delete $args{'-prevlastbox'};
131 my %new_args = (-box => $prevlastbox->{'-box'}.($prevlastbox->{'-box'}ne""?"\n":""),
132 -x => 0,
133 1 ▶ -y => $prevlastbox->{'-maxlineheight'}+$prevlastbox->{'-y'});
Branch: 1 branch
BranchTrueFalseTotal
$prevlastbox->{'-box'} ne '' ? : 1 0 1
134 1 break_text($text, $fontinfo, $boxwidth, $boxheight, %args, %new_args);
135 }
136  
137 0 ▶sub add_y {
Subroutine add_y: not called CC=2 SCAR=17.9
Pod undocumented
138 0 my($prevlastbox_ref, $yadd) = @_;
139 0 ▶ $prevlastbox_ref->{'y'} = 0 if !$prevlastbox_ref->{'y'};
Branch: 1 branch
BranchTrueFalseTotal
unless $prevlastbox_ref->{"y"} 0 0 0
140 0 $prevlastbox_ref->{'y'} += $yadd;
141 }
142  
143 110 ▶sub get_bounds {
Subroutine get_bounds: called CC=5 SCAR=16.3
Pod undocumented
144 110 my($text, $fontinfo) = @_;
145 110 my($x,$y) = (0,0);
146  
147 110 foreach my $ch (split //, $text) {
148 732 my $ord = ord $ch;
149 732 ▶ if ($ord >= $fontinfo->{'firstchar'} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($ord >= $fontinfo->{"firstchar"} and $ord <= $fontinfo->{"lastchar"}) 732 0 732
Condition33%1/3
$ord >= $fontinfo->{"firstchar"} and $ord <= $fontinfo->{"lastchar"}
!ll&&!rl&&r
00732
MC/DC0%0/2
$ord >= $fontinfo->{"firstchar"} and $ord <= $fontinfo->{"lastchar"}
$ord >= $fontinfo->{"firstchar"} $ord <= $fontinfo->{"lastchar"}
Truth table33%1/3
A and B
A= $ord >= $fontinfo->{"firstchar"}B= $ord <= $fontinfo->{"lastchar"}
ABresult
0X0
100
111
150 $ord <= $fontinfo->{'lastchar'}) {
151 732 my $inx = $ord-$fontinfo->{'firstchar'};
152 732 $x += $fontinfo->{'widths'}[$inx];
153 732 my $chheight = $fontinfo->{'heights'}[$inx];
154 732 ▶ if ($chheight > $y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chheight > $y) 110 622 732
155 110 $y = $chheight;
156 }
157 }
158 }
159  
160 110 ($x,$y);
161 }
162  
163 2 1;
164  
165 __END__
« WE_Content/PerlDD.pm ↑ Summary WE/Util/Escape.pm »