• 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 #define LOG_TAG "Minikin"
18 
19 #include "LayoutUtils.h"
20 
21 namespace minikin {
22 
23 const uint16_t CHAR_NBSP = 0x00A0;
24 
25 /*
26  * Determine whether the code unit is a word space for the purposes of justification.
27  */
isWordSpace(uint16_t code_unit)28 bool isWordSpace(uint16_t code_unit) {
29     return code_unit == ' ' || code_unit == CHAR_NBSP;
30 }
31 
32 /**
33  * For the purpose of layout, a word break is a boundary with no
34  * kerning or complex script processing. This is necessarily a
35  * heuristic, but should be accurate most of the time.
36  */
isWordBreakAfter(uint16_t c)37 static bool isWordBreakAfter(uint16_t c) {
38     if (isWordSpace(c) || (c >= 0x2000 && c <= 0x200a) || c == 0x3000) {
39         // spaces
40         return true;
41     }
42     // Note: kana is not included, as sophisticated fonts may kern kana
43     return false;
44 }
45 
isWordBreakBefore(uint16_t c)46 static bool isWordBreakBefore(uint16_t c) {
47     // CJK ideographs (and yijing hexagram symbols)
48     return isWordBreakAfter(c) || (c >= 0x3400 && c <= 0x9fff);
49 }
50 
51 /**
52  * Return offset of previous word break. It is either < offset or == 0.
53  */
getPrevWordBreakForCache(const uint16_t * chars,size_t offset,size_t len)54 size_t getPrevWordBreakForCache(
55         const uint16_t* chars, size_t offset, size_t len) {
56     if (offset == 0) return 0;
57     if (offset > len) offset = len;
58     if (isWordBreakBefore(chars[offset - 1])) {
59         return offset - 1;
60     }
61     for (size_t i = offset - 1; i > 0; i--) {
62         if (isWordBreakBefore(chars[i]) || isWordBreakAfter(chars[i - 1])) {
63             return i;
64         }
65     }
66     return 0;
67 }
68 
69 /**
70  * Return offset of next word break. It is either > offset or == len.
71  */
getNextWordBreakForCache(const uint16_t * chars,size_t offset,size_t len)72 size_t getNextWordBreakForCache(
73         const uint16_t* chars, size_t offset, size_t len) {
74     if (offset >= len) return len;
75     if (isWordBreakAfter(chars[offset])) {
76         return offset + 1;
77     }
78     for (size_t i = offset + 1; i < len; 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(chars[i])) {
83             return i;
84         }
85     }
86     return len;
87 }
88 
89 }  // namespace minikin
90