2 * Copyright (C) 2003, 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 #include "InlineFlowBox.h"
23 #include "CachedImage.h"
25 #include "EllipsisBox.h"
26 #include "GraphicsContext.h"
27 #include "InlineTextBox.h"
28 #include "HitTestResult.h"
29 #include "RootInlineBox.h"
30 #include "RenderBlock.h"
31 #include "RenderInline.h"
32 #include "RenderListMarker.h"
33 #include "RenderTableCell.h"
34 #include "RootInlineBox.h"
45 InlineFlowBox::~InlineFlowBox()
47 if (!m_hasBadChildList)
48 for (InlineBox* child = firstChild(); child; child = child->nextOnLine())
49 child->setHasBadParent();
54 int InlineFlowBox::getFlowSpacingWidth()
56 int totWidth = marginBorderPaddingLeft() + marginBorderPaddingRight();
57 for (InlineBox* curr = firstChild(); curr; curr = curr->nextOnLine()) {
58 if (curr->isInlineFlowBox())
59 totWidth += static_cast<InlineFlowBox*>(curr)->getFlowSpacingWidth();
64 void InlineFlowBox::addToLine(InlineBox* child)
66 ASSERT(!child->parent());
67 ASSERT(!child->nextOnLine());
68 ASSERT(!child->prevOnLine());
71 child->setParent(this);
76 m_lastChild->setNextOnLine(child);
77 child->setPrevOnLine(m_lastChild);
80 child->setFirstLineStyleBit(m_firstLine);
82 m_hasTextChildren = true;
83 if (child->renderer()->selectionState() != RenderObject::SelectionNone)
84 root()->setHasSelectedChildren(true);
89 void InlineFlowBox::removeChild(InlineBox* child)
96 root()->childRemoved(child);
98 if (child == m_firstChild)
99 m_firstChild = child->nextOnLine();
100 if (child == m_lastChild)
101 m_lastChild = child->prevOnLine();
102 if (child->nextOnLine())
103 child->nextOnLine()->setPrevOnLine(child->prevOnLine());
104 if (child->prevOnLine())
105 child->prevOnLine()->setNextOnLine(child->nextOnLine());
112 void InlineFlowBox::deleteLine(RenderArena* arena)
114 InlineBox* child = firstChild();
117 ASSERT(this == child->parent());
118 next = child->nextOnLine();
122 child->deleteLine(arena);
130 removeLineBoxFromRenderObject();
134 void InlineFlowBox::removeLineBoxFromRenderObject()
136 toRenderInline(renderer())->lineBoxes()->removeLineBox(this);
139 void InlineFlowBox::extractLine()
142 extractLineBoxFromRenderObject();
143 for (InlineBox* child = firstChild(); child; child = child->nextOnLine())
144 child->extractLine();
147 void InlineFlowBox::extractLineBoxFromRenderObject()
149 toRenderInline(renderer())->lineBoxes()->extractLineBox(this);
152 void InlineFlowBox::attachLine()
155 attachLineBoxToRenderObject();
156 for (InlineBox* child = firstChild(); child; child = child->nextOnLine())
160 void InlineFlowBox::attachLineBoxToRenderObject()
162 toRenderInline(renderer())->lineBoxes()->attachLineBox(this);
165 void InlineFlowBox::adjustPosition(int dx, int dy)
167 InlineRunBox::adjustPosition(dx, dy);
168 for (InlineBox* child = firstChild(); child; child = child->nextOnLine())
169 child->adjustPosition(dx, dy);
172 RenderLineBoxList* InlineFlowBox::rendererLineBoxes() const
174 return toRenderInline(renderer())->lineBoxes();
177 bool InlineFlowBox::onEndChain(RenderObject* endObject)
182 if (endObject == renderer())
185 RenderObject* curr = endObject;
186 RenderObject* parent = curr->parent();
187 while (parent && !parent->isRenderBlock()) {
188 if (parent->lastChild() != curr || parent == renderer())
192 parent = curr->parent();
198 void InlineFlowBox::determineSpacingForFlowBoxes(bool lastLine, RenderObject* endObject)
200 // All boxes start off open. They will not apply any margins/border/padding on
202 bool includeLeftEdge = false;
203 bool includeRightEdge = false;
205 // The root inline box never has borders/margins/padding.
207 bool ltr = renderer()->style()->direction() == LTR;
209 // Check to see if all initial lines are unconstructed. If so, then
210 // we know the inline began on this line (unless we are a continuation).
211 RenderLineBoxList* lineBoxList = rendererLineBoxes();
212 if (!lineBoxList->firstLineBox()->isConstructed() && !renderer()->isInlineContinuation()) {
213 if (ltr && lineBoxList->firstLineBox() == this)
214 includeLeftEdge = true;
215 else if (!ltr && lineBoxList->lastLineBox() == this)
216 includeRightEdge = true;
219 // In order to determine if the inline ends on this line, we check three things:
220 // (1) If we are the last line and we don't have a continuation(), then we can
222 // (2) If the last line box for the flow has an object following it on the line (ltr,
223 // reverse for rtl), then the inline has closed.
224 // (3) The line may end on the inline. If we are the last child (climbing up
225 // the end object's chain), then we just closed as well.
226 if (!lineBoxList->lastLineBox()->isConstructed()) {
227 RenderInline* inlineFlow = toRenderInline(renderer());
229 if (!nextLineBox() &&
230 ((lastLine && !inlineFlow->continuation()) || nextOnLineExists() || onEndChain(endObject)))
231 includeRightEdge = true;
233 if ((!prevLineBox() || prevLineBox()->isConstructed()) &&
234 ((lastLine && !inlineFlow->continuation()) || prevOnLineExists() || onEndChain(endObject)))
235 includeLeftEdge = true;
240 setEdges(includeLeftEdge, includeRightEdge);
242 // Recur into our children.
243 for (InlineBox* currChild = firstChild(); currChild; currChild = currChild->nextOnLine()) {
244 if (currChild->isInlineFlowBox()) {
245 InlineFlowBox* currFlow = static_cast<InlineFlowBox*>(currChild);
246 currFlow->determineSpacingForFlowBoxes(lastLine, endObject);
251 int InlineFlowBox::placeBoxesHorizontally(int xPos, int& leftPosition, int& rightPosition, bool& needsWordSpacing)
253 // Set our x position.
256 int boxShadowLeft = 0;
257 int boxShadowRight = 0;
258 for (ShadowData* boxShadow = renderer()->style(m_firstLine)->boxShadow(); boxShadow; boxShadow = boxShadow->next) {
259 boxShadowLeft = min(boxShadow->x - boxShadow->blur, boxShadowLeft);
260 boxShadowRight = max(boxShadow->x + boxShadow->blur, boxShadowRight);
262 leftPosition = min(xPos + boxShadowLeft, leftPosition);
265 xPos += borderLeft() + paddingLeft();
267 for (InlineBox* curr = firstChild(); curr; curr = curr->nextOnLine()) {
268 if (curr->renderer()->isText()) {
269 InlineTextBox* text = static_cast<InlineTextBox*>(curr);
270 RenderText* rt = toRenderText(text->renderer());
271 if (rt->textLength()) {
272 if (needsWordSpacing && isSpaceOrNewline(rt->characters()[text->start()]))
273 xPos += rt->style(m_firstLine)->font().wordSpacing();
274 needsWordSpacing = !isSpaceOrNewline(rt->characters()[text->end()]);
278 int strokeOverflow = static_cast<int>(ceilf(rt->style()->textStrokeWidth() / 2.0f));
280 // If letter-spacing is negative, we should factor that into right overflow. (Even in RTL, letter-spacing is
281 // applied to the right, so this is not an issue with left overflow.
282 int letterSpacing = min(0, (int)rt->style(m_firstLine)->font().letterSpacing());
284 int leftGlyphOverflow = -strokeOverflow;
285 int rightGlyphOverflow = strokeOverflow - letterSpacing;
287 int visualOverflowLeft = leftGlyphOverflow;
288 int visualOverflowRight = rightGlyphOverflow;
289 for (ShadowData* shadow = rt->style()->textShadow(); shadow; shadow = shadow->next) {
290 visualOverflowLeft = min(visualOverflowLeft, shadow->x - shadow->blur + leftGlyphOverflow);
291 visualOverflowRight = max(visualOverflowRight, shadow->x + shadow->blur + rightGlyphOverflow);
294 leftPosition = min(xPos + visualOverflowLeft, leftPosition);
295 rightPosition = max(xPos + text->width() + visualOverflowRight, rightPosition);
296 m_maxHorizontalVisualOverflow = max(max(visualOverflowRight, -visualOverflowLeft), (int)m_maxHorizontalVisualOverflow);
297 xPos += text->width();
299 if (curr->renderer()->isPositioned()) {
300 if (curr->renderer()->parent()->style()->direction() == LTR)
303 // Our offset that we cache needs to be from the edge of the right border box and
304 // not the left border box. We have to subtract |x| from the width of the block
305 // (which can be obtained from the root line box).
306 curr->setX(root()->block()->width() - xPos);
307 continue; // The positioned object has no effect on the width.
309 if (curr->renderer()->isRenderInline()) {
310 InlineFlowBox* flow = static_cast<InlineFlowBox*>(curr);
311 xPos += flow->marginLeft();
312 xPos = flow->placeBoxesHorizontally(xPos, leftPosition, rightPosition, needsWordSpacing);
313 xPos += flow->marginRight();
314 } else if (!curr->renderer()->isListMarker() || static_cast<RenderListMarker*>(curr->renderer())->isInside()) {
315 xPos += curr->boxModelObject()->marginLeft();
317 leftPosition = min(xPos + toRenderBox(curr->renderer())->overflowLeft(false), leftPosition);
318 rightPosition = max(xPos + toRenderBox(curr->renderer())->overflowWidth(false), rightPosition);
319 xPos += curr->width() + curr->boxModelObject()->marginRight();
324 xPos += borderRight() + paddingRight();
325 setWidth(xPos - startX);
326 rightPosition = max(x() + width() + boxShadowRight, rightPosition);
331 int InlineFlowBox::verticallyAlignBoxes(int heightOfBlock)
333 int maxPositionTop = 0;
334 int maxPositionBottom = 0;
338 // Figure out if we're in strict mode. Note that we can't simply use !style()->htmlHacks(),
339 // because that would match almost strict mode as well.
340 RenderObject* curr = renderer();
341 while (curr && !curr->node())
342 curr = curr->container();
343 bool strictMode = (curr && curr->document()->inStrictMode());
345 computeLogicalBoxHeights(maxPositionTop, maxPositionBottom, maxAscent, maxDescent, strictMode);
347 if (maxAscent + maxDescent < max(maxPositionTop, maxPositionBottom))
348 adjustMaxAscentAndDescent(maxAscent, maxDescent, maxPositionTop, maxPositionBottom);
350 int maxHeight = maxAscent + maxDescent;
351 int topPosition = heightOfBlock;
352 int bottomPosition = heightOfBlock;
353 int selectionTop = heightOfBlock;
354 int selectionBottom = heightOfBlock;
355 placeBoxesVertically(heightOfBlock, maxHeight, maxAscent, strictMode, topPosition, bottomPosition, selectionTop, selectionBottom);
357 setVerticalOverflowPositions(topPosition, bottomPosition);
358 setVerticalSelectionPositions(selectionTop, selectionBottom);
360 heightOfBlock += maxHeight;
362 return heightOfBlock;
365 void InlineFlowBox::adjustMaxAscentAndDescent(int& maxAscent, int& maxDescent,
366 int maxPositionTop, int maxPositionBottom)
368 for (InlineBox* curr = firstChild(); curr; curr = curr->nextOnLine()) {
369 // The computed lineheight needs to be extended for the
370 // positioned elements
371 if (curr->renderer()->isPositioned())
372 continue; // Positioned placeholders don't affect calculations.
373 if (curr->y() == PositionTop || curr->y() == PositionBottom) {
374 int lineHeight = curr->lineHeight(false);
375 if (curr->y() == PositionTop) {
376 if (maxAscent + maxDescent < lineHeight)
377 maxDescent = lineHeight - maxAscent;
380 if (maxAscent + maxDescent < lineHeight)
381 maxAscent = lineHeight - maxDescent;
384 if (maxAscent + maxDescent >= max(maxPositionTop, maxPositionBottom))
388 if (curr->isInlineFlowBox())
389 static_cast<InlineFlowBox*>(curr)->adjustMaxAscentAndDescent(maxAscent, maxDescent, maxPositionTop, maxPositionBottom);
393 static int verticalPositionForBox(InlineBox* curr, bool firstLine)
395 if (curr->renderer()->isText())
396 return curr->parent()->y();
397 if (curr->renderer()->isBox())
398 return toRenderBox(curr->renderer())->verticalPosition(firstLine);
399 return toRenderInline(curr->renderer())->verticalPositionFromCache(firstLine);
402 void InlineFlowBox::computeLogicalBoxHeights(int& maxPositionTop, int& maxPositionBottom,
403 int& maxAscent, int& maxDescent, bool strictMode)
405 if (isRootInlineBox()) {
406 // Examine our root box.
407 int height = lineHeight(true);
408 int baseline = baselinePosition(true);
409 if (hasTextChildren() || strictMode) {
410 int ascent = baseline;
411 int descent = height - ascent;
412 if (maxAscent < ascent)
414 if (maxDescent < descent)
415 maxDescent = descent;
419 for (InlineBox* curr = firstChild(); curr; curr = curr->nextOnLine()) {
420 if (curr->renderer()->isPositioned())
421 continue; // Positioned placeholders don't affect calculations.
423 bool isInlineFlow = curr->isInlineFlowBox();
427 Vector<const SimpleFontData*> usedFonts;
428 if (curr->isInlineTextBox())
429 static_cast<InlineTextBox*>(curr)->takeFallbackFonts(usedFonts);
431 if (!usedFonts.isEmpty()) {
432 usedFonts.append(curr->renderer()->style(m_firstLine)->font().primaryFont());
433 Length parentLineHeight = curr->renderer()->parent()->style()->lineHeight();
434 if (parentLineHeight.isNegative()) {
435 int baselineToBottom = 0;
437 for (size_t i = 0; i < usedFonts.size(); ++i) {
438 int halfLeading = (usedFonts[i]->lineSpacing() - usedFonts[i]->ascent() - usedFonts[i]->descent()) / 2;
439 baseline = max(baseline, halfLeading + usedFonts[i]->ascent());
440 baselineToBottom = max(baselineToBottom, usedFonts[i]->lineSpacing() - usedFonts[i]->ascent() - usedFonts[i]->descent() - halfLeading);
442 lineHeight = baseline + baselineToBottom;
443 } else if (parentLineHeight.isPercent()) {
444 lineHeight = parentLineHeight.calcMinValue(curr->renderer()->style()->fontSize());
446 for (size_t i = 0; i < usedFonts.size(); ++i) {
447 int halfLeading = (lineHeight - usedFonts[i]->ascent() - usedFonts[i]->descent()) / 2;
448 baseline = max(baseline, halfLeading + usedFonts[i]->ascent());
451 lineHeight = parentLineHeight.value();
453 for (size_t i = 0; i < usedFonts.size(); ++i) {
454 int halfLeading = (lineHeight - usedFonts[i]->ascent() - usedFonts[i]->descent()) / 2;
455 baseline = max(baseline, halfLeading + usedFonts[i]->ascent());
459 lineHeight = curr->lineHeight(false);
460 baseline = curr->baselinePosition(false);
463 curr->setY(verticalPositionForBox(curr, m_firstLine));
464 if (curr->y() == PositionTop) {
465 if (maxPositionTop < lineHeight)
466 maxPositionTop = lineHeight;
467 } else if (curr->y() == PositionBottom) {
468 if (maxPositionBottom < lineHeight)
469 maxPositionBottom = lineHeight;
470 } else if ((!isInlineFlow || static_cast<InlineFlowBox*>(curr)->hasTextChildren()) || curr->boxModelObject()->hasHorizontalBordersOrPadding() || strictMode) {
471 int ascent = baseline - curr->y();
472 int descent = lineHeight - ascent;
473 if (maxAscent < ascent)
475 if (maxDescent < descent)
476 maxDescent = descent;
479 if (curr->isInlineFlowBox())
480 static_cast<InlineFlowBox*>(curr)->computeLogicalBoxHeights(maxPositionTop, maxPositionBottom, maxAscent, maxDescent, strictMode);
484 void InlineFlowBox::placeBoxesVertically(int yPos, int maxHeight, int maxAscent, bool strictMode,
485 int& topPosition, int& bottomPosition, int& selectionTop, int& selectionBottom)
487 if (isRootInlineBox())
488 setY(yPos + max(0, maxAscent - baselinePosition(true))); // Place our root box.
490 for (InlineBox* curr = firstChild(); curr; curr = curr->nextOnLine()) {
491 if (curr->renderer()->isPositioned())
492 continue; // Positioned placeholders don't affect calculations.
494 // Adjust boxes to use their real box y/height and not the logical height (as dictated by
496 bool isInlineFlow = curr->isInlineFlowBox();
498 static_cast<InlineFlowBox*>(curr)->placeBoxesVertically(yPos, maxHeight, maxAscent, strictMode, topPosition, bottomPosition, selectionTop, selectionBottom);
500 bool childAffectsTopBottomPos = true;
501 if (curr->y() == PositionTop)
503 else if (curr->y() == PositionBottom)
504 curr->setY(yPos + maxHeight - curr->lineHeight(false));
506 if ((isInlineFlow && !static_cast<InlineFlowBox*>(curr)->hasTextChildren()) && !curr->boxModelObject()->hasHorizontalBordersOrPadding() && !strictMode)
507 childAffectsTopBottomPos = false;
508 int posAdjust = maxAscent - curr->baselinePosition(false);
509 if (!childAffectsTopBottomPos)
510 posAdjust = max(0, posAdjust);
511 curr->setY(curr->y() + yPos + posAdjust);
514 // FIXME: By only considering overflow as part of the root line box, we can't get an accurate picture regarding what the line
515 // actually needs to paint. A line box that is part of a self-painting layer technically shouldn't contribute to the overflow
516 // of the line, but in order to not do this and paint accurately, we have to track the overflow somewhere else (either by storing overflow
517 // in each InlineFlowBox up the chain or in the layer itself). Relative positioned objects on a line will cause scrollbars
518 // to appear when they shouldn't until we fix this issue.
519 int newY = curr->y();
521 int overflowBottom = 0;
522 if (curr->isText() || curr->isInlineFlowBox()) {
523 const Font& font = curr->renderer()->style(m_firstLine)->font();
524 newY += curr->baselinePosition(false) - font.ascent();
526 for (ShadowData* shadow = curr->renderer()->style()->textShadow(); shadow; shadow = shadow->next) {
527 overflowTop = min(overflowTop, shadow->y - shadow->blur);
528 overflowBottom = max(overflowBottom, shadow->y + shadow->blur);
531 for (ShadowData* boxShadow = curr->renderer()->style(m_firstLine)->boxShadow(); boxShadow; boxShadow = boxShadow->next) {
532 overflowTop = min(overflowTop, boxShadow->y - boxShadow->blur);
533 overflowBottom = max(overflowBottom, boxShadow->y + boxShadow->blur);
536 for (ShadowData* textShadow = curr->renderer()->style(m_firstLine)->textShadow(); textShadow; textShadow = textShadow->next) {
537 overflowTop = min(overflowTop, textShadow->y - textShadow->blur);
538 overflowBottom = max(overflowBottom, textShadow->y + textShadow->blur);
541 if (curr->renderer()->hasReflection()) {
542 RenderBox* box = toRenderBox(curr->renderer());
543 overflowTop = min(overflowTop, box->reflectionBox().y());
544 overflowBottom = max(overflowBottom, box->reflectionBox().bottom());
547 if (curr->isInlineFlowBox())
548 newY -= curr->boxModelObject()->borderTop() + curr->boxModelObject()->paddingTop();
549 } else if (!curr->renderer()->isBR()) {
550 RenderBox* box = toRenderBox(curr->renderer());
551 newY += box->marginTop();
552 overflowTop = box->overflowTop(false);
553 overflowBottom = box->overflowHeight(false) - box->height();
558 if (childAffectsTopBottomPos) {
559 int boxHeight = curr->height();
560 selectionTop = min(selectionTop, newY);
561 selectionBottom = max(selectionBottom, newY + boxHeight);
562 topPosition = min(topPosition, newY + overflowTop);
563 bottomPosition = max(bottomPosition, newY + boxHeight + overflowBottom);
567 if (isRootInlineBox()) {
568 const Font& font = renderer()->style(m_firstLine)->font();
569 setY(y() + baselinePosition(true) - font.ascent());
570 if (hasTextChildren() || strictMode) {
571 selectionTop = min(selectionTop, y());
572 selectionBottom = max(selectionBottom, y() + height());
577 bool InlineFlowBox::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, int x, int y, int tx, int ty)
579 // Check children first.
580 for (InlineBox* curr = lastChild(); curr; curr = curr->prevOnLine()) {
581 if ((curr->renderer()->isText() || !curr->boxModelObject()->hasSelfPaintingLayer()) && curr->nodeAtPoint(request, result, x, y, tx, ty)) {
582 renderer()->updateHitTestResult(result, IntPoint(x - tx, y - ty));
587 // Now check ourselves.
588 IntRect rect(tx + m_x, ty + m_y, m_width, height());
589 if (visibleToHitTesting() && rect.contains(x, y)) {
590 renderer()->updateHitTestResult(result, IntPoint(x - tx, y - ty)); // Don't add in m_x or m_y here, we want coords in the containing block's space.
597 void InlineFlowBox::paint(RenderObject::PaintInfo& paintInfo, int tx, int ty)
599 int xPos = tx + m_x - renderer()->maximalOutlineSize(paintInfo.phase);
600 int w = width() + 2 * renderer()->maximalOutlineSize(paintInfo.phase);
603 for (ShadowData* boxShadow = renderer()->style(m_firstLine)->boxShadow(); boxShadow; boxShadow = boxShadow->next) {
604 shadowLeft = min(boxShadow->x - boxShadow->blur, shadowLeft);
605 shadowRight = max(boxShadow->x + boxShadow->blur, shadowRight);
607 for (ShadowData* textShadow = renderer()->style(m_firstLine)->textShadow(); textShadow; textShadow = textShadow->next) {
608 shadowLeft = min(textShadow->x - textShadow->blur, shadowLeft);
609 shadowRight = max(textShadow->x + textShadow->blur, shadowRight);
612 w += -shadowLeft + shadowRight;
613 bool intersectsDamageRect = xPos < paintInfo.rect.right() && xPos + w > paintInfo.rect.x();
615 if (intersectsDamageRect && paintInfo.phase != PaintPhaseChildOutlines) {
616 if (paintInfo.phase == PaintPhaseOutline || paintInfo.phase == PaintPhaseSelfOutline) {
617 // Add ourselves to the paint info struct's list of inlines that need to paint their
619 if (renderer()->style()->visibility() == VISIBLE && renderer()->hasOutline() && !isRootInlineBox()) {
620 RenderInline* inlineFlow = toRenderInline(renderer());
621 if ((inlineFlow->continuation() || inlineFlow->isInlineContinuation()) && !boxModelObject()->hasSelfPaintingLayer()) {
622 // Add ourselves to the containing block of the entire continuation so that it can
623 // paint us atomically.
624 RenderBlock* block = renderer()->containingBlock()->containingBlock();
625 block->addContinuationWithOutline(toRenderInline(renderer()->node()->renderer()));
626 } else if (!inlineFlow->isInlineContinuation())
627 paintInfo.outlineObjects->add(inlineFlow);
629 } else if (paintInfo.phase == PaintPhaseMask) {
630 paintMask(paintInfo, tx, ty);
633 // 1. Paint our background, border and box-shadow.
634 paintBoxDecorations(paintInfo, tx, ty);
636 // 2. Paint our underline and overline.
637 paintTextDecorations(paintInfo, tx, ty, false);
641 if (paintInfo.phase == PaintPhaseMask)
644 PaintPhase paintPhase = paintInfo.phase == PaintPhaseChildOutlines ? PaintPhaseOutline : paintInfo.phase;
645 RenderObject::PaintInfo childInfo(paintInfo);
646 childInfo.phase = paintPhase;
647 childInfo.paintingRoot = renderer()->paintingRootForChildren(paintInfo);
649 // 3. Paint our children.
650 if (paintPhase != PaintPhaseSelfOutline) {
651 for (InlineBox* curr = firstChild(); curr; curr = curr->nextOnLine()) {
652 if (curr->renderer()->isText() || !curr->boxModelObject()->hasSelfPaintingLayer())
653 curr->paint(childInfo, tx, ty);
657 // 4. Paint our strike-through
658 if (intersectsDamageRect && (paintInfo.phase == PaintPhaseForeground || paintInfo.phase == PaintPhaseSelection))
659 paintTextDecorations(paintInfo, tx, ty, true);
662 void InlineFlowBox::paintFillLayers(const RenderObject::PaintInfo& paintInfo, const Color& c, const FillLayer* fillLayer, int _tx, int _ty, int w, int h, CompositeOperator op)
666 paintFillLayers(paintInfo, c, fillLayer->next(), _tx, _ty, w, h, op);
667 paintFillLayer(paintInfo, c, fillLayer, _tx, _ty, w, h, op);
670 void InlineFlowBox::paintFillLayer(const RenderObject::PaintInfo& paintInfo, const Color& c, const FillLayer* fillLayer, int tx, int ty, int w, int h, CompositeOperator op)
672 StyleImage* img = fillLayer->image();
673 bool hasFillImage = img && img->canRender(renderer()->style()->effectiveZoom());
674 if ((!hasFillImage && !renderer()->style()->hasBorderRadius()) || (!prevLineBox() && !nextLineBox()) || !parent())
675 boxModelObject()->paintFillLayerExtended(paintInfo, c, fillLayer, tx, ty, w, h, this, op);
677 // We have a fill image that spans multiple lines.
678 // We need to adjust _tx and _ty by the width of all previous lines.
679 // Think of background painting on inlines as though you had one long line, a single continuous
680 // strip. Even though that strip has been broken up across multiple lines, you still paint it
681 // as though you had one single line. This means each line has to pick up the background where
682 // the previous line left off.
683 // FIXME: What the heck do we do with RTL here? The math we're using is obviously not right,
684 // but it isn't even clear how this should work at all.
685 int xOffsetOnLine = 0;
686 for (InlineRunBox* curr = prevLineBox(); curr; curr = curr->prevLineBox())
687 xOffsetOnLine += curr->width();
688 int startX = tx - xOffsetOnLine;
689 int totalWidth = xOffsetOnLine;
690 for (InlineRunBox* curr = this; curr; curr = curr->nextLineBox())
691 totalWidth += curr->width();
692 paintInfo.context->save();
693 paintInfo.context->clip(IntRect(tx, ty, width(), height()));
694 boxModelObject()->paintFillLayerExtended(paintInfo, c, fillLayer, startX, ty, totalWidth, h, this, op);
695 paintInfo.context->restore();
699 void InlineFlowBox::paintBoxShadow(GraphicsContext* context, RenderStyle* s, int tx, int ty, int w, int h)
701 if ((!prevLineBox() && !nextLineBox()) || !parent())
702 boxModelObject()->paintBoxShadow(context, tx, ty, w, h, s);
704 // FIXME: We can do better here in the multi-line case. We want to push a clip so that the shadow doesn't
705 // protrude incorrectly at the edges, and we want to possibly include shadows cast from the previous/following lines
706 boxModelObject()->paintBoxShadow(context, tx, ty, w, h, s, includeLeftEdge(), includeRightEdge());
710 void InlineFlowBox::paintBoxDecorations(RenderObject::PaintInfo& paintInfo, int tx, int ty)
712 if (!renderer()->shouldPaintWithinRoot(paintInfo) || renderer()->style()->visibility() != VISIBLE || paintInfo.phase != PaintPhaseForeground)
715 // Move x/y to our coordinates.
722 GraphicsContext* context = paintInfo.context;
724 // You can use p::first-line to specify a background. If so, the root line boxes for
725 // a line may actually have to paint a background.
726 RenderStyle* styleToUse = renderer()->style(m_firstLine);
727 if ((!parent() && m_firstLine && styleToUse != renderer()->style()) || (parent() && renderer()->hasBoxDecorations())) {
728 // Shadow comes first and is behind the background and border.
729 if (styleToUse->boxShadow())
730 paintBoxShadow(context, styleToUse, tx, ty, w, h);
732 Color c = styleToUse->backgroundColor();
733 paintFillLayers(paintInfo, c, styleToUse->backgroundLayers(), tx, ty, w, h);
735 // :first-line cannot be used to put borders on a line. Always paint borders with our
736 // non-first-line style.
737 if (parent() && renderer()->style()->hasBorder()) {
738 StyleImage* borderImage = renderer()->style()->borderImage().image();
739 bool hasBorderImage = borderImage && borderImage->canRender(styleToUse->effectiveZoom());
740 if (hasBorderImage && !borderImage->isLoaded())
741 return; // Don't paint anything while we wait for the image to load.
743 // The simple case is where we either have no border image or we are the only box for this object. In those
744 // cases only a single call to draw is required.
745 if (!hasBorderImage || (!prevLineBox() && !nextLineBox()))
746 boxModelObject()->paintBorder(context, tx, ty, w, h, renderer()->style(), includeLeftEdge(), includeRightEdge());
748 // We have a border image that spans multiple lines.
749 // We need to adjust _tx and _ty by the width of all previous lines.
750 // Think of border image painting on inlines as though you had one long line, a single continuous
751 // strip. Even though that strip has been broken up across multiple lines, you still paint it
752 // as though you had one single line. This means each line has to pick up the image where
753 // the previous line left off.
754 // FIXME: What the heck do we do with RTL here? The math we're using is obviously not right,
755 // but it isn't even clear how this should work at all.
756 int xOffsetOnLine = 0;
757 for (InlineRunBox* curr = prevLineBox(); curr; curr = curr->prevLineBox())
758 xOffsetOnLine += curr->width();
759 int startX = tx - xOffsetOnLine;
760 int totalWidth = xOffsetOnLine;
761 for (InlineRunBox* curr = this; curr; curr = curr->nextLineBox())
762 totalWidth += curr->width();
764 context->clip(IntRect(tx, ty, w, h));
765 boxModelObject()->paintBorder(context, startX, ty, totalWidth, h, renderer()->style());
772 void InlineFlowBox::paintMask(RenderObject::PaintInfo& paintInfo, int tx, int ty)
774 if (!renderer()->shouldPaintWithinRoot(paintInfo) || renderer()->style()->visibility() != VISIBLE || paintInfo.phase != PaintPhaseMask)
777 // Move x/y to our coordinates.
784 // Figure out if we need to push a transparency layer to render our mask.
785 bool pushTransparencyLayer = false;
786 const NinePieceImage& maskNinePieceImage = renderer()->style()->maskBoxImage();
787 StyleImage* maskBoxImage = renderer()->style()->maskBoxImage().image();
788 if ((maskBoxImage && renderer()->style()->maskLayers()->hasImage()) || renderer()->style()->maskLayers()->next())
789 pushTransparencyLayer = true;
791 CompositeOperator compositeOp = CompositeDestinationIn;
792 if (pushTransparencyLayer) {
793 paintInfo.context->setCompositeOperation(CompositeDestinationIn);
794 paintInfo.context->beginTransparencyLayer(1.0f);
795 compositeOp = CompositeSourceOver;
798 paintFillLayers(paintInfo, Color(), renderer()->style()->maskLayers(), tx, ty, w, h, compositeOp);
800 bool hasBoxImage = maskBoxImage && maskBoxImage->canRender(renderer()->style()->effectiveZoom());
801 if (!hasBoxImage || !maskBoxImage->isLoaded())
802 return; // Don't paint anything while we wait for the image to load.
804 // The simple case is where we are the only box for this object. In those
805 // cases only a single call to draw is required.
806 if (!prevLineBox() && !nextLineBox()) {
807 boxModelObject()->paintNinePieceImage(paintInfo.context, tx, ty, w, h, renderer()->style(), maskNinePieceImage, compositeOp);
809 // We have a mask image that spans multiple lines.
810 // We need to adjust _tx and _ty by the width of all previous lines.
811 int xOffsetOnLine = 0;
812 for (InlineRunBox* curr = prevLineBox(); curr; curr = curr->prevLineBox())
813 xOffsetOnLine += curr->width();
814 int startX = tx - xOffsetOnLine;
815 int totalWidth = xOffsetOnLine;
816 for (InlineRunBox* curr = this; curr; curr = curr->nextLineBox())
817 totalWidth += curr->width();
818 paintInfo.context->save();
819 paintInfo.context->clip(IntRect(tx, ty, w, h));
820 boxModelObject()->paintNinePieceImage(paintInfo.context, startX, ty, totalWidth, h, renderer()->style(), maskNinePieceImage, compositeOp);
821 paintInfo.context->restore();
824 if (pushTransparencyLayer)
825 paintInfo.context->endTransparencyLayer();
828 static bool shouldDrawTextDecoration(RenderObject* obj)
830 for (RenderObject* curr = obj->firstChild(); curr; curr = curr->nextSibling()) {
831 if (curr->isRenderInline())
833 if (curr->isText() && !curr->isBR()) {
834 if (!curr->style()->collapseWhiteSpace())
836 Node* currElement = curr->node();
839 if (!currElement->isTextNode())
841 if (!static_cast<Text*>(currElement)->containsOnlyWhitespace())
848 void InlineFlowBox::paintTextDecorations(RenderObject::PaintInfo& paintInfo, int tx, int ty, bool paintedChildren)
850 // Paint text decorations like underlines/overlines. We only do this if we aren't in quirks mode (i.e., in
851 // almost-strict mode or strict mode).
852 if (renderer()->style()->htmlHacks() || !renderer()->shouldPaintWithinRoot(paintInfo) ||
853 renderer()->style()->visibility() != VISIBLE)
856 // We don't want underlines or other decorations when we're trying to draw nothing but the selection as white text.
857 if (paintInfo.phase == PaintPhaseSelection && paintInfo.forceBlackText)
860 GraphicsContext* context = paintInfo.context;
863 RenderStyle* styleToUse = renderer()->style(m_firstLine);
864 int deco = parent() ? styleToUse->textDecoration() : styleToUse->textDecorationsInEffect();
865 if (deco != TDNONE &&
866 ((!paintedChildren && ((deco & UNDERLINE) || (deco & OVERLINE))) || (paintedChildren && (deco & LINE_THROUGH))) &&
867 shouldDrawTextDecoration(renderer())) {
868 int x = m_x + borderLeft() + paddingLeft();
869 int w = m_width - (borderLeft() + paddingLeft() + borderRight() + paddingRight());
870 RootInlineBox* rootLine = root();
871 if (rootLine->ellipsisBox()) {
872 int ellipsisX = m_x + rootLine->ellipsisBox()->x();
873 int ellipsisWidth = rootLine->ellipsisBox()->width();
874 bool ltr = renderer()->style()->direction() == LTR;
875 if (rootLine == this) {
876 // Trim w and x so that the underline isn't drawn underneath the ellipsis.
877 // ltr: is our right edge farther right than the right edge of the ellipsis.
878 // rtl: is the left edge of our box farther left than the left edge of the ellipsis.
879 bool ltrTruncation = ltr && (x + w >= ellipsisX + ellipsisWidth);
880 bool rtlTruncation = !ltr && (x <= ellipsisX + ellipsisWidth);
882 w -= (x + w) - (ellipsisX + ellipsisWidth);
883 else if (rtlTruncation) {
884 int dx = m_x - ((ellipsisX - m_x) + ellipsisWidth);
889 bool ltrPastEllipsis = ltr && x >= ellipsisX;
890 bool rtlPastEllipsis = !ltr && (x + w) <= (ellipsisX + ellipsisWidth);
891 if (ltrPastEllipsis || rtlPastEllipsis)
894 bool ltrTruncation = ltr && x + w >= ellipsisX;
895 bool rtlTruncation = !ltr && x <= ellipsisX;
897 w -= (x + w - ellipsisX);
898 else if (rtlTruncation) {
899 int dx = m_x - ((ellipsisX - m_x) + ellipsisWidth);
906 // We must have child boxes and have decorations defined.
907 tx += borderLeft() + paddingLeft();
909 Color underline, overline, linethrough;
910 underline = overline = linethrough = styleToUse->color();
912 renderer()->getTextDecorationColors(deco, underline, overline, linethrough);
914 bool isPrinting = renderer()->document()->printing();
915 context->setStrokeThickness(1.0f); // FIXME: We should improve this rule and not always just assume 1.
917 bool paintUnderline = deco & UNDERLINE && !paintedChildren;
918 bool paintOverline = deco & OVERLINE && !paintedChildren;
919 bool paintLineThrough = deco & LINE_THROUGH && paintedChildren;
921 bool linesAreOpaque = !isPrinting && (!paintUnderline || underline.alpha() == 255) && (!paintOverline || overline.alpha() == 255) && (!paintLineThrough || linethrough.alpha() == 255);
923 int baselinePos = renderer()->style(m_firstLine)->font().ascent();
924 if (!isRootInlineBox())
925 baselinePos += borderTop() + paddingTop();
927 bool setClip = false;
929 ShadowData* shadow = styleToUse->textShadow();
930 if (!linesAreOpaque && shadow && shadow->next) {
931 IntRect clipRect(tx, ty, w, baselinePos + 2);
932 for (ShadowData* s = shadow; s; s = s->next) {
933 IntRect shadowRect(tx, ty, w, baselinePos + 2);
934 shadowRect.inflate(s->blur);
935 shadowRect.move(s->x, s->y);
936 clipRect.unite(shadowRect);
937 extraOffset = max(extraOffset, max(0, s->y) + s->blur);
940 context->clip(clipRect);
941 extraOffset += baselinePos + 2;
946 bool setShadow = false;
950 // The last set of lines paints normally inside the clip.
954 context->setShadow(IntSize(shadow->x, shadow->y - extraOffset), shadow->blur, shadow->color);
956 shadow = shadow->next;
959 if (paintUnderline) {
960 context->setStrokeColor(underline);
961 context->setStrokeStyle(SolidStroke);
962 // Leave one pixel of white between the baseline and the underline.
963 context->drawLineForText(IntPoint(tx, ty + baselinePos + 1), w, isPrinting);
966 context->setStrokeColor(overline);
967 context->setStrokeStyle(SolidStroke);
968 context->drawLineForText(IntPoint(tx, ty), w, isPrinting);
970 if (paintLineThrough) {
971 context->setStrokeColor(linethrough);
972 context->setStrokeStyle(SolidStroke);
973 context->drawLineForText(IntPoint(tx, ty + 2 * baselinePos / 3), w, isPrinting);
980 context->clearShadow();
984 InlineBox* InlineFlowBox::firstLeafChild()
986 return firstLeafChildAfterBox();
989 InlineBox* InlineFlowBox::lastLeafChild()
991 return lastLeafChildBeforeBox();
994 InlineBox* InlineFlowBox::firstLeafChildAfterBox(InlineBox* start)
997 for (InlineBox* box = start ? start->nextOnLine() : firstChild(); box && !leaf; box = box->nextOnLine())
998 leaf = box->firstLeafChild();
999 if (start && !leaf && parent())
1000 return parent()->firstLeafChildAfterBox(this);
1004 InlineBox* InlineFlowBox::lastLeafChildBeforeBox(InlineBox* start)
1006 InlineBox* leaf = 0;
1007 for (InlineBox* box = start ? start->prevOnLine() : lastChild(); box && !leaf; box = box->prevOnLine())
1008 leaf = box->lastLeafChild();
1009 if (start && !leaf && parent())
1010 return parent()->lastLeafChildBeforeBox(this);
1014 RenderObject::SelectionState InlineFlowBox::selectionState()
1016 return RenderObject::SelectionNone;
1019 bool InlineFlowBox::canAccommodateEllipsis(bool ltr, int blockEdge, int ellipsisWidth)
1021 for (InlineBox *box = firstChild(); box; box = box->nextOnLine()) {
1022 if (!box->canAccommodateEllipsis(ltr, blockEdge, ellipsisWidth))
1028 int InlineFlowBox::placeEllipsisBox(bool ltr, int blockLeftEdge, int blockRightEdge, int ellipsisWidth, bool& foundBox)
1031 // We iterate over all children, the foundBox variable tells us when we've found the
1032 // box containing the ellipsis. All boxes after that one in the flow are hidden.
1033 // If our flow is ltr then iterate over the boxes from left to right, otherwise iterate
1034 // from right to left. Varying the order allows us to correctly hide the boxes following the ellipsis.
1035 InlineBox *box = ltr ? firstChild() : lastChild();
1037 // NOTE: these will cross after foundBox = true.
1038 int visibleLeftEdge = blockLeftEdge;
1039 int visibleRightEdge = blockRightEdge;
1042 int currResult = box->placeEllipsisBox(ltr, visibleLeftEdge, visibleRightEdge, ellipsisWidth, foundBox);
1043 if (currResult != -1 && result == -1)
1044 result = currResult;
1047 visibleLeftEdge += box->width();
1048 box = box->nextOnLine();
1051 visibleRightEdge -= box->width();
1052 box = box->prevOnLine();
1058 void InlineFlowBox::clearTruncation()
1060 for (InlineBox *box = firstChild(); box; box = box->nextOnLine())
1061 box->clearTruncation();
1066 void InlineFlowBox::checkConsistency() const
1068 #ifdef CHECK_CONSISTENCY
1069 ASSERT(!m_hasBadChildList);
1070 const InlineBox* prev = 0;
1071 for (const InlineBox* child = m_firstChild; child; child = child->nextOnLine()) {
1072 ASSERT(child->parent() == this);
1073 ASSERT(child->prevOnLine() == prev);
1076 ASSERT(prev == m_lastChild);
1082 } // namespace WebCore