| 1 |
/* |
| 2 |
* pklib.c |
| 3 |
* |
| 4 |
* This file is part of the ttf2pk package. |
| 5 |
* |
| 6 |
* Copyright 1997-1999, 2000 by |
| 7 |
* Frederic Loyer <[email protected]> |
| 8 |
* Werner Lemberg <[email protected]> |
| 9 |
* |
| 10 |
* Trivial declaration change in 2020 by |
| 11 |
* Karl Berry <[email protected]> |
| 12 |
*/ |
| 13 |
|
| 14 |
/* |
| 15 |
* This code has been derived from the program gsftopk. |
| 16 |
* Here the original copyright. |
| 17 |
*/ |
| 18 |
|
| 19 |
/* |
| 20 |
* Copyright (c) 1994 Paul Vojta. All rights reserved. |
| 21 |
* |
| 22 |
* Redistribution and use in source and binary forms, with or without |
| 23 |
* modification, are permitted provided that the following conditions |
| 24 |
* are met: |
| 25 |
* 1. Redistributions of source code must retain the above copyright |
| 26 |
* notice, this list of conditions and the following disclaimer. |
| 27 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 28 |
* notice, this list of conditions and the following disclaimer in the |
| 29 |
* documentation and/or other materials provided with the distribution. |
| 30 |
* |
| 31 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 32 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 33 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 34 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 35 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 36 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 37 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 38 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 39 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 40 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 41 |
* SUCH DAMAGE. |
| 42 |
*/ |
| 43 |
|
| 44 |
|
| 45 |
#ifdef HAVE_CONFIG_H |
| 46 |
#include <config.h> |
| 47 |
#endif |
| 48 |
|
| 49 |
#include <stdio.h> |
| 50 |
#include <stdlib.h> |
| 51 |
#include <stddef.h> /* for size_t */ |
| 52 |
#include <string.h> |
| 53 |
#include <errno.h> |
| 54 |
#include <ctype.h> |
| 55 |
|
| 56 |
#include "newobj.h" |
| 57 |
#include "pklib.h" |
| 58 |
#include "errormsg.h" |
| 59 |
#include "filesrch.h" |
| 60 |
|
| 61 |
#ifndef MAXPATHLEN |
| 62 |
#define MAXPATHLEN 256 |
| 63 |
#endif |
| 64 |
|
| 65 |
#define PK_PRE (char)247 |
| 66 |
#define PK_ID 89 |
| 67 |
#define PK_POST (char)245 |
| 68 |
#define PK_NOP (char)246 |
| 69 |
|
| 70 |
extern int dpi; /* defined in ftlib.c, avoiding -fno-common failure */ |
| 71 |
|
| 72 |
FILE *pk_file; |
| 73 |
|
| 74 |
|
| 75 |
/* |
| 76 |
* Information from the .tfm file. |
| 77 |
*/ |
| 78 |
|
| 79 |
int tfm_lengths[12]; |
| 80 |
|
| 81 |
#define lh tfm_lengths[1] |
| 82 |
#define bc tfm_lengths[2] |
| 83 |
#define ec tfm_lengths[3] |
| 84 |
#define nw tfm_lengths[4] |
| 85 |
|
| 86 |
long checksum; |
| 87 |
long design; |
| 88 |
byte width_index[256]; |
| 89 |
long tfm_widths[256]; |
| 90 |
|
| 91 |
/* |
| 92 |
* Information on the bitmap currently being worked on. |
| 93 |
*/ |
| 94 |
|
| 95 |
byte *bitmap; |
| 96 |
int width; |
| 97 |
int skip; |
| 98 |
int height; |
| 99 |
int hoff; |
| 100 |
int voff; |
| 101 |
int bytes_wide; |
| 102 |
size_t bm_size; |
| 103 |
byte *bitmap_end; |
| 104 |
int pk_len; |
| 105 |
|
| 106 |
/* |
| 107 |
* Here's the path searching stuff. First the typedefs and variables. |
| 108 |
*/ |
| 109 |
|
| 110 |
static char searchpath[MAXPATHLEN + 1]; |
| 111 |
|
| 112 |
#define HUNKSIZE (MAXPATHLEN + 2) |
| 113 |
|
| 114 |
struct spacenode /* used for storage of directory names */ |
| 115 |
{ |
| 116 |
struct spacenode *next; |
| 117 |
char *sp_end; /* end of data for this chunk */ |
| 118 |
char sp[HUNKSIZE]; |
| 119 |
} firstnode; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
static FILE * |
| 124 |
search_tfm(char **name) |
| 125 |
{ |
| 126 |
char *p; |
| 127 |
FILE *f; |
| 128 |
|
| 129 |
|
| 130 |
p = TeX_search_tfm(name); |
| 131 |
if (p == NULL) |
| 132 |
return NULL; |
| 133 |
strcpy(searchpath, p); |
| 134 |
f = fopen(searchpath, "rb"); |
| 135 |
return f; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
static long |
| 140 |
getlong(FILE *f) |
| 141 |
{ |
| 142 |
unsigned long value; |
| 143 |
|
| 144 |
|
| 145 |
value = (unsigned long)getc(f) << 24; |
| 146 |
value |= (unsigned long)getc(f) << 16; |
| 147 |
value |= (unsigned long)getc(f) << 8; |
| 148 |
value |= (unsigned long)getc(f); |
| 149 |
return value; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
char line[82]; |
| 154 |
|
| 155 |
|
| 156 |
static byte masks[] = {0, 1, 3, 7, 017, 037, 077, 0177, 0377}; |
| 157 |
|
| 158 |
byte flag; |
| 159 |
int pk_dyn_f; |
| 160 |
int pk_dyn_g; |
| 161 |
int base; /* cost of this character if pk_dyn_f = 0 */ |
| 162 |
int deltas[13]; /* cost of increasing pk_dyn_f from i to i+1 */ |
| 163 |
|
| 164 |
|
| 165 |
/* |
| 166 |
* Add up statistics for putting out the given shift count. |
| 167 |
*/ |
| 168 |
|
| 169 |
static void |
| 170 |
tallyup(int n) |
| 171 |
{ |
| 172 |
int m; |
| 173 |
|
| 174 |
|
| 175 |
if (n > 208) |
| 176 |
{ |
| 177 |
++base; |
| 178 |
n -= 192; |
| 179 |
for (m = 0x100; m != 0 && m < n; m <<= 4) |
| 180 |
base += 2; |
| 181 |
if (m != 0 && (m = (m - n) / 15) < 13) |
| 182 |
deltas[m] += 2; |
| 183 |
} |
| 184 |
else if (n > 13) |
| 185 |
++deltas[(208 - n) / 15]; |
| 186 |
else |
| 187 |
--deltas[n - 1]; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
/* |
| 192 |
* Routines for storing the shift counts. |
| 193 |
*/ |
| 194 |
|
| 195 |
static Boolean odd = False; |
| 196 |
static byte part; |
| 197 |
|
| 198 |
|
| 199 |
static void |
| 200 |
pk_put_nyb(int n) |
| 201 |
{ |
| 202 |
if (odd) |
| 203 |
{ |
| 204 |
*bitmap_end++ = (part << 4) | n; |
| 205 |
odd = False; |
| 206 |
} |
| 207 |
else |
| 208 |
{ |
| 209 |
part = n; |
| 210 |
odd = True; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
static void |
| 216 |
pk_put_long(int n) |
| 217 |
{ |
| 218 |
if (n >= 16) |
| 219 |
{ |
| 220 |
pk_put_nyb(0); |
| 221 |
pk_put_long(n / 16); |
| 222 |
} |
| 223 |
pk_put_nyb(n % 16); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
static void |
| 228 |
pk_put_count(int n) |
| 229 |
{ |
| 230 |
if (n > pk_dyn_f) |
| 231 |
{ |
| 232 |
if (n > pk_dyn_g) |
| 233 |
pk_put_long(n - pk_dyn_g + 15); |
| 234 |
else |
| 235 |
{ |
| 236 |
pk_put_nyb(pk_dyn_f + (n - pk_dyn_f + 15) / 16); |
| 237 |
pk_put_nyb((n - pk_dyn_f - 1) % 16); |
| 238 |
} |
| 239 |
} |
| 240 |
else |
| 241 |
pk_put_nyb(n); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
static void |
| 246 |
trim_bitmap(void) |
| 247 |
{ |
| 248 |
byte *p; |
| 249 |
byte mask; |
| 250 |
|
| 251 |
|
| 252 |
/* clear out garbage bits in bitmap */ |
| 253 |
|
| 254 |
if (width % 8 != 0) |
| 255 |
{ |
| 256 |
mask = ~masks[8 - width % 8]; |
| 257 |
for (p = bitmap + bytes_wide - 1; p < bitmap_end; p += bytes_wide) |
| 258 |
*p &= mask; |
| 259 |
} |
| 260 |
|
| 261 |
/* Find the bounding box of the bitmap. */ |
| 262 |
|
| 263 |
/* trim top */ |
| 264 |
|
| 265 |
skip = 0; |
| 266 |
mask = 0; |
| 267 |
|
| 268 |
for (;;) |
| 269 |
{ |
| 270 |
if (bitmap >= bitmap_end) /* if bitmap is empty */ |
| 271 |
{ |
| 272 |
width = height = hoff = voff = 0; |
| 273 |
return; |
| 274 |
} |
| 275 |
|
| 276 |
p = bitmap + bytes_wide; |
| 277 |
while (p > bitmap) |
| 278 |
mask |= *--p; |
| 279 |
if (mask) |
| 280 |
break; |
| 281 |
++skip; |
| 282 |
bitmap += bytes_wide; |
| 283 |
} |
| 284 |
|
| 285 |
height -= skip; |
| 286 |
voff -= skip; |
| 287 |
|
| 288 |
#ifdef DEBUG |
| 289 |
if (skip < 2 || skip > 3) |
| 290 |
printf("Character has %d empty rows at top\n", skip); |
| 291 |
#endif |
| 292 |
|
| 293 |
/* trim bottom */ |
| 294 |
|
| 295 |
skip = 0; |
| 296 |
mask = 0; |
| 297 |
|
| 298 |
for (;;) |
| 299 |
{ |
| 300 |
p = bitmap_end - bytes_wide; |
| 301 |
while (p < bitmap_end) |
| 302 |
mask |= *p++; |
| 303 |
if (mask) |
| 304 |
break; |
| 305 |
++skip; |
| 306 |
bitmap_end -= bytes_wide; |
| 307 |
} |
| 308 |
|
| 309 |
height -= skip; |
| 310 |
|
| 311 |
#ifdef DEBUG |
| 312 |
if (skip < 2 || skip > 3) |
| 313 |
printf("Character has %d empty rows at bottom\n", skip); |
| 314 |
#endif |
| 315 |
|
| 316 |
/* trim right */ |
| 317 |
|
| 318 |
skip = 0; |
| 319 |
--width; |
| 320 |
|
| 321 |
for (;;) |
| 322 |
{ |
| 323 |
mask = 0; |
| 324 |
for (p = bitmap + width / 8; p < bitmap_end; p += bytes_wide) |
| 325 |
mask |= *p; |
| 326 |
if (mask & (0x80 >> (width % 8))) |
| 327 |
break; |
| 328 |
|
| 329 |
--width; |
| 330 |
++skip; |
| 331 |
} |
| 332 |
|
| 333 |
++width; |
| 334 |
|
| 335 |
#ifdef DEBUG |
| 336 |
if (skip < 2 || skip > 3) |
| 337 |
printf("Character has %d empty columns at right\n", skip); |
| 338 |
#endif |
| 339 |
|
| 340 |
/* trim left */ |
| 341 |
|
| 342 |
skip = 0; |
| 343 |
|
| 344 |
for (;;) |
| 345 |
{ |
| 346 |
mask = 0; |
| 347 |
for (p = bitmap + skip / 8; p < bitmap_end; p += bytes_wide) |
| 348 |
mask |= *p; |
| 349 |
if (mask & (0x80 >> (skip % 8))) |
| 350 |
break; |
| 351 |
|
| 352 |
++skip; |
| 353 |
} |
| 354 |
|
| 355 |
width -= skip; |
| 356 |
hoff -= skip; |
| 357 |
|
| 358 |
#ifdef DEBUG |
| 359 |
if (skip < 2 || skip > 3) |
| 360 |
printf("Character has %d empty columns at left\n", skip); |
| 361 |
#endif |
| 362 |
|
| 363 |
bitmap += skip / 8; |
| 364 |
skip = skip % 8; |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
/* |
| 369 |
* Pack the bitmap using the rll method. (Return false if it's better |
| 370 |
* to just pack the bits.) |
| 371 |
*/ |
| 372 |
|
| 373 |
static Boolean |
| 374 |
pk_rll_cvt(void) |
| 375 |
{ |
| 376 |
static int *counts = NULL; /* area for saving bit counts */ |
| 377 |
static int maxcounts = 0; /* size of this area */ |
| 378 |
unsigned int ncounts; /* max to allow this time */ |
| 379 |
int *nextcount; /* next count value */ |
| 380 |
|
| 381 |
int *counts_end; /* pointer to end */ |
| 382 |
byte *rowptr; |
| 383 |
byte *p; |
| 384 |
byte mask; |
| 385 |
byte *rowdup; /* last row checked for dup */ |
| 386 |
byte paint_switch; /* 0 or 0xff */ |
| 387 |
int bits_left; /* bits left in row */ |
| 388 |
int cost; |
| 389 |
int i; |
| 390 |
|
| 391 |
|
| 392 |
/* Allocate space for bit counts. */ |
| 393 |
|
| 394 |
ncounts = (width * height + 3) / 4; |
| 395 |
if (ncounts > maxcounts) |
| 396 |
{ |
| 397 |
if (counts != NULL) |
| 398 |
free(counts); |
| 399 |
counts = (int *)mymalloc((ncounts + 2) * sizeof (int)); |
| 400 |
maxcounts = ncounts; |
| 401 |
} |
| 402 |
counts_end = counts + ncounts; |
| 403 |
|
| 404 |
/* Form bit counts and collect statistics */ |
| 405 |
|
| 406 |
base = 0; |
| 407 |
memset(deltas, 0, sizeof (deltas)); |
| 408 |
rowdup = NULL; /* last row checked for duplicates */ |
| 409 |
p = rowptr = bitmap; |
| 410 |
mask = 0x80 >> skip; |
| 411 |
flag = 0; |
| 412 |
paint_switch = 0; |
| 413 |
|
| 414 |
if (*p & mask) |
| 415 |
{ |
| 416 |
flag = 8; |
| 417 |
paint_switch = 0xff; |
| 418 |
} |
| 419 |
|
| 420 |
bits_left = width; |
| 421 |
nextcount = counts; |
| 422 |
|
| 423 |
while (rowptr < bitmap_end) /* loop over shift counts */ |
| 424 |
{ |
| 425 |
int shift_count = bits_left; |
| 426 |
|
| 427 |
|
| 428 |
for (;;) |
| 429 |
{ |
| 430 |
if (bits_left == 0) |
| 431 |
{ |
| 432 |
if ((p = rowptr += bytes_wide) >= bitmap_end) |
| 433 |
break; |
| 434 |
mask = 0x80 >> skip; |
| 435 |
bits_left = width; |
| 436 |
shift_count += width; |
| 437 |
} |
| 438 |
if (((*p ^ paint_switch) & mask) != 0) |
| 439 |
break; |
| 440 |
--bits_left; |
| 441 |
mask >>= 1; |
| 442 |
if (mask == 0) |
| 443 |
{ |
| 444 |
++p; |
| 445 |
while (*p == paint_switch && bits_left >= 8) |
| 446 |
{ |
| 447 |
++p; |
| 448 |
bits_left -= 8; |
| 449 |
} |
| 450 |
mask = 0x80; |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
if (nextcount >= counts_end) |
| 455 |
return False; |
| 456 |
shift_count -= bits_left; |
| 457 |
*nextcount++ = shift_count; |
| 458 |
tallyup(shift_count); |
| 459 |
|
| 460 |
/* check for duplicate rows */ |
| 461 |
if (rowptr != rowdup && bits_left != width) |
| 462 |
{ |
| 463 |
byte *p1 = rowptr; |
| 464 |
byte *q = rowptr + bytes_wide; |
| 465 |
int repeat_count; |
| 466 |
|
| 467 |
|
| 468 |
while (q < bitmap_end && *p1 == *q) |
| 469 |
{ |
| 470 |
++p1; |
| 471 |
++q; |
| 472 |
} |
| 473 |
repeat_count = (p1 - rowptr) / bytes_wide; |
| 474 |
if (repeat_count > 0) |
| 475 |
{ |
| 476 |
*nextcount++ = -repeat_count; |
| 477 |
if (repeat_count == 1) |
| 478 |
--base; |
| 479 |
else |
| 480 |
{ |
| 481 |
++base; |
| 482 |
tallyup(repeat_count); |
| 483 |
} |
| 484 |
rowptr += repeat_count * bytes_wide; |
| 485 |
} |
| 486 |
rowdup = rowptr; |
| 487 |
} |
| 488 |
paint_switch = ~paint_switch; |
| 489 |
} |
| 490 |
|
| 491 |
#ifdef DEBUG |
| 492 |
/* |
| 493 |
* Dump the bitmap |
| 494 |
*/ |
| 495 |
|
| 496 |
for (p = bitmap; p < bitmap_end; p += bytes_wide) |
| 497 |
{ |
| 498 |
byte *p1 = p; |
| 499 |
int j; |
| 500 |
|
| 501 |
|
| 502 |
mask = 0x80 >> skip; |
| 503 |
for (j = 0; j < width; ++j) |
| 504 |
{ |
| 505 |
putchar(*p1 & mask ? '@' : '.'); |
| 506 |
if ((mask >>= 1) == 0) |
| 507 |
{ |
| 508 |
mask = 0x80; |
| 509 |
++p1; |
| 510 |
} |
| 511 |
} |
| 512 |
putchar('\n'); |
| 513 |
} |
| 514 |
putchar('\n'); |
| 515 |
#endif |
| 516 |
|
| 517 |
/* Determine the best pk_dyn_f */ |
| 518 |
|
| 519 |
pk_dyn_f = 0; |
| 520 |
cost = base += 2 * (nextcount - counts); |
| 521 |
|
| 522 |
for (i = 1; i < 14; ++i) |
| 523 |
{ |
| 524 |
base += deltas[i - 1]; |
| 525 |
if (base < cost) |
| 526 |
{ |
| 527 |
pk_dyn_f = i; |
| 528 |
cost = base; |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
/* last chance to bail out */ |
| 533 |
|
| 534 |
if (cost * 4 > width * height) |
| 535 |
return False; |
| 536 |
|
| 537 |
/* Pack the bit counts */ |
| 538 |
|
| 539 |
pk_dyn_g = 208 - 15 * pk_dyn_f; |
| 540 |
flag |= pk_dyn_f << 4; |
| 541 |
bitmap_end = bitmap; |
| 542 |
*nextcount = 0; |
| 543 |
nextcount = counts; |
| 544 |
|
| 545 |
while (*nextcount != 0) |
| 546 |
{ |
| 547 |
if (*nextcount > 0) |
| 548 |
pk_put_count(*nextcount); |
| 549 |
else |
| 550 |
if (*nextcount == -1) |
| 551 |
pk_put_nyb(15); |
| 552 |
else |
| 553 |
{ |
| 554 |
pk_put_nyb(14); |
| 555 |
pk_put_count(-*nextcount); |
| 556 |
} |
| 557 |
++nextcount; |
| 558 |
} |
| 559 |
|
| 560 |
if (odd) |
| 561 |
{ |
| 562 |
pk_put_nyb(0); |
| 563 |
++cost; |
| 564 |
} |
| 565 |
|
| 566 |
if (cost != 2 * (bitmap_end - bitmap)) |
| 567 |
printf("Cost miscalculation: expected %d, got %ld\n", |
| 568 |
cost, (long)(2 * (bitmap_end - bitmap))); |
| 569 |
pk_len = bitmap_end - bitmap; |
| 570 |
return True; |
| 571 |
} |
| 572 |
|
| 573 |
|
| 574 |
static void |
| 575 |
pk_bm_cvt(void) |
| 576 |
{ |
| 577 |
byte *rowptr; |
| 578 |
byte *p; |
| 579 |
int blib1; /* bits left in byte */ |
| 580 |
int bits_left; /* bits left in row */ |
| 581 |
byte *q; |
| 582 |
int blib2; |
| 583 |
byte nextbyte; |
| 584 |
|
| 585 |
|
| 586 |
flag = 14 << 4; |
| 587 |
q = bitmap; |
| 588 |
blib2 = 8; |
| 589 |
nextbyte = 0; |
| 590 |
|
| 591 |
for (rowptr = bitmap; rowptr < bitmap_end; rowptr += bytes_wide) |
| 592 |
{ |
| 593 |
p = rowptr; |
| 594 |
blib1 = 8 - skip; |
| 595 |
bits_left = width; |
| 596 |
|
| 597 |
if (blib2 != 8) |
| 598 |
{ |
| 599 |
int n; |
| 600 |
|
| 601 |
|
| 602 |
if (blib1 < blib2) |
| 603 |
{ |
| 604 |
nextbyte |= *p << (blib2 - blib1); |
| 605 |
n = blib1; |
| 606 |
} |
| 607 |
else |
| 608 |
{ |
| 609 |
nextbyte |= *p >> (blib1 - blib2); |
| 610 |
n = blib2; |
| 611 |
} |
| 612 |
blib2 -= n; |
| 613 |
if ((bits_left -= n) < 0) |
| 614 |
{ |
| 615 |
blib2 -= bits_left; |
| 616 |
continue; |
| 617 |
} |
| 618 |
if ((blib1 -= n) == 0) |
| 619 |
{ |
| 620 |
blib1 = 8; |
| 621 |
++p; |
| 622 |
if (blib2 > 0) |
| 623 |
{ |
| 624 |
nextbyte |= *p >> (8 - blib2); |
| 625 |
blib1 -= blib2; |
| 626 |
bits_left -= blib2; |
| 627 |
if (bits_left < 0) |
| 628 |
{ |
| 629 |
blib2 = -bits_left; |
| 630 |
continue; |
| 631 |
} |
| 632 |
} |
| 633 |
} |
| 634 |
*q++ = nextbyte; |
| 635 |
} |
| 636 |
|
| 637 |
/* fill up whole (destination) bytes */ |
| 638 |
|
| 639 |
while (bits_left >= 8) |
| 640 |
{ |
| 641 |
nextbyte = *p++ << (8 - blib1); |
| 642 |
*q++ = nextbyte | (*p >> blib1); |
| 643 |
bits_left -= 8; |
| 644 |
} |
| 645 |
|
| 646 |
/* now do the remainder */ |
| 647 |
|
| 648 |
nextbyte = *p << (8 - blib1); |
| 649 |
if (bits_left > blib1) |
| 650 |
nextbyte |= p[1] >> blib1; |
| 651 |
blib2 = 8 - bits_left; |
| 652 |
} |
| 653 |
|
| 654 |
if (blib2 != 8) |
| 655 |
*q++ = nextbyte; |
| 656 |
|
| 657 |
pk_len = q - bitmap; |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
static void |
| 662 |
putshort(int w) |
| 663 |
{ |
| 664 |
putc(w >> 8, pk_file); |
| 665 |
putc(w, pk_file); |
| 666 |
} |
| 667 |
|
| 668 |
|
| 669 |
static void |
| 670 |
putmed(long w) |
| 671 |
{ |
| 672 |
putc(w >> 16, pk_file); |
| 673 |
putc(w >> 8, pk_file); |
| 674 |
putc(w, pk_file); |
| 675 |
} |
| 676 |
|
| 677 |
|
| 678 |
static void |
| 679 |
putlong(long w) |
| 680 |
{ |
| 681 |
putc(w >> 24, pk_file); |
| 682 |
putc(w >> 16, pk_file); |
| 683 |
putc(w >> 8, pk_file); |
| 684 |
putc(w, pk_file); |
| 685 |
} |
| 686 |
|
| 687 |
|
| 688 |
static char |
| 689 |
xgetc(FILE *f) |
| 690 |
{ |
| 691 |
int c; |
| 692 |
|
| 693 |
|
| 694 |
c = getc(f); |
| 695 |
if (c == EOF) |
| 696 |
oops("Premature end of file."); |
| 697 |
return (byte)c; |
| 698 |
} |
| 699 |
|
| 700 |
|
| 701 |
/* |
| 702 |
* Open and read the tfm file. |
| 703 |
*/ |
| 704 |
|
| 705 |
void |
| 706 |
TFMopen(char **filename) |
| 707 |
{ |
| 708 |
FILE *tfm_file; |
| 709 |
int i; |
| 710 |
int cc; |
| 711 |
|
| 712 |
|
| 713 |
tfm_file = search_tfm(filename); |
| 714 |
if (tfm_file == NULL) |
| 715 |
oops("Cannot find tfm file."); |
| 716 |
|
| 717 |
for (i = 0; i < 12; i++) |
| 718 |
{ |
| 719 |
int j; |
| 720 |
|
| 721 |
|
| 722 |
j = (int)((byte)getc(tfm_file)) << 8; |
| 723 |
tfm_lengths[i] = j | (int)((byte)xgetc(tfm_file)); |
| 724 |
} |
| 725 |
|
| 726 |
checksum = getlong(tfm_file); |
| 727 |
design = getlong(tfm_file); |
| 728 |
fseek(tfm_file, 4 * (lh + 6), 0); |
| 729 |
|
| 730 |
for (cc = bc; cc <= ec; ++cc) |
| 731 |
{ |
| 732 |
width_index[cc] = (byte)xgetc(tfm_file); |
| 733 |
|
| 734 |
(void)xgetc(tfm_file); |
| 735 |
(void)xgetc(tfm_file); |
| 736 |
(void)xgetc(tfm_file); |
| 737 |
} |
| 738 |
|
| 739 |
for (i = 0; i < nw; ++i) |
| 740 |
tfm_widths[i] = getlong(tfm_file); |
| 741 |
|
| 742 |
fclose(tfm_file); |
| 743 |
} |
| 744 |
|
| 745 |
|
| 746 |
/* |
| 747 |
* Create pk file and write preamble. |
| 748 |
*/ |
| 749 |
|
| 750 |
void |
| 751 |
PKopen(char *filename, |
| 752 |
char *ident, |
| 753 |
int resolution) |
| 754 |
{ |
| 755 |
int ppp; |
| 756 |
int i; |
| 757 |
|
| 758 |
|
| 759 |
dpi = resolution; |
| 760 |
|
| 761 |
if ((pk_file = fopen(filename, "wb")) == NULL) |
| 762 |
{ |
| 763 |
perror(filename); |
| 764 |
exit(1); |
| 765 |
} |
| 766 |
|
| 767 |
putc(PK_PRE, pk_file); |
| 768 |
putc(PK_ID, pk_file); |
| 769 |
|
| 770 |
i = strlen(ident); |
| 771 |
|
| 772 |
putc(i, pk_file); |
| 773 |
fwrite(ident, 1, i, pk_file); |
| 774 |
putlong(design); |
| 775 |
putlong(checksum); |
| 776 |
ppp = dpi / 72.27 * 65536.0 + 0.5; |
| 777 |
putlong(ppp); /* hppp */ |
| 778 |
putlong(ppp); /* vppp */ |
| 779 |
} |
| 780 |
|
| 781 |
|
| 782 |
void |
| 783 |
PKputglyph(int cc, |
| 784 |
int llx, int lly, int urx, int ury, |
| 785 |
int w, int h, |
| 786 |
byte *b) |
| 787 |
{ |
| 788 |
float char_width; |
| 789 |
|
| 790 |
long dm; |
| 791 |
long tfm_wid; |
| 792 |
|
| 793 |
|
| 794 |
bitmap = b; |
| 795 |
width = w; |
| 796 |
height = h; |
| 797 |
|
| 798 |
hoff = -llx; |
| 799 |
voff = ury - 2; /* Don't ask me why `-2' */ |
| 800 |
/* Fred */ |
| 801 |
|
| 802 |
if (width != urx - llx || height != ury - lly) |
| 803 |
oops("Dimensions do not match: (%d - %d) (%d - %d) <=> %d %d", |
| 804 |
llx, lly, urx, ury, width, height); |
| 805 |
|
| 806 |
bytes_wide = (width + 7) / 8; |
| 807 |
bm_size = bytes_wide * height; |
| 808 |
bitmap_end = bitmap + bm_size; |
| 809 |
|
| 810 |
trim_bitmap(); |
| 811 |
|
| 812 |
if (height == 0 || !pk_rll_cvt()) |
| 813 |
pk_bm_cvt(); |
| 814 |
|
| 815 |
if (!width_index[cc]) |
| 816 |
return; |
| 817 |
|
| 818 |
tfm_wid = tfm_widths[width_index[cc]]; |
| 819 |
char_width = tfm_wid / 1048576.0 * design / 1048576.0 * dpi / 72.27; |
| 820 |
dm = (long)(char_width + 0.5) - (char_width < -0.5); |
| 821 |
|
| 822 |
if (pk_len + 8 < 4 * 256 && tfm_wid < (1<<24) && |
| 823 |
dm >= 0 && dm < 256 && width < 256 && height < 256 && |
| 824 |
hoff >= -128 && hoff < 128 && voff >= -128 && voff < 128) |
| 825 |
{ |
| 826 |
putc(flag | ((pk_len + 8) >> 8), pk_file); |
| 827 |
putc(pk_len + 8, pk_file); |
| 828 |
putc(cc, pk_file); |
| 829 |
putmed(tfm_wid); |
| 830 |
putc(dm, pk_file); |
| 831 |
putc(width, pk_file); |
| 832 |
putc(height, pk_file); |
| 833 |
putc(hoff, pk_file); |
| 834 |
putc(voff, pk_file); |
| 835 |
} |
| 836 |
else if (pk_len + 13 < 3 * 65536L && tfm_wid < (1<<24) && |
| 837 |
dm >= 0 && dm < 65536L && width < 65536L && height < 65536L && |
| 838 |
hoff >= -65536L && hoff < 65536L && |
| 839 |
voff >= -65536L && voff < 65536L) |
| 840 |
{ |
| 841 |
putc(flag | 4 | ((pk_len + 13) >> 16), pk_file); |
| 842 |
putshort(pk_len + 13); |
| 843 |
putc(cc, pk_file); |
| 844 |
putmed(tfm_wid); |
| 845 |
putshort(dm); |
| 846 |
putshort(width); |
| 847 |
putshort(height); |
| 848 |
putshort(hoff); |
| 849 |
putshort(voff); |
| 850 |
} |
| 851 |
else |
| 852 |
{ |
| 853 |
putc(flag | 7, pk_file); |
| 854 |
putlong(pk_len + 28); |
| 855 |
putlong(cc); |
| 856 |
putlong(tfm_wid); |
| 857 |
putlong((long)(char_width * 65536.0 + 0.5) - (char_width < -0.5)); |
| 858 |
putlong(0); |
| 859 |
putlong(width); |
| 860 |
putlong(height); |
| 861 |
putlong(hoff); |
| 862 |
putlong(voff); |
| 863 |
} |
| 864 |
fwrite(bitmap, 1, pk_len, pk_file); |
| 865 |
} |
| 866 |
|
| 867 |
|
| 868 |
void |
| 869 |
PKclose(void) |
| 870 |
{ |
| 871 |
putc(PK_POST, pk_file); |
| 872 |
while (ftell(pk_file) % 4 != 0) |
| 873 |
putc(PK_NOP, pk_file); |
| 874 |
|
| 875 |
fclose(pk_file); |
| 876 |
} |
| 877 |
|
| 878 |
|
| 879 |
/* end */ |