SDL_fribidi.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #ifdef HAVE_FRIBIDI_H
  19. #include "SDL_internal.h"
  20. #include "SDL_fribidi.h"
  21. #include <fribidi.h>
  22. SDL_ELF_NOTE_DLOPEN(
  23. "fribidi",
  24. "Bidirectional text support",
  25. SDL_ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED,
  26. SDL_FRIBIDI_DYNAMIC
  27. );
  28. SDL_FriBidi *SDL_FriBidi_Create(void) {
  29. SDL_FriBidi *fribidi;
  30. fribidi = (SDL_FriBidi *)SDL_malloc(sizeof(SDL_FriBidi));
  31. if (!fribidi) {
  32. return NULL;
  33. }
  34. #ifdef SDL_FRIBIDI_DYNAMIC
  35. #define SDL_FRIBIDI_LOAD_SYM(x, n, t) x = ((t)SDL_LoadFunction(fribidi->lib, n)); if (!x) { SDL_UnloadObject(fribidi->lib); SDL_free(fribidi); return NULL; }
  36. fribidi->lib = SDL_LoadObject(SDL_FRIBIDI_DYNAMIC);
  37. if (!fribidi->lib) {
  38. SDL_free(fribidi);
  39. return NULL;
  40. }
  41. SDL_FRIBIDI_LOAD_SYM(fribidi->unicode_to_charset, "fribidi_unicode_to_charset", SDL_FriBidiUnicodeToCharset);
  42. SDL_FRIBIDI_LOAD_SYM(fribidi->charset_to_unicode, "fribidi_charset_to_unicode", SDL_FriBidiCharsetToUnicode);
  43. SDL_FRIBIDI_LOAD_SYM(fribidi->get_bidi_types, "fribidi_get_bidi_types", SDL_FriBidiGetBidiTypes);
  44. SDL_FRIBIDI_LOAD_SYM(fribidi->get_par_direction, "fribidi_get_par_direction", SDL_FriBidiGetParDirection);
  45. SDL_FRIBIDI_LOAD_SYM(fribidi->get_par_embedding_levels, "fribidi_get_par_embedding_levels", SDL_FriBidiGetParEmbeddingLevels);
  46. SDL_FRIBIDI_LOAD_SYM(fribidi->get_joining_types, "fribidi_get_joining_types", SDL_FriBidiGetJoiningTypes);
  47. SDL_FRIBIDI_LOAD_SYM(fribidi->join_arabic, "fribidi_join_arabic", SDL_FriBidiJoinArabic);
  48. SDL_FRIBIDI_LOAD_SYM(fribidi->shape, "fribidi_shape", SDL_FriBidiShape);
  49. SDL_FRIBIDI_LOAD_SYM(fribidi->reorder_line, "fribidi_reorder_line", SDL_FriBidiReorderLine);
  50. #else
  51. fribidi->unicode_to_charset = fribidi_unicode_to_charset;
  52. fribidi->charset_to_unicode = fribidi_charset_to_unicode;
  53. fribidi->get_bidi_types = fribidi_get_bidi_types;
  54. fribidi->get_par_direction = fribidi_get_par_direction;
  55. fribidi->get_par_embedding_levels = fribidi_get_par_embedding_levels;
  56. fribidi->get_joining_types = fribidi_get_joining_types;
  57. fribidi->join_arabic = fribidi_join_arabic;
  58. fribidi->shape = fribidi_shape;
  59. fribidi->reorder_line = fribidi_reorder_line;
  60. #endif
  61. return fribidi;
  62. }
  63. char *SDL_FriBidi_Process(SDL_FriBidi *fribidi, char *utf8, ssize_t utf8_len, bool shaping, FriBidiParType *out_par_type) {
  64. FriBidiCharType *types;
  65. FriBidiLevel *levels;
  66. FriBidiArabicProp *props;
  67. FriBidiChar *str;
  68. char *result;
  69. FriBidiStrIndex len;
  70. FriBidiLevel max_level;
  71. FriBidiLevel start;
  72. FriBidiLevel end;
  73. FriBidiParType direction;
  74. FriBidiParType str_direction;
  75. unsigned int i;
  76. unsigned int c;
  77. if (!fribidi || !utf8) {
  78. return NULL;
  79. }
  80. /* Convert to UTF32 */
  81. if (utf8_len < 0) {
  82. utf8_len = SDL_strlen(utf8);
  83. }
  84. str = SDL_calloc(SDL_utf8strnlen(utf8, utf8_len), sizeof(FriBidiChar));
  85. len = fribidi->charset_to_unicode(FRIBIDI_CHAR_SET_UTF8, utf8, utf8_len, str);
  86. /* Setup various BIDI structures */
  87. direction = FRIBIDI_PAR_LTR;
  88. types = NULL;
  89. levels = NULL;
  90. props = SDL_calloc(len + 1, sizeof(FriBidiArabicProp));
  91. levels = SDL_calloc(len + 1, sizeof(FriBidiLevel));
  92. types = SDL_calloc(len + 1, sizeof(FriBidiCharType));
  93. /* Shape */
  94. fribidi->get_bidi_types(str, len, types);
  95. str_direction = fribidi->get_par_direction(types, len);
  96. max_level = fribidi->get_par_embedding_levels(types, len, &direction, levels);
  97. if (shaping) {
  98. fribidi->get_joining_types(str, len, props);
  99. fribidi->join_arabic(types, len, levels, props);
  100. fribidi->shape(FRIBIDI_FLAGS_DEFAULT | FRIBIDI_FLAGS_ARABIC, levels, len, props, str);
  101. }
  102. /* BIDI */
  103. for (end = 0, start = 0; end < len; end++) {
  104. if (str[end] == '\n' || str[end] == '\r' || str[end] == '\f' || str[end] == '\v' || end == len - 1) {
  105. max_level = fribidi->reorder_line(FRIBIDI_FLAGS_DEFAULT | FRIBIDI_FLAGS_ARABIC, types, end - start + 1, start, direction, levels, str, NULL);
  106. start = end + 1;
  107. }
  108. }
  109. /* Silence warning */
  110. (void)max_level;
  111. /* Remove fillers */
  112. for (i = 0, c = 0; i < len; i++) {
  113. if (str[i] != FRIBIDI_CHAR_FILL) {
  114. str[c++] = str[i];
  115. }
  116. }
  117. len = c;
  118. /* Convert back to UTF8 */
  119. result = SDL_malloc(len * 4 + 1);
  120. fribidi->unicode_to_charset(FRIBIDI_CHAR_SET_UTF8, str, len, result);
  121. /* Cleanup */
  122. SDL_free(levels);
  123. SDL_free(props);
  124. SDL_free(types);
  125. /* Return */
  126. if (out_par_type) {
  127. *out_par_type = str_direction;
  128. }
  129. return result;
  130. }
  131. void SDL_FriBidi_Destroy(SDL_FriBidi *fribidi) {
  132. if (!fribidi) {
  133. return;
  134. }
  135. #ifdef SDL_FRIBIDI_DYNAMIC
  136. SDL_UnloadObject(fribidi->lib);
  137. #endif
  138. SDL_free(fribidi);
  139. }
  140. #endif