text.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348
  1. /* -*- mode: c; tab-width: 4; c-basic-offset: 4; c-file-style: "linux" -*- */
  2. //
  3. // Copyright (c) 2009-2011, Wei Mingzhi <whistler_wmz@users.sf.net>.
  4. // Copyright (c) 2011-2017, SDLPAL development team.
  5. // All rights reserved.
  6. //
  7. // This file is part of SDLPAL.
  8. //
  9. // SDLPAL is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU General Public License as published by
  11. // the Free Software Foundation, either version 3 of the License, or
  12. // (at your option) any later version.
  13. //
  14. // This program is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. // GNU General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public License
  20. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. //
  22. // Portions based on PALx Project by palxex.
  23. // Copyright (c) 2006-2008, Pal Lockheart <palxex@gmail.com>.
  24. //
  25. #include "main.h"
  26. #include <errno.h>
  27. #include <wctype.h>
  28. #define FONT_COLOR_DEFAULT 0x4F
  29. #define FONT_COLOR_YELLOW 0x2D
  30. #define FONT_COLOR_RED 0x1A
  31. #define FONT_COLOR_CYAN 0x8D
  32. #define FONT_COLOR_CYAN_ALT 0x8C
  33. BOOL g_fUpdatedInBattle = FALSE;
  34. #define MESSAGE_MAX_BUFFER_SIZE 512
  35. #define INCLUDE_CODEPAGE_H
  36. #include "codepage.h"
  37. #ifndef PAL_CLASSIC
  38. # define ATB_WORD_COUNT 6
  39. static LPWSTR gc_rgszAdditionalWords[CP_MAX][ATB_WORD_COUNT] = {
  40. { L"\x6230\x9B25\x901F\x5EA6", L"\x4E00", L"\x4E8C", L"\x4E09", L"\x56DB", L"\x4E94" },
  41. { L"\x6218\x6597\x901F\x5EA6", L"\x4E00", L"\x4E8C", L"\x4E09", L"\x56DB", L"\x4E94" },
  42. //{ L"\x6226\x95D8\x901F\x5EA6", L"\x4E00", L"\x4E8C", L"\x4E09", L"\x56DB", L"\x4E94" },
  43. };
  44. static LPWSTR gc_rgszDefaultAdditionalWords[ATB_WORD_COUNT] = { NULL, L"\xFF11", L"\xFF12", L"\xFF13", L"\xFF14", L"\xFF15" };
  45. #endif
  46. #define SDLPAL_EXTRA_WORD_COUNT 1
  47. static LPWSTR gc_rgszSDLPalWords[CP_MAX][SDLPAL_EXTRA_WORD_COUNT] = {
  48. { L"\x8FD4\x56DE\x8A2D\x5B9A" },
  49. { L"\x8FD4\x56DE\x8BBE\x7F6E" },
  50. };
  51. LPWSTR g_rcCredits[12];
  52. TEXTLIB g_TextLib;
  53. PAL_FORCE_INLINE int
  54. PAL_ParseLine(
  55. char *line,
  56. char **value,
  57. int *length,
  58. int deltrail
  59. )
  60. {
  61. //
  62. // Remove the leading spaces
  63. //
  64. while (*line && iswspace(*line)) line++;
  65. //
  66. // Skip comments starting with '#'
  67. //
  68. if (*line && *line != '#')
  69. {
  70. //
  71. // Split the index and value
  72. //
  73. LPSTR val = strchr(line, '=');
  74. if (val)
  75. {
  76. //
  77. // Remove the trailing spaces
  78. //
  79. LPSTR end = line + strlen(line);
  80. int index;
  81. if (end > line && end[-1] == '\n') *(--end) = 0;
  82. if (deltrail) while (end > line && iswspace(end[-1])) *(--end) = 0;
  83. //
  84. // Parse the index and pass out value
  85. //
  86. if (sscanf(line, "%d", &index) == 1)
  87. {
  88. *value = val + 1;
  89. *length = end - *value;
  90. return index;
  91. }
  92. }
  93. }
  94. return 0;
  95. }
  96. PAL_FORCE_INLINE char *
  97. PAL_ReadOneLine(
  98. char *temp,
  99. int limit,
  100. FILE *fp
  101. )
  102. {
  103. if (fgets(temp, limit, fp))
  104. {
  105. int n = strlen(temp);
  106. if (n == limit - 1 && temp[n - 1] != '\n' && !feof(fp))
  107. {
  108. // Line too long, try to read it as a whole
  109. int nn = 2;
  110. char *tmp = strdup(temp);
  111. while (!feof(fp))
  112. {
  113. if (!(tmp = (char *)realloc(tmp, nn * limit)))
  114. {
  115. TerminateOnError("PAL_ReadOneLine(): failed to allocate memory for long line!");
  116. }
  117. if (fgets(tmp + n, limit + 1, fp))
  118. {
  119. n += strlen(tmp + n);
  120. if (n < limit - 1 || temp[n - 1] == '\n')
  121. break;
  122. else
  123. nn++;
  124. }
  125. }
  126. if (tmp[n - 1] == '\n') tmp[n - 1] = 0;
  127. return tmp;
  128. }
  129. else
  130. {
  131. while (n > 0 && (temp[n - 1] == '\n' || temp[n - 1] == '\r')) temp[--n] = 0;
  132. return temp;
  133. }
  134. }
  135. else
  136. return NULL;
  137. }
  138. static int
  139. PAL_ReadMessageFile(
  140. FILE *fp
  141. )
  142. {
  143. char temp[MESSAGE_MAX_BUFFER_SIZE];
  144. struct _msg_entry
  145. {
  146. struct _msg_entry *next;
  147. wchar_t *value;
  148. } *cur_val = NULL;
  149. struct _msg_list_entry
  150. {
  151. struct _msg_list_entry *next;
  152. struct _msg_entry *value;
  153. int index;
  154. int count;
  155. } *head = NULL, *item = NULL;
  156. struct _word_list_entry
  157. {
  158. struct _word_list_entry *next;
  159. wchar_t *value;
  160. int index;
  161. } whead = { NULL, NULL }, *witem = NULL;
  162. enum _message_state
  163. {
  164. ST_OUTSIDE,
  165. ST_SETTING,
  166. ST_DIALOG,
  167. ST_WORD,
  168. ST_DESC,
  169. ST_CREDIT,
  170. ST_LAYOUT
  171. } state = ST_OUTSIDE;
  172. int idx_cnt = 0, msg_cnt = 0, word_cnt = 0, sid, eid = -1;
  173. while (!feof(fp))
  174. {
  175. char *buffer;
  176. if ((buffer = PAL_ReadOneLine(temp, MESSAGE_MAX_BUFFER_SIZE, fp)) != NULL)
  177. {
  178. switch(state)
  179. {
  180. case ST_OUTSIDE:
  181. //
  182. // Skip comments starting with '#'
  183. //
  184. if (*buffer && *buffer != '#')
  185. {
  186. if (strncmp(buffer, "[BEGIN MESSAGE]", 15) == 0 &&
  187. sscanf(buffer + 15, "%d", &sid) == 1)
  188. {
  189. state = ST_DIALOG;
  190. //
  191. // First save values (converted wide string) into a linked list
  192. //
  193. if (head)
  194. {
  195. item->next = (struct _msg_list_entry *)UTIL_malloc(sizeof(struct _msg_list_entry));
  196. item = item->next;
  197. }
  198. else
  199. {
  200. head = (struct _msg_list_entry *)UTIL_malloc(sizeof(struct _msg_list_entry));
  201. item = head;
  202. }
  203. item->value = NULL; item->index = sid;
  204. item->count = 0; item->next = NULL; cur_val = NULL;
  205. if (idx_cnt < item->index) idx_cnt = item->index;
  206. }
  207. else if (strncmp(buffer, "[BEGIN SETTING]", 15) == 0 && !witem)
  208. {
  209. state = ST_SETTING;
  210. }
  211. else if (strncmp(buffer, "[BEGIN WORDS]", 13) == 0 && !witem)
  212. {
  213. state = ST_WORD;
  214. //
  215. // First save values (converted wide string) into a linked list
  216. //
  217. witem = &whead;
  218. }
  219. else if (strncmp(buffer, "[BEGIN DESCRIPTIONS]", 13) == 0)
  220. {
  221. state = ST_DESC;
  222. }
  223. else if (strncmp(buffer, "[BEGIN CREDITS]", 15) == 0 && !witem)
  224. {
  225. state = ST_CREDIT;
  226. }
  227. else if (strncmp(buffer, "[BEGIN LAYOUT]", 14) == 0 && !witem)
  228. {
  229. state = ST_LAYOUT;
  230. gConfig.fUseCustomScreenLayout = TRUE;
  231. }
  232. else
  233. {
  234. // Just ignore invalid lines
  235. UTIL_LogOutput(LOGLEVEL_WARNING, "PAL_ReadMessageFile(): encounter invalid line '%s'!\n", buffer);
  236. }
  237. }
  238. break;
  239. case ST_DIALOG:
  240. //
  241. // Check if to end one dialog
  242. //
  243. if (strncmp(buffer, "[END MESSAGE]", 13) == 0 &&
  244. sscanf(buffer + 13, "%d", &eid) == 1 && eid >= sid)
  245. {
  246. // End dialog
  247. state = ST_OUTSIDE;
  248. }
  249. else
  250. {
  251. if (cur_val)
  252. {
  253. cur_val->next = (struct _msg_entry *)UTIL_malloc(sizeof(struct _msg_entry));
  254. cur_val = cur_val->next;
  255. }
  256. else
  257. cur_val = (struct _msg_entry *)UTIL_malloc(sizeof(struct _msg_entry));
  258. if (strncmp(buffer, "[CLEAR MESSAGE]", 15) == 0)
  259. {
  260. cur_val->value = NULL;
  261. }
  262. else
  263. {
  264. int len = PAL_MultiByteToWideCharCP(CP_UTF_8, buffer, -1, NULL, 0);
  265. cur_val->value = (wchar_t *)UTIL_malloc(len * sizeof(wchar_t));
  266. PAL_MultiByteToWideCharCP(CP_UTF_8, buffer, -1, cur_val->value, len);
  267. msg_cnt++;
  268. }
  269. if (!item->value) item->value = cur_val;
  270. cur_val->next = NULL; item->count++;
  271. }
  272. break;
  273. case ST_WORD:
  274. //
  275. // Check if to end word list
  276. //
  277. if (strncmp(buffer, "[END WORDS]", 11) == 0)
  278. {
  279. // End word list
  280. state = ST_OUTSIDE;
  281. }
  282. else
  283. {
  284. char *v;
  285. int l, i = PAL_ParseLine(buffer, &v, &l, FALSE);
  286. if (i > 0)
  287. {
  288. int len = PAL_MultiByteToWideCharCP(CP_UTF_8, v, -1, NULL, 0);
  289. struct _word_list_entry *val = (struct _word_list_entry *)UTIL_malloc(sizeof(struct _word_list_entry));
  290. val->value = (wchar_t *)UTIL_malloc(len * sizeof(wchar_t));
  291. PAL_MultiByteToWideCharCP(CP_UTF_8, v, -1, val->value, len);
  292. val->index = i; val->next = NULL;
  293. witem->next = val; witem = witem->next;
  294. if (word_cnt < i) word_cnt = i;
  295. }
  296. }
  297. break;
  298. case ST_DESC:
  299. //
  300. // Check if to end setting list
  301. //
  302. if (strncmp(buffer, "[END DESCRIPTIONS]", 18) == 0)
  303. {
  304. // End setting list
  305. state = ST_OUTSIDE;
  306. }
  307. else
  308. {
  309. char *line = buffer;
  310. while (*buffer && iswspace(*buffer)) line++;
  311. //
  312. // Skip comments starting with '#'
  313. //
  314. if (*line && *line != '#')
  315. {
  316. //
  317. // Split the index and value
  318. //
  319. LPSTR p = strchr(line, '=');
  320. int wlen,strip_count=2;
  321. if (p)
  322. {
  323. int index;
  324. //
  325. // Remove the trailing spaces
  326. //
  327. LPSTR end = line + strlen(line);
  328. if (end > line && end[-1] == '\n') *(--end) = 0;
  329. if (FALSE) while (end > line && iswspace(end[-1])) *(--end) = 0;
  330. *p++ = '\0';
  331. while(strip_count--){
  332. if(p[strlen(p)-1]=='\r') p[strlen(p)-1]='\0';
  333. if(p[strlen(p)-1]=='\n') p[strlen(p)-1]='\0';
  334. }
  335. wlen = PAL_MultiByteToWideCharCP(CP_UTF_8, p, -1, NULL, 0);
  336. //
  337. // Parse the index and pass out value
  338. //
  339. sscanf(line, "%x", &index);
  340. LPOBJECTDESC lpObjectDesc = gpGlobals->lpObjectDesc;
  341. LPOBJECTDESC prevObjectDesc = lpObjectDesc;
  342. BOOL isFirst = gpGlobals->lpObjectDesc == NULL;
  343. while (lpObjectDesc != NULL)
  344. {
  345. if (lpObjectDesc->wObjectID == index)
  346. {
  347. break;
  348. }
  349. prevObjectDesc = lpObjectDesc;
  350. lpObjectDesc = lpObjectDesc->next;
  351. }
  352. if( !lpObjectDesc )
  353. {
  354. lpObjectDesc = UTIL_calloc(1, sizeof(OBJECTDESC));
  355. memset(lpObjectDesc,0,sizeof(OBJECTDESC));
  356. if( prevObjectDesc )
  357. prevObjectDesc->next = lpObjectDesc;
  358. }
  359. if( isFirst )
  360. gpGlobals->lpObjectDesc = lpObjectDesc;
  361. lpObjectDesc->wObjectID = index;
  362. lpObjectDesc->lpDesc = (LPWSTR)UTIL_calloc(1, wlen * sizeof(WCHAR));
  363. PAL_MultiByteToWideCharCP(CP_UTF_8, p, -1, lpObjectDesc->lpDesc, wlen);
  364. }
  365. }
  366. }
  367. break;
  368. case ST_SETTING:
  369. //
  370. // Check if to end setting list
  371. //
  372. if (strncmp(buffer, "[END SETTING]", 13) == 0)
  373. {
  374. // End setting list
  375. state = ST_OUTSIDE;
  376. }
  377. else
  378. {
  379. char *line = buffer;
  380. while (*buffer && iswspace(*buffer)) line++;
  381. //
  382. // Skip comments starting with '#'
  383. //
  384. if (*line && *line != '#')
  385. {
  386. //
  387. // Split the index and value
  388. //
  389. LPSTR val = strchr(line, '=');
  390. if (val)
  391. {
  392. char index[80];
  393. *val = '\0';
  394. //
  395. // Remove the trailing spaces
  396. //
  397. LPSTR end = line + strlen(line);
  398. if (end > line && end[-1] == '\n') *(--end) = 0;
  399. if (FALSE) while (end > line && iswspace(end[-1])) *(--end) = 0;
  400. //
  401. // Parse the index and pass out value
  402. //
  403. if (sscanf(line, "%s", index) == 1 && strncasecmp(index, "UseISOFont", 10) == 0 )
  404. {
  405. g_TextLib.fUseISOFont = atoi(val + 1) == 1;
  406. }
  407. }
  408. }
  409. }
  410. break;
  411. case ST_CREDIT:
  412. //
  413. // Check if to end credit list
  414. //
  415. if (strncmp(buffer, "[END CREDITS]", 13) == 0)
  416. {
  417. // End credit list
  418. state = ST_OUTSIDE;
  419. }
  420. else
  421. {
  422. char *v;
  423. int l, i = PAL_ParseLine(buffer, &v, &l, FALSE);
  424. if ((i == 1 || (i >= 6 && i <= 11)) && !g_rcCredits[i])
  425. {
  426. int limit = (i == 1) ? 24 * 8 : 40 * 8, w = 0, j = 0, len;
  427. if (i == 6 || i == 7)
  428. {
  429. if (PAL_PLATFORM && PAL_CREDIT && PAL_PORTYEAR)
  430. {
  431. const char *templates[] = { "${platform}", "${author}", "${year}" };
  432. const char *values[] = { PAL_PLATFORM, PAL_CREDIT, PAL_PORTYEAR };
  433. const int matchlen[] = { 11, 9, 7 };
  434. const int valuelen[] = { sizeof(PAL_PLATFORM) - 1, sizeof(PAL_CREDIT) - 1, sizeof(PAL_PORTYEAR) - 1 };
  435. char *tmp = (char *)alloca(valuelen[0] + valuelen[1] + valuelen[2] + l + 1);
  436. char *dst = tmp, *src = v;
  437. while (*src)
  438. {
  439. if (*src == '$')
  440. {
  441. int k;
  442. for (k = 0; k < 3 && strncmp(src, templates[k], matchlen[k]); k++);
  443. if (k < 3)
  444. {
  445. strcpy(dst, values[k]);
  446. dst += valuelen[k];
  447. src += matchlen[k];
  448. continue;
  449. }
  450. }
  451. *dst++ = *src++;
  452. }
  453. *dst = 0;
  454. len = PAL_MultiByteToWideCharCP(CP_UTF_8, tmp, -1, NULL, 0);
  455. g_rcCredits[i] = (wchar_t *)UTIL_malloc(len * sizeof(wchar_t));
  456. PAL_MultiByteToWideCharCP(CP_UTF_8, tmp, -1, g_rcCredits[i], len);
  457. }
  458. }
  459. else
  460. {
  461. len = PAL_MultiByteToWideCharCP(CP_UTF_8, v, -1, NULL, 0);
  462. g_rcCredits[i] = (wchar_t *)UTIL_malloc(len * sizeof(wchar_t));
  463. PAL_MultiByteToWideCharCP(CP_UTF_8, v, -1, g_rcCredits[i], len);
  464. }
  465. if (g_rcCredits[i])
  466. {
  467. // Limit the length of texts
  468. while (w < limit && j < len - 1) w += PAL_CharWidth(g_rcCredits[i][j++]);
  469. if (w >= limit) g_rcCredits[i][w > limit ? j - 1 : j] = 0;
  470. }
  471. }
  472. }
  473. break;
  474. case ST_LAYOUT:
  475. if (strncmp(buffer, "[END LAYOUT]", 12) == 0)
  476. {
  477. // End layout
  478. state = ST_OUTSIDE;
  479. }
  480. else
  481. {
  482. char *v;
  483. int x, y, f, n, l, i = PAL_ParseLine(buffer, &v, &l, FALSE);
  484. if (i >= 1 && i <= (sizeof(SCREENLAYOUT) / sizeof(PAL_POS)))
  485. {
  486. if ((n = sscanf(v, "%d,%d,%d", &x, &y, &f)) >= 2 && x < 320 && y < 200)
  487. {
  488. gConfig.ScreenLayoutArray[i - 1] = PAL_XY(x, y);
  489. if (n == 3) gConfig.ScreenLayoutFlag[i - 1] = f;
  490. }
  491. }
  492. }
  493. break;
  494. default:
  495. TerminateOnError("PAL_ReadMessageFile(): Reached an unknown state. Something really wrong may have happened!");
  496. break;
  497. }
  498. if (buffer != temp) free(buffer);
  499. }
  500. }
  501. if (msg_cnt > 0)
  502. {
  503. //
  504. // Move values from linked list to array
  505. //
  506. int idx_msg = 1;
  507. g_TextLib.nIndices = (idx_cnt += 1);
  508. g_TextLib.nMsgs = (msg_cnt += 1);
  509. g_TextLib.lpIndexBuf = (int **)UTIL_calloc(idx_cnt, sizeof(int *));
  510. g_TextLib.lpMsgBuf = (LPWSTR *)UTIL_calloc(msg_cnt, sizeof(LPWSTR));
  511. for (item = head; item; )
  512. {
  513. struct _msg_list_entry *temp = item->next;
  514. struct _msg_entry *msg = item->value;
  515. int index = 0;
  516. g_TextLib.lpIndexBuf[item->index] = (int *)UTIL_calloc(item->count + 1, sizeof(int));
  517. while (msg)
  518. {
  519. struct _msg_entry *tmp = msg->next;
  520. if (msg->value)
  521. {
  522. g_TextLib.lpIndexBuf[item->index][index++] = idx_msg;
  523. g_TextLib.lpMsgBuf[idx_msg++] = msg->value;
  524. }
  525. else
  526. g_TextLib.lpIndexBuf[item->index][index++] = 0;
  527. free(msg); msg = tmp;
  528. }
  529. g_TextLib.lpIndexBuf[item->index][item->count] = -1;
  530. free(item); item = temp;
  531. }
  532. }
  533. if (word_cnt > 0)
  534. {
  535. //
  536. // Move values from linked list to array
  537. //
  538. #ifndef PAL_CLASSIC
  539. int i;
  540. #endif
  541. if (word_cnt < MINIMAL_WORD_COUNT - 1) word_cnt = MINIMAL_WORD_COUNT - 1;
  542. g_TextLib.nWords = (word_cnt += 1);
  543. g_TextLib.lpWordBuf = (LPWSTR *)UTIL_calloc(word_cnt, sizeof(LPWSTR));
  544. for (witem = whead.next; witem; )
  545. {
  546. struct _word_list_entry *temp = witem->next;
  547. g_TextLib.lpWordBuf[witem->index] = witem->value;
  548. free(witem); witem = temp;
  549. }
  550. #ifndef PAL_CLASSIC
  551. for (i = 1; i < ATB_WORD_COUNT; i++)
  552. if (!g_TextLib.lpWordBuf[i + SYSMENU_LABEL_BATTLEMODE])
  553. g_TextLib.lpWordBuf[i + SYSMENU_LABEL_BATTLEMODE] = gc_rgszDefaultAdditionalWords[i];
  554. #endif
  555. }
  556. fclose(fp);
  557. return (msg_cnt > 0 && word_cnt > 0) ? 1 : 0;
  558. }
  559. INT
  560. PAL_InitText(
  561. VOID
  562. )
  563. /*++
  564. Purpose:
  565. Initialize the in-game texts.
  566. Parameters:
  567. None.
  568. Return value:
  569. 0 = success.
  570. -1 = memory allocation error.
  571. --*/
  572. {
  573. if (gConfig.pszMsgFile)
  574. {
  575. //
  576. // Open the message, index and word data files.
  577. //
  578. FILE *fp = UTIL_OpenRequiredFileForMode(gConfig.pszMsgFile, "r");
  579. //
  580. // Read the contents of the message, index and word data files.
  581. //
  582. if (!PAL_ReadMessageFile(fp))
  583. {
  584. return -1;
  585. }
  586. else
  587. {
  588. DWORD dwWordLength = 0;
  589. int i;
  590. for (i = 1; i < g_TextLib.nWords; i++)
  591. {
  592. if (g_TextLib.lpWordBuf[i])
  593. {
  594. LPWSTR ptr = g_TextLib.lpWordBuf[i];
  595. DWORD n = 0;
  596. while (*ptr) n += PAL_CharWidth(*ptr++) >> 3;
  597. if (dwWordLength < n) dwWordLength = n;
  598. }
  599. }
  600. gConfig.dwWordLength = dwWordLength;
  601. for (i = 0; i < 12; i++)
  602. {
  603. if (!g_rcCredits[i])
  604. g_rcCredits[i] = L"";
  605. }
  606. }
  607. }
  608. else
  609. {
  610. FILE *fpMsg, *fpWord;
  611. DWORD *offsets;
  612. LPWSTR tmp;
  613. LPBYTE temp;
  614. int wpos, wlen, i;
  615. //
  616. // Open the message and word data files.
  617. //
  618. fpMsg = UTIL_OpenRequiredFile("m.msg");
  619. fpWord = UTIL_OpenRequiredFile("word.dat");
  620. //
  621. // See how many words we have
  622. //
  623. fseek(fpWord, 0, SEEK_END);
  624. i = ftell(fpWord);
  625. //
  626. // Each word has 10 bytes
  627. //
  628. g_TextLib.nWords = (i + (gConfig.dwWordLength - 1)) / gConfig.dwWordLength;
  629. if (g_TextLib.nWords < MINIMAL_WORD_COUNT) g_TextLib.nWords = MINIMAL_WORD_COUNT;
  630. //
  631. // Read the words
  632. //
  633. temp = (LPBYTE)malloc(gConfig.dwWordLength * g_TextLib.nWords);
  634. if (temp == NULL)
  635. {
  636. fclose(fpWord);
  637. fclose(fpMsg);
  638. return -1;
  639. }
  640. fseek(fpWord, 0, SEEK_SET);
  641. if (fread(temp, 1, i, fpWord) < (size_t)i)
  642. {
  643. fclose(fpWord);
  644. fclose(fpMsg);
  645. return -1;
  646. }
  647. memset(temp + i, 0, gConfig.dwWordLength * g_TextLib.nWords - i);
  648. //
  649. // Close the words file
  650. //
  651. fclose(fpWord);
  652. // Split the words and do code page conversion
  653. for (i = 0, wlen = 0; i < g_TextLib.nWords; i++)
  654. {
  655. int base = i * gConfig.dwWordLength;
  656. int pos = base + gConfig.dwWordLength - 1;
  657. while (pos >= base && temp[pos] == ' ') temp[pos--] = 0;
  658. wlen += PAL_MultiByteToWideChar((LPCSTR)temp + base, gConfig.dwWordLength, NULL, 0) + 1;
  659. }
  660. g_TextLib.lpWordBuf = (LPWSTR*)malloc(g_TextLib.nWords * sizeof(LPWSTR));
  661. if (g_TextLib.lpWordBuf == NULL)
  662. {
  663. free(temp);
  664. fclose(fpMsg);
  665. return -1;
  666. }
  667. tmp = (LPWSTR)malloc(wlen * sizeof(WCHAR));
  668. if (tmp == NULL)
  669. {
  670. free(g_TextLib.lpWordBuf);
  671. free(temp);
  672. fclose(fpMsg);
  673. return -1;
  674. }
  675. for (i = 0, wpos = 0; i < g_TextLib.nWords; i++)
  676. {
  677. int l;
  678. g_TextLib.lpWordBuf[i] = tmp + wpos;
  679. l = PAL_MultiByteToWideChar((LPCSTR)temp + i * gConfig.dwWordLength, gConfig.dwWordLength, g_TextLib.lpWordBuf[i], wlen - wpos);
  680. if (l > 0 && g_TextLib.lpWordBuf[i][l - 1] == '1')
  681. g_TextLib.lpWordBuf[i][l - 1] = 0;
  682. g_TextLib.lpWordBuf[i][l] = 0;
  683. wpos += l + 1;
  684. }
  685. free(temp);
  686. //
  687. // Read the message offsets. The message offsets are in SSS.MKF #3
  688. //
  689. i = PAL_MKFGetChunkSize(3, gpGlobals->f.fpSSS) / sizeof(DWORD);
  690. g_TextLib.nMsgs = i - 1;
  691. offsets = (LPDWORD)malloc(i * sizeof(DWORD));
  692. if (offsets == NULL)
  693. {
  694. free(g_TextLib.lpWordBuf[0]);
  695. free(g_TextLib.lpWordBuf);
  696. fclose(fpMsg);
  697. return -1;
  698. }
  699. PAL_MKFReadChunk((LPBYTE)(offsets), i * sizeof(DWORD), 3, gpGlobals->f.fpSSS);
  700. //
  701. // Read the messages.
  702. //
  703. fseek(fpMsg, 0, SEEK_END);
  704. i = ftell(fpMsg);
  705. temp = (LPBYTE)malloc(i);
  706. if (temp == NULL)
  707. {
  708. free(offsets);
  709. free(g_TextLib.lpWordBuf[0]);
  710. free(g_TextLib.lpWordBuf);
  711. fclose(fpMsg);
  712. return -1;
  713. }
  714. fseek(fpMsg, 0, SEEK_SET);
  715. if (fread(temp, 1, i, fpMsg) < (size_t)i)
  716. {
  717. free(offsets);
  718. free(g_TextLib.lpWordBuf[0]);
  719. free(g_TextLib.lpWordBuf);
  720. fclose(fpMsg);
  721. return -1;
  722. }
  723. fclose(fpMsg);
  724. // Split messages and do code page conversion here
  725. for (i = 0, wlen = 0; i < g_TextLib.nMsgs; i++)
  726. {
  727. wlen += PAL_MultiByteToWideChar((LPCSTR)temp + SDL_SwapLE32(offsets[i]), SDL_SwapLE32(offsets[i + 1]) - SDL_SwapLE32(offsets[i]), NULL, 0) + 1;
  728. }
  729. g_TextLib.lpMsgBuf = (LPWSTR*)malloc(g_TextLib.nMsgs * sizeof(LPWSTR));
  730. if (g_TextLib.lpMsgBuf == NULL)
  731. {
  732. free(g_TextLib.lpWordBuf[0]);
  733. free(g_TextLib.lpWordBuf);
  734. free(offsets);
  735. return -1;
  736. }
  737. tmp = (LPWSTR)malloc(wlen * sizeof(WCHAR));
  738. if (tmp == NULL)
  739. {
  740. free(g_TextLib.lpMsgBuf);
  741. free(g_TextLib.lpWordBuf[0]);
  742. free(g_TextLib.lpWordBuf);
  743. free(offsets);
  744. return -1;
  745. }
  746. for (i = 0, wpos = 0; i < g_TextLib.nMsgs; i++)
  747. {
  748. int l;
  749. g_TextLib.lpMsgBuf[i] = tmp + wpos;
  750. l = PAL_MultiByteToWideChar((LPCSTR)temp + SDL_SwapLE32(offsets[i]), SDL_SwapLE32(offsets[i + 1]) - SDL_SwapLE32(offsets[i]), g_TextLib.lpMsgBuf[i], wlen - wpos);
  751. g_TextLib.lpMsgBuf[i][l] = 0;
  752. wpos += l + 1;
  753. }
  754. free(temp);
  755. free(offsets);
  756. g_TextLib.lpIndexBuf = NULL;
  757. memcpy(g_TextLib.lpWordBuf + SYSMENU_LABEL_LAUNCHSETTING, gc_rgszSDLPalWords[PAL_GetCodePage()], SDLPAL_EXTRA_WORD_COUNT * sizeof(LPCWSTR));
  758. #ifndef PAL_CLASSIC
  759. memcpy(g_TextLib.lpWordBuf + SYSMENU_LABEL_BATTLEMODE, gc_rgszAdditionalWords[PAL_GetCodePage()], ATB_WORD_COUNT * sizeof(LPCWSTR));
  760. #endif
  761. }
  762. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  763. g_TextLib.bIcon = 0;
  764. g_TextLib.posIcon = 0;
  765. g_TextLib.nCurrentDialogLine = 0;
  766. g_TextLib.iDelayTime = 3;
  767. g_TextLib.posDialogTitle = PAL_XY(12, 8);
  768. g_TextLib.posDialogText = PAL_XY(44, 26);
  769. g_TextLib.bDialogPosition = kDialogUpper;
  770. g_TextLib.fUserSkip = FALSE;
  771. PAL_MKFReadChunk(g_TextLib.bufDialogIcons, 282, 12, gpGlobals->f.fpDATA);
  772. return 0;
  773. }
  774. VOID
  775. PAL_FreeText(
  776. VOID
  777. )
  778. /*++
  779. Purpose:
  780. Free the memory used by the texts.
  781. Parameters:
  782. None.
  783. Return value:
  784. None.
  785. --*/
  786. {
  787. int i;
  788. if (g_TextLib.lpMsgBuf != NULL)
  789. {
  790. if (gConfig.pszMsgFile)
  791. for(i = 0; i < g_TextLib.nMsgs; i++) free(g_TextLib.lpMsgBuf[i]);
  792. else
  793. free(g_TextLib.lpMsgBuf[0]);
  794. free(g_TextLib.lpMsgBuf);
  795. g_TextLib.lpMsgBuf = NULL;
  796. }
  797. if (g_TextLib.lpWordBuf != NULL)
  798. {
  799. if (gConfig.pszMsgFile)
  800. for(i = 0; i < g_TextLib.nWords; i++) free(g_TextLib.lpWordBuf[i]);
  801. else
  802. free(g_TextLib.lpWordBuf[0]);
  803. free(g_TextLib.lpWordBuf);
  804. g_TextLib.lpWordBuf = NULL;
  805. }
  806. if (g_TextLib.lpIndexBuf != NULL)
  807. {
  808. if (gConfig.pszMsgFile)
  809. for(i = 0; i < g_TextLib.nIndices; i++) free(g_TextLib.lpIndexBuf[i]);
  810. else
  811. free(g_TextLib.lpIndexBuf[0]);
  812. free(g_TextLib.lpIndexBuf);
  813. g_TextLib.lpIndexBuf = NULL;
  814. }
  815. }
  816. LPCWSTR
  817. PAL_GetWord(
  818. int iNumWord
  819. )
  820. /*++
  821. Purpose:
  822. Get the specified word.
  823. Parameters:
  824. [IN] wNumWord - the number of the requested word.
  825. Return value:
  826. Pointer to the requested word. NULL if not found.
  827. --*/
  828. {
  829. return (iNumWord >= g_TextLib.nWords || !g_TextLib.lpWordBuf[iNumWord]) ? L"" : g_TextLib.lpWordBuf[iNumWord];
  830. }
  831. LPCWSTR
  832. PAL_GetMsg(
  833. int iNumMsg
  834. )
  835. /*++
  836. Purpose:
  837. Get the specified message.
  838. Parameters:
  839. [IN] wNumMsg - the number of the requested message.
  840. Return value:
  841. Pointer to the requested message. NULL if not found.
  842. --*/
  843. {
  844. return (iNumMsg >= g_TextLib.nMsgs || !g_TextLib.lpMsgBuf[iNumMsg]) ? L"" : g_TextLib.lpMsgBuf[iNumMsg];
  845. }
  846. int
  847. PAL_GetMsgNum(
  848. int iIndex,
  849. int iOrder
  850. )
  851. /*++
  852. Purpose:
  853. Get the number of specified message from index & order.
  854. Parameters:
  855. [IN] iMsgIndex - index.
  856. [IN] iOrder - order inside the index.
  857. Return value:
  858. The number of message. Zero means pausing for key, and -1 means end.
  859. --*/
  860. {
  861. return (iIndex >= g_TextLib.nMsgs || !g_TextLib.lpIndexBuf[iIndex]) ? -1 : g_TextLib.lpIndexBuf[iIndex][iOrder];
  862. }
  863. VOID
  864. PAL_DrawText(
  865. LPCWSTR lpszText,
  866. PAL_POS pos,
  867. BYTE bColor,
  868. BOOL fShadow,
  869. BOOL fUpdate,
  870. BOOL fUse8x8Font
  871. )
  872. /*++
  873. Purpose:
  874. Draw text on the screen.
  875. Parameters:
  876. [IN] lpszText - the text to be drawn.
  877. [IN] pos - Position of the text.
  878. [IN] bColor - Color of the text.
  879. [IN] fShadow - TRUE if the text is shadowed or not.
  880. [IN] fUpdate - TRUE if update the screen area.
  881. [IN] fUse8x8Font - TRUE if use 8x8 font.
  882. Return value:
  883. None.
  884. --*/
  885. {
  886. SDL_Rect rect, urect;
  887. urect.x = rect.x = PAL_X(pos);
  888. urect.y = rect.y = PAL_Y(pos);
  889. urect.h = (fUse8x8Font ? 8 : PAL_FontHeight()) + (fShadow ? 1 : 0);
  890. urect.w = 0;
  891. // Handle text overflow
  892. if (rect.x >= 320) return;
  893. while (*lpszText)
  894. {
  895. //
  896. // Draw the character
  897. //
  898. int char_width = fUse8x8Font ? 8 : PAL_CharWidth(*lpszText);
  899. if (fShadow)
  900. {
  901. PAL_DrawCharOnSurface(*lpszText, gpScreen, PAL_XY(rect.x + 1, rect.y + 1), 0, fUse8x8Font);
  902. PAL_DrawCharOnSurface(*lpszText, gpScreen, PAL_XY(rect.x + 1, rect.y), 0, fUse8x8Font);
  903. }
  904. PAL_DrawCharOnSurface(*lpszText++, gpScreen, PAL_XY(rect.x, rect.y), bColor, fUse8x8Font);
  905. rect.x += char_width; urect.w += char_width;
  906. }
  907. //
  908. // Update the screen area
  909. //
  910. if (fUpdate && urect.w > 0)
  911. {
  912. if (fShadow) urect.w++;
  913. if (urect.x + urect.w > 320)
  914. {
  915. urect.w = 320 - urect.x;
  916. }
  917. VIDEO_UpdateScreen(&urect);
  918. }
  919. }
  920. VOID
  921. PAL_DialogSetDelayTime(
  922. INT iDelayTime
  923. )
  924. /*++
  925. Purpose:
  926. Set the delay time for dialog.
  927. Parameters:
  928. [IN] iDelayTime - the delay time to be set.
  929. Return value:
  930. None.
  931. --*/
  932. {
  933. g_TextLib.iDelayTime = iDelayTime;
  934. }
  935. VOID
  936. PAL_StartDialog(
  937. BYTE bDialogLocation,
  938. BYTE bFontColor,
  939. INT iNumCharFace,
  940. BOOL fPlayingRNG
  941. )
  942. /*++
  943. Purpose:
  944. Start a new dialog.
  945. Parameters:
  946. [IN] bDialogLocation - the location of the text on the screen.
  947. [IN] bFontColor - the font color of the text.
  948. [IN] iNumCharFace - number of the character face in RGM.MKF.
  949. [IN] fPlayingRNG - whether we are playing a RNG video or not.
  950. Return value:
  951. None.
  952. --*/
  953. {
  954. PAL_LARGE BYTE buf[16384];
  955. SDL_Rect rect;
  956. if (gpGlobals->fInBattle && !g_fUpdatedInBattle)
  957. {
  958. //
  959. // Update the screen in battle, or the graphics may seem messed up
  960. //
  961. VIDEO_UpdateScreen(NULL);
  962. g_fUpdatedInBattle = TRUE;
  963. }
  964. g_TextLib.bIcon = 0;
  965. g_TextLib.posIcon = 0;
  966. g_TextLib.nCurrentDialogLine = 0;
  967. g_TextLib.posDialogTitle = PAL_XY(12, 8);
  968. g_TextLib.fUserSkip = FALSE;
  969. if (bFontColor != 0)
  970. {
  971. g_TextLib.bCurrentFontColor = bFontColor;
  972. }
  973. if (fPlayingRNG && iNumCharFace)
  974. {
  975. VIDEO_BackupScreen(gpScreen);
  976. g_TextLib.fPlayingRNG = TRUE;
  977. }
  978. switch (bDialogLocation)
  979. {
  980. case kDialogUpper:
  981. if (iNumCharFace > 0)
  982. {
  983. //
  984. // Display the character face at the upper part of the screen
  985. //
  986. if (PAL_MKFReadChunk(buf, 16384, iNumCharFace, gpGlobals->f.fpRGM) > 0)
  987. {
  988. rect.w = PAL_RLEGetWidth((LPCBITMAPRLE)buf);
  989. rect.h = PAL_RLEGetHeight((LPCBITMAPRLE)buf);
  990. rect.x = 48 - rect.w / 2;
  991. rect.y = 55 - rect.h / 2;
  992. if (rect.x < 0)
  993. {
  994. rect.x = 0;
  995. }
  996. if (rect.y < 0)
  997. {
  998. rect.y = 0;
  999. }
  1000. PAL_RLEBlitToSurface((LPCBITMAPRLE)buf, gpScreen, PAL_XY(rect.x, rect.y));
  1001. if (rect.x < 0)
  1002. {
  1003. rect.x = 0;
  1004. }
  1005. if (rect.y < 0)
  1006. {
  1007. rect.y = 0;
  1008. }
  1009. VIDEO_UpdateScreen(&rect);
  1010. }
  1011. }
  1012. g_TextLib.posDialogTitle = PAL_XY(iNumCharFace > 0 ? 80 : 12, 8);
  1013. g_TextLib.posDialogText = PAL_XY(iNumCharFace > 0 ? 96 : 44, 26);
  1014. break;
  1015. case kDialogCenter:
  1016. g_TextLib.posDialogText = PAL_XY(80, 40);
  1017. break;
  1018. case kDialogLower:
  1019. if (iNumCharFace > 0)
  1020. {
  1021. //
  1022. // Display the character face at the lower part of the screen
  1023. //
  1024. if (PAL_MKFReadChunk(buf, 16384, iNumCharFace, gpGlobals->f.fpRGM) > 0)
  1025. {
  1026. rect.x = 270 - PAL_RLEGetWidth((LPCBITMAPRLE)buf) / 2;
  1027. rect.y = 144 - PAL_RLEGetHeight((LPCBITMAPRLE)buf) / 2;
  1028. PAL_RLEBlitToSurface((LPCBITMAPRLE)buf, gpScreen, PAL_XY(rect.x, rect.y));
  1029. VIDEO_UpdateScreen(NULL);
  1030. }
  1031. }
  1032. g_TextLib.posDialogTitle = PAL_XY(iNumCharFace > 0 ? 4 : 12, 108);
  1033. g_TextLib.posDialogText = PAL_XY(iNumCharFace > 0 ? 20 : 44, 126);
  1034. break;
  1035. case kDialogCenterWindow:
  1036. g_TextLib.posDialogText = PAL_XY(160, 40);
  1037. break;
  1038. }
  1039. g_TextLib.bDialogPosition = bDialogLocation;
  1040. }
  1041. static VOID
  1042. PAL_DialogWaitForKeyWithMaximumSeconds(
  1043. FLOAT fMaxSeconds
  1044. )
  1045. /*++
  1046. Purpose:
  1047. Wait for player to press a key after showing a dialog.
  1048. Parameters:
  1049. None.
  1050. Return value:
  1051. None.
  1052. --*/
  1053. {
  1054. PAL_LARGE SDL_Color palette[256];
  1055. SDL_Color *pCurrentPalette, t;
  1056. int i;
  1057. uint32_t dwBeginningTicks = SDL_GetTicks();
  1058. //
  1059. // get the current palette
  1060. //
  1061. pCurrentPalette = PAL_GetPalette(gpGlobals->wNumPalette, gpGlobals->fNightPalette);
  1062. memcpy(palette, pCurrentPalette, sizeof(palette));
  1063. if (g_TextLib.bDialogPosition != kDialogCenterWindow &&
  1064. g_TextLib.bDialogPosition != kDialogCenter)
  1065. {
  1066. //
  1067. // show the icon
  1068. //
  1069. LPCBITMAPRLE p = PAL_SpriteGetFrame(g_TextLib.bufDialogIcons, g_TextLib.bIcon);
  1070. if (p != NULL)
  1071. {
  1072. SDL_Rect rect;
  1073. rect.x = PAL_X(g_TextLib.posIcon);
  1074. rect.y = PAL_Y(g_TextLib.posIcon);
  1075. rect.w = 16;
  1076. rect.h = 16;
  1077. PAL_RLEBlitToSurface(p, gpScreen, g_TextLib.posIcon);
  1078. VIDEO_UpdateScreen(&rect);
  1079. }
  1080. }
  1081. PAL_ClearKeyState();
  1082. while (TRUE)
  1083. {
  1084. UTIL_Delay(100);
  1085. if (g_TextLib.bDialogPosition != kDialogCenterWindow &&
  1086. g_TextLib.bDialogPosition != kDialogCenter)
  1087. {
  1088. //
  1089. // palette shift
  1090. //
  1091. t = palette[0xF9];
  1092. for (i = 0xF9; i < 0xFE; i++)
  1093. {
  1094. palette[i] = palette[i + 1];
  1095. }
  1096. palette[0xFE] = t;
  1097. VIDEO_SetPalette(palette);
  1098. }
  1099. if (fabs(fMaxSeconds) > FLT_EPSILON && SDL_GetTicks() - dwBeginningTicks > 1000 * fMaxSeconds)
  1100. {
  1101. break;
  1102. }
  1103. if (g_InputState.dwKeyPress != 0)
  1104. {
  1105. break;
  1106. }
  1107. }
  1108. if (g_TextLib.bDialogPosition != kDialogCenterWindow &&
  1109. g_TextLib.bDialogPosition != kDialogCenter)
  1110. {
  1111. PAL_SetPalette(gpGlobals->wNumPalette, gpGlobals->fNightPalette);
  1112. }
  1113. PAL_ClearKeyState();
  1114. g_TextLib.fUserSkip = FALSE;
  1115. }
  1116. static VOID
  1117. PAL_DialogWaitForKey(
  1118. VOID
  1119. )
  1120. {
  1121. PAL_DialogWaitForKeyWithMaximumSeconds(0);
  1122. }
  1123. VOID
  1124. PAL_ShowDialogText(
  1125. LPCWSTR lpszText
  1126. )
  1127. /*++
  1128. Purpose:
  1129. Show one line of the dialog text.
  1130. Parameters:
  1131. [IN] lpszText - the text to be shown.
  1132. Return value:
  1133. None.
  1134. --*/
  1135. {
  1136. SDL_Rect rect;
  1137. int x, y;
  1138. PAL_ClearKeyState();
  1139. g_TextLib.bIcon = 0;
  1140. if (gpGlobals->fInBattle && !g_fUpdatedInBattle)
  1141. {
  1142. //
  1143. // Update the screen in battle, or the graphics may seem messed up
  1144. //
  1145. VIDEO_UpdateScreen(NULL);
  1146. g_fUpdatedInBattle = TRUE;
  1147. }
  1148. if (g_TextLib.nCurrentDialogLine > 3)
  1149. {
  1150. //
  1151. // The rest dialogs should be shown in the next page.
  1152. //
  1153. PAL_DialogWaitForKey();
  1154. g_TextLib.nCurrentDialogLine = 0;
  1155. VIDEO_RestoreScreen(gpScreen);
  1156. VIDEO_UpdateScreen(NULL);
  1157. }
  1158. x = PAL_X(g_TextLib.posDialogText);
  1159. y = PAL_Y(g_TextLib.posDialogText) + g_TextLib.nCurrentDialogLine * 18;
  1160. if (g_TextLib.bDialogPosition == kDialogCenterWindow)
  1161. {
  1162. //
  1163. // The text should be shown in a small window at the center of the screen
  1164. //
  1165. #ifndef PAL_CLASSIC
  1166. if (gpGlobals->fInBattle && g_Battle.BattleResult == kBattleResultOnGoing)
  1167. {
  1168. PAL_BattleUIShowText(lpszText, 1400);
  1169. }
  1170. else
  1171. #endif
  1172. {
  1173. PAL_POS pos;
  1174. LPBOX lpBox;
  1175. int i, w = wcslen(lpszText), len = 0;
  1176. for (i = 0; i < w; i++)
  1177. len += PAL_CharWidth(lpszText[i]) >> 3;
  1178. //
  1179. // Create the window box
  1180. //
  1181. pos = PAL_XY(PAL_X(g_TextLib.posDialogText) - len * 4, PAL_Y(g_TextLib.posDialogText));
  1182. // Follow behavior of original version
  1183. lpBox = PAL_CreateSingleLineBoxWithShadow(pos, (len + 1) / 2, FALSE, 0);
  1184. rect.x = PAL_X(pos);
  1185. rect.y = PAL_Y(pos);
  1186. rect.w = 320 - rect.x * 2 + 32;
  1187. rect.h = 64;
  1188. //
  1189. // Show the text on the screen
  1190. //
  1191. pos = PAL_XY(PAL_X(pos) + 8 + ((len & 1) << 2), PAL_Y(pos) + 10);
  1192. PAL_DrawText(lpszText, pos, 0, FALSE, FALSE, FALSE);
  1193. VIDEO_UpdateScreen(&rect);
  1194. PAL_DialogWaitForKeyWithMaximumSeconds(1.4);
  1195. //
  1196. // Delete the box
  1197. //
  1198. PAL_DeleteBox(lpBox);
  1199. VIDEO_UpdateScreen(&rect);
  1200. PAL_EndDialog();
  1201. }
  1202. }
  1203. else
  1204. {
  1205. int len = wcslen(lpszText);
  1206. if (g_TextLib.nCurrentDialogLine == 0 &&
  1207. g_TextLib.bDialogPosition != kDialogCenter &&
  1208. (lpszText[len - 1] == 0xff1a ||
  1209. lpszText[len - 1] == 0x2236 || // Special case for Pal WIN95 Simplified Chinese version
  1210. lpszText[len - 1] == ':')
  1211. )
  1212. {
  1213. //
  1214. // name of character
  1215. //
  1216. PAL_DrawText(lpszText, g_TextLib.posDialogTitle, FONT_COLOR_CYAN_ALT, TRUE, TRUE, FALSE);
  1217. }
  1218. else
  1219. {
  1220. //
  1221. // normal texts
  1222. //
  1223. WCHAR text[2];
  1224. if (!g_TextLib.fPlayingRNG && g_TextLib.nCurrentDialogLine == 0)
  1225. {
  1226. //
  1227. // Save the screen before we show the first line of dialog
  1228. //
  1229. VIDEO_BackupScreen(gpScreen);
  1230. }
  1231. while (lpszText != NULL && *lpszText != '\0')
  1232. {
  1233. switch (*lpszText)
  1234. {
  1235. case '-':
  1236. //
  1237. // Set the font color to Cyan
  1238. //
  1239. if (g_TextLib.bCurrentFontColor == FONT_COLOR_CYAN)
  1240. {
  1241. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  1242. }
  1243. else
  1244. {
  1245. g_TextLib.bCurrentFontColor = FONT_COLOR_CYAN;
  1246. }
  1247. lpszText++;
  1248. break;
  1249. #if 0
  1250. /* Not used */
  1251. case '\'':
  1252. //
  1253. // Set the font color to Red
  1254. //
  1255. if (g_TextLib.bCurrentFontColor == FONT_COLOR_RED)
  1256. {
  1257. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  1258. }
  1259. else
  1260. {
  1261. g_TextLib.bCurrentFontColor = FONT_COLOR_RED;
  1262. }
  1263. lpszText++;
  1264. break;
  1265. #endif
  1266. case '\"':
  1267. //
  1268. // Set the font color to Yellow
  1269. //
  1270. if (g_TextLib.bCurrentFontColor == FONT_COLOR_YELLOW)
  1271. {
  1272. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  1273. }
  1274. else
  1275. {
  1276. g_TextLib.bCurrentFontColor = FONT_COLOR_YELLOW;
  1277. }
  1278. lpszText++;
  1279. break;
  1280. case '$':
  1281. //
  1282. // Set the delay time of text-displaying
  1283. //
  1284. g_TextLib.iDelayTime = wcstol(lpszText + 1, NULL, 10) * 10 / 7;
  1285. lpszText += 3;
  1286. break;
  1287. case '~':
  1288. //
  1289. // Delay for a period and quit
  1290. //
  1291. if (g_TextLib.fUserSkip)
  1292. {
  1293. VIDEO_UpdateScreen(NULL);
  1294. }
  1295. UTIL_Delay(wcstol(lpszText + 1, NULL, 10) * 80 / 7);
  1296. g_TextLib.nCurrentDialogLine = 0;
  1297. g_TextLib.fUserSkip = FALSE;
  1298. return; // don't go further
  1299. case ')':
  1300. //
  1301. // Set the waiting icon
  1302. //
  1303. g_TextLib.bIcon = 1;
  1304. lpszText++;
  1305. break;
  1306. case '(':
  1307. //
  1308. // Set the waiting icon
  1309. //
  1310. g_TextLib.bIcon = 2;
  1311. lpszText++;
  1312. break;
  1313. case '\\':
  1314. lpszText++;
  1315. default:
  1316. text[0] = *lpszText++;
  1317. text[1] = 0;
  1318. // Update the screen on each draw operation is time-consuming, so disable it if user want to skip
  1319. PAL_DrawText(text, PAL_XY(x, y), g_TextLib.bCurrentFontColor, TRUE, !g_TextLib.fUserSkip, FALSE);
  1320. x += PAL_CharWidth(text[0]);
  1321. if (!g_TextLib.fUserSkip)
  1322. {
  1323. PAL_ClearKeyState();
  1324. UTIL_Delay(g_TextLib.iDelayTime * 8);
  1325. if (g_InputState.dwKeyPress & (kKeySearch | kKeyMenu))
  1326. {
  1327. //
  1328. // User pressed a key to skip the dialog
  1329. //
  1330. g_TextLib.fUserSkip = TRUE;
  1331. }
  1332. }
  1333. }
  1334. }
  1335. // and update the full screen at once after all texts are drawn
  1336. if (g_TextLib.fUserSkip)
  1337. {
  1338. VIDEO_UpdateScreen(NULL);
  1339. }
  1340. g_TextLib.posIcon = PAL_XY(x, y);
  1341. g_TextLib.nCurrentDialogLine++;
  1342. }
  1343. }
  1344. }
  1345. VOID
  1346. PAL_ClearDialog(
  1347. BOOL fWaitForKey
  1348. )
  1349. /*++
  1350. Purpose:
  1351. Clear the state of the dialog.
  1352. Parameters:
  1353. [IN] fWaitForKey - whether wait for any key or not.
  1354. Return value:
  1355. None.
  1356. --*/
  1357. {
  1358. if (g_TextLib.nCurrentDialogLine > 0 && fWaitForKey)
  1359. {
  1360. PAL_DialogWaitForKey();
  1361. }
  1362. g_TextLib.nCurrentDialogLine = 0;
  1363. if (g_TextLib.bDialogPosition == kDialogCenter)
  1364. {
  1365. g_TextLib.posDialogTitle = PAL_XY(12, 8);
  1366. g_TextLib.posDialogText = PAL_XY(44, 26);
  1367. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  1368. g_TextLib.bDialogPosition = kDialogUpper;
  1369. }
  1370. }
  1371. VOID
  1372. PAL_EndDialog(
  1373. VOID
  1374. )
  1375. /*++
  1376. Purpose:
  1377. Ends a dialog.
  1378. Parameters:
  1379. None.
  1380. Return value:
  1381. None.
  1382. --*/
  1383. {
  1384. PAL_ClearDialog(TRUE);
  1385. //
  1386. // Set some default parameters, as there are some parts of script
  1387. // which doesn't have a "start dialog" instruction before showing the dialog.
  1388. //
  1389. g_TextLib.posDialogTitle = PAL_XY(12, 8);
  1390. g_TextLib.posDialogText = PAL_XY(44, 26);
  1391. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  1392. g_TextLib.bDialogPosition = kDialogUpper;
  1393. g_TextLib.fUserSkip = FALSE;
  1394. g_TextLib.fPlayingRNG = FALSE;
  1395. }
  1396. BOOL
  1397. PAL_IsInDialog(
  1398. VOID
  1399. )
  1400. /*++
  1401. Purpose:
  1402. Check if there are dialog texts on the screen.
  1403. Parameters:
  1404. None.
  1405. Return value:
  1406. TRUE if there are dialog texts on the screen, FALSE if not.
  1407. --*/
  1408. {
  1409. return (g_TextLib.nCurrentDialogLine != 0);
  1410. }
  1411. BOOL
  1412. PAL_DialogIsPlayingRNG(
  1413. VOID
  1414. )
  1415. /*++
  1416. Purpose:
  1417. Check if the script used the RNG playing parameter when displaying texts.
  1418. Parameters:
  1419. None.
  1420. Return value:
  1421. TRUE if the script used the RNG playing parameter, FALSE if not.
  1422. --*/
  1423. {
  1424. return g_TextLib.fPlayingRNG;
  1425. }
  1426. WCHAR
  1427. PAL_GetInvalidChar(
  1428. CODEPAGE uCodePage
  1429. )
  1430. {
  1431. switch (uCodePage)
  1432. {
  1433. case CP_BIG5: return 0x3f;
  1434. case CP_GBK: return 0x3f;
  1435. //case CP_SHIFTJIS: return 0x30fb;
  1436. case CP_UTF_8: return 0x3f;
  1437. case CP_UCS: return 0x3f;
  1438. default: return 0;
  1439. }
  1440. }
  1441. static CODEPAGE g_codepage = CP_UTF_8;
  1442. CODEPAGE
  1443. PAL_GetCodePage(
  1444. void
  1445. )
  1446. {
  1447. return g_codepage;
  1448. }
  1449. void
  1450. PAL_SetCodePage(
  1451. CODEPAGE uCodePage
  1452. )
  1453. {
  1454. g_codepage = uCodePage;
  1455. }
  1456. CODEPAGE
  1457. PAL_DetectCodePageForString(
  1458. const char * text,
  1459. int text_len,
  1460. CODEPAGE default_cp,
  1461. int * probability
  1462. )
  1463. {
  1464. // Try to convert the content of word.dat with different codepages,
  1465. // and use the codepage with minimal inconvertible characters
  1466. // Works fine currently for detecting Simplified Chinese & Traditional Chinese.
  1467. // Since we're using language files to support additional languages, this detection
  1468. // should be fine for us now.
  1469. int min_invalids = INT_MAX;
  1470. if (text && text_len > 0)
  1471. {
  1472. // The file to be detected should not contain characters outside these ranges
  1473. const static int valid_ranges[][2] = {
  1474. { 0x4E00, 0x9FFF }, // CJK Unified Ideographs
  1475. { 0x3400, 0x4DBF }, // CJK Unified Ideographs Extension A
  1476. { 0xF900, 0xFAFF }, // CJK Compatibility Ideographs
  1477. { 0x0020, 0x007E }, // Basic ASCII
  1478. { 0x3000, 0x301E }, // CJK Symbols
  1479. { 0xFF01, 0xFF5E }, // Fullwidth Forms
  1480. };
  1481. for (CODEPAGE i = CP_BIG5; i <= CP_GBK; i++)
  1482. {
  1483. int invalids, length = PAL_MultiByteToWideCharCP(i, text, text_len, NULL, 0);
  1484. WCHAR *wbuf = (WCHAR *)malloc(length * sizeof(WCHAR));
  1485. PAL_MultiByteToWideCharCP(i, text, text_len, wbuf, length);
  1486. for (int j = invalids = 0; j < length; j++)
  1487. {
  1488. int score = 1;
  1489. for (int k = 0; k < sizeof(valid_ranges) / sizeof(valid_ranges[0]); k++)
  1490. {
  1491. if (wbuf[j] >= valid_ranges[k][0] &&
  1492. wbuf[j] <= valid_ranges[k][1])
  1493. {
  1494. score = 0;
  1495. break;
  1496. }
  1497. }
  1498. invalids += score;
  1499. }
  1500. // code page with less invalid chars wins
  1501. if (invalids < min_invalids)
  1502. {
  1503. min_invalids = invalids;
  1504. default_cp = i;
  1505. }
  1506. free(wbuf);
  1507. }
  1508. }
  1509. if (probability)
  1510. {
  1511. if (min_invalids < text_len / 2)
  1512. *probability = (text_len / 2 - min_invalids) * 200 / text_len;
  1513. else
  1514. *probability = 0;
  1515. }
  1516. return default_cp;
  1517. }
  1518. INT
  1519. PAL_MultiByteToWideCharCP(
  1520. CODEPAGE cp,
  1521. LPCSTR mbs,
  1522. int mbslength,
  1523. LPWSTR wcs,
  1524. int wcslength
  1525. )
  1526. /*++
  1527. Purpose:
  1528. Convert multi-byte string into the corresponding unicode string.
  1529. Parameters:
  1530. [IN] cp - Code page for conversion.
  1531. [IN] mbs - Pointer to the multi-byte string.
  1532. [IN] mbslength - Length of the multi-byte string, or -1 for auto-detect.
  1533. [IN] wcs - Pointer to the wide string buffer.
  1534. [IN] wcslength - Length of the wide string buffer.
  1535. Return value:
  1536. The length of converted wide string. If mbslength is set to -1, the returned
  1537. value includes the terminal null-char; otherwise, the null-char is not included.
  1538. If wcslength is set to 0, wcs can be set to NULL and the return value is the
  1539. required length of the wide string buffer.
  1540. --*/
  1541. {
  1542. int i = 0, state = 0, wlen = 0, null = 0;
  1543. if (mbslength == -1)
  1544. {
  1545. mbslength = strlen(mbs);
  1546. null = 1;
  1547. }
  1548. if (!wcs)
  1549. {
  1550. switch (cp)
  1551. {
  1552. //case CP_SHIFTJIS:
  1553. // for (i = 0; i < mbslength && mbs[i]; i++)
  1554. // {
  1555. // if (state == 0)
  1556. // {
  1557. // if ((BYTE)mbs[i] <= 0x80 || (BYTE)mbs[i] >= 0xfd || ((BYTE)mbs[i] >= 0xa0 && (BYTE)mbs[i] <= 0xdf))
  1558. // wlen++;
  1559. // else
  1560. // state = 1;
  1561. // }
  1562. // else
  1563. // {
  1564. // wlen++;
  1565. // state = 0;
  1566. // }
  1567. // }
  1568. // break;
  1569. case CP_GBK:
  1570. case CP_BIG5:
  1571. for (i = 0; i < mbslength && mbs[i]; i++)
  1572. {
  1573. if (state == 0)
  1574. {
  1575. if ((BYTE)mbs[i] <= 0x80 || (BYTE)mbs[i] == 0xff)
  1576. wlen++;
  1577. else
  1578. state = 1;
  1579. }
  1580. else
  1581. {
  1582. wlen++;
  1583. state = 0;
  1584. }
  1585. }
  1586. break;
  1587. case CP_UTF_8:
  1588. for (i = 0; i < mbslength && mbs[i]; i++)
  1589. {
  1590. if (state == 0)
  1591. {
  1592. if ((BYTE)mbs[i] >= 0x80)
  1593. {
  1594. BYTE s = (BYTE)mbs[i] << 1;
  1595. while (s >= 0x80) { state++; s <<= 1; }
  1596. if (state < 1 || state > 3)
  1597. {
  1598. state = 0;
  1599. wlen++;
  1600. }
  1601. }
  1602. else
  1603. wlen++;
  1604. }
  1605. else
  1606. {
  1607. if ((BYTE)mbs[i] >= 0x80 && (BYTE)mbs[i] < 0xc0)
  1608. {
  1609. if (--state == 0) wlen++;
  1610. }
  1611. else
  1612. {
  1613. state = 0; wlen++;
  1614. }
  1615. }
  1616. }
  1617. break;
  1618. case CP_UCS:
  1619. i = mbslength;
  1620. wlen = mbslength/2;
  1621. break;
  1622. default:
  1623. return -1;
  1624. }
  1625. if (i < mbslength && !mbs[i]) null = 1;
  1626. return wlen + null + (state != 0);
  1627. }
  1628. else
  1629. {
  1630. WCHAR invalid_char;
  1631. switch (cp)
  1632. {
  1633. //case CP_SHIFTJIS:
  1634. // invalid_char = 0x30fb;
  1635. // for (i = 0; i < mbslength && wlen < wcslength && mbs[i]; i++)
  1636. // {
  1637. // if (state == 0)
  1638. // {
  1639. // if ((BYTE)mbs[i] <= 0x80)
  1640. // wcs[wlen++] = mbs[i];
  1641. // else if ((BYTE)mbs[i] >= 0xa0 && (BYTE)mbs[i] <= 0xdf)
  1642. // wcs[wlen++] = cptbl_jis_half[(BYTE)mbs[i] - 0xa0];
  1643. // else if ((BYTE)mbs[i] == 0xfd)
  1644. // wcs[wlen++] = 0xf8f1;
  1645. // else if ((BYTE)mbs[i] == 0xfe)
  1646. // wcs[wlen++] = 0xf8f2;
  1647. // else if ((BYTE)mbs[i] == 0xff)
  1648. // wcs[wlen++] = 0xf8f3;
  1649. // else
  1650. // state = 1;
  1651. // }
  1652. // else
  1653. // {
  1654. // if ((BYTE)mbs[i] < 0x40)
  1655. // wcs[wlen++] = 0x30fb;
  1656. // else if ((BYTE)mbs[i - 1] < 0xa0)
  1657. // wcs[wlen++] = cptbl_jis[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x40];
  1658. // else
  1659. // wcs[wlen++] = cptbl_jis[(BYTE)mbs[i - 1] - 0xc1][(BYTE)mbs[i] - 0x40];
  1660. // state = 0;
  1661. // }
  1662. // }
  1663. // break;
  1664. case CP_GBK:
  1665. invalid_char = 0x3f;
  1666. for (i = 0; i < mbslength && wlen < wcslength && mbs[i]; i++)
  1667. {
  1668. if (state == 0)
  1669. {
  1670. if ((BYTE)mbs[i] < 0x80)
  1671. wcs[wlen++] = mbs[i];
  1672. else if ((BYTE)mbs[i] == 0x80)
  1673. wcs[wlen++] = 0x20ac;
  1674. else if ((BYTE)mbs[i] == 0xff)
  1675. wcs[wlen++] = 0xf8f5;
  1676. else
  1677. state = 1;
  1678. }
  1679. else
  1680. {
  1681. if ((BYTE)mbs[i] < 0x40)
  1682. wcs[wlen++] = invalid_char;
  1683. else
  1684. wcs[wlen++] = cptbl_gbk[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x40];
  1685. state = 0;
  1686. }
  1687. }
  1688. break;
  1689. case CP_BIG5:
  1690. invalid_char = 0x3f;
  1691. for (i = 0; i < mbslength && wlen < wcslength && mbs[i]; i++)
  1692. {
  1693. if (state == 0)
  1694. {
  1695. if ((BYTE)mbs[i] <= 0x80)
  1696. wcs[wlen++] = mbs[i];
  1697. else if ((BYTE)mbs[i] == 0xff)
  1698. wcs[wlen++] = 0xf8f8;
  1699. else
  1700. state = 1;
  1701. }
  1702. else
  1703. {
  1704. if ((BYTE)mbs[i] < 0x40 || ((BYTE)mbs[i] >= 0x7f && (BYTE)mbs[i] <= 0xa0))
  1705. wcs[wlen++] = invalid_char;
  1706. else if ((BYTE)mbs[i] <= 0x7e)
  1707. wcs[wlen++] = cptbl_big5[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x40];
  1708. else
  1709. wcs[wlen++] = cptbl_big5[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x60];
  1710. state = 0;
  1711. }
  1712. }
  1713. break;
  1714. case CP_UTF_8:
  1715. invalid_char = 0x3f;
  1716. for (i = 0; i < mbslength && wlen < wcslength && mbs[i]; i++)
  1717. {
  1718. if (state == 0)
  1719. {
  1720. if ((BYTE)mbs[i] >= 0x80)
  1721. {
  1722. BYTE s = (BYTE)mbs[i] << 1;
  1723. while (s >= 0x80) { state++; s <<= 1; }
  1724. if (state < 1 || state > 3)
  1725. {
  1726. state = 0;
  1727. wcs[wlen++] = invalid_char;
  1728. }
  1729. else
  1730. {
  1731. wcs[wlen] = s >> (state + 1);
  1732. }
  1733. }
  1734. else
  1735. wcs[wlen++] = mbs[i];
  1736. }
  1737. else
  1738. {
  1739. if ((BYTE)mbs[i] >= 0x80 && (BYTE)mbs[i] < 0xc0)
  1740. {
  1741. wcs[wlen] <<= 6;
  1742. wcs[wlen] |= (BYTE)mbs[i] & 0x3f;
  1743. if (--state == 0) wlen++;
  1744. }
  1745. else
  1746. {
  1747. state = 0;
  1748. wcs[wlen++] = invalid_char;
  1749. }
  1750. }
  1751. }
  1752. break;
  1753. case CP_UCS:
  1754. for (i = 0; i < mbslength && wlen < wcslength; i+=2){
  1755. uint8_t *ptr = (uint8_t*)&wcs[wlen++];
  1756. *(ptr+1)=mbs[i];
  1757. *ptr =mbs[i+1];
  1758. }
  1759. break;
  1760. default:
  1761. return -1;
  1762. }
  1763. if (state != 0 && wlen < wcslength)
  1764. {
  1765. wcs[wlen++] = invalid_char;
  1766. }
  1767. if (null || (i < mbslength && !mbs[i]))
  1768. {
  1769. if (wlen < wcslength)
  1770. wcs[wlen++] = 0;
  1771. else
  1772. wcs[wlen - 1] = 0;
  1773. }
  1774. return wlen;
  1775. }
  1776. }
  1777. INT
  1778. PAL_MultiByteToWideChar(
  1779. LPCSTR mbs,
  1780. int mbslength,
  1781. LPWSTR wcs,
  1782. int wcslength
  1783. )
  1784. /*++
  1785. Purpose:
  1786. Convert multi-byte string into the corresponding unicode string.
  1787. Parameters:
  1788. [IN] mbs - Pointer to the multi-byte string.
  1789. [IN] mbslength - Length of the multi-byte string, or -1 for auto-detect.
  1790. [IN] wcs - Pointer to the wide string buffer.
  1791. [IN] wcslength - Length of the wide string buffer.
  1792. Return value:
  1793. The length of converted wide string. If mbslength is set to -1, the returned
  1794. value includes the terminal null-char; otherwise, the null-char is not included.
  1795. If wcslength is set to 0, wcs can be set to NULL and the return value is the
  1796. required length of the wide string buffer.
  1797. --*/
  1798. {
  1799. return PAL_MultiByteToWideCharCP(g_codepage, mbs, mbslength, wcs, wcslength);
  1800. }
  1801. INT
  1802. PAL_swprintf(
  1803. LPWSTR buffer,
  1804. size_t count,
  1805. LPCWSTR format,
  1806. ...
  1807. )
  1808. /*++
  1809. Purpose:
  1810. Formatted wide-character output conversion that output Chinese characters correctly.
  1811. This function supported a subset of format strings that are commonly supported by
  1812. various C libraries, which can be formalized as following:
  1813. %[flags] [width] [.precision] [{h | l | ll}] type
  1814. When handling '%c' and '%s', this function follows the Linux's library convention,
  1815. which means '%c' and '%s' always output multi-byte strings, and '%lc' and '%ls'
  1816. always output wide-char strings.
  1817. Parameters:
  1818. [IN] buffer - Storage location for output.
  1819. [IN] count - Length of the output buffer in characters including the termination null one.
  1820. [IN] format - Format-control string.
  1821. [IN] ... - Optional arguments.
  1822. Return value:
  1823. The length of outputed wide string, not including the termination null character.
  1824. --*/
  1825. {
  1826. va_list ap;
  1827. const WCHAR * const format_end = format + wcslen(format);
  1828. const WCHAR * const buffer_end = buffer + count - 1;
  1829. WCHAR chr_buf[2] = { 0, 0 };
  1830. LPCWSTR fmt_start = NULL;
  1831. LPWSTR cur_fmt = NULL;
  1832. size_t fmt_len = 0;
  1833. int state, precision = 0, width = 0;
  1834. int left_aligned = 0, wide = 0, narrow = 0;
  1835. int width_var = 0, precision_var = 0, precision_defined = 0;
  1836. // Buffer & length check
  1837. if (buffer == NULL || format == NULL)
  1838. {
  1839. errno = EINVAL;
  1840. return -1;
  1841. }
  1842. if (buffer_end <= buffer)
  1843. return 0;
  1844. va_start(ap, format);
  1845. count = 0; state = 0;
  1846. while (buffer < buffer_end && format < format_end)
  1847. {
  1848. switch (state)
  1849. {
  1850. case 0: // Outside format spec
  1851. if (*format != L'%')
  1852. {
  1853. *buffer++ = *format++;
  1854. count++;
  1855. }
  1856. else
  1857. {
  1858. fmt_start = format++;
  1859. left_aligned = wide = narrow = 0;
  1860. precision_var = width_var = 0;
  1861. precision_defined = 0;
  1862. state = 1;
  1863. }
  1864. continue;
  1865. case 1: // [flags]
  1866. switch (*format)
  1867. {
  1868. case L'-':
  1869. left_aligned = 1;
  1870. case L'+':
  1871. case L' ':
  1872. case L'#':
  1873. case L'0':
  1874. format++;
  1875. continue;
  1876. default:
  1877. state = 2;
  1878. width = width_var = 0;
  1879. }
  1880. case 2: // [width]
  1881. switch (*format)
  1882. {
  1883. case '0':
  1884. case '1':
  1885. case '2':
  1886. case '3':
  1887. case '4':
  1888. case '5':
  1889. case '6':
  1890. case '7':
  1891. case '8':
  1892. case '9':
  1893. if (width >= 0)
  1894. width = width * 10 + (*format - L'0');
  1895. format++;
  1896. continue;
  1897. case '*':
  1898. if (width == 0)
  1899. width_var = 1;
  1900. format++;
  1901. continue;
  1902. case '.':
  1903. format++;
  1904. precision = precision_var = 0;
  1905. precision_defined = 1;
  1906. state = 3;
  1907. continue;
  1908. default:
  1909. state = 4;
  1910. continue;
  1911. }
  1912. case 3: // [.precision]
  1913. switch (*format)
  1914. {
  1915. case '0':
  1916. case '1':
  1917. case '2':
  1918. case '3':
  1919. case '4':
  1920. case '5':
  1921. case '6':
  1922. case '7':
  1923. case '8':
  1924. case '9':
  1925. if (precision >= 0)
  1926. precision = precision * 10 + (*format - L'0');
  1927. format++;
  1928. continue;
  1929. case '*':
  1930. if (precision == 0)
  1931. precision_var = 1;
  1932. format++;
  1933. continue;
  1934. default:
  1935. state = 4;
  1936. }
  1937. case 4: // [{h | l | ll}]
  1938. switch (*format)
  1939. {
  1940. case 'l': if (narrow == 0) wide++; format++; continue;
  1941. case 'h': if (wide == 0) narrow++; format++; continue;
  1942. default: state = 5;
  1943. }
  1944. case 5: // type
  1945. if (*format == 'c' || *format == 's')
  1946. {
  1947. // We handle char & str specially
  1948. LPWSTR buf;
  1949. size_t len;
  1950. int i;
  1951. // Check width
  1952. if (width_var)
  1953. {
  1954. width = va_arg(ap, int);
  1955. left_aligned = (width < 0);
  1956. width = left_aligned ? -width : width;
  1957. }
  1958. // Although precision has no meaning to '%c' output, however
  1959. // the argument still needs to be read if '.*' is provided
  1960. if (precision_var)
  1961. precision = va_arg(ap, int);
  1962. else if (!precision_defined)
  1963. precision = INT_MAX;
  1964. if (*format == 's')
  1965. {
  1966. // For ANSI string, convert it through PAL_MultiByteToWideChar
  1967. // To improve effciency, here just test the length and left
  1968. // actual conversion later directly into the output buffer
  1969. if (wide)
  1970. {
  1971. buf = va_arg(ap, LPWSTR);
  1972. len = wcslen(buf);
  1973. }
  1974. else
  1975. {
  1976. buf = (LPWSTR)va_arg(ap, LPSTR);
  1977. len = PAL_MultiByteToWideChar((LPCSTR)buf, -1, NULL, 0) - 1;
  1978. }
  1979. }
  1980. else
  1981. {
  1982. // For ANSI character, put it into the internal buffer
  1983. if (wide)
  1984. chr_buf[0] = va_arg(ap, WCHAR);
  1985. else
  1986. chr_buf[0] = va_arg(ap, int);
  1987. buf = chr_buf; len = 1;
  1988. }
  1989. // Limit output length no longer then precision
  1990. if (precision > (int)len)
  1991. precision = len;
  1992. // Left-side padding
  1993. for (i = 0; !left_aligned && i < width - precision && buffer < buffer_end; i++)
  1994. *buffer++ = L' ', count++;
  1995. // Do not overflow the output buffer
  1996. if (buffer + precision > buffer_end)
  1997. precision = buffer_end - buffer;
  1998. // Convert or copy string (char) into output buffer
  1999. if (*format == 's' && !wide)
  2000. PAL_MultiByteToWideChar((LPCSTR)buf, -1, buffer, precision);
  2001. else
  2002. wcsncpy(buffer, buf, precision);
  2003. buffer += precision; count += precision;
  2004. // Right-side padding
  2005. for (i = 0; left_aligned && i < width - precision && buffer < buffer_end; i++)
  2006. *buffer++ = L' ', count++;
  2007. }
  2008. else
  2009. {
  2010. // For other types, pass them directly into vswprintf
  2011. int cur_cnt = 0;
  2012. va_list apd;
  2013. // We copy this argument's format string into internal buffer
  2014. if (fmt_len < (size_t)(format - fmt_start + 1))
  2015. cur_fmt = realloc(cur_fmt, ((fmt_len = format - fmt_start + 1) + 1) * sizeof(WCHAR));
  2016. wcsncpy(cur_fmt, fmt_start, fmt_len);
  2017. cur_fmt[fmt_len] = L'\0';
  2018. // And pass it into vswprintf to get the output
  2019. va_copy(apd, ap);
  2020. cur_cnt = vswprintf(buffer, buffer_end - buffer, cur_fmt, apd);
  2021. va_end(apd);
  2022. buffer += cur_cnt; count += cur_cnt;
  2023. // Then we need to move the argument pointer into next one
  2024. // Check if width/precision should be read from arguments
  2025. if (width_var) va_arg(ap, int);
  2026. if (precision_var) va_arg(ap, int);
  2027. // Move pointer to pass the actual value argument
  2028. switch (*format)
  2029. {
  2030. case 'd':
  2031. case 'i':
  2032. case 'o':
  2033. case 'u':
  2034. case 'x':
  2035. case 'X':
  2036. if (wide == 1)
  2037. va_arg(ap, long);
  2038. else if (wide >= 2)
  2039. va_arg(ap, long long);
  2040. else
  2041. va_arg(ap, int);
  2042. break;
  2043. case 'e':
  2044. case 'E':
  2045. case 'f':
  2046. case 'g':
  2047. case 'G':
  2048. case 'a':
  2049. case 'A':
  2050. va_arg(ap, double);
  2051. break;
  2052. case 'p':
  2053. case 'n':
  2054. va_arg(ap, void*);
  2055. break;
  2056. }
  2057. }
  2058. state = 0;
  2059. format++;
  2060. break;
  2061. }
  2062. }
  2063. // If the format string is malformed, try to copy it into the dest buffer
  2064. if (state && buffer < buffer_end)
  2065. {
  2066. int fmt_len = format - fmt_start;
  2067. int buf_len = buffer_end - buffer;
  2068. if (fmt_len <= buf_len)
  2069. {
  2070. wcsncpy(buffer, fmt_start, buf_len);
  2071. buffer += fmt_len;
  2072. }
  2073. else
  2074. {
  2075. wcsncpy(buffer, fmt_start, buf_len);
  2076. buffer += buf_len;
  2077. }
  2078. }
  2079. // NULL-terminate the string
  2080. *buffer = L'\0';
  2081. va_end(ap);
  2082. return count;
  2083. }