Back to index

tetex-bin  3.0
ft.c
Go to the documentation of this file.
00001 /* ft.c */
00002 
00003 /************************************************************************
00004 
00005   Part of the dvipng distribution
00006 
00007   This program is free software; you can redistribute it and/or modify
00008   it under the terms of the GNU General Public License as published by
00009   the Free Software Foundation; either version 2 of the License, or
00010   (at your option) any later version.
00011 
00012   This program is distributed in the hope that it will be useful, but
00013   WITHOUT ANY WARRANTY; without even the implied warranty of
00014   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015   General Public License for more details.
00016 
00017   You should have received a copy of the GNU General Public License
00018   along with this program; if not, write to the Free Software
00019   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00020   02111-1307, USA.
00021 
00022   Copyright (C) 2002-2005 Jan-Åke Larsson
00023 
00024 ************************************************************************/
00025 
00026 #include "dvipng.h"
00027 #if HAVE_ALLOCA_H
00028 # include <alloca.h>
00029 #endif
00030 
00031 void LoadFT(int32_t c, struct char_entry * ptr)
00032 {
00033   FT_Bitmap  bitmap;
00034   FT_UInt    glyph_i;
00035   int i,j,k;
00036   unsigned char* bit;
00037 
00038   DEBUG_PRINT(DEBUG_FT,("\n  LOAD FT CHAR\t%d (%d)",c,ptr->tfmw));
00039   if (currentfont->psfontmap!=NULL
00040       && currentfont->psfontmap->encoding != NULL)
00041     glyph_i = FT_Get_Name_Index(currentfont->face,
00042                             currentfont->psfontmap->encoding->charname[c]);
00043   else 
00044     glyph_i = FT_Get_Char_Index( currentfont->face, c );
00045   if (FT_Load_Glyph( currentfont->face,    /* handle to face object */
00046                    glyph_i,              /* glyph index           */
00047                    FT_LOAD_RENDER | FT_LOAD_NO_HINTING ))
00048                                            /* load flags            */
00049     Fatal("can't load char_entry %d",c);
00050   ptr->xOffset = -currentfont->face->glyph->bitmap_left*shrinkfactor;
00051   ptr->yOffset = (currentfont->face->glyph->bitmap_top-1)*shrinkfactor;
00052   bitmap=currentfont->face->glyph->bitmap;
00053   DEBUG_PRINT(DEBUG_FT,(" (%dx%d)",bitmap.width,bitmap.rows));
00054     
00055   if ((ptr->data = calloc(bitmap.width*bitmap.rows,sizeof(char))) == NULL)
00056     Fatal("Unable to allocate image space for char <%c>\n", (char)c);
00057   ptr->w = bitmap.width;
00058   ptr->h = bitmap.rows;
00059 
00060 #define GREYLEVELS 16
00061   DEBUG_PRINT(DEBUG_GLYPH,("\nDRAW GLYPH %d\n", (int)c));
00062   bit=ptr->data;
00063   for(i=0;i<bitmap.rows;i++) {
00064     for(j=0;j<bitmap.width;j++) {
00065       k=bitmap.buffer[i*bitmap.pitch+j]/(256/GREYLEVELS)*17;
00066       /* k=(bitmap.buffer[i*bitmap.pitch+j]+1)/16; */
00067       /* k= k>0 ? k*16-1 : 0; */
00068       DEBUG_PRINT(DEBUG_GLYPH,("%3u ",k));
00069       bit[i*bitmap.width+j]=k;
00070     }
00071     DEBUG_PRINT(DEBUG_GLYPH,("|\n"));
00072   }
00073 }
00074 
00075 bool InitFT(struct font_entry * tfontp)
00076 {
00077   int error;
00078 
00079   if (libfreetype==NULL) {
00080     if (FT_Init_FreeType( &libfreetype )) {
00081       Warning("an error occured during freetype initialisation, disabling it"); 
00082       flags &= ~USE_FREETYPE;
00083       return(false);
00084     } 
00085 # ifdef DEBUG
00086     else {
00087       FT_Int      amajor, aminor, apatch;
00088       
00089       FT_Library_Version( libfreetype, &amajor, &aminor, &apatch );
00090       DEBUG_PRINT(DEBUG_FT,("\n  FREETYPE VERSION: FreeType %d.%d.%d", 
00091                          amajor, aminor, apatch));
00092     }
00093 # endif
00094   }
00095 
00096   DEBUG_PRINT((DEBUG_DVI|DEBUG_FT),("\n  OPEN FT FONT:\t'%s'", tfontp->name));
00097   error = FT_New_Face( libfreetype, tfontp->name, 0, &tfontp->face );
00098   if (error == FT_Err_Unknown_File_Format) {
00099     Warning("font file %s has unknown format", tfontp->name);
00100     return(false);
00101   } else if (error) { 
00102     Warning("font file %s could not be opened", tfontp->name);
00103     return(false);
00104   } 
00105   Message(BE_VERBOSE,"<%s>", tfontp->name);
00106   if (tfontp->psfontmap == NULL || tfontp->psfontmap->encoding == NULL) {
00107 #ifndef FT_ENCODING_ADOBE_CUSTOM
00108 # define FT_ENCODING_ADOBE_CUSTOM ft_encoding_adobe_custom
00109 # define FT_ENCODING_ADOBE_STANDARD ft_encoding_adobe_standard
00110 #endif
00111     if (FT_Select_Charmap( tfontp->face, FT_ENCODING_ADOBE_CUSTOM )) {
00112       Warning("unable to set font encoding FT_ENCODING_ADOBE_CUSTOM for %s", 
00113              tfontp->name);
00114       if(FT_Select_Charmap( tfontp->face, FT_ENCODING_ADOBE_STANDARD )) {
00115        Warning("unable to set font encoding for %s", tfontp->name);
00116        return(false);
00117       }
00118     }
00119   } 
00120   if (FT_Set_Char_Size( tfontp->face, /* handle to face object           */
00121                      0,            /* char_width in 1/64th of points  */
00122                      (tfontp->d*64)/65536,
00123                      /* char_height in 1/64th of points */
00124                      tfontp->dpi/shrinkfactor,   /* horizontal resolution */
00125                      tfontp->dpi/shrinkfactor )) /* vertical resolution   */ {
00126     Warning("unable to set font size for %s", tfontp->name);
00127     return(false);
00128   }
00129   if (tfontp->psfontmap!=NULL)
00130     FT_Set_Transform(tfontp->face, tfontp->psfontmap->ft_transformp, NULL);
00131   tfontp->type = FONT_TYPE_FT;
00132   return(true);
00133 }
00134 
00135 
00136 void UnLoadFT(struct char_entry *ptr)
00137 {
00138   if (ptr->data!=NULL)
00139     free(ptr->data);
00140   ptr->data=NULL;
00141 }
00142 
00143 
00144 void DoneFT(struct font_entry *tfontp)
00145 {
00146   int c=0;
00147 
00148   int error = FT_Done_Face( tfontp->face );
00149   if (error)
00150     Warning("font file %s could not be closed", tfontp->name);
00151   while(c<NFNTCHARS-1) {
00152     if (tfontp->chr[c]!=NULL) {
00153       UnLoadFT((struct char_entry*)tfontp->chr[c]);
00154       free(tfontp->chr[c]);
00155       tfontp->chr[c]=NULL;
00156     }
00157     c++;
00158   }
00159   tfontp->name[0]='\0';
00160 }
00161 
00162