script_iterator.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*************************************************************************/
  2. /* script_iterator.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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. bool ScriptIterator::same_script(int32_t p_script_one, int32_t p_script_two) {
  32. return p_script_one <= USCRIPT_INHERITED || p_script_two <= USCRIPT_INHERITED || p_script_one == p_script_two;
  33. }
  34. ScriptIterator::ScriptIterator(const String &p_string, int p_start, int p_length) {
  35. struct ParenStackEntry {
  36. int pair_index;
  37. UScriptCode script_code;
  38. };
  39. if (p_start >= p_length) {
  40. p_start = p_length - 1;
  41. }
  42. if (p_start < 0) {
  43. p_start = 0;
  44. }
  45. ParenStackEntry paren_stack[128];
  46. int script_start;
  47. int script_end = p_start;
  48. UScriptCode script_code;
  49. int paren_sp = -1;
  50. int start_sp = paren_sp;
  51. UErrorCode err = U_ZERO_ERROR;
  52. const char32_t *str = p_string.ptr();
  53. do {
  54. script_code = USCRIPT_COMMON;
  55. for (script_start = script_end; script_end < p_length; script_end++) {
  56. UChar32 ch = str[script_end];
  57. UScriptCode sc = uscript_getScript(ch, &err);
  58. if (U_FAILURE(err)) {
  59. ERR_FAIL_MSG(u_errorName(err));
  60. }
  61. if (u_getIntPropertyValue(ch, UCHAR_BIDI_PAIRED_BRACKET_TYPE) != U_BPT_NONE) {
  62. if (u_getIntPropertyValue(ch, UCHAR_BIDI_PAIRED_BRACKET_TYPE) == U_BPT_OPEN) {
  63. paren_stack[++paren_sp].pair_index = ch;
  64. paren_stack[paren_sp].script_code = script_code;
  65. } else if (paren_sp >= 0) {
  66. UChar32 paired_ch = u_getBidiPairedBracket(ch);
  67. while (paren_sp >= 0 && paren_stack[paren_sp].pair_index != paired_ch) {
  68. paren_sp -= 1;
  69. }
  70. if (paren_sp < start_sp)
  71. start_sp = paren_sp;
  72. if (paren_sp >= 0)
  73. sc = paren_stack[paren_sp].script_code;
  74. }
  75. }
  76. if (same_script(script_code, sc)) {
  77. if (script_code <= USCRIPT_INHERITED && sc > USCRIPT_INHERITED) {
  78. script_code = sc;
  79. while (start_sp < paren_sp) {
  80. paren_stack[++start_sp].script_code = script_code;
  81. }
  82. }
  83. if ((u_getIntPropertyValue(ch, UCHAR_BIDI_PAIRED_BRACKET_TYPE) == U_BPT_CLOSE) && paren_sp >= 0) {
  84. paren_sp -= 1;
  85. if (start_sp >= 0) {
  86. start_sp -= 1;
  87. }
  88. }
  89. } else {
  90. break;
  91. }
  92. }
  93. ScriptRange rng;
  94. rng.script = hb_icu_script_to_script(script_code);
  95. rng.start = script_start;
  96. rng.end = script_end;
  97. script_ranges.push_back(rng);
  98. } while (script_end < p_length);
  99. }