text.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * text.c
  3. * Copyright (C) 1999-2004 A.J. van Os; Released under GNU GPL
  4. *
  5. * Description:
  6. * Functions to deal with the Text format
  7. *
  8. */
  9. #include <string.h>
  10. #include "antiword.h"
  11. /* The character set */
  12. static encoding_type eEncoding = encoding_neutral;
  13. /* Current vertical position information */
  14. static long lYtopCurr = 0;
  15. /* Local representation of the non-breaking space */
  16. static UCHAR ucNbsp = 0;
  17. /*
  18. * vPrologueTXT - set options and perform the Text initialization
  19. */
  20. void
  21. vPrologueTXT(diagram_type *pDiag, const options_type *pOptions)
  22. {
  23. fail(pDiag == NULL);
  24. fail(pOptions == NULL);
  25. eEncoding = pOptions->eEncoding;
  26. pDiag->lXleft = 0;
  27. pDiag->lYtop = 0;
  28. lYtopCurr = 0;
  29. } /* end of vPrologueTXT */
  30. /*
  31. * vEpilogueTXT - clean up after everything is done
  32. */
  33. void
  34. vEpilogueTXT(FILE *pOutFile)
  35. {
  36. fail(pOutFile == NULL);
  37. fprintf(pOutFile, "\n");
  38. } /* end of vEpilogueTXT */
  39. /*
  40. * vPrintTXT - print a Text string
  41. */
  42. static void
  43. vPrintTXT(FILE *pFile, const char *szString, size_t tStringLength)
  44. {
  45. const UCHAR *ucBytes;
  46. size_t tCount;
  47. fail(szString == NULL);
  48. if (szString == NULL || szString[0] == '\0' || tStringLength == 0) {
  49. return;
  50. }
  51. if (eEncoding == encoding_utf_8) {
  52. fprintf(pFile, "%.*s", (int)tStringLength, szString);
  53. return;
  54. }
  55. if (ucNbsp == 0) {
  56. ucNbsp = ucGetNbspCharacter();
  57. DBG_HEX_C(ucNbsp != 0xa0, ucNbsp);
  58. }
  59. ucBytes = (UCHAR *)szString;
  60. for (tCount = 0; tCount < tStringLength ; tCount++) {
  61. if (ucBytes[tCount] == ucNbsp) {
  62. (void)putc(' ', pFile);
  63. } else {
  64. (void)putc(szString[tCount], pFile);
  65. }
  66. }
  67. } /* end of vPrintTXT */
  68. /*
  69. * vMoveTo - move to the given X,Y coordinates
  70. *
  71. * Move the current position of the given diagram to its X,Y coordinates,
  72. * start on a new page if needed
  73. */
  74. static void
  75. vMoveTo(diagram_type *pDiag)
  76. {
  77. int iCount, iNbr;
  78. fail(pDiag == NULL);
  79. fail(pDiag->pOutFile == NULL);
  80. if (pDiag->lYtop != lYtopCurr) {
  81. iNbr = iDrawUnits2Char(pDiag->lXleft);
  82. for (iCount = 0; iCount < iNbr; iCount++) {
  83. (void)putc(FILLER_CHAR, pDiag->pOutFile);
  84. }
  85. lYtopCurr = pDiag->lYtop;
  86. }
  87. } /* end of vMoveTo */
  88. /*
  89. * vMove2NextLineTXT - move to the next line
  90. */
  91. void
  92. vMove2NextLineTXT(diagram_type *pDiag)
  93. {
  94. fail(pDiag == NULL);
  95. fail(pDiag->pOutFile == NULL);
  96. pDiag->lYtop++;
  97. (void)fprintf(pDiag->pOutFile, "\n");
  98. } /* end of vMove2NextLineTXT */
  99. /*
  100. * vSubstringTXT - print a sub string
  101. */
  102. void
  103. vSubstringTXT(diagram_type *pDiag,
  104. const char *szString, size_t tStringLength, long lStringWidth)
  105. {
  106. fail(pDiag == NULL || szString == NULL);
  107. fail(pDiag->pOutFile == NULL);
  108. fail(pDiag->lXleft < 0);
  109. fail(tStringLength != strlen(szString));
  110. if (szString[0] == '\0' || tStringLength == 0) {
  111. return;
  112. }
  113. vMoveTo(pDiag);
  114. vPrintTXT(pDiag->pOutFile, szString, tStringLength);
  115. pDiag->lXleft += lStringWidth;
  116. } /* end of vSubstringTXT */
  117. /*
  118. * Create an start of paragraph by moving the y-top mark
  119. */
  120. void
  121. vStartOfParagraphTXT(diagram_type *pDiag, long lBeforeIndentation)
  122. {
  123. fail(pDiag == NULL);
  124. fail(lBeforeIndentation < 0);
  125. if (lBeforeIndentation >= lTwips2MilliPoints(HEADING_GAP)) {
  126. /* A large gap is replaced by an empty line */
  127. vMove2NextLineTXT(pDiag);
  128. }
  129. } /* end of vStartOfParagraphTXT */
  130. /*
  131. * Create an end of paragraph by moving the y-top mark
  132. */
  133. void
  134. vEndOfParagraphTXT(diagram_type *pDiag, long lAfterIndentation)
  135. {
  136. fail(pDiag == NULL);
  137. fail(pDiag->pOutFile == NULL);
  138. fail(lAfterIndentation < 0);
  139. if (pDiag->lXleft > 0) {
  140. /* To the start of the line */
  141. vMove2NextLineTXT(pDiag);
  142. }
  143. if (lAfterIndentation >= lTwips2MilliPoints(HEADING_GAP)) {
  144. /* A large gap is replaced by an empty line */
  145. vMove2NextLineTXT(pDiag);
  146. }
  147. } /* end of vEndOfParagraphTXT */
  148. /*
  149. * Create an end of page
  150. */
  151. void
  152. vEndOfPageTXT(diagram_type *pDiag, long lAfterIndentation)
  153. {
  154. vEndOfParagraphTXT(pDiag, lAfterIndentation);
  155. } /* end of vEndOfPageTXT */