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 TYPEFACE_H 17 #define TYPEFACE_H 18 19 #include <functional> 20 #include <memory> 21 #include <cstdint> 22 23 #include "impl_interface/typeface_impl.h" 24 #include "text/font_arguments.h" 25 #include "text/font_style.h" 26 #include "utils/data.h" 27 #include "utils/memory_stream.h" 28 29 namespace OHOS { 30 namespace Rosen { 31 namespace Drawing { 32 class DRAWING_API Typeface { 33 public: 34 explicit Typeface(std::shared_ptr<TypefaceImpl> typefaceImpl) noexcept; 35 virtual ~Typeface() = default; 36 37 static std::shared_ptr<Typeface> MakeDefault(); 38 static std::shared_ptr<Typeface> MakeFromFile(const char path[], int index = 0); 39 static std::shared_ptr<Typeface> MakeFromFile(const char path[], const FontArguments& fontArguments); 40 static std::shared_ptr<Typeface> MakeFromStream(std::unique_ptr<MemoryStream> memoryStream, int32_t index = 0); 41 static std::shared_ptr<Typeface> MakeFromStream(std::unique_ptr<MemoryStream> memoryStream, 42 const FontArguments& fontArguments); 43 44 static std::shared_ptr<Typeface> MakeFromName(const char familyName[], FontStyle fontStyle); 45 static void RegisterCallBackFunc(std::function<bool(std::shared_ptr<Typeface>)> func); 46 static void UnRegisterCallBackFunc(std::function<bool(std::shared_ptr<Typeface>)> func); 47 static std::function<bool(std::shared_ptr<Typeface>)>& GetTypefaceRegisterCallBack(); 48 static std::function<bool(std::shared_ptr<Typeface>)>& GetTypefaceUnRegisterCallBack(); 49 static std::vector<std::shared_ptr<Typeface>> GetSystemFonts(); 50 51 /** 52 * @brief Get the familyName for this typeface. 53 * @return FamilyName. 54 */ 55 std::string GetFamilyName() const; 56 57 std::string GetFontPath() const; 58 59 /** 60 * @brief Get the fontStyle for this typeface. 61 * @return FontStyle. 62 */ 63 FontStyle GetFontStyle() const; 64 65 /** 66 * @brief Get the size of its contents for the given tag. 67 * @param tag The given table tag. 68 * @return If not present, return 0. 69 */ 70 size_t GetTableSize(uint32_t tag) const; 71 72 /** 73 * @brief Get the size of its contents for the given tag. 74 * @param tag The given table tag. 75 * @param offset The offset in bytes into the table's contents where the copy should start from. 76 * @param length The number of bytes. 77 * @param data Storage address. 78 * @return The number of bytes actually copied into data. 79 */ 80 size_t GetTableData(uint32_t tag, size_t offset, size_t length, void* data) const; 81 82 /** 83 * @brief Get fontStyle is italic. 84 * @return If fontStyle is italic, return true. 85 */ 86 bool GetItalic() const; 87 88 /** 89 * @brief Get a 32bit value for this typeface, unique for the underlying font data. 90 * @return UniqueID. 91 */ 92 uint32_t GetUniqueID() const; 93 94 int32_t GetUnitsPerEm() const; 95 96 std::shared_ptr<Typeface> MakeClone(const FontArguments&) const; 97 98 bool IsCustomTypeface() const; 99 bool IsThemeTypeface() const; 100 101 std::shared_ptr<Data> Serialize() const; 102 static std::shared_ptr<Typeface> Deserialize(const void* data, size_t size); 103 104 template<typename T> GetImpl()105 T* GetImpl() const 106 { 107 if (typefaceImpl_) { 108 return typefaceImpl_->DowncastingTo<T>(); 109 } 110 return nullptr; 111 } 112 113 /** 114 * @brief Get a 32bit hash for this typeface, unique for the underlying font data. 115 * @return process independent hash 116 */ 117 uint32_t GetHash() const; 118 119 /** 120 * @brief Set a 32bit hash for this typeface, unique for the underlying font data. 121 */ 122 void SetHash(uint32_t hash); 123 124 /** 125 * @brief Get serialized data size of typeface. Firstly set size before GetSize(). 126 * @return serialized data size 127 */ 128 uint32_t GetSize(); 129 130 /** 131 * @brief Set serialized data size of typeface. 132 */ 133 void SetSize(uint32_t size); 134 135 private: 136 std::shared_ptr<TypefaceImpl> typefaceImpl_; 137 static std::function<bool(std::shared_ptr<Typeface>)> registerTypefaceCallBack_; 138 static std::function<bool(std::shared_ptr<Typeface>)> unregisterTypefaceCallBack_; 139 uint32_t size_ = 0; 140 }; 141 } // namespace Drawing 142 } // namespace Rosen 143 } // namespace OHOS 144 #endif