提交 2eed43ea 编写于 作者: P pssea 提交者: Lizhiqi

Description: support ttc typee font

IssueNo:https://gitee.com/openharmony/graphic_ui/issues/I5QVUS
Feature or Bugfix: Feature
Binary Source:No
Signed-off-by: Nlizhiqi <lizhiqi1@huawei.com>
上级 8e76a7fb
......@@ -18,7 +18,8 @@
#include "font/ui_font_cache.h"
#if ENABLE_VECTOR_FONT
#include "font/ui_font_vector.h"
#else
#endif
#if ENABLE_BITMAP_FONT
#include "font/ui_font_bitmap.h"
#endif
#include "graphic_config.h"
......@@ -35,15 +36,16 @@ UIFont::~UIFont(){}
UIFont* UIFont::GetInstance()
{
static UIFont instance;
#if ENABLE_VECTOR_FONT
#if ENABLE_BITMAP_FONT
if (instance.instance_ == nullptr) {
instance.defaultInstance_ = new UIFontVector();
instance.defaultInstance_ = new UIFontBitmap();
instance.instance_ = instance.defaultInstance_;
setFontAllocFlag_ = true;
}
#else
#endif
#if ENABLE_VECTOR_FONT
if (instance.instance_ == nullptr) {
instance.defaultInstance_ = new UIFontBitmap();
instance.defaultInstance_ = new UIFontVector();
instance.instance_ = instance.defaultInstance_;
setFontAllocFlag_ = true;
}
......@@ -64,6 +66,63 @@ void UIFont::SetFont(BaseFont* font)
}
}
#if (defined(ENABLE_MIX_FONT) && (ENABLE_MIX_FONT == 1))
void UIFont::SetBitampFont(BaseFont* font)
{
if (font == nullptr) {
return;
}
GetBitmapInstance()->SetFont(font);
}
UIFont* UIFont::GetBitmapInstance()
{
static UIFont instance;
if (instance.instance_ == nullptr) {
instance.defaultInstance_ = new UIFontBitmap();
instance.instance_ = instance.defaultInstance_;
setFontAllocFlag_ = true;
}
return &instance;
}
#endif
int8_t UIFont::SetCurrentLangId(uint8_t langId)
{
#if (defined(ENABLE_MIX_FONT) && (ENABLE_MIX_FONT == 1))
GetBitmapInstance()->SetCurrentLangId(langId);
#else
return instance_->SetCurrentLangId(langId);
#endif
}
uint8_t UIFont::GetCurrentLangId() const
{
#if (defined(ENABLE_MIX_FONT) && (ENABLE_MIX_FONT == 1))
return GetBitmapInstance()->GetCurrentLangId(langId);
#else
return instance_->GetCurrentLangId();
#endif
}
int8_t UIFont::GetTextUtf8(uint16_t textId, uint8_t** utf8Addr, uint16_t& utf8Len) const
{
#if (defined(ENABLE_MIX_FONT) && (ENABLE_MIX_FONT == 1))
return GetBitmapInstance()->GetTextUtf8(textId, utf8Addr, utf8Len);
#else
return instance_->GetTextUtf8(textId, utf8Addr, utf8Len);
#endif
}
int8_t UIFont::GetTextParam(uint16_t textId, UITextLanguageTextParam& param) const
{
#if (defined(ENABLE_MIX_FONT) && (ENABLE_MIX_FONT == 1))
return GetBitmapInstance()->GetTextParam(textId, param);
#else
return instance_->GetTextParam(textId, param);
#endif
}
uint8_t* UIFont::GetBitmap(uint32_t unicode, GlyphNode& glyphNode, uint8_t fontId, uint8_t fontSize,
uint8_t shapingFont)
{
......
......@@ -16,6 +16,7 @@
#include "font/ui_font_vector.h"
#include <freetype/ftoutln.h>
#include <freetype/internal/ftobjs.h>
#include <freetype/internal/ftstream.h>
#include <freetype/ftbitmap.h>
#include "common/typed_text.h"
#include "draw/draw_utils.h"
......@@ -143,6 +144,78 @@ uint8_t UIFontVector::RegisterFontInfo(const UITextLanguageFontParam* fontsTable
return count;
}
/* Note: when use ttc font file, freetype should export FT_Stream_New/FT_Stream_Free function*/
uint8_t UIFontVector::RegisterTtcFontInfo(const char* ttcName, TtfInfo* ttfInfo, uint8_t count)
{
if ((ttcName == nullptr) || !freeTypeInited_) {
return FONT_INVALID_TTF_ID;
}
if (bitmapCache_ == nullptr) {
bitmapCache_ = new (std::nothrow) UIFontCache(reinterpret_cast<uint8_t*>(GetRamAddr()), GetRamLen());
if (bitmapCache_ == nullptr) {
return FONT_INVALID_TTF_ID;
}
}
int32_t i = 0;
int32_t error = 0;
while (i < FONT_ID_MAX) {
if ((ttcInfos_[i].ttcName != nullptr) && !strncmp(ttcInfos_[i].ttcName, ttcName, TTF_NAME_LEN_MAX)) {
return i;
} else if (ttcInfos_[i].ttcName == nullptr) {
std::string ttcPath = ttfDir_;
ttcPath.append(ttcName);
FT_Open_Args args = {FT_OPEN_PATHNAME, nullptr, 0, const_cast<char*>(ttcPath.c_str()),
nullptr, nullptr, 0, nullptr};
error = FT_Stream_New(ftLibrary_, &args, &ttcInfos_[i].stream);
if (error != 0) {
return FONT_INVALID_TTF_ID;
}
ttcInfos_[i].ttcName = ttcName;
args = {FT_OPEN_STREAM, nullptr, 0, nullptr, ttcInfos_[i].stream, nullptr, 0, nullptr};
for (uint8_t j = 0; j < count; j++) {
error = FT_Open_Face(ftLibrary_, &args, j, &ftFaces_[i]);
if (error != 0) {
continue;
}
fontInfo_[i].ttfName = ttfInfo[j].ttfName;
fontInfo_[i].shaping = ttfInfo[j].shaping;
fontInfo_[i].ttfId = i;
if (IsColorEmojiFont(ftFaces_[i])) {
SetupColorFont(ftFaces_[i]);
}
#if ENABLE_MULTI_FONT
UIMultiFontManager::GetInstance()->UpdateScript(fontInfo_[j]);
#endif
}
return i;
}
i++;
}
return FONT_INVALID_TTF_ID;
}
uint8_t UIFontVector::UnregisterTtcFontInfo(const char* ttcName, TtfInfo* ttfInfo, uint8_t count)
{
if (ttcName == nullptr || ttfInfo == nullptr) {
return FONT_INVALID_TTF_ID;
}
uint8_t i = 0;
while (i < FONT_ID_MAX) {
if ((ttcInfos_[i].ttcName != nullptr) && !strncmp(ttcInfos_[i].ttcName, ttcName, TTF_NAME_LEN_MAX)) {
FT_Stream_Free(ttcInfos_[i].stream, 1);
for (uint8_t j = 0; j < count; j++) {
UnregisterFontInfo(ttfInfo[j].ttfName);
}
return i;
}
i++;
}
return FONT_INVALID_TTF_ID;
}
uint8_t UIFontVector::UnregisterFontInfo(const UITextLanguageFontParam* fontsTable, uint8_t num)
{
if (fontsTable == nullptr) {
......@@ -526,10 +599,7 @@ int8_t UIFontVector::LoadGlyphIntoFace(uint8_t& fontId, uint32_t unicode, FT_Fac
{
bool isHaveBitmap = false;
int32_t error;
if (IsGlyphFont(unicode) != 0) {
if (fontId != GetFontId(unicode)) {
return INVALID_RET_VALUE;
}
if (fontInfo_[fontId].shaping != 0) {
unicode = unicode & (0xFFFFFF); // Whether 0 ~24 bit storage is unicode
error = FT_Load_Glyph(face, unicode, FT_LOAD_RENDER);
isHaveBitmap = true;
......
......@@ -46,6 +46,8 @@ public:
uint8_t RegisterFontInfo(const UITextLanguageFontParam* fontsTable, uint8_t num) override;
uint8_t UnregisterFontInfo(const char* ttfName) override;
uint8_t UnregisterFontInfo(const UITextLanguageFontParam* fontsTable, uint8_t num) override;
uint8_t RegisterTtcFontInfo(const char* ttcName, TtfInfo* ttfInfo, uint8_t count) override;
uint8_t UnregisterTtcFontInfo(const char* ttcName, TtfInfo* ttfInfo, uint8_t count) override;
const UITextLanguageFontParam* GetFontInfo(uint8_t fontId) const override;
int32_t OpenVectorFont(uint8_t ttfId) override;
bool IsColorEmojiFont(FT_Face &face);
......@@ -71,6 +73,10 @@ private:
FT_Face face;
uint32_t key;
};
struct TtcInfo {
const char* ttcName;
FT_Stream stream;
};
struct Metric {
int left;
int top;
......@@ -79,6 +85,7 @@ private:
int advance;
uint8_t buf[0];
};
TtcInfo ttcInfos_[FONT_ID_MAX] = {};
void SetFace(FaceInfo& faceInfo, uint32_t unicode) const;
#if ENABLE_VECTOR_FONT
void SetFace(FaceInfo& faceInfo, uint32_t unicode, TextStyle textStyle) const;
......
......@@ -170,6 +170,16 @@ public:
return 0;
}
virtual uint8_t RegisterTtcFontInfo(const char* ttcName, TtfInfo* ttfInfo, uint8_t count)
{
return 0;
}
virtual uint8_t UnregisterTtcFontInfo(const char* ttcName, TtfInfo* ttfInfo, uint8_t count)
{
return 0;
}
virtual uint8_t UnregisterFontInfo(const char* ttfName)
{
return 0;
......
......@@ -124,25 +124,16 @@ public:
return instance_->IsVectorFont();
}
int8_t SetCurrentLangId(uint8_t langId)
{
return instance_->SetCurrentLangId(langId);
}
int8_t SetCurrentLangId(uint8_t langId);
uint8_t GetCurrentLangId() const
{
return instance_->GetCurrentLangId();
}
uint8_t GetCurrentLangId() const;
int8_t GetDefaultParamByLangId(uint8_t langId, LangTextParam** pParam) const
{
return instance_->GetDefaultParamByLangId(langId, pParam);
}
int8_t GetTextUtf8(uint16_t textId, uint8_t** utf8Addr, uint16_t& utf8Len) const
{
return instance_->GetTextUtf8(textId, utf8Addr, utf8Len);
}
int8_t GetTextUtf8(uint16_t textId, uint8_t** utf8Addr, uint16_t& utf8Len) const;
uint8_t GetFontTtfId(uint8_t fontId, uint8_t size) const
{
......@@ -179,6 +170,16 @@ public:
return instance_->RegisterFontInfo(fontsTable, num);
}
uint8_t RegisterTtcFontInfo(const char* ttcName, TtfInfo* ttfInfo, uint8_t count)
{
return instance_->RegisterTtcFontInfo(ttcName, ttfInfo, count);
}
uint8_t UnregisterTtcFontInfo(const char* ttcName, TtfInfo* ttfInfo, uint8_t count)
{
return instance_->UnregisterTtcFontInfo(ttcName, ttfInfo, count);
}
uint8_t UnregisterFontInfo(const char* ttfName)
{
return instance_->UnregisterFontInfo(ttfName);
......@@ -189,10 +190,7 @@ public:
return instance_->UnregisterFontInfo(fontsTable, num);
}
int8_t GetTextParam(uint16_t textId, UITextLanguageTextParam& param) const
{
return instance_->GetTextParam(textId, param);
}
int8_t GetTextParam(uint16_t textId, UITextLanguageTextParam& param) const;
int8_t GetWildCardStaticStr(uint16_t textId, UITextWildcardStaticType type,
uint8_t** strAddr, uint16_t& strLen) const
......@@ -226,6 +224,16 @@ public:
{
return instance_->IsEmojiFont(fontid);
}
#if (defined(ENABLE_MIX_FONT) && (ENABLE_MIX_FONT == 1))
/**
* @brief Set bitmap font, only needed when using both vector font and bitmap font
*
* @param font bitmap font
*/
void SetBitampFont(BaseFont* font);
#endif
private:
UIFont();
/**
......@@ -234,6 +242,15 @@ private:
*/
~UIFont();
#if (defined(ENABLE_MIX_FONT) && (ENABLE_MIX_FONT == 1))
/**
* @brief Get bitmap font, only needed when using both vector font and bitmap font
*
* @return UIFont bitmap font instance
*/
static UIFont* GetBitmapInstance();
#endif
BaseFont* instance_;
BaseFont* defaultInstance_;
static bool setFontAllocFlag_;
......
......@@ -389,6 +389,12 @@ struct FileCommonHeader {
const char magicWord[5]; // 5:file identifier's length is 5
uint32_t fileLength;
};
struct TtfInfo {
const char* ttfName;
uint8_t shaping;
};
#pragma pack()
} // namespace OHOS
#endif /* UI_FONT_HEADER_H */
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册