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 <unicode/uchar.h>
20 #include <cmath>
21
22 #include <log/log.h>
23
24 #include <minikin/GraphemeBreak.h>
25 #include <minikin/Measurement.h>
26
27 namespace minikin {
28
29 // These could be considered helper methods of layout, but need only be loosely
30 // coupled, so are separate.
31
getRunAdvance(const float * advances,const uint16_t * buf,size_t layoutStart,size_t start,size_t count,size_t offset)32 static float getRunAdvance(const float* advances,
33 const uint16_t* buf,
34 size_t layoutStart,
35 size_t start,
36 size_t count,
37 size_t offset) {
38 float advance = 0.0f;
39 size_t lastCluster = start;
40 float clusterWidth = 0.0f;
41 for (size_t i = start; i < offset; i++) {
42 float charAdvance = advances[i - layoutStart];
43 if (charAdvance != 0.0f) {
44 advance += charAdvance;
45 lastCluster = i;
46 clusterWidth = charAdvance;
47 }
48 }
49 if (offset < start + count && advances[offset - layoutStart] == 0.0f) {
50 // In the middle of a cluster, distribute width of cluster so that each
51 // grapheme cluster gets an equal share.
52 // TODO: get caret information out of font when that's available
53 size_t nextCluster;
54 for (nextCluster = offset + 1; nextCluster < start + count; nextCluster++) {
55 if (advances[nextCluster - layoutStart] != 0.0f)
56 break;
57 }
58 int numGraphemeClusters = 0;
59 int numGraphemeClustersAfter = 0;
60 for (size_t i = lastCluster; i < nextCluster; i++) {
61 bool isAfter = i >= offset;
62 if (GraphemeBreak::isGraphemeBreak(advances + (start - layoutStart), buf,
63 start, count, i)) {
64 numGraphemeClusters++;
65 if (isAfter) {
66 numGraphemeClustersAfter++;
67 }
68 }
69 }
70 if (numGraphemeClusters > 0) {
71 advance -= clusterWidth * numGraphemeClustersAfter / numGraphemeClusters;
72 }
73 }
74 return advance;
75 }
76
getRunAdvance(const float * advances,const uint16_t * buf,size_t start,size_t count,size_t offset)77 float getRunAdvance(const float* advances,
78 const uint16_t* buf,
79 size_t start,
80 size_t count,
81 size_t offset) {
82 return getRunAdvance(advances, buf, start, start, count, offset);
83 }
84
85 /**
86 * Essentially the inverse of getRunAdvance. Compute the value of offset for
87 * which the measured caret comes closest to the provided advance param, and
88 * which is on a grapheme cluster boundary.
89 *
90 * The actual implementation fast-forwards through clusters to get "close", then
91 * does a finer-grain search within the cluster and grapheme breaks.
92 */
getOffsetForAdvance(const float * advances,const uint16_t * buf,size_t start,size_t count,float advance)93 size_t getOffsetForAdvance(const float* advances,
94 const uint16_t* buf,
95 size_t start,
96 size_t count,
97 float advance) {
98 float x = 0.0f, xLastClusterStart = 0.0f, xSearchStart = 0.0f;
99 size_t lastClusterStart = start, searchStart = start;
100 for (size_t i = start; i < start + count; i++) {
101 if (GraphemeBreak::isGraphemeBreak(advances, buf, start, count, i)) {
102 searchStart = lastClusterStart;
103 xSearchStart = xLastClusterStart;
104 }
105 float width = advances[i - start];
106 if (width != 0.0f) {
107 lastClusterStart = i;
108 xLastClusterStart = x;
109 x += width;
110 if (x > advance) {
111 break;
112 }
113 }
114 }
115 size_t best = searchStart;
116 float bestDist = FLT_MAX;
117 for (size_t i = searchStart; i <= start + count; i++) {
118 if (GraphemeBreak::isGraphemeBreak(advances, buf, start, count, i)) {
119 // "getRunAdvance(layout, buf, start, count, i) - advance" but more
120 // efficient
121 float delta = getRunAdvance(advances, buf, start, searchStart,
122 count - searchStart, i)
123
124 + xSearchStart - advance;
125 if (std::abs(delta) < bestDist) {
126 bestDist = std::abs(delta);
127 best = i;
128 }
129 if (delta >= 0.0f) {
130 break;
131 }
132 }
133 }
134 return best;
135 }
136
137 } // namespace minikin
138