home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / anwor032.zip / antiword.0.32 / fonts_r.c < prev    next >
C/C++ Source or Header  |  2001-09-25  |  5KB  |  225 lines

  1. /*
  2.  * fonts_r.c
  3.  * Copyright (C) 1999,2000 A.J. van Os; Released under GPL
  4.  *
  5.  * Description:
  6.  * Functions to deal with fonts (RiscOs version)
  7.  */
  8.  
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "antiword.h"
  12.  
  13. static font        tFontCurr = (font)-1;
  14.  
  15. /*
  16.  * pOpenFontTableFile - open the Font translation file
  17.  * Copy the file to the proper place if necessary.
  18.  *
  19.  * Returns the file pointer or NULL
  20.  */
  21. FILE *
  22. pOpenFontTableFile(void)
  23. {
  24.     FILE    *pFileR, *pFileW;
  25.     char    *szFontNamesFile;
  26.     size_t    tSize;
  27.     BOOL    bFailed;
  28.     char    acBuffer[256];
  29.  
  30.     pFileR = fopen("<AntiWord$FontNamesFile>", "r");
  31.     if (pFileR != NULL) {
  32.         /* The font table is already in the right directory */
  33.         return pFileR;
  34.     }
  35.  
  36.     szFontNamesFile = getenv("AntiWord$FontNamesSave");
  37.     if (szFontNamesFile == NULL) {
  38.         werr(0, "Warning: Name of the FontNames file not found");
  39.         return NULL;
  40.     }
  41.     DBG_MSG(szFontNamesFile);
  42.  
  43.     pFileR = fopen("<AntiWord$Dir>.Resources.Default", "r");
  44.     if (pFileR == NULL) {
  45.         werr(0, "I can't find 'Resources.Default'");
  46.         return NULL;
  47.     }
  48.     /* Here the default font translation table is known to exist */
  49.  
  50.     if (!bMakeDirectory(szFontNamesFile)) {
  51.         werr(0,
  52.         "I can't make a directory for the FontNames file");
  53.         return NULL;
  54.     }
  55.     /* Here the proper directory is known to exist */
  56.  
  57.     pFileW = fopen(szFontNamesFile, "w");
  58.     if (pFileW == NULL) {
  59.         (void)fclose(pFileR);
  60.         werr(0, "I can't create a default FontNames file");
  61.         return NULL;
  62.     }
  63.     /* Here the proper directory is known to be writeable */
  64.  
  65.     /* Copy the default FontNames file */
  66.     bFailed = FALSE;
  67.     while (!feof(pFileR)) {
  68.         tSize = fread(acBuffer, 1, sizeof(acBuffer), pFileR);
  69.         if (ferror(pFileR)) {
  70.             DBG_MSG("Read error");
  71.             bFailed = TRUE;
  72.             break;
  73.         }
  74.         if (fwrite(acBuffer, 1, tSize, pFileW) != tSize) {
  75.             DBG_MSG("Write error");
  76.             bFailed = TRUE;
  77.             break;
  78.         }
  79.     }
  80.     (void)fclose(pFileW);
  81.     (void)fclose(pFileR);
  82.     if (bFailed) {
  83.         DBG_MSG("Copying the FontNames file failed");
  84.         (void)remove(szFontNamesFile);
  85.         return NULL;
  86.     }
  87.     return fopen(szFontNamesFile, "r");
  88. } /* end of pOpenFontTableFile */
  89.  
  90. /*
  91.  * vCloseFont - close the current font, if any
  92.  */
  93. void
  94. vCloseFont(void)
  95. {
  96.     os_error    *e;
  97.  
  98.     NO_DBG_MSG("vCloseFont");
  99.  
  100.     if (tFontCurr == (font)-1) {
  101.         return;
  102.     }
  103.     e = font_lose(tFontCurr);
  104.     if (e != NULL) {
  105.         werr(0, "Close font error %d: %s", e->errnum, e->errmess);
  106.     }
  107.     tFontCurr = -1;
  108. } /* end of vCloseFont */
  109.  
  110. /*
  111.  * tOpenFont - make the given font the current font
  112.  *
  113.  * Returns the font reference number for use in a draw file
  114.  */
  115. draw_fontref
  116. tOpenFont(int iWordFontNumber, unsigned char ucFontstyle, int iWordFontsize)
  117. {
  118.     os_error    *e;
  119.     const char    *szOurFontname;
  120.     font    tFont;
  121.     int    iFontnumber;
  122.  
  123.     NO_DBG_MSG("tOpenFont");
  124.     NO_DBG_DEC(iWordFontNumber);
  125.     NO_DBG_HEX(ucFontstyle);
  126.     NO_DBG_DEC(iWordFontsize);
  127.  
  128.     /* Keep the relevant bits */
  129.     ucFontstyle &= FONT_BOLD|FONT_ITALIC;
  130.     NO_DBG_HEX(ucFontstyle);
  131.  
  132.     iFontnumber = iGetFontByNumber(iWordFontNumber, ucFontstyle);
  133.     szOurFontname = szGetOurFontname(iFontnumber);
  134.     if (szOurFontname == NULL || szOurFontname[0] == '\0') {
  135.         tFontCurr = (font)-1;
  136.         return (draw_fontref)0;
  137.     }
  138.     NO_DBG_MSG(szOurFontname);
  139.     e = font_find((char *)szOurFontname,
  140.             iWordFontsize * 8, iWordFontsize * 8,
  141.             0, 0, &tFont);
  142.     if (e != NULL) {
  143.         switch (e->errnum) {
  144.         case 523:
  145.             werr(0, "%s", e->errmess);
  146.             break;
  147.         default:
  148.             werr(0, "Open font error %d: %s",
  149.                 e->errnum, e->errmess);
  150.             break;
  151.         }
  152.         tFontCurr = (font)-1;
  153.         return (draw_fontref)0;
  154.     }
  155.     tFontCurr = tFont;
  156.     NO_DBG_DEC(tFontCurr);
  157.     return (draw_fontref)(iFontnumber + 1);
  158. } /* end of tOpenFont */
  159.  
  160. /*
  161.  * tOpenTableFont - make the table font the current font
  162.  *
  163.  * Returns the font reference number for use in a draw file
  164.  */
  165. draw_fontref
  166. tOpenTableFont(int iWordFontsize)
  167. {
  168.     int    iWordFontnumber;
  169.  
  170.     NO_DBG_MSG("tOpenTableFont");
  171.  
  172.     iWordFontnumber = iFontname2Fontnumber(TABLE_FONT, FONT_REGULAR);
  173.     if (iWordFontnumber < 0 || iWordFontnumber > (int)UCHAR_MAX) {
  174.         DBG_DEC(iWordFontnumber);
  175.         tFontCurr = (font)-1;
  176.         return (draw_fontref)0;
  177.     }
  178.  
  179.     return tOpenFont(iWordFontnumber, FONT_REGULAR, iWordFontsize);
  180. } /* end of tOpenTableFont */
  181.  
  182. /*
  183.  * lComputeStringWidth - compute the string width
  184.  *
  185.  * Returns string width in millipoints
  186.  */
  187. long
  188. lComputeStringWidth(char *szString, int iStringLength,
  189.         draw_fontref tFontRef, int iFontsize)
  190. {
  191.     font_string    tStr;
  192.     os_error    *e;
  193.  
  194.     fail(szString == NULL || iStringLength < 0);
  195.     fail(iFontsize < MIN_FONT_SIZE || iFontsize > MAX_FONT_SIZE);
  196.  
  197.     if (szString[0] == '\0' || iStringLength <= 0) {
  198.         /* Empty string */
  199.         return 0;
  200.     }
  201.     if (iStringLength == 1 && szString[0] == TABLE_SEPARATOR) {
  202.         /* Font_strwidth doesn't like control characters */
  203.         return 0;
  204.     }
  205.     if (tFontCurr == (font)-1) {
  206.         /* No current font, use systemfont */
  207.         return lChar2MilliPoints(iStringLength);
  208.     }
  209.     tStr.s = szString;
  210.     tStr.x = INT_MAX;
  211.     tStr.y = INT_MAX;
  212.     tStr.split = -1;
  213.     tStr.term = iStringLength;
  214.     e = font_strwidth(&tStr);
  215.     if (e == NULL) {
  216.         return (long)tStr.x;
  217.     }
  218.     DBG_DEC(e->errnum);
  219.     DBG_MSG(e->errmess);
  220.     DBG_DEC(iStringLength);
  221.     DBG_MSG(szString);
  222.     werr(0, "String width error %d: %s", e->errnum, e->errmess);
  223.     return lChar2MilliPoints(iStringLength);
  224. } /* end of lComputeStringWidth */
  225.