text.c 53 KB

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