【typst-rs】Typst 字体管理命令实现解析

张开发
2026/4/7 10:31:16 15 分钟阅读

分享文章

【typst-rs】Typst 字体管理命令实现解析
Typst 字体管理命令实现解析这是一个Rust语言中的Typst字体管理模块用于列出和发现系统中可用的字体。完整代码usestd::any::Any;usestd::path::Path;usetypst::text::FontVariant;usetypst_kit::fonts::{self,FontPath,FontStore};usecrate::args::{FontArgs,FontsCommand};/// Execute a font listing command.pubfnfonts(command:FontsCommand){letfontsdiscover_fonts(command.font);for(family,indices)infonts.book().families(){println!({family});ifcommand.variants{forindexinindices{letSome(info)fonts.book().info(index)else{continue};letFontVariant{style,weight,stretch}info.variant;letpathfonts.source(index).and_then(|source|(sourceasdynAny).downcast_ref::FontPath()).map(|font|font.path.as_path()).unwrap_or_else(||Path::new(embedded)).display();println!(- Style: {style:?}, Weight: {weight:?}, Stretch: {stretch:?}, Path: {path},);}}}}/// Discovers the fonts as specified by the CLI flags.#[typst_macros::time(name discover fonts)]pubfndiscover_fonts(args:FontArgs)-FontStore{letmutfontsFontStore::new();if!args.ignore_system_fonts{fonts.extend(fonts::system());}#[cfg(feature embedded-fonts)]if!args.ignore_embedded_fonts{fonts.extend(fonts::embedded());}forpathinargs.font_paths{fonts.extend(fonts::scan(path));}fonts}代码结构解析1. 主函数 -fontspubfnfonts(command:FontsCommand){letfontsdiscover_fonts(command.font);for(family,indices)infonts.book().families(){println!({family});ifcommand.variants{// 打印字体系列中的每个字体变体}}}该函数负责调用discover_fonts发现所有可用字体遍历每个字体系列根据variants标志决定是否打印详细变体信息2. 字体发现函数 -discover_fonts#[typst_macros::time(name discover fonts)]pubfndiscover_fonts(args:FontArgs)-FontStore{letmutfontsFontStore::new();// 系统字体if!args.ignore_system_fonts{fonts.extend(fonts::system());}// 内嵌字体需要embedded-fonts特性#[cfg(feature embedded-fonts)]if!args.ignore_embedded_fonts{fonts.extend(fonts::embedded());}// 用户自定义路径forpathinargs.font_paths{fonts.extend(fonts::scan(path));}fonts}字体发现优先级字体按以下顺序加载优先级字体来源控制标志1系统字体ignore_system_fonts2内嵌字体ignore_embedded_fonts(需要feature)3用户自定义路径font_paths字体变体属性当command.variants为true时会打印每个字体的详细信息letFontVariant{style,weight,stretch}info.variant;Style: 字体样式正常、斜体、倾斜Weight: 字体粗细100-900或细体、常规、粗体等Stretch: 字体拉伸压缩、正常、扩展等字体路径处理letpathfonts.source(index).and_then(|source|(sourceasdynAny).downcast_ref::FontPath()).map(|font|font.path.as_path()).unwrap_or_else(||Path::new(embedded)).display();这个链式调用实现了获取字体源数据尝试向下转换为FontPath类型提取文件路径如果是内嵌字体显示embedded转换为可显示格式输出示例基础模式仅字体系列Arial Helvetica Times New Roman详细模式显示变体Arial - Style: Normal, Weight: Regular, Stretch: Normal, Path: /System/Library/Fonts/Arial.ttf - Style: Italic, Weight: Regular, Stretch: Normal, Path: /System/Library/Fonts/Arial Italic.ttf - Style: Normal, Weight: Bold, Stretch: Normal, Path: /System/Library/Fonts/Arial Bold.ttf Helvetica - Style: Normal, Weight: Regular, Stretch: Normal, Path: /System/Library/Fonts/Helvetica.ttc命令行使用示例# 列出所有字体系列typst fonts# 列出所有字体系列及其变体typst fonts--variants# 忽略系统字体只使用自定义路径typst fonts --ignore-system-fonts --font-path ./my-fonts# 忽略内嵌字体typst fonts --ignore-embedded-fonts特性说明可选特性featureembedded-fonts: 启用内嵌字体支持Typst自带的默认字体性能优化#[typst_macros::time]宏用于测量字体发现的时间设计特点渐进式加载按照优先级顺序加载字体后者覆盖前者灵活性允许用户控制字体来源系统/内嵌/自定义类型安全使用Any类型进行向下转换安全获取路径信息跨平台支持通过typst_kit::fonts::system()抽象系统字体API总结这个模块是Typst CLI的字体管理子命令提供了字体发现和枚举能力灵活的字体来源配置详细的字体变体信息查询支持自定义字体路径它使得用户可以查看系统中可用的字体调试字体加载问题确认自定义字体是否正确加载了解字体的具体变体信息

更多文章