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