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