Skip to main content

ohos_arkui_binding/component/attribute/
font.rs

1//! Module component::attribute::font wrappers and related types.
2
3use crate::{
4    ArkUINodeAttributeItem, ArkUINodeAttributeNumber, ArkUIResult, ARK_UI_NATIVE_NODE_API_1,
5};
6
7use super::ArkUIAttributeBasic;
8
9/// Font-related attribute helpers shared by text-capable components.
10pub trait ArkUICommonFontAttribute: ArkUIAttributeBasic {
11    fn font_size(&self, font_size: f32) -> ArkUIResult<()> {
12        let font_size_property =
13            ArkUINodeAttributeItem::NumberValue(vec![ArkUINodeAttributeNumber::Float(font_size)]);
14        ARK_UI_NATIVE_NODE_API_1.with(|api| {
15            api.set_attribute(
16                self.raw(),
17                crate::ArkUINodeAttributeType::FontSize,
18                font_size_property,
19            )
20        })?;
21        Ok(())
22    }
23
24    fn get_font_size(&self) -> ArkUIResult<ArkUINodeAttributeItem> {
25        ARK_UI_NATIVE_NODE_API_1
26            .with(|api| api.get_attribute(self.raw(), crate::ArkUINodeAttributeType::FontSize))
27    }
28
29    fn font_color(&self, font_color: u32) -> ArkUIResult<()> {
30        let font_color_property =
31            ArkUINodeAttributeItem::NumberValue(vec![ArkUINodeAttributeNumber::Uint(font_color)]);
32        ARK_UI_NATIVE_NODE_API_1.with(|api| {
33            api.set_attribute(
34                self.raw(),
35                crate::ArkUINodeAttributeType::FontColor,
36                font_color_property,
37            )
38        })?;
39        Ok(())
40    }
41
42    fn get_font_color(&self) -> ArkUIResult<ArkUINodeAttributeItem> {
43        ARK_UI_NATIVE_NODE_API_1
44            .with(|api| api.get_attribute(self.raw(), crate::ArkUINodeAttributeType::FontColor))
45    }
46
47    fn font_style(&self, font_style: i32) -> ArkUIResult<()> {
48        let font_style_property =
49            ArkUINodeAttributeItem::NumberValue(vec![ArkUINodeAttributeNumber::Int(font_style)]);
50        ARK_UI_NATIVE_NODE_API_1.with(|api| {
51            api.set_attribute(
52                self.raw(),
53                crate::ArkUINodeAttributeType::FontStyle,
54                font_style_property,
55            )
56        })?;
57        Ok(())
58    }
59
60    fn get_font_style(&self) -> ArkUIResult<ArkUINodeAttributeItem> {
61        ARK_UI_NATIVE_NODE_API_1
62            .with(|api| api.get_attribute(self.raw(), crate::ArkUINodeAttributeType::FontStyle))
63    }
64
65    fn font_weight(&self, font_weight: i32) -> ArkUIResult<()> {
66        let font_weight_property =
67            ArkUINodeAttributeItem::NumberValue(vec![ArkUINodeAttributeNumber::Int(font_weight)]);
68        ARK_UI_NATIVE_NODE_API_1.with(|api| {
69            api.set_attribute(
70                self.raw(),
71                crate::ArkUINodeAttributeType::FontWeight,
72                font_weight_property,
73            )
74        })?;
75        Ok(())
76    }
77
78    fn get_font_weight(&self) -> ArkUIResult<ArkUINodeAttributeItem> {
79        ARK_UI_NATIVE_NODE_API_1
80            .with(|api| api.get_attribute(self.raw(), crate::ArkUINodeAttributeType::FontWeight))
81    }
82
83    fn font_family<T: Into<String>>(&self, font_family: T) -> ArkUIResult<()> {
84        let font_family_property = ArkUINodeAttributeItem::String(font_family.into());
85        ARK_UI_NATIVE_NODE_API_1.with(|api| {
86            api.set_attribute(
87                self.raw(),
88                crate::ArkUINodeAttributeType::FontFamily,
89                font_family_property,
90            )
91        })?;
92        Ok(())
93    }
94
95    fn get_font_family(&self) -> ArkUIResult<ArkUINodeAttributeItem> {
96        ARK_UI_NATIVE_NODE_API_1
97            .with(|api| api.get_attribute(self.raw(), crate::ArkUINodeAttributeType::FontFamily))
98    }
99
100    fn font_feature<T: Into<String>>(&self, font_feature: T) -> ArkUIResult<()> {
101        let font_feature_property = ArkUINodeAttributeItem::String(font_feature.into());
102        ARK_UI_NATIVE_NODE_API_1.with(|api| {
103            api.set_attribute(
104                self.raw(),
105                crate::ArkUINodeAttributeType::FontFeature,
106                font_feature_property,
107            )
108        })?;
109        Ok(())
110    }
111
112    fn get_font_feature(&self) -> ArkUIResult<ArkUINodeAttributeItem> {
113        ARK_UI_NATIVE_NODE_API_1
114            .with(|api| api.get_attribute(self.raw(), crate::ArkUINodeAttributeType::FontFeature))
115    }
116
117    #[cfg(feature = "api-15")]
118    fn immutable_font_weight(&self, font_weight: i32) -> ArkUIResult<()> {
119        let font_weight_property =
120            ArkUINodeAttributeItem::NumberValue(vec![ArkUINodeAttributeNumber::Int(font_weight)]);
121        ARK_UI_NATIVE_NODE_API_1.with(|api| {
122            api.set_attribute(
123                self.raw(),
124                crate::ArkUINodeAttributeType::ImmutableFontWeight,
125                font_weight_property,
126            )
127        })?;
128        Ok(())
129    }
130
131    #[cfg(feature = "api-15")]
132    fn get_immutable_font_weight(&self) -> ArkUIResult<ArkUINodeAttributeItem> {
133        ARK_UI_NATIVE_NODE_API_1.with(|api| {
134            api.get_attribute(
135                self.raw(),
136                crate::ArkUINodeAttributeType::ImmutableFontWeight,
137            )
138        })
139    }
140}