1 | /* |
2 | * Copyright (C) 2003-2017 Apple Inc. All rights reserved. |
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 "RootInlineBox.h" |
22 | |
23 | #include "BidiResolver.h" |
24 | #include "Chrome.h" |
25 | #include "ChromeClient.h" |
26 | #include "Document.h" |
27 | #include "EllipsisBox.h" |
28 | #include "Frame.h" |
29 | #include "GraphicsContext.h" |
30 | #include "HitTestResult.h" |
31 | #include "InlineTextBox.h" |
32 | #include "LogicalSelectionOffsetCaches.h" |
33 | #include "PaintInfo.h" |
34 | #include "RenderFragmentedFlow.h" |
35 | #include "RenderInline.h" |
36 | #include "RenderLayoutState.h" |
37 | #include "RenderRubyBase.h" |
38 | #include "RenderRubyRun.h" |
39 | #include "RenderRubyText.h" |
40 | #include "RenderView.h" |
41 | #include "VerticalPositionCache.h" |
42 | #include <wtf/IsoMallocInlines.h> |
43 | |
44 | namespace WebCore { |
45 | |
46 | WTF_MAKE_ISO_ALLOCATED_IMPL(RootInlineBox); |
47 | |
48 | struct SameSizeAsRootInlineBox : public InlineFlowBox, public CanMakeWeakPtr<RootInlineBox> { |
49 | unsigned variables[7]; |
50 | void* pointers[3]; |
51 | }; |
52 | |
53 | COMPILE_ASSERT(sizeof(RootInlineBox) == sizeof(SameSizeAsRootInlineBox), RootInlineBox_should_stay_small); |
54 | |
55 | typedef WTF::HashMap<const RootInlineBox*, std::unique_ptr<EllipsisBox>> EllipsisBoxMap; |
56 | static EllipsisBoxMap* gEllipsisBoxMap; |
57 | |
58 | static ContainingFragmentMap& containingFragmentMap(RenderBlockFlow& block) |
59 | { |
60 | ASSERT(block.enclosingFragmentedFlow()); |
61 | return block.enclosingFragmentedFlow()->containingFragmentMap(); |
62 | } |
63 | |
64 | RootInlineBox::RootInlineBox(RenderBlockFlow& block) |
65 | : InlineFlowBox(block) |
66 | { |
67 | setIsHorizontal(block.isHorizontalWritingMode()); |
68 | } |
69 | |
70 | RootInlineBox::~RootInlineBox() |
71 | { |
72 | detachEllipsisBox(); |
73 | |
74 | if (blockFlow().enclosingFragmentedFlow()) |
75 | containingFragmentMap(blockFlow()).remove(this); |
76 | } |
77 | |
78 | void RootInlineBox::detachEllipsisBox() |
79 | { |
80 | if (hasEllipsisBox()) { |
81 | auto box = gEllipsisBoxMap->take(this); |
82 | box->setParent(nullptr); |
83 | setHasEllipsisBox(false); |
84 | } |
85 | } |
86 | |
87 | void RootInlineBox::clearTruncation() |
88 | { |
89 | if (hasEllipsisBox()) { |
90 | detachEllipsisBox(); |
91 | InlineFlowBox::clearTruncation(); |
92 | } |
93 | } |
94 | |
95 | bool RootInlineBox::isHyphenated() const |
96 | { |
97 | for (InlineBox* box = firstLeafChild(); box; box = box->nextLeafChild()) { |
98 | if (is<InlineTextBox>(*box) && downcast<InlineTextBox>(*box).hasHyphen()) |
99 | return true; |
100 | } |
101 | return false; |
102 | } |
103 | |
104 | int RootInlineBox::baselinePosition(FontBaseline baselineType) const |
105 | { |
106 | return renderer().baselinePosition(baselineType, isFirstLine(), isHorizontal() ? HorizontalLine : VerticalLine, PositionOfInteriorLineBoxes); |
107 | } |
108 | |
109 | LayoutUnit RootInlineBox::lineHeight() const |
110 | { |
111 | return renderer().lineHeight(isFirstLine(), isHorizontal() ? HorizontalLine : VerticalLine, PositionOfInteriorLineBoxes); |
112 | } |
113 | |
114 | bool RootInlineBox::lineCanAccommodateEllipsis(bool ltr, int blockEdge, int lineBoxEdge, int ellipsisWidth) |
115 | { |
116 | // First sanity-check the unoverflowed width of the whole line to see if there is sufficient room. |
117 | int delta = ltr ? lineBoxEdge - blockEdge : blockEdge - lineBoxEdge; |
118 | if (logicalWidth() - delta < ellipsisWidth) |
119 | return false; |
120 | |
121 | // Next iterate over all the line boxes on the line. If we find a replaced element that intersects |
122 | // then we refuse to accommodate the ellipsis. Otherwise we're ok. |
123 | return InlineFlowBox::canAccommodateEllipsis(ltr, blockEdge, ellipsisWidth); |
124 | } |
125 | |
126 | float RootInlineBox::placeEllipsis(const AtomicString& ellipsisStr, bool ltr, float blockLeftEdge, float blockRightEdge, float ellipsisWidth, InlineBox* markupBox) |
127 | { |
128 | if (!gEllipsisBoxMap) |
129 | gEllipsisBoxMap = new EllipsisBoxMap(); |
130 | |
131 | ASSERT(!hasEllipsisBox()); |
132 | auto* ellipsisBox = gEllipsisBoxMap->set(this, std::make_unique<EllipsisBox>(blockFlow(), ellipsisStr, this, ellipsisWidth - (markupBox ? markupBox->logicalWidth() : 0), logicalHeight(), y(), !prevRootBox(), isHorizontal(), markupBox)).iterator->value.get(); |
133 | setHasEllipsisBox(true); |
134 | // FIXME: Do we need an RTL version of this? |
135 | if (ltr && (x() + logicalWidth() + ellipsisWidth) <= blockRightEdge) { |
136 | ellipsisBox->setX(x() + logicalWidth()); |
137 | return logicalWidth() + ellipsisWidth; |
138 | } |
139 | |
140 | // Now attempt to find the nearest glyph horizontally and place just to the right (or left in RTL) |
141 | // of that glyph. Mark all of the objects that intersect the ellipsis box as not painting (as being |
142 | // truncated). |
143 | bool foundBox = false; |
144 | float truncatedWidth = 0; |
145 | float position = placeEllipsisBox(ltr, blockLeftEdge, blockRightEdge, ellipsisWidth, truncatedWidth, foundBox); |
146 | ellipsisBox->setX(position); |
147 | return truncatedWidth; |
148 | } |
149 | |
150 | float RootInlineBox::placeEllipsisBox(bool ltr, float blockLeftEdge, float blockRightEdge, float ellipsisWidth, float &truncatedWidth, bool& foundBox) |
151 | { |
152 | float result = InlineFlowBox::placeEllipsisBox(ltr, blockLeftEdge, blockRightEdge, ellipsisWidth, truncatedWidth, foundBox); |
153 | if (result == -1) { |
154 | result = ltr ? blockRightEdge - ellipsisWidth : blockLeftEdge; |
155 | truncatedWidth = blockRightEdge - blockLeftEdge; |
156 | } |
157 | return result; |
158 | } |
159 | |
160 | void RootInlineBox::paintEllipsisBox(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit lineTop, LayoutUnit lineBottom) const |
161 | { |
162 | if (hasEllipsisBox() && paintInfo.shouldPaintWithinRoot(renderer()) && renderer().style().visibility() == Visibility::Visible && paintInfo.phase == PaintPhase::Foreground) |
163 | ellipsisBox()->paint(paintInfo, paintOffset, lineTop, lineBottom); |
164 | } |
165 | |
166 | void RootInlineBox::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit lineTop, LayoutUnit lineBottom) |
167 | { |
168 | InlineFlowBox::paint(paintInfo, paintOffset, lineTop, lineBottom); |
169 | paintEllipsisBox(paintInfo, paintOffset, lineTop, lineBottom); |
170 | } |
171 | |
172 | bool RootInlineBox::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit lineTop, LayoutUnit lineBottom, HitTestAction hitTestAction) |
173 | { |
174 | if (hasEllipsisBox() && visibleToHitTesting()) { |
175 | if (ellipsisBox()->nodeAtPoint(request, result, locationInContainer, accumulatedOffset, lineTop, lineBottom, hitTestAction)) { |
176 | renderer().updateHitTestResult(result, locationInContainer.point() - toLayoutSize(accumulatedOffset)); |
177 | return true; |
178 | } |
179 | } |
180 | return InlineFlowBox::nodeAtPoint(request, result, locationInContainer, accumulatedOffset, lineTop, lineBottom, hitTestAction); |
181 | } |
182 | |
183 | void RootInlineBox::adjustPosition(float dx, float dy) |
184 | { |
185 | InlineFlowBox::adjustPosition(dx, dy); |
186 | LayoutUnit blockDirectionDelta = isHorizontal() ? dy : dx; // The block direction delta is a LayoutUnit. |
187 | m_lineTop += blockDirectionDelta; |
188 | m_lineBottom += blockDirectionDelta; |
189 | m_lineTopWithLeading += blockDirectionDelta; |
190 | m_lineBottomWithLeading += blockDirectionDelta; |
191 | if (hasEllipsisBox()) |
192 | ellipsisBox()->adjustPosition(dx, dy); |
193 | } |
194 | |
195 | void RootInlineBox::childRemoved(InlineBox* box) |
196 | { |
197 | if (&box->renderer() == m_lineBreakObj) |
198 | setLineBreakInfo(nullptr, 0, BidiStatus()); |
199 | |
200 | for (RootInlineBox* prev = prevRootBox(); prev && prev->lineBreakObj() == &box->renderer(); prev = prev->prevRootBox()) { |
201 | prev->setLineBreakInfo(nullptr, 0, BidiStatus()); |
202 | prev->markDirty(); |
203 | } |
204 | } |
205 | |
206 | RenderFragmentContainer* RootInlineBox::containingFragment() const |
207 | { |
208 | ContainingFragmentMap& fragmentMap = containingFragmentMap(blockFlow()); |
209 | bool hasContainingFragment = fragmentMap.contains(this); |
210 | RenderFragmentContainer* fragment = hasContainingFragment ? fragmentMap.get(this) : nullptr; |
211 | |
212 | #ifndef NDEBUG |
213 | if (hasContainingFragment) { |
214 | RenderFragmentedFlow* fragmentedFlow = blockFlow().enclosingFragmentedFlow(); |
215 | const RenderFragmentContainerList& fragmentList = fragmentedFlow->renderFragmentContainerList(); |
216 | ASSERT_WITH_SECURITY_IMPLICATION(fragmentList.contains(fragment)); |
217 | } |
218 | #endif |
219 | |
220 | return fragment; |
221 | } |
222 | |
223 | void RootInlineBox::clearContainingFragment() |
224 | { |
225 | ASSERT(!isDirty()); |
226 | |
227 | if (!containingFragmentMap(blockFlow()).contains(this)) |
228 | return; |
229 | |
230 | containingFragmentMap(blockFlow()).remove(this); |
231 | } |
232 | |
233 | void RootInlineBox::setContainingFragment(RenderFragmentContainer& fragment) |
234 | { |
235 | ASSERT(!isDirty()); |
236 | |
237 | containingFragmentMap(blockFlow()).set(this, &fragment); |
238 | } |
239 | |
240 | LayoutUnit RootInlineBox::alignBoxesInBlockDirection(LayoutUnit heightOfBlock, GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache) |
241 | { |
242 | // SVG will handle vertical alignment on its own. |
243 | if (isSVGRootInlineBox()) |
244 | return 0; |
245 | |
246 | LayoutUnit maxPositionTop; |
247 | LayoutUnit maxPositionBottom; |
248 | int maxAscent = 0; |
249 | int maxDescent = 0; |
250 | bool setMaxAscent = false; |
251 | bool setMaxDescent = false; |
252 | |
253 | // Figure out if we're in no-quirks mode. |
254 | bool noQuirksMode = renderer().document().inNoQuirksMode(); |
255 | |
256 | m_baselineType = requiresIdeographicBaseline(textBoxDataMap) ? IdeographicBaseline : AlphabeticBaseline; |
257 | |
258 | computeLogicalBoxHeights(*this, maxPositionTop, maxPositionBottom, maxAscent, maxDescent, setMaxAscent, setMaxDescent, noQuirksMode, |
259 | textBoxDataMap, baselineType(), verticalPositionCache); |
260 | |
261 | if (maxAscent + maxDescent < std::max(maxPositionTop, maxPositionBottom)) |
262 | adjustMaxAscentAndDescent(maxAscent, maxDescent, maxPositionTop, maxPositionBottom); |
263 | |
264 | LayoutUnit maxHeight = maxAscent + maxDescent; |
265 | LayoutUnit lineTop = heightOfBlock; |
266 | LayoutUnit lineBottom = heightOfBlock; |
267 | LayoutUnit lineTopIncludingMargins = heightOfBlock; |
268 | LayoutUnit lineBottomIncludingMargins = heightOfBlock; |
269 | bool setLineTop = false; |
270 | bool hasAnnotationsBefore = false; |
271 | bool hasAnnotationsAfter = false; |
272 | placeBoxesInBlockDirection(heightOfBlock, maxHeight, maxAscent, noQuirksMode, lineTop, lineBottom, setLineTop, |
273 | lineTopIncludingMargins, lineBottomIncludingMargins, hasAnnotationsBefore, hasAnnotationsAfter, baselineType()); |
274 | m_hasAnnotationsBefore = hasAnnotationsBefore; |
275 | m_hasAnnotationsAfter = hasAnnotationsAfter; |
276 | |
277 | maxHeight = std::max<LayoutUnit>(0, maxHeight); // FIXME: Is this really necessary? |
278 | |
279 | LayoutUnit lineTopWithLeading = heightOfBlock; |
280 | LayoutUnit lineBottomWithLeading = heightOfBlock + maxHeight; |
281 | setLineTopBottomPositions(lineTop, lineBottom, lineTopWithLeading, lineBottomWithLeading); |
282 | setPaginatedLineWidth(blockFlow().availableLogicalWidthForContent(heightOfBlock)); |
283 | |
284 | LayoutUnit annotationsAdjustment = beforeAnnotationsAdjustment(); |
285 | if (annotationsAdjustment) { |
286 | // FIXME: Need to handle pagination here. We might have to move to the next page/column as a result of the |
287 | // ruby expansion. |
288 | adjustBlockDirectionPosition(annotationsAdjustment); |
289 | heightOfBlock += annotationsAdjustment; |
290 | } |
291 | |
292 | LayoutUnit gridSnapAdjustment = lineSnapAdjustment(); |
293 | if (gridSnapAdjustment) { |
294 | adjustBlockDirectionPosition(gridSnapAdjustment); |
295 | heightOfBlock += gridSnapAdjustment; |
296 | } |
297 | |
298 | return heightOfBlock + maxHeight; |
299 | } |
300 | |
301 | LayoutUnit RootInlineBox::beforeAnnotationsAdjustment() const |
302 | { |
303 | LayoutUnit result; |
304 | |
305 | if (!renderer().style().isFlippedLinesWritingMode()) { |
306 | // Annotations under the previous line may push us down. |
307 | if (prevRootBox() && prevRootBox()->hasAnnotationsAfter()) |
308 | result = prevRootBox()->computeUnderAnnotationAdjustment(lineTop()); |
309 | |
310 | if (!hasAnnotationsBefore()) |
311 | return result; |
312 | |
313 | // Annotations over this line may push us further down. |
314 | LayoutUnit highestAllowedPosition = prevRootBox() ? std::min(prevRootBox()->lineBottom(), lineTop()) + result : blockFlow().borderBefore(); |
315 | result = computeOverAnnotationAdjustment(highestAllowedPosition); |
316 | } else { |
317 | // Annotations under this line may push us up. |
318 | if (hasAnnotationsBefore()) |
319 | result = computeUnderAnnotationAdjustment(prevRootBox() ? prevRootBox()->lineBottom() : blockFlow().borderBefore()); |
320 | |
321 | if (!prevRootBox() || !prevRootBox()->hasAnnotationsAfter()) |
322 | return result; |
323 | |
324 | // We have to compute the expansion for annotations over the previous line to see how much we should move. |
325 | LayoutUnit lowestAllowedPosition = std::max(prevRootBox()->lineBottom(), lineTop()) - result; |
326 | result = prevRootBox()->computeOverAnnotationAdjustment(lowestAllowedPosition); |
327 | } |
328 | |
329 | return result; |
330 | } |
331 | |
332 | LayoutUnit RootInlineBox::lineSnapAdjustment(LayoutUnit delta) const |
333 | { |
334 | // If our block doesn't have snapping turned on, do nothing. |
335 | // FIXME: Implement bounds snapping. |
336 | if (blockFlow().style().lineSnap() == LineSnap::None) |
337 | return 0; |
338 | |
339 | // Get the current line grid and offset. |
340 | auto* layoutState = blockFlow().view().frameView().layoutContext().layoutState(); |
341 | RenderBlockFlow* lineGrid = layoutState->lineGrid(); |
342 | LayoutSize lineGridOffset = layoutState->lineGridOffset(); |
343 | if (!lineGrid || lineGrid->style().writingMode() != blockFlow().style().writingMode()) |
344 | return 0; |
345 | |
346 | // Get the hypothetical line box used to establish the grid. |
347 | RootInlineBox* lineGridBox = lineGrid->lineGridBox(); |
348 | if (!lineGridBox) |
349 | return 0; |
350 | |
351 | LayoutUnit lineGridBlockOffset = lineGrid->isHorizontalWritingMode() ? lineGridOffset.height() : lineGridOffset.width(); |
352 | LayoutUnit blockOffset = blockFlow().isHorizontalWritingMode() ? layoutState->layoutOffset().height() : layoutState->layoutOffset().width(); |
353 | |
354 | // Now determine our position on the grid. Our baseline needs to be adjusted to the nearest baseline multiple |
355 | // as established by the line box. |
356 | // FIXME: Need to handle crazy line-box-contain values that cause the root line box to not be considered. I assume |
357 | // the grid should honor line-box-contain. |
358 | LayoutUnit gridLineHeight = lineGridBox->lineBottomWithLeading() - lineGridBox->lineTopWithLeading(); |
359 | if (!gridLineHeight) |
360 | return 0; |
361 | |
362 | LayoutUnit lineGridFontAscent = lineGrid->style().fontMetrics().ascent(baselineType()); |
363 | LayoutUnit lineGridFontHeight = lineGridBox->logicalHeight(); |
364 | LayoutUnit firstTextTop = lineGridBlockOffset + lineGridBox->logicalTop(); |
365 | LayoutUnit firstLineTopWithLeading = lineGridBlockOffset + lineGridBox->lineTopWithLeading(); |
366 | LayoutUnit firstBaselinePosition = firstTextTop + lineGridFontAscent; |
367 | |
368 | LayoutUnit currentTextTop = blockOffset + logicalTop() + delta; |
369 | LayoutUnit currentFontAscent = blockFlow().style().fontMetrics().ascent(baselineType()); |
370 | LayoutUnit currentBaselinePosition = currentTextTop + currentFontAscent; |
371 | |
372 | LayoutUnit = isHorizontal() ? layoutState->lineGridPaginationOrigin().height() : layoutState->lineGridPaginationOrigin().width(); |
373 | |
374 | // If we're paginated, see if we're on a page after the first one. If so, the grid resets on subsequent pages. |
375 | // FIXME: If the grid is an ancestor of the pagination establisher, then this is incorrect. |
376 | LayoutUnit pageLogicalTop; |
377 | if (layoutState->isPaginated() && layoutState->pageLogicalHeight()) { |
378 | pageLogicalTop = blockFlow().pageLogicalTopForOffset(lineTopWithLeading() + delta); |
379 | if (pageLogicalTop > firstLineTopWithLeading) |
380 | firstTextTop = pageLogicalTop + lineGridBox->logicalTop() - lineGrid->borderAndPaddingBefore() + lineGridPaginationOrigin; |
381 | } |
382 | |
383 | if (blockFlow().style().lineSnap() == LineSnap::Contain) { |
384 | // Compute the desired offset from the text-top of a grid line. |
385 | // Look at our height (logicalHeight()). |
386 | // Look at the total available height. It's going to be (textBottom - textTop) + (n-1)*(multiple with leading) |
387 | // where n is number of grid lines required to enclose us. |
388 | if (logicalHeight() <= lineGridFontHeight) |
389 | firstTextTop += (lineGridFontHeight - logicalHeight()) / 2; |
390 | else { |
391 | LayoutUnit numberOfLinesWithLeading = ceilf(static_cast<float>(logicalHeight() - lineGridFontHeight) / gridLineHeight); |
392 | LayoutUnit totalHeight = lineGridFontHeight + numberOfLinesWithLeading * gridLineHeight; |
393 | firstTextTop += (totalHeight - logicalHeight()) / 2; |
394 | } |
395 | firstBaselinePosition = firstTextTop + currentFontAscent; |
396 | } else |
397 | firstBaselinePosition = firstTextTop + lineGridFontAscent; |
398 | |
399 | // If we're above the first line, just push to the first line. |
400 | if (currentBaselinePosition < firstBaselinePosition) |
401 | return delta + firstBaselinePosition - currentBaselinePosition; |
402 | |
403 | // Otherwise we're in the middle of the grid somewhere. Just push to the next line. |
404 | LayoutUnit baselineOffset = currentBaselinePosition - firstBaselinePosition; |
405 | LayoutUnit remainder = roundToInt(baselineOffset) % roundToInt(gridLineHeight); |
406 | LayoutUnit result = delta; |
407 | if (remainder) |
408 | result += gridLineHeight - remainder; |
409 | |
410 | // If we aren't paginated we can return the result. |
411 | if (!layoutState->isPaginated() || !layoutState->pageLogicalHeight() || result == delta) |
412 | return result; |
413 | |
414 | // We may end up shifted to a new page. We need to do a re-snap when that happens. |
415 | LayoutUnit newPageLogicalTop = blockFlow().pageLogicalTopForOffset(lineBottomWithLeading() + result); |
416 | if (newPageLogicalTop == pageLogicalTop) |
417 | return result; |
418 | |
419 | // Put ourselves at the top of the next page to force a snap onto the new grid established by that page. |
420 | return lineSnapAdjustment(newPageLogicalTop - (blockOffset + lineTopWithLeading())); |
421 | } |
422 | |
423 | GapRects RootInlineBox::lineSelectionGap(RenderBlock& rootBlock, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock, |
424 | LayoutUnit selTop, LayoutUnit selHeight, const LogicalSelectionOffsetCaches& cache, const PaintInfo* paintInfo) |
425 | { |
426 | RenderObject::SelectionState lineState = selectionState(); |
427 | |
428 | bool leftGap, rightGap; |
429 | blockFlow().getSelectionGapInfo(lineState, leftGap, rightGap); |
430 | |
431 | GapRects result; |
432 | |
433 | InlineBox* firstBox = firstSelectedBox(); |
434 | InlineBox* lastBox = lastSelectedBox(); |
435 | if (leftGap) { |
436 | result.uniteLeft(blockFlow().logicalLeftSelectionGap(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock, &firstBox->parent()->renderer(), firstBox->logicalLeft(), |
437 | selTop, selHeight, cache, paintInfo)); |
438 | } |
439 | if (rightGap) { |
440 | result.uniteRight(blockFlow().logicalRightSelectionGap(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock, &lastBox->parent()->renderer(), lastBox->logicalRight(), |
441 | selTop, selHeight, cache, paintInfo)); |
442 | } |
443 | |
444 | // When dealing with bidi text, a non-contiguous selection region is possible. |
445 | // e.g. The logical text aaaAAAbbb (capitals denote RTL text and non-capitals LTR) is layed out |
446 | // visually as 3 text runs |aaa|bbb|AAA| if we select 4 characters from the start of the text the |
447 | // selection will look like (underline denotes selection): |
448 | // |aaa|bbb|AAA| |
449 | // ___ _ |
450 | // We can see that the |bbb| run is not part of the selection while the runs around it are. |
451 | if (firstBox && firstBox != lastBox) { |
452 | // Now fill in any gaps on the line that occurred between two selected elements. |
453 | LayoutUnit lastLogicalLeft = firstBox->logicalRight(); |
454 | bool isPreviousBoxSelected = firstBox->selectionState() != RenderObject::SelectionNone; |
455 | for (InlineBox* box = firstBox->nextLeafChild(); box; box = box->nextLeafChild()) { |
456 | if (box->selectionState() != RenderObject::SelectionNone) { |
457 | LayoutRect logicalRect(lastLogicalLeft, selTop, box->logicalLeft() - lastLogicalLeft, selHeight); |
458 | logicalRect.move(renderer().isHorizontalWritingMode() ? offsetFromRootBlock : LayoutSize(offsetFromRootBlock.height(), offsetFromRootBlock.width())); |
459 | LayoutRect gapRect = rootBlock.logicalRectToPhysicalRect(rootBlockPhysicalPosition, logicalRect); |
460 | if (isPreviousBoxSelected && gapRect.width() > 0 && gapRect.height() > 0) { |
461 | if (paintInfo && box->parent()->renderer().style().visibility() == Visibility::Visible) |
462 | paintInfo->context().fillRect(gapRect, box->parent()->renderer().selectionBackgroundColor()); |
463 | // VisibleSelection may be non-contiguous, see comment above. |
464 | result.uniteCenter(gapRect); |
465 | } |
466 | lastLogicalLeft = box->logicalRight(); |
467 | } |
468 | if (box == lastBox) |
469 | break; |
470 | isPreviousBoxSelected = box->selectionState() != RenderObject::SelectionNone; |
471 | } |
472 | } |
473 | |
474 | return result; |
475 | } |
476 | |
477 | IntRect RootInlineBox::computeCaretRect(float logicalLeftPosition, unsigned caretWidth, LayoutUnit* ) const |
478 | { |
479 | int height = selectionHeight(); |
480 | int top = selectionTop(); |
481 | |
482 | // Distribute the caret's width to either side of the offset. |
483 | float left = logicalLeftPosition; |
484 | int caretWidthLeftOfOffset = caretWidth / 2; |
485 | left -= caretWidthLeftOfOffset; |
486 | int caretWidthRightOfOffset = caretWidth - caretWidthLeftOfOffset; |
487 | left = roundf(left); |
488 | |
489 | float rootLeft = logicalLeft(); |
490 | float rootRight = logicalRight(); |
491 | |
492 | if (extraWidthToEndOfLine) |
493 | *extraWidthToEndOfLine = (logicalWidth() + rootLeft) - (left + caretWidth); |
494 | |
495 | const RenderStyle& blockStyle = blockFlow().style(); |
496 | |
497 | bool rightAligned = false; |
498 | switch (blockStyle.textAlign()) { |
499 | case TextAlignMode::Right: |
500 | case TextAlignMode::WebKitRight: |
501 | rightAligned = true; |
502 | break; |
503 | case TextAlignMode::Left: |
504 | case TextAlignMode::WebKitLeft: |
505 | case TextAlignMode::Center: |
506 | case TextAlignMode::WebKitCenter: |
507 | break; |
508 | case TextAlignMode::Justify: |
509 | case TextAlignMode::Start: |
510 | rightAligned = !blockStyle.isLeftToRightDirection(); |
511 | break; |
512 | case TextAlignMode::End: |
513 | rightAligned = blockStyle.isLeftToRightDirection(); |
514 | break; |
515 | } |
516 | |
517 | float leftEdge = std::min<float>(0, rootLeft); |
518 | float rightEdge = std::max<float>(blockFlow().logicalWidth(), rootRight); |
519 | |
520 | if (rightAligned) { |
521 | left = std::max(left, leftEdge); |
522 | left = std::min(left, rootRight - caretWidth); |
523 | } else { |
524 | left = std::min(left, rightEdge - caretWidthRightOfOffset); |
525 | left = std::max(left, rootLeft); |
526 | } |
527 | return blockStyle.isHorizontalWritingMode() ? IntRect(left, top, caretWidth, height) : IntRect(top, left, height, caretWidth); |
528 | } |
529 | |
530 | RenderObject::SelectionState RootInlineBox::selectionState() |
531 | { |
532 | // Walk over all of the selected boxes. |
533 | RenderObject::SelectionState state = RenderObject::SelectionNone; |
534 | for (InlineBox* box = firstLeafChild(); box; box = box->nextLeafChild()) { |
535 | RenderObject::SelectionState boxState = box->selectionState(); |
536 | if ((boxState == RenderObject::SelectionStart && state == RenderObject::SelectionEnd) || |
537 | (boxState == RenderObject::SelectionEnd && state == RenderObject::SelectionStart)) |
538 | state = RenderObject::SelectionBoth; |
539 | else if (state == RenderObject::SelectionNone || |
540 | ((boxState == RenderObject::SelectionStart || boxState == RenderObject::SelectionEnd) && |
541 | (state == RenderObject::SelectionNone || state == RenderObject::SelectionInside))) |
542 | state = boxState; |
543 | else if (boxState == RenderObject::SelectionNone && state == RenderObject::SelectionStart) { |
544 | // We are past the end of the selection. |
545 | state = RenderObject::SelectionBoth; |
546 | } |
547 | if (state == RenderObject::SelectionBoth) |
548 | break; |
549 | } |
550 | |
551 | return state; |
552 | } |
553 | |
554 | InlineBox* RootInlineBox::firstSelectedBox() |
555 | { |
556 | for (auto* box = firstLeafChild(); box; box = box->nextLeafChild()) { |
557 | if (box->selectionState() != RenderObject::SelectionNone) |
558 | return box; |
559 | } |
560 | return nullptr; |
561 | } |
562 | |
563 | InlineBox* RootInlineBox::lastSelectedBox() |
564 | { |
565 | for (auto* box = lastLeafChild(); box; box = box->prevLeafChild()) { |
566 | if (box->selectionState() != RenderObject::SelectionNone) |
567 | return box; |
568 | } |
569 | return nullptr; |
570 | } |
571 | |
572 | LayoutUnit RootInlineBox::selectionTop() const |
573 | { |
574 | LayoutUnit selectionTop = m_lineTop; |
575 | |
576 | if (m_hasAnnotationsBefore) |
577 | selectionTop -= !renderer().style().isFlippedLinesWritingMode() ? computeOverAnnotationAdjustment(m_lineTop) : computeUnderAnnotationAdjustment(m_lineTop); |
578 | |
579 | if (renderer().style().isFlippedLinesWritingMode()) |
580 | return selectionTop; |
581 | |
582 | #if !PLATFORM(IOS_FAMILY) |
583 | // See rdar://problem/19692206 ... don't want to do this adjustment for iOS where overlap is ok and handled. |
584 | if (renderer().isRubyBase()) { |
585 | // The ruby base selection should avoid intruding into the ruby text. This is only the case if there is an actual ruby text above us. |
586 | RenderRubyBase* base = &downcast<RenderRubyBase>(renderer()); |
587 | RenderRubyRun* run = base->rubyRun(); |
588 | if (run) { |
589 | RenderRubyText* text = run->rubyText(); |
590 | if (text && text->logicalTop() < base->logicalTop()) { |
591 | // The ruby text is above the ruby base. Just return now in order to avoid painting on top of the ruby text. |
592 | return selectionTop; |
593 | } |
594 | } |
595 | } else if (renderer().isRubyText()) { |
596 | // The ruby text selection should go all the way to the selection top of the containing line. |
597 | RenderRubyText* text = &downcast<RenderRubyText>(renderer()); |
598 | RenderRubyRun* run = text->rubyRun(); |
599 | if (run && run->inlineBoxWrapper()) { |
600 | RenderRubyBase* base = run->rubyBase(); |
601 | if (base && text->logicalTop() < base->logicalTop()) { |
602 | // The ruby text is above the ruby base. |
603 | const RootInlineBox& containingLine = run->inlineBoxWrapper()->root(); |
604 | LayoutUnit enclosingSelectionTop = containingLine.selectionTop(); |
605 | LayoutUnit deltaBetweenObjects = text->logicalTop() + run->logicalTop(); |
606 | LayoutUnit selectionTopInRubyTextCoords = enclosingSelectionTop - deltaBetweenObjects; |
607 | return std::min(selectionTop, selectionTopInRubyTextCoords); |
608 | } |
609 | } |
610 | } |
611 | #endif |
612 | |
613 | LayoutUnit prevBottom = prevRootBox() ? prevRootBox()->selectionBottom() : blockFlow().borderAndPaddingBefore(); |
614 | if (prevBottom < selectionTop && blockFlow().containsFloats()) { |
615 | // This line has actually been moved further down, probably from a large line-height, but possibly because the |
616 | // line was forced to clear floats. If so, let's check the offsets, and only be willing to use the previous |
617 | // line's bottom if the offsets are greater on both sides. |
618 | LayoutUnit prevLeft = blockFlow().logicalLeftOffsetForLine(prevBottom, DoNotIndentText); |
619 | LayoutUnit prevRight = blockFlow().logicalRightOffsetForLine(prevBottom, DoNotIndentText); |
620 | LayoutUnit newLeft = blockFlow().logicalLeftOffsetForLine(selectionTop, DoNotIndentText); |
621 | LayoutUnit newRight = blockFlow().logicalRightOffsetForLine(selectionTop, DoNotIndentText); |
622 | if (prevLeft > newLeft || prevRight < newRight) |
623 | return selectionTop; |
624 | } |
625 | |
626 | return prevBottom; |
627 | } |
628 | |
629 | static RenderBlock* blockBeforeWithinSelectionRoot(const RenderBlockFlow& blockFlow, LayoutSize& offset) |
630 | { |
631 | if (blockFlow.isSelectionRoot()) |
632 | return nullptr; |
633 | |
634 | const RenderElement* object = &blockFlow; |
635 | RenderObject* sibling; |
636 | do { |
637 | sibling = object->previousSibling(); |
638 | while (sibling && (!is<RenderBlock>(*sibling) || downcast<RenderBlock>(*sibling).isSelectionRoot())) |
639 | sibling = sibling->previousSibling(); |
640 | |
641 | offset -= LayoutSize(downcast<RenderBlock>(*object).logicalLeft(), downcast<RenderBlock>(*object).logicalTop()); |
642 | object = object->parent(); |
643 | } while (!sibling && is<RenderBlock>(object) && !downcast<RenderBlock>(*object).isSelectionRoot()); |
644 | |
645 | if (!sibling) |
646 | return nullptr; |
647 | |
648 | RenderBlock* beforeBlock = downcast<RenderBlock>(sibling); |
649 | |
650 | offset += LayoutSize(beforeBlock->logicalLeft(), beforeBlock->logicalTop()); |
651 | |
652 | RenderObject* child = beforeBlock->lastChild(); |
653 | while (is<RenderBlock>(child)) { |
654 | beforeBlock = downcast<RenderBlock>(child); |
655 | offset += LayoutSize(beforeBlock->logicalLeft(), beforeBlock->logicalTop()); |
656 | child = beforeBlock->lastChild(); |
657 | } |
658 | return beforeBlock; |
659 | } |
660 | |
661 | LayoutUnit RootInlineBox::selectionTopAdjustedForPrecedingBlock() const |
662 | { |
663 | const RootInlineBox& rootBox = root(); |
664 | LayoutUnit top = selectionTop(); |
665 | |
666 | auto blockSelectionState = rootBox.blockFlow().selectionState(); |
667 | if (blockSelectionState != RenderObject::SelectionInside && blockSelectionState != RenderObject::SelectionEnd) |
668 | return top; |
669 | |
670 | LayoutSize offsetToBlockBefore; |
671 | auto* blockBefore = blockBeforeWithinSelectionRoot(rootBox.blockFlow(), offsetToBlockBefore); |
672 | if (!is<RenderBlockFlow>(blockBefore)) |
673 | return top; |
674 | |
675 | // Do not adjust blocks sharing the same line. |
676 | if (!offsetToBlockBefore.height()) |
677 | return top; |
678 | |
679 | if (auto* lastLine = downcast<RenderBlockFlow>(*blockBefore).lastRootBox()) { |
680 | RenderObject::SelectionState lastLineSelectionState = lastLine->selectionState(); |
681 | if (lastLineSelectionState != RenderObject::SelectionInside && lastLineSelectionState != RenderObject::SelectionStart) |
682 | return top; |
683 | |
684 | LayoutUnit lastLineSelectionBottom = lastLine->selectionBottom() + offsetToBlockBefore.height(); |
685 | top = std::max(top, lastLineSelectionBottom); |
686 | } |
687 | return top; |
688 | } |
689 | |
690 | LayoutUnit RootInlineBox::selectionBottom() const |
691 | { |
692 | LayoutUnit selectionBottom = m_lineBottom; |
693 | |
694 | if (m_hasAnnotationsAfter) |
695 | selectionBottom += !renderer().style().isFlippedLinesWritingMode() ? computeUnderAnnotationAdjustment(m_lineBottom) : computeOverAnnotationAdjustment(m_lineBottom); |
696 | |
697 | if (!renderer().style().isFlippedLinesWritingMode() || !nextRootBox()) |
698 | return selectionBottom; |
699 | |
700 | #if !PLATFORM(IOS_FAMILY) |
701 | // See rdar://problem/19692206 ... don't want to do this adjustment for iOS where overlap is ok and handled. |
702 | if (renderer().isRubyBase()) { |
703 | // The ruby base selection should avoid intruding into the ruby text. This is only the case if there is an actual ruby text below us. |
704 | RenderRubyBase* base = &downcast<RenderRubyBase>(renderer()); |
705 | RenderRubyRun* run = base->rubyRun(); |
706 | if (run) { |
707 | RenderRubyText* text = run->rubyText(); |
708 | if (text && text->logicalTop() > base->logicalTop()) { |
709 | // The ruby text is below the ruby base. Just return now in order to avoid painting on top of the ruby text. |
710 | return selectionBottom; |
711 | } |
712 | } |
713 | } else if (renderer().isRubyText()) { |
714 | // The ruby text selection should go all the way to the selection bottom of the containing line. |
715 | RenderRubyText* text = &downcast<RenderRubyText>(renderer()); |
716 | RenderRubyRun* run = text->rubyRun(); |
717 | if (run && run->inlineBoxWrapper()) { |
718 | RenderRubyBase* base = run->rubyBase(); |
719 | if (base && text->logicalTop() > base->logicalTop()) { |
720 | // The ruby text is above the ruby base. |
721 | const RootInlineBox& containingLine = run->inlineBoxWrapper()->root(); |
722 | LayoutUnit enclosingSelectionBottom = containingLine.selectionBottom(); |
723 | LayoutUnit deltaBetweenObjects = text->logicalTop() + run->logicalTop(); |
724 | LayoutUnit selectionBottomInRubyTextCoords = enclosingSelectionBottom - deltaBetweenObjects; |
725 | return std::min(selectionBottom, selectionBottomInRubyTextCoords); |
726 | } |
727 | } |
728 | } |
729 | #endif |
730 | |
731 | LayoutUnit nextTop = nextRootBox()->selectionTop(); |
732 | if (nextTop > selectionBottom && blockFlow().containsFloats()) { |
733 | // The next line has actually been moved further over, probably from a large line-height, but possibly because the |
734 | // line was forced to clear floats. If so, let's check the offsets, and only be willing to use the next |
735 | // line's top if the offsets are greater on both sides. |
736 | LayoutUnit nextLeft = blockFlow().logicalLeftOffsetForLine(nextTop, DoNotIndentText); |
737 | LayoutUnit nextRight = blockFlow().logicalRightOffsetForLine(nextTop, DoNotIndentText); |
738 | LayoutUnit newLeft = blockFlow().logicalLeftOffsetForLine(selectionBottom, DoNotIndentText); |
739 | LayoutUnit newRight = blockFlow().logicalRightOffsetForLine(selectionBottom, DoNotIndentText); |
740 | if (nextLeft > newLeft || nextRight < newRight) |
741 | return selectionBottom; |
742 | } |
743 | |
744 | return nextTop; |
745 | } |
746 | |
747 | int RootInlineBox::blockDirectionPointInLine() const |
748 | { |
749 | return !blockFlow().style().isFlippedBlocksWritingMode() ? std::max(lineTop(), selectionTop()) : std::min(lineBottom(), selectionBottom()); |
750 | } |
751 | |
752 | RenderBlockFlow& RootInlineBox::blockFlow() const |
753 | { |
754 | return downcast<RenderBlockFlow>(renderer()); |
755 | } |
756 | |
757 | static bool isEditableLeaf(InlineBox* leaf) |
758 | { |
759 | return leaf && leaf->renderer().node() && leaf->renderer().node()->hasEditableStyle(); |
760 | } |
761 | |
762 | InlineBox* RootInlineBox::closestLeafChildForPoint(const IntPoint& pointInContents, bool onlyEditableLeaves) |
763 | { |
764 | return closestLeafChildForLogicalLeftPosition(blockFlow().isHorizontalWritingMode() ? pointInContents.x() : pointInContents.y(), onlyEditableLeaves); |
765 | } |
766 | |
767 | InlineBox* RootInlineBox::closestLeafChildForLogicalLeftPosition(int leftPosition, bool onlyEditableLeaves) |
768 | { |
769 | InlineBox* firstLeaf = firstLeafChild(); |
770 | InlineBox* lastLeaf = lastLeafChild(); |
771 | |
772 | if (firstLeaf != lastLeaf) { |
773 | if (firstLeaf->isLineBreak()) |
774 | firstLeaf = firstLeaf->nextLeafChildIgnoringLineBreak(); |
775 | else if (lastLeaf->isLineBreak()) |
776 | lastLeaf = lastLeaf->prevLeafChildIgnoringLineBreak(); |
777 | } |
778 | |
779 | if (firstLeaf == lastLeaf && (!onlyEditableLeaves || isEditableLeaf(firstLeaf))) |
780 | return firstLeaf; |
781 | |
782 | // Avoid returning a list marker when possible. |
783 | if (firstLeaf && leftPosition <= firstLeaf->logicalLeft() && !firstLeaf->renderer().isListMarker() && (!onlyEditableLeaves || isEditableLeaf(firstLeaf))) |
784 | // The leftPosition coordinate is less or equal to left edge of the firstLeaf. |
785 | // Return it. |
786 | return firstLeaf; |
787 | |
788 | if (lastLeaf && leftPosition >= lastLeaf->logicalRight() && !lastLeaf->renderer().isListMarker() && (!onlyEditableLeaves || isEditableLeaf(lastLeaf))) |
789 | // The leftPosition coordinate is greater or equal to right edge of the lastLeaf. |
790 | // Return it. |
791 | return lastLeaf; |
792 | |
793 | InlineBox* closestLeaf = nullptr; |
794 | for (InlineBox* leaf = firstLeaf; leaf; leaf = leaf->nextLeafChildIgnoringLineBreak()) { |
795 | if (!leaf->renderer().isListMarker() && (!onlyEditableLeaves || isEditableLeaf(leaf))) { |
796 | closestLeaf = leaf; |
797 | if (leftPosition < leaf->logicalRight()) |
798 | // The x coordinate is less than the right edge of the box. |
799 | // Return it. |
800 | return leaf; |
801 | } |
802 | } |
803 | |
804 | return closestLeaf ? closestLeaf : lastLeaf; |
805 | } |
806 | |
807 | BidiStatus RootInlineBox::lineBreakBidiStatus() const |
808 | { |
809 | return { static_cast<UCharDirection>(m_lineBreakBidiStatusEor), static_cast<UCharDirection>(m_lineBreakBidiStatusLastStrong), static_cast<UCharDirection>(m_lineBreakBidiStatusLast), m_lineBreakContext.copyRef() }; |
810 | } |
811 | |
812 | void RootInlineBox::setLineBreakInfo(RenderObject* object, unsigned breakPosition, const BidiStatus& status) |
813 | { |
814 | m_lineBreakObj = makeWeakPtr(object); |
815 | m_lineBreakPos = breakPosition; |
816 | m_lineBreakBidiStatusEor = status.eor; |
817 | m_lineBreakBidiStatusLastStrong = status.lastStrong; |
818 | m_lineBreakBidiStatusLast = status.last; |
819 | m_lineBreakContext = status.context; |
820 | } |
821 | |
822 | EllipsisBox* RootInlineBox::ellipsisBox() const |
823 | { |
824 | if (!hasEllipsisBox()) |
825 | return nullptr; |
826 | return gEllipsisBoxMap->get(this); |
827 | } |
828 | |
829 | void RootInlineBox::removeLineBoxFromRenderObject() |
830 | { |
831 | blockFlow().lineBoxes().removeLineBox(this); |
832 | } |
833 | |
834 | void RootInlineBox::() |
835 | { |
836 | blockFlow().lineBoxes().extractLineBox(this); |
837 | } |
838 | |
839 | void RootInlineBox::attachLineBoxToRenderObject() |
840 | { |
841 | blockFlow().lineBoxes().attachLineBox(this); |
842 | } |
843 | |
844 | LayoutRect RootInlineBox::paddedLayoutOverflowRect(LayoutUnit endPadding) const |
845 | { |
846 | LayoutRect lineLayoutOverflow = layoutOverflowRect(lineTop(), lineBottom()); |
847 | if (!endPadding) |
848 | return lineLayoutOverflow; |
849 | |
850 | if (isHorizontal()) { |
851 | if (isLeftToRightDirection()) |
852 | lineLayoutOverflow.shiftMaxXEdgeTo(std::max<LayoutUnit>(lineLayoutOverflow.maxX(), logicalRight() + endPadding)); |
853 | else |
854 | lineLayoutOverflow.shiftXEdgeTo(std::min<LayoutUnit>(lineLayoutOverflow.x(), logicalLeft() - endPadding)); |
855 | } else { |
856 | if (isLeftToRightDirection()) |
857 | lineLayoutOverflow.shiftMaxYEdgeTo(std::max<LayoutUnit>(lineLayoutOverflow.maxY(), logicalRight() + endPadding)); |
858 | else |
859 | lineLayoutOverflow.shiftYEdgeTo(std::min<LayoutUnit>(lineLayoutOverflow.y(), logicalLeft() - endPadding)); |
860 | } |
861 | |
862 | return lineLayoutOverflow; |
863 | } |
864 | |
865 | static void setAscentAndDescent(int& ascent, int& descent, int newAscent, int newDescent, bool& ascentDescentSet) |
866 | { |
867 | if (!ascentDescentSet) { |
868 | ascentDescentSet = true; |
869 | ascent = newAscent; |
870 | descent = newDescent; |
871 | } else { |
872 | ascent = std::max(ascent, newAscent); |
873 | descent = std::max(descent, newDescent); |
874 | } |
875 | } |
876 | |
877 | void RootInlineBox::ascentAndDescentForBox(InlineBox& box, GlyphOverflowAndFallbackFontsMap& textBoxDataMap, int& ascent, int& descent, |
878 | bool& affectsAscent, bool& affectsDescent) const |
879 | { |
880 | bool ascentDescentSet = false; |
881 | |
882 | // Replaced boxes will return 0 for the line-height if line-box-contain says they are |
883 | // not to be included. |
884 | if (box.renderer().isReplaced()) { |
885 | if (lineStyle().lineBoxContain() & LineBoxContainReplaced) { |
886 | ascent = box.baselinePosition(baselineType()); |
887 | descent = box.lineHeight() - ascent; |
888 | |
889 | // Replaced elements always affect both the ascent and descent. |
890 | affectsAscent = true; |
891 | affectsDescent = true; |
892 | } |
893 | return; |
894 | } |
895 | |
896 | Vector<const Font*>* usedFonts = nullptr; |
897 | GlyphOverflow* glyphOverflow = nullptr; |
898 | if (is<InlineTextBox>(box)) { |
899 | GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.find(&downcast<InlineTextBox>(box)); |
900 | usedFonts = it == textBoxDataMap.end() ? nullptr : &it->value.first; |
901 | glyphOverflow = it == textBoxDataMap.end() ? nullptr : &it->value.second; |
902 | } |
903 | |
904 | bool includeLeading = includeLeadingForBox(box); |
905 | bool includeFont = includeFontForBox(box); |
906 | |
907 | bool setUsedFont = false; |
908 | bool setUsedFontWithLeading = false; |
909 | |
910 | const RenderStyle& boxLineStyle = box.lineStyle(); |
911 | if (usedFonts && !usedFonts->isEmpty() && (includeFont || (boxLineStyle.lineHeight().isNegative() && includeLeading))) { |
912 | usedFonts->append(&boxLineStyle.fontCascade().primaryFont()); |
913 | for (auto& font : *usedFonts) { |
914 | auto& fontMetrics = font->fontMetrics(); |
915 | int usedFontAscent = fontMetrics.ascent(baselineType()); |
916 | int usedFontDescent = fontMetrics.descent(baselineType()); |
917 | int halfLeading = (fontMetrics.lineSpacing() - fontMetrics.height()) / 2; |
918 | int usedFontAscentAndLeading = usedFontAscent + halfLeading; |
919 | int usedFontDescentAndLeading = fontMetrics.lineSpacing() - usedFontAscentAndLeading; |
920 | if (includeFont) { |
921 | setAscentAndDescent(ascent, descent, usedFontAscent, usedFontDescent, ascentDescentSet); |
922 | setUsedFont = true; |
923 | } |
924 | if (includeLeading) { |
925 | setAscentAndDescent(ascent, descent, usedFontAscentAndLeading, usedFontDescentAndLeading, ascentDescentSet); |
926 | setUsedFontWithLeading = true; |
927 | } |
928 | if (!affectsAscent) |
929 | affectsAscent = usedFontAscent - box.logicalTop() > 0; |
930 | if (!affectsDescent) |
931 | affectsDescent = usedFontDescent + box.logicalTop() > 0; |
932 | } |
933 | } |
934 | |
935 | // If leading is included for the box, then we compute that box. |
936 | if (includeLeading && !setUsedFontWithLeading) { |
937 | int ascentWithLeading = box.baselinePosition(baselineType()); |
938 | int descentWithLeading = box.lineHeight() - ascentWithLeading; |
939 | setAscentAndDescent(ascent, descent, ascentWithLeading, descentWithLeading, ascentDescentSet); |
940 | |
941 | // Examine the font box for inline flows and text boxes to see if any part of it is above the baseline. |
942 | // If the top of our font box relative to the root box baseline is above the root box baseline, then |
943 | // we are contributing to the maxAscent value. Descent is similar. If any part of our font box is below |
944 | // the root box's baseline, then we contribute to the maxDescent value. |
945 | affectsAscent = ascentWithLeading - box.logicalTop() > 0; |
946 | affectsDescent = descentWithLeading + box.logicalTop() > 0; |
947 | } |
948 | |
949 | if (includeFontForBox(box) && !setUsedFont) { |
950 | int fontAscent = boxLineStyle.fontMetrics().ascent(baselineType()); |
951 | int fontDescent = boxLineStyle.fontMetrics().descent(baselineType()); |
952 | setAscentAndDescent(ascent, descent, fontAscent, fontDescent, ascentDescentSet); |
953 | affectsAscent = fontAscent - box.logicalTop() > 0; |
954 | affectsDescent = fontDescent + box.logicalTop() > 0; |
955 | } |
956 | |
957 | if (includeGlyphsForBox(box) && glyphOverflow && glyphOverflow->computeBounds) { |
958 | setAscentAndDescent(ascent, descent, glyphOverflow->top, glyphOverflow->bottom, ascentDescentSet); |
959 | affectsAscent = glyphOverflow->top - box.logicalTop() > 0; |
960 | affectsDescent = glyphOverflow->bottom + box.logicalTop() > 0; |
961 | glyphOverflow->top = std::min(glyphOverflow->top, std::max(0, glyphOverflow->top - boxLineStyle.fontMetrics().ascent(baselineType()))); |
962 | glyphOverflow->bottom = std::min(glyphOverflow->bottom, std::max(0, glyphOverflow->bottom - boxLineStyle.fontMetrics().descent(baselineType()))); |
963 | } |
964 | |
965 | if (includeInitialLetterForBox(box)) { |
966 | bool canUseGlyphs = glyphOverflow && glyphOverflow->computeBounds; |
967 | int letterAscent = baselineType() == AlphabeticBaseline ? boxLineStyle.fontMetrics().capHeight() : (canUseGlyphs ? glyphOverflow->top : boxLineStyle.fontMetrics().ascent(baselineType())); |
968 | int letterDescent = canUseGlyphs ? glyphOverflow->bottom : (box.isRootInlineBox() ? 0 : boxLineStyle.fontMetrics().descent(baselineType())); |
969 | setAscentAndDescent(ascent, descent, letterAscent, letterDescent, ascentDescentSet); |
970 | affectsAscent = letterAscent - box.logicalTop() > 0; |
971 | affectsDescent = letterDescent + box.logicalTop() > 0; |
972 | if (canUseGlyphs) { |
973 | glyphOverflow->top = std::min(glyphOverflow->top, std::max(0, glyphOverflow->top - boxLineStyle.fontMetrics().ascent(baselineType()))); |
974 | glyphOverflow->bottom = std::min(glyphOverflow->bottom, std::max(0, glyphOverflow->bottom - boxLineStyle.fontMetrics().descent(baselineType()))); |
975 | } |
976 | } |
977 | |
978 | if (includeMarginForBox(box)) { |
979 | LayoutUnit ascentWithMargin = boxLineStyle.fontMetrics().ascent(baselineType()); |
980 | LayoutUnit descentWithMargin = boxLineStyle.fontMetrics().descent(baselineType()); |
981 | if (box.parent() && !box.renderer().isTextOrLineBreak()) { |
982 | ascentWithMargin += box.boxModelObject()->borderAndPaddingBefore() + box.boxModelObject()->marginBefore(); |
983 | descentWithMargin += box.boxModelObject()->borderAndPaddingAfter() + box.boxModelObject()->marginAfter(); |
984 | } |
985 | setAscentAndDescent(ascent, descent, ascentWithMargin, descentWithMargin, ascentDescentSet); |
986 | |
987 | // Treat like a replaced element, since we're using the margin box. |
988 | affectsAscent = true; |
989 | affectsDescent = true; |
990 | } |
991 | } |
992 | |
993 | LayoutUnit RootInlineBox::verticalPositionForBox(InlineBox* box, VerticalPositionCache& verticalPositionCache) |
994 | { |
995 | if (box->renderer().isTextOrLineBreak()) |
996 | return box->parent()->logicalTop(); |
997 | |
998 | RenderBoxModelObject* renderer = box->boxModelObject(); |
999 | ASSERT(renderer->isInline()); |
1000 | if (!renderer->isInline()) |
1001 | return 0; |
1002 | |
1003 | // This method determines the vertical position for inline elements. |
1004 | bool firstLine = isFirstLine(); |
1005 | if (firstLine && !blockFlow().view().usesFirstLineRules()) |
1006 | firstLine = false; |
1007 | |
1008 | // Check the cache. |
1009 | bool isRenderInline = renderer->isRenderInline(); |
1010 | if (isRenderInline && !firstLine) { |
1011 | LayoutUnit cachedPosition; |
1012 | if (verticalPositionCache.get(renderer, baselineType(), cachedPosition)) |
1013 | return cachedPosition; |
1014 | } |
1015 | |
1016 | LayoutUnit verticalPosition; |
1017 | VerticalAlign verticalAlign = renderer->style().verticalAlign(); |
1018 | if (verticalAlign == VerticalAlign::Top || verticalAlign == VerticalAlign::Bottom) |
1019 | return 0; |
1020 | |
1021 | RenderElement* parent = renderer->parent(); |
1022 | if (parent->isRenderInline() && parent->style().verticalAlign() != VerticalAlign::Top && parent->style().verticalAlign() != VerticalAlign::Bottom) |
1023 | verticalPosition = box->parent()->logicalTop(); |
1024 | |
1025 | if (verticalAlign != VerticalAlign::Baseline) { |
1026 | const RenderStyle& parentLineStyle = firstLine ? parent->firstLineStyle() : parent->style(); |
1027 | const FontCascade& font = parentLineStyle.fontCascade(); |
1028 | const FontMetrics& fontMetrics = font.fontMetrics(); |
1029 | int fontSize = font.pixelSize(); |
1030 | |
1031 | LineDirectionMode lineDirection = parent->isHorizontalWritingMode() ? HorizontalLine : VerticalLine; |
1032 | |
1033 | if (verticalAlign == VerticalAlign::Sub) |
1034 | verticalPosition += fontSize / 5 + 1; |
1035 | else if (verticalAlign == VerticalAlign::Super) |
1036 | verticalPosition -= fontSize / 3 + 1; |
1037 | else if (verticalAlign == VerticalAlign::TextTop) |
1038 | verticalPosition += renderer->baselinePosition(baselineType(), firstLine, lineDirection) - fontMetrics.ascent(baselineType()); |
1039 | else if (verticalAlign == VerticalAlign::Middle) |
1040 | verticalPosition = (verticalPosition - LayoutUnit(fontMetrics.xHeight() / 2) - renderer->lineHeight(firstLine, lineDirection) / 2 + renderer->baselinePosition(baselineType(), firstLine, lineDirection)).round(); |
1041 | else if (verticalAlign == VerticalAlign::TextBottom) { |
1042 | verticalPosition += fontMetrics.descent(baselineType()); |
1043 | // lineHeight - baselinePosition is always 0 for replaced elements (except inline blocks), so don't bother wasting time in that case. |
1044 | if (!renderer->isReplaced() || renderer->isInlineBlockOrInlineTable()) |
1045 | verticalPosition -= (renderer->lineHeight(firstLine, lineDirection) - renderer->baselinePosition(baselineType(), firstLine, lineDirection)); |
1046 | } else if (verticalAlign == VerticalAlign::BaselineMiddle) |
1047 | verticalPosition += -renderer->lineHeight(firstLine, lineDirection) / 2 + renderer->baselinePosition(baselineType(), firstLine, lineDirection); |
1048 | else if (verticalAlign == VerticalAlign::Length) { |
1049 | LayoutUnit lineHeight; |
1050 | //Per http://www.w3.org/TR/CSS21/visudet.html#propdef-vertical-align: 'Percentages: refer to the 'line-height' of the element itself'. |
1051 | if (renderer->style().verticalAlignLength().isPercentOrCalculated()) |
1052 | lineHeight = renderer->style().computedLineHeight(); |
1053 | else |
1054 | lineHeight = renderer->lineHeight(firstLine, lineDirection); |
1055 | verticalPosition -= valueForLength(renderer->style().verticalAlignLength(), lineHeight); |
1056 | } |
1057 | } |
1058 | |
1059 | // Store the cached value. |
1060 | if (isRenderInline && !firstLine) |
1061 | verticalPositionCache.set(renderer, baselineType(), verticalPosition); |
1062 | |
1063 | return verticalPosition; |
1064 | } |
1065 | |
1066 | bool RootInlineBox::includeLeadingForBox(InlineBox& box) const |
1067 | { |
1068 | if (box.renderer().isReplaced() || (box.renderer().isTextOrLineBreak() && !box.behavesLikeText())) |
1069 | return false; |
1070 | |
1071 | LineBoxContain lineBoxContain = renderer().style().lineBoxContain(); |
1072 | return (lineBoxContain & LineBoxContainInline) || (&box == this && (lineBoxContain & LineBoxContainBlock)); |
1073 | } |
1074 | |
1075 | bool RootInlineBox::includeFontForBox(InlineBox& box) const |
1076 | { |
1077 | if (box.renderer().isReplaced() || (box.renderer().isTextOrLineBreak() && !box.behavesLikeText())) |
1078 | return false; |
1079 | |
1080 | if (!box.behavesLikeText() && is<InlineFlowBox>(box) && !downcast<InlineFlowBox>(box).hasTextChildren()) |
1081 | return false; |
1082 | |
1083 | LineBoxContain lineBoxContain = renderer().style().lineBoxContain(); |
1084 | return (lineBoxContain & LineBoxContainFont); |
1085 | } |
1086 | |
1087 | bool RootInlineBox::includeGlyphsForBox(InlineBox& box) const |
1088 | { |
1089 | if (box.renderer().isReplaced() || (box.renderer().isTextOrLineBreak() && !box.behavesLikeText())) |
1090 | return false; |
1091 | |
1092 | if (!box.behavesLikeText() && is<InlineFlowBox>(box) && !downcast<InlineFlowBox>(box).hasTextChildren()) |
1093 | return false; |
1094 | |
1095 | LineBoxContain lineBoxContain = renderer().style().lineBoxContain(); |
1096 | return (lineBoxContain & LineBoxContainGlyphs); |
1097 | } |
1098 | |
1099 | bool RootInlineBox::includeInitialLetterForBox(InlineBox& box) const |
1100 | { |
1101 | if (box.renderer().isReplaced() || (box.renderer().isTextOrLineBreak() && !box.behavesLikeText())) |
1102 | return false; |
1103 | |
1104 | if (!box.behavesLikeText() && is<InlineFlowBox>(box) && !downcast<InlineFlowBox>(box).hasTextChildren()) |
1105 | return false; |
1106 | |
1107 | LineBoxContain lineBoxContain = renderer().style().lineBoxContain(); |
1108 | return (lineBoxContain & LineBoxContainInitialLetter); |
1109 | } |
1110 | |
1111 | bool RootInlineBox::includeMarginForBox(InlineBox& box) const |
1112 | { |
1113 | if (box.renderer().isReplaced() || (box.renderer().isTextOrLineBreak() && !box.behavesLikeText())) |
1114 | return false; |
1115 | |
1116 | LineBoxContain lineBoxContain = renderer().style().lineBoxContain(); |
1117 | return lineBoxContain & LineBoxContainInlineBox; |
1118 | } |
1119 | |
1120 | |
1121 | bool RootInlineBox::fitsToGlyphs() const |
1122 | { |
1123 | LineBoxContain lineBoxContain = renderer().style().lineBoxContain(); |
1124 | return ((lineBoxContain & LineBoxContainGlyphs) || (lineBoxContain & LineBoxContainInitialLetter)); |
1125 | } |
1126 | |
1127 | bool RootInlineBox::includesRootLineBoxFontOrLeading() const |
1128 | { |
1129 | LineBoxContain lineBoxContain = renderer().style().lineBoxContain(); |
1130 | return (lineBoxContain & LineBoxContainBlock) || (lineBoxContain & LineBoxContainInline) || (lineBoxContain & LineBoxContainFont); |
1131 | } |
1132 | |
1133 | Node* RootInlineBox::getLogicalStartBoxWithNode(InlineBox*& startBox) const |
1134 | { |
1135 | Vector<InlineBox*> leafBoxesInLogicalOrder; |
1136 | collectLeafBoxesInLogicalOrder(leafBoxesInLogicalOrder); |
1137 | for (size_t i = 0; i < leafBoxesInLogicalOrder.size(); ++i) { |
1138 | if (leafBoxesInLogicalOrder[i]->renderer().node()) { |
1139 | startBox = leafBoxesInLogicalOrder[i]; |
1140 | return startBox->renderer().node(); |
1141 | } |
1142 | } |
1143 | startBox = nullptr; |
1144 | return nullptr; |
1145 | } |
1146 | |
1147 | Node* RootInlineBox::getLogicalEndBoxWithNode(InlineBox*& endBox) const |
1148 | { |
1149 | Vector<InlineBox*> leafBoxesInLogicalOrder; |
1150 | collectLeafBoxesInLogicalOrder(leafBoxesInLogicalOrder); |
1151 | for (size_t i = leafBoxesInLogicalOrder.size(); i > 0; --i) { |
1152 | if (leafBoxesInLogicalOrder[i - 1]->renderer().node()) { |
1153 | endBox = leafBoxesInLogicalOrder[i - 1]; |
1154 | return endBox->renderer().node(); |
1155 | } |
1156 | } |
1157 | endBox = nullptr; |
1158 | return nullptr; |
1159 | } |
1160 | |
1161 | #if ENABLE(TREE_DEBUGGING) |
1162 | const char* RootInlineBox::boxName() const |
1163 | { |
1164 | return "RootInlineBox" ; |
1165 | } |
1166 | #endif |
1167 | |
1168 | } // namespace WebCore |
1169 | |