FontCollection.h revision 89566f0ada1cafe673efa064cde38467990235d4
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_COLLECTION_H 18#define MINIKIN_FONT_COLLECTION_H 19 20#include <vector> 21 22#include <minikin/MinikinRefCounted.h> 23#include <minikin/MinikinFont.h> 24#include <minikin/SparseBitSet.h> 25#include <minikin/FontFamily.h> 26 27namespace android { 28 29class FontCollection : public MinikinRefCounted { 30public: 31 explicit FontCollection(const std::vector<FontFamily*>& typefaces); 32 33 ~FontCollection(); 34 35 class Run { 36 public: 37 // Do copy constructor, assignment, destructor so it can be used in vectors 38 Run() : font(NULL) { } 39 Run(const Run& other): font(other.font), start(other.start), end(other.end) { 40 if (font) font->RefLocked(); 41 } 42 Run& operator=(const Run& other) { 43 if (other.font) other.font->RefLocked(); 44 if (font) font->UnrefLocked(); 45 font = other.font; 46 start = other.start; 47 end = other.end; 48 return *this; 49 } 50 ~Run() { if (font) font->UnrefLocked(); } 51 52 MinikinFont* font; 53 int start; 54 int end; 55 }; 56 57 void itemize(const uint16_t *string, size_t string_length, FontStyle style, 58 std::vector<Run>* result) const; 59 60 // Get the base font for the given style, useful for font-wide metrics. 61 MinikinFont* baseFont(FontStyle style); 62 63 uint32_t getId() const; 64private: 65 static const int kLogCharsPerPage = 8; 66 static const int kPageMask = (1 << kLogCharsPerPage) - 1; 67 68 struct FontInstance { 69 SparseBitSet* mCoverage; 70 FontFamily* mFamily; 71 }; 72 73 struct Range { 74 size_t start; 75 size_t end; 76 }; 77 78 const FontInstance* getInstanceForChar(uint32_t ch, FontLanguage lang, int variant) const; 79 80 // static for allocating unique id's 81 static uint32_t sNextId; 82 83 // unique id for this font collection (suitable for cache key) 84 uint32_t mId; 85 86 // Highest UTF-32 code point that can be mapped 87 uint32_t mMaxChar; 88 89 // This vector has ownership of the bitsets and typeface objects. 90 std::vector<FontInstance> mInstances; 91 92 // This vector contains pointers into mInstances 93 std::vector<const FontInstance*> mInstanceVec; 94 95 // These are offsets into mInstanceVec, one range per page 96 std::vector<Range> mRanges; 97}; 98 99} // namespace android 100 101#endif // MINIKIN_FONT_COLLECTION_H 102