text.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654
  1. /* -*- mode: c; tab-width: 4; c-basic-offset: 3; c-file-style: "linux" -*- */
  2. //
  3. // Copyright (c) 2008, Wei Mingzhi <whistler_wmz@users.sf.net>.
  4. // All rights reserved.
  5. //
  6. // Portions based on PALx Project by palxex.
  7. // Copyright (c) 2006-2008, Pal Lockheart <palxex@gmail.com>.
  8. //
  9. // This file is part of SDLPAL.
  10. //
  11. // SDLPAL is free software: you can redistribute it and/or modify
  12. // it under the terms of the GNU General Public License as published by
  13. // the Free Software Foundation, either version 3 of the License, or
  14. // (at your option) any later version.
  15. //
  16. // This program is distributed in the hope that it will be useful,
  17. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. // GNU General Public License for more details.
  20. //
  21. // You should have received a copy of the GNU General Public License
  22. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. //
  24. // Modified by Lou Yihua <louyihua@21cn.com> with Unicode support, 2015
  25. //
  26. #include "main.h"
  27. #define FONT_COLOR_DEFAULT 0x4F
  28. #define FONT_COLOR_YELLOW 0x2D
  29. #define FONT_COLOR_RED 0x1A
  30. #define FONT_COLOR_CYAN 0x8D
  31. #define FONT_COLOR_CYAN_ALT 0x8C
  32. BOOL g_fUpdatedInBattle = FALSE;
  33. #define MESSAGE_MAX_BUFFER_SIZE 512
  34. #define INCLUDE_CODEPAGE_H
  35. #include "codepage.h"
  36. #ifndef PAL_CLASSIC
  37. static const WCHAR* gc_rgszAdditionalWords[CP_MAX][6] = {
  38. { L"\x6230\x9B25\x901F\x5EA6", L"\x4E00", L"\x4E8C", L"\x4E09", L"\x56DB", L"\x4E94" },
  39. { L"\x6218\x6597\x901F\x5EA6", L"\x4E00", L"\x4E8C", L"\x4E09", L"\x56DB", L"\x4E94" },
  40. { L"\x6226\x95D8\x901F\x5EA6", L"\x4E00", L"\x4E8C", L"\x4E09", L"\x56DB", L"\x4E94" },
  41. };
  42. static const WCHAR** g_rgszAdditionalWords;
  43. #endif
  44. typedef struct tagTEXTLIB
  45. {
  46. LPWSTR *lpWordBuf;
  47. LPWSTR *lpMsgBuf;
  48. int **lpIndexBuf;
  49. int nWords;
  50. int nMsgs;
  51. int nIndices;
  52. int nCurrentDialogLine;
  53. BYTE bCurrentFontColor;
  54. PAL_POS posIcon;
  55. PAL_POS posDialogTitle;
  56. PAL_POS posDialogText;
  57. BYTE bDialogPosition;
  58. BYTE bIcon;
  59. int iDelayTime;
  60. BOOL fUserSkip;
  61. BOOL fPlayingRNG;
  62. BYTE bufDialogIcons[282];
  63. } TEXTLIB, *LPTEXTLIB;
  64. static TEXTLIB g_TextLib;
  65. PAL_FORCE_INLINE int
  66. PAL_ParseLine(
  67. char *line,
  68. char **value,
  69. int *length
  70. )
  71. {
  72. //
  73. // Remove the leading spaces
  74. //
  75. while (*line && iswspace(*line)) line++;
  76. //
  77. // Skip comments starting with '#'
  78. //
  79. if (*line && *line != '#')
  80. {
  81. //
  82. // Split the index and value
  83. //
  84. LPSTR val = strchr(line, '=');
  85. if (val)
  86. {
  87. //
  88. // Remove the trailing spaces
  89. //
  90. LPSTR end = line + strlen(line);
  91. int index;
  92. if (end > line && end[-1] == '\n') *(--end) = 0;
  93. while (end > line && iswspace(end[-1])) *(--end) = 0;
  94. //
  95. // Parse the index and pass out value
  96. //
  97. if (sscanf(line, "%d", &index) == 1)
  98. {
  99. *value = val + 1;
  100. *length = end - *value;
  101. return index;
  102. }
  103. }
  104. }
  105. return 0;
  106. }
  107. PAL_FORCE_INLINE char *
  108. PAL_ReadOneLine(
  109. char *temp,
  110. int limit,
  111. FILE *fp
  112. )
  113. {
  114. if (fgets(temp, limit, fp))
  115. {
  116. int n = strlen(temp);
  117. if (n == limit - 1 && temp[n - 1] != '\n' && !feof(fp))
  118. {
  119. // Line too long, try to read it as a whole
  120. int nn = 2;
  121. char *tmp = strdup(temp);
  122. while (!feof(fp))
  123. {
  124. if (!(tmp = (char *)realloc(tmp, nn * limit)))
  125. {
  126. TerminateOnError("PAL_ReadOneLine(): failed to allocate memory for long line!");
  127. }
  128. if (fgets(tmp + n, limit + 1, fp))
  129. {
  130. n += strlen(tmp + n);
  131. if (n < limit - 1 || temp[n - 1] == '\n')
  132. break;
  133. else
  134. nn++;
  135. }
  136. }
  137. if (tmp[n - 1] == '\n') tmp[n - 1] = 0;
  138. return tmp;
  139. }
  140. else
  141. {
  142. if (n > 0 && temp[n - 1] == '\n') temp[n - 1] = 0;
  143. return temp;
  144. }
  145. }
  146. else
  147. return NULL;
  148. }
  149. static int
  150. PAL_ReadMessageFile(
  151. FILE *fp
  152. )
  153. {
  154. char temp[MESSAGE_MAX_BUFFER_SIZE];
  155. struct _msg_entry
  156. {
  157. struct _msg_entry *next;
  158. wchar_t *value;
  159. } *cur_val;
  160. struct _msg_list_entry
  161. {
  162. struct _msg_list_entry *next;
  163. struct _msg_entry *value;
  164. int index;
  165. int count;
  166. } *head = NULL, *item;
  167. struct _word_list_entry
  168. {
  169. struct _word_list_entry *next;
  170. wchar_t *value;
  171. } whead = { NULL, NULL }, *witem = NULL;
  172. enum _message_state
  173. {
  174. ST_OUTSIDE,
  175. ST_DIALOG,
  176. ST_WORD
  177. } state = ST_OUTSIDE;
  178. int idx_cnt = 0, msg_cnt = 0, word_cnt = 0, sid, eid = -1;
  179. while (!feof(fp))
  180. {
  181. char *buffer;
  182. if (buffer = PAL_ReadOneLine(temp, MESSAGE_MAX_BUFFER_SIZE, fp))
  183. {
  184. switch(state)
  185. {
  186. case ST_OUTSIDE:
  187. //
  188. // Skip comments starting with '#'
  189. //
  190. if (*buffer && *buffer != '#')
  191. {
  192. if (strncmp(buffer, "[BEGIN MESSAGE]", 15) == 0 &&
  193. sscanf(buffer + 15, "%d", &sid) == 1 && sid > eid)
  194. {
  195. state = ST_DIALOG;
  196. //
  197. // First save values (converted wide string) into a linked list
  198. //
  199. if (head)
  200. {
  201. item->next = (struct _msg_list_entry *)malloc(sizeof(struct _msg_list_entry));
  202. item = item->next;
  203. }
  204. else
  205. {
  206. head = (struct _msg_list_entry *)malloc(sizeof(struct _msg_list_entry));
  207. item = head;
  208. }
  209. item->value = NULL; item->index = sid;
  210. item->count = 0; cur_val = NULL;
  211. if (idx_cnt < item->index) idx_cnt = item->index;
  212. }
  213. else if (strncmp(buffer, "[BEGIN WORDS]", 13) == 0 && !witem)
  214. {
  215. state = ST_WORD;
  216. //
  217. // First save values (converted wide string) into a linked list
  218. //
  219. witem = &whead;
  220. }
  221. else
  222. {
  223. // Just ignore invalid lines
  224. #ifdef ENABLE_LOG
  225. UTIL_WriteLog(LOG_ERR, "PAL_ReadMessageFile(): encounter invalid line '%s'!\n", line);
  226. #endif
  227. }
  228. }
  229. break;
  230. case ST_DIALOG:
  231. //
  232. // Check if to end one dialog
  233. //
  234. if (strncmp(buffer, "[END MESSAGE]", 13) == 0 &&
  235. sscanf(buffer + 13, "%d", &eid) == 1 && eid >= sid)
  236. {
  237. // End dialog
  238. state = ST_OUTSIDE;
  239. }
  240. else
  241. {
  242. if (cur_val)
  243. {
  244. cur_val->next = (struct _msg_entry *)malloc(sizeof(struct _msg_entry));
  245. cur_val = cur_val->next;
  246. }
  247. else
  248. cur_val = (struct _msg_entry *)malloc(sizeof(struct _msg_entry));
  249. if (strncmp(buffer, "[CLEAR MESSAGE]", 15) == 0)
  250. {
  251. cur_val->value = NULL;
  252. }
  253. else
  254. {
  255. int len = PAL_MultiByteToWideCharCP(CP_UTF_8, buffer, -1, NULL, 0);
  256. cur_val->value = (wchar_t *)malloc(len * sizeof(wchar_t));
  257. PAL_MultiByteToWideCharCP(CP_UTF_8, buffer, -1, cur_val->value, len);
  258. msg_cnt++;
  259. }
  260. if (!item->value) item->value = cur_val;
  261. cur_val->next = NULL; item->count++;
  262. }
  263. break;
  264. case ST_WORD:
  265. //
  266. // Check if to end word list
  267. //
  268. if (strncmp(buffer, "[END WORDS]", 11) == 0)
  269. {
  270. // End word list
  271. state = ST_OUTSIDE;
  272. }
  273. else
  274. {
  275. char *v;
  276. int l, i = PAL_ParseLine(buffer, &v, &l);
  277. if (i > 0)
  278. {
  279. int len = PAL_MultiByteToWideCharCP(CP_UTF_8, v, -1, NULL, 0);
  280. struct _word_list_entry *val = (struct _word_list_entry *)malloc(sizeof(struct _word_list_entry));
  281. val->value = (wchar_t *)malloc(len * sizeof(wchar_t));
  282. PAL_MultiByteToWideCharCP(CP_UTF_8, v, -1, val->value, len);
  283. val->next = NULL; witem->next = val; witem = witem->next;
  284. if (word_cnt < i) word_cnt = i;
  285. }
  286. }
  287. break;
  288. default:
  289. TerminateOnError("PAL_ReadMessageFile(): Reached an unknown state. Something really wrong may have happened!");
  290. break;
  291. }
  292. if (buffer != temp) free(buffer);
  293. }
  294. }
  295. if (msg_cnt > 0)
  296. {
  297. //
  298. // Move values from linked list to array
  299. //
  300. int idx_msg = 1;
  301. g_TextLib.nIndices = (idx_cnt += 1);
  302. g_TextLib.nMsgs = (msg_cnt += 1);
  303. g_TextLib.lpIndexBuf = (int **)calloc(idx_cnt, sizeof(int *));
  304. g_TextLib.lpMsgBuf = (LPWSTR *)calloc(msg_cnt, sizeof(LPWSTR));
  305. for (item = head; item; )
  306. {
  307. struct _msg_list_entry *temp = item->next;
  308. struct _msg_entry *msg = item->value;
  309. int index = 0;
  310. g_TextLib.lpIndexBuf[item->index] = (int *)calloc(item->count + 1, sizeof(int));
  311. while (msg)
  312. {
  313. struct _msg_entry *tmp = msg->next;
  314. if (msg->value)
  315. {
  316. g_TextLib.lpIndexBuf[item->index][index++] = idx_msg;
  317. g_TextLib.lpMsgBuf[idx_msg++] = msg->value;
  318. }
  319. else
  320. g_TextLib.lpIndexBuf[item->index][index++] = 0;
  321. free(msg); msg = tmp;
  322. }
  323. g_TextLib.lpIndexBuf[item->index][item->count] = -1;
  324. free(item); item = temp;
  325. }
  326. }
  327. if (word_cnt > 0)
  328. {
  329. //
  330. // Move values from linked list to array
  331. //
  332. int index = 1;
  333. g_TextLib.nWords = (word_cnt += 1);
  334. g_TextLib.lpWordBuf = (LPWSTR *)calloc(word_cnt, sizeof(LPWSTR));
  335. for (witem = whead.next; witem; )
  336. {
  337. struct _word_list_entry *temp = witem->next;
  338. g_TextLib.lpWordBuf[index++] = witem->value;
  339. free(witem); witem = temp;
  340. }
  341. }
  342. fclose(fp);
  343. return (msg_cnt > 0 && word_cnt > 0) ? 1 : 0;
  344. }
  345. INT
  346. PAL_InitText(
  347. VOID
  348. )
  349. /*++
  350. Purpose:
  351. Initialize the in-game texts.
  352. Parameters:
  353. None.
  354. Return value:
  355. 0 = success.
  356. -1 = memory allocation error.
  357. --*/
  358. {
  359. if (gpGlobals->pszMsgName)
  360. {
  361. //
  362. // Open the message, index and word data files.
  363. //
  364. FILE *fp = UTIL_OpenRequiredFileForMode(gpGlobals->pszMsgName, "r");
  365. //
  366. // Read the contents of the message, index and word data files.
  367. //
  368. if (!PAL_ReadMessageFile(fp))
  369. {
  370. return -1;
  371. }
  372. else
  373. {
  374. DWORD dwWordLength = 0;
  375. int i;
  376. for (i = 1; i < g_TextLib.nWords; i++)
  377. {
  378. LPWSTR ptr = g_TextLib.lpWordBuf[i];
  379. DWORD n = 0;
  380. while (*ptr) n += PAL_CharWidth(*ptr++) >> 3;
  381. if (dwWordLength < n) dwWordLength = n;
  382. }
  383. gpGlobals->dwWordLength = dwWordLength;
  384. }
  385. }
  386. else
  387. {
  388. FILE *fpMsg, *fpWord;
  389. DWORD *offsets;
  390. LPWSTR tmp;
  391. LPBYTE temp;
  392. int wpos, wlen, i;
  393. //
  394. // Open the message and word data files.
  395. //
  396. fpMsg = UTIL_OpenRequiredFile("m.msg");
  397. fpWord = UTIL_OpenRequiredFile("word.dat");
  398. //
  399. // See how many words we have
  400. //
  401. fseek(fpWord, 0, SEEK_END);
  402. i = ftell(fpWord);
  403. //
  404. // Each word has 10 or 16 bytes
  405. //
  406. g_TextLib.nWords = (i + (gpGlobals->dwWordLength - 1)) / gpGlobals->dwWordLength;
  407. //
  408. // Read the words
  409. //
  410. temp = (LPBYTE)malloc(i);
  411. if (temp == NULL)
  412. {
  413. fclose(fpWord);
  414. fclose(fpMsg);
  415. return -1;
  416. }
  417. fseek(fpWord, 0, SEEK_SET);
  418. fread(temp, i, 1, fpWord);
  419. //
  420. // Close the words file
  421. //
  422. fclose(fpWord);
  423. // Split the words and do code page conversion
  424. for (i = 0, wlen = 0; i < g_TextLib.nWords; i++)
  425. {
  426. int base = i * gpGlobals->dwWordLength;
  427. int pos = base + gpGlobals->dwWordLength - 1;
  428. while (pos >= base && temp[pos] == ' ') temp[pos--] = 0;
  429. wlen += PAL_MultiByteToWideChar((LPCSTR)temp + base, gpGlobals->dwWordLength, NULL, 0) + 1;
  430. }
  431. g_TextLib.lpWordBuf = (LPWSTR*)malloc(g_TextLib.nWords * sizeof(LPWSTR));
  432. tmp = (LPWSTR)malloc(wlen * sizeof(WCHAR));
  433. for (i = 0, wpos = 0; i < g_TextLib.nWords; i++)
  434. {
  435. int l;
  436. g_TextLib.lpWordBuf[i] = tmp + wpos;
  437. l = PAL_MultiByteToWideChar((LPCSTR)temp + i * gpGlobals->dwWordLength, gpGlobals->dwWordLength, g_TextLib.lpWordBuf[i], wlen - wpos);
  438. if (l > 0 && g_TextLib.lpWordBuf[i][l - 1] == '1')
  439. g_TextLib.lpWordBuf[i][l - 1] = 0;
  440. g_TextLib.lpWordBuf[i][l] = 0;
  441. wpos += l + 1;
  442. }
  443. free(temp);
  444. //
  445. // Read the message offsets. The message offsets are in SSS.MKF #3
  446. //
  447. i = PAL_MKFGetChunkSize(3, gpGlobals->f.fpSSS) / sizeof(DWORD);
  448. g_TextLib.nMsgs = i - 1;
  449. offsets = (LPDWORD)malloc(i * sizeof(DWORD));
  450. if (offsets == NULL)
  451. {
  452. free(g_TextLib.lpWordBuf);
  453. fclose(fpMsg);
  454. return -1;
  455. }
  456. PAL_MKFReadChunk((LPBYTE)(offsets), i * sizeof(DWORD), 3, gpGlobals->f.fpSSS);
  457. //
  458. // Read the messages.
  459. //
  460. fseek(fpMsg, 0, SEEK_END);
  461. i = ftell(fpMsg);
  462. temp = (LPBYTE)malloc(i);
  463. if (temp == NULL)
  464. {
  465. free(offsets);
  466. free(g_TextLib.lpWordBuf[0]);
  467. free(g_TextLib.lpWordBuf);
  468. fclose(fpMsg);
  469. return -1;
  470. }
  471. fseek(fpMsg, 0, SEEK_SET);
  472. fread(temp, 1, i, fpMsg);
  473. fclose(fpMsg);
  474. // Split messages and do code page conversion here
  475. for (i = 0, wlen = 0; i < g_TextLib.nMsgs; i++)
  476. {
  477. wlen += PAL_MultiByteToWideChar((LPCSTR)temp + SDL_SwapLE32(offsets[i]), SDL_SwapLE32(offsets[i + 1]) - SDL_SwapLE32(offsets[i]), NULL, 0) + 1;
  478. }
  479. g_TextLib.lpMsgBuf = (LPWSTR*)malloc(g_TextLib.nMsgs * sizeof(LPWSTR));
  480. tmp = (LPWSTR)malloc(wlen * sizeof(WCHAR));
  481. for (i = 0, wpos = 0; i < g_TextLib.nMsgs; i++)
  482. {
  483. int l;
  484. g_TextLib.lpMsgBuf[i] = tmp + wpos;
  485. l = PAL_MultiByteToWideChar((LPCSTR)temp + SDL_SwapLE32(offsets[i]), SDL_SwapLE32(offsets[i + 1]) - SDL_SwapLE32(offsets[i]), g_TextLib.lpMsgBuf[i], wlen - wpos);
  486. g_TextLib.lpMsgBuf[i][l] = 0;
  487. wpos += l + 1;
  488. }
  489. free(temp);
  490. free(offsets);
  491. g_TextLib.lpIndexBuf = NULL;
  492. }
  493. #ifndef PAL_CLASSIC
  494. g_rgszAdditionalWords = gc_rgszAdditionalWords[gpGlobals->iCodePage];
  495. #endif
  496. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  497. g_TextLib.bIcon = 0;
  498. g_TextLib.posIcon = 0;
  499. g_TextLib.nCurrentDialogLine = 0;
  500. g_TextLib.iDelayTime = 3;
  501. g_TextLib.posDialogTitle = PAL_XY(12, 8);
  502. g_TextLib.posDialogText = PAL_XY(44, 26);
  503. g_TextLib.bDialogPosition = kDialogUpper;
  504. g_TextLib.fUserSkip = FALSE;
  505. PAL_MKFReadChunk(g_TextLib.bufDialogIcons, 282, 12, gpGlobals->f.fpDATA);
  506. return 0;
  507. }
  508. VOID
  509. PAL_FreeText(
  510. VOID
  511. )
  512. /*++
  513. Purpose:
  514. Free the memory used by the texts.
  515. Parameters:
  516. None.
  517. Return value:
  518. None.
  519. --*/
  520. {
  521. int i;
  522. if (g_TextLib.lpMsgBuf != NULL)
  523. {
  524. if (gpGlobals->pszMsgName)
  525. for(i = 0; i < g_TextLib.nMsgs; i++) free(g_TextLib.lpMsgBuf[i]);
  526. else
  527. free(g_TextLib.lpMsgBuf[0]);
  528. free(g_TextLib.lpMsgBuf);
  529. g_TextLib.lpMsgBuf = NULL;
  530. }
  531. if (g_TextLib.lpWordBuf != NULL)
  532. {
  533. if (gpGlobals->pszMsgName)
  534. for(i = 0; i < g_TextLib.nWords; i++) free(g_TextLib.lpWordBuf[i]);
  535. else
  536. free(g_TextLib.lpWordBuf[0]);
  537. free(g_TextLib.lpWordBuf);
  538. g_TextLib.lpWordBuf = NULL;
  539. }
  540. if (g_TextLib.lpIndexBuf != NULL)
  541. {
  542. if (gpGlobals->pszMsgName)
  543. for(i = 0; i < g_TextLib.nIndices; i++) free(g_TextLib.lpIndexBuf[i]);
  544. else
  545. free(g_TextLib.lpIndexBuf[0]);
  546. free(g_TextLib.lpIndexBuf);
  547. g_TextLib.lpIndexBuf = NULL;
  548. }
  549. }
  550. LPCWSTR
  551. PAL_GetWord(
  552. int iNumWord
  553. )
  554. /*++
  555. Purpose:
  556. Get the specified word.
  557. Parameters:
  558. [IN] wNumWord - the number of the requested word.
  559. Return value:
  560. Pointer to the requested word. NULL if not found.
  561. --*/
  562. {
  563. #ifndef PAL_CLASSIC
  564. if (wNumWord >= PAL_ADDITIONAL_WORD_FIRST)
  565. {
  566. return g_rgszAdditionalWords[wNumWord - PAL_ADDITIONAL_WORD_FIRST];
  567. }
  568. #endif
  569. return (iNumWord >= g_TextLib.nWords || !g_TextLib.lpWordBuf[iNumWord]) ? L"" : g_TextLib.lpWordBuf[iNumWord];
  570. }
  571. LPCWSTR
  572. PAL_GetMsg(
  573. int iNumMsg
  574. )
  575. /*++
  576. Purpose:
  577. Get the specified message.
  578. Parameters:
  579. [IN] wNumMsg - the number of the requested message.
  580. Return value:
  581. Pointer to the requested message. NULL if not found.
  582. --*/
  583. {
  584. return (iNumMsg >= g_TextLib.nMsgs || !g_TextLib.lpMsgBuf[iNumMsg]) ? L"" : g_TextLib.lpMsgBuf[iNumMsg];
  585. }
  586. int
  587. PAL_GetMsgNum(
  588. int iIndex,
  589. int iOrder
  590. )
  591. /*++
  592. Purpose:
  593. Get the number of specified message from index & order.
  594. Parameters:
  595. [IN] iMsgIndex - index.
  596. [IN] iOrder - order inside the index.
  597. Return value:
  598. The number of message. Zero means pausing for key, and -1 means end.
  599. --*/
  600. {
  601. return (iIndex >= g_TextLib.nMsgs || !g_TextLib.lpIndexBuf[iIndex]) ? -1 : g_TextLib.lpIndexBuf[iIndex][iOrder];
  602. }
  603. VOID
  604. PAL_DrawText(
  605. LPCWSTR lpszText,
  606. PAL_POS pos,
  607. BYTE bColor,
  608. BOOL fShadow,
  609. BOOL fUpdate
  610. )
  611. /*++
  612. Purpose:
  613. Draw text on the screen.
  614. Parameters:
  615. [IN] lpszText - the text to be drawn.
  616. [IN] pos - Position of the text.
  617. [IN] bColor - Color of the text.
  618. [IN] fShadow - TRUE if the text is shadowed or not.
  619. [IN] fUpdate - TRUE if update the screen area.
  620. Return value:
  621. None.
  622. --*/
  623. {
  624. SDL_Rect rect, urect;
  625. rect.x = PAL_X(pos);
  626. rect.y = PAL_Y(pos);
  627. urect.x = rect.x;
  628. urect.y = rect.y;
  629. urect.h = gpGlobals->fUseEmbeddedFonts ? 16 : 17;
  630. urect.w = 0;
  631. while (*lpszText)
  632. {
  633. //
  634. // Draw the character
  635. //
  636. int char_width = PAL_CharWidth(*lpszText);
  637. if (fShadow)
  638. {
  639. PAL_DrawCharOnSurface(*lpszText, gpScreen, PAL_XY(rect.x + 1, rect.y + 1), 0);
  640. PAL_DrawCharOnSurface(*lpszText, gpScreen, PAL_XY(rect.x + 1, rect.y), 0);
  641. }
  642. PAL_DrawCharOnSurface(*lpszText, gpScreen, PAL_XY(rect.x, rect.y), bColor);
  643. lpszText++;
  644. rect.x += char_width;
  645. urect.w += char_width;
  646. }
  647. //
  648. // Update the screen area
  649. //
  650. if (fUpdate && urect.w > 0)
  651. {
  652. if (gpGlobals->fIsWIN95)
  653. {
  654. urect.w++;
  655. if (urect.x + urect.w > 320)
  656. {
  657. urect.w = 320 - urect.x;
  658. }
  659. }
  660. VIDEO_UpdateScreen(&urect);
  661. }
  662. }
  663. VOID
  664. PAL_DialogSetDelayTime(
  665. INT iDelayTime
  666. )
  667. /*++
  668. Purpose:
  669. Set the delay time for dialog.
  670. Parameters:
  671. [IN] iDelayTime - the delay time to be set.
  672. Return value:
  673. None.
  674. --*/
  675. {
  676. g_TextLib.iDelayTime = iDelayTime;
  677. }
  678. VOID
  679. PAL_StartDialog(
  680. BYTE bDialogLocation,
  681. BYTE bFontColor,
  682. INT iNumCharFace,
  683. BOOL fPlayingRNG
  684. )
  685. /*++
  686. Purpose:
  687. Start a new dialog.
  688. Parameters:
  689. [IN] bDialogLocation - the location of the text on the screen.
  690. [IN] bFontColor - the font color of the text.
  691. [IN] iNumCharFace - number of the character face in RGM.MKF.
  692. [IN] fPlayingRNG - whether we are playing a RNG video or not.
  693. Return value:
  694. None.
  695. --*/
  696. {
  697. PAL_LARGE BYTE buf[16384];
  698. SDL_Rect rect;
  699. if (gpGlobals->fInBattle && !g_fUpdatedInBattle)
  700. {
  701. //
  702. // Update the screen in battle, or the graphics may seem messed up
  703. //
  704. VIDEO_UpdateScreen(NULL);
  705. g_fUpdatedInBattle = TRUE;
  706. }
  707. g_TextLib.bIcon = 0;
  708. g_TextLib.posIcon = 0;
  709. g_TextLib.nCurrentDialogLine = 0;
  710. g_TextLib.posDialogTitle = PAL_XY(12, 8);
  711. g_TextLib.fUserSkip = FALSE;
  712. if (bFontColor != 0)
  713. {
  714. g_TextLib.bCurrentFontColor = bFontColor;
  715. }
  716. if (fPlayingRNG && iNumCharFace)
  717. {
  718. VIDEO_BackupScreen();
  719. g_TextLib.fPlayingRNG = TRUE;
  720. }
  721. switch (bDialogLocation)
  722. {
  723. case kDialogUpper:
  724. if (iNumCharFace > 0)
  725. {
  726. //
  727. // Display the character face at the upper part of the screen
  728. //
  729. if (PAL_MKFReadChunk(buf, 16384, iNumCharFace, gpGlobals->f.fpRGM) > 0)
  730. {
  731. rect.w = PAL_RLEGetWidth((LPCBITMAPRLE)buf);
  732. rect.h = PAL_RLEGetHeight((LPCBITMAPRLE)buf);
  733. rect.x = 48 - rect.w / 2;
  734. rect.y = 55 - rect.h / 2;
  735. if (rect.x < 0)
  736. {
  737. rect.x = 0;
  738. }
  739. if (rect.y < 0)
  740. {
  741. rect.y = 0;
  742. }
  743. PAL_RLEBlitToSurface((LPCBITMAPRLE)buf, gpScreen, PAL_XY(rect.x, rect.y));
  744. if (rect.x < 0)
  745. {
  746. rect.x = 0;
  747. }
  748. if (rect.y < 0)
  749. {
  750. rect.y = 0;
  751. }
  752. VIDEO_UpdateScreen(&rect);
  753. }
  754. }
  755. g_TextLib.posDialogTitle = PAL_XY(iNumCharFace > 0 ? 80 : 12, 8);
  756. g_TextLib.posDialogText = PAL_XY(iNumCharFace > 0 ? 96 : 44, 26);
  757. break;
  758. case kDialogCenter:
  759. g_TextLib.posDialogText = PAL_XY(80, 40);
  760. break;
  761. case kDialogLower:
  762. if (iNumCharFace > 0)
  763. {
  764. //
  765. // Display the character face at the lower part of the screen
  766. //
  767. if (PAL_MKFReadChunk(buf, 16384, iNumCharFace, gpGlobals->f.fpRGM) > 0)
  768. {
  769. rect.x = 270 - PAL_RLEGetWidth((LPCBITMAPRLE)buf) / 2;
  770. rect.y = 144 - PAL_RLEGetHeight((LPCBITMAPRLE)buf) / 2;
  771. PAL_RLEBlitToSurface((LPCBITMAPRLE)buf, gpScreen, PAL_XY(rect.x, rect.y));
  772. VIDEO_UpdateScreen(NULL);
  773. }
  774. }
  775. g_TextLib.posDialogTitle = PAL_XY(iNumCharFace > 0 ? 4 : 12, 108);
  776. g_TextLib.posDialogText = PAL_XY(iNumCharFace > 0 ? 20 : 44, 126);
  777. break;
  778. case kDialogCenterWindow:
  779. g_TextLib.posDialogText = PAL_XY(160, 40);
  780. break;
  781. }
  782. g_TextLib.bDialogPosition = bDialogLocation;
  783. }
  784. static VOID
  785. PAL_DialogWaitForKey(
  786. VOID
  787. )
  788. /*++
  789. Purpose:
  790. Wait for player to press a key after showing a dialog.
  791. Parameters:
  792. None.
  793. Return value:
  794. None.
  795. --*/
  796. {
  797. PAL_LARGE SDL_Color palette[256];
  798. SDL_Color *pCurrentPalette, t;
  799. int i;
  800. //
  801. // get the current palette
  802. //
  803. pCurrentPalette = PAL_GetPalette(gpGlobals->wNumPalette, gpGlobals->fNightPalette);
  804. memcpy(palette, pCurrentPalette, sizeof(palette));
  805. if (g_TextLib.bDialogPosition != kDialogCenterWindow &&
  806. g_TextLib.bDialogPosition != kDialogCenter)
  807. {
  808. //
  809. // show the icon
  810. //
  811. LPCBITMAPRLE p = PAL_SpriteGetFrame(g_TextLib.bufDialogIcons, g_TextLib.bIcon);
  812. if (p != NULL)
  813. {
  814. SDL_Rect rect;
  815. rect.x = PAL_X(g_TextLib.posIcon);
  816. rect.y = PAL_Y(g_TextLib.posIcon);
  817. rect.w = 16;
  818. rect.h = 16;
  819. PAL_RLEBlitToSurface(p, gpScreen, g_TextLib.posIcon);
  820. VIDEO_UpdateScreen(&rect);
  821. }
  822. }
  823. PAL_ClearKeyState();
  824. while (TRUE)
  825. {
  826. UTIL_Delay(100);
  827. if (g_TextLib.bDialogPosition != kDialogCenterWindow &&
  828. g_TextLib.bDialogPosition != kDialogCenter)
  829. {
  830. //
  831. // palette shift
  832. //
  833. t = palette[0xF9];
  834. for (i = 0xF9; i < 0xFE; i++)
  835. {
  836. palette[i] = palette[i + 1];
  837. }
  838. palette[0xFE] = t;
  839. VIDEO_SetPalette(palette);
  840. }
  841. if (g_InputState.dwKeyPress != 0)
  842. {
  843. break;
  844. }
  845. }
  846. if (g_TextLib.bDialogPosition != kDialogCenterWindow &&
  847. g_TextLib.bDialogPosition != kDialogCenter)
  848. {
  849. PAL_SetPalette(gpGlobals->wNumPalette, gpGlobals->fNightPalette);
  850. }
  851. PAL_ClearKeyState();
  852. g_TextLib.fUserSkip = FALSE;
  853. }
  854. VOID
  855. PAL_ShowDialogText(
  856. LPCWSTR lpszText
  857. )
  858. /*++
  859. Purpose:
  860. Show one line of the dialog text.
  861. Parameters:
  862. [IN] lpszText - the text to be shown.
  863. Return value:
  864. None.
  865. --*/
  866. {
  867. SDL_Rect rect;
  868. int x, y;
  869. PAL_ClearKeyState();
  870. g_TextLib.bIcon = 0;
  871. if (gpGlobals->fInBattle && !g_fUpdatedInBattle)
  872. {
  873. //
  874. // Update the screen in battle, or the graphics may seem messed up
  875. //
  876. VIDEO_UpdateScreen(NULL);
  877. g_fUpdatedInBattle = TRUE;
  878. }
  879. if (g_TextLib.nCurrentDialogLine > 3)
  880. {
  881. //
  882. // The rest dialogs should be shown in the next page.
  883. //
  884. PAL_DialogWaitForKey();
  885. g_TextLib.nCurrentDialogLine = 0;
  886. VIDEO_RestoreScreen();
  887. VIDEO_UpdateScreen(NULL);
  888. }
  889. x = PAL_X(g_TextLib.posDialogText);
  890. y = PAL_Y(g_TextLib.posDialogText) + g_TextLib.nCurrentDialogLine * 18;
  891. if (g_TextLib.bDialogPosition == kDialogCenterWindow)
  892. {
  893. //
  894. // The text should be shown in a small window at the center of the screen
  895. //
  896. #ifndef PAL_CLASSIC
  897. if (gpGlobals->fInBattle && g_Battle.BattleResult == kBattleResultOnGoing)
  898. {
  899. PAL_BattleUIShowText(lpszText, 1400);
  900. }
  901. else
  902. #endif
  903. {
  904. PAL_POS pos;
  905. LPBOX lpBox;
  906. int i, w = wcslen(lpszText), len = 0;
  907. for (i = 0; i < w; i++)
  908. len += PAL_CharWidth(lpszText[i]) >> 3;
  909. //
  910. // Create the window box
  911. //
  912. pos = PAL_XY(PAL_X(g_TextLib.posDialogText) - len * 4, PAL_Y(g_TextLib.posDialogText));
  913. lpBox = PAL_CreateSingleLineBox(pos, (len + 1) / 2, TRUE);
  914. rect.x = PAL_X(pos);
  915. rect.y = PAL_Y(pos);
  916. rect.w = 320 - rect.x * 2 + 32;
  917. rect.h = 64;
  918. //
  919. // Show the text on the screen
  920. //
  921. pos = PAL_XY(PAL_X(pos) + 8 + ((len & 1) << 2), PAL_Y(pos) + 10);
  922. PAL_DrawText(lpszText, pos, 0, FALSE, FALSE);
  923. VIDEO_UpdateScreen(&rect);
  924. PAL_DialogWaitForKey();
  925. //
  926. // Delete the box
  927. //
  928. PAL_DeleteBox(lpBox);
  929. VIDEO_UpdateScreen(&rect);
  930. PAL_EndDialog();
  931. }
  932. }
  933. else
  934. {
  935. int len = wcslen(lpszText);
  936. if (g_TextLib.nCurrentDialogLine == 0 &&
  937. g_TextLib.bDialogPosition != kDialogCenter &&
  938. (lpszText[len - 1] == 0xff1a ||
  939. lpszText[len - 1] == 0x2236 || // Special case for Pal WIN95 Simplified Chinese version
  940. lpszText[len - 1] == ':')
  941. )
  942. {
  943. //
  944. // name of character
  945. //
  946. PAL_DrawText(lpszText, g_TextLib.posDialogTitle, FONT_COLOR_CYAN_ALT, TRUE, TRUE);
  947. }
  948. else
  949. {
  950. //
  951. // normal texts
  952. //
  953. WCHAR text[2];
  954. if (!g_TextLib.fPlayingRNG && g_TextLib.nCurrentDialogLine == 0)
  955. {
  956. //
  957. // Save the screen before we show the first line of dialog
  958. //
  959. VIDEO_BackupScreen();
  960. }
  961. while (lpszText != NULL && *lpszText != '\0')
  962. {
  963. switch (*lpszText)
  964. {
  965. case '-':
  966. //
  967. // Set the font color to Cyan
  968. //
  969. if (g_TextLib.bCurrentFontColor == FONT_COLOR_CYAN)
  970. {
  971. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  972. }
  973. else
  974. {
  975. g_TextLib.bCurrentFontColor = FONT_COLOR_CYAN;
  976. }
  977. lpszText++;
  978. break;
  979. #if 0
  980. /* Not used */
  981. case '\'':
  982. //
  983. // Set the font color to Red
  984. //
  985. if (g_TextLib.bCurrentFontColor == FONT_COLOR_RED)
  986. {
  987. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  988. }
  989. else
  990. {
  991. g_TextLib.bCurrentFontColor = FONT_COLOR_RED;
  992. }
  993. lpszText++;
  994. break;
  995. #endif
  996. case '\"':
  997. //
  998. // Set the font color to Yellow
  999. //
  1000. if (g_TextLib.bCurrentFontColor == FONT_COLOR_YELLOW)
  1001. {
  1002. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  1003. }
  1004. else
  1005. {
  1006. g_TextLib.bCurrentFontColor = FONT_COLOR_YELLOW;
  1007. }
  1008. lpszText++;
  1009. break;
  1010. case '$':
  1011. //
  1012. // Set the delay time of text-displaying
  1013. //
  1014. g_TextLib.iDelayTime = wcstol(lpszText + 1, NULL, 10) * 10 / 7;
  1015. lpszText += 3;
  1016. break;
  1017. case '~':
  1018. //
  1019. // Delay for a period and quit
  1020. //
  1021. UTIL_Delay(wcstol(lpszText + 1, NULL, 10) * 80 / 7);
  1022. g_TextLib.nCurrentDialogLine = 0;
  1023. g_TextLib.fUserSkip = FALSE;
  1024. return; // don't go further
  1025. case ')':
  1026. //
  1027. // Set the waiting icon
  1028. //
  1029. g_TextLib.bIcon = 1;
  1030. lpszText++;
  1031. break;
  1032. case '(':
  1033. //
  1034. // Set the waiting icon
  1035. //
  1036. g_TextLib.bIcon = 2;
  1037. lpszText++;
  1038. break;
  1039. case '\\':
  1040. lpszText++;
  1041. default:
  1042. text[0] = *lpszText++;
  1043. text[1] = 0;
  1044. PAL_DrawText(text, PAL_XY(x, y), g_TextLib.bCurrentFontColor, TRUE, TRUE);
  1045. x += PAL_CharWidth(text[0]);
  1046. if (!g_TextLib.fUserSkip)
  1047. {
  1048. PAL_ClearKeyState();
  1049. UTIL_Delay(g_TextLib.iDelayTime * 8);
  1050. if (g_InputState.dwKeyPress & (kKeySearch | kKeyMenu))
  1051. {
  1052. //
  1053. // User pressed a key to skip the dialog
  1054. //
  1055. g_TextLib.fUserSkip = TRUE;
  1056. }
  1057. }
  1058. }
  1059. }
  1060. g_TextLib.posIcon = PAL_XY(x, y);
  1061. g_TextLib.nCurrentDialogLine++;
  1062. }
  1063. }
  1064. }
  1065. VOID
  1066. PAL_ClearDialog(
  1067. BOOL fWaitForKey
  1068. )
  1069. /*++
  1070. Purpose:
  1071. Clear the state of the dialog.
  1072. Parameters:
  1073. [IN] fWaitForKey - whether wait for any key or not.
  1074. Return value:
  1075. None.
  1076. --*/
  1077. {
  1078. if (g_TextLib.nCurrentDialogLine > 0 && fWaitForKey)
  1079. {
  1080. PAL_DialogWaitForKey();
  1081. }
  1082. g_TextLib.nCurrentDialogLine = 0;
  1083. if (g_TextLib.bDialogPosition == kDialogCenter)
  1084. {
  1085. g_TextLib.posDialogTitle = PAL_XY(12, 8);
  1086. g_TextLib.posDialogText = PAL_XY(44, 26);
  1087. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  1088. g_TextLib.bDialogPosition = kDialogUpper;
  1089. }
  1090. }
  1091. VOID
  1092. PAL_EndDialog(
  1093. VOID
  1094. )
  1095. /*++
  1096. Purpose:
  1097. Ends a dialog.
  1098. Parameters:
  1099. None.
  1100. Return value:
  1101. None.
  1102. --*/
  1103. {
  1104. PAL_ClearDialog(TRUE);
  1105. //
  1106. // Set some default parameters, as there are some parts of script
  1107. // which doesn't have a "start dialog" instruction before showing the dialog.
  1108. //
  1109. g_TextLib.posDialogTitle = PAL_XY(12, 8);
  1110. g_TextLib.posDialogText = PAL_XY(44, 26);
  1111. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  1112. g_TextLib.bDialogPosition = kDialogUpper;
  1113. g_TextLib.fUserSkip = FALSE;
  1114. g_TextLib.fPlayingRNG = FALSE;
  1115. }
  1116. BOOL
  1117. PAL_IsInDialog(
  1118. VOID
  1119. )
  1120. /*++
  1121. Purpose:
  1122. Check if there are dialog texts on the screen.
  1123. Parameters:
  1124. None.
  1125. Return value:
  1126. TRUE if there are dialog texts on the screen, FALSE if not.
  1127. --*/
  1128. {
  1129. return (g_TextLib.nCurrentDialogLine != 0);
  1130. }
  1131. BOOL
  1132. PAL_DialogIsPlayingRNG(
  1133. VOID
  1134. )
  1135. /*++
  1136. Purpose:
  1137. Check if the script used the RNG playing parameter when displaying texts.
  1138. Parameters:
  1139. None.
  1140. Return value:
  1141. TRUE if the script used the RNG playing parameter, FALSE if not.
  1142. --*/
  1143. {
  1144. return g_TextLib.fPlayingRNG;
  1145. }
  1146. INT
  1147. PAL_MultiByteToWideCharCP(
  1148. CODEPAGE cp,
  1149. LPCSTR mbs,
  1150. int mbslength,
  1151. LPWSTR wcs,
  1152. int wcslength
  1153. )
  1154. /*++
  1155. Purpose:
  1156. Convert multi-byte string into the corresponding unicode string.
  1157. Parameters:
  1158. [IN] cp - Code page for conversion.
  1159. [IN] mbs - Pointer to the multi-byte string.
  1160. [IN] mbslength - Length of the multi-byte string, or -1 for auto-detect.
  1161. [IN] wcs - Pointer to the wide string buffer.
  1162. [IN] wcslength - Length of the wide string buffer.
  1163. Return value:
  1164. The length of converted wide string. If mbslength is set to -1, the returned
  1165. value includes the terminal null-char; otherwise, the null-char is not included.
  1166. If wcslength is set to 0, wcs can be set to NULL and the return value is the
  1167. required length of the wide string buffer.
  1168. --*/
  1169. {
  1170. int i = 0, state = 0, wlen = 0, null = 0;
  1171. if (mbslength == -1)
  1172. {
  1173. mbslength = strlen(mbs);
  1174. null = 1;
  1175. }
  1176. if (!wcs)
  1177. {
  1178. switch (cp)
  1179. {
  1180. case CP_SHIFTJIS:
  1181. for (i = 0; i < mbslength && mbs[i]; i++)
  1182. {
  1183. if (state == 0)
  1184. {
  1185. if ((BYTE)mbs[i] <= 0x80 || (BYTE)mbs[i] >= 0xfd || ((BYTE)mbs[i] >= 0xa0 && (BYTE)mbs[i] <= 0xdf))
  1186. wlen++;
  1187. else
  1188. state = 1;
  1189. }
  1190. else
  1191. {
  1192. wlen++;
  1193. state = 0;
  1194. }
  1195. }
  1196. break;
  1197. case CP_GBK:
  1198. case CP_BIG5:
  1199. for (i = 0; i < mbslength && mbs[i]; i++)
  1200. {
  1201. if (state == 0)
  1202. {
  1203. if ((BYTE)mbs[i] <= 0x80 || (BYTE)mbs[i] == 0xff)
  1204. wlen++;
  1205. else
  1206. state = 1;
  1207. }
  1208. else
  1209. {
  1210. wlen++;
  1211. state = 0;
  1212. }
  1213. }
  1214. break;
  1215. case CP_UTF_8:
  1216. for (i = 0; i < mbslength && mbs[i]; i++)
  1217. {
  1218. if (state == 0)
  1219. {
  1220. if ((BYTE)mbs[i] >= 0x80)
  1221. {
  1222. BYTE s = (BYTE)mbs[i] << 1;
  1223. while (s >= 0x80) { state++; s <<= 1; }
  1224. if (state < 1 || state > 3)
  1225. {
  1226. state = 0;
  1227. wlen++;
  1228. }
  1229. }
  1230. else
  1231. wlen++;
  1232. }
  1233. else
  1234. {
  1235. if ((BYTE)mbs[i] >= 0x80 && (BYTE)mbs[i] < 0xc0)
  1236. {
  1237. if (--state == 0) wlen++;
  1238. }
  1239. else
  1240. {
  1241. state = 0; wlen++;
  1242. }
  1243. }
  1244. }
  1245. break;
  1246. default:
  1247. return -1;
  1248. }
  1249. if (i < mbslength && !mbs[i]) null = 1;
  1250. return wlen + null + (state != 0);
  1251. }
  1252. else
  1253. {
  1254. WCHAR invalid_char;
  1255. switch (cp)
  1256. {
  1257. case CP_SHIFTJIS:
  1258. invalid_char = 0x30fb;
  1259. for (i = 0; i < mbslength && wlen < wcslength && mbs[i]; i++)
  1260. {
  1261. if (state == 0)
  1262. {
  1263. if ((BYTE)mbs[i] <= 0x80)
  1264. wcs[wlen++] = mbs[i];
  1265. else if ((BYTE)mbs[i] >= 0xa0 && (BYTE)mbs[i] <= 0xdf)
  1266. wcs[wlen++] = cptbl_jis_half[(BYTE)mbs[i] - 0xa0];
  1267. else if ((BYTE)mbs[i] == 0xfd)
  1268. wcs[wlen++] = 0xf8f1;
  1269. else if ((BYTE)mbs[i] == 0xfe)
  1270. wcs[wlen++] = 0xf8f2;
  1271. else if ((BYTE)mbs[i] == 0xff)
  1272. wcs[wlen++] = 0xf8f3;
  1273. else
  1274. state = 1;
  1275. }
  1276. else
  1277. {
  1278. if ((BYTE)mbs[i] < 0x40)
  1279. wcs[wlen++] = 0x30fb;
  1280. else if ((BYTE)mbs[i - 1] < 0xa0)
  1281. wcs[wlen++] = cptbl_jis[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x40];
  1282. else
  1283. wcs[wlen++] = cptbl_jis[(BYTE)mbs[i - 1] - 0xc1][(BYTE)mbs[i] - 0x40];
  1284. state = 0;
  1285. }
  1286. }
  1287. break;
  1288. case CP_GBK:
  1289. invalid_char = 0x3f;
  1290. for (i = 0; i < mbslength && wlen < wcslength && mbs[i]; i++)
  1291. {
  1292. if (state == 0)
  1293. {
  1294. if ((BYTE)mbs[i] < 0x80)
  1295. wcs[wlen++] = mbs[i];
  1296. else if ((BYTE)mbs[i] == 0x80)
  1297. wcs[wlen++] = 0x20ac;
  1298. else if ((BYTE)mbs[i] == 0xff)
  1299. wcs[wlen++] = 0xf8f5;
  1300. else
  1301. state = 1;
  1302. }
  1303. else
  1304. {
  1305. if ((BYTE)mbs[i] < 0x40)
  1306. wcs[wlen++] = invalid_char;
  1307. else
  1308. wcs[wlen++] = cptbl_gbk[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x40];
  1309. state = 0;
  1310. }
  1311. }
  1312. break;
  1313. case CP_BIG5:
  1314. invalid_char = 0x3f;
  1315. for (i = 0; i < mbslength && wlen < wcslength && mbs[i]; i++)
  1316. {
  1317. if (state == 0)
  1318. {
  1319. if ((BYTE)mbs[i] <= 0x80)
  1320. wcs[wlen++] = mbs[i];
  1321. else if ((BYTE)mbs[i] == 0xff)
  1322. wcs[wlen++] = 0xf8f8;
  1323. else
  1324. state = 1;
  1325. }
  1326. else
  1327. {
  1328. if ((BYTE)mbs[i] < 0x40 || ((BYTE)mbs[i] >= 0x7f && (BYTE)mbs[i] <= 0xa0))
  1329. wcs[wlen++] = invalid_char;
  1330. else if ((BYTE)mbs[i] <= 0x7e)
  1331. wcs[wlen++] = cptbl_big5[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x40];
  1332. else
  1333. wcs[wlen++] = cptbl_big5[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x60];
  1334. state = 0;
  1335. }
  1336. }
  1337. break;
  1338. case CP_UTF_8:
  1339. invalid_char = 0x3f;
  1340. for (i = 0; i < mbslength && wlen < wcslength && mbs[i]; i++)
  1341. {
  1342. if (state == 0)
  1343. {
  1344. if ((BYTE)mbs[i] >= 0x80)
  1345. {
  1346. BYTE s = (BYTE)mbs[i] << 1;
  1347. while (s >= 0x80) { state++; s <<= 1; }
  1348. if (state < 1 || state > 3)
  1349. {
  1350. state = 0;
  1351. wcs[wlen++] = invalid_char;
  1352. }
  1353. else
  1354. {
  1355. wcs[wlen] = s >> (state + 1);
  1356. }
  1357. }
  1358. else
  1359. wcs[wlen++] = mbs[i];
  1360. }
  1361. else
  1362. {
  1363. if ((BYTE)mbs[i] >= 0x80 && (BYTE)mbs[i] < 0xc0)
  1364. {
  1365. wcs[wlen] <<= 6;
  1366. wcs[wlen] |= (BYTE)mbs[i] & 0x3f;
  1367. if (--state == 0) wlen++;
  1368. }
  1369. else
  1370. {
  1371. state = 0;
  1372. wcs[wlen++] = invalid_char;
  1373. }
  1374. }
  1375. }
  1376. break;
  1377. default:
  1378. return -1;
  1379. }
  1380. if (state != 0 && wlen < wcslength)
  1381. {
  1382. wcs[wlen++] = invalid_char;
  1383. }
  1384. if (null || (i < mbslength && !mbs[i]))
  1385. {
  1386. if (wlen < wcslength)
  1387. wcs[wlen++] = 0;
  1388. else
  1389. wcs[wlen - 1] = 0;
  1390. }
  1391. return wlen;
  1392. }
  1393. }
  1394. INT
  1395. PAL_MultiByteToWideChar(
  1396. LPCSTR mbs,
  1397. int mbslength,
  1398. LPWSTR wcs,
  1399. int wcslength
  1400. )
  1401. /*++
  1402. Purpose:
  1403. Convert multi-byte string into the corresponding unicode string.
  1404. Parameters:
  1405. [IN] mbs - Pointer to the multi-byte string.
  1406. [IN] mbslength - Length of the multi-byte string, or -1 for auto-detect.
  1407. [IN] wcs - Pointer to the wide string buffer.
  1408. [IN] wcslength - Length of the wide string buffer.
  1409. Return value:
  1410. The length of converted wide string. If mbslength is set to -1, the returned
  1411. value includes the terminal null-char; otherwise, the null-char is not included.
  1412. If wcslength is set to 0, wcs can be set to NULL and the return value is the
  1413. required length of the wide string buffer.
  1414. --*/
  1415. {
  1416. return PAL_MultiByteToWideCharCP(gpGlobals->iCodePage, mbs, mbslength, wcs, wcslength);
  1417. }
  1418. WCHAR
  1419. PAL_GetInvalidChar(
  1420. CODEPAGE iCodePage
  1421. )
  1422. {
  1423. switch(iCodePage)
  1424. {
  1425. case CP_BIG5: return 0x3f;
  1426. case CP_GBK: return 0x3f;
  1427. case CP_SHIFTJIS: return 0x30fb;
  1428. case CP_UTF_8: return 0x3f;
  1429. default: return 0;
  1430. }
  1431. }