| 1 | # /=====================================================================\ # | ||
| 2 | # | LaTeXML::Core::Alignment | # | ||
| 3 | # | Support for tabular/array environments | # | ||
| 4 | # |=====================================================================| # | ||
| 5 | # | Part of LaTeXML: | # | ||
| 6 | # | Public domain software, produced as part of work done by the | # | ||
| 7 | # | United States Government & not subject to copyright in the US. | # | ||
| 8 | # |---------------------------------------------------------------------| # | ||
| 9 | # | Bruce Miller <bruce.miller@nist.gov> #_# | # | ||
| 10 | # | http://dlmf.nist.gov/LaTeXML/ (o o) | # | ||
| 11 | # \=========================================================ooo==U==ooo=/ # | ||
| 12 | package LaTeXML::Core::Alignment; | ||
| 13 | 5 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 5 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 5 | ▶ | use LaTeXML::Global; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | 5 | ▶ | use LaTeXML::Common::Error; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | 5 | ▶ | use LaTeXML::Core::Token; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 18 | 5 | ▶ | use LaTeXML::Core::Tokens; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | 5 | ▶ | use LaTeXML::Common::Object; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | 0 | ▶ | use LaTeXML::Common::XML; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 21 | use LaTeXML::Common::Dimension; | ||
| 22 | use LaTeXML::Core::Alignment::Template; | ||
| 23 | use List::Util qw(max sum); | ||
| 24 | use base qw(LaTeXML::Core::Whatsit); | ||
| 25 | use base qw(Exporter); | ||
| 26 | our @EXPORT = (qw( | ||
| 27 | &ReadAlignmentTemplate &MatrixTemplate)); | ||
| 28 | |||
| 29 | #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
| 30 | # An "Alignment" is an array/tabular construct as: | ||
| 31 | # <tabular><tr><td>... | ||
| 32 | # or, for math mode | ||
| 33 | # <XMArray><XMRow><XMCell>... | ||
| 34 | # (where initially, each XMCell will contain an XMArg to indicate | ||
| 35 | # individual parsing of each cell's content is desired) | ||
| 36 | # | ||
| 37 | # An Alignment object is a sort of fake Whatsit; | ||
| 38 | # It takes some magic to sneak it into the Digestion stream | ||
| 39 | # (see TeX.pool \@start@alignment), but it needs to be created | ||
| 40 | # BEFORE the contents of the alignment are digested, | ||
| 41 | # since we stuff a lot of information into it | ||
| 42 | # (row, column boxes, borders, spacing, etc...) | ||
| 43 | # But once it has been captured, it should otherwise act | ||
| 44 | # like a Whatsit and be responsible for construction (beAbsorbed), | ||
| 45 | # and sizing estimation (computeSize) | ||
| 46 | # | ||
| 47 | # Ultimately, this should be better tied into DefConstructor | ||
| 48 | # because an Alignment currently doesn't know what CS created it (debugging!); | ||
| 49 | # Also, it would better connect the things being constructed, reversion, etc. | ||
| 50 | #====================================================================== | ||
| 51 | DebuggableFeature('alignment', "Debug guessing headers of alignments/tables"); | ||
| 52 | |||
| 53 | # Create a new Alignment. | ||
| 54 | # %data can contain: | ||
| 55 | # template : an Alignment::Template object | ||
| 56 | # openContainer = sub($doc,%attrib); creates the container element with given attributes | ||
| 57 | # closeContainer = sub($doc); closes the container | ||
| 58 | # openRow = sub($doc,%attrib); creates the row element with given attributes | ||
| 59 | # closeRow = closes the row | ||
| 60 | # openColumn = sub($doc,%attrib); creates the column element with given attributes | ||
| 61 | # closeColumn = closes the column | ||
| 62 | # attributes = hashref containing extra attributes for the container element. | ||
| 63 | sub new { | ||
| 64 | my ($class, %data) = @_; | ||
| 65 | my $self = bless {%data}, $class; | ||
| 66 | $$self{template} = LaTeXML::Core::Alignment::Template->new() unless $$self{template}; | ||
| 67 | $$self{template} = parseAlignmentTemplate($$self{template}) unless ref $$self{template}; | ||
| 68 | $$self{rows} = []; | ||
| 69 | $$self{current_column} = 0; | ||
| 70 | $$self{current_row} = undef; | ||
| 71 | $$self{level} = $STATE->getFrameDepth; | ||
| 72 | $$self{properties} = {} unless $$self{properties}; | ||
| 73 | # Copy any attribute width, height, depth to main properties. | ||
| 74 | # BUT remove them from XML attributes!!! (?) | ||
| 75 | if (my $attributes = $$self{properties}{attributes}) { | ||
| 76 | foreach my $key (qw(width height depth)) { | ||
| 77 | if (my $val = $$attributes{$key}) { | ||
| 78 | $$self{properties}{$key} = $val; delete $$attributes{$key}; } } } | ||
| 79 | return $self; } | ||
| 80 | |||
| 81 | #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
| 82 | # Alignment specific accessors | ||
| 83 | sub getTemplate { | ||
| 84 | my ($self, $template) = @_; | ||
| 85 | return $$self{template}; } | ||
| 86 | |||
| 87 | sub currentRow { | ||
| 88 | my ($self) = @_; | ||
| 89 | return $$self{current_row}; } | ||
| 90 | |||
| 91 | sub newRow { | ||
| 92 | my ($self) = @_; | ||
| 93 | my $row = $$self{template}->clone; | ||
| 94 | $$self{current_row} = $row; | ||
| 95 | $$self{current_column} = 0; | ||
| 96 | push(@{ $$self{rows} }, $row); | ||
| 97 | return $row; } | ||
| 98 | |||
| 99 | sub removeRow { | ||
| 100 | my ($self) = @_; | ||
| 101 | my @rows = @{ $$self{rows} }; | ||
| 102 | if (@rows) { | ||
| 103 | my $row = pop(@rows); | ||
| 104 | $$self{rows} = [@rows]; | ||
| 105 | return $row; } | ||
| 106 | else { | ||
| 107 | return; } } | ||
| 108 | |||
| 109 | sub prependRows { | ||
| 110 | my ($self, @rows) = @_; | ||
| 111 | unshift(@{ $$self{rows} }, @rows); | ||
| 112 | return; } | ||
| 113 | |||
| 114 | sub appendRows { | ||
| 115 | my ($self, @rows) = @_; | ||
| 116 | push(@{ $$self{rows} }, @rows); | ||
| 117 | return; } | ||
| 118 | |||
| 119 | sub rows { | ||
| 120 | my ($self) = @_; | ||
| 121 | return @{ $$self{rows} }; } | ||
| 122 | |||
| 123 | sub addLine { | ||
| 124 | my ($self, $border, @cols) = @_; | ||
| 125 | if (my $row = $$self{current_row}) { | ||
| 126 | $$self{current_column} = 1; # Pretend doesn't occupy any columns | ||
| 127 | if (@cols) { | ||
| 128 | foreach my $c (@cols) { | ||
| 129 | my $colspec = $row->column($c); | ||
| 130 | $$colspec{border} .= $border; } } | ||
| 131 | else { | ||
| 132 | foreach my $colspec (@{ $$row{columns} }) { | ||
| 133 | $$colspec{border} .= $border; } } } | ||
| 134 | return; } | ||
| 135 | |||
| 136 | sub nextColumn { | ||
| 137 | my ($self) = @_; | ||
| 138 | return unless $$self{current_row}; | ||
| 139 | my $colspec = $$self{current_row}->column(++$$self{current_column}); | ||
| 140 | if (!$colspec) { | ||
| 141 | Error('unexpected', '&', $STATE->getStomach->getGullet, "Extra alignment tab '&'"); | ||
| 142 | $$self{current_row}->addColumn(align => 'center'); | ||
| 143 | $colspec = $$self{current_row}->column($$self{current_column}); } | ||
| 144 | return $colspec; } | ||
| 145 | |||
| 146 | sub lastColumn { | ||
| 147 | my ($self) = @_; | ||
| 148 | my $row = $$self{current_row}; | ||
| 149 | return unless $row; | ||
| 150 | $$self{current_column} = scalar @{ $$row{columns} }; | ||
| 151 | return $row->column($$self{current_column}); } | ||
| 152 | |||
| 153 | sub currentColumnNumber { | ||
| 154 | my ($self) = @_; | ||
| 155 | return $$self{current_column}; } | ||
| 156 | |||
| 157 | sub currentRowNumber { | ||
| 158 | my ($self) = @_; | ||
| 159 | my $n = 0; | ||
| 160 | foreach my $row (@{ $$self{rows} }) { | ||
| 161 | $n++ unless $$row{pseudorow}; } | ||
| 162 | return $n; } | ||
| 163 | |||
| 164 | sub currentColumn { | ||
| 165 | my ($self) = @_; | ||
| 166 | return $$self{current_row} && $$self{current_row}->column($$self{current_column}); } | ||
| 167 | |||
| 168 | sub getColumn { | ||
| 169 | my ($self, $n) = @_; | ||
| 170 | return $$self{current_row} && $$self{current_row}->column($n); } | ||
| 171 | |||
| 172 | # Ugh... these take boxes; adding before/after columns takes tokens! | ||
| 173 | sub addBeforeRow { | ||
| 174 | my ($self, @boxes) = @_; | ||
| 175 | $$self{current_row}{before} = [@{ $$self{current_row}{before} || [] }, @boxes]; | ||
| 176 | return; } | ||
| 177 | |||
| 178 | sub addAfterRow { | ||
| 179 | my ($self, @boxes) = @_; | ||
| 180 | $$self{current_row}{after} = [@{ $$self{current_row}{after} || [] }, @boxes]; | ||
| 181 | return; } | ||
| 182 | |||
| 183 | sub omitColumn { | ||
| 184 | my ($self) = @_; | ||
| 185 | if (my $column = $self->currentColumn) { | ||
| 186 | $$column{omitted} = 1; } | ||
| 187 | return; } | ||
| 188 | |||
| 189 | sub omitNextColumn { | ||
| 190 | my ($self) = @_; | ||
| 191 | if (my $column = $$self{current_row} && $$self{current_row}->column($$self{current_column} + 1)) { | ||
| 192 | $$column{omitted} = 1; } | ||
| 193 | return; } | ||
| 194 | |||
| 195 | sub getColumnBefore { | ||
| 196 | my ($self) = @_; | ||
| 197 | my $column; | ||
| 198 | if (($column = $self->currentColumn) && !$$column{omitted}) { | ||
| 199 | return Tokens(T_CS('\@column@before'), @{ $$column{before} }); } | ||
| 200 | else { | ||
| 201 | return Tokens(); } } | ||
| 202 | |||
| 203 | sub getColumnAfter { | ||
| 204 | my ($self) = @_; | ||
| 205 | my $column; | ||
| 206 | if (($column = $self->currentColumn) && !$$column{omitted}) { | ||
| 207 | return Tokens(@{ $$column{after} }, T_CS('\@column@after')); } | ||
| 208 | else { | ||
| 209 | return Tokens(); } } | ||
| 210 | |||
| 211 | #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
| 212 | # Support for building an alignment's Rows & Columns | ||
| 213 | |||
| 214 | sub startRow { | ||
| 215 | my ($self, $pseudorow) = @_; | ||
| 216 | $self->newRow; | ||
| 217 | my $stomach = $STATE->getStomach; | ||
| 218 | $stomach->bgroup; # Grouping around ROW! | ||
| 219 | if ($pseudorow) { | ||
| 220 | $self->currentRow->{pseudorow} = 1; } | ||
| 221 | else { | ||
| 222 | push(@LaTeXML::LIST, $stomach->digest(T_CS('\@row@before'))); } | ||
| 223 | $$self{in_row} = 1; | ||
| 224 | $STATE->assignValue(alignmentStartColumn => 0); # ??? | ||
| 225 | return; } | ||
| 226 | |||
| 227 | sub endRow { | ||
| 228 | my ($self) = @_; | ||
| 229 | return unless $$self{in_row}; | ||
| 230 | $self->endColumn() if $$self{in_column}; | ||
| 231 | $STATE->getStomach->egroup; # Grouping around ROW! | ||
| 232 | # Digest(T_CS('\@row@after')); | ||
| 233 | $$self{in_row} = undef; | ||
| 234 | return; } | ||
| 235 | |||
| 236 | sub startColumn { | ||
| 237 | my ($self, $pseudorow) = @_; | ||
| 238 | if (!$$self{in_row}) { | ||
| 239 | $self->startRow($pseudorow); } | ||
| 240 | elsif ($pseudorow) { | ||
| 241 | $self->currentRow->{pseudorow} = 1; } | ||
| 242 | $STATE->getStomach->bgroup; # Grouping around CELL! | ||
| 243 | # Note: a VERY round-about way of tracking the column spanning! | ||
| 244 | $STATE->assignValue(alignmentStartColumn => $self->currentColumnNumber); | ||
| 245 | my $colspec = $self->nextColumn; | ||
| 246 | $LaTeXML::ALIGN_STATE = 1000000; | ||
| 247 | $$self{in_column} = 1; | ||
| 248 | return; } | ||
| 249 | |||
| 250 | sub endColumn { | ||
| 251 | my ($self) = @_; | ||
| 252 | return unless $$self{in_column}; | ||
| 253 | $STATE->getStomach->egroup; # Grouping around CELL! | ||
| 254 | $$self{in_column} = undef; | ||
| 255 | return; } | ||
| 256 | |||
| 257 | #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
| 258 | # Making the Alignment act like a Whatsit | ||
| 259 | sub toString { | ||
| 260 | my ($self) = @_; | ||
| 261 | return "Alignment[]"; } | ||
| 262 | |||
| 263 | # Methods for overloaded operators | ||
| 264 | sub stringify { | ||
| 265 | my ($self) = @_; | ||
| 266 | return "Alignment[]"; } | ||
| 267 | |||
| 268 | sub revert { | ||
| 269 | my ($self) = @_; | ||
| 270 | if ($LaTeXML::DUAL_BRANCH && ($LaTeXML::DUAL_BRANCH eq 'content') && $$self{content_reversion}) { | ||
| 271 | return $$self{content_reversion}->unlist; } | ||
| 272 | return $$self{reversion}->unlist; } | ||
| 273 | |||
| 274 | sub setReversion { | ||
| 275 | my ($self, $tokens) = @_; | ||
| 276 | $$self{reversion} = $tokens; | ||
| 277 | return; } | ||
| 278 | |||
| 279 | sub setContentReversion { | ||
| 280 | my ($self, $tokens) = @_; | ||
| 281 | $$self{content_reversion} = $tokens; | ||
| 282 | return; } | ||
| 283 | |||
| 284 | sub computeSize { | ||
| 285 | my ($self) = @_; | ||
| 286 | $self->normalizeAlignment; | ||
| 287 | return ($$self{cwidth}, $$self{cheight}, $$self{cdepth}); } | ||
| 288 | |||
| 289 | #====================================================================== | ||
| 290 | # Constructing the XML for the alignment. | ||
| 291 | |||
| 292 | sub beAbsorbed { | ||
| 293 | my ($self, $document) = @_; | ||
| 294 | my $attr = $self->getProperty('attributes'); | ||
| 295 | my $body = $self->getBody; | ||
| 296 | my $ismath = $$self{isMath}; | ||
| 297 | $self->normalizeAlignment; | ||
| 298 | my @rows = @{ $$self{rows} }; | ||
| 299 | return unless @rows; | ||
| 300 | |||
| 301 | # Guard via the absorb limit to avoid infinite loops | ||
| 302 | if ($LaTeXML::ABSORB_LIMIT) { | ||
| 303 | my $absorb_counter = $STATE->lookupValue('absorb_count') || 0; | ||
| 304 | $STATE->assignValue(absorb_count => ++$absorb_counter, 'global'); | ||
| 305 | if ($absorb_counter > $LaTeXML::ABSORB_LIMIT) { | ||
| 306 | Fatal('timeout', 'absorb_limit', $self, | ||
| 307 | "Whatsit absorb limit of $LaTeXML::ABSORB_LIMIT exceeded, infinite loop?"); } } | ||
| 308 | |||
| 309 | # We _should_ attach boxes to the alignment and rows, | ||
| 310 | # but (ATM) we've only got sensible boxes for the cells. | ||
| 311 | &{ $$self{openContainer} }($document, ($attr ? %$attr : ()), | ||
| 312 | cwidth => $$self{cwidth}, cheight => $$self{cheight}, cdepth => $$self{cdepth}, | ||
| 313 | rowheights => $$self{rowheights}, | ||
| 314 | columnwidths => $$self{columnwidths}, | ||
| 315 | ); | ||
| 316 | foreach my $row (@rows) { | ||
| 317 | my $vpad = $$row{padding}; | ||
| 318 | &{ $$self{openRow} }($document, | ||
| 319 | 'xml:id' => $$row{id}, tags => $$row{tags}, | ||
| 320 | # Which properties do we expose to the constructor? | ||
| 321 | x => $$row{x}, y => $$row{y}, | ||
| 322 | cwidth => $$row{cwidth}, cheight => $$row{cheight}, cdepth => $$row{cdepth}, | ||
| 323 | ); | ||
| 324 | if (my $before = $$row{before}) { | ||
| 325 | map { $document->absorb($_) } @$before; } | ||
| 326 | foreach my $cell (@{ $$row{columns} }) { | ||
| 327 | next if $$cell{skipped}; | ||
| 328 | # Normalize the border attribute | ||
| 329 | my $border = join(' ', sort(map { split(/ */, $_) } $$cell{border} || '')); | ||
| 330 | $border =~ s/(.) \1/$1$1/g; | ||
| 331 | my @classes = ($$cell{class}); | ||
| 332 | my $empty = $$cell{empty}; | ||
| 333 | my ($pre, $post); | ||
| 334 | # Heuristic: use common td CSS padding, or none, or explicit spacing within cell | ||
| 335 | # Ideally, we'd compare to a current \(tab|array)colsep | ||
| 336 | # and create a special CSS class for non-standard values | ||
| 337 | my $lpad = ($$cell{lspaces} ? $$cell{lspaces}->getWidth->valueOf : 0); | ||
| 338 | my $rpad = ($$cell{rspaces} ? $$cell{rspaces}->getWidth->valueOf : 0); | ||
| 339 | if ((!$empty || $$cell{boxes}) && ($lpad < Dimension('0.2em')->valueOf)) { | ||
| 340 | push(@classes, 'ltx_nopad_l') unless $ismath; } | ||
| 341 | elsif ($lpad < Dimension('1.5em')->valueOf) { } # do nothing | ||
| 342 | else { $pre = $$cell{lspaces}; } | ||
| 343 | if ((!$empty || $$cell{boxes}) && ($rpad < Dimension('0.2em')->valueOf)) { | ||
| 344 | push(@classes, 'ltx_nopad_r') unless $ismath; } | ||
| 345 | elsif ($rpad < Dimension('1.5em')->valueOf) { } # do nothing | ||
| 346 | else { $post = $$cell{rspaces}; } | ||
| 347 | my $class = join(' ', grep { $_; } @classes); | ||
| 348 | $$cell{cell} = &{ $$self{openColumn} }($document, | ||
| 349 | align => $$cell{align}, width => $$cell{width}, | ||
| 350 | vattach => $$cell{vattach}, | ||
| 351 | ($vpad ? (cssstyle => 'padding-bottom:' . ToString($vpad)) : ()), | ||
| 352 | (($$cell{colspan} || 1) != 1 ? (colspan => $$cell{colspan}) : ()), | ||
| 353 | (($$cell{rowspan} || 1) != 1 ? (rowspan => $$cell{rowspan}) : ()), | ||
| 354 | ($border ? (border => $border) : ()), | ||
| 355 | ($$cell{thead} ? (thead => join(' ', sort keys %{ $$cell{thead} })) : ()), | ||
| 356 | # Which properties do we expose to the constructor? | ||
| 357 | x => $$cell{x}, y => $$cell{y}, | ||
| 358 | cwidth => $$cell{cwidth}, cheight => $$cell{cheight}, cdepth => $$cell{cdepth}, | ||
| 359 | class => $class | ||
| 360 | ); | ||
| 361 | if (!$$cell{skippable}) { | ||
| 362 | local $LaTeXML::BOX = $$cell{boxes}; | ||
| 363 | $document->openElement('ltx:XMArg', rule => 'Anything,') if $ismath; # Hacky! | ||
| 364 | $document->absorb($pre) if $pre; | ||
| 365 | $document->absorb($$cell{boxes}); | ||
| 366 | $document->absorb($post) if $post; | ||
| 367 | $document->closeElement('ltx:XMArg') if $ismath; | ||
| 368 | } | ||
| 369 | &{ $$self{closeColumn} }($document); } | ||
| 370 | if (my $after = $$row{after}) { | ||
| 371 | map { $document->absorb($_) } @$after; } | ||
| 372 | &{ $$self{closeRow} }($document); } | ||
| 373 | my $node = &{ $$self{closeContainer} }($document); | ||
| 374 | |||
| 375 | # If we're not nested inside another tabular | ||
| 376 | # [This should be an afterConstruct somewhere?] | ||
| 377 | # If requested to guess headers & we're not nested inside another tabular | ||
| 378 | if (!$document->findnodes("ancestor::ltx:tabular", $node)) { | ||
| 379 | my $hashead = $document->findnodes('descendant::ltx:td[@thead]', $node); | ||
| 380 | # If requested && no cells are already marked as being thead, apply heuristic | ||
| 381 | if ($self->getProperty('guess_headers') && !$hashead) { | ||
| 382 | guess_alignment_headers($document, $node, $self); } | ||
| 383 | # Otherwise, if not a math array, group thead & tbody rows | ||
| 384 | elsif ($hashead && !$body->isMath) { # in case already marked w/thead|tbody | ||
| 385 | alignment_regroup_rows($document, $node); } } | ||
| 386 | |||
| 387 | return $node; } | ||
| 388 | |||
| 389 | #====================================================================== | ||
| 390 | # Normalize an alignment before construction | ||
| 391 | # * consolodating column & row spanning information | ||
| 392 | # * scanning for empty rows & columns and collapsing them | ||
| 393 | # (while accounting for spanning, and copying borders appropriately) | ||
| 394 | # Note that a trailing \\ in allignment (often needed to effect \hline) | ||
| 395 | # causes an empty row at the end. Other fancy layout fine-tuning often | ||
| 396 | # involves adding extra rows & columsn for spacing. HTML's table model | ||
| 397 | # is more forgiving that TeX's, so we don't need these extras | ||
| 398 | # and, in fact, they often mess up the html layout! | ||
| 399 | # However, math alignments, and those with expected structure (eg. eqnarray) | ||
| 400 | # should generally NOT have rows & columns collapsed --- except the last row! | ||
| 401 | |||
| 402 | # Also note the inconsistency between TeX & HTML's table models regarding spans. | ||
| 403 | # \multicolumn creates a cell that covers a certain number of columns | ||
| 404 | # which are then omitted from the LaTeX AND the HTML. | ||
| 405 | # OTOH, \multirow creates a cell which overlaps following rows! | ||
| 406 | # The & is still needed to allocate the cells in those rows. | ||
| 407 | # And in fact they need not even be empty! TeX will just pile them up! | ||
| 408 | # However, in HTML the spanned rows ARE omitted! | ||
| 409 | sub normalizeAlignment { | ||
| 410 | my ($self) = @_; | ||
| 411 | return if $$self{normalized}; | ||
| 412 | #====================================================================== | ||
| 413 | $self->normalize_cell_sizes(); | ||
| 414 | $self->normalize_mark_spans(); | ||
| 415 | $self->normalize_prune_rows(); | ||
| 416 | $self->normalize_prune_columns(); | ||
| 417 | $self->normalize_sum_sizes(); | ||
| 418 | #====================================================================== | ||
| 419 | $$self{normalized} = 1; | ||
| 420 | return; } | ||
| 421 | |||
| 422 | # Compute (approximate) sizes of all cells | ||
| 423 | sub normalize_cell_sizes { | ||
| 424 | my ($self) = @_; | ||
| 425 | # Examines: boxes, align, vattach | ||
| 426 | # Sets: cwidth, cheight, cdepth (per cell) & empty | ||
| 427 | my $i = -1; | ||
| 428 | foreach my $row (@{ $$self{rows} }) { | ||
| 429 | my $j = -1; | ||
| 430 | $i++; | ||
| 431 | # Do we need to account for any space in the $$row{before} or $$row{after}? | ||
| 432 | foreach my $cell (@{ $$row{columns} }) { | ||
| 433 | $j++; | ||
| 434 | if (my $boxes = $$cell{boxes}) { | ||
| 435 | my ($w, $h, $d, $cw, $ch, $cd) | ||
| 436 | = $boxes->getSize(align => $$cell{align}, width => $$cell{width}, | ||
| 437 | vattach => $$cell{vattach}); | ||
| 438 | my $fullw = $cw; | ||
| 439 | my ($lpad, $rpad, $padh, $padd); | ||
| 440 | if (my $lspaces = $$cell{lspaces}) { | ||
| 441 | ($lpad, $padh, $padd) = $lspaces->getSize; | ||
| 442 | $fullw = ($fullw ? $fullw->add($lpad) : $lpad) if $lpad; | ||
| 443 | $h = ($h ? $h->larger($padh) : $padh) if $padh; | ||
| 444 | $d = ($d ? $d->larger($padd) : $padd) if $padd; } | ||
| 445 | if (my $rspaces = $$cell{rspaces}) { | ||
| 446 | ($rpad, $padh, $padd) = $rspaces->getSize; | ||
| 447 | $fullw = ($fullw ? $fullw->add($rpad) : $rpad) if $rpad; | ||
| 448 | $h = ($h ? $h->larger($padh) : $padh) if $padh; | ||
| 449 | $d = ($d ? $d->larger($padd) : $padd) if $padd; } | ||
| 450 | my @boxes = $boxes->unlist; | ||
| 451 | my $isrule = scalar(@boxes) | ||
| 452 | && !(grep { !($_->getProperty('isHorizontalRule') || $_->getProperty('isVerticalRule') | ||
| 453 | || $_->getProperty('alignmentSkippable') | ||
| 454 | || (ref $_ eq 'LaTeXML::Core::Comment') | ||
| 455 | ); } @boxes); | ||
| 456 | my $empty = | ||
| 457 | (((!$fullw) || $fullw->valueOf < 1) | ||
| 458 | && (((!$ch) || $ch->valueOf < 1) | ||
| 459 | && ((!$cd) || $cd->valueOf < 1)) | ||
| 460 | || $isrule | ||
| 461 | ) && !preservedBoxes($boxes); | ||
| 462 | $$cell{cwidth} = $w || Dimension(0); | ||
| 463 | $$cell{cheight} = $h || Dimension(0); | ||
| 464 | $$cell{cdepth} = $d || Dimension(0); | ||
| 465 | $$cell{lpadding} = $lpad; | ||
| 466 | $$cell{rpadding} = $rpad; | ||
| 467 | $$cell{empty} = $empty; | ||
| 468 | $$cell{skippable} = $empty || isSkippable($boxes); | ||
| 469 | $$cell{align} = undef if $$cell{skippable}; | ||
| 470 | Debug("CELL[$i,$j] size=" . showSize($w, $h, $d) . " csize " . showSize($cw, $ch, $cd) | ||
| 471 | . ";\n " . join(',', map { $_ . "=" . ToString($$cell{$_}); } sort keys %$cell) | ||
| 472 | . "\n Boxes=" . Stringify($boxes) | ||
| 473 | ) if $LaTeXML::DEBUG{alignment_normalize}; | ||
| 474 | } | ||
| 475 | else { | ||
| 476 | $$cell{empty} = 1; | ||
| 477 | $$cell{skippable} = 1; } | ||
| 478 | } } | ||
| 479 | return; } | ||
| 480 | |||
| 481 | # Check whether all these things are "empty" or only spaces or otherwise skippable in a table cell | ||
| 482 | # Very similar to IsEmpty, but also recognizes spaces or alignmentSkippable items | ||
| 483 | sub isSkippable { | ||
| 484 | my (@things) = @_; | ||
| 485 | foreach my $thing (@things) { | ||
| 486 | my $ref = ref $thing; | ||
| 487 | if (!$thing) { } | ||
| 488 | elsif ($ref eq 'LaTeXML::Core::Comment') { } | ||
| 489 | elsif ($ref eq 'LaTeXML::Core::Tokens') { | ||
| 490 | return 0 unless isSkippable($thing->unlist); } | ||
| 491 | elsif ($ref eq 'LaTeXML::Core::Token') { | ||
| 492 | my $cc = $$thing[1]; | ||
| 493 | return 0 if ($cc == CC_LETTER) || ($cc == CC_OTHER) || ($cc == CC_ACTIVE) || ($cc == CC_CS); } | ||
| 494 | elsif ((!$thing->getProperty('isEmpty')) | ||
| 495 | && (!$thing->getProperty('isSpace')) | ||
| 496 | && (!$thing->getProperty('alignmentSkippable'))) { | ||
| 497 | if ($ref eq 'LaTeXML::Core::Box') { | ||
| 498 | my $s = $thing->getString; | ||
| 499 | return 0 if (defined $s) && ($s !~ /^\s*$/); } | ||
| 500 | elsif ($ref eq 'LaTeXML::Core::List') { | ||
| 501 | return 0 unless isSkippable($thing->unlist); } | ||
| 502 | elsif ($ref eq 'LaTeXML::Core::Whatsit') { | ||
| 503 | if (my $body = $thing->getProperty('content_box')) { | ||
| 504 | return 0 unless isSkippable($body); } | ||
| 505 | else { | ||
| 506 | return 0; } } } } | ||
| 507 | return 1; } | ||
| 508 | |||
| 509 | sub showSize { | ||
| 510 | my ($w, $h, $d) = @_; | ||
| 511 | return '[' . ToString($w) . ' x ' . ToString($h) . ' + ' . ToString($d) . ']'; } | ||
| 512 | |||
| 513 | sub normalize_sum_sizes { | ||
| 514 | my ($self) = @_; | ||
| 515 | my @rowheights = (); | ||
| 516 | my @colwidths = (); | ||
| 517 | my @colrights = (); | ||
| 518 | my @collefts = (); | ||
| 519 | # Uses cell's cwidth,cheight,cdepth | ||
| 520 | # Computes net row & column sizes & positions | ||
| 521 | # add spacing between rows? Or only from \\[..] ? | ||
| 522 | my $strut = $self->getProperty('strut') || Dimension(0); | ||
| 523 | my $hs = $strut->multiply(0.7); | ||
| 524 | my $ds = $strut->multiply(0.3); | ||
| 525 | my @rows = @{ $$self{rows} }; | ||
| 526 | my $nrows = scalar(@rows); | ||
| 527 | |||
| 528 | for (my $i = 0 ; $i < $nrows ; $i++) { | ||
| 529 | my $row = $rows[$i]; | ||
| 530 | my @cols = @{ $$row{columns} }; | ||
| 531 | my $ncols = scalar(@cols); | ||
| 532 | if (my $short = $ncols - scalar(@colwidths)) { # Extend column arrays, if needed | ||
| 533 | push(@colwidths, map { 0 } 1 .. $short); | ||
| 534 | push(@collefts, map { 0 } 1 .. $short); | ||
| 535 | push(@colrights, map { 0 } 1 .. $short); } | ||
| 536 | my ($rowh, $rowd) = (0, 0); | ||
| 537 | my ($rowt, $rowb) = (($$row{tpadding} ? $$row{tpadding}->valueOf : 0), | ||
| 538 | ($$row{bpadding} ? $$row{bpadding}->valueOf : 0)); | ||
| 539 | for (my $j = 0 ; $j < $ncols ; $j++) { | ||
| 540 | my $cell = $cols[$j]; | ||
| 541 | next if $$cell{skipped}; | ||
| 542 | next unless $$cell{boxes}; | ||
| 543 | my $w = $$cell{cwidth}; | ||
| 544 | my $h = $$cell{cheight}; | ||
| 545 | my $d = $$cell{cdepth}; | ||
| 546 | my $t = $$cell{tpadding}; | ||
| 547 | my $b = $$cell{bpadding}; | ||
| 548 | my $r = $$cell{rpadding}; | ||
| 549 | my $l = $$cell{lpadding}; | ||
| 550 | |||
| 551 | if (($$cell{colspan} || 1) == 1) { | ||
| 552 | $colwidths[$j] = max($colwidths[$j], $w->valueOf) if $w; | ||
| 553 | $collefts[$j] = max($collefts[$j], $l->valueOf) if $l; | ||
| 554 | $colrights[$j] = max($colrights[$j], $r->valueOf) if $r; } | ||
| 555 | if (($$cell{rowspan} || 1) == 1) { | ||
| 556 | $rowh = max($rowh, $h->valueOf) if $h; | ||
| 557 | $rowd = max($rowd, $d->valueOf) if $d; | ||
| 558 | $rowt = max($rowt, $t->valueOf) if $t; | ||
| 559 | $rowb = max($rowb, $b->valueOf) if $b; } | ||
| 560 | else { } # Ditto spanned rows | ||
| 561 | } | ||
| 562 | $$row{cheight} = Dimension($rowh)->larger($hs); | ||
| 563 | $$row{cdepth} = Dimension($rowd)->larger($ds); | ||
| 564 | $$row{tpadding} = Dimension($rowt); | ||
| 565 | $$row{bpadding} = Dimension($rowb); | ||
| 566 | # NOTE: Should be storing column widths to; individually, as well as per-column! | ||
| 567 | push(@rowheights, $rowh + $rowd); } # somehow our heights are way too short???? | ||
| 568 | ## Now compute the positions | ||
| 569 | my @rowpos = (); | ||
| 570 | my @colpos = (); | ||
| 571 | my $y = 0; | ||
| 572 | # Row & column positions: left,top | ||
| 573 | for (my $i = 0 ; $i < scalar(@rowheights) ; $i++) { | ||
| 574 | my $row = $rows[$i]; | ||
| 575 | $y += $$row{tpadding}->valueOf if $$row{tpadding}; | ||
| 576 | $rowpos[$i] = Dimension($y); | ||
| 577 | $y += $$row{cheight}->valueOf if $$row{cheight}; | ||
| 578 | $y += $$row{cdepth}->valueOf if $$row{cdepth}; | ||
| 579 | $y += $$row{bpadding}->valueOf if $$row{bpadding}; } | ||
| 580 | my $x = 0; | ||
| 581 | for (my $j = 0 ; $j < scalar(@colwidths) ; $j++) { | ||
| 582 | $x += $collefts[$j]; | ||
| 583 | $colpos[$j] = Dimension($x); | ||
| 584 | $x += $colwidths[$j]; | ||
| 585 | $x += $colrights[$j]; } | ||
| 586 | $$self{cwidth} = Dimension($x); | ||
| 587 | $$self{cheight} = Dimension($y); # or account for vertical position of array as a whole? | ||
| 588 | $$self{cdepth} = Dimension(0); | ||
| 589 | @colwidths = map { Dimension($_); } @colwidths; | ||
| 590 | @rowheights = map { Dimension($_); } @rowheights; | ||
| 591 | $$self{columnwidths} = [@colwidths]; | ||
| 592 | $$self{rowheights} = [@rowheights]; | ||
| 593 | |||
| 594 | for (my $i = 0 ; $i < scalar(@rowheights) ; $i++) { | ||
| 595 | my $row = $rows[$i]; | ||
| 596 | my @cols = @{ $$row{columns} }; | ||
| 597 | my $ncols = scalar(@cols); | ||
| 598 | $$row{x} = $colpos[0]; $$row{y} = $rowpos[$i]; | ||
| 599 | $$row{cwidth} = Dimension($x); | ||
| 600 | for (my $j = 0 ; $j < $ncols ; $j++) { | ||
| 601 | my $cell = $cols[$j]; | ||
| 602 | my $colx = $colpos[$j]; | ||
| 603 | my $a = $$cell{align} || 'left'; | ||
| 604 | # Adjust position according to alignment | ||
| 605 | if ($colwidths[$j] && $$cell{cwidth} && ($a ne 'left')) { # If these are defined | ||
| 606 | my $dx = $colwidths[$j]->subtract($$cell{cwidth}); | ||
| 607 | if ($a eq 'center') { $colx = $colx->add($dx->multiply(0.5)); } | ||
| 608 | elsif ($a eq 'right') { $colx = $colx->add($dx); } } | ||
| 609 | $$cell{x} = $colx; | ||
| 610 | $$cell{y} = $rowpos[$i]; | ||
| 611 | Debug("CELL[$i,$j] " . showSize($$cell{cwidth}, $$cell{cheight}, $$cell{cdepth}) | ||
| 612 | . " @ " . ToString($$cell{x}) . "," . ToString($$cell{y}) | ||
| 613 | . " w/ " . join(',', map { $_ . '=' . ToString($$cell{$_}); } | ||
| 614 | (qw(align vattach skipped colspan rowspan)))) | ||
| 615 | if $LaTeXML::DEBUG{halign} && $LaTeXML::DEBUG{size}; | ||
| 616 | } } | ||
| 617 | Debug("ALIGNMENT " . showSize($$self{cwidth}, $$self{cheight}, $$self{cdepth})) | ||
| 618 | if $LaTeXML::DEBUG{halign} && $LaTeXML::DEBUG{size}; | ||
| 619 | return; } | ||
| 620 | |||
| 621 | # Mark any cells that are covered by rowspan or colspan | ||
| 622 | sub normalize_mark_spans { | ||
| 623 | my ($self) = @_; | ||
| 624 | # Examines: rowspan, colspan, pseudorow, empty | ||
| 625 | # Sets: skipped, colspanned, rowspanned | ||
| 626 | my @rows = @{ $$self{rows} }; | ||
| 627 | for (my $i = 0 ; $i < scalar(@rows) ; $i++) { | ||
| 628 | my $row = $rows[$i]; | ||
| 629 | my @row = @{ $$row{columns} }; | ||
| 630 | for (my $j = 0 ; $j < scalar(@row) ; $j++) { | ||
| 631 | my $col = $row[$j]; | ||
| 632 | my ($nc, $nr); | ||
| 633 | # scan the row for spanned columns that also span rows! Move rowspan to leading column | ||
| 634 | if (($nc = $$col{colspan} || 1) > 1) { | ||
| 635 | foreach (my $jj = $j + 1 ; $jj < $j + $nc ; $jj++) { | ||
| 636 | my $ccol = $row[$jj]; | ||
| 637 | $$ccol{skipped} = 1; | ||
| 638 | $$ccol{colspanned} = $j; # note that this column is spanned by column $j | ||
| 639 | if (my $cnr = $$ccol{rowspan}) { # If this spanned column has rowspan | ||
| 640 | $$col{rowspan} = $cnr; } } } # copy rowspan to initial column | ||
| 641 | if (($nr = $$col{rowspan} || 1) > 1) { # If this column spans rows | ||
| 642 | my $nr_orig = $nr; | ||
| 643 | my $ncspan = $$col{colspan} || 1; | ||
| 644 | # Mark all spanned columns in following rows as skipped. | ||
| 645 | my $nrc = $nr; | ||
| 646 | my $ii; | ||
| 647 | for ($ii = $i + 1 ; $nrc ; $ii++) { | ||
| 648 | # Prescan the columns to make sure they're empty! | ||
| 649 | my $rowempty = 1; | ||
| 650 | my $rrow = $rows[$ii]; | ||
| 651 | my $rowpseudo = $$rrow{pseudorow}; | ||
| 652 | $nrc-- unless $rowpseudo; | ||
| 653 | if ($rrow) { | ||
| 654 | for (my $jj = $j ; $jj < $j + $ncspan ; $jj++) { | ||
| 655 | if (my $ccol = $$rrow{columns}[$jj]) { | ||
| 656 | if (!$$ccol{skippable}) { | ||
| 657 | $rowempty = 0; } } } } | ||
| 658 | if (!$nrc) { } | ||
| 659 | elsif (!$rrow || !$rowempty) { | ||
| 660 | # Prescan the columns to make sure they're empty! | ||
| 661 | my $oldnr = $nr; | ||
| 662 | $nr = $$col{rowspan} = $nr - $nrc; | ||
| 663 | $nrc = 0; | ||
| 664 | Info('unexpected', 'rowspan', undef, | ||
| 665 | "Rowspan $oldnr in cell($i,$j) covers non-empty cells; truncating.to $nr"); } | ||
| 666 | elsif ($rrow) { | ||
| 667 | for (my $jj = $j ; $jj < $j + $ncspan ; $jj++) { | ||
| 668 | if (my $ccol = $$rrow{columns}[$jj]) { | ||
| 669 | $$ccol{skipped} = 1; | ||
| 670 | $$ccol{rowspanned} = $i; } } } } # note that this column is spanned by row $i | ||
| 671 | # And, if the last (skipped) columns have a bottom border, copy that to the rowspanned col | ||
| 672 | if (my $rrow = $rows[$ii - 1]) { | ||
| 673 | my $sborder = ''; | ||
| 674 | for (my $jj = $j ; $jj < $j + $nc ; $jj++) { | ||
| 675 | if (my $ccol = $$rrow{columns}[$jj]) { | ||
| 676 | my $border = $$ccol{border} || ''; | ||
| 677 | $border =~ s/[^bB]//g; # mask all but bottom border | ||
| 678 | $sborder = $border unless $sborder; } } | ||
| 679 | $$col{border} .= $sborder if $sborder; } | ||
| 680 | } } } | ||
| 681 | return; } | ||
| 682 | |||
| 683 | # Now scan for and remove empty rows & columns | ||
| 684 | # but copying borders and adjusting rowspan's & colspan's appropriately. | ||
| 685 | sub normalize_prune_rows { | ||
| 686 | my ($self) = @_; | ||
| 687 | # Examines: rowspan,rowspanned, border, pseudorow, empty | ||
| 688 | # Sets: border, rowspan | ||
| 689 | my $preserve = $$self{isMath} || $self->getProperty('preserve_structure'); | ||
| 690 | # First, do rows. | ||
| 691 | my @rows = @{ $$self{rows} }; | ||
| 692 | my @filtered = (); | ||
| 693 | for (my $i = 0 ; $i < scalar(@rows) ; $i++) { | ||
| 694 | my $row = $rows[$i]; | ||
| 695 | my $next = $rows[$i + 1]; | ||
| 696 | # prunable if all cells are empty | ||
| 697 | # OR are only spacing NOT made visible by requiring both top & bottom borders | ||
| 698 | my $prunable = 1; | ||
| 699 | my $check_bracketting; | ||
| 700 | foreach my $c (@{ $$row{columns} }) { # Check if all cells are either empty or space only | ||
| 701 | $check_bracketting = 1 if $$c{skippable} && !$$c{empty}; | ||
| 702 | $prunable = 0 unless $$c{skippable}; } | ||
| 703 | if ($prunable && $check_bracketting # If spaces, make sure not bracketted by borders | ||
| 704 | && scalar(grep { ($$_{border} || '') =~ /t/i } @{ $$row{columns} })) { | ||
| 705 | if ($next) { | ||
| 706 | $prunable = 0 if scalar(grep { ($$_{border} || '') =~ /t/i } @{ $$next{columns} }); } | ||
| 707 | else { | ||
| 708 | $prunable = 0 if scalar(grep { ($$_{border} || '') =~ /b/i } @{ $$row{columns} }); } } | ||
| 709 | if (!$prunable) { | ||
| 710 | push(@filtered, $row); } | ||
| 711 | elsif ($next) { # Remove empty row, but copy top border to NEXT row | ||
| 712 | if ($preserve) { | ||
| 713 | push(@filtered, $row); next; } # don't remove inner rows from math EXCEPT last row!! | ||
| 714 | my $nc = scalar(@{ $$row{columns} }); | ||
| 715 | my ($pruneh, $pruned) = (0, 0); | ||
| 716 | for (my $j = 0 ; $j < $nc ; $j++) { | ||
| 717 | my $col = $$row{columns}[$j]; | ||
| 718 | $pruneh = max($pruneh, $$col{cheight}->valueOf) if $$col{cheight}; | ||
| 719 | $pruned = max($pruned, $$col{cdepth}->valueOf) if $$col{cdepth}; | ||
| 720 | if (!$$row{pseudorow} && defined $$col{rowspanned}) { | ||
| 721 | $rows[$$col{rowspanned}]{columns}[$j]{rowspan}--; } # Decrement rowspan of spanning column | ||
| 722 | my $border = $$col{border} || ''; | ||
| 723 | $border =~ s/[^tTbB]//g; # mask all but top & bottom border | ||
| 724 | $border =~ s/b/t/g; # but convert to top | ||
| 725 | $border =~ s/B/T/g; # but convert to top | ||
| 726 | $$next{columns}[$j]{border} .= $border; } # add to NEXT row | ||
| 727 | # This tpadding should be combined w/any extra rowspacing from \\[dim] ! | ||
| 728 | $$next{tpadding} = Dimension($pruneh + $pruned) if $pruneh + $pruned; # And save padding. | ||
| 729 | Debug("PRUNE ROW $i") if $LaTeXML::DEBUG{alignment_normalize}; | ||
| 730 | } | ||
| 731 | else { # Remove empty last row, but copy top border to bottom of prev. | ||
| 732 | my $prev = $filtered[-1]; | ||
| 733 | my $nc = scalar(@{ $$row{columns} }); | ||
| 734 | my ($pruneh, $pruned) = (0, 0); | ||
| 735 | for (my $j = 0 ; $j < $nc ; $j++) { | ||
| 736 | my $col = $$row{columns}[$j]; | ||
| 737 | $pruneh = max($pruneh, $$col{cheight}->valueOf) if $$col{cheight}; | ||
| 738 | $pruned = max($pruned, $$col{cdepth}->valueOf) if $$col{cdepth}; | ||
| 739 | if (!$$row{pseudorow} && defined $$col{rowspanned}) { | ||
| 740 | $rows[$$col{rowspanned}]{columns}[$j]{rowspan}--; } # Decrement rowspan of spanning column | ||
| 741 | my $border = $$col{border} || ''; | ||
| 742 | $border =~ s/[^tT]//g; # mask all but top border | ||
| 743 | $border =~ s/t/b/g; # convert to bottom | ||
| 744 | $border =~ s/T/B/g; # convert to bottom | ||
| 745 | my $ccol = $$prev{columns}[$j]; | ||
| 746 | if (defined $$ccol{rowspanned}) { # skip to spanning column if rowspanned! | ||
| 747 | $ccol = $rows[$$ccol{rowspanned}]{columns}[$j]; } | ||
| 748 | $$ccol{border} .= $border; } # add to PREVIOUS row. | ||
| 749 | $$prev{bpadding} = Dimension($pruneh + $pruned) if $pruneh + $pruned; # And save padding. | ||
| 750 | Debug("PRUNE ROW (last) $i") if $LaTeXML::DEBUG{alignment_normalize}; } | ||
| 751 | } | ||
| 752 | @rows = @filtered; | ||
| 753 | $$self{rows} = [@filtered]; | ||
| 754 | return; } | ||
| 755 | |||
| 756 | sub normalize_prune_columns { | ||
| 757 | my ($self) = @_; | ||
| 758 | my $preserve = $$self{isMath} || $self->getProperty('preserve_structure'); | ||
| 759 | # Now prune empty columns. | ||
| 760 | if (!$preserve) { # Don't remove empty columns from math. | ||
| 761 | my @rows = @{ $$self{rows} }; | ||
| 762 | my $nc = 0; | ||
| 763 | foreach my $row (@rows) { | ||
| 764 | my $n = scalar(@{ $$row{columns} }); | ||
| 765 | $nc = $n if $n > $nc; } | ||
| 766 | for (my $j = $nc - 1 ; $j >= 0 ; $j--) { # Prune from RIGHT! | ||
| 767 | if (!grep { (defined $$_{columns}[$j]) && !$$_{columns}[$j]{empty} } @rows) { # Empty! | ||
| 768 | my $prunew = 0; | ||
| 769 | foreach my $row (@rows) { | ||
| 770 | if (my $col = $$row{columns}[$j]) { | ||
| 771 | if (defined $$col{colspanned}) { | ||
| 772 | $$row{columns}[$$col{colspanned}]{colspan}--; } # Decrement colspan of spanning column | ||
| 773 | $prunew = max($prunew, $$col{cwidth}) if $$col{cwidth}; | ||
| 774 | my $border = $$col{border} || ''; | ||
| 775 | if ($j > 0) { | ||
| 776 | my $prev = $$row{columns}[$j - 1]; | ||
| 777 | if (my $jj = $$prev{colspanned}) { | ||
| 778 | $prev = $$row{columns}[$jj]; } | ||
| 779 | $border =~ s/[^rRlL]//g; # mask all but left border | ||
| 780 | $border =~ s/l/r/g; # convert to right | ||
| 781 | $border =~ s/L/R/g; # convert to right | ||
| 782 | $$prev{border} .= $border; | ||
| 783 | # Copy left spacing to right column, as well | ||
| 784 | $$prev{rspaces} = LaTeXML::Core::List::List($$prev{rspaces} || (), $$col{lspaces} || ()) if $$col{lspaces}; | ||
| 785 | $$prev{rpadding} = $$prev{rspaces}->getWidth if $$prev{rspaces}; | ||
| 786 | if (my @preserve = preservedBoxes($$col{boxes})) { # Copy boxes over, in case side effects? | ||
| 787 | $$prev{boxes} = LaTeXML::Core::List($$prev{boxes} | ||
| 788 | ? ($$prev{boxes}->unlist, @preserve) : @preserve); } | ||
| 789 | } | ||
| 790 | elsif (my $next = $$row{columns}[1]) { | ||
| 791 | $border =~ s/[^rRlL]//g; # mask all but left & right border | ||
| 792 | $border =~ s/r/l/g; # but convert to left | ||
| 793 | $border =~ s/R/L/g; # but convert to left | ||
| 794 | $$next{border} .= $border; # add to next row | ||
| 795 | if (my @preserve = preservedBoxes($$col{boxes})) { # Copy boxes over, in case side effects? | ||
| 796 | $$next{boxes} = LaTeXML::Core::List($$col{boxes} | ||
| 797 | ? (@preserve, $$next{boxes}->unlist) : @preserve); } | ||
| 798 | } # Now, remove the column | ||
| 799 | $$row{columns} = [grep { $_ ne $col } @{ $$row{columns} }]; | ||
| 800 | } } | ||
| 801 | $prunew = Dimension($prunew); | ||
| 802 | if ($j) { # If not 1st row, add right padding to previous column | ||
| 803 | foreach my $row (@rows) { | ||
| 804 | if (my $col = $$row{columns}[$j - 1]) { | ||
| 805 | $$col{rpadding} = ($$col{rpadding} ? $$col{rpadding}->add($prunew) : $prunew); } } } | ||
| 806 | else { # Else add left padding to (newly) first column | ||
| 807 | foreach my $row (@rows) { # And add the padding to previous column | ||
| 808 | if (my $col = $$row{columns}[0]) { | ||
| 809 | $$col{lpadding} = ($$col{lpadding} ? $$col{lpadding}->add($prunew) : $prunew); } } } | ||
| 810 | Debug("PRUNE COLUMN $j") if $LaTeXML::DEBUG{alignment_normalize}; | ||
| 811 | } } } | ||
| 812 | return; } | ||
| 813 | |||
| 814 | sub show_row { | ||
| 815 | my ($i, $row) = @_; | ||
| 816 | Debug("\nRow[$i]:" . join(', ', map { $_ . '=' . ToString($$row{$_}); } | ||
| 817 | grep { $_ ne 'columns'; } sort keys %$row)); | ||
| 818 | my @c = @{ $$row{columns} }; | ||
| 819 | for (my $j = 1 ; @c ; $j++) { | ||
| 820 | show_col($i, $j, shift(@c)); } | ||
| 821 | return; } | ||
| 822 | |||
| 823 | sub show_col { | ||
| 824 | my ($i, $j, $col) = @_; | ||
| 825 | Debug("Column[$i,$j]:" . join(', ', map { $_ . '=' . ToString($$col{$_}); } sort keys %$col)); | ||
| 826 | return; } | ||
| 827 | |||
| 828 | sub preservedBoxes { | ||
| 829 | my ($boxes) = @_; | ||
| 830 | return ($boxes ? grep { $_->getProperty('alignmentPreserve') } $boxes->unlist : ()); } | ||
| 831 | #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
| 832 | # Dealing with templates | ||
| 833 | |||
| 834 | # newcolumntype | ||
| 835 | # defines \NC@rewrite@<char> | ||
| 836 | # As macro | ||
| 837 | # or "constructor" (or just sub that creates a column) | ||
| 838 | |||
| 839 | sub ReadAlignmentTemplate { | ||
| 840 | my ($gullet) = @_; | ||
| 841 | $gullet->skipSpaces; | ||
| 842 | local $LaTeXML::BUILD_TEMPLATE = | ||
| 843 | LaTeXML::Core::Alignment::Template->new(columns => [], tokens => []); | ||
| 844 | my @tokens = (T_BEGIN); | ||
| 845 | my $nopens = 0; | ||
| 846 | while (my $open = $gullet->readToken) { | ||
| 847 | if ($open->getCatcode == CC_BEGIN) { $nopens++; } | ||
| 848 | else { $gullet->unread($open); last; } } | ||
| 849 | my $defn; | ||
| 850 | while (my $op = $gullet->readToken) { | ||
| 851 | my $cc = $op->getCatcode; | ||
| 852 | if ($cc == CC_SPACE) { } | ||
| 853 | elsif ($cc == CC_END) { | ||
| 854 | while (--$nopens && (($op = $gullet->readToken)->getCatcode == CC_END)) { } | ||
| 855 | last unless $nopens; | ||
| 856 | $gullet->unread($op); } | ||
| 857 | elsif (defined($defn = $STATE->lookupDefinition(T_CS('\NC@rewrite@' . ToString($op)))) | ||
| 858 | && $defn->isExpandable) { | ||
| 859 | $gullet->unread($defn->invoke($gullet, 1)); } | ||
| 860 | elsif ($cc == CC_BEGIN) { # Wrong, but a safety valve | ||
| 861 | $gullet->unread($gullet->readBalanced); } | ||
| 862 | else { | ||
| 863 | Warn('unexpected', $op, $gullet, "Unrecognized tabular template '" . Stringify($op) . "'"); } | ||
| 864 | last unless $nopens; } | ||
| 865 | push(@tokens, T_END); | ||
| 866 | $LaTeXML::BUILD_TEMPLATE->setReversion(@tokens); | ||
| 867 | $LaTeXML::BUILD_TEMPLATE->finish; | ||
| 868 | return $LaTeXML::BUILD_TEMPLATE; } | ||
| 869 | |||
| 870 | sub parseAlignmentTemplate { | ||
| 871 | my ($spec) = @_; | ||
| 872 | return $STATE->getStomach->getGullet->readingFromMouth(LaTeXML::Core::Mouth->new("{" . $spec . "}"), sub { | ||
| 873 | ReadAlignmentTemplate($_[0]); }); } | ||
| 874 | |||
| 875 | sub MatrixTemplate { | ||
| 876 | return LaTeXML::Core::Alignment::Template->new(repeated => [{ before => Tokens(T_CS('\hfil')), | ||
| 877 | after => Tokens(T_CS('\hfil')) }]); } | ||
| 878 | |||
| 879 | #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
| 880 | # Experimental alignment heading heuristications. | ||
| 881 | #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
| 882 | # We attempt to recognize patterns of rows/columns that indicate which might be headers. | ||
| 883 | # We'll characterize the cells by alignment, content and borders. | ||
| 884 | # Then, assuming that headers will be first and be noticably `different' from data lines, | ||
| 885 | # and also that the data lines will have similar structure, we'll attempt to | ||
| 886 | # recognize groups of header lines and groups data lines, possibly alternating. | ||
| 887 | |||
| 888 | sub guess_alignment_headers { | ||
| 889 | my ($document, $table, $alignment) = @_; | ||
| 890 | # Assume that headers don't make sense for nested tables. | ||
| 891 | # OR Maybe we should only do this within table environments??? | ||
| 892 | return if !$table || $document->findnodes("ancestor::ltx:tabular", $table); | ||
| 893 | |||
| 894 | my $tag = $document->getModel->getNodeQName($table); | ||
| 895 | my $x; | ||
| 896 | Debug(('=' x 50) . "\nGuessing alignment headers for " | ||
| 897 | . (($x = $document->findnode('ancestor-or-self::*[@xml:id]', $table)) ? $x->getAttribute('xml:id') : $tag)) | ||
| 898 | if $LaTeXML::DEBUG{alignment}; | ||
| 899 | |||
| 900 | my $ismath = $tag eq 'ltx:XMArray'; | ||
| 901 | local $LaTeXML::TR = ($ismath ? 'ltx:XMRow' : 'ltx:tr'); | ||
| 902 | local $LaTeXML::TD = ($ismath ? 'ltx:XMCell' : 'ltx:td'); | ||
| 903 | my $reversed = 0; | ||
| 904 | # Build a view of the table by extracting the rows, collecting & characterizing each cell. | ||
| 905 | my @rows = collect_alignment_rows($document, $table, $alignment); | ||
| 906 | # Flip the rows around to produce a column view. | ||
| 907 | my @cols = (); | ||
| 908 | return unless @rows; | ||
| 909 | for (my $c = 0 ; $c < scalar(@{ $rows[0] }) ; $c++) { | ||
| 910 | push(@cols, [map { $$_[$c] } @rows]); } | ||
| 911 | |||
| 912 | # Attempt to recognize header lines. | ||
| 913 | if (alignment_characterize_lines($document, 0, 0, @rows)) { } | ||
| 914 | # This usually does something unpleasant | ||
| 915 | ## else { | ||
| 916 | ## Debug("Retry characterizing lines in reverse") if $LaTeXML::DEBUG{alignment}; | ||
| 917 | ## $reversed=alignment_characterize_lines(0,1,reverse(@rows)); } | ||
| 918 | alignment_characterize_lines($document, 1, 0, @cols); | ||
| 919 | # Did we go overboard? | ||
| 920 | my %n = (h => 0, d => 0); | ||
| 921 | foreach my $r (@rows) { | ||
| 922 | foreach my $c (@$r) { | ||
| 923 | $n{ $$c{cell_type} }++; } } | ||
| 924 | Debug("$n{h} header, $n{d} data cells") if $LaTeXML::DEBUG{alignment}; | ||
| 925 | if ($n{d} == 1) { # Or any other heuristic? | ||
| 926 | $n{h} = 0; | ||
| 927 | foreach my $r (@rows) { | ||
| 928 | foreach my $c (@$r) { | ||
| 929 | $$c{cell_type} = 'd'; | ||
| 930 | $$c{cell}->removeAttribute('thead') if $$c{cell}; } } } | ||
| 931 | # Regroup the rows into thead & tbody elements. | ||
| 932 | # But not if it's a math array, or if reversed (since browsers get confused?) | ||
| 933 | if (!$ismath && !$reversed) { | ||
| 934 | alignment_regroup_rows($document, $table); } | ||
| 935 | if ($n{h}) { # Found some headers? | ||
| 936 | $document->addClass($table, 'ltx_guessed_headers'); } | ||
| 937 | |||
| 938 | # Debugging report! | ||
| 939 | summarize_alignment([@rows], [@cols]) if $LaTeXML::DEBUG{alignment}; | ||
| 940 | return; } | ||
| 941 | |||
| 942 | #====================================================================== | ||
| 943 | # Regroup the rows into thead, tbody & tfoot | ||
| 944 | # Any leading rows, all of whose cells have attribute thead should be in thead. | ||
| 945 | # UNLESS any of them have a rowspan that extends PAST the end of the thead!!!! | ||
| 946 | # trailing rows marked as thead go into tfoot. | ||
| 947 | sub alignment_regroup_rows { | ||
| 948 | my ($document, $table) = @_; | ||
| 949 | my @rows = $document->findnodes("ltx:tr", $table); | ||
| 950 | my @heads = (); | ||
| 951 | my $maxreach = 0; | ||
| 952 | # Scan initial rows as potential thead | ||
| 953 | while (@rows) { | ||
| 954 | my @cells = $document->findnodes('ltx:td', $rows[0]); | ||
| 955 | # Non header cells, done. | ||
| 956 | last if scalar(grep { (!$_->getAttribute('thead')) } @cells); | ||
| 957 | my $line = scalar(@heads); | ||
| 958 | push(@heads, shift(@rows)); | ||
| 959 | $maxreach = max($maxreach, map { ($_->getAttribute('rowspan') || 0) + $line } @cells); } | ||
| 960 | if ($maxreach > scalar(@heads)) { # rowspan crossed over thead boundary! | ||
| 961 | unshift(@rows, @heads); @heads = (); } | ||
| 962 | # scan trailing rows as potential tfoot | ||
| 963 | my @foots = (); | ||
| 964 | while (@rows) { | ||
| 965 | my @cells = $document->findnodes('ltx:td', $rows[-1]); | ||
| 966 | # Non header cells, done. | ||
| 967 | last if scalar(grep { (!$_->getAttribute('thead')) } @cells); | ||
| 968 | unshift(@foots, pop(@rows)); } | ||
| 969 | $document->wrapNodes('ltx:thead', @heads) if @heads; | ||
| 970 | $document->wrapNodes('ltx:tbody', @rows) if @rows; | ||
| 971 | $document->wrapNodes('ltx:tfoot', @foots) if @foots; | ||
| 972 | return; } | ||
| 973 | |||
| 974 | #====================================================================== | ||
| 975 | # Build a View of the alignment, with characterized cells, for analysis. | ||
| 976 | my %ALIGNMENT_CODE = ( # CONSTANT | ||
| 977 | right => 'r', left => 'l', center => 'c', justify => 'p'); | ||
| 978 | |||
| 979 | sub collect_alignment_rows { | ||
| 980 | my ($document, $table, $alignment) = @_; | ||
| 981 | my @arows = @{ $$alignment{rows} }; | ||
| 982 | my $nrows = scalar(@arows); | ||
| 983 | my $ncols = 0; | ||
| 984 | foreach my $arow (@arows) { | ||
| 985 | my $n = scalar(@{ $$arow{columns} }); | ||
| 986 | $ncols = $n if $n > $ncols; } | ||
| 987 | my @rows = (); | ||
| 988 | my ($h, $v) = (0, 0); | ||
| 989 | foreach my $arow (@arows) { | ||
| 990 | push(@rows, []); | ||
| 991 | my @cols = @{ $$arow{columns} }; | ||
| 992 | foreach my $col (@cols) { | ||
| 993 | push(@{ $rows[-1] }, $col); | ||
| 994 | $$col{cell_type} = 'd'; | ||
| 995 | $$col{content_class} = (($$col{align} || '') eq 'justify' ? 'mx' # Assume mixed content for any justified cell??? | ||
| 996 | : ($$col{cell} ? classify_alignment_cell($document, $$col{cell}) : '?')); | ||
| 997 | $$col{content_length} = ($$col{content_class} eq 'g' ? 1000 | ||
| 998 | : ($$col{cell} ? length($$col{cell}->textContent) : 0)); | ||
| 999 | my %border = (t => 0, r => 0, b => 0, l => 0); # Decode border | ||
| 1000 | map { $border{$_}++ } split(/ */, $$col{border} || ''); | ||
| 1001 | $h = 1 if $border{t} || $border{b}; | ||
| 1002 | $v = 1 if $border{r} || $border{l}; | ||
| 1003 | map { $$col{$_} = $border{$_} } keys %border; } | ||
| 1004 | # pad the columns out. | ||
| 1005 | for (my $c = scalar(@cols) ; $c < $ncols ; $c++) { | ||
| 1006 | my $col = {}; | ||
| 1007 | push(@{ $rows[-1] }, $col); | ||
| 1008 | $$col{align} = 'c'; | ||
| 1009 | $$col{cell_type} = 'd'; | ||
| 1010 | $$col{content_class} = '_'; | ||
| 1011 | $$col{content_length} = 0; | ||
| 1012 | map { $$col{$_} = 0 } qw(t r b l); } | ||
| 1013 | } | ||
| 1014 | # copy the characterizations to spanned cells | ||
| 1015 | for (my $r = 0 ; $r < $nrows ; $r++) { | ||
| 1016 | for (my $c = 0 ; $c < $ncols ; $c++) { | ||
| 1017 | my $rs = $rows[$r][$c]{rowspan} || 1; | ||
| 1018 | my $cs = $rows[$r][$c]{colspan} || 1; | ||
| 1019 | my $ca = $rows[$r][$c]{align}; | ||
| 1020 | my $cc = $rows[$r][$c]{content_class}; | ||
| 1021 | my $cl = $rows[$r][$c]{content_length}; | ||
| 1022 | my $rb = $rows[$r][$c]{r}; $rows[$r][$c]{r} = 0; | ||
| 1023 | my $bb = $rows[$r][$c]{b}; $rows[$r][$c]{b} = 0; | ||
| 1024 | for (my $sc = 1 ; $sc < $cs ; $sc++) { | ||
| 1025 | $rows[$r][$c + $sc]{align} = $ca; | ||
| 1026 | $rows[$r][$c + $sc]{content_class} = $cc; | ||
| 1027 | $rows[$r][$c + $sc]{content_length} = $cl; } | ||
| 1028 | for (my $sr = 1 ; $sr < $rs ; $sr++) { | ||
| 1029 | for (my $sc = 0 ; $sc < $cs ; $sc++) { | ||
| 1030 | $rows[$r + $sr][$c + $sc]{align} = $ca; | ||
| 1031 | $rows[$r + $sr][$c + $sc]{content_class} = $cc; | ||
| 1032 | $rows[$r + $sr][$c + $sc]{content_length} = $cl; } } | ||
| 1033 | # move the outer borders | ||
| 1034 | for (my $sr = 0 ; $sr < $rs ; $sr++) { | ||
| 1035 | $rows[$r + $sr][$c + $cs - 1]{r} = $rb; } | ||
| 1036 | for (my $sc = 0 ; $sc < $cs ; $sc++) { | ||
| 1037 | $rows[$r + $rs - 1][$c + $sc]{b} = $bb; } | ||
| 1038 | } } | ||
| 1039 | |||
| 1040 | # Now, do some border massaging... | ||
| 1041 | for (my $r = 0 ; $r < $nrows ; $r++) { | ||
| 1042 | $rows[$r][0]{l} = $v; | ||
| 1043 | $rows[$r][0]{r} = $rows[$r][1]{l} if ($ncols > 1) && $rows[$r][1]{l}; | ||
| 1044 | $rows[$r][$ncols - 1]{l} = $rows[$r][$ncols - 2]{r} if ($ncols > 1) && $rows[$r][$ncols - 2]{r}; | ||
| 1045 | $rows[$r][$ncols - 1]{r} = $v; } | ||
| 1046 | for (my $c = 0 ; $c < $ncols ; $c++) { | ||
| 1047 | $rows[0][$c]{t} = $h; | ||
| 1048 | $rows[0][$c]{b} = $rows[1][$c]{t} if ($nrows > 1) && $rows[1][$c]{t}; | ||
| 1049 | $rows[$nrows - 1][$c]{t} = $rows[$nrows - 2][$c]{b} if ($nrows > 1) && $rows[$nrows - 2][$c]{b}; | ||
| 1050 | $rows[$nrows - 1][$c]{b} = $h; } | ||
| 1051 | for (my $r = 1 ; $r < $nrows - 1 ; $r++) { | ||
| 1052 | for (my $c = 1 ; $c < $ncols - 1 ; $c++) { | ||
| 1053 | $rows[$r][$c]{t} = $rows[$r - 1][$c]{b} if $rows[$r - 1][$c]{b}; | ||
| 1054 | $rows[$r][$c]{b} = $rows[$r + 1][$c]{t} if $rows[$r + 1][$c]{t}; | ||
| 1055 | $rows[$r][$c]{l} = $rows[$r][$c - 1]{r} if $rows[$r][$c - 1]{r}; | ||
| 1056 | $rows[$r][$c]{r} = $rows[$r][$c + 1]{l} if $rows[$r][$c + 1]{l}; } } | ||
| 1057 | if ($LaTeXML::DEBUG{alignment}) { | ||
| 1058 | Debug("Cell characterizations:"); | ||
| 1059 | for (my $r = 0 ; $r < $nrows ; $r++) { | ||
| 1060 | for (my $c = 0 ; $c < $ncols ; $c++) { | ||
| 1061 | my $col = $rows[$r][$c]; | ||
| 1062 | Debug("[$r,$c]=>" . ($$col{cell_type} || '?') | ||
| 1063 | . ($$col{align} ? $ALIGNMENT_CODE{ $$col{align} } : ' ') | ||
| 1064 | . ($$col{content_class} || '?') | ||
| 1065 | . ' ' . $$col{content_length} | ||
| 1066 | . ' ' . $$col{border} . "=>" . join('', grep { $$col{$_} } qw(t r b l)) | ||
| 1067 | . (($$col{rowspan} || 1) > 1 ? " rowspan=" . $$col{rowspan} : '') | ||
| 1068 | . (($$col{colspan} || 1) > 1 ? " colspan=" . $$col{colspan} : '')); } } } | ||
| 1069 | return @rows; } | ||
| 1070 | |||
| 1071 | # Return one of: i(nteger), t(ext), m(ath), ? (unknown) or '_' (empty) (or some combination) | ||
| 1072 | # or 'mx' for alternating text & math. | ||
| 1073 | sub classify_alignment_cell { | ||
| 1074 | my ($document, $xcell) = @_; | ||
| 1075 | my $content = $xcell->textContent; | ||
| 1076 | my $class = ''; | ||
| 1077 | # if($content =~ /^\s*\d+\s*$/){ | ||
| 1078 | if ($content =~ /^[\s\d]+$/) { | ||
| 1079 | $class = 'i'; } | ||
| 1080 | else { | ||
| 1081 | my @nodes = $xcell->childNodes; | ||
| 1082 | while (@nodes) { | ||
| 1083 | my $ch = shift(@nodes); | ||
| 1084 | my $chtype = $ch->nodeType; | ||
| 1085 | if ($chtype == XML_TEXT_NODE) { | ||
| 1086 | my $text = $ch->textContent; | ||
| 1087 | $class .= 't' | ||
| 1088 | unless $text =~ /^\s*$/ || (($class eq 'm') && ($text =~ /^\s*[\.,;]\s*$/)); } | ||
| 1089 | elsif ($chtype == XML_ELEMENT_NODE) { | ||
| 1090 | my $chtag = $document->getModel->getNodeQName($ch); | ||
| 1091 | if ($chtag eq 'ltx:text') { # Font would be useful, but haven't "resolved" it, yet! | ||
| 1092 | $class .= 't' unless $class eq 't'; } | ||
| 1093 | elsif ($chtag eq 'ltx:graphics') { | ||
| 1094 | $class .= 'g' unless $class eq 'g'; } | ||
| 1095 | elsif ($chtag eq 'ltx:Math') { | ||
| 1096 | $class .= 'm' unless $class eq 'm'; } | ||
| 1097 | elsif ($chtag eq 'ltx:XMText') { | ||
| 1098 | $class .= 't' unless $class eq 't'; } | ||
| 1099 | elsif ($chtag eq 'ltx:XMArg') { | ||
| 1100 | unshift(@nodes, $ch->childNodes); } | ||
| 1101 | elsif ($chtag =~ /^ltx:XM/) { | ||
| 1102 | $class .= 'm' unless $class eq 'm'; } | ||
| 1103 | else { | ||
| 1104 | $class .= '?' unless $class; } | ||
| 1105 | } } } | ||
| 1106 | $class = 'mx' if $class && (($class =~ /^((m|i)t)+(m|i)?$/) || ($class =~ /^(t(m|i))+t?$/)); | ||
| 1107 | return $class || '_'; } | ||
| 1108 | |||
| 1109 | #====================================================================== | ||
| 1110 | # Scan pairs of rows/columns attempting to recognize differences that | ||
| 1111 | # might indicate which are headers and which are data. | ||
| 1112 | # Warning: This section is full of "magic numbers" | ||
| 1113 | # guessed by sampling various test cases. | ||
| 1114 | |||
| 1115 | my $MIN_ALIGNMENT_DATA_LINES = 1; # (or 2?) [CONSTANT] | ||
| 1116 | my $MAX_ALIGNMENT_HEADER_LINES = 4; # [CONSTANT] | ||
| 1117 | |||
| 1118 | # We expect to find header lines at the beginning, noticably different from the eventual data lines. | ||
| 1119 | # Both header lines and data lines can consist of several neighboring lines. | ||
| 1120 | # Check that header lines are `similar' to each other. So, the strategy is to look | ||
| 1121 | # for a `hump' in the line differences and consider blocks containing these lines to be potential headers. | ||
| 1122 | my @axisname = ('column', 'row'); | ||
| 1123 | |||
| 1124 | sub alignment_characterize_lines { | ||
| 1125 | my ($document, $axis, $reversed, @lines) = @_; | ||
| 1126 | my $n = scalar(@lines); | ||
| 1127 | return if $n < 2; | ||
| 1128 | local @::TABLINES = @lines; | ||
| 1129 | Debug("Characterizing $n " . ($axis ? "columns" : "rows")) | ||
| 1130 | if $LaTeXML::DEBUG{alignment}; | ||
| 1131 | |||
| 1132 | # Establish a scale of differences for the table. | ||
| 1133 | my ($diffhi, $difflo, $diffavg) = (0, 99999999, 0); | ||
| 1134 | for (my $l = 0 ; $l < $n - 1 ; $l++) { | ||
| 1135 | my $d = alignment_compare($axis, 1, $reversed, $l, $l + 1); | ||
| 1136 | $diffavg += $d; | ||
| 1137 | $diffhi = $d if $d > $diffhi; | ||
| 1138 | $difflo = $d if $d < $difflo; } | ||
| 1139 | $diffavg = $diffavg / ($n - 1); | ||
| 1140 | if ($diffhi < 0.05) { # virtually no differences. | ||
| 1141 | Debug("Lines are almost identical => Fail") if $LaTeXML::DEBUG{alignment}; | ||
| 1142 | return; } | ||
| 1143 | if (($n > 2) && (($diffhi - $difflo) < $diffhi * 0.5)) { # differences too similar to establish pattern | ||
| 1144 | Debug("Differences between lines are almost identical => Fail") | ||
| 1145 | if $LaTeXML::DEBUG{alignment}; | ||
| 1146 | return; } | ||
| 1147 | # local $::TAB_THRESHOLD = $difflo + 0.4*($diffhi-$difflo); | ||
| 1148 | local $::TAB_THRESHOLD = $difflo + 0.3 * ($diffhi - $difflo); | ||
| 1149 | # local $::TAB_THRESHOLD = $difflo + 0.2*($diffhi-$difflo); | ||
| 1150 | # local $::TAB_THRESHOLD = $diffavg; | ||
| 1151 | local $::TAB_AXIS = $axis; | ||
| 1152 | Debug("Differences $difflo -- $diffhi => threshold = $::TAB_THRESHOLD") | ||
| 1153 | if $LaTeXML::DEBUG{alignment}; | ||
| 1154 | # Find the first hump in differences. These are candidates for header lines. | ||
| 1155 | Debug("Scanning for headers") if $LaTeXML::DEBUG{alignment}; | ||
| 1156 | my $diff; | ||
| 1157 | my ($minh, $maxh) = (1, 1); | ||
| 1158 | while (($diff = alignment_compare($axis, 1, $reversed, $maxh - 1, $maxh)) < $::TAB_THRESHOLD) { | ||
| 1159 | $maxh++; } | ||
| 1160 | return if $maxh > $MAX_ALIGNMENT_HEADER_LINES; # too many before even finding diffs? give up! | ||
| 1161 | # while( alignment_compare($axis,1,$reversed,$maxh,$maxh+1) > $difflo + ($diff-$difflo)/6){ | ||
| 1162 | while (alignment_compare($axis, 1, $reversed, $maxh, $maxh + 1) > $::TAB_THRESHOLD) { | ||
| 1163 | $maxh++; } | ||
| 1164 | $maxh = $MAX_ALIGNMENT_HEADER_LINES if $maxh > $MAX_ALIGNMENT_HEADER_LINES; | ||
| 1165 | Debug("Found from $minh--$maxh potential headers") if $LaTeXML::DEBUG{alignment}; | ||
| 1166 | |||
| 1167 | my $nn = scalar(@{ $lines[0] }) - 1; | ||
| 1168 | # The sets of lines 1--$minh, .. 1--$maxh are potential headers. | ||
| 1169 | for (my $nh = $maxh ; $nh >= $minh ; $nh--) { | ||
| 1170 | # for(my $nh = $minh; $nh <= $maxh; $nh++){ | ||
| 1171 | # Check whether the set 1..$nh is plausable. | ||
| 1172 | if (my @heads = alignment_test_headers($nh)) { | ||
| 1173 | # Now, change all cells marked as header from td => th. | ||
| 1174 | foreach my $h (@heads) { | ||
| 1175 | my $i = 0; | ||
| 1176 | foreach my $cell (@{ $lines[$h] }) { | ||
| 1177 | $$cell{cell_type} = 'h'; | ||
| 1178 | if (my $xcell = $$cell{cell}) { | ||
| 1179 | if (($$cell{content_class} eq '_') # But NOT empty cells on outer edges. | ||
| 1180 | && ((($i == 0) && !$$cell{ ($axis == 0 ? 'l' : 't') }) | ||
| 1181 | || (($i == $nn) && !$$cell{ ($axis == 0 ? 'r' : 'b') }))) { } | ||
| 1182 | else { | ||
| 1183 | $document->addSSValues($$cell{cell}, thead => $axisname[$axis]); } } | ||
| 1184 | $i++; } } | ||
| 1185 | return 1; } } | ||
| 1186 | return; } | ||
| 1187 | |||
| 1188 | # Test whether $nhead lines makes a good fit for the headers | ||
| 1189 | sub alignment_test_headers { | ||
| 1190 | my ($nhead) = @_; | ||
| 1191 | Debug("Testing $nhead headers") if $LaTeXML::DEBUG{alignment}; | ||
| 1192 | my ($headlength, $datalength) = (0, 0); | ||
| 1193 | my @heads = (0 .. $nhead - 1); # The indices of heading lines. | ||
| 1194 | $headlength = alignment_max_content_length($headlength, 0, $nhead - 1); | ||
| 1195 | my $nextline = $nhead; # Start from the end of the proposed headings. | ||
| 1196 | |||
| 1197 | # Watch out for the assumed header being really data that is a repeated pattern. | ||
| 1198 | my $nrep = scalar(@::TABLINES) / $nhead; | ||
| 1199 | if (($nhead > 1) && ($nrep == int($nrep))) { | ||
| 1200 | Debug("Check for apparent header repeated $nrep times") if $LaTeXML::DEBUG{alignment}; | ||
| 1201 | my $matched = 1; | ||
| 1202 | for (my $r = 1 ; $r < $nrep ; $r++) { | ||
| 1203 | $matched &&= alignment_match_head(0, $r * $nhead, $nhead); } | ||
| 1204 | Debug("Repeated headers: " . ($matched ? "Matched=> Fail" : "Nomatch => Succeed")) | ||
| 1205 | if $LaTeXML::DEBUG{alignment}; | ||
| 1206 | return if $matched; } | ||
| 1207 | |||
| 1208 | # And find a following grouping of data lines. | ||
| 1209 | my $ndata = alignment_skip_data($nextline); | ||
| 1210 | return if $ndata < $nhead; # ???? Well, maybe if _really_ convincing??? | ||
| 1211 | return if ($ndata < $nhead) && ($ndata < 2); | ||
| 1212 | # Check that the content of the headers isn't dramatically larger than the content in the data | ||
| 1213 | $datalength = alignment_max_content_length($datalength, $nextline, $nextline + $ndata - 1); | ||
| 1214 | $nextline += $ndata; | ||
| 1215 | |||
| 1216 | my $nd; | ||
| 1217 | # If there are more lines, they should match either the previous data block, or the head/data pattern. | ||
| 1218 | while ($nextline < scalar(@::TABLINES)) { | ||
| 1219 | # First try to match a repeat of the 1st data block; | ||
| 1220 | # This would be the case when groups of data have borders around them. | ||
| 1221 | # Could want to match a variable number of datalines, but they should be similar!!!??!?!? | ||
| 1222 | if (($ndata > 1) && ($nd = alignment_match_data($nhead, $nextline, $ndata))) { | ||
| 1223 | $datalength = alignment_max_content_length($datalength, $nextline, $nextline + $nd - 1); | ||
| 1224 | $nextline += $nd; } | ||
| 1225 | # Else, try to match the first header block; less common. | ||
| 1226 | elsif (alignment_match_head(0, $nextline, $nhead)) { | ||
| 1227 | push(@heads, $nextline .. $nextline + $nhead - 1); | ||
| 1228 | $headlength = alignment_max_content_length($headlength, $nextline, $nextline + $nhead - 1); | ||
| 1229 | $nextline += $nhead; | ||
| 1230 | # Then attempt to match a new data block. | ||
| 1231 | # my $d = alignment_skip_data($nextline); | ||
| 1232 | # return unless ($d >= $nhead) || ($d >= 2); | ||
| 1233 | # $nextline += $d; } | ||
| 1234 | # No, better be the same data block? | ||
| 1235 | return unless ($nd = alignment_match_data($nhead, $nextline, $ndata)); | ||
| 1236 | $datalength = alignment_max_content_length($datalength, $nextline, $nextline + $nd - 1); | ||
| 1237 | $nextline += $nd; } | ||
| 1238 | else { return; } } | ||
| 1239 | # Header content seems too large relative to data? | ||
| 1240 | Debug("header content = $headlength; data content = $datalength") | ||
| 1241 | if $LaTeXML::DEBUG{alignment}; | ||
| 1242 | ## if(($headlength > 10) && (0.3*$headlength > $datalength)){ | ||
| 1243 | if (($headlength > 10) && (0.25 * $headlength > $datalength)) { | ||
| 1244 | Debug("header content too much longer than data content") | ||
| 1245 | if $LaTeXML::DEBUG{alignment}; | ||
| 1246 | return; } | ||
| 1247 | # Or if a header cell has "large" content? | ||
| 1248 | if ($headlength >= 1000) { # Or if a header cell has "large" content? | ||
| 1249 | Debug("header content too large") | ||
| 1250 | if $LaTeXML::DEBUG{alignment}; | ||
| 1251 | return; } | ||
| 1252 | |||
| 1253 | Debug("Succeeded with $nhead headers") if $LaTeXML::DEBUG{alignment}; | ||
| 1254 | return @heads; } | ||
| 1255 | |||
| 1256 | sub alignment_match_head { | ||
| 1257 | my ($p1, $p2, $nhead) = @_; | ||
| 1258 | Debug("Try match $nhead header lines from $p1 to $p2") if $LaTeXML::DEBUG{alignment}; | ||
| 1259 | my $nh = alignment_match_lines($p1, $p2, $nhead); | ||
| 1260 | my $ok = $nhead == $nh; | ||
| 1261 | Debug("Matched $nh header lines => " . ($ok ? "Succeed" : "Failed")) if $LaTeXML::DEBUG{alignment}; | ||
| 1262 | return ($ok ? $nhead : 0); } | ||
| 1263 | |||
| 1264 | sub alignment_match_data { | ||
| 1265 | my ($p1, $p2, $ndata) = @_; | ||
| 1266 | Debug("Try match $ndata data lines from $p1 to $p2") | ||
| 1267 | if $LaTeXML::DEBUG{alignment}; | ||
| 1268 | my $nd = alignment_match_lines($p1, $p2, $ndata); | ||
| 1269 | my $ok = ($nd * 1.0) / $ndata > 0.66; | ||
| 1270 | Debug("Matched $nd data lines => " . ($ok ? "Succeed" : "Failed")) | ||
| 1271 | if $LaTeXML::DEBUG{alignment}; | ||
| 1272 | return ($ok ? $nd : 0); } | ||
| 1273 | |||
| 1274 | # Match the $n lines starting at $i2 to those starting at $i1. | ||
| 1275 | sub alignment_match_lines { | ||
| 1276 | my ($p1, $p2, $n) = @_; | ||
| 1277 | for (my $i = 0 ; $i < $n ; $i++) { | ||
| 1278 | return $i if ($p1 + $i >= scalar(@::TABLINES)) || ($p2 + $i >= scalar(@::TABLINES)) | ||
| 1279 | || alignment_compare($::TAB_AXIS, 0, 0, $p1 + $i, $p2 + $i) >= $::TAB_THRESHOLD; } | ||
| 1280 | return $n; } | ||
| 1281 | |||
| 1282 | # Skip through a block of lines starting at $i that appear to be data, returning the number of lines. | ||
| 1283 | # We'll assume the 1st line is data, compare it to following lines, | ||
| 1284 | # but also accept `continuation' data lines. | ||
| 1285 | sub alignment_skip_data { | ||
| 1286 | my ($i) = @_; | ||
| 1287 | return 0 if $i >= scalar(@::TABLINES); | ||
| 1288 | Debug("Scanning for data") if $LaTeXML::DEBUG{alignment}; | ||
| 1289 | my $n = 1; | ||
| 1290 | while ($i + $n < scalar(@::TABLINES)) { | ||
| 1291 | last if (alignment_compare($::TAB_AXIS, 1, 0, $i + $n - 1, $i + $n) >= $::TAB_THRESHOLD) | ||
| 1292 | # Accept an outlying `continuation line' as data, if mostly empty | ||
| 1293 | && (($n < 2) | ||
| 1294 | || (scalar(grep { $$_{content_class} eq '_' } @{ $::TABLINES[$i + $n] }) <= 0.4 * scalar($::TABLINES[0]))); | ||
| 1295 | $n++; } | ||
| 1296 | Debug("Found $n data lines at $i") if $LaTeXML::DEBUG{alignment}; | ||
| 1297 | return ($n >= $MIN_ALIGNMENT_DATA_LINES ? $n : 0); } | ||
| 1298 | |||
| 1299 | sub XXXalignment_max_content_length { | ||
| 1300 | my ($length, $from, $to) = @_; | ||
| 1301 | foreach my $j (($from .. $to)) { | ||
| 1302 | foreach my $cell (@{ $::TABLINES[$j] }) { | ||
| 1303 | $length = $$cell{content_length} | ||
| 1304 | if $$cell{content_length} && ($$cell{content_length} > $length); } } | ||
| 1305 | return $length; } | ||
| 1306 | |||
| 1307 | # Return the maximum "content length" for lines from $from to $to. | ||
| 1308 | sub alignment_max_content_length { | ||
| 1309 | my ($length, $from, $to) = @_; | ||
| 1310 | foreach my $j (($from .. $to)) { | ||
| 1311 | my $l = 0; | ||
| 1312 | foreach my $cell (@{ $::TABLINES[$j] }) { | ||
| 1313 | $l += $$cell{content_length} || 0; } | ||
| 1314 | $length = $l if $l > $length; } | ||
| 1315 | return $length; } | ||
| 1316 | |||
| 1317 | #====================================================================== | ||
| 1318 | # The comparator. | ||
| 1319 | # our %cell_class_diff = | ||
| 1320 | # ('_'=>{'_'=>0.0, m=>0.1, i=>0.1, t=>0.1, '?'=>0.1, mx=>0.1}, | ||
| 1321 | # m =>{'_'=>0.1, m=>0.0, i=>0.1, mx=>0.2}, | ||
| 1322 | # i =>{'_'=>0.1, m=>0.1, i=>0.0, mx=>0.2}, | ||
| 1323 | # t =>{'_'=>0.1, t=>0.0, mx=>0.2}, | ||
| 1324 | # '?'=>{'_'=>0.1, '?'=>0.0, mx=>0.2}, | ||
| 1325 | # mx=>{'_'=>0.1, m=>0.2, i=>0.2, t=>0.2, '?'=>0.2, mx=>0.0}); | ||
| 1326 | |||
| 1327 | my %cell_class_diff = ( # [CONSTANT] | ||
| 1328 | '_' => { '_' => 0.0, m => 0.05, i => 0.05, t => 0.05, '?' => 0.05, mx => 0.05 }, | ||
| 1329 | m => { '_' => 0.05, m => 0.0, i => 0.1, mx => 0.2 }, | ||
| 1330 | i => { '_' => 0.05, m => 0.1, i => 0.0, mx => 0.2 }, | ||
| 1331 | t => { '_' => 0.05, t => 0.0, mx => 0.2 }, | ||
| 1332 | '?' => { '_' => 0.05, '?' => 0.0, mx => 0.2 }, | ||
| 1333 | mx => { '_' => 0.05, m => 0.2, i => 0.2, t => 0.2, '?' => 0.2, mx => 0.0 }); | ||
| 1334 | |||
| 1335 | # Compare two lines along $axis (0=row,1=column), returning a measure of the difference. | ||
| 1336 | # The borders are compared differently if | ||
| 1337 | # $foradjacency: we adjacent lines that might belong to the same block, | ||
| 1338 | # otherwise : comparing two lines that ought to have identical patterns (eg. in a repeated block) | ||
| 1339 | sub alignment_compare { | ||
| 1340 | my ($axis, $foradjacency, $reversed, $p1, $p2) = @_; | ||
| 1341 | my $line1 = $::TABLINES[$p1]; | ||
| 1342 | my $line2 = $::TABLINES[$p2]; | ||
| 1343 | return 0 if !($line1 && $line2); | ||
| 1344 | return 999999 if $line1 xor $line2; | ||
| 1345 | my @cells1 = @$line1; | ||
| 1346 | my @cells2 = @$line2; | ||
| 1347 | my $ncells = scalar(@cells1); | ||
| 1348 | my $diff = 0.0; | ||
| 1349 | |||
| 1350 | while (@cells1 && @cells2) { | ||
| 1351 | my $cell1 = shift(@cells1); | ||
| 1352 | my $cell2 = shift(@cells2); | ||
| 1353 | # Annoying test avoids warnings if cells inconsistent; likely due to incorrect row/col spans | ||
| 1354 | next if grep { !defined $$cell1{$_} } qw(content_class r l t b); | ||
| 1355 | next if grep { !defined $$cell2{$_} } qw(content_class r l t b); | ||
| 1356 | # $diff += 0.5 if (($$cell1{align}||'') ne ($$cell2{align}||'')) | ||
| 1357 | $diff += 0.75 if (($$cell1{align} || '') ne ($$cell2{align} || '')) | ||
| 1358 | && ($$cell1{content_class} ne '_') && ($$cell2{content_class} ne '_'); | ||
| 1359 | if (my $d = $cell_class_diff{ $$cell1{content_class} }{ $$cell2{content_class} }) { | ||
| 1360 | $diff += $d; } | ||
| 1361 | elsif ($$cell1{content_class} ne $$cell2{content_class}) { | ||
| 1362 | $diff += 0.75; } | ||
| 1363 | # compare certain edges | ||
| 1364 | if ($foradjacency) { # Compare edges for adjacent rows of potentially different purpose | ||
| 1365 | $diff += 0.3 * scalar(grep { $$cell1{$_} != $$cell2{$_} } ($axis == 0 ? qw(r l) : qw(t b))); | ||
| 1366 | # Penalty for apparent divider between. | ||
| 1367 | my $pedge = ($axis == 0 ? ($reversed ? 't' : 'b') : ($reversed ? 'l' : 'r')); | ||
| 1368 | if ($$cell1{$pedge} && ($$cell1{$pedge} != $$cell2{$pedge})) { | ||
| 1369 | $diff += abs($$cell1{$pedge} - $$cell2{$pedge}) * 1.0; } | ||
| 1370 | } | ||
| 1371 | else { # Compare edges for rows from diff places for potential similarity | ||
| 1372 | $diff += 0.3 * scalar(grep { $$cell1{$_} != $$cell2{$_} } qw(r l t b)); } | ||
| 1373 | } | ||
| 1374 | $diff /= $ncells; | ||
| 1375 | Debug("$p1-$p2 => $diff; ") if $LaTeXML::DEBUG{alignment}; | ||
| 1376 | return $diff; } | ||
| 1377 | |||
| 1378 | #====================================================================== | ||
| 1379 | # Debugging. | ||
| 1380 | sub summarize_alignment { | ||
| 1381 | my ($rows, $cols) = @_; | ||
| 1382 | my $r = 0; | ||
| 1383 | my ($nrows, $ncols) = (scalar(@$rows), scalar(@{ $$rows[0] })); | ||
| 1384 | foreach my $cell (@{ $$rows[0] }) { | ||
| 1385 | Debug(' ' . ($$cell{t} ? ('-' x 6) : (' ' x 6))); } | ||
| 1386 | foreach my $row (@$rows) { | ||
| 1387 | my $maxb = 0; | ||
| 1388 | Debug(($$row[0]{l} ? ('|' x $$row[0]{l}) : ' ')); | ||
| 1389 | foreach my $cell (@$row) { | ||
| 1390 | Debug(sprintf(" %4s ", | ||
| 1391 | ($$cell{cell_type} || '?') | ||
| 1392 | . ($$cell{align} ? $ALIGNMENT_CODE{ $$cell{align} } : ' ') | ||
| 1393 | . ($$cell{content_class} || '?') | ||
| 1394 | . ($$cell{r} ? ('|' x $$cell{r}) : ' '))); | ||
| 1395 | $maxb = $$cell{b} if $$cell{b} > $maxb; } | ||
| 1396 | # Debug(sprintf("%.3f",alignment_compare(0,1,$$rows[$r],$$rows[$r+1])) if ($r < $nrows-1)); | ||
| 1397 | for (my $b = 0 ; $b < $maxb ; $b++) { | ||
| 1398 | foreach my $cell (@$row) { | ||
| 1399 | Debug(' ' . ($b < $$cell{b} ? ('-' x 6) : (' ' x 6))); } } | ||
| 1400 | $r++; } | ||
| 1401 | # for(my $c = 0; $c < $ncols-1; $c++){ | ||
| 1402 | # Debug(sprintf(" %.3f ",alignment_compare(1,1,$$cols[$c],$$cols[$c+1]))); } | ||
| 1403 | return; } | ||
| 1404 | |||
| 1405 | #====================================================================== | ||
| 1406 | 1; | ||
| 1407 | |||
| 1408 | __END__ | ||