| 1 | package Font::TTF::Delta; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Font::TTF::Delta - Opentype Device tables | ||
| 6 | |||
| 7 | =head1 DESCRIPTION | ||
| 8 | |||
| 9 | Each device table corresponds to a set of deltas for a particular point over | ||
| 10 | a range of ppem values. | ||
| 11 | |||
| 12 | =over | ||
| 13 | |||
| 14 | =item first | ||
| 15 | |||
| 16 | The first ppem value in the range | ||
| 17 | |||
| 18 | =item last | ||
| 19 | |||
| 20 | The last ppem value in the range | ||
| 21 | |||
| 22 | =item val | ||
| 23 | |||
| 24 | This is an array of deltas corresponding to each ppem in the range between | ||
| 25 | first and last inclusive. | ||
| 26 | |||
| 27 | =item fmt | ||
| 28 | |||
| 29 | This is the fmt used (log2 of number bits per value) when the device table was | ||
| 30 | read. It is recalculated on output. | ||
| 31 | |||
| 32 | =back | ||
| 33 | |||
| 34 | =head1 METHODS | ||
| 35 | |||
| 36 | =cut | ||
| 37 | |||
| 38 | use strict; | ||
| 39 | use Font::TTF::Utils; | ||
| 40 | |||
| 41 | =head2 new | ||
| 42 | |||
| 43 | Creates a new device table | ||
| 44 | |||
| 45 | =cut | ||
| 46 | |||
| 47 | sub new | ||
| 48 | { | ||
| 49 | my ($class) = @_; | ||
| 50 | my ($self) = {}; | ||
| 51 | |||
| 52 | bless $self, $class; | ||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 56 | =head2 read | ||
| 57 | |||
| 58 | Reads a device table from the given IO object at the current location | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | sub read | ||
| 63 | { | ||
| 64 | my ($self, $fh) = @_; | ||
| 65 | my ($dat, $fmt, $num, $i, $j, $mask); | ||
| 66 | |||
| 67 | $fh->read($dat, 6); | ||
| 68 | ($self->{'first'}, $self->{'last'}, $fmt) = TTF_Unpack("S3", $dat); | ||
| 69 | $self->{'fmt'} = $fmt; | ||
| 70 | |||
| 71 | $fmt = 1 << $fmt; | ||
| 72 | $num = ((($self->{'last'} - $self->{'first'} + 1) * $fmt) + 15) >> 8; | ||
| 73 | $fh->read($dat, $num); | ||
| 74 | |||
| 75 | $mask = (0xffff << (16 - $fmt)) & 0xffff; | ||
| 76 | $j = 0; | ||
| 77 | for ($i = $self->{'first'}; $i <= $self->{'last'}; $i++) | ||
| 78 | { | ||
| 79 | if ($j == 0) | ||
| 80 | { | ||
| 81 | $num = TTF_Unpack("S", substr($dat, 0, 2)); | ||
| 82 | substr($dat, 0, 2) = ''; | ||
| 83 | } | ||
| 84 | push (@{$self->{'val'}}, ($num & $mask) >> (16 - $fmt)); | ||
| 85 | $num <<= $fmt; | ||
| 86 | $j += $fmt; | ||
| 87 | $j = 0 if ($j >= 16); | ||
| 88 | } | ||
| 89 | $self; | ||
| 90 | } | ||
| 91 | |||
| 92 | |||
| 93 | =head2 out($fh, $style) | ||
| 94 | |||
| 95 | Outputs a device table to the given IO object at the current location, or just | ||
| 96 | returns the data to be output if $style != 0 | ||
| 97 | |||
| 98 | =cut | ||
| 99 | |||
| 100 | sub out | ||
| 101 | { | ||
| 102 | my ($self, $fh, $style) = @_; | ||
| 103 | my ($dat, $fmt, $num, $mask, $j, $f, $out); | ||
| 104 | |||
| 105 | foreach $f (@{$self->{'val'}}) | ||
| 106 | { | ||
| 107 | my ($tfmt) = $f > 0 ? $f + 1 : -$f; | ||
| 108 | $fmt = $tfmt if $tfmt > $fmt; | ||
| 109 | } | ||
| 110 | |||
| 111 | if ($fmt > 8) | ||
| 112 | { $fmt = 3; } | ||
| 113 | elsif ($fmt > 2) | ||
| 114 | { $fmt = 2; } | ||
| 115 | else | ||
| 116 | { $fmt = 1; } | ||
| 117 | |||
| 118 | $out = TTF_Pack("S3", $self->{'first'}, $self->{'last'}, $fmt); | ||
| 119 | |||
| 120 | $fmt = 1 << $fmt; | ||
| 121 | $mask = 0xffff >> (16 - $fmt); | ||
| 122 | $j = 0; $dat = 0; | ||
| 123 | foreach $f (@{$self->{'val'}}) | ||
| 124 | { | ||
| 125 | $dat |= ($f & $mask) << (16 - $fmt - $j); | ||
| 126 | $j += $fmt; | ||
| 127 | if ($j >= 16) | ||
| 128 | { | ||
| 129 | $j = 0; | ||
| 130 | $out .= TTF_Pack("S", $dat); | ||
| 131 | $dat = 0; | ||
| 132 | } | ||
| 133 | } | ||
| 134 | $out .= pack('n', $dat) if ($j > 0); | ||
| 135 | $fh->print($out) unless $style; | ||
| 136 | $out; | ||
| 137 | } | ||
| 138 | |||
| 139 | =head2 $d->signature() | ||
| 140 | |||
| 141 | Returns a content based identifying string for this delta for | ||
| 142 | compression purposes | ||
| 143 | |||
| 144 | =cut | ||
| 145 | |||
| 146 | sub signature | ||
| 147 | { | ||
| 148 | my ($self) = @_; | ||
| 149 | return join (",", $self->{'first'}, $self->{'last'}, @{$self->{'val'}}); | ||
| 150 | } | ||
| 151 | |||
| 152 | |||
| 153 | =head2 $d->out_xml($context) | ||
| 154 | |||
| 155 | Outputs a delta in XML | ||
| 156 | |||
| 157 | =cut | ||
| 158 | |||
| 159 | sub out_xml | ||
| 160 | { | ||
| 161 | my ($self, $context, $depth) = @_; | ||
| 162 | my ($fh) = $context->{'fh'}; | ||
| 163 | |||
| 164 | $fh->printf("%s<delta first='%s' last='%s'>\n", $depth, $self->{'first'}, $self->{'last'}); | ||
| 165 | $fh->print("$depth$context->{'indent'}" . join (' ', @{$self->{'val'}}) . "\n") if defined ($self->{'val'}); | ||
| 166 | $fh->print("$depth</delta>\n"); | ||
| 167 | } | ||
| 168 | |||
| 169 | 1; | ||
| 170 | |||
| 171 | =head1 AUTHOR | ||
| 172 | |||
| 173 | Martin Hosken L<http://scripts.sil.org/FontUtils>. | ||
| 174 | |||
| 175 | |||
| 176 | =head1 LICENSING | ||
| 177 | |||
| 178 | Copyright (c) 1998-2016, SIL International (http://www.sil.org) | ||
| 179 | |||
| 180 | This module is released under the terms of the Artistic License 2.0. | ||
| 181 | For details, see the full text of the license in the file LICENSE. | ||
| 182 | |||
| 183 | |||
| 184 | |||
| 185 | =cut | ||
| 186 |