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