Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

353 lines
6.7 KiB

  1. /***
  2. **
  3. ** Module: T1Parser
  4. **
  5. ** Description:
  6. ** This is a module of the T1 to TT font converter. The module
  7. ** contains functions that is used by the Builder moduler, to
  8. ** manage the lowlevel writing to the TT font file, as well as
  9. ** generic check sum, table length and table offset computations.
  10. **
  11. ** Author: Michael Jansson
  12. **
  13. ** Created: 5/26/93
  14. **
  15. ***/
  16. /**** INCLUDES */
  17. /* General types and definitions. */
  18. /*-none-*/
  19. /* Special types and definitions. */
  20. #include "types.h"
  21. /* Module dependent types and prototypes. */
  22. #include "fileio.h"
  23. #include "fwriter.h"
  24. /***** LOCAL TYPES */
  25. /*-none-*/
  26. /***** CONSTANTS */
  27. static const char *dir[] = {
  28. "OS/2",
  29. "cmap",
  30. "cvt ",
  31. "fpgm",
  32. "gasp",
  33. "glyf",
  34. "head",
  35. "hhea",
  36. "hmtx",
  37. "kern",
  38. "loca",
  39. "maxp",
  40. "name",
  41. "post",
  42. "prep",
  43. };
  44. #define MAGIC_CHECKSUM 0xB1B0AFBA
  45. /***** MACROS */
  46. /*-none-*/
  47. /***** STATIC FUNCTIONS */
  48. /***
  49. ** Function: SumCheckSum
  50. **
  51. ** Description:
  52. ** This function computes the check sum of
  53. ** a section of the output file.
  54. ***/
  55. static ULONG SumCheckSum(OutputFile *file, long length)
  56. {
  57. ULONG sum = 0;
  58. UBYTE tbl[32];
  59. /* Unwrap the loop a bit. */
  60. while (length>16) {
  61. (void)io_ReadBytes(tbl, (USHORT)16, file);
  62. sum += MkLong(tbl[0], tbl[1], tbl[2], tbl[3]);
  63. sum += MkLong(tbl[4], tbl[5], tbl[6], tbl[7]);
  64. sum += MkLong(tbl[8], tbl[9], tbl[10], tbl[11]);
  65. sum += MkLong(tbl[12], tbl[13], tbl[14], tbl[15]);
  66. length -= 16;
  67. }
  68. /* Do the sentinel DWORDS. */
  69. while (length>0) {
  70. (void)io_ReadBytes(tbl, (USHORT)4, file);
  71. sum += MkLong(tbl[0], tbl[1], tbl[2], tbl[3]);
  72. length -= 4;
  73. }
  74. return sum;
  75. }
  76. /***** FUNCTIONS */
  77. /***
  78. ** Function: WriteLong
  79. **
  80. ** Description:
  81. ** This function writes a 32-bit integer in the
  82. ** Big Endian byte order, regardless of the
  83. ** used byte order.
  84. ***/
  85. void WriteLong(const ULONG val, OutputFile *file)
  86. {
  87. UBYTE bytes[4];
  88. bytes[0] = (UBYTE)((val>>24)&0xff);
  89. bytes[1] = (UBYTE)((val>>16)&0xff);
  90. bytes[2] = (UBYTE)((val>>8)&0xff);
  91. bytes[3] = (UBYTE)((val)&0xff);
  92. (void)WriteBytes(bytes, (USHORT)4, file);
  93. }
  94. /***
  95. ** Function: WriteShort
  96. **
  97. ** Description:
  98. ** This function writes a 16-bit integer in the
  99. ** Big Endian byte order, regardless of the used
  100. ** byte order.
  101. ***/
  102. void WriteShort(const USHORT val, OutputFile *file)
  103. {
  104. UBYTE bytes[2];
  105. bytes[0] = (UBYTE)((val>>8)&0xff);
  106. bytes[1] = (UBYTE)((val)&0xff);
  107. (void)WriteBytes(bytes, (USHORT)2, file);
  108. }
  109. /***
  110. ** Function: WriteByte
  111. **
  112. ** Description:
  113. ** This function writes an 8-bit integer in the
  114. ** Big Endian byte order, regardless of used
  115. ** byte order.
  116. ***/
  117. void WriteByte(const UBYTE byte, OutputFile *file)
  118. {
  119. (void)WriteBytes(&byte, (USHORT)1, file);
  120. }
  121. /***
  122. ** Function: CompleteTable
  123. **
  124. ** Description:
  125. ** This function completes a TT font file table,
  126. ** by computing the check sum and writing it, the
  127. ** table length and table offset to the table directory
  128. ** of the TT font file.
  129. **
  130. ** Please note the dependency that this function must
  131. ** be called right after the last byte of the contents
  132. ** of the table have been written.
  133. ***/
  134. errcode CompleteTable(const long offset,
  135. const USHORT num,
  136. OutputFile *file)
  137. {
  138. long end;
  139. long length;
  140. ULONG sum = 0;
  141. long curr;
  142. short i;
  143. /* Determine the end of the table. */
  144. end = FileTell(file);
  145. /* Write pad bytes. */
  146. length = end - offset;
  147. if (length%4)
  148. for (i=0; i<(4-(long)(length%4)); i++)
  149. WriteByte(0, file);
  150. /* Record end of file position. */
  151. curr = io_FileTell(file);
  152. /* Compute the check sum */
  153. (void)io_FileSeek(file, offset);
  154. sum = SumCheckSum(file, end - offset);
  155. /* Write table directory entry */
  156. (void)io_FileSeek(file, (ULONG)(12L + TBLDIRSIZE*num + 4L));
  157. WriteLong(sum, file);
  158. WriteLong((ULONG)offset, file);
  159. WriteLong((ULONG)length, file);
  160. /* Go to end of file. */
  161. (void)io_FileSeek(file, curr);
  162. return FileError(file);
  163. }
  164. /***
  165. ** Function: WriteChecksum
  166. **
  167. ** Description:
  168. ** This function completes the whole TT font file,
  169. ** by computing the check sum of the whole file and writing
  170. ** it at the designated place.
  171. ***/
  172. void WriteChecksum(const long offset, OutputFile *file)
  173. {
  174. long end;
  175. ULONG sum = 0;
  176. end = io_FileTell(file);
  177. (void)io_FileSeek(file, 0L);
  178. sum = SumCheckSum(file, end);
  179. sum = MAGIC_CHECKSUM - sum;
  180. (void)io_FileSeek(file, offset);
  181. WriteLong(sum, file);
  182. }
  183. /***
  184. ** Function: WriteTableHeader
  185. **
  186. ** Description:
  187. ** This function initiates a TT font file, by initiating
  188. ** a handle used when writing the tables and by writing
  189. ** the leading table dictionary of the file.
  190. ***/
  191. void WriteTableHeader(OutputFile *file)
  192. {
  193. USHORT segcount;
  194. USHORT i;
  195. /* Count the segcount */ /*lint -e650 */
  196. for (segcount=0; (1UL<<(segcount+1)) <= NUMTBL; segcount++)
  197. continue; /*lint +e650*/
  198. /* Write the offset table. */
  199. WriteLong(0x00010000L, file);
  200. WriteShort((USHORT)NUMTBL, file);
  201. WriteShort((USHORT)((1<<segcount)*16), file);
  202. WriteShort(segcount, file);
  203. WriteShort((USHORT)(NUMTBL*16-(1<<segcount)*16), file);
  204. /* Write the table directory entries. */
  205. for (i=0; i<NUMTBL; i++) {
  206. (void)WriteBytes((UBYTE*)&(dir[i][0]), (USHORT)4, file);
  207. WriteLong(0L, file);
  208. WriteLong(0L, file);
  209. WriteLong(0L, file);
  210. }
  211. }
  212. /***
  213. ** Function: OpenOutputFile
  214. **
  215. ** Description:
  216. ***/
  217. OutputFile *OpenOutputFile(const char *name)
  218. {
  219. return io_OpenFile(name, READWRITE);
  220. }
  221. /***
  222. ** Function: CloseOutputFile
  223. **
  224. ** Description:
  225. ***/
  226. errcode CloseOutputFile(OutputFile *fp)
  227. {
  228. return io_CloseFile(fp);
  229. }
  230. /***
  231. ** Function: WriteBytes
  232. **
  233. ** Description:
  234. ***/
  235. USHORT WriteBytes(const UBYTE *buf,
  236. const USHORT len,
  237. OutputFile *fp)
  238. {
  239. return io_WriteBytes(buf, len, fp);
  240. }
  241. /***
  242. ** Function: FileError
  243. **
  244. ** Description:
  245. ***/
  246. boolean FileError(OutputFile *fp)
  247. {
  248. return io_FileError(fp);
  249. }
  250. /***
  251. ** Function: FileTell
  252. **
  253. ** Description:
  254. ***/
  255. long FileTell(OutputFile *fp)
  256. {
  257. return io_FileTell(fp);
  258. }
  259. /***
  260. ** Function: FileSeek
  261. **
  262. ** Description:
  263. ***/
  264. long FileSeek(OutputFile *fp,
  265. const long where)
  266. {
  267. return io_FileSeek(fp, where);
  268. }
  269. /***
  270. ** Function: RemoveFile
  271. **
  272. ** Description:
  273. ** Removes an already closed output file.
  274. ***/
  275. void RemoveFile(const char *name)
  276. {
  277. io_RemoveFile(name);
  278. }