FontFamily.h revision 4d4e6bc8118d15542f1f2a9218f0f7a91a29474f
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
22#include <utils/TypeHelpers.h>
23
24#include <minikin/MinikinRefCounted.h>
25
26namespace android {
27
28// FontStyle represents all style information needed to select an actual font
29// from a collection. The implementation is packed into a single 32-bit word
30// so it can be efficiently copied, embedded in other objects, etc.
31class FontStyle {
32public:
33    FontStyle(int weight = 4, bool italic = false) {
34        bits = (weight & kWeightMask) | (italic ? kItalicMask : 0);
35    }
36    int getWeight() const { return bits & kWeightMask; }
37    bool getItalic() const { return (bits & kItalicMask) != 0; }
38    bool operator==(const FontStyle other) const { return bits == other.bits; }
39    // TODO: language, variant
40
41    hash_t hash() const { return bits; }
42private:
43    static const int kWeightMask = 0xf;
44    static const int kItalicMask = 16;
45    uint32_t bits;
46};
47
48inline hash_t hash_type(const FontStyle &style) {
49    return style.hash();
50}
51
52class FontFamily : public MinikinRefCounted {
53public:
54    ~FontFamily();
55
56    // Add font to family, extracting style information from the font
57    bool addFont(MinikinFont* typeface);
58
59    void addFont(MinikinFont* typeface, FontStyle style);
60    MinikinFont* getClosestMatch(FontStyle style) const;
61
62    // API's for enumerating the fonts in a family. These don't guarantee any particular order
63    size_t getNumFonts() const;
64    MinikinFont* getFont(size_t index) const;
65    FontStyle getStyle(size_t index) const;
66private:
67    void addFontLocked(MinikinFont* typeface, FontStyle style);
68
69    class Font {
70    public:
71        Font(MinikinFont* typeface, FontStyle style) :
72            typeface(typeface), style(style) { }
73        MinikinFont* typeface;
74        FontStyle style;
75    };
76    std::vector<Font> mFonts;
77};
78
79}  // namespace android
80
81#endif  // MINIKIN_FONT_FAMILY_H
82