summaryrefslogtreecommitdiffstats
path: root/include/minikin/CssParse.h
blob: 259b933a54adf3d128008e824928b91ccc962f39 (plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * 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,
    fontScaleX,
    fontSize,
    fontSkewX,
    fontStyle,
    fontWeight,
    cssLang,
    letterSpacing,
    minikinBidi,
    minikinHinting,
    minikinVariant,
    paintFlags,
};

const std::string cssTagNames[] = {
    "unknown",
    "font-scale-x",
    "font-size",
    "font-skew-x",
    "font-style",
    "font-weight",
    "lang",
    "letter-spacing",
    "-minikin-bidi",
    "-minikin-hinting",
    "-minikin-variant",
    "-paint-flags",
};

class CssValue {
public:
    enum Type {
        UNKNOWN,
        DOUBLE,
        STRING
    };
    enum Units {
        SCALAR,
        PERCENT,
        PX,
        EM
    };
    CssValue() : mType(UNKNOWN) { }
    explicit CssValue(double v) :
        mType(DOUBLE), doubleValue(v), mUnits(SCALAR) { }
    Type getType() const { return mType; }
    double getDoubleValue() const { return doubleValue; }
    int32_t getIntValue() const { return doubleValue; }
    uint32_t getUintValue() const { return doubleValue; }
    std::string getStringValue() const { return stringValue; }
    std::string toString(CssTag tag) const;
    void setDoubleValue(double v) {
        mType = DOUBLE;
        doubleValue = v;
    }
    void setStringValue(const std::string& v) {
        mType = STRING;
        stringValue = v;
    }
private:
    Type mType;
    double doubleValue;
    std::string stringValue;
    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

#endif  // MINIKIN_CSS_PARSE_H