render/
render.rs

1// Copyright 2016 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15extern crate font_rs;
16
17use std::fs::File;
18use std::io::{Read, Write};
19use std::time::SystemTime;
20
21use font_rs::font::{parse, GlyphBitmap};
22
23fn dump_pgm(glyph: &GlyphBitmap, out_filename: &str) {
24    let mut o = File::create(&out_filename).unwrap();
25    let _ = o.write(format!("P5\n{} {}\n255\n", glyph.width, glyph.height).as_bytes());
26    println!("data len = {}", glyph.data.len());
27    let _ = o.write(&glyph.data);
28}
29
30fn main() {
31    let mut args = std::env::args();
32    let _ = args.next();
33    let filename = args.next().unwrap();
34    let glyph_id: u16 = args.next().unwrap().parse().unwrap();
35    let out_filename = args.next().unwrap();
36    let mut f = File::open(&filename).unwrap();
37    let mut data = Vec::new();
38    match f.read_to_end(&mut data) {
39        Err(e) => println!("failed to read {}, {}", filename, e),
40        Ok(_) => match parse(&data) {
41            Ok(font) => {
42                if out_filename == "__bench__" {
43                    for size in 1..201 {
44                        let start = SystemTime::now();
45                        let n_iter = 1000;
46                        for _ in 0..n_iter {
47                            match font.render_glyph(glyph_id, size) {
48                                Some(_glyph) => (),
49                                None => (),
50                            }
51                        }
52                        let elapsed = start.elapsed().unwrap();
53                        let elapsed =
54                            elapsed.as_secs() as f64 + 1e-9 * (elapsed.subsec_nanos() as f64);
55                        println!("{} {}", size, elapsed * (1e6 / n_iter as f64));
56                    }
57                } else {
58                    match font.render_glyph(glyph_id, 400) {
59                        Some(glyph) => dump_pgm(&glyph, &out_filename),
60                        None => println!("failed to render {} {}", filename, glyph_id),
61                    }
62                }
63            }
64            Err(_) => println!("failed to parse {}", filename),
65        },
66    }
67}