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.

288 lines
9.8 KiB

  1. /***
  2. **
  3. ** Module: PFM
  4. **
  5. ** Description:
  6. ** This is a module of the T1 to TT font converter. The module
  7. ** will extract information from a T1 font metrics file, by parsing
  8. ** the data/commands found in a PFM file.
  9. **
  10. ** Please note that all data stored in a PFM file is represented
  11. ** in the little-endian order.
  12. **
  13. ** Author: Michael Jansson
  14. **
  15. ** Created: 5/26/93
  16. **
  17. ***/
  18. /**** INCLUDES */
  19. /* General types and definitions. */
  20. #include <string.h>
  21. /* Special types and definitions. */
  22. #include "titott.h"
  23. #include "types.h"
  24. #include "safemem.h"
  25. #include "metrics.h"
  26. #include "t1msg.h"
  27. /* Module dependent types and prototypes. */
  28. #include "fileio.h"
  29. /***** CONSTANTS */
  30. /*-none-*/
  31. /***** LOCAL TYPES */
  32. /*-none-*/
  33. /***** MACROS */
  34. /*-none-*/
  35. /***** STATIC FUNCTIONS */
  36. /***
  37. ** Function: GetNextWord
  38. **
  39. ** Description:
  40. ** This function pulls two bytes from a file
  41. ** and convert them into a 16-bit integer.
  42. ***/
  43. static short GetNextWord(struct ioFile *file)
  44. {
  45. short iWord;
  46. iWord = (short)io_ReadOneByte(file);
  47. iWord |= (short)(io_ReadOneByte(file) * 256);
  48. return(iWord);
  49. }
  50. /***
  51. ** Function: GetLong
  52. **
  53. ** Description:
  54. ** This function pulls four bytes from a file
  55. ** and convert them into a 32-bit integer.
  56. ***/
  57. static long GetLong(struct ioFile *file)
  58. {
  59. short low;
  60. short high;
  61. low = GetNextWord(file);
  62. high = GetNextWord(file);
  63. return (long)((long)low+((long)high * 65535L));
  64. }
  65. /***
  66. ** Function: ReadString
  67. **
  68. ** Description:
  69. ** This function pulls a null terminated
  70. ** string from the file.
  71. ***/
  72. static void ReadString(UBYTE *dst, int size, struct ioFile *file)
  73. {
  74. int i;
  75. i=0;
  76. while (io_FileError(file)==SUCCESS && i<size) {
  77. dst[i] = (UBYTE)io_ReadOneByte(file);
  78. if (dst[i]=='\0')
  79. break;
  80. i++;
  81. }
  82. if (i==size)
  83. dst[i-1] = '\0';
  84. }
  85. /***** FUNCTIONS */
  86. /***
  87. ** Function: ReadPFMMetrics
  88. **
  89. ** Description:
  90. ** This function parses a Printer Font Metrics
  91. ** (*.pfm) file.
  92. ***/
  93. errcode ReadPFMMetrics(const char *metrics, struct T1Metrics *t1m)
  94. {
  95. errcode status = SUCCESS;
  96. struct ioFile *file;
  97. UBYTE buf[256];
  98. long kernoffset;
  99. long widthoffset;
  100. long etmoffset;
  101. long faceoffset;
  102. short ver;
  103. short i;
  104. if (metrics==NULL || (file = io_OpenFile(metrics, READONLY))==NULL) {
  105. status = NOMETRICS;
  106. } else {
  107. (void)io_ReadOneByte(file); /* Skip the revision number. */
  108. ver = (short)io_ReadOneByte(file);
  109. if (ver>3) {
  110. SetError(status=UNSUPPORTEDFORMAT);
  111. } else {
  112. (void)GetLong(file); /* dfSize */
  113. /* Get Copyright */
  114. if (t1m->copyright)
  115. Free(t1m->copyright);
  116. if ((t1m->copyright = Malloc(60))==NULL) {
  117. SetError(status=NOMEM);
  118. } else {
  119. (void)io_ReadBytes((UBYTE *)t1m->copyright, (USHORT)60, file);
  120. (void)GetNextWord(file); /* dfType */
  121. (void)GetNextWord(file); /* dfPoints */
  122. (void)GetNextWord(file); /* dfVertRes */
  123. (void)GetNextWord(file); /* dfHorizRes */
  124. t1m->ascent = GetNextWord(file); /* dfAscent */
  125. t1m->intLeading = GetNextWord(file); /* dfInternalLeading */
  126. t1m->extLeading = GetNextWord(file); /* dfExternalLeading */
  127. (void)io_ReadOneByte(file); /* dfItalic */
  128. (void)io_ReadOneByte(file); /* dfUnderline */
  129. (void)io_ReadOneByte(file); /* dfStrikeOut */
  130. t1m->tmweight = (USHORT)GetNextWord(file); /* dfWeight */
  131. t1m->CharSet = (UBYTE)io_ReadOneByte(file); /* dfCharSet */
  132. (void)GetNextWord(file); /* dfPixWidth */
  133. (void)GetNextWord(file); /* dfPixHeight */
  134. t1m->pitchfam = (UBYTE)io_ReadOneByte(file);/* dfPitchAndFamily */
  135. t1m->avgCharWidth = GetNextWord(file); /* dfAvgWidth */
  136. (void)GetNextWord(file); /* dfMaxWidth */
  137. t1m->firstChar = (UBYTE)io_ReadOneByte(file); /* dfFirstChar */
  138. t1m->lastChar = (UBYTE)io_ReadOneByte(file); /* dfLastChar */
  139. t1m->DefaultChar = (UBYTE)io_ReadOneByte(file); /* dfDefaultChar */
  140. t1m->BreakChar = (UBYTE)io_ReadOneByte(file); /* dfBreakChar */
  141. (void)GetNextWord(file); /* dfWidthBytes */
  142. (void)GetLong(file); /* dfDevice */
  143. faceoffset = GetLong(file); /* dfFace */
  144. (void)GetLong(file); /* dfBitsPointer */
  145. (void)GetLong(file); /* dfBitsOffset */
  146. (void)GetNextWord(file); /* dfSizeFields */
  147. etmoffset = GetLong(file); /* dfExtMetricsOffset */
  148. widthoffset = GetLong(file); /* dfExtentTable */
  149. (void)GetLong(file); /* dfOriginTable */
  150. kernoffset = GetLong(file); /* dfPairKernTable */
  151. (void)GetLong(file); /* dfTrackKernTable */
  152. (void)GetLong(file); /* dfDriverInfo */
  153. (void)GetLong(file); /* dfReserved */
  154. if (io_FileError(file)!=SUCCESS) {
  155. SetError(status = BADMETRICS);
  156. }
  157. /* Get extended type metrics */
  158. (void)io_FileSeek(file, etmoffset);
  159. (void)GetNextWord(file); /* etmSize */
  160. (void)GetNextWord(file); /* etmPointSize */
  161. (void)GetNextWord(file); /* etmOrientation */
  162. (void)GetNextWord(file); /* etmMasterHeight */
  163. (void)GetNextWord(file); /* etmMinScale */
  164. (void)GetNextWord(file); /* etmMaxScale */
  165. (void)GetNextWord(file); /* etmMasterUnits */
  166. (void)GetNextWord(file); /* etmCapHeight */
  167. (void)GetNextWord(file); /* etmXHeight */
  168. (void)GetNextWord(file); /* etmLowerCaseAscent */
  169. t1m->descent = GetNextWord(file); /* etmLowerCaseDecent */
  170. (void)GetNextWord(file); /* etmSlant */
  171. t1m->superoff = GetNextWord(file); /* etmSuperScript */
  172. t1m->suboff = GetNextWord(file); /* etmSubScript */
  173. t1m->supersize = GetNextWord(file); /* etmSuperScriptSize */
  174. t1m->subsize = GetNextWord(file); /* etmSubScriptSize */
  175. (void)GetNextWord(file); /* etmUnderlineOffset */
  176. (void)GetNextWord(file); /* etmUnderlineWidth */
  177. (void)GetNextWord(file); /* etmDoubleUpperUnderlineOffset*/
  178. (void)GetNextWord(file); /* etmDoubleLowerUnderlineOffset*/
  179. (void)GetNextWord(file); /* etmDoubleUpperUnderlineWidth */
  180. (void)GetNextWord(file); /* etmDoubleLowerUnderlineWidth */
  181. t1m->strikeoff = GetNextWord(file); /* etmStrikeOutOffset */
  182. t1m->strikesize = GetNextWord(file); /* etmStrikeOutWidth */
  183. (void)GetNextWord(file); /* etmNKernPairs */
  184. (void)GetNextWord(file); /* etmNKernTracks */
  185. /* Get the advance width for the characters. */
  186. if ((t1m->widths = Malloc(sizeof(funit)*
  187. (t1m->lastChar -
  188. t1m->firstChar + 1)))==NULL) {
  189. SetError(status=NOMEM);
  190. } else {
  191. (void)io_FileSeek(file, widthoffset);
  192. for (i=0; i<=t1m->lastChar-t1m->firstChar; i++)
  193. t1m->widths[i] = GetNextWord(file);
  194. if (io_FileError(file)!=SUCCESS) {
  195. SetError(status = BADMETRICS);
  196. }
  197. }
  198. /* Get the face name. */
  199. if ((status==SUCCESS) && faceoffset) {
  200. (void)io_FileSeek(file, faceoffset);
  201. if (t1m->family)
  202. Free(t1m->family);
  203. ReadString(buf, sizeof(buf), file);
  204. if (io_FileError(file)) {
  205. SetError(status = BADMETRICS);
  206. } else {
  207. if ((t1m->family = Strdup((char*)buf))==NULL) {
  208. SetError(status=NOMEM);
  209. }
  210. }
  211. }
  212. /* Get the pair-kerning the typeface. */
  213. if ((status==SUCCESS) && kernoffset) {
  214. (void)io_FileSeek(file, kernoffset);
  215. t1m->kernsize = (USHORT)GetNextWord(file);
  216. if (io_FileError(file)!=SUCCESS) {
  217. SetError(status = BADMETRICS);
  218. } else {
  219. if ((t1m->kerns = Malloc(sizeof(struct kerning)*
  220. t1m->kernsize))==NULL) {
  221. SetError(status=NOMEM);
  222. } else {
  223. for (i=0; i<(int)t1m->kernsize; i++) {
  224. t1m->kerns[i].left = (UBYTE)io_ReadOneByte(file);
  225. t1m->kerns[i].right = (UBYTE)io_ReadOneByte(file);
  226. t1m->kerns[i].delta = GetNextWord(file);
  227. }
  228. if (io_FileError(file)!=SUCCESS) {
  229. SetError(status = BADMETRICS);
  230. }
  231. }
  232. }
  233. }
  234. }
  235. }
  236. if (io_CloseFile(file)!=SUCCESS)
  237. status = BADMETRICS;
  238. }
  239. return status;
  240. }