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/RenderBlock.h"
27 #include "core/rendering/RootInlineBox.h"
28 #include "core/rendering/TextRunConstructor.h"
29 #include "core/rendering/style/ShadowList.h"
30 #include "platform/fonts/Font.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 = renderer().style(isFirstLineStyle());
41 const Font& font = style->font();
42 FloatPoint boxOrigin = locationIncludingFlipping();
43 boxOrigin.moveBy(FloatPoint(paintOffset));
45 boxOrigin.move(0, -virtualLogicalHeight());
46 FloatRect boxRect(boxOrigin, LayoutSize(logicalWidth(), virtualLogicalHeight()));
47 GraphicsContextStateSaver stateSaver(*context);
49 context->concatCTM(InlineTextBox::rotation(boxRect, InlineTextBox::Clockwise));
50 FloatPoint textOrigin = FloatPoint(boxOrigin.x(), boxOrigin.y() + font.fontMetrics().ascent());
52 Color styleTextColor = renderer().resolveColor(style, CSSPropertyWebkitTextFillColor);
53 if (styleTextColor != context->fillColor())
54 context->setFillColor(styleTextColor);
56 if (selectionState() != RenderObject::SelectionNone) {
57 paintSelection(context, boxOrigin, style, font);
59 // Select the correct color for painting the text.
60 Color foreground = paintInfo.forceBlackText() ? Color::black : renderer().selectionForegroundColor();
61 if (foreground != styleTextColor)
62 context->setFillColor(foreground);
65 // Text shadows are disabled when printing. http://crbug.com/258321
66 const ShadowList* shadowList = context->printing() ? 0 : style->textShadow();
67 bool hasShadow = shadowList;
69 context->setDrawLooper(shadowList->createDrawLooper(DrawLooperBuilder::ShadowIgnoresAlpha, isHorizontal()));
71 TextRun textRun = constructTextRun(&renderer(), font, m_str, style, TextRun::AllowTrailingExpansion);
72 TextRunPaintInfo textRunPaintInfo(textRun);
73 textRunPaintInfo.bounds = boxRect;
74 context->drawText(font, textRunPaintInfo, textOrigin);
76 // Restore the regular fill color.
77 if (styleTextColor != context->fillColor())
78 context->setFillColor(styleTextColor);
81 context->clearDrawLooper();
83 paintMarkupBox(paintInfo, paintOffset, lineTop, lineBottom, style);
86 InlineBox* EllipsisBox::markupBox() const
88 if (!m_shouldPaintMarkupBox || !renderer().isRenderBlock())
91 RenderBlock& block = toRenderBlock(renderer());
92 RootInlineBox* lastLine = block.lineAtIndex(block.lineCount() - 1);
96 // If the last line-box on the last line of a block is a link, -webkit-line-clamp paints that box after the ellipsis.
97 // It does not actually move the link.
98 InlineBox* anchorBox = lastLine->lastChild();
99 if (!anchorBox || !anchorBox->renderer().style()->isLink())
105 void EllipsisBox::paintMarkupBox(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit lineTop, LayoutUnit lineBottom, RenderStyle* style)
107 InlineBox* markupBox = this->markupBox();
111 LayoutPoint adjustedPaintOffset = paintOffset;
112 adjustedPaintOffset.move(x() + m_logicalWidth - markupBox->x(),
113 y() + style->fontMetrics().ascent() - (markupBox->y() + markupBox->renderer().style(isFirstLineStyle())->fontMetrics().ascent()));
114 markupBox->paint(paintInfo, adjustedPaintOffset, lineTop, lineBottom);
117 IntRect EllipsisBox::selectionRect()
119 RenderStyle* style = renderer().style(isFirstLineStyle());
120 const Font& font = style->font();
121 return enclosingIntRect(font.selectionRectForText(constructTextRun(&renderer(), font, m_str, style, TextRun::AllowTrailingExpansion), IntPoint(logicalLeft(), logicalTop() + root().selectionTopAdjustedForPrecedingBlock()), root().selectionHeightAdjustedForPrecedingBlock()));
124 void EllipsisBox::paintSelection(GraphicsContext* context, const FloatPoint& boxOrigin, RenderStyle* style, const Font& font)
126 Color textColor = renderer().resolveColor(style, CSSPropertyColor);
127 Color c = renderer().selectionBackgroundColor();
131 // If the text color ends up being the same as the selection background, invert the selection
134 c = Color(0xff - c.red(), 0xff - c.green(), 0xff - c.blue());
136 GraphicsContextStateSaver stateSaver(*context);
137 LayoutUnit selectionBottom = root().selectionBottom();
138 LayoutUnit top = root().selectionTop();
139 LayoutUnit h = root().selectionHeight();
140 const int deltaY = roundToInt(renderer().style()->isFlippedLinesWritingMode() ? selectionBottom - logicalBottom() : logicalTop() - top);
141 const FloatPoint localOrigin(boxOrigin.x(), boxOrigin.y() - deltaY);
142 FloatRect clipRect(localOrigin, FloatSize(m_logicalWidth, h.toFloat()));
143 context->clip(clipRect);
144 context->drawHighlightForText(font, constructTextRun(&renderer(), font, m_str, style, TextRun::AllowTrailingExpansion), localOrigin, h, c);
147 bool EllipsisBox::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit lineTop, LayoutUnit lineBottom)
149 LayoutPoint adjustedLocation = accumulatedOffset + roundedLayoutPoint(topLeft());
151 // Hit test the markup box.
152 if (InlineBox* markupBox = this->markupBox()) {
153 RenderStyle* style = renderer().style(isFirstLineStyle());
154 LayoutUnit mtx = adjustedLocation.x() + m_logicalWidth - markupBox->x();
155 LayoutUnit mty = adjustedLocation.y() + style->fontMetrics().ascent() - (markupBox->y() + markupBox->renderer().style(isFirstLineStyle())->fontMetrics().ascent());
156 if (markupBox->nodeAtPoint(request, result, locationInContainer, LayoutPoint(mtx, mty), lineTop, lineBottom)) {
157 renderer().updateHitTestResult(result, locationInContainer.point() - LayoutSize(mtx, mty));
162 FloatPoint boxOrigin = locationIncludingFlipping();
163 boxOrigin.moveBy(accumulatedOffset);
164 FloatRect boundsRect(boxOrigin, size());
165 if (visibleToHitTestRequest(request) && boundsRect.intersects(HitTestLocation::rectForPoint(locationInContainer.point(), 0, 0, 0, 0))) {
166 renderer().updateHitTestResult(result, locationInContainer.point() - toLayoutSize(adjustedLocation));
167 if (!result.addNodeToRectBasedTestResult(renderer().node(), request, locationInContainer, boundsRect))