EllipsisBox.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #include "config.h"
  20. #include "EllipsisBox.h"
  21. #include "Document.h"
  22. #include "Font.h"
  23. #include "GraphicsContext.h"
  24. #include "HitTestResult.h"
  25. #include "InlineTextBox.h"
  26. #include "PaintInfo.h"
  27. #include "RenderBlock.h"
  28. #include "RootInlineBox.h"
  29. #include "TextRun.h"
  30. namespace WebCore {
  31. void EllipsisBox::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit lineTop, LayoutUnit lineBottom)
  32. {
  33. GraphicsContext* context = paintInfo.context;
  34. RenderStyle* style = m_renderer->style(isFirstLineStyle());
  35. Color textColor = style->visitedDependentColor(CSSPropertyWebkitTextFillColor);
  36. if (textColor != context->fillColor())
  37. context->setFillColor(textColor, style->colorSpace());
  38. bool setShadow = false;
  39. if (style->textShadow()) {
  40. context->setShadow(LayoutSize(style->textShadow()->x(), style->textShadow()->y()),
  41. style->textShadow()->radius(), style->textShadow()->color(), style->colorSpace());
  42. setShadow = true;
  43. }
  44. const Font& font = style->font();
  45. if (selectionState() != RenderObject::SelectionNone) {
  46. paintSelection(context, paintOffset, style, font);
  47. // Select the correct color for painting the text.
  48. Color foreground = paintInfo.forceBlackText() ? Color::black : renderer()->selectionForegroundColor();
  49. if (foreground.isValid() && foreground != textColor)
  50. context->setFillColor(foreground, style->colorSpace());
  51. }
  52. // FIXME: Why is this always LTR? Fix by passing correct text run flags below.
  53. context->drawText(font, RenderBlock::constructTextRun(renderer(), font, m_str, style, TextRun::AllowTrailingExpansion), LayoutPoint(x() + paintOffset.x(), y() + paintOffset.y() + style->fontMetrics().ascent()));
  54. // Restore the regular fill color.
  55. if (textColor != context->fillColor())
  56. context->setFillColor(textColor, style->colorSpace());
  57. if (setShadow)
  58. context->clearShadow();
  59. paintMarkupBox(paintInfo, paintOffset, lineTop, lineBottom, style);
  60. }
  61. InlineBox* EllipsisBox::markupBox() const
  62. {
  63. if (!m_shouldPaintMarkupBox || !m_renderer->isRenderBlock())
  64. return 0;
  65. RenderBlock* block = toRenderBlock(m_renderer);
  66. RootInlineBox* lastLine = block->lineAtIndex(block->lineCount() - 1);
  67. if (!lastLine)
  68. return 0;
  69. // If the last line-box on the last line of a block is a link, -webkit-line-clamp paints that box after the ellipsis.
  70. // It does not actually move the link.
  71. InlineBox* anchorBox = lastLine->lastChild();
  72. if (!anchorBox || !anchorBox->renderer()->style()->isLink())
  73. return 0;
  74. return anchorBox;
  75. }
  76. void EllipsisBox::paintMarkupBox(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit lineTop, LayoutUnit lineBottom, RenderStyle* style)
  77. {
  78. InlineBox* markupBox = this->markupBox();
  79. if (!markupBox)
  80. return;
  81. LayoutPoint adjustedPaintOffset = paintOffset;
  82. adjustedPaintOffset.move(x() + m_logicalWidth - markupBox->x(),
  83. y() + style->fontMetrics().ascent() - (markupBox->y() + markupBox->renderer()->style(isFirstLineStyle())->fontMetrics().ascent()));
  84. markupBox->paint(paintInfo, adjustedPaintOffset, lineTop, lineBottom);
  85. }
  86. IntRect EllipsisBox::selectionRect()
  87. {
  88. RenderStyle* style = m_renderer->style(isFirstLineStyle());
  89. const Font& font = style->font();
  90. // FIXME: Why is this always LTR? Fix by passing correct text run flags below.
  91. return enclosingIntRect(font.selectionRectForText(RenderBlock::constructTextRun(renderer(), font, m_str, style, TextRun::AllowTrailingExpansion), IntPoint(x(), y() + root()->selectionTopAdjustedForPrecedingBlock()), root()->selectionHeightAdjustedForPrecedingBlock()));
  92. }
  93. void EllipsisBox::paintSelection(GraphicsContext* context, const LayoutPoint& paintOffset, RenderStyle* style, const Font& font)
  94. {
  95. Color textColor = style->visitedDependentColor(CSSPropertyColor);
  96. Color c = m_renderer->selectionBackgroundColor();
  97. if (!c.isValid() || !c.alpha())
  98. return;
  99. // If the text color ends up being the same as the selection background, invert the selection
  100. // background.
  101. if (textColor == c)
  102. c = Color(0xff - c.red(), 0xff - c.green(), 0xff - c.blue());
  103. GraphicsContextStateSaver stateSaver(*context);
  104. LayoutUnit top = root()->selectionTop();
  105. LayoutUnit h = root()->selectionHeight();
  106. FloatRect clipRect(x() + paintOffset.x(), top + paintOffset.y(), m_logicalWidth, h);
  107. alignSelectionRectToDevicePixels(clipRect);
  108. context->clip(clipRect);
  109. // FIXME: Why is this always LTR? Fix by passing correct text run flags below.
  110. 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());
  111. }
  112. bool EllipsisBox::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit lineTop, LayoutUnit lineBottom)
  113. {
  114. LayoutPoint adjustedLocation = accumulatedOffset + roundedLayoutPoint(topLeft());
  115. // Hit test the markup box.
  116. if (InlineBox* markupBox = this->markupBox()) {
  117. RenderStyle* style = m_renderer->style(isFirstLineStyle());
  118. LayoutUnit mtx = adjustedLocation.x() + m_logicalWidth - markupBox->x();
  119. LayoutUnit mty = adjustedLocation.y() + style->fontMetrics().ascent() - (markupBox->y() + markupBox->renderer()->style(isFirstLineStyle())->fontMetrics().ascent());
  120. if (markupBox->nodeAtPoint(request, result, locationInContainer, LayoutPoint(mtx, mty), lineTop, lineBottom)) {
  121. renderer()->updateHitTestResult(result, locationInContainer.point() - LayoutSize(mtx, mty));
  122. return true;
  123. }
  124. }
  125. LayoutRect boundsRect(adjustedLocation, LayoutSize(m_logicalWidth, m_height));
  126. if (visibleToHitTesting() && boundsRect.intersects(HitTestLocation::rectForPoint(locationInContainer.point(), 0, 0, 0, 0))) {
  127. renderer()->updateHitTestResult(result, locationInContainer.point() - toLayoutSize(adjustedLocation));
  128. if (!result.addNodeToRectBasedTestResult(renderer()->node(), request, locationInContainer, boundsRect))
  129. return true;
  130. }
  131. return false;
  132. }
  133. } // namespace WebCore