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#pragma once
24
25#include "AffineTransform.h"
26#include "FloatRect.h"
27#include "GlyphDisplayListCache.h"
28#include "TextFlags.h"
29#include "TextPaintStyle.h"
30#include <wtf/text/AtomString.h>
31
32namespace WebCore {
33
34class FilterOperations;
35class FontCascade;
36class RenderCombineText;
37class ShadowData;
38class TextRun;
39
40struct TextPaintStyle;
41
42enum RotationDirection { Counterclockwise, Clockwise };
43
44static inline AffineTransform rotation(const FloatRect& boxRect, RotationDirection clockwise)
45{
46 return clockwise ? AffineTransform(0, 1, -1, 0, boxRect.x() + boxRect.maxY(), boxRect.maxY() - boxRect.x())
47 : AffineTransform(0, -1, 1, 0, boxRect.x() - boxRect.maxY(), boxRect.x() + boxRect.maxY());
48}
49
50class TextPainter {
51public:
52 TextPainter(GraphicsContext&);
53
54 void setStyle(const TextPaintStyle& textPaintStyle) { m_style = textPaintStyle; }
55 void setShadow(const ShadowData* shadow) { m_shadow = shadow; }
56 void setShadowColorFilter(const FilterOperations* colorFilter) { m_shadowColorFilter = colorFilter; }
57 void setFont(const FontCascade& font) { m_font = &font; }
58 void setIsHorizontal(bool isHorizontal) { m_textBoxIsHorizontal = isHorizontal; }
59 void setEmphasisMark(const AtomString& mark, float offset, const RenderCombineText*);
60
61 void paint(const TextRun&, const FloatRect& boxRect, const FloatPoint& textOrigin);
62 void paintRange(const TextRun&, const FloatRect& boxRect, const FloatPoint& textOrigin, unsigned start, unsigned end);
63
64 template<typename LayoutRun>
65 void setGlyphDisplayListIfNeeded(const LayoutRun& run, const PaintInfo& paintInfo, const FontCascade& font, GraphicsContext& context, const TextRun& textRun)
66 {
67 if (!TextPainter::shouldUseGlyphDisplayList(paintInfo))
68 TextPainter::removeGlyphDisplayList(run);
69 else
70 m_glyphDisplayList = GlyphDisplayListCache<LayoutRun>::singleton().get(run, font, context, textRun);
71 }
72
73 template<typename LayoutRun>
74 static void removeGlyphDisplayList(const LayoutRun& run) { GlyphDisplayListCache<LayoutRun>::singleton().remove(run); }
75
76 static void clearGlyphDisplayLists();
77 static bool shouldUseGlyphDisplayList(const PaintInfo&);
78
79private:
80 void paintTextOrEmphasisMarks(const FontCascade&, const TextRun&, const AtomString& emphasisMark, float emphasisMarkOffset,
81 const FloatPoint& textOrigin, unsigned startOffset, unsigned endOffset);
82 void paintTextWithShadows(const ShadowData*, const FilterOperations*, const FontCascade&, const TextRun&, const FloatRect& boxRect, const FloatPoint& textOrigin,
83 unsigned startOffset, unsigned endOffset, const AtomString& emphasisMark, float emphasisMarkOffset, bool stroked);
84 void paintTextAndEmphasisMarksIfNeeded(const TextRun&, const FloatRect& boxRect, const FloatPoint& textOrigin, unsigned startOffset, unsigned endOffset,
85 const TextPaintStyle&, const ShadowData*, const FilterOperations*);
86
87 GraphicsContext& m_context;
88 const FontCascade* m_font { nullptr };
89 TextPaintStyle m_style;
90 AtomString m_emphasisMark;
91 const ShadowData* m_shadow { nullptr };
92 const FilterOperations* m_shadowColorFilter { nullptr };
93 const RenderCombineText* m_combinedText { nullptr };
94 DisplayList::DisplayList* m_glyphDisplayList { nullptr };
95 float m_emphasisMarkOffset { 0 };
96 bool m_textBoxIsHorizontal { true };
97};
98
99inline void TextPainter::setEmphasisMark(const AtomString& mark, float offset, const RenderCombineText* combinedText)
100{
101 m_emphasisMark = mark;
102 m_emphasisMarkOffset = offset;
103 m_combinedText = combinedText;
104}
105
106class ShadowApplier {
107public:
108 ShadowApplier(GraphicsContext&, const ShadowData*, const FilterOperations* colorFilter, const FloatRect& textRect, bool lastShadowIterationShouldDrawText = true, bool opaque = false, FontOrientation = FontOrientation::Horizontal);
109 FloatSize extraOffset() const { return m_extraOffset; }
110 bool nothingToDraw() const { return m_nothingToDraw; }
111 bool didSaveContext() const { return m_didSaveContext; }
112 ~ShadowApplier();
113
114private:
115 bool isLastShadowIteration();
116 bool shadowIsCompletelyCoveredByText(bool textIsOpaque);
117
118 FloatSize m_extraOffset;
119 GraphicsContext& m_context;
120 const ShadowData* m_shadow;
121 bool m_onlyDrawsShadow : 1;
122 bool m_avoidDrawingShadow : 1;
123 bool m_nothingToDraw : 1;
124 bool m_didSaveContext : 1;
125};
126
127} // namespace WebCore
128