* Copyright (C) 2015 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_LANGUAGE_H
#define MINIKIN_FONT_LANGUAGE_H
#include <string>
#include <vector>
#include <hb.h>
namespace minikin {
const size_t FONT_LANGUAGES_LIMIT = 12;
const uint16_t INVALID_CODE = 0x7fff;
class FontLanguages;
struct FontLanguage {
public:
enum EmojiStyle : uint8_t {
EMSTYLE_EMPTY = 0,
EMSTYLE_DEFAULT = 1,
EMSTYLE_EMOJI = 2,
EMSTYLE_TEXT = 3,
};
FontLanguage()
: mScript(0ul),
mLanguage(INVALID_CODE),
mRegion(INVALID_CODE),
mHbLanguage(HB_LANGUAGE_INVALID),
mSubScriptBits(0ul),
mEmojiStyle(EMSTYLE_EMPTY) {}
FontLanguage(const char* buf, size_t length);
bool operator==(const FontLanguage other) const {
return !isUnsupported() && isEqualScript(other) &&
mLanguage == other.mLanguage && mRegion == other.mRegion &&
mEmojiStyle == other.mEmojiStyle;
}
bool operator!=(const FontLanguage other) const { return !(*this == other); }
bool isUnsupported() const { return mLanguage == INVALID_CODE; }
EmojiStyle getEmojiStyle() const { return mEmojiStyle; }
hb_language_t getHbLanguage() const { return mHbLanguage; }
bool isEqualScript(const FontLanguage& other) const;
bool supportsHbScript(hb_script_t script) const;
std::string getString() const;
int calcScoreFor(const FontLanguages& supported) const;
uint64_t getIdentifier() const {
return ((uint64_t)mLanguage << 49) | ((uint64_t)mScript << 17) |
((uint64_t)mRegion << 2) | mEmojiStyle;
}
private:
friend class FontLanguages;
uint32_t mScript;
uint16_t mLanguage;
uint16_t mRegion;
hb_language_t mHbLanguage;
static const uint8_t kBopomofoFlag = 1u;
static const uint8_t kHanFlag = 1u << 1;
static const uint8_t kHangulFlag = 1u << 2;
static const uint8_t kHiraganaFlag = 1u << 3;
static const uint8_t kKatakanaFlag = 1u << 4;
static const uint8_t kSimplifiedChineseFlag = 1u << 5;
static const uint8_t kTraditionalChineseFlag = 1u << 6;
uint8_t mSubScriptBits;
EmojiStyle mEmojiStyle;
static uint8_t scriptToSubScriptBits(uint32_t script);
static EmojiStyle resolveEmojiStyle(const char* buf,
size_t length,
uint32_t script);
static bool supportsScript(uint8_t providedBits, uint8_t requestedBits);
};
class FontLanguages {
public:
explicit FontLanguages(std::vector<FontLanguage>&& languages);
FontLanguages() : mUnionOfSubScriptBits(0), mIsAllTheSameLanguage(false) {}
FontLanguages(FontLanguages&&) = default;
size_t size() const { return mLanguages.size(); }
bool empty() const { return mLanguages.empty(); }
const FontLanguage& operator[](size_t n) const { return mLanguages[n]; }
private:
friend struct FontLanguage;
std::vector<FontLanguage> mLanguages;
uint8_t mUnionOfSubScriptBits;
bool mIsAllTheSameLanguage;
uint8_t getUnionOfSubScriptBits() const { return mUnionOfSubScriptBits; }
bool isAllTheSameLanguage() const { return mIsAllTheSameLanguage; }
FontLanguages(const FontLanguages&) = delete;
void operator=(const FontLanguages&) = delete;
};
}
#endif