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_FAMILY_H
     18 #define MINIKIN_FONT_FAMILY_H
     19 
     20 #include <vector>
     21 #include <string>
     22 
     23 #include <utils/TypeHelpers.h>
     24 
     25 #include <minikin/MinikinRefCounted.h>
     26 
     27 namespace android {
     28 
     29 class MinikinFont;
     30 
     31 // FontLanguage is a compact representation of a bcp-47 language tag. It
     32 // does not capture all possible information, only what directly affects
     33 // font rendering.
     34 class FontLanguage {
     35     friend class FontStyle;
     36 public:
     37     FontLanguage() : mBits(0) { }
     38 
     39     // Parse from string
     40     FontLanguage(const char* buf, size_t size);
     41 
     42     bool operator==(const FontLanguage other) const { return mBits == other.mBits; }
     43     operator bool() const { return mBits != 0; }
     44 
     45     std::string getString() const;
     46 
     47     // 0 = no match, 1 = language matches, 2 = language and script match
     48     int match(const FontLanguage other) const;
     49 
     50 private:
     51     explicit FontLanguage(uint32_t bits) : mBits(bits) { }
     52 
     53     uint32_t bits() const { return mBits; }
     54 
     55     static const uint32_t kBaseLangMask = 0xffff;
     56     static const uint32_t kScriptMask = (1 << 18) - (1 << 16);
     57     static const uint32_t kHansFlag = 1 << 16;
     58     static const uint32_t kHantFlag = 1 << 17;
     59     uint32_t mBits;
     60 };
     61 
     62 // FontStyle represents all style information needed to select an actual font
     63 // from a collection. The implementation is packed into a single 32-bit word
     64 // so it can be efficiently copied, embedded in other objects, etc.
     65 class FontStyle {
     66 public:
     67     FontStyle(int weight = 4, bool italic = false) {
     68         bits = (weight & kWeightMask) | (italic ? kItalicMask : 0);
     69     }
     70     FontStyle(FontLanguage lang, int variant = 0, int weight = 4, bool italic = false) {
     71         bits = (weight & kWeightMask) | (italic ? kItalicMask : 0)
     72                 | (variant << kVariantShift) | (lang.bits() << kLangShift);
     73     }
     74     int getWeight() const { return bits & kWeightMask; }
     75     bool getItalic() const { return (bits & kItalicMask) != 0; }
     76     int getVariant() const { return (bits >> kVariantShift) & kVariantMask; }
     77     FontLanguage getLanguage() const { return FontLanguage(bits >> kLangShift); }
     78 
     79     bool operator==(const FontStyle other) const { return bits == other.bits; }
     80 
     81     hash_t hash() const { return bits; }
     82 private:
     83     static const uint32_t kWeightMask = (1 << 4) - 1;
     84     static const uint32_t kItalicMask = 1 << 4;
     85     static const int kVariantShift = 5;
     86     static const uint32_t kVariantMask = (1 << 2) - 1;
     87     static const int kLangShift = 7;
     88     uint32_t bits;
     89 };
     90 
     91 enum FontVariant {
     92     VARIANT_DEFAULT = 0,
     93     VARIANT_COMPACT = 1,
     94     VARIANT_ELEGANT = 2,
     95 };
     96 
     97 inline hash_t hash_type(const FontStyle &style) {
     98     return style.hash();
     99 }
    100 
    101 // attributes representing transforms (fake bold, fake italic) to match styles
    102 class FontFakery {
    103 public:
    104     FontFakery() : mFakeBold(false), mFakeItalic(false) { }
    105     FontFakery(bool fakeBold, bool fakeItalic) : mFakeBold(fakeBold), mFakeItalic(fakeItalic) { }
    106     // TODO: want to support graded fake bolding
    107     bool isFakeBold() { return mFakeBold; }
    108     bool isFakeItalic() { return mFakeItalic; }
    109 private:
    110     bool mFakeBold;
    111     bool mFakeItalic;
    112 };
    113 
    114 struct FakedFont {
    115     // ownership is the enclosing FontCollection
    116     MinikinFont* font;
    117     FontFakery fakery;
    118 };
    119 
    120 class FontFamily : public MinikinRefCounted {
    121 public:
    122     FontFamily() { }
    123 
    124     FontFamily(FontLanguage lang, int variant) : mLang(lang), mVariant(variant) {
    125     }
    126 
    127     ~FontFamily();
    128 
    129     // Add font to family, extracting style information from the font
    130     bool addFont(MinikinFont* typeface);
    131 
    132     void addFont(MinikinFont* typeface, FontStyle style);
    133     FakedFont getClosestMatch(FontStyle style) const;
    134 
    135     FontLanguage lang() const { return mLang; }
    136     int variant() const { return mVariant; }
    137 
    138     // API's for enumerating the fonts in a family. These don't guarantee any particular order
    139     size_t getNumFonts() const;
    140     MinikinFont* getFont(size_t index) const;
    141     FontStyle getStyle(size_t index) const;
    142 private:
    143     void addFontLocked(MinikinFont* typeface, FontStyle style);
    144 
    145     class Font {
    146     public:
    147         Font(MinikinFont* typeface, FontStyle style) :
    148             typeface(typeface), style(style) { }
    149         MinikinFont* typeface;
    150         FontStyle style;
    151     };
    152     FontLanguage mLang;
    153     int mVariant;
    154     std::vector<Font> mFonts;
    155 };
    156 
    157 }  // namespace android
    158 
    159 #endif  // MINIKIN_FONT_FAMILY_H
    160