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