CssParse.h 2.5 KB
Newer Older
R
Raph Levien 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef MINIKIN_CSS_PARSE_H
#define MINIKIN_CSS_PARSE_H

#include <map>
#include <string>

namespace android {

enum CssTag {
    unknown,
R
Raph Levien 已提交
27
    fontScaleX,
R
Raph Levien 已提交
28
    fontSize,
R
Raph Levien 已提交
29
    fontSkewX,
R
Raph Levien 已提交
30
    fontStyle,
R
Raph Levien 已提交
31 32
    fontWeight,
    cssLang,
B
Behdad Esfahbod 已提交
33
    letterSpacing,
R
Raph Levien 已提交
34
    minikinBidi,
R
Raph Levien 已提交
35 36
    minikinHinting,
    minikinVariant,
R
Raph Levien 已提交
37
    paintFlags,
R
Raph Levien 已提交
38 39 40 41
};

const std::string cssTagNames[] = {
    "unknown",
R
Raph Levien 已提交
42
    "font-scale-x",
R
Raph Levien 已提交
43
    "font-size",
R
Raph Levien 已提交
44
    "font-skew-x",
R
Raph Levien 已提交
45
    "font-style",
R
Raph Levien 已提交
46 47
    "font-weight",
    "lang",
B
Behdad Esfahbod 已提交
48
    "letter-spacing",
R
Raph Levien 已提交
49
    "-minikin-bidi",
R
Raph Levien 已提交
50 51
    "-minikin-hinting",
    "-minikin-variant",
R
Raph Levien 已提交
52
    "-paint-flags",
R
Raph Levien 已提交
53 54 55 56 57 58
};

class CssValue {
public:
    enum Type {
        UNKNOWN,
59
        DOUBLE,
R
Raph Levien 已提交
60
        STRING
R
Raph Levien 已提交
61 62 63 64 65 66 67 68 69
    };
    enum Units {
        SCALAR,
        PERCENT,
        PX,
        EM
    };
    CssValue() : mType(UNKNOWN) { }
    explicit CssValue(double v) :
70
        mType(DOUBLE), doubleValue(v), mUnits(SCALAR) { }
R
Raph Levien 已提交
71
    Type getType() const { return mType; }
72 73 74
    double getDoubleValue() const { return doubleValue; }
    int32_t getIntValue() const { return doubleValue; }
    uint32_t getUintValue() const { return doubleValue; }
R
Raph Levien 已提交
75
    std::string getStringValue() const { return stringValue; }
R
Raph Levien 已提交
76
    std::string toString(CssTag tag) const;
77 78 79
    void setDoubleValue(double v) {
        mType = DOUBLE;
        doubleValue = v;
R
Raph Levien 已提交
80
    }
R
Raph Levien 已提交
81 82 83 84
    void setStringValue(const std::string& v) {
        mType = STRING;
        stringValue = v;
    }
R
Raph Levien 已提交
85 86
private:
    Type mType;
87
    double doubleValue;
R
Raph Levien 已提交
88
    std::string stringValue;
R
Raph Levien 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    Units mUnits;
};

class CssProperties {
public:
    bool parse(const std::string& str);
    bool hasTag(CssTag tag) const;
    CssValue value(CssTag tag) const;

    // primarily for debugging
    std::string toString() const;
private:
    // We'll use STL map for now but can replace it with something
    // more efficient if needed
    std::map<CssTag, CssValue> mMap;
};

}  // namespace android

R
Raph Levien 已提交
108
#endif  // MINIKIN_CSS_PARSE_H