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
56private:
57    static const int kLogCharsPerPage = 8;
58    static const int kPageMask = (1 << kLogCharsPerPage) - 1;
59
60    struct Range {
61        size_t start;
62        size_t end;
63    };
64
65    FontFamily* getFamilyForChar(uint32_t ch, uint32_t vs, uint32_t langListId, int variant) const;
66
67    uint32_t calcFamilyScore(uint32_t ch, uint32_t vs, int variant, uint32_t langListId,
68                             FontFamily* fontFamily) const;
69
70    uint32_t calcCoverageScore(uint32_t ch, uint32_t vs, FontFamily* fontFamily) const;
71
72    static uint32_t calcLanguageMatchingScore(uint32_t userLangListId,
73                                              const FontFamily& fontFamily);
74
75    static uint32_t calcVariantMatchingScore(int variant, const FontFamily& fontFamily);
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    // This vector can't be empty.
88    std::vector<FontFamily*> mFamilies;
89
90    // This vector contains pointers into mInstances
91    // This vector can't be empty.
92    std::vector<FontFamily*> mFamilyVec;
93
94    // This vector has pointers to the font family instance which has cmap 14 subtable.
95    std::vector<FontFamily*> mVSFamilyVec;
96
97    // These are offsets into mInstanceVec, one range per page
98    std::vector<Range> mRanges;
99};
100
101}  // namespace android
102
103#endif  // MINIKIN_FONT_COLLECTION_H
104