2 * This file is part of the html renderer for KDE.
4 * Copyright (C) 2003, 2006 Apple Computer, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
21 // -------------------------------------------------------------------------
24 #include "render_line.h"
26 #include "CachedImage.h"
28 #include "GraphicsContext.h"
29 #include "InlineTextBox.h"
30 #include "RenderBlock.h"
31 #include "RenderTableCell.h"
32 #include "RenderArena.h"
33 #include "RenderInline.h"
34 #include "render_list.h"
42 static bool inInlineBoxDetach;
45 class EllipsisBox : public InlineBox
48 EllipsisBox(RenderObject* obj, const WebCore::AtomicString& ellipsisStr, InlineFlowBox* p,
49 int w, int y, int h, int b, bool firstLine, InlineBox* markupBox)
50 :InlineBox(obj), m_str(ellipsisStr) {
56 m_firstLine = firstLine;
58 m_markupBox = markupBox;
61 virtual void paint(RenderObject::PaintInfo& i, int _tx, int _ty);
62 virtual bool nodeAtPoint(RenderObject::NodeInfo& info, int _x, int _y, int _tx, int _ty);
65 WebCore::AtomicString m_str;
66 InlineBox* m_markupBox;
69 void InlineBox::remove()
72 parent()->removeChild(this);
75 void InlineBox::destroy(RenderArena* renderArena)
78 inInlineBoxDetach = true;
82 inInlineBoxDetach = false;
85 // Recover the size left there for us by operator delete and free the memory.
86 renderArena->free(*(size_t *)this, this);
89 void* InlineBox::operator new(size_t sz, RenderArena* renderArena) throw()
91 return renderArena->allocate(sz);
95 void InlineBox::operator delete(void* ptr, size_t sz)
97 assert(inInlineBoxDetach);
99 // Stash size where destroy can find it.
104 void InlineBox::showTreeForThis() const
107 m_object->showTreeForThis();
111 int InlineBox::caretMinOffset() const
116 int InlineBox::caretMaxOffset() const
121 unsigned InlineBox::caretMaxRenderedOffset() const
126 void InlineBox::dirtyLineBoxes()
129 for (InlineFlowBox* curr = parent(); curr && !curr->isDirty(); curr = curr->parent())
133 void InlineBox::deleteLine(RenderArena* arena)
136 m_object->setInlineBoxWrapper(0);
140 void InlineBox::extractLine()
143 m_object->setInlineBoxWrapper(0);
146 void InlineBox::attachLine()
149 m_object->setInlineBoxWrapper(this);
152 void InlineBox::adjustPosition(int dx, int dy)
156 if (m_object->isReplaced() || m_object->isBR())
157 m_object->setPos(m_object->xPos() + dx, m_object->yPos() + dy);
160 void InlineBox::paint(RenderObject::PaintInfo& i, int tx, int ty)
162 if (!object()->shouldPaintWithinRoot(i) || (i.phase != PaintPhaseForeground && i.phase != PaintPhaseSelection))
165 // Paint all phases of replaced elements atomically, as though the replaced element established its
166 // own stacking context. (See Appendix E.2, section 6.4 on inline block/table elements in the CSS2.1
168 bool paintSelectionOnly = i.phase == PaintPhaseSelection;
169 RenderObject::PaintInfo info(i);
170 info.phase = paintSelectionOnly ? i.phase : PaintPhaseBlockBackground;
171 object()->paint(info, tx, ty);
172 if (!paintSelectionOnly) {
173 info.phase = PaintPhaseChildBlockBackgrounds;
174 object()->paint(info, tx, ty);
175 info.phase = PaintPhaseFloat;
176 object()->paint(info, tx, ty);
177 info.phase = PaintPhaseForeground;
178 object()->paint(info, tx, ty);
179 info.phase = PaintPhaseOutline;
180 object()->paint(info, tx, ty);
184 bool InlineBox::nodeAtPoint(RenderObject::NodeInfo& i, int x, int y, int tx, int ty)
186 // Hit test all phases of replaced elements atomically, as though the replaced element established its
187 // own stacking context. (See Appendix E.2, section 6.4 on inline block/table elements in the CSS2.1
189 return object()->hitTest(i, x, y, tx, ty);
192 RootInlineBox* InlineBox::root()
195 return m_parent->root();
196 return static_cast<RootInlineBox*>(this);
199 bool InlineBox::nextOnLineExists() const
207 return parent()->nextOnLineExists();
210 bool InlineBox::prevOnLineExists() const
218 return parent()->prevOnLineExists();
221 InlineBox* InlineBox::firstLeafChild()
226 InlineBox* InlineBox::lastLeafChild()
231 InlineBox* InlineBox::nextLeafChild()
233 return parent() ? parent()->firstLeafChildAfterBox(this) : 0;
236 InlineBox* InlineBox::prevLeafChild()
238 return parent() ? parent()->lastLeafChildBeforeBox(this) : 0;
241 RenderObject::SelectionState InlineBox::selectionState()
243 return object()->selectionState();
246 bool InlineBox::canAccommodateEllipsis(bool ltr, int blockEdge, int ellipsisWidth)
248 // Non-replaced elements can always accommodate an ellipsis.
249 if (!m_object || !m_object->isReplaced())
252 IntRect boxRect(m_x, 0, m_width, 10);
253 IntRect ellipsisRect(ltr ? blockEdge - ellipsisWidth : blockEdge, 0, ellipsisWidth, 10);
254 return !(boxRect.intersects(ellipsisRect));
257 int InlineBox::placeEllipsisBox(bool ltr, int blockEdge, int ellipsisWidth, bool&)
259 // Use -1 to mean "we didn't set the position."
263 RenderFlow* InlineFlowBox::flowObject()
265 return static_cast<RenderFlow*>(m_object);
268 int InlineFlowBox::marginLeft()
270 if (!includeLeftEdge())
273 Length margin = object()->style()->marginLeft();
276 if (margin.isFixed())
277 return margin.value();
278 return object()->marginLeft();
281 int InlineFlowBox::marginRight()
283 if (!includeRightEdge())
286 Length margin = object()->style()->marginRight();
289 if (margin.isFixed())
290 return margin.value();
291 return object()->marginRight();
294 int InlineFlowBox::marginBorderPaddingLeft()
296 return marginLeft() + borderLeft() + paddingLeft();
299 int InlineFlowBox::marginBorderPaddingRight()
301 return marginRight() + borderRight() + paddingRight();
304 int InlineFlowBox::getFlowSpacingWidth()
306 int totWidth = marginBorderPaddingLeft() + marginBorderPaddingRight();
307 for (InlineBox* curr = firstChild(); curr; curr = curr->nextOnLine()) {
308 if (curr->isInlineFlowBox())
309 totWidth += static_cast<InlineFlowBox*>(curr)->getFlowSpacingWidth();
314 void InlineFlowBox::addToLine(InlineBox* child) {
316 m_firstChild = m_lastChild = child;
318 m_lastChild->m_next = child;
319 child->m_prev = m_lastChild;
322 child->setFirstLineStyleBit(m_firstLine);
323 child->setParent(this);
325 m_hasTextChildren = true;
326 if (child->object()->selectionState() != RenderObject::SelectionNone)
327 root()->setHasSelectedChildren(true);
330 void InlineFlowBox::removeChild(InlineBox* child)
335 root()->childRemoved(child);
337 if (child == m_firstChild)
338 m_firstChild = child->nextOnLine();
339 if (child == m_lastChild)
340 m_lastChild = child->prevOnLine();
341 if (child->nextOnLine())
342 child->nextOnLine()->setPrevOnLine(child->prevOnLine());
343 if (child->prevOnLine())
344 child->prevOnLine()->setNextOnLine(child->nextOnLine());
349 void InlineFlowBox::deleteLine(RenderArena* arena)
351 InlineBox* child = m_firstChild;
354 next = child->nextOnLine();
355 child->deleteLine(arena);
359 static_cast<RenderFlow*>(m_object)->removeLineBox(this);
363 void InlineFlowBox::extractLine()
366 static_cast<RenderFlow*>(m_object)->extractLineBox(this);
367 for (InlineBox* child = m_firstChild; child; child = child->nextOnLine())
368 child->extractLine();
371 void InlineFlowBox::attachLine()
374 static_cast<RenderFlow*>(m_object)->attachLineBox(this);
375 for (InlineBox* child = m_firstChild; child; child = child->nextOnLine())
379 void InlineFlowBox::adjustPosition(int dx, int dy)
381 InlineRunBox::adjustPosition(dx, dy);
382 for (InlineBox* child = m_firstChild; child; child = child->nextOnLine())
383 child->adjustPosition(dx, dy);
386 bool InlineFlowBox::onEndChain(RenderObject* endObject)
391 if (endObject == object())
394 RenderObject* curr = endObject;
395 RenderObject* parent = curr->parent();
396 while (parent && !parent->isRenderBlock()) {
397 if (parent->lastChild() != curr || parent == object())
401 parent = curr->parent();
407 void InlineFlowBox::determineSpacingForFlowBoxes(bool lastLine, RenderObject* endObject)
409 // All boxes start off open. They will not apply any margins/border/padding on
411 bool includeLeftEdge = false;
412 bool includeRightEdge = false;
414 RenderFlow* flow = static_cast<RenderFlow*>(object());
416 if (!flow->firstChild())
417 includeLeftEdge = includeRightEdge = true; // Empty inlines never split across lines.
418 else if (parent()) { // The root inline box never has borders/margins/padding.
419 bool ltr = flow->style()->direction() == LTR;
421 // Check to see if all initial lines are unconstructed. If so, then
422 // we know the inline began on this line.
423 if (!flow->firstLineBox()->isConstructed()) {
424 if (ltr && flow->firstLineBox() == this)
425 includeLeftEdge = true;
426 else if (!ltr && flow->lastLineBox() == this)
427 includeRightEdge = true;
430 // In order to determine if the inline ends on this line, we check three things:
431 // (1) If we are the last line and we don't have a continuation(), then we can
433 // (2) If the last line box for the flow has an object following it on the line (ltr,
434 // reverse for rtl), then the inline has closed.
435 // (3) The line may end on the inline. If we are the last child (climbing up
436 // the end object's chain), then we just closed as well.
437 if (!flow->lastLineBox()->isConstructed()) {
439 if (!nextLineBox() &&
440 ((lastLine && !object()->continuation()) || nextOnLineExists()
441 || onEndChain(endObject)))
442 includeRightEdge = true;
445 if ((!prevLineBox() || !prevLineBox()->isConstructed()) &&
446 ((lastLine && !object()->continuation()) ||
447 prevOnLineExists() || onEndChain(endObject)))
448 includeLeftEdge = true;
453 setEdges(includeLeftEdge, includeRightEdge);
455 // Recur into our children.
456 for (InlineBox* currChild = firstChild(); currChild; currChild = currChild->nextOnLine()) {
457 if (currChild->isInlineFlowBox()) {
458 InlineFlowBox* currFlow = static_cast<InlineFlowBox*>(currChild);
459 currFlow->determineSpacingForFlowBoxes(lastLine, endObject);
464 int InlineFlowBox::placeBoxesHorizontally(int x, int& leftPosition, int& rightPosition, bool & needsWordSpacing)
466 // Set our x position.
468 leftPosition = min(x, leftPosition);
471 x += borderLeft() + paddingLeft();
473 for (InlineBox* curr = firstChild(); curr; curr = curr->nextOnLine()) {
474 if (curr->object()->isText()) {
475 InlineTextBox *text = static_cast<InlineTextBox*>(curr);
476 RenderText *rt = static_cast<RenderText*>(text->object());
478 if (needsWordSpacing && QChar(rt->text()[text->start()]).isSpace())
479 x += rt->font(m_firstLine)->wordSpacing();
480 needsWordSpacing = !QChar(rt->text()[text->end()]).isSpace();
485 for (ShadowData* shadow = rt->style()->textShadow(); shadow; shadow = shadow->next) {
486 shadowLeft = min(shadowLeft, shadow->x - shadow->blur);
487 shadowRight = max(shadowRight, shadow->x + shadow->blur);
489 leftPosition = min(x + shadowLeft, leftPosition);
490 rightPosition = max(x + text->width() + shadowRight, rightPosition);
491 m_maxHorizontalShadow = max(max(shadowRight, -shadowLeft), m_maxHorizontalShadow);
494 if (curr->object()->isPositioned()) {
495 if (curr->object()->parent()->style()->direction() == LTR)
498 // Our offset that we cache needs to be from the edge of the right border box and
499 // not the left border box. We have to subtract |x| from the width of the block
500 // (which can be obtained from the root line box).
501 curr->setXPos(root()->object()->width()-x);
502 continue; // The positioned object has no effect on the width.
504 if (curr->object()->isInlineFlow()) {
505 InlineFlowBox* flow = static_cast<InlineFlowBox*>(curr);
506 if (curr->object()->isCompact()) {
508 flow->placeBoxesHorizontally(ignoredX, leftPosition, rightPosition, needsWordSpacing);
510 x += flow->marginLeft();
511 x = flow->placeBoxesHorizontally(x, leftPosition, rightPosition, needsWordSpacing);
512 x += flow->marginRight();
514 } else if (!curr->object()->isCompact() && (!curr->object()->isListMarker() || static_cast<RenderListMarker*>(curr->object())->isInside())) {
515 x += curr->object()->marginLeft();
517 leftPosition = min(x, leftPosition);
518 rightPosition = max(x + curr->width(), rightPosition);
519 x += curr->width() + curr->object()->marginRight();
524 x += borderRight() + paddingRight();
526 rightPosition = max(xPos() + width(), rightPosition);
531 void InlineFlowBox::verticallyAlignBoxes(int& heightOfBlock)
533 int maxPositionTop = 0;
534 int maxPositionBottom = 0;
538 // Figure out if we're in strict mode. Note that we can't simply use !style()->htmlHacks(),
539 // because that would match almost strict mode as well.
540 RenderObject* curr = object();
541 while (curr && !curr->element())
542 curr = curr->container();
543 bool strictMode = (curr && curr->document()->inStrictMode());
545 computeLogicalBoxHeights(maxPositionTop, maxPositionBottom, maxAscent, maxDescent, strictMode);
547 if (maxAscent + maxDescent < max(maxPositionTop, maxPositionBottom))
548 adjustMaxAscentAndDescent(maxAscent, maxDescent, maxPositionTop, maxPositionBottom);
550 int maxHeight = maxAscent + maxDescent;
551 int topPosition = heightOfBlock;
552 int bottomPosition = heightOfBlock;
553 int selectionTop = heightOfBlock;
554 int selectionBottom = heightOfBlock;
555 placeBoxesVertically(heightOfBlock, maxHeight, maxAscent, strictMode, topPosition, bottomPosition, selectionTop, selectionBottom);
557 setVerticalOverflowPositions(topPosition, bottomPosition);
558 setVerticalSelectionPositions(selectionTop, selectionBottom);
560 // Shrink boxes with no text children in quirks and almost strict mode.
562 shrinkBoxesWithNoTextChildren(topPosition, bottomPosition);
564 heightOfBlock += maxHeight;
567 void InlineFlowBox::adjustMaxAscentAndDescent(int& maxAscent, int& maxDescent,
568 int maxPositionTop, int maxPositionBottom)
570 for (InlineBox* curr = firstChild(); curr; curr = curr->nextOnLine()) {
571 // The computed lineheight needs to be extended for the
572 // positioned elements
573 if (curr->object()->isPositioned())
574 continue; // Positioned placeholders don't affect calculations.
575 if (curr->yPos() == PositionTop || curr->yPos() == PositionBottom) {
576 if (curr->yPos() == PositionTop) {
577 if (maxAscent + maxDescent < curr->height())
578 maxDescent = curr->height() - maxAscent;
581 if (maxAscent + maxDescent < curr->height())
582 maxAscent = curr->height() - maxDescent;
585 if (maxAscent + maxDescent >= max(maxPositionTop, maxPositionBottom))
589 if (curr->isInlineFlowBox())
590 static_cast<InlineFlowBox*>(curr)->adjustMaxAscentAndDescent(maxAscent, maxDescent, maxPositionTop, maxPositionBottom);
594 void InlineFlowBox::computeLogicalBoxHeights(int& maxPositionTop, int& maxPositionBottom,
595 int& maxAscent, int& maxDescent, bool strictMode)
597 if (isRootInlineBox()) {
598 // Examine our root box.
599 setHeight(object()->lineHeight(m_firstLine, true));
600 bool isTableCell = object()->isTableCell();
602 RenderTableCell* tableCell = static_cast<RenderTableCell*>(object());
603 setBaseline(tableCell->RenderBlock::baselinePosition(m_firstLine, true));
606 setBaseline(object()->baselinePosition(m_firstLine, true));
607 if (hasTextChildren() || strictMode) {
608 int ascent = baseline();
609 int descent = height() - ascent;
610 if (maxAscent < ascent)
612 if (maxDescent < descent)
613 maxDescent = descent;
617 for (InlineBox* curr = firstChild(); curr; curr = curr->nextOnLine()) {
618 if (curr->object()->isPositioned())
619 continue; // Positioned placeholders don't affect calculations.
621 curr->setHeight(curr->object()->lineHeight(m_firstLine));
622 curr->setBaseline(curr->object()->baselinePosition(m_firstLine));
623 curr->setYPos(curr->object()->verticalPositionHint(m_firstLine));
624 if (curr->yPos() == PositionTop) {
625 if (maxPositionTop < curr->height())
626 maxPositionTop = curr->height();
628 else if (curr->yPos() == PositionBottom) {
629 if (maxPositionBottom < curr->height())
630 maxPositionBottom = curr->height();
632 else if (curr->hasTextChildren() || strictMode) {
633 int ascent = curr->baseline() - curr->yPos();
634 int descent = curr->height() - ascent;
635 if (maxAscent < ascent)
637 if (maxDescent < descent)
638 maxDescent = descent;
641 if (curr->isInlineFlowBox())
642 static_cast<InlineFlowBox*>(curr)->computeLogicalBoxHeights(maxPositionTop, maxPositionBottom, maxAscent, maxDescent, strictMode);
646 void InlineFlowBox::placeBoxesVertically(int y, int maxHeight, int maxAscent, bool strictMode,
647 int& topPosition, int& bottomPosition, int& selectionTop, int& selectionBottom)
649 if (isRootInlineBox())
650 setYPos(y + maxAscent - baseline());// Place our root box.
652 for (InlineBox* curr = firstChild(); curr; curr = curr->nextOnLine()) {
653 if (curr->object()->isPositioned())
654 continue; // Positioned placeholders don't affect calculations.
656 // Adjust boxes to use their real box y/height and not the logical height (as dictated by
658 if (curr->isInlineFlowBox())
659 static_cast<InlineFlowBox*>(curr)->placeBoxesVertically(y, maxHeight, maxAscent, strictMode, topPosition, bottomPosition, selectionTop, selectionBottom);
661 bool childAffectsTopBottomPos = true;
662 if (curr->yPos() == PositionTop)
664 else if (curr->yPos() == PositionBottom)
665 curr->setYPos(y + maxHeight - curr->height());
667 if (!curr->hasTextChildren() && !strictMode)
668 childAffectsTopBottomPos = false;
669 curr->setYPos(curr->yPos() + y + maxAscent - curr->baseline());
672 int newY = curr->yPos();
673 int newHeight = curr->height();
674 int newBaseline = curr->baseline();
676 int overflowBottom = 0;
677 if (curr->isText() || curr->isInlineFlowBox()) {
678 const Font& font = curr->object()->font(m_firstLine);
679 newBaseline = font.ascent();
680 newY += curr->baseline() - newBaseline;
681 newHeight = newBaseline + font.descent();
682 for (ShadowData* shadow = curr->object()->style()->textShadow(); shadow; shadow = shadow->next) {
683 overflowTop = min(overflowTop, shadow->y - shadow->blur);
684 overflowBottom = max(overflowBottom, shadow->y + shadow->blur);
686 if (curr->isInlineFlowBox()) {
687 newHeight += curr->object()->borderTop() + curr->object()->paddingTop() +
688 curr->object()->borderBottom() + curr->object()->paddingBottom();
689 newY -= curr->object()->borderTop() + curr->object()->paddingTop();
690 newBaseline += curr->object()->borderTop() + curr->object()->paddingTop();
693 else if (!curr->object()->isBR()) {
694 newY += curr->object()->marginTop();
695 newHeight = curr->height() - (curr->object()->marginTop() + curr->object()->marginBottom());
696 overflowTop = curr->object()->overflowTop(false);
697 overflowBottom = curr->object()->overflowHeight(false) - newHeight;
701 curr->setHeight(newHeight);
702 curr->setBaseline(newBaseline);
704 if (childAffectsTopBottomPos) {
705 selectionTop = min(selectionTop, newY);
706 selectionBottom = max(selectionBottom, newY + newHeight);
707 topPosition = min(topPosition, newY + overflowTop);
708 bottomPosition = max(bottomPosition, newY + newHeight + overflowBottom);
712 if (isRootInlineBox()) {
713 const Font& font = object()->font(m_firstLine);
714 setHeight(font.ascent() + font.descent());
715 setYPos(yPos() + baseline() - font.ascent());
716 setBaseline(font.ascent());
717 if (hasTextChildren() || strictMode) {
718 selectionTop = min(selectionTop, yPos());
719 selectionBottom = max(selectionBottom, yPos() + height());
724 void InlineFlowBox::shrinkBoxesWithNoTextChildren(int topPos, int bottomPos)
726 // First shrink our kids.
727 for (InlineBox* curr = firstChild(); curr; curr = curr->nextOnLine()) {
728 if (curr->object()->isPositioned())
729 continue; // Positioned placeholders don't affect calculations.
731 if (curr->isInlineFlowBox())
732 static_cast<InlineFlowBox*>(curr)->shrinkBoxesWithNoTextChildren(topPos, bottomPos);
735 // See if we have text children. If not, then we need to shrink ourselves to fit on the line.
736 if (!hasTextChildren()) {
739 if (yPos() + height() > bottomPos)
740 setHeight(bottomPos - yPos());
741 if (baseline() > height())
742 setBaseline(height());
746 bool InlineFlowBox::nodeAtPoint(RenderObject::NodeInfo& i, int x, int y, int tx, int ty)
748 // Check children first.
749 for (InlineBox* curr = lastChild(); curr; curr = curr->prevOnLine()) {
750 if (!curr->object()->layer() && curr->nodeAtPoint(i, x, y, tx, ty)) {
751 object()->setInnerNode(i);
756 // Now check ourselves.
757 IntRect rect(tx + m_x, ty + m_y, m_width, m_height);
758 if (object()->style()->visibility() == VISIBLE && rect.contains(x, y)) {
759 object()->setInnerNode(i);
766 void InlineFlowBox::paint(RenderObject::PaintInfo& i, int tx, int ty)
768 int xPos = tx + m_x - object()->maximalOutlineSize(i.phase);
769 int w = width() + 2 * object()->maximalOutlineSize(i.phase);
770 bool intersectsDamageRect = xPos < i.r.right() && xPos + w > i.r.x();
772 if (intersectsDamageRect && i.phase != PaintPhaseChildOutlines) {
773 if (i.phase == PaintPhaseOutline || i.phase == PaintPhaseSelfOutline) {
774 // Add ourselves to the paint info struct's list of inlines that need to paint their
776 if (object()->style()->visibility() == VISIBLE && object()->style()->outlineWidth() > 0 &&
777 !object()->isInlineContinuation() && !isRootInlineBox()) {
778 i.outlineObjects->add(flowObject());
782 // 1. Paint our background and border.
783 paintBackgroundAndBorder(i, tx, ty);
785 // 2. Paint our underline and overline.
786 paintDecorations(i, tx, ty, false);
790 PaintPhase paintPhase = i.phase == PaintPhaseChildOutlines ? PaintPhaseOutline : i.phase;
791 RenderObject::PaintInfo childInfo(i);
792 childInfo.phase = paintPhase;
794 // 3. Paint our children.
795 if (paintPhase != PaintPhaseSelfOutline) {
796 for (InlineBox* curr = firstChild(); curr; curr = curr->nextOnLine()) {
797 if (!curr->object()->layer())
798 curr->paint(childInfo, tx, ty);
802 // 4. Paint our strike-through
803 if (intersectsDamageRect && (i.phase == PaintPhaseForeground || i.phase == PaintPhaseSelection))
804 paintDecorations(i, tx, ty, true);
807 void InlineFlowBox::paintBackgrounds(GraphicsContext* p, const Color& c, const BackgroundLayer* bgLayer,
808 int my, int mh, int _tx, int _ty, int w, int h)
812 paintBackgrounds(p, c, bgLayer->next(), my, mh, _tx, _ty, w, h);
813 paintBackground(p, c, bgLayer, my, mh, _tx, _ty, w, h);
816 void InlineFlowBox::paintBackground(GraphicsContext* p, const Color& c, const BackgroundLayer* bgLayer,
817 int my, int mh, int _tx, int _ty, int w, int h)
819 CachedImage* bg = bgLayer->backgroundImage();
820 bool hasBackgroundImage = bg && bg->canRender();
821 if (!hasBackgroundImage || (!prevLineBox() && !nextLineBox()) || !parent())
822 object()->paintBackgroundExtended(p, c, bgLayer, my, mh, _tx, _ty, w, h,
823 borderLeft(), borderRight(), paddingLeft(), paddingRight());
825 // We have a background image that spans multiple lines.
826 // We need to adjust _tx and _ty by the width of all previous lines.
827 // Think of background painting on inlines as though you had one long line, a single continuous
828 // strip. Even though that strip has been broken up across multiple lines, you still paint it
829 // as though you had one single line. This means each line has to pick up the background where
830 // the previous line left off.
831 // FIXME: What the heck do we do with RTL here? The math we're using is obviously not right,
832 // but it isn't even clear how this should work at all.
833 int xOffsetOnLine = 0;
834 for (InlineRunBox* curr = prevLineBox(); curr; curr = curr->prevLineBox())
835 xOffsetOnLine += curr->width();
836 int startX = _tx - xOffsetOnLine;
837 int totalWidth = xOffsetOnLine;
838 for (InlineRunBox* curr = this; curr; curr = curr->nextLineBox())
839 totalWidth += curr->width();
841 p->addClip(IntRect(_tx, _ty, width(), height()));
842 object()->paintBackgroundExtended(p, c, bgLayer, my, mh, startX, _ty,
843 totalWidth, h, borderLeft(), borderRight(), paddingLeft(), paddingRight());
848 void InlineFlowBox::paintBackgroundAndBorder(RenderObject::PaintInfo& i, int _tx, int _ty)
850 if (!object()->shouldPaintWithinRoot(i) || object()->style()->visibility() != VISIBLE ||
851 i.phase != PaintPhaseForeground)
854 // Move x/y to our coordinates.
861 int my = max(_ty, i.r.y());
864 mh = max(0, h - (i.r.y() - _ty));
866 mh = min(i.r.height(), h);
868 GraphicsContext* p = i.p;
870 // You can use p::first-line to specify a background. If so, the root line boxes for
871 // a line may actually have to paint a background.
872 RenderStyle* styleToUse = object()->style(m_firstLine);
873 if ((!parent() && m_firstLine && styleToUse != object()->style()) ||
874 (parent() && object()->shouldPaintBackgroundOrBorder())) {
875 Color c = styleToUse->backgroundColor();
876 paintBackgrounds(p, c, styleToUse->backgroundLayers(), my, mh, _tx, _ty, w, h);
878 // :first-line cannot be used to put borders on a line. Always paint borders with our
879 // non-first-line style.
880 if (parent() && object()->style()->hasBorder()) {
881 CachedImage* borderImage = object()->style()->borderImage().image();
882 bool hasBorderImage = borderImage && borderImage->canRender();
883 if (hasBorderImage && !borderImage->isLoaded())
884 return; // Don't paint anything while we wait for the image to load.
886 // The simple case is where we either have no border image or we are the only box for this object. In those
887 // cases only a single call to draw is required.
888 if (!hasBorderImage || (!prevLineBox() && !nextLineBox()))
889 object()->paintBorder(p, _tx, _ty, w, h, object()->style(), includeLeftEdge(), includeRightEdge());
891 // We have a border image that spans multiple lines.
892 // We need to adjust _tx and _ty by the width of all previous lines.
893 // Think of border image painting on inlines as though you had one long line, a single continuous
894 // strip. Even though that strip has been broken up across multiple lines, you still paint it
895 // as though you had one single line. This means each line has to pick up the image where
896 // the previous line left off.
897 // FIXME: What the heck do we do with RTL here? The math we're using is obviously not right,
898 // but it isn't even clear how this should work at all.
899 int xOffsetOnLine = 0;
900 for (InlineRunBox* curr = prevLineBox(); curr; curr = curr->prevLineBox())
901 xOffsetOnLine += curr->width();
902 int startX = _tx - xOffsetOnLine;
903 int totalWidth = xOffsetOnLine;
904 for (InlineRunBox* curr = this; curr; curr = curr->nextLineBox())
905 totalWidth += curr->width();
907 p->addClip(IntRect(_tx, _ty, width(), height()));
908 object()->paintBorder(p, startX, _ty, totalWidth, h, object()->style());
915 static bool shouldDrawDecoration(RenderObject* obj)
917 for (RenderObject* curr = obj->firstChild(); curr; curr = curr->nextSibling()) {
918 if (curr->isInlineFlow())
920 if (curr->isText() && !curr->isBR()) {
921 if (!curr->style()->collapseWhiteSpace())
923 Node* currElement = curr->element();
926 if (!currElement->isTextNode())
928 if (!static_cast<Text*>(currElement)->containsOnlyWhitespace())
935 void InlineFlowBox::paintDecorations(RenderObject::PaintInfo& i, int _tx, int _ty, bool paintedChildren)
937 // Paint text decorations like underlines/overlines. We only do this if we aren't in quirks mode (i.e., in
938 // almost-strict mode or strict mode).
939 if (object()->style()->htmlHacks() || !object()->shouldPaintWithinRoot(i) ||
940 object()->style()->visibility() != VISIBLE)
943 GraphicsContext* p = i.p;
946 RenderStyle* styleToUse = object()->style(m_firstLine);
947 int deco = parent() ? styleToUse->textDecoration() : styleToUse->textDecorationsInEffect();
948 if (deco != TDNONE &&
949 ((!paintedChildren && ((deco & UNDERLINE) || (deco & OVERLINE))) || (paintedChildren && (deco & LINE_THROUGH))) &&
950 shouldDrawDecoration(object())) {
951 int x = m_x + borderLeft() + paddingLeft();
952 int w = m_width - (borderLeft() + paddingLeft() + borderRight() + paddingRight());
953 RootInlineBox* rootLine = root();
954 if (rootLine->ellipsisBox()) {
955 int ellipsisX = rootLine->ellipsisBox()->xPos();
956 int ellipsisWidth = rootLine->ellipsisBox()->width();
958 // FIXME: Will need to work with RTL
959 if (rootLine == this) {
960 if (x + w >= ellipsisX + ellipsisWidth)
961 w -= (x + w - ellipsisX - ellipsisWidth);
966 if (x + w >= ellipsisX)
967 w -= (x + w - ellipsisX);
971 // Set up the appropriate text-shadow effect for the decoration.
972 // FIXME: Support multiple shadow effects. Need more from the CG API before we can do this.
973 bool setShadow = false;
974 if (styleToUse->textShadow()) {
975 p->setShadow(IntSize(styleToUse->textShadow()->x, styleToUse->textShadow()->y),
976 styleToUse->textShadow()->blur, styleToUse->textShadow()->color);
980 // We must have child boxes and have decorations defined.
981 _tx += borderLeft() + paddingLeft();
983 Color underline, overline, linethrough;
984 underline = overline = linethrough = styleToUse->color();
986 object()->getTextDecorationColors(deco, underline, overline, linethrough);
988 if (styleToUse->font() != p->font())
989 p->setFont(styleToUse->font());
991 bool isPrinting = object()->document()->printing();
992 if (deco & UNDERLINE && !paintedChildren) {
993 p->setPen(underline);
994 p->drawLineForText(IntPoint(_tx, _ty), m_baseline, w, isPrinting);
996 if (deco & OVERLINE && !paintedChildren) {
998 p->drawLineForText(IntPoint(_tx, _ty), 0, w, isPrinting);
1000 if (deco & LINE_THROUGH && paintedChildren) {
1001 p->setPen(linethrough);
1002 p->drawLineForText(IntPoint(_tx, _ty), 2*m_baseline/3, w, isPrinting);
1010 InlineBox* InlineFlowBox::firstLeafChild()
1012 return firstLeafChildAfterBox();
1015 InlineBox* InlineFlowBox::lastLeafChild()
1017 return lastLeafChildBeforeBox();
1020 InlineBox* InlineFlowBox::firstLeafChildAfterBox(InlineBox* start)
1022 InlineBox* leaf = 0;
1023 for (InlineBox* box = start ? start->nextOnLine() : firstChild(); box && !leaf; box = box->nextOnLine())
1024 leaf = box->firstLeafChild();
1025 if (start && !leaf && parent())
1026 return parent()->firstLeafChildAfterBox(this);
1030 InlineBox* InlineFlowBox::lastLeafChildBeforeBox(InlineBox* start)
1032 InlineBox* leaf = 0;
1033 for (InlineBox* box = start ? start->prevOnLine() : lastChild(); box && !leaf; box = box->prevOnLine())
1034 leaf = box->lastLeafChild();
1035 if (start && !leaf && parent())
1036 return parent()->lastLeafChildBeforeBox(this);
1040 RenderObject::SelectionState InlineFlowBox::selectionState()
1042 return RenderObject::SelectionNone;
1045 bool InlineFlowBox::canAccommodateEllipsis(bool ltr, int blockEdge, int ellipsisWidth)
1047 for (InlineBox *box = firstChild(); box; box = box->nextOnLine()) {
1048 if (!box->canAccommodateEllipsis(ltr, blockEdge, ellipsisWidth))
1054 int InlineFlowBox::placeEllipsisBox(bool ltr, int blockEdge, int ellipsisWidth, bool& foundBox)
1057 for (InlineBox *box = firstChild(); box; box = box->nextOnLine()) {
1058 int currResult = box->placeEllipsisBox(ltr, blockEdge, ellipsisWidth, foundBox);
1059 if (currResult != -1 && result == -1)
1060 result = currResult;
1065 void InlineFlowBox::clearTruncation()
1067 for (InlineBox *box = firstChild(); box; box = box->nextOnLine())
1068 box->clearTruncation();
1071 void EllipsisBox::paint(RenderObject::PaintInfo& i, int _tx, int _ty)
1073 GraphicsContext* p = i.p;
1074 RenderStyle* _style = m_firstLine ? m_object->firstLineStyle() : m_object->style();
1075 if (_style->font() != p->font())
1076 p->setFont(_style->font());
1078 Color textColor = _style->color();
1079 if (textColor != p->pen().color())
1080 p->setPen(textColor);
1081 bool setShadow = false;
1082 if (_style->textShadow()) {
1083 p->setShadow(IntSize(_style->textShadow()->x, _style->textShadow()->y),
1084 _style->textShadow()->blur, _style->textShadow()->color);
1088 const String& str = m_str;
1089 p->drawText(TextRun(str.impl()), IntPoint(m_x + _tx, m_y + _ty + m_baseline), TextStyle(0, 0, 0, false, _style->visuallyOrdered()));
1095 // Paint the markup box
1096 _tx += m_x + m_width - m_markupBox->xPos();
1097 _ty += m_y + m_baseline - (m_markupBox->yPos() + m_markupBox->baseline());
1098 m_markupBox->paint(i, _tx, _ty);
1102 bool EllipsisBox::nodeAtPoint(RenderObject::NodeInfo& info, int x, int y, int tx, int ty)
1107 // Hit test the markup box.
1109 int mtx = tx + m_width - m_markupBox->xPos();
1110 int mty = ty + m_baseline - (m_markupBox->yPos() + m_markupBox->baseline());
1111 if (m_markupBox->nodeAtPoint(info, x, y, mtx, mty)) {
1112 object()->setInnerNode(info);
1117 if (object()->style()->visibility() == VISIBLE && IntRect(tx, ty, m_width, m_height).contains(x, y)) {
1118 object()->setInnerNode(info);
1125 void RootInlineBox::destroy(RenderArena* arena)
1127 detachEllipsisBox(arena);
1128 InlineFlowBox::destroy(arena);
1131 void RootInlineBox::detachEllipsisBox(RenderArena* arena)
1133 if (m_ellipsisBox) {
1134 m_ellipsisBox->destroy(arena);
1139 void RootInlineBox::clearTruncation()
1141 if (m_ellipsisBox) {
1142 detachEllipsisBox(m_object->renderArena());
1143 InlineFlowBox::clearTruncation();
1147 bool RootInlineBox::canAccommodateEllipsis(bool ltr, int blockEdge, int lineBoxEdge, int ellipsisWidth)
1149 // First sanity-check the unoverflowed width of the whole line to see if there is sufficient room.
1150 int delta = ltr ? lineBoxEdge - blockEdge : blockEdge - lineBoxEdge;
1151 if (width() - delta < ellipsisWidth)
1154 // Next iterate over all the line boxes on the line. If we find a replaced element that intersects
1155 // then we refuse to accommodate the ellipsis. Otherwise we're ok.
1156 return InlineFlowBox::canAccommodateEllipsis(ltr, blockEdge, ellipsisWidth);
1159 void RootInlineBox::placeEllipsis(const AtomicString& ellipsisStr, bool ltr, int blockEdge, int ellipsisWidth,
1160 InlineBox* markupBox)
1162 // Create an ellipsis box.
1163 m_ellipsisBox = new (m_object->renderArena()) EllipsisBox(m_object, ellipsisStr, this,
1164 ellipsisWidth - (markupBox ? markupBox->width() : 0),
1165 yPos(), height(), baseline(), !prevRootBox(),
1168 if (ltr && (xPos() + width() + ellipsisWidth) <= blockEdge) {
1169 m_ellipsisBox->m_x = xPos() + width();
1173 // Now attempt to find the nearest glyph horizontally and place just to the right (or left in RTL)
1174 // of that glyph. Mark all of the objects that intersect the ellipsis box as not painting (as being
1176 bool foundBox = false;
1177 m_ellipsisBox->m_x = placeEllipsisBox(ltr, blockEdge, ellipsisWidth, foundBox);
1180 int RootInlineBox::placeEllipsisBox(bool ltr, int blockEdge, int ellipsisWidth, bool& foundBox)
1182 int result = InlineFlowBox::placeEllipsisBox(ltr, blockEdge, ellipsisWidth, foundBox);
1184 result = ltr ? blockEdge - ellipsisWidth : blockEdge;
1188 void RootInlineBox::paintEllipsisBox(RenderObject::PaintInfo& i, int _tx, int _ty) const
1190 if (m_ellipsisBox && object()->shouldPaintWithinRoot(i) && object()->style()->visibility() == VISIBLE &&
1191 i.phase == PaintPhaseForeground)
1192 m_ellipsisBox->paint(i, _tx, _ty);
1195 void RootInlineBox::paint(RenderObject::PaintInfo& i, int tx, int ty)
1197 InlineFlowBox::paint(i, tx, ty);
1198 paintEllipsisBox(i, tx, ty);
1201 bool RootInlineBox::nodeAtPoint(RenderObject::NodeInfo& i, int x, int y, int tx, int ty)
1203 if (m_ellipsisBox && object()->style()->visibility() == VISIBLE) {
1204 if (m_ellipsisBox->nodeAtPoint(i, x, y, tx, ty)) {
1205 object()->setInnerNode(i);
1209 return InlineFlowBox::nodeAtPoint(i, x, y, tx, ty);
1212 void RootInlineBox::adjustPosition(int dx, int dy)
1214 InlineFlowBox::adjustPosition(dx, dy);
1215 m_topOverflow += dy;
1216 m_bottomOverflow += dy;
1217 m_blockHeight += dy;
1218 m_selectionTop += dy;
1219 m_selectionBottom += dy;
1222 void RootInlineBox::childRemoved(InlineBox* box)
1224 if (box->object() == m_lineBreakObj)
1225 setLineBreakInfo(0, 0, 0, 0);
1227 RootInlineBox* prev = prevRootBox();
1228 if (prev && prev->lineBreakObj() == box->object()) {
1229 prev->setLineBreakInfo(0, 0, 0, 0);
1234 GapRects RootInlineBox::fillLineSelectionGap(int selTop, int selHeight, RenderBlock* rootBlock, int blockX, int blockY, int tx, int ty,
1235 const RenderObject::PaintInfo* i)
1238 RenderObject::SelectionState lineState = selectionState();
1240 bool leftGap, rightGap;
1241 block()->getHorizontalSelectionGapInfo(lineState, leftGap, rightGap);
1243 InlineBox* firstBox = firstSelectedBox();
1244 InlineBox* lastBox = lastSelectedBox();
1246 result.uniteLeft(block()->fillLeftSelectionGap(firstBox->parent()->object(),
1247 firstBox->xPos(), selTop, selHeight,
1248 rootBlock, blockX, blockY, tx, ty, i));
1250 result.uniteRight(block()->fillRightSelectionGap(lastBox->parent()->object(),
1251 lastBox->xPos() + lastBox->width(), selTop, selHeight,
1252 rootBlock, blockX, blockY, tx, ty, i));
1254 if (firstBox && firstBox != lastBox) {
1255 // Now fill in any gaps on the line that occurred between two selected elements.
1256 int lastX = firstBox->xPos() + firstBox->width();
1257 for (InlineBox* box = firstBox->nextLeafChild(); box; box = box->nextLeafChild()) {
1258 if (box->selectionState() != RenderObject::SelectionNone) {
1259 result.uniteCenter(block()->fillHorizontalSelectionGap(box->parent()->object(),
1260 lastX + tx, selTop + ty,
1261 box->xPos() - lastX, selHeight, i));
1262 lastX = box->xPos() + box->width();
1272 void RootInlineBox::setHasSelectedChildren(bool b)
1274 if (m_hasSelectedChildren == b)
1276 m_hasSelectedChildren = b;
1279 RenderObject::SelectionState RootInlineBox::selectionState()
1281 // Walk over all of the selected boxes.
1282 RenderObject::SelectionState state = RenderObject::SelectionNone;
1283 for (InlineBox* box = firstLeafChild(); box; box = box->nextLeafChild()) {
1284 RenderObject::SelectionState boxState = box->selectionState();
1285 if ((boxState == RenderObject::SelectionStart && state == RenderObject::SelectionEnd) ||
1286 (boxState == RenderObject::SelectionEnd && state == RenderObject::SelectionStart))
1287 state = RenderObject::SelectionBoth;
1288 else if (state == RenderObject::SelectionNone ||
1289 ((boxState == RenderObject::SelectionStart || boxState == RenderObject::SelectionEnd) &&
1290 (state == RenderObject::SelectionNone || state == RenderObject::SelectionInside)))
1292 if (state == RenderObject::SelectionBoth)
1299 InlineBox* RootInlineBox::firstSelectedBox()
1301 for (InlineBox* box = firstLeafChild(); box; box = box->nextLeafChild())
1302 if (box->selectionState() != RenderObject::SelectionNone)
1307 InlineBox* RootInlineBox::lastSelectedBox()
1309 for (InlineBox* box = lastLeafChild(); box; box = box->prevLeafChild())
1310 if (box->selectionState() != RenderObject::SelectionNone)
1315 int RootInlineBox::selectionTop()
1318 return m_selectionTop;
1320 int prevBottom = prevRootBox()->selectionBottom();
1321 if (prevBottom < m_selectionTop && block()->containsFloats()) {
1322 // This line has actually been moved further down, probably from a large line-height, but possibly because the
1323 // line was forced to clear floats. If so, let's check the offsets, and only be willing to use the previous
1324 // line's bottom overflow if the offsets are greater on both sides.
1325 int prevLeft = block()->leftOffset(prevBottom);
1326 int prevRight = block()->rightOffset(prevBottom);
1327 int newLeft = block()->leftOffset(m_selectionTop);
1328 int newRight = block()->rightOffset(m_selectionTop);
1329 if (prevLeft > newLeft || prevRight < newRight)
1330 return m_selectionTop;
1336 RenderBlock* RootInlineBox::block() const
1338 return static_cast<RenderBlock*>(m_object);
1341 InlineBox* RootInlineBox::closestLeafChildForXPos(int _x, int _tx)
1343 InlineBox *firstLeaf = firstLeafChildAfterBox();
1344 InlineBox *lastLeaf = lastLeafChildBeforeBox();
1345 if (firstLeaf == lastLeaf)
1348 // Avoid returning a list marker when possible.
1349 if (_x <= _tx + firstLeaf->m_x && !firstLeaf->object()->isListMarker())
1350 // The x coordinate is less or equal to left edge of the firstLeaf.
1354 if (_x >= _tx + lastLeaf->m_x + lastLeaf->m_width && !lastLeaf->object()->isListMarker())
1355 // The x coordinate is greater or equal to right edge of the lastLeaf.
1359 for (InlineBox *leaf = firstLeaf; leaf && leaf != lastLeaf; leaf = leaf->nextLeafChild()) {
1360 if (!leaf->object()->isListMarker()) {
1361 int leafX = _tx + leaf->m_x;
1362 if (_x < leafX + leaf->m_width)
1363 // The x coordinate is less than the right edge of the box.
1372 void RootInlineBox::setLineBreakInfo(RenderObject* obj, unsigned breakPos, BidiStatus* status, BidiContext* context)
1374 m_lineBreakObj = obj;
1375 m_lineBreakPos = breakPos;
1376 m_lineBreakContext = context;
1378 m_lineBreakBidiStatus = *status;
1385 void showTree(const WebCore::InlineBox* b)
1388 b->showTreeForThis();