1/*
2 * Copyright (C) 2014 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "SimpleLineLayoutFlowContents.h"
28
29#include "RenderBlockFlow.h"
30#include "RenderChildIterator.h"
31#include "RenderLineBreak.h"
32#include "RenderText.h"
33
34namespace WebCore {
35namespace SimpleLineLayout {
36
37static Vector<FlowContents::Segment> initializeSegments(const RenderBlockFlow& flow)
38{
39 unsigned numberOfChildren = 0;
40 auto children = childrenOfType<RenderObject>(flow);
41 for (auto it = children.begin(), end = children.end(); it != end; ++it)
42 ++numberOfChildren;
43 Vector<FlowContents::Segment> segments;
44 segments.reserveCapacity(numberOfChildren);
45 unsigned startPosition = 0;
46 for (auto& child : childrenOfType<RenderObject>(flow)) {
47 if (is<RenderText>(child)) {
48 auto& textChild = downcast<RenderText>(child);
49 unsigned textLength = textChild.text().length();
50 segments.append(FlowContents::Segment { startPosition, startPosition + textLength, textChild.text(),
51 textChild, textChild.canUseSimplifiedTextMeasuring() });
52 startPosition += textLength;
53 continue;
54 }
55 if (is<RenderLineBreak>(child)) {
56 segments.append(FlowContents::Segment { startPosition, startPosition, String(), child, true });
57 continue;
58 }
59 ASSERT_NOT_REACHED();
60 }
61 return segments;
62}
63
64FlowContents::FlowContents(const RenderBlockFlow& flow)
65 : m_segments(initializeSegments(flow))
66{
67}
68
69unsigned FlowContents::segmentIndexForRunSlow(unsigned start, unsigned end) const
70{
71 auto isEmptyRange = start == end;
72 auto it = std::lower_bound(m_segments.begin(), m_segments.end(), start, [isEmptyRange](const Segment& segment, unsigned start) {
73 // FIXME: This always find the first empty run (.vs subsequent <br> elements)
74 return (isEmptyRange && segment.start == segment.end) ? segment.start < start : segment.end <= start;
75 });
76 ASSERT(it != m_segments.end());
77 ASSERT(end <= it->end);
78 m_lastSegmentIndex = it - m_segments.begin();
79 return m_lastSegmentIndex;
80}
81
82}
83}
84