#include "config.h"
#include "EllipsisBox.h"
#include "Document.h"
#include "GraphicsContext.h"
#include "HitTestResult.h"
#include "PaintInfo.h"
#include "RenderBlock.h"
#include "RootInlineBox.h"
#include "TextRun.h"
namespace WebCore {
void EllipsisBox::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit lineTop, LayoutUnit lineBottom)
{
GraphicsContext* context = paintInfo.context;
RenderStyle* style = m_renderer->style(isFirstLineStyle());
Color textColor = style->visitedDependentColor(CSSPropertyColor);
if (textColor != context->fillColor())
context->setFillColor(textColor, style->colorSpace());
bool setShadow = false;
if (style->textShadow()) {
context->setShadow(LayoutSize(style->textShadow()->x(), style->textShadow()->y()),
style->textShadow()->blur(), style->textShadow()->color(), style->colorSpace());
setShadow = true;
}
const Font& font = style->font();
if (selectionState() != RenderObject::SelectionNone) {
paintSelection(context, paintOffset, style, font);
Color foreground = paintInfo.forceBlackText ? Color::black : renderer()->selectionForegroundColor();
if (foreground.isValid() && foreground != textColor)
context->setFillColor(foreground, style->colorSpace());
}
context->drawText(font, RenderBlock::constructTextRun(renderer(), font, m_str, style, TextRun::AllowTrailingExpansion), LayoutPoint(x() + paintOffset.x(), y() + paintOffset.y() + style->fontMetrics().ascent()));
if (textColor != context->fillColor())
context->setFillColor(textColor, style->colorSpace());
if (setShadow)
context->clearShadow();
paintMarkupBox(paintInfo, paintOffset, lineTop, lineBottom, style);
}
void EllipsisBox::paintMarkupBox(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit lineTop, LayoutUnit lineBottom, RenderStyle* style)
{
if (!m_shouldPaintMarkupBox || !m_renderer->isRenderBlock())
return;
RenderBlock* block = toRenderBlock(m_renderer);
RootInlineBox* lastLine = block->lineAtIndex(block->lineCount() - 1);
if (!lastLine)
return;
InlineBox* anchorBox = lastLine->lastChild();
if (!anchorBox || !anchorBox->renderer()->style()->isLink())
return;
LayoutPoint adjustedPaintOffset = paintOffset;
adjustedPaintOffset.move(x() + m_logicalWidth - anchorBox->x(),
y() + style->fontMetrics().ascent() - (anchorBox->y() + anchorBox->renderer()->style(isFirstLineStyle())->fontMetrics().ascent()));
anchorBox->paint(paintInfo, adjustedPaintOffset, lineTop, lineBottom);
}
IntRect EllipsisBox::selectionRect()
{
RenderStyle* style = m_renderer->style(isFirstLineStyle());
const Font& font = style->font();
return enclosingIntRect(font.selectionRectForText(RenderBlock::constructTextRun(renderer(), font, m_str, style, TextRun::AllowTrailingExpansion), IntPoint(x(), y() + root()->selectionTopAdjustedForPrecedingBlock()), root()->selectionHeightAdjustedForPrecedingBlock()));
}
void EllipsisBox::paintSelection(GraphicsContext* context, const LayoutPoint& paintOffset, RenderStyle* style, const Font& font)
{
Color textColor = style->visitedDependentColor(CSSPropertyColor);
Color c = m_renderer->selectionBackgroundColor();
if (!c.isValid() || !c.alpha())
return;
if (textColor == c)
c = Color(0xff - c.red(), 0xff - c.green(), 0xff - c.blue());
GraphicsContextStateSaver stateSaver(*context);
LayoutUnit top = root()->selectionTop();
LayoutUnit h = root()->selectionHeight();
context->clip(IntRect(x() + paintOffset.x(), top + paintOffset.y(), m_logicalWidth, h));
context->drawHighlightForText(font, RenderBlock::constructTextRun(renderer(), font, m_str, style, TextRun::AllowTrailingExpansion), IntPoint(x() + paintOffset.x(), y() + paintOffset.y() + top), h, c, style->colorSpace());
}
bool EllipsisBox::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const LayoutPoint& pointInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit lineTop, LayoutUnit lineBottom)
{
if (!m_shouldPaintMarkupBox || !m_renderer->isRenderBlock())
return false;
RenderBlock* block = toRenderBlock(m_renderer);
RootInlineBox* lastLine = block->lineAtIndex(block->lineCount() - 1);
if (!lastLine)
return false;
InlineBox* anchorBox = lastLine->lastChild();
if (!anchorBox || !anchorBox->renderer()->style()->isLink())
return false;
LayoutPoint adjustedLocation = accumulatedOffset + roundedLayoutPoint(topLeft());
if (anchorBox) {
RenderStyle* style = m_renderer->style(isFirstLineStyle());
LayoutUnit mtx = adjustedLocation.x() + m_logicalWidth - anchorBox->x();
LayoutUnit mty = adjustedLocation.y() + style->fontMetrics().ascent() - (anchorBox->y() + anchorBox->renderer()->style(isFirstLineStyle())->fontMetrics().ascent());
if (anchorBox->nodeAtPoint(request, result, pointInContainer, LayoutPoint(mtx, mty), lineTop, lineBottom)) {
renderer()->updateHitTestResult(result, pointInContainer - LayoutSize(mtx, mty));
return true;
}
}
LayoutRect boundsRect(adjustedLocation, LayoutSize(m_logicalWidth, m_height));
if (visibleToHitTesting() && boundsRect.intersects(result.rectForPoint(pointInContainer))) {
renderer()->updateHitTestResult(result, pointInContainer - toLayoutSize(adjustedLocation));
if (!result.addNodeToRectBasedTestResult(renderer()->node(), pointInContainer, boundsRect))
return true;
}
return false;
}
}