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) */
166 /* see if there is a font with the same characteristics */
167 for(font = (DviFont *)fontlist.head; font; font = font->next) {
168 if(strcmp(name, font->fontname) == 0
169 && (!sum || !font->checksum || font->checksum == sum)
170 && font->hdpi == hdpi
171 && font->vdpi == vdpi
172 && font->scale == scale)
175 /* try to load the font */
177 font = mdvi_add_font(name, sum, hdpi, vdpi, scale);
180 listh_append(&fontlist, LIST(font));
182 if(!font->links && !font->chars && load_font_file(params, font) < 0) {
183 DEBUG((DBG_FONTS, "font_reference(%s) -> Error\n", name));
186 ref = xalloc(DviFontRef);
191 if(LIST(font) != fontlist.head) {
192 listh_remove(&fontlist, LIST(font));
193 listh_prepend(&fontlist, LIST(font));
196 DEBUG((DBG_FONTS, "font_reference(%s) -> %d links\n",
197 font->fontname, font->links));
201 void font_transform_glyph(DviOrientation orient, DviGlyph *g)
206 map = (BITMAP *)g->data;
207 if(MDVI_GLYPH_ISEMPTY(map))
210 /* put the glyph in the right orientation */
212 case MDVI_ORIENT_TBLR:
214 case MDVI_ORIENT_TBRL:
216 if(map) bitmap_flip_horizontally(map);
218 case MDVI_ORIENT_BTLR:
220 if(map) bitmap_flip_vertically(map);
222 case MDVI_ORIENT_BTRL:
225 if(map) bitmap_flip_diagonally(map);
227 case MDVI_ORIENT_RP90:
228 if(map) bitmap_rotate_counter_clockwise(map);
235 case MDVI_ORIENT_RM90:
236 if(map) bitmap_rotate_clockwise(map);
243 case MDVI_ORIENT_IRP90:
244 if(map) bitmap_flip_rotate_counter_clockwise(map);
251 case MDVI_ORIENT_IRM90:
252 if(map) bitmap_flip_rotate_clockwise(map);
262 static int load_one_glyph(DviContext *dvi, DviFont *font, int code)
269 ch = FONTCHAR(font, code);
270 DEBUG((DBG_GLYPHS, "loading glyph code %d in %s (at %u)\n",
271 code, font->fontname, ch->offset));
273 if(font->finfo->getglyph == NULL) {
274 /* font type does not need to load glyphs (e.g. vf) */
278 status = font->finfo->getglyph(&dvi->params, font, code);
281 /* get the glyph again (font->chars may have changed) */
282 ch = FONTCHAR(font, code);
284 map = (BITMAP *)ch->glyph.data;
285 if(DEBUGGING(BITMAP_DATA)) {
286 DEBUG((DBG_BITMAP_DATA,
287 "%s: new %s bitmap for character %d:\n",
288 font->fontname, TYPENAME(font), code));
289 if(MDVI_GLYPH_ISEMPTY(map))
290 DEBUG((DBG_BITMAP_DATA, "blank bitmap\n"));
292 bitmap_print(stderr, map);
295 /* check if we have to scale it */
296 if(!font->finfo->scalable && font->hdpi != font->vdpi) {
299 /* we scale it ourselves */
300 d = Max(font->hdpi, font->vdpi);
303 if(ch->width && ch->height && (hs > 1 || vs > 1)) {
308 "%s: scaling glyph %d to resolution %dx%d\n",
309 font->fontname, code, font->hdpi, font->vdpi));
310 h = dvi->params.hshrink;
311 v = dvi->params.vshrink;
312 d = dvi->params.density;
313 dvi->params.hshrink = hs;
314 dvi->params.vshrink = vs;
315 dvi->params.density = 50;
317 font->finfo->shrink0(dvi, font, ch, &glyph);
318 /* restore parameters */
319 dvi->params.hshrink = h;
320 dvi->params.vshrink = v;
321 dvi->params.density = d;
322 /* update glyph data */
323 if(!MDVI_GLYPH_ISEMPTY(ch->glyph.data))
324 bitmap_destroy((BITMAP *)ch->glyph.data);
325 ch->glyph.data = glyph.data;
326 ch->glyph.x = glyph.x;
327 ch->glyph.y = glyph.y;
328 ch->glyph.w = glyph.w;
329 ch->glyph.h = glyph.h;
333 font_transform_glyph(dvi->params.orientation, &ch->glyph);
338 DviFontChar *font_get_glyph(DviContext *dvi, DviFont *font, int code)
343 /* if we have not loaded the font yet, do so now */
344 if(!font->chars && load_font_file(&dvi->params, font) < 0)
347 /* get the unscaled glyph, maybe loading it from disk */
348 ch = FONTCHAR(font, code);
349 if(!ch || !glyph_present(ch))
351 if(!ch->loaded && load_one_glyph(dvi, font, code) == -1) {
352 if(font->chars == NULL) {
353 /* we need to try another font class */
358 /* yes, we have to do this again */
359 ch = FONTCHAR(font, code);
361 /* Got the glyph. If we also have the right scaled glyph, do no more */
362 if(!ch->width || !ch->height ||
363 font->finfo->getglyph == NULL ||
364 (dvi->params.hshrink == 1 && dvi->params.vshrink == 1))
367 /* If the glyph is empty, we just need to shrink the box */
368 if(ch->missing || MDVI_GLYPH_ISEMPTY(ch->glyph.data)) {
369 if(MDVI_GLYPH_UNSET(ch->shrunk.data))
370 mdvi_shrink_box(dvi, font, ch, &ch->shrunk);
372 } else if(MDVI_ENABLED(dvi, MDVI_PARAM_ANTIALIASED)) {
374 ch->fg == dvi->curr_fg &&
375 ch->bg == dvi->curr_bg)
378 if(dvi->device.free_image)
379 dvi->device.free_image(ch->grey.data);
380 ch->grey.data = NULL;
382 font->finfo->shrink1(dvi, font, ch, &ch->grey);
383 } else if(!ch->shrunk.data)
384 font->finfo->shrink0(dvi, font, ch, &ch->shrunk);
389 void font_reset_one_glyph(DviDevice *dev, DviFontChar *ch, int what)
391 if(!glyph_present(ch))
393 if(what & MDVI_FONTSEL_BITMAP) {
394 if(MDVI_GLYPH_NONEMPTY(ch->shrunk.data))
395 bitmap_destroy((BITMAP *)ch->shrunk.data);
396 ch->shrunk.data = NULL;
398 if(what & MDVI_FONTSEL_GREY) {
399 if(MDVI_GLYPH_NONEMPTY(ch->grey.data)) {
401 dev->free_image(ch->grey.data);
403 ch->grey.data = NULL;
405 if(what & MDVI_FONTSEL_GLYPH) {
406 if(MDVI_GLYPH_NONEMPTY(ch->glyph.data))
407 bitmap_destroy((BITMAP *)ch->glyph.data);
408 ch->glyph.data = NULL;
413 void font_reset_font_glyphs(DviDevice *dev, DviFont *font, int what)
418 if(what & MDVI_FONTSEL_GLYPH)
419 what |= MDVI_FONTSEL_BITMAP|MDVI_FONTSEL_GREY;
423 for(ref = font->subfonts; ref; ref = ref->next)
424 font_reset_font_glyphs(dev, ref->ref, what);
427 DEBUG((DBG_FILES, "close(%s)\n", font->filename));
431 if(font->finfo->getglyph == NULL)
433 DEBUG((DBG_FONTS, "resetting glyphs in font `%s'\n", font->fontname));
434 for(ch = font->chars, i = font->loc; i <= font->hic; ch++, i++) {
435 if(glyph_present(ch))
436 font_reset_one_glyph(dev, ch, what);
438 if((what & MDVI_FONTSEL_GLYPH) && font->finfo->reset)
439 font->finfo->reset(font);
442 void font_reset_chain_glyphs(DviDevice *dev, DviFontRef *head, int what)
446 for(ref = head; ref; ref = ref->next)
447 font_reset_font_glyphs(dev, ref->ref, what);
450 static int compare_refs(const void *p1, const void *p2)
452 return ((*(DviFontRef **)p1)->fontid - (*(DviFontRef **)p2)->fontid);
455 void font_finish_definitions(DviContext *dvi)
458 DviFontRef **map, *ref;
460 /* first get rid of unused fonts */
461 font_free_unused(&dvi->device);
463 if(dvi->fonts == NULL) {
464 warning(_("%s: no fonts defined\n"), dvi->filename);
467 map = xnalloc(DviFontRef *, dvi->nfonts);
468 for(count = 0, ref = dvi->fonts; ref; ref = ref->next)
470 /* sort the array by font id */
471 qsort(map, dvi->nfonts, sizeof(DviFontRef *), compare_refs);
475 DviFontRef *font_find_flat(DviContext *dvi, Int32 id)
479 for(ref = dvi->fonts; ref; ref = ref->next)
480 if(ref->fontid == id)
485 DviFontRef *font_find_mapped(DviContext *dvi, Int32 id)
490 /* do a binary search */
491 lo = 0; hi = dvi->nfonts;
497 sign = (map[n]->fontid - id);