1/*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 *
4 *     * Redistributions of source code must retain the above copyright
5 * notice, this list of conditions and the following disclaimer.
6 *     * Redistributions in binary form must reproduce the above
7 * copyright notice, this list of conditions and the following disclaimer
8 * in the documentation and/or other materials provided with the
9 * distribution.
10 *     * Neither the name of Google Inc. nor the names of its
11 * contributors may be used to endorse or promote products derived from
12 * this software without specific prior written permission.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#ifndef StyleBuilderConverter_h
28#define StyleBuilderConverter_h
29
30#include "core/css/CSSValue.h"
31#include "core/css/resolver/StyleResolverState.h"
32#include "core/rendering/RenderView.h"
33#include "core/rendering/style/QuotesData.h"
34#include "core/rendering/style/ShadowList.h"
35#include "core/rendering/style/StyleReflection.h"
36#include "core/svg/SVGLength.h"
37#include "platform/LengthSize.h"
38#include "platform/fonts/FontDescription.h"
39
40namespace blink {
41
42// Note that we assume the parser only allows valid CSSValue types.
43
44class StyleBuilderConverter {
45public:
46    static PassRefPtr<StyleReflection> convertBoxReflect(StyleResolverState&, CSSValue*);
47    static AtomicString convertFragmentIdentifier(StyleResolverState&, CSSValue*);
48    static Color convertColor(StyleResolverState&, CSSValue*, bool forVisitedLink = false);
49    template <typename T> static T convertComputedLength(StyleResolverState&, CSSValue*);
50    static LengthBox convertClip(StyleResolverState&, CSSValue*);
51    template <typename T> static T convertFlags(StyleResolverState&, CSSValue*);
52    static PassRefPtr<FontFeatureSettings> convertFontFeatureSettings(StyleResolverState&, CSSValue*);
53    static FontDescription::Size convertFontSize(StyleResolverState&, CSSValue*);
54    static FontWeight convertFontWeight(StyleResolverState&, CSSValue*);
55    static FontDescription::VariantLigatures convertFontVariantLigatures(StyleResolverState&, CSSValue*);
56    static EGlyphOrientation convertGlyphOrientation(StyleResolverState&, CSSValue*);
57    static GridPosition convertGridPosition(StyleResolverState&, CSSValue*);
58    static GridTrackSize convertGridTrackSize(StyleResolverState&, CSSValue*);
59    template <typename T> static T convertLineWidth(StyleResolverState&, CSSValue*);
60    static Length convertLength(StyleResolverState&, CSSValue*);
61    static Length convertLengthOrAuto(StyleResolverState&, CSSValue*);
62    static Length convertLengthSizing(StyleResolverState&, CSSValue*);
63    static Length convertLengthMaxSizing(StyleResolverState&, CSSValue*);
64    static LengthPoint convertLengthPoint(StyleResolverState&, CSSValue*);
65    static LineBoxContain convertLineBoxContain(StyleResolverState&, CSSValue*);
66    static float convertNumberOrPercentage(StyleResolverState&, CSSValue*);
67    static PassRefPtr<QuotesData> convertQuotes(StyleResolverState&, CSSValue*);
68    static LengthSize convertRadius(StyleResolverState&, CSSValue*);
69    static EPaintOrder convertPaintOrder(StyleResolverState&, CSSValue*);
70    static PassRefPtr<ShadowList> convertShadow(StyleResolverState&, CSSValue*);
71    static float convertSpacing(StyleResolverState&, CSSValue*);
72    template <CSSValueID IdForNone> static AtomicString convertString(StyleResolverState&, CSSValue*);
73    static PassRefPtr<SVGLengthList> convertStrokeDasharray(StyleResolverState&, CSSValue*);
74    static StyleColor convertStyleColor(StyleResolverState&, CSSValue*, bool forVisitedLink = false);
75    static Color convertSVGColor(StyleResolverState&, CSSValue*);
76    static PassRefPtr<SVGLength> convertSVGLength(StyleResolverState&, CSSValue*);
77    static float convertTextStrokeWidth(StyleResolverState&, CSSValue*);
78
79    static bool convertGridTrackList(CSSValue*, Vector<GridTrackSize>&, NamedGridLinesMap&, OrderedNamedGridLines&, StyleResolverState&);
80    static void createImplicitNamedGridLinesFromGridArea(const NamedGridAreaMap&, NamedGridLinesMap&, GridTrackSizingDirection);
81};
82
83template <typename T>
84T StyleBuilderConverter::convertComputedLength(StyleResolverState& state, CSSValue* value)
85{
86    return toCSSPrimitiveValue(value)->computeLength<T>(state.cssToLengthConversionData());
87}
88
89template <typename T>
90T StyleBuilderConverter::convertFlags(StyleResolverState& state, CSSValue* value)
91{
92    T flags = static_cast<T>(0);
93    for (CSSValueListIterator i(value); i.hasMore(); i.advance())
94        flags |= *toCSSPrimitiveValue(i.value());
95    return flags;
96}
97
98template <typename T>
99T StyleBuilderConverter::convertLineWidth(StyleResolverState& state, CSSValue* value)
100{
101    CSSPrimitiveValue* primitiveValue = toCSSPrimitiveValue(value);
102    CSSValueID valueID = primitiveValue->getValueID();
103    if (valueID == CSSValueThin)
104        return 1;
105    if (valueID == CSSValueMedium)
106        return 3;
107    if (valueID == CSSValueThick)
108        return 5;
109    if (valueID == CSSValueInvalid) {
110        // Any original result that was >= 1 should not be allowed to fall below 1.
111        // This keeps border lines from vanishing.
112        T result = primitiveValue->computeLength<T>(state.cssToLengthConversionData());
113        if (state.style()->effectiveZoom() < 1.0f && result < 1.0) {
114            T originalLength = primitiveValue->computeLength<T>(state.cssToLengthConversionData().copyWithAdjustedZoom(1.0));
115            if (originalLength >= 1.0)
116                return 1.0;
117        }
118        return result;
119    }
120    ASSERT_NOT_REACHED();
121    return 0;
122}
123
124template <CSSValueID IdForNone>
125AtomicString StyleBuilderConverter::convertString(StyleResolverState&, CSSValue* value)
126{
127    CSSPrimitiveValue* primitiveValue = toCSSPrimitiveValue(value);
128    if (primitiveValue->getValueID() == IdForNone)
129        return nullAtom;
130    return AtomicString(primitiveValue->getStringValue());
131}
132
133} // namespace blink
134
135#endif
136