2 * (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 2000 Dirk Mueller (mueller@kde.org)
4 * Copyright (C) 2004-2007, 2013-2015 Apple Inc. All rights reserved.
5 * Copyright (C) 2006 Andrew Wellington (proton@wiretapped.net)
6 * Copyright (C) 2006 Graham Dennis (graham.dennis@gmail.com)
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
26 #include "RenderText.h"
28 #include "AXObjectCache.h"
29 #include "BreakLines.h"
30 #include "BreakingContext.h"
31 #include "CharacterProperties.h"
32 #include "EllipsisBox.h"
33 #include "FloatQuad.h"
35 #include "FrameView.h"
36 #include "Hyphenation.h"
37 #include "InlineTextBox.h"
39 #include "RenderBlock.h"
40 #include "RenderCombineText.h"
41 #include "RenderInline.h"
42 #include "RenderLayer.h"
43 #include "RenderView.h"
45 #include "SimpleLineLayoutFunctions.h"
47 #include <wtf/text/TextBreakIterator.h>
48 #include "TextResourceDecoder.h"
49 #include "VisiblePosition.h"
50 #include <wtf/NeverDestroyed.h>
51 #include <wtf/text/StringBuffer.h>
52 #include <wtf/text/StringBuilder.h>
53 #include <wtf/unicode/CharacterNames.h>
57 #include "EditorClient.h"
58 #include "LogicalSelectionOffsetCaches.h"
60 #include "SelectionRect.h"
64 using namespace Unicode;
68 struct SameSizeAsRenderText : public RenderObject {
69 uint32_t bitfields : 16;
70 #if ENABLE(TEXT_AUTOSIZING)
71 float candidateTextSize;
78 COMPILE_ASSERT(sizeof(RenderText) == sizeof(SameSizeAsRenderText), RenderText_should_stay_small);
80 class SecureTextTimer final : private TimerBase {
81 WTF_MAKE_FAST_ALLOCATED;
83 explicit SecureTextTimer(RenderText&);
84 void restart(unsigned offsetAfterLastTypedCharacter);
86 unsigned takeOffsetAfterLastTypedCharacter();
89 void fired() override;
90 RenderText& m_renderer;
91 unsigned m_offsetAfterLastTypedCharacter { 0 };
94 typedef HashMap<RenderText*, std::unique_ptr<SecureTextTimer>> SecureTextTimerMap;
96 static SecureTextTimerMap& secureTextTimers()
98 static NeverDestroyed<SecureTextTimerMap> map;
102 inline SecureTextTimer::SecureTextTimer(RenderText& renderer)
103 : m_renderer(renderer)
107 inline void SecureTextTimer::restart(unsigned offsetAfterLastTypedCharacter)
109 m_offsetAfterLastTypedCharacter = offsetAfterLastTypedCharacter;
110 startOneShot(m_renderer.settings().passwordEchoDurationInSeconds());
113 inline unsigned SecureTextTimer::takeOffsetAfterLastTypedCharacter()
115 unsigned offset = m_offsetAfterLastTypedCharacter;
116 m_offsetAfterLastTypedCharacter = 0;
120 void SecureTextTimer::fired()
122 ASSERT(secureTextTimers().get(&m_renderer) == this);
123 m_offsetAfterLastTypedCharacter = 0;
124 m_renderer.setText(m_renderer.text(), true /* forcing setting text as it may be masked later */);
127 static HashMap<const RenderText*, String>& originalTextMap()
129 static NeverDestroyed<HashMap<const RenderText*, String>> map;
133 void makeCapitalized(String* string, UChar previous)
135 // FIXME: Need to change this to use u_strToTitle instead of u_totitle and to consider locale.
137 if (string->isNull())
140 unsigned length = string->length();
141 const StringImpl& stringImpl = *string->impl();
143 if (length >= std::numeric_limits<unsigned>::max())
146 StringBuffer<UChar> stringWithPrevious(length + 1);
147 stringWithPrevious[0] = previous == noBreakSpace ? ' ' : previous;
148 for (unsigned i = 1; i < length + 1; i++) {
149 // Replace   with a real space since ICU no longer treats   as a word separator.
150 if (stringImpl[i - 1] == noBreakSpace)
151 stringWithPrevious[i] = ' ';
153 stringWithPrevious[i] = stringImpl[i - 1];
156 UBreakIterator* boundary = wordBreakIterator(StringView(stringWithPrevious.characters(), length + 1));
160 StringBuilder result;
161 result.reserveCapacity(length);
164 int32_t startOfWord = ubrk_first(boundary);
165 for (endOfWord = ubrk_next(boundary); endOfWord != UBRK_DONE; startOfWord = endOfWord, endOfWord = ubrk_next(boundary)) {
166 if (startOfWord) // Ignore first char of previous string
167 result.append(stringImpl[startOfWord - 1] == noBreakSpace ? noBreakSpace : u_totitle(stringWithPrevious[startOfWord]));
168 for (int i = startOfWord + 1; i < endOfWord; i++)
169 result.append(stringImpl[i - 1]);
172 *string = result.toString();
175 inline RenderText::RenderText(Node& node, const String& text)
178 , m_linesDirty(false)
179 , m_containsReversedText(false)
180 , m_isAllASCII(text.containsOnlyASCII())
181 , m_knownToHaveNoOverflowAndNoFallbackFonts(false)
182 , m_useBackslashAsYenSymbol(false)
183 , m_originalTextDiffersFromRendered(false)
184 #if ENABLE(TEXT_AUTOSIZING)
185 , m_candidateComputedTextSize(0)
193 ASSERT(!m_text.isNull());
195 m_canUseSimpleFontCodePath = computeCanUseSimpleFontCodePath();
196 view().frameView().incrementVisuallyNonEmptyCharacterCount(textLength());
199 RenderText::RenderText(Text& textNode, const String& text)
200 : RenderText(static_cast<Node&>(textNode), text)
204 RenderText::RenderText(Document& document, const String& text)
205 : RenderText(static_cast<Node&>(document), text)
209 RenderText::~RenderText()
211 if (m_originalTextDiffersFromRendered)
212 originalTextMap().remove(this);
215 const char* RenderText::renderName() const
220 Text* RenderText::textNode() const
222 return downcast<Text>(RenderObject::node());
225 bool RenderText::isTextFragment() const
230 bool RenderText::computeUseBackslashAsYenSymbol() const
232 const RenderStyle& style = this->style();
233 const auto& fontDescription = style.fontDescription();
234 if (style.fontCascade().useBackslashAsYenSymbol())
236 if (fontDescription.isSpecifiedFont())
238 const TextEncoding* encoding = document().decoder() ? &document().decoder()->encoding() : 0;
239 if (encoding && encoding->backslashAsCurrencySymbol() != '\\')
244 void RenderText::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
246 // There is no need to ever schedule repaints from a style change of a text run, since
247 // we already did this for the parent of the text run.
248 // We do have to schedule layouts, though, since a style change can force us to
250 if (diff == StyleDifferenceLayout) {
251 setNeedsLayoutAndPrefWidthsRecalc();
252 m_knownToHaveNoOverflowAndNoFallbackFonts = false;
255 const RenderStyle& newStyle = style();
256 bool needsResetText = false;
258 m_useBackslashAsYenSymbol = computeUseBackslashAsYenSymbol();
259 needsResetText = m_useBackslashAsYenSymbol;
260 // It should really be computed in the c'tor, but during construction we don't have parent yet -and RenderText style == parent()->style()
261 m_canUseSimplifiedTextMeasuring = computeCanUseSimplifiedTextMeasuring();
262 } else if (oldStyle->fontCascade().useBackslashAsYenSymbol() != newStyle.fontCascade().useBackslashAsYenSymbol()) {
263 m_useBackslashAsYenSymbol = computeUseBackslashAsYenSymbol();
264 needsResetText = true;
267 ETextTransform oldTransform = oldStyle ? oldStyle->textTransform() : TTNONE;
268 ETextSecurity oldSecurity = oldStyle ? oldStyle->textSecurity() : TSNONE;
269 if (needsResetText || oldTransform != newStyle.textTransform() || oldSecurity != newStyle.textSecurity())
270 RenderText::setText(originalText(), true);
273 void RenderText::removeAndDestroyTextBoxes()
275 if (!documentBeingDestroyed())
276 m_lineBoxes.removeAllFromParent(*this);
277 #if !ASSERT_WITH_SECURITY_IMPLICATION_DISABLED
279 m_lineBoxes.invalidateParentChildLists();
281 m_lineBoxes.deleteAll();
284 void RenderText::willBeDestroyed()
286 secureTextTimers().remove(this);
288 removeAndDestroyTextBoxes();
289 RenderObject::willBeDestroyed();
292 void RenderText::deleteLineBoxesBeforeSimpleLineLayout()
294 m_lineBoxes.deleteAll();
297 String RenderText::originalText() const
299 return m_originalTextDiffersFromRendered ? originalTextMap().get(this) : m_text;
302 void RenderText::absoluteRects(Vector<IntRect>& rects, const LayoutPoint& accumulatedOffset) const
304 if (auto* layout = simpleLineLayout()) {
305 rects.appendVector(SimpleLineLayout::collectAbsoluteRects(*this, *layout, accumulatedOffset));
308 rects.appendVector(m_lineBoxes.absoluteRects(accumulatedOffset));
311 Vector<IntRect> RenderText::absoluteRectsForRange(unsigned start, unsigned end, bool useSelectionHeight, bool* wasFixed) const
313 const_cast<RenderText&>(*this).ensureLineBoxes();
315 // Work around signed/unsigned issues. This function takes unsigneds, and is often passed UINT_MAX
316 // to mean "all the way to the end". InlineTextBox coordinates are unsigneds, so changing this
317 // function to take ints causes various internal mismatches. But selectionRect takes ints, and
318 // passing UINT_MAX to it causes trouble. Ideally we'd change selectionRect to take unsigneds, but
319 // that would cause many ripple effects, so for now we'll just clamp our unsigned parameters to INT_MAX.
320 ASSERT(end == UINT_MAX || end <= INT_MAX);
321 ASSERT(start <= INT_MAX);
322 start = std::min(start, static_cast<unsigned>(INT_MAX));
323 end = std::min(end, static_cast<unsigned>(INT_MAX));
325 return m_lineBoxes.absoluteRectsForRange(*this, start, end, useSelectionHeight, wasFixed);
329 // This function is similar in spirit to addLineBoxRects, but returns rectangles
330 // which are annotated with additional state which helps the iPhone draw selections in its unique way.
331 // Full annotations are added in this class.
332 void RenderText::collectSelectionRects(Vector<SelectionRect>& rects, unsigned start, unsigned end)
334 // FIXME: Work around signed/unsigned issues. This function takes unsigneds, and is often passed UINT_MAX
335 // to mean "all the way to the end". InlineTextBox coordinates are unsigneds, so changing this
336 // function to take ints causes various internal mismatches. But selectionRect takes ints, and
337 // passing UINT_MAX to it causes trouble. Ideally we'd change selectionRect to take unsigneds, but
338 // that would cause many ripple effects, so for now we'll just clamp our unsigned parameters to INT_MAX.
339 ASSERT(end == std::numeric_limits<unsigned>::max() || end <= std::numeric_limits<int>::max());
340 ASSERT(start <= std::numeric_limits<int>::max());
341 start = std::min(start, static_cast<unsigned>(std::numeric_limits<int>::max()));
342 end = std::min(end, static_cast<unsigned>(std::numeric_limits<int>::max()));
344 for (InlineTextBox* box = firstTextBox(); box; box = box->nextTextBox()) {
346 // Note, box->end() returns the index of the last character, not the index past it.
347 if (start <= box->start() && box->end() < end)
348 rect = box->localSelectionRect(start, end);
350 unsigned realEnd = std::min(box->end() + 1, end);
351 rect = box->localSelectionRect(start, realEnd);
356 if (box->root().isFirstAfterPageBreak()) {
357 if (box->isHorizontal())
358 rect.shiftYEdgeTo(box->root().lineTopWithLeading());
360 rect.shiftXEdgeTo(box->root().lineTopWithLeading());
363 RenderBlock* containingBlock = this->containingBlock();
364 // Map rect, extended left to leftOffset, and right to rightOffset, through transforms to get minX and maxX.
365 LogicalSelectionOffsetCaches cache(*containingBlock);
366 LayoutUnit leftOffset = containingBlock->logicalLeftSelectionOffset(*containingBlock, box->logicalTop(), cache);
367 LayoutUnit rightOffset = containingBlock->logicalRightSelectionOffset(*containingBlock, box->logicalTop(), cache);
368 LayoutRect extentsRect = rect;
369 if (box->isHorizontal()) {
370 extentsRect.setX(leftOffset);
371 extentsRect.setWidth(rightOffset - leftOffset);
373 extentsRect.setY(leftOffset);
374 extentsRect.setHeight(rightOffset - leftOffset);
376 extentsRect = localToAbsoluteQuad(FloatRect(extentsRect)).enclosingBoundingBox();
377 if (!box->isHorizontal())
378 extentsRect = extentsRect.transposedRect();
379 bool isFirstOnLine = !box->previousOnLineExists();
380 bool isLastOnLine = !box->nextOnLineExists();
381 if (containingBlock->isRubyBase() || containingBlock->isRubyText())
382 isLastOnLine = !containingBlock->containingBlock()->inlineBoxWrapper()->nextOnLineExists();
384 bool containsStart = box->start() <= start && box->end() + 1 >= start;
385 bool containsEnd = box->start() <= end && box->end() + 1 >= end;
387 bool isFixed = false;
388 IntRect absRect = localToAbsoluteQuad(FloatRect(rect), UseTransforms, &isFixed).enclosingBoundingBox();
389 bool boxIsHorizontal = !box->isSVGInlineTextBox() ? box->isHorizontal() : !style().isVerticalWritingMode();
390 // If the containing block is an inline element, we want to check the inlineBoxWrapper orientation
391 // to determine the orientation of the block. In this case we also use the inlineBoxWrapper to
392 // determine if the element is the last on the line.
393 if (containingBlock->inlineBoxWrapper()) {
394 if (containingBlock->inlineBoxWrapper()->isHorizontal() != boxIsHorizontal) {
395 boxIsHorizontal = containingBlock->inlineBoxWrapper()->isHorizontal();
396 isLastOnLine = !containingBlock->inlineBoxWrapper()->nextOnLineExists();
400 rects.append(SelectionRect(absRect, box->direction(), extentsRect.x(), extentsRect.maxX(), extentsRect.maxY(), 0, box->isLineBreak(), isFirstOnLine, isLastOnLine, containsStart, containsEnd, boxIsHorizontal, isFixed, containingBlock->isRubyText(), view().pageNumberForBlockProgressionOffset(absRect.x())));
405 Vector<FloatQuad> RenderText::absoluteQuadsClippedToEllipsis() const
407 if (auto* layout = simpleLineLayout()) {
408 ASSERT(style().textOverflow() != TextOverflowEllipsis);
409 return SimpleLineLayout::collectAbsoluteQuads(*this, *layout, nullptr);
411 return m_lineBoxes.absoluteQuads(*this, nullptr, RenderTextLineBoxes::ClipToEllipsis);
414 void RenderText::absoluteQuads(Vector<FloatQuad>& quads, bool* wasFixed) const
416 if (auto* layout = simpleLineLayout()) {
417 quads.appendVector(SimpleLineLayout::collectAbsoluteQuads(*this, *layout, wasFixed));
420 quads.appendVector(m_lineBoxes.absoluteQuads(*this, wasFixed, RenderTextLineBoxes::NoClipping));
423 Vector<FloatQuad> RenderText::absoluteQuadsForRange(unsigned start, unsigned end, bool useSelectionHeight, bool* wasFixed) const
425 // Work around signed/unsigned issues. This function takes unsigneds, and is often passed UINT_MAX
426 // to mean "all the way to the end". InlineTextBox coordinates are unsigneds, so changing this
427 // function to take ints causes various internal mismatches. But selectionRect takes ints, and
428 // passing UINT_MAX to it causes trouble. Ideally we'd change selectionRect to take unsigneds, but
429 // that would cause many ripple effects, so for now we'll just clamp our unsigned parameters to INT_MAX.
430 ASSERT(end == UINT_MAX || end <= INT_MAX);
431 ASSERT(start <= INT_MAX);
432 start = std::min(start, static_cast<unsigned>(INT_MAX));
433 end = std::min(end, static_cast<unsigned>(INT_MAX));
434 if (simpleLineLayout() && !useSelectionHeight)
435 return collectAbsoluteQuadsForRange(*this, start, end, *simpleLineLayout(), wasFixed);
436 const_cast<RenderText&>(*this).ensureLineBoxes();
437 return m_lineBoxes.absoluteQuadsForRange(*this, start, end, useSelectionHeight, wasFixed);
440 Position RenderText::positionForPoint(const LayoutPoint& point)
442 if (auto* layout = simpleLineLayout()) {
443 auto position = Position(textNode(), SimpleLineLayout::textOffsetForPoint(point, *this, *layout));
444 ASSERT(position == positionForPoint(point, nullptr).deepEquivalent());
447 return positionForPoint(point, nullptr).deepEquivalent();
450 VisiblePosition RenderText::positionForPoint(const LayoutPoint& point, const RenderRegion*)
453 return m_lineBoxes.positionForPoint(*this, point);
456 LayoutRect RenderText::localCaretRect(InlineBox* inlineBox, unsigned caretOffset, LayoutUnit* extraWidthToEndOfLine)
461 auto& box = downcast<InlineTextBox>(*inlineBox);
462 float left = box.positionForOffset(caretOffset);
463 return box.root().computeCaretRect(left, caretWidth, extraWidthToEndOfLine);
466 ALWAYS_INLINE float RenderText::widthFromCache(const FontCascade& f, unsigned start, unsigned len, float xPos, HashSet<const Font*>* fallbackFonts, GlyphOverflow* glyphOverflow, const RenderStyle& style) const
468 if (style.hasTextCombine() && is<RenderCombineText>(*this)) {
469 const RenderCombineText& combineText = downcast<RenderCombineText>(*this);
470 if (combineText.isCombined())
471 return combineText.combinedTextWidth(f);
474 if (f.isFixedPitch() && f.fontDescription().variantSettings().isAllNormal() && m_isAllASCII && (!glyphOverflow || !glyphOverflow->computeBounds)) {
475 float monospaceCharacterWidth = f.spaceWidth();
479 StringImpl& text = *m_text.impl();
480 for (unsigned i = start; i < start + len; i++) {
483 if (c == ' ' || c == '\n') {
484 w += monospaceCharacterWidth;
486 } else if (c == '\t') {
487 if (style.collapseWhiteSpace()) {
488 w += monospaceCharacterWidth;
491 w += f.tabWidth(style.tabSize(), xPos + w);
497 w += monospaceCharacterWidth;
500 if (isSpace && i > start)
501 w += f.wordSpacing();
506 TextRun run = RenderBlock::constructTextRun(*this, start, len, style);
507 run.setCharactersLength(textLength() - start);
508 ASSERT(run.charactersLength() >= run.length());
510 run.setCharacterScanForCodePath(!canUseSimpleFontCodePath());
511 run.setTabSize(!style.collapseWhiteSpace(), style.tabSize());
513 return f.width(run, fallbackFonts, glyphOverflow);
516 inline bool isHangablePunctuationAtLineStart(UChar c)
518 return U_GET_GC_MASK(c) & (U_GC_PS_MASK | U_GC_PI_MASK | U_GC_PF_MASK);
521 inline bool isHangablePunctuationAtLineEnd(UChar c)
523 return U_GET_GC_MASK(c) & (U_GC_PE_MASK | U_GC_PI_MASK | U_GC_PF_MASK);
526 float RenderText::hangablePunctuationStartWidth(unsigned index) const
528 unsigned len = textLength();
529 if (!len || index >= len)
533 StringImpl& text = *m_text.impl();
535 if (!isHangablePunctuationAtLineStart(text[index]))
538 const RenderStyle& style = this->style();
539 const FontCascade& font = style.fontCascade();
541 return widthFromCache(font, index, 1, 0, 0, 0, style);
544 float RenderText::hangablePunctuationEndWidth(unsigned index) const
546 unsigned len = textLength();
547 if (!len || index >= len)
551 StringImpl& text = *m_text.impl();
553 if (!isHangablePunctuationAtLineEnd(text[index]))
556 const RenderStyle& style = this->style();
557 const FontCascade& font = style.fontCascade();
559 return widthFromCache(font, index, 1, 0, 0, 0, style);
562 bool RenderText::isHangableStopOrComma(UChar c) const
564 return c == 0x002C || c == 0x002E || c == 0x060C || c == 0x06D4 || c == 0x3001
565 || c == 0x3002 || c == 0xFF0C || c == 0xFF0E || c == 0xFE50 || c == 0xFE51
566 || c == 0xFE52 || c == 0xFF61 || c == 0xFF64;
569 unsigned RenderText::firstCharacterIndexStrippingSpaces() const
571 if (!style().collapseWhiteSpace())
575 StringImpl& text = *m_text.impl();
578 for ( ; i < textLength(); ++i) {
579 if (text[i] != ' ' && (text[i] != '\n' || style().preserveNewline()) && text[i] != '\t')
585 unsigned RenderText::lastCharacterIndexStrippingSpaces() const
590 if (!style().collapseWhiteSpace())
591 return textLength() - 1;
594 StringImpl& text = *m_text.impl();
596 int i = textLength() - 1;
597 for ( ; i >= 0; --i) {
598 if (text[i] != ' ' && (text[i] != '\n' || style().preserveNewline()) && text[i] != '\t')
604 void RenderText::trimmedPrefWidths(float leadWidth,
605 float& beginMinW, bool& beginWS,
606 float& endMinW, bool& endWS,
607 bool& hasBreakableChar, bool& hasBreak,
608 float& beginMaxW, float& endMaxW,
609 float& minW, float& maxW, bool& stripFrontSpaces)
611 const RenderStyle& style = this->style();
612 bool collapseWhiteSpace = style.collapseWhiteSpace();
613 if (!collapseWhiteSpace)
614 stripFrontSpaces = false;
616 if (m_hasTab || preferredLogicalWidthsDirty())
617 computePreferredLogicalWidths(leadWidth);
619 beginWS = !stripFrontSpaces && m_hasBeginWS;
622 unsigned len = textLength();
624 if (!len || (stripFrontSpaces && text()->containsOnlyWhitespace())) {
638 beginMinW = m_beginMinWidth;
639 endMinW = m_endMinWidth;
641 hasBreakableChar = m_hasBreakableChar;
642 hasBreak = m_hasBreak;
645 StringImpl& text = *m_text.impl();
646 if (text[0] == space || (text[0] == newlineCharacter && !style.preserveNewline()) || text[0] == '\t') {
647 const FontCascade& font = style.fontCascade(); // FIXME: This ignores first-line.
648 if (stripFrontSpaces) {
649 float spaceWidth = font.width(RenderBlock::constructTextRun(&space, 1, style));
652 maxW += font.wordSpacing();
655 stripFrontSpaces = collapseWhiteSpace && m_hasEndWS;
657 if (!style.autoWrap() || minW > maxW)
660 // Compute our max widths by scanning the string for newlines.
662 const FontCascade& f = style.fontCascade(); // FIXME: This ignores first-line.
663 bool firstLine = true;
666 for (unsigned i = 0; i < len; i++) {
667 unsigned linelen = 0;
668 while (i + linelen < len && text[i + linelen] != '\n')
672 endMaxW = widthFromCache(f, i, linelen, leadWidth + endMaxW, 0, 0, style);
679 } else if (firstLine) {
686 // A <pre> run that ends with a newline, as in, e.g.,
687 // <pre>Some text\n\n<span>More text</pre>
693 static inline bool isSpaceAccordingToStyle(UChar c, const RenderStyle& style)
695 return c == ' ' || (c == noBreakSpace && style.nbspMode() == SPACE);
698 float RenderText::minLogicalWidth() const
700 if (preferredLogicalWidthsDirty())
701 const_cast<RenderText*>(this)->computePreferredLogicalWidths(0);
706 float RenderText::maxLogicalWidth() const
708 if (preferredLogicalWidthsDirty())
709 const_cast<RenderText*>(this)->computePreferredLogicalWidths(0);
714 LineBreakIteratorMode mapLineBreakToIteratorMode(LineBreak lineBreak)
718 case LineBreakAfterWhiteSpace:
719 return LineBreakIteratorMode::Default;
721 return LineBreakIteratorMode::Loose;
722 case LineBreakNormal:
723 return LineBreakIteratorMode::Normal;
724 case LineBreakStrict:
725 return LineBreakIteratorMode::Strict;
727 ASSERT_NOT_REACHED();
728 return LineBreakIteratorMode::Default;
731 void RenderText::computePreferredLogicalWidths(float leadWidth)
733 HashSet<const Font*> fallbackFonts;
734 GlyphOverflow glyphOverflow;
735 computePreferredLogicalWidths(leadWidth, fallbackFonts, glyphOverflow);
736 if (fallbackFonts.isEmpty() && !glyphOverflow.left && !glyphOverflow.right && !glyphOverflow.top && !glyphOverflow.bottom)
737 m_knownToHaveNoOverflowAndNoFallbackFonts = true;
740 static inline float hyphenWidth(RenderText& renderer, const FontCascade& font)
742 const RenderStyle& style = renderer.style();
743 auto textRun = RenderBlock::constructTextRun(style.hyphenString().string(), style);
744 return font.width(textRun);
747 static float maxWordFragmentWidth(RenderText& renderer, const RenderStyle& style, const FontCascade& font, StringView word, unsigned minimumPrefixLength, unsigned minimumSuffixLength, unsigned& suffixStart, HashSet<const Font*>& fallbackFonts, GlyphOverflow& glyphOverflow)
750 if (word.length() <= minimumSuffixLength)
753 Vector<int, 8> hyphenLocations;
754 ASSERT(word.length() >= minimumSuffixLength);
755 unsigned hyphenLocation = word.length() - minimumSuffixLength;
756 while ((hyphenLocation = lastHyphenLocation(word, hyphenLocation, style.locale())) >= std::max(minimumPrefixLength, 1U))
757 hyphenLocations.append(hyphenLocation);
759 if (hyphenLocations.isEmpty())
762 hyphenLocations.reverse();
764 // FIXME: Breaking the string at these places in the middle of words is completely broken with complex text.
765 float minimumFragmentWidthToConsider = font.pixelSize() * 5 / 4 + hyphenWidth(renderer, font);
766 float maxFragmentWidth = 0;
767 for (size_t k = 0; k < hyphenLocations.size(); ++k) {
768 int fragmentLength = hyphenLocations[k] - suffixStart;
769 StringBuilder fragmentWithHyphen;
770 fragmentWithHyphen.append(word.substring(suffixStart, fragmentLength));
771 fragmentWithHyphen.append(style.hyphenString());
773 TextRun run = RenderBlock::constructTextRun(fragmentWithHyphen.toString(), style);
774 run.setCharactersLength(fragmentWithHyphen.length());
775 run.setCharacterScanForCodePath(!renderer.canUseSimpleFontCodePath());
776 float fragmentWidth = font.width(run, &fallbackFonts, &glyphOverflow);
778 // Narrow prefixes are ignored. See tryHyphenating in RenderBlockLineLayout.cpp.
779 if (fragmentWidth <= minimumFragmentWidthToConsider)
782 suffixStart += fragmentLength;
783 maxFragmentWidth = std::max(maxFragmentWidth, fragmentWidth);
786 return maxFragmentWidth;
789 void RenderText::computePreferredLogicalWidths(float leadWidth, HashSet<const Font*>& fallbackFonts, GlyphOverflow& glyphOverflow)
791 ASSERT(m_hasTab || preferredLogicalWidthsDirty() || !m_knownToHaveNoOverflowAndNoFallbackFonts);
798 float currMaxWidth = 0;
799 m_hasBreakableChar = false;
802 m_hasBeginWS = false;
805 const RenderStyle& style = this->style();
806 const FontCascade& font = style.fontCascade(); // FIXME: This ignores first-line.
807 float wordSpacing = font.wordSpacing();
808 unsigned len = textLength();
809 auto iteratorMode = mapLineBreakToIteratorMode(style.lineBreak());
810 LazyLineBreakIterator breakIterator(m_text, style.locale(), iteratorMode);
811 bool needsWordSpacing = false;
812 bool ignoringSpaces = false;
813 bool isSpace = false;
814 bool firstWord = true;
815 bool firstLine = true;
816 std::optional<unsigned> nextBreakable;
817 unsigned lastWordBoundary = 0;
819 // Non-zero only when kerning is enabled, in which case we measure words with their trailing
820 // space, then subtract its width.
821 WordTrailingSpace wordTrailingSpace(style);
822 // If automatic hyphenation is allowed, we keep track of the width of the widest word (or word
823 // fragment) encountered so far, and only try hyphenating words that are wider.
824 float maxWordWidth = std::numeric_limits<float>::max();
825 unsigned minimumPrefixLength = 0;
826 unsigned minimumSuffixLength = 0;
827 if (style.hyphens() == HyphensAuto && canHyphenate(style.locale())) {
830 // Map 'hyphenate-limit-{before,after}: auto;' to 2.
831 auto before = style.hyphenationLimitBefore();
832 minimumPrefixLength = before < 0 ? 2 : before;
834 auto after = style.hyphenationLimitAfter();
835 minimumSuffixLength = after < 0 ? 2 : after;
838 std::optional<int> firstGlyphLeftOverflow;
840 bool breakNBSP = style.autoWrap() && style.nbspMode() == SPACE;
842 // Note the deliberate omission of word-wrap and overflow-wrap from this breakAll check. Those
843 // do not affect minimum preferred sizes. Note that break-word is a non-standard value for
844 // word-break, but we support it as though it means break-all.
845 bool breakAll = (style.wordBreak() == BreakAllWordBreak || style.wordBreak() == BreakWordBreak) && style.autoWrap();
846 bool keepAllWords = style.wordBreak() == KeepAllWordBreak;
847 bool canUseLineBreakShortcut = iteratorMode == LineBreakIteratorMode::Default;
849 for (unsigned i = 0; i < len; i++) {
850 UChar c = uncheckedCharacterAt(i);
852 bool previousCharacterIsSpace = isSpace;
854 bool isNewline = false;
856 if (style.preserveNewline()) {
862 } else if (c == '\t') {
863 if (!style.collapseWhiteSpace()) {
871 if ((isSpace || isNewline) && !i)
873 if ((isSpace || isNewline) && i == len - 1)
876 ignoringSpaces |= style.collapseWhiteSpace() && previousCharacterIsSpace && isSpace;
877 ignoringSpaces &= isSpace;
879 // Ignore spaces and soft hyphens
880 if (ignoringSpaces) {
881 ASSERT(lastWordBoundary == i);
884 } else if (c == softHyphen && style.hyphens() != HyphensNone) {
885 ASSERT(i >= lastWordBoundary);
886 currMaxWidth += widthFromCache(font, lastWordBoundary, i - lastWordBoundary, leadWidth + currMaxWidth, &fallbackFonts, &glyphOverflow, style);
887 if (!firstGlyphLeftOverflow)
888 firstGlyphLeftOverflow = glyphOverflow.left;
889 lastWordBoundary = i + 1;
893 bool hasBreak = breakAll || isBreakable(breakIterator, i, nextBreakable, breakNBSP, canUseLineBreakShortcut, keepAllWords);
894 bool betweenWords = true;
896 while (c != '\n' && !isSpaceAccordingToStyle(c, style) && c != '\t' && (c != softHyphen || style.hyphens() == HyphensNone)) {
900 c = uncheckedCharacterAt(j);
901 if (isBreakable(breakIterator, j, nextBreakable, breakNBSP, canUseLineBreakShortcut, keepAllWords) && characterAt(j - 1) != softHyphen)
904 betweenWords = false;
909 unsigned wordLen = j - i;
911 float currMinWidth = 0;
912 bool isSpace = (j < len) && isSpaceAccordingToStyle(c, style);
914 std::optional<float> wordTrailingSpaceWidth;
916 wordTrailingSpaceWidth = wordTrailingSpace.width(fallbackFonts);
917 if (wordTrailingSpaceWidth)
918 w = widthFromCache(font, i, wordLen + 1, leadWidth + currMaxWidth, &fallbackFonts, &glyphOverflow, style) - wordTrailingSpaceWidth.value();
920 w = widthFromCache(font, i, wordLen, leadWidth + currMaxWidth, &fallbackFonts, &glyphOverflow, style);
921 if (c == softHyphen && style.hyphens() != HyphensNone)
922 currMinWidth = hyphenWidth(*this, font);
925 if (w > maxWordWidth) {
926 unsigned suffixStart;
927 float maxFragmentWidth = maxWordFragmentWidth(*this, style, font, StringView(m_text).substring(i, wordLen), minimumPrefixLength, minimumSuffixLength, suffixStart, fallbackFonts, glyphOverflow);
931 std::optional<float> wordTrailingSpaceWidth;
933 wordTrailingSpaceWidth = wordTrailingSpace.width(fallbackFonts);
934 if (wordTrailingSpaceWidth)
935 suffixWidth = widthFromCache(font, i + suffixStart, wordLen - suffixStart + 1, leadWidth + currMaxWidth, 0, 0, style) - wordTrailingSpaceWidth.value();
937 suffixWidth = widthFromCache(font, i + suffixStart, wordLen - suffixStart, leadWidth + currMaxWidth, 0, 0, style);
939 maxFragmentWidth = std::max(maxFragmentWidth, suffixWidth);
941 currMinWidth += maxFragmentWidth - w;
942 maxWordWidth = std::max(maxWordWidth, maxFragmentWidth);
947 if (!firstGlyphLeftOverflow)
948 firstGlyphLeftOverflow = glyphOverflow.left;
951 if (lastWordBoundary == i)
954 ASSERT(j >= lastWordBoundary);
955 currMaxWidth += widthFromCache(font, lastWordBoundary, j - lastWordBoundary, leadWidth + currMaxWidth, &fallbackFonts, &glyphOverflow, style);
957 lastWordBoundary = j;
960 bool isCollapsibleWhiteSpace = (j < len) && style.isCollapsibleWhiteSpace(c);
961 if (j < len && style.autoWrap())
962 m_hasBreakableChar = true;
964 // Add in wordSpacing to our currMaxWidth, but not if this is the last word on a line or the
965 // last word in the run.
966 if ((isSpace || isCollapsibleWhiteSpace) && !containsOnlyWhitespace(j, len-j))
967 currMaxWidth += wordSpacing;
971 // If the first character in the run is breakable, then we consider ourselves to have a beginning
972 // minimum width of 0, since a break could occur right before our run starts, preventing us from ever
973 // being appended to a previous text run when considering the total minimum width of the containing block.
975 m_hasBreakableChar = true;
976 m_beginMinWidth = hasBreak ? 0 : currMinWidth;
978 m_endMinWidth = currMinWidth;
980 m_minWidth = std::max(currMinWidth, m_minWidth);
984 // Nowrap can never be broken, so don't bother setting the
985 // breakable character boolean. Pre can only be broken if we encounter a newline.
986 if (style.autoWrap() || isNewline)
987 m_hasBreakableChar = true;
989 if (isNewline) { // Only set if preserveNewline was true and we saw a newline.
993 if (!style.autoWrap())
994 m_beginMinWidth = currMaxWidth;
997 if (currMaxWidth > m_maxWidth)
998 m_maxWidth = currMaxWidth;
1001 TextRun run = RenderBlock::constructTextRun(*this, i, 1, style);
1002 run.setCharactersLength(len - i);
1003 ASSERT(run.charactersLength() >= run.length());
1004 run.setTabSize(!style.collapseWhiteSpace(), style.tabSize());
1005 run.setXPos(leadWidth + currMaxWidth);
1007 currMaxWidth += font.width(run, &fallbackFonts);
1008 glyphOverflow.right = 0;
1009 needsWordSpacing = isSpace && !previousCharacterIsSpace && i == len - 1;
1011 ASSERT(lastWordBoundary == i);
1016 glyphOverflow.left = firstGlyphLeftOverflow.value_or(glyphOverflow.left);
1018 if ((needsWordSpacing && len > 1) || (ignoringSpaces && !firstWord))
1019 currMaxWidth += wordSpacing;
1021 m_maxWidth = std::max(currMaxWidth, m_maxWidth);
1023 if (!style.autoWrap())
1024 m_minWidth = m_maxWidth;
1026 if (style.whiteSpace() == PRE) {
1028 m_beginMinWidth = m_maxWidth;
1029 m_endMinWidth = currMaxWidth;
1032 setPreferredLogicalWidthsDirty(false);
1035 bool RenderText::isAllCollapsibleWhitespace() const
1037 const RenderStyle& style = this->style();
1038 unsigned length = textLength();
1040 for (unsigned i = 0; i < length; ++i) {
1041 if (!style.isCollapsibleWhiteSpace(characters8()[i]))
1046 for (unsigned i = 0; i < length; ++i) {
1047 if (!style.isCollapsibleWhiteSpace(characters16()[i]))
1053 bool RenderText::containsOnlyWhitespace(unsigned from, unsigned len) const
1056 StringImpl& text = *m_text.impl();
1058 for (currPos = from;
1059 currPos < from + len && (text[currPos] == '\n' || text[currPos] == ' ' || text[currPos] == '\t');
1061 return currPos >= (from + len);
1064 IntPoint RenderText::firstRunLocation() const
1066 if (auto* layout = simpleLineLayout())
1067 return SimpleLineLayout::computeFirstRunLocation(*this, *layout);
1069 return m_lineBoxes.firstRunLocation();
1072 void RenderText::setSelectionState(SelectionState state)
1074 if (state != SelectionNone)
1077 RenderObject::setSelectionState(state);
1079 if (canUpdateSelectionOnRootLineBoxes())
1080 m_lineBoxes.setSelectionState(*this, state);
1082 // The containing block can be null in case of an orphaned tree.
1083 RenderBlock* containingBlock = this->containingBlock();
1084 if (containingBlock && !containingBlock->isRenderView())
1085 containingBlock->setSelectionState(state);
1088 void RenderText::setTextWithOffset(const String& text, unsigned offset, unsigned len, bool force)
1090 if (!force && m_text == text)
1093 int delta = text.length() - textLength();
1094 unsigned end = len ? offset + len - 1 : offset;
1096 m_linesDirty = simpleLineLayout() || m_lineBoxes.dirtyRange(*this, offset, end, delta);
1098 setText(text, force || m_linesDirty);
1101 static inline bool isInlineFlowOrEmptyText(const RenderObject& renderer)
1103 if (is<RenderInline>(renderer))
1105 if (!is<RenderText>(renderer))
1107 StringImpl* text = downcast<RenderText>(renderer).text();
1110 return !text->length();
1113 UChar RenderText::previousCharacter() const
1115 // find previous text renderer if one exists
1116 const RenderObject* previousText = this;
1117 while ((previousText = previousText->previousInPreOrder()))
1118 if (!isInlineFlowOrEmptyText(*previousText))
1121 if (is<RenderText>(previousText)) {
1122 if (StringImpl* previousString = downcast<RenderText>(*previousText).text())
1123 prev = (*previousString)[previousString->length() - 1];
1128 LayoutUnit RenderText::topOfFirstText() const
1130 return firstTextBox()->root().lineTop();
1133 void applyTextTransform(const RenderStyle& style, String& text, UChar previousCharacter)
1135 switch (style.textTransform()) {
1139 makeCapitalized(&text, previousCharacter);
1142 text = text.convertToUppercaseWithLocale(style.locale());
1145 text = text.convertToLowercaseWithLocale(style.locale());
1150 void RenderText::setRenderedText(const String& text)
1152 ASSERT(!text.isNull());
1154 String originalText = this->originalText();
1158 if (m_useBackslashAsYenSymbol)
1159 m_text.replace('\\', yenSign);
1163 applyTextTransform(style(), m_text, previousCharacter());
1165 switch (style().textSecurity()) {
1169 // We use the same characters here as for list markers.
1170 // See the listMarkerText function in RenderListMarker.cpp.
1172 secureText(whiteBullet);
1178 secureText(blackSquare);
1181 // FIXME: Why this quirk on iOS?
1185 secureText(blackCircle);
1190 ASSERT(!m_text.isNull());
1192 m_isAllASCII = m_text.containsOnlyASCII();
1193 m_canUseSimpleFontCodePath = computeCanUseSimpleFontCodePath();
1194 m_canUseSimplifiedTextMeasuring = computeCanUseSimplifiedTextMeasuring();
1196 if (m_text != originalText) {
1197 originalTextMap().set(this, originalText);
1198 m_originalTextDiffersFromRendered = true;
1199 } else if (m_originalTextDiffersFromRendered) {
1200 originalTextMap().remove(this);
1201 m_originalTextDiffersFromRendered = false;
1205 void RenderText::secureText(UChar maskingCharacter)
1207 // This hides the text by replacing all the characters with the masking character.
1208 // Offsets within the hidden text have to match offsets within the original text
1209 // to handle things like carets and selection, so this won't work right if any
1210 // of the characters are surrogate pairs or combining marks. Thus, this function
1211 // does not attempt to handle either of those.
1213 unsigned length = textLength();
1217 UChar characterToReveal = 0;
1218 unsigned revealedCharactersOffset = 0;
1220 if (SecureTextTimer* timer = secureTextTimers().get(this)) {
1221 // We take the offset out of the timer to make this one-shot. We count on this being called only once.
1222 // If it's called a second time we assume the text is different and a character should not be revealed.
1223 revealedCharactersOffset = timer->takeOffsetAfterLastTypedCharacter();
1224 if (revealedCharactersOffset && revealedCharactersOffset <= length)
1225 characterToReveal = m_text[--revealedCharactersOffset];
1229 m_text = String::createUninitialized(length, characters);
1231 for (unsigned i = 0; i < length; ++i)
1232 characters[i] = maskingCharacter;
1233 if (characterToReveal)
1234 characters[revealedCharactersOffset] = characterToReveal;
1237 bool RenderText::computeCanUseSimplifiedTextMeasuring() const
1239 if (!m_canUseSimpleFontCodePath)
1242 auto& font = style().fontCascade();
1243 if (font.wordSpacing() || font.letterSpacing())
1246 // Additional check on the font codepath.
1247 TextRun run(m_text);
1248 run.setCharacterScanForCodePath(false);
1249 if (font.codePath(run) != FontCascade::Simple)
1252 auto whitespaceIsCollapsed = style().collapseWhiteSpace();
1253 for (unsigned i = 0; i < m_text.length(); ++i) {
1254 if ((!whitespaceIsCollapsed && m_text[i] == '\t') || m_text[i] == noBreakSpace || m_text[i] >= HiraganaLetterSmallA)
1260 void RenderText::setText(const String& text, bool force)
1262 ASSERT(!text.isNull());
1264 if (!force && text == originalText())
1268 if (m_originalTextDiffersFromRendered) {
1269 originalTextMap().remove(this);
1270 m_originalTextDiffersFromRendered = false;
1273 setRenderedText(text);
1275 setNeedsLayoutAndPrefWidthsRecalc();
1276 m_knownToHaveNoOverflowAndNoFallbackFonts = false;
1278 if (is<RenderBlockFlow>(*parent()))
1279 downcast<RenderBlockFlow>(*parent()).invalidateLineLayoutPath();
1281 if (AXObjectCache* cache = document().existingAXObjectCache())
1282 cache->textChanged(this);
1285 String RenderText::textWithoutConvertingBackslashToYenSymbol() const
1287 if (!m_useBackslashAsYenSymbol || style().textSecurity() != TSNONE)
1290 String text = originalText();
1291 applyTextTransform(style(), text, previousCharacter());
1295 void RenderText::dirtyLineBoxes(bool fullLayout)
1298 m_lineBoxes.deleteAll();
1299 else if (!m_linesDirty)
1300 m_lineBoxes.dirtyAll();
1301 m_linesDirty = false;
1304 std::unique_ptr<InlineTextBox> RenderText::createTextBox()
1306 return std::make_unique<InlineTextBox>(*this);
1309 void RenderText::positionLineBox(InlineTextBox& textBox)
1313 m_containsReversedText |= !textBox.isLeftToRightDirection();
1316 void RenderText::ensureLineBoxes()
1318 if (!is<RenderBlockFlow>(*parent()))
1320 downcast<RenderBlockFlow>(*parent()).ensureLineBoxes();
1323 const SimpleLineLayout::Layout* RenderText::simpleLineLayout() const
1325 if (!is<RenderBlockFlow>(*parent()))
1327 return downcast<RenderBlockFlow>(*parent()).simpleLineLayout();
1330 float RenderText::width(unsigned from, unsigned len, float xPos, bool firstLine, HashSet<const Font*>* fallbackFonts, GlyphOverflow* glyphOverflow) const
1332 if (from >= textLength())
1335 if (from + len > textLength())
1336 len = textLength() - from;
1338 const RenderStyle& lineStyle = firstLine ? firstLineStyle() : style();
1339 return width(from, len, lineStyle.fontCascade(), xPos, fallbackFonts, glyphOverflow);
1342 float RenderText::width(unsigned from, unsigned len, const FontCascade& f, float xPos, HashSet<const Font*>* fallbackFonts, GlyphOverflow* glyphOverflow) const
1344 ASSERT(from + len <= textLength());
1348 const RenderStyle& style = this->style();
1350 if (&f == &style.fontCascade()) {
1351 if (!style.preserveNewline() && !from && len == textLength() && (!glyphOverflow || !glyphOverflow->computeBounds)) {
1352 if (fallbackFonts) {
1353 ASSERT(glyphOverflow);
1354 if (preferredLogicalWidthsDirty() || !m_knownToHaveNoOverflowAndNoFallbackFonts) {
1355 const_cast<RenderText*>(this)->computePreferredLogicalWidths(0, *fallbackFonts, *glyphOverflow);
1356 if (fallbackFonts->isEmpty() && !glyphOverflow->left && !glyphOverflow->right && !glyphOverflow->top && !glyphOverflow->bottom)
1357 m_knownToHaveNoOverflowAndNoFallbackFonts = true;
1361 w = maxLogicalWidth();
1363 w = widthFromCache(f, from, len, xPos, fallbackFonts, glyphOverflow, style);
1365 TextRun run = RenderBlock::constructTextRun(*this, from, len, style);
1366 run.setCharactersLength(textLength() - from);
1367 ASSERT(run.charactersLength() >= run.length());
1369 run.setCharacterScanForCodePath(!canUseSimpleFontCodePath());
1370 run.setTabSize(!style.collapseWhiteSpace(), style.tabSize());
1372 w = f.width(run, fallbackFonts, glyphOverflow);
1378 IntRect RenderText::linesBoundingBox() const
1380 if (auto* layout = simpleLineLayout())
1381 return SimpleLineLayout::computeBoundingBox(*this, *layout);
1383 return m_lineBoxes.boundingBox(*this);
1386 LayoutRect RenderText::linesVisualOverflowBoundingBox() const
1388 ASSERT(!simpleLineLayout());
1389 return m_lineBoxes.visualOverflowBoundingBox(*this);
1392 LayoutRect RenderText::clippedOverflowRectForRepaint(const RenderLayerModelObject* repaintContainer) const
1394 RenderObject* rendererToRepaint = containingBlock();
1396 // Do not cross self-painting layer boundaries.
1397 RenderObject& enclosingLayerRenderer = enclosingLayer()->renderer();
1398 if (&enclosingLayerRenderer != rendererToRepaint && !rendererToRepaint->isDescendantOf(&enclosingLayerRenderer))
1399 rendererToRepaint = &enclosingLayerRenderer;
1401 // The renderer we chose to repaint may be an ancestor of repaintContainer, but we need to do a repaintContainer-relative repaint.
1402 if (repaintContainer && repaintContainer != rendererToRepaint && !rendererToRepaint->isDescendantOf(repaintContainer))
1403 return repaintContainer->clippedOverflowRectForRepaint(repaintContainer);
1405 return rendererToRepaint->clippedOverflowRectForRepaint(repaintContainer);
1408 LayoutRect RenderText::collectSelectionRectsForLineBoxes(const RenderLayerModelObject* repaintContainer, bool clipToVisibleContent, Vector<LayoutRect>* rects)
1410 ASSERT(!needsLayout());
1411 ASSERT(!simpleLineLayout());
1413 if (selectionState() == SelectionNone)
1414 return LayoutRect();
1415 if (!containingBlock())
1416 return LayoutRect();
1418 // Now calculate startPos and endPos for painting selection.
1419 // We include a selection while endPos > 0
1420 unsigned startPos, endPos;
1421 if (selectionState() == SelectionInside) {
1422 // We are fully selected.
1424 endPos = textLength();
1426 selectionStartEnd(startPos, endPos);
1427 if (selectionState() == SelectionStart)
1428 endPos = textLength();
1429 else if (selectionState() == SelectionEnd)
1433 if (startPos == endPos)
1436 LayoutRect resultRect;
1438 resultRect = m_lineBoxes.selectionRectForRange(startPos, endPos);
1440 m_lineBoxes.collectSelectionRectsForRange(startPos, endPos, *rects);
1441 for (auto& rect : *rects) {
1442 resultRect.unite(rect);
1443 rect = localToContainerQuad(FloatRect(rect), repaintContainer).enclosingBoundingBox();
1447 if (clipToVisibleContent)
1448 return computeRectForRepaint(resultRect, repaintContainer);
1449 return localToContainerQuad(FloatRect(resultRect), repaintContainer).enclosingBoundingBox();
1452 LayoutRect RenderText::collectSelectionRectsForLineBoxes(const RenderLayerModelObject* repaintContainer, bool clipToVisibleContent, Vector<LayoutRect>& rects)
1454 return collectSelectionRectsForLineBoxes(repaintContainer, clipToVisibleContent, &rects);
1457 LayoutRect RenderText::selectionRectForRepaint(const RenderLayerModelObject* repaintContainer, bool clipToVisibleContent)
1459 return collectSelectionRectsForLineBoxes(repaintContainer, clipToVisibleContent, nullptr);
1462 int RenderText::caretMinOffset() const
1464 if (auto* layout = simpleLineLayout())
1465 return SimpleLineLayout::findCaretMinimumOffset(*this, *layout);
1466 return m_lineBoxes.caretMinOffset();
1469 int RenderText::caretMaxOffset() const
1471 if (auto* layout = simpleLineLayout())
1472 return SimpleLineLayout::findCaretMaximumOffset(*this, *layout);
1473 return m_lineBoxes.caretMaxOffset(*this);
1476 unsigned RenderText::countRenderedCharacterOffsetsUntil(unsigned offset) const
1478 ASSERT(!simpleLineLayout());
1479 return m_lineBoxes.countCharacterOffsetsUntil(offset);
1482 bool RenderText::containsRenderedCharacterOffset(unsigned offset) const
1484 ASSERT(!simpleLineLayout());
1485 return m_lineBoxes.containsOffset(*this, offset, RenderTextLineBoxes::CharacterOffset);
1488 bool RenderText::containsCaretOffset(unsigned offset) const
1490 if (auto* layout = simpleLineLayout())
1491 return SimpleLineLayout::containsCaretOffset(*this, *layout, offset);
1492 return m_lineBoxes.containsOffset(*this, offset, RenderTextLineBoxes::CaretOffset);
1495 bool RenderText::hasRenderedText() const
1497 if (auto* layout = simpleLineLayout())
1498 return SimpleLineLayout::isTextRendered(*this, *layout);
1499 return m_lineBoxes.hasRenderedText();
1502 int RenderText::previousOffset(int current) const
1504 if (isAllASCII() || m_text.is8Bit())
1507 StringImpl* textImpl = m_text.impl();
1508 CachedTextBreakIterator iterator(StringView(textImpl->characters16(), textImpl->length()), TextBreakIterator::Mode::Caret, nullAtom);
1509 auto result = iterator.preceding(current).value_or(current - 1);
1513 #if PLATFORM(COCOA) || PLATFORM(GTK)
1515 const UChar hangulChoseongStart = 0x1100;
1516 const UChar hangulChoseongEnd = 0x115F;
1517 const UChar hangulJungseongStart = 0x1160;
1518 const UChar hangulJungseongEnd = 0x11A2;
1519 const UChar hangulJongseongStart = 0x11A8;
1520 const UChar hangulJongseongEnd = 0x11F9;
1521 const UChar hangulSyllableStart = 0xAC00;
1522 const UChar hangulSyllableEnd = 0xD7AF;
1523 const UChar hangulJongseongCount = 28;
1525 enum class HangulState { L, V, T, LV, LVT, Break };
1527 static inline bool isHangulLVT(UChar character)
1529 return (character - hangulSyllableStart) % hangulJongseongCount;
1532 static inline bool isMark(UChar32 character)
1534 return U_GET_GC_MASK(character) & U_GC_M_MASK;
1537 static inline bool isRegionalIndicator(UChar32 character)
1539 // National flag emoji each consists of a pair of regional indicator symbols.
1540 return 0x1F1E6 <= character && character <= 0x1F1FF;
1543 static inline bool isInArmenianToLimbuRange(UChar32 character)
1545 return character >= 0x0530 && character < 0x1950;
1550 int RenderText::previousOffsetForBackwardDeletion(int current) const
1552 ASSERT(!m_text.isNull());
1553 StringImpl& text = *m_text.impl();
1555 // FIXME: Unclear why this has so much handrolled code rather than using UBreakIterator.
1556 // Also unclear why this is so different from advanceByCombiningCharacterSequence.
1558 // FIXME: Seems like this fancier case could be used on all platforms now, no
1559 // need for the #else case below.
1560 #if PLATFORM(COCOA) || PLATFORM(GTK)
1561 bool sawRegionalIndicator = false;
1562 bool sawEmojiGroupCandidate = false;
1563 bool sawEmojiFitzpatrickModifier = false;
1565 while (current > 0) {
1567 U16_PREV(text, 0, current, character);
1569 if (sawEmojiGroupCandidate) {
1570 sawEmojiGroupCandidate = false;
1571 if (character == zeroWidthJoiner)
1573 // We could have two emoji group candidates without a joiner in between.
1574 // Those should not be treated as a group.
1575 U16_FWD_1_UNSAFE(text, current);
1579 if (sawEmojiFitzpatrickModifier) {
1580 if (isEmojiFitzpatrickModifier(character)) {
1581 // Don't treat two emoji modifiers in a row as a group.
1582 U16_FWD_1_UNSAFE(text, current);
1585 if (!isVariationSelector(character))
1589 if (sawRegionalIndicator) {
1590 // We don't check if the pair of regional indicator symbols before current position can actually be combined
1591 // into a flag, and just delete it. This may not agree with how the pair is rendered in edge cases,
1592 // but is good enough in practice.
1593 if (isRegionalIndicator(character))
1595 // Don't delete a preceding character that isn't a regional indicator symbol.
1596 U16_FWD_1_UNSAFE(text, current);
1599 // We don't combine characters in Armenian ... Limbu range for backward deletion.
1600 if (isInArmenianToLimbuRange(character))
1603 if (isRegionalIndicator(character)) {
1604 sawRegionalIndicator = true;
1608 if (isEmojiFitzpatrickModifier(character)) {
1609 sawEmojiFitzpatrickModifier = true;
1613 if (isEmojiGroupCandidate(character)) {
1614 sawEmojiGroupCandidate = true;
1618 // FIXME: Why are FF9E and FF9F special cased here?
1619 if (!isMark(character) && character != 0xFF9E && character != 0xFF9F)
1627 UChar character = text[current];
1628 if ((character >= hangulChoseongStart && character <= hangulJongseongEnd) || (character >= hangulSyllableStart && character <= hangulSyllableEnd)) {
1631 if (character < hangulJungseongStart)
1632 state = HangulState::L;
1633 else if (character < hangulJongseongStart)
1634 state = HangulState::V;
1635 else if (character < hangulSyllableStart)
1636 state = HangulState::T;
1638 state = isHangulLVT(character) ? HangulState::LVT : HangulState::LV;
1640 while (current > 0 && (character = text[current - 1]) >= hangulChoseongStart && character <= hangulSyllableEnd && (character <= hangulJongseongEnd || character >= hangulSyllableStart)) {
1642 case HangulState::V:
1643 if (character <= hangulChoseongEnd)
1644 state = HangulState::L;
1645 else if (character >= hangulSyllableStart && character <= hangulSyllableEnd && !isHangulLVT(character))
1646 state = HangulState::LV;
1647 else if (character > hangulJungseongEnd)
1648 state = HangulState::Break;
1650 case HangulState::T:
1651 if (character >= hangulJungseongStart && character <= hangulJungseongEnd)
1652 state = HangulState::V;
1653 else if (character >= hangulSyllableStart && character <= hangulSyllableEnd)
1654 state = isHangulLVT(character) ? HangulState::LVT : HangulState::LV;
1655 else if (character < hangulJungseongStart)
1656 state = HangulState::Break;
1659 state = (character < hangulJungseongStart) ? HangulState::L : HangulState::Break;
1662 if (state == HangulState::Break)
1670 U16_BACK_1(text, 0, current);
1675 int RenderText::nextOffset(int current) const
1677 if (isAllASCII() || m_text.is8Bit())
1680 StringImpl* textImpl = m_text.impl();
1681 CachedTextBreakIterator iterator(StringView(textImpl->characters16(), textImpl->length()), TextBreakIterator::Mode::Caret, nullAtom);
1682 auto result = iterator.following(current).value_or(current + 1);
1686 bool RenderText::computeCanUseSimpleFontCodePath() const
1688 if (isAllASCII() || m_text.is8Bit())
1690 return FontCascade::characterRangeCodePath(characters16(), length()) == FontCascade::Simple;
1693 void RenderText::momentarilyRevealLastTypedCharacter(unsigned offsetAfterLastTypedCharacter)
1695 if (style().textSecurity() == TSNONE)
1697 auto& secureTextTimer = secureTextTimers().add(this, nullptr).iterator->value;
1698 if (!secureTextTimer)
1699 secureTextTimer = std::make_unique<SecureTextTimer>(*this);
1700 secureTextTimer->restart(offsetAfterLastTypedCharacter);
1703 StringView RenderText::stringView(unsigned start, std::optional<unsigned> stop) const
1705 unsigned destination = stop.value_or(textLength());
1706 ASSERT(start <= length());
1707 ASSERT(destination <= length());
1708 ASSERT(start <= destination);
1710 return StringView(characters8() + start, destination - start);
1711 return StringView(characters16() + start, destination - start);
1714 } // namespace WebCore