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
22 #ifdef WITH_TRUETYPE_FONTS
31 static TT_Engine tt_handle;
32 static int initialized = 0;
34 typedef struct ftinfo {
46 DviFontMapInfo mapinfo;
47 DviEncoding *encoding;
50 static int tt_load_font __PROTO((DviParams *, DviFont *));
51 static int tt_font_get_glyph __PROTO((DviParams *, DviFont *, int));
52 static void tt_free_data __PROTO((DviFont *));
53 static void tt_reset_font __PROTO((DviFont *));
54 static void tt_shrink_glyph
55 __PROTO((DviContext *, DviFont *, DviFontChar *, DviGlyph *));
56 static void tt_font_remove __PROTO((FTInfo *));
58 DviFontInfo tt_font_info = {
64 mdvi_shrink_glyph_grey,
65 tt_free_data, /* free */
66 tt_reset_font, /* reset */
72 #define FT_HASH_SIZE 31
74 static ListHead ttfonts = {NULL, NULL, 0};
76 static int init_freetype(void)
80 ASSERT(initialized == 0);
81 code = TT_Init_FreeType(&tt_handle);
83 DEBUG((DBG_TT, "(tt) Init_Freetype: error %d\n", code));
86 code = TT_Init_Post_Extension(tt_handle);
88 TT_Done_FreeType(tt_handle);
96 static void tt_encode_font(DviFont *font, FTInfo *info)
98 TT_Face_Properties prop;
101 if(TT_Get_Face_Properties(info->face, &prop))
104 for(i = 0; i < prop.num_Glyphs; i++) {
108 if(TT_Get_PS_Name(info->face, i, &string))
110 ndx = mdvi_encode_glyph(info->encoding, string);
111 if(ndx < font->loc || ndx > font->hic)
113 font->chars[ndx - font->loc].code = i;
117 static int tt_really_load_font(DviParams *params, DviFont *font, FTInfo *info)
121 Int32 z, alpha, beta;
126 static int warned = 0;
128 TT_Face_Properties props;
131 DEBUG((DBG_TT, "(tt) really_load_font(%s)\n", info->fontname));
133 /* get the point size */
134 point_size = (double)font->scale / (params->tfm_conv * 0x100000);
135 point_size = 72.0 * point_size / 72.27;
137 /* just reset the size info */
138 TT_Set_Instance_Resolutions(info->instance,
139 params->dpi, params->vdpi);
140 TT_Set_Instance_CharSize(info->instance, FROUND(point_size * 64));
141 /* FIXME: should extend/slant again */
142 info->hasmetrics = 1;
147 DEBUG((DBG_TT, "(tt) loading new face `%s'\n",
149 status = TT_Open_Face(tt_handle, font->filename, &info->face);
151 warning(_("(tt) %s: could not load face: %s\n"),
152 info->fontname, TT_ErrToString18(status));
156 /* create a new instance of this face */
157 status = TT_New_Instance(info->face, &info->instance);
159 warning(_("(tt) %s: could not create face: %s\n"),
160 info->fontname, TT_ErrToString18(status));
161 TT_Close_Face(info->face);
166 status = TT_New_Glyph(info->face, &info->glyph);
168 warning(_("(tt) %s: could not create glyph: %s\n"),
169 info->fontname, TT_ErrToString18(status));
174 * We'll try to find a Unicode charmap. It's not that important that we
175 * actually find one, especially if the fontmap files are installed
176 * properly, but it's good to have some predefined behaviour
178 TT_Get_Face_Properties(info->face, &props);
181 for(i = 0; map_found < 0 && i < props.num_CharMaps; i++) {
184 TT_Get_CharMap_ID(info->face, i, &pid, &eid);
186 case TT_PLATFORM_APPLE_UNICODE:
189 case TT_PLATFORM_ISO:
190 if(eid == TT_ISO_ID_7BIT_ASCII ||
191 eid == TT_ISO_ID_8859_1)
194 case TT_PLATFORM_MICROSOFT:
195 if(eid == TT_MS_ID_UNICODE_CS)
201 warning(_("(tt) %s: no acceptable map found, using #0\n"),
205 DEBUG((DBG_TT, "(tt) %s: using charmap #%d\n",
206 info->fontname, map_found));
207 TT_Get_CharMap(info->face, map_found, &cmap);
209 DEBUG((DBG_TT, "(tt) %s: Set_Char_Size(%.2f, %d, %d)\n",
210 font->fontname, point_size, font->hdpi, font->vdpi));
211 status = TT_Set_Instance_Resolutions(info->instance,
212 params->dpi, params->vdpi);
214 error(_("(tt) %s: could not set resolution: %s\n"),
215 info->fontname, TT_ErrToString18(status));
218 status = TT_Set_Instance_CharSize(info->instance,
219 FROUND(point_size * 64));
221 error(_("(tt) %s: could not set point size: %s\n"),
222 info->fontname, TT_ErrToString18(status));
226 /* after this point we don't fail */
228 /* get information from the fontmap */
229 status = mdvi_query_fontmap(&info->mapinfo, info->fontname);
230 if(!status && info->mapinfo.encoding)
231 info->encoding = mdvi_request_encoding(info->mapinfo.encoding);
233 info->encoding = NULL;
235 if(info->encoding != NULL) {
238 status = TT_Load_PS_Names(info->face, &post);
240 warning(_("(tt) %s: could not load PS name table\n"),
242 mdvi_release_encoding(info->encoding, 0);
243 info->encoding = NULL;
247 /* get the metrics. If this fails, it's not fatal, but certainly bad */
248 info->tfminfo = get_font_metrics(info->fontname,
249 info->fmftype, info->fmfname);
251 if(info->tfminfo == NULL) {
252 warning("(tt) %s: no metrics data, font ignored\n",
257 font->design = info->tfminfo->design;
259 /* get the scaled character metrics */
260 get_tfm_chars(params, font, info->tfminfo, 0);
263 tt_encode_font(font, info);
265 warning(_("%s: no encoding vector found, expect bad output\n"),
267 /* this is better than nothing */
268 for(i = font->loc; i <= font->hic; i++)
269 font->chars[i - font->loc].code = TT_Char_Index(cmap, i);
273 info->hasmetrics = 1;
277 tt_font_remove(info);
278 mdvi_free(font->chars);
280 font->loc = font->hic = 0;
284 static int tt_load_font(DviParams *params, DviFont *font)
289 if(!initialized && init_freetype() < 0)
292 if(font->in != NULL) {
297 info = xalloc(FTInfo);
299 memzero(info, sizeof(FTInfo));
300 info->fmftype = DviFontAny; /* any metrics type will do */
301 info->fmfname = lookup_font_metrics(font->fontname, &info->fmftype);
302 info->fontname = font->fontname;
303 info->hasmetrics = 0;
306 /* these will be obtained from the fontmaps */
307 info->mapinfo.psname = NULL;
308 info->mapinfo.encoding = NULL;
309 info->mapinfo.fontfile = NULL;
310 info->mapinfo.extend = 0;
311 info->mapinfo.slant = 0;
313 /* initialize these */
314 font->chars = xnalloc(DviFontChar, 256);
317 for(i = 0; i < 256; i++) {
318 font->chars[i].offset = 1;
319 font->chars[i].glyph.data = NULL;
320 font->chars[i].shrunk.data = NULL;
321 font->chars[i].grey.data = NULL;
324 if(info->fmfname == NULL)
325 warning(_("(tt) %s: no font metric data\n"), font->fontname);
327 listh_append(&ttfonts, LIST(info));
328 font->private = info;
333 static int tt_get_bitmap(DviParams *params, DviFont *font,
334 int code, double xscale, double yscale, DviGlyph *glyph)
337 TT_Raster_Map raster;
339 TT_Glyph_Metrics metrics;
343 int have_outline = 0;
346 info = (FTInfo *)font->private;
350 error = TT_Load_Glyph(info->instance, info->glyph,
351 code, TTLOAD_DEFAULT);
352 if(error) goto tt_error;
353 error = TT_Get_Glyph_Outline(info->glyph, &outline);
354 if(error) goto tt_error;
356 mat.xx = FROUND(xscale * 65536);
357 mat.yy = FROUND(yscale * 65536);
360 TT_Transform_Outline(&outline, &mat);
361 error = TT_Get_Outline_BBox(&outline, &bbox);
362 if(error) goto tt_error;
365 bbox.xMax = (bbox.xMax + 63) & -64;
366 bbox.yMax = (bbox.yMax + 63) & -64;
367 w = (bbox.xMax - bbox.xMin) / 64;
368 h = (bbox.yMax - bbox.yMin) / 64;
372 glyph->x = -bbox.xMin / 64;
373 glyph->y = bbox.yMax / 64;
378 raster.cols = ROUND(w, 8);
379 raster.size = h * raster.cols;
380 raster.flow = TT_Flow_Down;
381 raster.bitmap = mdvi_calloc(h, raster.cols);
383 TT_Translate_Outline(&outline, -bbox.xMin, -bbox.yMin);
384 TT_Get_Outline_Bitmap(tt_handle, &outline, &raster);
385 glyph->data = bitmap_convert_msb8(raster.bitmap, w, h);
386 TT_Done_Outline(&outline);
387 mdvi_free(raster.bitmap);
392 TT_Done_Outline(&outline);
396 static int tt_font_get_glyph(DviParams *params, DviFont *font, int code)
398 FTInfo *info = (FTInfo *)font->private;
404 ASSERT(info != NULL);
405 if(!info->hasmetrics && tt_really_load_font(params, font, info) < 0)
407 ch = FONTCHAR(font, code);
408 if(!ch || !glyph_present(ch))
411 if(!ch->width || !ch->height)
414 ch->glyph.data = NULL;
418 dpi = Max(font->hdpi, font->vdpi);
419 error = tt_get_bitmap(params, font, ch->code,
420 (double)font->hdpi / dpi,
421 (double)font->vdpi / dpi,
431 ch->glyph.data = MDVI_GLYPH_EMPTY;
434 ch->glyph.w = ch->width;
435 ch->glyph.h = ch->height;
441 static void tt_shrink_glyph(DviContext *dvi, DviFont *font, DviFontChar *ch, DviGlyph *dest)
443 tt_get_bitmap(&dvi->params, font,
445 (double)font->hdpi / (dvi->params.dpi * dvi->params.hshrink),
446 (double)font->vdpi / (dvi->params.vdpi * dvi->params.vshrink),
448 /* transform the glyph for the current orientation */
449 font_transform_glyph(dvi->params.orientation, dest);
452 static void tt_reset_font(DviFont *font)
454 FTInfo *info = (FTInfo *)font->private;
458 info->hasmetrics = 0;
461 static void tt_font_remove(FTInfo *info)
466 /* all fonts in the hash table have called TT_Open_Face */
467 TT_Done_Instance(info->instance);
468 TT_Close_Face(info->face);
470 listh_remove(&ttfonts, LIST(info));
471 /* release our encodings */
473 mdvi_release_encoding(info->encoding, 1);
474 /* and destroy the font */
476 free_font_metrics(info->tfminfo);
478 mdvi_free(info->fmfname);
482 static void tt_free_data(DviFont *font)
484 if(font->private == NULL)
487 tt_font_remove((FTInfo *)font->private);
488 if(initialized && ttfonts.count == 0) {
489 DEBUG((DBG_TT, "(tt) last font removed -- closing FreeType\n"));
490 TT_Done_FreeType(tt_handle);
495 #endif /* WITH_TRUETYPE_FONTS */