FontCollection.h revision 066e8575af64fb452617ac6005de6ccf6509553b
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    uint32_t getId() const;
61private:
62    static const int kLogCharsPerPage = 8;
63    static const int kPageMask = (1 << kLogCharsPerPage) - 1;
64
65    struct FontInstance {
66        SparseBitSet* mCoverage;
67        FontFamily* mFamily;
68    };
69
70    struct Range {
71        size_t start;
72        size_t end;
73    };
74
75    const FontInstance* getInstanceForChar(uint32_t ch, FontLanguage lang, int variant) const;
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