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 <atomic> 20 #include <cstdint> 21 #include <memory> 22 23 #include "draw/path.h" 24 #include "impl_interface/text_blob_impl.h" 25 #include "text/font.h" 26 #include "text/font_types.h" 27 #include "text/rs_xform.h" 28 #include "utils/data.h" 29 30 namespace OHOS { 31 namespace Rosen { 32 namespace Drawing { 33 enum class TextContrast { 34 FOLLOW_SYSTEM, // Follow the system configuration. 35 DISABLE_CONTRAST, // Do not follow the system, APP prohibits high contrast. 36 ENABLE_CONTRAST // Do not follow the system, APP enables high contrast. 37 }; 38 39 class DRAWING_API ProcessTextConstrast { 40 public: Instance()41 static ProcessTextConstrast& Instance() { 42 static ProcessTextConstrast instance; 43 return instance; 44 } 45 SetTextContrast(TextContrast contrast)46 void SetTextContrast(TextContrast contrast) { 47 textContrast_.store(contrast); 48 } 49 GetTextContrast()50 TextContrast GetTextContrast() { 51 return textContrast_.load(); 52 } 53 private: 54 ProcessTextConstrast() = default; 55 ~ProcessTextConstrast() = default; 56 ProcessTextConstrast(const ProcessTextConstrast&) = delete; 57 ProcessTextConstrast(const ProcessTextConstrast&&) = delete; 58 ProcessTextConstrast& operator=(const ProcessTextConstrast&) = delete; 59 ProcessTextConstrast& operator=(const ProcessTextConstrast&&) = delete; 60 61 std::atomic<TextContrast> textContrast_ {TextContrast::FOLLOW_SYSTEM}; 62 }; 63 class DRAWING_API TextBlob { 64 public: 65 explicit TextBlob(std::shared_ptr<TextBlobImpl> textBlobImpl) noexcept; 66 virtual ~TextBlob() = default; 67 68 static std::shared_ptr<TextBlob> MakeFromText(const void* text, size_t byteLength, 69 const Font& font, TextEncoding encoding = TextEncoding::UTF8); 70 static std::shared_ptr<TextBlob> MakeFromPosText(const void* text, size_t byteLength, 71 const Point pos[], const Font& font, TextEncoding encoding = TextEncoding::UTF8); 72 static std::shared_ptr<TextBlob> MakeFromString(const char* str, 73 const Font& font, TextEncoding encoding = TextEncoding::UTF8); 74 static std::shared_ptr<TextBlob> MakeFromRSXform(const void* text, size_t byteLength, 75 const RSXform xform[], const Font& font, TextEncoding encoding = TextEncoding::UTF8); 76 int GetIntercepts(const float bounds[], float intervals[], const Paint* paint); 77 78 /** 79 * @brief Serialize TextBlob. 80 * @param ctx Serialize context. 81 * @return A shared point to serialized data. 82 */ 83 std::shared_ptr<Data> Serialize(void* ctx) const; 84 85 /** 86 * @brief Deserialize TextBlob. 87 * @param data Serialized data. 88 * @param size Data size. 89 * @param ctx Deserialize context. 90 * @return A shared point to deserialized data. 91 */ 92 static std::shared_ptr<TextBlob> Deserialize(const void* data, size_t size, void* ctx); 93 static void GetDrawingGlyphIDforTextBlob(const TextBlob* blob, std::vector<uint16_t>& glyphIds); 94 static Path GetDrawingPathforTextBlob(uint16_t glyphId, const TextBlob* blob); 95 static void GetDrawingPointsForTextBlob(const TextBlob* blob, std::vector<Point>& points); 96 97 template<typename T> GetImpl()98 T* GetImpl() const 99 { 100 if (textBlobImpl_) { 101 return textBlobImpl_->DowncastingTo<T>(); 102 } 103 return nullptr; 104 } 105 106 std::shared_ptr<Rect> Bounds() const; 107 108 uint32_t UniqueID() const; 109 IsEmoji()110 bool IsEmoji() { 111 return this->isEmoji; 112 } 113 SetEmoji(bool emoji)114 void SetEmoji(bool emoji) { 115 this->isEmoji = emoji; 116 } 117 SetTextContrast(TextContrast contrast)118 void SetTextContrast(TextContrast contrast) { 119 textContrast_ = contrast; 120 } 121 GetTextContrast()122 TextContrast GetTextContrast() const { 123 return textContrast_; 124 } 125 126 class Context { 127 public: Context(std::shared_ptr<Typeface> typeface,bool isCustomTypeface)128 explicit Context(std::shared_ptr<Typeface> typeface, bool isCustomTypeface) noexcept 129 : typeface_(typeface), isCustomTypeface_(isCustomTypeface) {} 130 GetTypeface()131 std::shared_ptr<Typeface>& GetTypeface() 132 { 133 return typeface_; 134 } 135 SetTypeface(std::shared_ptr<Typeface> typeface)136 void SetTypeface(std::shared_ptr<Typeface> typeface) 137 { 138 typeface_ = typeface; 139 } 140 IsCustomTypeface()141 bool IsCustomTypeface() 142 { 143 return isCustomTypeface_; 144 } 145 SetIsCustomTypeface(bool isCustomTypeface)146 void SetIsCustomTypeface(bool isCustomTypeface) 147 { 148 isCustomTypeface_ = isCustomTypeface; 149 } 150 private: 151 std::shared_ptr<Typeface> typeface_ = nullptr; 152 bool isCustomTypeface_ = false; 153 }; 154 155 private: 156 std::shared_ptr<TextBlobImpl> textBlobImpl_; 157 bool isEmoji = false; 158 TextContrast textContrast_ = TextContrast::FOLLOW_SYSTEM; 159 }; 160 } // namespace Drawing 161 } // namespace Rosen 162 } // namespace OHOS 163 #endif