~raph/interp-toy

ref: 4cfe4c880ba40b18c5887781a1cce2da9f2400e9 interp-toy/glyphstool/src/main.rs -rw-r--r-- 2.2 KiB
4cfe4c88Raph Levien Add "merge" command 2 years ago
                                                                                
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use std::env;
use std::fs;
use std::path::{Path, PathBuf};

use structopt::StructOpt;

#[derive(StructOpt, Debug)]
enum Cmd {
    Merge(MergeCmd)
}

#[derive(StructOpt, Debug)]
struct MergeCmd {
    /// The font file to merge in.
    #[structopt(parse(from_os_str))]
    font: PathBuf,

    /// The other font file, to use as a source.
    #[structopt(parse(from_os_str))]
    other: PathBuf,

    /// The layer to merge (UUID).
    layer: String,
}

use glyphstool::{ops, stretch, Font, FromPlist, Plist, ToPlist};

fn usage() {
    eprintln!("usage: glyphstool font.glyphs");
}

fn read_font(path: &Path) -> Font {
    let contents = fs::read_to_string(path).expect("error reading font file");
    let plist = Plist::parse(&contents).expect("error parsing font file");
    FromPlist::from_plist(plist)
}

fn write_font(path: &Path, font: Font) {
    let plist = font.to_plist();
    fs::write(path, &plist.to_string());
}

fn main() {
    let cmd = Cmd::from_args();

    match cmd {
        Cmd::Merge(m) => {
            println!("merge {:?}", m);
            let mut font = read_font(&m.font);
            let other = read_font(&m.other);
            ops::merge(&mut font, &other, &m.layer);
            write_font(&m.font, font);
        }
    }
    /*
    let mut filename = None;
    for arg in env::args().skip(1) {
        if filename.is_none() {
            filename = Some(arg);
        }
    }
    if filename.is_none() {
        usage();
        return;
    }
    let filename = filename.unwrap();
    let contents = fs::read_to_string(filename).expect("error reading font");
    let plist = Plist::parse(&contents).expect("parse error");
    //println!("Plist: {:?}", plist);
    /*
    let font = Font::from_plist(plist);
    for glyph in font.glyphs() {
        println!("glyphname: {}", glyph.glyphname());
        for layer in glyph.layers() {
            println!("  layer: {}, width = {}", layer.layer_id(), layer.width());
        }
    }
    */
    let mut font: Font = FromPlist::from_plist(plist);
    //println!("{:?}", font);
    stretch(&mut font, 0.5, "051EFAE4-8BBE-4FBB-A016-4335C3E52F59");
    let plist = font.to_plist();
    println!("{}", plist.to_string());
    */
}