| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) Research In Motion Limited 2010-2012. All rights reserved. | 2 * Copyright (C) Research In Motion Limited 2010-2012. All rights reserved. |
| 3 * | 3 * |
| 4 * This library is free software; you can redistribute it and/or | 4 * This library is free software; you can redistribute it and/or |
| 5 * modify it under the terms of the GNU Library General Public | 5 * modify it under the terms of the GNU Library General Public |
| 6 * License as published by the Free Software Foundation; either | 6 * License as published by the Free Software Foundation; either |
| 7 * version 2 of the License, or (at your option) any later version. | 7 * version 2 of the License, or (at your option) any later version. |
| 8 * | 8 * |
| 9 * This library is distributed in the hope that it will be useful, | 9 * This library is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 | 31 |
| 32 namespace WebCore { | 32 namespace WebCore { |
| 33 | 33 |
| 34 // Base structure for callback user data | 34 // Base structure for callback user data |
| 35 struct SVGTextQuery::Data { | 35 struct SVGTextQuery::Data { |
| 36 Data() | 36 Data() |
| 37 : isVerticalText(false) | 37 : isVerticalText(false) |
| 38 , processedCharacters(0) | 38 , processedCharacters(0) |
| 39 , textRenderer(0) | 39 , textRenderer(0) |
| 40 , textBox(0) | 40 , textBox(0) |
| 41 , ligatureAdjustmentLastStartPositionQuery(-1) | |
| 42 , ligatureAdjustmentLastEndPositionQuery(-1) | |
| 43 , ligatureAdjustmentLastStartPositionResult(-1) | |
| 44 , ligatureAdjustmentLastEndPositionResult(-1) | |
| 45 { | 41 { |
| 46 } | 42 } |
| 47 | 43 |
| 48 void invalidateLigatureAdjustmentCache() | |
| 49 { | |
| 50 ligatureAdjustmentLastStartPositionQuery = ligatureAdjustmentLastEndPosi
tionQuery = -1; | |
| 51 ligatureAdjustmentLastStartPositionResult = ligatureAdjustmentLastEndPos
itionResult = -1; | |
| 52 } | |
| 53 | |
| 54 bool isVerticalText; | 44 bool isVerticalText; |
| 55 unsigned processedCharacters; | 45 unsigned processedCharacters; |
| 56 RenderSVGInlineText* textRenderer; | 46 RenderSVGInlineText* textRenderer; |
| 57 const SVGInlineTextBox* textBox; | 47 const SVGInlineTextBox* textBox; |
| 58 // Ligature adjustment cache. | |
| 59 int ligatureAdjustmentLastStartPositionQuery; | |
| 60 int ligatureAdjustmentLastEndPositionQuery; | |
| 61 int ligatureAdjustmentLastStartPositionResult; | |
| 62 int ligatureAdjustmentLastEndPositionResult; | |
| 63 }; | 48 }; |
| 64 | 49 |
| 65 static inline InlineFlowBox* flowBoxForRenderer(RenderObject* renderer) | 50 static inline InlineFlowBox* flowBoxForRenderer(RenderObject* renderer) |
| 66 { | 51 { |
| 67 if (!renderer) | 52 if (!renderer) |
| 68 return 0; | 53 return 0; |
| 69 | 54 |
| 70 if (renderer->isRenderBlock()) { | 55 if (renderer->isRenderBlock()) { |
| 71 // If we're given a block element, it has to be a RenderSVGText. | 56 // If we're given a block element, it has to be a RenderSVGText. |
| 72 ASSERT(renderer->isSVGText()); | 57 ASSERT(renderer->isSVGText()); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 unsigned fragmentCount = fragments.size(); | 123 unsigned fragmentCount = fragments.size(); |
| 139 for (unsigned i = 0; i < fragmentCount; ++i) { | 124 for (unsigned i = 0; i < fragmentCount; ++i) { |
| 140 const SVGTextFragment& fragment = fragments.at(i); | 125 const SVGTextFragment& fragment = fragments.at(i); |
| 141 if ((this->*fragmentCallback)(queryData, fragment)) | 126 if ((this->*fragmentCallback)(queryData, fragment)) |
| 142 return true; | 127 return true; |
| 143 | 128 |
| 144 processedCharacters += fragment.length; | 129 processedCharacters += fragment.length; |
| 145 } | 130 } |
| 146 | 131 |
| 147 queryData->processedCharacters = processedCharacters; | 132 queryData->processedCharacters = processedCharacters; |
| 148 queryData->invalidateLigatureAdjustmentCache(); | |
| 149 } | 133 } |
| 150 | 134 |
| 151 return false; | 135 return false; |
| 152 } | 136 } |
| 153 | 137 |
| 154 bool SVGTextQuery::mapStartEndPositionsIntoFragmentCoordinates(Data* queryData,
const SVGTextFragment& fragment, int& startPosition, int& endPosition) const | 138 bool SVGTextQuery::mapStartEndPositionsIntoFragmentCoordinates(Data* queryData,
const SVGTextFragment& fragment, int& startPosition, int& endPosition) const |
| 155 { | 139 { |
| 156 // Reuse the same logic used for text selection & painting, to map our query
start/length into start/endPositions of the current text fragment. | 140 // Reuse the same logic used for text selection & painting, to map our query
start/length into start/endPositions of the current text fragment. |
| 157 startPosition -= queryData->processedCharacters; | 141 startPosition -= queryData->processedCharacters; |
| 158 endPosition -= queryData->processedCharacters; | 142 endPosition -= queryData->processedCharacters; |
| 159 | 143 |
| 160 startPosition = max(0, startPosition); | 144 // <startPosition, endPosition> is now a tuple of offsets relative to the cu
rrent text box. |
| 145 // Compute the offsets of the fragment in the same offset space. |
| 146 int fragmentStartInBox = fragment.characterOffset - queryData->textBox->star
t(); |
| 147 int fragmentEndInBox = fragmentStartInBox + fragment.length; |
| 148 |
| 149 // Check if the ranges intersect. |
| 150 startPosition = std::max(startPosition, fragmentStartInBox); |
| 151 endPosition = std::min(endPosition, fragmentEndInBox); |
| 161 | 152 |
| 162 if (startPosition >= endPosition) | 153 if (startPosition >= endPosition) |
| 163 return false; | 154 return false; |
| 164 | 155 |
| 165 modifyStartEndPositionsRespectingLigatures(queryData, startPosition, endPosi
tion); | 156 modifyStartEndPositionsRespectingLigatures(queryData, startPosition, endPosi
tion); |
| 166 if (!queryData->textBox->mapStartEndPositionsIntoFragmentCoordinates(fragmen
t, startPosition, endPosition)) | 157 if (!queryData->textBox->mapStartEndPositionsIntoFragmentCoordinates(fragmen
t, startPosition, endPosition)) |
| 167 return false; | 158 return false; |
| 168 | 159 |
| 169 ASSERT(startPosition < endPosition); | 160 ASSERT(startPosition < endPosition); |
| 170 return true; | 161 return true; |
| 171 } | 162 } |
| 172 | 163 |
| 173 void SVGTextQuery::modifyStartEndPositionsRespectingLigatures(Data* queryData, i
nt& startPosition, int& endPosition) const | 164 void SVGTextQuery::modifyStartEndPositionsRespectingLigatures(Data* queryData, i
nt& startPosition, int& endPosition) const |
| 174 { | 165 { |
| 175 if (queryData->ligatureAdjustmentLastStartPositionQuery == startPosition &&
queryData->ligatureAdjustmentLastEndPositionQuery == endPosition) { | |
| 176 startPosition = queryData->ligatureAdjustmentLastStartPositionResult; | |
| 177 endPosition = queryData->ligatureAdjustmentLastEndPositionResult; | |
| 178 return; | |
| 179 } | |
| 180 | |
| 181 queryData->ligatureAdjustmentLastStartPositionQuery = startPosition; | |
| 182 queryData->ligatureAdjustmentLastEndPositionQuery = endPosition; | |
| 183 | |
| 184 SVGTextLayoutAttributes* layoutAttributes = queryData->textRenderer->layoutA
ttributes(); | 166 SVGTextLayoutAttributes* layoutAttributes = queryData->textRenderer->layoutA
ttributes(); |
| 185 Vector<SVGTextMetrics>& textMetricsValues = layoutAttributes->textMetricsVal
ues(); | 167 Vector<SVGTextMetrics>& textMetricsValues = layoutAttributes->textMetricsVal
ues(); |
| 186 unsigned boxStart = queryData->textBox->start(); | 168 unsigned boxStart = queryData->textBox->start(); |
| 187 unsigned boxLength = queryData->textBox->len(); | 169 unsigned boxLength = queryData->textBox->len(); |
| 188 | 170 |
| 189 unsigned textMetricsOffset = 0; | 171 unsigned textMetricsOffset = 0; |
| 190 unsigned textMetricsSize = textMetricsValues.size(); | 172 unsigned textMetricsSize = textMetricsValues.size(); |
| 191 | 173 |
| 192 unsigned positionOffset = 0; | 174 unsigned positionOffset = 0; |
| 193 unsigned positionSize = layoutAttributes->context()->textLength(); | 175 unsigned positionSize = layoutAttributes->context()->textLength(); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 } | 212 } |
| 231 } | 213 } |
| 232 | 214 |
| 233 if (!alterStartPosition && !alterEndPosition) | 215 if (!alterStartPosition && !alterEndPosition) |
| 234 break; | 216 break; |
| 235 | 217 |
| 236 lastPositionOffset = positionOffset; | 218 lastPositionOffset = positionOffset; |
| 237 positionOffset += metrics.length(); | 219 positionOffset += metrics.length(); |
| 238 } | 220 } |
| 239 | 221 |
| 240 if (alterStartPosition || alterEndPosition) { | 222 if (!alterStartPosition && !alterEndPosition) |
| 241 if (lastPositionOffset != -1 && lastPositionOffset - positionOffset > 1)
{ | 223 return; |
| 242 if (alterStartPosition && startPosition > lastPositionOffset && star
tPosition < static_cast<int>(positionOffset)) | |
| 243 startPosition = lastPositionOffset; | |
| 244 | 224 |
| 245 if (alterEndPosition && endPosition > lastPositionOffset && endPosit
ion < static_cast<int>(positionOffset)) | 225 if (lastPositionOffset != -1 && lastPositionOffset - positionOffset > 1) { |
| 246 endPosition = positionOffset; | 226 if (alterStartPosition && startPosition > lastPositionOffset && startPos
ition < static_cast<int>(positionOffset)) |
| 247 } | 227 startPosition = lastPositionOffset; |
| 228 |
| 229 if (alterEndPosition && endPosition > lastPositionOffset && endPosition
< static_cast<int>(positionOffset)) |
| 230 endPosition = positionOffset; |
| 248 } | 231 } |
| 249 | |
| 250 queryData->ligatureAdjustmentLastStartPositionResult = startPosition; | |
| 251 queryData->ligatureAdjustmentLastEndPositionResult = endPosition; | |
| 252 } | 232 } |
| 253 | 233 |
| 254 // numberOfCharacters() implementation | 234 // numberOfCharacters() implementation |
| 255 bool SVGTextQuery::numberOfCharactersCallback(Data*, const SVGTextFragment&) con
st | 235 bool SVGTextQuery::numberOfCharactersCallback(Data*, const SVGTextFragment&) con
st |
| 256 { | 236 { |
| 257 // no-op | 237 // no-op |
| 258 return false; | 238 return false; |
| 259 } | 239 } |
| 260 | 240 |
| 261 unsigned SVGTextQuery::numberOfCharacters() const | 241 unsigned SVGTextQuery::numberOfCharacters() const |
| (...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 572 return -1; | 552 return -1; |
| 573 | 553 |
| 574 CharacterNumberAtPositionData data(position); | 554 CharacterNumberAtPositionData data(position); |
| 575 if (!executeQuery(&data, &SVGTextQuery::characterNumberAtPositionCallback)) | 555 if (!executeQuery(&data, &SVGTextQuery::characterNumberAtPositionCallback)) |
| 576 return -1; | 556 return -1; |
| 577 | 557 |
| 578 return data.processedCharacters; | 558 return data.processedCharacters; |
| 579 } | 559 } |
| 580 | 560 |
| 581 } | 561 } |
| OLD | NEW |