SimpleLineLayoutFunctions.cpp 6.7 KB
Newer Older
antti@apple.com's avatar
antti@apple.com committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 * Copyright (C) 2013 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "SimpleLineLayoutFunctions.h"

#include "FontCache.h"
30
#include "Frame.h"
antti@apple.com's avatar
antti@apple.com committed
31 32 33 34 35 36 37 38 39
#include "GraphicsContext.h"
#include "HitTestLocation.h"
#include "HitTestRequest.h"
#include "HitTestResult.h"
#include "InlineTextBox.h"
#include "PaintInfo.h"
#include "RenderBlockFlow.h"
#include "RenderStyle.h"
#include "RenderText.h"
antti@apple.com's avatar
antti@apple.com committed
40
#include "RenderView.h"
41
#include "Settings.h"
antti@apple.com's avatar
antti@apple.com committed
42 43 44 45 46 47 48 49
#include "SimpleLineLayoutResolver.h"
#include "Text.h"
#include "TextPaintStyle.h"
#include <wtf/unicode/Unicode.h>

namespace WebCore {
namespace SimpleLineLayout {

50 51
static void paintDebugBorders(GraphicsContext& context, const LayoutRect& borderRect, const LayoutPoint& paintOffset)
{
52 53
    if (borderRect.isEmpty())
        return;
54 55 56 57 58 59 60 61
    GraphicsContextStateSaver stateSaver(context);
    context.setStrokeColor(Color(0, 255, 0), ColorSpaceDeviceRGB);
    context.setFillColor(Color::transparent, ColorSpaceDeviceRGB);
    IntRect rect(pixelSnappedIntRect(borderRect));
    rect.moveBy(flooredIntPoint(paintOffset));
    context.drawRect(rect);
}

62
void paintFlow(const RenderBlockFlow& flow, const Layout& layout, PaintInfo& paintInfo, const LayoutPoint& paintOffset)
antti@apple.com's avatar
antti@apple.com committed
63 64 65 66 67 68
{
    if (paintInfo.phase != PaintPhaseForeground)
        return;
    RenderText& textRenderer = toRenderText(*flow.firstChild());
    ASSERT(!textRenderer.firstTextBox());

69 70
    bool debugBordersEnabled = flow.frame().settings().simpleLineLayoutDebugBordersEnabled();

71
    RenderStyle& style = flow.style();
antti@apple.com's avatar
antti@apple.com committed
72 73 74 75 76 77 78 79 80
    const Font& font = style.font();

    GraphicsContext& context = *paintInfo.context;

    TextPaintStyle textPaintStyle = computeTextPaintStyle(textRenderer, style, paintInfo);
    GraphicsContextStateSaver stateSaver(context, textPaintStyle.strokeWidth > 0);

    updateGraphicsContext(context, textPaintStyle);

81
    auto resolver = runResolver(flow, layout);
antti@apple.com's avatar
antti@apple.com committed
82
    for (auto it = resolver.begin(), end = resolver.end(); it != end; ++it) {
83
        auto run = *it;
84 85 86
        TextRun textRun(run.text());
        textRun.setTabSize(!style.collapseWhiteSpace(), style.tabSize());
        context.drawText(font, textRun, run.baseline() + paintOffset);
87 88
        if (debugBordersEnabled)
            paintDebugBorders(context, run.rect(), paintOffset);
antti@apple.com's avatar
antti@apple.com committed
89 90 91
    }
}

92
bool hitTestFlow(const RenderBlockFlow& flow, const Layout& layout, const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction hitTestAction)
antti@apple.com's avatar
antti@apple.com committed
93 94 95 96
{
    if (hitTestAction != HitTestForeground)
        return false;

97
    if (!layout.runCount())
antti@apple.com's avatar
antti@apple.com committed
98 99
        return false;

100
    RenderStyle& style = flow.style();
antti@apple.com's avatar
antti@apple.com committed
101 102 103 104 105
    if (style.visibility() != VISIBLE || style.pointerEvents() == PE_NONE)
        return false;

    RenderText& textRenderer = toRenderText(*flow.firstChild());

106
    auto resolver = lineResolver(flow, layout);
antti@apple.com's avatar
antti@apple.com committed
107
    for (auto it = resolver.begin(), end = resolver.end(); it != end; ++it) {
108
        auto lineRect = *it;
antti@apple.com's avatar
antti@apple.com committed
109 110 111 112 113 114 115 116 117 118 119
        lineRect.moveBy(accumulatedOffset);
        if (!locationInContainer.intersects(lineRect))
            continue;
        textRenderer.updateHitTestResult(result, locationInContainer.point() - toLayoutSize(accumulatedOffset));
        if (!result.addNodeToRectBasedTestResult(textRenderer.textNode(), request, locationInContainer, lineRect))
            return true;
    }

    return false;
}

120
void collectFlowOverflow(RenderBlockFlow& flow, const Layout& layout)
antti@apple.com's avatar
antti@apple.com committed
121
{
122
    auto resolver = lineResolver(flow, layout);
antti@apple.com's avatar
antti@apple.com committed
123
    for (auto it = resolver.begin(), end = resolver.end(); it != end; ++it) {
124
        auto rect = *it;
antti@apple.com's avatar
antti@apple.com committed
125 126 127 128 129
        flow.addLayoutOverflow(rect);
        flow.addVisualOverflow(rect);
    }
}

130
IntRect computeTextBoundingBox(const RenderText& textRenderer, const Layout& layout)
antti@apple.com's avatar
antti@apple.com committed
131
{
132
    auto resolver = lineResolver(toRenderBlockFlow(*textRenderer.parent()), layout);
antti@apple.com's avatar
antti@apple.com committed
133 134 135 136
    auto it = resolver.begin();
    auto end = resolver.end();
    if (it == end)
        return IntRect();
137
    auto firstLineRect = *it;
antti@apple.com's avatar
antti@apple.com committed
138 139
    float left = firstLineRect.x();
    float right = firstLineRect.maxX();
140
    float bottom = firstLineRect.maxY();
antti@apple.com's avatar
antti@apple.com committed
141
    for (++it; it != end; ++it) {
142
        auto rect = *it;
antti@apple.com's avatar
antti@apple.com committed
143 144 145 146
        if (rect.x() < left)
            left = rect.x();
        if (rect.maxX() > right)
            right = rect.maxX();
147 148
        if (rect.maxY() > bottom)
            bottom = rect.maxY();
antti@apple.com's avatar
antti@apple.com committed
149 150 151 152
    }
    float x = firstLineRect.x();
    float y = firstLineRect.y();
    float width = right - left;
153
    float height = bottom - y;
antti@apple.com's avatar
antti@apple.com committed
154 155 156
    return enclosingIntRect(FloatRect(x, y, width, height));
}

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
Vector<IntRect> collectTextAbsoluteRects(const RenderText& textRenderer, const Layout& layout, const LayoutPoint& accumulatedOffset)
{
    Vector<IntRect> rects;
    auto resolver = runResolver(toRenderBlockFlow(*textRenderer.parent()), layout);
    for (auto it = resolver.begin(), end = resolver.end(); it != end; ++it) {
        auto run = *it;
        auto rect = run.rect();
        rects.append(enclosingIntRect(FloatRect(accumulatedOffset + rect.location(), rect.size())));
    }
    return rects;
}

Vector<FloatQuad> collectTextAbsoluteQuads(const RenderText& textRenderer, const Layout& layout, bool* wasFixed)
{
    Vector<FloatQuad> quads;
    auto resolver = runResolver(toRenderBlockFlow(*textRenderer.parent()), layout);
    for (auto it = resolver.begin(), end = resolver.end(); it != end; ++it) {
        auto run = *it;
        auto rect = run.rect();
        quads.append(textRenderer.localToAbsoluteQuad(FloatQuad(rect), 0, wasFixed));
    }
    return quads;
}

antti@apple.com's avatar
antti@apple.com committed
181 182
}
}