1 package Font::TTF::Segarr;
5 Font::TTF::Segarr - Segmented array
9 Holds data either directly or indirectly as a series of arrays. This class
10 looks after the set of arrays and masks the individual sub-arrays, thus saving
13 =head1 INSTANCE VARIABLES
15 All instance variables do not start with a space.
17 The segmented array is simply an array of segments
19 Each segment is a more complex affair:
25 In terms of the array, the address for the 0th element in this segment.
29 Number of elements in this segment
33 The array which contains the elements
42 use vars qw(@types $VERSION);
45 @types = ('', 'C', 'n', '', 'N');
47 =head2 Font::TTF::Segarr->new($size)
49 Creates a new segmented array with a given data size
58 bless $self, (ref($class) || $class);
62 =head2 $s->fastadd_segment($start, $is_sparse, @dat)
64 Creates a new segment and adds it to the array assuming no overlap between
65 the new segment and any others in the array. $is_sparse indicates whether the
66 passed in array contains C<undef>s or not. If false no checking is done (which
67 is faster, but riskier). If equal to 2 then 0 is considered undef as well.
69 Returns the number of segments inserted.
78 my ($p, $i, $seg, @seg);
83 for ($i = 0; $i <= $#_; $i++)
85 if (!defined $seg && (($sparse != 2 && defined $_[$i]) || $_[$i] != 0))
86 { $seg->{'START'} = $start + $i; $seg->{'VAL'} = []; }
88 if (defined $seg && (($sparse == 2 && $_[$i] == 0) || !defined $_[$i]))
90 $seg->{'LEN'} = $start + $i - $seg->{'START'};
93 } elsif (defined $seg)
94 { push (@{$seg->{'VAL'}}, $_[$i]); }
99 $seg->{'LEN'} = $start + $i - $seg->{'START'};
103 $seg->{'START'} = $start;
104 $seg->{'LEN'} = $#_ + 1;
105 $seg->{'VAL'} = [@_];
109 for ($i = 0; $i <= $#$self; $i++)
111 if ($self->[$i]{'START'} > $start)
113 splice(@$self, $i, 0, @seg);
114 return wantarray ? @seg : scalar(@seg);
118 return wantarray ? @seg : scalar(@seg);
122 =head2 $s->add_segment($start, $overwrite, @dat)
124 Creates a new segment and adds it to the array allowing for possible overlaps
125 between the new segment and the existing ones. In the case of overlaps, elements
126 from the new segment are deleted unless $overwrite is set in which case the
127 elements already there are over-written.
129 This method also checks the data coming in to see if it is sparse (i.e. contains
130 undef values). Gaps cause new segments to be created or not to over-write existing
140 my ($seg, $i, $s, $offset, $j, $newi);
142 return $self->fastadd_segment($start, $over, @_) if ($#$self < 0);
144 for ($i = 0; $i <= $#$self && $offset <= $#_; $i++)
147 if ($s->{'START'} <= $start + $offset) # only < for $offset == 0
149 if ($s->{'START'} + $s->{'LEN'} > $start + $#_)
151 for ($j = $offset; $j <= $#_; $j++)
154 { $s->{'VAL'}[$start - $s->{'START'} + $j] = $_[$j] if defined $_[$j]; }
156 { $s->{'VAL'}[$start - $s->{'START'} + $j] ||= $_[$j] if defined $_[$j]; }
160 } elsif ($s->{'START'} + $s->{'LEN'} > $start + $offset) # is $offset needed here?
162 for ($j = $offset; $j < $s->{'START'} + $s->{'LEN'} - $start; $j++)
165 { $s->{'VAL'}[$start - $s->{'START'} + $j] = $_[$j] if defined $_[$j]; }
167 { $s->{'VAL'}[$start - $s->{'START'} + $j] ||= $_[$j] if defined $_[$j]; }
169 $offset = $s->{'START'} + $s->{'LEN'} - $start;
171 } else # new seg please
173 if ($s->{'START'} > $start + $#_ + 1)
175 $i += $self->fastadd_segment($start + $offset, 1, @_[$offset .. $#_]) - 1;
180 $i += $self->fastadd_segment($start + $offset, 1, @_[$offset .. $s->{'START'} - $start]) - 1;
181 $offset = $s->{'START'} - $start + 1;
187 $seg->{'START'} = $start + $offset;
188 $seg->{'LEN'} = $#_ - $offset + 1;
189 $seg->{'VAL'} = [@_[$offset .. $#_]];
198 Merges any immediately adjacent segments
207 for ($i = 1; $i <= $#$self; $i++)
209 $sl = $self->[$i - 1];
211 if ($s->{'START'} == $sl->{'START'} + $sl->{'LEN'})
213 $sl->{'LEN'} += $s->{'LEN'};
214 push (@{$sl->{'VAL'}}, @{$s->{'VAL'}});
215 splice(@$self, $i, 1);
223 =head2 $s->at($addr, [$len])
225 Looks up the data held at the given address by locating the appropriate segment
226 etc. If $len > 1 then returns an array of values, spaces being filled with undef.
232 my ($self, $addr, $len) = @_;
233 my ($i, $dat, $s, @res, $offset);
235 $len = 1 unless defined $len;
237 for ($i = 0; $i <= $#$self; $i++)
240 next if ($s->{'START'} + $s->{'LEN'} < $addr + $offset); # only fires on $offset == 0
241 if ($s->{'START'} > $addr + $offset)
243 push (@res, (undef) x ($s->{'START'} > $addr + $len ?
244 $len - $offset : $s->{'START'} - $addr - $offset));
245 $offset = $s->{'START'} - $addr;
247 last if ($s->{'START'} >= $addr + $len);
249 if ($s->{'START'} + $s->{'LEN'} >= $addr + $len)
251 push (@res, @{$s->{'VAL'}}[$addr + $offset - $s->{'START'} ..
252 $addr + $len - $s->{'START'} - 1]);
257 push (@res, @{$s->{'VAL'}}[$addr + $offset - $s->{'START'} .. $s->{'LEN'} - 1]);
258 $offset = $s->{'START'} + $s->{'LEN'} - $addr;
261 push (@res, (undef) x ($len - $offset)) if ($offset < $len);
262 return wantarray ? @res : $res[0];
266 =head2 $s->remove($addr, [$len])
268 Removes the item or items from addr returning them as an array or the first
269 value in a scalar context. This is very like C<at>, including padding with
270 undef, but it deletes stuff as it goes.
276 my ($self, $addr, $len) = @_;
277 my ($i, $dat, $s, @res, $offset);
279 $len = 1 unless defined $len;
281 for ($i = 0; $i <= $#$self; $i++)
284 next if ($s->{'START'} + $s->{'LEN'} < $addr + $offset);
285 if ($s->{'START'} > $addr + $offset)
287 push (@res, (undef) x ($s->{'START'} > $addr + $len ?
288 $len - $offset : $s->{'START'} - $addr - $offset));
289 $offset = $s->{'START'} - $addr;
291 last if ($s->{'START'} >= $addr + $len);
293 unless ($s->{'START'} == $addr + $offset)
297 $seg->{'START'} = $s->{'START'};
298 $seg->{'LEN'} = $addr + $offset - $s->{'START'};
299 $seg->{'VAL'} = [splice(@{$s->{'VAL'}}, 0, $addr + $offset - $s->{'START'})];
300 $s->{'LEN'} -= $addr + $offset - $s->{'START'};
301 $s->{'START'} = $addr + $offset;
303 splice(@$self, $i, 0, $seg);
307 if ($s->{'START'} + $s->{'LEN'} >= $addr + $len)
309 push (@res, splice(@{$s->{'VAL'}}, 0, $len - $offset));
310 $s->{'LEN'} -= $len - $offset;
311 $s->{'START'} += $len - $offset;
316 push (@res, @{$s->{'VAL'}});
317 $offset = $s->{'START'} + $s->{'LEN'} - $addr;
318 splice(@$self, $i, 0);
322 push (@res, (undef) x ($len - $offset)) if ($offset < $len);
323 return wantarray ? @res : $res[0];
329 Deep copies this array
340 { push (@$res, $self->copy_seg($p)); }
345 =head2 $s->copy_seg($seg)
347 Creates a deep copy of a segment
353 my ($self, $seg) = @_;
357 $res->{'VAL'} = [@{$seg->{'VAL'}}];
358 foreach $p (keys %$seg)
359 { $res->{$p} = $seg->{$p} unless defined $res->{$p}; }
372 Martin Hosken Martin_Hosken@sil.org. See L<Font::TTF::Font> for copyright and