FontFamily.h revision 9cc9bbe1461f359f0b27c5e7645c17dda001ab1d
1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef MINIKIN_FONT_FAMILY_H
18#define MINIKIN_FONT_FAMILY_H
19
20#include <vector>
21
22namespace android {
23
24// FontStyle represents all style information needed to select an actual font
25// from a collection. The implementation is packed into a single 32-bit word
26// so it can be efficiently copied, embedded in other objects, etc.
27class FontStyle {
28public:
29    FontStyle(int weight = 4, bool italic = false) {
30        bits = (weight & kWeightMask) | (italic ? kItalicMask : 0);
31    }
32    int getWeight() { return bits & kWeightMask; }
33    bool getItalic() { return (bits & kItalicMask) != 0; }
34    bool operator==(const FontStyle other) { return bits == other.bits; }
35    // TODO: language, variant
36private:
37    static const int kWeightMask = 0xf;
38    static const int kItalicMask = 16;
39    uint32_t bits;
40};
41
42class FontFamily {
43public:
44    // Add font to family, extracting style information from the font
45    bool addFont(FT_Face typeface);
46
47    void addFont(FT_Face typeface, FontStyle style);
48    FT_Face getClosestMatch(FontStyle style) const;
49
50    // API's for enumerating the fonts in a family. These don't guarantee any particular order
51    size_t getNumFonts() const;
52    FT_Face getFont(size_t index) const;
53    FontStyle getStyle(size_t index) const;
54private:
55    class Font {
56    public:
57        Font(FT_Face typeface, FontStyle style) :
58            typeface(typeface), style(style) { }
59        FT_Face typeface;
60        FontStyle style;
61    };
62    std::vector<Font> mFonts;
63};
64
65}  // namespace android
66
67#endif  // MINIKIN_FONT_FAMILY_H
68