1 /*
2 * Copyright (C) 2015 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 "LayoutUtils.h"
18
19 namespace minikin {
20
21 /*
22 * Determine whether the code unit is a word space for the purposes of justification.
23 * TODO: Support NBSP and other stretchable whitespace (b/34013491 and b/68204709).
24 */
isWordSpace(uint16_t code_unit)25 bool isWordSpace(uint16_t code_unit) {
26 return code_unit == ' ';
27 }
28
29 /**
30 * For the purpose of layout, a word break is a boundary with no
31 * kerning or complex script processing. This is necessarily a
32 * heuristic, but should be accurate most of the time.
33 */
isWordBreakAfter(uint16_t c)34 static bool isWordBreakAfter(uint16_t c) {
35 if (c == ' ' || (0x2000 <= c && c <= 0x200A) || c == 0x3000) {
36 // spaces
37 return true;
38 }
39 // Break layout context before and after BiDi control character.
40 if ((0x2066 <= c && c <= 0x2069) || (0x202A <= c && c <= 0x202E) || c == 0x200E ||
41 c == 0x200F) {
42 return true;
43 }
44 // Note: kana is not included, as sophisticated fonts may kern kana
45 return false;
46 }
47
isWordBreakBefore(uint16_t c)48 static bool isWordBreakBefore(uint16_t c) {
49 // CJK ideographs (and yijing hexagram symbols)
50 return isWordBreakAfter(c) || (0x3400 <= c && c <= 0x9FFF);
51 }
52
53 /**
54 * Return offset of previous word break. It is either < offset or == 0.
55 */
getPrevWordBreakForCache(const U16StringPiece & textBuf,uint32_t offset)56 uint32_t getPrevWordBreakForCache(const U16StringPiece& textBuf, uint32_t offset) {
57 if (offset == 0) return 0;
58 if (offset > textBuf.size()) offset = textBuf.size();
59 if (isWordBreakBefore(textBuf[offset - 1])) {
60 return offset - 1;
61 }
62 for (uint32_t i = offset - 1; i > 0; i--) {
63 if (isWordBreakBefore(textBuf[i]) || isWordBreakAfter(textBuf[i - 1])) {
64 return i;
65 }
66 }
67 return 0;
68 }
69
70 /**
71 * Return offset of next word break. It is either > offset or == len.
72 */
getNextWordBreakForCache(const U16StringPiece & textBuf,uint32_t offset)73 uint32_t getNextWordBreakForCache(const U16StringPiece& textBuf, uint32_t offset) {
74 if (offset >= textBuf.size()) return textBuf.size();
75 if (isWordBreakAfter(textBuf[offset])) {
76 return offset + 1;
77 }
78 for (uint32_t i = offset + 1; i < textBuf.size(); i++) {
79 // No need to check isWordBreakAfter(chars[i - 1]) since it is checked
80 // in previous iteration. Note that isWordBreakBefore returns true
81 // whenever isWordBreakAfter returns true.
82 if (isWordBreakBefore(textBuf[i])) {
83 return i;
84 }
85 }
86 return textBuf.size();
87 }
88
89 } // namespace minikin
90