summaryrefslogtreecommitdiff
path: root/backend/dvi/mdvi-lib/font.c
blob: 2f655df0b70b7684fa91d8eaa0bbdc1fb587ed93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*
 * Copyright (C) 2000, Matias Atria
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <config.h>
#include <stdlib.h>

#include "mdvi.h"
#include "private.h"

static ListHead fontlist;

extern char *_mdvi_fallback_font;

extern void vf_free_macros(DviFont *);

#define finfo	search.info
#define TYPENAME(font)	\
	((font)->finfo ? (font)->finfo->name : "none")

int	font_reopen(DviFont *font)
{
	if(font->in)
		fseek(font->in, (long)0, SEEK_SET);
	else if((font->in = fopen(font->filename, "rb")) == NULL) {
		DEBUG((DBG_FILES, "reopen(%s) -> Error\n", font->filename));
		return -1;
	}
	DEBUG((DBG_FILES, "reopen(%s) -> Ok.\n", font->filename));
	return 0;
}

/* used from context: params and device */
static int load_font_file(DviParams *params, DviFont *font)
{
	int	status;
			
	if(SEARCH_DONE(font->search))
		return -1;
	if(font->in == NULL && font_reopen(font) < 0)
		return -1;
	DEBUG((DBG_FONTS, "%s: loading %s font from `%s'\n",
		font->fontname,
		font->finfo->name, font->filename));
	do {
		status = font->finfo->load(params, font);
	} while(status < 0 && mdvi_font_retry(params, font) == 0);
	if(status < 0)
		return -1;
	if(font->in) {
		fclose(font->in);
		font->in = NULL;
	}
	DEBUG((DBG_FONTS, "reload_font(%s) -> %s\n",
		font->fontname, status < 0 ? "Error" : "Ok"));
	return 0;
}

void	font_drop_one(DviFontRef *ref)
{
	DviFont *font;
	
	font = ref->ref;
	mdvi_free(ref);
	/* drop all children */
	for(ref = font->subfonts; ref; ref = ref->next) {
		/* just adjust the reference counts */
		ref->ref->links--;
	}
	if(--font->links == 0) {
		/* 
		 * this font doesn't have any more references, but
		 * we still keep it around in case a virtual font
		 * requests it.
		 */
		if(font->in) {
			fclose(font->in);
			font->in = NULL;
		}
		if(LIST(font) != fontlist.tail) {
			/* move it to the end of the list */
			listh_remove(&fontlist, LIST(font));
			listh_append(&fontlist, LIST(font));
		}
	}
	DEBUG((DBG_FONTS, "%s: reference dropped, %d more left\n",
		font->fontname, font->links));
}

void	font_drop_chain(DviFontRef *head)
{
	DviFontRef *ptr;
	
	for(; (ptr = head); ) {
		head = ptr->next;
		font_drop_one(ptr);
	}
}

int	font_free_unused(DviDevice *dev)
{
	DviFont	*font, *next;
	int	count = 0;

	DEBUG((DBG_FONTS, "destroying unused fonts\n"));	
	for(font = (DviFont *)fontlist.head; font; font = next) {
		DviFontRef *ref;
		
		next = font->next;
		if(font->links)
			continue;
		count++;
		DEBUG((DBG_FONTS, "removing unused %s font `%s'\n", 
			TYPENAME(font), font->fontname));
		listh_remove(&fontlist, LIST(font));
		if(font->in)
			fclose(font->in);
		/* get rid of subfonts (but can't use `drop_chain' here) */
		for(; (ref = font->subfonts); ) {
			font->subfonts = ref->next;
			mdvi_free(ref);
		}
		/* remove this font */
		font_reset_font_glyphs(dev, font, MDVI_FONTSEL_GLYPH);
		/* let the font destroy its private data */
		if(font->finfo->freedata)
			font->finfo->freedata(font);
		/* destroy characters */
		if(font->chars)
			mdvi_free(font->chars);
		mdvi_free(font->fontname);
		mdvi_free(font->filename);
		mdvi_free(font);
	}
	DEBUG((DBG_FONTS, "%d unused fonts removed\n", count));
	return count;
}

/* used from context: params and device */
DviFontRef *
font_reference(
	DviParams *params, 	/* rendering parameters */
	Int32 id, 		/* external id number */
	const char *name, 	/* font name */
	Int32 sum, 		/* checksum (from DVI of VF) */
	int hdpi, 		/* resolution */
	int vdpi,
	Int32 scale)		/* scaling factor (from DVI or VF) */
{
	DviFont	*font;
	DviFontRef *ref;
	DviFontRef *subfont_ref;
	
	/* see if there is a font with the same characteristics */
	for(font = (DviFont *)fontlist.head; font; font = font->next) {
		if(strcmp(name, font->fontname) == 0
		   && (!sum || !font->checksum || font->checksum == sum)
		   && font->hdpi == hdpi
		   && font->vdpi == vdpi
		   && font->scale == scale)
		   	break;
	}
	/* try to load the font */
	if(font == NULL) {
		font = mdvi_add_font(name, sum, hdpi, vdpi, scale);
		if(font == NULL)
			return NULL;
		listh_append(&fontlist, LIST(font));
	}
	if(!font->links && !font->chars && load_font_file(params, font) < 0) {
		DEBUG((DBG_FONTS, "font_reference(%s) -> Error\n", name));
		return NULL;
	}
	ref = xalloc(DviFontRef);
	ref->ref = font;

	font->links++;
	for(subfont_ref = font->subfonts; subfont_ref; subfont_ref = subfont_ref->next) {
		/* just adjust the reference counts */
		subfont_ref->ref->links++;
	}

	ref->fontid = id;

	if(LIST(font) != fontlist.head) {
		listh_remove(&fontlist, LIST(font));
		listh_prepend(&fontlist, LIST(font));
	}

	DEBUG((DBG_FONTS, "font_reference(%s) -> %d links\n",
		font->fontname, font->links));
	return ref;
}

void	font_transform_glyph(DviOrientation orient, DviGlyph *g)
{
	BITMAP	*map;
	int	x, y;
	
	map = (BITMAP *)g->data;
	if(MDVI_GLYPH_ISEMPTY(map))
		map = NULL;

	/* put the glyph in the right orientation */
	switch(orient) {
	case MDVI_ORIENT_TBLR:
		break;
	case MDVI_ORIENT_TBRL:
		g->x = g->w - g->x;
		if(map) bitmap_flip_horizontally(map);
		break;
	case MDVI_ORIENT_BTLR:
		g->y = g->h - g->y;
		if(map) bitmap_flip_vertically(map);
		break;
	case MDVI_ORIENT_BTRL:
		g->x = g->w - g->x;
		g->y = g->h - g->y;
		if(map) bitmap_flip_diagonally(map);
		break;
	case MDVI_ORIENT_RP90:
		if(map) bitmap_rotate_counter_clockwise(map);
		y = g->y;
		x = g->w - g->x;
		g->x = y;
		g->y = x;
		SWAPINT(g->w, g->h);
		break;
	case MDVI_ORIENT_RM90: 
		if(map) bitmap_rotate_clockwise(map);
		y = g->h - g->y;
		x = g->x;
		g->x = y;
		g->y = x;
		SWAPINT(g->w, g->h);
		break;
	case MDVI_ORIENT_IRP90:
		if(map) bitmap_flip_rotate_counter_clockwise(map);
		y = g->y;
		x = g->x;
		g->x = y;
		g->y = x;
		SWAPINT(g->w, g->h);
		break;
	case MDVI_ORIENT_IRM90:
		if(map) bitmap_flip_rotate_clockwise(map);
		y = g->h - g->y;
		x = g->w - g->x;
		g->x = y;
		g->y = x;
		SWAPINT(g->w, g->h);
		break;
	}
}

static int load_one_glyph(DviContext *dvi, DviFont *font, int code)
{
	BITMAP *map;
	DviFontChar *ch;
	int	status;

#ifndef NODEBUG
	ch = FONTCHAR(font, code);
	DEBUG((DBG_GLYPHS, "loading glyph code %d in %s (at %u)\n",
		code, font->fontname, ch->offset));
#endif
	if(font->finfo->getglyph == NULL) {
		/* font type does not need to load glyphs (e.g. vf) */
		return 0;
	}

	status = font->finfo->getglyph(&dvi->params, font, code);
	if(status < 0)
		return -1;
	/* get the glyph again (font->chars may have changed) */
	ch = FONTCHAR(font, code);
#ifndef NODEBUG
	map = (BITMAP *)ch->glyph.data;
	if(DEBUGGING(BITMAP_DATA)) {
		DEBUG((DBG_BITMAP_DATA,
			"%s: new %s bitmap for character %d:\n",
			font->fontname, TYPENAME(font), code));
		if(MDVI_GLYPH_ISEMPTY(map))
			DEBUG((DBG_BITMAP_DATA, "blank bitmap\n"));
		else
			bitmap_print(stderr, map);
	}
#endif
	/* check if we have to scale it */
	if(!font->finfo->scalable && font->hdpi != font->vdpi) {
		int	hs, vs, d;
		
		/* we scale it ourselves */
		d = Max(font->hdpi, font->vdpi);
		hs = d / font->hdpi;
		vs = d / font->vdpi;
		if(ch->width && ch->height && (hs > 1 || vs > 1)) {
			int	h, v;
			DviGlyph glyph;
			
			DEBUG((DBG_FONTS, 
				"%s: scaling glyph %d to resolution %dx%d\n",
				font->fontname, code, font->hdpi, font->vdpi));
			h = dvi->params.hshrink;
			v = dvi->params.vshrink;
			d = dvi->params.density;
			dvi->params.hshrink = hs;
			dvi->params.vshrink = vs;
			dvi->params.density = 50;
			/* shrink it */
			font->finfo->shrink0(dvi, font, ch, &glyph);
			/* restore parameters */
			dvi->params.hshrink = h;
			dvi->params.vshrink = v;
			dvi->params.density = d;
			/* update glyph data */
			if(!MDVI_GLYPH_ISEMPTY(ch->glyph.data))
				bitmap_destroy((BITMAP *)ch->glyph.data);
			ch->glyph.data = glyph.data;
			ch->glyph.x = glyph.x;
			ch->glyph.y = glyph.y;
			ch->glyph.w = glyph.w;
			ch->glyph.h = glyph.h;
		}
			
	}
	font_transform_glyph(dvi->params.orientation, &ch->glyph);
		
	return 0;
}

DviFontChar *font_get_glyph(DviContext *dvi, DviFont *font, int code)
{
	DviFontChar *ch;

again:
	/* if we have not loaded the font yet, do so now */
	if(!font->chars && load_font_file(&dvi->params, font) < 0)
		return NULL;
	
	/* get the unscaled glyph, maybe loading it from disk */
	ch = FONTCHAR(font, code);
	if(!ch || !glyph_present(ch))
		return NULL;
	if(!ch->loaded && load_one_glyph(dvi, font, code) == -1) {
		if(font->chars == NULL) {
			/* we need to try another font class */
			goto again;
		}
		return NULL;
	}
	/* yes, we have to do this again */
	ch = FONTCHAR(font, code);

	/* Got the glyph. If we also have the right scaled glyph, do no more */
	if(!ch->width || !ch->height ||
	   font->finfo->getglyph == NULL ||
	   (dvi->params.hshrink == 1 && dvi->params.vshrink == 1))
		return ch;
	
	/* If the glyph is empty, we just need to shrink the box */
	if(ch->missing || MDVI_GLYPH_ISEMPTY(ch->glyph.data)) {
		if(MDVI_GLYPH_UNSET(ch->shrunk.data))
			mdvi_shrink_box(dvi, font, ch, &ch->shrunk);
		return ch;
	} else if(MDVI_ENABLED(dvi, MDVI_PARAM_ANTIALIASED)) {
		if(ch->grey.data && 
		   !MDVI_GLYPH_ISEMPTY(ch->grey.data) &&
		   ch->fg == dvi->curr_fg && 
		   ch->bg == dvi->curr_bg)
		   	return ch;
		if(ch->grey.data &&
		   !MDVI_GLYPH_ISEMPTY(ch->grey.data)) {
			if(dvi->device.free_image)
				dvi->device.free_image(ch->grey.data);
			ch->grey.data = NULL;
		}
		font->finfo->shrink1(dvi, font, ch, &ch->grey);
	} else if(!ch->shrunk.data)
		font->finfo->shrink0(dvi, font, ch, &ch->shrunk);

	return ch;
}

void	font_reset_one_glyph(DviDevice *dev, DviFontChar *ch, int what)
{
	if(!glyph_present(ch))
		return;
	if(what & MDVI_FONTSEL_BITMAP) {
		if(MDVI_GLYPH_NONEMPTY(ch->shrunk.data))
			bitmap_destroy((BITMAP *)ch->shrunk.data);
		ch->shrunk.data = NULL;
	}
	if(what & MDVI_FONTSEL_GREY) {
		if(MDVI_GLYPH_NONEMPTY(ch->grey.data)) {
			if(dev->free_image)
				dev->free_image(ch->grey.data);
		}
		ch->grey.data = NULL;
	}
	if(what & MDVI_FONTSEL_GLYPH) {
		if(MDVI_GLYPH_NONEMPTY(ch->glyph.data))
			bitmap_destroy((BITMAP *)ch->glyph.data);
		ch->glyph.data = NULL;
		ch->loaded = 0;
	}
}

void	font_reset_font_glyphs(DviDevice *dev, DviFont *font, int what)
{
	int	i;
	DviFontChar *ch;
	
	if(what & MDVI_FONTSEL_GLYPH)
		what |= MDVI_FONTSEL_BITMAP|MDVI_FONTSEL_GREY;	
	if(font->subfonts) {
		DviFontRef *ref;
		
		for(ref = font->subfonts; ref; ref = ref->next)
			font_reset_font_glyphs(dev, ref->ref, what);
	}
	if(font->in) {
		DEBUG((DBG_FILES, "close(%s)\n", font->filename));
		fclose(font->in);
		font->in = NULL;
	}
	if(font->finfo->getglyph == NULL)
		return;
	DEBUG((DBG_FONTS, "resetting glyphs in font `%s'\n", font->fontname));
	for(ch = font->chars, i = font->loc; i <= font->hic; ch++, i++) {
		if(glyph_present(ch))
			font_reset_one_glyph(dev, ch, what);
	}
	if((what & MDVI_FONTSEL_GLYPH) && font->finfo->reset)
		font->finfo->reset(font);
}	

void	font_reset_chain_glyphs(DviDevice *dev, DviFontRef *head, int what)
{
	DviFontRef *ref;
	
	for(ref = head; ref; ref = ref->next)
		font_reset_font_glyphs(dev, ref->ref, what);
}

static int compare_refs(const void *p1, const void *p2)
{
	return ((*(DviFontRef **)p1)->fontid - (*(DviFontRef **)p2)->fontid);
}

void	font_finish_definitions(DviContext *dvi)
{
	int	count;
	DviFontRef **map, *ref;
	
	/* first get rid of unused fonts */
	font_free_unused(&dvi->device);

	if(dvi->fonts == NULL) {
		mdvi_warning(_("%s: no fonts defined\n"), dvi->filename);
		return;
	}
	map = xnalloc(DviFontRef *, dvi->nfonts);
	for(count = 0, ref = dvi->fonts; ref; ref = ref->next)
		map[count++] = ref;
	/* sort the array by font id */
	qsort(map, dvi->nfonts, sizeof(DviFontRef *), compare_refs);
	dvi->fontmap = map;
}

DviFontRef *font_find_flat(DviContext *dvi, Int32 id)
{
	DviFontRef *ref;
	
	for(ref = dvi->fonts; ref; ref = ref->next)
		if(ref->fontid == id)
			break;
	return ref;
}

DviFontRef *font_find_mapped(DviContext *dvi, Int32 id)
{
	int	lo, hi, n;
	DviFontRef **map;
	
	/* do a binary search */
	lo = 0; hi = dvi->nfonts;
	map = dvi->fontmap;
	while(lo < hi) {
		int	sign;
		
		n = (hi + lo) >> 1;
		sign = (map[n]->fontid - id);
		if(sign == 0)
			break;
		else if(sign < 0)
			lo = n;
		else
			hi = n;
	}
	if(lo >= hi)
		return NULL;
	return map[n];
}