This source file includes following definitions.
- m_lastTypedCharacterOffset
- restartWithNewText
- invalidate
- lastTypedCharacterOffset
- fired
- makeCapitalized
- m_lastTextBox
- renderName
- isTextFragment
- isWordBreak
- styleDidChange
- removeAndDestroyTextBoxes
- willBeDestroyed
- extractTextBox
- attachTextBox
- removeTextBox
- deleteTextBoxes
- originalText
- plainText
- absoluteRects
- localQuadForTextBox
- absoluteRectsForRange
- ellipsisRectForBox
- absoluteQuads
- absoluteQuads
- absoluteQuadsForRange
- findNextInlineTextBox
- lineDirectionPointFitsInBox
- createPositionWithAffinityForBox
- createPositionWithAffinityForBoxAfterAdjustingOffsetForBiDi
- positionForPoint
- localCaretRect
- widthFromCache
- trimmedPrefWidths
- minLogicalWidth
- maxLogicalWidth
- computePreferredLogicalWidths
- hyphenWidth
- computePreferredLogicalWidths
- isAllCollapsibleWhitespace
- containsOnlyWhitespace
- firstRunOrigin
- firstRunX
- firstRunY
- setSelectionState
- setTextWithOffset
- transformText
- isInlineFlowOrEmptyText
- previousCharacter
- addLayerHitTestRects
- applyTextTransform
- setTextInternal
- secureText
- setText
- dirtyLineBoxes
- createTextBox
- createInlineTextBox
- positionLineBox
- width
- width
- linesBoundingBox
- linesVisualOverflowBoundingBox
- clippedOverflowRectForRepaint
- selectionRectForRepaint
- caretMinOffset
- caretMaxOffset
- renderedTextLength
- previousOffset
- isHangulLVT
- isMark
- isRegionalIndicator
- previousOffsetForBackwardDeletion
- nextOffset
- computeCanUseSimpleFontCodePath
- checkConsistency
- momentarilyRevealLastTypedCharacter
- firstAbstractInlineTextBox
#include "config.h"
#include "core/rendering/RenderText.h"
#include "core/accessibility/AXObjectCache.h"
#include "core/dom/Text.h"
#include "core/editing/TextIterator.h"
#include "core/frame/FrameView.h"
#include "core/frame/Settings.h"
#include "core/html/parser/TextResourceDecoder.h"
#include "core/rendering/AbstractInlineTextBox.h"
#include "core/rendering/EllipsisBox.h"
#include "core/rendering/InlineTextBox.h"
#include "core/rendering/RenderBlock.h"
#include "core/rendering/RenderCombineText.h"
#include "core/rendering/RenderLayer.h"
#include "core/rendering/RenderView.h"
#include "core/rendering/break_lines.h"
#include "platform/fonts/Character.h"
#include "platform/geometry/FloatQuad.h"
#include "platform/text/BidiResolver.h"
#include "platform/text/TextBreakIterator.h"
#include "platform/text/TextRunIterator.h"
#include "wtf/text/StringBuffer.h"
#include "wtf/text/StringBuilder.h"
#include "wtf/unicode/CharacterNames.h"
using namespace std;
using namespace WTF;
using namespace Unicode;
namespace WebCore {
struct SameSizeAsRenderText : public RenderObject {
uint32_t bitfields : 16;
float widths[4];
String text;
void* pointers[2];
};
COMPILE_ASSERT(sizeof(RenderText) == sizeof(SameSizeAsRenderText), RenderText_should_stay_small);
class SecureTextTimer;
typedef HashMap<RenderText*, SecureTextTimer*> SecureTextTimerMap;
static SecureTextTimerMap* gSecureTextTimers = 0;
class SecureTextTimer FINAL : public TimerBase {
public:
SecureTextTimer(RenderText* renderText)
: m_renderText(renderText)
, m_lastTypedCharacterOffset(-1)
{
}
void restartWithNewText(unsigned lastTypedCharacterOffset)
{
m_lastTypedCharacterOffset = lastTypedCharacterOffset;
if (Settings* settings = m_renderText->document().settings())
startOneShot(settings->passwordEchoDurationInSeconds(), FROM_HERE);
}
void invalidate() { m_lastTypedCharacterOffset = -1; }
unsigned lastTypedCharacterOffset() { return m_lastTypedCharacterOffset; }
private:
virtual void fired() OVERRIDE
{
ASSERT(gSecureTextTimers->contains(m_renderText));
m_renderText->setText(m_renderText->text().impl(), true );
}
RenderText* m_renderText;
int m_lastTypedCharacterOffset;
};
static void makeCapitalized(String* string, UChar previous)
{
if (string->isNull())
return;
unsigned length = string->length();
const StringImpl& input = *string->impl();
if (length >= numeric_limits<unsigned>::max())
CRASH();
StringBuffer<UChar> stringWithPrevious(length + 1);
stringWithPrevious[0] = previous == noBreakSpace ? ' ' : previous;
for (unsigned i = 1; i < length + 1; i++) {
if (input[i - 1] == noBreakSpace)
stringWithPrevious[i] = ' ';
else
stringWithPrevious[i] = input[i - 1];
}
TextBreakIterator* boundary = wordBreakIterator(stringWithPrevious.characters(), length + 1);
if (!boundary)
return;
StringBuilder result;
result.reserveCapacity(length);
int32_t endOfWord;
int32_t startOfWord = boundary->first();
for (endOfWord = boundary->next(); endOfWord != TextBreakDone; startOfWord = endOfWord, endOfWord = boundary->next()) {
if (startOfWord)
result.append(input[startOfWord - 1] == noBreakSpace ? noBreakSpace : toTitleCase(stringWithPrevious[startOfWord]));
for (int i = startOfWord + 1; i < endOfWord; i++)
result.append(input[i - 1]);
}
*string = result.toString();
}
RenderText::RenderText(Node* node, PassRefPtr<StringImpl> str)
: RenderObject(!node || node->isDocumentNode() ? 0 : node)
, m_hasTab(false)
, m_linesDirty(false)
, m_containsReversedText(false)
, m_knownToHaveNoOverflowAndNoFallbackFonts(false)
, m_minWidth(-1)
, m_maxWidth(-1)
, m_firstLineMinWidth(0)
, m_lastLineLineMinWidth(0)
, m_text(str)
, m_firstTextBox(0)
, m_lastTextBox(0)
{
ASSERT(m_text);
if (node && node->isDocumentNode())
setDocumentForAnonymous(toDocument(node));
m_isAllASCII = m_text.containsOnlyASCII();
m_canUseSimpleFontCodePath = computeCanUseSimpleFontCodePath();
setIsText();
view()->frameView()->incrementVisuallyNonEmptyCharacterCount(m_text.length());
}
#ifndef NDEBUG
RenderText::~RenderText()
{
ASSERT(!m_firstTextBox);
ASSERT(!m_lastTextBox);
}
#endif
const char* RenderText::renderName() const
{
return "RenderText";
}
bool RenderText::isTextFragment() const
{
return false;
}
bool RenderText::isWordBreak() const
{
return false;
}
void RenderText::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
{
if (diff == StyleDifferenceLayout) {
setNeedsLayoutAndPrefWidthsRecalc();
m_knownToHaveNoOverflowAndNoFallbackFonts = false;
}
RenderStyle* newStyle = style();
ETextTransform oldTransform = oldStyle ? oldStyle->textTransform() : TTNONE;
ETextSecurity oldSecurity = oldStyle ? oldStyle->textSecurity() : TSNONE;
if (oldTransform != newStyle->textTransform() || oldSecurity != newStyle->textSecurity())
transformText();
if (!text().containsOnlyWhitespace())
newStyle->font().willUseFontData();
}
void RenderText::removeAndDestroyTextBoxes()
{
if (!documentBeingDestroyed()) {
if (firstTextBox()) {
if (isBR()) {
RootInlineBox* next = firstTextBox()->root().nextRootBox();
if (next)
next->markDirty();
}
for (InlineTextBox* box = firstTextBox(); box; box = box->nextTextBox())
box->remove();
} else if (parent())
parent()->dirtyLinesFromChangedChild(this);
}
deleteTextBoxes();
}
void RenderText::willBeDestroyed()
{
if (SecureTextTimer* secureTextTimer = gSecureTextTimers ? gSecureTextTimers->take(this) : 0)
delete secureTextTimer;
removeAndDestroyTextBoxes();
RenderObject::willBeDestroyed();
}
void RenderText::extractTextBox(InlineTextBox* box)
{
checkConsistency();
m_lastTextBox = box->prevTextBox();
if (box == m_firstTextBox)
m_firstTextBox = 0;
if (box->prevTextBox())
box->prevTextBox()->setNextTextBox(0);
box->setPreviousTextBox(0);
for (InlineTextBox* curr = box; curr; curr = curr->nextTextBox())
curr->setExtracted();
checkConsistency();
}
void RenderText::attachTextBox(InlineTextBox* box)
{
checkConsistency();
if (m_lastTextBox) {
m_lastTextBox->setNextTextBox(box);
box->setPreviousTextBox(m_lastTextBox);
} else
m_firstTextBox = box;
InlineTextBox* last = box;
for (InlineTextBox* curr = box; curr; curr = curr->nextTextBox()) {
curr->setExtracted(false);
last = curr;
}
m_lastTextBox = last;
checkConsistency();
}
void RenderText::removeTextBox(InlineTextBox* box)
{
checkConsistency();
if (box == m_firstTextBox)
m_firstTextBox = box->nextTextBox();
if (box == m_lastTextBox)
m_lastTextBox = box->prevTextBox();
if (box->nextTextBox())
box->nextTextBox()->setPreviousTextBox(box->prevTextBox());
if (box->prevTextBox())
box->prevTextBox()->setNextTextBox(box->nextTextBox());
checkConsistency();
}
void RenderText::deleteTextBoxes()
{
if (firstTextBox()) {
InlineTextBox* next;
for (InlineTextBox* curr = firstTextBox(); curr; curr = next) {
next = curr->nextTextBox();
curr->destroy();
}
m_firstTextBox = m_lastTextBox = 0;
}
}
PassRefPtr<StringImpl> RenderText::originalText() const
{
Node* e = node();
return (e && e->isTextNode()) ? toText(e)->dataImpl() : 0;
}
String RenderText::plainText() const
{
if (node())
return WebCore::plainText(rangeOfContents(node()).get());
StringBuilder plainTextBuilder;
for (InlineTextBox* textBox = firstTextBox(); textBox; textBox = textBox->nextTextBox()) {
String text = m_text.substring(textBox->start(), textBox->len()).simplifyWhiteSpace(WTF::DoNotStripWhiteSpace);
plainTextBuilder.append(text);
if (textBox->nextTextBox() && textBox->nextTextBox()->start() > textBox->end() && text.length() && !text.right(1).containsOnlyWhitespace())
plainTextBuilder.append(" ");
}
return plainTextBuilder.toString();
}
void RenderText::absoluteRects(Vector<IntRect>& rects, const LayoutPoint& accumulatedOffset) const
{
for (InlineTextBox* box = firstTextBox(); box; box = box->nextTextBox())
rects.append(enclosingIntRect(FloatRect(accumulatedOffset + box->topLeft(), box->size())));
}
static FloatRect localQuadForTextBox(InlineTextBox* box, unsigned start, unsigned end, bool useSelectionHeight)
{
unsigned realEnd = min(box->end() + 1, end);
LayoutRect r = box->localSelectionRect(start, realEnd);
if (r.height()) {
if (!useSelectionHeight) {
if (box->isHorizontal()) {
r.setHeight(box->height());
r.setY(box->y());
} else {
r.setWidth(box->width());
r.setX(box->x());
}
}
return FloatRect(r);
}
return FloatRect();
}
void RenderText::absoluteRectsForRange(Vector<IntRect>& rects, unsigned start, unsigned end, bool useSelectionHeight, bool* wasFixed)
{
ASSERT(end == UINT_MAX || end <= INT_MAX);
ASSERT(start <= INT_MAX);
start = min(start, static_cast<unsigned>(INT_MAX));
end = min(end, static_cast<unsigned>(INT_MAX));
for (InlineTextBox* box = firstTextBox(); box; box = box->nextTextBox()) {
if (start <= box->start() && box->end() < end) {
FloatRect r = box->calculateBoundaries();
if (useSelectionHeight) {
LayoutRect selectionRect = box->localSelectionRect(start, end);
if (box->isHorizontal()) {
r.setHeight(selectionRect.height().toFloat());
r.setY(selectionRect.y().toFloat());
} else {
r.setWidth(selectionRect.width().toFloat());
r.setX(selectionRect.x().toFloat());
}
}
rects.append(localToAbsoluteQuad(r, 0, wasFixed).enclosingBoundingBox());
} else {
FloatRect rect = localQuadForTextBox(box, start, end, useSelectionHeight);
if (!rect.isZero())
rects.append(localToAbsoluteQuad(rect, 0, wasFixed).enclosingBoundingBox());
}
}
}
static IntRect ellipsisRectForBox(InlineTextBox* box, unsigned startPos, unsigned endPos)
{
if (!box)
return IntRect();
unsigned short truncation = box->truncation();
if (truncation == cNoTruncation)
return IntRect();
IntRect rect;
if (EllipsisBox* ellipsis = box->root().ellipsisBox()) {
int ellipsisStartPosition = max<int>(startPos - box->start(), 0);
int ellipsisEndPosition = min<int>(endPos - box->start(), box->len());
if (ellipsisEndPosition >= truncation && ellipsisStartPosition <= truncation)
return ellipsis->selectionRect();
}
return IntRect();
}
void RenderText::absoluteQuads(Vector<FloatQuad>& quads, bool* wasFixed, ClippingOption option) const
{
for (InlineTextBox* box = firstTextBox(); box; box = box->nextTextBox()) {
FloatRect boundaries = box->calculateBoundaries();
IntRect ellipsisRect = (option == ClipToEllipsis) ? ellipsisRectForBox(box, 0, textLength()) : IntRect();
if (!ellipsisRect.isEmpty()) {
if (style()->isHorizontalWritingMode())
boundaries.setWidth(ellipsisRect.maxX() - boundaries.x());
else
boundaries.setHeight(ellipsisRect.maxY() - boundaries.y());
}
quads.append(localToAbsoluteQuad(boundaries, 0, wasFixed));
}
}
void RenderText::absoluteQuads(Vector<FloatQuad>& quads, bool* wasFixed) const
{
absoluteQuads(quads, wasFixed, NoClipping);
}
void RenderText::absoluteQuadsForRange(Vector<FloatQuad>& quads, unsigned start, unsigned end, bool useSelectionHeight, bool* wasFixed)
{
ASSERT(end == UINT_MAX || end <= INT_MAX);
ASSERT(start <= INT_MAX);
start = min(start, static_cast<unsigned>(INT_MAX));
end = min(end, static_cast<unsigned>(INT_MAX));
for (InlineTextBox* box = firstTextBox(); box; box = box->nextTextBox()) {
if (start <= box->start() && box->end() < end) {
FloatRect r = box->calculateBoundaries();
if (useSelectionHeight) {
LayoutRect selectionRect = box->localSelectionRect(start, end);
if (box->isHorizontal()) {
r.setHeight(selectionRect.height().toFloat());
r.setY(selectionRect.y().toFloat());
} else {
r.setWidth(selectionRect.width().toFloat());
r.setX(selectionRect.x().toFloat());
}
}
quads.append(localToAbsoluteQuad(r, 0, wasFixed));
} else {
FloatRect rect = localQuadForTextBox(box, start, end, useSelectionHeight);
if (!rect.isZero())
quads.append(localToAbsoluteQuad(rect, 0, wasFixed));
}
}
}
InlineTextBox* RenderText::findNextInlineTextBox(int offset, int& pos) const
{
if (!m_firstTextBox)
return 0;
InlineTextBox* s = m_firstTextBox;
int off = s->len();
while (offset > off && s->nextTextBox()) {
s = s->nextTextBox();
off = s->start() + s->len();
}
pos = (offset > off ? s->len() : s->len() - (off - offset) );
return s;
}
enum ShouldAffinityBeDownstream { AlwaysDownstream, AlwaysUpstream, UpstreamIfPositionIsNotAtStart };
static bool lineDirectionPointFitsInBox(int pointLineDirection, InlineTextBox* box, ShouldAffinityBeDownstream& shouldAffinityBeDownstream)
{
shouldAffinityBeDownstream = AlwaysDownstream;
if (pointLineDirection <= box->logicalLeft()) {
shouldAffinityBeDownstream = !box->prevLeafChild() ? UpstreamIfPositionIsNotAtStart : AlwaysDownstream;
return true;
}
if (pointLineDirection < box->logicalRight()) {
shouldAffinityBeDownstream = UpstreamIfPositionIsNotAtStart;
return true;
}
if (!box->prevLeafChildIgnoringLineBreak() && pointLineDirection < box->logicalLeft())
return true;
if (!box->nextLeafChildIgnoringLineBreak()) {
shouldAffinityBeDownstream = UpstreamIfPositionIsNotAtStart;
return true;
}
return false;
}
static PositionWithAffinity createPositionWithAffinityForBox(const InlineBox* box, int offset, ShouldAffinityBeDownstream shouldAffinityBeDownstream)
{
EAffinity affinity = VP_DEFAULT_AFFINITY;
switch (shouldAffinityBeDownstream) {
case AlwaysDownstream:
affinity = DOWNSTREAM;
break;
case AlwaysUpstream:
affinity = VP_UPSTREAM_IF_POSSIBLE;
break;
case UpstreamIfPositionIsNotAtStart:
affinity = offset > box->caretMinOffset() ? VP_UPSTREAM_IF_POSSIBLE : DOWNSTREAM;
break;
}
int textStartOffset = box->renderer().isText() ? toRenderText(box->renderer()).textStartOffset() : 0;
return box->renderer().createPositionWithAffinity(offset + textStartOffset, affinity);
}
static PositionWithAffinity createPositionWithAffinityForBoxAfterAdjustingOffsetForBiDi(const InlineTextBox* box, int offset, ShouldAffinityBeDownstream shouldAffinityBeDownstream)
{
ASSERT(box);
ASSERT(offset >= 0);
if (offset && static_cast<unsigned>(offset) < box->len())
return createPositionWithAffinityForBox(box, box->start() + offset, shouldAffinityBeDownstream);
bool positionIsAtStartOfBox = !offset;
if (positionIsAtStartOfBox == box->isLeftToRightDirection()) {
const InlineBox* prevBox = box->prevLeafChildIgnoringLineBreak();
if ((prevBox && prevBox->bidiLevel() == box->bidiLevel())
|| box->renderer().containingBlock()->style()->direction() == box->direction())
return createPositionWithAffinityForBox(box, box->caretLeftmostOffset(), shouldAffinityBeDownstream);
if (prevBox && prevBox->bidiLevel() > box->bidiLevel()) {
const InlineBox* leftmostBox;
do {
leftmostBox = prevBox;
prevBox = leftmostBox->prevLeafChildIgnoringLineBreak();
} while (prevBox && prevBox->bidiLevel() > box->bidiLevel());
return createPositionWithAffinityForBox(leftmostBox, leftmostBox->caretRightmostOffset(), shouldAffinityBeDownstream);
}
if (!prevBox || prevBox->bidiLevel() < box->bidiLevel()) {
const InlineBox* rightmostBox;
const InlineBox* nextBox = box;
do {
rightmostBox = nextBox;
nextBox = rightmostBox->nextLeafChildIgnoringLineBreak();
} while (nextBox && nextBox->bidiLevel() >= box->bidiLevel());
return createPositionWithAffinityForBox(rightmostBox,
box->isLeftToRightDirection() ? rightmostBox->caretMaxOffset() : rightmostBox->caretMinOffset(), shouldAffinityBeDownstream);
}
return createPositionWithAffinityForBox(box, box->caretRightmostOffset(), shouldAffinityBeDownstream);
}
const InlineBox* nextBox = box->nextLeafChildIgnoringLineBreak();
if ((nextBox && nextBox->bidiLevel() == box->bidiLevel())
|| box->renderer().containingBlock()->style()->direction() == box->direction())
return createPositionWithAffinityForBox(box, box->caretRightmostOffset(), shouldAffinityBeDownstream);
if (nextBox && nextBox->bidiLevel() > box->bidiLevel()) {
const InlineBox* rightmostBox;
do {
rightmostBox = nextBox;
nextBox = rightmostBox->nextLeafChildIgnoringLineBreak();
} while (nextBox && nextBox->bidiLevel() > box->bidiLevel());
return createPositionWithAffinityForBox(rightmostBox, rightmostBox->caretLeftmostOffset(), shouldAffinityBeDownstream);
}
if (!nextBox || nextBox->bidiLevel() < box->bidiLevel()) {
const InlineBox* leftmostBox;
const InlineBox* prevBox = box;
do {
leftmostBox = prevBox;
prevBox = leftmostBox->prevLeafChildIgnoringLineBreak();
} while (prevBox && prevBox->bidiLevel() >= box->bidiLevel());
return createPositionWithAffinityForBox(leftmostBox,
box->isLeftToRightDirection() ? leftmostBox->caretMinOffset() : leftmostBox->caretMaxOffset(), shouldAffinityBeDownstream);
}
return createPositionWithAffinityForBox(box, box->caretLeftmostOffset(), shouldAffinityBeDownstream);
}
PositionWithAffinity RenderText::positionForPoint(const LayoutPoint& point)
{
if (!firstTextBox() || textLength() == 0)
return createPositionWithAffinity(0, DOWNSTREAM);
LayoutUnit pointLineDirection = firstTextBox()->isHorizontal() ? point.x() : point.y();
LayoutUnit pointBlockDirection = firstTextBox()->isHorizontal() ? point.y() : point.x();
bool blocksAreFlipped = style()->isFlippedBlocksWritingMode();
InlineTextBox* lastBox = 0;
for (InlineTextBox* box = firstTextBox(); box; box = box->nextTextBox()) {
if (box->isLineBreak() && !box->prevLeafChild() && box->nextLeafChild() && !box->nextLeafChild()->isLineBreak())
box = box->nextTextBox();
RootInlineBox& rootBox = box->root();
LayoutUnit top = min(rootBox.selectionTop(), rootBox.lineTop());
if (pointBlockDirection > top || (!blocksAreFlipped && pointBlockDirection == top)) {
LayoutUnit bottom = rootBox.selectionBottom();
if (rootBox.nextRootBox())
bottom = min(bottom, rootBox.nextRootBox()->lineTop());
if (pointBlockDirection < bottom || (blocksAreFlipped && pointBlockDirection == bottom)) {
ShouldAffinityBeDownstream shouldAffinityBeDownstream;
if (lineDirectionPointFitsInBox(pointLineDirection, box, shouldAffinityBeDownstream))
return createPositionWithAffinityForBoxAfterAdjustingOffsetForBiDi(box, box->offsetForPosition(pointLineDirection.toFloat()), shouldAffinityBeDownstream);
}
}
lastBox = box;
}
if (lastBox) {
ShouldAffinityBeDownstream shouldAffinityBeDownstream;
lineDirectionPointFitsInBox(pointLineDirection, lastBox, shouldAffinityBeDownstream);
return createPositionWithAffinityForBoxAfterAdjustingOffsetForBiDi(lastBox, lastBox->offsetForPosition(pointLineDirection.toFloat()) + lastBox->start(), shouldAffinityBeDownstream);
}
return createPositionWithAffinity(0, DOWNSTREAM);
}
LayoutRect RenderText::localCaretRect(InlineBox* inlineBox, int caretOffset, LayoutUnit* extraWidthToEndOfLine)
{
if (!inlineBox)
return LayoutRect();
ASSERT(inlineBox->isInlineTextBox());
if (!inlineBox->isInlineTextBox())
return LayoutRect();
InlineTextBox* box = toInlineTextBox(inlineBox);
int height = box->root().selectionHeight();
int top = box->root().selectionTop();
float left = box->positionForOffset(caretOffset);
int caretWidthLeftOfOffset = caretWidth / 2;
left -= caretWidthLeftOfOffset;
int caretWidthRightOfOffset = caretWidth - caretWidthLeftOfOffset;
left = roundf(left);
float rootLeft = box->root().logicalLeft();
float rootRight = box->root().logicalRight();
if (extraWidthToEndOfLine)
*extraWidthToEndOfLine = (box->root().logicalWidth() + rootLeft) - (left + 1);
RenderBlock* cb = containingBlock();
RenderStyle* cbStyle = cb->style();
float leftEdge;
float rightEdge;
leftEdge = min<float>(0, rootLeft);
rightEdge = max<float>(cb->logicalWidth().toFloat(), rootRight);
bool rightAligned = false;
switch (cbStyle->textAlign()) {
case RIGHT:
case WEBKIT_RIGHT:
rightAligned = true;
break;
case LEFT:
case WEBKIT_LEFT:
case CENTER:
case WEBKIT_CENTER:
break;
case JUSTIFY:
case TASTART:
rightAligned = !cbStyle->isLeftToRightDirection();
break;
case TAEND:
rightAligned = cbStyle->isLeftToRightDirection();
break;
}
if (rightAligned) {
left = max(left, leftEdge);
left = min(left, rootRight - caretWidth);
} else {
left = min(left, rightEdge - caretWidthRightOfOffset);
left = max(left, rootLeft);
}
return style()->isHorizontalWritingMode() ? IntRect(left, top, caretWidth, height) : IntRect(top, left, height, caretWidth);
}
ALWAYS_INLINE float RenderText::widthFromCache(const Font& f, int start, int len, float xPos, TextDirection textDirection, HashSet<const SimpleFontData*>* fallbackFonts, GlyphOverflow* glyphOverflow) const
{
if (style()->hasTextCombine() && isCombineText()) {
const RenderCombineText* combineText = toRenderCombineText(this);
if (combineText->isCombined())
return combineText->combinedTextWidth(f);
}
if (f.isFixedPitch() && !f.fontDescription().variant() && m_isAllASCII && (!glyphOverflow || !glyphOverflow->computeBounds)) {
float monospaceCharacterWidth = f.spaceWidth();
float w = 0;
bool isSpace;
ASSERT(m_text);
StringImpl& text = *m_text.impl();
for (int i = start; i < start + len; i++) {
char c = text[i];
if (c <= ' ') {
if (c == ' ' || c == '\n') {
w += monospaceCharacterWidth;
isSpace = true;
} else if (c == '\t') {
if (style()->collapseWhiteSpace()) {
w += monospaceCharacterWidth;
isSpace = true;
} else {
w += f.tabWidth(style()->tabSize(), xPos + w);
isSpace = false;
}
} else
isSpace = false;
} else {
w += monospaceCharacterWidth;
isSpace = false;
}
if (isSpace && i > start)
w += f.fontDescription().wordSpacing();
}
return w;
}
TextRun run = RenderBlockFlow::constructTextRun(const_cast<RenderText*>(this), f, this, start, len, style(), textDirection);
run.setCharactersLength(textLength() - start);
ASSERT(run.charactersLength() >= run.length());
run.setCharacterScanForCodePath(!canUseSimpleFontCodePath());
run.setTabSize(!style()->collapseWhiteSpace(), style()->tabSize());
run.setXPos(xPos);
return f.width(run, fallbackFonts, glyphOverflow);
}
void RenderText::trimmedPrefWidths(float leadWidth,
float& firstLineMinWidth, bool& hasBreakableStart,
float& lastLineMinWidth, bool& hasBreakableEnd,
bool& hasBreakableChar, bool& hasBreak,
float& firstLineMaxWidth, float& lastLineMaxWidth,
float& minWidth, float& maxWidth, bool& stripFrontSpaces,
TextDirection direction)
{
bool collapseWhiteSpace = style()->collapseWhiteSpace();
if (!collapseWhiteSpace)
stripFrontSpaces = false;
if (m_hasTab || preferredLogicalWidthsDirty())
computePreferredLogicalWidths(leadWidth);
hasBreakableStart = !stripFrontSpaces && m_hasBreakableStart;
hasBreakableEnd = m_hasBreakableEnd;
int len = textLength();
if (!len || (stripFrontSpaces && text().impl()->containsOnlyWhitespace())) {
firstLineMinWidth = 0;
lastLineMinWidth = 0;
firstLineMaxWidth = 0;
lastLineMaxWidth = 0;
minWidth = 0;
maxWidth = 0;
hasBreak = false;
return;
}
minWidth = m_minWidth;
maxWidth = m_maxWidth;
firstLineMinWidth = m_firstLineMinWidth;
lastLineMinWidth = m_lastLineLineMinWidth;
hasBreakableChar = m_hasBreakableChar;
hasBreak = m_hasBreak;
ASSERT(m_text);
StringImpl& text = *m_text.impl();
if (text[0] == ' ' || (text[0] == '\n' && !style()->preserveNewline()) || text[0] == '\t') {
const Font& font = style()->font();
if (stripFrontSpaces) {
const UChar space = ' ';
float spaceWidth = font.width(RenderBlockFlow::constructTextRun(this, font, &space, 1, style(), direction));
maxWidth -= spaceWidth;
} else {
maxWidth += font.fontDescription().wordSpacing();
}
}
stripFrontSpaces = collapseWhiteSpace && m_hasEndWhiteSpace;
if (!style()->autoWrap() || minWidth > maxWidth)
minWidth = maxWidth;
if (hasBreak) {
const Font& f = style()->font();
bool firstLine = true;
firstLineMaxWidth = maxWidth;
lastLineMaxWidth = maxWidth;
for (int i = 0; i < len; i++) {
int linelen = 0;
while (i + linelen < len && text[i + linelen] != '\n')
linelen++;
if (linelen) {
lastLineMaxWidth = widthFromCache(f, i, linelen, leadWidth + lastLineMaxWidth, direction, 0, 0);
if (firstLine) {
firstLine = false;
leadWidth = 0;
firstLineMaxWidth = lastLineMaxWidth;
}
i += linelen;
} else if (firstLine) {
firstLineMaxWidth = 0;
firstLine = false;
leadWidth = 0;
}
if (i == len - 1) {
lastLineMaxWidth = 0;
}
}
}
}
float RenderText::minLogicalWidth() const
{
if (preferredLogicalWidthsDirty())
const_cast<RenderText*>(this)->computePreferredLogicalWidths(0);
return m_minWidth;
}
float RenderText::maxLogicalWidth() const
{
if (preferredLogicalWidthsDirty())
const_cast<RenderText*>(this)->computePreferredLogicalWidths(0);
return m_maxWidth;
}
void RenderText::computePreferredLogicalWidths(float leadWidth)
{
HashSet<const SimpleFontData*> fallbackFonts;
GlyphOverflow glyphOverflow;
computePreferredLogicalWidths(leadWidth, fallbackFonts, glyphOverflow);
if (fallbackFonts.isEmpty() && !glyphOverflow.left && !glyphOverflow.right && !glyphOverflow.top && !glyphOverflow.bottom)
m_knownToHaveNoOverflowAndNoFallbackFonts = true;
}
static inline float hyphenWidth(RenderText* renderer, const Font& font, TextDirection direction)
{
RenderStyle* style = renderer->style();
return font.width(RenderBlockFlow::constructTextRun(renderer, font, style->hyphenString().string(), style, direction));
}
void RenderText::computePreferredLogicalWidths(float leadWidth, HashSet<const SimpleFontData*>& fallbackFonts, GlyphOverflow& glyphOverflow)
{
ASSERT(m_hasTab || preferredLogicalWidthsDirty() || !m_knownToHaveNoOverflowAndNoFallbackFonts);
m_minWidth = 0;
m_maxWidth = 0;
m_firstLineMinWidth = 0;
m_lastLineLineMinWidth = 0;
if (isBR())
return;
float currMinWidth = 0;
float currMaxWidth = 0;
m_hasBreakableChar = false;
m_hasBreak = false;
m_hasTab = false;
m_hasBreakableStart = false;
m_hasBreakableEnd = false;
m_hasEndWhiteSpace = false;
RenderStyle* styleToUse = style();
const Font& f = styleToUse->font();
float wordSpacing = styleToUse->wordSpacing();
int len = textLength();
LazyLineBreakIterator breakIterator(m_text, styleToUse->locale());
bool needsWordSpacing = false;
bool ignoringSpaces = false;
bool isSpace = false;
bool firstWord = true;
bool firstLine = true;
int nextBreakable = -1;
int lastWordBoundary = 0;
float cachedWordTrailingSpaceWidth[2] = { 0, 0 };
int firstGlyphLeftOverflow = -1;
bool breakAll = (styleToUse->wordBreak() == BreakAllWordBreak || styleToUse->wordBreak() == BreakWordBreak) && styleToUse->autoWrap();
TextRun textRun(text());
BidiResolver<TextRunIterator, BidiCharacterRun> bidiResolver;
BidiCharacterRun* run;
TextDirection textDirection = styleToUse->direction();
if (isOverride(styleToUse->unicodeBidi())) {
run = 0;
} else {
BidiStatus status(LTR, false);
status.last = status.lastStrong = WTF::Unicode::OtherNeutral;
bidiResolver.setStatus(status);
bidiResolver.setPositionIgnoringNestedIsolates(TextRunIterator(&textRun, 0));
bool hardLineBreak = false;
bool reorderRuns = false;
bidiResolver.createBidiRunsForLine(TextRunIterator(&textRun, textRun.length()), NoVisualOverride, hardLineBreak, reorderRuns);
BidiRunList<BidiCharacterRun>& bidiRuns = bidiResolver.runs();
run = bidiRuns.firstRun();
}
for (int i = 0; i < len; i++) {
UChar c = uncheckedCharacterAt(i);
if (run) {
while (i > run->stop() || (run->next() && run->next()->direction() == run->direction()))
run = run->next();
ASSERT(run);
ASSERT(i <= run->stop());
textDirection = run->direction();
}
bool previousCharacterIsSpace = isSpace;
bool isNewline = false;
if (c == '\n') {
if (styleToUse->preserveNewline()) {
m_hasBreak = true;
isNewline = true;
isSpace = false;
} else
isSpace = true;
} else if (c == '\t') {
if (!styleToUse->collapseWhiteSpace()) {
m_hasTab = true;
isSpace = false;
} else
isSpace = true;
} else
isSpace = c == ' ';
bool isBreakableLocation = isNewline || (isSpace && styleToUse->autoWrap());
if (!i)
m_hasBreakableStart = isBreakableLocation;
if (i == len - 1) {
m_hasBreakableEnd = isBreakableLocation;
m_hasEndWhiteSpace = isNewline || isSpace;
}
if (!ignoringSpaces && styleToUse->collapseWhiteSpace() && previousCharacterIsSpace && isSpace)
ignoringSpaces = true;
if (ignoringSpaces && !isSpace)
ignoringSpaces = false;
if (ignoringSpaces) {
ASSERT(lastWordBoundary == i);
lastWordBoundary++;
continue;
} else if (c == softHyphen) {
currMaxWidth += widthFromCache(f, lastWordBoundary, i - lastWordBoundary, leadWidth + currMaxWidth, textDirection, &fallbackFonts, &glyphOverflow);
if (firstGlyphLeftOverflow < 0)
firstGlyphLeftOverflow = glyphOverflow.left;
lastWordBoundary = i + 1;
continue;
}
bool hasBreak = breakAll || isBreakable(breakIterator, i, nextBreakable);
bool betweenWords = true;
int j = i;
while (c != '\n' && c != ' ' && c != '\t' && (c != softHyphen)) {
j++;
if (j == len)
break;
c = uncheckedCharacterAt(j);
if (isBreakable(breakIterator, j, nextBreakable) && characterAt(j - 1) != softHyphen)
break;
if (breakAll) {
betweenWords = false;
break;
}
}
if (run)
j = min(j, run->stop() + 1);
int wordLen = j - i;
if (wordLen) {
bool isSpace = (j < len) && c == ' ';
float wordTrailingSpaceWidth = 0;
if (isSpace && (f.fontDescription().typesettingFeatures() & Kerning)) {
ASSERT(textDirection >=0 && textDirection <= 1);
if (!cachedWordTrailingSpaceWidth[textDirection])
cachedWordTrailingSpaceWidth[textDirection] = f.width(RenderBlockFlow::constructTextRun(this, f, &space, 1, styleToUse, textDirection)) + wordSpacing;
wordTrailingSpaceWidth = cachedWordTrailingSpaceWidth[textDirection];
}
float w;
if (wordTrailingSpaceWidth && isSpace)
w = widthFromCache(f, i, wordLen + 1, leadWidth + currMaxWidth, textDirection, &fallbackFonts, &glyphOverflow) - wordTrailingSpaceWidth;
else {
w = widthFromCache(f, i, wordLen, leadWidth + currMaxWidth, textDirection, &fallbackFonts, &glyphOverflow);
if (c == softHyphen)
currMinWidth += hyphenWidth(this, f, textDirection);
}
if (firstGlyphLeftOverflow < 0)
firstGlyphLeftOverflow = glyphOverflow.left;
currMinWidth += w;
if (betweenWords) {
if (lastWordBoundary == i)
currMaxWidth += w;
else
currMaxWidth += widthFromCache(f, lastWordBoundary, j - lastWordBoundary, leadWidth + currMaxWidth, textDirection, &fallbackFonts, &glyphOverflow);
lastWordBoundary = j;
}
bool isCollapsibleWhiteSpace = (j < len) && styleToUse->isCollapsibleWhiteSpace(c);
if (j < len && styleToUse->autoWrap())
m_hasBreakableChar = true;
if (wordSpacing && (isSpace || isCollapsibleWhiteSpace) && !containsOnlyWhitespace(j, len-j))
currMaxWidth += wordSpacing;
if (firstWord) {
firstWord = false;
if (hasBreak)
m_hasBreakableChar = true;
m_firstLineMinWidth = hasBreak ? 0 : currMinWidth;
}
m_lastLineLineMinWidth = currMinWidth;
if (currMinWidth > m_minWidth)
m_minWidth = currMinWidth;
currMinWidth = 0;
i += wordLen - 1;
} else {
if (style()->autoWrap() || isNewline)
m_hasBreakableChar = true;
if (currMinWidth > m_minWidth)
m_minWidth = currMinWidth;
currMinWidth = 0;
if (isNewline) {
if (firstLine) {
firstLine = false;
leadWidth = 0;
if (!styleToUse->autoWrap())
m_firstLineMinWidth = currMaxWidth;
}
if (currMaxWidth > m_maxWidth)
m_maxWidth = currMaxWidth;
currMaxWidth = 0;
} else {
TextRun run = RenderBlockFlow::constructTextRun(this, f, this, i, 1, styleToUse, textDirection);
run.setCharactersLength(len - i);
ASSERT(run.charactersLength() >= run.length());
run.setTabSize(!style()->collapseWhiteSpace(), style()->tabSize());
run.setXPos(leadWidth + currMaxWidth);
currMaxWidth += f.width(run);
glyphOverflow.right = 0;
needsWordSpacing = isSpace && !previousCharacterIsSpace && i == len - 1;
}
ASSERT(lastWordBoundary == i);
lastWordBoundary++;
}
}
if (run)
bidiResolver.runs().deleteRuns();
if (firstGlyphLeftOverflow > 0)
glyphOverflow.left = firstGlyphLeftOverflow;
if ((needsWordSpacing && len > 1) || (ignoringSpaces && !firstWord))
currMaxWidth += wordSpacing;
m_minWidth = max(currMinWidth, m_minWidth);
m_maxWidth = max(currMaxWidth, m_maxWidth);
if (!styleToUse->autoWrap())
m_minWidth = m_maxWidth;
if (styleToUse->whiteSpace() == PRE) {
if (firstLine)
m_firstLineMinWidth = m_maxWidth;
m_lastLineLineMinWidth = currMaxWidth;
}
clearPreferredLogicalWidthsDirty();
}
bool RenderText::isAllCollapsibleWhitespace() const
{
unsigned length = textLength();
if (is8Bit()) {
for (unsigned i = 0; i < length; ++i) {
if (!style()->isCollapsibleWhiteSpace(characters8()[i]))
return false;
}
return true;
}
for (unsigned i = 0; i < length; ++i) {
if (!style()->isCollapsibleWhiteSpace(characters16()[i]))
return false;
}
return true;
}
bool RenderText::containsOnlyWhitespace(unsigned from, unsigned len) const
{
ASSERT(m_text);
StringImpl& text = *m_text.impl();
unsigned currPos;
for (currPos = from;
currPos < from + len && (text[currPos] == '\n' || text[currPos] == ' ' || text[currPos] == '\t');
currPos++) { }
return currPos >= (from + len);
}
FloatPoint RenderText::firstRunOrigin() const
{
return IntPoint(firstRunX(), firstRunY());
}
float RenderText::firstRunX() const
{
return m_firstTextBox ? m_firstTextBox->x() : 0;
}
float RenderText::firstRunY() const
{
return m_firstTextBox ? m_firstTextBox->y() : 0;
}
void RenderText::setSelectionState(SelectionState state)
{
RenderObject::setSelectionState(state);
if (canUpdateSelectionOnRootLineBoxes()) {
if (state == SelectionStart || state == SelectionEnd || state == SelectionBoth) {
int startPos, endPos;
selectionStartEnd(startPos, endPos);
if (selectionState() == SelectionStart) {
endPos = textLength();
if (startPos && startPos == endPos)
startPos = endPos - 1;
} else if (selectionState() == SelectionEnd)
startPos = 0;
for (InlineTextBox* box = firstTextBox(); box; box = box->nextTextBox()) {
if (box->isSelected(startPos, endPos)) {
box->root().setHasSelectedChildren(true);
}
}
} else {
for (InlineTextBox* box = firstTextBox(); box; box = box->nextTextBox()) {
box->root().setHasSelectedChildren(state == SelectionInside);
}
}
}
RenderBlock* containingBlock = this->containingBlock();
if (containingBlock && !containingBlock->isRenderView())
containingBlock->setSelectionState(state);
}
void RenderText::setTextWithOffset(PassRefPtr<StringImpl> text, unsigned offset, unsigned len, bool force)
{
if (!force && equal(m_text.impl(), text.get()))
return;
unsigned oldLen = textLength();
unsigned newLen = text->length();
int delta = newLen - oldLen;
unsigned end = len ? offset + len - 1 : offset;
RootInlineBox* firstRootBox = 0;
RootInlineBox* lastRootBox = 0;
bool dirtiedLines = false;
for (InlineTextBox* curr = firstTextBox(); curr; curr = curr->nextTextBox()) {
if (curr->end() < offset)
continue;
if (curr->start() > end) {
curr->offsetRun(delta);
RootInlineBox* root = &curr->root();
if (!firstRootBox) {
firstRootBox = root;
firstRootBox->markDirty();
dirtiedLines = true;
}
lastRootBox = root;
} else if (curr->end() >= offset && curr->end() <= end) {
curr->dirtyLineBoxes();
dirtiedLines = true;
} else if (curr->start() <= offset && curr->end() >= end) {
curr->dirtyLineBoxes();
dirtiedLines = true;
} else if (curr->start() <= end && curr->end() >= end) {
curr->dirtyLineBoxes();
dirtiedLines = true;
}
}
if (lastRootBox)
lastRootBox = lastRootBox->nextRootBox();
if (firstRootBox) {
RootInlineBox* prev = firstRootBox->prevRootBox();
if (prev)
firstRootBox = prev;
} else if (lastTextBox()) {
ASSERT(!lastRootBox);
firstRootBox = &lastTextBox()->root();
firstRootBox->markDirty();
dirtiedLines = true;
}
for (RootInlineBox* curr = firstRootBox; curr && curr != lastRootBox; curr = curr->nextRootBox()) {
if (curr->lineBreakObj() == this && curr->lineBreakPos() > end)
curr->setLineBreakPos(clampToInteger(curr->lineBreakPos() + delta));
}
if (!firstTextBox() && parent()) {
parent()->dirtyLinesFromChangedChild(this);
dirtiedLines = true;
}
m_linesDirty = dirtiedLines;
setText(text, force || dirtiedLines);
}
void RenderText::transformText()
{
if (RefPtr<StringImpl> textToTransform = originalText())
setText(textToTransform.release(), true);
}
static inline bool isInlineFlowOrEmptyText(const RenderObject* o)
{
if (o->isRenderInline())
return true;
if (!o->isText())
return false;
return toRenderText(o)->text().isEmpty();
}
UChar RenderText::previousCharacter() const
{
const RenderObject* previousText = this;
while ((previousText = previousText->previousInPreOrder()))
if (!isInlineFlowOrEmptyText(previousText))
break;
UChar prev = ' ';
if (previousText && previousText->isText())
if (StringImpl* previousString = toRenderText(previousText)->text().impl())
prev = (*previousString)[previousString->length() - 1];
return prev;
}
void RenderText::addLayerHitTestRects(LayerHitTestRects&, const RenderLayer* currentLayer, const LayoutPoint& layerOffset, const LayoutRect& containerRect) const
{
}
void applyTextTransform(const RenderStyle* style, String& text, UChar previousCharacter)
{
if (!style)
return;
switch (style->textTransform()) {
case TTNONE:
break;
case CAPITALIZE:
makeCapitalized(&text, previousCharacter);
break;
case UPPERCASE:
text = text.upper(style->locale());
break;
case LOWERCASE:
text = text.lower(style->locale());
break;
}
}
void RenderText::setTextInternal(PassRefPtr<StringImpl> text)
{
ASSERT(text);
m_text = text;
if (style()) {
applyTextTransform(style(), m_text, previousCharacter());
switch (style()->textSecurity()) {
case TSNONE:
break;
case TSCIRCLE:
secureText(whiteBullet);
break;
case TSDISC:
secureText(bullet);
break;
case TSSQUARE:
secureText(blackSquare);
}
}
ASSERT(m_text);
ASSERT(!isBR() || (textLength() == 1 && m_text[0] == '\n'));
m_isAllASCII = m_text.containsOnlyASCII();
m_canUseSimpleFontCodePath = computeCanUseSimpleFontCodePath();
}
void RenderText::secureText(UChar mask)
{
if (!m_text.length())
return;
int lastTypedCharacterOffsetToReveal = -1;
UChar revealedText;
SecureTextTimer* secureTextTimer = gSecureTextTimers ? gSecureTextTimers->get(this) : 0;
if (secureTextTimer && secureTextTimer->isActive()) {
lastTypedCharacterOffsetToReveal = secureTextTimer->lastTypedCharacterOffset();
if (lastTypedCharacterOffsetToReveal >= 0)
revealedText = m_text[lastTypedCharacterOffsetToReveal];
}
m_text.fill(mask);
if (lastTypedCharacterOffsetToReveal >= 0) {
m_text.replace(lastTypedCharacterOffsetToReveal, 1, String(&revealedText, 1));
secureTextTimer->invalidate();
}
}
void RenderText::setText(PassRefPtr<StringImpl> text, bool force)
{
ASSERT(text);
if (!force && equal(m_text.impl(), text.get()))
return;
setTextInternal(text);
if (parent())
setNeedsLayoutAndPrefWidthsRecalc();
m_knownToHaveNoOverflowAndNoFallbackFonts = false;
if (AXObjectCache* cache = document().existingAXObjectCache())
cache->textChanged(this);
}
void RenderText::dirtyLineBoxes(bool fullLayout)
{
if (fullLayout)
deleteTextBoxes();
else if (!m_linesDirty) {
for (InlineTextBox* box = firstTextBox(); box; box = box->nextTextBox())
box->dirtyLineBoxes();
}
m_linesDirty = false;
}
InlineTextBox* RenderText::createTextBox()
{
return new InlineTextBox(*this);
}
InlineTextBox* RenderText::createInlineTextBox()
{
InlineTextBox* textBox = createTextBox();
if (!m_firstTextBox)
m_firstTextBox = m_lastTextBox = textBox;
else {
m_lastTextBox->setNextTextBox(textBox);
textBox->setPreviousTextBox(m_lastTextBox);
m_lastTextBox = textBox;
}
textBox->setIsText(true);
return textBox;
}
void RenderText::positionLineBox(InlineBox* box)
{
InlineTextBox* s = toInlineTextBox(box);
if (!s->len()) {
s->remove();
if (m_firstTextBox == s)
m_firstTextBox = s->nextTextBox();
else
s->prevTextBox()->setNextTextBox(s->nextTextBox());
if (m_lastTextBox == s)
m_lastTextBox = s->prevTextBox();
else
s->nextTextBox()->setPreviousTextBox(s->prevTextBox());
s->destroy();
return;
}
m_containsReversedText |= !s->isLeftToRightDirection();
}
float RenderText::width(unsigned from, unsigned len, float xPos, TextDirection textDirection, bool firstLine, HashSet<const SimpleFontData*>* fallbackFonts, GlyphOverflow* glyphOverflow) const
{
if (from >= textLength())
return 0;
if (from + len > textLength())
len = textLength() - from;
return width(from, len, style(firstLine)->font(), xPos, textDirection, fallbackFonts, glyphOverflow);
}
float RenderText::width(unsigned from, unsigned len, const Font& f, float xPos, TextDirection textDirection, HashSet<const SimpleFontData*>* fallbackFonts, GlyphOverflow* glyphOverflow) const
{
ASSERT(from + len <= textLength());
if (!textLength())
return 0;
float w;
if (&f == &style()->font()) {
if (!style()->preserveNewline() && !from && len == textLength() && (!glyphOverflow || !glyphOverflow->computeBounds)) {
if (fallbackFonts) {
ASSERT(glyphOverflow);
if (preferredLogicalWidthsDirty() || !m_knownToHaveNoOverflowAndNoFallbackFonts) {
const_cast<RenderText*>(this)->computePreferredLogicalWidths(0, *fallbackFonts, *glyphOverflow);
if (fallbackFonts->isEmpty() && !glyphOverflow->left && !glyphOverflow->right && !glyphOverflow->top && !glyphOverflow->bottom)
m_knownToHaveNoOverflowAndNoFallbackFonts = true;
}
w = m_maxWidth;
} else {
w = maxLogicalWidth();
}
} else {
w = widthFromCache(f, from, len, xPos, textDirection, fallbackFonts, glyphOverflow);
}
} else {
TextRun run = RenderBlockFlow::constructTextRun(const_cast<RenderText*>(this), f, this, from, len, style(), textDirection);
run.setCharactersLength(textLength() - from);
ASSERT(run.charactersLength() >= run.length());
run.setCharacterScanForCodePath(!canUseSimpleFontCodePath());
run.setTabSize(!style()->collapseWhiteSpace(), style()->tabSize());
run.setXPos(xPos);
w = f.width(run, fallbackFonts, glyphOverflow);
}
return w;
}
IntRect RenderText::linesBoundingBox() const
{
IntRect result;
ASSERT(!firstTextBox() == !lastTextBox());
if (firstTextBox() && lastTextBox()) {
float logicalLeftSide = 0;
float logicalRightSide = 0;
for (InlineTextBox* curr = firstTextBox(); curr; curr = curr->nextTextBox()) {
if (curr == firstTextBox() || curr->logicalLeft() < logicalLeftSide)
logicalLeftSide = curr->logicalLeft();
if (curr == firstTextBox() || curr->logicalRight() > logicalRightSide)
logicalRightSide = curr->logicalRight();
}
bool isHorizontal = style()->isHorizontalWritingMode();
float x = isHorizontal ? logicalLeftSide : firstTextBox()->x();
float y = isHorizontal ? firstTextBox()->y() : logicalLeftSide;
float width = isHorizontal ? logicalRightSide - logicalLeftSide : lastTextBox()->logicalBottom() - x;
float height = isHorizontal ? lastTextBox()->logicalBottom() - y : logicalRightSide - logicalLeftSide;
result = enclosingIntRect(FloatRect(x, y, width, height));
}
return result;
}
LayoutRect RenderText::linesVisualOverflowBoundingBox() const
{
if (!firstTextBox())
return LayoutRect();
LayoutUnit logicalLeftSide = LayoutUnit::max();
LayoutUnit logicalRightSide = LayoutUnit::min();
for (InlineTextBox* curr = firstTextBox(); curr; curr = curr->nextTextBox()) {
logicalLeftSide = min(logicalLeftSide, curr->logicalLeftVisualOverflow());
logicalRightSide = max(logicalRightSide, curr->logicalRightVisualOverflow());
}
LayoutUnit logicalTop = firstTextBox()->logicalTopVisualOverflow();
LayoutUnit logicalWidth = logicalRightSide - logicalLeftSide;
LayoutUnit logicalHeight = lastTextBox()->logicalBottomVisualOverflow() - logicalTop;
LayoutRect rect(logicalLeftSide, logicalTop, logicalWidth, logicalHeight);
if (!style()->isHorizontalWritingMode())
rect = rect.transposedRect();
return rect;
}
LayoutRect RenderText::clippedOverflowRectForRepaint(const RenderLayerModelObject* repaintContainer) const
{
RenderObject* rendererToRepaint = containingBlock();
RenderObject* enclosingLayerRenderer = enclosingLayer()->renderer();
if (enclosingLayerRenderer != rendererToRepaint && !rendererToRepaint->isDescendantOf(enclosingLayerRenderer))
rendererToRepaint = enclosingLayerRenderer;
if (repaintContainer && repaintContainer != rendererToRepaint && !rendererToRepaint->isDescendantOf(repaintContainer))
return repaintContainer->clippedOverflowRectForRepaint(repaintContainer);
return rendererToRepaint->clippedOverflowRectForRepaint(repaintContainer);
}
LayoutRect RenderText::selectionRectForRepaint(const RenderLayerModelObject* repaintContainer, bool clipToVisibleContent)
{
ASSERT(!needsLayout());
if (selectionState() == SelectionNone)
return LayoutRect();
RenderBlock* cb = containingBlock();
if (!cb)
return LayoutRect();
int startPos, endPos;
if (selectionState() == SelectionInside) {
startPos = 0;
endPos = textLength();
} else {
selectionStartEnd(startPos, endPos);
if (selectionState() == SelectionStart)
endPos = textLength();
else if (selectionState() == SelectionEnd)
startPos = 0;
}
if (startPos == endPos)
return IntRect();
LayoutRect rect;
for (InlineTextBox* box = firstTextBox(); box; box = box->nextTextBox()) {
rect.unite(box->localSelectionRect(startPos, endPos));
rect.unite(ellipsisRectForBox(box, startPos, endPos));
}
if (clipToVisibleContent)
computeRectForRepaint(repaintContainer, rect);
else {
if (cb->hasColumns())
cb->adjustRectForColumns(rect);
rect = localToContainerQuad(FloatRect(rect), repaintContainer).enclosingBoundingBox();
}
return rect;
}
int RenderText::caretMinOffset() const
{
InlineTextBox* box = firstTextBox();
if (!box)
return 0;
int minOffset = box->start();
for (box = box->nextTextBox(); box; box = box->nextTextBox())
minOffset = min<int>(minOffset, box->start());
return minOffset;
}
int RenderText::caretMaxOffset() const
{
InlineTextBox* box = lastTextBox();
if (!lastTextBox())
return textLength();
int maxOffset = box->start() + box->len();
for (box = box->prevTextBox(); box; box = box->prevTextBox())
maxOffset = max<int>(maxOffset, box->start() + box->len());
return maxOffset;
}
unsigned RenderText::renderedTextLength() const
{
int l = 0;
for (InlineTextBox* box = firstTextBox(); box; box = box->nextTextBox())
l += box->len();
return l;
}
int RenderText::previousOffset(int current) const
{
if (isAllASCII() || m_text.is8Bit())
return current - 1;
StringImpl* textImpl = m_text.impl();
TextBreakIterator* iterator = cursorMovementIterator(textImpl->characters16(), textImpl->length());
if (!iterator)
return current - 1;
long result = iterator->preceding(current);
if (result == TextBreakDone)
result = current - 1;
return result;
}
#if OS(POSIX)
#define HANGUL_CHOSEONG_START (0x1100)
#define HANGUL_CHOSEONG_END (0x115F)
#define HANGUL_JUNGSEONG_START (0x1160)
#define HANGUL_JUNGSEONG_END (0x11A2)
#define HANGUL_JONGSEONG_START (0x11A8)
#define HANGUL_JONGSEONG_END (0x11F9)
#define HANGUL_SYLLABLE_START (0xAC00)
#define HANGUL_SYLLABLE_END (0xD7AF)
#define HANGUL_JONGSEONG_COUNT (28)
enum HangulState {
HangulStateL,
HangulStateV,
HangulStateT,
HangulStateLV,
HangulStateLVT,
HangulStateBreak
};
inline bool isHangulLVT(UChar32 character)
{
return (character - HANGUL_SYLLABLE_START) % HANGUL_JONGSEONG_COUNT;
}
inline bool isMark(UChar32 c)
{
int8_t charType = u_charType(c);
return charType == U_NON_SPACING_MARK || charType == U_ENCLOSING_MARK || charType == U_COMBINING_SPACING_MARK;
}
inline bool isRegionalIndicator(UChar32 c)
{
return 0x1F1E6 <= c && c <= 0x1F1FF;
}
#endif
int RenderText::previousOffsetForBackwardDeletion(int current) const
{
#if OS(POSIX)
ASSERT(m_text);
StringImpl& text = *m_text.impl();
UChar32 character;
bool sawRegionalIndicator = false;
while (current > 0) {
if (U16_IS_TRAIL(text[--current]))
--current;
if (current < 0)
break;
UChar32 character = text.characterStartingAt(current);
if (sawRegionalIndicator) {
if (isRegionalIndicator(character))
break;
U16_FWD_1_UNSAFE(text, current);
}
if ((character >= 0x0530) && (character < 0x1950))
break;
if (isRegionalIndicator(character)) {
sawRegionalIndicator = true;
continue;
}
if (!isMark(character) && (character != 0xFF9E) && (character != 0xFF9F))
break;
}
if (current <= 0)
return current;
character = text.characterStartingAt(current);
if (((character >= HANGUL_CHOSEONG_START) && (character <= HANGUL_JONGSEONG_END)) || ((character >= HANGUL_SYLLABLE_START) && (character <= HANGUL_SYLLABLE_END))) {
HangulState state;
if (character < HANGUL_JUNGSEONG_START)
state = HangulStateL;
else if (character < HANGUL_JONGSEONG_START)
state = HangulStateV;
else if (character < HANGUL_SYLLABLE_START)
state = HangulStateT;
else
state = isHangulLVT(character) ? HangulStateLVT : HangulStateLV;
while (current > 0 && ((character = text.characterStartingAt(current - 1)) >= HANGUL_CHOSEONG_START) && (character <= HANGUL_SYLLABLE_END) && ((character <= HANGUL_JONGSEONG_END) || (character >= HANGUL_SYLLABLE_START))) {
switch (state) {
case HangulStateV:
if (character <= HANGUL_CHOSEONG_END)
state = HangulStateL;
else if ((character >= HANGUL_SYLLABLE_START) && (character <= HANGUL_SYLLABLE_END) && !isHangulLVT(character))
state = HangulStateLV;
else if (character > HANGUL_JUNGSEONG_END)
state = HangulStateBreak;
break;
case HangulStateT:
if ((character >= HANGUL_JUNGSEONG_START) && (character <= HANGUL_JUNGSEONG_END))
state = HangulStateV;
else if ((character >= HANGUL_SYLLABLE_START) && (character <= HANGUL_SYLLABLE_END))
state = (isHangulLVT(character) ? HangulStateLVT : HangulStateLV);
else if (character < HANGUL_JUNGSEONG_START)
state = HangulStateBreak;
break;
default:
state = (character < HANGUL_JUNGSEONG_START) ? HangulStateL : HangulStateBreak;
break;
}
if (state == HangulStateBreak)
break;
--current;
}
}
return current;
#else
if (U16_IS_TRAIL(m_text[--current]))
--current;
if (current < 0)
current = 0;
return current;
#endif
}
int RenderText::nextOffset(int current) const
{
if (isAllASCII() || m_text.is8Bit())
return current + 1;
StringImpl* textImpl = m_text.impl();
TextBreakIterator* iterator = cursorMovementIterator(textImpl->characters16(), textImpl->length());
if (!iterator)
return current + 1;
long result = iterator->following(current);
if (result == TextBreakDone)
result = current + 1;
return result;
}
bool RenderText::computeCanUseSimpleFontCodePath() const
{
if (isAllASCII() || m_text.is8Bit())
return true;
return Character::characterRangeCodePath(characters16(), length()) == SimplePath;
}
#ifndef NDEBUG
void RenderText::checkConsistency() const
{
#ifdef CHECK_CONSISTENCY
const InlineTextBox* prev = 0;
for (const InlineTextBox* child = m_firstTextBox; child != 0; child = child->nextTextBox()) {
ASSERT(child->renderer() == this);
ASSERT(child->prevTextBox() == prev);
prev = child;
}
ASSERT(prev == m_lastTextBox);
#endif
}
#endif
void RenderText::momentarilyRevealLastTypedCharacter(unsigned lastTypedCharacterOffset)
{
if (!gSecureTextTimers)
gSecureTextTimers = new SecureTextTimerMap;
SecureTextTimer* secureTextTimer = gSecureTextTimers->get(this);
if (!secureTextTimer) {
secureTextTimer = new SecureTextTimer(this);
gSecureTextTimers->add(this, secureTextTimer);
}
secureTextTimer->restartWithNewText(lastTypedCharacterOffset);
}
PassRefPtr<AbstractInlineTextBox> RenderText::firstAbstractInlineTextBox()
{
return AbstractInlineTextBox::getOrCreate(this, m_firstTextBox);
}
}