Debian Codegraph @

subprojects/fontconfig/fc-fontations/capabilities.rs

raw
1 /*
2 * fontconfig/fc-fontations/capabilities.rs
3 *
4 * Copyright 2025 Google LLC.
5 *
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the author(s) not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. The authors make no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
15 *
16 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22 * PERFORMANCE OF THIS SOFTWARE.
23 */
24
25 use font_types::Tag;
26 use read_fonts::tables::layout::ScriptRecord;
27 use read_fonts::TableProvider;
28 use skrifa::FontRef;
29 use std::ffi::CString;
30
31 // Mimicking issue in FreeType indexer inserting two delimiting spaces.
32 const SILF_CAPABILITIES_PREFIX: &str = "ttable:Silf";
33 const SILF_TAG: Tag = Tag::new(b"Silf");
34
35 fn capabilities_string<T: IntoIterator<Item = Tag>>(tags: T, has_silf: bool) -> Option<CString> {
36 let mut deduplicated_tags: Vec<Tag> = tags.into_iter().collect::<Vec<_>>();
37 deduplicated_tags.sort();
38 deduplicated_tags.dedup();
39 let mut capabilities_set = deduplicated_tags
40 .into_iter()
41 .map(|tag| format!("otlayout:{}", tag))
42 .collect::<Vec<_>>();
43 has_silf.then(|| capabilities_set.insert(0, SILF_CAPABILITIES_PREFIX.to_string()));
44
45 let capabilities = capabilities_set.join(" ");
46 if capabilities.is_empty() {
47 return None;
48 }
49 CString::new(capabilities).ok()
50 }
51
52 fn gsub_tags<'a>(font: &'a FontRef) -> Option<&'a [ScriptRecord]> {
53 Some(font.gsub().ok()?.script_list().ok()?.script_records())
54 }
55
56 fn gpos_tags<'a>(font: &'a FontRef) -> Option<&'a [ScriptRecord]> {
57 Some(font.gpos().ok()?.script_list().ok()?.script_records())
58 }
59
60 pub fn make_capabilities(font: &FontRef) -> Option<CString> {
61 let has_silf = font.table_data(SILF_TAG).is_some();
62 capabilities_string(
63 gsub_tags(font)
64 .into_iter()
65 .flatten()
66 .chain(gpos_tags(font).into_iter().flatten())
67 .map(|script_record| script_record.script_tag()),
68 has_silf,
69 )
70 }
71
72 #[cfg(test)]
73 mod test {
74 use super::capabilities_string;
75 use font_types::Tag;
76 #[test]
77 fn capabilities() {
78 let tags = [Tag::new(b"DFLT"), Tag::new(b"cyrl"), Tag::new(b"grek")];
79 assert_eq!(
80 "otlayout:DFLT otlayout:cyrl otlayout:grek",
81 capabilities_string(tags, false).unwrap().to_str().unwrap()
82 );
83 let tags = [
84 Tag::new(b"DFLT"),
85 Tag::new(b"cyrl"),
86 Tag::new(b"cyrl"),
87 Tag::new(b"grek"),
88 ];
89
90 assert_eq!(
91 "otlayout:DFLT otlayout:cyrl otlayout:grek",
92 capabilities_string(tags, false).unwrap().to_str().unwrap()
93 );
94 }
95
96 #[test]
97 fn empty_capabilities() {
98 assert_eq!(None, capabilities_string([], false));
99 }
100
101 #[test]
102 fn graphite_capabilities() {
103 let tags = [Tag::new(b"grek")];
104 assert_eq!(
105 "ttable:Silf otlayout:grek",
106 capabilities_string(tags, true).unwrap().to_str().unwrap()
107 );
108 }
109 }