Imported from antiword-0.37.tar.gz.
[antiword.git] / fmt_text.c
blobdb211e9438291f42110895bf45e31653d27111f4
1 /*
2 * fmt_text.c
3 * Copyright (C) 2004 A.J. van Os; Released under GNU GPL
5 * Description:
6 * Functions to deal with the Formatted Text format
8 * Based on patches send by: Ofir Reichenberg <ofir@qlusters.com>
10 * The credit should go to him, but all the bugs are mine.
13 #include <string.h>
14 #include "antiword.h"
16 /* The character set */
17 static encoding_type eEncoding = encoding_neutral;
18 /* Current vertical position information */
19 static long lYtopCurr = 0;
20 /* Local representation of the non-breaking space */
21 static UCHAR ucNbsp = 0;
25 * vPrologueFMT - set options and perform the Formatted Text initialization
27 void
28 vPrologueFMT(diagram_type *pDiag, const options_type *pOptions)
30 fail(pDiag == NULL);
31 fail(pOptions == NULL);
33 eEncoding = pOptions->eEncoding;
34 pDiag->lXleft = 0;
35 pDiag->lYtop = 0;
36 lYtopCurr = 0;
37 } /* end of vPrologueFMT */
40 * vPrintFMT - print a Formatted Text string
42 static void
43 vPrintFMT(FILE *pFile,
44 const char *szString, size_t tStringLength, USHORT usFontstyle)
46 const UCHAR *pucByte, *pucStart, *pucLast, *pucNonSpace;
48 fail(szString == NULL);
50 if (szString == NULL || szString[0] == '\0' || tStringLength == 0) {
51 return;
54 if (eEncoding == encoding_utf_8) {
55 fprintf(pFile, "%.*s", (int)tStringLength, szString);
56 return;
59 if (ucNbsp == 0) {
60 ucNbsp = ucGetNbspCharacter();
61 DBG_HEX_C(ucNbsp != 0xa0, ucNbsp);
64 pucStart = (UCHAR *)szString;
65 pucLast = pucStart + tStringLength - 1;
66 pucNonSpace = pucLast;
67 while ((*pucNonSpace == (UCHAR)' ' || *pucNonSpace == ucNbsp) &&
68 pucNonSpace > pucStart) {
69 pucNonSpace--;
72 /* 1: The spaces at the start */
73 pucByte = pucStart;
74 while ((*pucByte == (UCHAR)' ' || *pucByte == ucNbsp) &&
75 pucByte <= pucLast) {
76 (void)putc(' ', pFile);
77 pucByte++;
80 if (pucByte > pucLast) {
81 /* There is no text, just spaces */
82 return;
85 /* 2: Start the *bold*, /italic/ and _underline_ */
86 if (bIsBold(usFontstyle)) {
87 (void)putc('*', pFile);
89 if (bIsItalic(usFontstyle)) {
90 (void)putc('/', pFile);
92 if (bIsUnderline(usFontstyle)) {
93 (void)putc('_', pFile);
96 /* 3: The text itself */
97 while (pucByte <= pucNonSpace) {
98 if (*pucByte == ucNbsp) {
99 (void)putc(' ', pFile);
100 } else {
101 (void)putc((char)*pucByte, pFile);
103 pucByte++;
106 /* 4: End the *bold*, /italic/ and _underline_ */
107 if (bIsUnderline(usFontstyle)) {
108 (void)putc('_', pFile);
110 if (bIsItalic(usFontstyle)) {
111 (void)putc('/', pFile);
113 if (bIsBold(usFontstyle)) {
114 (void)putc('*', pFile);
117 /* 5: The spaces at the end */
118 while (pucByte <= pucLast) {
119 (void)putc(' ', pFile);
120 pucByte++;
122 } /* end of vPrintFMT */
125 * vMoveTo - move to the given X,Y coordinates
127 * Move the current position of the given diagram to its X,Y coordinates,
128 * start on a new page if needed
130 static void
131 vMoveTo(diagram_type *pDiag)
133 int iCount, iNbr;
135 fail(pDiag == NULL);
136 fail(pDiag->pOutFile == NULL);
138 if (pDiag->lYtop != lYtopCurr) {
139 iNbr = iDrawUnits2Char(pDiag->lXleft);
140 for (iCount = 0; iCount < iNbr; iCount++) {
141 (void)putc(FILLER_CHAR, pDiag->pOutFile);
143 lYtopCurr = pDiag->lYtop;
145 } /* end of vMoveTo */
148 * vSubstringFMT - print a sub string
150 void
151 vSubstringFMT(diagram_type *pDiag,
152 const char *szString, size_t tStringLength, long lStringWidth,
153 USHORT usFontstyle)
155 fail(pDiag == NULL || szString == NULL);
156 fail(pDiag->pOutFile == NULL);
157 fail(pDiag->lXleft < 0);
158 fail(tStringLength != strlen(szString));
160 if (szString[0] == '\0' || tStringLength == 0) {
161 return;
164 vMoveTo(pDiag);
165 vPrintFMT(pDiag->pOutFile, szString, tStringLength, usFontstyle);
166 pDiag->lXleft += lStringWidth;
167 } /* end of vSubstringFMT */