FontCollection.h 4.6 KB
Newer Older
R
Raph Levien 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef MINIKIN_FONT_COLLECTION_H
#define MINIKIN_FONT_COLLECTION_H

20
#include <memory>
21
#include <unordered_set>
22
#include <vector>
R
Raph Levien 已提交
23

R
Raph Levien 已提交
24 25
#include <minikin/MinikinFont.h>
#include <minikin/FontFamily.h>
R
Raph Levien 已提交
26

S
Seigo Nonaka 已提交
27
namespace minikin {
R
Raph Levien 已提交
28

29
class FontCollection {
R
Raph Levien 已提交
30
public:
31 32
    explicit FontCollection(const std::vector<std::shared_ptr<FontFamily>>& typefaces);
    explicit FontCollection(std::shared_ptr<FontFamily>&& typeface);
R
Raph Levien 已提交
33

R
Raph Levien 已提交
34 35
    struct Run {
        FakedFont fakedFont;
R
Raph Levien 已提交
36 37 38
        int start;
        int end;
    };
39

R
Raph Levien 已提交
40 41
    void itemize(const uint16_t *string, size_t string_length, FontStyle style,
            std::vector<Run>* result) const;
42

43 44 45 46 47
    // Returns true if there is a glyph for the code point and variation selector pair.
    // Returns false if no fonts have a glyph for the code point and variation
    // selector pair, or invalid variation selector is passed.
    bool hasVariationSelector(uint32_t baseCodepoint, uint32_t variationSelector) const;

R
Raph Levien 已提交
48 49
    // Get base font with fakery information (fake bold could affect metrics)
    FakedFont baseFontFaked(FontStyle style);
50

51 52
    // Creates new FontCollection based on this collection while applying font variations. Returns
    // nullptr if none of variations apply to this collection.
53 54
    std::shared_ptr<FontCollection>
            createCollectionWithVariation(const std::vector<FontVariation>& variations);
55

56 57 58 59
    const std::unordered_set<AxisTag>& getSupportedTags() const {
        return mSupportedAxes;
    }

60
    uint32_t getId() const;
61

62
private:
R
Raph Levien 已提交
63 64 65
    static const int kLogCharsPerPage = 8;
    static const int kPageMask = (1 << kLogCharsPerPage) - 1;

66 67 68 69 70
    // mFamilyVec holds the indices of the mFamilies and mRanges holds the range of indices of
    // mFamilyVec. The maximum number of pages is 0x10FF (U+10FFFF >> 8). The maximum number of
    // the fonts is 0xFF. Thus, technically the maximum length of mFamilyVec is 0x10EE01
    // (0x10FF * 0xFF). However, in practice, 16-bit integers are enough since most fonts supports
    // only limited range of code points.
R
Raph Levien 已提交
71
    struct Range {
72 73
        uint16_t start;
        uint16_t end;
R
Raph Levien 已提交
74 75
    };

76 77 78 79
    // Initialize the FontCollection.
    void init(const std::vector<std::shared_ptr<FontFamily>>& typefaces);

    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 86
    uint32_t calcCoverageScore(uint32_t ch, uint32_t vs,
            const std::shared_ptr<FontFamily>& fontFamily) const;
87

88
    static uint32_t calcLanguageMatchingScore(uint32_t userLangListId,
89 90 91 92
                                              const FontFamily& fontFamily);

    static uint32_t calcVariantMatchingScore(int variant, const FontFamily& fontFamily);

93 94 95 96 97 98
    // static for allocating unique id's
    static uint32_t sNextId;

    // unique id for this font collection (suitable for cache key)
    uint32_t mId;

R
Raph Levien 已提交
99 100 101
    // Highest UTF-32 code point that can be mapped
    uint32_t mMaxChar;

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;
R
Raph Levien 已提交
105

106 107
    // Following two vectors are pre-calculated tables for resolving coverage faster.
    // 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 111
    // This vector contains indices into mFamilies.
    // This vector can't be empty.
R
Raph Levien 已提交
112
    std::vector<Range> mRanges;
113
    std::vector<uint8_t> mFamilyVec;
114 115 116

    // This vector has pointers to the font family instances which have cmap 14 subtables.
    std::vector<std::shared_ptr<FontFamily>> mVSFamilyVec;
117 118 119

    // Set of supported axes in this collection.
    std::unordered_set<AxisTag> mSupportedAxes;
R
Raph Levien 已提交
120 121
};

S
Seigo Nonaka 已提交
122
}  // namespace minikin
R
Raph Levien 已提交
123 124

#endif  // MINIKIN_FONT_COLLECTION_H