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> MakeFromName(const char familyName[], FontStyle fontStyle); 42 static void RegisterCallBackFunc(std::function<bool(std::shared_ptr<Typeface>)> func); 43 static void UnRegisterCallBackFunc(std::function<bool(std::shared_ptr<Typeface>)> func); 44 static std::function<bool(std::shared_ptr<Typeface>)>& GetTypefaceRegisterCallBack(); 45 static std::function<bool(std::shared_ptr<Typeface>)>& GetTypefaceUnRegisterCallBack(); 46 47 /** 48 * @brief Get the familyName for this typeface. 49 * @return FamilyName. 50 */ 51 std::string GetFamilyName() const; 52 53 /** 54 * @brief Get the fontStyle for this typeface. 55 * @return FontStyle. 56 */ 57 FontStyle GetFontStyle() const; 58 59 /** 60 * @brief Get the size of its contents for the given tag. 61 * @param tag The given table tag. 62 * @return If not present, return 0. 63 */ 64 size_t GetTableSize(uint32_t tag) const; 65 66 /** 67 * @brief Get the size of its contents for the given tag. 68 * @param tag The given table tag. 69 * @param offset The offset in bytes into the table's contents where the copy should start from. 70 * @param length The number of bytes. 71 * @param data Storage address. 72 * @return The number of bytes actually copied into data. 73 */ 74 size_t GetTableData(uint32_t tag, size_t offset, size_t length, void* data) const; 75 76 /** 77 * @brief Get fontStyle is italic. 78 * @return If fontStyle is italic, return true. 79 */ 80 bool GetItalic() const; 81 82 /** 83 * @brief Get a 32bit value for this typeface, unique for the underlying font data. 84 * @return UniqueID. 85 */ 86 uint32_t GetUniqueID() const; 87 88 int32_t GetUnitsPerEm() const; 89 90 std::shared_ptr<Typeface> MakeClone(const FontArguments&) const; 91 92 bool IsCustomTypeface() const; 93 94 std::shared_ptr<Data> Serialize() const; 95 static std::shared_ptr<Typeface> Deserialize(const void* data, size_t size); 96 97 template<typename T> GetImpl()98 T* GetImpl() const 99 { 100 if (typefaceImpl_) { 101 return typefaceImpl_->DowncastingTo<T>(); 102 } 103 return nullptr; 104 } 105 106 /** 107 * @brief Get a 32bit hash for this typeface, unique for the underlying font data. 108 * @return process independent hash 109 */ 110 uint32_t GetHash() const; 111 112 /** 113 * @brief Set a 32bit hash for this typeface, unique for the underlying font data. 114 */ 115 void SetHash(uint32_t hash); 116 117 /** 118 * @brief Get serialized data size of typeface. Firstly set size before GetSize(). 119 * @return serialized data size 120 */ 121 uint32_t GetSize(); 122 123 /** 124 * @brief Set serialized data size of typeface. 125 */ 126 void SetSize(uint32_t size); 127 128 private: 129 std::shared_ptr<TypefaceImpl> typefaceImpl_; 130 static std::function<bool(std::shared_ptr<Typeface>)> registerTypefaceCallBack_; 131 static std::function<bool(std::shared_ptr<Typeface>)> unregisterTypefaceCallBack_; 132 uint32_t size_ = 0; 133 }; 134 } // namespace Drawing 135 } // namespace Rosen 136 } // namespace OHOS 137 #endif