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