| OLD | NEW |
| 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 Loading... |
| 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) { |
| 109 // For hit testing, find the closest caret point by incuding |
| 110 // end-half of the previous character and start-half of the current |
| 111 // character. |
| 112 currentAdvance = currentAdvance / 2.0; |
| 113 nextX = currentX + prevAdvance + currentAdvance; |
| 114 } else { |
| 115 nextX = currentX + currentAdvance; |
| 116 } |
| 117 if (currentX <= targetX && targetX <= nextX) | 117 if (currentX <= targetX && targetX <= nextX) |
| 118 return rtl() ? prevCharacterIndex : m_glyphData[glyphIndex].characte
rIndex; | 118 return includePartialGlyphs && rtl() ? prevCharacterIndex : currentC
haracterIndex; |
| 119 currentX = nextX; | 119 currentX = nextX; |
| 120 prevCharacterIndex = currentCharacterIndex; |
| 120 ++glyphIndex; | 121 ++glyphIndex; |
| 121 } | 122 } |
| 122 | 123 |
| 123 return rtl() ? 0 : m_numCharacters; | 124 return rtl() ? 0 : m_numCharacters; |
| 124 } | 125 } |
| 125 | 126 |
| 126 void ShapeResult::RunInfo::setGlyphAndPositions(unsigned index, | 127 void ShapeResult::RunInfo::setGlyphAndPositions(unsigned index, |
| 127 uint16_t glyphId, float advance, float offsetX, float offsetY) | 128 uint16_t glyphId, float advance, float offsetX, float offsetY) |
| 128 { | 129 { |
| 129 HarfBuzzRunGlyphData& data = m_glyphData[index]; | 130 HarfBuzzRunGlyphData& data = m_glyphData[index]; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 | 163 |
| 163 size_t ShapeResult::byteSize() const | 164 size_t ShapeResult::byteSize() const |
| 164 { | 165 { |
| 165 size_t selfByteSize = sizeof(this); | 166 size_t selfByteSize = sizeof(this); |
| 166 for (unsigned i = 0; i < m_runs.size(); ++i) { | 167 for (unsigned i = 0; i < m_runs.size(); ++i) { |
| 167 selfByteSize += m_runs[i]->byteSize(); | 168 selfByteSize += m_runs[i]->byteSize(); |
| 168 } | 169 } |
| 169 return selfByteSize; | 170 return selfByteSize; |
| 170 } | 171 } |
| 171 | 172 |
| 172 int ShapeResult::offsetForPosition(float targetX) const | 173 int ShapeResult::offsetForPosition(float targetX, bool includePartialGlyphs) con
st |
| 173 { | 174 { |
| 174 int charactersSoFar = 0; | 175 int charactersSoFar = 0; |
| 175 float currentX = 0; | 176 float currentX = 0; |
| 176 | 177 |
| 177 if (m_direction == RTL) { | 178 if (m_direction == RTL) { |
| 178 charactersSoFar = m_numCharacters; | 179 charactersSoFar = m_numCharacters; |
| 179 for (unsigned i = 0; i < m_runs.size(); ++i) { | 180 for (unsigned i = 0; i < m_runs.size(); ++i) { |
| 180 if (!m_runs[i]) | 181 if (!m_runs[i]) |
| 181 continue; | 182 continue; |
| 182 charactersSoFar -= m_runs[i]->m_numCharacters; | 183 charactersSoFar -= m_runs[i]->m_numCharacters; |
| 183 float nextX = currentX + m_runs[i]->m_width; | 184 float nextX = currentX + m_runs[i]->m_width; |
| 184 float offsetForRun = targetX - currentX; | 185 float offsetForRun = targetX - currentX; |
| 185 if (offsetForRun >= 0 && offsetForRun <= m_runs[i]->m_width) { | 186 if (offsetForRun >= 0 && offsetForRun <= m_runs[i]->m_width) { |
| 186 // The x value in question is within this script run. | 187 // The x value in question is within this script run. |
| 187 const unsigned index = m_runs[i]->characterIndexForXPosition(off
setForRun); | 188 const unsigned index = m_runs[i]->characterIndexForXPosition(off
setForRun, includePartialGlyphs); |
| 188 return charactersSoFar + index; | 189 return charactersSoFar + index; |
| 189 } | 190 } |
| 190 currentX = nextX; | 191 currentX = nextX; |
| 191 } | 192 } |
| 192 } else { | 193 } else { |
| 193 for (unsigned i = 0; i < m_runs.size(); ++i) { | 194 for (unsigned i = 0; i < m_runs.size(); ++i) { |
| 194 if (!m_runs[i]) | 195 if (!m_runs[i]) |
| 195 continue; | 196 continue; |
| 196 float nextX = currentX + m_runs[i]->m_width; | 197 float nextX = currentX + m_runs[i]->m_width; |
| 197 float offsetForRun = targetX - currentX; | 198 float offsetForRun = targetX - currentX; |
| 198 if (offsetForRun >= 0 && offsetForRun <= m_runs[i]->m_width) { | 199 if (offsetForRun >= 0 && offsetForRun <= m_runs[i]->m_width) { |
| 199 const unsigned index = m_runs[i]->characterIndexForXPosition(off
setForRun); | 200 const unsigned index = m_runs[i]->characterIndexForXPosition(off
setForRun, includePartialGlyphs); |
| 200 return charactersSoFar + index; | 201 return charactersSoFar + index; |
| 201 } | 202 } |
| 202 charactersSoFar += m_runs[i]->m_numCharacters; | 203 charactersSoFar += m_runs[i]->m_numCharacters; |
| 203 currentX = nextX; | 204 currentX = nextX; |
| 204 } | 205 } |
| 205 } | 206 } |
| 206 | 207 |
| 207 return charactersSoFar; | 208 return charactersSoFar; |
| 208 } | 209 } |
| 209 | 210 |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 347 break; | 348 break; |
| 348 } | 349 } |
| 349 } | 350 } |
| 350 } | 351 } |
| 351 // If we didn't find an existing slot to place it, append. | 352 // If we didn't find an existing slot to place it, append. |
| 352 if (run) | 353 if (run) |
| 353 m_runs.append(run.release()); | 354 m_runs.append(run.release()); |
| 354 } | 355 } |
| 355 | 356 |
| 356 } // namespace blink | 357 } // namespace blink |
| OLD | NEW |