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