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.

435 lines
14 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. writentf.c
  5. Abstract:
  6. Write a NTF file.
  7. Environment:
  8. Windows NT PostScript driver.
  9. Revision History:
  10. 07/08/98 -ksuzuki-
  11. Modified to support -v(verbose) and -o(optimize) options.
  12. 11/21/96 -slam-
  13. Created.
  14. dd-mm-yy -author-
  15. description
  16. --*/
  17. #include "writentf.h"
  18. extern bVerbose;
  19. extern bOptimize;
  20. int __cdecl compareGlyphSet(const void *elem1, const void *elem2)
  21. {
  22. PGLYPHSETDATA *p1;
  23. PGLYPHSETDATA *p2;
  24. PGLYPHSETDATA pGlyphSet1;
  25. PGLYPHSETDATA pGlyphSet2;
  26. DWORD hashValue1, hashValue2;
  27. p1 = (PGLYPHSETDATA*)elem1;
  28. p2 = (PGLYPHSETDATA*)elem2;
  29. pGlyphSet1 = *p1;
  30. pGlyphSet2 = *p2;
  31. hashValue1 = HashKeyword(MK_PTR(pGlyphSet1, dwGlyphSetNameOffset));
  32. hashValue2 = HashKeyword(MK_PTR(pGlyphSet2, dwGlyphSetNameOffset));
  33. if (hashValue1 == hashValue2)
  34. return(0);
  35. else if (hashValue1 < hashValue2)
  36. return(-1);
  37. else
  38. return(1);
  39. }
  40. int __cdecl compareFontMtx(const void *elem1, const void *elem2)
  41. {
  42. PNTM *p1;
  43. PNTM *p2;
  44. PNTM pNTM1;
  45. PNTM pNTM2;
  46. DWORD hashValue1, hashValue2;
  47. p1 = (PNTM*)elem1;
  48. p2 = (PNTM*)elem2;
  49. pNTM1 = *p1;
  50. pNTM2 = *p2;
  51. hashValue1 = HashKeyword(MK_PTR(pNTM1, dwFontNameOffset));
  52. hashValue2 = HashKeyword(MK_PTR(pNTM2, dwFontNameOffset));
  53. if (hashValue1 == hashValue2)
  54. return(0);
  55. else if (hashValue1 < hashValue2)
  56. return(-1);
  57. else
  58. return(1);
  59. }
  60. BOOL
  61. WriteNTF(
  62. IN PWSTR pwszNTFFile,
  63. IN DWORD dwGlyphSetCount,
  64. IN DWORD dwGlyphSetTotalSize,
  65. IN PGLYPHSETDATA *pGlyphSetData,
  66. IN DWORD dwFontMtxCount,
  67. IN DWORD dwFontMtxTotalSize,
  68. IN PNTM *pNTM
  69. )
  70. {
  71. HANDLE hNTFFile;
  72. NTF_FILEHEADER fileHeader;
  73. PNTF_GLYPHSETENTRY pGlyphSetEntry;
  74. PNTF_FONTMTXENTRY pFontMtxEntry;
  75. ULONG ulGlyphSetEntrySize;
  76. ULONG ulFontMtxEntrySize;
  77. ULONG ulByteWritten;
  78. ULONG i, j;
  79. DWORD dwOffset;
  80. DWORD dwGlyphSetCount2, dwGlyphSetTotalSize2;
  81. PGLYPHSETDATA pgsd;
  82. DWORD dwEofMark = NTF_EOF_MARK;
  83. dwGlyphSetCount2 = dwGlyphSetTotalSize2 = 0;
  84. //
  85. // Count the number of glyphsets necessary or referenced and their total
  86. // size. When optimization option is specified, don't count the glyphsets
  87. // without reference mark.
  88. //
  89. for (i = 0; i < dwGlyphSetCount; i++)
  90. {
  91. if (!bOptimize || pGlyphSetData[i]->dwReserved[0])
  92. {
  93. dwGlyphSetCount2++;
  94. dwGlyphSetTotalSize2 += pGlyphSetData[i]->dwSize;
  95. }
  96. }
  97. if (!bOptimize && (dwGlyphSetTotalSize != dwGlyphSetTotalSize2))
  98. {
  99. ERR(("WriteNTF:total size mismatch on optimization\n"));
  100. return FALSE;
  101. }
  102. if (bVerbose)
  103. {
  104. printf("Number of glyphset:%ld(total:%ld)\n",
  105. dwGlyphSetCount, dwGlyphSetTotalSize);
  106. if (bOptimize)
  107. {
  108. printf("Number of glyphset referenced:%ld(total:%ld)\n",
  109. dwGlyphSetCount2, dwGlyphSetTotalSize2);
  110. }
  111. printf("Number of font matrix:%ld(total:%ld)\n",
  112. dwFontMtxCount, dwFontMtxTotalSize);
  113. printf("\n");
  114. }
  115. // Fill in NTF file header.
  116. fileHeader.dwSignature = NTF_FILE_MAGIC;
  117. fileHeader.dwDriverType = NTF_DRIVERTYPE_PS;
  118. fileHeader.dwVersion = NTF_VERSION_NUMBER;
  119. for (i = 0; i < 5; i++)
  120. fileHeader.dwReserved[i] = 0;
  121. fileHeader.dwGlyphSetCount = dwGlyphSetCount2;
  122. fileHeader.dwFontMtxCount = dwFontMtxCount;
  123. ulGlyphSetEntrySize = dwGlyphSetCount2 * sizeof(NTF_GLYPHSETENTRY);
  124. ulFontMtxEntrySize = dwFontMtxCount * sizeof(NTF_FONTMTXENTRY);
  125. fileHeader.dwGlyphSetOffset = sizeof(NTF_FILEHEADER);
  126. fileHeader.dwFontMtxOffset = fileHeader.dwGlyphSetOffset + ulGlyphSetEntrySize;
  127. // Fill in glyph set entries.
  128. qsort(pGlyphSetData, dwGlyphSetCount, sizeof(PGLYPHSETDATA), compareGlyphSet);
  129. pGlyphSetEntry = MemAllocZ(ulGlyphSetEntrySize);
  130. if (!pGlyphSetEntry)
  131. {
  132. ERR(("WriteNTF:MemAllocZ\n"));
  133. return(FALSE);
  134. }
  135. dwOffset = fileHeader.dwFontMtxOffset + ulFontMtxEntrySize;
  136. for (i = j = 0; i < dwGlyphSetCount; i++)
  137. {
  138. pgsd = pGlyphSetData[i];
  139. // If no refernce mark is set with optimization option, ignore this
  140. // glyphset data.
  141. if (bOptimize && !(pgsd->dwReserved[0]))
  142. {
  143. pgsd = NULL;
  144. }
  145. if (pgsd)
  146. {
  147. pGlyphSetEntry[j].dwNameOffset = dwOffset + pgsd->dwGlyphSetNameOffset;
  148. pGlyphSetEntry[j].dwHashValue = HashKeyword(MK_PTR(pgsd, dwGlyphSetNameOffset));
  149. pGlyphSetEntry[j].dwDataSize = pgsd->dwSize;
  150. pGlyphSetEntry[j].dwDataOffset = dwOffset;
  151. pGlyphSetEntry[j].dwGlyphSetType = 0;
  152. pGlyphSetEntry[j].dwFlags = 0;
  153. pGlyphSetEntry[j].dwReserved[0] = 0;
  154. pGlyphSetEntry[j].dwReserved[1] = 0;
  155. dwOffset += pgsd->dwSize;
  156. j++;
  157. }
  158. }
  159. // Fill in font metrics entries.
  160. qsort(pNTM, dwFontMtxCount, sizeof(PNTM), compareFontMtx);
  161. pFontMtxEntry = MemAllocZ(ulFontMtxEntrySize);
  162. if (!pFontMtxEntry)
  163. {
  164. ERR(("WriteNTF:MemAllocZ\n"));
  165. MemFree(pGlyphSetEntry);
  166. return(FALSE);
  167. }
  168. if (dwOffset != (fileHeader.dwFontMtxOffset +
  169. ulFontMtxEntrySize +
  170. dwGlyphSetTotalSize2))
  171. {
  172. ERR(("WriteNTF:dwOffset\n"));
  173. MemFree(pGlyphSetEntry);
  174. MemFree(pFontMtxEntry);
  175. return(FALSE);
  176. }
  177. for (i = 0; i < dwFontMtxCount; i++)
  178. {
  179. pFontMtxEntry[i].dwFontNameOffset = dwOffset + pNTM[i]->dwFontNameOffset;
  180. pFontMtxEntry[i].dwHashValue = HashKeyword(MK_PTR(pNTM[i], dwFontNameOffset));
  181. pFontMtxEntry[i].dwDataSize = pNTM[i]->dwSize;
  182. pFontMtxEntry[i].dwDataOffset = dwOffset;
  183. pFontMtxEntry[i].dwVersion = 0;
  184. pFontMtxEntry[i].dwReserved[0] = 0;
  185. pFontMtxEntry[i].dwReserved[1] = 0;
  186. pFontMtxEntry[i].dwReserved[2] = 0;
  187. dwOffset += pNTM[i]->dwSize;
  188. }
  189. //
  190. // Begin to write everything into the NTF file!
  191. //
  192. hNTFFile = CreateFile(pwszNTFFile, GENERIC_WRITE, FILE_SHARE_READ, NULL,
  193. CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  194. if (hNTFFile == INVALID_HANDLE_VALUE)
  195. {
  196. ERR(("WriteNTF:CreateFile\n"));
  197. MemFree(pGlyphSetEntry);
  198. MemFree(pFontMtxEntry);
  199. return(FALSE);
  200. }
  201. if (!WriteFile(hNTFFile, (LPVOID)&fileHeader, sizeof(NTF_FILEHEADER),
  202. (LPDWORD)&ulByteWritten, (LPOVERLAPPED)NULL)
  203. || (ulByteWritten != sizeof(NTF_FILEHEADER)))
  204. {
  205. ERR(("WriteNTF:WriteFile\n"));
  206. MemFree(pGlyphSetEntry);
  207. MemFree(pFontMtxEntry);
  208. CloseHandle(hNTFFile);
  209. return(FALSE);
  210. }
  211. if (bVerbose)
  212. {
  213. char s[5];
  214. s[0] = (char)(fileHeader.dwSignature >> 24);
  215. s[1] = (char)(fileHeader.dwSignature >> 16);
  216. s[2] = (char)(fileHeader.dwSignature >> 8);
  217. s[3] = (char)(fileHeader.dwSignature );
  218. s[4] = '\0';
  219. printf("NTF_FILEHEADER:dwSignature:%08X('%s')\n", fileHeader.dwSignature, s);
  220. s[0] = (char)(fileHeader.dwDriverType >> 24);
  221. s[1] = (char)(fileHeader.dwDriverType >> 16);
  222. s[2] = (char)(fileHeader.dwDriverType >> 8);
  223. s[3] = (char)(fileHeader.dwDriverType );
  224. s[4] = '\0';
  225. printf("NTF_FILEHEADER:dwDriverType:%08X('%s')\n", fileHeader.dwDriverType, s);
  226. printf("NTF_FILEHEADER:dwVersion:%08X\n", fileHeader.dwVersion);
  227. printf("NTF_FILEHEADER:dwGlyphSetCount:%ld\n", fileHeader.dwGlyphSetCount);
  228. printf("NTF_FILEHEADER:dwFontMtxCount:%ld\n", fileHeader.dwFontMtxCount);
  229. printf("\n");
  230. }
  231. if (!WriteFile(hNTFFile, (LPVOID)pGlyphSetEntry, ulGlyphSetEntrySize,
  232. (LPDWORD)&ulByteWritten, (LPOVERLAPPED)NULL)
  233. || (ulByteWritten != ulGlyphSetEntrySize))
  234. {
  235. ERR(("WriteNTF:WriteFile\n"));
  236. MemFree(pGlyphSetEntry);
  237. MemFree(pFontMtxEntry);
  238. CloseHandle(hNTFFile);
  239. return(FALSE);
  240. }
  241. if (bVerbose)
  242. {
  243. for (i = 0; i < dwGlyphSetCount2; i++)
  244. {
  245. printf("NTF_GLYPHSETENTRY for GLYPHSETDATA #%d\n", i + 1);
  246. printf("NTF_GLYPHSETENTRY:dwHashValue:%08X\n", pGlyphSetEntry[i].dwHashValue);
  247. printf("NTF_GLYPHSETENTRY:dwDataSize:%ld\n", pGlyphSetEntry[i].dwDataSize);
  248. printf("\n");
  249. }
  250. }
  251. if (!WriteFile(hNTFFile, (LPVOID)pFontMtxEntry, ulFontMtxEntrySize,
  252. (LPDWORD)&ulByteWritten, (LPOVERLAPPED)NULL)
  253. || (ulByteWritten != ulFontMtxEntrySize))
  254. {
  255. ERR(("WriteNTF:WriteFile\n"));
  256. MemFree(pGlyphSetEntry);
  257. MemFree(pFontMtxEntry);
  258. CloseHandle(hNTFFile);
  259. return(FALSE);
  260. }
  261. if (bVerbose)
  262. {
  263. for (i = 0; i < dwFontMtxCount; i++)
  264. {
  265. printf("NTF_FONTMTXENTRY for NTM #%d\n", i + 1);
  266. printf("NTF_FONTMTXENTRY:dwHashValue:%08X\n", pFontMtxEntry[i].dwHashValue);
  267. printf("NTF_FONTMTXENTRY:dwDataSize:%ld\n", pFontMtxEntry[i].dwDataSize);
  268. printf("NTF_FONTMTXENTRY:dwVersion:%08X\n", pFontMtxEntry[i].dwVersion);
  269. printf("\n");
  270. }
  271. }
  272. for (i = j = 0; i < dwGlyphSetCount; i++)
  273. {
  274. pgsd = pGlyphSetData[i];
  275. // If no refernce mark is set with optimization option, ignore this
  276. // glyphset data.
  277. if (bOptimize && !(pgsd->dwReserved[0]))
  278. {
  279. pgsd = NULL;
  280. }
  281. if (pgsd)
  282. {
  283. LONG lBytes, lSize = pgsd->dwSize;
  284. PBYTE pTemp = (PBYTE)pgsd;
  285. pgsd->dwReserved[0] = 0; // Make sure it's cleared always.
  286. while (lSize > 0)
  287. {
  288. lBytes = (lSize > 20000) ? 20000 : lSize;
  289. if (!WriteFile(hNTFFile, (LPVOID)pTemp, lBytes,
  290. (LPDWORD)&ulByteWritten, (LPOVERLAPPED)NULL)
  291. || (ulByteWritten != (ULONG)lBytes))
  292. {
  293. ERR(("WriteNTF:WriteFile\n"));
  294. MemFree(pGlyphSetEntry);
  295. MemFree(pFontMtxEntry);
  296. CloseHandle(hNTFFile);
  297. return(FALSE);
  298. }
  299. lSize -= 20000;
  300. pTemp += 20000;
  301. }
  302. }
  303. if (bVerbose && pgsd)
  304. {
  305. printf("GLYPHSETDATA #%d\n", j++ + 1);
  306. printf("GLYPHSETDATA:dwSize:%ld\n", pgsd->dwSize);
  307. printf("GLYPHSETDATA:dwVersion:%08X\n", pgsd->dwVersion);
  308. printf("GLYPHSETDATA:dwFlags:%08X\n", pgsd->dwFlags);
  309. printf("GLYPHSETDATA:dwGlyphSetNameOffset:%s\n", (PSZ)MK_PTR(pgsd, dwGlyphSetNameOffset));
  310. printf("GLYPHSETDATA:dwGlyphCount:%ld\n", pgsd->dwGlyphCount);
  311. printf("GLYPHSETDATA:dwRunCount:%ld\n", pgsd->dwRunCount);
  312. printf("GLYPHSETDATA:dwCodePageCount:%ld\n", pgsd->dwCodePageCount);
  313. printf("\n");
  314. }
  315. }
  316. for (i = 0; i < dwFontMtxCount; i++)
  317. {
  318. if (!WriteFile(hNTFFile, (LPVOID)pNTM[i], pNTM[i]->dwSize,
  319. (LPDWORD)&ulByteWritten, (LPOVERLAPPED)NULL)
  320. || (ulByteWritten != pNTM[i]->dwSize))
  321. {
  322. ERR(("WriteNTF:WriteFile\n"));
  323. MemFree(pGlyphSetEntry);
  324. MemFree(pFontMtxEntry);
  325. CloseHandle(hNTFFile);
  326. return(FALSE);
  327. }
  328. if (bVerbose)
  329. {
  330. printf("NTM #%d\n", i + 1);
  331. printf("NTM:dwSize:%ld\n", pNTM[i]->dwSize);
  332. printf("NTM:dwVersion:%08X\n", pNTM[i]->dwVersion);
  333. printf("NTM:dwFlags:%08X\n", pNTM[i]->dwFlags);
  334. printf("NTM:dwFontNameOffset:%s\n", (PSZ)MK_PTR(pNTM[i], dwFontNameOffset));
  335. printf("NTM:dwDisplayNameOffset:%S\n", (PTSTR)MK_PTR(pNTM[i], dwDisplayNameOffset));
  336. printf("NTM:dwFontVersion:%08X\n", pNTM[i]->dwFontVersion);
  337. printf("NTM:dwGlyphSetNameOffset:%s\n", (PSZ)MK_PTR(pNTM[i], dwGlyphSetNameOffset));
  338. printf("NTM:dwGlyphCount:%ld\n", pNTM[i]->dwGlyphCount);
  339. printf("NTM:dwCharWidthCount:%ld\n", pNTM[i]->dwCharWidthCount);
  340. printf("NTM:dwDefaultCharWidth:%ld\n", pNTM[i]->dwDefaultCharWidth);
  341. printf("NTM:dwKernPairCount:%ld\n", pNTM[i]->dwKernPairCount);
  342. printf("NTM:dwCharSet:%ld\n", pNTM[i]->dwCharSet);
  343. printf("NTM:dwCodePage:%ld\n", pNTM[i]->dwCodePage);
  344. printf("\n");
  345. }
  346. }
  347. //
  348. // Write EOF marker
  349. //
  350. if (!WriteFile(hNTFFile, (LPVOID)&dwEofMark, sizeof (DWORD),
  351. (LPDWORD)&ulByteWritten, (LPOVERLAPPED)NULL)
  352. || (ulByteWritten != sizeof (DWORD)))
  353. {
  354. ERR(("WriteNTF:WriteFile:EOF\n"));
  355. MemFree(pGlyphSetEntry);
  356. MemFree(pFontMtxEntry);
  357. CloseHandle(hNTFFile);
  358. return (FALSE);
  359. }
  360. MemFree(pGlyphSetEntry);
  361. MemFree(pFontMtxEntry);
  362. CloseHandle(hNTFFile);
  363. return(TRUE);
  364. }