Intersects the script extensions set of codepoint with scripts and returns the updated size of the scripts. The output result will be a subset of the input result (thus resultSize can only be smaller).
| 40 | // Intersects the script extensions set of codepoint with scripts and returns the updated size of the scripts. |
| 41 | // The output result will be a subset of the input result (thus resultSize can only be smaller). |
| 42 | size_t ScriptSetIntersect(char32_t codepoint, TScriptsArray & inOutScripts, size_t inOutScriptsCount) |
| 43 | { |
| 44 | // Each codepoint has a Script property and a Script Extensions (Scx) property. |
| 45 | // |
| 46 | // The implicit Script property values 'Common' and 'Inherited' indicate that a codepoint is widely used in many |
| 47 | // scripts, rather than being associated to a specific script. |
| 48 | // |
| 49 | // However, some codepoints that are assigned a value of 'Common' or 'Inherited' are not commonly used with all |
| 50 | // scripts, but rather only with a limited set of scripts. The Script Extension property is used to specify the set |
| 51 | // of script which borrow the codepoint. |
| 52 | // |
| 53 | // Calls to GetScriptExtensions(...) return the set of scripts where the codepoints can be used. |
| 54 | // (see table 7 from http://www.unicode.org/reports/tr24/tr24-29.html) |
| 55 | // |
| 56 | // Script Script Extensions -> Results |
| 57 | // 1) Common {Common} -> {Common} |
| 58 | // Inherited {Inherited} -> {Inherited} |
| 59 | // 2) Latin {Latn} -> {Latn} |
| 60 | // Inherited {Latn} -> {Latn} |
| 61 | // 3) Common {Hira Kana} -> {Hira Kana} |
| 62 | // Inherited {Hira Kana} -> {Hira Kana} |
| 63 | // 4) Devanagari {Deva Dogr Kthi Mahj} -> {Deva Dogr Kthi Mahj} |
| 64 | // Myanmar {Cakm Mymr Tale} -> {Cakm Mymr Tale} |
| 65 | // |
| 66 | // For most of the codepoints, the script extensions set contains only one element. For CJK codepoints, it's common |
| 67 | // to see 3-4 scripts. For really rare cases, the set can go above 20 scripts. |
| 68 | TScriptsArray codepointScripts; |
| 69 | size_t const codepointScriptsCount = GetScriptExtensions(codepoint, codepointScripts); |
| 70 | |
| 71 | // Implicit script 'inherited' is inheriting scripts from preceding codepoint. |
| 72 | if (codepointScriptsCount == 1 && codepointScripts[0] == USCRIPT_INHERITED) |
| 73 | return inOutScriptsCount; |
| 74 | |
| 75 | auto const contains = [&codepointScripts, codepointScriptsCount](UScriptCode code) |
| 76 | { |
| 77 | for (size_t i = 0; i < codepointScriptsCount; ++i) |
| 78 | if (codepointScripts[i] == code) |
| 79 | return true; |
| 80 | |
| 81 | return false; |
| 82 | }; |
| 83 | |
| 84 | // Intersect both script sets. |
| 85 | ASSERT(!contains(USCRIPT_INHERITED), ()); |
| 86 | size_t outSize = 0; |
| 87 | for (size_t i = 0; i < inOutScriptsCount; ++i) |
| 88 | { |
| 89 | auto const currentScript = inOutScripts[i]; |
| 90 | if (contains(currentScript)) |
| 91 | inOutScripts[outSize++] = currentScript; |
| 92 | } |
| 93 | |
| 94 | return outSize; |
| 95 | } |
| 96 | |
| 97 | // Find the longest sequence of characters from 0 and up to length that have at least one common UScriptCode value. |
| 98 | // Writes the common script value to script and returns the length of the sequence. Takes the characters' script |
no test coverage detected