home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / anwor032.zip / antiword.0.32 / tabstop.c < prev    next >
C/C++ Source or Header  |  2000-12-18  |  4KB  |  142 lines

  1. /*
  2.  * tabstops.c
  3.  * Copyright (C) 1999,2000 A.J. van Os; Released under GPL
  4.  *
  5.  * Description:
  6.  * Read the tab stop information from a MS Word file
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include "antiword.h"
  11.  
  12. static long    lDefaultTabWidth = 36000;    /* In millipoints */
  13.  
  14.  
  15. /*
  16.  * vSet6DefaultTabWidth -
  17.  */
  18. static void
  19. vSet6DefaultTabWidth(FILE *pFile, long lStartBlock,
  20.     const long *alBBD, size_t tBBDLen,
  21.     const unsigned char *aucHeader)
  22. {
  23.     unsigned char    *aucBuffer;
  24.     long        lBeginDocpInfo;
  25.     size_t        tDocpInfoLen;
  26.  
  27.     lBeginDocpInfo = (long)ulGetLong(0x150, aucHeader);
  28.     DBG_HEX(lBeginDocpInfo);
  29.     tDocpInfoLen = (size_t)ulGetLong(0x154, aucHeader);
  30.     DBG_DEC(tDocpInfoLen);
  31.  
  32.     aucBuffer = xmalloc(tDocpInfoLen);
  33.     if (!bReadBuffer(pFile, lStartBlock,
  34.             alBBD, tBBDLen, BIG_BLOCK_SIZE,
  35.             aucBuffer, lBeginDocpInfo, tDocpInfoLen)) {
  36.         aucBuffer = xfree(aucBuffer);
  37.         return;
  38.     }
  39.     lDefaultTabWidth = lTwips2MilliPoints(usGetWord(0x0a, aucBuffer));
  40.     DBG_DEC(lDefaultTabWidth);
  41.     aucBuffer = xfree(aucBuffer);
  42. } /* end of vSet6DefaultTabWidth */
  43.  
  44. /*
  45.  * vSet8DefaultTabWidth -
  46.  */
  47. static void
  48. vSet8DefaultTabWidth(FILE *pFile, const pps_info_type *pPPS,
  49.     const long *alBBD, size_t tBBDLen, const long *alSBD, size_t tSBDLen,
  50.     const unsigned char *aucHeader)
  51. {
  52.         const long    *alBlockDepot;
  53.     unsigned char    *aucBuffer;
  54.     long    lTableSize, lTableStartBlock, lBeginDocpInfo;
  55.     size_t    tDocpInfoLen, tBlockDepotLen, tBlockSize;
  56.     unsigned short    usDocStatus;
  57.  
  58.     lBeginDocpInfo = (long)ulGetLong(0x192, aucHeader);
  59.     DBG_HEX(lBeginDocpInfo);
  60.     tDocpInfoLen = (size_t)ulGetLong(0x196, aucHeader);
  61.     DBG_DEC(tDocpInfoLen);
  62.  
  63.     /* Use 0Table or 1Table? */
  64.     usDocStatus = usGetWord(0x0a, aucHeader);
  65.     if (usDocStatus & BIT(9)) {
  66.         lTableStartBlock = pPPS->t1Table.lSb;
  67.         lTableSize = pPPS->t1Table.lSize;
  68.     } else {
  69.         lTableStartBlock = pPPS->t0Table.lSb;
  70.         lTableSize = pPPS->t0Table.lSize;
  71.     }
  72.     DBG_DEC(lTableStartBlock);
  73.     if (lTableStartBlock < 0) {
  74.         DBG_DEC(lTableStartBlock);
  75.         DBG_MSG("No TAB information");
  76.         return;
  77.     }
  78.     DBG_HEX(lTableSize);
  79.     if (lTableSize < MIN_SIZE_FOR_BBD_USE) {
  80.         /* Use the Small Block Depot */
  81.         alBlockDepot = alSBD;
  82.         tBlockDepotLen = tSBDLen;
  83.         tBlockSize = SMALL_BLOCK_SIZE;
  84.     } else {
  85.         /* Use the Big Block Depot */
  86.         alBlockDepot = alBBD;
  87.         tBlockDepotLen = tBBDLen;
  88.         tBlockSize = BIG_BLOCK_SIZE;
  89.     }
  90.     aucBuffer = xmalloc(tDocpInfoLen);
  91.     if (!bReadBuffer(pFile, lTableStartBlock,
  92.             alBlockDepot, tBlockDepotLen, tBlockSize,
  93.             aucBuffer, lBeginDocpInfo, tDocpInfoLen)) {
  94.         aucBuffer = xfree(aucBuffer);
  95.         return;
  96.     }
  97.     lDefaultTabWidth = lTwips2MilliPoints(usGetWord(0x0a, aucBuffer));
  98.     DBG_DEC(lDefaultTabWidth);
  99.     aucBuffer = xfree(aucBuffer);
  100. } /* end of vSet8DefaultTabWidth */
  101.  
  102. /*
  103.  * vSetDefaultTabWidth -
  104.  */
  105. void
  106. vSetDefaultTabWidth(FILE *pFile, const pps_info_type *pPPS,
  107.     const long *alBBD, size_t tBBDLen, const long *alSBD, size_t tSBDLen,
  108.     const unsigned char *aucHeader, int iWordVersion)
  109. {
  110.     fail(pFile == NULL || pPPS == NULL || aucHeader == NULL);
  111.     fail(iWordVersion < 6 || iWordVersion > 8);
  112.     fail(alBBD == NULL || alSBD == NULL);
  113.  
  114.     switch (iWordVersion) {
  115.     case 6:
  116.     case 7:
  117.         vSet6DefaultTabWidth(pFile, pPPS->tWordDocument.lSb,
  118.                 alBBD, tBBDLen, aucHeader);
  119.         break;
  120.     case 8:
  121.         vSet8DefaultTabWidth(pFile, pPPS,
  122.                 alBBD, tBBDLen, alSBD, tSBDLen, aucHeader);
  123.         break;
  124.     default:
  125.         werr(0, "Sorry, no TAB information");
  126.         break;
  127.     }
  128. } /* end of vSetDefaultTabWidth */
  129.  
  130. /*
  131.  * lGetDefaultTabWidth - Get the default tabwidth in millipoints
  132.  */
  133. long
  134. lGetDefaultTabWidth(void)
  135. {
  136.     if (lDefaultTabWidth <= 0) {
  137.         DBG_DEC(lDefaultTabWidth);
  138.         return lTwips2MilliPoints(1);
  139.     }
  140.     return lDefaultTabWidth;
  141. } /* end of lGetDefaultTabWidth */
  142.