chartrans.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. /*
  2. * chartrans.c
  3. * Copyright (C) 1999-2004 A.J. van Os; Released under GNU GPL
  4. *
  5. * Description:
  6. * Translate Word characters to local representation
  7. */
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <ctype.h>
  11. #if defined(__STDC_ISO_10646__)
  12. #include <wctype.h>
  13. #endif /* __STDC_ISO_10646__ */
  14. #include "antiword.h"
  15. static const USHORT usCp850[] = { /* DOS implementation of Latin1 */
  16. 0x00c7, 0x00fc, 0x00e9, 0x00e2, 0x00e4, 0x00e0, 0x00e5, 0x00e7,
  17. 0x00ea, 0x00eb, 0x00e8, 0x00ef, 0x00ee, 0x00ec, 0x00c4, 0x00c5,
  18. 0x00c9, 0x00e6, 0x00c6, 0x00f4, 0x00f6, 0x00f2, 0x00fb, 0x00f9,
  19. 0x00ff, 0x00d6, 0x00dc, 0x00f8, 0x00a3, 0x00d8, 0x00d7, 0x0192,
  20. 0x00e1, 0x00ed, 0x00f3, 0x00fa, 0x00f1, 0x00d1, 0x00aa, 0x00ba,
  21. 0x00bf, 0x00ae, 0x00ac, 0x00bd, 0x00bc, 0x00a1, 0x00ab, 0x00bb,
  22. 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x00c1, 0x00c2, 0x00c0,
  23. 0x00a9, 0x2563, 0x2551, 0x2557, 0x255d, 0x00a2, 0x00a5, 0x2510,
  24. 0x2514, 0x2534, 0x252c, 0x251c, 0x2500, 0x253c, 0x00e3, 0x00c3,
  25. 0x255a, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256c, 0x00a4,
  26. 0x00f0, 0x00d0, 0x00ca, 0x00cb, 0x00c8, 0x0131, 0x00cd, 0x00ce,
  27. 0x00cf, 0x2518, 0x250c, 0x2588, 0x2584, 0x00a6, 0x00cc, 0x2580,
  28. 0x00d3, 0x00df, 0x00d4, 0x00d2, 0x00f5, 0x00d5, 0x00b5, 0x00fe,
  29. 0x00de, 0x00da, 0x00db, 0x00d9, 0x00fd, 0x00dd, 0x00af, 0x00b4,
  30. 0x00ad, 0x00b1, 0x2017, 0x00be, 0x00b6, 0x00a7, 0x00f7, 0x00b8,
  31. 0x00b0, 0x00a8, 0x00b7, 0x00b9, 0x00b3, 0x00b2, 0x25a0, 0x00a0,
  32. };
  33. static const USHORT usCp1250[] = { /* Windows implementation of Latin2 */
  34. 0x20ac, 0x003f, 0x201a, 0x003f, 0x201e, 0x2026, 0x2020, 0x2021,
  35. 0x003f, 0x2030, 0x0160, 0x2039, 0x015a, 0x0164, 0x017d, 0x0179,
  36. 0x003f, 0x2018, 0x2019, 0x201c, 0x201d, 0x2022, 0x2013, 0x2014,
  37. 0x003f, 0x2122, 0x0161, 0x203a, 0x015b, 0x0165, 0x017e, 0x017a,
  38. 0x00a0, 0x02c7, 0x02d8, 0x0141, 0x00a4, 0x0104, 0x00a6, 0x00a7,
  39. 0x00a8, 0x00a9, 0x015e, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x017b,
  40. 0x00b0, 0x00b1, 0x02db, 0x0142, 0x00b4, 0x00b5, 0x00b6, 0x00b7,
  41. 0x00b8, 0x0105, 0x015f, 0x00bb, 0x013d, 0x02dd, 0x013e, 0x017c,
  42. 0x0154, 0x00c1, 0x00c2, 0x0102, 0x00c4, 0x0139, 0x0106, 0x00c7,
  43. 0x010c, 0x00c9, 0x0118, 0x00cb, 0x011a, 0x00cd, 0x00ce, 0x010e,
  44. 0x0110, 0x0143, 0x0147, 0x00d3, 0x00d4, 0x0150, 0x00d6, 0x00d7,
  45. 0x0158, 0x016e, 0x00da, 0x0170, 0x00dc, 0x00dd, 0x0162, 0x00df,
  46. 0x0155, 0x00e1, 0x00e2, 0x0103, 0x00e4, 0x013a, 0x0107, 0x00e7,
  47. 0x010d, 0x00e9, 0x0119, 0x00eb, 0x011b, 0x00ed, 0x00ee, 0x010f,
  48. 0x0111, 0x0144, 0x0148, 0x00f3, 0x00f4, 0x0151, 0x00f6, 0x00f7,
  49. 0x0159, 0x016f, 0x00fa, 0x0171, 0x00fc, 0x00fd, 0x0163, 0x02d9,
  50. };
  51. static const USHORT usCp1251[] = { /* Windows implementation of Cyrillic */
  52. 0x0402, 0x0403, 0x201a, 0x0453, 0x201e, 0x2026, 0x2020, 0x2021,
  53. 0x20ac, 0x2030, 0x0409, 0x2039, 0x040a, 0x040c, 0x040b, 0x040f,
  54. 0x0452, 0x2018, 0x2019, 0x201c, 0x201d, 0x2022, 0x2013, 0x2014,
  55. 0x00f3, 0x2122, 0x0459, 0x203a, 0x045a, 0x045c, 0x045b, 0x045f,
  56. 0x00a0, 0x040e, 0x045e, 0x0408, 0x00a4, 0x0490, 0x00a6, 0x00a7,
  57. 0x0401, 0x00a9, 0x0404, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x0407,
  58. 0x00b0, 0x00b1, 0x0406, 0x0456, 0x0491, 0x00b5, 0x00b6, 0x00b7,
  59. 0x0451, 0x2116, 0x0454, 0x00bb, 0x0458, 0x0405, 0x0455, 0x0457,
  60. 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417,
  61. 0x0418, 0x0419, 0x041a, 0x041b, 0x041c, 0x041d, 0x041e, 0x041f,
  62. 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427,
  63. 0x0428, 0x0429, 0x042a, 0x042b, 0x042c, 0x042d, 0x042e, 0x042f,
  64. 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437,
  65. 0x0438, 0x0439, 0x043a, 0x043b, 0x043c, 0x043d, 0x043e, 0x043f,
  66. 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447,
  67. 0x0448, 0x0449, 0x044a, 0x044b, 0x044c, 0x044d, 0x044e, 0x044f,
  68. };
  69. static const USHORT usCp1252[] = { /* Windows implementation of Latin1 */
  70. 0x20ac, 0x003f, 0x201a, 0x0192, 0x201e, 0x2026, 0x2020, 0x2021,
  71. 0x02c6, 0x2030, 0x0160, 0x2039, 0x0152, 0x003f, 0x017d, 0x003f,
  72. 0x003f, 0x2018, 0x2019, 0x201c, 0x201d, 0x2022, 0x2013, 0x2014,
  73. 0x02dc, 0x2122, 0x0161, 0x203a, 0x0153, 0x003f, 0x017e, 0x0178,
  74. 0x00a0, 0x00a1, 0x00a2, 0x00a3, 0x00a4, 0x00a5, 0x00a6, 0x00a7,
  75. 0x00a8, 0x00a9, 0x00aa, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x00af,
  76. 0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x00b6, 0x00b7,
  77. 0x00b8, 0x00b9, 0x00ba, 0x00bb, 0x00bc, 0x00bd, 0x00be, 0x00bf,
  78. 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x00c7,
  79. 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf,
  80. 0x00d0, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7,
  81. 0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x00de, 0x00df,
  82. 0x00e0, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7,
  83. 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef,
  84. 0x00f0, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x00f7,
  85. 0x00f8, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x00fd, 0x00fe, 0x00ff,
  86. };
  87. static const USHORT usMacRoman[] = { /* Apple implementation of Latin1 */
  88. 0x00c4, 0x00c5, 0x00c7, 0x00c9, 0x00d1, 0x00d6, 0x00dc, 0x00e1,
  89. 0x00e0, 0x00e2, 0x00e4, 0x00e3, 0x00e5, 0x00e7, 0x00e9, 0x00e8,
  90. 0x00ea, 0x00eb, 0x00ed, 0x00ec, 0x00ee, 0x00ef, 0x00f1, 0x00f3,
  91. 0x00f2, 0x00f4, 0x00f6, 0x00f5, 0x00fa, 0x00f9, 0x00fb, 0x00fc,
  92. 0x2020, 0x00b0, 0x00a2, 0x00a3, 0x00a7, 0x2022, 0x00b6, 0x00df,
  93. 0x00ae, 0x00a9, 0x2122, 0x00b4, 0x00a8, 0x2260, 0x00c6, 0x00d8,
  94. 0x221e, 0x00b1, 0x2264, 0x2265, 0x00a5, 0x00b5, 0x2202, 0x2211,
  95. 0x220f, 0x03c0, 0x222b, 0x00aa, 0x00ba, 0x2126, 0x00e6, 0x00f8,
  96. 0x00bf, 0x00a1, 0x00ac, 0x221a, 0x0192, 0x2248, 0x2206, 0x00ab,
  97. 0x00bb, 0x2026, 0x00a0, 0x00c0, 0x00c3, 0x00d5, 0x0152, 0x0153,
  98. 0x2013, 0x2014, 0x201c, 0x201d, 0x2018, 0x2019, 0x00f7, 0x25ca,
  99. 0x00ff, 0x0178, 0x2044, 0x00a4, 0x2039, 0x203a, 0xfb01, 0xfb02,
  100. 0x2021, 0x00b7, 0x201a, 0x201e, 0x2030, 0x00c2, 0x00ca, 0x00c1,
  101. 0x00cb, 0x00c8, 0x00cd, 0x00ce, 0x00cf, 0x00cc, 0x00d3, 0x00d4,
  102. 0x003f, 0x00d2, 0x00da, 0x00db, 0x00d9, 0x0131, 0x02c6, 0x02dc,
  103. 0x00af, 0x02d8, 0x02d9, 0x02da, 0x00b8, 0x02dd, 0x02db, 0x02c7,
  104. };
  105. static const USHORT usPrivateArea[] = {
  106. 0x0020, 0x0021, 0x2200, 0x0023, 0x2203, 0x0025, 0x0026, 0x220d,
  107. 0x0028, 0x0029, 0x2217, 0x002b, 0x002c, 0x2212, 0x002e, 0x002f,
  108. 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
  109. 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x2019, 0x003e, 0x003f,
  110. 0x201d, 0x201c, 0x0392, 0x03a7, 0x0394, 0x0395, 0x03a6, 0x0393,
  111. 0x0397, 0x0399, 0x03d1, 0x039a, 0x039b, 0x039c, 0x039d, 0x039f,
  112. 0x03a0, 0x0398, 0x03a1, 0x03a3, 0x03a4, 0x03a5, 0x03c2, 0x03a9,
  113. 0x039e, 0x03a8, 0x0396, 0x005b, 0x2234, 0x005d, 0x22a5, 0x005f,
  114. 0x003f, 0x03b1, 0x03b2, 0x03c7, 0x03b4, 0x03b5, 0x03c6, 0x03b3,
  115. 0x03b7, 0x03b9, 0x03d5, 0x03ba, 0x03bb, 0x03bc, 0x03bd, 0x03bf,
  116. 0x03c0, 0x03b8, 0x03c1, 0x03c3, 0x03c4, 0x03c5, 0x03d6, 0x03c9,
  117. 0x03be, 0x03c8, 0x03b6, 0x007b, 0x007c, 0x007d, 0x223c, 0x003f,
  118. 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f,
  119. 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f,
  120. 0x003f, 0x003f, 0x003f, 0x2022, 0x003f, 0x003f, 0x003f, 0x003f,
  121. 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f, 0x003f,
  122. 0x20ac, 0x03d2, 0x2032, 0x2264, 0x2044, 0x221e, 0x0192, 0x2663,
  123. 0x2666, 0x2665, 0x2660, 0x2194, 0x2190, 0x2191, 0x2192, 0x2193,
  124. 0x00b0, 0x00b1, 0x2033, 0x2265, 0x00d7, 0x221d, 0x2202, 0x2022,
  125. 0x00f7, 0x2260, 0x2261, 0x2248, 0x2026, 0x007c, 0x23af, 0x21b5,
  126. 0x2135, 0x2111, 0x211c, 0x2118, 0x2297, 0x2295, 0x2205, 0x2229,
  127. 0x222a, 0x2283, 0x2287, 0x2284, 0x2282, 0x2286, 0x2208, 0x2209,
  128. 0x2220, 0x2207, 0x00ae, 0x00a9, 0x2122, 0x220f, 0x221a, 0x22c5,
  129. 0x00ac, 0x2227, 0x2228, 0x21d4, 0x21d0, 0x21d1, 0x21d2, 0x21d3,
  130. 0x22c4, 0x3008, 0x00ae, 0x00a9, 0x2122, 0x2211, 0x239b, 0x239c,
  131. 0x239d, 0x23a1, 0x23a2, 0x23a3, 0x23a7, 0x23a8, 0x23a9, 0x23aa,
  132. 0x003f, 0x3009, 0x222b, 0x2320, 0x23ae, 0x2321, 0x239e, 0x239f,
  133. 0x23a0, 0x23a4, 0x23a5, 0x23a6, 0x23ab, 0x23ac, 0x23ad, 0x003f,
  134. };
  135. typedef struct char_table_tag {
  136. UCHAR ucLocal;
  137. USHORT usUnicode;
  138. } char_table_type;
  139. static char_table_type atCharTable[256];
  140. static size_t tNextPosFree = 0;
  141. /*
  142. * iCompare - compare two records
  143. *
  144. * Compares two records. For use by qsort(3C) and bsearch(3C).
  145. *
  146. * returns -1 if rec1 < rec2, 0 if rec1 == rec2, 1 if rec1 > rec2
  147. */
  148. static int
  149. iCompare(const void *pvRecord1, const void *pvRecord2)
  150. {
  151. USHORT usUnicode1, usUnicode2;
  152. usUnicode1 = ((char_table_type *)pvRecord1)->usUnicode;
  153. usUnicode2 = ((char_table_type *)pvRecord2)->usUnicode;
  154. if (usUnicode1 < usUnicode2) {
  155. return -1;
  156. }
  157. if (usUnicode1 > usUnicode2) {
  158. return 1;
  159. }
  160. return 0;
  161. } /* end of iCompare */
  162. /*
  163. * pGetCharTableRecord - get the character table record
  164. *
  165. * returns a pointer to the record when found, otherwise NULL
  166. */
  167. static const char_table_type *
  168. pGetCharTableRecord(USHORT usUnicode)
  169. {
  170. char_table_type tKey;
  171. if (tNextPosFree == 0) {
  172. return NULL;
  173. }
  174. tKey.usUnicode = usUnicode;
  175. tKey.ucLocal = 0;
  176. return (char_table_type *)bsearch(&tKey,
  177. atCharTable,
  178. tNextPosFree, sizeof(atCharTable[0]),
  179. iCompare);
  180. } /* end of pGetCharTableRecord */
  181. /*
  182. * ucGetBulletCharacter - get the local representation of the bullet
  183. */
  184. UCHAR
  185. ucGetBulletCharacter(conversion_type eConversionType, encoding_type eEncoding)
  186. {
  187. #if defined(__riscos)
  188. return 0x8f;
  189. #else
  190. const char_table_type *pRec;
  191. fail(eEncoding == encoding_utf_8);
  192. if (eEncoding == encoding_latin_1 &&
  193. (eConversionType == conversion_ps ||
  194. eConversionType == conversion_pdf)) {
  195. /* Ugly, but it makes the PostScript and PDF look better */
  196. return (UCHAR)143;
  197. }
  198. if (eConversionType != conversion_text &&
  199. eConversionType != conversion_fmt_text) {
  200. pRec = pGetCharTableRecord(UNICODE_BULLET);
  201. if (pRec != NULL) {
  202. return pRec->ucLocal;
  203. }
  204. pRec = pGetCharTableRecord(UNICODE_BULLET_OPERATOR);
  205. if (pRec != NULL) {
  206. return pRec->ucLocal;
  207. }
  208. pRec = pGetCharTableRecord(UNICODE_MIDDLE_DOT);
  209. if (pRec != NULL) {
  210. return pRec->ucLocal;
  211. }
  212. }
  213. return (UCHAR)'.';
  214. #endif /* __riscos */
  215. } /* end of ucGetBulletCharacter */
  216. /*
  217. * ucGetNbspCharacter - get the local representation of the non-breaking space
  218. */
  219. UCHAR
  220. ucGetNbspCharacter(void)
  221. {
  222. const char_table_type *pRec;
  223. pRec = pGetCharTableRecord(0x00a0); /* Unicode non-breaking space */
  224. if (pRec == NULL) {
  225. DBG_MSG("Non-breaking space record not found");
  226. /* No value found, use the best guess */
  227. return (UCHAR)0xa0;
  228. }
  229. return pRec->ucLocal;
  230. } /* end of ucGetNbspCharacter */
  231. /*
  232. * bReadCharacterMappingTable - read the mapping table
  233. *
  234. * Read the character mapping table from file and have the contents sorted
  235. *
  236. * returns TRUE if successful, otherwise FALSE
  237. */
  238. BOOL
  239. bReadCharacterMappingTable(FILE *pFile)
  240. {
  241. char *pcTmp;
  242. ULONG ulUnicode;
  243. UINT uiLocal;
  244. int iFields;
  245. char szLine[81];
  246. if (pFile == NULL) {
  247. return FALSE;
  248. }
  249. /* Clean the table first */
  250. (void)memset(atCharTable, 0, sizeof(atCharTable));
  251. /* Fill the table */
  252. while (fgets(szLine, (int)sizeof(szLine), pFile)) {
  253. if (szLine[0] == '#' ||
  254. szLine[0] == '\r' ||
  255. szLine[0] == '\n') {
  256. /* Comment or empty line */
  257. continue;
  258. }
  259. iFields = sscanf(szLine, "%x %lx %*s", &uiLocal, &ulUnicode);
  260. if (iFields != 2) {
  261. pcTmp = strchr(szLine, '\r');
  262. if (pcTmp != NULL) {
  263. *pcTmp = '\0';
  264. }
  265. pcTmp = strchr(szLine, '\n');
  266. if (pcTmp != NULL) {
  267. *pcTmp = '\0';
  268. }
  269. werr(0, "Syntax error in: '%s'", szLine);
  270. continue;
  271. }
  272. if (uiLocal > 0xff || ulUnicode > 0xffff) {
  273. werr(0, "Syntax error in: '%02x %04lx'",
  274. uiLocal, ulUnicode);
  275. continue;
  276. }
  277. /* Store only the relevant entries */
  278. if (uiLocal != ulUnicode || uiLocal >= 0x80) {
  279. atCharTable[tNextPosFree].ucLocal = (UCHAR)uiLocal;
  280. atCharTable[tNextPosFree].usUnicode = (USHORT)ulUnicode;
  281. tNextPosFree++;
  282. }
  283. if (tNextPosFree >= elementsof(atCharTable)) {
  284. werr(0, "Too many entries in the character mapping "
  285. "file. Ignoring the rest.");
  286. break;
  287. }
  288. }
  289. if (tNextPosFree != 0) {
  290. DBG_HEX(atCharTable[0].usUnicode);
  291. DBG_HEX(atCharTable[tNextPosFree - 1].usUnicode);
  292. qsort(atCharTable,
  293. tNextPosFree, sizeof(atCharTable[0]),
  294. iCompare);
  295. DBG_HEX(atCharTable[0].usUnicode);
  296. DBG_HEX(atCharTable[tNextPosFree - 1].usUnicode);
  297. }
  298. return TRUE;
  299. } /* end of bReadCharacterMappingTable */
  300. /*
  301. * ulTranslateCharacters - Translate characters to local representation
  302. *
  303. * Translate all characters to local representation
  304. *
  305. * returns the translated character
  306. */
  307. ULONG
  308. ulTranslateCharacters(USHORT usChar, ULONG ulFileOffset, int iWordVersion,
  309. conversion_type eConversionType, encoding_type eEncoding,
  310. BOOL bUseMacCharSet)
  311. {
  312. const char_table_type *pTmp;
  313. const USHORT *usCharSet;
  314. usCharSet = NULL;
  315. if (bUseMacCharSet) {
  316. /* Macintosh character set */
  317. usCharSet = usMacRoman;
  318. } else if (iWordVersion == 0) {
  319. /* DOS character set */
  320. usCharSet = usCp850;
  321. } else {
  322. /* Windows character set */
  323. switch (eEncoding) {
  324. case encoding_latin_2:
  325. usCharSet = usCp1250;
  326. break;
  327. case encoding_cyrillic:
  328. usCharSet = usCp1251;
  329. break;
  330. case encoding_latin_1:
  331. default:
  332. usCharSet = usCp1252;
  333. break;
  334. }
  335. }
  336. fail(usCharSet == NULL);
  337. if (usChar >= 0x80 && usChar <= 0x9f) {
  338. /* Translate implementation defined characters */
  339. usChar = usCharSet[usChar - 0x80];
  340. } else if (iWordVersion < 8 && usChar >= 0xa0 && usChar <= 0xff) {
  341. /* Translate old character set to Unixcode */
  342. usChar = usCharSet[usChar - 0x80];
  343. }
  344. /* Microsoft Unicode to real Unicode */
  345. if (usChar >= 0xf020 && usChar <= 0xf0ff) {
  346. DBG_HEX_C(usPrivateArea[usChar - 0xf020] == 0x003f, usChar);
  347. usChar = usPrivateArea[usChar - 0xf020];
  348. }
  349. /* Characters with a special meaning in Word */
  350. switch (usChar) {
  351. case IGNORE_CHARACTER:
  352. case FOOTNOTE_SEPARATOR:
  353. case FOOTNOTE_CONTINUATION:
  354. case ANNOTATION:
  355. case FRAME:
  356. case LINE_FEED:
  357. case WORD_SOFT_HYPHEN:
  358. case UNICODE_HYPHENATION_POINT:
  359. return IGNORE_CHARACTER;
  360. case PICTURE:
  361. case TABLE_SEPARATOR:
  362. case TAB:
  363. case HARD_RETURN:
  364. case PAGE_BREAK:
  365. case PAR_END:
  366. case COLUMN_FEED:
  367. return (ULONG)usChar;
  368. case FOOTNOTE_OR_ENDNOTE:
  369. NO_DBG_HEX(ulFileOffset);
  370. switch (eGetNotetype(ulFileOffset)) {
  371. case notetype_is_footnote:
  372. return FOOTNOTE_CHAR;
  373. case notetype_is_endnote:
  374. return ENDNOTE_CHAR;
  375. default:
  376. return UNKNOWN_NOTE_CHAR;
  377. }
  378. case WORD_UNBREAKABLE_JOIN:
  379. return (ULONG)OUR_UNBREAKABLE_JOIN;
  380. default:
  381. break;
  382. }
  383. if (eEncoding != encoding_utf_8) {
  384. /* Latin characters in an oriental text */
  385. if (usChar >= 0xff01 && usChar <= 0xff5e) {
  386. usChar -= 0xfee0;
  387. }
  388. }
  389. if (eEncoding == encoding_latin_1 &&
  390. (eConversionType == conversion_ps ||
  391. eConversionType == conversion_pdf)) {
  392. /* Ugly, but it makes the PostScript and PDF look better */
  393. switch (usChar) {
  394. case UNICODE_ELLIPSIS:
  395. return 140;
  396. case UNICODE_TRADEMARK_SIGN:
  397. return 141;
  398. case UNICODE_PER_MILLE_SIGN:
  399. return 142;
  400. case UNICODE_BULLET:
  401. case UNICODE_BULLET_OPERATOR:
  402. case UNICODE_BLACK_CLUB_SUIT:
  403. return 143;
  404. case UNICODE_LEFT_SINGLE_QMARK:
  405. return 144;
  406. case UNICODE_RIGHT_SINGLE_QMARK:
  407. return 145;
  408. case UNICODE_SINGLE_LEFT_ANGLE_QMARK:
  409. return 146;
  410. case UNICODE_SINGLE_RIGHT_ANGLE_QMARK:
  411. return 147;
  412. case UNICODE_LEFT_DOUBLE_QMARK:
  413. return 148;
  414. case UNICODE_RIGHT_DOUBLE_QMARK:
  415. return 149;
  416. case UNICODE_DOUBLE_LOW_9_QMARK:
  417. return 150;
  418. case UNICODE_EN_DASH:
  419. return 151;
  420. case UNICODE_EM_DASH:
  421. return 152;
  422. case UNICODE_MINUS_SIGN:
  423. return 153;
  424. case UNICODE_CAPITAL_LIGATURE_OE:
  425. return 154;
  426. case UNICODE_SMALL_LIGATURE_OE:
  427. return 155;
  428. case UNICODE_DAGGER:
  429. return 156;
  430. case UNICODE_DOUBLE_DAGGER:
  431. return 157;
  432. case UNICODE_SMALL_LIGATURE_FI:
  433. return 158;
  434. case UNICODE_SMALL_LIGATURE_FL:
  435. return 159;
  436. default:
  437. break;
  438. }
  439. }
  440. if (eConversionType == conversion_pdf) {
  441. if (eEncoding == encoding_latin_1) {
  442. switch (usChar) {
  443. case UNICODE_EURO_SIGN:
  444. return 128;
  445. default:
  446. break;
  447. }
  448. } else if (eEncoding == encoding_latin_2) {
  449. switch (usChar) {
  450. case UNICODE_CAPITAL_D_WITH_STROKE:
  451. case UNICODE_SMALL_D_WITH_STROKE:
  452. return 0x3f;
  453. default:
  454. break;
  455. }
  456. }
  457. }
  458. if (usChar < 0x80) {
  459. /* US ASCII */
  460. if (usChar < 0x20 || usChar == 0x7f) {
  461. /* Ignore control characters */
  462. DBG_HEX(usChar);
  463. DBG_FIXME();
  464. return IGNORE_CHARACTER;
  465. }
  466. return (ULONG)usChar;
  467. }
  468. if (eEncoding == encoding_utf_8) {
  469. /* No need to convert Unicode characters */
  470. return (ULONG)usChar;
  471. }
  472. /* Unicode to local representation */
  473. pTmp = pGetCharTableRecord(usChar);
  474. if (pTmp != NULL) {
  475. DBG_HEX_C(usChar >= 0x7f && usChar <= 0x9f, usChar);
  476. return (ULONG)pTmp->ucLocal;
  477. }
  478. /* Fancy characters to simple US ASCII */
  479. switch (usChar) {
  480. case UNICODE_SMALL_F_HOOK:
  481. return (ULONG)'f';
  482. case UNICODE_GREEK_CAPITAL_CHI:
  483. return (ULONG)'X';
  484. case UNICODE_GREEK_SMALL_UPSILON:
  485. return (ULONG)'v';
  486. case UNICODE_MODIFIER_CIRCUMFLEX:
  487. case UNICODE_UPWARDS_ARROW:
  488. return (ULONG)'^';
  489. case UNICODE_SMALL_TILDE:
  490. case UNICODE_TILDE_OPERATOR:
  491. return (ULONG)'~';
  492. case UNICODE_EN_QUAD:
  493. case UNICODE_EM_QUAD:
  494. case UNICODE_EN_SPACE:
  495. case UNICODE_EM_SPACE:
  496. case UNICODE_THREE_PER_EM_SPACE:
  497. case UNICODE_FOUR_PER_EM_SPACE:
  498. case UNICODE_SIX_PER_EM_SPACE:
  499. case UNICODE_FIGURE_SPACE:
  500. case UNICODE_PUNCTUATION_SPACE:
  501. case UNICODE_THIN_SPACE:
  502. case UNICODE_NARROW_NO_BREAK_SPACE:
  503. case UNICODE_LIGHT_SHADE:
  504. case UNICODE_MEDIUM_SHADE:
  505. case UNICODE_DARK_SHADE:
  506. return (ULONG)' ';
  507. case UNICODE_LEFT_DOUBLE_QMARK:
  508. case UNICODE_RIGHT_DOUBLE_QMARK:
  509. case UNICODE_DOUBLE_LOW_9_QMARK:
  510. case UNICODE_DOUBLE_HIGH_REV_9_QMARK:
  511. case UNICODE_DOUBLE_PRIME:
  512. return (ULONG)'"';
  513. case UNICODE_LEFT_SINGLE_QMARK:
  514. case UNICODE_RIGHT_SINGLE_QMARK:
  515. case UNICODE_SINGLE_LOW_9_QMARK:
  516. case UNICODE_SINGLE_HIGH_REV_9_QMARK:
  517. case UNICODE_PRIME:
  518. return (ULONG)'\'';
  519. case UNICODE_HYPHEN:
  520. case UNICODE_NON_BREAKING_HYPHEN:
  521. case UNICODE_FIGURE_DASH:
  522. case UNICODE_EN_DASH:
  523. case UNICODE_EM_DASH:
  524. case UNICODE_HORIZONTAL_BAR:
  525. case UNICODE_MINUS_SIGN:
  526. case UNICODE_BD_LIGHT_HORIZONTAL:
  527. case UNICODE_BD_DOUBLE_HORIZONTAL:
  528. return (ULONG)'-';
  529. case UNICODE_DOUBLE_VERTICAL_LINE:
  530. case UNICODE_BD_LIGHT_VERTICAL:
  531. case UNICODE_BD_DOUBLE_VERTICAL:
  532. return (ULONG)'|';
  533. case UNICODE_DOUBLE_LOW_LINE:
  534. return (ULONG)'_';
  535. case UNICODE_DAGGER:
  536. return (ULONG)'+';
  537. case UNICODE_DOUBLE_DAGGER:
  538. return (ULONG)'#';
  539. case UNICODE_BULLET:
  540. case UNICODE_BULLET_OPERATOR:
  541. case UNICODE_BLACK_CLUB_SUIT:
  542. return (ULONG)ucGetBulletCharacter(eConversionType, eEncoding);
  543. case UNICODE_ONE_DOT_LEADER:
  544. case UNICODE_TWO_DOT_LEADER:
  545. return (ULONG)'.';
  546. case UNICODE_ELLIPSIS:
  547. #if defined(__riscos)
  548. return (ULONG)OUR_ELLIPSIS;
  549. #else
  550. if (ulFileOffset == 0) {
  551. return (ULONG)OUR_ELLIPSIS;
  552. }
  553. return UNICODE_ELLIPSIS;
  554. #endif /* __riscos */
  555. case UNICODE_DOUBLE_LEFT_ANGLE_QMARK:
  556. case UNICODE_TRIANGULAR_BULLET:
  557. case UNICODE_SINGLE_LEFT_ANGLE_QMARK:
  558. case UNICODE_LEFTWARDS_ARROW:
  559. return (ULONG)'<';
  560. case UNICODE_DOUBLE_RIGHT_ANGLE_QMARK:
  561. case UNICODE_SINGLE_RIGHT_ANGLE_QMARK:
  562. case UNICODE_RIGHTWARDS_ARROW:
  563. return (ULONG)'>';
  564. case UNICODE_UNDERTIE:
  565. return (ULONG)'-';
  566. case UNICODE_N_ARY_SUMMATION:
  567. return (ULONG)'S';
  568. case UNICODE_EURO_SIGN:
  569. return (ULONG)'E';
  570. case UNICODE_CIRCLE:
  571. case UNICODE_SQUARE:
  572. return (ULONG)'O';
  573. case UNICODE_DIAMOND:
  574. return (ULONG)OUR_DIAMOND;
  575. case UNICODE_NUMERO_SIGN:
  576. return (ULONG)'N';
  577. case UNICODE_KELVIN_SIGN:
  578. return (ULONG)'K';
  579. case UNICODE_DOWNWARDS_ARROW:
  580. return (ULONG)'v';
  581. case UNICODE_FRACTION_SLASH:
  582. case UNICODE_DIVISION_SLASH:
  583. return (ULONG)'/';
  584. case UNICODE_ASTERISK_OPERATOR:
  585. return (ULONG)'*';
  586. case UNICODE_RATIO:
  587. return (ULONG)':';
  588. case UNICODE_BD_LIGHT_DOWN_RIGHT:
  589. case UNICODE_BD_LIGHT_DOWN_AND_LEFT:
  590. case UNICODE_BD_LIGHT_UP_AND_RIGHT:
  591. case UNICODE_BD_LIGHT_UP_AND_LEFT:
  592. case UNICODE_BD_LIGHT_VERTICAL_AND_RIGHT:
  593. case UNICODE_BD_LIGHT_VERTICAL_AND_LEFT:
  594. case UNICODE_BD_LIGHT_DOWN_AND_HORIZONTAL:
  595. case UNICODE_BD_LIGHT_UP_AND_HORIZONTAL:
  596. case UNICODE_BD_LIGHT_VERTICAL_AND_HORIZONTAL:
  597. case UNICODE_BD_DOUBLE_DOWN_AND_RIGHT:
  598. case UNICODE_BD_DOUBLE_DOWN_AND_LEFT:
  599. case UNICODE_BD_DOUBLE_UP_AND_RIGHT:
  600. case UNICODE_BD_DOUBLE_UP_AND_LEFT:
  601. case UNICODE_BD_DOUBLE_VERTICAL_AND_RIGHT:
  602. case UNICODE_BD_DOUBLE_VERTICAL_AND_LEFT:
  603. case UNICODE_BD_DOUBLE_DOWN_AND_HORIZONTAL:
  604. case UNICODE_BD_DOUBLE_UP_AND_HORIZONTAL:
  605. case UNICODE_BD_DOUBLE_VERTICAL_AND_HORIZONTAL:
  606. case UNICODE_BLACK_SQUARE:
  607. return (ULONG)'+';
  608. case UNICODE_HAIR_SPACE:
  609. case UNICODE_ZERO_WIDTH_SPACE:
  610. case UNICODE_ZERO_WIDTH_NON_JOINER:
  611. case UNICODE_ZERO_WIDTH_JOINER:
  612. case UNICODE_LEFT_TO_RIGHT_MARK:
  613. case UNICODE_RIGHT_TO_LEFT_MARK:
  614. case UNICODE_LEFT_TO_RIGHT_EMBEDDING:
  615. case UNICODE_RIGHT_TO_LEFT_EMBEDDING:
  616. case UNICODE_POP_DIRECTIONAL_FORMATTING:
  617. case UNICODE_LEFT_TO_RIGHT_OVERRIDE:
  618. case UNICODE_RIGHT_TO_LEFT_OVERRIDE:
  619. case UNICODE_ZERO_WIDTH_NO_BREAK_SPACE:
  620. return IGNORE_CHARACTER;
  621. default:
  622. break;
  623. }
  624. if (usChar == UNICODE_TRADEMARK_SIGN) {
  625. /*
  626. * No local representation, it doesn't look like anything in
  627. * US-ASCII and a question mark does more harm than good.
  628. */
  629. return IGNORE_CHARACTER;
  630. }
  631. if (usChar >= 0xa0 && usChar <= 0xff) {
  632. /* Before Word 97, Word did't use Unicode */
  633. return (ULONG)usChar;
  634. }
  635. DBG_HEX_C(usChar < 0x3000 || usChar >= 0xd800, ulFileOffset);
  636. DBG_HEX_C(usChar < 0x3000 || usChar >= 0xd800, usChar);
  637. DBG_MSG_C(usChar >= 0xe000 && usChar < 0xf900, "Private Use Area");
  638. /* Untranslated Unicode character */
  639. return 0x3f;
  640. } /* end of ulTranslateCharacters */
  641. /*
  642. * ulToUpper - convert letter to upper case
  643. *
  644. * This function converts a letter to upper case. Unlike toupper(3) this
  645. * function is independent from the settings of locale. This comes in handy
  646. * for people who have to read Word documents in more than one language or
  647. * contain more than one language.
  648. *
  649. * returns the converted letter, or ulChar if the conversion was not possible.
  650. */
  651. ULONG
  652. ulToUpper(ULONG ulChar)
  653. {
  654. if (ulChar < 0x80) {
  655. /* US ASCII: use standard function */
  656. return (ULONG)toupper((int)ulChar);
  657. }
  658. if (ulChar >= 0xe0 && ulChar <= 0xfe && ulChar != 0xf7) {
  659. /*
  660. * Lower case accented characters
  661. * 0xf7 is Division sign; 0xd7 is Multiplication sign
  662. * 0xff is y with diaeresis; 0xdf is Sharp s
  663. */
  664. return ulChar & ~0x20;
  665. }
  666. #if defined(__STDC_ISO_10646__)
  667. /*
  668. * If this is ISO C99 and all locales have wchar_t = ISO 10646
  669. * (e.g., glibc 2.2 or newer), then use standard function
  670. */
  671. if (ulChar > 0xff) {
  672. return (ULONG)towupper((wint_t)ulChar);
  673. }
  674. #endif /* __STDC_ISO_10646__ */
  675. return ulChar;
  676. } /* end of ulToUpper */