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