1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FONT_MGR_H 17 #define FONT_MGR_H 18 19 #include <memory> 20 #include <cstdint> 21 22 #include "impl_interface/font_mgr_impl.h" 23 #include "text/font_style.h" 24 #include "text/font_style_set.h" 25 #include "text/typeface.h" 26 27 namespace OHOS { 28 namespace Rosen { 29 namespace Drawing { 30 enum FontCheckCode { 31 SUCCESSED = 0, /** no error */ 32 ERROR_PARSE_CONFIG_FAILED = 1, /** failed to parse the JSON configuration file */ 33 ERROR_TYPE_OTHER = 2 /** other reasons, such as empty input parameters or other internal reasons */ 34 }; 35 36 struct FontByteArray { 37 public: FontByteArrayFontByteArray38 FontByteArray(std::unique_ptr<uint8_t[]> data, uint32_t dataLen) 39 : strData(std::move(data)), strLen(dataLen) {} 40 std::unique_ptr<uint8_t[]> strData; // A byte array in UTF-16BE encoding 41 uint32_t strLen; 42 }; 43 44 class DRAWING_API FontMgr { 45 public: 46 explicit FontMgr(std::shared_ptr<FontMgrImpl> fontMgrImpl) noexcept; 47 virtual ~FontMgr() = default; 48 49 /** 50 * @brief Create a default fontMgr. 51 * @return A shared pointer to default fontMgr. 52 */ 53 static std::shared_ptr<FontMgr> CreateDefaultFontMgr(); 54 55 /** 56 * @brief Create a dynamic fontMgr. 57 * @return A shared pointer to dynamic fontMgr. 58 */ 59 static std::shared_ptr<FontMgr> CreateDynamicFontMgr(); 60 61 /** 62 * @brief Load dynamic font typeface. 63 * @param familyName Font family name. 64 * @param data Font data. 65 * @param dataLength The size of font data. 66 * @return A pointer to typeface. 67 */ 68 Typeface* LoadDynamicFont(const std::string& familyName, const uint8_t* data, size_t dataLength); 69 70 /** 71 * @brief Load theme font typeface. 72 * @param familyName Font family name. 73 * @param themeName Theme name. 74 * @param data Font data. 75 * @param dataLength The size of font data. 76 * @return A pointer to typeface. 77 */ 78 Typeface* LoadThemeFont(const std::string& familyName, const std::string& themeName, 79 const uint8_t* data, size_t dataLength); 80 81 /* 82 * @brief Load theme font typeface. 83 * @param themeName Theme name. 84 * @param typeface Typeface 85 */ 86 void LoadThemeFont(const std::string& themeName, std::shared_ptr<Drawing::Typeface> typeface); 87 88 /** 89 * @brief Use the system fallback to find a typeface for the given character. 90 * @param familyName A const char array of familyName. 91 * @param fontStyle FontStyle. 92 * @param bcp47 Is a combination of ISO 639, 15924, and 3166-1 codes. 93 * @param bcp47Count Length of bcp47. 94 * @param character The given character. 95 * @return If find, return typeface. else, return nullptr. 96 */ 97 Typeface* MatchFamilyStyleCharacter(const char familyName[], const FontStyle& fontStyle, 98 const char* bcp47[], int bcp47Count, 99 int32_t character) const; 100 101 /** 102 * @brief Find a fontStyleSet for the given familyName. 103 * @param familyName A const char array of familyName. 104 * @return If find, return fontStyleSet. else, return nullptr. 105 */ 106 FontStyleSet* MatchFamily(const char familyName[]) const; 107 108 template<typename T> GetImpl()109 T* GetImpl() const 110 { 111 if (fontMgrImpl_) { 112 return fontMgrImpl_->DowncastingTo<T>(); 113 } 114 return nullptr; 115 } 116 117 /** 118 * @brief Find the corresponding font based on the style and font name 119 * @param familyName The name of the font you want to apply 120 * @param fontStyle The font style you want to achieve 121 * @return Returns the corresponding font 122 */ 123 Typeface* MatchFamilyStyle(const char familyName[], const FontStyle& fontStyle) const; 124 125 int CountFamilies() const; 126 127 void GetFamilyName(int index, std::string& str) const; 128 129 FontStyleSet* CreateStyleSet(int index) const; 130 131 /** 132 * @brief Get the fullname of font 133 * @param fontFd The file descriptor for the font file 134 * @param fullnameVec Read the font fullname list 135 * @return Returns Whether the fullnameVec was successfully obtained, 0 means success, 136 * see FontCheckCode for details 137 */ 138 int GetFontFullName(int fontFd, std::vector<FontByteArray>& fullnameVec); 139 140 /** 141 * @brief Parse the Installed font configuration file and get the font path list 142 * @param configPath The path to the configuration file 143 * @param fontPathVec Read a list of font file paths 144 * @return Returns Whether the configuration file is parsed successfully, 0 means success, 145 * see FontCheckCode for details 146 */ 147 int ParseInstallFontConfig(const std::string& configPath, std::vector<std::string>& fontPathVec); 148 private: 149 std::shared_ptr<FontMgrImpl> fontMgrImpl_; 150 }; 151 } // namespace Drawing 152 } // namespace Rosen 153 } // namespace OHOS 154 #endif