58374278c5aa47efbeaab076249a39dc24742acc
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / css / CSSValuePool.cpp
1 /*
2  * Copyright (C) 2011, 2012 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 #include "config.h"
27 #include "core/css/CSSValuePool.h"
28
29 #include "core/css/parser/BisonCSSParser.h"
30 #include "core/css/CSSValueList.h"
31 #include "core/rendering/style/RenderStyle.h"
32
33 namespace WebCore {
34
35 CSSValuePool& cssValuePool()
36 {
37     DEFINE_STATIC_LOCAL(CSSValuePool, pool, ());
38     return pool;
39 }
40
41 CSSValuePool::CSSValuePool()
42     : m_inheritedValue(CSSInheritedValue::create())
43     , m_implicitInitialValue(CSSInitialValue::createImplicit())
44     , m_explicitInitialValue(CSSInitialValue::createExplicit())
45     , m_colorTransparent(CSSPrimitiveValue::createColor(Color::transparent))
46     , m_colorWhite(CSSPrimitiveValue::createColor(Color::white))
47     , m_colorBlack(CSSPrimitiveValue::createColor(Color::black))
48 {
49 }
50
51 PassRefPtr<CSSPrimitiveValue> CSSValuePool::createIdentifierValue(CSSValueID ident)
52 {
53     if (ident <= 0)
54         return CSSPrimitiveValue::createIdentifier(ident);
55
56     if (!m_identifierValueCache[ident])
57         m_identifierValueCache[ident] = CSSPrimitiveValue::createIdentifier(ident);
58     return m_identifierValueCache[ident];
59 }
60
61 PassRefPtr<CSSPrimitiveValue> CSSValuePool::createIdentifierValue(CSSPropertyID ident)
62 {
63     return CSSPrimitiveValue::createIdentifier(ident);
64 }
65
66 PassRefPtr<CSSPrimitiveValue> CSSValuePool::createColorValue(unsigned rgbValue)
67 {
68     // These are the empty and deleted values of the hash table.
69     if (rgbValue == Color::transparent)
70         return m_colorTransparent;
71     if (rgbValue == Color::white)
72         return m_colorWhite;
73     // Just because it is common.
74     if (rgbValue == Color::black)
75         return m_colorBlack;
76
77     // Just wipe out the cache and start rebuilding if it gets too big.
78     const unsigned maximumColorCacheSize = 512;
79     if (m_colorValueCache.size() > maximumColorCacheSize)
80         m_colorValueCache.clear();
81
82     RefPtr<CSSPrimitiveValue> dummyValue;
83     ColorValueCache::AddResult entry = m_colorValueCache.add(rgbValue, dummyValue);
84     if (entry.isNewEntry)
85         entry.iterator->value = CSSPrimitiveValue::createColor(rgbValue);
86     return entry.iterator->value;
87 }
88
89 PassRefPtr<CSSPrimitiveValue> CSSValuePool::createValue(double value, CSSPrimitiveValue::UnitTypes type)
90 {
91     if (value < 0 || value > maximumCacheableIntegerValue)
92         return CSSPrimitiveValue::create(value, type);
93
94     int intValue = static_cast<int>(value);
95     if (value != intValue)
96         return CSSPrimitiveValue::create(value, type);
97
98     RefPtr<CSSPrimitiveValue>* cache;
99     switch (type) {
100     case CSSPrimitiveValue::CSS_PX:
101         cache = m_pixelValueCache;
102         break;
103     case CSSPrimitiveValue::CSS_PERCENTAGE:
104         cache = m_percentValueCache;
105         break;
106     case CSSPrimitiveValue::CSS_NUMBER:
107         cache = m_numberValueCache;
108         break;
109     default:
110         return CSSPrimitiveValue::create(value, type);
111     }
112
113     if (!cache[intValue])
114         cache[intValue] = CSSPrimitiveValue::create(value, type);
115     return cache[intValue];
116 }
117
118 PassRefPtr<CSSPrimitiveValue> CSSValuePool::createValue(const Length& value, const RenderStyle& style)
119 {
120     return CSSPrimitiveValue::create(value, style.effectiveZoom());
121 }
122
123 PassRefPtr<CSSPrimitiveValue> CSSValuePool::createFontFamilyValue(const String& familyName)
124 {
125     RefPtr<CSSPrimitiveValue>& value = m_fontFamilyValueCache.add(familyName, 0).iterator->value;
126     if (!value)
127         value = CSSPrimitiveValue::create(familyName, CSSPrimitiveValue::CSS_STRING);
128     return value;
129 }
130
131 PassRefPtr<CSSValueList> CSSValuePool::createFontFaceValue(const AtomicString& string)
132 {
133     // Just wipe out the cache and start rebuilding if it gets too big.
134     const unsigned maximumFontFaceCacheSize = 128;
135     if (m_fontFaceValueCache.size() > maximumFontFaceCacheSize)
136         m_fontFaceValueCache.clear();
137
138     RefPtr<CSSValueList>& value = m_fontFaceValueCache.add(string, 0).iterator->value;
139     if (!value)
140         value = BisonCSSParser::parseFontFaceValue(string);
141     return value;
142 }
143
144 }