script_iterator.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**************************************************************************/
  2. /* script_iterator.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "script_iterator.h"
  31. // This implementation is derived from ICU: icu4c/source/extra/scrptrun/scrptrun.cpp
  32. bool ScriptIterator::same_script(int32_t p_script_one, int32_t p_script_two) {
  33. return p_script_one <= USCRIPT_INHERITED || p_script_two <= USCRIPT_INHERITED || p_script_one == p_script_two;
  34. }
  35. ScriptIterator::ScriptIterator(const String &p_string, int p_start, int p_length) {
  36. struct ParenStackEntry {
  37. int pair_index;
  38. UScriptCode script_code;
  39. };
  40. if (p_start >= p_length) {
  41. p_start = p_length - 1;
  42. }
  43. if (p_start < 0) {
  44. p_start = 0;
  45. }
  46. int paren_size = PAREN_STACK_DEPTH;
  47. ParenStackEntry *paren_stack = static_cast<ParenStackEntry *>(memalloc(paren_size * sizeof(ParenStackEntry)));
  48. int script_start;
  49. int script_end = p_start;
  50. UScriptCode script_code;
  51. int paren_sp = -1;
  52. int start_sp = paren_sp;
  53. UErrorCode err = U_ZERO_ERROR;
  54. const char32_t *str = p_string.ptr();
  55. do {
  56. script_code = USCRIPT_COMMON;
  57. for (script_start = script_end; script_end < p_length; script_end++) {
  58. UChar32 ch = str[script_end];
  59. UScriptCode sc = uscript_getScript(ch, &err);
  60. if (U_FAILURE(err)) {
  61. memfree(paren_stack);
  62. ERR_FAIL_MSG(u_errorName(err));
  63. }
  64. if (u_getIntPropertyValue(ch, UCHAR_BIDI_PAIRED_BRACKET_TYPE) != U_BPT_NONE) {
  65. if (u_getIntPropertyValue(ch, UCHAR_BIDI_PAIRED_BRACKET_TYPE) == U_BPT_OPEN) {
  66. // If it's an open character, push it onto the stack.
  67. paren_sp++;
  68. if (unlikely(paren_sp >= paren_size)) {
  69. // If the stack is full, allocate more space to handle deeply nested parentheses. This is unlikely to happen with any real text.
  70. paren_size += PAREN_STACK_DEPTH;
  71. paren_stack = static_cast<ParenStackEntry *>(memrealloc(paren_stack, paren_size * sizeof(ParenStackEntry)));
  72. }
  73. paren_stack[paren_sp].pair_index = ch;
  74. paren_stack[paren_sp].script_code = script_code;
  75. } else if (paren_sp >= 0) {
  76. // If it's a close character, find the matching open on the stack, and use that script code. Any non-matching open characters above it on the stack will be popped.
  77. UChar32 paired_ch = u_getBidiPairedBracket(ch);
  78. while (paren_sp >= 0 && paren_stack[paren_sp].pair_index != paired_ch) {
  79. paren_sp -= 1;
  80. }
  81. if (paren_sp < start_sp) {
  82. start_sp = paren_sp;
  83. }
  84. if (paren_sp >= 0) {
  85. sc = paren_stack[paren_sp].script_code;
  86. }
  87. }
  88. }
  89. if (same_script(script_code, sc)) {
  90. if (script_code <= USCRIPT_INHERITED && sc > USCRIPT_INHERITED) {
  91. script_code = sc;
  92. // Now that we have a final script code, fix any open characters we pushed before we knew the script code.
  93. while (start_sp < paren_sp) {
  94. paren_stack[++start_sp].script_code = script_code;
  95. }
  96. }
  97. if ((u_getIntPropertyValue(ch, UCHAR_BIDI_PAIRED_BRACKET_TYPE) == U_BPT_CLOSE) && paren_sp >= 0) {
  98. // If this character is a close paired character pop the matching open character from the stack.
  99. paren_sp -= 1;
  100. if (start_sp >= 0) {
  101. start_sp -= 1;
  102. }
  103. }
  104. } else {
  105. break;
  106. }
  107. }
  108. ScriptRange rng;
  109. rng.script = hb_icu_script_to_script(script_code);
  110. rng.start = script_start;
  111. rng.end = script_end;
  112. script_ranges.push_back(rng);
  113. } while (script_end < p_length);
  114. memfree(paren_stack);
  115. }