• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "rs_graphic_test_text.h"
17 
18 #include "draw/brush.h"
19 #include "modifier/rs_extended_modifier.h"
20 #include "modifier/rs_property_modifier.h"
21 #include "text/font.h"
22 #include "text/font_types.h"
23 #include "text/text_blob.h"
24 
25 namespace OHOS {
26 namespace Rosen {
SetPosition(Vector2f position)27 void TextCustomModifier::SetPosition(Vector2f position)
28 {
29     if (position_ == nullptr) {
30         position_ = std::make_shared<RSProperty<Vector2f>>(position);
31         AttachProperty(position_);
32     } else {
33         position_->Set(position);
34     }
35 }
36 
SetBrushColor(uint32_t color)37 void TextCustomModifier::SetBrushColor(uint32_t color)
38 {
39     if (color_ == nullptr) {
40         color_ = std::make_shared<RSProperty<uint32_t>>(color);
41         AttachProperty(color_);
42     } else {
43         color_->Set(color);
44     }
45 }
46 
SetFontSize(float size)47 void TextCustomModifier::SetFontSize(float size)
48 {
49     if (size_ == nullptr) {
50         size_ = std::make_shared<RSProperty<float>>(size);
51         AttachProperty(size_);
52     } else {
53         size_->Set(size);
54     }
55 }
56 
SetText(std::string text)57 void TextCustomModifier::SetText(std::string text)
58 {
59     if (text_ == nullptr) {
60         text_ = std::make_shared<RSProperty<std::string>>(text);
61         AttachProperty(text_);
62     } else {
63         text_->Set(text);
64     }
65 }
66 
Draw(RSDrawingContext & context) const67 void TextCustomModifier::Draw(RSDrawingContext& context) const
68 {
69     if (text_ == nullptr)
70         return;
71     uint32_t colorInt = 0xff000000;
72     Vector2f position = { 0.0, 0.0 };
73     float fontSize = 16;
74     if (color_ != nullptr)
75         colorInt = color_->Get();
76     if (position_ != nullptr)
77         position = position_->Get();
78     if (size_ != nullptr)
79         fontSize = size_->Get();
80     auto brushColor = OHOS::Rosen::Drawing::Color(colorInt);
81     Drawing::Brush brush;
82     brush.SetColor(brushColor);
83     brush.SetAntiAlias(true);
84 
85     Drawing::Font font = Drawing::Font();
86     font.SetSize(fontSize); // text size 16
87     std::shared_ptr<Drawing::TextBlob> scaleInfoTextBlob =
88         Drawing::TextBlob::MakeFromString(text_->Get().c_str(), font, Drawing::TextEncoding::UTF8);
89     context.canvas->AttachBrush(brush);
90     context.canvas->DrawTextBlob(scaleInfoTextBlob.get(), position[0], position[1]);
91     context.canvas->DetachBrush();
92 }
93 } // namespace Rosen
94 } // namespace OHOS