| 1 |
/*========================================================================*\ |
| 2 |
|
| 3 |
Copyright (c) 1990-2004 Paul Vojta |
| 4 |
|
| 5 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 |
of this software and associated documentation files (the "Software"), to |
| 7 |
deal in the Software without restriction, including without limitation the |
| 8 |
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 9 |
sell copies of the Software, and to permit persons to whom the Software is |
| 10 |
furnished to do so, subject to the following conditions: |
| 11 |
|
| 12 |
The above copyright notice and this permission notice shall be included in |
| 13 |
all copies or substantial portions of the Software. |
| 14 |
|
| 15 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 |
PAUL VOJTA OR ANY OTHER AUTHOR OF THIS SOFTWARE BE LIABLE FOR ANY CLAIM, |
| 19 |
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 20 |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 21 |
OTHER DEALINGS IN THE SOFTWARE. |
| 22 |
|
| 23 |
NOTE: |
| 24 |
xdvi is based on prior work, as noted in the modification history |
| 25 |
in xdvi.c. |
| 26 |
|
| 27 |
\*========================================================================*/ |
| 28 |
|
| 29 |
/* |
| 30 |
* PK font reading routines. |
| 31 |
* Public routines are read_PK_index and read_PK_char. |
| 32 |
*/ |
| 33 |
|
| 34 |
#include "xdvi-config.h" |
| 35 |
#include "xdvi.h" |
| 36 |
#include "dvi-init.h" |
| 37 |
#include "util.h" |
| 38 |
|
| 39 |
#define PK_ID 89 |
| 40 |
#define PK_CMD_START 240 |
| 41 |
#define PK_X1 240 |
| 42 |
#define PK_X2 241 |
| 43 |
#define PK_X3 242 |
| 44 |
#define PK_X4 243 |
| 45 |
#define PK_Y 244 |
| 46 |
#define PK_POST 245 |
| 47 |
#define PK_NOOP 246 |
| 48 |
#define PK_PRE 247 |
| 49 |
|
| 50 |
static int PK_flag_byte; |
| 51 |
static unsigned PK_input_byte; |
| 52 |
static int PK_bitpos; |
| 53 |
static int PK_dyn_f; |
| 54 |
static int PK_repeat_count; |
| 55 |
|
| 56 |
static int |
| 57 |
PK_get_nyb(FILE *fp) |
| 58 |
{ |
| 59 |
unsigned temp; |
| 60 |
if (PK_bitpos < 0) { |
| 61 |
PK_input_byte = get_byte(fp); |
| 62 |
PK_bitpos = 4; |
| 63 |
} |
| 64 |
temp = PK_input_byte >> PK_bitpos; |
| 65 |
PK_bitpos -= 4; |
| 66 |
return (temp & 0xf); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
static int |
| 71 |
PK_packed_num(FILE *fp) |
| 72 |
{ |
| 73 |
int i, j; |
| 74 |
|
| 75 |
if ((i = PK_get_nyb(fp)) == 0) { |
| 76 |
do { |
| 77 |
j = PK_get_nyb(fp); |
| 78 |
++i; |
| 79 |
} |
| 80 |
while (j == 0); |
| 81 |
while (i > 0) { |
| 82 |
j = (j << 4) | PK_get_nyb(fp); |
| 83 |
--i; |
| 84 |
} |
| 85 |
return (j - 15 + ((13 - PK_dyn_f) << 4) + PK_dyn_f); |
| 86 |
} |
| 87 |
else { |
| 88 |
if (i <= PK_dyn_f) |
| 89 |
return i; |
| 90 |
if (i < 14) |
| 91 |
return (((i - PK_dyn_f - 1) << 4) + PK_get_nyb(fp) |
| 92 |
+ PK_dyn_f + 1); |
| 93 |
if (i == 14) |
| 94 |
PK_repeat_count = PK_packed_num(fp); |
| 95 |
else |
| 96 |
PK_repeat_count = 1; |
| 97 |
return PK_packed_num(fp); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
static void |
| 103 |
PK_skip_specials(struct font *fontp) |
| 104 |
{ |
| 105 |
int i, j; |
| 106 |
FILE *fp = fontp->file; |
| 107 |
|
| 108 |
do { |
| 109 |
PK_flag_byte = get_byte(fp); |
| 110 |
if (PK_flag_byte >= PK_CMD_START) { |
| 111 |
switch (PK_flag_byte) { |
| 112 |
case PK_X1: |
| 113 |
case PK_X2: |
| 114 |
case PK_X3: |
| 115 |
case PK_X4: |
| 116 |
i = 0; |
| 117 |
for (j = PK_CMD_START; j <= PK_flag_byte; ++j) |
| 118 |
i = (i << 8) | get_byte(fp); |
| 119 |
while (i--) |
| 120 |
(void)get_byte(fp); |
| 121 |
break; |
| 122 |
case PK_Y: |
| 123 |
(void)get_bytes(fp, 4); |
| 124 |
case PK_POST: |
| 125 |
case PK_NOOP: |
| 126 |
break; |
| 127 |
default: |
| 128 |
XDVI_FATAL((stderr, "Unexpected %d in PK file %s", PK_flag_byte, |
| 129 |
fontp->filename)); |
| 130 |
break; |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
while (PK_flag_byte != PK_POST && PK_flag_byte >= PK_CMD_START); |
| 135 |
} |
| 136 |
|
| 137 |
/* |
| 138 |
* Public routines |
| 139 |
*/ |
| 140 |
|
| 141 |
static void |
| 142 |
read_PK_char(struct font *fontp, |
| 143 |
wide_ubyte ch) |
| 144 |
{ |
| 145 |
int i, j; |
| 146 |
int n; |
| 147 |
int row_bit_pos; |
| 148 |
Boolean paint_switch; |
| 149 |
bmUnitT *cp; |
| 150 |
struct glyph *g; |
| 151 |
FILE *fp = fontp->file; |
| 152 |
long fpwidth; |
| 153 |
bmUnitT word = 0; |
| 154 |
int word_weight, bytes_wide; |
| 155 |
int rows_left, h_bit, count; |
| 156 |
|
| 157 |
g = &fontp->glyph[ch]; |
| 158 |
PK_flag_byte = g->x2; |
| 159 |
PK_dyn_f = PK_flag_byte >> 4; |
| 160 |
paint_switch = ((PK_flag_byte & 8) != 0); |
| 161 |
PK_flag_byte &= 0x7; |
| 162 |
if (PK_flag_byte == 7) |
| 163 |
n = 4; |
| 164 |
else if (PK_flag_byte > 3) |
| 165 |
n = 2; |
| 166 |
else |
| 167 |
n = 1; |
| 168 |
|
| 169 |
if (globals.debug & DBG_PK) |
| 170 |
printf("loading pk char %d, char type %d ", ch, n); |
| 171 |
|
| 172 |
/* |
| 173 |
* now read rest of character preamble |
| 174 |
*/ |
| 175 |
if (n != 4) |
| 176 |
fpwidth = get_bytes(fp, 3); |
| 177 |
else { |
| 178 |
fpwidth = get_lbytes(fp, 4); |
| 179 |
(void)get_bytes(fp, 4); /* horizontal escapement */ |
| 180 |
} |
| 181 |
(void)get_bytes(fp, n); /* vertical escapement */ |
| 182 |
{ |
| 183 |
unsigned long w, h; |
| 184 |
|
| 185 |
w = get_bytes(fp, n); |
| 186 |
h = get_bytes(fp, n); |
| 187 |
/* bitmap.w and bitmap.h are of type `unsigned short', so check for possible |
| 188 |
overflow here. USHRT_MAX should be sufficient, since a character of |
| 189 |
30pt at 1200dpi requires about 560 x 560 pixels; and the maximum |
| 190 |
bitmap of size USHRT_MAX * USHRT_MAX would consume USHRT_MAX * USHRT_MAX / 8 |
| 191 |
bytes ~= 530MB of RAM (per character/font!) |
| 192 |
*/ |
| 193 |
if (w > USHRT_MAX || h > USHRT_MAX) |
| 194 |
XDVI_FATAL((stderr, "Character %d too large (%ld x %ld, max is %d x %d) in file %s", |
| 195 |
ch, w, h, USHRT_MAX, USHRT_MAX, fontp->fontname)); |
| 196 |
g->bitmap.w = w; |
| 197 |
g->bitmap.h = h; |
| 198 |
} |
| 199 |
g->x = get_lbytes(fp, n); |
| 200 |
g->y = get_lbytes(fp, n); |
| 201 |
|
| 202 |
g->dvi_adv = fontp->dimconv * fpwidth; |
| 203 |
|
| 204 |
if (globals.debug & DBG_PK) { |
| 205 |
if (g->bitmap.w != 0) |
| 206 |
printf(", size=%dx%d, dvi_adv=%ld", g->bitmap.w, g->bitmap.h, |
| 207 |
g->dvi_adv); |
| 208 |
putchar('\n'); |
| 209 |
} |
| 210 |
|
| 211 |
alloc_bitmap(&g->bitmap); |
| 212 |
cp = (bmUnitT *) g->bitmap.bits; |
| 213 |
|
| 214 |
/* |
| 215 |
* read character data into *cp |
| 216 |
*/ |
| 217 |
bytes_wide = ROUNDUP((int)g->bitmap.w, BMBITS) * BMBYTES; |
| 218 |
PK_bitpos = -1; |
| 219 |
if (PK_dyn_f == 14) { /* get raster by bits */ |
| 220 |
memset(g->bitmap.bits, 0, (int)g->bitmap.h * bytes_wide); |
| 221 |
for (i = 0; i < (int)g->bitmap.h; i++) { /* get all rows */ |
| 222 |
cp = ADD(g->bitmap.bits, i * bytes_wide); |
| 223 |
#ifndef WORDS_BIGENDIAN |
| 224 |
row_bit_pos = -1; |
| 225 |
#else |
| 226 |
row_bit_pos = BMBITS; |
| 227 |
#endif |
| 228 |
for (j = 0; j < (int)g->bitmap.w; j++) { /* get one row */ |
| 229 |
if (--PK_bitpos < 0) { |
| 230 |
word = get_byte(fp); |
| 231 |
PK_bitpos = 7; |
| 232 |
} |
| 233 |
#ifndef WORDS_BIGENDIAN |
| 234 |
if (++row_bit_pos >= BMBITS) { |
| 235 |
cp++; |
| 236 |
row_bit_pos = 0; |
| 237 |
} |
| 238 |
#else |
| 239 |
if (--row_bit_pos < 0) { |
| 240 |
cp++; |
| 241 |
row_bit_pos = BMBITS - 1; |
| 242 |
} |
| 243 |
#endif |
| 244 |
if (word & (1 << PK_bitpos)) |
| 245 |
*cp |= 1 << row_bit_pos; |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
else { /* get packed raster */ |
| 250 |
rows_left = g->bitmap.h; |
| 251 |
h_bit = g->bitmap.w; |
| 252 |
PK_repeat_count = 0; |
| 253 |
word_weight = BMBITS; |
| 254 |
word = 0; |
| 255 |
while (rows_left > 0) { |
| 256 |
count = PK_packed_num(fp); |
| 257 |
while (count > 0) { |
| 258 |
if (count < word_weight && count < h_bit) { |
| 259 |
#ifndef WORDS_BIGENDIAN |
| 260 |
if (paint_switch) |
| 261 |
word |= bit_masks[count] << (BMBITS - word_weight); |
| 262 |
#endif |
| 263 |
h_bit -= count; |
| 264 |
word_weight -= count; |
| 265 |
#ifdef WORDS_BIGENDIAN |
| 266 |
if (paint_switch) |
| 267 |
word |= bit_masks[count] << word_weight; |
| 268 |
#endif |
| 269 |
count = 0; |
| 270 |
} |
| 271 |
else if (count >= h_bit && h_bit <= word_weight) { |
| 272 |
if (paint_switch) |
| 273 |
word |= bit_masks[h_bit] << |
| 274 |
#ifndef WORDS_BIGENDIAN |
| 275 |
(BMBITS - word_weight); |
| 276 |
#else |
| 277 |
(word_weight - h_bit); |
| 278 |
#endif |
| 279 |
*cp++ = word; |
| 280 |
/* "output" row(s) */ |
| 281 |
for (i = PK_repeat_count * bytes_wide / BMBYTES; i > 0; --i) { |
| 282 |
*cp = *SUB(cp, bytes_wide); |
| 283 |
++cp; |
| 284 |
} |
| 285 |
rows_left -= PK_repeat_count + 1; |
| 286 |
PK_repeat_count = 0; |
| 287 |
word = 0; |
| 288 |
word_weight = BMBITS; |
| 289 |
count -= h_bit; |
| 290 |
h_bit = g->bitmap.w; |
| 291 |
} |
| 292 |
else { |
| 293 |
if (paint_switch) |
| 294 |
#ifndef WORDS_BIGENDIAN |
| 295 |
word |= bit_masks[word_weight] << |
| 296 |
(BMBITS - word_weight); |
| 297 |
#else |
| 298 |
word |= bit_masks[word_weight]; |
| 299 |
#endif |
| 300 |
*cp++ = word; |
| 301 |
word = 0; |
| 302 |
count -= word_weight; |
| 303 |
h_bit -= word_weight; |
| 304 |
word_weight = BMBITS; |
| 305 |
} |
| 306 |
} |
| 307 |
paint_switch = 1 - paint_switch; |
| 308 |
} |
| 309 |
if (cp != ((bmUnitT *) (g->bitmap.bits + bytes_wide * g->bitmap.h))) |
| 310 |
XDVI_FATAL((stderr, "Wrong number of bits stored: char. %d, font %s", ch, |
| 311 |
fontp->fontname)); |
| 312 |
if (rows_left != 0 || h_bit != g->bitmap.w) |
| 313 |
XDVI_FATAL((stderr, "Bad pk file (%s), too many bits", fontp->fontname)); |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
void |
| 318 |
read_PK_index(struct font *fontp, wide_bool hushcs) |
| 319 |
{ |
| 320 |
int hppp, vppp; |
| 321 |
long checksum; |
| 322 |
|
| 323 |
fontp->read_char = read_PK_char; |
| 324 |
if (globals.debug & DBG_PK) |
| 325 |
printf("Reading PK pixel file %s\n", fontp->filename); |
| 326 |
|
| 327 |
fseek(fontp->file, (long)get_byte(fontp->file), SEEK_CUR); /* skip comment */ |
| 328 |
|
| 329 |
(void)get_bytes(fontp->file, 4); /* skip design size */ |
| 330 |
checksum = get_bytes(fontp->file, 4); |
| 331 |
if (checksum != fontp->checksum && checksum != 0 && fontp->checksum != 0 |
| 332 |
&& !hushcs) |
| 333 |
XDVI_WARNING((stderr, "Checksum mismatch (dvi = %lu, pk = %lu) in font file %s", |
| 334 |
fontp->checksum, checksum, fontp->filename)); |
| 335 |
hppp = get_lbytes(fontp->file, 4); |
| 336 |
vppp = get_lbytes(fontp->file, 4); |
| 337 |
if (hppp != vppp && (globals.debug & DBG_PK)) |
| 338 |
printf("Font has non-square aspect ratio %d:%d\n", vppp, hppp); |
| 339 |
/* |
| 340 |
* Prepare glyph array. |
| 341 |
*/ |
| 342 |
fontp->glyph = xmalloc(256 * sizeof(struct glyph)); |
| 343 |
memset((char *)fontp->glyph, 0, 256 * sizeof(struct glyph)); |
| 344 |
/* |
| 345 |
* Read glyph directory (really a whole pass over the file). |
| 346 |
*/ |
| 347 |
for (;;) { |
| 348 |
int bytes_left, flag_low_bits; |
| 349 |
unsigned int ch; |
| 350 |
|
| 351 |
PK_skip_specials(fontp); |
| 352 |
if (PK_flag_byte == PK_POST) |
| 353 |
break; |
| 354 |
flag_low_bits = PK_flag_byte & 0x7; |
| 355 |
if (flag_low_bits == 7) { |
| 356 |
bytes_left = get_bytes(fontp->file, 4); |
| 357 |
ch = get_bytes(fontp->file, 4); |
| 358 |
} |
| 359 |
else if (flag_low_bits > 3) { |
| 360 |
bytes_left = ((flag_low_bits - 4) << 16) + get_bytes(fontp->file, 2); |
| 361 |
ch = get_byte(fontp->file); |
| 362 |
} |
| 363 |
else { |
| 364 |
bytes_left = (flag_low_bits << 8) + get_byte(fontp->file); |
| 365 |
ch = get_byte(fontp->file); |
| 366 |
} |
| 367 |
fontp->glyph[ch].addr = ftell(fontp->file); |
| 368 |
fontp->glyph[ch].x2 = PK_flag_byte; |
| 369 |
#ifdef linux |
| 370 |
# ifndef SHORTSEEK |
| 371 |
# define SHORTSEEK 2048 |
| 372 |
# endif |
| 373 |
/* A bug in Linux libc (as of 18oct94) makes a short read faster |
| 374 |
than a short forward seek. Totally non-intuitive. */ |
| 375 |
if (bytes_left > 0 && bytes_left < SHORTSEEK) { |
| 376 |
char *dummy = xmalloc(bytes_left); |
| 377 |
(void)fread(dummy, 1, bytes_left, fontp->file); |
| 378 |
free(dummy); |
| 379 |
} |
| 380 |
else |
| 381 |
/* seek backward, or long forward */ |
| 382 |
#endif /* linux */ |
| 383 |
fseek(fontp->file, (long)bytes_left, SEEK_CUR); |
| 384 |
if (globals.debug & DBG_PK) |
| 385 |
printf("Scanning pk char %u, at %ld.\n", ch, fontp->glyph[ch].addr); |
| 386 |
} |
| 387 |
} |