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 <memory>
21#include <unordered_set>
22#include <vector>
23
24#include <minikin/MinikinFont.h>
25#include <minikin/FontFamily.h>
26
27namespace minikin {
28
29class FontCollection {
30public:
31    explicit FontCollection(const std::vector<std::shared_ptr<FontFamily>>& typefaces);
32    explicit FontCollection(std::shared_ptr<FontFamily>&& typeface);
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 base font with fakery information (fake bold could affect metrics)
49    FakedFont baseFontFaked(FontStyle style);
50
51    // Creates new FontCollection based on this collection while applying font variations. Returns
52    // nullptr if none of variations apply to this collection.
53    std::shared_ptr<FontCollection>
54            createCollectionWithVariation(const std::vector<FontVariation>& variations);
55
56    const std::unordered_set<AxisTag>& getSupportedTags() const {
57        return mSupportedAxes;
58    }
59
60    uint32_t getId() const;
61
62private:
63    static const int kLogCharsPerPage = 8;
64    static const int kPageMask = (1 << kLogCharsPerPage) - 1;
65
66    // mFamilyVec holds the indices of the mFamilies and mRanges holds the range of indices of
67    // mFamilyVec. The maximum number of pages is 0x10FF (U+10FFFF >> 8). The maximum number of
68    // the fonts is 0xFF. Thus, technically the maximum length of mFamilyVec is 0x10EE01
69    // (0x10FF * 0xFF). However, in practice, 16-bit integers are enough since most fonts supports
70    // only limited range of code points.
71    struct Range {
72        uint16_t start;
73        uint16_t end;
74    };
75
76    // Initialize the FontCollection.
77    void init(const std::vector<std::shared_ptr<FontFamily>>& typefaces);
78
79    const std::shared_ptr<FontFamily>& getFamilyForChar(uint32_t ch, uint32_t vs,
80            uint32_t langListId, int variant) const;
81
82    uint32_t calcFamilyScore(uint32_t ch, uint32_t vs, int variant, uint32_t langListId,
83            const std::shared_ptr<FontFamily>& fontFamily) const;
84
85    uint32_t calcCoverageScore(uint32_t ch, uint32_t vs,
86            const std::shared_ptr<FontFamily>& fontFamily) const;
87
88    static uint32_t calcLanguageMatchingScore(uint32_t userLangListId,
89                                              const FontFamily& fontFamily);
90
91    static uint32_t calcVariantMatchingScore(int variant, const FontFamily& fontFamily);
92
93    // static for allocating unique id's
94    static uint32_t sNextId;
95
96    // unique id for this font collection (suitable for cache key)
97    uint32_t mId;
98
99    // Highest UTF-32 code point that can be mapped
100    uint32_t mMaxChar;
101
102    // This vector has pointers to the all font family instances in this collection.
103    // This vector can't be empty.
104    std::vector<std::shared_ptr<FontFamily>> mFamilies;
105
106    // Following two vectors are pre-calculated tables for resolving coverage faster.
107    // For example, to iterate over all fonts which support Unicode code point U+XXYYZZ,
108    // iterate font families index from mFamilyVec[mRanges[0xXXYY].start] to
109    // mFamilyVec[mRange[0xXXYY].end] instead of whole mFamilies.
110    // This vector contains indices into mFamilies.
111    // This vector can't be empty.
112    std::vector<Range> mRanges;
113    std::vector<uint8_t> mFamilyVec;
114
115    // This vector has pointers to the font family instances which have cmap 14 subtables.
116    std::vector<std::shared_ptr<FontFamily>> mVSFamilyVec;
117
118    // Set of supported axes in this collection.
119    std::unordered_set<AxisTag> mSupportedAxes;
120};
121
122}  // namespace minikin
123
124#endif  // MINIKIN_FONT_COLLECTION_H
125