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