如何修改字体并使用Rust 'write-fonts'保存它?

xtupzzrd  于 2023-06-06  发布在  其他
关注(0)|答案(1)|浏览(140)

我想打开一个字体文件并修改例如usWeightClass并将其保存在不同的位置-使用Rust:https://github.com/googlefonts/fontations
我已经能够加载文件,从字体中读取数据,但我无法修改和保存它。
我希望,它应该以某种方式与WriteFont一起工作(但我不知道如何):https://docs.rs/write-fonts/0.5.0/write_fonts/trait.FontWrite.html#
任何帮助将不胜感激。先谢谢你了,奥利
Cargo.toml

[dependencies]
write-fonts = "0.5.0"
read-fonts = "0.3.0"
font-types = "0.1.8"

main.rs

use read_fonts::{FontRef, TableProvider};

fn main() {
    pub static FONT_DATA: &[u8] = include_bytes!("/path/to/a/font/somefont.ttf");
    let font = FontRef::new(FONT_DATA).unwrap();

    let mut os2 = font.os2().expect("missing OS/2 table");
    println!("os2.version: {}", os2.version());
    println!("os2.us_weight_class: {}", os2.us_weight_class());

    let mut name = font.name().expect("missing name table");

    for item in name.name_record() {
        println!("name_id: {:?}", item.name_id);
        println!("language_id: {:?}", item.language_id);
        let data = item.string(name.string_data()).unwrap();
        println!("String entry: {:?}", data.chars().collect::<String>());
    };
}
blmhpbnm

blmhpbnm1#

我还没有使用过这个库或一般的字体,但在文档中进行了一些挖掘后,这似乎可以工作:

use read_fonts::{FontRead, FontRef, TableProvider, TopLevelTable};
use write_fonts::{dump_table, tables::os2::Os2, FontBuilder};

fn main() {
    {
        let font_data = std::fs::read("Roboto-Regular.ttf").unwrap();
        let font = FontRef::new(&font_data).unwrap();

        let os2 = font.os2().expect("missing OS/2 table");
        println!("os2.us_weight_class: {}", os2.us_weight_class());

        // Create a new font builder
        let mut builder = FontBuilder::default();

        // Iterate over tables and add them to the builder
        for table in font.table_directory.table_records() {
            let tag = table.tag();

            println!("    Adding table {tag} ...");

            let font_data = font
                .table_data(tag)
                .expect(&format!("Table {tag} not found!"));

            let mut raw_data = font_data.as_ref().to_owned();

            // Modify the OS2 tag
            if tag == Os2::TAG {
                let mut os2 = Os2::read(font_data).unwrap();
                os2.us_weight_class = 420;
                raw_data = dump_table(&os2).unwrap();
            }

            builder.add_table(tag, raw_data);
        }

        // Build the font
        let data = builder.build();
        std::fs::write("Roboto-Regular-modified.ttf", data).unwrap();
    }

    {
        // Load the font again and check if it got modified
        let font_data = std::fs::read("Roboto-Regular-modified.ttf").unwrap();
        let font = FontRef::new(&font_data).unwrap();

        let os2 = font.os2().expect("missing OS/2 table");
        println!("os2.us_weight_class: {}", os2.us_weight_class());
    }
}
os2.us_weight_class: 400
    Adding table GDEF ...
    Adding table GPOS ...
    Adding table GSUB ...
    Adding table OS/2 ...
    Adding table cmap ...
    Adding table cvt  ...
    Adding table fpgm ...
    Adding table gasp ...
    Adding table glyf ...
    Adding table hdmx ...
    Adding table head ...
    Adding table hhea ...
    Adding table hmtx ...
    Adding table loca ...
    Adding table maxp ...
    Adding table name ...
    Adding table post ...
    Adding table prep ...
os2.us_weight_class: 420

相关问题