1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use {
    fidl_fuchsia_fonts::{
        self as fonts, CacheMissPolicy, FallbackGroup, FamilyName, FontFamilyInfo,
        GenericFontFamily, Style2, TypefaceQuery, TypefaceRequest, TypefaceRequestFlags,
        TypefaceResponse, Width,
    },
    fidl_fuchsia_intl as intl,
};

/// Extensions for [`FallbackGroup`](fidl_fuchsia_fonts::FallbackGroup).
pub trait FallbackGroupExt {
    fn to_generic_font_family(&self) -> Option<GenericFontFamily>;
}

impl FallbackGroupExt for FallbackGroup {
    fn to_generic_font_family(&self) -> Option<GenericFontFamily> {
        match self {
            FallbackGroup::None => None,
            FallbackGroup::Serif => Some(GenericFontFamily::Serif),
            FallbackGroup::SansSerif => Some(GenericFontFamily::SansSerif),
            FallbackGroup::Monospace => Some(GenericFontFamily::Monospace),
            FallbackGroup::Cursive => Some(GenericFontFamily::Cursive),
            FallbackGroup::Fantasy => Some(GenericFontFamily::Fantasy),
        }
    }
}

/// Extensions for [`Request`](fidl_fuchsia_fonts::Request).
pub trait RequestExt {
    fn into_typeface_request(self) -> TypefaceRequest;
}

impl RequestExt for fonts::Request {
    fn into_typeface_request(self) -> TypefaceRequest {
        let family: Option<FamilyName> = match self.family {
            Some(family) => Some(FamilyName { name: family }),
            None => None,
        };

        let style: Option<Style2> = Some(Style2 {
            weight: Some(self.weight as u16),
            slant: Some(self.slant),
            width: Width::from_primitive(self.width),
            ..Default::default()
        });

        let languages: Option<Vec<intl::LocaleId>> = self.language.map(|languages| {
            languages.iter().map(|lang_code| intl::LocaleId { id: lang_code.to_string() }).collect()
        });

        let mut flags = TypefaceRequestFlags::empty();
        if (self.flags & fonts::REQUEST_FLAG_NO_FALLBACK) != 0 {
            flags |= TypefaceRequestFlags::EXACT_FAMILY;
        }
        if (self.flags & fonts::REQUEST_FLAG_EXACT_MATCH) != 0 {
            flags |= TypefaceRequestFlags::EXACT_STYLE;
        }

        TypefaceRequest {
            query: Some(TypefaceQuery {
                family,
                style,
                languages,
                code_points: match self.character {
                    ch if ch > 0 => Some(vec![ch]),
                    _ => None,
                },
                fallback_family: self.fallback_group.to_generic_font_family(),
                ..Default::default()
            }),
            flags: Some(flags),
            cache_miss_policy: None,
            ..Default::default()
        }
    }
}

/// Extensions for [`TypefaceRequest`](fidl_fuchsia_fonts::TypefaceRequest).
pub trait TypefaceRequestExt {
    /// See [`fidl_fuchsia_fonts::TypefaceRequestFlags::ExactFamily`].
    fn exact_family(&self) -> bool;

    /// See [`fidl_fuchsia_fonts::TypefaceRequestFlags::ExactStyle`].
    fn exact_style(&self) -> bool;

    /// See ['fidl_fuchsia_fonts::CacheMissPolicy`].
    fn cache_miss_policy(&self) -> CacheMissPolicy;
}

impl TypefaceRequestExt for TypefaceRequest {
    fn exact_family(&self) -> bool {
        self.flags.map_or(false, |flags| flags.contains(fonts::TypefaceRequestFlags::EXACT_FAMILY))
    }

    fn exact_style(&self) -> bool {
        self.flags.map_or(false, |flags| flags.contains(fonts::TypefaceRequestFlags::EXACT_STYLE))
    }

    fn cache_miss_policy(&self) -> CacheMissPolicy {
        self.cache_miss_policy.unwrap_or(CacheMissPolicy::BlockUntilDownloaded)
    }
}

/// Extensions for [`TypefaceResponse`](fidl_fuchsia_fonts::TypefaceResponse).
pub trait TypefaceResponseExt {
    fn into_font_response(self) -> Option<fonts::Response>;
}

impl TypefaceResponseExt for TypefaceResponse {
    fn into_font_response(self) -> Option<fonts::Response> {
        if self == Self::default() {
            None
        } else {
            Some(fonts::Response {
                buffer: self.buffer.unwrap(),
                buffer_id: self.buffer_id.unwrap(),
                font_index: self.font_index.unwrap(),
            })
        }
    }
}

/// Extensions for [`FontFamilyInfo`](fidl_fuchsia_fonts::FontFamilyInfo).
pub trait FontFamilyInfoExt {
    fn into_family_info(self) -> Option<fonts::FamilyInfo>;
}

impl FontFamilyInfoExt for FontFamilyInfo {
    fn into_family_info(self) -> Option<fonts::FamilyInfo> {
        if self == Self::default() {
            None
        } else {
            Some(fonts::FamilyInfo {
                name: self.name.unwrap().name,
                styles: self
                    .styles
                    .unwrap()
                    .into_iter()
                    .flat_map(|style2| style2.into_style())
                    .collect(),
            })
        }
    }
}

/// Extensions for [`Style2`](fidl_fuchsia_fonts::Style2).
pub trait Style2Ext {
    fn into_style(self) -> Option<fonts::Style>;
}

impl Style2Ext for Style2 {
    fn into_style(self) -> Option<fonts::Style> {
        if self == Self::default() {
            None
        } else {
            Some(fonts::Style {
                weight: self.weight.unwrap() as u32, // Expanded from u16
                width: self.width.unwrap().into_primitive(),
                slant: self.slant.unwrap(),
            })
        }
    }
}