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.

483 lines
11 KiB

  1. //
  2. // Copyright (c) 1997-1999 Microsoft Corporation.
  3. //
  4. #include "stdafx.h"
  5. #pragma pack(2)
  6. #include "vdata.h"
  7. #include "ttfstruc.h"
  8. #include "extfunc.h"
  9. /*
  10. * Win3.1J EUDC fontfile i/o
  11. */
  12. #define EUDCCODEBASE ((unsigned short)0xe000)
  13. /*
  14. File Structure */
  15. struct W31_Header {
  16. char identify[72];
  17. short segCnt; /* ??? */
  18. unsigned short sCode,
  19. eCode;
  20. short cCnt;
  21. long ofsCmap;
  22. short sizCmap;
  23. long ofsFil;
  24. short sizFil;
  25. long ofsStbl; /* search tbl*/
  26. short sizStbl;
  27. long ofsBdatSub;
  28. };
  29. struct BDatSubTbl {
  30. long tail;
  31. long filler1;
  32. long head;
  33. short filler2;
  34. /* Following Pointer tbl. */
  35. };
  36. struct BMPHeader {
  37. long bitmapSiz;
  38. short xsiz, ysiz;
  39. };
  40. static int ReadBdatSub(HANDLE hdl,long ofs,struct BDatSubTbl *tbl);
  41. static int WriteBdatSub(HANDLE hdl,long ofs,struct BDatSubTbl *tbl);
  42. static int ReadBDatEntry(HANDLE hdl,long *ofs,long rec);
  43. static int WriteBDatEntry(HANDLE hdl,long ofs,long rec);
  44. static int ReadBMPHdr(HANDLE hdl,long ofs,struct BMPHeader *hdr);
  45. static int WriteBMPHdr(HANDLE hdl,long ofs,struct BMPHeader *hdr);
  46. static int init = 0;
  47. static long bdathead;
  48. static long bdatptr;
  49. static int maxRec;
  50. static TCHAR fpath[128];
  51. /***************************************************************
  52. * Initialize
  53. */
  54. /* */ int
  55. /* */ OpenW31JEUDC( TCHAR *path)
  56. /*
  57. * returns : 0, -1
  58. ***************************************************************/
  59. {
  60. HANDLE fHdl;
  61. struct W31_Header hdr;
  62. DWORD nByte;
  63. BOOL res;
  64. makeUniCodeTbl();
  65. lstrcpy( fpath, path);
  66. /* open EUDC Font File */
  67. fHdl = CreateFile(path,
  68. GENERIC_READ,
  69. FILE_SHARE_READ | FILE_SHARE_WRITE,
  70. NULL,
  71. OPEN_EXISTING,
  72. FILE_ATTRIBUTE_NORMAL,
  73. NULL);
  74. if ( fHdl == INVALID_HANDLE_VALUE)
  75. return -1;
  76. /* Read Header */
  77. res = ReadFile( fHdl, &hdr, sizeof(struct W31_Header), &nByte, NULL);
  78. if (!res || nByte !=sizeof(struct W31_Header))
  79. {
  80. CloseHandle(fHdl);
  81. return -1;
  82. }
  83. bdathead = hdr.ofsBdatSub;
  84. bdatptr = hdr.ofsBdatSub + sizeof(struct BDatSubTbl);
  85. maxRec = hdr.cCnt-1;
  86. /* close Font File */
  87. CloseHandle( fHdl);
  88. init = 1;
  89. return 0;
  90. }
  91. /***************************************************************
  92. * Terminate Close
  93. */
  94. /* */ void
  95. /* */ CloseW31JEUDC()
  96. /*
  97. * returns : none
  98. ***************************************************************/
  99. {
  100. init = 0;
  101. return;
  102. }
  103. static int
  104. codeToRec( unsigned short code, BOOL bUnicode)
  105. {
  106. return (int)((bUnicode ? code : sjisToUniEUDC(code)) - EUDCCODEBASE);
  107. }
  108. /***************************************************************
  109. * Read Bitmap
  110. */
  111. /* */ int
  112. /* */ GetW31JEUDCFont(
  113. /* */ unsigned short code, /* native-code */
  114. /* */ LPBYTE buf, /* buffer to set bitmap */
  115. /* */ int bufsiz, /* Buffer Size */
  116. /* */ int *xsiz, /* Bitmap X,Ysiz */
  117. /* */ int *ysiz,
  118. /* */ BOOL bUnicode)
  119. /*
  120. * returns : >=0, -1
  121. ***************************************************************/
  122. {
  123. HANDLE fHdl;
  124. long ofs;
  125. struct BMPHeader fhdr;
  126. int bmpsiz;
  127. int rdsiz;
  128. int rec;
  129. DWORD nByte;
  130. BOOL res;
  131. rec = codeToRec( code, bUnicode);
  132. if (init==0)
  133. return -1;
  134. else if ( maxRec < rec || rec < 0)
  135. return -1;
  136. /* Open Font File */
  137. fHdl = CreateFile(fpath,
  138. GENERIC_READ,
  139. FILE_SHARE_READ | FILE_SHARE_WRITE,
  140. NULL,
  141. OPEN_EXISTING,
  142. FILE_ATTRIBUTE_NORMAL,
  143. NULL);
  144. if ( fHdl == INVALID_HANDLE_VALUE)
  145. return -1;
  146. /* read bitmap ptr on subTable */
  147. ofs = bdatptr + sizeof(long)*rec;
  148. if ( (long) SetFilePointer( fHdl, ofs, NULL, FILE_BEGIN)!=ofs)
  149. goto ECLOSE_RET;
  150. res = ReadFile( fHdl, &ofs, sizeof(long), &nByte, NULL);
  151. if (!res || nByte !=sizeof(long))
  152. goto ECLOSE_RET;
  153. if ( ofs==0L)
  154. goto ECLOSE_RET;
  155. ofs += bdathead;
  156. /* read Bitmap Header
  157. bitmap is Word aligned */
  158. if ( (long) SetFilePointer( fHdl, ofs, NULL, FILE_BEGIN)!=ofs)
  159. goto ECLOSE_RET;
  160. res = ReadFile( fHdl, &fhdr, sizeof(struct BMPHeader), &nByte, NULL);
  161. if (!res || nByte != sizeof( struct BMPHeader))
  162. goto ECLOSE_RET;
  163. bmpsiz = ((int)fhdr.xsiz+15)/16 *2 * (int)fhdr.ysiz;
  164. /* Read Bitmap Body */
  165. rdsiz = bmpsiz > bufsiz ? bufsiz : bmpsiz;
  166. res = ReadFile( fHdl, buf, (unsigned short)rdsiz, &nByte, NULL);
  167. if (!res || nByte !=(unsigned short)rdsiz)
  168. goto ECLOSE_RET;
  169. rdsiz = bmpsiz > bufsiz ? bmpsiz - bufsiz : 0;
  170. *xsiz = fhdr.xsiz;
  171. *ysiz = fhdr.ysiz;
  172. CloseHandle (fHdl);
  173. return rdsiz;
  174. ECLOSE_RET:
  175. CloseHandle (fHdl);
  176. return -1;
  177. }
  178. /***************************************************************
  179. * Write Bitmap
  180. */
  181. /* */ int
  182. /* */ PutW31JEUDCFont(
  183. /* */ unsigned short code, /* native code */
  184. /* */ LPBYTE buf, /* buffer to set bitmap */
  185. /* */ int xsiz, /* Bitmap X,Ysiz */
  186. /* */ int ysiz,
  187. /* */ BOOL bUnicode)
  188. /*
  189. * returns : 0, -1
  190. ***************************************************************/
  191. {
  192. HANDLE fHdl;
  193. long ofs;
  194. struct BMPHeader fhdr;
  195. int bmpsiz;
  196. int wbmpsiz;
  197. struct BDatSubTbl subTbl;
  198. int rec;
  199. DWORD nByte;
  200. BOOL res;
  201. rec = codeToRec( code, bUnicode);
  202. if (init==0)
  203. return -1;
  204. else if ( maxRec < rec || rec < 0)
  205. return -1;
  206. /* Open Font File */
  207. fHdl = CreateFile(fpath,
  208. GENERIC_READ | GENERIC_WRITE,
  209. FILE_SHARE_READ | FILE_SHARE_WRITE,
  210. NULL,
  211. OPEN_EXISTING,
  212. FILE_ATTRIBUTE_NORMAL,
  213. NULL);
  214. if ( fHdl == INVALID_HANDLE_VALUE)
  215. return -1;
  216. /* read bitmap ptr on subTable */
  217. if (ReadBDatEntry( fHdl, &ofs, rec))
  218. goto ECLOSE_RET;
  219. wbmpsiz = (xsiz+15)/16 *2 * ysiz;
  220. if ( ofs != 0L) {
  221. /* read Bitmap Header
  222. bitmap is Word aligned */
  223. if ( ReadBMPHdr( fHdl, ofs, &fhdr))
  224. goto ECLOSE_RET;
  225. bmpsiz = ((int)fhdr.xsiz+15)/16 *2 * (int)fhdr.ysiz;
  226. if ( bmpsiz<wbmpsiz)
  227. ofs = 0L;
  228. }
  229. if ( ReadBdatSub( fHdl, bdathead, &subTbl))
  230. goto ECLOSE_RET;
  231. if ( ofs == 0L) {
  232. ofs = subTbl.tail;
  233. subTbl.tail += wbmpsiz+sizeof(fhdr);
  234. }
  235. /* Write Bitmap Header */
  236. fhdr.xsiz = (short)xsiz;
  237. fhdr.ysiz = (short)ysiz;
  238. fhdr.bitmapSiz = wbmpsiz+sizeof(fhdr);
  239. if ( WriteBMPHdr( fHdl, ofs, &fhdr))
  240. goto ECLOSE_RET;
  241. /* Write Bitmap Body */
  242. res = WriteFile( fHdl, buf, (unsigned short)wbmpsiz, &nByte, NULL);
  243. if (!res || nByte !=(unsigned short)wbmpsiz)
  244. goto ECLOSE_RET;
  245. /* write bitmap ptr on subTable */
  246. if (WriteBDatEntry( fHdl, ofs, rec))
  247. goto ECLOSE_RET;
  248. /* write subTable */
  249. if ( WriteBdatSub( fHdl, bdathead, &subTbl))
  250. goto ECLOSE_RET;
  251. CloseHandle (fHdl);
  252. return 0;
  253. ECLOSE_RET:
  254. CloseHandle (fHdl);
  255. return -1;
  256. }
  257. static int
  258. ReadBdatSub( HANDLE hdl, long ofs, struct BDatSubTbl *tbl)
  259. {
  260. DWORD nByte;
  261. BOOL res;
  262. if ( (long) SetFilePointer( hdl, ofs, NULL, FILE_BEGIN)!= ofs)
  263. goto ERET;
  264. res = ReadFile( hdl, tbl, sizeof (struct BDatSubTbl), &nByte, NULL);
  265. if (!res || nByte !=sizeof (struct BDatSubTbl))
  266. goto ERET;
  267. return 0;
  268. ERET:
  269. return -1;
  270. }
  271. static int
  272. WriteBdatSub( HANDLE hdl, long ofs, struct BDatSubTbl *tbl)
  273. {
  274. DWORD nByte;
  275. BOOL res;
  276. if ( (long) SetFilePointer( hdl, ofs, NULL, FILE_BEGIN)!= ofs)
  277. goto ERET;
  278. res = WriteFile( hdl, (char *)tbl, sizeof (struct BDatSubTbl), &nByte, NULL);
  279. if (!res || nByte !=sizeof (struct BDatSubTbl))
  280. goto ERET;
  281. return 0;
  282. ERET:
  283. return -1;
  284. }
  285. static int
  286. ReadBDatEntry( HANDLE hdl, long *ofs, long rec)
  287. {
  288. DWORD nByte;
  289. BOOL res;
  290. long ofsofs;
  291. ofsofs = bdatptr+(long)sizeof(long)*rec;
  292. if ( (long) SetFilePointer( hdl, ofsofs, NULL, FILE_BEGIN)!=ofsofs)
  293. goto ERET;
  294. res = ReadFile( hdl, ofs, sizeof (long), &nByte, NULL);
  295. if (!res || nByte != sizeof (long))
  296. goto ERET;
  297. return 0;
  298. ERET:
  299. return -1;
  300. }
  301. static int
  302. WriteBDatEntry( HANDLE hdl, long ofs, long rec)
  303. {
  304. long ofsofs;
  305. DWORD nByte;
  306. BOOL res;
  307. ofsofs = bdatptr+(long)sizeof(long)*rec;
  308. if ( (long) SetFilePointer( hdl, ofsofs, NULL, FILE_BEGIN)!=ofsofs)
  309. goto ERET;
  310. res = WriteFile( hdl, (char *)&ofs, sizeof(long), &nByte, NULL);
  311. if (!res || nByte != sizeof(long))
  312. goto ERET;
  313. return 0;
  314. ERET:
  315. return -1;
  316. }
  317. static int
  318. ReadBMPHdr( HANDLE hdl, long ofs, struct BMPHeader *hdr)
  319. {
  320. DWORD nByte;
  321. BOOL res;
  322. ofs += bdathead;
  323. if ( (long) SetFilePointer( hdl, ofs, NULL, FILE_BEGIN)!=ofs)
  324. goto ERET;
  325. res = ReadFile( hdl, hdr, sizeof( struct BMPHeader), &nByte, NULL);
  326. if (!res || nByte !=sizeof( struct BMPHeader))
  327. goto ERET;
  328. return 0;
  329. ERET:
  330. return -1;
  331. }
  332. static int
  333. WriteBMPHdr( HANDLE hdl, long ofs, struct BMPHeader *hdr)
  334. {
  335. DWORD nByte;
  336. BOOL res;
  337. ofs += bdathead;
  338. if ( (long) SetFilePointer( hdl, ofs, NULL, FILE_BEGIN)!=ofs)
  339. goto ERET;
  340. res = WriteFile( hdl, (char *)hdr, sizeof( struct BMPHeader), &nByte, NULL);
  341. if (!res || nByte !=sizeof( struct BMPHeader))
  342. goto ERET;
  343. return 0;
  344. ERET:
  345. return -1;
  346. }
  347. /***************************************************************
  348. * is Win95 EUDC bitmap
  349. */
  350. /* */ int
  351. /* */ IsWin95EUDCBmp(LPTSTR szBmpPath)
  352. /*
  353. * returns : 0 (other), 1 (EUDC bitmap), -1(error)
  354. ***************************************************************/
  355. {
  356. HANDLE fhdl;
  357. struct W31_Header hdr;
  358. DWORD nByte;
  359. BOOL res;
  360. fhdl = CreateFile(szBmpPath,
  361. GENERIC_READ,
  362. FILE_SHARE_READ | FILE_SHARE_WRITE,
  363. NULL,
  364. OPEN_EXISTING,
  365. FILE_ATTRIBUTE_NORMAL,
  366. NULL);
  367. if ( fhdl == INVALID_HANDLE_VALUE)
  368. {
  369. return -1;
  370. }
  371. res = ReadFile( fhdl, (LPBYTE)&hdr, sizeof(hdr), &nByte, NULL);
  372. CloseHandle( fhdl);
  373. if (!res || nByte !=sizeof(hdr)){
  374. return 0;
  375. }
  376. /* compare idendify leading 16 byte, sCode, eCode and cCnt*/
  377. if (memcmp( hdr.identify, "Windows95 EUDC", 14))
  378. {
  379. return 0;
  380. }
  381. if(hdr.sCode != 0x00e0){
  382. return 0;
  383. }
  384. return 1;
  385. }
  386. /* EOF */
  387. ////////////////////////////////////////////////////////////////////
  388. //
  389. // To work-around font linking in "Select Code" and "Save Char As".
  390. // so that a typeface specific font does not show any glyph it does
  391. // not have (from EUDC.TTE)
  392. //
  393. // path = *.euf
  394. // pGlyph = an array of 800 byte for 6400 EUDC chars
  395. //
  396. ////////////////////////////////////////////////////////////////////
  397. BOOL
  398. GetGlyph(TCHAR *Path, BYTE* pGlyph)
  399. {
  400. HANDLE fHdl;
  401. struct W31_Header hdr;
  402. DWORD nByte;
  403. long Offset;
  404. WORD wc;
  405. long lptr;
  406. TCHAR *pChar;
  407. TCHAR PathEUF[MAX_PATH];
  408. BOOL bRet = FALSE;
  409. lstrcpy(PathEUF, Path);
  410. pChar = PathEUF + lstrlen(PathEUF) - 3;
  411. *pChar = 0;
  412. lstrcat(PathEUF, TEXT("EUF"));
  413. fHdl = CreateFile(PathEUF,
  414. GENERIC_READ,
  415. FILE_SHARE_READ | FILE_SHARE_WRITE,
  416. NULL,
  417. OPEN_EXISTING,
  418. FILE_ATTRIBUTE_NORMAL,
  419. NULL);
  420. if(fHdl == INVALID_HANDLE_VALUE) return FALSE;
  421. bRet = ReadFile( fHdl, &hdr, sizeof(struct W31_Header), &nByte, NULL);
  422. if(!bRet && nByte !=sizeof(struct W31_Header)) goto Done;
  423. lptr = hdr.ofsBdatSub + sizeof(struct BDatSubTbl);
  424. if((long)SetFilePointer(fHdl, lptr, NULL, FILE_BEGIN) != lptr) goto Done;
  425. memset(pGlyph, 0, 800);
  426. for(wc = 0; wc < hdr.cCnt; wc++)
  427. {
  428. bRet = ReadFile( fHdl, &Offset, sizeof(long), &nByte, NULL);
  429. if(!bRet) goto Done;
  430. if(Offset == 0L || nByte !=sizeof(long)) continue;
  431. pGlyph[wc>>3] |= (0x80>>(wc%8));
  432. }
  433. Done:
  434. CloseHandle(fHdl);
  435. return bRet;
  436. }