1 /* 2 * Copyright (c) 2020-2021 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_Components 18 * @{ 19 * 20 * @brief Defines UI components such as buttons, texts, images, lists, and progress bars. 21 * 22 * @since 1.0 23 * @version 1.0 24 */ 25 26 /** 27 * @file ui_arc_label.h 28 * 29 * @brief Defines the attributes of an arc label. 30 * 31 * The attributes include the center and radius of an arc, angle range, and text orientation. 32 * 33 * @since 1.0 34 * @version 1.0 35 */ 36 37 #ifndef GRAPHIC_LITE_UI_ARC_LABEL_H 38 #define GRAPHIC_LITE_UI_ARC_LABEL_H 39 40 #include "common/text.h" 41 #include "components/ui_view.h" 42 43 namespace OHOS { 44 /** 45 * @brief Defines functions related to an arc label. 46 * 47 * @since 1.0 48 * @version 1.0 49 */ 50 class UIArcLabel : public UIView { 51 public: 52 /** 53 * @brief A default constructor used to create a <b>UIArcLabel</b> instance. 54 * 55 * @since 1.0 56 * @version 1.0 57 */ 58 UIArcLabel(); 59 60 /** 61 * @brief A destructor used to delete the <b>UIArcLabel</b> instance. 62 * 63 * @since 1.0 64 * @version 1.0 65 */ 66 virtual ~UIArcLabel(); 67 68 /** 69 * @brief Obtains the view type. 70 * 71 * @return Returns <b>UI_ARC_LABEL</b>, as defined in {link UIViewType}. 72 * @since 1.0 73 * @version 1.0 74 */ GetViewType()75 UIViewType GetViewType() const override 76 { 77 return UI_ARC_LABEL; 78 } 79 80 /** 81 * @brief Obtains the width of this arc text. 82 * 83 * @return Returns the width of this arc text. 84 * @since 1.0 85 * @version 1.0 86 */ GetWidth()87 int16_t GetWidth() override 88 { 89 ReMeasure(); 90 return UIView::GetWidth(); 91 } 92 93 /** 94 * @brief Obtains the height of this arc text. 95 * 96 * @return Returns the height of this arc text. 97 * @since 1.0 98 * @version 1.0 99 */ GetHeight()100 int16_t GetHeight() override 101 { 102 ReMeasure(); 103 return UIView::GetHeight(); 104 } 105 106 /** 107 * @brief Sets the view style. 108 * @param style Indicates the view style. 109 * @since 1.0 110 * @version 1.0 111 */ SetStyle(Style & style)112 void SetStyle(Style& style) override 113 { 114 UIView::SetStyle(style); 115 } 116 117 /** 118 * @brief Sets a style. 119 * 120 * @param key Indicates the key of the style to set. 121 * @param value Indicates the value matching the key. 122 * @since 1.0 123 * @version 1.0 124 */ 125 void SetStyle(uint8_t key, int64_t value) override; 126 127 /** 128 * @brief Sets the text content for this arc label. 129 * 130 * @param text Indicates the pointer to the text content. 131 * @since 1.0 132 * @version 1.0 133 */ 134 void SetText(const char* text); 135 136 /** 137 * @brief Obtains the text of this arc label. 138 * 139 * @return Returns the text. 140 * @since 1.0 141 * @version 1.0 142 */ 143 const char* GetText() const; 144 145 /** 146 * @brief Sets the alignment mode for this text. 147 * 148 * @param horizontalAlign Indicates the horizontal alignment mode to set, 149 * which can be {@link TEXT_ALIGNMENT_LEFT}, 150 * {@link TEXT_ALIGNMENT_CENTER}, or {@link TEXT_ALIGNMENT_RIGHT}. 151 * @since 1.0 152 * @version 1.0 153 */ 154 void SetAlign(UITextLanguageAlignment horizontalAlign); 155 156 /** 157 * @brief Obtains the horizontal alignment mode. 158 * 159 * @return Returns the horizontal alignment mode. 160 * @since 1.0 161 * @version 1.0 162 */ 163 UITextLanguageAlignment GetHorAlign(); 164 165 /** 166 * @brief Obtains the direction of this text. 167 * 168 * @return Returns the text direction, as defined in {@link UITextLanguageDirect}. 169 * @since 1.0 170 * @version 1.0 171 */ 172 UITextLanguageDirect GetDirect(); 173 174 /** 175 * @brief Sets the font ID for this arc label. 176 * 177 * @param fontId Indicates the font ID composed of font name and size. 178 * @since 1.0 179 * @version 1.0 180 */ 181 void SetFontId(uint16_t fontId); 182 183 /** 184 * @brief Obtains the font ID composed of font name and size. 185 * 186 * @return Returns the front ID of this arc label. 187 * @since 1.0 188 * @version 1.0 189 */ 190 uint16_t GetFontId(); 191 192 /** 193 * @brief Sets the font for this arc label. 194 * 195 * @param name Indicates the pointer to the font name. 196 * @param size Indicates the font size to set. 197 * @since 1.0 198 * @version 1.0 199 */ 200 void SetFont(const char* name, uint8_t size); 201 202 /** 203 * @brief Sets the center position for this arc text. 204 * 205 * @param x Indicates the x-coordinate to set. 206 * @param y Indicates the y-coordinate to set. 207 * @since 1.0 208 * @version 1.0 209 */ SetArcTextCenter(int16_t x,int16_t y)210 void SetArcTextCenter(int16_t x, int16_t y) 211 { 212 if ((arcCenter_.x != x) || (arcCenter_.y != y)) { 213 arcCenter_.x = x; 214 arcCenter_.y = y; 215 RefreshArcLabel(); 216 } 217 } 218 219 /** 220 * @brief Obtains the center position of this arc text. 221 * 222 * @return Returns the center position of this arc text. 223 * @since 1.0 224 * @version 1.0 225 */ GetArcTextCenter()226 Point GetArcTextCenter() const 227 { 228 return arcCenter_; 229 } 230 231 /** 232 * @brief Sets the radius for this arc text. 233 * 234 * @param radius Indicates the radius to set. 235 * @since 1.0 236 * @version 1.0 237 */ SetArcTextRadius(uint16_t radius)238 void SetArcTextRadius(uint16_t radius) 239 { 240 if (radius_ != radius) { 241 radius_ = radius; 242 RefreshArcLabel(); 243 } 244 } 245 246 /** 247 * @brief Obtains the radius of this arc text. 248 * 249 * @return Returns the radius of this arc text. 250 * @since 1.0 251 * @version 1.0 252 */ GetArcTextRadius()253 uint16_t GetArcTextRadius() const 254 { 255 return radius_; 256 } 257 258 /** 259 * @brief Sets the start angle and end angle for this arc text. 260 * 261 * The angle in 12 o'clock direction is 0 degrees, and the value increases clockwise. 262 * The text direction is clockwise when the end angle is greater than the start angle, and the text direction is 263 * counterclockwise otherwise. 264 * 265 * @param startAngle Indicates the start angle to set. 266 * @param endAngle Indicates the end angle to set. 267 * @since 1.0 268 * @version 1.0 269 */ SetArcTextAngle(int16_t startAngle,int16_t endAngle)270 void SetArcTextAngle(int16_t startAngle, int16_t endAngle) 271 { 272 if ((startAngle_ != startAngle) || (endAngle_ != endAngle)) { 273 startAngle_ = startAngle; 274 endAngle_ = endAngle; 275 RefreshArcLabel(); 276 } 277 } 278 279 /** 280 * @brief Obtains the start angle of this arc text. 281 * 282 * @return Returns the start angle of this arc text. 283 * @since 1.0 284 * @version 1.0 285 */ GetArcTextStartAngle()286 int16_t GetArcTextStartAngle() const 287 { 288 return startAngle_; 289 } 290 291 /** 292 * @brief Obtains the end angle of this arc text. 293 * 294 * @return Returns the end angle of this arc text. 295 * @since 1.0 296 * @version 1.0 297 */ GetArcTextEndAngle()298 int16_t GetArcTextEndAngle() const 299 { 300 return endAngle_; 301 } 302 303 /** 304 * @brief Sets the orientation for this arc text. 305 * 306 * @param orientation Indicates the text orientation to set. 307 * @since 1.0 308 * @version 1.0 309 */ SetArcTextOrientation(TextOrientation orientation)310 void SetArcTextOrientation(TextOrientation orientation) 311 { 312 if (orientation_ != orientation) { 313 orientation_ = orientation; 314 RefreshArcLabel(); 315 } 316 } 317 318 /** 319 * @brief Obtains the orientation of this arc text. 320 * 321 * @return Returns the orientation of this arc text. 322 * @since 1.0 323 * @version 1.0 324 */ GetArcTextOrientation()325 TextOrientation GetArcTextOrientation() const 326 { 327 return orientation_; 328 } 329 330 /** 331 * @brief Draws an arc text. 332 * 333 * @param invalidatedArea Indicates the area to draw. 334 * @since 1.0 335 * @version 1.0 336 */ 337 void OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override; 338 339 protected: 340 Text* arcLabelText_; 341 InitArcLabelText()342 virtual void InitArcLabelText() 343 { 344 if (arcLabelText_ == nullptr) { 345 arcLabelText_ = new Text(); 346 if (arcLabelText_ == nullptr) { 347 GRAPHIC_LOGE("new Text fail"); 348 return; 349 } 350 } 351 } 352 void RefreshArcLabel(); 353 354 virtual void DrawArcText(BufferInfo& gfxDstBuffer, 355 const Rect& mask, 356 OpacityType opaScale, 357 const ArcTextInfo arcTextInfo, 358 TextOrientation orientation); 359 360 virtual Rect GetArcTextRect(const char* text, 361 uint16_t fontId, 362 uint8_t fontSize, 363 const Point& arcCenter, 364 int16_t letterSpace, 365 TextOrientation orientation, 366 const ArcTextInfo& arcTextInfo); 367 368 private: 369 void ReMeasure() override; 370 void MeasureArcTextInfo(); 371 372 bool needRefresh_; 373 Point textSize_; 374 uint16_t radius_; 375 int16_t startAngle_; 376 int16_t endAngle_; 377 Point arcCenter_; 378 TextOrientation orientation_; 379 380 ArcTextInfo arcTextInfo_; 381 }; 382 } // namespace OHOS 383 #endif // GRAPHIC_LITE_UI_ARC_LABEL_H 384