| 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: Utils.pm,v 2.0 2005/11/16 02:16:00 areibens Exp $ | ||
| 25 | # | ||
| 26 | #======================================================================= | ||
| 27 | package PDF::API3::Compat::API2::Basic::TTF::Utils; | ||
| 28 | |||
| 29 | =head1 NAME | ||
| 30 | |||
| 31 | PDF::API3::Compat::API2::Basic::TTF::Utils - Utility functions to save fingers | ||
| 32 | |||
| 33 | =head1 DESCRIPTION | ||
| 34 | |||
| 35 | Lots of useful functions to save my fingers, especially for trivial tables | ||
| 36 | |||
| 37 | =head1 FUNCTIONS | ||
| 38 | |||
| 39 | The following functions are exported | ||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | use strict; | ||
| 44 | use vars qw(@ISA @EXPORT $VERSION @EXPORT_OK); | ||
| 45 | require Exporter; | ||
| 46 | |||
| 47 | @ISA = qw(Exporter); | ||
| 48 | @EXPORT = qw(TTF_Init_Fields TTF_Read_Fields TTF_Out_Fields TTF_Pack | ||
| 49 | TTF_Unpack TTF_word_utf8 TTF_utf8_word TTF_bininfo); | ||
| 50 | @EXPORT_OK = (@EXPORT, qw(XML_hexdump)); | ||
| 51 | $VERSION = 0.0001; | ||
| 52 | |||
| 53 | =head2 ($val, $pos) = TTF_Init_Fields ($str, $pos) | ||
| 54 | |||
| 55 | Given a field description from the C<DATA> section, creates an absolute entry | ||
| 56 | in the fields associative array for the class | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | sub TTF_Init_Fields | ||
| 61 | { | ||
| 62 | my ($str, $pos, $inval) = @_; | ||
| 63 | my ($key, $val, $res, $len, $rel); | ||
| 64 | |||
| 65 | $str =~ s/\r?\n$//o; | ||
| 66 | if ($inval) | ||
| 67 | { ($key, $val) = ($str, $inval); } | ||
| 68 | else | ||
| 69 | { ($key, $val) = split(',\s*', $str); } | ||
| 70 | return (undef, undef, 0) unless (defined $key && $key ne ""); | ||
| 71 | if ($val =~ m/^(\+?)(\d*)(\D+)(\d*)/oi) | ||
| 72 | { | ||
| 73 | $rel = $1; | ||
| 74 | if ($rel eq "+") | ||
| 75 | { $pos += $2; } | ||
| 76 | elsif ($2 ne "") | ||
| 77 | { $pos = $2; } | ||
| 78 | $val = $3; | ||
| 79 | $len = $4; | ||
| 80 | } | ||
| 81 | $len = "" unless defined $len; | ||
| 82 | $pos = 0 if !defined $pos || $pos eq ""; | ||
| 83 | $res = "$pos:$val:$len"; | ||
| 84 | if ($val eq "f" || $val =~ m/^[l]/oi) | ||
| 85 | { $pos += 4 * ($len ne "" ? $len : 1); } | ||
| 86 | elsif ($val eq "F" || $val =~ m/^[s]/oi) | ||
| 87 | { $pos += 2 * ($len ne "" ? $len : 1); } | ||
| 88 | else | ||
| 89 | { $pos += 1 * ($len ne "" ? $len : 1); } | ||
| 90 | |||
| 91 | ($key, $res, $pos); | ||
| 92 | } | ||
| 93 | |||
| 94 | |||
| 95 | =head2 TTF_Read_Fields($obj, $dat, $fields) | ||
| 96 | |||
| 97 | Given a block of data large enough to account for all the fields in a table, | ||
| 98 | processes the data block to convert to the values in the objects instance | ||
| 99 | variables by name based on the list in the C<DATA> block which has been run | ||
| 100 | through C<TTF_Init_Fields> | ||
| 101 | |||
| 102 | =cut | ||
| 103 | |||
| 104 | sub TTF_Read_Fields | ||
| 105 | { | ||
| 106 | my ($self, $dat, $fields) = @_; | ||
| 107 | my ($pos, $type, $res, $f, $arrlen, $arr, $frac); | ||
| 108 | |||
| 109 | foreach $f (keys %{$fields}) | ||
| 110 | { | ||
| 111 | ($pos, $type, $arrlen) = split(':', $fields->{$f}); | ||
| 112 | $pos = 0 if $pos eq ""; | ||
| 113 | if ($arrlen ne "") | ||
| 114 | { $self->{$f} = [TTF_Unpack("$type$arrlen", substr($dat, $pos))]; } | ||
| 115 | else | ||
| 116 | { $self->{$f} = TTF_Unpack("$type", substr($dat, $pos)); } | ||
| 117 | } | ||
| 118 | $self; | ||
| 119 | } | ||
| 120 | |||
| 121 | |||
| 122 | =head2 TTF_Unpack($fmt, $dat) | ||
| 123 | |||
| 124 | A TrueType types equivalent of Perls C<unpack> function. Thus $fmt consists of | ||
| 125 | type followed by an optional number of elements to read including *. The type | ||
| 126 | may be one of: | ||
| 127 | |||
| 128 | c BYTE | ||
| 129 | C CHAR | ||
| 130 | f FIXED | ||
| 131 | F F2DOT14 | ||
| 132 | l LONG | ||
| 133 | L ULONG | ||
| 134 | s SHORT | ||
| 135 | S USHORT | ||
| 136 | |||
| 137 | Note that C<FUNIT>, C<FWORD> and C<UFWORD> are not data types but units. | ||
| 138 | |||
| 139 | Returns array of scalar (first element) depending on context | ||
| 140 | |||
| 141 | =cut | ||
| 142 | |||
| 143 | sub TTF_Unpack | ||
| 144 | { | ||
| 145 | my ($fmt, $dat) = @_; | ||
| 146 | my ($res, $frac, $i, $arrlen, $type, @res); | ||
| 147 | |||
| 148 | while ($fmt =~ s/^([cfls])(\d+|\*)?//oi) | ||
| 149 | { | ||
| 150 | $type = $1; | ||
| 151 | $arrlen = $2; | ||
| 152 | $arrlen = 1 if !defined $arrlen || $arrlen eq ""; | ||
| 153 | $arrlen = -1 if $arrlen eq "*"; | ||
| 154 | |||
| 155 | for ($i = 0; ($arrlen == -1 && $dat ne "") || $i < $arrlen; $i++) | ||
| 156 | { | ||
| 157 | if ($type eq "f") | ||
| 158 | { | ||
| 159 | ($res, $frac) = unpack("nn", $dat); | ||
| 160 | substr($dat, 0, 4) = ""; | ||
| 161 | $res -= 65536 if $res > 32767; | ||
| 162 | $res += $frac / 65536.; | ||
| 163 | } | ||
| 164 | elsif ($type eq "F") | ||
| 165 | { | ||
| 166 | $res = unpack("n", $dat); | ||
| 167 | substr($dat, 0, 2) = ""; | ||
| 168 | # $res -= 65536 if $res >= 32768; | ||
| 169 | $frac = $res & 0x3fff; | ||
| 170 | $res >>= 14; | ||
| 171 | $res -= 4 if $res > 1; | ||
| 172 | # $frac -= 16384 if $frac > 8191; | ||
| 173 | $res += $frac / 16384.; | ||
| 174 | } | ||
| 175 | elsif ($type =~ m/^[l]/oi) | ||
| 176 | { | ||
| 177 | $res = unpack("N", $dat); | ||
| 178 | substr($dat, 0, 4) = ""; | ||
| 179 | $res -= (1 << 32) if ($type eq "l" && $res >= 1 << 31); | ||
| 180 | } | ||
| 181 | elsif ($type =~ m/^[s]/oi) | ||
| 182 | { | ||
| 183 | $res = unpack("n", $dat); | ||
| 184 | substr($dat, 0, 2) = ""; | ||
| 185 | $res -= 65536 if ($type eq "s" && $res >= 32768); | ||
| 186 | } | ||
| 187 | elsif ($type eq "c") | ||
| 188 | { | ||
| 189 | $res = unpack("c", $dat); | ||
| 190 | substr($dat, 0, 1) = ""; | ||
| 191 | } | ||
| 192 | else | ||
| 193 | { | ||
| 194 | $res = unpack("C", $dat); | ||
| 195 | substr($dat, 0, 1) = ""; | ||
| 196 | } | ||
| 197 | push (@res, $res); | ||
| 198 | } | ||
| 199 | } | ||
| 200 | return wantarray ? @res : $res[0]; | ||
| 201 | } | ||
| 202 | |||
| 203 | |||
| 204 | =head2 $dat = TTF_Out_Fields($obj, $fields, $len) | ||
| 205 | |||
| 206 | Given the fields table from C<TTF_Init_Fields> writes out the instance variables from | ||
| 207 | the object to the filehandle in TTF binary form. | ||
| 208 | |||
| 209 | =cut | ||
| 210 | |||
| 211 | sub TTF_Out_Fields | ||
| 212 | { | ||
| 213 | my ($obj, $fields, $len) = @_; | ||
| 214 | my ($dat) = "\000" x $len; | ||
| 215 | my ($f, $pos, $type, $res, $arr, $arrlen, $frac); | ||
| 216 | |||
| 217 | foreach $f (keys %{$fields}) | ||
| 218 | { | ||
| 219 | ($pos, $type, $arrlen) = split(':', $fields->{$f}); | ||
| 220 | if ($arrlen ne "") | ||
| 221 | { $res = TTF_Pack("$type$arrlen", @{$obj->{$f}}); } | ||
| 222 | else | ||
| 223 | { $res = TTF_Pack("$type", $obj->{$f}); } | ||
| 224 | substr($dat, $pos, length($res)) = $res; | ||
| 225 | } | ||
| 226 | $dat; | ||
| 227 | } | ||
| 228 | |||
| 229 | |||
| 230 | =head2 $dat = TTF_Pack($fmt, @data) | ||
| 231 | |||
| 232 | The TrueType equivalent to Perl's C<pack> function. See details of C<TTF_Unpack> | ||
| 233 | for how to work the $fmt string. | ||
| 234 | |||
| 235 | =cut | ||
| 236 | |||
| 237 | sub TTF_Pack | ||
| 238 | { | ||
| 239 | my ($fmt, @obj) = @_; | ||
| 240 | my ($type, $i, $arrlen, $dat, $res, $frac); | ||
| 241 | |||
| 242 | while ($fmt =~ s/^([flsc])(\d+|\*)?//oi) | ||
| 243 | { | ||
| 244 | $type = $1; | ||
| 245 | $arrlen = $2 || ""; | ||
| 246 | $arrlen = $#obj + 1 if $arrlen eq "*"; | ||
| 247 | $arrlen = 1 if $arrlen eq ""; | ||
| 248 | |||
| 249 | for ($i = 0; $i < $arrlen; $i++) | ||
| 250 | { | ||
| 251 | $res = shift(@obj); | ||
| 252 | if ($type eq "f") | ||
| 253 | { | ||
| 254 | $frac = int(($res - int($res)) * 65536); | ||
| 255 | $res = (int($res) << 16) + $frac; | ||
| 256 | $dat .= pack("N", $res); | ||
| 257 | } | ||
| 258 | elsif ($type eq "F") | ||
| 259 | { | ||
| 260 | $frac = int(($res - int($res)) * 16384); | ||
| 261 | $res = (int($res) << 14) + $frac; | ||
| 262 | $dat .= pack("n", $res); | ||
| 263 | } | ||
| 264 | elsif ($type =~ m/^[l]/oi) | ||
| 265 | { | ||
| 266 | $res += 1 << 32 if ($type eq 'L' && $res < 0); | ||
| 267 | $dat .= pack("N", $res); | ||
| 268 | } | ||
| 269 | elsif ($type =~ m/^[s]/oi) | ||
| 270 | { | ||
| 271 | $res += 1 << 16 if ($type eq 'S' && $res < 0); | ||
| 272 | $dat .= pack("n", $res); | ||
| 273 | } | ||
| 274 | elsif ($type eq "c") | ||
| 275 | { $dat .= pack("c", $res); } | ||
| 276 | else | ||
| 277 | { $dat .= pack("C", $res); } | ||
| 278 | } | ||
| 279 | } | ||
| 280 | $dat; | ||
| 281 | } | ||
| 282 | |||
| 283 | |||
| 284 | =head2 ($num, $range, $select, $shift) = TTF_bininfo($num) | ||
| 285 | |||
| 286 | Calculates binary search information from a number of elements | ||
| 287 | |||
| 288 | =cut | ||
| 289 | |||
| 290 | sub TTF_bininfo | ||
| 291 | { | ||
| 292 | my ($num, $block) = @_; | ||
| 293 | my ($range, $select, $shift); | ||
| 294 | |||
| 295 | $range = 1; | ||
| 296 | for ($select = 0; $range <= $num; $select++) | ||
| 297 | { $range *= 2; } | ||
| 298 | $select--; $range /= 2; | ||
| 299 | $range *= $block; | ||
| 300 | |||
| 301 | $shift = $num * $block - $range; | ||
| 302 | ($num, $range, $select, $shift); | ||
| 303 | } | ||
| 304 | |||
| 305 | |||
| 306 | =head2 TTF_word_utf8($str) | ||
| 307 | |||
| 308 | Returns the UTF8 form of the 16 bit string, assumed to be in big endian order, | ||
| 309 | including surrogate handling | ||
| 310 | |||
| 311 | =cut | ||
| 312 | |||
| 313 | sub TTF_word_utf8 | ||
| 314 | { | ||
| 315 | my ($str) = @_; | ||
| 316 | my ($res, $i); | ||
| 317 | my (@dat) = unpack("n*", $str); | ||
| 318 | |||
| 319 | return pack("U*", @dat) if ($^V && $^V ge v5.6.0); | ||
| 320 | for ($i = 0; $i <= $#dat; $i++) | ||
| 321 | { | ||
| 322 | my ($dat) = $dat[$i]; | ||
| 323 | if ($dat < 0x80) # Thanks to Gisle Aas for some of his old code | ||
| 324 | { $res .= chr($dat); } | ||
| 325 | elsif ($dat < 0x800) | ||
| 326 | { $res .= chr(0xC0 | ($dat >> 6)) . chr(0x80 | ($dat & 0x3F)); } | ||
| 327 | elsif ($dat >= 0xD800 && $dat < 0xDC00) | ||
| 328 | { | ||
| 329 | my ($dat1) = $dat[++$i]; | ||
| 330 | my ($top) = (($dat & 0x3C0) >> 6) + 1; | ||
| 331 | $res .= chr(0xF0 | ($top >> 2)) | ||
| 332 | . chr(0x80 | (($top & 1) << 4) | (($dat & 0x3C) >> 2)) | ||
| 333 | . chr(0x80 | (($dat & 0x3) << 4) | (($dat1 & 0x3C0) >> 6)) | ||
| 334 | . chr(0x80 | ($dat1 & 0x3F)); | ||
| 335 | } else | ||
| 336 | { $res .= chr(0xE0 | ($dat >> 12)) . chr(0x80 | (($dat >> 6) & 0x3F)) | ||
| 337 | . chr(0x80 | ($dat & 0x3F)); } | ||
| 338 | } | ||
| 339 | $res; | ||
| 340 | } | ||
| 341 | |||
| 342 | |||
| 343 | =head2 TTF_utf8_word($str) | ||
| 344 | |||
| 345 | Returns the 16-bit form in big endian order of the UTF 8 string, including | ||
| 346 | surrogate handling to Unicode. | ||
| 347 | |||
| 348 | =cut | ||
| 349 | |||
| 350 | sub TTF_utf8_word | ||
| 351 | { | ||
| 352 | my ($str) = @_; | ||
| 353 | my ($res); | ||
| 354 | |||
| 355 | return pack("n*", unpack("U*", $str)) if ($^V ge v5.6.0); | ||
| 356 | $str = "$str"; # copy $str | ||
| 357 | while (length($str)) # Thanks to Gisle Aas for some of his old code | ||
| 358 | { | ||
| 359 | $str =~ s/^[\x80-\xBF]+//o; | ||
| 360 | if ($str =~ s/^([\x00-\x7F]+)//o) | ||
| 361 | { $res .= pack("n*", unpack("C*", $1)); } | ||
| 362 | elsif ($str =~ s/^([\xC0-\xDF])([\x80-\xBF])//o) | ||
| 363 | { $res .= pack("n", ((ord($1) & 0x1F) << 6) | (ord($2) & 0x3F)); } | ||
| 364 | elsif ($str =~ s/^([\0xE0-\xEF])([\x80-\xBF])([\x80-\xBF])//o) | ||
| 365 | { $res .= pack("n", ((ord($1) & 0x0F) << 12) | ||
| 366 | | ((ord($2) & 0x3F) << 6) | ||
| 367 | | (ord($3) & 0x3F)); } | ||
| 368 | elsif ($str =~ s/^([\xF0-\xF7])([\x80-\xBF])([\x80-\xBF])([\x80-\xBF])//o) | ||
| 369 | { | ||
| 370 | my ($b1, $b2, $b3, $b4) = (ord($1), ord($2), ord($3), ord($4)); | ||
| 371 | $res .= pack("n", ((($b1 & 0x07) << 8) | (($b2 & 0x3F) << 2) | ||
| 372 | | (($b3 & 0x30) >> 4)) + 0xD600); # account for offset | ||
| 373 | $res .= pack("n", ((($b3 & 0x0F) << 6) | ($b4 & 0x3F)) + 0xDC00); | ||
| 374 | } | ||
| 375 | elsif ($str =~ s/^[\xF8-\xFF][\x80-\xBF]*//o) | ||
| 376 | { } | ||
| 377 | } | ||
| 378 | $res; | ||
| 379 | } | ||
| 380 | |||
| 381 | |||
| 382 | =head2 XML_hexdump($context, $dat) | ||
| 383 | |||
| 384 | Dumps out the given data as a sequence of <data> blocks each 16 bytes wide | ||
| 385 | |||
| 386 | =cut | ||
| 387 | |||
| 388 | sub XML_hexdump | ||
| 389 | { | ||
| 390 | my ($context, $depth, $dat) = @_; | ||
| 391 | my ($fh) = $context->{'fh'}; | ||
| 392 | my ($i, $len, $out); | ||
| 393 | |||
| 394 | $len = length($dat); | ||
| 395 | for ($i = 0; $i < $len; $i += 16) | ||
| 396 | { | ||
| 397 | $out = join(' ', map {sprintf("%02X", ord($_))} (split('', substr($dat, $i, 16)))); | ||
| 398 | $fh->printf("%s<data addr='%04X'>%s</data>\n", $depth, $i, $out); | ||
| 399 | } | ||
| 400 | } | ||
| 401 | |||
| 402 | |||
| 403 | =head2 XML_outhints | ||
| 404 | |||
| 405 | Converts a binary string of hinting code into a textual representation | ||
| 406 | |||
| 407 | =cut | ||
| 408 | |||
| 409 | { | ||
| 410 | my (@hints) = ( | ||
| 411 | ['SVTCA[0]'], ['SVTCA[1]'], ['SPVTCA[0]'], ['SPVTCA[1]'], ['SFVTCA[0]'], ['SFVTCA[1]'], ['SPVTL[0]'], ['SPVTL[1]'], | ||
| 412 | ['SFVTL[0]'], ['SFVTL[1]'], ['SPVFS'], ['SFVFS'], ['GPV'], ['GFV'], ['SVFTPV'], ['ISECT'], | ||
| 413 | # 10 | ||
| 414 | ['SRP0'], ['SRP1'], ['SRP2'], ['SZP0'], ['SZP1'], ['SZP2'], ['SZPS'], ['SLOOP'], | ||
| 415 | ['RTG'], ['RTHG'], ['SMD'], ['ELSE'], ['JMPR'], ['SCVTCI'], ['SSWCI'], ['SSW'], | ||
| 416 | # 20 | ||
| 417 | ['DUP'], ['POP'], ['CLEAR'], ['SWAP'], ['DEPTH'], ['CINDEX'], ['MINDEX'], ['ALIGNPTS'], | ||
| 418 | [], ['UTP'], ['LOOPCALL'], ['CALL'], ['FDEF'], ['ENDF'], ['MDAP[0]'], ['MDAP[1]'], | ||
| 419 | # 30 | ||
| 420 | ['IUP[0]'], ['IUP[1]'], ['SHP[0]'], ['SHP[1]'], ['SHC[0]'], ['SHC[1]'], ['SHZ[0]'], ['SHZ[1]'], | ||
| 421 | ['SHPIX'], ['IP'], ['MSIRP[0]'], ['MSIRP[1]'], ['ALIGNRP'], ['RTDG'], ['MIAP[0]'], ['MIAP[1]'], | ||
| 422 | # 40 | ||
| 423 | ['NPUSHB', -1, 1], ['NPUSHW', -1, 2], ['WS', 0, 0], ['RS', 0, 0], ['WCVTP', 0, 0], ['RCVT', 0, 0], ['GC[0]'], ['GC[1]'], | ||
| 424 | ['SCFS'], ['MD[0]'], ['MD[1]'], ['MPPEM'], ['MPS'], ['FLIPON'], ['FLIPOFF'], ['DEBUG'], | ||
| 425 | # 50 | ||
| 426 | ['LT'], ['LTEQ'], ['GT'], ['GTEQ'], ['EQ'], ['NEQ'], ['ODD'], ['EVEN'], | ||
| 427 | ['IF'], ['EIF'], ['AND'], ['OR'], ['NOT'], ['DELTAP1'], ['SDB'], ['SDS'], | ||
| 428 | # 60 | ||
| 429 | ['ADD'], ['SUB'], ['DIV'], ['MULT'], ['ABS'], ['NEG'], ['FLOOR'], ['CEILING'], | ||
| 430 | ['ROUND[0]'], ['ROUND[1]'], ['ROUND[2]'], ['ROUND[3]'], ['NROUND[0]'], ['NROUND[1]'], ['NROUND[2]'], ['NROUND[3]'], | ||
| 431 | # 70 | ||
| 432 | ['WCVTF'], ['DELTAP2'], ['DELTAP3'], ['DELTAC1'], ['DELTAC2'], ['DELTAC3'], ['SROUND'], ['S45ROUND'], | ||
| 433 | ['JROT'], ['JROF'], ['ROFF'], [], ['RUTG'], ['RDTG'], ['SANGW'], [], | ||
| 434 | # 80 | ||
| 435 | ['FLIPPT'], ['FLIPRGON'], ['FLIPRGOFF'], [], [], ['SCANCTRL'], ['SDPVTL[0]'], ['SDPVTL[1]'], | ||
| 436 | ['GETINFO'], ['IDEF'], ['ROLL'], ['MAX'], ['MIN'], ['SCANTYPE'], ['INSTCTRL'], [], | ||
| 437 | # 90 | ||
| 438 | [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], | ||
| 439 | # A0 | ||
| 440 | [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], | ||
| 441 | # B0 | ||
| 442 | ['PUSHB1', 1, 1], ['PUSHB2', 2, 1], ['PUSHB3', 3, 1], ['PUSHB4', 4, 1], ['PUSHB5', 5, 1], ['PUSHB6', 6, 1], ['PUSHB7', 7, 1], ['PUSHB8', 8, 1], | ||
| 443 | ['PUSHW1', 1, 2], ['PUSHW2', 2, 2], ['PUSHW3', 3, 2], ['PUSHW4', 4, 2], ['PUSHW5', 5, 2], ['PUSHW6', 6, 2], ['PUSHW7', 7, 2], ['PUSHW8', 8, 2], | ||
| 444 | # C0 | ||
| 445 | ['MDRP[0]'], ['MDRP[1]'], ['MDRP[2]'], ['MDRP[3]'], ['MDRP[4]'], ['MDRP[5]'], ['MDRP[6]'], ['MDRP[7]'], | ||
| 446 | ['MDRP[8]'], ['MDRP[9]'], ['MDRP[A]'], ['MDRP[B]'], ['MDRP[C]'], ['MDRP[D]'], ['MDRP[E]'], ['MDRP[F]'], | ||
| 447 | # D0 | ||
| 448 | ['MDRP[10]'], ['MDRP[11]'], ['MDRP[12]'], ['MDRP[13]'], ['MDRP[14]'], ['MDRP[15]'], ['MDRP[16]'], ['MDRP[17]'], | ||
| 449 | ['MDRP[18]'], ['MDRP[19]'], ['MDRP[1A]'], ['MDRP[1B]'], ['MDRP[1C]'], ['MDRP[1D]'], ['MDRP[1E]'], ['MDRP[1F]'], | ||
| 450 | # E0 | ||
| 451 | ['MIRP[0]'], ['MIRP[1]'], ['MIRP[2]'], ['MIRP[3]'], ['MIRP[4]'], ['MIRP[5]'], ['MIRP[6]'], ['MIRP[7]'], | ||
| 452 | ['MIRP[8]'], ['MIRP[9]'], ['MIRP[A]'], ['MIRP[B]'], ['MIRP[C]'], ['MIRP[D]'], ['MIRP[E]'], ['MIRP[F]'], | ||
| 453 | # F0 | ||
| 454 | ['MIRP[10]'], ['MIRP[11]'], ['MIRP[12]'], ['MIRP[13]'], ['MIRP[14]'], ['MIRP[15]'], ['MIRP[16]'], ['MIRP[17]'], | ||
| 455 | ['MIRP[18]'], ['MIRP[19]'], ['MIRP[1A]'], ['MIRP[1B]'], ['MIRP[1C]'], ['MIRP[1D]'], ['MIRP[1E]'], ['MIRP[1F]']); | ||
| 456 | |||
| 457 | my ($i); | ||
| 458 | my (%hints) = map { $_->[0] => $i++ if (defined $_->[0]); } @hints; | ||
| 459 | |||
| 460 | sub XML_binhint | ||
| 461 | { | ||
| 462 | my ($dat) = @_; | ||
| 463 | my ($len) = length($dat); | ||
| 464 | my ($res, $i); | ||
| 465 | my ($text, $num, $size); | ||
| 466 | |||
| 467 | for ($i = 0; $i < $len; $i++) | ||
| 468 | { | ||
| 469 | ($text, $num, $size) = @{$hints[ord(substr($dat, $i, 1))]}; | ||
| 470 | $text = sprintf("UNK[%02X]", ord(substr($dat, $i, 1))) unless defined $text; | ||
| 471 | $res .= $text; | ||
| 472 | if ($num != 0) | ||
| 473 | { | ||
| 474 | if ($num < 0) | ||
| 475 | { | ||
| 476 | $i++; | ||
| 477 | my ($nnum) = unpack($num == -1 ? 'C' : 'n', substr($dat, $i, -$num)); | ||
| 478 | $i += -$num - 1; | ||
| 479 | $num = $nnum; | ||
| 480 | } | ||
| 481 | $res .= "\t" . join(' ', unpack($size == 1 ? 'C*' : 'n*', substr($dat, $i + 1, $num * $size))); | ||
| 482 | $i += $num * $size; | ||
| 483 | } | ||
| 484 | $res .= "\n"; | ||
| 485 | } | ||
| 486 | $res; | ||
| 487 | } | ||
| 488 | |||
| 489 | sub XML_hintbin | ||
| 490 | { | ||
| 491 | my ($dat) = @_; | ||
| 492 | my ($l, $res, @words, $num); | ||
| 493 | |||
| 494 | foreach $l (split(/\s*\n\s*/, $dat)) | ||
| 495 | { | ||
| 496 | @words = split(/\s*/, $l); | ||
| 497 | next unless (defined $hints{$words[0]}); | ||
| 498 | $num = $hints{$words[0]}; | ||
| 499 | $res .= pack('C', $num); | ||
| 500 | if ($hints[$num][1] < 0) | ||
| 501 | { | ||
| 502 | $res .= pack($hints[$num][1] == -1 ? 'C' : 'n', $#words); | ||
| 503 | $res .= pack($hints[$num][2] == 1 ? 'C*' : 'n*', @words[1 .. $#words]); | ||
| 504 | } | ||
| 505 | elsif ($hints[$num][1] > 0) | ||
| 506 | { | ||
| 507 | $res .= pack($hints[$num][2] == 1 ? 'C*' : 'n*', @words[1 .. $hints[$num][1]]); | ||
| 508 | } | ||
| 509 | } | ||
| 510 | $res; | ||
| 511 | } | ||
| 512 | } | ||
| 513 | |||
| 514 | |||
| 515 | =head2 make_circle($f, $cmap, [$dia, $sb, $opts]) | ||
| 516 | |||
| 517 | Adds a dotted circle to a font. This function is very configurable. The | ||
| 518 | parameters passed in are: | ||
| 519 | |||
| 520 | =over 4 | ||
| 521 | |||
| 522 | =item $f | ||
| 523 | |||
| 524 | Font to work with. This is required. | ||
| 525 | |||
| 526 | =item $cmap | ||
| 527 | |||
| 528 | A cmap table (not the 'val' sub-element of a cmap) to add the glyph too. Optional. | ||
| 529 | |||
| 530 | =item $dia | ||
| 531 | |||
| 532 | Optional diameter for the main circle. Defaults to 80% em | ||
| 533 | |||
| 534 | =item $sb | ||
| 535 | |||
| 536 | Side bearing. The left and right side-bearings are always the same. This value | ||
| 537 | defaults to 10% em. | ||
| 538 | |||
| 539 | =back | ||
| 540 | |||
| 541 | There are various options to control all sorts of interesting aspects of the circle | ||
| 542 | |||
| 543 | =over 4 | ||
| 544 | |||
| 545 | =item numDots | ||
| 546 | |||
| 547 | Number of dots in the circle | ||
| 548 | |||
| 549 | =item numPoints | ||
| 550 | |||
| 551 | Number of curve points to use to create each dot | ||
| 552 | |||
| 553 | =item uid | ||
| 554 | |||
| 555 | Unicode reference to store this glyph under in the cmap. Defaults to 0x25CC | ||
| 556 | |||
| 557 | =item pname | ||
| 558 | |||
| 559 | Postscript name to give the glyph. Defaults to uni25CC. | ||
| 560 | |||
| 561 | =item -dRadius | ||
| 562 | |||
| 563 | Radius of each dot. | ||
| 564 | |||
| 565 | =back | ||
| 566 | |||
| 567 | =cut | ||
| 568 | |||
| 569 | sub make_circle | ||
| 570 | { | ||
| 571 | my ($font, $cmap, $dia, $sb, %opts) = @_; | ||
| 572 | my ($upem) = $font->{'head'}{'unitsPerEm'}; | ||
| 573 | my ($glyph) = PDF::API3::Compat::API2::Basic::TTF::Glyph->new('PARENT' => $font, 'read' => 2); | ||
| 574 | my ($PI) = 3.1415926535; | ||
| 575 | my ($R, $r, $xorg, $yorg); | ||
| 576 | my ($i, $j, $numg, $maxp); | ||
| 577 | my ($numc) = $opts{'-numDots'} || 16; | ||
| 578 | my ($nump) = ($opts{'-numPoints'} * 2) || 8; | ||
| 579 | my ($uid) = $opts{'-uid'} || 0x25CC; | ||
| 580 | my ($pname) = $opts{'-pname'} || 'uni25CC'; | ||
| 581 | |||
| 582 | $dia ||= $upem * .8; # .95 to fit exactly | ||
| 583 | $sb ||= $upem * .1; | ||
| 584 | $R = $dia / 2; | ||
| 585 | $r = $opts{'-dRadius'} || ($R * .1); | ||
| 586 | ($xorg, $yorg) = ($R + $r, $R); | ||
| 587 | |||
| 588 | $xorg += $sb; | ||
| 589 | $font->{'post'}->read; | ||
| 590 | $font->{'glyf'}->read; | ||
| 591 | for ($i = 0; $i < $numc; $i++) | ||
| 592 | { | ||
| 593 | my ($pxorg, $pyorg) = ($xorg + $R * cos(2 * $PI * $i / $numc), | ||
| 594 | $yorg + $R * sin(2 * $PI * $i / $numc)); | ||
| 595 | for ($j = 0; $j < $nump; $j++) | ||
| 596 | { | ||
| 597 | push (@{$glyph->{'x'}}, int ($pxorg + ($j & 1 ? 1/cos(2*$PI/$nump) : 1) * $r * cos(2 * $PI * $j / $nump))); | ||
| 598 | push (@{$glyph->{'y'}}, int ($pyorg + ($j & 1 ? 1/cos(2*$PI/$nump) : 1) * $r * sin(2 * $PI * $j / $nump))); | ||
| 599 | push (@{$glyph->{'flags'}}, $j & 1 ? 0 : 1); | ||
| 600 | } | ||
| 601 | push (@{$glyph->{'endPoints'}}, $#{$glyph->{'x'}}); | ||
| 602 | } | ||
| 603 | $glyph->{'numberOfContours'} = $#{$glyph->{'endPoints'}} + 1; | ||
| 604 | $glyph->{'numPoints'} = $#{$glyph->{'x'}} + 1; | ||
| 605 | $glyph->update; | ||
| 606 | $numg = $font->{'maxp'}{'numGlyphs'}; | ||
| 607 | $font->{'maxp'}->read->{'numGlyphs'}++; | ||
| 608 | |||
| 609 | $font->{'hmtx'}{'advance'}[$numg] = int($xorg + $R + $r + $sb + .5); | ||
| 610 | $font->{'hmtx'}{'lsb'}[$numg] = int($xorg - $R - $r + .5); | ||
| 611 | $font->{'loca'}{'glyphs'}[$numg] = $glyph; | ||
| 612 | $cmap->{'val'}{$uid} = $numg if ($cmap); | ||
| 613 | $font->{'post'}{'VAL'}[$numg] = $pname; | ||
| 614 | delete $font->{'hdmx'}; | ||
| 615 | delete $font->{'VDMX'}; | ||
| 616 | delete $font->{'LTSH'}; | ||
| 617 | |||
| 618 | $font->tables_do(sub {$_[0]->dirty;}); | ||
| 619 | $font->update; | ||
| 620 | return ($numg - 1); | ||
| 621 | } | ||
| 622 | |||
| 623 | |||
| 624 | 1; | ||
| 625 | |||
| 626 | =head1 BUGS | ||
| 627 | |||
| 628 | No known bugs | ||
| 629 | |||
| 630 | =head1 AUTHOR | ||
| 631 | |||
| 632 | Martin Hosken Martin_Hosken@sil.org. See L<PDF::API3::Compat::API2::Basic::TTF::Font> for copyright and | ||
| 633 | licensing. | ||
| 634 | |||
| 635 | =cut | ||
| 636 |