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