1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 #include "core/rendering/TextPainter.h"
8 #include "core/CSSPropertyNames.h"
9 #include "core/frame/Settings.h"
10 #include "core/rendering/InlineTextBox.h"
11 #include "core/rendering/RenderCombineText.h"
12 #include "core/rendering/RenderObject.h"
13 #include "core/rendering/style/RenderStyle.h"
14 #include "core/rendering/style/ShadowList.h"
15 #include "platform/fonts/Font.h"
16 #include "platform/graphics/GraphicsContext.h"
17 #include "platform/graphics/GraphicsContextStateSaver.h"
18 #include "platform/text/TextRun.h"
19 #include "wtf/Assertions.h"
20 #include "wtf/unicode/CharacterNames.h"
24 TextPainter::TextPainter(GraphicsContext* context, const Font& font, const TextRun& run, const FloatPoint& textOrigin, const FloatRect& textBounds, bool horizontal)
25 : m_graphicsContext(context)
28 , m_textOrigin(textOrigin)
29 , m_textBounds(textBounds)
30 , m_horizontal(horizontal)
31 , m_emphasisMarkOffset(0)
36 TextPainter::~TextPainter()
40 void TextPainter::setEmphasisMark(const AtomicString& emphasisMark, TextEmphasisPosition position)
42 m_emphasisMark = emphasisMark;
44 if (emphasisMark.isNull()) {
45 m_emphasisMarkOffset = 0;
46 } else if (position == TextEmphasisPositionOver) {
47 m_emphasisMarkOffset = -m_font.fontMetrics().ascent() - m_font.emphasisMarkDescent(emphasisMark);
49 ASSERT(position == TextEmphasisPositionUnder);
50 m_emphasisMarkOffset = m_font.fontMetrics().descent() + m_font.emphasisMarkAscent(emphasisMark);
54 void TextPainter::paint(int startOffset, int endOffset, int length, const Style& textStyle, TextBlobPtr* cachedTextBlob)
56 GraphicsContextStateSaver stateSaver(*m_graphicsContext, false);
57 updateGraphicsContext(textStyle, stateSaver);
58 paintInternal<PaintText>(startOffset, endOffset, length, cachedTextBlob);
60 if (!m_emphasisMark.isEmpty()) {
61 if (textStyle.emphasisMarkColor != textStyle.fillColor)
62 m_graphicsContext->setFillColor(textStyle.emphasisMarkColor);
65 paintEmphasisMarkForCombinedText();
67 paintInternal<PaintEmphasisMark>(startOffset, endOffset, length);
72 void TextPainter::updateGraphicsContext(GraphicsContext* context, const Style& textStyle, bool horizontal, GraphicsContextStateSaver& stateSaver)
74 TextDrawingModeFlags mode = context->textDrawingMode();
75 if (textStyle.strokeWidth > 0) {
76 TextDrawingModeFlags newMode = mode | TextModeStroke;
77 if (mode != newMode) {
78 if (!stateSaver.saved())
80 context->setTextDrawingMode(newMode);
85 if (mode & TextModeFill && textStyle.fillColor != context->fillColor())
86 context->setFillColor(textStyle.fillColor);
88 if (mode & TextModeStroke) {
89 if (textStyle.strokeColor != context->strokeColor())
90 context->setStrokeColor(textStyle.strokeColor);
91 if (textStyle.strokeWidth != context->strokeThickness())
92 context->setStrokeThickness(textStyle.strokeWidth);
95 // Text shadows are disabled when printing. http://crbug.com/258321
96 if (textStyle.shadow && !context->printing()) {
97 if (!stateSaver.saved())
99 context->setDrawLooper(textStyle.shadow->createDrawLooper(DrawLooperBuilder::ShadowIgnoresAlpha, horizontal));
103 static Color textColorForWhiteBackground(Color textColor)
105 int distanceFromWhite = differenceSquared(textColor, Color::white);
106 // semi-arbitrarily chose 65025 (255^2) value here after a few tests;
107 return distanceFromWhite > 65025 ? textColor : textColor.dark();
111 TextPainter::Style TextPainter::textPaintingStyle(RenderObject& renderer, RenderStyle* style, bool forceBlackText, bool isPrinting)
113 TextPainter::Style textStyle;
115 if (forceBlackText) {
116 textStyle.fillColor = Color::black;
117 textStyle.strokeColor = Color::black;
118 textStyle.emphasisMarkColor = Color::black;
119 textStyle.strokeWidth = style->textStrokeWidth();
120 textStyle.shadow = 0;
122 textStyle.fillColor = renderer.resolveColor(style, CSSPropertyWebkitTextFillColor);
123 textStyle.strokeColor = renderer.resolveColor(style, CSSPropertyWebkitTextStrokeColor);
124 textStyle.emphasisMarkColor = renderer.resolveColor(style, CSSPropertyWebkitTextEmphasisColor);
125 textStyle.strokeWidth = style->textStrokeWidth();
126 textStyle.shadow = style->textShadow();
128 // Adjust text color when printing with a white background.
129 bool forceBackgroundToWhite = false;
131 if (style->printColorAdjust() == PrintColorAdjustEconomy)
132 forceBackgroundToWhite = true;
133 if (renderer.document().settings() && renderer.document().settings()->shouldPrintBackgrounds())
134 forceBackgroundToWhite = false;
136 if (forceBackgroundToWhite) {
137 textStyle.fillColor = textColorForWhiteBackground(textStyle.fillColor);
138 textStyle.strokeColor = textColorForWhiteBackground(textStyle.strokeColor);
139 textStyle.emphasisMarkColor = textColorForWhiteBackground(textStyle.emphasisMarkColor);
142 // Text shadows are disabled when printing. http://crbug.com/258321
144 textStyle.shadow = 0;
150 TextPainter::Style TextPainter::selectionPaintingStyle(RenderObject& renderer, bool haveSelection, bool forceBlackText, bool isPrinting, const TextPainter::Style& textStyle)
152 TextPainter::Style selectionStyle = textStyle;
155 if (!forceBlackText) {
156 selectionStyle.fillColor = renderer.selectionForegroundColor();
157 selectionStyle.emphasisMarkColor = renderer.selectionEmphasisMarkColor();
160 if (RenderStyle* pseudoStyle = renderer.getCachedPseudoStyle(SELECTION)) {
161 selectionStyle.strokeColor = forceBlackText ? Color::black : renderer.resolveColor(pseudoStyle, CSSPropertyWebkitTextStrokeColor);
162 selectionStyle.strokeWidth = pseudoStyle->textStrokeWidth();
163 selectionStyle.shadow = forceBlackText ? 0 : pseudoStyle->textShadow();
166 // Text shadows are disabled when printing. http://crbug.com/258321
168 selectionStyle.shadow = 0;
171 return selectionStyle;
174 template <TextPainter::PaintInternalStep step>
175 void TextPainter::paintInternalRun(TextRunPaintInfo& textRunPaintInfo, int from, int to)
177 textRunPaintInfo.from = from;
178 textRunPaintInfo.to = to;
180 if (step == PaintEmphasisMark) {
181 m_graphicsContext->drawEmphasisMarks(m_font, textRunPaintInfo, m_emphasisMark,
182 m_textOrigin + IntSize(0, m_emphasisMarkOffset));
184 ASSERT(step == PaintText);
185 m_graphicsContext->drawText(m_font, textRunPaintInfo, m_textOrigin);
189 template <TextPainter::PaintInternalStep Step>
190 void TextPainter::paintInternal(int startOffset, int endOffset, int truncationPoint, TextBlobPtr* cachedTextBlob)
192 TextRunPaintInfo textRunPaintInfo(m_run);
193 textRunPaintInfo.bounds = m_textBounds;
194 if (startOffset <= endOffset) {
195 // FIXME: We should be able to use cachedTextBlob in more cases.
196 textRunPaintInfo.cachedTextBlob = cachedTextBlob;
197 paintInternalRun<Step>(textRunPaintInfo, startOffset, endOffset);
200 paintInternalRun<Step>(textRunPaintInfo, 0, endOffset);
201 if (startOffset < truncationPoint)
202 paintInternalRun<Step>(textRunPaintInfo, startOffset, truncationPoint);
206 void TextPainter::paintEmphasisMarkForCombinedText()
208 ASSERT(m_combinedText);
209 DEFINE_STATIC_LOCAL(TextRun, placeholderTextRun, (&ideographicFullStop, 1));
210 FloatPoint emphasisMarkTextOrigin(m_textBounds.x(), m_textBounds.y() + m_font.fontMetrics().ascent() + m_emphasisMarkOffset);
211 TextRunPaintInfo textRunPaintInfo(placeholderTextRun);
212 textRunPaintInfo.bounds = m_textBounds;
213 m_graphicsContext->concatCTM(InlineTextBox::rotation(m_textBounds, InlineTextBox::Clockwise));
214 m_graphicsContext->drawEmphasisMarks(m_combinedText->originalFont(), textRunPaintInfo, m_emphasisMark, emphasisMarkTextOrigin);
215 m_graphicsContext->concatCTM(InlineTextBox::rotation(m_textBounds, InlineTextBox::Counterclockwise));