Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / EllipsisBox.cpp
1 /**
2  * Copyright (C) 2003, 2006 Apple Computer, Inc.
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include "config.h"
21 #include "core/rendering/EllipsisBox.h"
22
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"
33
34 namespace WebCore {
35
36 void EllipsisBox::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit lineTop, LayoutUnit lineBottom)
37 {
38     GraphicsContext* context = paintInfo.context;
39     RenderStyle* style = m_renderer->style(isFirstLineStyle());
40
41     const Font& font = style->font();
42     FloatPoint boxOrigin = locationIncludingFlipping();
43     LayoutPoint adjustedPaintOffset = paintOffset;
44     if (!isHorizontal())
45         adjustedPaintOffset.move(0, -virtualLogicalHeight());
46     boxOrigin.move(adjustedPaintOffset.x(), adjustedPaintOffset.y());
47     FloatRect boxRect(boxOrigin, LayoutSize(logicalWidth(), virtualLogicalHeight()));
48     GraphicsContextStateSaver stateSaver(*context);
49     if (!isHorizontal())
50         context->concatCTM(InlineTextBox::rotation(boxRect, InlineTextBox::Clockwise));
51     FloatPoint textOrigin = FloatPoint(boxOrigin.x(), boxOrigin.y() + font.fontMetrics().ascent());
52
53     Color styleTextColor = m_renderer->resolveColor(style, CSSPropertyWebkitTextFillColor);
54     if (styleTextColor != context->fillColor())
55         context->setFillColor(styleTextColor);
56
57     if (selectionState() != RenderObject::SelectionNone) {
58         paintSelection(context, boxOrigin, style, font);
59
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);
64     }
65
66     // Text shadows are disabled when printing. http://crbug.com/258321
67     const ShadowList* shadowList = context->printing() ? 0 : style->textShadow();
68     bool hasShadow = shadowList;
69     if (hasShadow) {
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);
78         }
79         drawLooper.addUnmodifiedContent();
80         context->setDrawLooper(drawLooper);
81     }
82
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);
88
89     // Restore the regular fill color.
90     if (styleTextColor != context->fillColor())
91         context->setFillColor(styleTextColor);
92
93     if (hasShadow)
94         context->clearDrawLooper();
95
96     paintMarkupBox(paintInfo, paintOffset, lineTop, lineBottom, style);
97 }
98
99 InlineBox* EllipsisBox::markupBox() const
100 {
101     if (!m_shouldPaintMarkupBox || !m_renderer->isRenderBlock())
102         return 0;
103
104     RenderBlock* block = toRenderBlock(m_renderer);
105     RootInlineBox* lastLine = block->lineAtIndex(block->lineCount() - 1);
106     if (!lastLine)
107         return 0;
108
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())
113         return 0;
114
115     return anchorBox;
116 }
117
118 void EllipsisBox::paintMarkupBox(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit lineTop, LayoutUnit lineBottom, RenderStyle* style)
119 {
120     InlineBox* markupBox = this->markupBox();
121     if (!markupBox)
122         return;
123
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);
128 }
129
130 IntRect EllipsisBox::selectionRect()
131 {
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()));
136 }
137
138 void EllipsisBox::paintSelection(GraphicsContext* context, const FloatPoint& boxOrigin, RenderStyle* style, const Font& font)
139 {
140     Color textColor = m_renderer->resolveColor(style, CSSPropertyColor);
141     Color c = m_renderer->selectionBackgroundColor();
142     if (!c.alpha())
143         return;
144
145     // If the text color ends up being the same as the selection background, invert the selection
146     // background.
147     if (textColor == c)
148         c = Color(0xff - c.red(), 0xff - c.green(), 0xff - c.blue());
149
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);
161 }
162
163 bool EllipsisBox::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit lineTop, LayoutUnit lineBottom)
164 {
165     LayoutPoint adjustedLocation = accumulatedOffset + roundedLayoutPoint(topLeft());
166
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));
174             return true;
175         }
176     }
177
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))
184             return true;
185     }
186
187     return false;
188 }
189
190 } // namespace WebCore