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.

584 lines
12 KiB

  1. /*
  2. * Win3.1J EUDC fontfile i/o ( MS-Code base)
  3. *
  4. * Copyright (c) 1997-1999 Microsoft Corporation.
  5. */
  6. #include "stdafx.h"
  7. #pragma pack(2)
  8. #include "extfunc.h"
  9. /*
  10. File Structure */
  11. struct W31_Header {
  12. char identify[72];
  13. short segCnt; /* ??? */
  14. unsigned short sCode,
  15. eCode;
  16. short cCnt;
  17. long ofsCmap;
  18. short sizCmap;
  19. long ofsFil;
  20. short sizFil;
  21. long ofsStbl; /* search tbl*/
  22. short sizStbl;
  23. long ofsBdatSub;
  24. };
  25. struct BDatSubTbl {
  26. long tail;
  27. long ptrOfs;
  28. long head;
  29. short filler2;
  30. /* Following Pointer tbl. */
  31. };
  32. struct BMPHeader {
  33. long bitmapSiz;
  34. short xsiz, ysiz;
  35. };
  36. #define EUDCCODEBASE ((unsigned short)0xe000)
  37. int OpenW31JBMP(TCHAR *path,int omd);
  38. int CloseW31JBMP(void);
  39. int isW31JEUDCBMP(TCHAR *path);
  40. int GetW31JBMPnRecs(int *nRec, int *nGlyph, int *xsiz, int *ysiz);
  41. int GetW31JBMPMeshSize( int *xsiz, int *ysiz);
  42. static int readcmap(void);
  43. static int rectocode(int rec,unsigned short *code);
  44. static int searchCode(unsigned short code);
  45. int GetW31JBMP(unsigned short code,LPBYTE buf,int bufsiz,int *xsiz,int *ysiz);
  46. int GetW31JBMPRec(int rec,LPBYTE buf,int bufsiz,int *xsiz,int *ysiz,unsigned short *code);
  47. int PutW31JBMPRec(int rec,LPBYTE buf,int xsiz,int ysiz);
  48. static int ReadBMPHdr(HANDLE hdl,long ofs,struct BMPHeader *bhdr);
  49. static int WriteBMPHdr(HANDLE hdl,long ofs,struct BMPHeader *bhdr);
  50. static int init = 0;
  51. static HANDLE fHdl;
  52. struct W31_Header hdr;
  53. struct BDatSubTbl bdTbl;
  54. static int rwmode = 0;
  55. static long *ofstbl=0;
  56. static unsigned short *cmap=0;
  57. static int *recordTbl=0;
  58. /***************************************************************
  59. * Initialize
  60. */
  61. /* */ int
  62. /* */ OpenW31JBMP( TCHAR *path, int omd)
  63. /*
  64. * returns : 0, -1
  65. ***************************************************************/
  66. {
  67. int msiz;
  68. DWORD nByte;
  69. BOOL res;
  70. /* open EUDC Font File */
  71. rwmode = omd ? 1 : 0;
  72. fHdl = CreateFile(path,
  73. omd==0 ? GENERIC_READ : GENERIC_READ | GENERIC_WRITE,
  74. FILE_SHARE_READ | FILE_SHARE_WRITE,
  75. NULL,
  76. OPEN_EXISTING,
  77. FILE_ATTRIBUTE_NORMAL,
  78. NULL);
  79. if ( fHdl == INVALID_HANDLE_VALUE)
  80. return -1;
  81. /* Read Header */
  82. res = ReadFile( fHdl, (LPBYTE)&hdr, sizeof(struct W31_Header), &nByte, NULL);
  83. if (!res || nByte !=sizeof(struct W31_Header))
  84. goto ERET;
  85. //
  86. // WinSE #13986,
  87. // In Win9x, character number could be well over 1880,
  88. // EUDC EUF file will be trashed if we limit character number to 1880
  89. //
  90. /*
  91. if( hdr.cCnt > 1880)
  92. hdr.cCnt = 1880;
  93. */
  94. /* allocate ofs. tbl. */
  95. msiz = hdr.cCnt*sizeof(long);
  96. if ((ofstbl = (long *)malloc( msiz))==(long *)0)
  97. goto ERET;
  98. /* Read Ofs. tbl.*/
  99. if ( (long) SetFilePointer( fHdl, hdr.ofsBdatSub, NULL, FILE_BEGIN)!=hdr.ofsBdatSub)
  100. goto ERET;
  101. res = ReadFile( fHdl, (LPBYTE)&bdTbl, sizeof(bdTbl), &nByte, NULL);
  102. if (!res || nByte !=sizeof(bdTbl))
  103. goto ERET;
  104. res = ReadFile( fHdl, (LPBYTE)ofstbl, (unsigned int)msiz, &nByte, NULL);
  105. if (!res || nByte !=(unsigned int)msiz)
  106. goto ERET;
  107. init = 1;
  108. /*
  109. if (fHdl != INVALID_HANDLE_VALUE)
  110. {
  111. CloseHandle(fHdl);
  112. fHdl = INVALID_HANDLE_VALUE;
  113. }
  114. */
  115. return 0;
  116. ERET:
  117. if (fHdl != INVALID_HANDLE_VALUE)
  118. {
  119. CloseHandle (fHdl);
  120. fHdl = INVALID_HANDLE_VALUE;
  121. }
  122. if (ofstbl)
  123. {
  124. free( ofstbl);
  125. ofstbl = 0;
  126. }
  127. return -1;
  128. }
  129. /***************************************************************
  130. * Terminate Close
  131. */
  132. /* */ int
  133. /* */ CloseW31JBMP()
  134. /*
  135. * returns : none
  136. ***************************************************************/
  137. {
  138. unsigned int siz;
  139. DWORD nByte;
  140. BOOL res;
  141. if ( rwmode>=1) {
  142. /* update ofstbl*/
  143. if ((long) SetFilePointer( fHdl, hdr.ofsBdatSub, NULL, FILE_BEGIN)!=hdr.ofsBdatSub)
  144. goto ERET;
  145. res = WriteFile( fHdl, (LPBYTE)&bdTbl, sizeof( bdTbl), &nByte, NULL);
  146. if (!res || nByte !=sizeof(bdTbl))
  147. goto ERET;
  148. siz = (unsigned int)hdr.cCnt*sizeof(long);
  149. res = WriteFile( fHdl, (LPBYTE)ofstbl, siz, &nByte, NULL);
  150. if (!res || nByte !=siz)
  151. goto ERET;
  152. }
  153. if ( fHdl !=INVALID_HANDLE_VALUE) {
  154. CloseHandle( fHdl);
  155. fHdl = INVALID_HANDLE_VALUE;
  156. }
  157. if ( ofstbl) {
  158. free(ofstbl);
  159. ofstbl = 0;
  160. }
  161. if ( cmap) {
  162. free(cmap);
  163. cmap = 0;
  164. }
  165. if ( recordTbl) {
  166. free(recordTbl);
  167. recordTbl = 0;
  168. }
  169. init = 0;
  170. return 0;
  171. ERET:
  172. return -1;
  173. }
  174. /***************************************************************
  175. * is Win3.1J EUDC bitmap
  176. */
  177. /* */ int
  178. /* */ isW31JEUDCBMP( TCHAR *path)
  179. /*
  180. * returns : 0 (other), 1 (EUDC bitmap), -1(error)
  181. ***************************************************************/
  182. {
  183. HANDLE fhdl;
  184. struct W31_Header hdr;
  185. DWORD nByte;
  186. BOOL res;
  187. fhdl = CreateFile(path,
  188. GENERIC_READ,
  189. FILE_SHARE_READ | FILE_SHARE_WRITE,
  190. NULL,
  191. OPEN_EXISTING,
  192. FILE_ATTRIBUTE_NORMAL,
  193. NULL);
  194. if ( fhdl == INVALID_HANDLE_VALUE)
  195. return -1;
  196. res = ReadFile( fhdl, (LPBYTE)&hdr, sizeof(hdr), &nByte, NULL);
  197. if (!res || nByte !=sizeof(hdr))
  198. goto NO_WIN31J;
  199. CloseHandle( fhdl);
  200. fhdl = INVALID_HANDLE_VALUE;
  201. /* compare idendify leading 16 byte, sCode, eCode and cCnt*/
  202. if (memcmp( hdr.identify, "WINEUDC2Standard", 16))
  203. goto NO_WIN31J;
  204. #if 0
  205. if ( hdr.sCode != 0x40f0 || hdr.eCode != 0xfcf9 || hdr.cCnt != 1880)
  206. #endif
  207. if( hdr.sCode != 0x40f0)
  208. goto NO_WIN31J;
  209. return 1;
  210. NO_WIN31J:
  211. if (fhdl != INVALID_HANDLE_VALUE)
  212. {
  213. CloseHandle(fhdl);
  214. fhdl = INVALID_HANDLE_VALUE;
  215. }
  216. return 0;
  217. }
  218. /***************************************************************
  219. * Get number of records
  220. */
  221. /* */ int
  222. /* */ GetW31JBMPnRecs( int *nRec, int *nGlyph, int *xsiz, int *ysiz)
  223. /*
  224. * returns : 0, -1
  225. ***************************************************************/
  226. {
  227. struct BMPHeader fhdr;
  228. long ofs;
  229. BOOL bFirst;
  230. int rec;
  231. int gc;
  232. DWORD nByte;
  233. BOOL res;
  234. bFirst = FALSE;
  235. if ( init==0 || fHdl == INVALID_HANDLE_VALUE)
  236. return -1;
  237. else {
  238. gc = 0;
  239. for ( rec = 0; rec < (int)hdr.cCnt; rec++) {
  240. if( *(ofstbl+rec)){
  241. if( !bFirst){
  242. ofs = *(ofstbl+rec);
  243. ofs += hdr.ofsBdatSub;
  244. if ( (DWORD) SetFilePointer( fHdl,ofs, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER)
  245. {
  246. DWORD dwErr = GetLastError();
  247. goto ERET;
  248. }
  249. res = ReadFile( fHdl, (LPBYTE)&fhdr,
  250. sizeof(struct BMPHeader), &nByte, NULL);
  251. if (!res || nByte != sizeof( struct BMPHeader))
  252. goto ERET;
  253. bFirst = TRUE;
  254. }
  255. gc++;
  256. }
  257. }
  258. *nRec = (int)hdr.cCnt;
  259. *nGlyph = gc;
  260. *xsiz = fhdr.xsiz;
  261. *ysiz = fhdr.ysiz;
  262. return 0;
  263. }
  264. ERET:
  265. return( -1);
  266. }
  267. static int
  268. readcmap()
  269. {
  270. unsigned int msiz;
  271. DWORD nByte;
  272. BOOL res;
  273. msiz = (unsigned int)hdr.cCnt*sizeof(unsigned short);
  274. if ((cmap = (unsigned short*)malloc(msiz))==(unsigned short *)0)
  275. goto ERET;
  276. if ((long) SetFilePointer( fHdl, hdr.ofsCmap, NULL, FILE_BEGIN)!=hdr.ofsCmap)
  277. goto ERET;
  278. res = ReadFile( fHdl, (LPBYTE)cmap, msiz, &nByte, NULL);
  279. if (!res || nByte !=msiz)
  280. goto ERET;
  281. return 0;
  282. ERET:
  283. return -1;
  284. }
  285. static int
  286. rectocode( int rec, unsigned short *code)
  287. {
  288. if ( cmap==0) {
  289. if (readcmap())
  290. return -1;
  291. }
  292. *code = *(cmap+rec);
  293. return 0;
  294. }
  295. static int
  296. searchCode( unsigned short code)
  297. {
  298. int high, low, mid;
  299. if ( cmap==(unsigned short *)0) {
  300. if (readcmap())
  301. goto ERET;
  302. }
  303. high = hdr.cCnt-1;
  304. low = 0;
  305. while ( high >= low) {
  306. mid = (high+low)/2;
  307. if ( *(cmap+mid)==code)
  308. return mid;
  309. else if ( *(cmap+mid)>code)
  310. high = mid-1;
  311. else
  312. low = mid+1;
  313. }
  314. ERET:
  315. return -1;
  316. }
  317. /***************************************************************
  318. * Read Bitmap by code number
  319. */
  320. /* */ int
  321. /* */ GetW31JBMP(
  322. /* */ unsigned short code, /* code Number */
  323. /* */ LPBYTE buf, /* buffer to set bitmap */
  324. /* */ int bufsiz, /* Buffer Size */
  325. /* */ int *xsiz, /* Bitmap X,Ysiz */
  326. /* */ int *ysiz)
  327. /*
  328. * returns : >=0, -1
  329. ***************************************************************/
  330. {
  331. int rec;
  332. int sts;
  333. unsigned short rcode;
  334. /* search code */
  335. if ( (rec = searchCode( code)) <0)
  336. return -1;
  337. else {
  338. sts = GetW31JBMPRec( rec, buf, bufsiz, xsiz, ysiz, &rcode);
  339. return sts;
  340. }
  341. }
  342. /****************************************/
  343. /* */
  344. /* Get W31JEUDC's Bmp Mesh Size */
  345. /* */
  346. /****************************************/
  347. int
  348. GetW31JBMPMeshSize(
  349. int *xsiz,
  350. int *ysiz)
  351. {
  352. long ofs;
  353. struct BMPHeader fhdr;
  354. int bmpsiz;
  355. DWORD nByte;
  356. BOOL res;
  357. if (init==0)
  358. return -1;
  359. ofs = *(ofstbl);
  360. if ( ofs==0L)
  361. return 0;
  362. ofs += hdr.ofsBdatSub;
  363. if ( (long) SetFilePointer(fHdl, ofs, NULL, FILE_BEGIN)!=ofs)
  364. goto ERET;
  365. res = ReadFile( fHdl, (LPBYTE)&fhdr, sizeof(struct BMPHeader), &nByte, NULL);
  366. if (!res || nByte != sizeof( struct BMPHeader))
  367. goto ERET;
  368. *xsiz = fhdr.xsiz;
  369. *ysiz = fhdr.ysiz;
  370. bmpsiz = ((int)fhdr.xsiz+15)/16 *2 * (int)fhdr.ysiz;
  371. return bmpsiz;
  372. ERET:
  373. return (-1);
  374. }
  375. /***************************************************************
  376. * Read Bitmap by record number
  377. */
  378. /* */ int
  379. /* */ GetW31JBMPRec(
  380. /* */ int rec, /* Record Number */
  381. /* */ LPBYTE buf, /* buffer to set bitmap */
  382. /* */ int bufsiz, /* Buffer Size */
  383. /* */ int *xsiz, /* Bitmap X,Ysiz */
  384. /* */ int *ysiz,
  385. /* */ unsigned short *code)
  386. /*
  387. * returns : bitmapsiz >=0, -1
  388. ***************************************************************/
  389. {
  390. long ofs;
  391. struct BMPHeader fhdr;
  392. int bmpsiz;
  393. int rdsiz;
  394. DWORD nByte;
  395. BOOL res;
  396. if (init==0)
  397. return -1;
  398. ofs = *(ofstbl+rec);
  399. if ( ofs==0L)
  400. return 0;
  401. ofs += hdr.ofsBdatSub;
  402. /* read Bitmap Header
  403. bitmap is Word aligned */
  404. if ( (long) SetFilePointer( fHdl, ofs, NULL, FILE_BEGIN)!=ofs)
  405. goto ERET;
  406. res = ReadFile( fHdl, (LPBYTE)&fhdr, sizeof(struct BMPHeader), &nByte, NULL);
  407. if (!res || nByte != sizeof( struct BMPHeader))
  408. goto ERET;
  409. bmpsiz = ((int)fhdr.xsiz+15)/16 *2 * (int)fhdr.ysiz;
  410. /* Read Bitmap Body */
  411. rdsiz = bmpsiz > bufsiz ? bufsiz : bmpsiz;
  412. if ( rdsiz > 0) {
  413. res = ReadFile( fHdl, buf, (unsigned int)rdsiz, &nByte, NULL);
  414. if (!res || nByte !=(unsigned int)rdsiz)
  415. goto ERET;
  416. }
  417. *xsiz = fhdr.xsiz;
  418. *ysiz = fhdr.ysiz;
  419. if ( rectocode( rec, code))
  420. goto ERET;
  421. return bmpsiz;
  422. ERET:
  423. return -1;
  424. }
  425. /***************************************************************
  426. * Write Bitmap by record number
  427. */
  428. /* */ int
  429. /* */ PutW31JBMPRec(
  430. /* */ int rec, /* Record Number */
  431. /* */ LPBYTE buf, /* buffer to set bitmap */
  432. /* */ int xsiz, /* Bitmap X,Ysiz */
  433. /* */ int ysiz)
  434. /*
  435. * returns : 0, -1
  436. ***************************************************************/
  437. {
  438. long ofs;
  439. struct BMPHeader fhdr;
  440. int bmpsiz;
  441. unsigned int wbmpsiz;
  442. DWORD nByte;
  443. BOOL res;
  444. if (init==0)
  445. return -1;
  446. else if ( rwmode==0)
  447. return -1;
  448. rwmode = 2;
  449. wbmpsiz = (unsigned int) ((xsiz+15)/16 *2 * ysiz);
  450. ofs = *(ofstbl+rec);
  451. if ( ofs != 0L) {
  452. /* read Bitmap Header
  453. bitmap is Word aligned */
  454. if ( ReadBMPHdr( fHdl, ofs, &fhdr))
  455. goto ERET;
  456. bmpsiz = ((int)fhdr.xsiz+15)/16 *2 * (int)fhdr.ysiz;
  457. if ( bmpsiz<(int)wbmpsiz)
  458. ofs = 0L;
  459. }
  460. if ( ofs == 0L)
  461. ofs = bdTbl.tail;
  462. /* Write Bitmap Header */
  463. fhdr.xsiz = (short)xsiz;
  464. fhdr.ysiz = (short)ysiz;
  465. fhdr.bitmapSiz = wbmpsiz+sizeof(fhdr);
  466. if ( WriteBMPHdr( fHdl, ofs, &fhdr))
  467. goto ERET;
  468. /* Write Bitmap Body */
  469. res = WriteFile( fHdl, buf, wbmpsiz, &nByte, NULL);
  470. if (!res || nByte !=wbmpsiz)
  471. goto ERET;
  472. /* write bitmap ptr on subTable */
  473. *(ofstbl+rec) = ofs;
  474. bdTbl.tail = ofs + wbmpsiz+sizeof(fhdr);
  475. return 0;
  476. ERET:
  477. return -1;
  478. }
  479. static int
  480. ReadBMPHdr( HANDLE hdl, long ofs, struct BMPHeader *bhdr)
  481. {
  482. DWORD nByte;
  483. BOOL res;
  484. ofs += hdr.ofsBdatSub;
  485. if ( (long) SetFilePointer( hdl, ofs, NULL, FILE_BEGIN)!=ofs)
  486. goto ERET;
  487. res = ReadFile( hdl, (LPBYTE) bhdr, sizeof( struct BMPHeader), &nByte, NULL);
  488. if (!res || nByte !=sizeof( struct BMPHeader))
  489. goto ERET;
  490. return 0;
  491. ERET:
  492. return -1;
  493. }
  494. static int
  495. WriteBMPHdr( HANDLE hdl, long ofs, struct BMPHeader *bhdr)
  496. {
  497. DWORD nByte;
  498. BOOL res;
  499. ofs += hdr.ofsBdatSub;
  500. if ( (long) SetFilePointer( hdl, ofs, NULL, FILE_BEGIN)!=ofs)
  501. goto ERET;
  502. res = WriteFile(hdl, (LPBYTE )bhdr, sizeof( struct BMPHeader), &nByte, NULL);
  503. if (!res || nByte !=sizeof( struct BMPHeader))
  504. goto ERET;
  505. return 0;
  506. ERET:
  507. return -1;
  508. }
  509. int
  510. W31JrecTbl( int **recTbl, BOOL bIsWin95EUDC)
  511. {
  512. int rec;
  513. int *tp;
  514. unsigned short code;
  515. if ( cmap==0) {
  516. if (readcmap())
  517. return -1;
  518. }
  519. if ( (tp = (int *)malloc( sizeof(int)*hdr.cCnt))==(int *)0)
  520. return -1;
  521. for ( rec = 0; rec < hdr.cCnt; rec++) {
  522. if ( *(ofstbl + rec)!=0L) {
  523. code = *(cmap+rec);
  524. if (!bIsWin95EUDC)
  525. code = sjisToUniEUDC( code);
  526. tp[(int)(code - EUDCCODEBASE)] = rec;
  527. }
  528. else
  529. tp[rec] = -1;
  530. }
  531. *recTbl = recordTbl = tp;
  532. return 0;
  533. }
  534. /* EOF */