| 1 | package Gapp::TableMap; | ||
| 2 | { | ||
| 3 | $Gapp::TableMap::VERSION = '0.60'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use Moose; | ||
| 7 | use MooseX::SemiAffordanceAccessor; | ||
| 8 | use MooseX::StrictConstructor; | ||
| 9 | |||
| 10 | use Gapp::TableCell; | ||
| 11 | |||
| 12 | # array of Gapp::TableCell objects | ||
| 13 | has 'cells' => ( | ||
| 14 | is => 'ro', | ||
| 15 | isa => 'ArrayRef', | ||
| 16 | lazy_build => 1, | ||
| 17 | ); | ||
| 18 | |||
| 19 | # number of columns in table ( used when creating Gtk2::Table ) | ||
| 20 | has 'col_count' => ( | ||
| 21 | is => 'rw', | ||
| 22 | isa => 'Int', | ||
| 23 | writer => '_set_col_count', | ||
| 24 | default => 0, | ||
| 25 | ); | ||
| 26 | |||
| 27 | # number of rows in table ( used when creating Gtk2::Table ) | ||
| 28 | has 'row_count' => ( | ||
| 29 | is => 'rw', | ||
| 30 | isa => 'Int', | ||
| 31 | writer => '_set_row_count', | ||
| 32 | default => 0, | ||
| 33 | ); | ||
| 34 | |||
| 35 | # the ascii representation of the table | ||
| 36 | has 'string' => ( | ||
| 37 | is => 'rw', | ||
| 38 | isa => 'Str', | ||
| 39 | default => '', | ||
| 40 | trigger => sub { $_[0]->clear_cells }, | ||
| 41 | ); | ||
| 42 | |||
| 43 | # the ascii table, cleaned up and split into an array | ||
| 44 | has '_rows' => ( | ||
| 45 | is => 'rw', | ||
| 46 | isa => 'ArrayRef', | ||
| 47 | lazy_build => 1, | ||
| 48 | ); | ||
| 49 | |||
| 50 | # the horizontal vertices | ||
| 51 | has '_rasterx' => ( | ||
| 52 | is => 'rw', | ||
| 53 | isa => 'HashRef', | ||
| 54 | default => sub { { } }, | ||
| 55 | ); | ||
| 56 | |||
| 57 | sub _add_rasterx { | ||
| 58 | my ( $self, $point ) = @_; | ||
| 59 | $self->_rasterx->{$point} = 1; | ||
| 60 | } | ||
| 61 | |||
| 62 | # the vertical vertices | ||
| 63 | has '_rastery' => ( | ||
| 64 | is => 'rw', | ||
| 65 | isa => 'HashRef', | ||
| 66 | default => sub { { } }, | ||
| 67 | ); | ||
| 68 | |||
| 69 | sub _add_rastery { | ||
| 70 | my ( $self, $point ) = @_; | ||
| 71 | $self->_rastery->{$point} = 1; | ||
| 72 | } | ||
| 73 | |||
| 74 | # the width of the ascii table in chars | ||
| 75 | has '_xunits' => ( | ||
| 76 | is => 'rw', | ||
| 77 | isa => 'Int', | ||
| 78 | default => 0, | ||
| 79 | ); | ||
| 80 | |||
| 81 | # the height of the ascii table in chars | ||
| 82 | has '_yunits' => ( | ||
| 83 | is => 'rw', | ||
| 84 | isa => 'Int', | ||
| 85 | default => 0, | ||
| 86 | ); | ||
| 87 | |||
| 88 | sub _build_cells { | ||
| 89 | my ( $self ) = @_; | ||
| 90 | |||
| 91 | $self->_find_rasters; | ||
| 92 | |||
| 93 | my @cells; | ||
| 94 | my @rasterx = sort { $a <=> $b } keys %{ $self->_rasterx }; | ||
| 95 | my @rastery = sort { $a <=> $b } keys %{ $self->_rastery }; | ||
| 96 | |||
| 97 | |||
| 98 | |||
| 99 | my $ypos = 0; | ||
| 100 | for my $r ( @{ $self->_rows } ) { | ||
| 101 | my $row = $r; | ||
| 102 | |||
| 103 | # skip rows with no fields (borders and spacers); | ||
| 104 | $ypos++, next if $row !~ /[|^_']/; | ||
| 105 | |||
| 106 | # left_x, right_x, left_attach, right_attach | ||
| 107 | my ( $lx, $rx, $la, $ra ) = ( 0, 0, 0, 0 ); | ||
| 108 | |||
| 109 | # remove leading table border character and remember it | ||
| 110 | $row =~ s/^(.)//; | ||
| 111 | my $lead_char = $1; | ||
| 112 | |||
| 113 | # split row into field parts | ||
| 114 | for my $field ( split /[|+'_^~]/, $row ) { | ||
| 115 | $rx = $lx + 1 + length $field; | ||
| 116 | |||
| 117 | # calculate span of this field | ||
| 118 | my $spanx = 0; | ||
| 119 | for my $x ( @rasterx ) { | ||
| 120 | $spanx++ if $x >= $lx && $x < $rx; | ||
| 121 | last if $x > $rx; | ||
| 122 | } | ||
| 123 | |||
| 124 | # calculate right attach using cell span | ||
| 125 | $ra = $la + $spanx; | ||
| 126 | |||
| 127 | # process field only if doesn't span from previous row | ||
| 128 | my $last_row = $self->_rows->[$ypos-1]; | ||
| 129 | if ( $ypos > 0 && substr ( $last_row, $lx + 1, 1 ) =~ /[-=>%\[\]]/ ) { | ||
| 130 | |||
| 131 | |||
| 132 | # calculate top/bottom attach by searching for bottom of field | ||
| 133 | my ( $ta, $ba ) = ( 0, 0 ); | ||
| 134 | |||
| 135 | # skip first line, which defines no columns | ||
| 136 | my $seeky; | ||
| 137 | for ( $seeky = 1; $seeky < $self->_yunits; ++$seeky ) { | ||
| 138 | |||
| 139 | if ( $seeky < $ypos and $self->_rastery->{$seeky} ) { | ||
| 140 | $ta++; | ||
| 141 | $ba++; | ||
| 142 | } | ||
| 143 | elsif ( $self->_rastery->{$seeky} ) { | ||
| 144 | $ba++; | ||
| 145 | } | ||
| 146 | |||
| 147 | # out here if hit end of field | ||
| 148 | last if $seeky > $ypos and substr( $self->_rows->[$seeky], $lx+1, 1) =~ /[-+=\[\]\%]/; | ||
| 149 | $field .= "\n". substr($self->_rows->[$seeky], $lx+1, $rx-$lx-1) if $seeky > $ypos; | ||
| 150 | |||
| 151 | } | ||
| 152 | |||
| 153 | # create cells ( if field not emptry ) | ||
| 154 | if ( $field =~ /\S/ ) { | ||
| 155 | |||
| 156 | my $cell = Gapp::TableCell->new( | ||
| 157 | left => $la, right => $ra, top => $ta, bottom => $ba, | ||
| 158 | hexpand => $self->_find_xexpansion( $lx+1, $ypos, $rx ), | ||
| 159 | vexpand => $self->_find_yexpansion( $lx, $ypos, $seeky-1 ), | ||
| 160 | xalign => $self->_find_xalign( $lx+1, $ypos, $rx ), | ||
| 161 | yalign => $self->_find_yalign( $lx, $ypos, $seeky-1 ), | ||
| 162 | ); | ||
| 163 | |||
| 164 | push @cells, $cell; | ||
| 165 | } | ||
| 166 | } | ||
| 167 | |||
| 168 | # setup vars for next field | ||
| 169 | $lx = $rx, | ||
| 170 | $la = $ra; | ||
| 171 | |||
| 172 | } | ||
| 173 | |||
| 174 | # move on to next row | ||
| 175 | $ypos++; | ||
| 176 | } | ||
| 177 | |||
| 178 | # store cells and table dimensions | ||
| 179 | $self->_set_row_count( $#rastery ); | ||
| 180 | $self->_set_col_count( $#rasterx ); | ||
| 181 | return \@cells; | ||
| 182 | } | ||
| 183 | |||
| 184 | sub _build__rows { | ||
| 185 | my ( $self ) = @_; | ||
| 186 | |||
| 187 | my @rows = split /\n/, $self->string; | ||
| 188 | @rows = map { s/^\s*//; $_ } @rows; | ||
| 189 | @rows = map { s/\s*$//; $_ } @rows; | ||
| 190 | |||
| 191 | return \@rows; | ||
| 192 | } | ||
| 193 | |||
| 194 | # traverse the rows to dermine where the rasters occur | ||
| 195 | sub _find_rasters { | ||
| 196 | my ( $self ) = @_; | ||
| 197 | |||
| 198 | my $y = 0; | ||
| 199 | my $row_length = 0; | ||
| 200 | for my $r ( @{ $self->_rows } ) { | ||
| 201 | |||
| 202 | # if this row has + or -, then it is a layout row | ||
| 203 | if ( $r =~ /-+/ ) { | ||
| 204 | |||
| 205 | # store raster | ||
| 206 | $self->_add_rastery( $y ); | ||
| 207 | } | ||
| 208 | |||
| 209 | # if row has any of | ^ _ ' ~ it is a content row | ||
| 210 | if ( $r =~ /[|^_'~]/ ) { | ||
| 211 | |||
| 212 | # split on field separating characters | ||
| 213 | my $x = 0; | ||
| 214 | for my $field ( split /[|^_']/, $r ) { | ||
| 215 | |||
| 216 | # determine and store raster | ||
| 217 | $x += length $field; | ||
| 218 | $self->_add_rasterx( $x ); | ||
| 219 | $x++; | ||
| 220 | } | ||
| 221 | |||
| 222 | $row_length = $x if $x > $row_length; | ||
| 223 | } | ||
| 224 | |||
| 225 | # on to the next row | ||
| 226 | $y++; | ||
| 227 | } | ||
| 228 | |||
| 229 | $self->_set_xunits( $row_length ); | ||
| 230 | $self->_set_yunits( $y ); | ||
| 231 | } | ||
| 232 | |||
| 233 | |||
| 234 | sub _find_xexpansion { | ||
| 235 | my ( $self, $x, $y, $max_x ) = @_; | ||
| 236 | |||
| 237 | my $char; | ||
| 238 | my $segment = substr $self->_rows->[$y-1], $x, $max_x + 1 - $x ; | ||
| 239 | return [qw( fill expand )] if $segment =~ /[>^]/; | ||
| 240 | return ["fill"]; | ||
| 241 | } | ||
| 242 | |||
| 243 | sub _find_yexpansion { | ||
| 244 | my ( $self, $x, $y, $max_y ) = @_; | ||
| 245 | |||
| 246 | for my $row ( @{$self->_rows}[$y..$max_y] ) { | ||
| 247 | my $segment = substr $row, $x, 1; | ||
| 248 | return [qw( fill expand )] if $segment eq '^'; | ||
| 249 | } | ||
| 250 | |||
| 251 | return ["fill"]; | ||
| 252 | } | ||
| 253 | |||
| 254 | sub _find_xalign { | ||
| 255 | my ( $self, $x, $y, $max_x ) = @_; | ||
| 256 | |||
| 257 | my $row = $self->_rows->[$y-1]; | ||
| 258 | my $char; | ||
| 259 | while ( $x <= $max_x ) { | ||
| 260 | $char = substr($row, $x, 1); | ||
| 261 | return 0 if $char eq '['; | ||
| 262 | return 1 if $char eq ']'; | ||
| 263 | return 0.5 if $char eq '%'; | ||
| 264 | ++$x; | ||
| 265 | } | ||
| 266 | |||
| 267 | return -1; | ||
| 268 | } | ||
| 269 | |||
| 270 | sub _find_yalign { | ||
| 271 | my ( $self, $x, $y, $max_y ) = @_; | ||
| 272 | |||
| 273 | my $char; | ||
| 274 | while ( $y <= $max_y ) { | ||
| 275 | my $char = substr $self->_rows->[$y], $x, 1; | ||
| 276 | return 0 if $char eq "'"; | ||
| 277 | return 1 if $char eq '_'; | ||
| 278 | return 0.5 if $char eq '~'; | ||
| 279 | ++$y; | ||
| 280 | } | ||
| 281 | |||
| 282 | return -1; | ||
| 283 | } | ||
| 284 | |||
| 285 | |||
| 286 | 1; | ||
| 287 | |||
| 288 | |||
| 289 | |||
| 290 | __END__ |