fonts.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  1. /*
  2. * fonts.c
  3. * Copyright (C) 1998-2004 A.J. van Os; Released under GNU GPL
  4. *
  5. * Description:
  6. * Functions to deal with fonts (generic)
  7. */
  8. #include <ctype.h>
  9. #include <string.h>
  10. #include "antiword.h"
  11. /* Maximum line length in the font file */
  12. #define FONT_LINE_LENGTH 81
  13. /* Pitch */
  14. #define PITCH_UNKNOWN 0
  15. #define PITCH_FIXED 1
  16. #define PITCH_VARIABLE 2
  17. /* Font Family */
  18. #define FAMILY_UNKNOWN 0
  19. #define FAMILY_ROMAN 1
  20. #define FAMILY_SWISS 2
  21. #define FAMILY_MODERN 3
  22. #define FAMILY_SCRIPT 4
  23. #define FAMILY_DECORATIVE 5
  24. /* Font Translation Table */
  25. static size_t tFontTableRecords = 0;
  26. static font_table_type *pFontTable = NULL;
  27. /*
  28. * Find the given font in the font table
  29. *
  30. * returns the index into the FontTable, -1 if not found
  31. */
  32. int
  33. iGetFontByNumber(UCHAR ucWordFontNumber, USHORT usFontStyle)
  34. {
  35. int iIndex;
  36. for (iIndex = 0; iIndex < (int)tFontTableRecords; iIndex++) {
  37. if (ucWordFontNumber == pFontTable[iIndex].ucWordFontNumber &&
  38. usFontStyle == pFontTable[iIndex].usFontStyle &&
  39. pFontTable[iIndex].szOurFontname[0] != '\0') {
  40. return iIndex;
  41. }
  42. }
  43. DBG_DEC(ucWordFontNumber);
  44. DBG_HEX(usFontStyle);
  45. return -1;
  46. } /* end of iGetFontByNumber */
  47. /*
  48. * szGetOurFontname - Get our font name
  49. *
  50. * return our font name from the given index, NULL if not found
  51. */
  52. const char *
  53. szGetOurFontname(int iIndex)
  54. {
  55. if (iIndex < 0 || iIndex >= (int)tFontTableRecords) {
  56. return NULL;
  57. }
  58. return pFontTable[iIndex].szOurFontname;
  59. } /* end of szGetOurFontname */
  60. /*
  61. * Find the given font in the font table
  62. *
  63. * returns the Word font number, -1 if not found
  64. */
  65. int
  66. iFontname2Fontnumber(const char *szOurFontname, USHORT usFontStyle)
  67. {
  68. int iIndex;
  69. for (iIndex = 0; iIndex < (int)tFontTableRecords; iIndex++) {
  70. if (pFontTable[iIndex].usFontStyle == usFontStyle &&
  71. STREQ(pFontTable[iIndex].szOurFontname, szOurFontname)) {
  72. return (int)pFontTable[iIndex].ucWordFontNumber;
  73. }
  74. }
  75. return -1;
  76. } /* end of iFontname2Fontnumber */
  77. /*
  78. * szGetDefaultFont - get the default font that matches the parameters
  79. */
  80. static const char *
  81. szGetDefaultFont(UCHAR ucFFN, int iEmphasis)
  82. {
  83. UCHAR ucPrq, ucFf;
  84. fail(iEmphasis < 0 || iEmphasis > 3);
  85. ucPrq = ucFFN & 0x03;
  86. ucFf = (ucFFN & 0x70) >> 4;
  87. NO_DBG_DEC(ucPrq);
  88. NO_DBG_DEC(ucFf);
  89. if (ucPrq == PITCH_FIXED) {
  90. /* Set to the default monospaced font */
  91. switch (iEmphasis) {
  92. case 1: return FONT_MONOSPACED_BOLD;
  93. case 2: return FONT_MONOSPACED_ITALIC;
  94. case 3: return FONT_MONOSPACED_BOLDITALIC;
  95. default: return FONT_MONOSPACED_PLAIN;
  96. }
  97. } else if (ucFf == FAMILY_ROMAN) {
  98. /* Set to the default serif font */
  99. switch (iEmphasis) {
  100. case 1: return FONT_SERIF_BOLD;
  101. case 2: return FONT_SERIF_ITALIC;
  102. case 3: return FONT_SERIF_BOLDITALIC;
  103. default: return FONT_SERIF_PLAIN;
  104. }
  105. } else if (ucFf == FAMILY_SWISS) {
  106. /* Set to the default sans serif font */
  107. switch (iEmphasis) {
  108. case 1: return FONT_SANS_SERIF_BOLD;
  109. case 2: return FONT_SANS_SERIF_ITALIC;
  110. case 3: return FONT_SANS_SERIF_BOLDITALIC;
  111. default: return FONT_SANS_SERIF_PLAIN;
  112. }
  113. } else {
  114. /* Set to the default default font */
  115. switch (iEmphasis) {
  116. case 1: return FONT_SERIF_BOLD;
  117. case 2: return FONT_SERIF_ITALIC;
  118. case 3: return FONT_SERIF_BOLDITALIC;
  119. default: return FONT_SERIF_PLAIN;
  120. }
  121. }
  122. } /* end of szGetDefaultFont */
  123. /*
  124. * See if the fontname from the Word file matches the fontname from the
  125. * font translation file.
  126. * If iBytesPerChar is one than aucWord is in ISO-8859-x (Word 2/6/7),
  127. * if iBytesPerChar is two than aucWord is in Unicode (Word 8/9/10).
  128. */
  129. static BOOL
  130. bFontEqual(const UCHAR *aucWord, const char *szTable, int iBytesPerChar)
  131. {
  132. const UCHAR *pucTmp;
  133. const char *pcTmp;
  134. fail(aucWord == NULL || szTable == NULL);
  135. fail(iBytesPerChar != 1 && iBytesPerChar != 2);
  136. for (pucTmp = aucWord, pcTmp = szTable;
  137. *pucTmp != 0;
  138. pucTmp += iBytesPerChar, pcTmp++) {
  139. if (ulToUpper((ULONG)*pucTmp) !=
  140. ulToUpper((ULONG)(UCHAR)*pcTmp)) {
  141. return FALSE;
  142. }
  143. }
  144. return *pcTmp == '\0';
  145. } /* end of bFontEqual */
  146. /*
  147. * vFontname2Table - add fontnames to the font table
  148. */
  149. static void
  150. vFontname2Table(const UCHAR *aucFont, const UCHAR *aucAltFont,
  151. int iBytesPerChar, int iEmphasis, UCHAR ucFFN,
  152. const char *szWordFont, const char *szOurFont,
  153. font_table_type *pFontTableRecord)
  154. {
  155. BOOL bMatchFound;
  156. fail(aucFont == NULL || aucFont[0] == 0);
  157. fail(aucAltFont != NULL && aucAltFont[0] == 0);
  158. fail(iBytesPerChar != 1 && iBytesPerChar != 2);
  159. fail(iEmphasis < 0 || iEmphasis > 3);
  160. fail(szWordFont == NULL || szWordFont[0] == '\0');
  161. fail(szOurFont == NULL || szOurFont[0] == '\0');
  162. fail(pFontTableRecord == NULL);
  163. bMatchFound = bFontEqual(aucFont, szWordFont, iBytesPerChar);
  164. if (!bMatchFound && aucAltFont != NULL) {
  165. bMatchFound = bFontEqual(aucAltFont, szWordFont, iBytesPerChar);
  166. }
  167. if (!bMatchFound &&
  168. pFontTableRecord->szWordFontname[0] == '\0' &&
  169. szWordFont[0] == '*' &&
  170. szWordFont[1] == '\0') {
  171. /*
  172. * szWordFont contains a "*", so szOurFont will contain the
  173. * "default default" font. See if we can do better than that.
  174. */
  175. szOurFont = szGetDefaultFont(ucFFN, iEmphasis);
  176. bMatchFound = TRUE;
  177. }
  178. if (bMatchFound) {
  179. switch (iBytesPerChar) {
  180. case 1:
  181. (void)strncpy(pFontTableRecord->szWordFontname,
  182. (const char *)aucFont,
  183. sizeof(pFontTableRecord->szWordFontname) - 1);
  184. break;
  185. case 2:
  186. (void)unincpy(pFontTableRecord->szWordFontname,
  187. aucFont,
  188. sizeof(pFontTableRecord->szWordFontname) - 1);
  189. break;
  190. default:
  191. DBG_FIXME();
  192. pFontTableRecord->szWordFontname[0] = '\0';
  193. break;
  194. }
  195. pFontTableRecord->szWordFontname[
  196. sizeof(pFontTableRecord->szWordFontname) - 1] = '\0';
  197. (void)strncpy(pFontTableRecord->szOurFontname, szOurFont,
  198. sizeof(pFontTableRecord->szOurFontname) - 1);
  199. pFontTableRecord->szOurFontname[
  200. sizeof(pFontTableRecord->szOurFontname) - 1] = '\0';
  201. NO_DBG_MSG(pFontTableRecord->szWordFontname);
  202. NO_DBG_MSG(pFontTableRecord->szOurFontname);
  203. pFontTableRecord->ucFFN = ucFFN;
  204. pFontTableRecord->ucEmphasis = (UCHAR)iEmphasis;
  205. }
  206. } /* end of vFontname2Table */
  207. /*
  208. * vCreateFontTable - Create and initialize the internal font table
  209. */
  210. static void
  211. vCreateFontTable(void)
  212. {
  213. font_table_type *pTmp;
  214. int iNbr;
  215. if (tFontTableRecords == 0) {
  216. pFontTable = xfree(pFontTable);
  217. return;
  218. }
  219. /* Create the font table */
  220. pFontTable = xcalloc(tFontTableRecords, sizeof(*pFontTable));
  221. /* Initialize the font table */
  222. for (iNbr = 0, pTmp = pFontTable;
  223. pTmp < pFontTable + tFontTableRecords;
  224. iNbr++, pTmp++) {
  225. pTmp->ucWordFontNumber = (UCHAR)(iNbr / 4);
  226. switch (iNbr % 4) {
  227. case 0:
  228. pTmp->usFontStyle = FONT_REGULAR;
  229. break;
  230. case 1:
  231. pTmp->usFontStyle = FONT_BOLD;
  232. break;
  233. case 2:
  234. pTmp->usFontStyle = FONT_ITALIC;
  235. break;
  236. case 3:
  237. pTmp->usFontStyle = FONT_BOLD|FONT_ITALIC;
  238. break;
  239. default:
  240. DBG_DEC(iNbr);
  241. break;
  242. }
  243. }
  244. } /* end of vCreateFontTable */
  245. /*
  246. * vMinimizeFontTable - make the font table as small as possible
  247. */
  248. static void
  249. vMinimizeFontTable(void)
  250. {
  251. font_block_type tFontNext;
  252. const style_block_type *pStyle;
  253. const font_block_type *pFont;
  254. font_table_type *pTmp;
  255. int iUnUsed;
  256. BOOL bMustAddTableFont;
  257. NO_DBG_MSG("vMinimizeFontTable");
  258. if (tFontTableRecords == 0) {
  259. pFontTable = xfree(pFontTable);
  260. return;
  261. }
  262. /* See if we must add a font for our tables */
  263. bMustAddTableFont = TRUE;
  264. #if 0
  265. DBG_MSG("Before");
  266. DBG_DEC(tFontTableRecords);
  267. for (pTmp = pFontTable;
  268. pTmp < pFontTable + tFontTableRecords;
  269. pTmp++) {
  270. DBG_DEC(pTmp->ucWordFontNumber);
  271. DBG_HEX(pTmp->usFontStyle);
  272. DBG_MSG(pTmp->szWordFontname);
  273. DBG_MSG(pTmp->szOurFontname);
  274. }
  275. #endif /* DEBUG */
  276. /* See which fonts/styles we really need */
  277. /* Default font/style is by definition in use */
  278. pFontTable[0].ucInUse = 1;
  279. /* Make InUse 1 for all the fonts/styles that WILL be used */
  280. pFont = NULL;
  281. while((pFont = pGetNextFontInfoListItem(pFont)) != NULL) {
  282. pTmp = pFontTable + 4 * (int)pFont->ucFontNumber;
  283. if (bIsBold(pFont->usFontStyle)) {
  284. pTmp++;
  285. }
  286. if (bIsItalic(pFont->usFontStyle)) {
  287. pTmp += 2;
  288. }
  289. if (pTmp >= pFontTable + tFontTableRecords) {
  290. continue;
  291. }
  292. if (STREQ(pTmp->szOurFontname, TABLE_FONT)) {
  293. /* The table font is already present */
  294. bMustAddTableFont = FALSE;
  295. }
  296. pTmp->ucInUse = 1;
  297. }
  298. /* Make InUse 1 for all the fonts/styles that MIGHT be used */
  299. pStyle = NULL;
  300. while((pStyle = pGetNextStyleInfoListItem(pStyle)) != NULL) {
  301. vFillFontFromStylesheet(pStyle->usIstdNext, &tFontNext);
  302. vCorrectFontValues(&tFontNext);
  303. pTmp = pFontTable + 4 * (int)tFontNext.ucFontNumber;
  304. if (bIsBold(tFontNext.usFontStyle)) {
  305. pTmp++;
  306. }
  307. if (bIsItalic(tFontNext.usFontStyle)) {
  308. pTmp += 2;
  309. }
  310. if (pTmp >= pFontTable + tFontTableRecords) {
  311. continue;
  312. }
  313. if (STREQ(pTmp->szOurFontname, TABLE_FONT)) {
  314. /* The table font is already present */
  315. bMustAddTableFont = FALSE;
  316. }
  317. pTmp->ucInUse = 1;
  318. }
  319. /* Remove the unused font entries from the font table */
  320. iUnUsed = 0;
  321. for (pTmp = pFontTable;
  322. pTmp < pFontTable + tFontTableRecords;
  323. pTmp++) {
  324. if (pTmp->ucInUse == 0) {
  325. iUnUsed++;
  326. continue;
  327. }
  328. if (iUnUsed > 0) {
  329. fail(pTmp - iUnUsed <= pFontTable);
  330. *(pTmp - iUnUsed) = *pTmp;
  331. }
  332. }
  333. fail(iUnUsed < 0);
  334. fail(tFontTableRecords <= (size_t)iUnUsed);
  335. tFontTableRecords -= (size_t)iUnUsed;
  336. if (bMustAddTableFont) {
  337. pTmp = pFontTable + tFontTableRecords;
  338. fail(pTmp <= pFontTable);
  339. pTmp->ucWordFontNumber = (pTmp - 1)->ucWordFontNumber + 1;
  340. pTmp->usFontStyle = FONT_REGULAR;
  341. pTmp->ucInUse = 1;
  342. strcpy(pTmp->szWordFontname, "Extra Table Font");
  343. strcpy(pTmp->szOurFontname, TABLE_FONT);
  344. tFontTableRecords++;
  345. iUnUsed--;
  346. }
  347. if (iUnUsed > 0) {
  348. /* Resize the font table */
  349. pFontTable = xrealloc(pFontTable,
  350. tFontTableRecords * sizeof(*pFontTable));
  351. }
  352. #if defined(DEBUG)
  353. DBG_MSG("After");
  354. DBG_DEC(tFontTableRecords);
  355. for (pTmp = pFontTable;
  356. pTmp < pFontTable + tFontTableRecords;
  357. pTmp++) {
  358. DBG_DEC(pTmp->ucWordFontNumber);
  359. DBG_HEX(pTmp->usFontStyle);
  360. DBG_MSG(pTmp->szWordFontname);
  361. DBG_MSG(pTmp->szOurFontname);
  362. }
  363. #endif /* DEBUG */
  364. } /* end of vMinimizeFontTable */
  365. /*
  366. * bReadFontFile - read and check a line from the font translation file
  367. *
  368. * returns TRUE when a correct line has been read, otherwise FALSE
  369. */
  370. static BOOL
  371. bReadFontFile(FILE *pFontTableFile, char *szWordFont,
  372. int *piItalic, int *piBold, char *szOurFont, int *piSpecial)
  373. {
  374. char *pcTmp;
  375. int iFields;
  376. char szLine[FONT_LINE_LENGTH];
  377. fail(szWordFont == NULL || szOurFont == NULL);
  378. fail(piItalic == NULL || piBold == NULL || piSpecial == NULL);
  379. while (fgets(szLine, (int)sizeof(szLine), pFontTableFile) != NULL) {
  380. if (szLine[0] == '#' ||
  381. szLine[0] == '\n' ||
  382. szLine[0] == '\r') {
  383. continue;
  384. }
  385. iFields = sscanf(szLine, "%[^,],%d,%d,%1s%[^,],%d",
  386. szWordFont, piItalic, piBold,
  387. &szOurFont[0], &szOurFont[1], piSpecial);
  388. if (iFields != 6) {
  389. pcTmp = strchr(szLine, '\r');
  390. if (pcTmp != NULL) {
  391. *pcTmp = '\0';
  392. }
  393. pcTmp = strchr(szLine, '\n');
  394. if (pcTmp != NULL) {
  395. *pcTmp = '\0';
  396. }
  397. DBG_DEC(iFields);
  398. werr(0, "Syntax error in: '%s'", szLine);
  399. continue;
  400. }
  401. if (strlen(szWordFont) >=
  402. sizeof(pFontTable[0].szWordFontname)) {
  403. werr(0, "Word fontname too long: '%s'", szWordFont);
  404. continue;
  405. }
  406. if (strlen(szOurFont) >=
  407. sizeof(pFontTable[0].szOurFontname)) {
  408. werr(0, "Local fontname too long: '%s'", szOurFont);
  409. continue;
  410. }
  411. /* The current line passed all the tests */
  412. return TRUE;
  413. }
  414. return FALSE;
  415. } /* end of bReadFontFile */
  416. /*
  417. * vCreate0FontTable - create a font table from Word for DOS
  418. */
  419. void
  420. vCreate0FontTable(void)
  421. {
  422. FILE *pFontTableFile;
  423. font_table_type *pTmp;
  424. UCHAR *aucFont;
  425. int iBold, iItalic, iSpecial, iEmphasis, iFtc;
  426. UCHAR ucPrq, ucFf, ucFFN;
  427. char szWordFont[FONT_LINE_LENGTH], szOurFont[FONT_LINE_LENGTH];
  428. tFontTableRecords = 0;
  429. pFontTable = xfree(pFontTable);
  430. pFontTableFile = pOpenFontTableFile();
  431. if (pFontTableFile == NULL) {
  432. /* No translation table file, no translation table */
  433. return;
  434. }
  435. /* Get the maximum number of entries in the font table */
  436. tFontTableRecords = 64;
  437. tFontTableRecords *= 4; /* Plain, Bold, Italic and Bold/italic */
  438. tFontTableRecords++; /* One extra for the table-font */
  439. vCreateFontTable();
  440. /* Read the font translation file */
  441. iItalic = 0;
  442. iBold = 0;
  443. iSpecial = 0;
  444. while (bReadFontFile(pFontTableFile, szWordFont,
  445. &iItalic, &iBold, szOurFont, &iSpecial)) {
  446. iEmphasis = 0;
  447. if (iBold != 0) {
  448. iEmphasis++;
  449. }
  450. if (iItalic != 0) {
  451. iEmphasis += 2;
  452. }
  453. for (iFtc = 0, pTmp = pFontTable + iEmphasis;
  454. pTmp < pFontTable + tFontTableRecords;
  455. iFtc++, pTmp += 4) {
  456. if (iFtc >= 16 && iFtc <= 55) {
  457. ucPrq = PITCH_VARIABLE;
  458. ucFf = FAMILY_ROMAN;
  459. aucFont = (UCHAR *)"Times";
  460. } else {
  461. ucPrq = PITCH_FIXED;
  462. ucFf = FAMILY_MODERN;
  463. aucFont = (UCHAR *)"Courier";
  464. }
  465. ucFFN = (ucFf << 4) | ucPrq;
  466. vFontname2Table(aucFont, NULL, 1, iEmphasis,
  467. ucFFN, szWordFont, szOurFont, pTmp);
  468. }
  469. }
  470. (void)fclose(pFontTableFile);
  471. vMinimizeFontTable();
  472. } /* end of vCreate0FontTable */
  473. /*
  474. * vCreate2FontTable - create a font table from WinWord 1/2
  475. */
  476. void
  477. vCreate2FontTable(FILE *pFile, int iWordVersion, const UCHAR *aucHeader)
  478. {
  479. FILE *pFontTableFile;
  480. font_table_type *pTmp;
  481. UCHAR *aucFont;
  482. UCHAR *aucBuffer;
  483. ULONG ulBeginFontInfo;
  484. size_t tFontInfoLen;
  485. int iPos, iOff, iRecLen;
  486. int iBold, iItalic, iSpecial, iEmphasis;
  487. UCHAR ucFFN;
  488. char szWordFont[FONT_LINE_LENGTH], szOurFont[FONT_LINE_LENGTH];
  489. fail(pFile == NULL || aucHeader == NULL);
  490. fail(iWordVersion != 1 && iWordVersion != 2);
  491. tFontTableRecords = 0;
  492. pFontTable = xfree(pFontTable);
  493. pFontTableFile = pOpenFontTableFile();
  494. if (pFontTableFile == NULL) {
  495. /* No translation table file, no translation table */
  496. return;
  497. }
  498. ulBeginFontInfo = ulGetLong(0xb2, aucHeader); /* fcSttbfffn */
  499. DBG_HEX(ulBeginFontInfo);
  500. tFontInfoLen = (size_t)usGetWord(0xb6, aucHeader); /* cbSttbfffn */
  501. DBG_DEC(tFontInfoLen);
  502. if (ulBeginFontInfo > (ULONG)LONG_MAX || tFontInfoLen == 0) {
  503. /* Don't ask me why this is needed */
  504. DBG_HEX_C(tFontInfoLen != 0, ulBeginFontInfo);
  505. (void)fclose(pFontTableFile);
  506. return;
  507. }
  508. aucBuffer = xmalloc(tFontInfoLen);
  509. if (!bReadBytes(aucBuffer, tFontInfoLen, ulBeginFontInfo, pFile)) {
  510. aucBuffer = xfree(aucBuffer);
  511. (void)fclose(pFontTableFile);
  512. return;
  513. }
  514. NO_DBG_PRINT_BLOCK(aucBuffer, tFontInfoLen);
  515. DBG_DEC(usGetWord(0, aucBuffer));
  516. /* Compute the maximum number of entries in the font table */
  517. if (iWordVersion == 1) {
  518. fail(tFontInfoLen < 2);
  519. /* WinWord 1 has three implicit fonts */
  520. tFontTableRecords = 3;
  521. iOff = 2;
  522. } else {
  523. fail(tFontInfoLen < 6);
  524. /* WinWord 2 and up have no implicit fonts */
  525. tFontTableRecords = 0;
  526. iOff = 3;
  527. }
  528. iPos = 2;
  529. while (iPos + iOff < (int)tFontInfoLen) {
  530. iRecLen = (int)ucGetByte(iPos, aucBuffer);
  531. NO_DBG_DEC(iRecLen);
  532. NO_DBG_MSG(aucBuffer + iPos + iOff);
  533. iPos += iRecLen + 1;
  534. tFontTableRecords++;
  535. }
  536. tFontTableRecords *= 4; /* Plain, Bold, Italic and Bold/Italic */
  537. tFontTableRecords++; /* One extra for the table-font */
  538. vCreateFontTable();
  539. /* Add the tree implicit fonts (in four variations) */
  540. if (iWordVersion == 1) {
  541. fail(tFontTableRecords < 13);
  542. vFontname2Table((UCHAR *)"Tms Rmn", NULL, 1, 0,
  543. (UCHAR)((FAMILY_ROMAN << 4) | PITCH_VARIABLE),
  544. "*", "Times-Roman", pFontTable + 0);
  545. vFontname2Table((UCHAR *)"Tms Rmn", NULL, 1, 1,
  546. (UCHAR)((FAMILY_ROMAN << 4) | PITCH_VARIABLE),
  547. "*", "Times-Bold", pFontTable + 1);
  548. vFontname2Table((UCHAR *)"Tms Rmn", NULL, 1, 2,
  549. (UCHAR)((FAMILY_ROMAN << 4) | PITCH_VARIABLE),
  550. "*", "Times-Italic", pFontTable + 2);
  551. vFontname2Table((UCHAR *)"Tms Rmn", NULL, 1, 3,
  552. (UCHAR)((FAMILY_ROMAN << 4) | PITCH_VARIABLE),
  553. "*", "Times-BoldItalic", pFontTable + 3);
  554. vFontname2Table((UCHAR *)"Symbol", NULL, 1, 0,
  555. (UCHAR)((FAMILY_ROMAN << 4) | PITCH_VARIABLE),
  556. "*", "Times-Roman", pFontTable + 4);
  557. vFontname2Table((UCHAR *)"Symbol", NULL, 1, 1,
  558. (UCHAR)((FAMILY_ROMAN << 4) | PITCH_VARIABLE),
  559. "*", "Times-Bold", pFontTable + 5);
  560. vFontname2Table((UCHAR *)"Symbol", NULL, 1, 2,
  561. (UCHAR)((FAMILY_ROMAN << 4) | PITCH_VARIABLE),
  562. "*", "Times-Italic", pFontTable + 6);
  563. vFontname2Table((UCHAR *)"Symbol", NULL, 1, 3,
  564. (UCHAR)((FAMILY_ROMAN << 4) | PITCH_VARIABLE),
  565. "*", "Times-BoldItalic", pFontTable + 7);
  566. vFontname2Table((UCHAR *)"Helv", NULL, 1, 0,
  567. (UCHAR)((FAMILY_SWISS << 4) | PITCH_VARIABLE),
  568. "*", "Helvetica", pFontTable + 8);
  569. vFontname2Table((UCHAR *)"Helv", NULL, 1, 1,
  570. (UCHAR)((FAMILY_SWISS << 4) | PITCH_VARIABLE),
  571. "*", "Helvetica-Bold", pFontTable + 9);
  572. vFontname2Table((UCHAR *)"Helv", NULL, 1, 2,
  573. (UCHAR)((FAMILY_SWISS << 4) | PITCH_VARIABLE),
  574. "*", "Helvetica-Oblique", pFontTable + 10);
  575. vFontname2Table((UCHAR *)"Helv", NULL, 1, 3,
  576. (UCHAR)((FAMILY_SWISS << 4) | PITCH_VARIABLE),
  577. "*", "Helvetica-BoldOblique", pFontTable + 11);
  578. }
  579. /* Read the font translation file */
  580. iItalic = 0;
  581. iBold = 0;
  582. iSpecial = 0;
  583. while (bReadFontFile(pFontTableFile, szWordFont,
  584. &iItalic, &iBold, szOurFont, &iSpecial)) {
  585. iEmphasis = 0;
  586. if (iBold != 0) {
  587. iEmphasis++;
  588. }
  589. if (iItalic != 0) {
  590. iEmphasis += 2;
  591. }
  592. pTmp = pFontTable + iEmphasis;
  593. iPos = 2;
  594. while (iPos + iOff < (int)tFontInfoLen) {
  595. iRecLen = (int)ucGetByte(iPos, aucBuffer);
  596. ucFFN = ucGetByte(iPos + 1, aucBuffer);
  597. aucFont = aucBuffer + iPos + iOff;
  598. vFontname2Table(aucFont, NULL, 1, iEmphasis,
  599. ucFFN, szWordFont, szOurFont, pTmp);
  600. pTmp += 4;
  601. iPos += iRecLen + 1;
  602. }
  603. }
  604. (void)fclose(pFontTableFile);
  605. aucBuffer = xfree(aucBuffer);
  606. vMinimizeFontTable();
  607. } /* end of vCreate2FontTable */
  608. /*
  609. * vCreate6FontTable - create a font table from Word 6/7
  610. */
  611. void
  612. vCreate6FontTable(FILE *pFile, ULONG ulStartBlock,
  613. const ULONG *aulBBD, size_t tBBDLen,
  614. const UCHAR *aucHeader)
  615. {
  616. FILE *pFontTableFile;
  617. font_table_type *pTmp;
  618. UCHAR *aucFont, *aucAltFont;
  619. UCHAR *aucBuffer;
  620. ULONG ulBeginFontInfo;
  621. size_t tFontInfoLen;
  622. int iPos, iRecLen, iOffsetAltName;
  623. int iBold, iItalic, iSpecial, iEmphasis;
  624. UCHAR ucFFN;
  625. char szWordFont[FONT_LINE_LENGTH], szOurFont[FONT_LINE_LENGTH];
  626. fail(pFile == NULL || aucHeader == NULL);
  627. fail(ulStartBlock > MAX_BLOCKNUMBER && ulStartBlock != END_OF_CHAIN);
  628. fail(aulBBD == NULL);
  629. tFontTableRecords = 0;
  630. pFontTable = xfree(pFontTable);
  631. pFontTableFile = pOpenFontTableFile();
  632. if (pFontTableFile == NULL) {
  633. /* No translation table file, no translation table */
  634. return;
  635. }
  636. ulBeginFontInfo = ulGetLong(0xd0, aucHeader); /* fcSttbfffn */
  637. DBG_HEX(ulBeginFontInfo);
  638. tFontInfoLen = (size_t)ulGetLong(0xd4, aucHeader); /* lcbSttbfffn */
  639. DBG_DEC(tFontInfoLen);
  640. fail(tFontInfoLen < 9);
  641. aucBuffer = xmalloc(tFontInfoLen);
  642. if (!bReadBuffer(pFile, ulStartBlock,
  643. aulBBD, tBBDLen, BIG_BLOCK_SIZE,
  644. aucBuffer, ulBeginFontInfo, tFontInfoLen)) {
  645. aucBuffer = xfree(aucBuffer);
  646. (void)fclose(pFontTableFile);
  647. return;
  648. }
  649. DBG_DEC(usGetWord(0, aucBuffer));
  650. /* Compute the maximum number of entries in the font table */
  651. tFontTableRecords = 0;
  652. iPos = 2;
  653. while (iPos + 6 < (int)tFontInfoLen) {
  654. iRecLen = (int)ucGetByte(iPos, aucBuffer);
  655. NO_DBG_DEC(iRecLen);
  656. iOffsetAltName = (int)ucGetByte(iPos + 5, aucBuffer);
  657. NO_DBG_MSG(aucBuffer + iPos + 6);
  658. NO_DBG_MSG_C(iOffsetAltName > 0,
  659. aucBuffer + iPos + 6 + iOffsetAltName);
  660. iPos += iRecLen + 1;
  661. tFontTableRecords++;
  662. }
  663. tFontTableRecords *= 4; /* Plain, Bold, Italic and Bold/italic */
  664. tFontTableRecords++; /* One extra for the table-font */
  665. vCreateFontTable();
  666. /* Read the font translation file */
  667. iItalic = 0;
  668. iBold = 0;
  669. iSpecial = 0;
  670. while (bReadFontFile(pFontTableFile, szWordFont,
  671. &iItalic, &iBold, szOurFont, &iSpecial)) {
  672. iEmphasis = 0;
  673. if (iBold != 0) {
  674. iEmphasis++;
  675. }
  676. if (iItalic != 0) {
  677. iEmphasis += 2;
  678. }
  679. pTmp = pFontTable + iEmphasis;
  680. iPos = 2;
  681. while (iPos + 6 < (int)tFontInfoLen) {
  682. iRecLen = (int)ucGetByte(iPos, aucBuffer);
  683. ucFFN = ucGetByte(iPos + 1, aucBuffer);
  684. aucFont = aucBuffer + iPos + 6;
  685. iOffsetAltName = (int)ucGetByte(iPos + 5, aucBuffer);
  686. if (iOffsetAltName <= 0) {
  687. aucAltFont = NULL;
  688. } else {
  689. aucAltFont = aucFont + iOffsetAltName;
  690. NO_DBG_MSG(aucFont);
  691. NO_DBG_MSG(aucAltFont);
  692. }
  693. vFontname2Table(aucFont, aucAltFont, 1, iEmphasis,
  694. ucFFN, szWordFont, szOurFont, pTmp);
  695. pTmp += 4;
  696. iPos += iRecLen + 1;
  697. }
  698. }
  699. (void)fclose(pFontTableFile);
  700. aucBuffer = xfree(aucBuffer);
  701. vMinimizeFontTable();
  702. } /* end of vCreate6FontTable */
  703. /*
  704. * vCreate8FontTable - create a font table from Word 8/9/10
  705. */
  706. void
  707. vCreate8FontTable(FILE *pFile, const pps_info_type *pPPS,
  708. const ULONG *aulBBD, size_t tBBDLen,
  709. const ULONG *aulSBD, size_t tSBDLen,
  710. const UCHAR *aucHeader)
  711. {
  712. FILE *pFontTableFile;
  713. font_table_type *pTmp;
  714. const ULONG *aulBlockDepot;
  715. UCHAR *aucFont, *aucAltFont;
  716. UCHAR *aucBuffer;
  717. ULONG ulBeginFontInfo;
  718. size_t tFontInfoLen, tBlockDepotLen, tBlockSize;
  719. int iPos, iRecLen, iOffsetAltName;
  720. int iBold, iItalic, iSpecial, iEmphasis;
  721. UCHAR ucFFN;
  722. char szWordFont[FONT_LINE_LENGTH], szOurFont[FONT_LINE_LENGTH];
  723. fail(pFile == NULL || pPPS == NULL || aucHeader == NULL);
  724. fail(aulBBD == NULL || aulSBD == NULL);
  725. tFontTableRecords = 0;
  726. pFontTable = xfree(pFontTable);
  727. pFontTableFile = pOpenFontTableFile();
  728. if (pFontTableFile == NULL) {
  729. /* No translation table file, no translation table */
  730. return;
  731. }
  732. ulBeginFontInfo = ulGetLong(0x112, aucHeader); /* fcSttbfffn */
  733. DBG_HEX(ulBeginFontInfo);
  734. tFontInfoLen = (size_t)ulGetLong(0x116, aucHeader); /* lcbSttbfffn */
  735. DBG_DEC(tFontInfoLen);
  736. fail(tFontInfoLen < 46);
  737. DBG_DEC(pPPS->tTable.ulSB);
  738. DBG_HEX(pPPS->tTable.ulSize);
  739. if (pPPS->tTable.ulSize == 0) {
  740. DBG_MSG("No fontname table");
  741. (void)fclose(pFontTableFile);
  742. return;
  743. }
  744. if (pPPS->tTable.ulSize < MIN_SIZE_FOR_BBD_USE) {
  745. /* Use the Small Block Depot */
  746. aulBlockDepot = aulSBD;
  747. tBlockDepotLen = tSBDLen;
  748. tBlockSize = SMALL_BLOCK_SIZE;
  749. } else {
  750. /* Use the Big Block Depot */
  751. aulBlockDepot = aulBBD;
  752. tBlockDepotLen = tBBDLen;
  753. tBlockSize = BIG_BLOCK_SIZE;
  754. }
  755. aucBuffer = xmalloc(tFontInfoLen);
  756. if (!bReadBuffer(pFile, pPPS->tTable.ulSB,
  757. aulBlockDepot, tBlockDepotLen, tBlockSize,
  758. aucBuffer, ulBeginFontInfo, tFontInfoLen)) {
  759. aucBuffer = xfree(aucBuffer);
  760. (void)fclose(pFontTableFile);
  761. return;
  762. }
  763. NO_DBG_PRINT_BLOCK(aucBuffer, tFontInfoLen);
  764. /* Get the maximum number of entries in the font table */
  765. tFontTableRecords = (size_t)usGetWord(0, aucBuffer);
  766. tFontTableRecords *= 4; /* Plain, Bold, Italic and Bold/italic */
  767. tFontTableRecords++; /* One extra for the table-font */
  768. vCreateFontTable();
  769. /* Read the font translation file */
  770. iItalic = 0;
  771. iBold = 0;
  772. iSpecial = 0;
  773. while (bReadFontFile(pFontTableFile, szWordFont,
  774. &iItalic, &iBold, szOurFont, &iSpecial)) {
  775. iEmphasis = 0;
  776. if (iBold != 0) {
  777. iEmphasis++;
  778. }
  779. if (iItalic != 0) {
  780. iEmphasis += 2;
  781. }
  782. pTmp = pFontTable + iEmphasis;
  783. iPos = 4;
  784. while (iPos + 40 < (int)tFontInfoLen) {
  785. iRecLen = (int)ucGetByte(iPos, aucBuffer);
  786. ucFFN = ucGetByte(iPos + 1, aucBuffer);
  787. aucFont = aucBuffer + iPos + 40;
  788. iOffsetAltName = (int)unilen(aucFont);
  789. if (iPos + 40 + iOffsetAltName + 4 >= iRecLen) {
  790. aucAltFont = NULL;
  791. } else {
  792. aucAltFont = aucFont + iOffsetAltName + 2;
  793. NO_DBG_UNICODE(aucFont);
  794. NO_DBG_UNICODE(aucAltFont);
  795. }
  796. vFontname2Table(aucFont, aucAltFont, 2, iEmphasis,
  797. ucFFN, szWordFont, szOurFont, pTmp);
  798. pTmp += 4;
  799. iPos += iRecLen + 1;
  800. }
  801. }
  802. (void)fclose(pFontTableFile);
  803. aucBuffer = xfree(aucBuffer);
  804. vMinimizeFontTable();
  805. } /* end of vCreate8FontTable */
  806. /*
  807. * Destroy the internal font table by freeing its memory
  808. */
  809. void
  810. vDestroyFontTable(void)
  811. {
  812. DBG_MSG("vDestroyFontTable");
  813. tFontTableRecords = 0;
  814. pFontTable = xfree(pFontTable);
  815. } /* end of vDestroyFontTable */
  816. /*
  817. * pGetNextFontTableRecord
  818. *
  819. * returns the next record in the table or NULL if there is no next record
  820. */
  821. const font_table_type *
  822. pGetNextFontTableRecord(const font_table_type *pRecordCurr)
  823. {
  824. size_t tIndexCurr;
  825. if (pRecordCurr == NULL) {
  826. /* No current record, so start with the first one */
  827. return &pFontTable[0];
  828. }
  829. if (pRecordCurr < pFontTable ||
  830. pRecordCurr >= pFontTable + tFontTableRecords) {
  831. /* Not a pointer in the array */
  832. DBG_HEX(pRecordCurr);
  833. DBG_HEX(pFontTable);
  834. return NULL;
  835. }
  836. tIndexCurr = (size_t)(pRecordCurr - pFontTable);
  837. if (tIndexCurr + 1 < tFontTableRecords) {
  838. /* There is a next record, so return it */
  839. return &pFontTable[tIndexCurr + 1];
  840. }
  841. /* There is no next record */
  842. return NULL;
  843. } /* end of pGetNextFontTableRecord */
  844. /*
  845. * tGetFontTableLength
  846. *
  847. * returns the number of records in the internal font table
  848. */
  849. size_t
  850. tGetFontTableLength(void)
  851. {
  852. return tFontTableRecords;
  853. } /* end of tGetFontTableLength */
  854. #if !defined(__riscos)
  855. /*
  856. * vCorrect4PDF - only include PDF default fonts
  857. */
  858. static void
  859. vCorrect4PDF(void)
  860. {
  861. font_table_type *pTmp;
  862. const char *szOurFont;
  863. for (pTmp = pFontTable; pTmp < pFontTable + tFontTableRecords; pTmp++) {
  864. if (STRCEQ(pTmp->szOurFontname, FONT_MONOSPACED_PLAIN) ||
  865. STRCEQ(pTmp->szOurFontname, FONT_MONOSPACED_BOLD) ||
  866. STRCEQ(pTmp->szOurFontname, FONT_MONOSPACED_ITALIC) ||
  867. STRCEQ(pTmp->szOurFontname, FONT_MONOSPACED_BOLDITALIC) ||
  868. STRCEQ(pTmp->szOurFontname, FONT_SERIF_PLAIN) ||
  869. STRCEQ(pTmp->szOurFontname, FONT_SERIF_BOLD) ||
  870. STRCEQ(pTmp->szOurFontname, FONT_SERIF_ITALIC) ||
  871. STRCEQ(pTmp->szOurFontname, FONT_SERIF_BOLDITALIC) ||
  872. STRCEQ(pTmp->szOurFontname, FONT_SANS_SERIF_PLAIN) ||
  873. STRCEQ(pTmp->szOurFontname, FONT_SANS_SERIF_BOLD) ||
  874. STRCEQ(pTmp->szOurFontname, FONT_SANS_SERIF_ITALIC) ||
  875. STRCEQ(pTmp->szOurFontname, FONT_SANS_SERIF_BOLDITALIC)) {
  876. /* Already a default font */
  877. continue;
  878. }
  879. szOurFont =
  880. szGetDefaultFont(pTmp->ucFFN, (int)pTmp->ucEmphasis);
  881. (void)strncpy(pTmp->szOurFontname, szOurFont,
  882. sizeof(pTmp->szOurFontname) - 1);
  883. pTmp->szOurFontname[sizeof(pTmp->szOurFontname) - 1] = '\0';
  884. }
  885. } /* end of vCorrect4PDF */
  886. /*
  887. * vCorrect4CyrPS - only include monospaced fonts
  888. */
  889. static void
  890. vCorrect4CyrPS(void)
  891. {
  892. font_table_type *pTmp;
  893. const char *szOurFont;
  894. UCHAR ucFFN;
  895. ucFFN = (FAMILY_UNKNOWN << 4) | PITCH_FIXED;
  896. for (pTmp = pFontTable; pTmp < pFontTable + tFontTableRecords; pTmp++) {
  897. szOurFont = szGetDefaultFont(ucFFN, (int)pTmp->ucEmphasis);
  898. (void)strncpy(pTmp->szOurFontname, szOurFont,
  899. sizeof(pTmp->szOurFontname) - 1);
  900. pTmp->szOurFontname[sizeof(pTmp->szOurFontname) - 1] = '\0';
  901. }
  902. } /* end of vCorrect4CyrPS */
  903. #endif /* __riscos */
  904. /*
  905. * vCorrectFontTable - correct the font table in special cases
  906. */
  907. void
  908. vCorrectFontTable(conversion_type eConversionType, encoding_type eEncoding)
  909. {
  910. #if !defined(__riscos)
  911. if (eConversionType == conversion_pdf) {
  912. vCorrect4PDF();
  913. }
  914. if (eConversionType == conversion_ps &&
  915. eEncoding == encoding_cyrillic) {
  916. vCorrect4CyrPS();
  917. }
  918. #endif /* __riscos */
  919. } /* end of vCorrectFontTable */
  920. /*
  921. * lComputeSpaceWidth - compute the width of a space character
  922. *
  923. * Returns the space width in millipoints
  924. */
  925. long
  926. lComputeSpaceWidth(drawfile_fontref tFontRef, USHORT usFontSize)
  927. {
  928. char szSpace[] = " ";
  929. fail(usFontSize < MIN_FONT_SIZE || usFontSize > MAX_FONT_SIZE);
  930. return lComputeStringWidth(szSpace, 1, tFontRef, usFontSize);
  931. } /* end of lComputeSpaceWidth */