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
|
// font-kit/examples/match-font.rs
//
// Copyright © 2020 The Pathfinder Project Developers.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Looks up fonts by name.
extern crate font_kit;
use font_kit::family_name::FamilyName;
use font_kit::handle::Handle;
use font_kit::properties::Properties;
use font_kit::source::SystemSource;
use std::env;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let args: Vec<_> = env::args().collect();
if args.len() != 2 {
println!("Usage:\n\tmatch-font \"Times New Roman, Arial, serif\"");
std::process::exit(1);
}
let mut families = Vec::new();
for family in args[1].split(',') {
let family = family.replace('\'', "");
let family = family.trim();
families.push(match family {
"serif" => FamilyName::Serif,
"sans-serif" => FamilyName::SansSerif,
"monospace" => FamilyName::Monospace,
"cursive" => FamilyName::Cursive,
"fantasy" => FamilyName::Fantasy,
_ => FamilyName::Title(family.to_string()),
});
}
let properties = Properties::default();
let handle = SystemSource::new().select_best_match(&families, &properties)?;
if let Handle::Path {
ref path,
font_index,
} = handle
{
println!("Path: {}", path.display());
println!("Index: {}", font_index);
}
let font = handle.load()?;
println!("Family name: {}", font.family_name());
println!(
"PostScript name: {}",
font.postscript_name().unwrap_or("?".to_string())
);
println!("Style: {:?}", font.properties().style);
println!("Weight: {:?}", font.properties().weight);
println!("Stretch: {:?}", font.properties().stretch);
Ok(())
}
|