1// font-kit/src/utils.rs
2//
3// Copyright © 2018 The Pathfinder Project Developers.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
11//! Miscellaneous utilities for use in this crate.
12
13#![allow(dead_code)]
14
15use std::fs::File;
16use std::io::{Error as IOError, Read};
17
18pub(crate) static SFNT_VERSIONS: [[u8; 4]; 4] = [
19 [0x00, 0x01, 0x00, 0x00],
20 [b'O', b'T', b'T', b'O'],
21 [b't', b'r', b'u', b'e'],
22 [b't', b'y', b'p', b'1'],
23];
24
25pub(crate) fn clamp(x: f32, min: f32, max: f32) -> f32 {
26 if x < min {
27 min
28 } else if x > max {
29 max
30 } else {
31 x
32 }
33}
34
35#[inline]
36pub(crate) fn lerp(a: f32, b: f32, t: f32) -> f32 {
37 a + (b - a) * t
38}
39
40#[inline]
41pub(crate) fn div_round_up(a: usize, b: usize) -> usize {
42 (a + b - 1) / b
43}
44
45pub(crate) fn slurp_file(file: &mut File) -> Result<Vec<u8>, IOError> {
46 let mut data: Vec = match file.metadata() {
47 Ok(metadata: Metadata) => Vec::with_capacity(metadata.len() as usize),
48 Err(_) => vec![],
49 };
50 file.read_to_end(&mut data)?;
51 Ok(data)
52}
53