mirror of
https://gitee.com/openharmony/graphic_graphic_2d
synced 2024-11-28 01:34:16 +00:00
commit
1d9ddb58e1
@ -52,6 +52,7 @@ template("skia_libtxt") {
|
||||
"$graphic_2d_root/rosen/modules/2d_engine/rosen_text/adapter/skia_txt",
|
||||
"$graphic_2d_root/rosen/modules/2d_engine/rosen_text/skia_txt",
|
||||
"$graphic_2d_root/rosen/modules/2d_engine/rosen_text/skia_txt/txt",
|
||||
"$graphic_2d_root/rosen/modules/texgine/src",
|
||||
]
|
||||
|
||||
sources = [
|
||||
|
@ -17,8 +17,10 @@
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "font_config.h"
|
||||
#include "include/core/SkString.h"
|
||||
#include "include/core/SkTypeface.h"
|
||||
#include "utils/text_log.h"
|
||||
|
||||
namespace txt {
|
||||
AssetFontManager::AssetFontManager(std::unique_ptr<FontAssetProvider> fontProvider)
|
||||
@ -112,4 +114,16 @@ SkFontStyleSet* TestFontManager::onMatchFamily(const char familyName[]) const
|
||||
}
|
||||
return AssetFontManager::onMatchFamily(sanitizedName.c_str());
|
||||
}
|
||||
|
||||
int DynamicFontManager::ParseInstallFontConfig(const std::string& configPath,
|
||||
std::vector<std::string>& fontPathVec)
|
||||
{
|
||||
OHOS::Rosen::TextEngine::FontConfigJson fontConfigJson;
|
||||
int ret = fontConfigJson.ParseInstallConfig(configPath.c_str(), fontPathVec);
|
||||
if (ret != SUCCESSED) {
|
||||
TEXT_LOGE_LIMIT3_MIN("Failed to parse json config file, ret = %d", ret);
|
||||
return ERROR_PARSE_CONFIG_FAILED;
|
||||
}
|
||||
return SUCCESSED;
|
||||
}
|
||||
} // namespace txt
|
||||
|
@ -71,6 +71,7 @@ public:
|
||||
{
|
||||
return static_cast<TypefaceFontAssetProvider&>(*fontProvider_);
|
||||
}
|
||||
int ParseInstallFontConfig(const std::string& configPath, std::vector<std::string>& fontPathVec);
|
||||
};
|
||||
|
||||
class TestFontManager : public AssetFontManager {
|
||||
|
54
rosen/modules/2d_graphics/include/text/common_utils.h
Normal file
54
rosen/modules/2d_graphics/include/text/common_utils.h
Normal file
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
||||
* 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 COMMON_UTILS_H
|
||||
#define COMMON_UTILS_H
|
||||
|
||||
namespace OHOS {
|
||||
namespace Rosen {
|
||||
namespace Drawing {
|
||||
static bool IsUtf8(const char* text)
|
||||
{
|
||||
int len = strlen(text);
|
||||
int n = 0;
|
||||
for (int i = 0; i < len; i++) {
|
||||
uint32_t c = text[i];
|
||||
if (c <= 0x7F) { // 0x00 and 0x7F is the range of utf-8
|
||||
n = 0;
|
||||
} else if ((c & 0xE0) == 0xC0) { // 0xE0 and 0xC0 is the range of utf-8
|
||||
n = 1;
|
||||
} else if (c == 0xED && i < (len - 1) && (text[i + 1] & 0xA0) == 0xA0) { // 0xA0 and 0xED is the range of utf-8
|
||||
return false;
|
||||
} else if ((c & 0xF0) == 0xE0) { // 0xE0 and 0xF0 is the range of utf-8
|
||||
n = 2; // 2 means the size of range
|
||||
} else if ((c & 0xF8) == 0xF0) { // 0xF0 and 0xF8 is the range of utf-8
|
||||
n = 3; // 3 means the size of range
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
for (int j = 0; j < n && i < len; j++) {
|
||||
// 0x80 and 0xC0 is the range of utf-8
|
||||
i++;
|
||||
if ((i == len) || ((text[i] & 0xC0) != 0x80)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
@ -27,6 +27,20 @@
|
||||
namespace OHOS {
|
||||
namespace Rosen {
|
||||
namespace Drawing {
|
||||
enum FontCheckCode {
|
||||
SUCCESSED = 0, /** no error */
|
||||
ERROR_PARSE_CONFIG_FAILED = 1, /** failed to parse the JSON configuration file */
|
||||
ERROR_TYPE_OTHER = 2 /** other reasons, such as empty input parameters or other internal reasons */
|
||||
};
|
||||
|
||||
struct FontByteArray {
|
||||
public:
|
||||
FontByteArray(std::unique_ptr<uint8_t[]> data, uint32_t dataLen)
|
||||
: strData(std::move(data)), strLen(dataLen) {}
|
||||
std::unique_ptr<uint8_t[]> strData; // A byte array in UTF-16BE encoding
|
||||
uint32_t strLen;
|
||||
};
|
||||
|
||||
class DRAWING_API FontMgr {
|
||||
public:
|
||||
explicit FontMgr(std::shared_ptr<FontMgrImpl> fontMgrImpl) noexcept;
|
||||
@ -116,6 +130,23 @@ public:
|
||||
|
||||
FontStyleSet* CreateStyleSet(int index) const;
|
||||
|
||||
/**
|
||||
* @brief Get the fullname of font
|
||||
* @param fontFd The file descriptor for the font file
|
||||
* @param fullnameVec Read the font fullname list
|
||||
* @return Returns Whether the fullnameVec was successfully obtained, 0 means success,
|
||||
* see FontCheckCode for details
|
||||
*/
|
||||
int GetFontFullName(int fontFd, std::vector<FontByteArray>& fullnameVec);
|
||||
|
||||
/**
|
||||
* @brief Parse the Installed font configuration file and get the font path list
|
||||
* @param configPath The path to the configuration file
|
||||
* @param fontPathVec Read a list of font file paths
|
||||
* @return Returns Whether the configuration file is parsed successfully, 0 means success,
|
||||
* see FontCheckCode for details
|
||||
*/
|
||||
int ParseInstallFontConfig(const std::string& configPath, std::vector<std::string>& fontPathVec);
|
||||
private:
|
||||
std::shared_ptr<FontMgrImpl> fontMgrImpl_;
|
||||
};
|
||||
|
@ -24,6 +24,7 @@
|
||||
namespace OHOS {
|
||||
namespace Rosen {
|
||||
namespace Drawing {
|
||||
struct FontByteArray;
|
||||
class FontMgrImpl : public BaseImpl {
|
||||
public:
|
||||
~FontMgrImpl() override = default;
|
||||
@ -44,6 +45,8 @@ public:
|
||||
virtual int CountFamilies() const = 0;
|
||||
virtual void GetFamilyName(int index, std::string& str) const = 0;
|
||||
virtual FontStyleSet* CreateStyleSet(int index) const = 0;
|
||||
virtual int GetFontFullName(int fontFd, std::vector<FontByteArray>& fullnameVec) = 0;
|
||||
virtual int ParseInstallFontConfig(const std::string& configPath, std::vector<std::string>& fontPathVec) = 0;
|
||||
protected:
|
||||
FontMgrImpl() noexcept = default;
|
||||
};
|
||||
|
@ -14,6 +14,9 @@
|
||||
*/
|
||||
|
||||
#include "skia_font_mgr.h"
|
||||
#include <codecvt>
|
||||
#include <locale>
|
||||
#include <securec.h>
|
||||
|
||||
#include "include/core/SkString.h"
|
||||
#include "include/core/SkTypeface.h"
|
||||
@ -21,14 +24,70 @@
|
||||
#include "txt/asset_font_manager.h"
|
||||
#endif
|
||||
|
||||
#include "text/font_mgr.h"
|
||||
#include "skia_adapter/skia_convert_utils.h"
|
||||
#include "skia_adapter/skia_font_style_set.h"
|
||||
#include "skia_adapter/skia_typeface.h"
|
||||
#include "text/common_utils.h"
|
||||
#include "utils/log.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace Rosen {
|
||||
namespace Drawing {
|
||||
namespace {
|
||||
const uint8_t MOVEBITS = 8;
|
||||
void SwapBytes(char16_t* srcStr, uint32_t len)
|
||||
{
|
||||
if (srcStr == nullptr || len == 0) {
|
||||
return;
|
||||
}
|
||||
// if is big endian, high-order byte first
|
||||
int num = 1;
|
||||
if (*(reinterpret_cast<const uint8_t*>(&num)) == 0) {
|
||||
return;
|
||||
}
|
||||
// swap bytes
|
||||
for (uint32_t i = 0; i < len; i++) {
|
||||
uint16_t temp = static_cast<uint16_t>(srcStr[i]);
|
||||
// Swap the byte order of the 16-bit value
|
||||
srcStr[i] = static_cast<char16_t>((temp & 0xff) << MOVEBITS | (temp & 0xff00) >> MOVEBITS);
|
||||
}
|
||||
}
|
||||
|
||||
bool ConvertToUTF16BE(uint8_t* data, uint32_t dataLen, FontByteArray& fullname)
|
||||
{
|
||||
if (data == nullptr || dataLen == 0) {
|
||||
return false;
|
||||
}
|
||||
// If the encoding format of data is UTF-16, copy it directly
|
||||
if (strlen((char*)data) < dataLen || !IsUtf8((const char*)data)) {
|
||||
fullname.strData = std::make_unique<uint8_t[]>(dataLen);
|
||||
if (memcpy_s(fullname.strData.get(), dataLen, data, dataLen) == EOK) {
|
||||
fullname.strLen = dataLen;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
// If the data format is utf-8, create a converter from UTF-8 to UTF-16
|
||||
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> converter;
|
||||
std::string utf8String((char*)data, dataLen);
|
||||
std::u16string utf16String = converter.from_bytes(utf8String);
|
||||
char16_t* u16Data = const_cast<char16_t*>(utf16String.c_str());
|
||||
// Get the byte length and copy the data
|
||||
size_t strByteLen = utf16String.size() * sizeof(char16_t);
|
||||
if (strByteLen == 0) {
|
||||
return false;
|
||||
}
|
||||
SwapBytes(u16Data, strByteLen / sizeof(char16_t));
|
||||
fullname.strData = std::make_unique<uint8_t[]>(strByteLen);
|
||||
if (memcpy_s(fullname.strData.get(), strByteLen, u16Data, strByteLen) == EOK) {
|
||||
fullname.strLen = strByteLen;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
SkiaFontMgr::SkiaFontMgr(sk_sp<SkFontMgr> skFontMgr) : skFontMgr_(skFontMgr) {}
|
||||
|
||||
std::shared_ptr<FontMgrImpl> SkiaFontMgr::CreateDefaultFontMgr()
|
||||
@ -191,6 +250,34 @@ FontStyleSet* SkiaFontMgr::CreateStyleSet(int index) const
|
||||
return new FontStyleSet(fontStyleSetImpl);
|
||||
}
|
||||
|
||||
int SkiaFontMgr::GetFontFullName(int fontFd, std::vector<FontByteArray>& fullnameVec)
|
||||
{
|
||||
if (skFontMgr_ == nullptr) {
|
||||
return ERROR_TYPE_OTHER;
|
||||
}
|
||||
std::vector<SkByteArray> skFullnameVec;
|
||||
int ret = skFontMgr_->GetFontFullName(fontFd, skFullnameVec);
|
||||
if (ret != SUCCESSED) {
|
||||
return ret;
|
||||
}
|
||||
for (SkByteArray &skFullname : skFullnameVec) {
|
||||
FontByteArray newFullname = {nullptr, 0};
|
||||
if (ConvertToUTF16BE(skFullname.strData.get(), skFullname.strLen, newFullname)) {
|
||||
fullnameVec.push_back(std::move(newFullname));
|
||||
} else {
|
||||
return ERROR_TYPE_OTHER;
|
||||
}
|
||||
}
|
||||
return SUCCESSED;
|
||||
}
|
||||
|
||||
int SkiaFontMgr::ParseInstallFontConfig(const std::string& configPath, std::vector<std::string>& fontPathVec)
|
||||
{
|
||||
if (skFontMgr_ == nullptr) {
|
||||
return ERROR_TYPE_OTHER;
|
||||
}
|
||||
return skFontMgr_->ParseInstallFontConfig(configPath, fontPathVec);
|
||||
}
|
||||
} // namespace Drawing
|
||||
} // namespace Rosen
|
||||
} // namespace OHOS
|
@ -54,6 +54,8 @@ public:
|
||||
int CountFamilies() const override;
|
||||
void GetFamilyName(int index, std::string& str) const override;
|
||||
FontStyleSet* CreateStyleSet(int index) const override;
|
||||
int GetFontFullName(int fontFd, std::vector<FontByteArray>& fullnameVec) override;
|
||||
int ParseInstallFontConfig(const std::string& configPath, std::vector<std::string>& fontPathVec) override;
|
||||
private:
|
||||
sk_sp<SkFontMgr> skFontMgr_;
|
||||
};
|
||||
|
@ -110,6 +110,21 @@ FontStyleSet* FontMgr::CreateStyleSet(int index) const
|
||||
return fontMgrImpl_->CreateStyleSet(index);
|
||||
}
|
||||
|
||||
int FontMgr::GetFontFullName(int fontFd, std::vector<FontByteArray>& fullnameVec)
|
||||
{
|
||||
if (fontMgrImpl_ == nullptr) {
|
||||
return ERROR_TYPE_OTHER;
|
||||
}
|
||||
return fontMgrImpl_->GetFontFullName(fontFd, fullnameVec);
|
||||
};
|
||||
|
||||
int FontMgr::ParseInstallFontConfig(const std::string& configPath, std::vector<std::string>& fontPathVec)
|
||||
{
|
||||
if (fontMgrImpl_ == nullptr) {
|
||||
return ERROR_TYPE_OTHER;
|
||||
}
|
||||
return fontMgrImpl_->ParseInstallFontConfig(configPath, fontPathVec);
|
||||
}
|
||||
} // namespace Drawing
|
||||
} // namespace Rosen
|
||||
} // namespace OHOS
|
||||
|
@ -23,6 +23,7 @@
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "utils/text_log.h"
|
||||
#ifdef BUILD_NON_SDK_VER
|
||||
#include "securec.h"
|
||||
#endif
|
||||
@ -442,6 +443,56 @@ int FontConfigJson::ParseFontMap(const cJSON* root, const char* key)
|
||||
return SUCCESSED;
|
||||
}
|
||||
|
||||
int FontConfigJson::ParseInstallFont(const cJSON* root, std::vector<std::string>& fontPathList)
|
||||
{
|
||||
const char* tag = "fontlist";
|
||||
cJSON* rootObj = cJSON_GetObjectItem(root, tag);
|
||||
if (rootObj == nullptr) {
|
||||
TEXT_LOGE("Failed to get json object");
|
||||
return FAILED;
|
||||
}
|
||||
int size = cJSON_GetArraySize(rootObj);
|
||||
if (size <= 0) {
|
||||
TEXT_LOGE("Failed to get json array size");
|
||||
return FAILED;
|
||||
}
|
||||
fontPathList.reserve(size);
|
||||
for (int i = 0; i < size; i++) {
|
||||
cJSON* item = cJSON_GetArrayItem(rootObj, i);
|
||||
if (item == nullptr) {
|
||||
TEXT_LOGE("Failed to get json item");
|
||||
return FAILED;
|
||||
}
|
||||
cJSON* fullPath = cJSON_GetObjectItem(item, "fontfullpath");
|
||||
if (fullPath == nullptr || !cJSON_IsString(fullPath) || fullPath->valuestring == nullptr) {
|
||||
TEXT_LOGE("Failed to get fullPath");
|
||||
return FAILED;
|
||||
}
|
||||
fontPathList.emplace_back(std::string(fullPath->valuestring));
|
||||
}
|
||||
return SUCCESSED;
|
||||
}
|
||||
|
||||
int FontConfigJson::ParseInstallConfig(const char* fontPath, std::vector<std::string>& fontPathList)
|
||||
{
|
||||
if (fontPath == nullptr) {
|
||||
TEXT_LOGE("Font path is null");
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
cJSON* root = CheckConfigFile(fontPath);
|
||||
if (root == nullptr) {
|
||||
TEXT_LOGE("Failed to check config file");
|
||||
return FAILED;
|
||||
}
|
||||
if (ParseInstallFont(root, fontPathList) != SUCCESSED) {
|
||||
cJSON_Delete(root);
|
||||
return FAILED;
|
||||
}
|
||||
cJSON_Delete(root);
|
||||
return SUCCESSED;
|
||||
}
|
||||
|
||||
void FontConfigJson::DumpAlias(const AliasSet &aliasSet) const
|
||||
{
|
||||
if (!aliasSet.empty()) {
|
||||
|
@ -87,6 +87,7 @@ public:
|
||||
FontConfigJson() = default;
|
||||
int ParseFile(const char* fname = nullptr);
|
||||
int ParseFontFileMap(const char* fname = nullptr);
|
||||
int ParseInstallConfig(const char* fname, std::vector<std::string>& fontPathList);
|
||||
std::shared_ptr<FontConfigJsonInfo> GetFontConfigJsonInfo()
|
||||
{
|
||||
return fontPtr;
|
||||
@ -110,6 +111,7 @@ private:
|
||||
void AnalyseFontDir(const cJSON* root);
|
||||
int ParseAdjustArr(const cJSON* arr, FontGenericInfo &genericInfo);
|
||||
int ParseAliasArr(const cJSON* arr, FontGenericInfo &genericInfo);
|
||||
int ParseInstallFont(const cJSON* root, std::vector<std::string>& fontPathList);
|
||||
void DumpFontDir() const;
|
||||
void DumpGeneric() const;
|
||||
void DumpForbak() const;
|
||||
|
@ -336,6 +336,54 @@ bool FontMgrFuzzTest008(const uint8_t* data, size_t size)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FontMgrFuzzTest009(const uint8_t* data, size_t size)
|
||||
{
|
||||
if (data == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// initialize
|
||||
g_data = data;
|
||||
g_size = size;
|
||||
g_pos = 0;
|
||||
|
||||
std::shared_ptr<FontMgr> fontMgr = FontMgr::CreateDefaultFontMgr();
|
||||
int fd = GetObject<int>() % MAX_SIZE;
|
||||
std::vector<FontByteArray> fullnameVec;
|
||||
fontMgr->GetFontFullName(fd, fullnameVec);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FontMgrFuzzTest010(const uint8_t* data, size_t size)
|
||||
{
|
||||
if (data == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// initialize
|
||||
g_data = data;
|
||||
g_size = size;
|
||||
g_pos = 0;
|
||||
|
||||
std::shared_ptr<FontMgr> fontMgr = FontMgr::CreateDynamicFontMgr();
|
||||
uint32_t countT = GetObject<uint32_t>() % MAX_SIZE + 1;
|
||||
char* path = new char[countT];
|
||||
if (path == nullptr) {
|
||||
return false;
|
||||
}
|
||||
for (size_t i = 0; i < countT; i++) {
|
||||
path[i] = GetObject<char>();
|
||||
}
|
||||
path[countT - 1] = '\0';
|
||||
std::string strPath(path);
|
||||
std::vector<std::string> fontPathVec;
|
||||
fontMgr->ParseInstallFontConfig(strPath, fontPathVec);
|
||||
if (path != nullptr) {
|
||||
delete [] path;
|
||||
path = nullptr;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
} // namespace Drawing
|
||||
} // namespace Rosen
|
||||
} // namespace OHOS
|
||||
@ -352,5 +400,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
|
||||
OHOS::Rosen::Drawing::FontMgrFuzzTest006(data, size);
|
||||
OHOS::Rosen::Drawing::FontMgrFuzzTest007(data, size);
|
||||
OHOS::Rosen::Drawing::FontMgrFuzzTest008(data, size);
|
||||
OHOS::Rosen::Drawing::FontMgrFuzzTest009(data, size);
|
||||
OHOS::Rosen::Drawing::FontMgrFuzzTest010(data, size);
|
||||
return 0;
|
||||
}
|
@ -13,8 +13,13 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
#include <codecvt>
|
||||
#include <cstddef>
|
||||
#include <fcntl.h>
|
||||
#include <fstream>
|
||||
#include <locale>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "impl_factory.h"
|
||||
@ -127,6 +132,199 @@ HWTEST_F(FontMgrTest, CreateStyleSet001, TestSize.Level1)
|
||||
FontStyleSet* fontStyleSet = FontMgr->CreateStyleSet(0);
|
||||
ASSERT_TRUE(fontStyleSet != nullptr);
|
||||
}
|
||||
|
||||
const char* TTF_FILE_PATH = "/system/fonts/Roboto-Regular.ttf";
|
||||
// The ttf font file fullname is the utf16BE format content corresponding to "Noto Sans Regular"
|
||||
const uint8_t TTF_FULLNAME[] = {0x0, 0x4e, 0x0, 0x6f, 0x0, 0x74, 0x0, 0x6f, 0x0, 0x20, 0x0, 0x53,
|
||||
0x0, 0x61, 0x0, 0x6e, 0x0, 0x73, 0x0, 0x20, 0x0, 0x52, 0x0, 0x65,
|
||||
0x0, 0x67, 0x0, 0x75, 0x0, 0x6c, 0x0, 0x61, 0x0, 0x72};
|
||||
const char* OTF_FILE_PATH = "/data/fonts/Igiari-2.otf";
|
||||
// The otf font file fullname is the utf16BE format content corresponding to "Igiari"
|
||||
const uint8_t OTF_FULLNAME[] = {0x0, 0x49, 0x0, 0x67, 0x0, 0x69, 0x0, 0x61, 0x0, 0x72, 0x0, 0x69};
|
||||
const char* TTC_FILE_PATH = "/system/fonts/NotoSerifCJK-Regular.ttc";
|
||||
// The ttc font file fullname is the utf16BE format content corresponding to "Noto Serif CJK JP"
|
||||
const uint8_t TTC_FULLNAME[] = {0x0, 0x4e, 0x0, 0x6f, 0x0, 0x74, 0x0, 0x6f, 0x0, 0x20, 0x0, 0x53,
|
||||
0x0, 0x65, 0x0, 0x72, 0x0, 0x69, 0x0, 0x66, 0x0, 0x20, 0x0, 0x43,
|
||||
0x0, 0x4a, 0x0, 0x4b, 0x0, 0x20, 0x0, 0x4a, 0x0, 0x50};
|
||||
const char* ERRORPATH_FILE_PATH = "/system/fonts/error_path.ttf";
|
||||
const char* ERRORFORMAT_FILE_PATH = "/system/etc/fontconfig.json";
|
||||
const char* JSON_CONFIG_PATH = "/data/fonts/install_fontconfig.json";
|
||||
const char* ERROR_JSON_CONFIG_PATH = "/data/fonts/error_path.json";
|
||||
const char* CONFIG_FIRST_FONT_PATH = "/data/fonts/Igiari-2.otf";
|
||||
/**
|
||||
* @tc.name:GetFontFullName001
|
||||
* @tc.desc: Check the validity of the TTF file and obtain the full name
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: I91F9L
|
||||
*/
|
||||
HWTEST_F(FontMgrTest, GetFontFullName001, TestSize.Level1)
|
||||
{
|
||||
std::shared_ptr<FontMgr> fontMgr = FontMgr::CreateDefaultFontMgr();
|
||||
EXPECT_TRUE(fontMgr != nullptr);
|
||||
std::vector<FontByteArray> fullnameVec;
|
||||
std::string fontPath = TTF_FILE_PATH;
|
||||
int fd = open(fontPath.c_str(), O_RDONLY);
|
||||
if (fd != -1) {
|
||||
int ret = fontMgr->GetFontFullName(fd, fullnameVec);
|
||||
close(fd);
|
||||
EXPECT_TRUE(ret == 0 && fullnameVec.size() > 0);
|
||||
EXPECT_TRUE(fullnameVec[0].strLen == sizeof(TTF_FULLNAME));
|
||||
EXPECT_TRUE(memcmp(fullnameVec[0].strData.get(), TTF_FULLNAME, fullnameVec[0].strLen) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name:GetFontFullName002
|
||||
* @tc.desc: Check the validity of the OTF file and obtain the full name
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: I91F9L
|
||||
*/
|
||||
HWTEST_F(FontMgrTest, GetFontFullName002, TestSize.Level1)
|
||||
{
|
||||
std::shared_ptr<FontMgr> fontMgr = FontMgr::CreateDefaultFontMgr();
|
||||
EXPECT_TRUE(fontMgr != nullptr);
|
||||
std::vector<FontByteArray> fullnameVec;
|
||||
std::string fontPath = OTF_FILE_PATH;
|
||||
int fd = open(fontPath.c_str(), O_RDONLY);
|
||||
if (fd != -1) {
|
||||
int ret = fontMgr->GetFontFullName(fd, fullnameVec);
|
||||
close(fd);
|
||||
EXPECT_TRUE(ret == 0 && fullnameVec.size() > 0);
|
||||
EXPECT_TRUE(fullnameVec[0].strLen == sizeof(OTF_FULLNAME));
|
||||
EXPECT_TRUE(memcmp(fullnameVec[0].strData.get(), OTF_FULLNAME, fullnameVec[0].strLen) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name:GetFontFullName003
|
||||
* @tc.desc: Check the validity of the TTC file and obtain the full name
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: I91F9L
|
||||
*/
|
||||
HWTEST_F(FontMgrTest, GetFontFullName003, TestSize.Level1)
|
||||
{
|
||||
std::shared_ptr<FontMgr> fontMgr = FontMgr::CreateDefaultFontMgr();
|
||||
EXPECT_TRUE(fontMgr != nullptr);
|
||||
std::vector<FontByteArray> fullnameVec;
|
||||
std::string fontPath = TTC_FILE_PATH;
|
||||
int fd = open(fontPath.c_str(), O_RDONLY);
|
||||
if (fd != -1) {
|
||||
int ret = fontMgr->GetFontFullName(fd, fullnameVec);
|
||||
close(fd);
|
||||
EXPECT_TRUE(ret == 0 && fullnameVec.size() > 0);
|
||||
EXPECT_TRUE(fullnameVec[0].strLen == sizeof(TTC_FULLNAME));
|
||||
EXPECT_TRUE(memcmp(fullnameVec[0].strData.get(), TTC_FULLNAME, fullnameVec[0].strLen) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name:GetFontFullName004
|
||||
* @tc.desc: Enter an incorrect path and return the corresponding error reason code
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: I91F9L
|
||||
*/
|
||||
HWTEST_F(FontMgrTest, GetFontFullName004, TestSize.Level1)
|
||||
{
|
||||
std::shared_ptr<FontMgr> fontMgr = FontMgr::CreateDefaultFontMgr();
|
||||
EXPECT_TRUE(fontMgr != nullptr);
|
||||
std::vector<FontByteArray> fullnameVec;
|
||||
std::string fontPath = ERRORPATH_FILE_PATH;
|
||||
int fd = open(fontPath.c_str(), O_RDONLY);
|
||||
int ret = fontMgr->GetFontFullName(fd, fullnameVec);
|
||||
if (fd != -1) {
|
||||
close(fd);
|
||||
}
|
||||
EXPECT_TRUE(ret == ERROR_TYPE_OTHER);
|
||||
}
|
||||
/**
|
||||
* @tc.name:GetFontFullName005
|
||||
* @tc.desc: Enter an empty path and return the corresponding error reason code
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: I91F9L
|
||||
*/
|
||||
HWTEST_F(FontMgrTest, GetFontFullName005, TestSize.Level1)
|
||||
{
|
||||
std::shared_ptr<FontMgr> fontMgr = FontMgr::CreateDefaultFontMgr();
|
||||
EXPECT_TRUE(fontMgr != nullptr);
|
||||
std::vector<FontByteArray> fullnameVec;
|
||||
std::string filepath = "";
|
||||
int fd = open(filepath.c_str(), O_RDONLY);
|
||||
int ret = fontMgr->GetFontFullName(fd, fullnameVec);
|
||||
if (fd != -1) {
|
||||
close(fd);
|
||||
}
|
||||
EXPECT_TRUE(ret == ERROR_TYPE_OTHER);
|
||||
}
|
||||
/**
|
||||
* @tc.name:GetFontFullName006
|
||||
* @tc.desc: Enter the file path of an incorrectly formatted font file and return the corresponding error reason code
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: I91F9L
|
||||
*/
|
||||
HWTEST_F(FontMgrTest, GetFontFullName006, TestSize.Level1)
|
||||
{
|
||||
std::shared_ptr<FontMgr> fontMgr = FontMgr::CreateDefaultFontMgr();
|
||||
EXPECT_TRUE(fontMgr != nullptr);
|
||||
std::vector<FontByteArray> fullnameVec;
|
||||
std::string fontPath = ERRORFORMAT_FILE_PATH;
|
||||
int fd = open(fontPath.c_str(), O_RDONLY);
|
||||
int ret = fontMgr->GetFontFullName(fd, fullnameVec);
|
||||
if (fd != -1) {
|
||||
close(fd);
|
||||
}
|
||||
EXPECT_TRUE(ret == ERROR_TYPE_OTHER);
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name:ParseInstallFontConfig001
|
||||
* @tc.desc: Enter the path of a properly formatted JSON configuration file and parse it successfully
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: I91F9L
|
||||
*/
|
||||
HWTEST_F(FontMgrTest, ParseInstallFontConfig001, TestSize.Level1)
|
||||
{
|
||||
std::shared_ptr<FontMgr> fontMgr = FontMgr::CreateDynamicFontMgr();
|
||||
EXPECT_TRUE(fontMgr != nullptr);
|
||||
std::vector<std::string> fontPathVec;
|
||||
std::string configPath = JSON_CONFIG_PATH;
|
||||
std::ifstream configFile(configPath, std::ios::in);
|
||||
if (configFile.is_open()) {
|
||||
configFile.close();
|
||||
int ret = fontMgr->ParseInstallFontConfig(configPath, fontPathVec);
|
||||
EXPECT_TRUE(ret == 0 && fontPathVec.size() > 0);
|
||||
EXPECT_TRUE(fontPathVec[0] == CONFIG_FIRST_FONT_PATH);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @tc.name:ParseInstallFontConfig002
|
||||
* @tc.desc: Enter the path of a valid JSON configuration file and fail to parse it
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: I91F9L
|
||||
*/
|
||||
HWTEST_F(FontMgrTest, ParseInstallFontConfig002, TestSize.Level1)
|
||||
{
|
||||
std::shared_ptr<FontMgr> fontMgr = FontMgr::CreateDynamicFontMgr();
|
||||
EXPECT_TRUE(fontMgr != nullptr);
|
||||
std::vector<std::string> fontPathVec;
|
||||
std::string configPath = ERROR_JSON_CONFIG_PATH;
|
||||
int ret = fontMgr->ParseInstallFontConfig(configPath, fontPathVec);
|
||||
EXPECT_TRUE(ret == ERROR_PARSE_CONFIG_FAILED);
|
||||
}
|
||||
/**
|
||||
* @tc.name:ParseInstallFontConfig003
|
||||
* @tc.desc: Enter an empty file path and fail to parse
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: I91F9L
|
||||
*/
|
||||
HWTEST_F(FontMgrTest, ParseInstallFontConfig003, TestSize.Level1)
|
||||
{
|
||||
std::shared_ptr<FontMgr> fontMgr = FontMgr::CreateDynamicFontMgr();
|
||||
EXPECT_TRUE(fontMgr != nullptr);
|
||||
std::vector<std::string> fontPathVec;
|
||||
std::string configPath = "";
|
||||
int ret = fontMgr->ParseInstallFontConfig(configPath, fontPathVec);
|
||||
EXPECT_TRUE(ret == ERROR_PARSE_CONFIG_FAILED);
|
||||
}
|
||||
} // namespace Drawing
|
||||
} // namespace Rosen
|
||||
} // namespace OHOS
|
||||
|
Loading…
Reference in New Issue
Block a user