text.c 54 KB

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