examples/PIPS/antiword/src/fmt_text.c

00001 /*
00002  * fmt_text.c
00003  * Copyright (C) 2004 A.J. van Os; Released under GNU GPL
00004  *
00005  * Description:
00006  * Functions to deal with the Formatted Text format
00007  *
00008  * Based on patches send by: Ofir Reichenberg <[email protected]>
00009  *
00010  * The credit should go to him, but all the bugs are mine.
00011  */
00012 
00013 #include <string.h>
00014 #include "antiword.h"
00015 
00016 /* The character set */
00017 static encoding_type    eEncoding = encoding_neutral;
00018 /* Current vertical position information */
00019 static long             lYtopCurr = 0;
00020 /* Local representation of the non-breaking space */
00021 static UCHAR            ucNbsp = 0;
00022 
00023 
00024 /*
00025  * vPrologueFMT - set options and perform the Formatted Text initialization
00026  */
00027 void
00028 vPrologueFMT(diagram_type *pDiag, const options_type *pOptions)
00029 {
00030         fail(pDiag == NULL);
00031         fail(pOptions == NULL);
00032 
00033         eEncoding = pOptions->eEncoding;
00034         pDiag->lXleft = 0;
00035         pDiag->lYtop = 0;
00036         lYtopCurr = 0;
00037 } /* end of vPrologueFMT */
00038 
00039 /*
00040  * vPrintFMT - print a Formatted Text string
00041  */
00042 static void
00043 vPrintFMT(FILE *pFile,
00044         const char *szString, size_t tStringLength, USHORT usFontstyle)
00045 {
00046         const UCHAR     *pucByte, *pucStart, *pucLast, *pucNonSpace;
00047 
00048         fail(szString == NULL);
00049 
00050         if (szString == NULL || szString[0] == '\0' || tStringLength == 0) {
00051                 return;
00052         }
00053 
00054         if (eEncoding == encoding_utf_8) {
00055                 fprintf(pFile, "%.*s", (int)tStringLength, szString);
00056                 return;
00057         }
00058 
00059         if (ucNbsp == 0) {
00060                 ucNbsp = ucGetNbspCharacter();
00061                 DBG_HEX_C(ucNbsp != 0xa0, ucNbsp);
00062         }
00063 
00064         pucStart = (UCHAR *)szString;
00065         pucLast = pucStart + tStringLength - 1;
00066         pucNonSpace = pucLast;
00067         while ((*pucNonSpace == (UCHAR)' ' || *pucNonSpace == ucNbsp) &&
00068                pucNonSpace > pucStart) {
00069                 pucNonSpace--;
00070         }
00071 
00072         /* 1: The spaces at the start */
00073         pucByte = pucStart;
00074         while ((*pucByte == (UCHAR)' ' || *pucByte == ucNbsp) &&
00075                pucByte <= pucLast) {
00076                 (void)putc(' ', pFile);
00077                 pucByte++;
00078         }
00079 
00080         if (pucByte > pucLast) {
00081                 /* There is no text, just spaces */
00082                 return;
00083         }
00084 
00085         /* 2: Start the *bold*, /italic/ and _underline_ */
00086         if (bIsBold(usFontstyle)) {
00087                 (void)putc('*', pFile);
00088         }
00089         if (bIsItalic(usFontstyle)) {
00090                 (void)putc('/', pFile);
00091         }
00092         if (bIsUnderline(usFontstyle)) {
00093                 (void)putc('_', pFile);
00094         }
00095 
00096         /* 3: The text itself */
00097         while (pucByte <= pucNonSpace) {
00098                 if (*pucByte == ucNbsp) {
00099                         (void)putc(' ', pFile);
00100                 } else {
00101                         (void)putc((char)*pucByte, pFile);
00102                 }
00103                 pucByte++;
00104         }
00105 
00106         /* 4: End the *bold*, /italic/ and _underline_ */
00107         if (bIsUnderline(usFontstyle)) {
00108                 (void)putc('_', pFile);
00109         }
00110         if (bIsItalic(usFontstyle)) {
00111                 (void)putc('/', pFile);
00112         }
00113         if (bIsBold(usFontstyle)) {
00114                 (void)putc('*', pFile);
00115         }
00116 
00117         /* 5: The spaces at the end */
00118         while (pucByte <= pucLast) {
00119                 (void)putc(' ', pFile);
00120                 pucByte++;
00121         }
00122 } /* end of vPrintFMT */
00123 
00124 /*
00125  * vMoveTo - move to the given X,Y coordinates
00126  *
00127  * Move the current position of the given diagram to its X,Y coordinates,
00128  * start on a new page if needed
00129  */
00130 static void
00131 vMoveTo(diagram_type *pDiag)
00132 {
00133         int     iCount, iNbr;
00134 
00135         fail(pDiag == NULL);
00136         fail(pDiag->pOutFile == NULL);
00137 
00138         if (pDiag->lYtop != lYtopCurr) {
00139                 iNbr = iDrawUnits2Char(pDiag->lXleft);
00140                 for (iCount = 0; iCount < iNbr; iCount++) {
00141                         (void)putc(FILLER_CHAR, pDiag->pOutFile);
00142                 }
00143                 lYtopCurr = pDiag->lYtop;
00144         }
00145 } /* end of vMoveTo */
00146 
00147 /*
00148  * vSubstringFMT - print a sub string
00149  */
00150 void
00151 vSubstringFMT(diagram_type *pDiag,
00152         const char *szString, size_t tStringLength, long lStringWidth,
00153         USHORT usFontstyle)
00154 {
00155         fail(pDiag == NULL || szString == NULL);
00156         fail(pDiag->pOutFile == NULL);
00157         fail(pDiag->lXleft < 0);
00158         fail(tStringLength != strlen(szString));
00159 
00160         if (szString[0] == '\0' || tStringLength == 0) {
00161                 return;
00162         }
00163 
00164         vMoveTo(pDiag);
00165         vPrintFMT(pDiag->pOutFile, szString, tStringLength, usFontstyle);
00166         pDiag->lXleft += lStringWidth;
00167 } /* end of vSubstringFMT */

Generated by  doxygen 1.6.2