2 * (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 2000 Dirk Mueller (mueller@kde.org)
4 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
24 #include "InlineTextBox.h"
27 #include "ChromeClient.h"
29 #include "DocumentMarkerController.h"
31 #include "EllipsisBox.h"
32 #include "FontCache.h"
34 #include "GraphicsContext.h"
35 #include "HitTestResult.h"
37 #include "PaintInfo.h"
38 #include "RenderedDocumentMarker.h"
39 #include "RenderArena.h"
41 #include "RenderBlock.h"
42 #include "RenderCombineText.h"
43 #include "RenderRubyRun.h"
44 #include "RenderRubyText.h"
45 #include "RenderTheme.h"
47 #include "SVGTextRunRenderingContext.h"
49 #include "WebCoreMemoryInstrumentation.h"
50 #include "break_lines.h"
51 #include <wtf/AlwaysInline.h>
52 #include <wtf/text/CString.h>
58 typedef WTF::HashMap<const InlineTextBox*, LayoutRect> InlineTextBoxOverflowMap;
59 static InlineTextBoxOverflowMap* gTextBoxesWithOverflow;
61 void InlineTextBox::destroy(RenderArena* arena)
63 if (!knownToHaveNoOverflow() && gTextBoxesWithOverflow)
64 gTextBoxesWithOverflow->remove(this);
65 InlineBox::destroy(arena);
68 void InlineTextBox::markDirty(bool dirty)
74 InlineBox::markDirty(dirty);
77 LayoutRect InlineTextBox::logicalOverflowRect() const
79 if (knownToHaveNoOverflow() || !gTextBoxesWithOverflow)
80 return enclosingIntRect(logicalFrameRect());
81 return gTextBoxesWithOverflow->get(this);
84 void InlineTextBox::setLogicalOverflowRect(const LayoutRect& rect)
86 ASSERT(!knownToHaveNoOverflow());
87 if (!gTextBoxesWithOverflow)
88 gTextBoxesWithOverflow = new InlineTextBoxOverflowMap;
89 gTextBoxesWithOverflow->add(this, rect);
92 int InlineTextBox::baselinePosition(FontBaseline baselineType) const
94 if (!isText() || !parent())
96 if (parent()->renderer() == renderer()->parent())
97 return parent()->baselinePosition(baselineType);
98 return toRenderBoxModelObject(renderer()->parent())->baselinePosition(baselineType, isFirstLineStyle(), isHorizontal() ? HorizontalLine : VerticalLine, PositionOnContainingLine);
101 LayoutUnit InlineTextBox::lineHeight() const
103 if (!isText() || !renderer()->parent())
105 if (m_renderer->isBR())
106 return toRenderBR(m_renderer)->lineHeight(isFirstLineStyle());
107 if (parent()->renderer() == renderer()->parent())
108 return parent()->lineHeight();
109 return toRenderBoxModelObject(renderer()->parent())->lineHeight(isFirstLineStyle(), isHorizontal() ? HorizontalLine : VerticalLine, PositionOnContainingLine);
112 LayoutUnit InlineTextBox::selectionTop()
114 return root()->selectionTop();
117 LayoutUnit InlineTextBox::selectionBottom()
119 return root()->selectionBottom();
122 LayoutUnit InlineTextBox::selectionHeight()
124 return root()->selectionHeight();
127 bool InlineTextBox::isSelected(int startPos, int endPos) const
129 LayoutUnit sPos = max<LayoutUnit>(startPos - m_start, 0);
130 LayoutUnit ePos = min<LayoutUnit>(endPos - m_start, m_len);
131 return (sPos < ePos);
134 RenderObject::SelectionState InlineTextBox::selectionState()
136 RenderObject::SelectionState state = renderer()->selectionState();
137 if (state == RenderObject::SelectionStart || state == RenderObject::SelectionEnd || state == RenderObject::SelectionBoth) {
138 int startPos, endPos;
139 renderer()->selectionStartEnd(startPos, endPos);
140 // The position after a hard line break is considered to be past its end.
141 int lastSelectable = start() + len() - (isLineBreak() ? 1 : 0);
143 bool start = (state != RenderObject::SelectionEnd && startPos >= m_start && startPos < m_start + m_len);
144 bool end = (state != RenderObject::SelectionStart && endPos > m_start && endPos <= lastSelectable);
146 state = RenderObject::SelectionBoth;
148 state = RenderObject::SelectionStart;
150 state = RenderObject::SelectionEnd;
151 else if ((state == RenderObject::SelectionEnd || startPos < m_start) &&
152 (state == RenderObject::SelectionStart || endPos > lastSelectable))
153 state = RenderObject::SelectionInside;
154 else if (state == RenderObject::SelectionBoth)
155 state = RenderObject::SelectionNone;
158 // If there are ellipsis following, make sure their selection is updated.
159 if (m_truncation != cNoTruncation && root()->ellipsisBox()) {
160 EllipsisBox* ellipsis = root()->ellipsisBox();
161 if (state != RenderObject::SelectionNone) {
163 selectionStartEnd(start, end);
164 // The ellipsis should be considered to be selected if the end of
165 // the selection is past the beginning of the truncation and the
166 // beginning of the selection is before or at the beginning of the
168 ellipsis->setSelectionState(end >= m_truncation && start <= m_truncation ?
169 RenderObject::SelectionInside : RenderObject::SelectionNone);
171 ellipsis->setSelectionState(RenderObject::SelectionNone);
177 static void adjustCharactersAndLengthForHyphen(BufferForAppendingHyphen& charactersWithHyphen, RenderStyle* style, String& string, int& length)
179 const AtomicString& hyphenString = style->hyphenString();
180 charactersWithHyphen.reserveCapacity(length + hyphenString.length());
181 charactersWithHyphen.append(string);
182 charactersWithHyphen.append(hyphenString);
183 string = charactersWithHyphen.toString();
184 length += hyphenString.length();
187 LayoutRect InlineTextBox::localSelectionRect(int startPos, int endPos)
189 int sPos = max(startPos - m_start, 0);
190 int ePos = min(endPos - m_start, (int)m_len);
195 FontCachePurgePreventer fontCachePurgePreventer;
197 RenderText* textObj = textRenderer();
198 LayoutUnit selTop = selectionTop();
199 LayoutUnit selHeight = selectionHeight();
200 RenderStyle* styleToUse = textObj->style(isFirstLineStyle());
201 const Font& font = styleToUse->font();
203 BufferForAppendingHyphen charactersWithHyphen;
204 bool respectHyphen = ePos == m_len && hasHyphen();
205 TextRun textRun = constructTextRun(styleToUse, font, respectHyphen ? &charactersWithHyphen : 0);
207 endPos = textRun.length();
209 FloatPoint startingPoint = FloatPoint(logicalLeft(), selTop);
211 if (sPos || ePos != static_cast<int>(m_len))
212 r = enclosingIntRect(font.selectionRectForText(textRun, startingPoint, selHeight, sPos, ePos));
213 else // Avoid computing the font width when the entire line box is selected as an optimization.
214 r = enclosingIntRect(FloatRect(startingPoint, FloatSize(m_logicalWidth, selHeight)));
216 LayoutUnit logicalWidth = r.width();
217 if (r.x() > logicalRight())
219 else if (r.maxX() > logicalRight())
220 logicalWidth = logicalRight() - r.x();
222 LayoutPoint topPoint = isHorizontal() ? LayoutPoint(r.x(), selTop) : LayoutPoint(selTop, r.x());
223 LayoutUnit width = isHorizontal() ? logicalWidth : selHeight;
224 LayoutUnit height = isHorizontal() ? selHeight : logicalWidth;
226 return LayoutRect(topPoint, LayoutSize(width, height));
229 void InlineTextBox::deleteLine(RenderArena* arena)
231 toRenderText(renderer())->removeTextBox(this);
235 void InlineTextBox::extractLine()
240 toRenderText(renderer())->extractTextBox(this);
243 void InlineTextBox::attachLine()
248 toRenderText(renderer())->attachTextBox(this);
251 float InlineTextBox::placeEllipsisBox(bool flowIsLTR, float visibleLeftEdge, float visibleRightEdge, float ellipsisWidth, float &truncatedWidth, bool& foundBox)
254 m_truncation = cFullTruncation;
258 // For LTR this is the left edge of the box, for RTL, the right edge in parent coordinates.
259 float ellipsisX = flowIsLTR ? visibleRightEdge - ellipsisWidth : visibleLeftEdge + ellipsisWidth;
261 // Criteria for full truncation:
262 // LTR: the left edge of the ellipsis is to the left of our text run.
263 // RTL: the right edge of the ellipsis is to the right of our text run.
264 bool ltrFullTruncation = flowIsLTR && ellipsisX <= left();
265 bool rtlFullTruncation = !flowIsLTR && ellipsisX >= left() + logicalWidth();
266 if (ltrFullTruncation || rtlFullTruncation) {
267 // Too far. Just set full truncation, but return -1 and let the ellipsis just be placed at the edge of the box.
268 m_truncation = cFullTruncation;
273 bool ltrEllipsisWithinBox = flowIsLTR && (ellipsisX < right());
274 bool rtlEllipsisWithinBox = !flowIsLTR && (ellipsisX > left());
275 if (ltrEllipsisWithinBox || rtlEllipsisWithinBox) {
278 // The inline box may have different directionality than it's parent. Since truncation
279 // behavior depends both on both the parent and the inline block's directionality, we
280 // must keep track of these separately.
281 bool ltr = isLeftToRightDirection();
282 if (ltr != flowIsLTR) {
283 // Width in pixels of the visible portion of the box, excluding the ellipsis.
284 int visibleBoxWidth = visibleRightEdge - visibleLeftEdge - ellipsisWidth;
285 ellipsisX = ltr ? left() + visibleBoxWidth : right() - visibleBoxWidth;
288 int offset = offsetForPosition(ellipsisX, false);
290 // No characters should be rendered. Set ourselves to full truncation and place the ellipsis at the min of our start
291 // and the ellipsis edge.
292 m_truncation = cFullTruncation;
293 truncatedWidth += ellipsisWidth;
294 return min(ellipsisX, x());
297 // Set the truncation index on the text run.
298 m_truncation = offset;
300 // If we got here that means that we were only partially truncated and we need to return the pixel offset at which
301 // to place the ellipsis.
302 float widthOfVisibleText = toRenderText(renderer())->width(m_start, offset, textPos(), isFirstLineStyle());
304 // The ellipsis needs to be placed just after the last visible character.
305 // Where "after" is defined by the flow directionality, not the inline
306 // box directionality.
307 // e.g. In the case of an LTR inline box truncated in an RTL flow then we can
308 // have a situation such as |Hello| -> |...He|
309 truncatedWidth += widthOfVisibleText + ellipsisWidth;
311 return left() + widthOfVisibleText;
313 return right() - widthOfVisibleText - ellipsisWidth;
315 truncatedWidth += logicalWidth();
319 Color correctedTextColor(Color textColor, Color backgroundColor)
321 // Adjust the text color if it is too close to the background color,
322 // by darkening or lightening it to move it further away.
324 int d = differenceSquared(textColor, backgroundColor);
325 // semi-arbitrarily chose 65025 (255^2) value here after a few tests;
330 int distanceFromWhite = differenceSquared(textColor, Color::white);
331 int distanceFromBlack = differenceSquared(textColor, Color::black);
333 if (distanceFromWhite < distanceFromBlack) {
334 return textColor.dark();
337 return textColor.light();
340 void updateGraphicsContext(GraphicsContext* context, const Color& fillColor, const Color& strokeColor, float strokeThickness, ColorSpace colorSpace)
342 TextDrawingModeFlags mode = context->textDrawingMode();
343 if (strokeThickness > 0) {
344 TextDrawingModeFlags newMode = mode | TextModeStroke;
345 if (mode != newMode) {
346 context->setTextDrawingMode(newMode);
351 if (mode & TextModeFill && (fillColor != context->fillColor() || colorSpace != context->fillColorSpace()))
352 context->setFillColor(fillColor, colorSpace);
354 if (mode & TextModeStroke) {
355 if (strokeColor != context->strokeColor())
356 context->setStrokeColor(strokeColor, colorSpace);
357 if (strokeThickness != context->strokeThickness())
358 context->setStrokeThickness(strokeThickness);
362 bool InlineTextBox::isLineBreak() const
364 return renderer()->isBR() || (renderer()->style()->preserveNewline() && len() == 1 && (*textRenderer()->text())[start()] == '\n');
367 bool InlineTextBox::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit /* lineTop */, LayoutUnit /*lineBottom*/)
372 FloatPoint boxOrigin = locationIncludingFlipping();
373 boxOrigin.moveBy(accumulatedOffset);
374 FloatRect rect(boxOrigin, size());
375 if (m_truncation != cFullTruncation && visibleToHitTesting() && locationInContainer.intersects(rect)) {
376 renderer()->updateHitTestResult(result, flipForWritingMode(locationInContainer.point() - toLayoutSize(accumulatedOffset)));
377 if (!result.addNodeToRectBasedTestResult(renderer()->node(), request, locationInContainer, rect))
383 FloatSize InlineTextBox::applyShadowToGraphicsContext(GraphicsContext* context, const ShadowData* shadow, const FloatRect& textRect, bool stroked, bool opaque, bool horizontal)
388 FloatSize extraOffset;
389 int shadowX = horizontal ? shadow->x() : shadow->y();
390 int shadowY = horizontal ? shadow->y() : -shadow->x();
391 FloatSize shadowOffset(shadowX, shadowY);
392 int shadowRadius = shadow->radius();
393 const Color& shadowColor = shadow->color();
395 if (shadow->next() || stroked || !opaque) {
396 FloatRect shadowRect(textRect);
397 shadowRect.inflate(shadow->paintingExtent());
398 shadowRect.move(shadowOffset);
400 context->clip(shadowRect);
402 extraOffset = FloatSize(0, 2 * textRect.height() + max(0.0f, shadowOffset.height()) + shadowRadius);
403 shadowOffset -= extraOffset;
406 context->setShadow(shadowOffset, shadowRadius, shadowColor, context->fillColorSpace());
410 static void paintTextWithShadows(GraphicsContext* context, const Font& font, const TextRun& textRun, const AtomicString& emphasisMark, int emphasisMarkOffset, int startOffset, int endOffset, int truncationPoint, const FloatPoint& textOrigin,
411 const FloatRect& boxRect, const ShadowData* shadow, bool stroked, bool horizontal)
413 Color fillColor = context->fillColor();
414 ColorSpace fillColorSpace = context->fillColorSpace();
415 bool opaque = fillColor.alpha() == 255;
417 context->setFillColor(Color::black, fillColorSpace);
422 extraOffset = roundedIntSize(InlineTextBox::applyShadowToGraphicsContext(context, shadow, boxRect, stroked, opaque, horizontal));
424 context->setFillColor(fillColor, fillColorSpace);
426 if (startOffset <= endOffset) {
427 if (emphasisMark.isEmpty())
428 context->drawText(font, textRun, textOrigin + extraOffset, startOffset, endOffset);
430 context->drawEmphasisMarks(font, textRun, emphasisMark, textOrigin + extraOffset + IntSize(0, emphasisMarkOffset), startOffset, endOffset);
433 if (emphasisMark.isEmpty())
434 context->drawText(font, textRun, textOrigin + extraOffset, 0, endOffset);
436 context->drawEmphasisMarks(font, textRun, emphasisMark, textOrigin + extraOffset + IntSize(0, emphasisMarkOffset), 0, endOffset);
438 if (startOffset < truncationPoint) {
439 if (emphasisMark.isEmpty())
440 context->drawText(font, textRun, textOrigin + extraOffset, startOffset, truncationPoint);
442 context->drawEmphasisMarks(font, textRun, emphasisMark, textOrigin + extraOffset + IntSize(0, emphasisMarkOffset), startOffset, truncationPoint);
449 if (shadow->next() || stroked || !opaque)
452 context->clearShadow();
454 shadow = shadow->next();
455 } while (shadow || stroked || !opaque);
458 bool InlineTextBox::getEmphasisMarkPosition(RenderStyle* style, TextEmphasisPosition& emphasisPosition) const
460 // This function returns true if there are text emphasis marks and they are suppressed by ruby text.
461 if (style->textEmphasisMark() == TextEmphasisMarkNone)
464 emphasisPosition = style->textEmphasisPosition();
465 if (emphasisPosition == TextEmphasisPositionUnder)
466 return true; // Ruby text is always over, so it cannot suppress emphasis marks under.
468 RenderBlock* containingBlock = renderer()->containingBlock();
469 if (!containingBlock->isRubyBase())
470 return true; // This text is not inside a ruby base, so it does not have ruby text over it.
472 if (!containingBlock->parent()->isRubyRun())
473 return true; // Cannot get the ruby text.
475 RenderRubyText* rubyText = toRenderRubyRun(containingBlock->parent())->rubyText();
477 // The emphasis marks over are suppressed only if there is a ruby text box and it not empty.
478 return !rubyText || !rubyText->firstLineBox();
481 enum RotationDirection { Counterclockwise, Clockwise };
483 static inline AffineTransform rotation(const FloatRect& boxRect, RotationDirection clockwise)
485 return clockwise ? AffineTransform(0, 1, -1, 0, boxRect.x() + boxRect.maxY(), boxRect.maxY() - boxRect.x())
486 : AffineTransform(0, -1, 1, 0, boxRect.x() - boxRect.maxY(), boxRect.x() + boxRect.maxY());
489 void InlineTextBox::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit /*lineTop*/, LayoutUnit /*lineBottom*/)
491 if (isLineBreak() || !paintInfo.shouldPaintWithinRoot(renderer()) || renderer()->style()->visibility() != VISIBLE ||
492 m_truncation == cFullTruncation || paintInfo.phase == PaintPhaseOutline || !m_len)
495 ASSERT(paintInfo.phase != PaintPhaseSelfOutline && paintInfo.phase != PaintPhaseChildOutlines);
497 LayoutUnit logicalLeftSide = logicalLeftVisualOverflow();
498 LayoutUnit logicalRightSide = logicalRightVisualOverflow();
499 LayoutUnit logicalStart = logicalLeftSide + (isHorizontal() ? paintOffset.x() : paintOffset.y());
500 LayoutUnit logicalExtent = logicalRightSide - logicalLeftSide;
502 LayoutUnit paintEnd = isHorizontal() ? paintInfo.rect.maxX() : paintInfo.rect.maxY();
503 LayoutUnit paintStart = isHorizontal() ? paintInfo.rect.x() : paintInfo.rect.y();
505 LayoutPoint adjustedPaintOffset = roundedIntPoint(paintOffset);
507 if (logicalStart >= paintEnd || logicalStart + logicalExtent <= paintStart)
510 bool isPrinting = textRenderer()->document()->printing();
512 // Determine whether or not we're selected.
513 bool haveSelection = !isPrinting && paintInfo.phase != PaintPhaseTextClip && selectionState() != RenderObject::SelectionNone;
514 if (!haveSelection && paintInfo.phase == PaintPhaseSelection)
515 // When only painting the selection, don't bother to paint if there is none.
518 if (m_truncation != cNoTruncation) {
519 if (renderer()->containingBlock()->style()->isLeftToRightDirection() != isLeftToRightDirection()) {
520 // Make the visible fragment of text hug the edge closest to the rest of the run by moving the origin
521 // at which we start drawing text.
522 // e.g. In the case of LTR text truncated in an RTL Context, the correct behavior is:
523 // |Hello|CBA| -> |...He|CBA|
524 // In order to draw the fragment "He" aligned to the right edge of it's box, we need to start drawing
525 // farther to the right.
526 // NOTE: WebKit's behavior differs from that of IE which appears to just overlay the ellipsis on top of the
527 // truncated string i.e. |Hello|CBA| -> |...lo|CBA|
528 LayoutUnit widthOfVisibleText = toRenderText(renderer())->width(m_start, m_truncation, textPos(), isFirstLineStyle());
529 LayoutUnit widthOfHiddenText = m_logicalWidth - widthOfVisibleText;
530 // FIXME: The hit testing logic also needs to take this translation into account.
531 LayoutSize truncationOffset(isLeftToRightDirection() ? widthOfHiddenText : -widthOfHiddenText, 0);
532 adjustedPaintOffset.move(isHorizontal() ? truncationOffset : truncationOffset.transposedSize());
536 GraphicsContext* context = paintInfo.context;
538 RenderStyle* styleToUse = renderer()->style(isFirstLineStyle());
540 adjustedPaintOffset.move(0, styleToUse->isHorizontalWritingMode() ? 0 : -logicalHeight());
542 FloatPoint boxOrigin = locationIncludingFlipping();
543 boxOrigin.move(adjustedPaintOffset.x(), adjustedPaintOffset.y());
544 FloatRect boxRect(boxOrigin, LayoutSize(logicalWidth(), logicalHeight()));
546 RenderCombineText* combinedText = styleToUse->hasTextCombine() && textRenderer()->isCombineText() && toRenderCombineText(textRenderer())->isCombined() ? toRenderCombineText(textRenderer()) : 0;
548 bool shouldRotate = !isHorizontal() && !combinedText;
550 context->concatCTM(rotation(boxRect, Clockwise));
552 // Determine whether or not we have composition underlines to draw.
553 bool containsComposition = renderer()->node() && renderer()->frame()->editor()->compositionNode() == renderer()->node();
554 bool useCustomUnderlines = containsComposition && renderer()->frame()->editor()->compositionUsesCustomUnderlines();
556 // Determine the text colors and selection colors.
558 Color textStrokeColor;
559 Color emphasisMarkColor;
560 float textStrokeWidth = styleToUse->textStrokeWidth();
561 const ShadowData* textShadow = paintInfo.forceBlackText() ? 0 : styleToUse->textShadow();
563 if (paintInfo.forceBlackText()) {
564 textFillColor = Color::black;
565 textStrokeColor = Color::black;
566 emphasisMarkColor = Color::black;
568 textFillColor = styleToUse->visitedDependentColor(CSSPropertyWebkitTextFillColor);
570 bool forceBackgroundToWhite = false;
572 if (styleToUse->printColorAdjust() == PrintColorAdjustEconomy)
573 forceBackgroundToWhite = true;
574 if (textRenderer()->document()->settings() && textRenderer()->document()->settings()->shouldPrintBackgrounds())
575 forceBackgroundToWhite = false;
578 // Make the text fill color legible against a white background
579 if (forceBackgroundToWhite)
580 textFillColor = correctedTextColor(textFillColor, Color::white);
582 textStrokeColor = styleToUse->visitedDependentColor(CSSPropertyWebkitTextStrokeColor);
584 // Make the text stroke color legible against a white background
585 if (forceBackgroundToWhite)
586 textStrokeColor = correctedTextColor(textStrokeColor, Color::white);
588 emphasisMarkColor = styleToUse->visitedDependentColor(CSSPropertyWebkitTextEmphasisColor);
590 // Make the text stroke color legible against a white background
591 if (forceBackgroundToWhite)
592 emphasisMarkColor = correctedTextColor(emphasisMarkColor, Color::white);
595 bool paintSelectedTextOnly = (paintInfo.phase == PaintPhaseSelection);
596 bool paintSelectedTextSeparately = false;
598 Color selectionFillColor = textFillColor;
599 Color selectionStrokeColor = textStrokeColor;
600 Color selectionEmphasisMarkColor = emphasisMarkColor;
601 float selectionStrokeWidth = textStrokeWidth;
602 const ShadowData* selectionShadow = textShadow;
604 // Check foreground color first.
605 Color foreground = paintInfo.forceBlackText() ? Color::black : renderer()->selectionForegroundColor();
606 if (foreground.isValid() && foreground != selectionFillColor) {
607 if (!paintSelectedTextOnly)
608 paintSelectedTextSeparately = true;
609 selectionFillColor = foreground;
612 Color emphasisMarkForeground = paintInfo.forceBlackText() ? Color::black : renderer()->selectionEmphasisMarkColor();
613 if (emphasisMarkForeground.isValid() && emphasisMarkForeground != selectionEmphasisMarkColor) {
614 if (!paintSelectedTextOnly)
615 paintSelectedTextSeparately = true;
616 selectionEmphasisMarkColor = emphasisMarkForeground;
619 if (RenderStyle* pseudoStyle = renderer()->getCachedPseudoStyle(SELECTION)) {
620 const ShadowData* shadow = paintInfo.forceBlackText() ? 0 : pseudoStyle->textShadow();
621 if (shadow != selectionShadow) {
622 if (!paintSelectedTextOnly)
623 paintSelectedTextSeparately = true;
624 selectionShadow = shadow;
627 float strokeWidth = pseudoStyle->textStrokeWidth();
628 if (strokeWidth != selectionStrokeWidth) {
629 if (!paintSelectedTextOnly)
630 paintSelectedTextSeparately = true;
631 selectionStrokeWidth = strokeWidth;
634 Color stroke = paintInfo.forceBlackText() ? Color::black : pseudoStyle->visitedDependentColor(CSSPropertyWebkitTextStrokeColor);
635 if (stroke != selectionStrokeColor) {
636 if (!paintSelectedTextOnly)
637 paintSelectedTextSeparately = true;
638 selectionStrokeColor = stroke;
644 const Font& font = styleToUse->font();
646 FloatPoint textOrigin = FloatPoint(boxOrigin.x(), boxOrigin.y() + font.fontMetrics().ascent());
649 combinedText->adjustTextOrigin(textOrigin, boxRect);
651 // 1. Paint backgrounds behind text if needed. Examples of such backgrounds include selection
652 // and composition underlines.
653 if (paintInfo.phase != PaintPhaseSelection && paintInfo.phase != PaintPhaseTextClip && !isPrinting) {
655 // Custom highlighters go behind everything else.
656 if (styleToUse->highlight() != nullAtom && !context->paintingDisabled())
657 paintCustomHighlight(adjustedPaintOffset, styleToUse->highlight());
660 if (containsComposition && !useCustomUnderlines)
661 paintCompositionBackground(context, boxOrigin, styleToUse, font,
662 renderer()->frame()->editor()->compositionStart(),
663 renderer()->frame()->editor()->compositionEnd());
665 paintDocumentMarkers(context, boxOrigin, styleToUse, font, true);
667 if (haveSelection && !useCustomUnderlines)
668 paintSelection(context, boxOrigin, styleToUse, font, selectionFillColor);
671 if (Frame* frame = renderer()->frame()) {
672 if (Page* page = frame->page()) {
673 // FIXME: Right now, InlineTextBoxes never call addRelevantUnpaintedObject() even though they might
674 // legitimately be unpainted if they are waiting on a slow-loading web font. We should fix that, and
675 // when we do, we will have to account for the fact the InlineTextBoxes do not always have unique
676 // renderers and Page currently relies on each unpainted object having a unique renderer.
677 if (paintInfo.phase == PaintPhaseForeground)
678 page->addRelevantRepaintedObject(renderer(), IntRect(boxOrigin.x(), boxOrigin.y(), logicalWidth(), logicalHeight()));
682 // 2. Now paint the foreground, including text and decorations like underline/overline (in quirks mode only).
687 string = textRenderer()->text();
688 if (static_cast<unsigned>(length) != string.length() || m_start) {
689 ASSERT_WITH_SECURITY_IMPLICATION(static_cast<unsigned>(m_start + length) <= string.length());
690 string = string.substringSharingImpl(m_start, length);
692 maximumLength = textRenderer()->textLength() - m_start;
694 combinedText->getStringToRender(m_start, string, length);
695 maximumLength = length;
698 BufferForAppendingHyphen charactersWithHyphen;
699 TextRun textRun = constructTextRun(styleToUse, font, string, maximumLength, hasHyphen() ? &charactersWithHyphen : 0);
701 length = textRun.length();
705 if (paintSelectedTextOnly || paintSelectedTextSeparately)
706 selectionStartEnd(sPos, ePos);
708 if (m_truncation != cNoTruncation) {
709 sPos = min<int>(sPos, m_truncation);
710 ePos = min<int>(ePos, m_truncation);
711 length = m_truncation;
714 int emphasisMarkOffset = 0;
715 TextEmphasisPosition emphasisMarkPosition;
716 bool hasTextEmphasis = getEmphasisMarkPosition(styleToUse, emphasisMarkPosition);
717 const AtomicString& emphasisMark = hasTextEmphasis ? styleToUse->textEmphasisMarkString() : nullAtom;
718 if (!emphasisMark.isEmpty())
719 emphasisMarkOffset = emphasisMarkPosition == TextEmphasisPositionOver ? -font.fontMetrics().ascent() - font.emphasisMarkDescent(emphasisMark) : font.fontMetrics().descent() + font.emphasisMarkAscent(emphasisMark);
721 if (!paintSelectedTextOnly) {
722 // For stroked painting, we have to change the text drawing mode. It's probably dangerous to leave that mutated as a side
723 // effect, so only when we know we're stroking, do a save/restore.
724 GraphicsContextStateSaver stateSaver(*context, textStrokeWidth > 0);
726 updateGraphicsContext(context, textFillColor, textStrokeColor, textStrokeWidth, styleToUse->colorSpace());
727 if (!paintSelectedTextSeparately || ePos <= sPos) {
728 // FIXME: Truncate right-to-left text correctly.
729 paintTextWithShadows(context, font, textRun, nullAtom, 0, 0, length, length, textOrigin, boxRect, textShadow, textStrokeWidth > 0, isHorizontal());
731 paintTextWithShadows(context, font, textRun, nullAtom, 0, ePos, sPos, length, textOrigin, boxRect, textShadow, textStrokeWidth > 0, isHorizontal());
733 if (!emphasisMark.isEmpty()) {
734 updateGraphicsContext(context, emphasisMarkColor, textStrokeColor, textStrokeWidth, styleToUse->colorSpace());
736 DEFINE_STATIC_LOCAL(TextRun, objectReplacementCharacterTextRun, (&objectReplacementCharacter, 1));
737 TextRun& emphasisMarkTextRun = combinedText ? objectReplacementCharacterTextRun : textRun;
738 FloatPoint emphasisMarkTextOrigin = combinedText ? FloatPoint(boxOrigin.x() + boxRect.width() / 2, boxOrigin.y() + font.fontMetrics().ascent()) : textOrigin;
740 context->concatCTM(rotation(boxRect, Clockwise));
742 if (!paintSelectedTextSeparately || ePos <= sPos) {
743 // FIXME: Truncate right-to-left text correctly.
744 paintTextWithShadows(context, combinedText ? combinedText->originalFont() : font, emphasisMarkTextRun, emphasisMark, emphasisMarkOffset, 0, length, length, emphasisMarkTextOrigin, boxRect, textShadow, textStrokeWidth > 0, isHorizontal());
746 paintTextWithShadows(context, combinedText ? combinedText->originalFont() : font, emphasisMarkTextRun, emphasisMark, emphasisMarkOffset, ePos, sPos, length, emphasisMarkTextOrigin, boxRect, textShadow, textStrokeWidth > 0, isHorizontal());
749 context->concatCTM(rotation(boxRect, Counterclockwise));
753 if ((paintSelectedTextOnly || paintSelectedTextSeparately) && sPos < ePos) {
754 // paint only the text that is selected
755 GraphicsContextStateSaver stateSaver(*context, selectionStrokeWidth > 0);
757 updateGraphicsContext(context, selectionFillColor, selectionStrokeColor, selectionStrokeWidth, styleToUse->colorSpace());
758 paintTextWithShadows(context, font, textRun, nullAtom, 0, sPos, ePos, length, textOrigin, boxRect, selectionShadow, selectionStrokeWidth > 0, isHorizontal());
759 if (!emphasisMark.isEmpty()) {
760 updateGraphicsContext(context, selectionEmphasisMarkColor, textStrokeColor, textStrokeWidth, styleToUse->colorSpace());
762 DEFINE_STATIC_LOCAL(TextRun, objectReplacementCharacterTextRun, (&objectReplacementCharacter, 1));
763 TextRun& emphasisMarkTextRun = combinedText ? objectReplacementCharacterTextRun : textRun;
764 FloatPoint emphasisMarkTextOrigin = combinedText ? FloatPoint(boxOrigin.x() + boxRect.width() / 2, boxOrigin.y() + font.fontMetrics().ascent()) : textOrigin;
766 context->concatCTM(rotation(boxRect, Clockwise));
768 paintTextWithShadows(context, combinedText ? combinedText->originalFont() : font, emphasisMarkTextRun, emphasisMark, emphasisMarkOffset, sPos, ePos, length, emphasisMarkTextOrigin, boxRect, selectionShadow, selectionStrokeWidth > 0, isHorizontal());
771 context->concatCTM(rotation(boxRect, Counterclockwise));
776 ETextDecoration textDecorations = styleToUse->textDecorationsInEffect();
777 if (textDecorations != TDNONE && paintInfo.phase != PaintPhaseSelection) {
778 updateGraphicsContext(context, textFillColor, textStrokeColor, textStrokeWidth, styleToUse->colorSpace());
780 context->concatCTM(rotation(boxRect, Clockwise));
781 paintDecoration(context, boxOrigin, textDecorations, styleToUse->textDecorationStyle(), textShadow);
783 context->concatCTM(rotation(boxRect, Counterclockwise));
786 if (paintInfo.phase == PaintPhaseForeground) {
787 paintDocumentMarkers(context, boxOrigin, styleToUse, font, false);
789 if (useCustomUnderlines) {
790 const Vector<CompositionUnderline>& underlines = renderer()->frame()->editor()->customCompositionUnderlines();
791 size_t numUnderlines = underlines.size();
793 for (size_t index = 0; index < numUnderlines; ++index) {
794 const CompositionUnderline& underline = underlines[index];
796 if (underline.endOffset <= start())
797 // underline is completely before this run. This might be an underline that sits
798 // before the first run we draw, or underlines that were within runs we skipped
799 // due to truncation.
802 if (underline.startOffset <= end()) {
803 // underline intersects this run. Paint it.
804 paintCompositionUnderline(context, boxOrigin, underline);
805 if (underline.endOffset > end() + 1)
806 // underline also runs into the next run. Bail now, no more marker advancement.
809 // underline is completely after this run, bail. A later run will paint it.
816 context->concatCTM(rotation(boxRect, Counterclockwise));
819 void InlineTextBox::selectionStartEnd(int& sPos, int& ePos)
821 int startPos, endPos;
822 if (renderer()->selectionState() == RenderObject::SelectionInside) {
824 endPos = textRenderer()->textLength();
826 textRenderer()->selectionStartEnd(startPos, endPos);
827 if (renderer()->selectionState() == RenderObject::SelectionStart)
828 endPos = textRenderer()->textLength();
829 else if (renderer()->selectionState() == RenderObject::SelectionEnd)
833 sPos = max(startPos - m_start, 0);
834 ePos = min(endPos - m_start, (int)m_len);
837 void alignSelectionRectToDevicePixels(FloatRect& rect)
839 float maxX = floorf(rect.maxX());
840 rect.setX(floorf(rect.x()));
841 rect.setWidth(roundf(maxX - rect.x()));
844 void InlineTextBox::paintSelection(GraphicsContext* context, const FloatPoint& boxOrigin, RenderStyle* style, const Font& font, Color textColor)
846 if (context->paintingDisabled())
849 // See if we have a selection to paint at all.
851 selectionStartEnd(sPos, ePos);
855 Color c = renderer()->selectionBackgroundColor();
856 if (!c.isValid() || c.alpha() == 0)
859 // If the text color ends up being the same as the selection background, invert the selection
862 c = Color(0xff - c.red(), 0xff - c.green(), 0xff - c.blue());
864 GraphicsContextStateSaver stateSaver(*context);
865 updateGraphicsContext(context, c, c, 0, style->colorSpace()); // Don't draw text at all!
867 // If the text is truncated, let the thing being painted in the truncation
868 // draw its own highlight.
869 int length = m_truncation != cNoTruncation ? m_truncation : m_len;
870 String string = textRenderer()->text();
872 if (string.length() != static_cast<unsigned>(length) || m_start) {
873 ASSERT_WITH_SECURITY_IMPLICATION(static_cast<unsigned>(m_start + length) <= string.length());
874 string = string.substringSharingImpl(m_start, length);
877 BufferForAppendingHyphen charactersWithHyphen;
878 bool respectHyphen = ePos == length && hasHyphen();
879 TextRun textRun = constructTextRun(style, font, string, textRenderer()->textLength() - m_start, respectHyphen ? &charactersWithHyphen : 0);
881 ePos = textRun.length();
883 LayoutUnit selectionBottom = root()->selectionBottom();
884 LayoutUnit selectionTop = root()->selectionTopAdjustedForPrecedingBlock();
886 int deltaY = roundToInt(renderer()->style()->isFlippedLinesWritingMode() ? selectionBottom - logicalBottom() : logicalTop() - selectionTop);
887 int selHeight = max(0, roundToInt(selectionBottom - selectionTop));
889 FloatPoint localOrigin(boxOrigin.x(), boxOrigin.y() - deltaY);
890 FloatRect clipRect(localOrigin, FloatSize(m_logicalWidth, selHeight));
891 alignSelectionRectToDevicePixels(clipRect);
893 context->clip(clipRect);
895 context->drawHighlightForText(font, textRun, localOrigin, selHeight, c, style->colorSpace(), sPos, ePos);
898 void InlineTextBox::paintCompositionBackground(GraphicsContext* context, const FloatPoint& boxOrigin, RenderStyle* style, const Font& font, int startPos, int endPos)
900 int offset = m_start;
901 int sPos = max(startPos - offset, 0);
902 int ePos = min(endPos - offset, (int)m_len);
907 GraphicsContextStateSaver stateSaver(*context);
909 Color c = Color(225, 221, 85);
911 updateGraphicsContext(context, c, c, 0, style->colorSpace()); // Don't draw text at all!
913 int deltaY = renderer()->style()->isFlippedLinesWritingMode() ? selectionBottom() - logicalBottom() : logicalTop() - selectionTop();
914 int selHeight = selectionHeight();
915 FloatPoint localOrigin(boxOrigin.x(), boxOrigin.y() - deltaY);
916 context->drawHighlightForText(font, constructTextRun(style, font), localOrigin, selHeight, c, style->colorSpace(), sPos, ePos);
921 void InlineTextBox::paintCustomHighlight(const LayoutPoint& paintOffset, const AtomicString& type)
923 Frame* frame = renderer()->frame();
926 Page* page = frame->page();
930 RootInlineBox* r = root();
931 FloatRect rootRect(paintOffset.x() + r->x(), paintOffset.y() + selectionTop(), r->logicalWidth(), selectionHeight());
932 FloatRect textRect(paintOffset.x() + x(), rootRect.y(), logicalWidth(), rootRect.height());
934 page->chrome()->client()->paintCustomHighlight(renderer()->node(), type, textRect, rootRect, true, false);
939 static StrokeStyle textDecorationStyleToStrokeStyle(TextDecorationStyle decorationStyle)
941 StrokeStyle strokeStyle = SolidStroke;
942 switch (decorationStyle) {
943 case TextDecorationStyleSolid:
944 strokeStyle = SolidStroke;
946 #if ENABLE(CSS3_TEXT)
947 case TextDecorationStyleDouble:
948 strokeStyle = DoubleStroke;
950 case TextDecorationStyleDotted:
951 strokeStyle = DottedStroke;
953 case TextDecorationStyleDashed:
954 strokeStyle = DashedStroke;
956 case TextDecorationStyleWavy:
957 strokeStyle = WavyStroke;
965 #if ENABLE(CSS3_TEXT)
966 static int computeUnderlineOffset(const TextUnderlinePosition underlinePosition, const FontMetrics& fontMetrics, const InlineTextBox* inlineTextBox, const int textDecorationThickness)
968 // Compute the gap between the font and the underline. Use at least one
969 // pixel gap, if underline is thick then use a bigger gap.
970 const int gap = max<int>(1, ceilf(textDecorationThickness / 2.0));
972 // According to the specification TextUnderlinePositionAuto should default to 'alphabetic' for horizontal text
973 // and to 'under Left' for vertical text (e.g. japanese). We support only horizontal text for now.
974 switch (underlinePosition) {
975 case TextUnderlinePositionAlphabetic:
976 case TextUnderlinePositionAuto:
977 return fontMetrics.ascent() + gap; // Position underline near the alphabetic baseline.
978 case TextUnderlinePositionUnder: {
979 // Position underline relative to the under edge of the lowest element's content box.
980 const float offset = inlineTextBox->root()->maxLogicalTop() - inlineTextBox->logicalTop();
982 return inlineTextBox->logicalHeight() + gap + offset;
983 return inlineTextBox->logicalHeight() + gap;
987 ASSERT_NOT_REACHED();
988 return fontMetrics.ascent() + gap;
992 #if ENABLE(CSS3_TEXT)
993 static void adjustStepToDecorationLength(float& step, float& controlPointDistance, float length)
1000 unsigned stepCount = static_cast<unsigned>(length / step);
1002 // Each Bezier curve starts at the same pixel that the previous one
1003 // ended. We need to subtract (stepCount - 1) pixels when calculating the
1004 // length covered to account for that.
1005 float uncoveredLength = length - (stepCount * step - (stepCount - 1));
1006 float adjustment = uncoveredLength / stepCount;
1008 controlPointDistance += adjustment;
1012 * Draw one cubic Bezier curve and repeat the same pattern long the the decoration's axis.
1013 * The start point (p1), controlPoint1, controlPoint2 and end point (p2) of the Bezier curve
1014 * form a diamond shape:
1026 * (x1, y1) p1 + . + p2 (x2, y2) - <--- Decoration's axis
1029 * . . | controlPointDistance
1038 static void strokeWavyTextDecoration(GraphicsContext* context, FloatPoint& p1, FloatPoint& p2, float strokeThickness)
1040 context->adjustLineToPixelBoundaries(p1, p2, strokeThickness, context->strokeStyle());
1045 // Distance between decoration's axis and Bezier curve's control points.
1046 // The height of the curve is based on this distance. Use a minimum of 6 pixels distance since
1047 // the actual curve passes approximately at half of that distance, that is 3 pixels.
1048 // The minimum height of the curve is also approximately 3 pixels. Increases the curve's height
1049 // as strockThickness increases to make the curve looks better.
1050 float controlPointDistance = 3 * max<float>(2, strokeThickness);
1052 // Increment used to form the diamond shape between start point (p1), control
1053 // points and end point (p2) along the axis of the decoration. Makes the
1054 // curve wider as strockThickness increases to make the curve looks better.
1055 float step = 2 * max<float>(2, strokeThickness);
1057 bool isVerticalLine = (p1.x() == p2.x());
1059 if (isVerticalLine) {
1060 ASSERT(p1.x() == p2.x());
1062 float xAxis = p1.x();
1066 if (p1.y() < p2.y()) {
1074 adjustStepToDecorationLength(step, controlPointDistance, y2 - y1);
1075 FloatPoint controlPoint1(xAxis + controlPointDistance, 0);
1076 FloatPoint controlPoint2(xAxis - controlPointDistance, 0);
1078 for (float y = y1; y + 2 * step <= y2;) {
1079 controlPoint1.setY(y + step);
1080 controlPoint2.setY(y + step);
1082 path.addBezierCurveTo(controlPoint1, controlPoint2, FloatPoint(xAxis, y));
1085 ASSERT(p1.y() == p2.y());
1087 float yAxis = p1.y();
1091 if (p1.x() < p2.x()) {
1099 adjustStepToDecorationLength(step, controlPointDistance, x2 - x1);
1100 FloatPoint controlPoint1(0, yAxis + controlPointDistance);
1101 FloatPoint controlPoint2(0, yAxis - controlPointDistance);
1103 for (float x = x1; x + 2 * step <= x2;) {
1104 controlPoint1.setX(x + step);
1105 controlPoint2.setX(x + step);
1107 path.addBezierCurveTo(controlPoint1, controlPoint2, FloatPoint(x, yAxis));
1111 context->setShouldAntialias(true);
1112 context->strokePath(path);
1116 void InlineTextBox::paintDecoration(GraphicsContext* context, const FloatPoint& boxOrigin, ETextDecoration deco, TextDecorationStyle decorationStyle, const ShadowData* shadow)
1118 // FIXME: We should improve this rule and not always just assume 1.
1119 const float textDecorationThickness = 1.f;
1121 if (m_truncation == cFullTruncation)
1124 FloatPoint localOrigin = boxOrigin;
1126 float width = m_logicalWidth;
1127 if (m_truncation != cNoTruncation) {
1128 width = toRenderText(renderer())->width(m_start, m_truncation, textPos(), isFirstLineStyle());
1129 if (!isLeftToRightDirection())
1130 localOrigin.move(m_logicalWidth - width, 0);
1133 // Get the text decoration colors.
1134 Color underline, overline, linethrough;
1135 renderer()->getTextDecorationColors(deco, underline, overline, linethrough, true);
1136 if (isFirstLineStyle())
1137 renderer()->getTextDecorationColors(deco, underline, overline, linethrough, true, true);
1139 // Use a special function for underlines to get the positioning exactly right.
1140 bool isPrinting = textRenderer()->document()->printing();
1141 context->setStrokeThickness(textDecorationThickness);
1143 bool linesAreOpaque = !isPrinting && (!(deco & UNDERLINE) || underline.alpha() == 255) && (!(deco & OVERLINE) || overline.alpha() == 255) && (!(deco & LINE_THROUGH) || linethrough.alpha() == 255);
1145 RenderStyle* styleToUse = renderer()->style(isFirstLineStyle());
1146 int baseline = styleToUse->fontMetrics().ascent();
1148 bool setClip = false;
1149 int extraOffset = 0;
1150 if (!linesAreOpaque && shadow && shadow->next()) {
1151 FloatRect clipRect(localOrigin, FloatSize(width, baseline + 2));
1152 for (const ShadowData* s = shadow; s; s = s->next()) {
1153 int shadowExtent = s->paintingExtent();
1154 FloatRect shadowRect(localOrigin, FloatSize(width, baseline + 2));
1155 shadowRect.inflate(shadowExtent);
1156 int shadowX = isHorizontal() ? s->x() : s->y();
1157 int shadowY = isHorizontal() ? s->y() : -s->x();
1158 shadowRect.move(shadowX, shadowY);
1159 clipRect.unite(shadowRect);
1160 extraOffset = max(extraOffset, max(0, shadowY) + shadowExtent);
1163 context->clip(clipRect);
1164 extraOffset += baseline + 2;
1165 localOrigin.move(0, extraOffset);
1169 ColorSpace colorSpace = renderer()->style()->colorSpace();
1170 bool setShadow = false;
1174 if (!shadow->next()) {
1175 // The last set of lines paints normally inside the clip.
1176 localOrigin.move(0, -extraOffset);
1179 int shadowX = isHorizontal() ? shadow->x() : shadow->y();
1180 int shadowY = isHorizontal() ? shadow->y() : -shadow->x();
1181 context->setShadow(FloatSize(shadowX, shadowY - extraOffset), shadow->radius(), shadow->color(), colorSpace);
1183 shadow = shadow->next();
1186 #if ENABLE(CSS3_TEXT)
1187 // Offset between lines - always non-zero, so lines never cross each other.
1188 float doubleOffset = textDecorationThickness + 1.f;
1190 context->setStrokeStyle(textDecorationStyleToStrokeStyle(decorationStyle));
1191 if (deco & UNDERLINE) {
1192 context->setStrokeColor(underline, colorSpace);
1193 #if ENABLE(CSS3_TEXT)
1194 TextUnderlinePosition underlinePosition = styleToUse->textUnderlinePosition();
1195 const int underlineOffset = computeUnderlineOffset(underlinePosition, styleToUse->fontMetrics(), this, textDecorationThickness);
1197 switch (decorationStyle) {
1198 case TextDecorationStyleWavy: {
1199 FloatPoint start(localOrigin.x(), localOrigin.y() + underlineOffset + doubleOffset);
1200 FloatPoint end(localOrigin.x() + width, localOrigin.y() + underlineOffset + doubleOffset);
1201 strokeWavyTextDecoration(context, start, end, textDecorationThickness);
1205 context->drawLineForText(FloatPoint(localOrigin.x(), localOrigin.y() + underlineOffset), width, isPrinting);
1207 if (decorationStyle == TextDecorationStyleDouble)
1208 context->drawLineForText(FloatPoint(localOrigin.x(), localOrigin.y() + underlineOffset + doubleOffset), width, isPrinting);
1211 // Leave one pixel of white between the baseline and the underline.
1212 context->drawLineForText(FloatPoint(localOrigin.x(), localOrigin.y() + baseline + 1), width, isPrinting);
1215 if (deco & OVERLINE) {
1216 context->setStrokeColor(overline, colorSpace);
1217 #if ENABLE(CSS3_TEXT)
1218 switch (decorationStyle) {
1219 case TextDecorationStyleWavy: {
1220 FloatPoint start(localOrigin.x(), localOrigin.y() - doubleOffset);
1221 FloatPoint end(localOrigin.x() + width, localOrigin.y() - doubleOffset);
1222 strokeWavyTextDecoration(context, start, end, textDecorationThickness);
1227 context->drawLineForText(localOrigin, width, isPrinting);
1228 #if ENABLE(CSS3_TEXT)
1229 if (decorationStyle == TextDecorationStyleDouble)
1230 context->drawLineForText(FloatPoint(localOrigin.x(), localOrigin.y() - doubleOffset), width, isPrinting);
1234 if (deco & LINE_THROUGH) {
1235 context->setStrokeColor(linethrough, colorSpace);
1236 #if ENABLE(CSS3_TEXT)
1237 switch (decorationStyle) {
1238 case TextDecorationStyleWavy: {
1239 FloatPoint start(localOrigin.x(), localOrigin.y() + 2 * baseline / 3);
1240 FloatPoint end(localOrigin.x() + width, localOrigin.y() + 2 * baseline / 3);
1241 strokeWavyTextDecoration(context, start, end, textDecorationThickness);
1246 context->drawLineForText(FloatPoint(localOrigin.x(), localOrigin.y() + 2 * baseline / 3), width, isPrinting);
1247 #if ENABLE(CSS3_TEXT)
1248 if (decorationStyle == TextDecorationStyleDouble)
1249 context->drawLineForText(FloatPoint(localOrigin.x(), localOrigin.y() + doubleOffset + 2 * baseline / 3), width, isPrinting);
1258 context->clearShadow();
1261 static GraphicsContext::DocumentMarkerLineStyle lineStyleForMarkerType(DocumentMarker::MarkerType markerType)
1263 switch (markerType) {
1264 case DocumentMarker::Spelling:
1265 return GraphicsContext::DocumentMarkerSpellingLineStyle;
1266 case DocumentMarker::Grammar:
1267 return GraphicsContext::DocumentMarkerGrammarLineStyle;
1268 case DocumentMarker::CorrectionIndicator:
1269 return GraphicsContext::DocumentMarkerAutocorrectionReplacementLineStyle;
1270 case DocumentMarker::DictationAlternatives:
1271 return GraphicsContext::DocumentMarkerDictationAlternativesLineStyle;
1273 ASSERT_NOT_REACHED();
1274 return GraphicsContext::DocumentMarkerSpellingLineStyle;
1278 void InlineTextBox::paintDocumentMarker(GraphicsContext* pt, const FloatPoint& boxOrigin, DocumentMarker* marker, RenderStyle* style, const Font& font, bool grammar)
1280 // Never print spelling/grammar markers (5327887)
1281 if (textRenderer()->document()->printing())
1284 if (m_truncation == cFullTruncation)
1287 float start = 0; // start of line to draw, relative to tx
1288 float width = m_logicalWidth; // how much line to draw
1290 // Determine whether we need to measure text
1291 bool markerSpansWholeBox = true;
1292 if (m_start <= (int)marker->startOffset())
1293 markerSpansWholeBox = false;
1294 if ((end() + 1) != marker->endOffset()) // end points at the last char, not past it
1295 markerSpansWholeBox = false;
1296 if (m_truncation != cNoTruncation)
1297 markerSpansWholeBox = false;
1299 bool isDictationMarker = marker->type() == DocumentMarker::DictationAlternatives;
1300 if (!markerSpansWholeBox || grammar || isDictationMarker) {
1301 int startPosition = max<int>(marker->startOffset() - m_start, 0);
1302 int endPosition = min<int>(marker->endOffset() - m_start, m_len);
1304 if (m_truncation != cNoTruncation)
1305 endPosition = min<int>(endPosition, m_truncation);
1307 // Calculate start & width
1308 int deltaY = renderer()->style()->isFlippedLinesWritingMode() ? selectionBottom() - logicalBottom() : logicalTop() - selectionTop();
1309 int selHeight = selectionHeight();
1310 FloatPoint startPoint(boxOrigin.x(), boxOrigin.y() - deltaY);
1311 TextRun run = constructTextRun(style, font);
1313 // FIXME: Convert the document markers to float rects.
1314 IntRect markerRect = enclosingIntRect(font.selectionRectForText(run, startPoint, selHeight, startPosition, endPosition));
1315 start = markerRect.x() - startPoint.x();
1316 width = markerRect.width();
1318 // Store rendered rects for bad grammar markers, so we can hit-test against it elsewhere in order to
1319 // display a toolTip. We don't do this for misspelling markers.
1320 if (grammar || isDictationMarker) {
1321 markerRect.move(-boxOrigin.x(), -boxOrigin.y());
1322 markerRect = renderer()->localToAbsoluteQuad(FloatRect(markerRect)).enclosingBoundingBox();
1323 toRenderedDocumentMarker(marker)->setRenderedRect(markerRect);
1327 // IMPORTANT: The misspelling underline is not considered when calculating the text bounds, so we have to
1328 // make sure to fit within those bounds. This means the top pixel(s) of the underline will overlap the
1329 // bottom pixel(s) of the glyphs in smaller font sizes. The alternatives are to increase the line spacing (bad!!)
1330 // or decrease the underline thickness. The overlap is actually the most useful, and matches what AppKit does.
1331 // So, we generally place the underline at the bottom of the text, but in larger fonts that's not so good so
1332 // we pin to two pixels under the baseline.
1333 int lineThickness = cMisspellingLineThickness;
1334 int baseline = renderer()->style(isFirstLineStyle())->fontMetrics().ascent();
1335 int descent = logicalHeight() - baseline;
1336 int underlineOffset;
1337 if (descent <= (2 + lineThickness)) {
1338 // Place the underline at the very bottom of the text in small/medium fonts.
1339 underlineOffset = logicalHeight() - lineThickness;
1341 // In larger fonts, though, place the underline up near the baseline to prevent a big gap.
1342 underlineOffset = baseline + 2;
1344 pt->drawLineForDocumentMarker(FloatPoint(boxOrigin.x() + start, boxOrigin.y() + underlineOffset), width, lineStyleForMarkerType(marker->type()));
1347 void InlineTextBox::paintTextMatchMarker(GraphicsContext* pt, const FloatPoint& boxOrigin, DocumentMarker* marker, RenderStyle* style, const Font& font)
1349 // Use same y positioning and height as for selection, so that when the selection and this highlight are on
1350 // the same word there are no pieces sticking out.
1351 int deltaY = renderer()->style()->isFlippedLinesWritingMode() ? selectionBottom() - logicalBottom() : logicalTop() - selectionTop();
1352 int selHeight = selectionHeight();
1354 int sPos = max(marker->startOffset() - m_start, (unsigned)0);
1355 int ePos = min(marker->endOffset() - m_start, (unsigned)m_len);
1356 TextRun run = constructTextRun(style, font);
1358 // Always compute and store the rect associated with this marker. The computed rect is in absolute coordinates.
1359 IntRect markerRect = enclosingIntRect(font.selectionRectForText(run, IntPoint(x(), selectionTop()), selHeight, sPos, ePos));
1360 markerRect = renderer()->localToAbsoluteQuad(FloatRect(markerRect)).enclosingBoundingBox();
1361 toRenderedDocumentMarker(marker)->setRenderedRect(markerRect);
1363 // Optionally highlight the text
1364 if (renderer()->frame()->editor()->markedTextMatchesAreHighlighted()) {
1365 Color color = marker->activeMatch() ?
1366 renderer()->theme()->platformActiveTextSearchHighlightColor() :
1367 renderer()->theme()->platformInactiveTextSearchHighlightColor();
1368 GraphicsContextStateSaver stateSaver(*pt);
1369 updateGraphicsContext(pt, color, color, 0, style->colorSpace()); // Don't draw text at all!
1370 pt->clip(FloatRect(boxOrigin.x(), boxOrigin.y() - deltaY, m_logicalWidth, selHeight));
1371 pt->drawHighlightForText(font, run, FloatPoint(boxOrigin.x(), boxOrigin.y() - deltaY), selHeight, color, style->colorSpace(), sPos, ePos);
1375 void InlineTextBox::computeRectForReplacementMarker(DocumentMarker* marker, RenderStyle* style, const Font& font)
1377 // Replacement markers are not actually drawn, but their rects need to be computed for hit testing.
1378 int top = selectionTop();
1379 int h = selectionHeight();
1381 int sPos = max(marker->startOffset() - m_start, (unsigned)0);
1382 int ePos = min(marker->endOffset() - m_start, (unsigned)m_len);
1383 TextRun run = constructTextRun(style, font);
1384 IntPoint startPoint = IntPoint(x(), top);
1386 // Compute and store the rect associated with this marker.
1387 IntRect markerRect = enclosingIntRect(font.selectionRectForText(run, startPoint, h, sPos, ePos));
1388 markerRect = renderer()->localToAbsoluteQuad(FloatRect(markerRect)).enclosingBoundingBox();
1389 toRenderedDocumentMarker(marker)->setRenderedRect(markerRect);
1392 void InlineTextBox::paintDocumentMarkers(GraphicsContext* pt, const FloatPoint& boxOrigin, RenderStyle* style, const Font& font, bool background)
1394 if (!renderer()->node())
1397 Vector<DocumentMarker*> markers = renderer()->document()->markers()->markersFor(renderer()->node());
1398 Vector<DocumentMarker*>::const_iterator markerIt = markers.begin();
1400 // Give any document markers that touch this run a chance to draw before the text has been drawn.
1401 // Note end() points at the last char, not one past it like endOffset and ranges do.
1402 for ( ; markerIt != markers.end(); ++markerIt) {
1403 DocumentMarker* marker = *markerIt;
1405 // Paint either the background markers or the foreground markers, but not both
1406 switch (marker->type()) {
1407 case DocumentMarker::Grammar:
1408 case DocumentMarker::Spelling:
1409 case DocumentMarker::CorrectionIndicator:
1410 case DocumentMarker::Replacement:
1411 case DocumentMarker::DictationAlternatives:
1415 case DocumentMarker::TextMatch:
1423 if (marker->endOffset() <= start())
1424 // marker is completely before this run. This might be a marker that sits before the
1425 // first run we draw, or markers that were within runs we skipped due to truncation.
1428 if (marker->startOffset() > end())
1429 // marker is completely after this run, bail. A later run will paint it.
1432 // marker intersects this run. Paint it.
1433 switch (marker->type()) {
1434 case DocumentMarker::Spelling:
1435 case DocumentMarker::CorrectionIndicator:
1436 case DocumentMarker::DictationAlternatives:
1437 paintDocumentMarker(pt, boxOrigin, marker, style, font, false);
1439 case DocumentMarker::Grammar:
1440 paintDocumentMarker(pt, boxOrigin, marker, style, font, true);
1442 case DocumentMarker::TextMatch:
1443 paintTextMatchMarker(pt, boxOrigin, marker, style, font);
1445 case DocumentMarker::Replacement:
1446 computeRectForReplacementMarker(marker, style, font);
1449 ASSERT_NOT_REACHED();
1455 void InlineTextBox::paintCompositionUnderline(GraphicsContext* ctx, const FloatPoint& boxOrigin, const CompositionUnderline& underline)
1457 if (m_truncation == cFullTruncation)
1460 float start = 0; // start of line to draw, relative to tx
1461 float width = m_logicalWidth; // how much line to draw
1462 bool useWholeWidth = true;
1463 unsigned paintStart = m_start;
1464 unsigned paintEnd = end() + 1; // end points at the last char, not past it
1465 if (paintStart <= underline.startOffset) {
1466 paintStart = underline.startOffset;
1467 useWholeWidth = false;
1468 start = toRenderText(renderer())->width(m_start, paintStart - m_start, textPos(), isFirstLineStyle());
1470 if (paintEnd != underline.endOffset) { // end points at the last char, not past it
1471 paintEnd = min(paintEnd, (unsigned)underline.endOffset);
1472 useWholeWidth = false;
1474 if (m_truncation != cNoTruncation) {
1475 paintEnd = min(paintEnd, (unsigned)m_start + m_truncation);
1476 useWholeWidth = false;
1478 if (!useWholeWidth) {
1479 width = toRenderText(renderer())->width(paintStart, paintEnd - paintStart, textPos() + start, isFirstLineStyle());
1482 // Thick marked text underlines are 2px thick as long as there is room for the 2px line under the baseline.
1483 // All other marked text underlines are 1px thick.
1484 // If there's not enough space the underline will touch or overlap characters.
1485 int lineThickness = 1;
1486 int baseline = renderer()->style(isFirstLineStyle())->fontMetrics().ascent();
1487 if (underline.thick && logicalHeight() - baseline >= 2)
1490 // We need to have some space between underlines of subsequent clauses, because some input methods do not use different underline styles for those.
1491 // We make each line shorter, which has a harmless side effect of shortening the first and last clauses, too.
1495 ctx->setStrokeColor(underline.color, renderer()->style()->colorSpace());
1496 ctx->setStrokeThickness(lineThickness);
1497 ctx->drawLineForText(FloatPoint(boxOrigin.x() + start, boxOrigin.y() + logicalHeight() - lineThickness), width, textRenderer()->document()->printing());
1500 int InlineTextBox::caretMinOffset() const
1505 int InlineTextBox::caretMaxOffset() const
1507 return m_start + m_len;
1510 float InlineTextBox::textPos() const
1512 // When computing the width of a text run, RenderBlock::computeInlineDirectionPositionsForLine() doesn't include the actual offset
1513 // from the containing block edge in its measurement. textPos() should be consistent so the text are rendered in the same width.
1514 if (logicalLeft() == 0)
1516 return logicalLeft() - root()->logicalLeft();
1519 int InlineTextBox::offsetForPosition(float lineOffset, bool includePartialGlyphs) const
1524 if (lineOffset - logicalLeft() > logicalWidth())
1525 return isLeftToRightDirection() ? len() : 0;
1526 if (lineOffset - logicalLeft() < 0)
1527 return isLeftToRightDirection() ? 0 : len();
1529 FontCachePurgePreventer fontCachePurgePreventer;
1531 RenderText* text = toRenderText(renderer());
1532 RenderStyle* style = text->style(isFirstLineStyle());
1533 const Font& font = style->font();
1534 return font.offsetForPosition(constructTextRun(style, font), lineOffset - logicalLeft(), includePartialGlyphs);
1537 float InlineTextBox::positionForOffset(int offset) const
1539 ASSERT(offset >= m_start);
1540 ASSERT(offset <= m_start + m_len);
1543 return logicalLeft();
1545 FontCachePurgePreventer fontCachePurgePreventer;
1547 RenderText* text = toRenderText(renderer());
1548 RenderStyle* styleToUse = text->style(isFirstLineStyle());
1550 const Font& font = styleToUse->font();
1551 int from = !isLeftToRightDirection() ? offset - m_start : 0;
1552 int to = !isLeftToRightDirection() ? m_len : offset - m_start;
1553 // FIXME: Do we need to add rightBearing here?
1554 return font.selectionRectForText(constructTextRun(styleToUse, font), IntPoint(logicalLeft(), 0), 0, from, to).maxX();
1557 bool InlineTextBox::containsCaretOffset(int offset) const
1559 // Offsets before the box are never "in".
1560 if (offset < m_start)
1563 int pastEnd = m_start + m_len;
1565 // Offsets inside the box (not at either edge) are always "in".
1566 if (offset < pastEnd)
1569 // Offsets outside the box are always "out".
1570 if (offset > pastEnd)
1573 // Offsets at the end are "out" for line breaks (they are on the next line).
1577 // Offsets at the end are "in" for normal boxes (but the caller has to check affinity).
1581 TextRun InlineTextBox::constructTextRun(RenderStyle* style, const Font& font, BufferForAppendingHyphen* charactersWithHyphen) const
1585 RenderText* textRenderer = this->textRenderer();
1586 ASSERT(textRenderer);
1587 ASSERT(textRenderer->text());
1589 String string = textRenderer->text();
1590 unsigned startPos = start();
1591 unsigned length = len();
1593 if (string.length() != length || startPos)
1594 string = string.substringSharingImpl(startPos, length);
1596 return constructTextRun(style, font, string, textRenderer->textLength() - startPos, charactersWithHyphen);
1599 TextRun InlineTextBox::constructTextRun(RenderStyle* style, const Font& font, String string, int maximumLength, BufferForAppendingHyphen* charactersWithHyphen) const
1603 RenderText* textRenderer = this->textRenderer();
1604 ASSERT(textRenderer);
1606 int length = string.length();
1608 if (charactersWithHyphen) {
1609 adjustCharactersAndLengthForHyphen(*charactersWithHyphen, style, string, length);
1610 maximumLength = length;
1613 ASSERT(maximumLength >= length);
1615 TextRun run(string, textPos(), expansion(), expansionBehavior(), direction(), dirOverride() || style->rtlOrdering() == VisualOrder, !textRenderer->canUseSimpleFontCodePath());
1616 run.setTabSize(!style->collapseWhiteSpace(), style->tabSize());
1617 if (textRunNeedsRenderingContext(font))
1618 run.setRenderingContext(SVGTextRunRenderingContext::create(textRenderer));
1620 // Propagate the maximum length of the characters buffer to the TextRun, even when we're only processing a substring.
1621 run.setCharactersLength(maximumLength);
1622 ASSERT(run.charactersLength() >= run.length());
1628 const char* InlineTextBox::boxName() const
1630 return "InlineTextBox";
1633 void InlineTextBox::showBox(int printedCharacters) const
1635 const RenderText* obj = toRenderText(renderer());
1636 String value = obj->text();
1637 value = value.substring(start(), len());
1638 value.replaceWithLiteral('\\', "\\\\");
1639 value.replaceWithLiteral('\n', "\\n");
1640 printedCharacters += fprintf(stderr, "%s\t%p", boxName(), this);
1641 for (; printedCharacters < showTreeCharacterOffset; printedCharacters++)
1643 printedCharacters = fprintf(stderr, "\t%s %p", obj->renderName(), obj);
1644 const int rendererCharacterOffset = 24;
1645 for (; printedCharacters < rendererCharacterOffset; printedCharacters++)
1647 fprintf(stderr, "(%d,%d) \"%s\"\n", start(), start() + len(), value.utf8().data());
1652 void InlineTextBox::reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
1654 MemoryClassInfo info(memoryObjectInfo, this, WebCoreMemoryTypes::Rendering);
1655 InlineBox::reportMemoryUsage(memoryObjectInfo);
1656 info.addMember(m_prevTextBox, "prevTextBox");
1657 info.addMember(m_nextTextBox, "nextTextBox");
1660 } // namespace WebCore