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