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 "FontDescription.h"
32
33namespace WebCore {
34
35struct SameSizeAsFontDescription {
36    void* pointers[2];
37    float sizes[2];
38    // FXIME: Make them fit into one word.
39    uint32_t bitfields;
40    uint32_t bitfields2 : 8;
41};
42
43COMPILE_ASSERT(sizeof(FontDescription) == sizeof(SameSizeAsFontDescription), FontDescription_should_stay_small);
44
45FontWeight FontDescription::lighterWeight(void) const
46{
47    // FIXME: Should actually return the CSS weight corresponding to next lightest
48    // weight of the currently used font family.
49    switch (m_weight) {
50        case FontWeight100:
51        case FontWeight200:
52            return FontWeight100;
53
54        case FontWeight300:
55            return FontWeight200;
56
57        case FontWeight400:
58        case FontWeight500:
59            return FontWeight300;
60
61        case FontWeight600:
62        case FontWeight700:
63            return FontWeight400;
64
65        case FontWeight800:
66            return FontWeight500;
67
68        case FontWeight900:
69            return FontWeight700;
70    }
71    ASSERT_NOT_REACHED();
72    return FontWeightNormal;
73}
74
75FontWeight FontDescription::bolderWeight(void) const
76{
77    // FIXME: Should actually return the CSS weight corresponding to next heaviest
78    // weight of the currently used font family.
79    switch (m_weight) {
80        case FontWeight100:
81        case FontWeight200:
82            return FontWeight300;
83
84        case FontWeight300:
85            return FontWeight400;
86
87        case FontWeight400:
88        case FontWeight500:
89            return FontWeight700;
90
91        case FontWeight600:
92        case FontWeight700:
93            return FontWeight800;
94
95        case FontWeight800:
96        case FontWeight900:
97            return FontWeight900;
98    }
99    ASSERT_NOT_REACHED();
100    return FontWeightNormal;
101}
102
103FontTraitsMask FontDescription::traitsMask() const
104{
105    return static_cast<FontTraitsMask>((m_italic ? FontStyleItalicMask : FontStyleNormalMask)
106            | (m_smallCaps ? FontVariantSmallCapsMask : FontVariantNormalMask)
107            | (FontWeight100Mask << (m_weight - FontWeight100)));
108
109}
110
111FontDescription FontDescription::makeNormalFeatureSettings() const
112{
113    FontDescription normalDescription(*this);
114    normalDescription.setFeatureSettings(0);
115    return normalDescription;
116}
117
118#if ENABLE(IOS_TEXT_AUTOSIZING)
119bool FontDescription::familiesEqualForTextAutoSizing(const FontDescription& other) const
120{
121    unsigned thisFamilyCount = familyCount();
122    unsigned otherFamilyCount = other.familyCount();
123
124    if (thisFamilyCount != otherFamilyCount)
125        return false;
126
127    for (unsigned i = 0; i < thisFamilyCount; ++i) {
128        if (!equalIgnoringCase(familyAt(i), other.familyAt(i)))
129            return false;
130    }
131
132    return true;
133}
134#endif // ENABLE(IOS_TEXT_AUTOSIZING)
135
136} // namespace WebCore
137