fc-fontations/foundries.rs | 69 +++++++++++++++++++++++++++++++++++++++ fc-fontations/mod.rs | 13 ++++++- test/test_fontations_ft_query.py | 1 3 files changed, 81 insertions(+), 2 deletions(-) New commits: commit 86fd90c69117b327b6ec42da4ce32de877908684 Merge: 83f34e1 40d8ed9 Author: Akira TAGOH <akira@xxxxxxxxx> Date: Thu May 1 08:43:32 2025 +0000 Merge branch 'foundries' into 'main' [Fontations] Add support for "foundry" pattern element See merge request fontconfig/fontconfig!394 commit 40d8ed917aec9e27288398a653c315681a665364 Author: Dominik Röttsches <drott@xxxxxxxxxxxx> Date: Fri Apr 25 15:51:37 2025 +0300 [Fontations] Add support for "foundry" pattern element Parse foundry from OS/2 with fallback to name table entries. diff --git a/fc-fontations/foundries.rs b/fc-fontations/foundries.rs new file mode 100644 index 0000000..d416d9f --- /dev/null +++ b/fc-fontations/foundries.rs @@ -0,0 +1,69 @@ +/* + * fontconfig/fc-fontations/foundries.rs + * + * Copyright 2025 Google LLC. + * + * Permission to use, copy, modify, distribute, and sell this software and its + * documentation for any purpose is hereby granted without fee, provided that + * the above copyright notice appear in all copies and that both that + * copyright notice and this permission notice appear in supporting + * documentation, and that the name of the author(s) not be used in + * advertising or publicity pertaining to distribution of the software without + * specific, written prior permission. The authors make no + * representations about the suitability of this software for any purpose. It + * is provided "as is" without express or implied warranty. + * + * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO + * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR + * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, + * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER + * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + * PERFORMANCE OF THIS SOFTWARE. + */ + +use read_fonts::TableProvider; +use skrifa::{ + string::{LocalizedStrings, StringId}, + FontRef, MetadataProvider, +}; +use std::ffi::CString; + +fn foundry_name_to_taglike(foundry: &str) -> Option<&'static str> { + match foundry { + "Adobe" => Some("adobe"), + "Bigelow" => Some("b&h"), + "Bitstream" => Some("bitstream"), + "Gnat" | "Iorsh" => Some("culmus"), + "HanYang System" => Some("hanyang"), + "Font21" => Some("hwan"), + "IBM" => Some("ibm"), + "International Typeface Corporation" => Some("itc"), + "Linotype" | "LINOTYPE-HELL" => Some("linotype"), + "Microsoft" => Some("microsoft"), + "Monotype" => Some("monotype"), + "Omega" => Some("omega"), + "Tiro Typeworks" => Some("tiro"), + "URW" => Some("urw"), + "XFree86" => Some("xfree86"), + "Xorg" => Some("xorg"), + _ => None, + } +} + +fn map_foundry_from_name_entry(localized_strings: &mut LocalizedStrings) -> Option<CString> { + localized_strings.into_iter().find_map(|foundry_name| { + foundry_name_to_taglike(foundry_name.to_string().as_str()) + .map(|foundry| CString::new(foundry).unwrap()) + }) +} + +pub fn make_foundry(font: &FontRef) -> Option<CString> { + if let Ok(os2) = font.os2() { + return CString::new(os2.ach_vend_id().to_be_bytes()).ok(); + } + + return map_foundry_from_name_entry(&mut font.localized_strings(StringId::TRADEMARK)).or_else( + || map_foundry_from_name_entry(&mut font.localized_strings(StringId::MANUFACTURER)), + ); +} diff --git a/fc-fontations/mod.rs b/fc-fontations/mod.rs index f039471..272b8d3 100644 --- a/fc-fontations/mod.rs +++ b/fc-fontations/mod.rs @@ -24,13 +24,15 @@ mod names; mod pattern_bindings; +mod foundries; +use foundries::make_foundry; use names::add_names; use fc_fontations_bindgen::{ fcint::{ - FC_COLOR_OBJECT, FC_FONTFORMAT_OBJECT, FC_FONT_HAS_HINT_OBJECT, FC_OUTLINE_OBJECT, - FC_SCALABLE_OBJECT, + FC_COLOR_OBJECT, FC_FONTFORMAT_OBJECT, FC_FONT_HAS_HINT_OBJECT, FC_FOUNDRY_OBJECT, + FC_OUTLINE_OBJECT, FC_SCALABLE_OBJECT, }, FcFontSet, FcFontSetAdd, FcPattern, }; @@ -179,6 +181,13 @@ fn build_patterns_for_font( } } + let foundry_string = make_foundry(font).unwrap_or(CString::new("unknown").unwrap()); + + pattern.append_element(PatternElement::new( + FC_FOUNDRY_OBJECT as i32, + foundry_string.into(), + )); + pattern .create_fc_pattern() .map(|p| p.into_raw() as *mut FcPattern) diff --git a/test/test_fontations_ft_query.py b/test/test_fontations_ft_query.py index 6a678aa..706b564 100644 --- a/test/test_fontations_ft_query.py +++ b/test/test_fontations_ft_query.py @@ -60,6 +60,7 @@ def test_fontations_freetype_fcquery_equal(font_file): "fontformat", "color", "fonthashint", + "foundry", ] format_string = ":".join( "%{" + entity + "}" for entity in supported_format_entitites