2 /* Copyright (C) 2000, Matias Atria
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * - First working version
25 * - FIXED: entirely white/black rows were missed.
27 * - TESTED: Glyphs are rendered correctly in different byte orders.
28 * - Made bitmap code much more efficient and compact.
41 #define PK_CMD_START 240
51 #define PK_DYN_F(x) (((x) >> 4) & 0xf)
52 #define PK_PACKED(x) (PK_DYN_F(x) != 14)
54 static int pk_load_font __PROTO((DviParams *, DviFont *));
55 static int pk_font_get_glyph __PROTO((DviParams *, DviFont *, int));
57 static int pk_auto_generate = 1; /* this is ON by default */
65 static char *pk_lookup __PROTO((const char *, Ushort *, Ushort *));
66 static char *pk_lookupn __PROTO((const char *, Ushort *, Ushort *));
68 /* only symbols exported by this file */
69 DviFontInfo pk_font_info = {
71 0, /* scaling not supported natively */
75 mdvi_shrink_glyph_grey,
78 pk_lookup, /* lookup */
83 DviFontInfo pkn_font_info = {
85 0, /* scaling not supported natively */
89 mdvi_shrink_glyph_grey,
92 pk_lookupn, /* lookup */
97 static char *pk_lookup(const char *name, Ushort *hdpi, Ushort *vdpi)
99 kpse_glyph_file_type type;
102 if(pk_auto_generate == 0) {
103 kpse_set_program_enabled(kpse_pk_format, 1, kpse_src_cmdline);
104 pk_auto_generate = 1;
106 filename = kpse_find_glyph(name, Max(*hdpi, *vdpi),
107 kpse_pk_format, &type);
108 if(filename && type.source == kpse_glyph_source_fallback) {
111 } else if(filename) {
112 *hdpi = *vdpi = type.dpi;
117 static char *pk_lookupn(const char *name, Ushort *hdpi, Ushort *vdpi)
119 kpse_glyph_file_type type;
122 if(pk_auto_generate) {
123 kpse_set_program_enabled(kpse_pk_format, 0, kpse_src_cmdline);
124 pk_auto_generate = 0;
126 filename = kpse_find_glyph(name, Max(*hdpi, *vdpi),
127 kpse_pk_format, &type);
128 if(filename && type.source == kpse_glyph_source_fallback) {
131 } else if(filename) {
132 *hdpi = *vdpi = type.dpi;
137 static inline int pk_get_nyb(FILE *p, pkread *pk)
143 t = c = pk->currbyte;
148 c = pk->currbyte = fuget1(p);
160 * this is a bit cumbersome because we have to pass around
161 * the `pkread' data...
163 static int pk_packed_num(FILE *p, pkread *pkr, int *repeat)
166 int dyn_f = pkr->dyn_f;
168 i = pk_get_nyb(p, pkr);
171 j = pk_get_nyb(p, pkr);
175 j = (j << 4) + pk_get_nyb(p, pkr);
176 return (j - 15 + ((13 - dyn_f) << 4) +
178 } else if(i <= dyn_f)
181 return ((i - dyn_f - 1) << 4) +
182 pk_get_nyb(p, pkr) + dyn_f + 1;
186 *repeat = pk_packed_num(p, pkr, repeat);
187 return pk_packed_num(p, pkr, repeat);
191 #define ROUND(x,y) (((x) + (y) - 1) / (y))
193 static BITMAP *get_bitmap(FILE *p, int w, int h, int flags)
201 flags = 0; /* shut up that compiler */
203 if((bm = bitmap_alloc(w, h)) == NULL)
205 DEBUG((DBG_BITMAPS, "get_bitmap(%d,%d,%d): reading raw bitmap\n",
209 for(i = 0; i < h; i++) {
213 for(j = 0; j < w; j++) {
218 if(currch & (1 << bitpos))
221 if(mask == LASTMASK) {
227 ptr = bm_offset(ptr, bm->stride);
232 static BITMAP *get_packed(FILE *p, int w, int h, int flags)
243 pkr.dyn_f = PK_DYN_F(flags);
244 paint = !!(flags & 0x8);
249 if((bm = bitmap_alloc(w, h)) == NULL)
251 DEBUG((DBG_BITMAPS, "get_packed(%d,%d,%d): reading packed glyph\n",
256 count = pk_packed_num(p, &pkr, &i);
259 fprintf(stderr, "second repeat count for this row (had %d and got %d)\n",
268 /* first finish current row */
270 bitmap_set_row(bm, row, w - inrow, inrow, paint);
271 /* now copy it as many times as required */
272 r = (Uchar *)bm->data + row * bm->stride;
273 while(repeat_count-- > 0) {
275 /* copy entire lines */
276 memcpy(t, r, bm->stride);
281 /* count first row we drew */
283 /* update run count */
285 /* now r points to the beginning of the last row we finished */
291 a = (BmUnit *)(r + bm->stride);
292 /* deal with entirely with/black rows */
294 /* count number of atoms in a row */
295 i = ROUND(w, BITMAP_BITS);
304 bitmap_set_row(bm, row, w - inrow, count, paint);
308 if(row != h || inrow != w) {
309 error(_("Bad PK file: More bits than required\n"));
316 static BITMAP *get_char(FILE *p, int w, int h, int flags)
318 /* check if dyn_f == 14 */
319 if(((flags >> 4) & 0xf) == 14)
320 return get_bitmap(p, w, h, flags);
322 return get_packed(p, w, h, flags);
325 /* supports any number of characters in a font */
326 static int pk_load_font(DviParams *unused, DviFont *font)
338 font->chars = xnalloc(DviFontChar, 256);
340 memzero(font->chars, 256 * sizeof(DviFontChar));
341 for(i = 0; i < 256; i++)
342 font->chars[i].offset = 0;
344 /* check the preamble */
345 loc = fuget1(p); hic = fuget1(p);
346 if(loc != PK_PRE || hic != PK_ID)
350 for(loc = 0; loc < i; loc++)
353 DEBUG((DBG_FONTS, "(pk) %s: %s\n", font->fontname, s));
355 fseek(in, (long)i, SEEK_CUR);
357 /* get the design size */
358 font->design = fuget4(p);
359 /* get the checksum */
360 checksum = fuget4(p);
361 if(checksum && font->checksum && font->checksum != checksum) {
362 warning(_("%s: checksum mismatch (expected %u, got %u)\n"),
363 font->fontname, font->checksum, checksum);
364 } else if(!font->checksum)
365 font->checksum = checksum;
366 /* skip pixel per point ratios */
372 /* now start reading the font */
373 loc = 256; hic = -1; maxch = 256;
375 /* initialize alpha and beta for TFM width computation */
376 TFMPREPARE(font->scale, z, alpha, beta);
378 while((flag_byte = fuget1(p)) != PK_POST) {
381 if(flag_byte >= PK_CMD_START) {
391 i = fugetn(p, flag_byte - PK_X1 + 1);
395 t = mdvi_malloc(i + 1);
396 for(n = 0; n < i; n++)
399 DEBUG((DBG_SPECIAL, "(pk) %s: Special \"%s\"\n",
404 i = fugetn(p, flag_byte - PK_X1 + 1);
412 DEBUG((DBG_SPECIAL, "(pk) %s: MF special %u\n",
413 font->fontname, (unsigned)i));
419 error(_("%s: unexpected preamble\n"), font->fontname);
430 switch(flag_byte & 0x7) {
434 offset = ftell(p) + pl;
436 fsget4(p); /* skip dx */
437 fsget4(p); /* skip dy */
446 pl = (flag_byte % 4) * 65536 + fuget2(p);
448 offset = ftell(p) + pl;
450 fsget2(p); /* skip dx */
458 pl = (flag_byte % 4) * 256 + fuget1(p);
460 offset = ftell(p) + pl;
462 fsget1(p); /* skip dx */
476 font->chars = xresize(font->chars,
477 DviFontChar, cc + 16);
478 for(i = maxch; i < cc + 16; i++)
479 font->chars[i].offset = 0;
482 font->chars[cc].code = cc;
483 font->chars[cc].flags = flag_byte;
484 font->chars[cc].offset = ftell(p);
485 font->chars[cc].width = w;
486 font->chars[cc].height = h;
487 font->chars[cc].glyph.data = NULL;
488 font->chars[cc].x = x;
489 font->chars[cc].y = y;
490 font->chars[cc].glyph.x = x;
491 font->chars[cc].glyph.y = y;
492 font->chars[cc].glyph.w = w;
493 font->chars[cc].glyph.h = h;
494 font->chars[cc].grey.data = NULL;
495 font->chars[cc].shrunk.data = NULL;
496 font->chars[cc].tfmwidth = TFMSCALE(z, tfm, alpha, beta);
497 font->chars[cc].loaded = 0;
498 fseek(p, (long)offset, SEEK_SET);
501 if(flag_byte != PK_POST) {
502 error(_("%s: unexpected end of file (no postamble)\n"),
506 while((flag_byte = fuget1(p)) != EOF) {
507 if(flag_byte != PK_NOOP) {
508 error(_("invalid PK file! (junk in postamble)\n"));
513 /* resize font char data */
514 if(loc > 0 || hic < maxch-1) {
515 memmove(font->chars, font->chars + loc,
516 (hic - loc + 1) * sizeof(DviFontChar));
517 font->chars = xresize(font->chars,
518 DviFontChar, hic - loc + 1);
525 error(_("%s: File corrupted, or not a PK file\n"), font->fontname);
527 mdvi_free(font->chars);
529 font->loc = font->hic = 0;
533 static int pk_font_get_glyph(DviParams *params, DviFont *font, int code)
537 if((ch = FONTCHAR(font, code)) == NULL)
542 DEBUG((DBG_GLYPHS, "(pk) loading glyph for character %d (%dx%d) in font `%s'\n",
543 code, ch->width, ch->height, font->fontname));
544 if(font->in == NULL && font_reopen(font) < 0)
546 if(!ch->width || !ch->height) {
547 /* this happens for ` ' (ASCII 32) in some fonts */
550 ch->glyph.w = ch->width;
551 ch->glyph.h = ch->height;
552 ch->glyph.data = NULL;
555 if(fseek(font->in, ch->offset, SEEK_SET) == -1)
557 ch->glyph.data = get_char(font->in,
558 ch->width, ch->height, ch->flags);
560 /* restore original settings */
563 ch->glyph.w = ch->width;
564 ch->glyph.h = ch->height;