Main Page   Modules   Alphabetical List   Compound List   File List   Compound Members   File Members  

ttf.c

Go to the documentation of this file.
00001 /*  $Id: ttf.c,v 1.7 2000/12/08 00:47:46 dbryson Exp $
00002 **
00003 **  Copyright (c) 2000 Derry Bryson <derry@techass.com>
00004 **
00005 **  Description:
00006 **  
00007 **    Postscript Library
00008 **
00009 **
00010 **  License:
00011 **
00012 **    This library is free software; you can redistribute it and/or
00013 **    modify it under the terms of the GNU Lesser General Public
00014 **    License as published by the Free Software Foundation; either
00015 **    version 2.1 of the License, or (at your option) any later version.
00016 **
00017 **    This library is distributed in the hope that it will be useful,
00018 **    but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 **    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020 **    Lesser General Public License for more details.
00021 **
00022 **    You should have received a copy of the GNU Lesser General Public
00023 **    License along with this library; if not, write to the Free Software
00024 **    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 **
00026 **
00027 **  Contact:
00028 **
00029 **      Mail:
00030 **
00031 **        Technology Associates, Inc.
00032 **        LIBPS Project
00033 **        1455 Deming Way #11
00034 **        Sparks, NV  89431
00035 **        USA
00036 **
00037 **      Email:
00038 **
00039 **        libps@techass.com
00040 **
00041 **      See our website at:
00042 **
00043 **        libps.sourceforge.net
00044 **
00045 */
00046 
00050 #ifdef HAVE_CONFIG_H
00051 # include "psconfig.h"
00052 #endif
00053 
00054 #include <stdio.h>
00055 #include <stdlib.h>
00056 #include <string.h>
00057 
00058 #include "ps.h"
00059 #include "freetype/freetype.h"
00060 
00061 static int
00062   ttfInit = 0;
00063   
00064 static TT_Engine
00065   psTTEngine;
00066  
00075 int
00076 psTTInit(void)
00077 {
00078   if(!ttfInit)
00079   {
00080     ttfInit = !TT_Init_FreeType(&psTTEngine);
00081     if(ttfInit)
00082       atexit(psTTDone);
00083     else
00084       printf("unable init psTTEngine!\n");
00085   }
00086     
00087   return ttfInit;
00088 }
00089 
00096 void
00097 psTTDone(void)
00098 {
00099   if(ttfInit)
00100     TT_Done_FreeType(psTTEngine);
00101 }
00102 
00111 PSTTFontInfo* 
00112 psTTFontInfoAlloc(void)
00113 {
00114   return calloc(1, sizeof(PSTTFontInfo));
00115 }
00116 
00125 void 
00126 psTTFontInfoFree(PSTTFontInfo* info)
00127 {
00128   if(info)
00129   {
00130     if(info->name)
00131       free(info->name);
00132     if(info->family)
00133       free(info->family);
00134     if(info->foundry)
00135       free(info->foundry);
00136     free(info);
00137   }
00138 }
00139 
00149 PSTTFontInfo*
00150 psTTGetFontInfo(char *fileName)
00151 {
00152   int
00153     err;
00154     
00155   PSTTFontInfo
00156     *info = NULL;
00157     
00158   TT_Face
00159     face;
00160     
00161   TT_Face_Properties
00162     prop;
00163     
00164   char
00165     *s;
00166     
00167   TT_UShort
00168     i,
00169     len,
00170     platformID,
00171     encodingID,
00172     languageID,
00173     nameID;
00174     
00175   if(psTTInit())
00176   {
00177     if(!(err = TT_Open_Face(psTTEngine, fileName, &face)))
00178     {
00179       if(!(err = TT_Get_Face_Properties(face, &prop)))
00180       {
00181         info = psTTFontInfoAlloc();
00182         if(info)
00183         {
00184           for(i = 0; i < prop.num_Names; i++)
00185           {
00186             if(!TT_Get_Name_ID(face, i, &platformID, &encodingID, &languageID,
00187                                &nameID))
00188             {
00189 
00190               TT_Get_Name_String(face, i, &s, &len);
00191 //              printf("string %d: platformID = %d, encodingID = %d, languageID = %x, nameID = %d, s = %*.*s\n",
00192 //                     i, platformID, encodingID, languageID, nameID, len, len, s);
00193               if(platformID == 3 && encodingID == 1 && languageID == 0x409 && nameID == 6)
00194                 info->name = psUStrndup(s, len);
00195               if(platformID == 3 && encodingID == 1 && languageID == 0x409 && nameID == 4)
00196                 info->fullName = psUStrndup(s, len);
00197               if(platformID == 3 && encodingID == 1 && languageID == 0x409 && nameID == 1)
00198                 info->family = psUStrndup(s, len);
00199             }
00200           }
00201           info->weight = prop.os2->usWeightClass;
00202           info->width = prop.os2->usWidthClass;
00203           info->italic = (prop.os2->fsSelection & PS_TT_FSSELECTION_ITALIC);
00204         }
00205       }
00206       TT_Close_Face(face);
00207     }
00208   }
00209   
00210   return info;
00211 }
00212 
00221 int 
00222 psTTGetPSFontWidth(PSTTFontInfo* info)
00223 {
00224   static int
00225     widths[9][2] =
00226     {
00227       { PS_TT_WIDTH_ULTRACONDENSED, PS_FONT_WIDTH_ULTRACONDENSED },
00228       { PS_TT_WIDTH_CONDENSED, PS_FONT_WIDTH_CONDENSED },
00229       { PS_TT_WIDTH_SEMICONDENSED, PS_FONT_WIDTH_SEMICONDENSED },
00230       { PS_TT_WIDTH_NORMAL, PS_FONT_WIDTH_NORMAL },
00231       { PS_TT_WIDTH_SEMIEXPANDED, PS_FONT_WIDTH_SEMIEXPANDED },
00232       { PS_TT_WIDTH_EXPANDED, PS_FONT_WIDTH_EXPANDED },
00233       { PS_TT_WIDTH_EXTRAEXPANDED, PS_FONT_WIDTH_EXTRAEXPANDED },
00234       { PS_TT_WIDTH_ULTRAEXPANDED, PS_FONT_WIDTH_ULTRAEXPANDED }
00235     };
00236     
00237   int
00238     i;
00239     
00240   for(i = 0; i < 9; i++)
00241     if(info->width == widths[i][0])
00242       return widths[i][1];
00243       
00244   return PS_FONT_WIDTH_UNKNOWN;
00245 }
00246 
00255 int 
00256 psTTGetPSFontWeight(PSTTFontInfo* info)
00257 {
00258   static int
00259     weights[9][2] =
00260     {
00261       { PS_TT_WEIGHT_THIN, PS_FONT_WEIGHT_THIN },
00262       { PS_TT_WEIGHT_EXTRALIGHT, PS_FONT_WEIGHT_EXTRALIGHT },
00263       { PS_TT_WEIGHT_LIGHT, PS_FONT_WEIGHT_LIGHT },
00264       { PS_TT_WEIGHT_NORMAL, PS_FONT_WEIGHT_NORMAL },
00265       { PS_TT_WEIGHT_MEDIUM, PS_FONT_WEIGHT_MEDIUM },
00266       { PS_TT_WEIGHT_SEMIBOLD, PS_FONT_WEIGHT_SEMIBOLD },
00267       { PS_TT_WEIGHT_BOLD, PS_FONT_WEIGHT_BOLD },
00268       { PS_TT_WEIGHT_EXTRABOLD, PS_FONT_WEIGHT_EXTRABOLD },
00269       { PS_TT_WEIGHT_BLACK, PS_FONT_WEIGHT_BLACK }
00270     };
00271     
00272   int
00273     i;
00274     
00275   for(i = 0; i < 9; i++)
00276     if(info->weight == weights[i][0])
00277       return weights[i][1];
00278       
00279   return PS_FONT_WEIGHT_UNKNOWN;
00280 }
00281 
00294 int 
00295 psTTFToT1PFA(char *ttfFileName, PSStream *pfa, int flags)
00296 {
00297   FILE
00298     *fp;
00299     
00300   char
00301     buf[1024];
00302     
00303   int
00304     retVal,
00305     numRead;
00306     
00307   sprintf(buf, "ttf2pt1 -W 0 -e %s -", ttfFileName);
00308   fp = popen(buf, "r");
00309   if(fp)
00310   {
00311     while((numRead = fread(&buf, 1, 1024, fp)) > 0)
00312       psWrite(buf, 1, numRead, pfa);
00313     if(!pclose(fp))
00314       retVal = 1;
00315   }
00316   return retVal;
00317 }

Generated at Mon Dec 11 22:46:28 2000 for Postscript Utility Library by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000