1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
/**
* Copyright (C) 2007 Rob Buis <buis@kde.org>
* Copyright (C) 2007 Nikolas Zimmermann <zimmermann@kde.org>
* Copyright (C) Research In Motion Limited 2010. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "core/rendering/svg/SVGInlineTextBox.h"
#include "core/dom/DocumentMarkerController.h"
#include "core/dom/RenderedDocumentMarker.h"
#include "core/editing/Editor.h"
#include "core/frame/LocalFrame.h"
#include "core/paint/SVGInlineTextBoxPainter.h"
#include "core/rendering/HitTestResult.h"
#include "core/rendering/InlineFlowBox.h"
#include "core/rendering/PaintInfo.h"
#include "core/rendering/PointerEventsHitRules.h"
#include "core/rendering/RenderInline.h"
#include "core/rendering/RenderTheme.h"
#include "core/rendering/svg/RenderSVGInlineText.h"
#include "platform/FloatConversion.h"
#include "platform/fonts/FontCache.h"
namespace blink {
struct ExpectedSVGInlineTextBoxSize : public InlineTextBox {
FloatWillBeLayoutUnit float1;
uint32_t bitfields : 1;
Vector<SVGTextFragment> vector;
};
static_assert(sizeof(SVGInlineTextBox) == sizeof(ExpectedSVGInlineTextBoxSize), "SVGInlineTextBox has an unexpected size");
SVGInlineTextBox::SVGInlineTextBox(RenderObject& object, int start, unsigned short length)
: InlineTextBox(object, start, length)
, m_logicalHeight(0)
, m_startsNewTextChunk(false)
{
}
void SVGInlineTextBox::dirtyLineBoxes()
{
InlineTextBox::dirtyLineBoxes();
// Clear the now stale text fragments
clearTextFragments();
// And clear any following text fragments as the text on which they
// depend may now no longer exist, or glyph positions may be wrong
InlineTextBox* nextBox = nextTextBox();
if (nextBox)
nextBox->dirtyLineBoxes();
}
int SVGInlineTextBox::offsetForPosition(FloatWillBeLayoutUnit, bool) const
{
// SVG doesn't use the standard offset <-> position selection system, as it's not suitable for SVGs complex needs.
// vertical text selection, inline boxes spanning multiple lines (contrary to HTML, etc.)
ASSERT_NOT_REACHED();
return 0;
}
int SVGInlineTextBox::offsetForPositionInFragment(const SVGTextFragment& fragment, FloatWillBeLayoutUnit position, bool includePartialGlyphs) const
{
RenderSVGInlineText& textRenderer = toRenderSVGInlineText(this->renderer());
FloatWillBeLayoutUnit scalingFactor = textRenderer.scalingFactor();
ASSERT(scalingFactor);
RenderStyle* style = textRenderer.style();
ASSERT(style);
TextRun textRun = constructTextRun(style, fragment);
// Eventually handle lengthAdjust="spacingAndGlyphs".
// FIXME: Handle vertical text.
AffineTransform fragmentTransform;
fragment.buildFragmentTransform(fragmentTransform);
if (!fragmentTransform.isIdentity())
textRun.setHorizontalGlyphStretch(narrowPrecisionToFloat(fragmentTransform.xScale()));
return fragment.characterOffset - start() + textRenderer.scaledFont().offsetForPosition(textRun, position * scalingFactor, includePartialGlyphs);
}
FloatWillBeLayoutUnit SVGInlineTextBox::positionForOffset(int) const
{
// SVG doesn't use the offset <-> position selection system.
ASSERT_NOT_REACHED();
return 0;
}
FloatRectWillBeLayoutRect SVGInlineTextBox::selectionRectForTextFragment(const SVGTextFragment& fragment, int startPosition, int endPosition, RenderStyle* style)
{
ASSERT(startPosition < endPosition);
ASSERT(style);
RenderSVGInlineText& textRenderer = toRenderSVGInlineText(this->renderer());
FloatWillBeLayoutUnit scalingFactor = textRenderer.scalingFactor();
ASSERT(scalingFactor);
const Font& scaledFont = textRenderer.scaledFont();
const FontMetrics& scaledFontMetrics = scaledFont.fontMetrics();
FloatPointWillBeLayoutPoint textOrigin(fragment.x, fragment.y);
if (scalingFactor != 1)
textOrigin.scale(scalingFactor, scalingFactor);
textOrigin.move(0, -scaledFontMetrics.floatAscent());
FloatRectWillBeLayoutRect selectionRect = scaledFont.selectionRectForText(constructTextRun(style, fragment), textOrigin, fragment.height * scalingFactor, startPosition, endPosition);
if (scalingFactor == 1)
return selectionRect;
selectionRect.scale(1 / scalingFactor);
return selectionRect;
}
LayoutRect SVGInlineTextBox::localSelectionRect(int startPosition, int endPosition)
{
int boxStart = start();
startPosition = std::max(startPosition - boxStart, 0);
endPosition = std::min(endPosition - boxStart, static_cast<int>(len()));
if (startPosition >= endPosition)
return LayoutRect();
RenderStyle* style = renderer().style();
ASSERT(style);
AffineTransform fragmentTransform;
FloatRectWillBeLayoutRect selectionRect;
int fragmentStartPosition = 0;
int fragmentEndPosition = 0;
unsigned textFragmentsSize = m_textFragments.size();
for (unsigned i = 0; i < textFragmentsSize; ++i) {
const SVGTextFragment& fragment = m_textFragments.at(i);
fragmentStartPosition = startPosition;
fragmentEndPosition = endPosition;
if (!mapStartEndPositionsIntoFragmentCoordinates(fragment, fragmentStartPosition, fragmentEndPosition))
continue;
FloatRectWillBeLayoutRect fragmentRect = selectionRectForTextFragment(fragment, fragmentStartPosition, fragmentEndPosition, style);
fragment.buildFragmentTransform(fragmentTransform);
fragmentRect = fragmentTransform.mapRect(fragmentRect);
selectionRect.unite(fragmentRect);
}
// FIXME: the call to rawValue() below is temporary and should be removed once the transition
// to LayoutUnit-based types is complete (crbug.com/321237)
return enclosingIntRect(selectionRect.rawValue());
}
void SVGInlineTextBox::paint(const PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit, LayoutUnit)
{
SVGInlineTextBoxPainter(*this).paint(paintInfo, paintOffset);
}
TextRun SVGInlineTextBox::constructTextRun(RenderStyle* style, const SVGTextFragment& fragment) const
{
ASSERT(style);
RenderText* text = &renderer();
// FIXME(crbug.com/264211): This should not be necessary but can occur if we
// layout during layout. Remove this when 264211 is fixed.
RELEASE_ASSERT(!text->needsLayout());
TextRun run(static_cast<const LChar*>(0) // characters, will be set below if non-zero.
, 0 // length, will be set below if non-zero.
, 0 // xPos, only relevant with allowTabs=true
, 0 // padding, only relevant for justified text, not relevant for SVG
, TextRun::AllowTrailingExpansion
, direction()
, dirOverride() || style->rtlOrdering() == VisualOrder /* directionalOverride */);
if (fragment.length) {
if (text->is8Bit())
run.setText(text->characters8() + fragment.characterOffset, fragment.length);
else
run.setText(text->characters16() + fragment.characterOffset, fragment.length);
}
// We handle letter & word spacing ourselves.
run.disableSpacing();
// Propagate the maximum length of the characters buffer to the TextRun, even when we're only processing a substring.
run.setCharactersLength(text->textLength() - fragment.characterOffset);
ASSERT(run.charactersLength() >= run.length());
return run;
}
bool SVGInlineTextBox::mapStartEndPositionsIntoFragmentCoordinates(const SVGTextFragment& fragment, int& startPosition, int& endPosition) const
{
if (startPosition >= endPosition)
return false;
int offset = static_cast<int>(fragment.characterOffset) - start();
int length = static_cast<int>(fragment.length);
if (startPosition >= offset + length || endPosition <= offset)
return false;
if (startPosition < offset)
startPosition = 0;
else
startPosition -= offset;
if (endPosition > offset + length)
endPosition = length;
else {
ASSERT(endPosition >= offset);
endPosition -= offset;
}
ASSERT(startPosition < endPosition);
return true;
}
void SVGInlineTextBox::paintDocumentMarker(GraphicsContext*, const FloatPointWillBeLayoutPoint&, DocumentMarker*, RenderStyle*, const Font&, bool)
{
// SVG does not have support for generic document markers (e.g., spellchecking, etc).
}
void SVGInlineTextBox::paintTextMatchMarker(GraphicsContext* context, const FloatPointWillBeLayoutPoint& point, DocumentMarker* marker, RenderStyle* style, const Font& font)
{
SVGInlineTextBoxPainter(*this).paintTextMatchMarker(context, point, marker, style, font);
}
FloatRectWillBeLayoutRect SVGInlineTextBox::calculateBoundaries() const
{
FloatRectWillBeLayoutRect textRect;
RenderSVGInlineText& textRenderer = toRenderSVGInlineText(this->renderer());
FloatWillBeLayoutUnit scalingFactor = textRenderer.scalingFactor();
ASSERT(scalingFactor);
FloatWillBeLayoutUnit baseline = textRenderer.scaledFont().fontMetrics().floatAscent() / scalingFactor;
AffineTransform fragmentTransform;
unsigned textFragmentsSize = m_textFragments.size();
for (unsigned i = 0; i < textFragmentsSize; ++i) {
const SVGTextFragment& fragment = m_textFragments.at(i);
FloatRectWillBeLayoutRect fragmentRect(fragment.x, fragment.y - baseline, fragment.width, fragment.height);
fragment.buildFragmentTransform(fragmentTransform);
fragmentRect = fragmentTransform.mapRect(fragmentRect);
textRect.unite(fragmentRect);
}
return textRect;
}
bool SVGInlineTextBox::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit, LayoutUnit)
{
// FIXME: integrate with InlineTextBox::nodeAtPoint better.
ASSERT(!isLineBreak());
PointerEventsHitRules hitRules(PointerEventsHitRules::SVG_TEXT_HITTESTING, request, renderer().style()->pointerEvents());
bool isVisible = renderer().style()->visibility() == VISIBLE;
if (isVisible || !hitRules.requireVisible) {
if (hitRules.canHitBoundingBox
|| (hitRules.canHitStroke && (renderer().style()->svgStyle().hasStroke() || !hitRules.requireStroke))
|| (hitRules.canHitFill && (renderer().style()->svgStyle().hasFill() || !hitRules.requireFill))) {
FloatPointWillBeLayoutPoint boxOrigin(x(), y());
boxOrigin.moveBy(accumulatedOffset);
FloatRectWillBeLayoutRect rect(boxOrigin, size());
// FIXME: both calls to rawValue() below is temporary and should be removed once the transition
// to LayoutUnit-based types is complete (crbug.com/321237)
if (locationInContainer.intersects(rect.rawValue())) {
renderer().updateHitTestResult(result, locationInContainer.point() - toLayoutSize(accumulatedOffset));
if (!result.addNodeToRectBasedTestResult(renderer().node(), request, locationInContainer, rect.rawValue()))
return true;
}
}
}
return false;
}
} // namespace blink
|