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.

594 lines
12 KiB

  1. //
  2. // Copyright (c) 1997-1999 Microsoft Corporation.
  3. //
  4. /*
  5. * Import function for W31JEUDC and ETEN
  6. *----------------------------------------------
  7. * bitmap proccessing steps
  8. * 1. Read bitmap
  9. * 2. Make outline
  10. * 3. Smoothing
  11. * 4. Rasterize -> editting bitmap image
  12. * 5. Make outline
  13. * 6. Smoothing
  14. * 7. Fitting
  15. * 8. Output TTF and bitmap
  16. *
  17. * File proccessing
  18. * 1.Copy .EUF as temp to update
  19. * 2.Copy TTF tables as temp to update
  20. * 3.Make input bitamp code-rec table
  21. * 4.Per glyph proc.
  22. * 5.Replace files
  23. *
  24. * Per glyph proccessing
  25. * 1.judge to merge to make glyph with input bitmap code-rec table.
  26. * 2.merge or make glyphdata and metrics
  27. *
  28. */
  29. #include "stdafx.h"
  30. #include "eudcedit.h"
  31. #pragma pack(2)
  32. extern BOOL SendImportMessage(unsigned int cEUDC, unsigned int nRec);
  33. #include "vdata.h"
  34. #include "ttfstruc.h"
  35. #include "extfunc.h"
  36. #define OUTLSTH 0
  37. #define TMPLSTH 1
  38. #define EUDCCODEBASE ((unsigned short)0xe000)
  39. static void pline(int bmpNo,int sx,int sy,int tx,int ty);
  40. static int rasterize(int lstHdl,int bmpNo, int mesh, int outSiz);
  41. static int initmem(int iSiz,int oSiz);
  42. static void termmem(void);
  43. static int modmem(int iSiz);
  44. int Import(TCHAR *eudcPath, TCHAR *bmpPath,TCHAR *ttfPath,int oWidth,int oHeight,int level, BOOL bIsWin95EUDC);
  45. /* For Import static */
  46. static int iBmpSiz;
  47. static int oBmpSiz;
  48. static BYTE *rBuf, *wkBuf, *refBuf;
  49. static BYTE *oBuf, *owkBuf, *orefBuf;
  50. static int iBmpNo, wkBmpNo, refBmpNo;
  51. static int oBmpNo, owkBmpNo, orefBmpNo;
  52. static int *recTbl=0;
  53. static void
  54. pline( int bmpNo, int sx, int sy, int tx, int ty)
  55. {
  56. int dx, dy;
  57. int dx2, dy2;
  58. int exy;
  59. int tmp;
  60. dx = abs( sx - tx);
  61. dy = abs( sy - ty);
  62. dx2 = dx*2;
  63. dy2 = dy*2;
  64. if ( dx==0) {
  65. if( sy>ty) {
  66. while ( sy>ty) {
  67. sy--;
  68. ReverseRight( bmpNo, sx, sy);
  69. }
  70. }
  71. else if ( sy < ty) {
  72. while ( sy < ty) {
  73. ReverseRight( bmpNo, sx, sy);
  74. sy++;
  75. }
  76. }
  77. }
  78. else if ( dy==0)
  79. ;
  80. /*Loose*/
  81. else if ( dx >= dy){
  82. if (sx > tx) {
  83. tmp = tx;
  84. tx = sx;
  85. sx = tmp;
  86. tmp = ty;
  87. ty = sy;
  88. sy = tmp;
  89. }
  90. exy = -dx ;
  91. if ( sy < ty ) {
  92. while ( sx <= tx) {
  93. exy += dy2;
  94. sx++;
  95. if ( exy > 0) {
  96. exy -= dx2;
  97. if ( sy!=ty)
  98. ReverseRight( bmpNo, sx, sy);
  99. sy++;
  100. }
  101. }
  102. }
  103. else {
  104. while ( sx <= tx) {
  105. exy += dy2;
  106. sx++;
  107. if ( exy > 0) {
  108. exy -= dx2;
  109. sy--;
  110. if ( sy >= ty)
  111. ReverseRight( bmpNo, sx, sy);
  112. }
  113. }
  114. }
  115. /*Steep*/
  116. }
  117. else {
  118. if (sy > ty) {
  119. tmp = tx;
  120. tx = sx;
  121. sx = tmp;
  122. tmp = ty;
  123. ty = sy;
  124. sy = tmp;
  125. }
  126. exy = -dy ;
  127. /* while ( sy <= ty) { */
  128. while ( sy < ty) {
  129. ReverseRight( bmpNo, sx, sy);
  130. exy += dx2;
  131. if ( exy >= 0) {
  132. exy -= dy2;
  133. if ( sx < tx) sx++;
  134. else sx--;
  135. }
  136. sy++;
  137. }
  138. }
  139. }
  140. static int
  141. rasterize( int lstHdl, int bmpNo, int mesh, int outSiz)
  142. /* lstHdl : abs coord*/
  143. {
  144. int nliais, nelm;
  145. int liais;
  146. struct VHEAD *vhead;
  147. struct VDATA *vp;
  148. struct vecdata lvd, cvd;
  149. if ( (nliais = VDGetNCont( lstHdl))<0)
  150. goto ERET;
  151. BMPClear( bmpNo);
  152. if ( VDGetHead( lstHdl, &vhead))
  153. goto ERET;
  154. for ( liais = 0; liais < nliais; liais++) {
  155. nelm = vhead->nPoints;
  156. lvd = vhead->headp->vd;
  157. lvd.x = (lvd.x * outSiz+mesh/2)/mesh;
  158. lvd.y = (lvd.y * outSiz+mesh/2)/mesh;
  159. vp = vhead->headp->next;
  160. while ( nelm-- > 0) {
  161. cvd = vp->vd;
  162. cvd.x = (cvd.x * outSiz+mesh/2)/mesh;
  163. cvd.y = (cvd.y * outSiz+mesh/2)/mesh;
  164. pline( bmpNo, lvd.x, lvd.y, cvd.x, cvd.y);
  165. lvd = cvd;
  166. vp = vp->next;
  167. }
  168. vhead = vhead->next;
  169. }
  170. return 0;
  171. ERET:
  172. return -1;
  173. }
  174. static int
  175. initmem( int iSiz, int oSiz)
  176. {
  177. iBmpSiz = (iSiz+15)/16*2*iSiz;
  178. rBuf = wkBuf = refBuf = 0;
  179. oBuf = owkBuf = orefBuf = 0;
  180. if ( (rBuf = (LPBYTE)malloc( iBmpSiz))==0)
  181. goto ERET;
  182. if ( (wkBuf =(LPBYTE)malloc( iBmpSiz))==0)
  183. goto ERET;
  184. if ( (refBuf =(LPBYTE)malloc( iBmpSiz))==0)
  185. goto ERET;
  186. if ( (iBmpNo = BMPDefine( rBuf, iSiz, iSiz))<0)
  187. goto ERET;
  188. if ( (wkBmpNo = BMPDefine( wkBuf, iSiz, iSiz))<0)
  189. goto ERET;
  190. if ( (refBmpNo = BMPDefine( refBuf, iSiz, iSiz))<0)
  191. goto ERET;
  192. oBmpSiz = (oSiz+15)/16*2*oSiz;
  193. if ( (oBuf = (LPBYTE)malloc( oBmpSiz))==0)
  194. goto ERET;
  195. if ( (owkBuf = (LPBYTE)malloc( oBmpSiz))==0)
  196. goto ERET;
  197. if ( (orefBuf = (LPBYTE)malloc( oBmpSiz))==0)
  198. goto ERET;
  199. if ( (oBmpNo = BMPDefine( oBuf, oSiz, oSiz))<0)
  200. goto ERET;
  201. if ( (owkBmpNo = BMPDefine( owkBuf, oSiz, oSiz))<0)
  202. goto ERET;
  203. if ( (orefBmpNo = BMPDefine( orefBuf, oSiz, oSiz))<0)
  204. goto ERET;
  205. return 0;
  206. ERET:
  207. return -1;
  208. }
  209. static void
  210. termmem()
  211. {
  212. if ( rBuf ) free( rBuf);
  213. if ( refBuf ) free( refBuf);
  214. if ( wkBuf ) free( wkBuf);
  215. if ( oBuf ) free( oBuf);
  216. if ( orefBuf ) free( orefBuf);
  217. if ( owkBuf ) free( owkBuf);
  218. oBuf = wkBuf = refBuf = 0;
  219. rBuf = orefBuf = owkBuf = 0;
  220. recTbl = 0;
  221. }
  222. static int
  223. modmem( int iSiz)
  224. {
  225. free( rBuf);
  226. free( wkBuf);
  227. free( refBuf);
  228. BMPFreDef( iBmpNo);
  229. BMPFreDef( wkBmpNo);
  230. BMPFreDef( refBmpNo);
  231. if ( (rBuf = (LPBYTE)malloc( iBmpSiz))==0)
  232. goto ERET;
  233. if ( (wkBuf = (LPBYTE)malloc( iBmpSiz))==0)
  234. goto ERET;
  235. if ( (refBuf = (LPBYTE)malloc( iBmpSiz))==0)
  236. goto ERET;
  237. if ( (iBmpNo = BMPDefine( rBuf, iSiz, iSiz))<0)
  238. goto ERET;
  239. if ( (wkBmpNo = BMPDefine( wkBuf, iSiz, iSiz))<0)
  240. goto ERET;
  241. if ( (refBmpNo = BMPDefine( refBuf, iSiz, iSiz))<0)
  242. goto ERET;
  243. return 0;
  244. ERET:
  245. return -1;
  246. }
  247. /*********************************************************************
  248. * Make rec-gid table of input bitmap
  249. */
  250. /* */ static int
  251. /* */ makeRecTbl(
  252. /* */ int nRec,
  253. /* */ BOOL bIsWin95EUDC)
  254. /*
  255. * returns : none
  256. *********************************************************************/
  257. {
  258. int sts;
  259. if ( CountryInfo.LangID == EUDC_JPN || bIsWin95EUDC)
  260. sts = W31JrecTbl(&recTbl, bIsWin95EUDC);
  261. else
  262. sts = ETENrecTbl(&recTbl);
  263. return sts;
  264. }
  265. static int
  266. impSub(
  267. int rec,
  268. struct BBX *bbx,
  269. short uPEm,
  270. int oWidth, /* output bmp width */
  271. int oHeight, /* output bmp height(==width) */
  272. struct SMOOTHPRM *prm,
  273. BOOL bIsWin95EUDC)
  274. {
  275. int rdsiz;
  276. int width, height;
  277. char UserFontSign[8];
  278. WORD BankID;
  279. unsigned short code;
  280. int sts;
  281. int nRec;
  282. int nGlyph;
  283. BOOL bUnicode;
  284. if (!bbx)
  285. {
  286. sts = -1;
  287. goto ERET;
  288. }
  289. /* Read EUDC Bitmap */
  290. if ( CountryInfo.LangID == EUDC_JPN || bIsWin95EUDC) {
  291. rdsiz = GetW31JBMPRec( rec, (LPBYTE)rBuf, iBmpSiz, &width, &height, &code);
  292. if ( rdsiz < 0)
  293. {
  294. sts = -1;
  295. goto ERET;
  296. }
  297. else if ( rdsiz==0)
  298. return 0;
  299. if ( rdsiz > iBmpSiz) {
  300. iBmpSiz = rdsiz;
  301. modmem( width);
  302. if ( GetW31JBMPRec( rec, (LPBYTE)rBuf, iBmpSiz,
  303. &width, &height, &code)<0)
  304. {
  305. sts = -1;
  306. goto ERET;
  307. }
  308. }
  309. }
  310. else {
  311. if ( getETENBMPInf( &nRec, &nGlyph, &width, &height,
  312. UserFontSign, &BankID)) {
  313. sts = -2;
  314. goto ERET;
  315. }
  316. iBmpSiz = (width+7)/8*height;
  317. if (readETENBMPRec( rec, (LPBYTE)rBuf, iBmpSiz, &code)) {
  318. sts = -3;
  319. goto ERET;
  320. }
  321. }
  322. if( !memcmp( UserFontSign,"CMEX_PTN", 8) && BankID == 0x8001 || bIsWin95EUDC)
  323. bUnicode = TRUE;
  324. else bUnicode = FALSE;
  325. /* vectorize */
  326. if( memcmp( UserFontSign,"CMEX_PTN", 8))
  327. BMPReverse( iBmpNo);
  328. if ( (BMPMkCont( iBmpNo, wkBmpNo, refBmpNo, OUTLSTH))<0) {
  329. sts = -4;
  330. goto ERET;
  331. }
  332. /* Smoothing */
  333. if (SmoothLight( OUTLSTH, TMPLSTH, width, height, oWidth*4, 16)) {
  334. sts = -5;
  335. goto ERET;
  336. }
  337. rasterize( OUTLSTH, oBmpNo, oWidth*4, oWidth);
  338. /* Write Bitmap */
  339. BMPReverse( oBmpNo);
  340. if (PutW31JEUDCFont(code,(LPBYTE)oBuf, oWidth, oWidth, bUnicode)) {
  341. sts = -6;
  342. goto ERET;
  343. }
  344. BMPReverse( oBmpNo);
  345. if ( BMPMkCont( oBmpNo, owkBmpNo, orefBmpNo, OUTLSTH)<0) {
  346. sts = -7;
  347. goto ERET;
  348. }
  349. if (SmoothVector( OUTLSTH, TMPLSTH, oWidth, oHeight, oWidth*4,prm , 16)) {
  350. sts = -8;
  351. goto ERET;
  352. }
  353. if (ConvMesh( OUTLSTH,oWidth*4, uPEm)) {
  354. sts = -9;
  355. goto ERET;
  356. }
  357. if ( RemoveFp( OUTLSTH, uPEm, 16)) {
  358. sts = -10;
  359. goto ERET;
  360. }
  361. if ( toTTFFrame( OUTLSTH, bbx)) {
  362. sts = -11;
  363. goto ERET;
  364. }
  365. if( !bUnicode)
  366. code = sjisToUniEUDC( code);
  367. /* write TTF */
  368. if ( TTFAppend( code, bbx, OUTLSTH)) {
  369. sts = -12;
  370. goto ERET;
  371. }
  372. return 0;
  373. ERET:
  374. return sts;
  375. }
  376. /*********************************************************************
  377. * Import WIN31J EUDC or ETEN contiguous
  378. */
  379. /* */ int
  380. /* */ Import(
  381. /* */ TCHAR *eudcPath, /* W31J EUDC Bitmap .fon*/
  382. /* */ TCHAR *bmpPath, /* Win95 EUDCEDIT bitmap .euf*/
  383. /* */ TCHAR *ttfPath, /* TTF EUDC .ttf */
  384. /* */ int oWidth, /* output bmp width */
  385. /* */ int oHeight, /* output bmp height(==width) */
  386. /* */ int level,
  387. /* */ BOOL bIsWin95EUDC)
  388. /*
  389. * returns : 0, -1
  390. *********************************************************************/
  391. {
  392. int nRec;
  393. int rec;
  394. int width, height;
  395. char UserFontSign[8];
  396. short uPEm;
  397. struct BBX bbx;
  398. WORD BankID;
  399. unsigned short maxC;
  400. TCHAR tmpPath[MAX_PATH];
  401. TCHAR savPath[MAX_PATH];
  402. HANDLE orgFh=INVALID_HANDLE_VALUE;
  403. int sts;
  404. struct SMOOTHPRM prm;
  405. int nGlyph;
  406. int gCnt;
  407. int cancelFlg;
  408. if ((!eudcPath) || (!bmpPath) || (!ttfPath))
  409. {
  410. return -1;
  411. }
  412. // orgFh = 0;
  413. BMPInit();
  414. VDInit();
  415. makeUniCodeTbl();
  416. maxC = getMaxUniCode();
  417. prm.SmoothLevel = level;
  418. prm.UseConic = 1;
  419. TTFTmpPath( ttfPath, tmpPath);
  420. if ( TTFImpCopy( ttfPath, tmpPath))
  421. goto ERET;
  422. /* Open W31J EUDC bitmap font file userfont.fon or CWin31 ETEN*/
  423. if ( CountryInfo.LangID == EUDC_JPN || bIsWin95EUDC) {
  424. if (OpenW31JBMP( eudcPath, 0))
  425. goto ERET;
  426. }
  427. else {
  428. if (openETENBMP( eudcPath, 0))
  429. goto ERET;
  430. }
  431. /* Open EUDCEDIT .EUF File */
  432. if ( OpenW31JEUDC( bmpPath))
  433. {
  434. if (creatW31JEUDC(bmpPath))
  435. goto ERET;
  436. else
  437. if (OpenW31JEUDC( bmpPath))
  438. goto ERET;
  439. }
  440. if ( CountryInfo.LangID == EUDC_JPN || bIsWin95EUDC) {
  441. /* get number of record */
  442. if ( GetW31JBMPnRecs(&nRec, &nGlyph, &width, &height))
  443. goto ERET;
  444. iBmpSiz = (width + 7)/8 * height;
  445. }
  446. else{
  447. if ( getETENBMPInf( &nRec, &nGlyph, &width, &height,
  448. UserFontSign, &BankID))
  449. goto ERET;
  450. iBmpSiz = (width+7)/8*height;
  451. }
  452. /* Limit nRec */
  453. if ( nRec > (int)( maxC-EUDCCODEBASE+1))
  454. nRec = (int)( maxC-EUDCCODEBASE+1);
  455. initmem( width, oWidth);
  456. if ( makeRecTbl( nRec, bIsWin95EUDC))
  457. goto ERET;
  458. /* Get BBX */
  459. if ( TTFGetEUDCBBX( ttfPath, &bbx, &uPEm))
  460. goto ERET;
  461. /* Open temporaly */
  462. if ( TTFOpen( tmpPath))
  463. goto ERET;
  464. /* Open Original */
  465. orgFh = CreateFile(ttfPath,
  466. GENERIC_READ,
  467. FILE_SHARE_READ | FILE_SHARE_WRITE,
  468. NULL,
  469. OPEN_EXISTING,
  470. FILE_ATTRIBUTE_NORMAL,
  471. NULL);
  472. if ( orgFh == INVALID_HANDLE_VALUE)
  473. goto ERET;
  474. /* copy missing glyph*/
  475. TTFImpGlyphCopy(orgFh, 0);
  476. /* per glyph */
  477. gCnt = 0;
  478. cancelFlg = 0;
  479. for ( rec = 0; rec < nRec; rec++) {
  480. if ( recTbl[rec]>= 0) {
  481. gCnt++;
  482. if ( gCnt < nGlyph) {
  483. if (SendImportMessage((unsigned int)gCnt,
  484. (unsigned int)nGlyph)==0)
  485. cancelFlg=1;
  486. }
  487. }
  488. if ( cancelFlg==0 && recTbl[rec]>= 0) {
  489. if ((sts = impSub(recTbl[rec],&bbx,uPEm,oWidth, oHeight,&prm,bIsWin95EUDC))<0)
  490. goto ERET;
  491. else if (sts >0)
  492. break;
  493. }
  494. else {
  495. if (TTFImpGlyphCopy(orgFh, rec+2))
  496. goto ERET;
  497. }
  498. }
  499. SendImportMessage((unsigned int)nGlyph, (unsigned int)nGlyph);
  500. if ( TTFImpTerm(orgFh, rec+2))
  501. goto ERET;
  502. CloseHandle( orgFh);
  503. if ( TTFClose())
  504. goto ERET;
  505. if ( CountryInfo.LangID == EUDC_JPN || bIsWin95EUDC) {
  506. if (CloseW31JBMP())
  507. goto ERET;
  508. }
  509. else {
  510. if (closeETENBMP())
  511. goto ERET;
  512. }
  513. CloseW31JEUDC();
  514. /* Replace file */
  515. TTFTmpPath( ttfPath, savPath);
  516. if ( DeleteFile( savPath)==0)
  517. goto ERET;
  518. if (MoveFile( ttfPath, savPath)==0)
  519. goto ERET;
  520. if (MoveFile( tmpPath, ttfPath)==0)
  521. goto ERET;
  522. if ( DeleteFile( savPath)==0)
  523. goto ERET;
  524. VDTerm();
  525. termmem();
  526. return 0;
  527. ERET:
  528. if ( orgFh != INVALID_HANDLE_VALUE) {
  529. CloseHandle( orgFh);
  530. orgFh = INVALID_HANDLE_VALUE;
  531. }
  532. TTFClose();
  533. CloseW31JBMP();
  534. CloseW31JEUDC();
  535. VDTerm();
  536. termmem();
  537. return -1;
  538. }
  539. /* EOF */