FontFamily.h 2.3 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
/*
 * 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_FONT_FAMILY_H
#define MINIKIN_FONT_FAMILY_H

#include <vector>

R
Raph Levien 已提交
22 23
#include <minikin/MinikinRefCounted.h>

R
Raph Levien 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
namespace android {

// FontStyle represents all style information needed to select an actual font
// from a collection. The implementation is packed into a single 32-bit word
// so it can be efficiently copied, embedded in other objects, etc.
class FontStyle {
public:
    FontStyle(int weight = 4, bool italic = false) {
        bits = (weight & kWeightMask) | (italic ? kItalicMask : 0);
    }
    int getWeight() { return bits & kWeightMask; }
    bool getItalic() { return (bits & kItalicMask) != 0; }
    bool operator==(const FontStyle other) { return bits == other.bits; }
    // TODO: language, variant
private:
    static const int kWeightMask = 0xf;
    static const int kItalicMask = 16;
    uint32_t bits;
};

R
Raph Levien 已提交
44
class FontFamily : public MinikinRefCounted {
R
Raph Levien 已提交
45
public:
R
Raph Levien 已提交
46 47
    ~FontFamily();

R
Raph Levien 已提交
48
    // Add font to family, extracting style information from the font
R
Raph Levien 已提交
49
    bool addFont(MinikinFont* typeface);
R
Raph Levien 已提交
50

R
Raph Levien 已提交
51 52
    void addFont(MinikinFont* typeface, FontStyle style);
    MinikinFont* getClosestMatch(FontStyle style) const;
R
Raph Levien 已提交
53 54 55

    // API's for enumerating the fonts in a family. These don't guarantee any particular order
    size_t getNumFonts() const;
R
Raph Levien 已提交
56
    MinikinFont* getFont(size_t index) const;
R
Raph Levien 已提交
57 58
    FontStyle getStyle(size_t index) const;
private:
59 60
    void addFontLocked(MinikinFont* typeface, FontStyle style);

R
Raph Levien 已提交
61 62
    class Font {
    public:
R
Raph Levien 已提交
63
        Font(MinikinFont* typeface, FontStyle style) :
R
Raph Levien 已提交
64
            typeface(typeface), style(style) { }
R
Raph Levien 已提交
65
        MinikinFont* typeface;
R
Raph Levien 已提交
66 67 68 69 70 71 72 73
        FontStyle style;
    };
    std::vector<Font> mFonts;
};

}  // namespace android

#endif  // MINIKIN_FONT_FAMILY_H