| File: | backend/dvi/mdvi-lib/gf.c |
| Warning: | line 290, column 3 This statement is never executed |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* gf.c - GF font support */ |
| 2 | /* |
| 3 | * Copyright (C) 2000, Matias Atria |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | /* functions to read GF fonts */ |
| 21 | |
| 22 | #include <config.h> |
| 23 | #include <string.h> |
| 24 | #include "common.h" |
| 25 | #include "mdvi.h" |
| 26 | #include "private.h" |
| 27 | |
| 28 | /* opcodes */ |
| 29 | |
| 30 | #define GF_PAINT00 0 |
| 31 | #define GF_PAINT164 64 |
| 32 | #define GF_PAINT265 65 |
| 33 | #define GF_PAINT366 66 |
| 34 | #define GF_BOC67 67 |
| 35 | #define GF_BOC168 68 |
| 36 | #define GF_EOC69 69 |
| 37 | #define GF_SKIP070 70 |
| 38 | #define GF_SKIP171 71 |
| 39 | #define GF_SKIP272 72 |
| 40 | #define GF_SKIP373 73 |
| 41 | #define GF_NEW_ROW_074 74 |
| 42 | #define GF_NEW_ROW_175 75 |
| 43 | #define GF_NEW_ROW_MAX238 238 |
| 44 | #define GF_XXX1239 239 |
| 45 | #define GF_XXX2240 240 |
| 46 | #define GF_XXX3241 241 |
| 47 | #define GF_XXX4242 242 |
| 48 | #define GF_YYY243 243 |
| 49 | #define GF_NOOP244 244 |
| 50 | #define GF_LOC245 245 |
| 51 | #define GF_LOC0246 246 |
| 52 | #define GF_PRE247 247 |
| 53 | #define GF_POST248 248 |
| 54 | #define GF_POST_POST249 249 |
| 55 | |
| 56 | #define GF_ID131 131 |
| 57 | #define GF_TRAILER223 223 |
| 58 | |
| 59 | #define BLACK1 1 |
| 60 | #define WHITE0 0 |
| 61 | |
| 62 | static int gf_load_font __PROTO((DviParams *, DviFont *))(DviParams *, DviFont *); |
| 63 | static int gf_font_get_glyph __PROTO((DviParams *, DviFont *, int))(DviParams *, DviFont *, int); |
| 64 | |
| 65 | /* only symbol exported by this file */ |
| 66 | DviFontInfo gf_font_info = { |
| 67 | "GF", |
| 68 | 0, /* scaling not supported natively */ |
| 69 | gf_load_font, |
| 70 | gf_font_get_glyph, |
| 71 | mdvi_shrink_glyph, |
| 72 | mdvi_shrink_glyph_grey, |
| 73 | NULL((void*)0), /* free */ |
| 74 | NULL((void*)0), /* reset */ |
| 75 | NULL((void*)0), /* lookup */ |
| 76 | kpse_gf_format, |
| 77 | NULL((void*)0) |
| 78 | }; |
| 79 | |
| 80 | static int gf_read_bitmap(FILE *p, DviFontChar *ch) |
| 81 | { |
| 82 | int op; |
| 83 | int min_n, max_n; |
| 84 | int min_m, max_m; |
| 85 | int paint_switch; |
| 86 | int x, y; |
| 87 | int bpl; |
| 88 | Int32 par; |
| 89 | BmUnit *line; |
| 90 | BITMAP *map; |
| 91 | |
| 92 | fseek(p, (long)ch->offset, SEEK_SET0); |
| 93 | op = fuget1(p)((unsigned)getc(p)); |
| 94 | if(op == GF_BOC67) { |
| 95 | /* skip character code */ |
| 96 | fuget4(p)fugetn((p), 4); |
| 97 | /* skip pointer */ |
| 98 | fuget4(p)fugetn((p), 4); |
| 99 | min_m = fsget4(p)fsgetn((p), 4); |
| 100 | max_m = fsget4(p)fsgetn((p), 4); |
| 101 | min_n = fsget4(p)fsgetn((p), 4); |
| 102 | max_n = fsget4(p)fsgetn((p), 4); |
| 103 | } else if(op == GF_BOC168) { |
| 104 | /* skip character code */ |
| 105 | fuget1(p)((unsigned)getc(p)); |
| 106 | min_m = fuget1(p)((unsigned)getc(p)); /* this is max_m - min_m */ |
| 107 | max_m = fuget1(p)((unsigned)getc(p)); |
| 108 | min_n = fuget1(p)((unsigned)getc(p)); /* this is max_n - min_n */ |
| 109 | max_n = fuget1(p)((unsigned)getc(p)); |
| 110 | min_m = max_m - min_m; |
| 111 | min_n = max_n - min_n; |
| 112 | } else { |
| 113 | mdvi_error(_("GF: invalid opcode %d in character %d\n")gettext("GF: invalid opcode %d in character %d\n"), |
| 114 | op, ch->code); |
| 115 | return -1; |
| 116 | } |
| 117 | |
| 118 | ch->x = -min_m; |
| 119 | ch->y = max_n; |
| 120 | ch->width = max_m - min_m + 1; |
| 121 | ch->height = max_n - min_n + 1; |
| 122 | map = bitmap_alloc(ch->width, ch->height); |
| 123 | |
| 124 | ch->glyph.data = map; |
| 125 | ch->glyph.x = ch->x; |
| 126 | ch->glyph.y = ch->y; |
| 127 | ch->glyph.w = ch->width; |
| 128 | ch->glyph.h = ch->height; |
| 129 | |
| 130 | #define COLOR(x)((x) ? "BLACK" : "WHITE") ((x) ? "BLACK" : "WHITE") |
| 131 | |
| 132 | paint_switch = WHITE0; |
| 133 | x = y = 0; |
| 134 | line = map->data; |
| 135 | bpl = map->stride; |
| 136 | DEBUG((DBG_BITMAPS, "(gf) reading character %d\n", ch->code))__debug ((1 << 8), "(gf) reading character %d\n", ch-> code); |
| 137 | while((op = fuget1(p)((unsigned)getc(p))) != GF_EOC69) { |
| 138 | Int32 n; |
| 139 | |
| 140 | if(feof(p)) |
| 141 | break; |
| 142 | if(op == GF_PAINT00) { |
| 143 | DEBUG((DBG_BITMAPS, "(gf) Paint0 %s -> %s\n",__debug ((1 << 8), "(gf) Paint0 %s -> %s\n", ((paint_switch ) ? "BLACK" : "WHITE"), ((!paint_switch) ? "BLACK" : "WHITE") ) |
| 144 | COLOR(paint_switch), COLOR(!paint_switch)))__debug ((1 << 8), "(gf) Paint0 %s -> %s\n", ((paint_switch ) ? "BLACK" : "WHITE"), ((!paint_switch) ? "BLACK" : "WHITE") ); |
| 145 | paint_switch = !paint_switch; |
| 146 | } else if(op <= GF_PAINT366) { |
| 147 | if(op < GF_PAINT164) |
| 148 | par = op; |
| 149 | else |
| 150 | par = fugetn(p, op - GF_PAINT164 + 1); |
| 151 | if(y >= ch->height || x + par >= ch->width) |
| 152 | goto toobig; |
| 153 | /* paint everything between columns x and x + par - 1 */ |
| 154 | DEBUG((DBG_BITMAPS, "(gf) Paint %d %s from (%d,%d)\n",__debug ((1 << 8), "(gf) Paint %d %s from (%d,%d)\n", par , ((paint_switch) ? "BLACK" : "WHITE"), x, y) |
| 155 | par, COLOR(paint_switch), x, y))__debug ((1 << 8), "(gf) Paint %d %s from (%d,%d)\n", par , ((paint_switch) ? "BLACK" : "WHITE"), x, y); |
| 156 | if(paint_switch == BLACK1) |
| 157 | bitmap_paint_bits(line + (x / BITMAP_BITS(4 << 3)), |
| 158 | x % BITMAP_BITS(4 << 3), par); |
| 159 | paint_switch = !paint_switch; |
| 160 | x += par; |
| 161 | } else if(op >= GF_NEW_ROW_074 && op <= GF_NEW_ROW_MAX238) { |
| 162 | y++; |
| 163 | line = bm_offset(line, bpl)(BmUnit *)((Uchar *)(line) + (bpl)); |
| 164 | x = op - GF_NEW_ROW_074; |
| 165 | paint_switch = BLACK1; |
| 166 | DEBUG((DBG_BITMAPS, "(gf) new_row_%d\n", x))__debug ((1 << 8), "(gf) new_row_%d\n", x); |
| 167 | } else switch(op) { |
| 168 | case GF_SKIP070: |
| 169 | y++; |
| 170 | line = bm_offset(line, bpl)(BmUnit *)((Uchar *)(line) + (bpl)); |
| 171 | x = 0; |
| 172 | paint_switch = WHITE0; |
| 173 | DEBUG((DBG_BITMAPS, "(gf) skip_0\n"))__debug ((1 << 8), "(gf) skip_0\n"); |
| 174 | break; |
| 175 | case GF_SKIP171: |
| 176 | case GF_SKIP272: |
| 177 | case GF_SKIP373: |
| 178 | par = fugetn(p, op - GF_SKIP171 + 1); |
| 179 | y += par + 1; |
| 180 | line = bm_offset(line, (par + 1) * bpl)(BmUnit *)((Uchar *)(line) + ((par + 1) * bpl)); |
| 181 | x = 0; |
| 182 | paint_switch = WHITE0; |
| 183 | DEBUG((DBG_BITMAPS, "(gf) skip_%d\n", op - GF_SKIP1))__debug ((1 << 8), "(gf) skip_%d\n", op - 71); |
| 184 | break; |
| 185 | case GF_XXX1239: |
| 186 | case GF_XXX2240: |
| 187 | case GF_XXX3241: |
| 188 | case GF_XXX4242: { |
| 189 | #ifndef NODEBUG |
| 190 | char *s; |
| 191 | |
| 192 | s = read_string(p, op - GF_XXX1239 + 1, NULL((void*)0), 0); |
| 193 | DEBUG((DBG_SPECIAL, "(gf) Character %d: Special \"%s\"\n",__debug ((1 << 5), "(gf) Character %d: Special \"%s\"\n" , ch->code, s) |
| 194 | ch->code, s))__debug ((1 << 5), "(gf) Character %d: Special \"%s\"\n" , ch->code, s); |
| 195 | mdvi_free(s); |
| 196 | #else |
| 197 | n = fugetn(p, op - GF_XXX1239 + 1); |
| 198 | fseek(p, (long)n, SEEK_CUR1); |
| 199 | #endif |
| 200 | break; |
| 201 | } |
| 202 | case GF_YYY243: |
| 203 | n = fuget4(p)fugetn((p), 4); |
| 204 | DEBUG((DBG_SPECIAL, "(gf) Character %d: MF special %u\n",__debug ((1 << 5), "(gf) Character %d: MF special %u\n" , ch->code, n) |
| 205 | ch->code, n))__debug ((1 << 5), "(gf) Character %d: MF special %u\n" , ch->code, n); |
| 206 | break; |
| 207 | case GF_NOOP244: |
| 208 | DEBUG((DBG_BITMAPS, "(gf) no_op\n"))__debug ((1 << 8), "(gf) no_op\n"); |
| 209 | break; |
| 210 | default: |
| 211 | mdvi_error(_("(gf) Character %d: invalid opcode %d\n")gettext("(gf) Character %d: invalid opcode %d\n"), |
| 212 | ch->code, op); |
| 213 | goto error; |
| 214 | } |
| 215 | /* chech that we're still inside the bitmap */ |
| 216 | if(x > ch->width || y > ch->height) |
| 217 | goto toobig; |
| 218 | DEBUG((DBG_BITMAPS, "(gf) curr_loc @ (%d,%d)\n", x, y))__debug ((1 << 8), "(gf) curr_loc @ (%d,%d)\n", x, y); |
| 219 | } |
| 220 | |
| 221 | if(op != GF_EOC69) |
| 222 | goto error; |
| 223 | DEBUG((DBG_BITMAPS, "(gf) end of character %d\n", ch->code))__debug ((1 << 8), "(gf) end of character %d\n", ch-> code); |
| 224 | return 0; |
| 225 | |
| 226 | toobig: |
| 227 | mdvi_error(_("(gf) character %d has an incorrect bounding box\n")gettext("(gf) character %d has an incorrect bounding box\n"), |
| 228 | ch->code); |
| 229 | error: |
| 230 | bitmap_destroy(map); |
| 231 | ch->glyph.data = NULL((void*)0); |
| 232 | return -1; |
| 233 | } |
| 234 | |
| 235 | static int gf_load_font(DviParams *unused, DviFont *font) |
| 236 | { |
| 237 | int i; |
| 238 | int n; |
| 239 | int loc; |
| 240 | int hic; |
| 241 | FILE *p; |
| 242 | Int32 word; |
| 243 | int op; |
| 244 | long alpha, beta, z; |
| 245 | #ifndef NODEBUG |
| 246 | char s[256]; |
| 247 | #endif |
| 248 | |
| 249 | p = font->in; |
| 250 | |
| 251 | /* check preamble */ |
| 252 | loc = fuget1(p)((unsigned)getc(p)); hic = fuget1(p)((unsigned)getc(p)); |
| 253 | if(loc != GF_PRE247 || hic != GF_ID131) |
| 254 | goto badgf; |
| 255 | loc = fuget1(p)((unsigned)getc(p)); |
| 256 | #ifndef NODEBUG |
| 257 | for(i = 0; i < loc; i++) |
| 258 | s[i] = fuget1(p)((unsigned)getc(p)); |
| 259 | s[i] = 0; |
| 260 | DEBUG((DBG_FONTS, "(gf) %s: %s\n", font->fontname, s))__debug ((1 << 1), "(gf) %s: %s\n", font->fontname, s ); |
| 261 | #else |
| 262 | fseek(p, (long)loc, SEEK_CUR1); |
| 263 | #endif |
| 264 | /* now read character locators in postamble */ |
| 265 | if(fseek(p, (long)-1, SEEK_END2) == -1) |
| 266 | return -1; |
| 267 | |
| 268 | n = 0; |
| 269 | while((op = fuget1(p)((unsigned)getc(p))) == GF_TRAILER223) { |
| 270 | if(fseek(p, (long)-2, SEEK_CUR1) < 0) |
| 271 | break; |
| 272 | n++; |
| 273 | } |
| 274 | if(op != GF_ID131 || n < 4) |
| 275 | goto badgf; |
| 276 | /* get the pointer to the postamble */ |
| 277 | fseek(p, (long)-5, SEEK_CUR1); |
| 278 | op = fuget4(p)fugetn((p), 4); |
| 279 | /* jump to it */ |
| 280 | fseek(p, (long)op, SEEK_SET0); |
| 281 | if(fuget1(p)((unsigned)getc(p)) != GF_POST248) |
| 282 | goto badgf; |
| 283 | /* skip pointer to last EOC */ |
| 284 | fuget4(p)fugetn((p), 4); |
| 285 | /* get the design size */ |
| 286 | font->design = fuget4(p)fugetn((p), 4); |
| 287 | /* the checksum */ |
| 288 | word = fuget4(p)fugetn((p), 4); |
| 289 | if(word && font->checksum && font->checksum != word) { |
| 290 | mdvi_warning(_("%s: bad checksum (expected %u, found %u)\n")gettext("%s: bad checksum (expected %u, found %u)\n"), |
This statement is never executed | |
| 291 | font->fontname, font->checksum, word); |
| 292 | } else if(!font->checksum) |
| 293 | font->checksum = word; |
| 294 | /* skip pixels per point ratio */ |
| 295 | fuget4(p)fugetn((p), 4); |
| 296 | fuget4(p)fugetn((p), 4); |
| 297 | font->chars = xnalloc(DviFontChar, 256)(DviFontChar *)mdvi_calloc((256), sizeof(DviFontChar)); |
| 298 | for(loc = 0; loc < 256; loc++) |
| 299 | font->chars[loc].offset = 0; |
| 300 | /* skip glyph "bounding box" */ |
| 301 | fseek(p, (long)16, SEEK_CUR1); |
| 302 | loc = 256; |
| 303 | hic = -1; |
| 304 | TFMPREPARE(font->scale, z, alpha, beta)do { alpha = 16; z = (font->scale); while(z > 040000000L ) { z >>= 1; alpha <<= 1; } beta = 256 / alpha; alpha *= z; } while(0); |
| 305 | while((op = fuget1(p)((unsigned)getc(p))) != GF_POST_POST249) { |
| 306 | DviFontChar *ch; |
| 307 | int cc; |
| 308 | |
| 309 | /* get the character code */ |
| 310 | cc = fuget1(p)((unsigned)getc(p)); |
| 311 | if(cc < loc) |
| 312 | loc = cc; |
| 313 | if(cc > hic) |
| 314 | hic = cc; |
| 315 | ch = &font->chars[cc]; |
| 316 | switch(op) { |
| 317 | case GF_LOC245: |
| 318 | fsget4(p)fsgetn((p), 4); /* skip dx */ |
| 319 | fsget4(p)fsgetn((p), 4); /* skip dy */ |
| 320 | break; |
| 321 | case GF_LOC0246: |
| 322 | fuget1(p)((unsigned)getc(p)); /* skip dx */ |
| 323 | /* dy assumed 0 */ |
| 324 | break; |
| 325 | default: |
| 326 | mdvi_error(_("%s: junk in postamble\n")gettext("%s: junk in postamble\n"), font->fontname); |
| 327 | goto error; |
| 328 | } |
| 329 | ch->code = cc; |
| 330 | ch->tfmwidth = fuget4(p)fugetn((p), 4); |
| 331 | ch->tfmwidth = TFMSCALE(ch->tfmwidth, z, alpha, beta)(((((z) >> 24) & 0xff) == 255) ? ((((((((z)) & 0xff ) * ((ch->tfmwidth))) >> 8) + (((((z)) >> 8) & 0xff) * ((ch->tfmwidth)))) >> 8) + ((((z)) >> 16) & 0xff) * ((ch->tfmwidth))) / (beta) - (alpha) : ( (((((((z)) & 0xff) * ((ch->tfmwidth))) >> 8) + ( ((((z)) >> 8) & 0xff) * ((ch->tfmwidth)))) >> 8) + ((((z)) >> 16) & 0xff) * ((ch->tfmwidth))) / (beta)); |
| 332 | ch->offset = fuget4(p)fugetn((p), 4); |
| 333 | if(ch->offset == -1) |
| 334 | ch->offset = 0; |
| 335 | /* initialize the rest of the glyph information */ |
| 336 | ch->x = 0; |
| 337 | ch->y = 0; |
| 338 | ch->width = 0; |
| 339 | ch->height = 0; |
| 340 | ch->glyph.data = NULL((void*)0); |
| 341 | ch->shrunk.data = NULL((void*)0); |
| 342 | ch->grey.data = NULL((void*)0); |
| 343 | ch->flags = 0; |
| 344 | ch->loaded = 0; |
| 345 | } |
| 346 | |
| 347 | if(op != GF_POST_POST249) |
| 348 | goto badgf; |
| 349 | |
| 350 | if(loc > 0 || hic < 255) { |
| 351 | /* shrink to optimal size */ |
| 352 | memmove(font->chars, font->chars + loc, |
| 353 | (hic - loc + 1) * sizeof(DviFontChar)); |
| 354 | font->chars = xresize(font->chars,(DviFontChar *)mdvi_realloc((font->chars), (hic - loc + 1) * sizeof(DviFontChar)) |
| 355 | DviFontChar, hic - loc + 1)(DviFontChar *)mdvi_realloc((font->chars), (hic - loc + 1) * sizeof(DviFontChar)); |
| 356 | } |
| 357 | font->loc = loc; |
| 358 | font->hic = hic; |
| 359 | |
| 360 | return 0; |
| 361 | |
| 362 | badgf: |
| 363 | mdvi_error(_("%s: File corrupted, or not a GF file\n")gettext("%s: File corrupted, or not a GF file\n"), font->fontname); |
| 364 | error: |
| 365 | if(font->chars) { |
| 366 | mdvi_free(font->chars); |
| 367 | font->chars = NULL((void*)0); |
| 368 | } |
| 369 | font->loc = font->hic = 0; |
| 370 | return -1; |
| 371 | } |
| 372 | |
| 373 | static int gf_font_get_glyph(DviParams *params, DviFont *font, int code) |
| 374 | { |
| 375 | DviFontChar *ch; |
| 376 | |
| 377 | if(code < font->loc || code > font->hic || !font->chars) |
| 378 | return -1; |
| 379 | ch = &font->chars[code - font->loc]; |
| 380 | |
| 381 | if(!ch->loaded) { |
| 382 | if(ch->offset == 0) |
| 383 | return -1; |
| 384 | DEBUG((DBG_GLYPHS, "(gf) %s: loading GF glyph for character %d\n",__debug ((1 << 7), "(gf) %s: loading GF glyph for character %d\n" , font->fontname, code) |
| 385 | font->fontname, code))__debug ((1 << 7), "(gf) %s: loading GF glyph for character %d\n" , font->fontname, code); |
| 386 | if(font->in == NULL((void*)0) && font_reopen(font) < 0) |
| 387 | return -1; |
| 388 | if(fseek(font->in, ch->offset, SEEK_SET0) == -1) |
| 389 | return -1; |
| 390 | if(gf_read_bitmap(font->in, ch) < 0) |
| 391 | return -1; |
| 392 | ch->loaded = 1; |
| 393 | } |
| 394 | return 0; |
| 395 | } |