Home | History | Annotate | Download | only in minikin
      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  */
     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  */
     34 static bool isWordBreakAfter(uint16_t c) {
     35     if (c == ' ' || (0x2000 <= c && c <= 0x200A) || c == 0x3000) {
     36         // spaces
     37         return true;
     38     }
     39     // Note: kana is not included, as sophisticated fonts may kern kana
     40     return false;
     41 }
     42 
     43 static bool isWordBreakBefore(uint16_t c) {
     44     // CJK ideographs (and yijing hexagram symbols)
     45     return isWordBreakAfter(c) || (0x3400 <= c && c <= 0x9FFF);
     46 }
     47 
     48 /**
     49  * Return offset of previous word break. It is either < offset or == 0.
     50  */
     51 size_t getPrevWordBreakForCache(const uint16_t* chars, size_t offset, size_t len) {
     52     if (offset == 0) return 0;
     53     if (offset > len) offset = len;
     54     if (isWordBreakBefore(chars[offset - 1])) {
     55         return offset - 1;
     56     }
     57     for (size_t i = offset - 1; i > 0; i--) {
     58         if (isWordBreakBefore(chars[i]) || isWordBreakAfter(chars[i - 1])) {
     59             return i;
     60         }
     61     }
     62     return 0;
     63 }
     64 
     65 /**
     66  * Return offset of next word break. It is either > offset or == len.
     67  */
     68 size_t getNextWordBreakForCache(const uint16_t* chars, size_t offset, size_t len) {
     69     if (offset >= len) return len;
     70     if (isWordBreakAfter(chars[offset])) {
     71         return offset + 1;
     72     }
     73     for (size_t i = offset + 1; i < len; i++) {
     74         // No need to check isWordBreakAfter(chars[i - 1]) since it is checked
     75         // in previous iteration.  Note that isWordBreakBefore returns true
     76         // whenever isWordBreakAfter returns true.
     77         if (isWordBreakBefore(chars[i])) {
     78             return i;
     79         }
     80     }
     81     return len;
     82 }
     83 
     84 }  // namespace minikin
     85