1 /* 2 * Copyright (c) 2020-2022 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 /** 17 * @addtogroup UI_Common 18 * @{ 19 * 20 * @brief Defines common UI capabilities, such as image and text processing. 21 * 22 * @since 1.0 23 * @version 1.0 24 */ 25 26 /** 27 * @file text.h 28 * 29 * @brief Declares the <b>Text</b> class that provides functions to set basic text attributes, such as the text 30 * direction and alignment mode. 31 * 32 * @since 1.0 33 * @version 1.0 34 */ 35 36 #ifndef GRAPHIC_LITE_TEXT_H 37 #define GRAPHIC_LITE_TEXT_H 38 39 #include "gfx_utils/geometry2d.h" 40 #include "gfx_utils/graphic_types.h" 41 #include "gfx_utils/list.h" 42 #include "gfx_utils/style.h" 43 #include "gfx_utils/vector.h" 44 #include "engines/gfx/gfx_engine_manager.h" 45 #include "font/ui_font_header.h" 46 #if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING 47 #include "common/spannable_string.h" 48 #endif 49 50 namespace OHOS { 51 /** 52 * @brief Enumerates text alignment modes. 53 */ 54 enum UITextLanguageAlignment : uint8_t { 55 /** Left-aligned */ 56 TEXT_ALIGNMENT_LEFT = 0, 57 /** Right-aligned */ 58 TEXT_ALIGNMENT_RIGHT, 59 /** Centered */ 60 TEXT_ALIGNMENT_CENTER, 61 /** Top-aligned */ 62 TEXT_ALIGNMENT_TOP, 63 /** Bottom-aligned */ 64 TEXT_ALIGNMENT_BOTTOM, 65 }; 66 67 /** 68 * @brief Enumerates text directions. 69 */ 70 enum UITextLanguageDirect : uint8_t { 71 /** Left-to-right */ 72 TEXT_DIRECT_LTR = 0, 73 /** Right-to-left */ 74 TEXT_DIRECT_RTL, 75 TEXT_DIRECT_MIXED, 76 }; 77 78 /** 79 * @brief Stores the attribute information about this arc text to draw. 80 */ 81 struct ArcTextInfo { 82 uint16_t radius; 83 float startAngle; 84 Point arcCenter; 85 uint32_t lineStart; 86 uint32_t lineEnd; 87 UITextLanguageDirect direct; 88 uint32_t* codePoints; 89 uint16_t codePointsNum; 90 uint8_t shapingFontId; 91 }; 92 93 /** 94 * @brief Enumerates text orientations. 95 */ 96 enum class TextOrientation : uint8_t { 97 /** Inside */ 98 INSIDE, 99 /** Outside */ 100 OUTSIDE, 101 }; 102 103 struct BackgroundColor : public HeapBase { 104 int16_t start; 105 int16_t end; 106 ColorType backgroundColor; 107 }; 108 109 struct ForegroundColor : public HeapBase { 110 int16_t start; 111 int16_t end; 112 ColorType fontColor; 113 }; 114 115 struct LineBackgroundColor : public HeapBase { 116 int16_t start; 117 int16_t end; 118 ColorType linebackgroundColor; 119 }; 120 121 struct SizeSpan { 122 bool isSizeSpan; 123 uint8_t size; 124 uint16_t fontId; 125 int16_t height; 126 }; 127 128 struct LabelLineInfo; 129 130 /** 131 * @brief Represents the base class of <b>Text</b>, providing the text attribute setting and text drawing 132 * capabilities for components that require font display. 133 * 134 * @since 1.0 135 * @version 1.0 136 */ 137 class Text : public HeapBase { 138 public: 139 /** Invalid value for the ellipsis position */ 140 static constexpr uint16_t TEXT_ELLIPSIS_END_INV = 0xFFFF; 141 static constexpr uint16_t TEXT_ELLIPSIS_UNICODE = 0x2026; 142 143 /** 144 * @brief A constructor used to create a <b>Text</b> instance. 145 * 146 * @since 1.0 147 * @version 1.0 148 */ 149 Text(); 150 151 /** 152 * @brief A destructor used to delete the <b>Text</b> instance. 153 * 154 * @since 1.0 155 * @version 1.0 156 */ 157 virtual ~Text(); 158 159 /** 160 * @brief Sets the content for this text. 161 * 162 * @param text Indicates the pointer to the text content. 163 * @since 1.0 164 * @version 1.0 165 */ 166 virtual void SetText(const char* text); 167 168 #if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING 169 /** 170 * @brief Sets the SpannableString for this text. 171 * 172 * @param text Indicates the pointer to the text content. 173 * @since 1.0 174 * @version 1.0 175 */ 176 void SetSpannableString(const SpannableString* spannableString); 177 #endif 178 /** 179 * @brief Obtains the content of this text. 180 * 181 * @return Returns the text content. 182 * @since 1.0 183 * @version 1.0 184 */ GetText()185 const char* GetText() const 186 { 187 return text_; 188 } 189 190 /** 191 * @brief Sets the font name and size. 192 * 193 * @param name Indicates the pointer to the font name. 194 * @param size Indicates the font size to set. 195 * @since 1.0 196 * @version 1.0 197 */ 198 void SetFont(const char* name, uint8_t size); 199 200 static void SetFont(const char* name, uint8_t size, char*& destName, uint8_t& destSize); 201 202 /** 203 * @brief Sets the font ID. 204 * 205 * @param fontId Indicates the font ID to set. 206 * @since 1.0 207 * @version 1.0 208 */ 209 void SetFontId(uint16_t fontId); 210 211 /** 212 * @brief Obtains the font ID. 213 * 214 * @return Returns the front ID. 215 * @since 1.0 216 * @version 1.0 217 */ GetFontId()218 uint16_t GetFontId() const 219 { 220 return fontId_; 221 } 222 223 /** 224 * @brief Obtains the font size. 225 * 226 * @return Returns the front size. 227 * @since 1.0 228 * @version 1.0 229 */ GetFontSize()230 uint8_t GetFontSize() const 231 { 232 return fontSize_; 233 } 234 235 /** 236 * @brief Sets the direction for this text. 237 * 238 * @param direct Indicates the text direction, as defined in {@link UITextLanguageDirect}. 239 * @since 1.0 240 * @version 1.0 241 */ SetDirect(UITextLanguageDirect direct)242 void SetDirect(UITextLanguageDirect direct) 243 { 244 direct_ = direct; 245 } 246 247 /** 248 * @brief Obtains the direction of this text. 249 * 250 * @return Returns the text direction, as defined in {@link UITextLanguageDirect}. 251 * @since 1.0 252 * @version 1.0 253 */ GetDirect()254 UITextLanguageDirect GetDirect() const 255 { 256 return static_cast<UITextLanguageDirect>(direct_); 257 } 258 259 /** 260 * @brief Sets the alignment mode for this text. 261 * 262 * @param horizontalAlign Indicates the horizontal alignment mode to set, 263 * which can be {@link TEXT_ALIGNMENT_LEFT}, 264 * {@link TEXT_ALIGNMENT_CENTER}, or {@link TEXT_ALIGNMENT_RIGHT}. 265 * @param verticalAlign Indicates the vertical alignment mode to set, which can be 266 * {@link TEXT_ALIGNMENT_TOP} (default mode), {@link TEXT_ALIGNMENT_CENTER}, 267 * or {@link TEXT_ALIGNMENT_BOTTOM}. 268 * @since 1.0 269 * @version 1.0 270 */ 271 void SetAlign(UITextLanguageAlignment horizontalAlign, UITextLanguageAlignment verticalAlign = TEXT_ALIGNMENT_TOP) 272 { 273 if ((horizontalAlign_ != horizontalAlign) || (verticalAlign_ != verticalAlign)) { 274 needRefresh_ = true; 275 horizontalAlign_ = horizontalAlign; 276 verticalAlign_ = verticalAlign; 277 } 278 } 279 280 /** 281 * @brief Obtains the horizontal alignment mode. 282 * 283 * @return Returns the horizontal alignment mode. 284 * @since 1.0 285 * @version 1.0 286 */ GetHorAlign()287 UITextLanguageAlignment GetHorAlign() const 288 { 289 return static_cast<UITextLanguageAlignment>(horizontalAlign_); 290 } 291 292 /** 293 * @brief Obtains the vertical alignment mode. 294 * 295 * @return Returns the vertical alignment mode. 296 * @since 1.0 297 * @version 1.0 298 */ GetVerAlign()299 UITextLanguageAlignment GetVerAlign() const 300 { 301 return static_cast<UITextLanguageAlignment>(verticalAlign_); 302 } 303 304 /** 305 * @brief Obtains the size of this text. 306 * 307 * @return Returns the text size. 308 * @since 1.0 309 * @version 1.0 310 */ GetTextSize()311 Point GetTextSize() const 312 { 313 return textSize_; 314 } 315 316 virtual void ReMeasureTextSize(const Rect& textRect, const Style& style); 317 318 void ReMeasureTextWidthInEllipsisMode(const Rect& textRect, const Style& style, uint16_t ellipsisIndex); 319 320 void OnDraw(BufferInfo& gfxDstBuffer, 321 const Rect& invalidatedArea, 322 const Rect& viewOrigRect, 323 const Rect& textRect, 324 int16_t offsetX, 325 const Style& style, 326 uint16_t ellipsisIndex, 327 OpacityType opaScale); 328 329 /** 330 * @brief Sets whether to adapt the component width to this text. 331 * 332 * @param expand Specifies whether to adapt the component width to this text. The value <b>true</b> indicates 333 * that the component width will adapt to this text, and <b>false</b> indicates not. 334 * @since 1.0 335 * @version 1.0 336 */ SetExpandWidth(bool expand)337 void SetExpandWidth(bool expand) 338 { 339 expandWidth_ = expand; 340 } 341 342 /** 343 * @brief Checks whether the component width adapts to this text. 344 * 345 * @return Returns <b>true</b> if the component width adapts to this text; returns <b>false</b> otherwise. 346 * @since 1.0 347 * @version 1.0 348 */ IsExpandWidth()349 bool IsExpandWidth() const 350 { 351 return expandWidth_; 352 } 353 354 /** 355 * @brief Sets whether to adapt the component height to this text. 356 * 357 * @param expand Specifies whether to adapt the component height to this text. The value <b>true</b> indicates 358 * that the component height will adapt to this text, and <b>false</b> indicates not. 359 * @since 1.0 360 * @version 1.0 361 */ SetExpandHeight(bool expand)362 void SetExpandHeight(bool expand) 363 { 364 expandHeight_ = expand; 365 } 366 367 /** 368 * @brief Checks whether the component height adapts to this text. 369 * 370 * @return Returns <b>true</b> if the component height adapts to this text; returns <b>false</b> otherwise. 371 * @since 1.0 372 * @version 1.0 373 */ IsExpandHeight()374 bool IsExpandHeight() const 375 { 376 return expandHeight_; 377 } 378 IsNeedRefresh()379 bool IsNeedRefresh() const 380 { 381 return needRefresh_; 382 } 383 384 /** 385 * @brief Obtains the index of the character from where text will be replaced by ellipses based on 386 * the text rectangle and style. 387 * 388 * @param textRect Indicates the text rectangle. 389 * @param style Indicates the text style. 390 * @since 1.0 391 * @version 1.0 392 */ 393 uint16_t GetEllipsisIndex(const Rect& textRect, const Style& style); 394 395 /** 396 * @brief Get the GetShapingFontId of text 397 * 398 * @return Return ShapingFontId 399 */ GetShapingFontId()400 virtual uint8_t GetShapingFontId() const 401 { 402 return 0; 403 } 404 405 /** 406 * @brief Get the GetCodePointNum of text 407 * 408 * @return Return num of CodePoints 409 */ GetCodePointNum()410 virtual uint16_t GetCodePointNum() const 411 { 412 return 0; 413 } 414 415 /** 416 * @brief Get the GetCodePoints of text 417 * 418 * @return Return CodePoints of text 419 */ GetCodePoints()420 virtual uint32_t* GetCodePoints() const 421 { 422 return nullptr; 423 } 424 SetSupportBaseLine(bool baseLine)425 void SetSupportBaseLine(bool baseLine) 426 { 427 baseLine_ = baseLine; 428 } 429 SetBackgroundColorSpan(ColorType backgroundColor,int16_t start,int16_t end)430 void SetBackgroundColorSpan(ColorType backgroundColor, int16_t start, int16_t end) 431 { 432 BackgroundColor bgcolor; 433 bgcolor.start = start; 434 bgcolor.end = end; 435 bgcolor.backgroundColor= backgroundColor; 436 backgroundColor_.PushBack(bgcolor); 437 } 438 GetBackgroundColorSpan()439 List<BackgroundColor> GetBackgroundColorSpan() 440 { 441 return backgroundColor_; 442 } 443 SetForegroundColorSpan(ColorType fontColor,int16_t start,int16_t end)444 void SetForegroundColorSpan(ColorType fontColor, int16_t start, int16_t end) 445 { 446 ForegroundColor fColor; 447 fColor.start = start; 448 fColor.end = end; 449 fColor.fontColor= fontColor; 450 foregroundColor_.PushBack(fColor); 451 } 452 GetForegroundColorSpan()453 List<ForegroundColor> GetForegroundColorSpan() 454 { 455 return foregroundColor_; 456 } 457 SetLineBackgroundSpan(ColorType linebackgroundColor,int16_t start,int16_t end)458 void SetLineBackgroundSpan(ColorType linebackgroundColor, int16_t start, int16_t end) 459 { 460 LineBackgroundColor lineBackground; 461 lineBackground.start = start; 462 lineBackground.end = end; 463 lineBackground.linebackgroundColor= linebackgroundColor; 464 linebackgroundColor_.PushBack(lineBackground); 465 } 466 GetLineBackgroundSpan()467 List<LineBackgroundColor> GetLineBackgroundSpan() 468 { 469 return linebackgroundColor_; 470 } 471 472 void SetAbsoluteSizeSpan(uint16_t start, uint16_t end, uint8_t size); 473 void SetRelativeSizeSpan(uint16_t start, uint16_t end, float size); 474 GetSizeSpan()475 uint16_t GetSizeSpan() 476 { 477 return characterSize_; 478 } 479 480 protected: 481 struct TextLine { 482 uint16_t lineBytes; 483 uint16_t linePixelWidth; 484 }; 485 486 /** Maximum number of lines */ 487 static constexpr uint16_t MAX_LINE_COUNT = 50; 488 static TextLine textLine_[MAX_LINE_COUNT]; 489 490 static constexpr const char* TEXT_ELLIPSIS = "…"; 491 492 virtual uint32_t GetTextStrLen(); 493 494 virtual uint32_t 495 GetTextLine(uint32_t begin, uint32_t textLen, int16_t width, uint16_t lineNum, uint8_t letterSpace, 496 uint16_t& letterIndex, SizeSpan* sizeSpans); 497 498 virtual uint16_t GetLetterIndexByPosition(const Rect& textRect, const Style& style, const Point& pos); 499 500 virtual void Draw(BufferInfo& gfxDstBuffer, 501 const Rect& mask, 502 const Rect& coords, 503 const Style& style, 504 int16_t offsetX, 505 uint16_t ellipsisIndex, 506 OpacityType opaScale); 507 508 uint16_t GetLine(int16_t width, uint8_t letterSpace, uint16_t ellipsisIndex, uint32_t& maxLineBytes); 509 int16_t TextPositionY(const Rect& textRect, int16_t textHeight); 510 int16_t LineStartPos(const Rect& textRect, uint16_t lineWidth); 511 void DrawEllipsis(BufferInfo& gfxDstBuffer, LabelLineInfo& labelLine, uint16_t& letterIndex); 512 uint32_t CalculateLineWithEllipsis(uint32_t begin, uint32_t textLen, int16_t width, 513 uint8_t letterSpace, uint16_t& lineNum, 514 uint16_t& letterIndex, 515 SizeSpan* sizeSpans); 516 uint16_t GetSpanFontIdBySize(uint8_t size); 517 void InitSizeSpans(); 518 #if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING 519 TextStyle* textStyles_; 520 #endif 521 char* text_; 522 uint16_t fontId_; 523 uint8_t fontSize_; // Only the vector font library has a valid value. 524 Point textSize_; 525 bool needRefresh_ : 1; 526 bool expandWidth_ : 1; 527 bool expandHeight_ : 1; 528 bool baseLine_ : 1; 529 uint8_t direct_ : 4; // UITextLanguageDirect 530 List<BackgroundColor> backgroundColor_; 531 List<ForegroundColor> foregroundColor_; 532 List<LineBackgroundColor> linebackgroundColor_; 533 SizeSpan* sizeSpans_; 534 uint32_t characterSize_; 535 536 private: 537 uint8_t horizontalAlign_ : 4; // UITextLanguageAlignment 538 uint8_t verticalAlign_ : 4; // UITextLanguageAlignment 539 540 static constexpr uint8_t FONT_ID_MAX = 0xFF; 541 }; 542 } // namespace OHOS 543 #endif // GRAPHIC_LITE_TEXT_H 544