1/*
2 * Copyright (C) 2018 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#pragma once
27
28#include "Color.h"
29#include "FontShadow.h"
30#include <wtf/Forward.h>
31#include <wtf/Optional.h>
32
33namespace WebCore {
34
35class EditingStyle;
36class MutableStyleProperties;
37
38enum class EditAction : uint8_t;
39enum class VerticalAlignChange : uint8_t { Superscript, Baseline, Subscript };
40
41class FontChanges {
42public:
43 template<class Encoder> void encode(Encoder&) const;
44 template<class Decoder> static bool decode(Decoder&, FontChanges&);
45
46 void setFontName(const String& fontName) { m_fontName = fontName; }
47 void setFontFamily(const String& fontFamily) { m_fontFamily = fontFamily; }
48 void setFontSize(double fontSize) { m_fontSize = fontSize; }
49 void setFontSizeDelta(double fontSizeDelta) { m_fontSizeDelta = fontSizeDelta; }
50 void setBold(bool bold) { m_bold = bold; }
51 void setItalic(bool italic) { m_italic = italic; }
52
53 bool isEmpty() const
54 {
55 return !m_fontName && !m_fontFamily && !m_fontSize && !m_fontSizeDelta && !m_bold && !m_italic;
56 }
57
58 WEBCORE_EXPORT Ref<EditingStyle> createEditingStyle() const;
59 Ref<MutableStyleProperties> createStyleProperties() const;
60
61private:
62 const String& platformFontFamilyNameForCSS() const;
63
64 String m_fontName;
65 String m_fontFamily;
66 Optional<double> m_fontSize;
67 Optional<double> m_fontSizeDelta;
68 Optional<bool> m_bold;
69 Optional<bool> m_italic;
70};
71
72class FontAttributeChanges {
73public:
74 template<class Encoder> void encode(Encoder&) const;
75 template<class Decoder> static bool decode(Decoder&, FontAttributeChanges&);
76
77 void setVerticalAlign(VerticalAlignChange align) { m_verticalAlign = align; }
78 void setBackgroundColor(const Color& color) { m_backgroundColor = color; }
79 void setForegroundColor(const Color& color) { m_foregroundColor = color; }
80 void setShadow(const FontShadow& shadow) { m_shadow = shadow; }
81 void setStrikeThrough(bool strikeThrough) { m_strikeThrough = strikeThrough; }
82 void setUnderline(bool underline) { m_underline = underline; }
83 void setFontChanges(const FontChanges& fontChanges) { m_fontChanges = fontChanges; }
84
85 WEBCORE_EXPORT Ref<EditingStyle> createEditingStyle() const;
86 WEBCORE_EXPORT EditAction editAction() const;
87
88private:
89 Optional<VerticalAlignChange> m_verticalAlign;
90 Optional<Color> m_backgroundColor;
91 Optional<Color> m_foregroundColor;
92 Optional<FontShadow> m_shadow;
93 Optional<bool> m_strikeThrough;
94 Optional<bool> m_underline;
95 FontChanges m_fontChanges;
96};
97
98template<class Encoder>
99void FontChanges::encode(Encoder& encoder) const
100{
101 ASSERT(!m_fontSize || !m_fontSizeDelta);
102 encoder << m_fontName << m_fontFamily << m_fontSize << m_fontSizeDelta << m_bold << m_italic;
103}
104
105template<class Decoder>
106bool FontChanges::decode(Decoder& decoder, FontChanges& changes)
107{
108 if (!decoder.decode(changes.m_fontName))
109 return false;
110
111 if (!decoder.decode(changes.m_fontFamily))
112 return false;
113
114 if (!decoder.decode(changes.m_fontSize))
115 return false;
116
117 if (!decoder.decode(changes.m_fontSizeDelta))
118 return false;
119
120 if (!decoder.decode(changes.m_bold))
121 return false;
122
123 if (!decoder.decode(changes.m_italic))
124 return false;
125
126 ASSERT(!changes.m_fontSize || !changes.m_fontSizeDelta);
127 return true;
128}
129
130template<class Encoder>
131void FontAttributeChanges::encode(Encoder& encoder) const
132{
133 encoder << m_verticalAlign << m_backgroundColor << m_foregroundColor << m_shadow << m_strikeThrough << m_underline << m_fontChanges;
134}
135
136template<class Decoder>
137bool FontAttributeChanges::decode(Decoder& decoder, FontAttributeChanges& changes)
138{
139 if (!decoder.decode(changes.m_verticalAlign))
140 return false;
141
142 if (!decoder.decode(changes.m_backgroundColor))
143 return false;
144
145 if (!decoder.decode(changes.m_foregroundColor))
146 return false;
147
148 if (!decoder.decode(changes.m_shadow))
149 return false;
150
151 if (!decoder.decode(changes.m_strikeThrough))
152 return false;
153
154 if (!decoder.decode(changes.m_underline))
155 return false;
156
157 if (!decoder.decode(changes.m_fontChanges))
158 return false;
159
160 return true;
161}
162
163} // namespace WebCore
164
165namespace WTF {
166template<> struct EnumTraits<WebCore::VerticalAlignChange> {
167 using values = EnumValues<
168 WebCore::VerticalAlignChange,
169 WebCore::VerticalAlignChange::Superscript,
170 WebCore::VerticalAlignChange::Baseline,
171 WebCore::VerticalAlignChange::Subscript
172 >;
173};
174}
175