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