1 /*
2  * Copyright (c) 2023-2024 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 TEXT_BLOB_H
17 #define TEXT_BLOB_H
18 
19 #include <cstdint>
20 #include <memory>
21 
22 #include "draw/path.h"
23 #include "impl_interface/text_blob_impl.h"
24 #include "text/font.h"
25 #include "text/font_types.h"
26 #include "text/rs_xform.h"
27 #include "utils/data.h"
28 
29 namespace OHOS {
30 namespace Rosen {
31 namespace Drawing {
32 class DRAWING_API TextBlob {
33 public:
34     explicit TextBlob(std::shared_ptr<TextBlobImpl> textBlobImpl) noexcept;
35     virtual ~TextBlob() = default;
36 
37     static std::shared_ptr<TextBlob> MakeFromText(const void* text, size_t byteLength,
38         const Font& font, TextEncoding encoding = TextEncoding::UTF8);
39     static std::shared_ptr<TextBlob> MakeFromPosText(const void* text, size_t byteLength,
40         const Point pos[], const Font& font, TextEncoding encoding = TextEncoding::UTF8);
41     static std::shared_ptr<TextBlob> MakeFromString(const char* str,
42         const Font& font, TextEncoding encoding = TextEncoding::UTF8);
43     static std::shared_ptr<TextBlob> MakeFromRSXform(const void* text, size_t byteLength,
44         const RSXform xform[], const Font& font, TextEncoding encoding = TextEncoding::UTF8);
45     int GetIntercepts(const float bounds[], float intervals[], const Paint* paint);
46 
47     /**
48      * @brief   Serialize TextBlob.
49      * @param ctx  Serialize context.
50      * @return  A shared point to serialized data.
51      */
52     std::shared_ptr<Data> Serialize(void* ctx) const;
53 
54     /**
55      * @brief       Deserialize TextBlob.
56      * @param data  Serialized data.
57      * @param size  Data size.
58      * @param ctx   Deserialize context.
59      * @return      A shared point to deserialized data.
60      */
61     static std::shared_ptr<TextBlob> Deserialize(const void* data, size_t size, void* ctx);
62     static void GetDrawingGlyphIDforTextBlob(const TextBlob* blob, std::vector<uint16_t>& glyphIds);
63     static Path GetDrawingPathforTextBlob(uint16_t glyphId, const TextBlob* blob);
64     static void GetDrawingPointsForTextBlob(const TextBlob* blob, std::vector<Point>& points);
65 
66     template<typename T>
GetImpl()67     T* GetImpl() const
68     {
69         if (textBlobImpl_) {
70             return textBlobImpl_->DowncastingTo<T>();
71         }
72         return nullptr;
73     }
74 
75     std::shared_ptr<Rect> Bounds() const;
76 
77     uint32_t UniqueID() const;
78 
IsEmoji()79     bool IsEmoji() {
80         return this->isEmoji;
81     }
82 
SetEmoji(bool emoji)83     void SetEmoji(bool emoji) {
84         this->isEmoji = emoji;
85     }
86 
87     class Context {
88     public:
Context(std::shared_ptr<Typeface> typeface,bool isCustomTypeface)89         explicit Context(std::shared_ptr<Typeface> typeface, bool isCustomTypeface) noexcept
90             : typeface_(typeface), isCustomTypeface_(isCustomTypeface) {}
91 
GetTypeface()92         std::shared_ptr<Typeface>& GetTypeface()
93         {
94             return typeface_;
95         }
96 
SetTypeface(std::shared_ptr<Typeface> typeface)97         void SetTypeface(std::shared_ptr<Typeface> typeface)
98         {
99             typeface_ = typeface;
100         }
101 
IsCustomTypeface()102         bool IsCustomTypeface()
103         {
104             return isCustomTypeface_;
105         }
106 
SetIsCustomTypeface(bool isCustomTypeface)107         void SetIsCustomTypeface(bool isCustomTypeface)
108         {
109             isCustomTypeface_ = isCustomTypeface;
110         }
111     private:
112         std::shared_ptr<Typeface> typeface_ = nullptr;
113         bool isCustomTypeface_ = false;
114     };
115 
116 private:
117     std::shared_ptr<TextBlobImpl> textBlobImpl_;
118     bool isEmoji = false;
119 };
120 } // namespace Drawing
121 } // namespace Rosen
122 } // namespace OHOS
123 #endif