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 "core/rendering/EllipsisBox.h"
23 #include "core/rendering/HitTestResult.h"
24 #include "core/rendering/InlineTextBox.h"
25 #include "core/rendering/PaintInfo.h"
26 #include "core/rendering/RenderBlockFlow.h"
27 #include "core/rendering/RootInlineBox.h"
28 #include "core/rendering/style/ShadowList.h"
29 #include "platform/fonts/Font.h"
30 #include "platform/graphics/DrawLooper.h"
31 #include "platform/graphics/GraphicsContextStateSaver.h"
32 #include "platform/text/TextRun.h"
36 void EllipsisBox::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit lineTop, LayoutUnit lineBottom)
38 GraphicsContext* context = paintInfo.context;
39 RenderStyle* style = m_renderer->style(isFirstLineStyle());
41 const Font& font = style->font();
42 FloatPoint boxOrigin = locationIncludingFlipping();
43 LayoutPoint adjustedPaintOffset = paintOffset;
45 adjustedPaintOffset.move(0, -virtualLogicalHeight());
46 boxOrigin.move(adjustedPaintOffset.x(), adjustedPaintOffset.y());
47 FloatRect boxRect(boxOrigin, LayoutSize(logicalWidth(), virtualLogicalHeight()));
48 GraphicsContextStateSaver stateSaver(*context);
50 context->concatCTM(InlineTextBox::rotation(boxRect, InlineTextBox::Clockwise));
51 FloatPoint textOrigin = FloatPoint(boxOrigin.x(), boxOrigin.y() + font.fontMetrics().ascent());
53 Color styleTextColor = m_renderer->resolveColor(style, CSSPropertyWebkitTextFillColor);
54 if (styleTextColor != context->fillColor())
55 context->setFillColor(styleTextColor);
57 if (selectionState() != RenderObject::SelectionNone) {
58 paintSelection(context, boxOrigin, style, font);
60 // Select the correct color for painting the text.
61 Color foreground = paintInfo.forceBlackText() ? Color::black : renderer()->selectionForegroundColor();
62 if (foreground != styleTextColor)
63 context->setFillColor(foreground);
66 // Text shadows are disabled when printing. http://crbug.com/258321
67 const ShadowList* shadowList = context->printing() ? 0 : style->textShadow();
68 bool hasShadow = shadowList;
70 DrawLooper drawLooper;
71 for (size_t i = shadowList->shadows().size(); i--; ) {
72 const ShadowData& shadow = shadowList->shadows()[i];
73 float shadowX = isHorizontal() ? shadow.x() : shadow.y();
74 float shadowY = isHorizontal() ? shadow.y() : -shadow.x();
75 FloatSize offset(shadowX, shadowY);
76 drawLooper.addShadow(offset, shadow.blur(), shadow.color(),
77 DrawLooper::ShadowRespectsTransforms, DrawLooper::ShadowIgnoresAlpha);
79 drawLooper.addUnmodifiedContent();
80 context->setDrawLooper(drawLooper);
83 // FIXME: Why is this always LTR? Fix by passing correct text run flags below.
84 TextRun textRun = RenderBlockFlow::constructTextRun(renderer(), font, m_str, style, TextRun::AllowTrailingExpansion);
85 TextRunPaintInfo textRunPaintInfo(textRun);
86 textRunPaintInfo.bounds = boxRect;
87 context->drawText(font, textRunPaintInfo, textOrigin);
89 // Restore the regular fill color.
90 if (styleTextColor != context->fillColor())
91 context->setFillColor(styleTextColor);
94 context->clearDrawLooper();
96 paintMarkupBox(paintInfo, paintOffset, lineTop, lineBottom, style);
99 InlineBox* EllipsisBox::markupBox() const
101 if (!m_shouldPaintMarkupBox || !m_renderer->isRenderBlock())
104 RenderBlock* block = toRenderBlock(m_renderer);
105 RootInlineBox* lastLine = block->lineAtIndex(block->lineCount() - 1);
109 // If the last line-box on the last line of a block is a link, -webkit-line-clamp paints that box after the ellipsis.
110 // It does not actually move the link.
111 InlineBox* anchorBox = lastLine->lastChild();
112 if (!anchorBox || !anchorBox->renderer()->style()->isLink())
118 void EllipsisBox::paintMarkupBox(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit lineTop, LayoutUnit lineBottom, RenderStyle* style)
120 InlineBox* markupBox = this->markupBox();
124 LayoutPoint adjustedPaintOffset = paintOffset;
125 adjustedPaintOffset.move(x() + m_logicalWidth - markupBox->x(),
126 y() + style->fontMetrics().ascent() - (markupBox->y() + markupBox->renderer()->style(isFirstLineStyle())->fontMetrics().ascent()));
127 markupBox->paint(paintInfo, adjustedPaintOffset, lineTop, lineBottom);
130 IntRect EllipsisBox::selectionRect()
132 RenderStyle* style = m_renderer->style(isFirstLineStyle());
133 const Font& font = style->font();
134 // FIXME: Why is this always LTR? Fix by passing correct text run flags below.
135 return enclosingIntRect(font.selectionRectForText(RenderBlockFlow::constructTextRun(renderer(), font, m_str, style, TextRun::AllowTrailingExpansion), IntPoint(logicalLeft(), logicalTop() + root()->selectionTopAdjustedForPrecedingBlock()), root()->selectionHeightAdjustedForPrecedingBlock()));
138 void EllipsisBox::paintSelection(GraphicsContext* context, const FloatPoint& boxOrigin, RenderStyle* style, const Font& font)
140 Color textColor = m_renderer->resolveColor(style, CSSPropertyColor);
141 Color c = m_renderer->selectionBackgroundColor();
145 // If the text color ends up being the same as the selection background, invert the selection
148 c = Color(0xff - c.red(), 0xff - c.green(), 0xff - c.blue());
150 GraphicsContextStateSaver stateSaver(*context);
151 LayoutUnit selectionBottom = root()->selectionBottom();
152 LayoutUnit top = root()->selectionTop();
153 LayoutUnit h = root()->selectionHeight();
154 const int deltaY = roundToInt(renderer()->style()->isFlippedLinesWritingMode() ? selectionBottom - logicalBottom() : logicalTop() - top);
155 const FloatPoint localOrigin(boxOrigin.x(), boxOrigin.y() - deltaY);
156 FloatRect clipRect(localOrigin, FloatSize(m_logicalWidth, h));
157 alignSelectionRectToDevicePixels(clipRect);
158 context->clip(clipRect);
159 // FIXME: Why is this always LTR? Fix by passing correct text run flags below.
160 context->drawHighlightForText(font, RenderBlockFlow::constructTextRun(renderer(), font, m_str, style, TextRun::AllowTrailingExpansion), localOrigin, h, c);
163 bool EllipsisBox::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit lineTop, LayoutUnit lineBottom)
165 LayoutPoint adjustedLocation = accumulatedOffset + roundedLayoutPoint(topLeft());
167 // Hit test the markup box.
168 if (InlineBox* markupBox = this->markupBox()) {
169 RenderStyle* style = m_renderer->style(isFirstLineStyle());
170 LayoutUnit mtx = adjustedLocation.x() + m_logicalWidth - markupBox->x();
171 LayoutUnit mty = adjustedLocation.y() + style->fontMetrics().ascent() - (markupBox->y() + markupBox->renderer()->style(isFirstLineStyle())->fontMetrics().ascent());
172 if (markupBox->nodeAtPoint(request, result, locationInContainer, LayoutPoint(mtx, mty), lineTop, lineBottom)) {
173 renderer()->updateHitTestResult(result, locationInContainer.point() - LayoutSize(mtx, mty));
178 FloatPoint boxOrigin = locationIncludingFlipping();
179 boxOrigin.moveBy(accumulatedOffset);
180 FloatRect boundsRect(boxOrigin, size());
181 if (visibleToHitTestRequest(request) && boundsRect.intersects(HitTestLocation::rectForPoint(locationInContainer.point(), 0, 0, 0, 0))) {
182 renderer()->updateHitTestResult(result, locationInContainer.point() - toLayoutSize(adjustedLocation));
183 if (!result.addNodeToRectBasedTestResult(renderer()->node(), request, locationInContainer, boundsRect))
190 } // namespace WebCore