1/*
2 * Copyright (C) 2007 Nicholas Shanks <contact@nickshanks.com>
3 * Copyright (C) 2008, 2013 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "config.h"
31#include "FontCascadeDescription.h"
32
33#include <wtf/text/StringHash.h>
34
35namespace WebCore {
36
37struct SameSizeAsFontCascadeDescription {
38 Vector<void*> vector;
39#if ENABLE(VARIATION_FONTS)
40 Vector<void*> vector2;
41#else
42 char c;
43#endif
44 AtomicString string;
45 int16_t fontSelectionRequest[3];
46 float size;
47 unsigned bitfields1;
48 unsigned bitfields2 : 22;
49 void* array;
50 float size2;
51 unsigned bitfields3 : 10;
52};
53
54COMPILE_ASSERT(sizeof(FontCascadeDescription) == sizeof(SameSizeAsFontCascadeDescription), FontCascadeDescription_should_stay_small);
55
56FontCascadeDescription::FontCascadeDescription()
57 : m_isAbsoluteSize(false)
58 , m_kerning(static_cast<unsigned>(Kerning::Auto))
59 , m_keywordSize(0)
60 , m_fontSmoothing(static_cast<unsigned>(FontSmoothingMode::AutoSmoothing))
61 , m_isSpecifiedFont(false)
62{
63}
64
65#if !USE_PLATFORM_SYSTEM_FALLBACK_LIST
66
67unsigned FontCascadeDescription::effectiveFamilyCount() const
68{
69 return familyCount();
70}
71
72FontFamilySpecification FontCascadeDescription::effectiveFamilyAt(unsigned i) const
73{
74 return familyAt(i);
75}
76
77#endif
78
79FontSelectionValue FontCascadeDescription::lighterWeight(FontSelectionValue weight)
80{
81 if (weight < FontSelectionValue(100))
82 return weight;
83 if (weight < FontSelectionValue(550))
84 return FontSelectionValue(100);
85 if (weight < FontSelectionValue(750))
86 return FontSelectionValue(400);
87 return FontSelectionValue(700);
88}
89
90FontSelectionValue FontCascadeDescription::bolderWeight(FontSelectionValue weight)
91{
92 if (weight < FontSelectionValue(350))
93 return FontSelectionValue(400);
94 if (weight < FontSelectionValue(550))
95 return FontSelectionValue(700);
96 if (weight < FontSelectionValue(900))
97 return FontSelectionValue(900);
98 return weight;
99}
100
101#if ENABLE(TEXT_AUTOSIZING)
102
103bool FontCascadeDescription::familiesEqualForTextAutoSizing(const FontCascadeDescription& other) const
104{
105 unsigned thisFamilyCount = familyCount();
106 unsigned otherFamilyCount = other.familyCount();
107
108 if (thisFamilyCount != otherFamilyCount)
109 return false;
110
111 for (unsigned i = 0; i < thisFamilyCount; ++i) {
112 if (!familyNamesAreEqual(familyAt(i), other.familyAt(i)))
113 return false;
114 }
115
116 return true;
117}
118
119#endif // ENABLE(TEXT_AUTOSIZING)
120
121bool FontCascadeDescription::familyNamesAreEqual(const AtomicString& family1, const AtomicString& family2)
122{
123#if PLATFORM(COCOA)
124 if (family1.startsWith('.'))
125 return family1 == family2;
126#endif
127 return ASCIICaseInsensitiveHash::equal(family1, family2);
128}
129
130unsigned FontCascadeDescription::familyNameHash(const AtomicString& family)
131{
132#if PLATFORM(COCOA)
133 if (family.startsWith('.'))
134 return family.existingHash();
135#endif
136 return ASCIICaseInsensitiveHash::hash(family);
137}
138
139String FontCascadeDescription::foldedFamilyName(const String& family)
140{
141#if PLATFORM(COCOA)
142 if (family.startsWith('.'))
143 return family;
144#endif
145 return family.convertToASCIILowercase();
146}
147
148} // namespace WebCore
149