static void ttf_get_nice_charmap(TT_Face face, TT_CharMap *charMap, char *where) { int n,i,res,got=-1,best=-1; if (-1==(n=TT_Get_CharMap_Count(face))) Pike_error("%s: illegal face handle\n",where); for (i=0; i<n; i++) { int ihas=0; TT_UShort platformID, encodingID; if ((res=TT_Get_CharMap_ID(face, (TT_UShort)i, &platformID, &encodingID))) my_tt_error(where,"TT_Get_CharMap_ID: ",res); switch (platformID*100+encodingID) { case 301: /* M$: unicode */ case 300: /* M$: unicode (?) */ ihas=30; break; case 202: /* ISO: iso-8859-1 */ ihas=20; break; default: ihas = 1; break; } if (ihas>got) { best=i; got=ihas; } } if (got==-1) Pike_error("%s: no charmaps at all\n",where); if ((res=TT_Get_CharMap(face, (TT_UShort)best, charMap))) my_tt_error(where,"TT_Get_CharMap: ",res); }
TTF_Font *TTF_OpenFont(const char *file, int ptsize) { TTF_Font *font; TT_Face_Properties properties; TT_Instance_Metrics imetrics; int i, n; TT_UShort platform, encoding; TT_Error error; font = (TTF_Font *)malloc(sizeof(*font)); if ( font == NULL ) { SDL_SetError("Out of memory"); return(NULL); } memset(font, 0, sizeof(*font)); /* Open the font and create ancillary data */ error = TT_Open_Face(engine, file, &font->face); if ( error ) { SDL_SetError("Couldn't load font file"); free(font); return(NULL); } error = TT_New_Glyph(font->face, &font->glyph); if ( error ) { SDL_SetError("Couldn't create glyph container"); TTF_CloseFont(font); return(NULL); } error = TT_New_Instance(font->face, &font->inst); if ( error ) { SDL_SetError("Couldn't create font instance"); TTF_CloseFont(font); return(NULL); } /* Set the display resolution */ error = TT_Set_Instance_Resolutions(font->inst, 72, 72); if ( error ) { SDL_SetError("Couldn't set font resolution"); TTF_CloseFont(font); return(NULL); } error = TT_Set_Instance_CharSize(font->inst, ptsize*64); if ( error ) { SDL_SetError("Couldn't set font size"); TTF_CloseFont(font); return(NULL); } /* Get a Unicode mapping for this font */ n = TT_Get_CharMap_Count(font->face); for ( i=0; i<n; ++i ) { TT_Get_CharMap_ID(font->face, i, &platform, &encoding); if ( ((platform == TT_PLATFORM_MICROSOFT) && (encoding == TT_MS_ID_UNICODE_CS)) || ((platform == TT_PLATFORM_APPLE_UNICODE) && (encoding == TT_APPLE_ID_DEFAULT)) ) { TT_Get_CharMap(font->face, i, &font->map); break; } } if ( i == n ) { SDL_SetError("Font doesn't have a Unicode mapping"); TTF_CloseFont(font); return(NULL); } /* Get the font metrics for this font */ TT_Get_Face_Properties(font->face, &properties ); TT_Get_Instance_Metrics(font->inst, &imetrics); font->pointsize = imetrics.y_ppem; font->ascent = (float)properties.horizontal->Ascender / properties.header->Units_Per_EM; font->ascent *= font->pointsize; font->descent = (float)properties.horizontal->Descender / properties.header->Units_Per_EM; font->descent *= font->pointsize; font->lineskip = (float)properties.horizontal->Line_Gap / properties.header->Units_Per_EM; font->lineskip *= font->pointsize; font->height = round(font->ascent - font->descent); /* Set the default font style */ font->style = TTF_STYLE_NORMAL; font->glyph_overhang = font->pointsize/10; /* x offset = cos(((90.0-12)/360)*2*M_PI), or 12 degree angle */ font->glyph_italics = 0.207; font->glyph_italics *= font->height; return(font); }