~ubuntu-branches/ubuntu/maverick/webkit/maverick

« back to all changes in this revision

Viewing changes to WebCore/html/HTMLFontElement.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Mike Hommey
  • Date: 2007-08-19 15:54:12 UTC
  • Revision ID: james.westby@ubuntu.com-20070819155412-uxxg1h9plpghmtbi
Tags: upstream-0~svn25144
ImportĀ upstreamĀ versionĀ 0~svn25144

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * This file is part of the DOM implementation for KDE.
 
3
 *
 
4
 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
 
5
 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
 
6
 *           (C) 2000 Simon Hausmann <hausmann@kde.org>
 
7
 * Copyright (C) 2003, 2006 Apple Computer, Inc.
 
8
 *
 
9
 * This library is free software; you can redistribute it and/or
 
10
 * modify it under the terms of the GNU Library General Public
 
11
 * License as published by the Free Software Foundation; either
 
12
 * version 2 of the License, or (at your option) any later version.
 
13
 *
 
14
 * This library is distributed in the hope that it will be useful,
 
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
17
 * Library General Public License for more details.
 
18
 *
 
19
 * You should have received a copy of the GNU Library General Public License
 
20
 * along with this library; see the file COPYING.LIB.  If not, write to
 
21
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
22
 * Boston, MA 02111-1307, USA.
 
23
 */
 
24
 
 
25
#include "config.h"
 
26
#include "HTMLFontElement.h"
 
27
 
 
28
#include "CSSPropertyNames.h"
 
29
#include "CSSValueKeywords.h"
 
30
#include "HTMLNames.h"
 
31
 
 
32
using namespace WTF;
 
33
 
 
34
namespace WebCore {
 
35
 
 
36
using namespace HTMLNames;
 
37
 
 
38
HTMLFontElement::HTMLFontElement(Document* doc)
 
39
    : HTMLElement(fontTag, doc)
 
40
{
 
41
}
 
42
 
 
43
HTMLFontElement::~HTMLFontElement()
 
44
{
 
45
}
 
46
 
 
47
// Allows leading spaces.
 
48
// Allows trailing nonnumeric characters.
 
49
// Returns 10 for any size greater than 9.
 
50
static bool parseFontSizeNumber(const String& s, int& size)
 
51
{
 
52
    unsigned pos = 0;
 
53
    
 
54
    // Skip leading spaces.
 
55
    while (DeprecatedChar(s[pos]).isSpace())
 
56
        ++pos;
 
57
    
 
58
    // Skip a plus or minus.
 
59
    bool sawPlus = false;
 
60
    bool sawMinus = false;
 
61
    if (s[pos] == '+') {
 
62
        ++pos;
 
63
        sawPlus = true;
 
64
    } else if (s[pos] == '-') {
 
65
        ++pos;
 
66
        sawMinus = true;
 
67
    }
 
68
    
 
69
    // Parse a single digit.
 
70
    if (!Unicode::isDigit(s[pos]))
 
71
        return false;
 
72
    int num = Unicode::digitValue(s[pos++]);
 
73
    
 
74
    // Check for an additional digit.
 
75
    if (Unicode::isDigit(s[pos]))
 
76
        num = 10;
 
77
    
 
78
    if (sawPlus) {
 
79
        size = num + 3;
 
80
        return true;
 
81
    }
 
82
    
 
83
    // Don't return 0 (which means 3) or a negative number (which means the same as 1).
 
84
    if (sawMinus) {
 
85
        size = num == 1 ? 2 : 1;
 
86
        return true;
 
87
    }
 
88
    
 
89
    size = num;
 
90
    return true;
 
91
}
 
92
 
 
93
bool HTMLFontElement::mapToEntry(const QualifiedName& attrName, MappedAttributeEntry& result) const
 
94
{
 
95
    if (attrName == sizeAttr ||
 
96
        attrName == colorAttr ||
 
97
        attrName == faceAttr) {
 
98
        result = eUniversal;
 
99
        return false;
 
100
    }
 
101
    
 
102
    return HTMLElement::mapToEntry(attrName, result);
 
103
}
 
104
 
 
105
bool HTMLFontElement::cssValueFromFontSizeNumber(const String& s, int& size)
 
106
{
 
107
    int num;
 
108
    if (!parseFontSizeNumber(s, num))
 
109
        return false;
 
110
        
 
111
    switch (num) {
 
112
        case 2: 
 
113
            size = CSS_VAL_SMALL; 
 
114
            break;
 
115
        case 0: // treat 0 the same as 3, because people expect it to be between -1 and +1
 
116
        case 3: 
 
117
            size = CSS_VAL_MEDIUM; 
 
118
            break;
 
119
        case 4: 
 
120
            size = CSS_VAL_LARGE; 
 
121
            break;
 
122
        case 5: 
 
123
            size = CSS_VAL_X_LARGE; 
 
124
            break;
 
125
        case 6: 
 
126
            size = CSS_VAL_XX_LARGE; 
 
127
            break;
 
128
        default:
 
129
            if (num > 6)
 
130
                size = CSS_VAL__WEBKIT_XXX_LARGE;
 
131
            else
 
132
                size = CSS_VAL_X_SMALL;
 
133
    }
 
134
    return true;
 
135
}
 
136
 
 
137
void HTMLFontElement::parseMappedAttribute(MappedAttribute *attr)
 
138
{
 
139
    if (attr->name() == sizeAttr) {
 
140
        int size;
 
141
        if (cssValueFromFontSizeNumber(attr->value(), size))
 
142
            addCSSProperty(attr, CSS_PROP_FONT_SIZE, size);
 
143
    } else if (attr->name() == colorAttr) {
 
144
        addCSSColor(attr, CSS_PROP_COLOR, attr->value());
 
145
    } else if (attr->name() == faceAttr) {
 
146
        addCSSProperty(attr, CSS_PROP_FONT_FAMILY, attr->value());
 
147
    } else
 
148
        HTMLElement::parseMappedAttribute(attr);
 
149
}
 
150
 
 
151
String HTMLFontElement::color() const
 
152
{
 
153
    return getAttribute(colorAttr);
 
154
}
 
155
 
 
156
void HTMLFontElement::setColor(const String& value)
 
157
{
 
158
    setAttribute(colorAttr, value);
 
159
}
 
160
 
 
161
String HTMLFontElement::face() const
 
162
{
 
163
    return getAttribute(faceAttr);
 
164
}
 
165
 
 
166
void HTMLFontElement::setFace(const String& value)
 
167
{
 
168
    setAttribute(faceAttr, value);
 
169
}
 
170
 
 
171
String HTMLFontElement::size() const
 
172
{
 
173
    return getAttribute(sizeAttr);
 
174
}
 
175
 
 
176
void HTMLFontElement::setSize(const String& value)
 
177
{
 
178
    setAttribute(sizeAttr, value);
 
179
}
 
180
 
 
181
}