Skip to content

Commit

Permalink
OTX, TTX를 제외하고 구현함; 이후는 regex 필요
Browse files Browse the repository at this point in the history
  • Loading branch information
hwoongkang committed Jun 28, 2023
1 parent 7249b3f commit 16ac436
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
44 changes: 44 additions & 0 deletions packages/sfnt/src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 1,44 @@
use crate::types::FileType;
use std::fs::File;
use std::io::Read;

pub fn run() -> Result<(), String> {
let Some(second_arg) = std::env::args().nth(1) else {
return Err(format!("Usage: {} [string]", file!()))
};
let Ok(mut file_handle) = File::open(&second_arg) else {
return Err(format!("Error opening file: {}", second_arg))
};

let Some(file_type) = guess_file_type(&mut file_handle) else {
return Err(format!("Error guessing file type: {}", second_arg))
};
println!("File type: {}", file_type);

Ok(())
}

fn guess_file_type(file: &mut File) -> Option<FileType> {
let mut bytes = [0u8; 256];
let Ok(_) = file.read_exact(&mut bytes) else {
return None;
};
// Windows Bom Bytes 라고 해서, \xef\xbb\xbf 가 붙어 있는 경우가 있음. 대응해야 함.
let mut start_ind = 0;
let bom_bytes = b"\xef\xbb\xbf";
if bytes.starts_with(bom_bytes) {
start_ind = 3;
}

let head = &bytes[start_ind..(start_ind 4)];
match head {
b"OTTO" => Some(FileType::OTF),
b"ttcf" => Some(FileType::TTF),
b"wOFF" => Some(FileType::WOFF),
b"wOF2" => Some(FileType::WOFF2),
b"<?xm" => {
unimplemented!()
}
_ => None,
}
}
7 changes: 6 additions & 1 deletion packages/sfnt/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 1,8 @@
mod cli;
mod types;
fn main() {
println!("Hello, world!");
match cli::run() {
Ok(_) => (),
Err(e) => println!("{}", e),
}
}
23 changes: 23 additions & 0 deletions packages/sfnt/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 1,23 @@
pub enum FileType {
OTF,
TTC,
TTF,
WOFF,
WOFF2,
OTX,
TTX,
}

impl std::fmt::Display for FileType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
FileType::OTF => write!(f, "otf"),
FileType::TTC => write!(f, "ttc"),
FileType::TTF => write!(f, "ttf"),
FileType::WOFF => write!(f, "woff"),
FileType::WOFF2 => write!(f, "woff2"),
FileType::OTX => write!(f, "otx"),
FileType::TTX => write!(f, "ttx"),
}
}
}

0 comments on commit 16ac436

Please sign in to comment.