Debian Codegraph @

subprojects/fontconfig/fc-fontations/foundries.rs

raw
1 /*
2 * fontconfig/fc-fontations/foundries.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 read_fonts::TableProvider;
26 use skrifa::{
27 string::{LocalizedStrings, StringId},
28 FontRef, MetadataProvider,
29 };
30 use std::ffi::{CStr, CString};
31
32 fn foundry_name_to_taglike(foundry: &str) -> Option<&'static str> {
33 match foundry {
34 "Adobe" => Some("adobe"),
35 "Bigelow" => Some("b&h"),
36 "Bitstream" => Some("bitstream"),
37 "Gnat" | "Iorsh" => Some("culmus"),
38 "HanYang System" => Some("hanyang"),
39 "Font21" => Some("hwan"),
40 "IBM" => Some("ibm"),
41 "International Typeface Corporation" => Some("itc"),
42 "Linotype" | "LINOTYPE-HELL" => Some("linotype"),
43 "Microsoft" => Some("microsoft"),
44 "Monotype" => Some("monotype"),
45 "Omega" => Some("omega"),
46 "Tiro Typeworks" => Some("tiro"),
47 "URW" => Some("urw"),
48 "XFree86" => Some("xfree86"),
49 "Xorg" => Some("xorg"),
50 _ => None,
51 }
52 }
53
54 fn map_foundry_from_name_entry(localized_strings: &mut LocalizedStrings) -> Option<CString> {
55 localized_strings.into_iter().find_map(|foundry_name| {
56 foundry_name_to_taglike(foundry_name.to_string().as_str())
57 .map(|foundry| CString::new(foundry).unwrap())
58 })
59 }
60
61 pub fn make_foundry(font: &FontRef) -> Option<CString> {
62 if let Ok(os2) = font.os2() {
63 let vend_bytes = os2.ach_vend_id().to_be_bytes();
64 let foundry = if vend_bytes.contains(&0) {
65 CStr::from_bytes_until_nul(&vend_bytes)
66 .ok()
67 .map(|cstr| cstr.to_owned())
68 } else {
69 CString::new(vend_bytes).ok()
70 };
71
72 if let Some(foundry) = foundry {
73 if !foundry.is_empty() {
74 return Some(foundry);
75 }
76 }
77 }
78
79 map_foundry_from_name_entry(&mut font.localized_strings(StringId::TRADEMARK)).or_else(|| {
80 map_foundry_from_name_entry(&mut font.localized_strings(StringId::MANUFACTURER))
81 })
82 }