Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(265)

Side by Side Diff: third_party/WebKit/Source/platform/fonts/shaping/ShapeResult.cpp

Issue 1966703002: Support includePartialGlyphs=false in Font::offsetForPositionForComplexText (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use wordMeasurement.width instead of font.width() Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2012 Google Inc. All rights reserved. 2 * Copyright (c) 2012 Google Inc. All rights reserved.
3 * Copyright (C) 2013 BlackBerry Limited. All rights reserved. 3 * Copyright (C) 2013 BlackBerry Limited. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are 6 * modification, are permitted provided that the following conditions are
7 * met: 7 * met:
8 * 8 *
9 * * Redistributions of source code must retain the above copyright 9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 for (; m_glyphData[glyphIndex].characterIndex == offset; --glyphInde x) { 82 for (; m_glyphData[glyphIndex].characterIndex == offset; --glyphInde x) {
83 position -= m_glyphData[glyphIndex].advance; 83 position -= m_glyphData[glyphIndex].advance;
84 if (!glyphIndex) 84 if (!glyphIndex)
85 break; 85 break;
86 } 86 }
87 } 87 }
88 } 88 }
89 return position; 89 return position;
90 } 90 }
91 91
92 int ShapeResult::RunInfo::characterIndexForXPosition(float targetX) const 92 int ShapeResult::RunInfo::characterIndexForXPosition(float targetX, bool include PartialGlyphs) const
93 { 93 {
94 ASSERT(targetX <= m_width); 94 DCHECK(targetX >= 0 && targetX <= m_width);
95 const unsigned numGlyphs = m_glyphData.size(); 95 const unsigned numGlyphs = m_glyphData.size();
96 float currentX = 0; 96 float currentX = 0;
97 float currentAdvance = m_glyphData[0].advance; 97 float currentAdvance = 0;
98 unsigned glyphIndex = 0; 98 unsigned glyphIndex = 0;
99 unsigned prevCharacterIndex = m_numCharacters; // used only when rtl()
99 100
100 // Sum up advances that belong to the first character.
101 while (glyphIndex < numGlyphs - 1 && m_glyphData[glyphIndex].characterIndex == m_glyphData[glyphIndex + 1].characterIndex)
102 currentAdvance += m_glyphData[++glyphIndex].advance;
103 currentAdvance = currentAdvance / 2.0;
104 if (targetX <= currentAdvance)
105 return rtl() ? m_numCharacters : 0;
106
107 currentX = currentAdvance;
108 ++glyphIndex;
109 while (glyphIndex < numGlyphs) { 101 while (glyphIndex < numGlyphs) {
110 unsigned prevCharacterIndex = m_glyphData[glyphIndex - 1].characterIndex ;
111 float prevAdvance = currentAdvance; 102 float prevAdvance = currentAdvance;
103 unsigned currentCharacterIndex = m_glyphData[glyphIndex].characterIndex;
112 currentAdvance = m_glyphData[glyphIndex].advance; 104 currentAdvance = m_glyphData[glyphIndex].advance;
113 while (glyphIndex < numGlyphs - 1 && m_glyphData[glyphIndex].characterIn dex == m_glyphData[glyphIndex + 1].characterIndex) 105 while (glyphIndex < numGlyphs - 1 && currentCharacterIndex == m_glyphDat a[glyphIndex + 1].characterIndex)
114 currentAdvance += m_glyphData[++glyphIndex].advance; 106 currentAdvance += m_glyphData[++glyphIndex].advance;
115 currentAdvance = currentAdvance / 2.0; 107 float nextX;
116 float nextX = currentX + prevAdvance + currentAdvance; 108 if (includePartialGlyphs) {
eae 2016/05/12 16:12:45 Could you add a comment here explaining the logic
kojii 2016/05/12 16:35:58 Done, thank you!
109 currentAdvance = currentAdvance / 2.0;
110 nextX = currentX + prevAdvance + currentAdvance;
111 } else {
112 nextX = currentX + currentAdvance;
113 }
117 if (currentX <= targetX && targetX <= nextX) 114 if (currentX <= targetX && targetX <= nextX)
118 return rtl() ? prevCharacterIndex : m_glyphData[glyphIndex].characte rIndex; 115 return includePartialGlyphs && rtl() ? prevCharacterIndex : currentC haracterIndex;
119 currentX = nextX; 116 currentX = nextX;
117 prevCharacterIndex = currentCharacterIndex;
120 ++glyphIndex; 118 ++glyphIndex;
121 } 119 }
122 120
123 return rtl() ? 0 : m_numCharacters; 121 return rtl() ? 0 : m_numCharacters;
124 } 122 }
125 123
126 void ShapeResult::RunInfo::setGlyphAndPositions(unsigned index, 124 void ShapeResult::RunInfo::setGlyphAndPositions(unsigned index,
127 uint16_t glyphId, float advance, float offsetX, float offsetY) 125 uint16_t glyphId, float advance, float offsetX, float offsetY)
128 { 126 {
129 HarfBuzzRunGlyphData& data = m_glyphData[index]; 127 HarfBuzzRunGlyphData& data = m_glyphData[index];
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 160
163 size_t ShapeResult::byteSize() const 161 size_t ShapeResult::byteSize() const
164 { 162 {
165 size_t selfByteSize = sizeof(this); 163 size_t selfByteSize = sizeof(this);
166 for (unsigned i = 0; i < m_runs.size(); ++i) { 164 for (unsigned i = 0; i < m_runs.size(); ++i) {
167 selfByteSize += m_runs[i]->byteSize(); 165 selfByteSize += m_runs[i]->byteSize();
168 } 166 }
169 return selfByteSize; 167 return selfByteSize;
170 } 168 }
171 169
172 int ShapeResult::offsetForPosition(float targetX) const 170 int ShapeResult::offsetForPosition(float targetX, bool includePartialGlyphs) con st
173 { 171 {
174 int charactersSoFar = 0; 172 int charactersSoFar = 0;
175 float currentX = 0; 173 float currentX = 0;
176 174
177 if (m_direction == RTL) { 175 if (m_direction == RTL) {
178 charactersSoFar = m_numCharacters; 176 charactersSoFar = m_numCharacters;
179 for (unsigned i = 0; i < m_runs.size(); ++i) { 177 for (unsigned i = 0; i < m_runs.size(); ++i) {
180 if (!m_runs[i]) 178 if (!m_runs[i])
181 continue; 179 continue;
182 charactersSoFar -= m_runs[i]->m_numCharacters; 180 charactersSoFar -= m_runs[i]->m_numCharacters;
183 float nextX = currentX + m_runs[i]->m_width; 181 float nextX = currentX + m_runs[i]->m_width;
184 float offsetForRun = targetX - currentX; 182 float offsetForRun = targetX - currentX;
185 if (offsetForRun >= 0 && offsetForRun <= m_runs[i]->m_width) { 183 if (offsetForRun >= 0 && offsetForRun <= m_runs[i]->m_width) {
186 // The x value in question is within this script run. 184 // The x value in question is within this script run.
187 const unsigned index = m_runs[i]->characterIndexForXPosition(off setForRun); 185 const unsigned index = m_runs[i]->characterIndexForXPosition(off setForRun, includePartialGlyphs);
188 return charactersSoFar + index; 186 return charactersSoFar + index;
189 } 187 }
190 currentX = nextX; 188 currentX = nextX;
191 } 189 }
192 } else { 190 } else {
193 for (unsigned i = 0; i < m_runs.size(); ++i) { 191 for (unsigned i = 0; i < m_runs.size(); ++i) {
194 if (!m_runs[i]) 192 if (!m_runs[i])
195 continue; 193 continue;
196 float nextX = currentX + m_runs[i]->m_width; 194 float nextX = currentX + m_runs[i]->m_width;
197 float offsetForRun = targetX - currentX; 195 float offsetForRun = targetX - currentX;
198 if (offsetForRun >= 0 && offsetForRun <= m_runs[i]->m_width) { 196 if (offsetForRun >= 0 && offsetForRun <= m_runs[i]->m_width) {
199 const unsigned index = m_runs[i]->characterIndexForXPosition(off setForRun); 197 const unsigned index = m_runs[i]->characterIndexForXPosition(off setForRun, includePartialGlyphs);
200 return charactersSoFar + index; 198 return charactersSoFar + index;
201 } 199 }
202 charactersSoFar += m_runs[i]->m_numCharacters; 200 charactersSoFar += m_runs[i]->m_numCharacters;
203 currentX = nextX; 201 currentX = nextX;
204 } 202 }
205 } 203 }
206 204
207 return charactersSoFar; 205 return charactersSoFar;
208 } 206 }
209 207
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
347 break; 345 break;
348 } 346 }
349 } 347 }
350 } 348 }
351 // If we didn't find an existing slot to place it, append. 349 // If we didn't find an existing slot to place it, append.
352 if (run) 350 if (run)
353 m_runs.append(run.release()); 351 m_runs.append(run.release());
354 } 352 }
355 353
356 } // namespace blink 354 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698