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.

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