| 1 | package Catmandu::Importer::ALTOXML; | ||
| 2 | 0 | ▶ | use Catmandu::Sane; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 3 | use Moo; | ||
| 4 | use XML::LibXML::Reader; | ||
| 5 | use Catmandu::Error; | ||
| 6 | use namespace::clean; | ||
| 7 | |||
| 8 | with "Catmandu::Importer"; | ||
| 9 | |||
| 10 | has reader => ( is => "lazy", init_arg => undef ); | ||
| 11 | |||
| 12 | has current_page => ( | ||
| 13 | is => "rw", | ||
| 14 | default => sub { undef; } | ||
| 15 | ); | ||
| 16 | |||
| 17 | has current_block => ( | ||
| 18 | is => "rw", | ||
| 19 | default => sub { undef; } | ||
| 20 | ); | ||
| 21 | |||
| 22 | sub _build_reader { | ||
| 23 | my $self = $_[0]; | ||
| 24 | binmode $self->fh; | ||
| 25 | my $reader = XML::LibXML::Reader->new( | ||
| 26 | IO => $self->fh, | ||
| 27 | validation => 0, | ||
| 28 | load_ext_dtd => 0 | ||
| 29 | ) or Catmandu::Error->throw("unable to read file"); | ||
| 30 | |||
| 31 | $reader; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub _get_coords { | ||
| 35 | my $reader = $_[0]; | ||
| 36 | |||
| 37 | my $coords = +{ | ||
| 38 | x => $reader->getAttribute("HPOS"), | ||
| 39 | y => $reader->getAttribute("VPOS"), | ||
| 40 | w => $reader->getAttribute("WIDTH"), | ||
| 41 | h => $reader->getAttribute("HEIGHT") | ||
| 42 | }; | ||
| 43 | |||
| 44 | for(keys %$coords){ | ||
| 45 | $coords->{$_} += 0 if defined($coords->{$_}); | ||
| 46 | } | ||
| 47 | |||
| 48 | $coords; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub _read { | ||
| 52 | my $state = $_[0]->reader->read() or return; | ||
| 53 | $state < 0 && Catmandu::Error->throw("error occurred during parsing of file"); | ||
| 54 | $state; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub _next { | ||
| 58 | |||
| 59 | my $self = $_[0]; | ||
| 60 | state $start_element = XML_READER_TYPE_ELEMENT; | ||
| 61 | state $end_element = XML_READER_TYPE_END_ELEMENT; | ||
| 62 | |||
| 63 | my $reader = $self->reader(); | ||
| 64 | |||
| 65 | my $line; | ||
| 66 | |||
| 67 | while( $self->_read ){ | ||
| 68 | |||
| 69 | my $name = $reader->name(); | ||
| 70 | my $nodeType = $reader->nodeType(); | ||
| 71 | |||
| 72 | if ( $nodeType == $start_element && $name eq "Page" ) { | ||
| 73 | |||
| 74 | my $old_page = $self->current_page // +{ no => 0 }; | ||
| 75 | my $coords = _get_coords( $reader ); | ||
| 76 | |||
| 77 | $self->current_page({ | ||
| 78 | no => $old_page->{no} + 1, | ||
| 79 | %$coords | ||
| 80 | }); | ||
| 81 | |||
| 82 | next; | ||
| 83 | |||
| 84 | } | ||
| 85 | elsif ( $nodeType == $start_element && $name eq "TextBlock" ) { | ||
| 86 | |||
| 87 | my $old_block = $self->current_block() // +{ no => 0 }; | ||
| 88 | $self->current_block({ | ||
| 89 | %{ _get_coords( $reader ) }, | ||
| 90 | no => $old_block->{no} + 1, | ||
| 91 | no_lines => 0 | ||
| 92 | }); | ||
| 93 | |||
| 94 | } | ||
| 95 | elsif ( $nodeType == $start_element && $name eq "TextLine" ) { | ||
| 96 | |||
| 97 | my $current_block = $self->current_block(); | ||
| 98 | |||
| 99 | if ( $current_block ) { | ||
| 100 | |||
| 101 | $current_block->{no_lines}++; | ||
| 102 | |||
| 103 | } | ||
| 104 | |||
| 105 | my $coords = _get_coords( $reader ); | ||
| 106 | |||
| 107 | my $current_page = $self->current_page(); | ||
| 108 | $line = +{ | ||
| 109 | page => $current_page->{no}, | ||
| 110 | page_x => $current_page->{x}, | ||
| 111 | page_y => $current_page->{y}, | ||
| 112 | page_w => $current_page->{w}, | ||
| 113 | page_h => $current_page->{h}, | ||
| 114 | %$coords, | ||
| 115 | text => [] | ||
| 116 | }; | ||
| 117 | |||
| 118 | if ( $current_block ) { | ||
| 119 | |||
| 120 | $line->{block} = $current_block->{no}; | ||
| 121 | $line->{"block_$_"} = $current_block->{$_} for qw(x y w h); | ||
| 122 | |||
| 123 | } | ||
| 124 | |||
| 125 | } | ||
| 126 | elsif ( $nodeType == $end_element && $name eq "TextBlock" ) { | ||
| 127 | |||
| 128 | my $current_block = $self->current_block(); | ||
| 129 | |||
| 130 | #something wrong with text_line dimensions. Use all text_block coordinates if only one text_line | ||
| 131 | if ( defined( $line ) && ($line->{w} == 0 || $line->{h} == 0 ) && $current_block->{no_lines} == 1 ) { | ||
| 132 | |||
| 133 | $line->{$_} = $current_block->{$_} for qw(x y w h); | ||
| 134 | |||
| 135 | } | ||
| 136 | |||
| 137 | } | ||
| 138 | elsif ( $nodeType == $start_element && $name eq "String" ) { | ||
| 139 | |||
| 140 | if( $line ) { | ||
| 141 | |||
| 142 | push @{$line->{text}}, $reader->getAttribute("CONTENT"); | ||
| 143 | |||
| 144 | } | ||
| 145 | |||
| 146 | } | ||
| 147 | elsif ( $nodeType == $end_element && $name eq "TextLine" ) { | ||
| 148 | |||
| 149 | if ( $line ) { | ||
| 150 | |||
| 151 | $line->{text} = join(" ", @{ $line->{text} }); | ||
| 152 | |||
| 153 | } | ||
| 154 | last; | ||
| 155 | |||
| 156 | } | ||
| 157 | |||
| 158 | } | ||
| 159 | |||
| 160 | $line; | ||
| 161 | } | ||
| 162 | |||
| 163 | sub generator { | ||
| 164 | |||
| 165 | my $self = $_[0]; | ||
| 166 | |||
| 167 | sub { | ||
| 168 | $self->_next(); | ||
| 169 | }; | ||
| 170 | |||
| 171 | } | ||
| 172 | |||
| 173 | 1; | ||