This source file includes following definitions.
- flowBoxForRenderer
- collectTextBoxesInFlowBox
- executeQuery
- mapStartEndPositionsIntoFragmentCoordinates
- modifyStartEndPositionsRespectingLigatures
- numberOfCharactersCallback
- numberOfCharacters
- textLengthCallback
- textLength
- subStringLengthCallback
- subStringLength
- startPositionOfCharacterCallback
- startPositionOfCharacter
- endPositionOfCharacterCallback
- endPositionOfCharacter
- rotationOfCharacterCallback
- rotationOfCharacter
- calculateGlyphBoundaries
- extentOfCharacterCallback
- extentOfCharacter
- characterNumberAtPositionCallback
- characterNumberAtPosition
#include "config.h"
#include "core/rendering/svg/SVGTextQuery.h"
#include "core/rendering/InlineFlowBox.h"
#include "core/rendering/RenderBlock.h"
#include "core/rendering/RenderInline.h"
#include "core/rendering/svg/RenderSVGInlineText.h"
#include "core/rendering/svg/SVGInlineTextBox.h"
#include "core/rendering/svg/SVGTextMetrics.h"
#include "platform/FloatConversion.h"
#include "wtf/MathExtras.h"
namespace WebCore {
struct SVGTextQuery::Data {
Data()
: isVerticalText(false)
, processedCharacters(0)
, textRenderer(0)
, textBox(0)
{
}
bool isVerticalText;
unsigned processedCharacters;
RenderSVGInlineText* textRenderer;
const SVGInlineTextBox* textBox;
};
static inline InlineFlowBox* flowBoxForRenderer(RenderObject* renderer)
{
if (!renderer)
return 0;
if (renderer->isRenderBlock()) {
ASSERT(renderer->isSVGText());
RenderBlock* renderBlock = toRenderBlock(renderer);
InlineFlowBox* flowBox = renderBlock->firstLineBox();
ASSERT(flowBox == renderBlock->lastLineBox());
return flowBox;
}
if (renderer->isRenderInline()) {
RenderInline* renderInline = toRenderInline(renderer);
InlineFlowBox* flowBox = renderInline->firstLineBox();
ASSERT(flowBox == renderInline->lastLineBox());
return flowBox;
}
ASSERT_NOT_REACHED();
return 0;
}
SVGTextQuery::SVGTextQuery(RenderObject* renderer)
{
collectTextBoxesInFlowBox(flowBoxForRenderer(renderer));
}
void SVGTextQuery::collectTextBoxesInFlowBox(InlineFlowBox* flowBox)
{
if (!flowBox)
return;
for (InlineBox* child = flowBox->firstChild(); child; child = child->nextOnLine()) {
if (child->isInlineFlowBox()) {
if (!child->renderer().node())
continue;
collectTextBoxesInFlowBox(toInlineFlowBox(child));
continue;
}
if (child->isSVGInlineTextBox())
m_textBoxes.append(toSVGInlineTextBox(child));
}
}
bool SVGTextQuery::executeQuery(Data* queryData, ProcessTextFragmentCallback fragmentCallback) const
{
ASSERT(!m_textBoxes.isEmpty());
unsigned processedCharacters = 0;
unsigned textBoxCount = m_textBoxes.size();
for (unsigned textBoxPosition = 0; textBoxPosition < textBoxCount; ++textBoxPosition) {
queryData->textBox = m_textBoxes.at(textBoxPosition);
queryData->textRenderer = &toRenderSVGInlineText(queryData->textBox->textRenderer());
ASSERT(queryData->textRenderer->style());
ASSERT(queryData->textRenderer->style()->svgStyle());
queryData->isVerticalText = queryData->textRenderer->style()->svgStyle()->isVerticalWritingMode();
const Vector<SVGTextFragment>& fragments = queryData->textBox->textFragments();
unsigned fragmentCount = fragments.size();
for (unsigned i = 0; i < fragmentCount; ++i) {
const SVGTextFragment& fragment = fragments.at(i);
if ((this->*fragmentCallback)(queryData, fragment))
return true;
processedCharacters += fragment.length;
}
queryData->processedCharacters = processedCharacters;
}
return false;
}
bool SVGTextQuery::mapStartEndPositionsIntoFragmentCoordinates(Data* queryData, const SVGTextFragment& fragment, int& startPosition, int& endPosition) const
{
startPosition -= queryData->processedCharacters;
endPosition -= queryData->processedCharacters;
startPosition = max(0, startPosition);
if (startPosition >= endPosition)
return false;
modifyStartEndPositionsRespectingLigatures(queryData, startPosition, endPosition);
if (!queryData->textBox->mapStartEndPositionsIntoFragmentCoordinates(fragment, startPosition, endPosition))
return false;
ASSERT(startPosition < endPosition);
return true;
}
void SVGTextQuery::modifyStartEndPositionsRespectingLigatures(Data* queryData, int& startPosition, int& endPosition) const
{
SVGTextLayoutAttributes* layoutAttributes = queryData->textRenderer->layoutAttributes();
Vector<SVGTextMetrics>& textMetricsValues = layoutAttributes->textMetricsValues();
unsigned boxStart = queryData->textBox->start();
unsigned boxLength = queryData->textBox->len();
unsigned textMetricsOffset = 0;
unsigned textMetricsSize = textMetricsValues.size();
unsigned positionOffset = 0;
unsigned positionSize = layoutAttributes->context()->textLength();
bool alterStartPosition = true;
bool alterEndPosition = true;
int lastPositionOffset = -1;
for (; textMetricsOffset < textMetricsSize && positionOffset < positionSize; ++textMetricsOffset) {
SVGTextMetrics& metrics = textMetricsValues[textMetricsOffset];
if (positionOffset < boxStart) {
positionOffset += metrics.length();
continue;
}
if (positionOffset >= boxStart + boxLength)
break;
if (startPosition == static_cast<int>(positionOffset))
alterStartPosition = false;
if (endPosition == static_cast<int>(positionOffset))
alterEndPosition = false;
if (lastPositionOffset != -1 && lastPositionOffset - positionOffset > 1) {
if (alterStartPosition && startPosition > lastPositionOffset && startPosition < static_cast<int>(positionOffset)) {
startPosition = lastPositionOffset;
alterStartPosition = false;
}
if (alterEndPosition && endPosition > lastPositionOffset && endPosition < static_cast<int>(positionOffset)) {
endPosition = positionOffset;
alterEndPosition = false;
}
}
if (!alterStartPosition && !alterEndPosition)
break;
lastPositionOffset = positionOffset;
positionOffset += metrics.length();
}
if (!alterStartPosition && !alterEndPosition)
return;
if (lastPositionOffset != -1 && lastPositionOffset - positionOffset > 1) {
if (alterStartPosition && startPosition > lastPositionOffset && startPosition < static_cast<int>(positionOffset))
startPosition = lastPositionOffset;
if (alterEndPosition && endPosition > lastPositionOffset && endPosition < static_cast<int>(positionOffset))
endPosition = positionOffset;
}
}
bool SVGTextQuery::numberOfCharactersCallback(Data*, const SVGTextFragment&) const
{
return false;
}
unsigned SVGTextQuery::numberOfCharacters() const
{
if (m_textBoxes.isEmpty())
return 0;
Data data;
executeQuery(&data, &SVGTextQuery::numberOfCharactersCallback);
return data.processedCharacters;
}
struct TextLengthData : SVGTextQuery::Data {
TextLengthData()
: textLength(0)
{
}
float textLength;
};
bool SVGTextQuery::textLengthCallback(Data* queryData, const SVGTextFragment& fragment) const
{
TextLengthData* data = static_cast<TextLengthData*>(queryData);
data->textLength += queryData->isVerticalText ? fragment.height : fragment.width;
return false;
}
float SVGTextQuery::textLength() const
{
if (m_textBoxes.isEmpty())
return 0;
TextLengthData data;
executeQuery(&data, &SVGTextQuery::textLengthCallback);
return data.textLength;
}
struct SubStringLengthData : SVGTextQuery::Data {
SubStringLengthData(unsigned queryStartPosition, unsigned queryLength)
: startPosition(queryStartPosition)
, length(queryLength)
, subStringLength(0)
{
}
unsigned startPosition;
unsigned length;
float subStringLength;
};
bool SVGTextQuery::subStringLengthCallback(Data* queryData, const SVGTextFragment& fragment) const
{
SubStringLengthData* data = static_cast<SubStringLengthData*>(queryData);
int startPosition = data->startPosition;
int endPosition = startPosition + data->length;
if (!mapStartEndPositionsIntoFragmentCoordinates(queryData, fragment, startPosition, endPosition))
return false;
SVGTextMetrics metrics = SVGTextMetrics::measureCharacterRange(queryData->textRenderer, fragment.characterOffset + startPosition, endPosition - startPosition);
data->subStringLength += queryData->isVerticalText ? metrics.height() : metrics.width();
return false;
}
float SVGTextQuery::subStringLength(unsigned startPosition, unsigned length) const
{
if (m_textBoxes.isEmpty())
return 0;
SubStringLengthData data(startPosition, length);
executeQuery(&data, &SVGTextQuery::subStringLengthCallback);
return data.subStringLength;
}
struct StartPositionOfCharacterData : SVGTextQuery::Data {
StartPositionOfCharacterData(unsigned queryPosition)
: position(queryPosition)
{
}
unsigned position;
FloatPoint startPosition;
};
bool SVGTextQuery::startPositionOfCharacterCallback(Data* queryData, const SVGTextFragment& fragment) const
{
StartPositionOfCharacterData* data = static_cast<StartPositionOfCharacterData*>(queryData);
int startPosition = data->position;
int endPosition = startPosition + 1;
if (!mapStartEndPositionsIntoFragmentCoordinates(queryData, fragment, startPosition, endPosition))
return false;
data->startPosition = FloatPoint(fragment.x, fragment.y);
if (startPosition) {
SVGTextMetrics metrics = SVGTextMetrics::measureCharacterRange(queryData->textRenderer, fragment.characterOffset, startPosition);
if (queryData->isVerticalText)
data->startPosition.move(0, metrics.height());
else
data->startPosition.move(metrics.width(), 0);
}
AffineTransform fragmentTransform;
fragment.buildFragmentTransform(fragmentTransform, SVGTextFragment::TransformIgnoringTextLength);
if (fragmentTransform.isIdentity())
return true;
data->startPosition = fragmentTransform.mapPoint(data->startPosition);
return true;
}
FloatPoint SVGTextQuery::startPositionOfCharacter(unsigned position) const
{
if (m_textBoxes.isEmpty())
return FloatPoint();
StartPositionOfCharacterData data(position);
executeQuery(&data, &SVGTextQuery::startPositionOfCharacterCallback);
return data.startPosition;
}
struct EndPositionOfCharacterData : SVGTextQuery::Data {
EndPositionOfCharacterData(unsigned queryPosition)
: position(queryPosition)
{
}
unsigned position;
FloatPoint endPosition;
};
bool SVGTextQuery::endPositionOfCharacterCallback(Data* queryData, const SVGTextFragment& fragment) const
{
EndPositionOfCharacterData* data = static_cast<EndPositionOfCharacterData*>(queryData);
int startPosition = data->position;
int endPosition = startPosition + 1;
if (!mapStartEndPositionsIntoFragmentCoordinates(queryData, fragment, startPosition, endPosition))
return false;
data->endPosition = FloatPoint(fragment.x, fragment.y);
SVGTextMetrics metrics = SVGTextMetrics::measureCharacterRange(queryData->textRenderer, fragment.characterOffset, startPosition + 1);
if (queryData->isVerticalText)
data->endPosition.move(0, metrics.height());
else
data->endPosition.move(metrics.width(), 0);
AffineTransform fragmentTransform;
fragment.buildFragmentTransform(fragmentTransform, SVGTextFragment::TransformIgnoringTextLength);
if (fragmentTransform.isIdentity())
return true;
data->endPosition = fragmentTransform.mapPoint(data->endPosition);
return true;
}
FloatPoint SVGTextQuery::endPositionOfCharacter(unsigned position) const
{
if (m_textBoxes.isEmpty())
return FloatPoint();
EndPositionOfCharacterData data(position);
executeQuery(&data, &SVGTextQuery::endPositionOfCharacterCallback);
return data.endPosition;
}
struct RotationOfCharacterData : SVGTextQuery::Data {
RotationOfCharacterData(unsigned queryPosition)
: position(queryPosition)
, rotation(0)
{
}
unsigned position;
float rotation;
};
bool SVGTextQuery::rotationOfCharacterCallback(Data* queryData, const SVGTextFragment& fragment) const
{
RotationOfCharacterData* data = static_cast<RotationOfCharacterData*>(queryData);
int startPosition = data->position;
int endPosition = startPosition + 1;
if (!mapStartEndPositionsIntoFragmentCoordinates(queryData, fragment, startPosition, endPosition))
return false;
AffineTransform fragmentTransform;
fragment.buildFragmentTransform(fragmentTransform, SVGTextFragment::TransformIgnoringTextLength);
if (fragmentTransform.isIdentity())
data->rotation = 0;
else {
fragmentTransform.scale(1 / fragmentTransform.xScale(), 1 / fragmentTransform.yScale());
data->rotation = narrowPrecisionToFloat(rad2deg(atan2(fragmentTransform.b(), fragmentTransform.a())));
}
return true;
}
float SVGTextQuery::rotationOfCharacter(unsigned position) const
{
if (m_textBoxes.isEmpty())
return 0;
RotationOfCharacterData data(position);
executeQuery(&data, &SVGTextQuery::rotationOfCharacterCallback);
return data.rotation;
}
struct ExtentOfCharacterData : SVGTextQuery::Data {
ExtentOfCharacterData(unsigned queryPosition)
: position(queryPosition)
{
}
unsigned position;
FloatRect extent;
};
static inline void calculateGlyphBoundaries(SVGTextQuery::Data* queryData, const SVGTextFragment& fragment, int startPosition, FloatRect& extent)
{
float scalingFactor = queryData->textRenderer->scalingFactor();
ASSERT(scalingFactor);
extent.setLocation(FloatPoint(fragment.x, fragment.y - queryData->textRenderer->scaledFont().fontMetrics().floatAscent() / scalingFactor));
if (startPosition) {
SVGTextMetrics metrics = SVGTextMetrics::measureCharacterRange(queryData->textRenderer, fragment.characterOffset, startPosition);
if (queryData->isVerticalText)
extent.move(0, metrics.height());
else
extent.move(metrics.width(), 0);
}
SVGTextMetrics metrics = SVGTextMetrics::measureCharacterRange(queryData->textRenderer, fragment.characterOffset + startPosition, 1);
extent.setSize(FloatSize(metrics.width(), metrics.height()));
AffineTransform fragmentTransform;
fragment.buildFragmentTransform(fragmentTransform, SVGTextFragment::TransformIgnoringTextLength);
extent = fragmentTransform.mapRect(extent);
}
bool SVGTextQuery::extentOfCharacterCallback(Data* queryData, const SVGTextFragment& fragment) const
{
ExtentOfCharacterData* data = static_cast<ExtentOfCharacterData*>(queryData);
int startPosition = data->position;
int endPosition = startPosition + 1;
if (!mapStartEndPositionsIntoFragmentCoordinates(queryData, fragment, startPosition, endPosition))
return false;
calculateGlyphBoundaries(queryData, fragment, startPosition, data->extent);
return true;
}
FloatRect SVGTextQuery::extentOfCharacter(unsigned position) const
{
if (m_textBoxes.isEmpty())
return FloatRect();
ExtentOfCharacterData data(position);
executeQuery(&data, &SVGTextQuery::extentOfCharacterCallback);
return data.extent;
}
struct CharacterNumberAtPositionData : SVGTextQuery::Data {
CharacterNumberAtPositionData(const FloatPoint& queryPosition)
: position(queryPosition)
{
}
FloatPoint position;
};
bool SVGTextQuery::characterNumberAtPositionCallback(Data* queryData, const SVGTextFragment& fragment) const
{
CharacterNumberAtPositionData* data = static_cast<CharacterNumberAtPositionData*>(queryData);
FloatRect extent;
for (unsigned i = 0; i < fragment.length; ++i) {
int startPosition = data->processedCharacters + i;
int endPosition = startPosition + 1;
if (!mapStartEndPositionsIntoFragmentCoordinates(queryData, fragment, startPosition, endPosition))
continue;
calculateGlyphBoundaries(queryData, fragment, startPosition, extent);
if (extent.contains(data->position)) {
data->processedCharacters += i;
return true;
}
}
return false;
}
int SVGTextQuery::characterNumberAtPosition(const FloatPoint& position) const
{
if (m_textBoxes.isEmpty())
return -1;
CharacterNumberAtPositionData data(position);
if (!executeQuery(&data, &SVGTextQuery::characterNumberAtPositionCallback))
return -1;
return data.processedCharacters;
}
}