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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
/*
* Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. 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 "LegacyInlineFlowBox.h"
#include "CSSPropertyNames.h"
#include "Document.h"
#include "FontCascade.h"
#include "GraphicsContext.h"
#include "HitTestResult.h"
#include "InlineBoxPainter.h"
#include "LegacyInlineTextBox.h"
#include "LegacyRootInlineBox.h"
#include "RenderBlock.h"
#include "RenderBoxInlines.h"
#include "RenderElementInlines.h"
#include "RenderInline.h"
#include "RenderLayer.h"
#include "RenderTableCell.h"
#include "RenderTheme.h"
#include "RenderView.h"
#include "Settings.h"
#include "Text.h"
#include "TextBoxPainter.h"
#include "TextDecorationThickness.h"
#include "TextUnderlineOffset.h"
#include <math.h>
#include <wtf/TZoneMallocInlines.h>
namespace WebCore {
WTF_MAKE_TZONE_OR_ISO_ALLOCATED_IMPL(LegacyInlineFlowBox);
struct SameSizeAsLegacyInlineFlowBox : public LegacyInlineBox {
uint32_t bitfields : 23;
void* pointers[5];
};
static_assert(sizeof(LegacyInlineFlowBox) == sizeof(SameSizeAsLegacyInlineFlowBox), "LegacyInlineFlowBox should stay small");
#if !ASSERT_WITH_SECURITY_IMPLICATION_DISABLED
LegacyInlineFlowBox::~LegacyInlineFlowBox()
{
setHasBadChildList();
}
void LegacyInlineFlowBox::setHasBadChildList()
{
assertNotDeleted();
if (m_hasBadChildList)
return;
for (auto* child = firstChild(); child; child = child->nextOnLine())
child->setHasBadParent();
m_hasBadChildList = true;
}
#endif
static void setHasTextDescendantsOnAncestors(LegacyInlineFlowBox* box)
{
while (box && !box->hasTextDescendants()) {
box->setHasTextDescendants();
box = box->parent();
}
}
void LegacyInlineFlowBox::addToLine(LegacyInlineBox* child)
{
ASSERT(!child->parent());
ASSERT(!child->nextOnLine());
ASSERT(!child->previousOnLine());
checkConsistency();
child->setParent(this);
if (!m_firstChild) {
m_firstChild = child;
m_lastChild = child;
} else {
m_lastChild->setNextOnLine(child);
child->setPreviousOnLine(m_lastChild);
m_lastChild = child;
}
child->setIsFirstLine(isFirstLine());
child->setIsHorizontal(isHorizontal());
if (child->isInlineTextBox()) {
if (child->renderer().parent() == &renderer())
m_hasTextChildren = true;
setHasTextDescendantsOnAncestors(this);
} else if (auto* blockFlow = dynamicDowncast<LegacyInlineFlowBox>(*child)) {
if (blockFlow->hasTextDescendants())
setHasTextDescendantsOnAncestors(this);
}
const RenderStyle& childStyle = child->lineStyle();
if (child->isInlineTextBox()) {
const RenderStyle* childStyle = &child->lineStyle();
bool hasMarkers = false;
if (auto* textBox = dynamicDowncast<LegacyInlineTextBox>(*child))
hasMarkers = textBox->hasMarkers();
if (childStyle->letterSpacing() < 0 || childStyle->textShadow() || childStyle->textEmphasisMark() != TextEmphasisMark::None || childStyle->hasPositiveStrokeWidth() || hasMarkers || !childStyle->textUnderlineOffset().isAuto() || !childStyle->textDecorationThickness().isAuto() || !childStyle->textUnderlinePosition().isEmpty())
child->clearKnownToHaveNoOverflow();
} else if (child->boxModelObject()->hasSelfPaintingLayer())
child->clearKnownToHaveNoOverflow();
else if (childStyle.hasOutlineInVisualOverflow())
child->clearKnownToHaveNoOverflow();
if (lineStyle().hasOutlineInVisualOverflow())
clearKnownToHaveNoOverflow();
if (knownToHaveNoOverflow()) {
auto* flowBox = dynamicDowncast<LegacyInlineFlowBox>(*child);
if (flowBox && !flowBox->knownToHaveNoOverflow())
clearKnownToHaveNoOverflow();
}
if (auto* renderInline = dynamicDowncast<RenderInline>(child->renderer()); renderInline && renderInline->hasSelfPaintingLayer())
m_hasSelfPaintInlineBox = true;
checkConsistency();
}
void LegacyInlineFlowBox::removeChild(LegacyInlineBox* child)
{
checkConsistency();
if (!isDirty())
dirtyLineBoxes();
if (child == m_firstChild)
m_firstChild = child->nextOnLine();
if (child == m_lastChild)
m_lastChild = child->previousOnLine();
if (child->nextOnLine())
child->nextOnLine()->setPreviousOnLine(child->previousOnLine());
if (child->previousOnLine())
child->previousOnLine()->setNextOnLine(child->nextOnLine());
child->setParent(nullptr);
checkConsistency();
}
void LegacyInlineFlowBox::deleteLine()
{
LegacyInlineBox* child = firstChild();
LegacyInlineBox* next = nullptr;
while (child) {
ASSERT(this == child->parent());
next = child->nextOnLine();
#ifndef NDEBUG
child->setParent(nullptr);
#endif
child->deleteLine();
child = next;
}
#ifndef NDEBUG
m_firstChild = nullptr;
m_lastChild = nullptr;
#endif
removeLineBoxFromRenderObject();
delete this;
}
void LegacyInlineFlowBox::removeLineBoxFromRenderObject()
{
downcast<RenderInline>(renderer()).legacyLineBoxes().removeLineBox(this);
}
void LegacyInlineFlowBox::adjustPosition(float dx, float dy)
{
LegacyInlineBox::adjustPosition(dx, dy);
for (auto* child = firstChild(); child; child = child->nextOnLine())
child->adjustPosition(dx, dy);
if (m_overflow)
m_overflow->move(LayoutUnit(dx), LayoutUnit(dy));
}
inline void LegacyInlineFlowBox::addTextBoxVisualOverflow(LegacyInlineTextBox& textBox, GlyphOverflowAndFallbackFontsMap& textBoxDataMap, LayoutRect& logicalVisualOverflow)
{
if (textBox.knownToHaveNoOverflow())
return;
const RenderStyle& lineStyle = this->lineStyle();
GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.find(&textBox);
GlyphOverflow* glyphOverflow = it == textBoxDataMap.end() ? nullptr : &it->value.second;
bool isFlippedLine = lineStyle.writingMode().isLineInverted();
auto topGlyphEdge = glyphOverflow ? (isFlippedLine ? glyphOverflow->bottom : glyphOverflow->top) : 0_lu;
auto bottomGlyphEdge = glyphOverflow ? (isFlippedLine ? glyphOverflow->top : glyphOverflow->bottom) : 0_lu;
auto leftGlyphEdge = glyphOverflow ? glyphOverflow->left : 0_lu;
auto rightGlyphEdge = glyphOverflow ? glyphOverflow->right : 0_lu;
auto viewportSize = textBox.renderer().frame().view() ? textBox.renderer().frame().view()->size() : IntSize();
LayoutUnit strokeOverflow(std::ceil(lineStyle.computedStrokeWidth(viewportSize) / 2.0f));
auto topGlyphOverflow = -strokeOverflow - topGlyphEdge;
auto bottomGlyphOverflow = strokeOverflow + bottomGlyphEdge;
auto leftGlyphOverflow = -strokeOverflow - leftGlyphEdge;
auto rightGlyphOverflow = strokeOverflow + rightGlyphEdge;
if (auto markExistsAndIsAbove = RenderText::emphasisMarkExistsAndIsAbove(textBox.renderer(), lineStyle)) {
LayoutUnit emphasisMarkHeight = lineStyle.fontCascade().emphasisMarkHeight(lineStyle.textEmphasisMarkString());
if (*markExistsAndIsAbove == !lineStyle.writingMode().isBlockFlipped())
topGlyphOverflow = std::min(topGlyphOverflow, -emphasisMarkHeight);
else
bottomGlyphOverflow = std::max(bottomGlyphOverflow, emphasisMarkHeight);
}
// If letter-spacing is negative, we should factor that into right layout overflow. (Even in RTL, letter-spacing is
// applied to the right, so this is not an issue with left overflow.
rightGlyphOverflow -= std::min(0, (int)lineStyle.fontCascade().letterSpacing());
LayoutUnit textShadowLogicalTop;
LayoutUnit textShadowLogicalBottom;
lineStyle.getTextShadowBlockDirectionExtent(textShadowLogicalTop, textShadowLogicalBottom);
LayoutUnit childOverflowLogicalTop = std::min<LayoutUnit>(textShadowLogicalTop + topGlyphOverflow, topGlyphOverflow);
LayoutUnit childOverflowLogicalBottom = std::max<LayoutUnit>(textShadowLogicalBottom + bottomGlyphOverflow, bottomGlyphOverflow);
LayoutUnit textShadowLogicalLeft;
LayoutUnit textShadowLogicalRight;
lineStyle.getTextShadowInlineDirectionExtent(textShadowLogicalLeft, textShadowLogicalRight);
LayoutUnit childOverflowLogicalLeft = std::min<LayoutUnit>(textShadowLogicalLeft + leftGlyphOverflow, leftGlyphOverflow);
LayoutUnit childOverflowLogicalRight = std::max<LayoutUnit>(textShadowLogicalRight + rightGlyphOverflow, rightGlyphOverflow);
LayoutUnit logicalTopVisualOverflow = std::min(LayoutUnit(textBox.logicalTop() + childOverflowLogicalTop), logicalVisualOverflow.y());
LayoutUnit logicalBottomVisualOverflow = std::max(LayoutUnit(textBox.logicalBottom() + childOverflowLogicalBottom), logicalVisualOverflow.maxY());
LayoutUnit logicalLeftVisualOverflow = std::min(LayoutUnit(textBox.logicalLeft() + childOverflowLogicalLeft), logicalVisualOverflow.x());
LayoutUnit logicalRightVisualOverflow = std::max(LayoutUnit(textBox.logicalRight() + childOverflowLogicalRight), logicalVisualOverflow.maxX());
logicalVisualOverflow = LayoutRect(logicalLeftVisualOverflow, logicalTopVisualOverflow, logicalRightVisualOverflow - logicalLeftVisualOverflow, logicalBottomVisualOverflow - logicalTopVisualOverflow);
textBox.setLogicalOverflowRect(logicalVisualOverflow);
}
void LegacyInlineFlowBox::computeOverflow(LayoutUnit lineTop, LayoutUnit lineBottom, GlyphOverflowAndFallbackFontsMap& textBoxDataMap)
{
// If we know we have no overflow, we can just bail.
if (knownToHaveNoOverflow())
return;
m_overflow = { };
// Visual overflow just includes overflow for stuff we need to repaint ourselves. Self-painting layers are ignored.
// Layout overflow is used to determine scrolling extent, so it still includes child layers and also factors in
// transforms, relative positioning, etc.
LayoutRect logicalVisualOverflow(enclosingLayoutRect(logicalFrameRectIncludingLineHeight(lineTop, lineBottom)));
for (auto* child = firstChild(); child; child = child->nextOnLine()) {
if (is<RenderText>(child->renderer())) {
auto& textBox = downcast<LegacyInlineTextBox>(*child);
LayoutRect textBoxOverflow(enclosingLayoutRect(textBox.logicalFrameRect()));
addTextBoxVisualOverflow(textBox, textBoxDataMap, textBoxOverflow);
logicalVisualOverflow.unite(textBoxOverflow);
} else if (is<RenderInline>(child->renderer())) {
auto& flow = downcast<LegacyInlineFlowBox>(*child);
flow.computeOverflow(lineTop, lineBottom, textBoxDataMap);
if (!flow.renderer().hasSelfPaintingLayer())
logicalVisualOverflow.unite(flow.logicalVisualOverflowRect(lineTop, lineBottom));
}
}
setOverflowFromLogicalRects(logicalVisualOverflow, lineTop, lineBottom);
}
void LegacyInlineFlowBox::setVisualOverflow(const LayoutRect& rect, LayoutUnit lineTop, LayoutUnit lineBottom)
{
LayoutRect frameBox = enclosingLayoutRect(frameRectIncludingLineHeight(lineTop, lineBottom));
if (frameBox.contains(rect) || rect.isEmpty())
return;
if (!m_overflow)
m_overflow = makeUnique<RenderOverflow>(frameBox, frameBox);
m_overflow->setVisualOverflow(rect);
}
void LegacyInlineFlowBox::setOverflowFromLogicalRects(const LayoutRect& logicalVisualOverflow, LayoutUnit lineTop, LayoutUnit lineBottom)
{
LayoutRect visualOverflow(isHorizontal() ? logicalVisualOverflow : logicalVisualOverflow.transposedRect());
setVisualOverflow(visualOverflow, lineTop, lineBottom);
}
LegacyInlineBox* LegacyInlineFlowBox::firstLeafDescendant() const
{
LegacyInlineBox* leaf = nullptr;
for (auto* child = firstChild(); child && !leaf; child = child->nextOnLine())
leaf = child->isLeaf() ? child : downcast<LegacyInlineFlowBox>(*child).firstLeafDescendant();
return leaf;
}
LegacyInlineBox* LegacyInlineFlowBox::lastLeafDescendant() const
{
LegacyInlineBox* leaf = nullptr;
for (auto* child = lastChild(); child && !leaf; child = child->previousOnLine())
leaf = child->isLeaf() ? child : downcast<LegacyInlineFlowBox>(*child).lastLeafDescendant();
return leaf;
}
RenderObject::HighlightState LegacyInlineFlowBox::selectionState() const
{
return RenderObject::HighlightState::None;
}
#if ENABLE(TREE_DEBUGGING)
ASCIILiteral LegacyInlineFlowBox::boxName() const
{
return "InlineFlowBox"_s;
}
void LegacyInlineFlowBox::outputLineTreeAndMark(WTF::TextStream& stream, const LegacyInlineBox* markedBox, int depth) const
{
LegacyInlineBox::outputLineTreeAndMark(stream, markedBox, depth);
for (const LegacyInlineBox* box = firstChild(); box; box = box->nextOnLine())
box->outputLineTreeAndMark(stream, markedBox, depth + 1);
}
#endif
#ifndef NDEBUG
void LegacyInlineFlowBox::checkConsistency() const
{
assertNotDeleted();
ASSERT_WITH_SECURITY_IMPLICATION(!m_hasBadChildList);
#ifdef CHECK_CONSISTENCY
const LegacyInlineBox* previousChild = nullptr;
for (const LegacyInlineBox* child = firstChild(); child; child = child->nextOnLine()) {
ASSERT(child->parent() == this);
ASSERT(child->previousOnLine() == previousChild);
previousChild = child;
}
ASSERT(previousChild == m_lastChild);
#endif
}
#endif
} // namespace WebCore
|