Home | History | Annotate | Download | only in rendering
      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/DrawLooperBuilder.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 = renderer().style(isFirstLineStyle());
     40 
     41     const Font& font = style->font();
     42     FloatPoint boxOrigin = locationIncludingFlipping();
     43     boxOrigin.moveBy(FloatPoint(paintOffset));
     44     if (!isHorizontal())
     45         boxOrigin.move(0, -virtualLogicalHeight());
     46     FloatRect boxRect(boxOrigin, LayoutSize(logicalWidth(), virtualLogicalHeight()));
     47     GraphicsContextStateSaver stateSaver(*context);
     48     if (!isHorizontal())
     49         context->concatCTM(InlineTextBox::rotation(boxRect, InlineTextBox::Clockwise));
     50     FloatPoint textOrigin = FloatPoint(boxOrigin.x(), boxOrigin.y() + font.fontMetrics().ascent());
     51 
     52     Color styleTextColor = renderer().resolveColor(style, CSSPropertyWebkitTextFillColor);
     53     if (styleTextColor != context->fillColor())
     54         context->setFillColor(styleTextColor);
     55 
     56     if (selectionState() != RenderObject::SelectionNone) {
     57         paintSelection(context, boxOrigin, style, font);
     58 
     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);
     63     }
     64 
     65     // Text shadows are disabled when printing. http://crbug.com/258321
     66     const ShadowList* shadowList = context->printing() ? 0 : style->textShadow();
     67     bool hasShadow = shadowList;
     68     if (hasShadow) {
     69         OwnPtr<DrawLooperBuilder> drawLooperBuilder = DrawLooperBuilder::create();
     70         for (size_t i = shadowList->shadows().size(); i--; ) {
     71             const ShadowData& shadow = shadowList->shadows()[i];
     72             float shadowX = isHorizontal() ? shadow.x() : shadow.y();
     73             float shadowY = isHorizontal() ? shadow.y() : -shadow.x();
     74             FloatSize offset(shadowX, shadowY);
     75             drawLooperBuilder->addShadow(offset, shadow.blur(), shadow.color(),
     76                 DrawLooperBuilder::ShadowRespectsTransforms, DrawLooperBuilder::ShadowIgnoresAlpha);
     77         }
     78         drawLooperBuilder->addUnmodifiedContent();
     79         context->setDrawLooper(drawLooperBuilder.release());
     80     }
     81 
     82     TextRun textRun = RenderBlockFlow::constructTextRun(&renderer(), font, m_str, style, TextRun::AllowTrailingExpansion);
     83     TextRunPaintInfo textRunPaintInfo(textRun);
     84     textRunPaintInfo.bounds = boxRect;
     85     context->drawText(font, textRunPaintInfo, textOrigin);
     86 
     87     // Restore the regular fill color.
     88     if (styleTextColor != context->fillColor())
     89         context->setFillColor(styleTextColor);
     90 
     91     if (hasShadow)
     92         context->clearDrawLooper();
     93 
     94     paintMarkupBox(paintInfo, paintOffset, lineTop, lineBottom, style);
     95 }
     96 
     97 InlineBox* EllipsisBox::markupBox() const
     98 {
     99     if (!m_shouldPaintMarkupBox || !renderer().isRenderBlock())
    100         return 0;
    101 
    102     RenderBlock& block = toRenderBlock(renderer());
    103     RootInlineBox* lastLine = block.lineAtIndex(block.lineCount() - 1);
    104     if (!lastLine)
    105         return 0;
    106 
    107     // If the last line-box on the last line of a block is a link, -webkit-line-clamp paints that box after the ellipsis.
    108     // It does not actually move the link.
    109     InlineBox* anchorBox = lastLine->lastChild();
    110     if (!anchorBox || !anchorBox->renderer().style()->isLink())
    111         return 0;
    112 
    113     return anchorBox;
    114 }
    115 
    116 void EllipsisBox::paintMarkupBox(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit lineTop, LayoutUnit lineBottom, RenderStyle* style)
    117 {
    118     InlineBox* markupBox = this->markupBox();
    119     if (!markupBox)
    120         return;
    121 
    122     LayoutPoint adjustedPaintOffset = paintOffset;
    123     adjustedPaintOffset.move(x() + m_logicalWidth - markupBox->x(),
    124         y() + style->fontMetrics().ascent() - (markupBox->y() + markupBox->renderer().style(isFirstLineStyle())->fontMetrics().ascent()));
    125     markupBox->paint(paintInfo, adjustedPaintOffset, lineTop, lineBottom);
    126 }
    127 
    128 IntRect EllipsisBox::selectionRect()
    129 {
    130     RenderStyle* style = renderer().style(isFirstLineStyle());
    131     const Font& font = style->font();
    132     return enclosingIntRect(font.selectionRectForText(RenderBlockFlow::constructTextRun(&renderer(), font, m_str, style, TextRun::AllowTrailingExpansion), IntPoint(logicalLeft(), logicalTop() + root().selectionTopAdjustedForPrecedingBlock()), root().selectionHeightAdjustedForPrecedingBlock()));
    133 }
    134 
    135 void EllipsisBox::paintSelection(GraphicsContext* context, const FloatPoint& boxOrigin, RenderStyle* style, const Font& font)
    136 {
    137     Color textColor = renderer().resolveColor(style, CSSPropertyColor);
    138     Color c = renderer().selectionBackgroundColor();
    139     if (!c.alpha())
    140         return;
    141 
    142     // If the text color ends up being the same as the selection background, invert the selection
    143     // background.
    144     if (textColor == c)
    145         c = Color(0xff - c.red(), 0xff - c.green(), 0xff - c.blue());
    146 
    147     GraphicsContextStateSaver stateSaver(*context);
    148     LayoutUnit selectionBottom = root().selectionBottom();
    149     LayoutUnit top = root().selectionTop();
    150     LayoutUnit h = root().selectionHeight();
    151     const int deltaY = roundToInt(renderer().style()->isFlippedLinesWritingMode() ? selectionBottom - logicalBottom() : logicalTop() - top);
    152     const FloatPoint localOrigin(boxOrigin.x(), boxOrigin.y() - deltaY);
    153     FloatRect clipRect(localOrigin, FloatSize(m_logicalWidth, h.toFloat()));
    154     alignSelectionRectToDevicePixels(clipRect);
    155     context->clip(clipRect);
    156     context->drawHighlightForText(font, RenderBlockFlow::constructTextRun(&renderer(), font, m_str, style, TextRun::AllowTrailingExpansion), localOrigin, h, c);
    157 }
    158 
    159 bool EllipsisBox::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit lineTop, LayoutUnit lineBottom)
    160 {
    161     LayoutPoint adjustedLocation = accumulatedOffset + roundedLayoutPoint(topLeft());
    162 
    163     // Hit test the markup box.
    164     if (InlineBox* markupBox = this->markupBox()) {
    165         RenderStyle* style = renderer().style(isFirstLineStyle());
    166         LayoutUnit mtx = adjustedLocation.x() + m_logicalWidth - markupBox->x();
    167         LayoutUnit mty = adjustedLocation.y() + style->fontMetrics().ascent() - (markupBox->y() + markupBox->renderer().style(isFirstLineStyle())->fontMetrics().ascent());
    168         if (markupBox->nodeAtPoint(request, result, locationInContainer, LayoutPoint(mtx, mty), lineTop, lineBottom)) {
    169             renderer().updateHitTestResult(result, locationInContainer.point() - LayoutSize(mtx, mty));
    170             return true;
    171         }
    172     }
    173 
    174     FloatPoint boxOrigin = locationIncludingFlipping();
    175     boxOrigin.moveBy(accumulatedOffset);
    176     FloatRect boundsRect(boxOrigin, size());
    177     if (visibleToHitTestRequest(request) && boundsRect.intersects(HitTestLocation::rectForPoint(locationInContainer.point(), 0, 0, 0, 0))) {
    178         renderer().updateHitTestResult(result, locationInContainer.point() - toLayoutSize(adjustedLocation));
    179         if (!result.addNodeToRectBasedTestResult(renderer().node(), request, locationInContainer, boundsRect))
    180             return true;
    181     }
    182 
    183     return false;
    184 }
    185 
    186 } // namespace WebCore
    187