#include "config.h"
#include "InlineTextBox.h"
#include "ChromeClient.h"
#include "Document.h"
#include "Editor.h"
#include "Frame.h"
#include "GraphicsContext.h"
#include "HitTestResult.h"
#include "Page.h"
#include "RenderArena.h"
#include "RenderBlock.h"
#include "RenderTheme.h"
#include "Text.h"
#include "break_lines.h"
#include <wtf/AlwaysInline.h>
using namespace std;
namespace WebCore {
int InlineTextBox::selectionTop()
{
return root()->selectionTop();
}
int InlineTextBox::selectionHeight()
{
return root()->selectionHeight();
}
bool InlineTextBox::isSelected(int startPos, int endPos) const
{
int sPos = max(startPos - m_start, 0);
int ePos = min(endPos - m_start, (int)m_len);
return (sPos < ePos);
}
RenderObject::SelectionState InlineTextBox::selectionState()
{
RenderObject::SelectionState state = renderer()->selectionState();
if (state == RenderObject::SelectionStart || state == RenderObject::SelectionEnd || state == RenderObject::SelectionBoth) {
int startPos, endPos;
renderer()->selectionStartEnd(startPos, endPos);
int lastSelectable = start() + len() - (isLineBreak() ? 1 : 0);
bool start = (state != RenderObject::SelectionEnd && startPos >= m_start && startPos < m_start + m_len);
bool end = (state != RenderObject::SelectionStart && endPos > m_start && endPos <= lastSelectable);
if (start && end)
state = RenderObject::SelectionBoth;
else if (start)
state = RenderObject::SelectionStart;
else if (end)
state = RenderObject::SelectionEnd;
else if ((state == RenderObject::SelectionEnd || startPos < m_start) &&
(state == RenderObject::SelectionStart || endPos > lastSelectable))
state = RenderObject::SelectionInside;
else if (state == RenderObject::SelectionBoth)
state = RenderObject::SelectionNone;
}
return state;
}
IntRect InlineTextBox::selectionRect(int tx, int ty, int startPos, int endPos)
{
int sPos = max(startPos - m_start, 0);
int ePos = min(endPos - m_start, (int)m_len);
if (sPos >= ePos)
return IntRect();
RenderText* textObj = textRenderer();
int selTop = selectionTop();
int selHeight = selectionHeight();
const Font& f = textObj->style(m_firstLine)->font();
IntRect r = enclosingIntRect(f.selectionRectForText(TextRun(textObj->text()->characters() + m_start, m_len, textObj->allowTabs(), textPos(), m_toAdd, direction() == RTL, m_dirOverride),
IntPoint(tx + m_x, ty + selTop), selHeight, sPos, ePos));
if (r.x() > tx + m_x + m_width)
r.setWidth(0);
else if (r.right() - 1 > tx + m_x + m_width)
r.setWidth(tx + m_x + m_width - r.x());
return r;
}
void InlineTextBox::deleteLine(RenderArena* arena)
{
toRenderText(renderer())->removeTextBox(this);
destroy(arena);
}
void InlineTextBox::extractLine()
{
if (m_extracted)
return;
toRenderText(renderer())->extractTextBox(this);
}
void InlineTextBox::attachLine()
{
if (!m_extracted)
return;
toRenderText(renderer())->attachTextBox(this);
}
int InlineTextBox::placeEllipsisBox(bool flowIsLTR, int visibleLeftEdge, int visibleRightEdge, int ellipsisWidth, bool& foundBox)
{
if (foundBox) {
m_truncation = cFullTruncation;
return -1;
}
int ellipsisX = flowIsLTR ? visibleRightEdge - ellipsisWidth : visibleLeftEdge + ellipsisWidth;
bool ltrFullTruncation = flowIsLTR && ellipsisX <= m_x;
bool rtlFullTruncation = !flowIsLTR && ellipsisX >= (m_x + m_width);
if (ltrFullTruncation || rtlFullTruncation) {
m_truncation = cFullTruncation;
foundBox = true;
return -1;
}
bool ltrEllipsisWithinBox = flowIsLTR && (ellipsisX < m_x + m_width);
bool rtlEllipsisWithinBox = !flowIsLTR && (ellipsisX > m_x);
if (ltrEllipsisWithinBox || rtlEllipsisWithinBox) {
foundBox = true;
bool ltr = direction() == LTR;
if (ltr != flowIsLTR) {
int visibleBoxWidth = visibleRightEdge - visibleLeftEdge - ellipsisWidth;
ellipsisX = ltr ? m_x + visibleBoxWidth : m_x + m_width - visibleBoxWidth;
}
int offset = offsetForPosition(ellipsisX, false);
if (offset == 0) {
m_truncation = cFullTruncation;
return min(ellipsisX, m_x);
}
m_truncation = offset;
int widthOfVisibleText = toRenderText(renderer())->width(m_start, offset, textPos(), m_firstLine);
if (flowIsLTR)
return m_x + widthOfVisibleText;
else
return (m_x + m_width) - widthOfVisibleText - ellipsisWidth;
}
return -1;
}
Color correctedTextColor(Color textColor, Color backgroundColor)
{
int d = differenceSquared(textColor, backgroundColor);
if (d > 65025) {
return textColor;
}
int distanceFromWhite = differenceSquared(textColor, Color::white);
int distanceFromBlack = differenceSquared(textColor, Color::black);
if (distanceFromWhite < distanceFromBlack) {
return textColor.dark();
}
return textColor.light();
}
void updateGraphicsContext(GraphicsContext* context, const Color& fillColor, const Color& strokeColor, float strokeThickness)
{
int mode = context->textDrawingMode();
if (strokeThickness > 0) {
int newMode = mode | cTextStroke;
if (mode != newMode) {
context->setTextDrawingMode(newMode);
mode = newMode;
}
}
if (mode & cTextFill && fillColor != context->fillColor())
context->setFillColor(fillColor);
if (mode & cTextStroke) {
if (strokeColor != context->strokeColor())
context->setStrokeColor(strokeColor);
if (strokeThickness != context->strokeThickness())
context->setStrokeThickness(strokeThickness);
}
}
bool InlineTextBox::isLineBreak() const
{
return renderer()->isBR() || (renderer()->style()->preserveNewline() && len() == 1 && (*textRenderer()->text())[start()] == '\n');
}
bool InlineTextBox::nodeAtPoint(const HitTestRequest&, HitTestResult& result, int x, int y, int tx, int ty)
{
if (isLineBreak())
return false;
IntRect rect(tx + m_x, ty + m_y, m_width, height());
if (m_truncation != cFullTruncation && visibleToHitTesting() && rect.contains(x, y)) {
renderer()->updateHitTestResult(result, IntPoint(x - tx, y - ty));
return true;
}
return false;
}
static void paintTextWithShadows(GraphicsContext* context, const Font& font, const TextRun& textRun, int startOffset, int endOffset, const IntPoint& textOrigin, int x, int y, int w, int h, ShadowData* shadow, bool stroked)
{
do {
IntSize extraOffset;
if (shadow) {
IntSize shadowOffset(shadow->x, shadow->y);
int shadowBlur = shadow->blur;
const Color& shadowColor = shadow->color;
if (shadow->next || stroked) {
IntRect shadowRect(x, y, w, h);
shadowRect.inflate(shadowBlur);
shadowRect.move(shadowOffset);
context->save();
context->clip(shadowRect);
extraOffset = IntSize(0, 2 * h + max(0, shadowOffset.height()) + shadowBlur);
shadowOffset -= extraOffset;
}
context->setShadow(shadowOffset, shadowBlur, shadowColor);
}
if (startOffset <= endOffset)
context->drawText(font, textRun, textOrigin + extraOffset, startOffset, endOffset);
else {
if (endOffset > 0)
context->drawText(font, textRun, textOrigin + extraOffset, 0, endOffset);
if (startOffset < textRun.length())
context->drawText(font, textRun, textOrigin + extraOffset, startOffset);
}
if (!shadow)
break;
if (shadow->next || stroked)
context->restore();
else
context->clearShadow();
shadow = shadow->next;
} while (shadow || stroked);
}
void InlineTextBox::paint(RenderObject::PaintInfo& paintInfo, int tx, int ty)
{
if (isLineBreak() || !renderer()->shouldPaintWithinRoot(paintInfo) || renderer()->style()->visibility() != VISIBLE ||
m_truncation == cFullTruncation || paintInfo.phase == PaintPhaseOutline)
return;
ASSERT(paintInfo.phase != PaintPhaseSelfOutline && paintInfo.phase != PaintPhaseChildOutlines);
int xPos = tx + m_x - parent()->maxHorizontalVisualOverflow();
int w = width() + 2 * parent()->maxHorizontalVisualOverflow();
if (xPos >= paintInfo.rect.right() || xPos + w <= paintInfo.rect.x())
return;
bool isPrinting = textRenderer()->document()->printing();
bool haveSelection = !isPrinting && paintInfo.phase != PaintPhaseTextClip && selectionState() != RenderObject::SelectionNone;
if (!haveSelection && paintInfo.phase == PaintPhaseSelection)
return;
if (m_truncation != cNoTruncation) {
TextDirection flowDirection = renderer()->containingBlock()->style()->direction();
if (flowDirection != direction()) {
int widthOfVisibleText = toRenderText(renderer())->width(m_start, m_truncation, textPos(), m_firstLine);
int widthOfHiddenText = m_width - widthOfVisibleText;
tx += direction() == LTR ? widthOfHiddenText : -widthOfHiddenText;
}
}
GraphicsContext* context = paintInfo.context;
bool containsComposition = renderer()->node() && renderer()->document()->frame()->editor()->compositionNode() == renderer()->node();
bool useCustomUnderlines = containsComposition && renderer()->document()->frame()->editor()->compositionUsesCustomUnderlines();
RenderStyle* styleToUse = renderer()->style(m_firstLine);
int d = styleToUse->textDecorationsInEffect();
const Font& font = styleToUse->font();
if (paintInfo.phase != PaintPhaseSelection && paintInfo.phase != PaintPhaseTextClip && !isPrinting) {
#if PLATFORM(MAC)
if (styleToUse->highlight() != nullAtom && !context->paintingDisabled())
paintCustomHighlight(tx, ty, styleToUse->highlight());
#endif
if (containsComposition && !useCustomUnderlines)
paintCompositionBackground(context, tx, ty, styleToUse, font,
renderer()->document()->frame()->editor()->compositionStart(),
renderer()->document()->frame()->editor()->compositionEnd());
paintDocumentMarkers(context, tx, ty, styleToUse, font, true);
if (haveSelection && !useCustomUnderlines)
paintSelection(context, tx, ty, styleToUse, font);
}
if (m_len <= 0)
return;
Color textFillColor;
Color textStrokeColor;
float textStrokeWidth = styleToUse->textStrokeWidth();
ShadowData* textShadow = paintInfo.forceBlackText ? 0 : styleToUse->textShadow();
if (paintInfo.forceBlackText) {
textFillColor = Color::black;
textStrokeColor = Color::black;
} else {
textFillColor = styleToUse->textFillColor();
if (!textFillColor.isValid())
textFillColor = styleToUse->color();
if (styleToUse->forceBackgroundsToWhite())
textFillColor = correctedTextColor(textFillColor, Color::white);
textStrokeColor = styleToUse->textStrokeColor();
if (!textStrokeColor.isValid())
textStrokeColor = styleToUse->color();
if (styleToUse->forceBackgroundsToWhite())
textStrokeColor = correctedTextColor(textStrokeColor, Color::white);
}
bool paintSelectedTextOnly = (paintInfo.phase == PaintPhaseSelection);
bool paintSelectedTextSeparately = false;
Color selectionFillColor = textFillColor;
Color selectionStrokeColor = textStrokeColor;
float selectionStrokeWidth = textStrokeWidth;
ShadowData* selectionShadow = textShadow;
int baseline = renderer()->style(m_firstLine)->font().ascent();
IntPoint textOrigin(m_x + tx, m_y + ty + baseline);
TextRun textRun(textRenderer()->text()->characters() + m_start, m_len, textRenderer()->allowTabs(), textPos(), m_toAdd, direction() == RTL, m_dirOverride || styleToUse->visuallyOrdered());
int sPos = 0;
int ePos = 0;
if (paintSelectedTextOnly || paintSelectedTextSeparately)
selectionStartEnd(sPos, ePos);
if (!paintSelectedTextOnly) {
if (textStrokeWidth > 0)
context->save();
updateGraphicsContext(context, textFillColor, textStrokeColor, textStrokeWidth);
if (!paintSelectedTextSeparately || ePos <= sPos) {
paintTextWithShadows(context, font, textRun, 0, m_truncation == cNoTruncation ? m_len : m_truncation, textOrigin, m_x + tx, m_y + ty, width(), height(), textShadow, textStrokeWidth > 0);
} else
paintTextWithShadows(context, font, textRun, ePos, sPos, textOrigin, m_x + tx, m_y + ty, width(), height(), textShadow, textStrokeWidth > 0);
if (textStrokeWidth > 0)
context->restore();
}
if ((paintSelectedTextOnly || paintSelectedTextSeparately) && sPos < ePos) {
if (selectionStrokeWidth > 0)
context->save();
updateGraphicsContext(context, selectionFillColor, selectionStrokeColor, selectionStrokeWidth);
paintTextWithShadows(context, font, textRun, sPos, ePos, textOrigin, m_x + tx, m_y + ty, width(), height(), selectionShadow, selectionStrokeWidth > 0);
if (selectionStrokeWidth > 0)
context->restore();
}
if (d != TDNONE && paintInfo.phase != PaintPhaseSelection && styleToUse->htmlHacks()) {
context->setStrokeColor(styleToUse->color());
paintDecoration(context, tx, ty, d, textShadow);
}
if (paintInfo.phase == PaintPhaseForeground) {
paintDocumentMarkers(context, tx, ty, styleToUse, font, false);
if (useCustomUnderlines) {
const Vector<CompositionUnderline>& underlines = renderer()->document()->frame()->editor()->customCompositionUnderlines();
size_t numUnderlines = underlines.size();
for (size_t index = 0; index < numUnderlines; ++index) {
const CompositionUnderline& underline = underlines[index];
if (underline.endOffset <= start())
continue;
if (underline.startOffset <= end()) {
paintCompositionUnderline(context, tx, ty, underline);
if (underline.endOffset > end() + 1)
break;
} else
break;
}
}
}
}
void InlineTextBox::selectionStartEnd(int& sPos, int& ePos)
{
int startPos, endPos;
if (renderer()->selectionState() == RenderObject::SelectionInside) {
startPos = 0;
endPos = textRenderer()->textLength();
} else {
textRenderer()->selectionStartEnd(startPos, endPos);
if (renderer()->selectionState() == RenderObject::SelectionStart)
endPos = textRenderer()->textLength();
else if (renderer()->selectionState() == RenderObject::SelectionEnd)
startPos = 0;
}
sPos = max(startPos - m_start, 0);
ePos = min(endPos - m_start, (int)m_len);
}
void InlineTextBox::paintSelection(GraphicsContext* context, int tx, int ty, RenderStyle* style, const Font& font)
{
UNUSED_PARAM(context);
UNUSED_PARAM(tx);
UNUSED_PARAM(ty);
UNUSED_PARAM(style);
UNUSED_PARAM(font);
return;
}
void InlineTextBox::paintCompositionBackground(GraphicsContext* context, int tx, int ty, RenderStyle* style, const Font& font, int startPos, int endPos)
{
int offset = m_start;
int sPos = max(startPos - offset, 0);
int ePos = min(endPos - offset, (int)m_len);
if (sPos >= ePos)
return;
context->save();
Color c = style->compositionFillColor();
Color stroke = style->compositionFrameColor();
updateGraphicsContext(context, c, c, 0);
int y = selectionTop();
int h = selectionHeight();
FloatRect effectRect = font.selectionRectForText(TextRun(textRenderer()->text()->characters() + m_start, m_len, textRenderer()->allowTabs(), textPos(), m_toAdd, direction() == RTL, m_dirOverride || style->visuallyOrdered()), IntPoint(m_x + tx, y + ty), h, sPos, ePos);
effectRect.inflate(-0.5f);
context->setStrokeColor(stroke);
context->strokeRect(effectRect, 1.0f);
effectRect.inflate(-0.5f);
context->fillRect(effectRect, c);
context->restore();
}
#if PLATFORM(MAC)
void InlineTextBox::paintCustomHighlight(int tx, int ty, const AtomicString& type)
{
Frame* frame = renderer()->document()->frame();
if (!frame)
return;
Page* page = frame->page();
if (!page)
return;
RootInlineBox* r = root();
FloatRect rootRect(tx + r->x(), ty + selectionTop(), r->width(), selectionHeight());
FloatRect textRect(tx + x(), rootRect.y(), width(), rootRect.height());
page->chrome()->client()->paintCustomHighlight(renderer()->node(), type, textRect, rootRect, true, false);
}
#endif
void InlineTextBox::paintDecoration(GraphicsContext* context, int tx, int ty, int deco, ShadowData* shadow)
{
tx += m_x;
ty += m_y;
if (m_truncation == cFullTruncation)
return;
int width = m_width;
if (m_truncation != cNoTruncation) {
width = toRenderText(renderer())->width(m_start, m_truncation, textPos(), m_firstLine);
if (direction() == RTL)
tx += (m_width - width);
}
Color underline, overline, linethrough;
renderer()->getTextDecorationColors(deco, underline, overline, linethrough, true);
bool isPrinting = textRenderer()->document()->printing();
context->setStrokeThickness(1.0f);
bool linesAreOpaque = !isPrinting && (!(deco & UNDERLINE) || underline.alpha() == 255) && (!(deco & OVERLINE) || overline.alpha() == 255) && (!(deco & LINE_THROUGH) || linethrough.alpha() == 255);
int baseline = renderer()->style(m_firstLine)->font().ascent();
bool setClip = false;
int extraOffset = 0;
if (!linesAreOpaque && shadow && shadow->next) {
context->save();
IntRect clipRect(tx, ty, width, baseline + 2);
for (ShadowData* s = shadow; s; s = s->next) {
IntRect shadowRect(tx, ty, width, baseline + 2);
shadowRect.inflate(s->blur);
shadowRect.move(s->x, s->y);
clipRect.unite(shadowRect);
extraOffset = max(extraOffset, max(0, s->y) + s->blur);
}
context->save();
context->clip(clipRect);
extraOffset += baseline + 2;
ty += extraOffset;
setClip = true;
}
bool setShadow = false;
do {
if (shadow) {
if (!shadow->next) {
ty -= extraOffset;
extraOffset = 0;
}
context->setShadow(IntSize(shadow->x, shadow->y - extraOffset), shadow->blur, shadow->color);
setShadow = true;
shadow = shadow->next;
}
if (deco & UNDERLINE) {
context->setStrokeColor(underline);
context->setStrokeStyle(SolidStroke);
context->drawLineForText(IntPoint(tx, ty + baseline), width, isPrinting);
}
if (deco & OVERLINE) {
context->setStrokeColor(overline);
context->setStrokeStyle(SolidStroke);
context->drawLineForText(IntPoint(tx, ty), width, isPrinting);
}
if (deco & LINE_THROUGH) {
context->setStrokeColor(linethrough);
context->setStrokeStyle(SolidStroke);
context->drawLineForText(IntPoint(tx, ty + 2 * baseline / 3), width, isPrinting);
}
} while (shadow);
if (setClip)
context->restore();
else if (setShadow)
context->clearShadow();
}
void InlineTextBox::paintSpellingOrGrammarMarker(GraphicsContext* pt, int tx, int ty, DocumentMarker marker, RenderStyle* style, const Font& font, bool grammar)
{
if (textRenderer()->document()->printing())
return;
if (m_truncation == cFullTruncation)
return;
int start = 0; int width = m_width;
bool markerSpansWholeBox = true;
if (m_start <= (int)marker.startOffset)
markerSpansWholeBox = false;
if ((end() + 1) != marker.endOffset) markerSpansWholeBox = false;
if (m_truncation != cNoTruncation)
markerSpansWholeBox = false;
if (!markerSpansWholeBox || grammar) {
int startPosition = max<int>(marker.startOffset - m_start, 0);
int endPosition = min<int>(marker.endOffset - m_start, m_len);
if (m_truncation != cNoTruncation)
endPosition = min<int>(endPosition, m_truncation);
IntPoint startPoint(tx + m_x, ty + selectionTop());
TextRun run(textRenderer()->text()->characters() + m_start, m_len, textRenderer()->allowTabs(), textPos(), m_toAdd, direction() == RTL, m_dirOverride || style->visuallyOrdered());
int h = selectionHeight();
IntRect markerRect = enclosingIntRect(font.selectionRectForText(run, startPoint, h, startPosition, endPosition));
start = markerRect.x() - startPoint.x();
width = markerRect.width();
if (grammar)
renderer()->document()->setRenderedRectForMarker(renderer()->node(), marker, markerRect);
}
int lineThickness = cMisspellingLineThickness;
int baseline = renderer()->style(m_firstLine)->font().ascent();
int descent = height() - baseline;
int underlineOffset;
if (descent <= (2 + lineThickness)) {
underlineOffset = height() - lineThickness;
} else {
underlineOffset = baseline + 2;
}
pt->drawLineForMisspellingOrBadGrammar(IntPoint(tx + m_x + start, ty + m_y + underlineOffset), width, grammar);
}
void InlineTextBox::paintTextMatchMarker(GraphicsContext* pt, int tx, int ty, DocumentMarker marker, RenderStyle* style, const Font& font)
{
int y = selectionTop();
int h = selectionHeight();
int sPos = max(marker.startOffset - m_start, (unsigned)0);
int ePos = min(marker.endOffset - m_start, (unsigned)m_len);
TextRun run(textRenderer()->text()->characters() + m_start, m_len, textRenderer()->allowTabs(), textPos(), m_toAdd, direction() == RTL, m_dirOverride || style->visuallyOrdered());
IntPoint startPoint = IntPoint(m_x + tx, y + ty);
IntRect markerRect = enclosingIntRect(font.selectionRectForText(run, startPoint, h, sPos, ePos));
renderer()->document()->setRenderedRectForMarker(renderer()->node(), marker, markerRect);
if (renderer()->document()->frame()->markedTextMatchesAreHighlighted()) {
Color color = marker.activeMatch ?
renderer()->theme()->platformActiveTextSearchHighlightColor() :
renderer()->theme()->platformInactiveTextSearchHighlightColor();
pt->save();
updateGraphicsContext(pt, color, color, 0); pt->clip(IntRect(tx + m_x, ty + y, m_width, h));
pt->drawHighlightForText(font, run, startPoint, h, color, sPos, ePos);
pt->restore();
}
}
void InlineTextBox::computeRectForReplacementMarker(int tx, int ty, DocumentMarker marker, RenderStyle* style, const Font& font)
{
int y = selectionTop();
int h = selectionHeight();
int sPos = max(marker.startOffset - m_start, (unsigned)0);
int ePos = min(marker.endOffset - m_start, (unsigned)m_len);
TextRun run(textRenderer()->text()->characters() + m_start, m_len, textRenderer()->allowTabs(), textPos(), m_toAdd, direction() == RTL, m_dirOverride || style->visuallyOrdered());
IntPoint startPoint = IntPoint(m_x + tx, y + ty);
IntRect markerRect = enclosingIntRect(font.selectionRectForText(run, startPoint, h, sPos, ePos));
renderer()->document()->setRenderedRectForMarker(renderer()->node(), marker, markerRect);
}
void InlineTextBox::paintDocumentMarkers(GraphicsContext* pt, int tx, int ty, RenderStyle* style, const Font& font, bool background)
{
if (!renderer()->node())
return;
Vector<DocumentMarker> markers = renderer()->document()->markersForNode(renderer()->node());
Vector<DocumentMarker>::iterator markerIt = markers.begin();
for ( ; markerIt != markers.end(); markerIt++) {
DocumentMarker marker = *markerIt;
switch (marker.type) {
case DocumentMarker::Grammar:
case DocumentMarker::Spelling:
case DocumentMarker::Replacement:
if (background)
continue;
break;
case DocumentMarker::TextMatch:
if (!background)
continue;
break;
default:
ASSERT_NOT_REACHED();
}
if (marker.endOffset <= start())
continue;
if (marker.startOffset > end())
break;
switch (marker.type) {
case DocumentMarker::Spelling:
paintSpellingOrGrammarMarker(pt, tx, ty, marker, style, font, false);
break;
case DocumentMarker::Grammar:
paintSpellingOrGrammarMarker(pt, tx, ty, marker, style, font, true);
break;
case DocumentMarker::TextMatch:
paintTextMatchMarker(pt, tx, ty, marker, style, font);
break;
case DocumentMarker::Replacement:
computeRectForReplacementMarker(tx, ty, marker, style, font);
break;
default:
ASSERT_NOT_REACHED();
}
}
}
void InlineTextBox::paintCompositionUnderline(GraphicsContext* ctx, int tx, int ty, const CompositionUnderline& underline)
{
tx += m_x;
ty += m_y;
if (m_truncation == cFullTruncation)
return;
int start = 0; int width = m_width; bool useWholeWidth = true;
unsigned paintStart = m_start;
unsigned paintEnd = end() + 1; if (paintStart <= underline.startOffset) {
paintStart = underline.startOffset;
useWholeWidth = false;
start = toRenderText(renderer())->width(m_start, paintStart - m_start, textPos(), m_firstLine);
}
if (paintEnd != underline.endOffset) { paintEnd = min(paintEnd, (unsigned)underline.endOffset);
useWholeWidth = false;
}
if (m_truncation != cNoTruncation) {
paintEnd = min(paintEnd, (unsigned)m_start + m_truncation);
useWholeWidth = false;
}
if (!useWholeWidth) {
width = toRenderText(renderer())->width(paintStart, paintEnd - paintStart, textPos() + start, m_firstLine);
}
int lineThickness = 1;
int baseline = renderer()->style(m_firstLine)->font().ascent();
if (underline.thick && height() - baseline >= 2)
lineThickness = 2;
start += 1;
width -= 2;
ctx->setStrokeColor(underline.color);
ctx->setStrokeThickness(lineThickness);
ctx->drawLineForText(IntPoint(tx + start, ty + height() - lineThickness), width, textRenderer()->document()->printing());
}
int InlineTextBox::caretMinOffset() const
{
return m_start;
}
int InlineTextBox::caretMaxOffset() const
{
return m_start + m_len;
}
unsigned InlineTextBox::caretMaxRenderedOffset() const
{
return m_start + m_len;
}
int InlineTextBox::textPos() const
{
if (x() == 0)
return 0;
RenderBlock *blockElement = renderer()->containingBlock();
return direction() == RTL ? x() - blockElement->borderRight() - blockElement->paddingRight()
: x() - blockElement->borderLeft() - blockElement->paddingLeft();
}
int InlineTextBox::offsetForPosition(int _x, bool includePartialGlyphs) const
{
if (isLineBreak())
return 0;
RenderText* text = toRenderText(renderer());
RenderStyle *style = text->style(m_firstLine);
const Font* f = &style->font();
return f->offsetForPosition(TextRun(textRenderer()->text()->characters() + m_start, m_len, textRenderer()->allowTabs(), textPos(), m_toAdd, direction() == RTL, m_dirOverride || style->visuallyOrdered()),
_x - m_x, includePartialGlyphs);
}
int InlineTextBox::positionForOffset(int offset) const
{
ASSERT(offset >= m_start);
ASSERT(offset <= m_start + m_len);
if (isLineBreak())
return m_x;
RenderText* text = toRenderText(renderer());
const Font& f = text->style(m_firstLine)->font();
int from = direction() == RTL ? offset - m_start : 0;
int to = direction() == RTL ? m_len : offset - m_start;
return enclosingIntRect(f.selectionRectForText(TextRun(text->text()->characters() + m_start, m_len, textRenderer()->allowTabs(), textPos(), m_toAdd, direction() == RTL, m_dirOverride),
IntPoint(m_x, 0), 0, from, to)).right();
}
bool InlineTextBox::containsCaretOffset(int offset) const
{
if (offset < m_start)
return false;
int pastEnd = m_start + m_len;
if (offset < pastEnd)
return true;
if (offset > pastEnd)
return false;
if (isLineBreak())
return false;
return true;
}
typedef HashMap<InlineTextBox*, Vector<const SimpleFontData*> > FallbackFontsMap;
static FallbackFontsMap* gFallbackFontsMap;
void InlineTextBox::setFallbackFonts(const HashSet<const SimpleFontData*>& fallbackFonts)
{
if (!gFallbackFontsMap)
gFallbackFontsMap = new FallbackFontsMap;
FallbackFontsMap::iterator it = gFallbackFontsMap->set(this, Vector<const SimpleFontData*>()).first;
ASSERT(it->second.isEmpty());
copyToVector(fallbackFonts, it->second);
}
void InlineTextBox::takeFallbackFonts(Vector<const SimpleFontData*>& fallbackFonts)
{
if (!gFallbackFontsMap)
return;
FallbackFontsMap::iterator it = gFallbackFontsMap->find(this);
if (it == gFallbackFontsMap->end())
return;
fallbackFonts.swap(it->second);
gFallbackFontsMap->remove(it);
}
}