2 * Copyright (C) 2003, 2006 Apple Computer, Inc.
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 "EllipsisBox.h"
25 #include "GraphicsContext.h"
26 #include "HitTestResult.h"
27 #include "InlineTextBox.h"
28 #include "PaintInfo.h"
29 #include "RenderBlock.h"
30 #include "RootInlineBox.h"
35 EllipsisBox::EllipsisBox(RenderBlock& renderer, const AtomicString& ellipsisStr, InlineFlowBox* parent, int width, int height, int y, bool firstLine, bool isVertical, InlineBox* markupBox)
36 : InlineBox(renderer, FloatPoint(0, y), width, firstLine, true, false, false, isVertical, 0, 0, parent)
37 , m_shouldPaintMarkupBox(markupBox)
40 , m_selectionState(RenderObject::SelectionNone)
44 void EllipsisBox::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit lineTop, LayoutUnit lineBottom)
46 GraphicsContext* context = paintInfo.context;
47 RenderStyle* style = renderer().style(isFirstLineStyle());
48 Color textColor = style->visitedDependentColor(CSSPropertyWebkitTextFillColor);
49 if (textColor != context->fillColor())
50 context->setFillColor(textColor, style->colorSpace());
51 bool setShadow = false;
52 if (style->textShadow()) {
53 context->setShadow(LayoutSize(style->textShadow()->x(), style->textShadow()->y()),
54 style->textShadow()->radius(), style->textShadow()->color(), style->colorSpace());
58 const Font& font = style->font();
59 if (selectionState() != RenderObject::SelectionNone) {
60 paintSelection(context, paintOffset, style, font);
62 // Select the correct color for painting the text.
63 Color foreground = paintInfo.forceBlackText() ? Color::black : renderer().selectionForegroundColor();
64 if (foreground.isValid() && foreground != textColor)
65 context->setFillColor(foreground, style->colorSpace());
68 // FIXME: Why is this always LTR? Fix by passing correct text run flags below.
69 context->drawText(font, RenderBlock::constructTextRun(&renderer(), font, m_str, style, TextRun::AllowTrailingExpansion), LayoutPoint(x() + paintOffset.x(), y() + paintOffset.y() + style->fontMetrics().ascent()));
71 // Restore the regular fill color.
72 if (textColor != context->fillColor())
73 context->setFillColor(textColor, style->colorSpace());
76 context->clearShadow();
78 paintMarkupBox(paintInfo, paintOffset, lineTop, lineBottom, style);
81 InlineBox* EllipsisBox::markupBox() const
83 if (!m_shouldPaintMarkupBox)
86 RootInlineBox* lastLine = renderer().lineAtIndex(renderer().lineCount() - 1);
90 // If the last line-box on the last line of a block is a link, -webkit-line-clamp paints that box after the ellipsis.
91 // It does not actually move the link.
92 InlineBox* anchorBox = lastLine->lastChild();
93 if (!anchorBox || !anchorBox->renderer().style()->isLink())
99 void EllipsisBox::paintMarkupBox(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit lineTop, LayoutUnit lineBottom, RenderStyle* style)
101 InlineBox* markupBox = this->markupBox();
105 LayoutPoint adjustedPaintOffset = paintOffset;
106 adjustedPaintOffset.move(x() + m_logicalWidth - markupBox->x(),
107 y() + style->fontMetrics().ascent() - (markupBox->y() + markupBox->renderer().style(isFirstLineStyle())->fontMetrics().ascent()));
108 markupBox->paint(paintInfo, adjustedPaintOffset, lineTop, lineBottom);
111 IntRect EllipsisBox::selectionRect()
113 RenderStyle* style = renderer().style(isFirstLineStyle());
114 const Font& font = style->font();
115 const RootInlineBox& rootBox = root();
116 // FIXME: Why is this always LTR? Fix by passing correct text run flags below.
117 return enclosingIntRect(font.selectionRectForText(RenderBlock::constructTextRun(&renderer(), font, m_str, style, TextRun::AllowTrailingExpansion), IntPoint(x(), y() + rootBox.selectionTopAdjustedForPrecedingBlock()), rootBox.selectionHeightAdjustedForPrecedingBlock()));
120 void EllipsisBox::paintSelection(GraphicsContext* context, const LayoutPoint& paintOffset, RenderStyle* style, const Font& font)
122 Color textColor = style->visitedDependentColor(CSSPropertyColor);
123 Color c = renderer().selectionBackgroundColor();
124 if (!c.isValid() || !c.alpha())
127 // If the text color ends up being the same as the selection background, invert the selection
130 c = Color(0xff - c.red(), 0xff - c.green(), 0xff - c.blue());
132 const RootInlineBox& rootBox = root();
133 LayoutUnit top = rootBox.selectionTop();
134 LayoutUnit h = rootBox.selectionHeight();
135 FloatRect clipRect(x() + paintOffset.x(), top + paintOffset.y(), m_logicalWidth, h);
136 alignSelectionRectToDevicePixels(clipRect);
138 GraphicsContextStateSaver stateSaver(*context);
139 context->clip(clipRect);
140 // FIXME: Why is this always LTR? Fix by passing correct text run flags below.
141 context->drawHighlightForText(font, RenderBlock::constructTextRun(&renderer(), font, m_str, style, TextRun::AllowTrailingExpansion), roundedIntPoint(LayoutPoint(x() + paintOffset.x(), y() + paintOffset.y() + top)), h, c, style->colorSpace());
144 bool EllipsisBox::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit lineTop, LayoutUnit lineBottom)
146 LayoutPoint adjustedLocation = accumulatedOffset + roundedLayoutPoint(topLeft());
148 // Hit test the markup box.
149 if (InlineBox* markupBox = this->markupBox()) {
150 RenderStyle* style = renderer().style(isFirstLineStyle());
151 LayoutUnit mtx = adjustedLocation.x() + m_logicalWidth - markupBox->x();
152 LayoutUnit mty = adjustedLocation.y() + style->fontMetrics().ascent() - (markupBox->y() + markupBox->renderer().style(isFirstLineStyle())->fontMetrics().ascent());
153 if (markupBox->nodeAtPoint(request, result, locationInContainer, LayoutPoint(mtx, mty), lineTop, lineBottom)) {
154 renderer().updateHitTestResult(result, locationInContainer.point() - LayoutSize(mtx, mty));
159 LayoutRect boundsRect(adjustedLocation, LayoutSize(m_logicalWidth, m_height));
160 if (visibleToHitTesting() && boundsRect.intersects(HitTestLocation::rectForPoint(locationInContainer.point(), 0, 0, 0, 0))) {
161 renderer().updateHitTestResult(result, locationInContainer.point() - toLayoutSize(adjustedLocation));
162 if (!result.addNodeToRectBasedTestResult(renderer().element(), request, locationInContainer, boundsRect))
169 } // namespace WebCore