examples/PIPS/antiword/src/fonts_r.c

00001 /*
00002  * fonts_r.c
00003  * Copyright (C) 1999-2002 A.J. van Os; Released under GPL
00004  *
00005  * Description:
00006  * Functions to deal with fonts (RiscOs version)
00007  */
00008 
00009 #include <stdlib.h>
00010 #include <string.h>
00011 #include "DeskLib:Font.h"
00012 #include "drawfile.h"
00013 #include "antiword.h"
00014 
00015 static font_handle      tFontCurr = (font_handle)-1;
00016 
00017 /*
00018  * pOpenFontTableFile - open the Font translation file
00019  * Copy the file to the proper place if necessary.
00020  *
00021  * Returns the file pointer or NULL
00022  */
00023 FILE *
00024 pOpenFontTableFile(void)
00025 {
00026         FILE    *pFileR, *pFileW;
00027         char    *szFontNamesFile;
00028         size_t  tSize;
00029         BOOL    bFailed;
00030         char    acBuffer[256];
00031 
00032         pFileR = fopen("<AntiWord$FontNamesFile>", "r");
00033         if (pFileR != NULL) {
00034                 /* The font table is already in the right directory */
00035                 return pFileR;
00036         }
00037 
00038         szFontNamesFile = getenv("AntiWord$FontNamesSave");
00039         if (szFontNamesFile == NULL) {
00040                 werr(0, "Warning: Name of the FontNames file not found");
00041                 return NULL;
00042         }
00043         DBG_MSG(szFontNamesFile);
00044 
00045         pFileR = fopen("<AntiWord$Dir>.Resources.Default", "r");
00046         if (pFileR == NULL) {
00047                 werr(0, "I can't find 'Resources.Default'");
00048                 return NULL;
00049         }
00050         /* Here the default font translation table is known to exist */
00051 
00052         if (!bMakeDirectory(szFontNamesFile)) {
00053                 werr(0,
00054                 "I can't make a directory for the FontNames file");
00055                 return NULL;
00056         }
00057         /* Here the proper directory is known to exist */
00058 
00059         pFileW = fopen(szFontNamesFile, "w");
00060         if (pFileW == NULL) {
00061                 (void)fclose(pFileR);
00062                 werr(0, "I can't create a default FontNames file");
00063                 return NULL;
00064         }
00065         /* Here the proper directory is known to be writeable */
00066 
00067         /* Copy the default FontNames file */
00068         bFailed = FALSE;
00069         while (!feof(pFileR)) {
00070                 tSize = fread(acBuffer, 1, sizeof(acBuffer), pFileR);
00071                 if (ferror(pFileR)) {
00072                         DBG_MSG("Read error");
00073                         bFailed = TRUE;
00074                         break;
00075                 }
00076                 if (fwrite(acBuffer, 1, tSize, pFileW) != tSize) {
00077                         DBG_MSG("Write error");
00078                         bFailed = TRUE;
00079                         break;
00080                 }
00081         }
00082         (void)fclose(pFileW);
00083         (void)fclose(pFileR);
00084         if (bFailed) {
00085                 DBG_MSG("Copying the FontNames file failed");
00086                 (void)remove(szFontNamesFile);
00087                 return NULL;
00088         }
00089         return fopen(szFontNamesFile, "r");
00090 } /* end of pOpenFontTableFile */
00091 
00092 /*
00093  * vCloseFont - close the current font, if any
00094  */
00095 void
00096 vCloseFont(void)
00097 {
00098         os_error        *e;
00099 
00100         NO_DBG_MSG("vCloseFont");
00101 
00102         if (tFontCurr == (font_handle)-1) {
00103                 return;
00104         }
00105         e = Font_LoseFont(tFontCurr);
00106         if (e != NULL) {
00107                 werr(0, "Close font error %d: %s", e->errnum, e->errmess);
00108         }
00109         tFontCurr = (font_handle)-1;
00110 } /* end of vCloseFont */
00111 
00112 /*
00113  * tOpenFont - make the specified font the current font
00114  *
00115  * Returns the font reference number for use in a draw file
00116  */
00117 drawfile_fontref
00118 tOpenFont(UCHAR ucWordFontNumber, USHORT usFontStyle, USHORT usWordFontSize)
00119 {
00120         os_error        *e;
00121         const char      *szOurFontname;
00122         font_handle     tFont;
00123         int     iFontnumber;
00124 
00125         NO_DBG_MSG("tOpenFont");
00126         NO_DBG_DEC(ucWordFontNumber);
00127         NO_DBG_HEX(usFontStyle);
00128         NO_DBG_DEC(usWordFontSize);
00129 
00130         /* Keep the relevant bits */
00131         usFontStyle &= FONT_BOLD|FONT_ITALIC;
00132         NO_DBG_HEX(usFontStyle);
00133 
00134         iFontnumber = iGetFontByNumber(ucWordFontNumber, usFontStyle);
00135         szOurFontname = szGetOurFontname(iFontnumber);
00136         if (szOurFontname == NULL || szOurFontname[0] == '\0') {
00137                 tFontCurr = (font_handle)-1;
00138                 return (byte)0;
00139         }
00140         NO_DBG_MSG(szOurFontname);
00141         e = Font_FindFont(&tFont, (char *)szOurFontname,
00142                         (int)usWordFontSize * 8, (int)usWordFontSize * 8,
00143                         0, 0);
00144         if (e != NULL) {
00145                 switch (e->errnum) {
00146                 case 523:
00147                         werr(0, "%s", e->errmess);
00148                         break;
00149                 default:
00150                         werr(0, "Open font error %d: %s",
00151                                 e->errnum, e->errmess);
00152                         break;
00153                 }
00154                 tFontCurr = (font_handle)-1;
00155                 return (drawfile_fontref)0;
00156         }
00157         tFontCurr = tFont;
00158         NO_DBG_DEC(tFontCurr);
00159         return (drawfile_fontref)(iFontnumber + 1);
00160 } /* end of tOpenFont */
00161 
00162 /*
00163  * tOpenTableFont - make the table font the current font
00164  *
00165  * Returns the font reference number for use in a draw file
00166  */
00167 drawfile_fontref
00168 tOpenTableFont(USHORT usWordFontSize)
00169 {
00170         int     iWordFontnumber;
00171 
00172         NO_DBG_MSG("tOpenTableFont");
00173 
00174         iWordFontnumber = iFontname2Fontnumber(TABLE_FONT, FONT_REGULAR);
00175         if (iWordFontnumber < 0 || iWordFontnumber > (int)UCHAR_MAX) {
00176                 DBG_DEC(iWordFontnumber);
00177                 tFontCurr = (font_handle)-1;
00178                 return (drawfile_fontref)0;
00179         }
00180 
00181         return tOpenFont((UCHAR)iWordFontnumber, FONT_REGULAR, usWordFontSize);
00182 } /* end of tOpenTableFont */
00183 
00184 /*
00185  * lComputeStringWidth - compute the string width
00186  *
00187  * Returns the string width in millipoints
00188  */
00189 long
00190 lComputeStringWidth(const char *szString, size_t tStringLength,
00191         drawfile_fontref tFontRef, USHORT usFontSize)
00192 {
00193         font_string     tStr;
00194         os_error        *e;
00195 
00196         fail(szString == NULL);
00197         fail(usFontSize < MIN_FONT_SIZE || usFontSize > MAX_FONT_SIZE);
00198 
00199         if (szString[0] == '\0' || tStringLength == 0) {
00200                 /* Empty string */
00201                 return 0;
00202         }
00203         if (tStringLength == 1 && szString[0] == TABLE_SEPARATOR) {
00204                 /* Font_strwidth doesn't like control characters */
00205                 return 0;
00206         }
00207         if (tFontCurr == (font_handle)-1) {
00208                 /* No current font, use systemfont */
00209                 return lChar2MilliPoints(tStringLength);
00210         }
00211         tStr.s = (char *)szString;
00212         tStr.x = INT_MAX;
00213         tStr.y = INT_MAX;
00214         tStr.split = -1;
00215         tStr.term = tStringLength;
00216         e = Font_StringWidth(&tStr);
00217         if (e == NULL) {
00218                 return (long)tStr.x;
00219         }
00220         DBG_DEC(e->errnum);
00221         DBG_MSG(e->errmess);
00222         DBG_DEC(tStringLength);
00223         DBG_MSG(szString);
00224         werr(0, "String width error %d: %s", e->errnum, e->errmess);
00225         return lChar2MilliPoints(tStringLength);
00226 } /* end of lComputeStringWidth */
00227 
00228 /*
00229  * tCountColumns - count the number of columns in a string
00230  *
00231  * Returns the number of columns
00232  */
00233 size_t
00234 tCountColumns(const char *szString, size_t tLength)
00235 {
00236         fail(szString == NULL);
00237 
00238         /* One byte, one character, one column */
00239         return tLength;
00240 } /* end of tCountColumns */
00241 
00242 /*
00243  * tGetCharacterLength - the length of the specified character in bytes
00244  *
00245  * Returns the length in bytes
00246  */
00247 size_t
00248 tGetCharacterLength(const char *szString)
00249 {
00250         return 1;
00251 } /* end of tGetCharacterLength */

Generated by  doxygen 1.6.2