FontCollection.h revision 6f9966ea7c1910fd780cf7779cc59701c9b98a2b
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/FontFamily.h>
25
26namespace android {
27
28class FontCollection : public MinikinRefCounted {
29public:
30    explicit FontCollection(const std::vector<FontFamily*>& typefaces);
31
32    ~FontCollection();
33
34    struct Run {
35        FakedFont fakedFont;
36        int start;
37        int end;
38    };
39
40    void itemize(const uint16_t *string, size_t string_length, FontStyle style,
41            std::vector<Run>* result) const;
42
43    // Returns true if there is a glyph for the code point and variation selector pair.
44    // Returns false if no fonts have a glyph for the code point and variation
45    // selector pair, or invalid variation selector is passed.
46    bool hasVariationSelector(uint32_t baseCodepoint, uint32_t variationSelector) const;
47
48    // Get the base font for the given style, useful for font-wide metrics.
49    MinikinFont* baseFont(FontStyle style);
50
51    // Get base font with fakery information (fake bold could affect metrics)
52    FakedFont baseFontFaked(FontStyle style);
53
54    uint32_t getId() const;
55
56    // Calls each managed font family's FontFamily::purgeHbFontCache method.
57    // Caller should acquire a lock before calling the method.
58    void purgeFontFamilyHbFontCache() const;
59private:
60    static const int kLogCharsPerPage = 8;
61    static const int kPageMask = (1 << kLogCharsPerPage) - 1;
62
63    struct Range {
64        size_t start;
65        size_t end;
66    };
67
68    FontFamily* getFamilyForChar(uint32_t ch, uint32_t vs, uint32_t langListId, int variant) const;
69
70    uint32_t calcFamilyScore(uint32_t ch, uint32_t vs, int variant, uint32_t langListId,
71                             FontFamily* fontFamily) const;
72
73    uint32_t calcCoverageScore(uint32_t ch, uint32_t vs, FontFamily* fontFamily) const;
74
75    static uint32_t calcLanguageMatchingScore(uint32_t userLangListId,
76                                              const FontFamily& fontFamily);
77
78    static uint32_t calcVariantMatchingScore(int variant, const FontFamily& fontFamily);
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<FontFamily*> mFamilies;
91
92    // This vector contains pointers into mInstances
93    std::vector<FontFamily*> mFamilyVec;
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