[Update] Font Family. Change the way to fetch font family list.

This commit is contained in:
Sakamoto Shiina
2024-10-02 18:26:58 +09:00
parent b1aadc7cda
commit 9f7101e233
4 changed files with 216 additions and 35 deletions

View File

@@ -11,43 +11,26 @@ fn main() {
Ok(())
})
.invoke_handler(tauri::generate_handler![get_font_list])
// .invoke_handler(tauri::generate_handler![greet, run_python_script])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
use std::fs;
use std::env;
use font_kit::{source::SystemSource};
use std::collections::HashSet;
#[tauri::command]
fn get_font_list() -> Vec<String> {
// システム全体のフォントディレクトリ
let system_font_dir = "C:\\Windows\\Fonts";
// ユーザーローカルのフォントディレクトリ
let local_font_dir = format!("{}\\Microsoft\\Windows\\Fonts", env::var("LOCALAPPDATA").unwrap());
async fn get_font_list() -> Vec<String> {
let source = SystemSource::new();
let mut font_families = HashSet::new();
let mut fonts = Vec::new();
// システムフォントとユーザーフォントのディレクトリをチェック
let font_dirs = vec![system_font_dir, &local_font_dir];
for dir in font_dirs {
if let Ok(entries) = fs::read_dir(dir) {
for entry in entries {
if let Ok(entry) = entry {
let path = entry.path();
if let Some(extension) = path.extension() {
if extension == "ttf" || extension == "otf" {
if let Some(font_name) = path.file_stem() {
fonts.push(font_name.to_string_lossy().to_string());
}
}
}
}
if let Ok(fonts) = source.all_fonts() {
for font in fonts {
if let Ok(info) = font.load() {
font_families.insert(info.family_name().to_string());
}
}
}
fonts
font_families.into_iter().collect()
}