1 | /* |
2 | * (C) 1999 Lars Knoll (knoll@kde.org) |
3 | * (C) 2000 Dirk Mueller (mueller@kde.org) |
4 | * Copyright (C) 2004-2017 Apple Inc. All rights reserved. |
5 | * |
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. |
10 | * |
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. |
15 | * |
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. |
20 | * |
21 | */ |
22 | |
23 | #include "config.h" |
24 | #include "TextDecorationPainter.h" |
25 | |
26 | #include "FilterOperations.h" |
27 | #include "FontCascade.h" |
28 | #include "GraphicsContext.h" |
29 | #include "HTMLAnchorElement.h" |
30 | #include "HTMLFontElement.h" |
31 | #include "InlineTextBoxStyle.h" |
32 | #include "RenderBlock.h" |
33 | #include "RenderStyle.h" |
34 | #include "RenderText.h" |
35 | #include "ShadowData.h" |
36 | #include "TextRun.h" |
37 | |
38 | namespace WebCore { |
39 | |
40 | static void adjustStepToDecorationLength(float& step, float& controlPointDistance, float length) |
41 | { |
42 | ASSERT(step > 0); |
43 | |
44 | if (length <= 0) |
45 | return; |
46 | |
47 | unsigned stepCount = static_cast<unsigned>(length / step); |
48 | |
49 | // Each Bezier curve starts at the same pixel that the previous one |
50 | // ended. We need to subtract (stepCount - 1) pixels when calculating the |
51 | // length covered to account for that. |
52 | float uncoveredLength = length - (stepCount * step - (stepCount - 1)); |
53 | float adjustment = uncoveredLength / stepCount; |
54 | step += adjustment; |
55 | controlPointDistance += adjustment; |
56 | } |
57 | |
58 | /* |
59 | * Draw one cubic Bezier curve and repeat the same pattern long the the decoration's axis. |
60 | * The start point (p1), controlPoint1, controlPoint2 and end point (p2) of the Bezier curve |
61 | * form a diamond shape: |
62 | * |
63 | * step |
64 | * |-----------| |
65 | * |
66 | * controlPoint1 |
67 | * + |
68 | * |
69 | * |
70 | * . . |
71 | * . . |
72 | * . . |
73 | * (x1, y1) p1 + . + p2 (x2, y2) - <--- Decoration's axis |
74 | * . . | |
75 | * . . | |
76 | * . . | controlPointDistance |
77 | * | |
78 | * | |
79 | * + - |
80 | * controlPoint2 |
81 | * |
82 | * |-----------| |
83 | * step |
84 | */ |
85 | static void strokeWavyTextDecoration(GraphicsContext& context, const FloatRect& rect, float fontSize) |
86 | { |
87 | FloatPoint p1 = rect.minXMinYCorner(); |
88 | FloatPoint p2 = rect.maxXMinYCorner(); |
89 | context.adjustLineToPixelBoundaries(p1, p2, rect.height(), context.strokeStyle()); |
90 | |
91 | Path path; |
92 | path.moveTo(p1); |
93 | |
94 | auto wavyStrokeParameters = getWavyStrokeParameters(fontSize); |
95 | |
96 | ASSERT(p1.y() == p2.y()); |
97 | |
98 | float yAxis = p1.y(); |
99 | float x1 = std::min(p1.x(), p2.x()); |
100 | float x2 = std::max(p1.x(), p2.x()); |
101 | |
102 | adjustStepToDecorationLength(wavyStrokeParameters.step, wavyStrokeParameters.controlPointDistance, x2 - x1); |
103 | FloatPoint controlPoint1(0, yAxis + wavyStrokeParameters.controlPointDistance); |
104 | FloatPoint controlPoint2(0, yAxis - wavyStrokeParameters.controlPointDistance); |
105 | |
106 | for (float x = x1; x + 2 * wavyStrokeParameters.step <= x2;) { |
107 | controlPoint1.setX(x + wavyStrokeParameters.step); |
108 | controlPoint2.setX(x + wavyStrokeParameters.step); |
109 | x += 2 * wavyStrokeParameters.step; |
110 | path.addBezierCurveTo(controlPoint1, controlPoint2, FloatPoint(x, yAxis)); |
111 | } |
112 | |
113 | context.setShouldAntialias(true); |
114 | auto strokeThickness = context.strokeThickness(); |
115 | context.setStrokeThickness(rect.height()); |
116 | context.strokePath(path); |
117 | context.setStrokeThickness(strokeThickness); |
118 | } |
119 | |
120 | static bool compareTuples(std::pair<float, float> l, std::pair<float, float> r) |
121 | { |
122 | return l.first < r.first; |
123 | } |
124 | |
125 | static DashArray translateIntersectionPointsToSkipInkBoundaries(const DashArray& intersections, float dilationAmount, float totalWidth) |
126 | { |
127 | ASSERT(!(intersections.size() % 2)); |
128 | |
129 | // Step 1: Make pairs so we can sort based on range starting-point. We dilate the ranges in this step as well. |
130 | Vector<std::pair<float, float>> tuples; |
131 | for (auto i = intersections.begin(); i != intersections.end(); i++, i++) |
132 | tuples.append(std::make_pair(*i - dilationAmount, *(i + 1) + dilationAmount)); |
133 | std::sort(tuples.begin(), tuples.end(), &compareTuples); |
134 | |
135 | // Step 2: Deal with intersecting ranges. |
136 | Vector<std::pair<float, float>> intermediateTuples; |
137 | if (tuples.size() >= 2) { |
138 | intermediateTuples.append(*tuples.begin()); |
139 | for (auto i = tuples.begin() + 1; i != tuples.end(); i++) { |
140 | float& firstEnd = intermediateTuples.last().second; |
141 | float secondStart = i->first; |
142 | float secondEnd = i->second; |
143 | if (secondStart <= firstEnd && secondEnd <= firstEnd) { |
144 | // Ignore this range completely |
145 | } else if (secondStart <= firstEnd) |
146 | firstEnd = secondEnd; |
147 | else |
148 | intermediateTuples.append(*i); |
149 | } |
150 | } else |
151 | intermediateTuples = tuples; |
152 | |
153 | // Step 3: Output the space between the ranges, but only if the space warrants an underline. |
154 | float previous = 0; |
155 | DashArray result; |
156 | for (const auto& tuple : intermediateTuples) { |
157 | if (tuple.first - previous > dilationAmount) { |
158 | result.append(previous); |
159 | result.append(tuple.first); |
160 | } |
161 | previous = tuple.second; |
162 | } |
163 | if (totalWidth - previous > dilationAmount) { |
164 | result.append(previous); |
165 | result.append(totalWidth); |
166 | } |
167 | |
168 | return result; |
169 | } |
170 | |
171 | static StrokeStyle textDecorationStyleToStrokeStyle(TextDecorationStyle decorationStyle) |
172 | { |
173 | StrokeStyle strokeStyle = SolidStroke; |
174 | switch (decorationStyle) { |
175 | case TextDecorationStyle::Solid: |
176 | strokeStyle = SolidStroke; |
177 | break; |
178 | case TextDecorationStyle::Double: |
179 | strokeStyle = DoubleStroke; |
180 | break; |
181 | case TextDecorationStyle::Dotted: |
182 | strokeStyle = DottedStroke; |
183 | break; |
184 | case TextDecorationStyle::Dashed: |
185 | strokeStyle = DashedStroke; |
186 | break; |
187 | case TextDecorationStyle::Wavy: |
188 | strokeStyle = WavyStroke; |
189 | break; |
190 | } |
191 | |
192 | return strokeStyle; |
193 | } |
194 | |
195 | bool TextDecorationPainter::Styles::operator==(const Styles& other) const |
196 | { |
197 | return underlineColor == other.underlineColor && overlineColor == other.overlineColor && linethroughColor == other.linethroughColor |
198 | && underlineStyle == other.underlineStyle && overlineStyle == other.overlineStyle && linethroughStyle == other.linethroughStyle; |
199 | } |
200 | |
201 | TextDecorationPainter::TextDecorationPainter(GraphicsContext& context, OptionSet<TextDecoration> decorations, const RenderText& renderer, bool isFirstLine, const FontCascade& font, Optional<Styles> styles) |
202 | : m_context { context } |
203 | , m_decorations { decorations } |
204 | , m_wavyOffset { wavyOffsetFromDecoration() } |
205 | , m_isPrinting { renderer.document().printing() } |
206 | , m_font { font } |
207 | , m_styles { styles ? *WTFMove(styles) : stylesForRenderer(renderer, decorations, isFirstLine, PseudoId::None) } |
208 | , m_lineStyle { isFirstLine ? renderer.firstLineStyle() : renderer.style() } |
209 | { |
210 | } |
211 | |
212 | void TextDecorationPainter::paintTextDecoration(const TextRun& textRun, const FloatPoint& textOrigin, const FloatPoint& boxOrigin) |
213 | { |
214 | const auto& fontMetrics = m_lineStyle.fontMetrics(); |
215 | float textDecorationThickness = m_lineStyle.textDecorationThickness().resolve(m_lineStyle.computedFontSize(), fontMetrics); |
216 | FloatPoint localOrigin = boxOrigin; |
217 | |
218 | auto paintDecoration = [&] (TextDecoration decoration, TextDecorationStyle style, const Color& color, const FloatRect& rect) { |
219 | m_context.setStrokeColor(color); |
220 | |
221 | auto strokeStyle = textDecorationStyleToStrokeStyle(style); |
222 | |
223 | if (style == TextDecorationStyle::Wavy) |
224 | strokeWavyTextDecoration(m_context, rect, m_lineStyle.computedFontPixelSize()); |
225 | else if (decoration == TextDecoration::Underline || decoration == TextDecoration::Overline) { |
226 | if ((m_lineStyle.textDecorationSkip() == TextDecorationSkip::Ink || m_lineStyle.textDecorationSkip() == TextDecorationSkip::Auto) && m_isHorizontal) { |
227 | if (!m_context.paintingDisabled()) { |
228 | FloatRect underlineBoundingBox = m_context.computeUnderlineBoundsForText(rect, m_isPrinting); |
229 | DashArray intersections = m_font.dashesForIntersectionsWithRect(textRun, textOrigin, underlineBoundingBox); |
230 | DashArray boundaries = translateIntersectionPointsToSkipInkBoundaries(intersections, underlineBoundingBox.height(), rect.width()); |
231 | ASSERT(!(boundaries.size() % 2)); |
232 | // We don't use underlineBoundingBox here because drawLinesForText() will run computeUnderlineBoundsForText() internally. |
233 | m_context.drawLinesForText(rect.location(), rect.height(), boundaries, m_isPrinting, style == TextDecorationStyle::Double, strokeStyle); |
234 | } |
235 | } else { |
236 | // FIXME: Need to support text-decoration-skip: none. |
237 | m_context.drawLineForText(rect, m_isPrinting, style == TextDecorationStyle::Double, strokeStyle); |
238 | } |
239 | } else { |
240 | ASSERT(decoration == TextDecoration::LineThrough); |
241 | m_context.drawLineForText(rect, m_isPrinting, style == TextDecorationStyle::Double, strokeStyle); |
242 | } |
243 | }; |
244 | |
245 | bool areLinesOpaque = !m_isPrinting && (!m_decorations.contains(TextDecoration::Underline) || m_styles.underlineColor.isOpaque()) |
246 | && (!m_decorations.contains(TextDecoration::Overline) || m_styles.overlineColor.isOpaque()) |
247 | && (!m_decorations.contains(TextDecoration::LineThrough) || m_styles.linethroughColor.isOpaque()); |
248 | |
249 | int = 0; |
250 | bool clipping = !areLinesOpaque && m_shadow && m_shadow->next(); |
251 | if (clipping) { |
252 | FloatRect clipRect(localOrigin, FloatSize(m_width, fontMetrics.ascent() + 2)); |
253 | for (const ShadowData* shadow = m_shadow; shadow; shadow = shadow->next()) { |
254 | int shadowExtent = shadow->paintingExtent(); |
255 | FloatRect shadowRect(localOrigin, FloatSize(m_width, fontMetrics.ascent() + 2)); |
256 | shadowRect.inflate(shadowExtent); |
257 | int shadowX = m_isHorizontal ? shadow->x() : shadow->y(); |
258 | int shadowY = m_isHorizontal ? shadow->y() : -shadow->x(); |
259 | shadowRect.move(shadowX, shadowY); |
260 | clipRect.unite(shadowRect); |
261 | extraOffset = std::max(extraOffset, std::max(0, shadowY) + shadowExtent); |
262 | } |
263 | m_context.save(); |
264 | m_context.clip(clipRect); |
265 | extraOffset += fontMetrics.ascent() + 2; |
266 | localOrigin.move(0, extraOffset); |
267 | } |
268 | |
269 | const ShadowData* shadow = m_shadow; |
270 | do { |
271 | if (shadow) { |
272 | if (!shadow->next()) { |
273 | // The last set of lines paints normally inside the clip. |
274 | localOrigin.move(0, -extraOffset); |
275 | extraOffset = 0; |
276 | } |
277 | int shadowX = m_isHorizontal ? shadow->x() : shadow->y(); |
278 | int shadowY = m_isHorizontal ? shadow->y() : -shadow->x(); |
279 | |
280 | Color shadowColor = shadow->color(); |
281 | if (m_shadowColorFilter) |
282 | m_shadowColorFilter->transformColor(shadowColor); |
283 | m_context.setShadow(FloatSize(shadowX, shadowY - extraOffset), shadow->radius(), shadowColor); |
284 | shadow = shadow->next(); |
285 | } |
286 | |
287 | // These decorations should match the visual overflows computed in visualOverflowForDecorations(). |
288 | if (m_decorations.contains(TextDecoration::Underline)) { |
289 | float textDecorationBaseFontSize = 16; |
290 | auto defaultGap = m_lineStyle.computedFontSize() / textDecorationBaseFontSize; |
291 | float offset = computeUnderlineOffset(m_lineStyle.textUnderlinePosition(), m_lineStyle.textUnderlineOffset(), m_lineStyle.fontMetrics(), m_inlineTextBox, defaultGap); |
292 | float wavyOffset = m_styles.underlineStyle == TextDecorationStyle::Wavy ? m_wavyOffset : 0; |
293 | FloatRect rect(localOrigin, FloatSize(m_width, textDecorationThickness)); |
294 | rect.move(0, offset + wavyOffset); |
295 | paintDecoration(TextDecoration::Underline, m_styles.underlineStyle, m_styles.underlineColor, rect); |
296 | } |
297 | if (m_decorations.contains(TextDecoration::Overline)) { |
298 | float wavyOffset = m_styles.overlineStyle == TextDecorationStyle::Wavy ? m_wavyOffset : 0; |
299 | FloatRect rect(localOrigin, FloatSize(m_width, textDecorationThickness)); |
300 | float autoTextDecorationThickness = TextDecorationThickness::createWithAuto().resolve(m_lineStyle.computedFontSize(), fontMetrics); |
301 | rect.move(0, autoTextDecorationThickness - textDecorationThickness - wavyOffset); |
302 | paintDecoration(TextDecoration::Overline, m_styles.overlineStyle, m_styles.overlineColor, rect); |
303 | } |
304 | if (m_decorations.contains(TextDecoration::LineThrough)) { |
305 | FloatRect rect(localOrigin, FloatSize(m_width, textDecorationThickness)); |
306 | float autoTextDecorationThickness = TextDecorationThickness::createWithAuto().resolve(m_lineStyle.computedFontSize(), fontMetrics); |
307 | auto center = 2 * fontMetrics.floatAscent() / 3 + autoTextDecorationThickness / 2; |
308 | rect.move(0, center - textDecorationThickness / 2); |
309 | paintDecoration(TextDecoration::LineThrough, m_styles.linethroughStyle, m_styles.linethroughColor, rect); |
310 | } |
311 | } while (shadow); |
312 | |
313 | if (clipping) |
314 | m_context.restore(); |
315 | else if (m_shadow) |
316 | m_context.clearShadow(); |
317 | } |
318 | |
319 | static Color decorationColor(const RenderStyle& style) |
320 | { |
321 | // Check for text decoration color first. |
322 | Color result = style.visitedDependentColorWithColorFilter(CSSPropertyTextDecorationColor); |
323 | if (result.isValid()) |
324 | return result; |
325 | if (style.hasPositiveStrokeWidth()) { |
326 | // Prefer stroke color if possible but not if it's fully transparent. |
327 | result = style.computedStrokeColor(); |
328 | if (result.isVisible()) |
329 | return result; |
330 | } |
331 | |
332 | return style.visitedDependentColorWithColorFilter(CSSPropertyWebkitTextFillColor); |
333 | } |
334 | |
335 | static void collectStylesForRenderer(TextDecorationPainter::Styles& result, const RenderObject& renderer, OptionSet<TextDecoration> remainingDecorations, bool firstLineStyle, PseudoId pseudoId) |
336 | { |
337 | auto = [&] (const RenderStyle& style, OptionSet<TextDecoration> decorations) { |
338 | auto color = decorationColor(style); |
339 | auto decorationStyle = style.textDecorationStyle(); |
340 | |
341 | if (decorations.contains(TextDecoration::Underline)) { |
342 | remainingDecorations.remove(TextDecoration::Underline); |
343 | result.underlineColor = color; |
344 | result.underlineStyle = decorationStyle; |
345 | } |
346 | if (decorations.contains(TextDecoration::Overline)) { |
347 | remainingDecorations.remove(TextDecoration::Overline); |
348 | result.overlineColor = color; |
349 | result.overlineStyle = decorationStyle; |
350 | } |
351 | if (decorations.contains(TextDecoration::LineThrough)) { |
352 | remainingDecorations.remove(TextDecoration::LineThrough); |
353 | result.linethroughColor = color; |
354 | result.linethroughStyle = decorationStyle; |
355 | } |
356 | |
357 | }; |
358 | |
359 | auto styleForRenderer = [&] (const RenderObject& renderer) -> const RenderStyle& { |
360 | if (pseudoId != PseudoId::None && renderer.style().hasPseudoStyle(pseudoId)) { |
361 | if (is<RenderText>(renderer)) |
362 | return *downcast<RenderText>(renderer).getCachedPseudoStyle(pseudoId); |
363 | return *downcast<RenderElement>(renderer).getCachedPseudoStyle(pseudoId); |
364 | } |
365 | return firstLineStyle ? renderer.firstLineStyle() : renderer.style(); |
366 | }; |
367 | |
368 | auto* current = &renderer; |
369 | do { |
370 | const auto& style = styleForRenderer(*current); |
371 | extractDecorations(style, style.textDecoration()); |
372 | |
373 | if (current->isRubyText()) |
374 | return; |
375 | |
376 | current = current->parent(); |
377 | if (current && current->isAnonymousBlock() && downcast<RenderBlock>(*current).continuation()) |
378 | current = downcast<RenderBlock>(*current).continuation(); |
379 | |
380 | if (remainingDecorations.isEmpty()) |
381 | break; |
382 | |
383 | } while (current && !is<HTMLAnchorElement>(current->node()) && !is<HTMLFontElement>(current->node())); |
384 | |
385 | // If we bailed out, use the element we bailed out at (typically a <font> or <a> element). |
386 | if (!remainingDecorations.isEmpty() && current) |
387 | extractDecorations(styleForRenderer(*current), remainingDecorations); |
388 | } |
389 | |
390 | auto TextDecorationPainter::stylesForRenderer(const RenderObject& renderer, OptionSet<TextDecoration> requestedDecorations, bool firstLineStyle, PseudoId pseudoId) -> Styles |
391 | { |
392 | Styles result; |
393 | collectStylesForRenderer(result, renderer, requestedDecorations, false, pseudoId); |
394 | if (firstLineStyle) |
395 | collectStylesForRenderer(result, renderer, requestedDecorations, true, pseudoId); |
396 | return result; |
397 | } |
398 | |
399 | } // namespace WebCore |
400 | |