SimpleLineLayoutFlowContents.cpp [plain text]
#include "config.h"
#include "SimpleLineLayoutFlowContents.h"
#include "RenderBlockFlow.h"
#include "RenderChildIterator.h"
#include "RenderLineBreak.h"
#include "RenderText.h"
namespace WebCore {
namespace SimpleLineLayout {
static Vector<FlowContents::Segment> initializeSegments(const RenderBlockFlow& flow)
{
unsigned numberOfChildren = 0;
auto children = childrenOfType<RenderObject>(flow);
for (auto it = children.begin(), end = children.end(); it != end; ++it)
++numberOfChildren;
Vector<FlowContents::Segment> segments;
segments.reserveCapacity(numberOfChildren);
unsigned startPosition = 0;
for (const auto& child : childrenOfType<RenderObject>(flow)) {
if (is<RenderText>(child)) {
const auto& textChild = downcast<RenderText>(child);
unsigned textLength = textChild.text()->length();
segments.append(FlowContents::Segment { startPosition, startPosition + textLength, textChild.text(), textChild });
startPosition += textLength;
continue;
}
if (is<RenderLineBreak>(child)) {
segments.append(FlowContents::Segment { startPosition, startPosition, String(), child });
continue;
}
ASSERT_NOT_REACHED();
}
return segments;
}
FlowContents::FlowContents(const RenderBlockFlow& flow)
: m_segments(initializeSegments(flow))
, m_lastSegmentIndex(0)
{
}
unsigned FlowContents::segmentIndexForRunSlow(unsigned start, unsigned end) const
{
auto it = std::lower_bound(m_segments.begin(), m_segments.end(), start, [](const Segment& segment, unsigned start) {
return segment.end <= start;
});
ASSERT(it != m_segments.end());
ASSERT_UNUSED(end, end <= it->end);
auto index = it - m_segments.begin();
m_lastSegmentIndex = index;
return index;
}
}
}