text.c 50 KB

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