-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
SimpleLineLayoutResolver.cpp
312 lines (285 loc) · 11.7 KB
/
SimpleLineLayoutResolver.cpp
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* Copyright (C) 2014 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 "SimpleLineLayoutResolver.h"
#include "InlineTextBoxStyle.h"
#include "RenderBlockFlow.h"
#include "RenderObject.h"
#include "SimpleLineLayoutFunctions.h"
namespace WebCore {
namespace SimpleLineLayout {
static FloatPoint linePosition(float logicalLeft, float logicalTop)
{
return FloatPoint(logicalLeft, roundf(logicalTop));
}
static FloatSize lineSize(float logicalLeft, float logicalRight, float height)
{
return FloatSize(logicalRight - logicalLeft, height);
}
RunResolver::Run::Run(const Iterator& iterator)
: m_iterator(iterator)
{
}
String RunResolver::Run::textWithHyphen() const
{
auto& run = m_iterator.simpleRun();
ASSERT(run.hasHyphen);
// Empty runs should not have hyphen.
ASSERT(run.start < run.end);
auto& segment = m_iterator.resolver().m_flowContents.segmentForRun(run.start, run.end);
auto text = StringView(segment.text).substring(segment.toSegmentPosition(run.start), run.end - run.start);
return makeString(text, m_iterator.resolver().flow().style().hyphenString());
}
FloatRect RunResolver::Run::rect() const
{
auto& run = m_iterator.simpleRun();
auto& resolver = m_iterator.resolver();
float baseline = computeBaselinePosition();
FloatPoint position = linePosition(run.logicalLeft, baseline - resolver.m_ascent);
FloatSize size = lineSize(run.logicalLeft, run.logicalRight, resolver.m_ascent + resolver.m_descent + resolver.m_visualOverflowOffset);
bool moveLineBreakToBaseline = false;
if (run.start == run.end && m_iterator != resolver.begin() && m_iterator.inQuirksMode()) {
auto previousRun = m_iterator;
--previousRun;
moveLineBreakToBaseline = !previousRun.simpleRun().isEndOfLine;
}
if (moveLineBreakToBaseline)
return FloatRect(FloatPoint(position.x(), baseline), FloatSize(size.width(), std::max<float>(0, resolver.m_ascent - resolver.m_baseline.toFloat())));
return FloatRect(position, size);
}
StringView RunResolver::Run::text() const
{
auto& run = m_iterator.simpleRun();
ASSERT(run.start < run.end);
auto& segment = m_iterator.resolver().m_flowContents.segmentForRun(run.start, run.end);
// We currently split runs on segment boundaries (different RenderObject).
ASSERT(run.end <= segment.end);
return StringView(segment.text).substring(segment.toSegmentPosition(run.start), run.end - run.start);
}
RunResolver::Iterator::Iterator(const RunResolver& resolver, unsigned runIndex, unsigned lineIndex)
: m_resolver(resolver)
, m_runIndex(runIndex)
, m_lineIndex(lineIndex)
{
}
RunResolver::Iterator& RunResolver::Iterator::advance()
{
if (simpleRun().isEndOfLine)
++m_lineIndex;
++m_runIndex;
return *this;
}
RunResolver::Iterator& RunResolver::Iterator::advanceLines(unsigned lineCount)
{
unsigned runCount = m_resolver.m_layout.runCount();
if (runCount == m_resolver.m_layout.lineCount()) {
m_runIndex = std::min(runCount, m_runIndex + lineCount);
m_lineIndex = m_runIndex;
return *this;
}
unsigned target = m_lineIndex + lineCount;
while (m_lineIndex < target && m_runIndex < runCount)
advance();
return *this;
}
RunResolver::RunResolver(const RenderBlockFlow& flow, const Layout& layout)
: m_flowRenderer(flow)
, m_layout(layout)
, m_flowContents(flow)
, m_lineHeight(lineHeightFromFlow(flow))
, m_baseline(baselineFromFlow(flow))
, m_borderAndPaddingBefore(flow.borderAndPaddingBefore())
, m_ascent(flow.style().fontCascade().fontMetrics().ascent())
, m_descent(flow.style().fontCascade().fontMetrics().descent())
, m_visualOverflowOffset(visualOverflowForDecorations(flow.style(), nullptr).bottom)
, m_inQuirksMode(flow.document().inQuirksMode())
{
}
unsigned RunResolver::adjustLineIndexForStruts(LayoutUnit y, IndexType type, unsigned lineIndexCandidate) const
{
auto& struts = m_layout.struts();
// We need to offset the lineIndex with line struts when there's an actual strut before the candidate.
auto& strut = struts.first();
if (strut.lineBreak >= lineIndexCandidate)
return lineIndexCandidate;
unsigned strutIndex = 0;
std::optional<unsigned> lastIndexCandidate;
auto top = strut.lineBreak * m_lineHeight;
auto lineHeightWithOverflow = m_lineHeight;
// If font is larger than the line height (glyphs overflow), use the font size when checking line boundaries.
if (m_ascent + m_descent > m_lineHeight) {
lineHeightWithOverflow = m_ascent + m_descent;
top += m_baseline - m_ascent;
}
auto bottom = top + lineHeightWithOverflow;
for (auto lineIndex = strut.lineBreak; lineIndex < m_layout.lineCount(); ++lineIndex) {
float strutOffset = 0;
if (strutIndex < struts.size() && struts.at(strutIndex).lineBreak == lineIndex)
strutOffset = struts.at(strutIndex++).offset;
bottom = top + strutOffset + lineHeightWithOverflow;
if (y >= top && y < bottom) {
if (type == IndexType::First)
return lineIndex;
lastIndexCandidate = lineIndex;
} else if (lastIndexCandidate)
return *lastIndexCandidate;
top += m_lineHeight + strutOffset;
}
if (lastIndexCandidate || y >= bottom)
return m_layout.lineCount() - 1;
// We missed the line.
ASSERT_NOT_REACHED();
return lineIndexCandidate;
}
unsigned RunResolver::lineIndexForHeight(LayoutUnit height, IndexType type) const
{
ASSERT(m_lineHeight);
float y = height - m_borderAndPaddingBefore;
// Lines may overlap, adjust to get the first or last line at this height.
auto adjustedY = y;
if (type == IndexType::First)
adjustedY += m_lineHeight - (m_baseline + m_descent);
else
adjustedY -= m_baseline - m_ascent;
adjustedY = std::max<float>(adjustedY, 0);
auto lineIndexCandidate = std::min<unsigned>(adjustedY / m_lineHeight, m_layout.lineCount() - 1);
if (m_layout.hasLineStruts())
return adjustLineIndexForStruts(y, type, lineIndexCandidate);
return lineIndexCandidate;
}
WTF::IteratorRange<RunResolver::Iterator> RunResolver::rangeForRect(const LayoutRect& rect) const
{
if (!m_lineHeight)
return { begin(), end() };
unsigned firstLine = lineIndexForHeight(rect.y(), IndexType::First);
unsigned lastLine = std::max(firstLine, lineIndexForHeight(rect.maxY(), IndexType::Last));
auto rangeBegin = begin().advanceLines(firstLine);
if (rangeBegin == end())
return { end(), end() };
auto rangeEnd = rangeBegin;
ASSERT(lastLine >= firstLine);
rangeEnd.advanceLines(lastLine - firstLine + 1);
return { rangeBegin, rangeEnd };
}
WTF::IteratorRange<RunResolver::Iterator> RunResolver::rangeForRenderer(const RenderObject& renderer) const
{
if (begin() == end())
return { end(), end() };
FlowContents::Iterator segment = m_flowContents.begin();
auto run = begin();
ASSERT(segment->start <= (*run).start());
// Move run to the beginning of the segment.
while (&segment->renderer != &renderer && run != end()) {
if ((*run).start() == segment->start && (*run).end() == segment->end) {
++run;
++segment;
} else if ((*run).start() < segment->end)
++run;
else
++segment;
ASSERT(segment != m_flowContents.end());
}
// Do we actually have a run for this renderer?
// Collapsed whitespace with dedicated renderer could end up with no run at all.
if (run == end() || (segment->start != segment->end && segment->end <= (*run).start()))
return { end(), end() };
auto rangeBegin = run;
// Move beyond the end of the segment.
while (run != end() && (*run).start() < segment->end)
++run;
// Special case when segment == run.
if (run == rangeBegin)
++run;
return { rangeBegin, run };
}
RunResolver::Iterator RunResolver::runForPoint(const LayoutPoint& point) const
{
if (!m_lineHeight)
return end();
if (begin() == end())
return end();
unsigned lineIndex = lineIndexForHeight(point.y(), IndexType::Last);
auto x = point.x() - m_borderAndPaddingBefore;
auto it = begin();
it.advanceLines(lineIndex);
// Point is at the left side of the first run on this line.
if ((*it).logicalLeft() > x)
return it;
// Advance to the first candidate run on this line.
while (it != end() && (*it).logicalRight() < x && lineIndex == it.lineIndex())
++it;
// We jumped to the next line so the point is at the right side of the previous line.
if (it.lineIndex() > lineIndex)
return --it;
// Now we have a candidate run.
// Find the last run that still contains this point (taking overlapping runs with odd word spacing values into account).
while (it != end() && (*it).logicalLeft() <= x && lineIndex == it.lineIndex())
++it;
return --it;
}
WTF::IteratorRange<RunResolver::Iterator> RunResolver::rangeForRendererWithOffsets(const RenderObject& renderer, unsigned startOffset, unsigned endOffset) const
{
ASSERT(startOffset <= endOffset);
auto range = rangeForRenderer(renderer);
auto it = range.begin();
// Advance to the firt run with the start offset inside.
while (it != range.end() && (*it).end() <= startOffset)
++it;
if (it == range.end())
return { end(), end() };
auto rangeBegin = it;
// Special case empty ranges that start at the edge of the run. Apparently normal line layout include those.
if (endOffset == startOffset && (*it).start() == endOffset)
return { rangeBegin, ++it };
// Advance beyond the last run with the end offset.
while (it != range.end() && (*it).start() < endOffset)
++it;
return { rangeBegin, it };
}
LineResolver::Iterator::Iterator(RunResolver::Iterator runIterator)
: m_runIterator(runIterator)
{
}
FloatRect LineResolver::Iterator::operator*() const
{
unsigned currentLine = m_runIterator.lineIndex();
auto it = m_runIterator;
FloatRect rect = (*it).rect();
while (it.advance().lineIndex() == currentLine)
rect.unite((*it).rect());
return rect;
}
const RenderObject& LineResolver::Iterator::renderer() const
{
// FIXME: This works as long as we've got only one renderer per line.
auto run = *m_runIterator;
return m_runIterator.resolver().flowContents().segmentForRun(run.start(), run.end()).renderer;
}
LineResolver::LineResolver(const RenderBlockFlow& flow, const Layout& layout)
: m_runResolver(flow, layout)
{
}
}
}