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

  1. /*
  2.  * fonts_u.c
  3.  * Copyright (C) 1999-2001 A.J. van Os; Released under GPL
  4.  *
  5.  * Description:
  6.  * Functions to deal with fonts (Unix version)
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "antiword.h"
  13. #include "fontinfo.h"
  14.  
  15. /* Don't use fonts, just plain text */
  16. static BOOL        bUsePlainText = TRUE;
  17. /* Which character set should be used */
  18. static encoding_type    eEncoding = encoding_neutral;
  19.  
  20.  
  21. /*
  22.  * pOpenFontTableFile - open the Font translation file
  23.  *
  24.  * Returns the file pointer or NULL
  25.  */
  26. FILE *
  27. pOpenFontTableFile(void)
  28. {
  29.     FILE        *pFile;
  30.     const char    *szHome;
  31.     const char    *szGlobalFile;
  32.     char        szLocalFile[PATH_MAX+1];
  33.  
  34.     /* Try the local version of the fontnames file */
  35.     szHome = szGetHomeDirectory();
  36.     if (strlen(szHome) +
  37.         sizeof(FILE_SEPARATOR ANTIWORD_DIR FILE_SEPARATOR FONTNAMES_FILE) >=
  38.         sizeof(szLocalFile)) {
  39.         werr(0, "The name of your HOME directory is too long");
  40.         return NULL;
  41.     }
  42.  
  43.     sprintf(szLocalFile, "%s%s",
  44.         szHome,
  45.         FILE_SEPARATOR ANTIWORD_DIR FILE_SEPARATOR FONTNAMES_FILE);
  46.     DBG_MSG(szLocalFile);
  47.  
  48.     pFile = fopen(szLocalFile, "r");
  49.     if (pFile != NULL) {
  50.         return pFile;
  51.     }
  52.  
  53.     /* Try the global version of the fontnames file */
  54.     szGlobalFile = GLOBAL_ANTIWORD_DIR FILE_SEPARATOR FONTNAMES_FILE;
  55.     DBG_MSG(szGlobalFile);
  56.     pFile = fopen(szGlobalFile, "r");
  57.     if (pFile == NULL) {
  58.         werr(0, "I can not open your fontnames file for reading");
  59.     }
  60.     return pFile;
  61. } /* end of pOpenFontTableFile */
  62.  
  63. /*
  64.  * vCloseFont - close the current font, if any
  65.  */
  66. void
  67. vCloseFont(void)
  68. {
  69.     NO_DBG_MSG("vCloseFont");
  70.     /* For safety: to be overwritten at the next call of tOpenfont() */
  71.     bUsePlainText = TRUE;
  72.     eEncoding = encoding_neutral;
  73. } /* end of vCloseFont */
  74.  
  75. /*
  76.  * tOpenFont - make the given font the current font
  77.  *
  78.  * Returns the font reference number
  79.  */
  80. draw_fontref
  81. tOpenFont(int iWordFontNumber, unsigned char ucFontstyle, int iWordFontsize)
  82. {
  83.     options_type    tOptions;
  84.     const char    *szOurFontname;
  85.     int    iIndex, iFontnumber;
  86.  
  87.     NO_DBG_MSG("tOpenFont");
  88.     NO_DBG_DEC(iWordFontNumber);
  89.     NO_DBG_HEX(ucFontstyle);
  90.     NO_DBG_DEC(iWordFontsize);
  91.  
  92.     /* Keep the relevant bits */
  93.     ucFontstyle &= FONT_BOLD|FONT_ITALIC;
  94.     NO_DBG_HEX(ucFontstyle);
  95.  
  96.     vGetOptions(&tOptions);
  97.     bUsePlainText = !tOptions.bUseOutlineFonts;
  98.     eEncoding = tOptions.eEncoding;
  99.  
  100.     if (bUsePlainText) {
  101.         /* No outline fonts, no postscript just plain text */
  102.         return (draw_fontref)0;
  103.     }
  104.  
  105.     iFontnumber = iGetFontByNumber(iWordFontNumber, ucFontstyle);
  106.     szOurFontname = szGetOurFontname(iFontnumber);
  107.     if (szOurFontname == NULL || szOurFontname[0] == '\0') {
  108.         DBG_DEC(iFontnumber);
  109.         return (draw_fontref)0;
  110.     }
  111.     NO_DBG_MSG(szOurFontname);
  112.  
  113.     for (iIndex = 0; iIndex < (int)elementsof(szFontnames); iIndex++) {
  114.         if (STREQ(szFontnames[iIndex], szOurFontname)) {
  115.             NO_DBG_DEC(iIndex);
  116.             return (draw_fontref)iIndex;
  117.         }
  118.     }
  119.     return (draw_fontref)0;
  120. } /* end of tOpenFont */
  121.  
  122. /*
  123.  * tOpenTableFont - make the table font the current font
  124.  *
  125.  * Returns the font reference number
  126.  */
  127. draw_fontref
  128. tOpenTableFont(int iWordFontsize)
  129. {
  130.     options_type    tOptions;
  131.     int    iWordFontnumber;
  132.  
  133.     NO_DBG_MSG("tOpenTableFont");
  134.  
  135.     vGetOptions(&tOptions);
  136.     bUsePlainText = !tOptions.bUseOutlineFonts;
  137.     if (bUsePlainText) {
  138.         /* No outline fonts, no postscript just plain text */
  139.         return (draw_fontref)0;
  140.     }
  141.  
  142.     iWordFontnumber = iFontname2Fontnumber(TABLE_FONT, FONT_REGULAR);
  143.     if (iWordFontnumber < 0 || iWordFontnumber > (int)UCHAR_MAX) {
  144.         DBG_DEC(iWordFontnumber);
  145.         return (draw_fontref)0;
  146.     }
  147.  
  148.     return tOpenFont(iWordFontnumber, FONT_REGULAR, iWordFontsize);
  149. } /* end of tOpenTableFont */
  150.  
  151. /*
  152.  * szGetFontname - get the fontname
  153.  */
  154. const char *
  155. szGetFontname(draw_fontref tFontRef)
  156. {
  157.     fail((size_t)(unsigned char)tFontRef >= elementsof(szFontnames));
  158.     return szFontnames[(int)(unsigned char)tFontRef];
  159. } /* end of szGetFontname */
  160.  
  161. /*
  162.  * lComputeStringWidth - compute the string width
  163.  *
  164.  * Note: the fontsize is given in half-points!
  165.  *       the stringlength is given in bytes, not characters!
  166.  *
  167.  * Returns the string width in millipoints
  168.  */
  169. long
  170. lComputeStringWidth(char *szString, int iStringLength,
  171.         draw_fontref tFontRef, int iFontsize)
  172. {
  173.     unsigned short    *ausCharWidths;
  174.     unsigned char    *pucChar;
  175.     long    lRelWidth;
  176.     int    iIndex, iFontRef;
  177.  
  178.     fail(szString == NULL || iStringLength < 0);
  179.     fail(iFontsize < MIN_FONT_SIZE || iFontsize > MAX_FONT_SIZE);
  180.  
  181.     if (szString[0] == '\0' || iStringLength <= 0) {
  182.         /* Empty string */
  183.         return 0;
  184.     }
  185.  
  186.     if (eEncoding == encoding_utf8) {
  187.         fail(!bUsePlainText);
  188.         return lChar2MilliPoints(
  189.                 utf8_strwidth(szString, iStringLength));
  190.     }
  191.  
  192.     if (bUsePlainText) {
  193.         /* No current font, use "systemfont" */
  194.         return lChar2MilliPoints(iStringLength);
  195.     }
  196.  
  197.     fail(eEncoding != encoding_iso_8859_1 &&
  198.         eEncoding != encoding_iso_8859_2);
  199.  
  200.     /* Compute the relative string width */
  201.     iFontRef = (int)(unsigned char)tFontRef;
  202.     if (eEncoding == encoding_iso_8859_2) {
  203.         ausCharWidths = ausCharacterWidths2[iFontRef];
  204.     } else {
  205.         ausCharWidths = ausCharacterWidths1[iFontRef];
  206.     }
  207.     lRelWidth = 0;
  208.     for (iIndex = 0, pucChar = (unsigned char *)szString;
  209.          iIndex < iStringLength;
  210.          iIndex++, pucChar++) {
  211.         lRelWidth += (long)ausCharWidths[(int)*pucChar];
  212.     }
  213.  
  214.     /* Compute the absolute string width */
  215.     return (lRelWidth * iFontsize + 1) / 2;
  216. } /* end of lComputeStringWidth */
  217.