Source code of Windows XP (NT5)
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.

286 lines
9.5 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. }
  83. /***** FUNCTIONS */
  84. /***
  85. ** Function: ReadPFMMetrics
  86. **
  87. ** Description:
  88. ** This function parses a Printer Font Metrics
  89. ** (*.pfm) file.
  90. ***/
  91. errcode ReadPFMMetrics(const char *metrics, struct T1Metrics *t1m)
  92. {
  93. errcode status = SUCCESS;
  94. struct ioFile *file;
  95. UBYTE buf[256];
  96. long kernoffset;
  97. long widthoffset;
  98. long etmoffset;
  99. long faceoffset;
  100. short ver;
  101. short i;
  102. if (metrics==NULL || (file = io_OpenFile(metrics, READONLY))==NULL) {
  103. status = NOMETRICS;
  104. } else {
  105. (void)io_ReadOneByte(file); /* Skip the revision number. */
  106. ver = (short)io_ReadOneByte(file);
  107. if (ver>3) {
  108. SetError(status=UNSUPPORTEDFORMAT);
  109. } else {
  110. (void)GetLong(file); /* dfSize */
  111. /* Get Copyright */
  112. if (t1m->copyright)
  113. Free(t1m->copyright);
  114. if ((t1m->copyright = Malloc(60))==NULL) {
  115. SetError(status=NOMEM);
  116. } else {
  117. (void)io_ReadBytes((UBYTE *)t1m->copyright, (USHORT)60, file);
  118. (void)GetNextWord(file); /* dfType */
  119. (void)GetNextWord(file); /* dfPoints */
  120. (void)GetNextWord(file); /* dfVertRes */
  121. (void)GetNextWord(file); /* dfHorizRes */
  122. t1m->ascent = GetNextWord(file); /* dfAscent */
  123. t1m->intLeading = GetNextWord(file); /* dfInternalLeading */
  124. t1m->extLeading = GetNextWord(file); /* dfExternalLeading */
  125. (void)io_ReadOneByte(file); /* dfItalic */
  126. (void)io_ReadOneByte(file); /* dfUnderline */
  127. (void)io_ReadOneByte(file); /* dfStrikeOut */
  128. t1m->tmweight = (USHORT)GetNextWord(file); /* dfWeight */
  129. t1m->CharSet = (UBYTE)io_ReadOneByte(file); /* dfCharSet */
  130. (void)GetNextWord(file); /* dfPixWidth */
  131. (void)GetNextWord(file); /* dfPixHeight */
  132. t1m->pitchfam = (UBYTE)io_ReadOneByte(file);/* dfPitchAndFamily */
  133. t1m->avgCharWidth = GetNextWord(file); /* dfAvgWidth */
  134. (void)GetNextWord(file); /* dfMaxWidth */
  135. t1m->firstChar = (UBYTE)io_ReadOneByte(file); /* dfFirstChar */
  136. t1m->lastChar = (UBYTE)io_ReadOneByte(file); /* dfLastChar */
  137. t1m->DefaultChar = (UBYTE)io_ReadOneByte(file); /* dfDefaultChar */
  138. t1m->BreakChar = (UBYTE)io_ReadOneByte(file); /* dfBreakChar */
  139. (void)GetNextWord(file); /* dfWidthBytes */
  140. (void)GetLong(file); /* dfDevice */
  141. faceoffset = GetLong(file); /* dfFace */
  142. (void)GetLong(file); /* dfBitsPointer */
  143. (void)GetLong(file); /* dfBitsOffset */
  144. (void)GetNextWord(file); /* dfSizeFields */
  145. etmoffset = GetLong(file); /* dfExtMetricsOffset */
  146. widthoffset = GetLong(file); /* dfExtentTable */
  147. (void)GetLong(file); /* dfOriginTable */
  148. kernoffset = GetLong(file); /* dfPairKernTable */
  149. (void)GetLong(file); /* dfTrackKernTable */
  150. (void)GetLong(file); /* dfDriverInfo */
  151. (void)GetLong(file); /* dfReserved */
  152. if (io_FileError(file)!=SUCCESS) {
  153. SetError(status = BADMETRICS);
  154. }
  155. /* Get extended type metrics */
  156. (void)io_FileSeek(file, etmoffset);
  157. (void)GetNextWord(file); /* etmSize */
  158. (void)GetNextWord(file); /* etmPointSize */
  159. (void)GetNextWord(file); /* etmOrientation */
  160. (void)GetNextWord(file); /* etmMasterHeight */
  161. (void)GetNextWord(file); /* etmMinScale */
  162. (void)GetNextWord(file); /* etmMaxScale */
  163. (void)GetNextWord(file); /* etmMasterUnits */
  164. (void)GetNextWord(file); /* etmCapHeight */
  165. (void)GetNextWord(file); /* etmXHeight */
  166. (void)GetNextWord(file); /* etmLowerCaseAscent */
  167. t1m->descent = GetNextWord(file); /* etmLowerCaseDecent */
  168. (void)GetNextWord(file); /* etmSlant */
  169. t1m->superoff = GetNextWord(file); /* etmSuperScript */
  170. t1m->suboff = GetNextWord(file); /* etmSubScript */
  171. t1m->supersize = GetNextWord(file); /* etmSuperScriptSize */
  172. t1m->subsize = GetNextWord(file); /* etmSubScriptSize */
  173. (void)GetNextWord(file); /* etmUnderlineOffset */
  174. (void)GetNextWord(file); /* etmUnderlineWidth */
  175. (void)GetNextWord(file); /* etmDoubleUpperUnderlineOffset*/
  176. (void)GetNextWord(file); /* etmDoubleLowerUnderlineOffset*/
  177. (void)GetNextWord(file); /* etmDoubleUpperUnderlineWidth */
  178. (void)GetNextWord(file); /* etmDoubleLowerUnderlineWidth */
  179. t1m->strikeoff = GetNextWord(file); /* etmStrikeOutOffset */
  180. t1m->strikesize = GetNextWord(file); /* etmStrikeOutWidth */
  181. (void)GetNextWord(file); /* etmNKernPairs */
  182. (void)GetNextWord(file); /* etmNKernTracks */
  183. /* Get the advance width for the characters. */
  184. if ((t1m->widths = Malloc(sizeof(funit)*
  185. (t1m->lastChar -
  186. t1m->firstChar + 1)))==NULL) {
  187. SetError(status=NOMEM);
  188. } else {
  189. (void)io_FileSeek(file, widthoffset);
  190. for (i=0; i<=t1m->lastChar-t1m->firstChar; i++)
  191. t1m->widths[i] = GetNextWord(file);
  192. if (io_FileError(file)!=SUCCESS) {
  193. SetError(status = BADMETRICS);
  194. }
  195. }
  196. /* Get the face name. */
  197. if ((status==SUCCESS) && faceoffset) {
  198. (void)io_FileSeek(file, faceoffset);
  199. if (t1m->family)
  200. Free(t1m->family);
  201. ReadString(buf, sizeof(buf), file);
  202. if (io_FileError(file)) {
  203. SetError(status = BADMETRICS);
  204. } else {
  205. if ((t1m->family = Strdup((char*)buf))==NULL) {
  206. SetError(status=NOMEM);
  207. }
  208. }
  209. }
  210. /* Get the pair-kerning the typeface. */
  211. if ((status==SUCCESS) && kernoffset) {
  212. (void)io_FileSeek(file, kernoffset);
  213. t1m->kernsize = (USHORT)GetNextWord(file);
  214. if (io_FileError(file)!=SUCCESS) {
  215. SetError(status = BADMETRICS);
  216. } else {
  217. if ((t1m->kerns = Malloc(sizeof(struct kerning)*
  218. t1m->kernsize))==NULL) {
  219. SetError(status=NOMEM);
  220. } else {
  221. for (i=0; i<(int)t1m->kernsize; i++) {
  222. t1m->kerns[i].left = (UBYTE)io_ReadOneByte(file);
  223. t1m->kerns[i].right = (UBYTE)io_ReadOneByte(file);
  224. t1m->kerns[i].delta = GetNextWord(file);
  225. }
  226. if (io_FileError(file)!=SUCCESS) {
  227. SetError(status = BADMETRICS);
  228. }
  229. }
  230. }
  231. }
  232. }
  233. }
  234. if (io_CloseFile(file)!=SUCCESS)
  235. status = BADMETRICS;
  236. }
  237. return status;
  238. }