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
24 static ListHead fontlist;
26 extern char *_mdvi_fallback_font;
28 extern void vf_free_macros(DviFont *);
30 #define finfo search.info
31 #define TYPENAME(font) \
32 ((font)->finfo ? (font)->finfo->name : "none")
34 int font_reopen(DviFont *font)
37 fseek(font->in, (long)0, SEEK_SET);
38 else if((font->in = fopen(font->filename, "r")) == NULL) {
39 DEBUG((DBG_FILES, "reopen(%s) -> Error\n", font->filename));
42 DEBUG((DBG_FILES, "reopen(%s) -> Ok.\n", font->filename));
46 /* used from context: params and device */
47 static int load_font_file(DviParams *params, DviFont *font)
51 if(SEARCH_DONE(font->search))
53 if(font->in == NULL && font_reopen(font) < 0)
55 DEBUG((DBG_FONTS, "%s: loading %s font from `%s'\n",
57 font->finfo->name, font->filename));
59 status = font->finfo->load(params, font);
60 } while(status < 0 && mdvi_font_retry(params, font) == 0);
67 DEBUG((DBG_FONTS, "reload_font(%s) -> %s\n",
68 font->fontname, status < 0 ? "Error" : "Ok"));
72 void font_drop_one(DviFontRef *ref)
78 /* drop all children */
79 for(ref = font->subfonts; ref; ref = ref->next) {
80 /* just adjust the reference counts */
83 if(--font->links == 0) {
85 * this font doesn't have any more references, but
86 * we still keep it around in case a virtual font
93 if(LIST(font) != fontlist.tail) {
94 /* move it to the end of the list */
95 listh_remove(&fontlist, LIST(font));
96 listh_append(&fontlist, LIST(font));
99 DEBUG((DBG_FONTS, "%s: reference dropped, %d more left\n",
100 font->fontname, font->links));
103 void font_drop_chain(DviFontRef *head)
107 for(; (ptr = head); ) {
113 int font_free_unused(DviDevice *dev)
115 DviFont *font, *next;
118 DEBUG((DBG_FONTS, "destroying unused fonts\n"));
119 for(font = (DviFont *)fontlist.head; font; font = next) {
126 DEBUG((DBG_FONTS, "removing unused %s font `%s'\n",
127 TYPENAME(font), font->fontname));
128 listh_remove(&fontlist, LIST(font));
131 /* get rid of subfonts (but can't use `drop_chain' here) */
132 for(; (ref = font->subfonts); ) {
133 font->subfonts = ref->next;
136 /* remove this font */
137 font_reset_font_glyphs(dev, font, MDVI_FONTSEL_GLYPH);
138 /* let the font destroy its private data */
139 if(font->finfo->freedata)
140 font->finfo->freedata(font);
141 /* destroy characters */
143 mdvi_free(font->chars);
144 mdvi_free(font->fontname);
145 mdvi_free(font->filename);
148 DEBUG((DBG_FONTS, "%d unused fonts removed\n", count));
152 /* used from context: params and device */
155 DviParams *params, /* rendering parameters */
156 Int32 id, /* external id number */
157 const char *name, /* font name */
158 Int32 sum, /* checksum (from DVI of VF) */
159 int hdpi, /* resolution */
161 Int32 scale) /* scaling factor (from DVI or VF) */
165 DviFontRef *subfont_ref;
167 /* see if there is a font with the same characteristics */
168 for(font = (DviFont *)fontlist.head; font; font = font->next) {
169 if(strcmp(name, font->fontname) == 0
170 && (!sum || !font->checksum || font->checksum == sum)
171 && font->hdpi == hdpi
172 && font->vdpi == vdpi
173 && font->scale == scale)
176 /* try to load the font */
178 font = mdvi_add_font(name, sum, hdpi, vdpi, scale);
181 listh_append(&fontlist, LIST(font));
183 if(!font->links && !font->chars && load_font_file(params, font) < 0) {
184 DEBUG((DBG_FONTS, "font_reference(%s) -> Error\n", name));
187 ref = xalloc(DviFontRef);
191 for(subfont_ref = font->subfonts; subfont_ref; subfont_ref = subfont_ref->next) {
192 /* just adjust the reference counts */
193 subfont_ref->ref->links++;
198 if(LIST(font) != fontlist.head) {
199 listh_remove(&fontlist, LIST(font));
200 listh_prepend(&fontlist, LIST(font));
203 DEBUG((DBG_FONTS, "font_reference(%s) -> %d links\n",
204 font->fontname, font->links));
208 void font_transform_glyph(DviOrientation orient, DviGlyph *g)
213 map = (BITMAP *)g->data;
214 if(MDVI_GLYPH_ISEMPTY(map))
217 /* put the glyph in the right orientation */
219 case MDVI_ORIENT_TBLR:
221 case MDVI_ORIENT_TBRL:
223 if(map) bitmap_flip_horizontally(map);
225 case MDVI_ORIENT_BTLR:
227 if(map) bitmap_flip_vertically(map);
229 case MDVI_ORIENT_BTRL:
232 if(map) bitmap_flip_diagonally(map);
234 case MDVI_ORIENT_RP90:
235 if(map) bitmap_rotate_counter_clockwise(map);
242 case MDVI_ORIENT_RM90:
243 if(map) bitmap_rotate_clockwise(map);
250 case MDVI_ORIENT_IRP90:
251 if(map) bitmap_flip_rotate_counter_clockwise(map);
258 case MDVI_ORIENT_IRM90:
259 if(map) bitmap_flip_rotate_clockwise(map);
269 static int load_one_glyph(DviContext *dvi, DviFont *font, int code)
276 ch = FONTCHAR(font, code);
277 DEBUG((DBG_GLYPHS, "loading glyph code %d in %s (at %u)\n",
278 code, font->fontname, ch->offset));
280 if(font->finfo->getglyph == NULL) {
281 /* font type does not need to load glyphs (e.g. vf) */
285 status = font->finfo->getglyph(&dvi->params, font, code);
288 /* get the glyph again (font->chars may have changed) */
289 ch = FONTCHAR(font, code);
291 map = (BITMAP *)ch->glyph.data;
292 if(DEBUGGING(BITMAP_DATA)) {
293 DEBUG((DBG_BITMAP_DATA,
294 "%s: new %s bitmap for character %d:\n",
295 font->fontname, TYPENAME(font), code));
296 if(MDVI_GLYPH_ISEMPTY(map))
297 DEBUG((DBG_BITMAP_DATA, "blank bitmap\n"));
299 bitmap_print(stderr, map);
302 /* check if we have to scale it */
303 if(!font->finfo->scalable && font->hdpi != font->vdpi) {
306 /* we scale it ourselves */
307 d = Max(font->hdpi, font->vdpi);
310 if(ch->width && ch->height && (hs > 1 || vs > 1)) {
315 "%s: scaling glyph %d to resolution %dx%d\n",
316 font->fontname, code, font->hdpi, font->vdpi));
317 h = dvi->params.hshrink;
318 v = dvi->params.vshrink;
319 d = dvi->params.density;
320 dvi->params.hshrink = hs;
321 dvi->params.vshrink = vs;
322 dvi->params.density = 50;
324 font->finfo->shrink0(dvi, font, ch, &glyph);
325 /* restore parameters */
326 dvi->params.hshrink = h;
327 dvi->params.vshrink = v;
328 dvi->params.density = d;
329 /* update glyph data */
330 if(!MDVI_GLYPH_ISEMPTY(ch->glyph.data))
331 bitmap_destroy((BITMAP *)ch->glyph.data);
332 ch->glyph.data = glyph.data;
333 ch->glyph.x = glyph.x;
334 ch->glyph.y = glyph.y;
335 ch->glyph.w = glyph.w;
336 ch->glyph.h = glyph.h;
340 font_transform_glyph(dvi->params.orientation, &ch->glyph);
345 DviFontChar *font_get_glyph(DviContext *dvi, DviFont *font, int code)
350 /* if we have not loaded the font yet, do so now */
351 if(!font->chars && load_font_file(&dvi->params, font) < 0)
354 /* get the unscaled glyph, maybe loading it from disk */
355 ch = FONTCHAR(font, code);
356 if(!ch || !glyph_present(ch))
358 if(!ch->loaded && load_one_glyph(dvi, font, code) == -1) {
359 if(font->chars == NULL) {
360 /* we need to try another font class */
365 /* yes, we have to do this again */
366 ch = FONTCHAR(font, code);
368 /* Got the glyph. If we also have the right scaled glyph, do no more */
369 if(!ch->width || !ch->height ||
370 font->finfo->getglyph == NULL ||
371 (dvi->params.hshrink == 1 && dvi->params.vshrink == 1))
374 /* If the glyph is empty, we just need to shrink the box */
375 if(ch->missing || MDVI_GLYPH_ISEMPTY(ch->glyph.data)) {
376 if(MDVI_GLYPH_UNSET(ch->shrunk.data))
377 mdvi_shrink_box(dvi, font, ch, &ch->shrunk);
379 } else if(MDVI_ENABLED(dvi, MDVI_PARAM_ANTIALIASED)) {
381 ch->fg == dvi->curr_fg &&
382 ch->bg == dvi->curr_bg)
385 if(dvi->device.free_image)
386 dvi->device.free_image(ch->grey.data);
387 ch->grey.data = NULL;
389 font->finfo->shrink1(dvi, font, ch, &ch->grey);
390 } else if(!ch->shrunk.data)
391 font->finfo->shrink0(dvi, font, ch, &ch->shrunk);
396 void font_reset_one_glyph(DviDevice *dev, DviFontChar *ch, int what)
398 if(!glyph_present(ch))
400 if(what & MDVI_FONTSEL_BITMAP) {
401 if(MDVI_GLYPH_NONEMPTY(ch->shrunk.data))
402 bitmap_destroy((BITMAP *)ch->shrunk.data);
403 ch->shrunk.data = NULL;
405 if(what & MDVI_FONTSEL_GREY) {
406 if(MDVI_GLYPH_NONEMPTY(ch->grey.data)) {
408 dev->free_image(ch->grey.data);
410 ch->grey.data = NULL;
412 if(what & MDVI_FONTSEL_GLYPH) {
413 if(MDVI_GLYPH_NONEMPTY(ch->glyph.data))
414 bitmap_destroy((BITMAP *)ch->glyph.data);
415 ch->glyph.data = NULL;
420 void font_reset_font_glyphs(DviDevice *dev, DviFont *font, int what)
425 if(what & MDVI_FONTSEL_GLYPH)
426 what |= MDVI_FONTSEL_BITMAP|MDVI_FONTSEL_GREY;
430 for(ref = font->subfonts; ref; ref = ref->next)
431 font_reset_font_glyphs(dev, ref->ref, what);
434 DEBUG((DBG_FILES, "close(%s)\n", font->filename));
438 if(font->finfo->getglyph == NULL)
440 DEBUG((DBG_FONTS, "resetting glyphs in font `%s'\n", font->fontname));
441 for(ch = font->chars, i = font->loc; i <= font->hic; ch++, i++) {
442 if(glyph_present(ch))
443 font_reset_one_glyph(dev, ch, what);
445 if((what & MDVI_FONTSEL_GLYPH) && font->finfo->reset)
446 font->finfo->reset(font);
449 void font_reset_chain_glyphs(DviDevice *dev, DviFontRef *head, int what)
453 for(ref = head; ref; ref = ref->next)
454 font_reset_font_glyphs(dev, ref->ref, what);
457 static int compare_refs(const void *p1, const void *p2)
459 return ((*(DviFontRef **)p1)->fontid - (*(DviFontRef **)p2)->fontid);
462 void font_finish_definitions(DviContext *dvi)
465 DviFontRef **map, *ref;
467 /* first get rid of unused fonts */
468 font_free_unused(&dvi->device);
470 if(dvi->fonts == NULL) {
471 warning(_("%s: no fonts defined\n"), dvi->filename);
474 map = xnalloc(DviFontRef *, dvi->nfonts);
475 for(count = 0, ref = dvi->fonts; ref; ref = ref->next)
477 /* sort the array by font id */
478 qsort(map, dvi->nfonts, sizeof(DviFontRef *), compare_refs);
482 DviFontRef *font_find_flat(DviContext *dvi, Int32 id)
486 for(ref = dvi->fonts; ref; ref = ref->next)
487 if(ref->fontid == id)
492 DviFontRef *font_find_mapped(DviContext *dvi, Int32 id)
497 /* do a binary search */
498 lo = 0; hi = dvi->nfonts;
504 sign = (map[n]->fontid - id);