| 1 | #======================================================================= | ||
| 2 | # ____ ____ _____ _ ____ ___ ____ | ||
| 3 | # | _ \| _ \| ___| _ _ / \ | _ \_ _| |___ \ | ||
| 4 | # | |_) | | | | |_ (_) (_) / _ \ | |_) | | __) | | ||
| 5 | # | __/| |_| | _| _ _ / ___ \| __/| | / __/ | ||
| 6 | # |_| |____/|_| (_) (_) /_/ \_\_| |___| |_____| | ||
| 7 | # | ||
| 8 | # A Perl Module Chain to faciliate the Creation and Modification | ||
| 9 | # of High-Quality "Portable Document Format (PDF)" Files. | ||
| 10 | # | ||
| 11 | #======================================================================= | ||
| 12 | # | ||
| 13 | # THIS IS A REUSED PERL MODULE, FOR PROPER LICENCING TERMS SEE BELOW: | ||
| 14 | # | ||
| 15 | # | ||
| 16 | # Copyright Martin Hosken <Martin_Hosken@sil.org> | ||
| 17 | # | ||
| 18 | # No warranty or expression of effectiveness, least of all regarding | ||
| 19 | # anyone's safety, is implied in this software or documentation. | ||
| 20 | # | ||
| 21 | # This specific module is licensed under the Perl Artistic License. | ||
| 22 | # | ||
| 23 | # | ||
| 24 | # $Id: Segarr.pm,v 2.0 2005/11/16 02:16:00 areibens Exp $ | ||
| 25 | # | ||
| 26 | #======================================================================= | ||
| 27 | package PDF::API3::Compat::API2::Basic::TTF::Segarr; | ||
| 28 | |||
| 29 | =head1 NAME | ||
| 30 | |||
| 31 | PDF::API3::Compat::API2::Basic::TTF::Segarr - Segmented array | ||
| 32 | |||
| 33 | =head1 DESCRIPTION | ||
| 34 | |||
| 35 | Holds data either directly or indirectly as a series of arrays. This class | ||
| 36 | looks after the set of arrays and masks the individual sub-arrays, thus saving | ||
| 37 | a class, we hope. | ||
| 38 | |||
| 39 | =head1 INSTANCE VARIABLES | ||
| 40 | |||
| 41 | All instance variables do not start with a space. | ||
| 42 | |||
| 43 | The segmented array is simply an array of segments | ||
| 44 | |||
| 45 | Each segment is a more complex affair: | ||
| 46 | |||
| 47 | =over 4 | ||
| 48 | |||
| 49 | =item START | ||
| 50 | |||
| 51 | In terms of the array, the address for the 0th element in this segment. | ||
| 52 | |||
| 53 | =item LEN | ||
| 54 | |||
| 55 | Number of elements in this segment | ||
| 56 | |||
| 57 | =item VAL | ||
| 58 | |||
| 59 | The array which contains the elements | ||
| 60 | |||
| 61 | =back | ||
| 62 | |||
| 63 | =head1 METHODS | ||
| 64 | |||
| 65 | =cut | ||
| 66 | |||
| 67 | use strict; | ||
| 68 | use vars qw(@types $VERSION); | ||
| 69 | $VERSION = 0.0001; | ||
| 70 | |||
| 71 | @types = ('', 'C', 'n', '', 'N'); | ||
| 72 | |||
| 73 | =head2 PDF::API3::Compat::API2::Basic::TTF::Segarr->new($size) | ||
| 74 | |||
| 75 | Creates a new segmented array with a given data size | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | sub new | ||
| 80 | { | ||
| 81 | my ($class) = @_; | ||
| 82 | my ($self) = []; | ||
| 83 | |||
| 84 | bless $self, (ref($class) || $class); | ||
| 85 | } | ||
| 86 | |||
| 87 | |||
| 88 | =head2 $s->fastadd_segment($start, $is_sparse, @dat) | ||
| 89 | |||
| 90 | Creates a new segment and adds it to the array assuming no overlap between | ||
| 91 | the new segment and any others in the array. $is_sparse indicates whether the | ||
| 92 | passed in array contains C<undef>s or not. If false no checking is done (which | ||
| 93 | is faster, but riskier). If equal to 2 then 0 is considered undef as well. | ||
| 94 | |||
| 95 | Returns the number of segments inserted. | ||
| 96 | |||
| 97 | =cut | ||
| 98 | |||
| 99 | sub fastadd_segment | ||
| 100 | { | ||
| 101 | my ($self) = shift; | ||
| 102 | my ($start) = shift; | ||
| 103 | my ($sparse) = shift; | ||
| 104 | my ($p, $i, $seg, @seg); | ||
| 105 | |||
| 106 | |||
| 107 | if ($sparse) | ||
| 108 | { | ||
| 109 | for ($i = 0; $i <= $#_; $i++) | ||
| 110 | { | ||
| 111 | if (!defined $seg && (($sparse != 2 && defined $_[$i]) || $_[$i] != 0)) | ||
| 112 | { $seg->{'START'} = $start + $i; $seg->{'VAL'} = []; } | ||
| 113 | |||
| 114 | if (defined $seg && (($sparse == 2 && $_[$i] == 0) || !defined $_[$i])) | ||
| 115 | { | ||
| 116 | $seg->{'LEN'} = $start + $i - $seg->{'START'}; | ||
| 117 | push(@seg, $seg); | ||
| 118 | $seg = undef; | ||
| 119 | } elsif (defined $seg) | ||
| 120 | { push (@{$seg->{'VAL'}}, $_[$i]); } | ||
| 121 | } | ||
| 122 | if (defined $seg) | ||
| 123 | { | ||
| 124 | push(@seg, $seg); | ||
| 125 | $seg->{'LEN'} = $start + $i - $seg->{'START'}; | ||
| 126 | } | ||
| 127 | } else | ||
| 128 | { | ||
| 129 | $seg->{'START'} = $start; | ||
| 130 | $seg->{'LEN'} = $#_ + 1; | ||
| 131 | $seg->{'VAL'} = [@_]; | ||
| 132 | @seg = ($seg); | ||
| 133 | } | ||
| 134 | |||
| 135 | for ($i = 0; $i <= $#$self; $i++) | ||
| 136 | { | ||
| 137 | if ($self->[$i]{'START'} > $start) | ||
| 138 | { | ||
| 139 | splice(@$self, $i, 0, @seg); | ||
| 140 | return wantarray ? @seg : scalar(@seg); | ||
| 141 | } | ||
| 142 | } | ||
| 143 | push(@$self, @seg); | ||
| 144 | return wantarray ? @seg : scalar(@seg); | ||
| 145 | } | ||
| 146 | |||
| 147 | |||
| 148 | =head2 $s->add_segment($start, $overwrite, @dat) | ||
| 149 | |||
| 150 | Creates a new segment and adds it to the array allowing for possible overlaps | ||
| 151 | between the new segment and the existing ones. In the case of overlaps, elements | ||
| 152 | from the new segment are deleted unless $overwrite is set in which case the | ||
| 153 | elements already there are over-written. | ||
| 154 | |||
| 155 | This method also checks the data coming in to see if it is sparse (i.e. contains | ||
| 156 | undef values). Gaps cause new segments to be created or not to over-write existing | ||
| 157 | values. | ||
| 158 | |||
| 159 | =cut | ||
| 160 | |||
| 161 | sub add_segment | ||
| 162 | { | ||
| 163 | my ($self) = shift; | ||
| 164 | my ($start) = shift; | ||
| 165 | my ($over) = shift; | ||
| 166 | my ($seg, $i, $s, $offset, $j, $newi); | ||
| 167 | |||
| 168 | return $self->fastadd_segment($start, $over, @_) if ($#$self < 0); | ||
| 169 | $offset = 0; | ||
| 170 | for ($i = 0; $i <= $#$self && $offset <= $#_; $i++) | ||
| 171 | { | ||
| 172 | $s = $self->[$i]; | ||
| 173 | if ($s->{'START'} <= $start + $offset) # only < for $offset == 0 | ||
| 174 | { | ||
| 175 | if ($s->{'START'} + $s->{'LEN'} > $start + $#_) | ||
| 176 | { | ||
| 177 | for ($j = $offset; $j <= $#_; $j++) | ||
| 178 | { | ||
| 179 | if ($over) | ||
| 180 | { $s->{'VAL'}[$start - $s->{'START'} + $j] = $_[$j] if defined $_[$j]; } | ||
| 181 | else | ||
| 182 | { $s->{'VAL'}[$start - $s->{'START'} + $j] ||= $_[$j] if defined $_[$j]; } | ||
| 183 | } | ||
| 184 | $offset = $#_ + 1; | ||
| 185 | last; | ||
| 186 | } elsif ($s->{'START'} + $s->{'LEN'} > $start + $offset) # is $offset needed here? | ||
| 187 | { | ||
| 188 | for ($j = $offset; $j < $s->{'START'} + $s->{'LEN'} - $start; $j++) | ||
| 189 | { | ||
| 190 | if ($over) | ||
| 191 | { $s->{'VAL'}[$start - $s->{'START'} + $j] = $_[$j] if defined $_[$j]; } | ||
| 192 | else | ||
| 193 | { $s->{'VAL'}[$start - $s->{'START'} + $j] ||= $_[$j] if defined $_[$j]; } | ||
| 194 | } | ||
| 195 | $offset = $s->{'START'} + $s->{'LEN'} - $start; | ||
| 196 | } | ||
| 197 | } else # new seg please | ||
| 198 | { | ||
| 199 | if ($s->{'START'} > $start + $#_ + 1) | ||
| 200 | { | ||
| 201 | $i += $self->fastadd_segment($start + $offset, 1, @_[$offset .. $#_]) - 1; | ||
| 202 | $offset = $#_ + 1; | ||
| 203 | } | ||
| 204 | else | ||
| 205 | { | ||
| 206 | $i += $self->fastadd_segment($start + $offset, 1, @_[$offset .. $s->{'START'} - $start]) - 1; | ||
| 207 | $offset = $s->{'START'} - $start + 1; | ||
| 208 | } | ||
| 209 | } | ||
| 210 | } | ||
| 211 | if ($offset <= $#_) | ||
| 212 | { | ||
| 213 | $seg->{'START'} = $start + $offset; | ||
| 214 | $seg->{'LEN'} = $#_ - $offset + 1; | ||
| 215 | $seg->{'VAL'} = [@_[$offset .. $#_]]; | ||
| 216 | push (@$self, $seg); | ||
| 217 | } | ||
| 218 | $self->tidy; | ||
| 219 | } | ||
| 220 | |||
| 221 | |||
| 222 | =head2 $s->tidy | ||
| 223 | |||
| 224 | Merges any immediately adjacent segments | ||
| 225 | |||
| 226 | =cut | ||
| 227 | |||
| 228 | sub tidy | ||
| 229 | { | ||
| 230 | my ($self) = @_; | ||
| 231 | my ($i, $sl, $s); | ||
| 232 | |||
| 233 | for ($i = 1; $i <= $#$self; $i++) | ||
| 234 | { | ||
| 235 | $sl = $self->[$i - 1]; | ||
| 236 | $s = $self->[$i]; | ||
| 237 | if ($s->{'START'} == $sl->{'START'} + $sl->{'LEN'}) | ||
| 238 | { | ||
| 239 | $sl->{'LEN'} += $s->{'LEN'}; | ||
| 240 | push (@{$sl->{'VAL'}}, @{$s->{'VAL'}}); | ||
| 241 | splice(@$self, $i, 1); | ||
| 242 | $i--; | ||
| 243 | } | ||
| 244 | } | ||
| 245 | $self; | ||
| 246 | } | ||
| 247 | |||
| 248 | |||
| 249 | =head2 $s->at($addr, [$len]) | ||
| 250 | |||
| 251 | Looks up the data held at the given address by locating the appropriate segment | ||
| 252 | etc. If $len > 1 then returns an array of values, spaces being filled with undef. | ||
| 253 | |||
| 254 | =cut | ||
| 255 | |||
| 256 | sub at | ||
| 257 | { | ||
| 258 | my ($self, $addr, $len) = @_; | ||
| 259 | my ($i, $dat, $s, @res, $offset); | ||
| 260 | |||
| 261 | $len = 1 unless defined $len; | ||
| 262 | $offset = 0; | ||
| 263 | for ($i = 0; $i <= $#$self; $i++) | ||
| 264 | { | ||
| 265 | $s = $self->[$i]; | ||
| 266 | next if ($s->{'START'} + $s->{'LEN'} < $addr + $offset); # only fires on $offset == 0 | ||
| 267 | if ($s->{'START'} > $addr + $offset) | ||
| 268 | { | ||
| 269 | push (@res, (undef) x ($s->{'START'} > $addr + $len ? | ||
| 270 | $len - $offset : $s->{'START'} - $addr - $offset)); | ||
| 271 | $offset = $s->{'START'} - $addr; | ||
| 272 | } | ||
| 273 | last if ($s->{'START'} >= $addr + $len); | ||
| 274 | |||
| 275 | if ($s->{'START'} + $s->{'LEN'} >= $addr + $len) | ||
| 276 | { | ||
| 277 | push (@res, @{$s->{'VAL'}}[$addr + $offset - $s->{'START'} .. | ||
| 278 | $addr + $len - $s->{'START'} - 1]); | ||
| 279 | $offset = $len; | ||
| 280 | last; | ||
| 281 | } else | ||
| 282 | { | ||
| 283 | push (@res, @{$s->{'VAL'}}[$addr + $offset - $s->{'START'} .. $s->{'LEN'} - 1]); | ||
| 284 | $offset = $s->{'START'} + $s->{'LEN'} - $addr; | ||
| 285 | } | ||
| 286 | } | ||
| 287 | push (@res, (undef) x ($len - $offset)) if ($offset < $len); | ||
| 288 | return wantarray ? @res : $res[0]; | ||
| 289 | } | ||
| 290 | |||
| 291 | |||
| 292 | =head2 $s->remove($addr, [$len]) | ||
| 293 | |||
| 294 | Removes the item or items from addr returning them as an array or the first | ||
| 295 | value in a scalar context. This is very like C<at>, including padding with | ||
| 296 | undef, but it deletes stuff as it goes. | ||
| 297 | |||
| 298 | =cut | ||
| 299 | |||
| 300 | sub remove | ||
| 301 | { | ||
| 302 | my ($self, $addr, $len) = @_; | ||
| 303 | my ($i, $dat, $s, @res, $offset); | ||
| 304 | |||
| 305 | $len = 1 unless defined $len; | ||
| 306 | $offset = 0; | ||
| 307 | for ($i = 0; $i <= $#$self; $i++) | ||
| 308 | { | ||
| 309 | $s = $self->[$i]; | ||
| 310 | next if ($s->{'START'} + $s->{'LEN'} < $addr + $offset); | ||
| 311 | if ($s->{'START'} > $addr + $offset) | ||
| 312 | { | ||
| 313 | push (@res, (undef) x ($s->{'START'} > $addr + $len ? | ||
| 314 | $len - $offset : $s->{'START'} - $addr - $offset)); | ||
| 315 | $offset = $s->{'START'} - $addr; | ||
| 316 | } | ||
| 317 | last if ($s->{'START'} >= $addr + $len); | ||
| 318 | |||
| 319 | unless ($s->{'START'} == $addr + $offset) | ||
| 320 | { | ||
| 321 | my ($seg) = {}; | ||
| 322 | |||
| 323 | $seg->{'START'} = $s->{'START'}; | ||
| 324 | $seg->{'LEN'} = $addr + $offset - $s->{'START'}; | ||
| 325 | $seg->{'VAL'} = [splice(@{$s->{'VAL'}}, 0, $addr + $offset - $s->{'START'})]; | ||
| 326 | $s->{'LEN'} -= $addr + $offset - $s->{'START'}; | ||
| 327 | $s->{'START'} = $addr + $offset; | ||
| 328 | |||
| 329 | splice(@$self, $i, 0, $seg); | ||
| 330 | $i++; | ||
| 331 | } | ||
| 332 | |||
| 333 | if ($s->{'START'} + $s->{'LEN'} >= $addr + $len) | ||
| 334 | { | ||
| 335 | push (@res, splice(@{$s->{'VAL'}}, 0, $len - $offset)); | ||
| 336 | $s->{'LEN'} -= $len - $offset; | ||
| 337 | $s->{'START'} += $len - $offset; | ||
| 338 | $offset = $len; | ||
| 339 | last; | ||
| 340 | } else | ||
| 341 | { | ||
| 342 | push (@res, @{$s->{'VAL'}}); | ||
| 343 | $offset = $s->{'START'} + $s->{'LEN'} - $addr; | ||
| 344 | splice(@$self, $i, 0); | ||
| 345 | $i--; | ||
| 346 | } | ||
| 347 | } | ||
| 348 | push (@res, (undef) x ($len - $offset)) if ($offset < $len); | ||
| 349 | return wantarray ? @res : $res[0]; | ||
| 350 | } | ||
| 351 | |||
| 352 | |||
| 353 | =head2 $s->copy | ||
| 354 | |||
| 355 | Deep copies this array | ||
| 356 | |||
| 357 | =cut | ||
| 358 | |||
| 359 | sub copy | ||
| 360 | { | ||
| 361 | my ($self) = @_; | ||
| 362 | my ($res, $p); | ||
| 363 | |||
| 364 | $res = []; | ||
| 365 | foreach $p (@$self) | ||
| 366 | { push (@$res, $self->copy_seg($p)); } | ||
| 367 | $res; | ||
| 368 | } | ||
| 369 | |||
| 370 | |||
| 371 | =head2 $s->copy_seg($seg) | ||
| 372 | |||
| 373 | Creates a deep copy of a segment | ||
| 374 | |||
| 375 | =cut | ||
| 376 | |||
| 377 | sub copy_seg | ||
| 378 | { | ||
| 379 | my ($self, $seg) = @_; | ||
| 380 | my ($p, $res); | ||
| 381 | |||
| 382 | $res = {}; | ||
| 383 | $res->{'VAL'} = [@{$seg->{'VAL'}}]; | ||
| 384 | foreach $p (keys %$seg) | ||
| 385 | { $res->{$p} = $seg->{$p} unless defined $res->{$p}; } | ||
| 386 | $res; | ||
| 387 | } | ||
| 388 | |||
| 389 | |||
| 390 | 1; | ||
| 391 | |||
| 392 | =head1 BUGS | ||
| 393 | |||
| 394 | No known bugs. | ||
| 395 | |||
| 396 | =head1 AUTHOR | ||
| 397 | |||
| 398 | Martin Hosken Martin_Hosken@sil.org. See L<PDF::API3::Compat::API2::Basic::TTF::Font> for copyright and | ||
| 399 | licensing. | ||
| 400 | |||
| 401 | =cut | ||
| 402 |