font_driver.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. /* RetroArch - A frontend for libretro.
  2. * Copyright (C) 2010-2014 - Hans-Kristian Arntzen
  3. * Copyright (C) 2011-2017 - Daniel De Matteis
  4. *
  5. * RetroArch is free software: you can redistribute it and/or modify it under the terms
  6. * of the GNU General Public License as published by the Free Software Found-
  7. * ation, either version 3 of the License, or (at your option) any later version.
  8. *
  9. * RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  10. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. * PURPOSE. See the GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with RetroArch.
  14. * If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <stdlib.h>
  17. #include <math.h>
  18. #ifdef HAVE_CONFIG_H
  19. #include "../config.h"
  20. #endif
  21. #include "font_driver.h"
  22. #include "video_thread_wrapper.h"
  23. /* TODO/FIXME - global */
  24. static void *video_font_driver = NULL;
  25. int font_renderer_create_default(
  26. const font_renderer_driver_t **drv,
  27. void **handle, const char *font_path, unsigned font_size)
  28. {
  29. static const font_renderer_driver_t *font_backends[] = {
  30. #ifdef HAVE_FREETYPE
  31. &freetype_font_renderer,
  32. #endif
  33. #if defined(__APPLE__) && defined(HAVE_CORETEXT)
  34. &coretext_font_renderer,
  35. #endif
  36. #ifdef HAVE_STB_FONT
  37. #if defined(VITA) || defined(ORBIS) || defined(WIIU) || defined(ANDROID) || (defined(_WIN32) && !defined(_XBOX) && !defined(_MSC_VER) && _MSC_VER >= 1400) || (defined(_WIN32) && !defined(_XBOX) && defined(_MSC_VER)) || defined(HAVE_LIBNX) || defined(__linux__) || defined (HAVE_EMSCRIPTEN) || defined(__APPLE__) || defined(HAVE_ODROIDGO2) || defined(__PS3__)
  38. &stb_unicode_font_renderer,
  39. #else
  40. &stb_unicode_font_renderer,
  41. #endif
  42. #endif
  43. &bitmap_font_renderer,
  44. NULL
  45. };
  46. unsigned i;
  47. for (i = 0; font_backends[i]; i++)
  48. {
  49. const char *path = font_path;
  50. if (!path)
  51. path = font_backends[i]->get_default_font();
  52. if (!path)
  53. continue;
  54. *handle = font_backends[i]->init(path, font_size);
  55. if (*handle)
  56. {
  57. *drv = font_backends[i];
  58. return 1;
  59. }
  60. }
  61. *drv = NULL;
  62. *handle = NULL;
  63. return 0;
  64. }
  65. static bool font_init_first(
  66. const void **font_driver, void **font_handle,
  67. void *video_data, const char *font_path, float font_size,
  68. enum font_driver_render_api api, bool is_threaded)
  69. {
  70. if (font_path && !font_path[0])
  71. font_path = NULL;
  72. switch (api)
  73. {
  74. #ifdef HAVE_OPENGL1
  75. case FONT_DRIVER_RENDER_OPENGL1_API:
  76. {
  77. void *data = gl1_raster_font.init(
  78. video_data, font_path, font_size, is_threaded);
  79. if (data)
  80. {
  81. *font_driver = &gl1_raster_font;
  82. *font_handle = data;
  83. return true;
  84. }
  85. }
  86. break;
  87. #endif
  88. #ifdef HAVE_OPENGL
  89. case FONT_DRIVER_RENDER_OPENGL_API:
  90. {
  91. void *data = gl2_raster_font.init(
  92. video_data, font_path, font_size, is_threaded);
  93. if (data)
  94. {
  95. *font_driver = &gl2_raster_font;
  96. *font_handle = data;
  97. return true;
  98. }
  99. }
  100. break;
  101. #endif
  102. #ifdef HAVE_OPENGL_CORE
  103. case FONT_DRIVER_RENDER_OPENGL_CORE_API:
  104. {
  105. void *data = gl3_raster_font.init(
  106. video_data, font_path, font_size, is_threaded);
  107. if (data)
  108. {
  109. *font_driver = &gl3_raster_font;
  110. *font_handle = data;
  111. return true;
  112. }
  113. }
  114. break;
  115. #endif
  116. #ifdef HAVE_VULKAN
  117. case FONT_DRIVER_RENDER_VULKAN_API:
  118. {
  119. void *data = vulkan_raster_font.init(video_data,
  120. font_path, font_size, is_threaded);
  121. if (data)
  122. {
  123. *font_driver = &vulkan_raster_font;
  124. *font_handle = data;
  125. return true;
  126. }
  127. }
  128. break;
  129. #endif
  130. #ifdef HAVE_METAL
  131. case FONT_DRIVER_RENDER_METAL_API:
  132. {
  133. void *data = metal_raster_font.init(video_data,
  134. font_path, font_size, is_threaded);
  135. if (data)
  136. {
  137. *font_driver = &metal_raster_font;
  138. *font_handle = data;
  139. return true;
  140. }
  141. }
  142. #endif
  143. #ifdef HAVE_D3D8
  144. case FONT_DRIVER_RENDER_D3D8_API:
  145. {
  146. static const font_renderer_t *d3d8_font_backends[] = {
  147. #if defined(_XBOX1)
  148. &d3d_xdk1_font,
  149. #endif
  150. NULL
  151. };
  152. unsigned i;
  153. for (i = 0; i < ARRAY_SIZE(d3d8_font_backends); i++)
  154. {
  155. void *data = d3d8_font_backends[i] ? d3d8_font_backends[i]->init(
  156. video_data, font_path, font_size, is_threaded) : NULL;
  157. if (data)
  158. {
  159. *font_driver = d3d8_font_backends[i];
  160. *font_handle = data;
  161. return true;
  162. }
  163. }
  164. }
  165. break;
  166. #endif
  167. #ifdef HAVE_D3D9
  168. case FONT_DRIVER_RENDER_D3D9_API:
  169. {
  170. static const font_renderer_t *d3d9_font_backends[] = {
  171. #if defined(_WIN32) && defined(HAVE_D3DX)
  172. &d3d_win32_font,
  173. #endif
  174. NULL
  175. };
  176. unsigned i;
  177. for (i = 0; i < ARRAY_SIZE(d3d9_font_backends); i++)
  178. {
  179. void *data = d3d9_font_backends[i] ? d3d9_font_backends[i]->init(
  180. video_data, font_path, font_size, is_threaded) : NULL;
  181. if (data)
  182. {
  183. *font_driver = d3d9_font_backends[i];
  184. *font_handle = data;
  185. return true;
  186. }
  187. }
  188. }
  189. break;
  190. #endif
  191. #ifdef HAVE_D3D10
  192. case FONT_DRIVER_RENDER_D3D10_API:
  193. {
  194. void *data = d3d10_font.init(video_data,
  195. font_path, font_size, is_threaded);
  196. if (data)
  197. {
  198. *font_driver = &d3d10_font;
  199. *font_handle = data;
  200. return true;
  201. }
  202. }
  203. break;
  204. #endif
  205. #ifdef HAVE_D3D11
  206. case FONT_DRIVER_RENDER_D3D11_API:
  207. {
  208. void *data = d3d11_font.init(video_data,
  209. font_path, font_size, is_threaded);
  210. if (data)
  211. {
  212. *font_driver = &d3d11_font;
  213. *font_handle = data;
  214. return true;
  215. }
  216. }
  217. break;
  218. #endif
  219. #ifdef HAVE_D3D12
  220. case FONT_DRIVER_RENDER_D3D12_API:
  221. {
  222. void *data = d3d12_font.init(video_data,
  223. font_path, font_size, is_threaded);
  224. if (data)
  225. {
  226. *font_driver = &d3d12_font;
  227. *font_handle = data;
  228. return true;
  229. }
  230. }
  231. break;
  232. #endif
  233. #ifdef HAVE_VITA2D
  234. case FONT_DRIVER_RENDER_VITA2D:
  235. {
  236. void *data = vita2d_vita_font.init(
  237. video_data, font_path, font_size, is_threaded);
  238. if (data)
  239. {
  240. *font_driver = &vita2d_vita_font;
  241. *font_handle = data;
  242. return true;
  243. }
  244. }
  245. break;
  246. #endif
  247. #ifdef PS2
  248. case FONT_DRIVER_RENDER_PS2:
  249. {
  250. void *data = ps2_font.init(
  251. video_data, font_path, font_size,
  252. is_threaded);
  253. if (data)
  254. {
  255. *font_driver = &ps2_font;
  256. *font_handle = data;
  257. return true;
  258. }
  259. }
  260. break;
  261. #endif
  262. #ifdef _3DS
  263. case FONT_DRIVER_RENDER_CTR:
  264. {
  265. void *data = ctr_font.init(
  266. video_data, font_path, font_size,
  267. is_threaded);
  268. if (data)
  269. {
  270. *font_driver = &ctr_font;
  271. *font_handle = data;
  272. return true;
  273. }
  274. }
  275. break;
  276. #endif
  277. #ifdef WIIU
  278. case FONT_DRIVER_RENDER_WIIU:
  279. {
  280. void *data = wiiu_font.init(
  281. video_data, font_path, font_size, is_threaded);
  282. if (data)
  283. {
  284. *font_driver = &wiiu_font;
  285. *font_handle = data;
  286. return true;
  287. }
  288. }
  289. break;
  290. #endif
  291. #ifdef HAVE_CACA
  292. case FONT_DRIVER_RENDER_CACA:
  293. {
  294. void *data = caca_font.init(
  295. video_data, font_path, font_size,
  296. is_threaded);
  297. if (data)
  298. {
  299. *font_driver = &caca_font;
  300. *font_handle = data;
  301. return true;
  302. }
  303. }
  304. break;
  305. #endif
  306. #ifdef HAVE_SIXEL
  307. case FONT_DRIVER_RENDER_SIXEL:
  308. {
  309. void *data = sixel_font.init(
  310. video_data, font_path, font_size,
  311. is_threaded);
  312. if (data)
  313. {
  314. *font_driver = &sixel_font;
  315. *font_handle = data;
  316. return true;
  317. }
  318. }
  319. break;
  320. #endif
  321. #ifdef HAVE_LIBNX
  322. case FONT_DRIVER_RENDER_SWITCH:
  323. {
  324. void *data = switch_font.init(
  325. video_data, font_path, font_size,
  326. is_threaded);
  327. if (data)
  328. {
  329. *font_driver = &switch_font;
  330. *font_handle = data;
  331. return true;
  332. }
  333. }
  334. break;
  335. #endif
  336. #ifdef HAVE_GCM
  337. case FONT_DRIVER_RENDER_RSX:
  338. {
  339. void *data = rsx_font.init(
  340. video_data, font_path, font_size,
  341. is_threaded);
  342. if (data)
  343. {
  344. *font_driver = &rsx_font;
  345. *font_handle = data;
  346. return true;
  347. }
  348. }
  349. break;
  350. #endif
  351. #ifdef HAVE_GDI
  352. #if defined(_WIN32) && !defined(_XBOX) && !defined(__WINRT__)
  353. case FONT_DRIVER_RENDER_GDI:
  354. {
  355. void *data = gdi_font.init(
  356. video_data, font_path, font_size,
  357. is_threaded);
  358. if (data)
  359. {
  360. *font_driver = &gdi_font;
  361. *font_handle = data;
  362. return true;
  363. }
  364. }
  365. break;
  366. #endif
  367. #endif
  368. #ifdef DJGPP
  369. case FONT_DRIVER_RENDER_VGA:
  370. {
  371. void *data = vga_font.init(
  372. video_data, font_path, font_size,
  373. is_threaded);
  374. if (data)
  375. {
  376. *font_driver = &vga_font;
  377. *font_handle = data;
  378. return true;
  379. }
  380. }
  381. break;
  382. #endif
  383. case FONT_DRIVER_RENDER_DONT_CARE:
  384. /* TODO/FIXME - lookup graphics driver's 'API' */
  385. break;
  386. default:
  387. break;
  388. }
  389. return false;
  390. }
  391. #ifdef HAVE_LANGEXTRA
  392. /* ASCII: 0xxxxxxx (c & 0x80) == 0x00
  393. * other start: 11xxxxxx (c & 0xC0) == 0xC0
  394. * other cont: 10xxxxxx (c & 0xC0) == 0x80
  395. * Neutral:
  396. * 0020 - 002F: 001xxxxx (c & 0xE0) == 0x20
  397. * misc. white space:
  398. * 2000 - 200D: 11100010 10000000 1000xxxx (c[2] < 0x8E) (3 bytes)
  399. * Hebrew:
  400. * 0591 - 05F4: 1101011x (c & 0xFE) == 0xD6 (2 bytes)
  401. * Arabic:
  402. * 0600 - 06FF: 110110xx (c & 0xFC) == 0xD8 (2 bytes)
  403. */
  404. /* clang-format off */
  405. #define IS_ASCII(p) ((*(p)&0x80) == 0x00)
  406. #define IS_MBSTART(p) ((*(p)&0xC0) == 0xC0)
  407. #define IS_MBCONT(p) ((*(p)&0xC0) == 0x80)
  408. #define IS_DIR_NEUTRAL(p) ((*(p)&0xE0) == 0x20)
  409. #define IS_HEBREW(p) ((*(p)&0xFE) == 0xD6)
  410. #define IS_ARABIC(p) ((*(p)&0xFC) == 0xD8)
  411. #define IS_RTL(p) (IS_HEBREW(p) || IS_ARABIC(p))
  412. #define GET_ID_ARABIC(p) (((unsigned char)(p)[0] << 6) | ((unsigned char)(p)[1] & 0x3F))
  413. /* Checks for miscellaneous whitespace characters in the range U+2000 to U+200D */
  414. static INLINE unsigned is_misc_ws(const unsigned char* src)
  415. {
  416. unsigned res = 0;
  417. if (*(src) == 0xE2) /* first byte */
  418. {
  419. src++;
  420. if (*(src) == 0x80) /* second byte */
  421. {
  422. src++;
  423. res = (*(src) < 0x8E); /* third byte */
  424. }
  425. }
  426. return res;
  427. }
  428. static INLINE unsigned font_get_arabic_replacement(
  429. const char* src, const char* start)
  430. {
  431. /* 0x0620 to 0x064F */
  432. static const unsigned arabic_shape_map[0x100][0x4] = {
  433. { 0 }, { 0 }, { 0 }, { 0 }, /* 0x0600 */
  434. { 0 }, { 0 }, { 0 }, { 0 },
  435. { 0 }, { 0 }, { 0 }, { 0 },
  436. { 0 }, { 0 }, { 0 }, { 0 },
  437. { 0 }, { 0 }, { 0 }, { 0 }, /* 0x0610 */
  438. { 0 }, { 0 }, { 0 }, { 0 },
  439. { 0 }, { 0 }, { 0 }, { 0 },
  440. { 0 }, { 0 }, { 0 }, { 0 },
  441. { 0 }, /* 0x0620 */
  442. { 0xFE80 },
  443. { 0xFE81, 0xFE82 },
  444. { 0xFE83, 0xFE84 },
  445. { 0xFE85, 0xFE86 },
  446. { 0xFE87, 0xFE88 },
  447. { 0xFE89, 0xFE8A, 0xFE8B, 0xFE8C },
  448. { 0xFE8D, 0xFE8E },
  449. { 0xFE8F, 0xFE90, 0xFE91, 0xFE92 },
  450. { 0xFE93, 0xFE94 },
  451. { 0xFE95, 0xFE96, 0xFE97, 0xFE98 },
  452. { 0xFE99, 0xFE9A, 0xFE9B, 0xFE9C },
  453. { 0xFE9D, 0xFE9E, 0xFE9F, 0xFEA0 },
  454. { 0xFEA1, 0xFEA2, 0xFEA3, 0xFEA4 },
  455. { 0xFEA5, 0xFEA6, 0xFEA7, 0xFEA8 },
  456. { 0xFEA9, 0xFEAA },
  457. { 0xFEAB, 0xFEAC }, /* 0x0630 */
  458. { 0xFEAD, 0xFEAE },
  459. { 0xFEAF, 0xFEB0 },
  460. { 0xFEB1, 0xFEB2, 0xFEB3, 0xFEB4 },
  461. { 0xFEB5, 0xFEB6, 0xFEB7, 0xFEB8 },
  462. { 0xFEB9, 0xFEBA, 0xFEBB, 0xFEBC },
  463. { 0xFEBD, 0xFEBE, 0xFEBF, 0xFEC0 },
  464. { 0xFEC1, 0xFEC2, 0xFEC3, 0xFEC4 },
  465. { 0xFEC5, 0xFEC6, 0xFEC7, 0xFEC8 },
  466. { 0xFEC9, 0xFECA, 0xFECB, 0xFECC },
  467. { 0xFECD, 0xFECE, 0xFECF, 0xFED0 },
  468. { 0 },
  469. { 0 }, { 0 }, { 0 }, { 0 },
  470. { 0 }, /* 0x0640 */
  471. { 0xFED1, 0xFED2, 0xFED3, 0xFED4 },
  472. { 0xFED5, 0xFED6, 0xFED7, 0xFED8 },
  473. { 0xFED9, 0xFEDA, 0xFEDB, 0xFEDC },
  474. { 0xFEDD, 0xFEDE, 0xFEDF, 0xFEE0 },
  475. { 0xFEE1, 0xFEE2, 0xFEE3, 0xFEE4 },
  476. { 0xFEE5, 0xFEE6, 0xFEE7, 0xFEE8 },
  477. { 0xFEE9, 0xFEEA, 0xFEEB, 0xFEEC },
  478. { 0xFEED, 0xFEEE },
  479. { 0xFEEF, 0xFEF0, 0xFBE8, 0xFBE9 },
  480. { 0xFEF1, 0xFEF2, 0xFEF3, 0xFEF4 },
  481. { 0 },
  482. { 0 }, { 0 }, { 0 }, { 0 },
  483. { 0 }, { 0 }, { 0 }, { 0 }, /* 0x0650 */
  484. { 0 }, { 0 }, { 0 }, { 0 },
  485. { 0 }, { 0 }, { 0 }, { 0 },
  486. { 0 }, { 0 }, { 0 }, { 0 },
  487. { 0 }, { 0 }, { 0 }, { 0 }, /* 0x0660 */
  488. { 0 }, { 0 }, { 0 }, { 0 },
  489. { 0 }, { 0 }, { 0 }, { 0 },
  490. { 0 }, { 0 }, { 0 }, { 0 },
  491. { 0 }, { 0 }, { 0 }, { 0 }, /* 0x0670 */
  492. { 0 }, { 0 }, { 0 }, { 0 },
  493. { 0 }, { 0 }, { 0 }, { 0 },
  494. { 0 }, { 0 },
  495. { 0xFB56, 0xFB57, 0xFB58, 0xFB59 },
  496. { 0 },
  497. { 0 }, { 0 }, { 0 }, { 0 }, /* 0x0680 */
  498. { 0 }, { 0 }, { 0 }, { 0 },
  499. { 0 }, { 0 }, { 0 }, { 0 },
  500. { 0 }, { 0 }, { 0 }, { 0 },
  501. { 0 }, { 0 }, { 0 }, { 0 }, /* 0x0690 */
  502. { 0 }, { 0 }, { 0 }, { 0 },
  503. { 0 }, { 0 }, { 0 }, { 0 },
  504. { 0 }, { 0 }, { 0 }, { 0 },
  505. { 0 }, { 0 }, { 0 }, { 0 }, /* 0x06A0 */
  506. { 0 }, { 0 }, { 0 }, { 0 },
  507. { 0 },
  508. { 0xFB8E, 0xFB8F, 0xFB90, 0xFB91 },
  509. { 0 }, { 0 },
  510. { 0 }, { 0 }, { 0 },
  511. { 0xFB92, 0xFB93, 0xFB94, 0xFB95 },
  512. { 0 }, { 0 }, { 0 }, { 0 }, /* 0x06B0 */
  513. { 0 }, { 0 }, { 0 }, { 0 },
  514. { 0 }, { 0 }, { 0 }, { 0 },
  515. { 0 }, { 0 }, { 0 }, { 0 },
  516. { 0 }, { 0 }, { 0 }, { 0 }, /* 0x06C0 */
  517. { 0 }, { 0 }, { 0 }, { 0 },
  518. { 0 }, { 0 }, { 0 }, { 0 },
  519. { 0xFBFC, 0xFBFD, 0xFBFE, 0xFBFF },
  520. { 0 }, { 0 }, { 0 },
  521. { 0 }, { 0 }, { 0 }, { 0 }, /* 0x06D0 */
  522. { 0 }, { 0 }, { 0 }, { 0 },
  523. { 0 }, { 0 }, { 0 }, { 0 },
  524. { 0 }, { 0 }, { 0 }, { 0 },
  525. { 0 }, { 0 }, { 0 }, { 0 }, /* 0x06E0 */
  526. { 0 }, { 0 }, { 0 }, { 0 },
  527. { 0 }, { 0 }, { 0 }, { 0 },
  528. { 0 }, { 0 }, { 0 }, { 0 },
  529. { 0 }, { 0 }, { 0 }, { 0 }, /* 0x06F0 */
  530. { 0 }, { 0 }, { 0 }, { 0 },
  531. { 0 }, { 0 }, { 0 }, { 0 },
  532. { 0 }, { 0 }, { 0 }, { 0 },
  533. };
  534. unsigned result = 0;
  535. bool prev_connected = false;
  536. bool next_connected = false;
  537. unsigned char id = GET_ID_ARABIC(src);
  538. const char* prev = src - 2;
  539. const char* next = src + 2;
  540. if (IS_ARABIC(prev) && (prev >= start))
  541. {
  542. unsigned char prev_id = GET_ID_ARABIC(prev);
  543. /* nonspacing diacritics 0x4b -- 0x5f */
  544. while (prev_id > 0x4A && prev_id < 0x60)
  545. {
  546. prev -= 2;
  547. if ((prev >= start) && IS_ARABIC(prev))
  548. prev_id = GET_ID_ARABIC(prev);
  549. else
  550. break;
  551. }
  552. if (prev_id == 0x44) /* Arabic Letter Lam */
  553. {
  554. unsigned char prev2_id = 0;
  555. const char* prev2 = prev - 2;
  556. if (prev2 >= start)
  557. prev2_id = (prev2[0] << 6) | (prev2[1] & 0x3F);
  558. /* nonspacing diacritics 0x4b -- 0x5f */
  559. while (prev2_id > 0x4A && prev2_id < 0x60)
  560. {
  561. prev2 -= 2;
  562. if ((prev2 >= start) && IS_ARABIC(prev2))
  563. prev2_id = GET_ID_ARABIC(prev2);
  564. else
  565. break;
  566. }
  567. prev_connected = !!arabic_shape_map[prev2_id][2];
  568. switch (id)
  569. {
  570. case 0x22: /* Arabic Letter Alef with Madda Above */
  571. return 0xFEF5 + prev_connected;
  572. case 0x23: /* Arabic Letter Alef with Hamza Above */
  573. return 0xFEF7 + prev_connected;
  574. case 0x25: /* Arabic Letter Alef with Hamza Below */
  575. return 0xFEF9 + prev_connected;
  576. case 0x27: /* Arabic Letter Alef */
  577. return 0xFEFB + prev_connected;
  578. }
  579. }
  580. prev_connected = !!arabic_shape_map[prev_id][2];
  581. }
  582. if (IS_ARABIC(next))
  583. {
  584. unsigned char next_id = GET_ID_ARABIC(next);
  585. /* nonspacing diacritics 0x4b -- 0x5f */
  586. while (next_id > 0x4A && next_id < 0x60)
  587. {
  588. next += 2;
  589. if (!IS_ARABIC(next))
  590. break;
  591. next_id = GET_ID_ARABIC(next);
  592. }
  593. next_connected = !!arabic_shape_map[next_id][1];
  594. }
  595. if ((result =
  596. arabic_shape_map[id][prev_connected | (next_connected <<
  597. 1)]))
  598. return result;
  599. return arabic_shape_map[id][prev_connected];
  600. }
  601. /* clang-format on */
  602. static char* font_driver_reshape_msg(const char* msg, unsigned char *buffer, size_t buffer_size)
  603. {
  604. const unsigned char *src = (const unsigned char*)msg;
  605. bool reverse = false;
  606. size_t msg_size = (strlen(msg) * 2) + 1;
  607. /* Fallback to heap allocated buffer if the buffer is too small */
  608. /* worst case transformations are 2 bytes to 4 bytes -- aliaspider */
  609. unsigned char* dst_buffer = (buffer_size < msg_size)
  610. ? (unsigned char*)malloc(msg_size)
  611. : buffer;
  612. unsigned char *dst = (unsigned char*)dst_buffer;
  613. while (*src || reverse)
  614. {
  615. if (reverse)
  616. {
  617. src--;
  618. while (src > (const unsigned char*)msg && IS_MBCONT(src))
  619. src--;
  620. if (src >= (const unsigned char*)msg && (IS_RTL(src) || IS_DIR_NEUTRAL(src) || is_misc_ws(src)))
  621. {
  622. if (IS_ARABIC(src))
  623. {
  624. unsigned replacement = font_get_arabic_replacement(
  625. (const char*)src, msg);
  626. if (replacement)
  627. {
  628. if (replacement < 0x80)
  629. *dst++ = replacement;
  630. else if (replacement < 0x800)
  631. {
  632. *dst++ = 0xC0 | (replacement >> 6);
  633. *dst++ = 0x80 | (replacement & 0x3F);
  634. }
  635. else if (replacement < 0x10000)
  636. {
  637. /* merged glyphs */
  638. if ((replacement >= 0xFEF5) && (replacement <= 0xFEFC))
  639. src -= 2;
  640. *dst++ = 0xE0 | ( replacement >> 12);
  641. *dst++ = 0x80 | ((replacement >> 6) & 0x3F);
  642. *dst++ = 0x80 | ( replacement & 0x3F);
  643. }
  644. else
  645. {
  646. *dst++ = 0xF0 | (replacement >> 18);
  647. *dst++ = 0x80 | ((replacement >> 12) & 0x3F);
  648. *dst++ = 0x80 | ((replacement >> 6) & 0x3F);
  649. *dst++ = 0x80 | ( replacement & 0x3F);
  650. }
  651. continue;
  652. }
  653. }
  654. *dst++ = *src++;
  655. while (IS_MBCONT(src))
  656. *dst++ = *src++;
  657. src--;
  658. while (IS_MBCONT(src))
  659. src--;
  660. }
  661. else
  662. {
  663. reverse = false;
  664. src++;
  665. while ( IS_MBCONT(src)
  666. || IS_RTL(src)
  667. || IS_DIR_NEUTRAL(src)
  668. || is_misc_ws(src))
  669. src++;
  670. }
  671. }
  672. else
  673. {
  674. if (IS_RTL(src))
  675. {
  676. reverse = true;
  677. while ( IS_MBCONT(src)
  678. || IS_RTL(src)
  679. || IS_DIR_NEUTRAL(src)
  680. || is_misc_ws(src))
  681. src++;
  682. }
  683. else
  684. *dst++ = *src++;
  685. }
  686. }
  687. *dst = '\0';
  688. return (char*)dst_buffer;
  689. }
  690. #endif
  691. void font_driver_render_msg(void *data, const char *msg,
  692. const struct font_params *params, void *font_data)
  693. {
  694. font_data_t *font = (font_data_t*)(font_data
  695. ? font_data : video_font_driver);
  696. if (msg && *msg && font && font->renderer && font->renderer->render_msg)
  697. {
  698. #ifdef HAVE_LANGEXTRA
  699. unsigned char tmp_buffer[64];
  700. char *new_msg = font_driver_reshape_msg(msg, tmp_buffer, sizeof(tmp_buffer));
  701. #else
  702. char *new_msg = (char*)msg;
  703. #endif
  704. font->renderer->render_msg(data,
  705. font->renderer_data, new_msg, params);
  706. #ifdef HAVE_LANGEXTRA
  707. if (new_msg != (char*)tmp_buffer)
  708. free(new_msg);
  709. #endif
  710. }
  711. }
  712. void font_driver_bind_block(void *font_data, void *block)
  713. {
  714. font_data_t *font = (font_data_t*)(font_data ? font_data : video_font_driver);
  715. if (font && font->renderer && font->renderer->bind_block)
  716. font->renderer->bind_block(font->renderer_data, block);
  717. }
  718. void font_driver_flush(unsigned width, unsigned height, void *font_data)
  719. {
  720. font_data_t *font = (font_data_t*)(font_data ? font_data : video_font_driver);
  721. if (font && font->renderer && font->renderer->flush)
  722. font->renderer->flush(width, height, font->renderer_data);
  723. }
  724. int font_driver_get_message_width(void *font_data,
  725. const char *msg, size_t len, float scale)
  726. {
  727. font_data_t *font = (font_data_t*)(font_data ? font_data : video_font_driver);
  728. if (len == 0 && msg)
  729. len = strlen(msg);
  730. if (font && font->renderer && font->renderer->get_message_width)
  731. return font->renderer->get_message_width(font->renderer_data, msg, len, scale);
  732. return -1;
  733. }
  734. int font_driver_get_line_height(font_data_t *font, float scale)
  735. {
  736. struct font_line_metrics *metrics = NULL;
  737. /* First try the line metrics implementation */
  738. if (font && font->renderer && font->renderer->get_line_metrics)
  739. if ((font->renderer->get_line_metrics(
  740. font->renderer_data, &metrics)))
  741. return (int)roundf(metrics->height * scale);
  742. /* Else return an approximation
  743. * (uses a fudge of standard font metrics - mostly garbage...)
  744. * > font_size = (width of 'a') / 0.6
  745. * > line_height = font_size * 1.7f */
  746. return (int)roundf(1.7f * (float)font_driver_get_message_width(font, "a", 1, scale) / 0.6f);
  747. }
  748. int font_driver_get_line_ascender(font_data_t *font, float scale)
  749. {
  750. struct font_line_metrics *metrics = NULL;
  751. /* First try the line metrics implementation */
  752. if (font && font->renderer && font->renderer->get_line_metrics)
  753. if ((font->renderer->get_line_metrics(font->renderer_data, &metrics)))
  754. return (int)roundf(metrics->ascender * scale);
  755. /* Else return an approximation
  756. * (uses a fudge of standard font metrics - mostly garbage...)
  757. * > font_size = (width of 'a') / 0.6
  758. * > ascender = 1.58 * font_size * 0.75 */
  759. return (int)roundf(1.58f * 0.75f * (float)font_driver_get_message_width(font, "a", 1, scale) / 0.6f);
  760. }
  761. int font_driver_get_line_descender(font_data_t *font, float scale)
  762. {
  763. struct font_line_metrics *metrics = NULL;
  764. /* First try the line metrics implementation */
  765. if (font && font->renderer && font->renderer->get_line_metrics)
  766. if ((font->renderer->get_line_metrics(font->renderer_data, &metrics)))
  767. return (int)roundf(metrics->descender * scale);
  768. /* Else return an approximation
  769. * (uses a fudge of standard font metrics - mostly garbage...)
  770. * > font_size = (width of 'a') / 0.6
  771. * > descender = 1.58 * font_size * 0.25 */
  772. return (int)roundf(1.58f * 0.25f * (float)font_driver_get_message_width(font, "a", 1, scale) / 0.6f);
  773. }
  774. int font_driver_get_line_centre_offset(font_data_t *font, float scale)
  775. {
  776. struct font_line_metrics *metrics = NULL;
  777. /* First try the line metrics implementation */
  778. if (font && font->renderer && font->renderer->get_line_metrics)
  779. if ((font->renderer->get_line_metrics(font->renderer_data, &metrics)))
  780. return (int)roundf((metrics->ascender - metrics->descender) * 0.5f * scale);
  781. /* Else return an approximation... */
  782. return (int)roundf((1.58f * 0.5f * (float)font_driver_get_message_width(font, "a", 1, scale) / 0.6f) / 2.0f);
  783. }
  784. void font_driver_free(font_data_t *font)
  785. {
  786. if (font)
  787. {
  788. bool is_threaded = false;
  789. #ifdef HAVE_THREADS
  790. bool *is_threaded_tmp = video_driver_get_threaded();
  791. is_threaded = *is_threaded_tmp;
  792. #endif
  793. if (font->renderer && font->renderer->free)
  794. font->renderer->free(font->renderer_data, is_threaded);
  795. font->renderer = NULL;
  796. font->renderer_data = NULL;
  797. free(font);
  798. }
  799. }
  800. font_data_t *font_driver_init_first(
  801. void *video_data, const char *font_path, float font_size,
  802. bool threading_hint, bool is_threaded,
  803. enum font_driver_render_api api)
  804. {
  805. const void *font_driver = NULL;
  806. void *font_handle = NULL;
  807. bool ok = false;
  808. #ifdef HAVE_THREADS
  809. if ( threading_hint
  810. && is_threaded
  811. && !video_driver_is_hw_context())
  812. ok = video_thread_font_init(&font_driver, &font_handle,
  813. video_data, font_path, font_size, api, font_init_first,
  814. is_threaded);
  815. else
  816. #endif
  817. ok = font_init_first(&font_driver, &font_handle,
  818. video_data, font_path, font_size, api, is_threaded);
  819. if (ok)
  820. {
  821. font_data_t *font = (font_data_t*)malloc(sizeof(*font));
  822. if (font)
  823. {
  824. font->renderer = (const font_renderer_t*)font_driver;
  825. font->renderer_data = font_handle;
  826. font->size = font_size;
  827. return font;
  828. }
  829. }
  830. return NULL;
  831. }
  832. void font_driver_init_osd(
  833. void *video_data,
  834. const void *video_info_data,
  835. bool threading_hint,
  836. bool is_threaded,
  837. enum font_driver_render_api api)
  838. {
  839. const video_info_t *video_info = (const video_info_t*)video_info_data;
  840. if (!video_font_driver && video_info)
  841. video_font_driver = font_driver_init_first(video_data,
  842. *video_info->path_font ? video_info->path_font : NULL,
  843. video_info->font_size, threading_hint, is_threaded, api);
  844. }
  845. void font_driver_free_osd(void)
  846. {
  847. if (video_font_driver)
  848. font_driver_free(video_font_driver);
  849. video_font_driver = NULL;
  850. }