RenderTheme does not need to be per-page
[WebKit-https.git] / Source / WebCore / rendering / TextPaintStyle.cpp
1 /*
2  * Copyright (C) 2013 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "TextPaintStyle.h"
28
29 #include "FocusController.h"
30 #include "Frame.h"
31 #include "GraphicsContext.h"
32 #include "Page.h"
33 #include "PaintInfo.h"
34 #include "RenderStyle.h"
35 #include "RenderText.h"
36 #include "RenderTheme.h"
37 #include "RenderView.h"
38 #include "Settings.h"
39
40 namespace WebCore {
41
42 TextPaintStyle::TextPaintStyle(const Color& color)
43     : fillColor(color)
44     , strokeColor(color)
45 {
46 }
47
48 static Color adjustColorForVisibilityOnBackground(const Color& textColor, const Color& backgroundColor)
49 {
50     int d = differenceSquared(textColor, backgroundColor);
51     // Semi-arbitrarily chose 65025 (255^2) value here after a few tests.
52     if (d > 65025)
53         return textColor;
54
55     int distanceFromWhite = differenceSquared(textColor, Color::white);
56     int distanceFromBlack = differenceSquared(textColor, Color::black);
57     if (distanceFromWhite < distanceFromBlack)
58         return textColor.dark();
59
60     return textColor.light();
61 }
62
63 TextPaintStyle computeTextPaintStyle(const Frame& frame, const RenderStyle& lineStyle, const PaintInfo& paintInfo)
64 {
65     TextPaintStyle paintStyle;
66
67 #if ENABLE(LETTERPRESS)
68     paintStyle.useLetterpressEffect = lineStyle.textDecorationsInEffect() & TextDecorationLetterpress;
69 #endif
70     auto viewportSize = frame.view() ? frame.view()->size() : IntSize();
71     paintStyle.strokeWidth = lineStyle.computedStrokeWidth(viewportSize);
72     paintStyle.paintOrder = lineStyle.paintOrder();
73     paintStyle.lineJoin = lineStyle.joinStyle();
74     paintStyle.lineCap = lineStyle.capStyle();
75     paintStyle.miterLimit = lineStyle.strokeMiterLimit();
76     
77     if (paintInfo.forceTextColor()) {
78         paintStyle.fillColor = paintInfo.forcedTextColor();
79         paintStyle.strokeColor = paintInfo.forcedTextColor();
80         paintStyle.emphasisMarkColor = paintInfo.forcedTextColor();
81         return paintStyle;
82     }
83
84     if (lineStyle.insideDefaultButton()) {
85         Page* page = frame.page();
86         if (page && page->focusController().isActive()) {
87             paintStyle.fillColor = RenderTheme::singleton().systemColor(CSSValueActivebuttontext);
88             return paintStyle;
89         }
90     }
91
92     paintStyle.fillColor = lineStyle.visitedDependentColor(CSSPropertyWebkitTextFillColor);
93
94     bool forceBackgroundToWhite = false;
95     if (frame.document() && frame.document()->printing()) {
96         if (lineStyle.printColorAdjust() == PrintColorAdjustEconomy)
97             forceBackgroundToWhite = true;
98         if (frame.settings().shouldPrintBackgrounds())
99             forceBackgroundToWhite = false;
100     }
101
102     // Make the text fill color legible against a white background
103     if (forceBackgroundToWhite)
104         paintStyle.fillColor = adjustColorForVisibilityOnBackground(paintStyle.fillColor, Color::white);
105
106     paintStyle.strokeColor = lineStyle.visitedDependentColor(lineStyle.hasExplicitlySetStrokeColor() ? CSSPropertyStrokeColor : CSSPropertyWebkitTextStrokeColor);
107
108     // Make the text stroke color legible against a white background
109     if (forceBackgroundToWhite)
110         paintStyle.strokeColor = adjustColorForVisibilityOnBackground(paintStyle.strokeColor, Color::white);
111
112     paintStyle.emphasisMarkColor = lineStyle.visitedDependentColor(CSSPropertyWebkitTextEmphasisColor);
113
114     // Make the text stroke color legible against a white background
115     if (forceBackgroundToWhite)
116         paintStyle.emphasisMarkColor = adjustColorForVisibilityOnBackground(paintStyle.emphasisMarkColor, Color::white);
117
118     return paintStyle;
119 }
120
121 TextPaintStyle computeTextSelectionPaintStyle(const TextPaintStyle& textPaintStyle, const RenderText& renderer, const RenderStyle& lineStyle, const PaintInfo& paintInfo, bool& paintSelectedTextOnly, bool& paintSelectedTextSeparately, bool& paintNonSelectedTextOnly, const ShadowData*& selectionShadow)
122 {
123     paintSelectedTextOnly = (paintInfo.phase == PaintPhaseSelection);
124     paintSelectedTextSeparately = paintInfo.paintBehavior & PaintBehaviorExcludeSelection;
125     paintNonSelectedTextOnly = paintInfo.paintBehavior & PaintBehaviorExcludeSelection;
126     selectionShadow = (paintInfo.forceTextColor()) ? nullptr : lineStyle.textShadow();
127
128     TextPaintStyle selectionPaintStyle = textPaintStyle;
129
130 #if ENABLE(TEXT_SELECTION)
131     Color foreground = paintInfo.forceTextColor() ? paintInfo.forcedTextColor() : renderer.selectionForegroundColor();
132     if (foreground.isValid() && foreground != selectionPaintStyle.fillColor) {
133         if (!paintSelectedTextOnly)
134             paintSelectedTextSeparately = true;
135         selectionPaintStyle.fillColor = foreground;
136     }
137
138     Color emphasisMarkForeground = paintInfo.forceTextColor() ? paintInfo.forcedTextColor() : renderer.selectionEmphasisMarkColor();
139     if (emphasisMarkForeground.isValid() && emphasisMarkForeground != selectionPaintStyle.emphasisMarkColor) {
140         if (!paintSelectedTextOnly)
141             paintSelectedTextSeparately = true;
142         selectionPaintStyle.emphasisMarkColor = emphasisMarkForeground;
143     }
144
145     if (auto* pseudoStyle = renderer.getCachedPseudoStyle(SELECTION)) {
146         const ShadowData* shadow = paintInfo.forceTextColor() ? nullptr : pseudoStyle->textShadow();
147         if (shadow != selectionShadow) {
148             if (!paintSelectedTextOnly)
149                 paintSelectedTextSeparately = true;
150             selectionShadow = shadow;
151         }
152
153         auto viewportSize = renderer.frame().view() ? renderer.frame().view()->size() : IntSize();
154         float strokeWidth = pseudoStyle->computedStrokeWidth(viewportSize);
155         if (strokeWidth != selectionPaintStyle.strokeWidth) {
156             if (!paintSelectedTextOnly)
157                 paintSelectedTextSeparately = true;
158             selectionPaintStyle.strokeWidth = strokeWidth;
159         }
160
161         Color stroke = paintInfo.forceTextColor() ? paintInfo.forcedTextColor() : pseudoStyle->visitedDependentColor(pseudoStyle->hasExplicitlySetStrokeColor() ? CSSPropertyStrokeColor : CSSPropertyWebkitTextStrokeColor);
162         if (stroke != selectionPaintStyle.strokeColor) {
163             if (!paintSelectedTextOnly)
164                 paintSelectedTextSeparately = true;
165             selectionPaintStyle.strokeColor = stroke;
166         }
167     }
168 #else
169     UNUSED_PARAM(renderer);
170     UNUSED_PARAM(lineStyle);
171     UNUSED_PARAM(paintInfo);
172 #endif
173     return selectionPaintStyle;
174 }
175
176 void updateGraphicsContext(GraphicsContext& context, const TextPaintStyle& paintStyle, FillColorType fillColorType)
177 {
178     TextDrawingModeFlags mode = context.textDrawingMode();
179     TextDrawingModeFlags newMode = mode;
180 #if ENABLE(LETTERPRESS)
181     if (paintStyle.useLetterpressEffect)
182         newMode |= TextModeLetterpress;
183     else
184         newMode &= ~TextModeLetterpress;
185 #endif
186     if (paintStyle.strokeWidth > 0)
187         newMode |= TextModeStroke;
188     if (mode != newMode) {
189         context.setTextDrawingMode(newMode);
190         mode = newMode;
191     }
192
193     Color fillColor = fillColorType == UseEmphasisMarkColor ? paintStyle.emphasisMarkColor : paintStyle.fillColor;
194     if (mode & TextModeFill && (fillColor != context.fillColor()))
195         context.setFillColor(fillColor);
196
197     if (mode & TextModeStroke) {
198         if (paintStyle.strokeColor != context.strokeColor())
199             context.setStrokeColor(paintStyle.strokeColor);
200         if (paintStyle.strokeWidth != context.strokeThickness())
201             context.setStrokeThickness(paintStyle.strokeWidth);
202         context.setLineJoin(paintStyle.lineJoin);
203         context.setLineCap(paintStyle.lineCap);
204         if (paintStyle.lineJoin == MiterJoin)
205             context.setMiterLimit(paintStyle.miterLimit);
206     }
207 }
208
209 }