GraphemeBreak.cpp revision 0036da164e3b25f1ac29c840c1fe15b03dc6677f
1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include <stdint.h> 18#include <unicode/uchar.h> 19#include <unicode/utf16.h> 20 21#include <minikin/GraphemeBreak.h> 22#include "MinikinInternal.h" 23 24namespace android { 25 26int32_t tailoredGraphemeClusterBreak(uint32_t c) { 27 // Characters defined as Control that we want to treat them as Extend. 28 // These are curated manually. 29 if (c == 0x00AD // SHY 30 || c == 0x061C // ALM 31 || c == 0x180E // MONGOLIAN VOWEL SEPARATOR 32 || c == 0x200B // ZWSP 33 || c == 0x200E // LRM 34 || c == 0x200F // RLM 35 || (0x202A <= c && c <= 0x202E) // LRE, RLE, PDF, LRO, RLO 36 || ((c | 0xF) == 0x206F) // WJ, invisible math operators, LRI, RLI, FSI, PDI, 37 // and the deprecated invisible format controls 38 || c == 0xFEFF // BOM 39 || ((c | 0x7F) == 0xE007F)) // recently undeprecated tag characters in Plane 14 40 return U_GCB_EXTEND; 41 // UTC-approved characters for the Prepend class, per 42 // http://www.unicode.org/L2/L2015/15183r-graph-cluster-brk.txt 43 // These should be removed when our copy of ICU gets updated to Unicode 9.0 (~2016 or 2017). 44 else if ((0x0600 <= c && c <= 0x0605) // Arabic subtending marks 45 || c == 0x06DD // ARABIC SUBTENDING MARK 46 || c == 0x070F // SYRIAC ABBREVIATION MARK 47 || c == 0x0D4E // MALAYALAM LETTER DOT REPH 48 || c == 0x110BD // KAITHI NUMBER SIGN 49 || c == 0x111C2 // SHARADA SIGN JIHVAMULIYA 50 || c == 0x111C3) // SHARADA SIGN UPADHMANIYA 51 return U_GCB_PREPEND; 52 // THAI CHARACTER SARA AM is treated as a normal letter by most other implementations: they 53 // allow a grapheme break before it. 54 else if (c == 0x0E33) 55 return U_GCB_OTHER; 56 else 57 return u_getIntPropertyValue(c, UCHAR_GRAPHEME_CLUSTER_BREAK); 58} 59 60// Returns true for all characters whose IndicSyllabicCategory is Pure_Killer. 61// From http://www.unicode.org/Public/8.0.0/ucd/IndicSyllabicCategory.txt 62bool isPureKiller(uint32_t c) { 63 return (c == 0x0E3A || c == 0x0E4E || c == 0x0F84 || c == 0x103A || c == 0x1714 || c == 0x1734 64 || c == 0x17D1 || c == 0x1BAA || c == 0x1BF2 || c == 0x1BF3 || c == 0xA806 65 || c == 0xA953 || c == 0xABED || c == 0x11134 || c == 0x112EA || c == 0x1172B); 66} 67 68// Returns true if the character appears before or after zwj in a zwj emoji sequence. See 69// http://www.unicode.org/emoji/charts/emoji-zwj-sequences.html 70bool isZwjEmoji(uint32_t c) { 71 return (c == 0x2764 // HEAVY BLACK HEART 72 || c == 0x1F468 // MAN 73 || c == 0x1F469 // WOMAN 74 || c == 0x1F48B // KISS MARK 75 || c == 0x1F466 // BOY 76 || c == 0x1F467 // GIRL 77 || c == 0x1F441 // EYE 78 || c == 0x1F5E8); // LEFT SPEECH BUBBLE 79} 80 81bool GraphemeBreak::isGraphemeBreak(const uint16_t* buf, size_t start, size_t count, 82 size_t offset) { 83 // This implementation closely follows Unicode Standard Annex #29 on 84 // Unicode Text Segmentation (http://www.unicode.org/reports/tr29/), 85 // implementing a tailored version of extended grapheme clusters. 86 // The GB rules refer to section 3.1.1, Grapheme Cluster Boundary Rules. 87 88 // Rule GB1, sot ÷; Rule GB2, ÷ eot 89 if (offset <= start || offset >= start + count) { 90 return true; 91 } 92 if (U16_IS_TRAIL(buf[offset])) { 93 // Don't break a surrogate pair, but a lonely trailing surrogate pair is a break 94 return !U16_IS_LEAD(buf[offset - 1]); 95 } 96 uint32_t c1 = 0; 97 uint32_t c2 = 0; 98 size_t offset_back = offset; 99 U16_PREV(buf, start, offset_back, c1); 100 U16_NEXT(buf, offset, start + count, c2); 101 int32_t p1 = tailoredGraphemeClusterBreak(c1); 102 int32_t p2 = tailoredGraphemeClusterBreak(c2); 103 // Rule GB3, CR x LF 104 if (p1 == U_GCB_CR && p2 == U_GCB_LF) { 105 return false; 106 } 107 // Rule GB4, (Control | CR | LF) ÷ 108 if (p1 == U_GCB_CONTROL || p1 == U_GCB_CR || p1 == U_GCB_LF) { 109 return true; 110 } 111 // Rule GB5, ÷ (Control | CR | LF) 112 if (p2 == U_GCB_CONTROL || p2 == U_GCB_CR || p2 == U_GCB_LF) { 113 return true; 114 } 115 // Rule GB6, L x ( L | V | LV | LVT ) 116 if (p1 == U_GCB_L && (p2 == U_GCB_L || p2 == U_GCB_V || p2 == U_GCB_LV || p2 == U_GCB_LVT)) { 117 return false; 118 } 119 // Rule GB7, ( LV | V ) x ( V | T ) 120 if ((p1 == U_GCB_LV || p1 == U_GCB_V) && (p2 == U_GCB_V || p2 == U_GCB_T)) { 121 return false; 122 } 123 // Rule GB8, ( LVT | T ) x T 124 if ((p1 == U_GCB_LVT || p1 == U_GCB_T) && p2 == U_GCB_T) { 125 return false; 126 } 127 // Rule GB8a, Regional_Indicator x Regional_Indicator 128 // 129 // Known limitation: This is overly conservative, and returns no grapheme breaks between two 130 // flags, such as in the character sequence "U+1F1FA U+1F1F8 [potential break] U+1F1FA U+1F1F8". 131 // Also, it assumes that all combinations of Regional Indicators produce a flag, where they 132 // don't. 133 // 134 // There is no easy solution for doing this correctly, except for querying the font and doing 135 // some lookback. 136 if (p1 == U_GCB_REGIONAL_INDICATOR && p2 == U_GCB_REGIONAL_INDICATOR) { 137 return false; 138 } 139 // Rule GB9, x Extend; Rule GB9a, x SpacingMark; Rule GB9b, Prepend x 140 if (p2 == U_GCB_EXTEND || p2 == U_GCB_SPACING_MARK || p1 == U_GCB_PREPEND) { 141 return false; 142 } 143 // Cluster indic syllables together (tailoring of UAX #29) 144 // Known limitation: this is overly conservative, and assumes that the virama may form a 145 // conjunct with the following letter, which doesn't always happen. 146 // 147 // There is no easy solution to do this correctly. Even querying the font does not help (with 148 // the current font technoloies), since the font may be creating the conjunct using multiple 149 // glyphs, while the user may be perceiving that sequence of glyphs as one conjunct or one 150 // letter. 151 if (u_getIntPropertyValue(c1, UCHAR_CANONICAL_COMBINING_CLASS) == 9 // virama 152 && !isPureKiller(c1) 153 && u_getIntPropertyValue(c2, UCHAR_GENERAL_CATEGORY) == U_OTHER_LETTER) { 154 return false; 155 } 156 // Tailoring: make emoji sequences with ZWJ a single grapheme cluster 157 if (c1 == 0x200D && isZwjEmoji(c2) && offset_back > start) { 158 // look at character before ZWJ to see that both can participate in an emoji zwj sequence 159 uint32_t c0 = 0; 160 U16_PREV(buf, start, offset_back, c0); 161 if (c0 == 0xFE0F && offset_back > start) { 162 // skip over emoji variation selector 163 U16_PREV(buf, start, offset_back, c0); 164 } 165 if (isZwjEmoji(c0)) { 166 return false; 167 } 168 } 169 // Proposed Rule GB9c from http://www.unicode.org/L2/L2016/16011r3-break-prop-emoji.pdf 170 // E_Base x E_Modifier 171 if (isEmojiModifier(c2)) { 172 if (c1 == 0xFE0F && offset_back > start) { 173 // skip over emoji variation selector 174 U16_PREV(buf, start, offset_back, c1); 175 } 176 if (isEmojiBase(c1)) { 177 return false; 178 } 179 } 180 // Rule GB10, Any ÷ Any 181 return true; 182} 183 184size_t GraphemeBreak::getTextRunCursor(const uint16_t* buf, size_t start, size_t count, 185 size_t offset, MoveOpt opt) { 186 switch (opt) { 187 case AFTER: 188 if (offset < start + count) { 189 offset++; 190 } 191 // fall through 192 case AT_OR_AFTER: 193 while (!isGraphemeBreak(buf, start, count, offset)) { 194 offset++; 195 } 196 break; 197 case BEFORE: 198 if (offset > start) { 199 offset--; 200 } 201 // fall through 202 case AT_OR_BEFORE: 203 while (!isGraphemeBreak(buf, start, count, offset)) { 204 offset--; 205 } 206 break; 207 case AT: 208 if (!isGraphemeBreak(buf, start, count, offset)) { 209 offset = (size_t)-1; 210 } 211 break; 212 } 213 return offset; 214} 215 216} // namespace android 217