RenderSVGInlineText.cpp [plain text]
#include "config.h"
#if ENABLE(SVG)
#include "RenderSVGInlineText.h"
#include "CSSFontSelector.h"
#include "FloatConversion.h"
#include "FloatQuad.h"
#include "RenderBlock.h"
#include "RenderSVGRoot.h"
#include "RenderSVGText.h"
#include "Settings.h"
#include "SVGInlineTextBox.h"
#include "SVGRenderingContext.h"
#include "SVGRootInlineBox.h"
#include "StyleResolver.h"
#include "VisiblePosition.h"
namespace WebCore {
static PassRefPtr<StringImpl> applySVGWhitespaceRules(PassRefPtr<StringImpl> string, bool preserveWhiteSpace)
{
if (preserveWhiteSpace) {
RefPtr<StringImpl> newString = string->replace('\t', ' ');
newString = newString->replace('\n', ' ');
newString = newString->replace('\r', ' ');
return newString.release();
}
RefPtr<StringImpl> newString = string->replace('\n', StringImpl::empty());
newString = newString->replace('\r', StringImpl::empty());
newString = newString->replace('\t', ' ');
return newString.release();
}
RenderSVGInlineText::RenderSVGInlineText(Node* n, PassRefPtr<StringImpl> string)
: RenderText(n, applySVGWhitespaceRules(string, false))
, m_scalingFactor(1)
, m_layoutAttributes(this)
{
}
void RenderSVGInlineText::willBeDestroyed()
{
RenderSVGText* textRenderer = RenderSVGText::locateRenderSVGTextAncestor(this);
if (!textRenderer) {
RenderText::willBeDestroyed();
return;
}
Vector<SVGTextLayoutAttributes*> affectedAttributes;
textRenderer->layoutAttributesWillBeDestroyed(this, affectedAttributes);
RenderText::willBeDestroyed();
if (affectedAttributes.isEmpty())
return;
if (!documentBeingDestroyed())
textRenderer->rebuildLayoutAttributes(affectedAttributes);
}
void RenderSVGInlineText::setTextInternal(PassRefPtr<StringImpl> text)
{
RenderText::setTextInternal(text);
if (RenderSVGText* textRenderer = RenderSVGText::locateRenderSVGTextAncestor(this)) {
textRenderer->invalidateTextPositioningElements();
textRenderer->layoutAttributesChanged(this);
}
}
void RenderSVGInlineText::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
{
RenderText::styleDidChange(diff, oldStyle);
updateScaledFont();
bool newPreserves = style() ? style()->whiteSpace() == PRE : false;
bool oldPreserves = oldStyle ? oldStyle->whiteSpace() == PRE : false;
if (oldPreserves && !newPreserves) {
setText(applySVGWhitespaceRules(originalText(), false), true);
return;
}
if (!oldPreserves && newPreserves) {
setText(applySVGWhitespaceRules(originalText(), true), true);
return;
}
if (diff != StyleDifferenceLayout)
return;
if (RenderSVGText* textRenderer = RenderSVGText::locateRenderSVGTextAncestor(this))
textRenderer->layoutAttributesChanged(this);
}
InlineTextBox* RenderSVGInlineText::createTextBox()
{
InlineTextBox* box = new (renderArena()) SVGInlineTextBox(this);
box->setHasVirtualLogicalHeight();
return box;
}
LayoutRect RenderSVGInlineText::localCaretRect(InlineBox* box, int caretOffset, LayoutUnit*)
{
if (!box || !box->isInlineTextBox())
return LayoutRect();
InlineTextBox* textBox = static_cast<InlineTextBox*>(box);
if (static_cast<unsigned>(caretOffset) < textBox->start() || static_cast<unsigned>(caretOffset) > textBox->start() + textBox->len())
return LayoutRect();
if (static_cast<unsigned>(caretOffset) < textBox->start() + textBox->len()) {
LayoutRect rect = textBox->localSelectionRect(caretOffset, caretOffset + 1);
LayoutUnit x = box->isLeftToRightDirection() ? rect.x() : rect.maxX();
return LayoutRect(x, rect.y(), caretWidth, rect.height());
}
LayoutRect rect = textBox->localSelectionRect(caretOffset - 1, caretOffset);
LayoutUnit x = box->isLeftToRightDirection() ? rect.maxX() : rect.x();
return LayoutRect(x, rect.y(), caretWidth, rect.height());
}
FloatRect RenderSVGInlineText::floatLinesBoundingBox() const
{
FloatRect boundingBox;
for (InlineTextBox* box = firstTextBox(); box; box = box->nextTextBox())
boundingBox.unite(box->calculateBoundaries());
return boundingBox;
}
IntRect RenderSVGInlineText::linesBoundingBox() const
{
return enclosingIntRect(floatLinesBoundingBox());
}
bool RenderSVGInlineText::characterStartsNewTextChunk(int position) const
{
ASSERT(position >= 0);
ASSERT(position < static_cast<int>(textLength()));
if (!position && parent()->isSVGTextPath() && !previousSibling())
return true;
const SVGCharacterDataMap::const_iterator it = m_layoutAttributes.characterDataMap().find(static_cast<unsigned>(position + 1));
if (it == m_layoutAttributes.characterDataMap().end())
return false;
return it->second.x != SVGTextLayoutAttributes::emptyValue() || it->second.y != SVGTextLayoutAttributes::emptyValue();
}
VisiblePosition RenderSVGInlineText::positionForPoint(const LayoutPoint& point)
{
if (!firstTextBox() || !textLength())
return createVisiblePosition(0, DOWNSTREAM);
float baseline = m_scaledFont.fontMetrics().floatAscent();
RenderBlock* containingBlock = this->containingBlock();
ASSERT(containingBlock);
FloatPoint absolutePoint(point);
absolutePoint.moveBy(containingBlock->location());
float closestDistance = std::numeric_limits<float>::max();
float closestDistancePosition = 0;
const SVGTextFragment* closestDistanceFragment = 0;
SVGInlineTextBox* closestDistanceBox = 0;
AffineTransform fragmentTransform;
for (InlineTextBox* box = firstTextBox(); box; box = box->nextTextBox()) {
if (!box->isSVGInlineTextBox())
continue;
SVGInlineTextBox* textBox = static_cast<SVGInlineTextBox*>(box);
Vector<SVGTextFragment>& fragments = textBox->textFragments();
unsigned textFragmentsSize = fragments.size();
for (unsigned i = 0; i < textFragmentsSize; ++i) {
const SVGTextFragment& fragment = fragments.at(i);
FloatRect fragmentRect(fragment.x, fragment.y - baseline, fragment.width, fragment.height);
fragment.buildFragmentTransform(fragmentTransform);
if (!fragmentTransform.isIdentity())
fragmentRect = fragmentTransform.mapRect(fragmentRect);
float distance = powf(fragmentRect.x() - absolutePoint.x(), 2) +
powf(fragmentRect.y() + fragmentRect.height() / 2 - absolutePoint.y(), 2);
if (distance < closestDistance) {
closestDistance = distance;
closestDistanceBox = textBox;
closestDistanceFragment = &fragment;
closestDistancePosition = fragmentRect.x();
}
}
}
if (!closestDistanceFragment)
return createVisiblePosition(0, DOWNSTREAM);
int offset = closestDistanceBox->offsetForPositionInFragment(*closestDistanceFragment, absolutePoint.x() - closestDistancePosition, true);
return createVisiblePosition(offset + closestDistanceBox->start(), offset > 0 ? VP_UPSTREAM_IF_POSSIBLE : DOWNSTREAM);
}
void RenderSVGInlineText::updateScaledFont()
{
computeNewScaledFontForStyle(this, style(), m_scalingFactor, m_scaledFont);
}
void RenderSVGInlineText::computeNewScaledFontForStyle(RenderObject* renderer, const RenderStyle* style, float& scalingFactor, Font& scaledFont)
{
ASSERT(style);
ASSERT(renderer);
Document* document = renderer->document();
ASSERT(document);
StyleResolver* styleResolver = document->styleResolver();
ASSERT(styleResolver);
AffineTransform ctm;
SVGRenderingContext::calculateTransformationToOutermostSVGCoordinateSystem(renderer, ctm);
scalingFactor = narrowPrecisionToFloat(sqrt((pow(ctm.xScale(), 2) + pow(ctm.yScale(), 2)) / 2));
if (scalingFactor == 1 || !scalingFactor || style->fontDescription().textRenderingMode() == GeometricPrecision) {
scalingFactor = 1;
scaledFont = style->font();
return;
}
FontDescription fontDescription(style->fontDescription());
fontDescription.setComputedSize(StyleResolver::getComputedSizeFromSpecifiedSize(document, scalingFactor, fontDescription.isAbsoluteSize(), fontDescription.computedSize(), DoNotUseSmartMinimumForFontSize));
scaledFont = Font(fontDescription, 0, 0);
scaledFont.update(styleResolver->fontSelector());
}
}
#endif // ENABLE(SVG)