accesskit_atspi_common/
text_attributes.rs

1// Copyright 2026 The AccessKit Authors. All rights reserved.
2// Licensed under the Apache License, Version 2.0 (found in
3// the LICENSE-APACHE file) or the MIT license (found in
4// the LICENSE-MIT file), at your option.
5
6use accesskit::{Color, TextAlign, TextDecorationStyle};
7use accesskit_consumer::Node;
8use phf::phf_map;
9
10fn color_to_string(color: Color) -> String {
11    format!("{},{},{}", color.red, color.green, color.blue)
12}
13
14fn family_name(node: &Node) -> Option<String> {
15    node.font_family().map(String::from)
16}
17
18fn size(node: &Node) -> Option<String> {
19    node.font_size().map(|value| value.to_string())
20}
21
22fn weight(node: &Node) -> Option<String> {
23    node.font_weight().map(|value| value.to_string())
24}
25
26fn style(node: &Node) -> Option<String> {
27    node.is_italic().then(|| "italic".into())
28}
29
30fn strikethrough(node: &Node) -> Option<String> {
31    node.strikethrough().map(|_| "true".into())
32}
33
34fn underline(node: &Node) -> Option<String> {
35    node.underline().map(|deco| {
36        match deco.style {
37            TextDecorationStyle::Double => "double",
38            _ => "single",
39        }
40        .into()
41    })
42}
43
44fn bg_color(node: &Node) -> Option<String> {
45    node.background_color().map(color_to_string)
46}
47
48fn fg_color(node: &Node) -> Option<String> {
49    node.foreground_color().map(color_to_string)
50}
51
52fn language(node: &Node) -> Option<String> {
53    node.language().map(String::from)
54}
55
56fn justification(node: &Node) -> Option<String> {
57    node.text_align().map(|align| {
58        match align {
59            TextAlign::Left => "left",
60            TextAlign::Center => "center",
61            TextAlign::Right => "right",
62            TextAlign::Justify => "fill",
63        }
64        .into()
65    })
66}
67
68pub(crate) const ATTRIBUTE_GETTERS: phf::Map<&'static str, fn(&Node) -> Option<String>> = phf_map! {
69    "family-name" => family_name,
70    "size" => size,
71    "weight" => weight,
72    "style" => style,
73    "strikethrough" => strikethrough,
74    "underline" => underline,
75    "bg-color" => bg_color,
76    "fg-color" => fg_color,
77    "language" => language,
78    "justification" => justification,
79};