1 /*
2 * Copyright (C) 2013 Google 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 are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32 #include "platform/fonts/GenericFontFamilySettings.h"
33
34 namespace blink {
35
GenericFontFamilySettings(const GenericFontFamilySettings & other)36 GenericFontFamilySettings::GenericFontFamilySettings(const GenericFontFamilySettings& other)
37 : m_standardFontFamilyMap(other.m_standardFontFamilyMap)
38 , m_serifFontFamilyMap(other.m_serifFontFamilyMap)
39 , m_fixedFontFamilyMap(other.m_fixedFontFamilyMap)
40 , m_sansSerifFontFamilyMap(other.m_sansSerifFontFamilyMap)
41 , m_cursiveFontFamilyMap(other.m_cursiveFontFamilyMap)
42 , m_fantasyFontFamilyMap(other.m_fantasyFontFamilyMap)
43 , m_pictographFontFamilyMap(other.m_pictographFontFamilyMap)
44 {
45 }
46
operator =(const GenericFontFamilySettings & other)47 GenericFontFamilySettings& GenericFontFamilySettings::operator=(const GenericFontFamilySettings& other)
48 {
49 m_standardFontFamilyMap = other.m_standardFontFamilyMap;
50 m_serifFontFamilyMap = other.m_serifFontFamilyMap;
51 m_fixedFontFamilyMap = other.m_fixedFontFamilyMap;
52 m_sansSerifFontFamilyMap = other.m_sansSerifFontFamilyMap;
53 m_cursiveFontFamilyMap = other.m_cursiveFontFamilyMap;
54 m_fantasyFontFamilyMap = other.m_fantasyFontFamilyMap;
55 m_pictographFontFamilyMap = other.m_pictographFontFamilyMap;
56 return *this;
57 }
58
59 // Sets the entry in the font map for the given script. If family is the empty string, removes the entry instead.
setGenericFontFamilyMap(ScriptFontFamilyMap & fontMap,const AtomicString & family,UScriptCode script)60 void GenericFontFamilySettings::setGenericFontFamilyMap(ScriptFontFamilyMap& fontMap, const AtomicString& family, UScriptCode script)
61 {
62 ScriptFontFamilyMap::iterator it = fontMap.find(static_cast<int>(script));
63 if (family.isEmpty()) {
64 if (it == fontMap.end())
65 return;
66 fontMap.remove(it);
67 } else if (it != fontMap.end() && it->value == family) {
68 return;
69 } else {
70 fontMap.set(static_cast<int>(script), family);
71 }
72 }
73
genericFontFamilyForScript(const ScriptFontFamilyMap & fontMap,UScriptCode script) const74 const AtomicString& GenericFontFamilySettings::genericFontFamilyForScript(const ScriptFontFamilyMap& fontMap, UScriptCode script) const
75 {
76 ScriptFontFamilyMap::const_iterator it = fontMap.find(static_cast<int>(script));
77 if (it != fontMap.end())
78 return it->value;
79 if (script != USCRIPT_COMMON)
80 return genericFontFamilyForScript(fontMap, USCRIPT_COMMON);
81 return emptyAtom;
82 }
83
standard(UScriptCode script) const84 const AtomicString& GenericFontFamilySettings::standard(UScriptCode script) const
85 {
86 return genericFontFamilyForScript(m_standardFontFamilyMap, script);
87 }
88
updateStandard(const AtomicString & family,UScriptCode script)89 bool GenericFontFamilySettings::updateStandard(const AtomicString& family, UScriptCode script)
90 {
91 if (family == standard())
92 return false;
93 setGenericFontFamilyMap(m_standardFontFamilyMap, family, script);
94 return true;
95 }
96
fixed(UScriptCode script) const97 const AtomicString& GenericFontFamilySettings::fixed(UScriptCode script) const
98 {
99 return genericFontFamilyForScript(m_fixedFontFamilyMap, script);
100 }
101
updateFixed(const AtomicString & family,UScriptCode script)102 bool GenericFontFamilySettings::updateFixed(const AtomicString& family, UScriptCode script)
103 {
104 if (family == fixed())
105 return false;
106 setGenericFontFamilyMap(m_fixedFontFamilyMap, family, script);
107 return true;
108 }
109
serif(UScriptCode script) const110 const AtomicString& GenericFontFamilySettings::serif(UScriptCode script) const
111 {
112 return genericFontFamilyForScript(m_serifFontFamilyMap, script);
113 }
114
updateSerif(const AtomicString & family,UScriptCode script)115 bool GenericFontFamilySettings::updateSerif(const AtomicString& family, UScriptCode script)
116 {
117 if (family == serif())
118 return false;
119 setGenericFontFamilyMap(m_serifFontFamilyMap, family, script);
120 return true;
121 }
122
sansSerif(UScriptCode script) const123 const AtomicString& GenericFontFamilySettings::sansSerif(UScriptCode script) const
124 {
125 return genericFontFamilyForScript(m_sansSerifFontFamilyMap, script);
126 }
127
updateSansSerif(const AtomicString & family,UScriptCode script)128 bool GenericFontFamilySettings::updateSansSerif(const AtomicString& family, UScriptCode script)
129 {
130 if (family == sansSerif())
131 return false;
132 setGenericFontFamilyMap(m_sansSerifFontFamilyMap, family, script);
133 return true;
134 }
135
cursive(UScriptCode script) const136 const AtomicString& GenericFontFamilySettings::cursive(UScriptCode script) const
137 {
138 return genericFontFamilyForScript(m_cursiveFontFamilyMap, script);
139 }
140
updateCursive(const AtomicString & family,UScriptCode script)141 bool GenericFontFamilySettings::updateCursive(const AtomicString& family, UScriptCode script)
142 {
143 if (family == cursive())
144 return false;
145 setGenericFontFamilyMap(m_cursiveFontFamilyMap, family, script);
146 return true;
147 }
148
fantasy(UScriptCode script) const149 const AtomicString& GenericFontFamilySettings::fantasy(UScriptCode script) const
150 {
151 return genericFontFamilyForScript(m_fantasyFontFamilyMap, script);
152 }
153
updateFantasy(const AtomicString & family,UScriptCode script)154 bool GenericFontFamilySettings::updateFantasy(const AtomicString& family, UScriptCode script)
155 {
156 if (family == fantasy())
157 return false;
158 setGenericFontFamilyMap(m_fantasyFontFamilyMap, family, script);
159 return true;
160 }
161
pictograph(UScriptCode script) const162 const AtomicString& GenericFontFamilySettings::pictograph(UScriptCode script) const
163 {
164 return genericFontFamilyForScript(m_pictographFontFamilyMap, script);
165 }
166
updatePictograph(const AtomicString & family,UScriptCode script)167 bool GenericFontFamilySettings::updatePictograph(const AtomicString& family, UScriptCode script)
168 {
169 if (family == pictograph())
170 return false;
171 setGenericFontFamilyMap(m_pictographFontFamilyMap, family, script);
172 return true;
173 }
174
reset()175 void GenericFontFamilySettings::reset()
176 {
177 m_standardFontFamilyMap.clear();
178 m_serifFontFamilyMap.clear();
179 m_fixedFontFamilyMap.clear();
180 m_sansSerifFontFamilyMap.clear();
181 m_cursiveFontFamilyMap.clear();
182 m_fantasyFontFamilyMap.clear();
183 m_pictographFontFamilyMap.clear();
184 }
185
186 } // namespace blink
187