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.

551 lines
14 KiB

  1. /*
  2. * CWIN3.1 and ETEN format file i/o
  3. *
  4. * Copyright (c) 1997-1999 Microsoft Corporation.
  5. */
  6. #include "stdafx.h"
  7. #pragma pack(2)
  8. #include "extfunc.h"
  9. #include "eten.h"
  10. #define EUDCCODEBASE ((unsigned short)0xe000)
  11. #define ETENBANKID 0x8001
  12. /*
  13. static unsigned short getsval(unsigned char *s);
  14. static unsigned long getlval(unsigned char *l);
  15. static void setsval(unsigned char *m,unsigned short s);
  16. static void setlval(unsigned char *m,unsigned long lval);
  17. static int readHdr(HANDLE fhdl,struct ETENHEADER *hdr);
  18. static int updHdr(void);
  19. static int getETENBankID( HANDLE fh, WORD *BankID);
  20. int openETENBMP(TCHAR *path,int md);
  21. int closeETENBMP(void);
  22. static void setIniHdr(struct R_ETENHEADER *hdr,int width,int height);
  23. int createETENBMP(TCHAR *path,int wid,int hei);
  24. int getETENBMPInf(int *n, int *ng, int *wid,int *hei, char *sign,WORD *bID);
  25. int readETENBMPRec(int rec,LPBYTE buf,int bufsiz,unsigned short *code);
  26. int appendETENBMP(LPBYTE buf,unsigned short code);
  27. int isETENBMP(TCHAR *path);
  28. */
  29. static HANDLE bmpFh ;
  30. static int openmd = -1;
  31. static int nChar;
  32. static int width, height;
  33. static int *recordBuf=0;
  34. int uNum=0;
  35. static int *codep=0;
  36. static char UserFontSign[8];
  37. static WORD BankID;
  38. static unsigned short
  39. getsval( unsigned char *s)
  40. {
  41. unsigned short sval;
  42. sval = (unsigned short )*(s+1);
  43. sval <<=8;
  44. sval |= (unsigned short )*s;
  45. return sval;
  46. }
  47. static unsigned long
  48. getlval( unsigned char *l)
  49. {
  50. unsigned long lval;
  51. int i;
  52. lval = (unsigned long)*(l+3);
  53. for ( i=2; i>=0; i--) {
  54. lval<<=8;
  55. lval |=(unsigned long)*(l+i);
  56. }
  57. return lval;
  58. }
  59. static void
  60. setsval( unsigned char *m, unsigned short s)
  61. {
  62. *m = (unsigned char)(s & 0xff);
  63. *(m+1) = (unsigned char)((s>>8) & 0xff);
  64. }
  65. static void
  66. setlval( unsigned char *m, unsigned long lval)
  67. {
  68. int i;
  69. for ( i=0; i<4; i++) {
  70. *m++ = (unsigned char)(lval & 0xff);
  71. lval>>=8;
  72. }
  73. }
  74. static int
  75. readHdr( HANDLE fhdl, struct ETENHEADER *hdr)
  76. {
  77. struct R_ETENHEADER rhdr;
  78. DWORD nByte;
  79. BOOL res;
  80. res = ReadFile( fhdl, &rhdr, 256, &nByte, NULL);
  81. if (!res || nByte !=256)
  82. goto ERET;
  83. memset( hdr, 0, sizeof(struct ETENHEADER));
  84. /* Set values to hdr */
  85. hdr->uHeaderSize = getsval( rhdr.uHeaderSize);
  86. memcpy( hdr->idUserFontSign, rhdr.idUserFontSign, 8);
  87. hdr->idMajor = rhdr.idMajor;
  88. hdr->idMinor = rhdr.idMinor;
  89. hdr->ulCharCount = getlval( rhdr.ulCharCount);
  90. hdr->uCharWidth = getsval( rhdr.uCharWidth);
  91. hdr->uCharHeight = getsval( rhdr.uCharHeight);
  92. hdr->cPatternSize = getlval( rhdr.cPatternSize);
  93. hdr->uchBankID = rhdr.uchBankID;
  94. hdr->idInternalBankID = getsval( rhdr.idInternalBankID);
  95. hdr->sFontInfo.uInfoSize = getsval(rhdr.sFontInfo.uInfoSize);
  96. hdr->sFontInfo.idCP = getsval(rhdr.sFontInfo.idCP);
  97. hdr->sFontInfo.idCharSet = rhdr.sFontInfo.idCharSet;
  98. hdr->sFontInfo.fbTypeFace = rhdr.sFontInfo.fbTypeFace;
  99. memcpy( hdr->sFontInfo.achFontName , rhdr.sFontInfo.achFontName,12);
  100. hdr->sFontInfo.ulCharDefine = getlval(rhdr.sFontInfo.ulCharDefine);
  101. hdr->sFontInfo.uCellWidth = getsval(rhdr.sFontInfo.uCellWidth);
  102. hdr->sFontInfo.uCellHeight = getsval(rhdr.sFontInfo.uCellHeight);
  103. hdr->sFontInfo.uCharHeight = getsval(rhdr.sFontInfo.uCharHeight);
  104. hdr->sFontInfo.uBaseLine = getsval(rhdr.sFontInfo.uBaseLine);
  105. hdr->sFontInfo.uUnderLine = getsval(rhdr.sFontInfo.uUnderLine);
  106. hdr->sFontInfo.uUnlnHeight = getsval(rhdr.sFontInfo.uUnlnHeight);
  107. hdr->sFontInfo.fchStrokeWeight = rhdr.sFontInfo.fchStrokeWeight;
  108. hdr->sFontInfo.fCharStyle = getsval(rhdr.sFontInfo.fCharStyle);
  109. hdr->sFontInfo.fbFontAttrib = rhdr.sFontInfo.fbFontAttrib;
  110. hdr->sFontInfo.ulCellWidthMax = getlval(rhdr.sFontInfo.ulCellWidthMax);
  111. hdr->sFontInfo.ulCellHeightMax= getlval(rhdr.sFontInfo.ulCellHeightMax);
  112. return 0;
  113. ERET:
  114. return -1;
  115. }
  116. static int
  117. updHdr( )
  118. {
  119. struct R_ETENHEADER rhdr;
  120. DWORD nByte;
  121. BOOL res;
  122. if ( (long) SetFilePointer( bmpFh, 0L, NULL, FILE_BEGIN)!=0L)
  123. goto ERET;
  124. res = ReadFile( bmpFh, &rhdr, 256, &nByte, NULL);
  125. if (!res || nByte !=256)
  126. goto ERET;
  127. setlval( rhdr.ulCharCount, (long)nChar);
  128. setlval( rhdr.sFontInfo.ulCharDefine, (long)nChar);
  129. if ( (long) SetFilePointer( bmpFh, 0L, NULL, FILE_BEGIN)!=0L)
  130. goto ERET;
  131. res = WriteFile(bmpFh, (char *)&rhdr, 256,&nByte, NULL);
  132. if (!res || nByte !=256)
  133. goto ERET;
  134. return 0;
  135. ERET:
  136. return -1;
  137. }
  138. static int
  139. getETENBankID( HANDLE fh, WORD *BankID)
  140. {
  141. struct R_CODEELEMENT cElm;
  142. long ofs;
  143. DWORD nByte;
  144. BOOL res;
  145. ofs = sizeof(struct R_ETENHEADER);
  146. if ((long) SetFilePointer( fh, ofs, NULL, FILE_BEGIN) != ofs)
  147. goto ERET;
  148. res = ReadFile( fh, &cElm, sizeof(struct R_CODEELEMENT), &nByte, NULL);
  149. if (!res || nByte !=sizeof(struct R_CODEELEMENT))
  150. goto ERET;
  151. *BankID = getsval( cElm.nBankID);
  152. return 0;
  153. ERET:
  154. return -1;
  155. }
  156. /***
  157. recBuf
  158. +-------+
  159. | rec# | E000
  160. +-------+
  161. | | E001
  162. +-------+
  163. |
  164. +-------+
  165. | | maxUCode
  166. +-------+
  167. ****/
  168. static int
  169. scanETENBMP( int **recBuf, unsigned int maxUCode, int nRec)
  170. {
  171. long ofs;
  172. int recsiz, bmpsiz;
  173. struct R_CODEELEMENT *bhd;
  174. int rec;
  175. char *rbuf;
  176. unsigned short code;
  177. unsigned short ucode;
  178. int urec;
  179. int *recp;
  180. DWORD nByte;
  181. BOOL res;
  182. recp = 0;
  183. rbuf = 0;
  184. if ( maxUCode < EUDCCODEBASE)
  185. return -1;
  186. else if ( nRec <=0)
  187. return -1;
  188. uNum = maxUCode - EUDCCODEBASE+1;
  189. if ( (codep = (int *)malloc( uNum*sizeof(int)))==0)
  190. goto ERET;
  191. ofs = sizeof( struct R_ETENHEADER);
  192. if ( (long) SetFilePointer( bmpFh, ofs, NULL, FILE_BEGIN)!=ofs)
  193. goto ERET;
  194. bmpsiz = (width+7)/8*height;
  195. recsiz =bmpsiz+sizeof (bhd);
  196. if ((rbuf = (char *)malloc( recsiz))==(char *)0)
  197. goto ERET;
  198. for ( code = EUDCCODEBASE; code <= maxUCode; code++)
  199. codep[code-EUDCCODEBASE] = -1;
  200. bhd = (struct R_CODEELEMENT *)rbuf;
  201. for ( rec = 0; rec < nRec; rec++) {
  202. res = ReadFile( bmpFh, rbuf, (unsigned int)recsiz, &nByte, NULL);
  203. if (!res || nByte !=(unsigned int)recsiz)
  204. goto ERET;
  205. code = getsval( bhd->nInternalCode);
  206. if( memcmp(UserFontSign, "CMEX_PTN", 8) ||
  207. BankID != ETENBANKID){
  208. ucode = sjisToUniEUDC( code);
  209. }else ucode = code;
  210. if( ucode > maxUCode || ucode < EUDCCODEBASE)
  211. continue;
  212. urec = (int)(ucode - EUDCCODEBASE);
  213. codep[urec] = rec;
  214. }
  215. free( rbuf);
  216. if ( (recp = (int *)malloc( nRec*sizeof(int)))==0)
  217. goto ERET;
  218. *recBuf=recp;
  219. for ( rec=0; rec < uNum; rec++) {
  220. if ( codep[rec]>0)
  221. *recp++ = codep[rec];
  222. }
  223. return 0;
  224. ERET:
  225. if ( codep) free( codep);
  226. if ( recp) free( recp);
  227. if ( rbuf) free( rbuf);
  228. return -1;
  229. }
  230. int
  231. openETENBMP( TCHAR *path, int md)
  232. {
  233. HANDLE fh;
  234. struct ETENHEADER hdr;
  235. makeUniCodeTbl();
  236. if ( md) {
  237. fh = CreateFile(path,
  238. GENERIC_READ | GENERIC_WRITE,
  239. FILE_SHARE_READ | FILE_SHARE_WRITE,
  240. NULL,
  241. OPEN_EXISTING,
  242. FILE_ATTRIBUTE_NORMAL,
  243. NULL);
  244. if ( fh == INVALID_HANDLE_VALUE)
  245. goto ERET;
  246. bmpFh = fh;
  247. openmd = 1;
  248. }
  249. else {
  250. fh = CreateFile(path,
  251. GENERIC_READ,
  252. FILE_SHARE_READ | FILE_SHARE_WRITE,
  253. NULL,
  254. OPEN_EXISTING,
  255. FILE_ATTRIBUTE_NORMAL,
  256. NULL);
  257. if ( fh == INVALID_HANDLE_VALUE)
  258. goto ERET;
  259. bmpFh = fh;
  260. openmd = 0;
  261. }
  262. if (readHdr( fh, &hdr))
  263. goto ERET;
  264. if (getETENBankID( fh, &BankID))
  265. goto ERET;
  266. nChar = (int)hdr.ulCharCount;
  267. width = (int)hdr.uCharWidth;
  268. height = (int)hdr.uCharHeight;
  269. memcpy((char *)UserFontSign, hdr.idUserFontSign, 8);
  270. if ( scanETENBMP( &recordBuf, getMaxUniCode(), nChar))
  271. goto ERET;
  272. return 0;
  273. ERET:
  274. if (fh != INVALID_HANDLE_VALUE)
  275. CloseHandle(fh);
  276. return -1;
  277. }
  278. int
  279. closeETENBMP( )
  280. {
  281. int sts;
  282. if ( openmd)
  283. sts = updHdr();
  284. else sts = 0;
  285. if ( bmpFh!=INVALID_HANDLE_VALUE)
  286. CloseHandle( bmpFh);
  287. if(recordBuf) {
  288. free(recordBuf);
  289. recordBuf = 0;
  290. }
  291. if ( codep) {
  292. free(codep);
  293. codep = 0;
  294. }
  295. return sts;
  296. }
  297. static void
  298. setIniHdr( struct R_ETENHEADER *hdr, int width, int height)
  299. {
  300. memset( hdr, 0, sizeof(struct R_ETENHEADER));
  301. setsval( hdr->uHeaderSize, sizeof(struct R_ETENHEADER));
  302. memcpy( hdr->idUserFontSign, "CWIN_PTN", 8);
  303. hdr->idMajor = 1;
  304. hdr->idMinor = 0;
  305. setsval( hdr->uCharWidth, (unsigned short)width);
  306. setsval( hdr->uCharHeight, (unsigned short)height);
  307. setlval( hdr->cPatternSize, (unsigned long)(((width+7)/8)*height));
  308. setsval( hdr->sFontInfo.uInfoSize,
  309. (unsigned short)sizeof(struct R_CFONTINFO));
  310. setsval( hdr->sFontInfo.idCP, 938);
  311. hdr->sFontInfo.idCharSet = (char)0x88;
  312. setsval( hdr->sFontInfo.uCellWidth, (unsigned short)width);
  313. setsval( hdr->sFontInfo.uCellHeight, (unsigned short)height);
  314. setsval( hdr->sFontInfo.uCharHeight, (unsigned short)height);
  315. setlval( hdr->sFontInfo.ulCellWidthMax, (unsigned long)width);
  316. setlval( hdr->sFontInfo.ulCellHeightMax, (unsigned long)height);
  317. }
  318. int
  319. createETENBMP( TCHAR *path, int wid, int hei)
  320. {
  321. HANDLE fh;
  322. struct R_ETENHEADER hdr;
  323. DWORD nByte;
  324. BOOL res;
  325. fh = CreateFile(path,
  326. GENERIC_WRITE,
  327. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  328. NULL,
  329. CREATE_ALWAYS,
  330. FILE_ATTRIBUTE_NORMAL,
  331. NULL);
  332. if ( fh == INVALID_HANDLE_VALUE)
  333. goto ERET;
  334. width = wid;
  335. height = hei;
  336. setIniHdr( &hdr, width, height);
  337. res = WriteFile( fh, (char *)&hdr, sizeof(hdr), &nByte, NULL);
  338. if (!res || nByte !=sizeof(hdr))
  339. goto ERET;
  340. bmpFh = fh;
  341. openmd = 1;
  342. nChar =0;
  343. return 0;
  344. ERET:
  345. if (fh != INVALID_HANDLE_VALUE)
  346. CloseHandle(fh);
  347. return -1;
  348. }
  349. int
  350. getETENBMPInf( int *nRec, int *nGlyph, int *wid, int *hei, char *sign,WORD *bID)
  351. {
  352. if ( bmpFh <0) return -1;
  353. *nRec = uNum;
  354. *nGlyph = nChar;
  355. *wid = width;
  356. *hei = height;
  357. *bID = BankID;
  358. memcpy( sign, UserFontSign, 8);
  359. return 0;
  360. }
  361. int
  362. readETENBMPRec( int rec, LPBYTE buf, int bufsiz, unsigned short *code)
  363. {
  364. long ofs;
  365. int recsiz;
  366. struct R_CODEELEMENT bhd;
  367. int rdsiz;
  368. int bWid, wWid;
  369. int y, ylim;
  370. unsigned char *rbuf;
  371. DWORD nByte;
  372. BOOL res;
  373. bWid = (width+7)/8;
  374. wWid = (bWid+1)/2*2;
  375. recsiz = (width+7)/8*height;
  376. ofs = sizeof( struct R_ETENHEADER)+(long)(recsiz+sizeof (bhd))*rec;
  377. if ( (long) SetFilePointer( bmpFh, ofs, NULL, FILE_BEGIN)!=ofs)
  378. goto ERET;
  379. res = ReadFile( bmpFh, &bhd, sizeof(bhd), &nByte, NULL);
  380. if (!res || nByte !=sizeof(bhd))
  381. goto ERET;
  382. if ( bufsiz<recsiz) rdsiz = bufsiz;
  383. else rdsiz = recsiz;
  384. if ( bWid!=wWid) {
  385. BYTE *src, *dst;
  386. if ((rbuf = (unsigned char *)malloc( recsiz))==(unsigned char *)0)
  387. goto ERET;
  388. res = ReadFile( bmpFh, (char *)rbuf, (unsigned int)recsiz, &nByte, NULL);
  389. if (!res || nByte !=(unsigned int)recsiz) {
  390. free(rbuf);
  391. goto ERET;
  392. }
  393. ylim = rdsiz / bWid;
  394. src = (LPBYTE)rbuf;
  395. dst = buf;
  396. memset( buf, 0xff, rdsiz);
  397. for ( y = 0; y < ylim; y++, src+=bWid, dst+=wWid)
  398. memcpy(dst , src , bWid);
  399. free( rbuf);
  400. }
  401. else {
  402. res = ReadFile( bmpFh, (char *)buf, (unsigned int)rdsiz, &nByte, NULL);
  403. if (!res || nByte !=(unsigned int)rdsiz)
  404. goto ERET;
  405. }
  406. *code = getsval( bhd.nInternalCode);
  407. return 0;
  408. ERET:
  409. return -1;
  410. }
  411. int
  412. appendETENBMP( LPBYTE buf, unsigned short code)
  413. {
  414. struct R_CODEELEMENT bhd;
  415. int bmpsiz;
  416. DWORD nByte;
  417. BOOL res;
  418. SetFilePointer( bmpFh, 0L, NULL, FILE_END);
  419. bmpsiz = (width+7)/8*height;
  420. setsval( bhd.nBankID, 1);
  421. setsval( bhd.nInternalCode, code);
  422. res = WriteFile( bmpFh, (LPBYTE)(&bhd), sizeof( bhd), &nByte, NULL);
  423. if (!res || nByte !=sizeof(bhd))
  424. goto ERET;
  425. res = WriteFile( bmpFh, (LPBYTE)buf, (unsigned int)bmpsiz, &nByte, NULL);
  426. if (!res || nByte != (unsigned int)bmpsiz)
  427. goto ERET;
  428. nChar++;
  429. return 0;
  430. ERET:
  431. return -1;
  432. }
  433. int
  434. isETENBMP(TCHAR *path)
  435. {
  436. struct ETENHEADER hdr;
  437. HANDLE fhdl;
  438. fhdl = CreateFile(path,
  439. GENERIC_READ,
  440. FILE_SHARE_READ | FILE_SHARE_WRITE,
  441. NULL,
  442. OPEN_EXISTING,
  443. FILE_ATTRIBUTE_NORMAL,
  444. NULL);
  445. if ( fhdl == INVALID_HANDLE_VALUE)
  446. return -1;
  447. if ( readHdr( fhdl, &hdr)) {
  448. CloseHandle( fhdl);
  449. return -1;
  450. }
  451. CloseHandle( fhdl);
  452. /* check Header size and keyWord*/
  453. if ( hdr.uHeaderSize != sizeof(struct R_ETENHEADER))
  454. goto NO_ETEN;
  455. if ( memcmp(hdr.idUserFontSign, "CWIN_PTN", 8) &&
  456. memcmp(hdr.idUserFontSign, "CMEX_PTN", 8))
  457. goto NO_ETEN;
  458. return 1;
  459. NO_ETEN:
  460. return 0;
  461. }
  462. int
  463. ETENrecTbl( int **recTbl)
  464. {
  465. *recTbl=codep;
  466. return 0;
  467. }
  468. /* EOF */
  469. /* For test +/
  470. static int
  471. dispHdr( struct ETENHEADER *hdr)
  472. {
  473. printf("hdr->uHeaderSize= %d\n", hdr->uHeaderSize );
  474. printf("hdr->idMajor %d\n", hdr->idMajor );
  475. printf("hdr->idMinor %d\n", hdr->idMinor );
  476. printf("hdr->ulCharCout %ld\n", hdr->ulCharCount );
  477. printf("hdr->uCharWidth %d\n", hdr->uCharWidth );
  478. printf(" hdr->uCharHeight %d\n", hdr->uCharHeight );
  479. printf(" hdr->cPatternSize %d\n", hdr->cPatternSize);
  480. printf("hdr->uchBankID %d\n", hdr->uchBankID);
  481. printf("hdr->idInternalBankID %d\n", hdr->idInternalBankID);
  482. printf("hdr->sFontInfo.uInfoSize %d\n", hdr->sFontInfo.uInfoSize);
  483. printf("hdr->sFontInfo.idCP %d\n", hdr->sFontInfo.idCP);
  484. printf("hdr->sFontInfo.idCharSet %d\n", hdr->sFontInfo.idCharSet);
  485. printf("hdr->sFontInfo.fbTypeFace %d\n", hdr->sFontInfo.fbTypeFace);
  486. printf("hdr->sFontInfo.ulCharDefine %ld\n", hdr->sFontInfo.ulCharDefine);
  487. printf("hdr->sFontInfo.uCellWidth %d\n", hdr->sFontInfo.uCellWidth);
  488. printf("hdr->sFontInfo.uCellHeight %d\n", hdr->sFontInfo.uCellHeight);
  489. printf("hdr->sFontInfo.uCharHeight %d\n", hdr->sFontInfo.uCharHeight);
  490. printf("hdr->sFontInfo.uBaseLine %d\n", hdr->sFontInfo.uBaseLine);
  491. printf("hdr->sFontInfo.uUnderLine %d\n", hdr->sFontInfo.uUnderLine);
  492. printf("hdr->sFontInfo.uUnlnHeight %d\n", hdr->sFontInfo.uUnlnHeight);
  493. printf("hdr->sFontInfo.fchStrokeWeight %d\n", hdr->sFontInfo.fchStrokeWeight);
  494. printf("hdr->sFontInfo.fCharStyle %d\n", hdr->sFontInfo.fCharStyle);
  495. printf("hdr->sFontInfo.fbFontAttrib %d\n", hdr->sFontInfo.fbFontAttrib);
  496. printf("hdr->sFontInfo.ulCellWidthMax %ld\n", hdr->sFontInfo.ulCellWidthMax);
  497. printf("hdr->sFontInfo.ulCellHeightMax %ld\n", hdr->sFontInfo.ulCellHeightMax);
  498. }
  499. main( int argc, char *argv[])
  500. {
  501. int fh;
  502. struct ETENHEADER hdr;
  503. fh = _lopen( argv[1], O_RDONLY | O_BINARY);
  504. readHdr( fh, &hdr);
  505. dispHdr( &hdr);
  506. _lclose(fh);
  507. exit(0);
  508. }
  509. /+ */
  510. /* EOF */