2018-01-15 10:34:59 +03:00
|
|
|
//! Higher level freetype bindings
|
2018-01-17 10:02:32 +03:00
|
|
|
|
2019-12-10 07:06:12 +03:00
|
|
|
use crate::font::locator::FontDataHandle;
|
2019-11-02 19:13:51 +03:00
|
|
|
use failure::{bail, format_err, Error, Fallible, ResultExt};
|
2019-03-23 19:28:40 +03:00
|
|
|
pub use freetype::*;
|
2018-01-15 10:34:59 +03:00
|
|
|
use std::ffi::CString;
|
|
|
|
use std::ptr;
|
|
|
|
|
2019-03-23 19:28:40 +03:00
|
|
|
#[inline]
|
|
|
|
pub fn succeeded(error: FT_Error) -> bool {
|
|
|
|
error == freetype::FT_Err_Ok as FT_Error
|
|
|
|
}
|
|
|
|
|
2018-01-15 10:34:59 +03:00
|
|
|
/// Translate an error and value into a result
|
|
|
|
fn ft_result<T>(err: FT_Error, t: T) -> Result<T, Error> {
|
2018-09-20 03:52:34 +03:00
|
|
|
if succeeded(err) {
|
2018-01-15 10:34:59 +03:00
|
|
|
Ok(t)
|
|
|
|
} else {
|
2019-02-26 19:38:51 +03:00
|
|
|
Err(format_err!("FreeType error {:?} 0x{:x}", err, err))
|
2018-01-15 10:34:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-09 23:12:54 +03:00
|
|
|
pub fn compute_load_flags_for_mode(render_mode: FT_Render_Mode) -> i32 {
|
|
|
|
FT_LOAD_COLOR as i32 |
|
|
|
|
// enable FT_LOAD_TARGET bits. There are no flags defined
|
|
|
|
// for these in the bindings so we do some bit magic for
|
|
|
|
// ourselves. This is how the FT_LOAD_TARGET_() macro
|
|
|
|
// assembles these bits.
|
|
|
|
(render_mode as i32) << 16
|
|
|
|
}
|
|
|
|
|
2018-01-15 10:34:59 +03:00
|
|
|
pub struct Face {
|
|
|
|
pub face: FT_Face,
|
2019-12-09 03:02:04 +03:00
|
|
|
_bytes: Vec<u8>,
|
2018-01-15 10:34:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Face {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {
|
|
|
|
FT_Done_Face(self.face);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Face {
|
2019-12-09 19:00:55 +03:00
|
|
|
/// This is a wrapper around set_char_size and select_size
|
|
|
|
/// that accounts for some weirdness with eg: color emoji
|
|
|
|
pub fn set_font_size(&mut self, size: f64, dpi: u32) -> Fallible<(f64, f64)> {
|
|
|
|
log::debug!("set_char_size {} dpi={}", size, dpi);
|
|
|
|
// Scaling before truncating to integer minimizes the chances of hitting
|
|
|
|
// the fallback code for set_pixel_sizes below.
|
|
|
|
let size = (size * 64.0) as FT_F26Dot6;
|
|
|
|
|
|
|
|
let (cell_width, cell_height) = match self.set_char_size(size, size, dpi, dpi) {
|
|
|
|
Ok(_) => {
|
|
|
|
// Compute metrics for the nominal monospace cell
|
|
|
|
self.cell_metrics()
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
let sizes = unsafe {
|
|
|
|
let rec = &(*self.face);
|
|
|
|
std::slice::from_raw_parts(rec.available_sizes, rec.num_fixed_sizes as usize)
|
|
|
|
};
|
|
|
|
if sizes.is_empty() {
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
// Find the best matching size.
|
|
|
|
// We just take the biggest.
|
|
|
|
let mut best = 0;
|
|
|
|
let mut best_size = 0;
|
|
|
|
let mut cell_width = 0;
|
|
|
|
let mut cell_height = 0;
|
|
|
|
|
|
|
|
for (idx, info) in sizes.iter().enumerate() {
|
|
|
|
let size = best_size.max(info.height);
|
|
|
|
if size > best_size {
|
|
|
|
best = idx;
|
|
|
|
best_size = size;
|
|
|
|
cell_width = info.width;
|
|
|
|
cell_height = info.height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.select_size(best)?;
|
|
|
|
(f64::from(cell_width), f64::from(cell_height))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok((cell_width, cell_height))
|
|
|
|
}
|
|
|
|
|
2018-01-15 10:34:59 +03:00
|
|
|
pub fn set_char_size(
|
|
|
|
&mut self,
|
|
|
|
char_width: FT_F26Dot6,
|
|
|
|
char_height: FT_F26Dot6,
|
|
|
|
horz_resolution: FT_UInt,
|
|
|
|
vert_resolution: FT_UInt,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
ft_result(
|
|
|
|
unsafe {
|
|
|
|
FT_Set_Char_Size(
|
|
|
|
self.face,
|
|
|
|
char_width,
|
|
|
|
char_height,
|
|
|
|
horz_resolution,
|
|
|
|
vert_resolution,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-09-20 03:52:34 +03:00
|
|
|
#[allow(unused)]
|
2019-11-02 19:13:51 +03:00
|
|
|
pub fn set_pixel_sizes(&mut self, char_width: u32, char_height: u32) -> Fallible<()> {
|
2018-01-16 09:45:25 +03:00
|
|
|
ft_result(
|
2018-02-21 09:08:19 +03:00
|
|
|
unsafe { FT_Set_Pixel_Sizes(self.face, char_width, char_height) },
|
2018-01-16 09:45:25 +03:00
|
|
|
(),
|
|
|
|
)
|
2019-11-02 19:13:51 +03:00
|
|
|
.map_err(|e| e.context("set_pixel_sizes").into())
|
2018-01-16 09:45:25 +03:00
|
|
|
}
|
2018-01-16 04:32:31 +03:00
|
|
|
|
2018-09-20 03:52:34 +03:00
|
|
|
pub fn select_size(&mut self, idx: usize) -> Result<(), Error> {
|
|
|
|
ft_result(unsafe { FT_Select_Size(self.face, idx as i32) }, ())
|
|
|
|
}
|
|
|
|
|
2018-01-15 10:34:59 +03:00
|
|
|
pub fn load_and_render_glyph(
|
|
|
|
&mut self,
|
|
|
|
glyph_index: FT_UInt,
|
|
|
|
load_flags: FT_Int32,
|
|
|
|
render_mode: FT_Render_Mode,
|
|
|
|
) -> Result<&FT_GlyphSlotRec_, Error> {
|
|
|
|
unsafe {
|
|
|
|
let res = FT_Load_Glyph(self.face, glyph_index, load_flags);
|
2018-09-20 03:52:34 +03:00
|
|
|
if succeeded(res) {
|
2018-01-27 22:15:53 +03:00
|
|
|
let render = FT_Render_Glyph((*self.face).glyph, render_mode);
|
2018-09-20 03:52:34 +03:00
|
|
|
if !succeeded(render) {
|
2018-01-27 22:15:53 +03:00
|
|
|
bail!("FT_Render_Glyph failed: {:?}", render);
|
|
|
|
}
|
2018-01-15 10:34:59 +03:00
|
|
|
}
|
|
|
|
ft_result(res, &*(*self.face).glyph)
|
|
|
|
}
|
|
|
|
}
|
2018-01-16 11:24:49 +03:00
|
|
|
|
2018-01-21 12:32:59 +03:00
|
|
|
pub fn cell_metrics(&mut self) -> (f64, f64) {
|
2018-01-16 11:24:49 +03:00
|
|
|
unsafe {
|
|
|
|
let metrics = &(*(*self.face).size).metrics;
|
2018-03-04 00:00:56 +03:00
|
|
|
let height = (metrics.y_scale as f64 * f64::from((*self.face).height))
|
|
|
|
/ (f64::from(0x1_0000) * 64.0);
|
2018-01-16 11:24:49 +03:00
|
|
|
|
2018-01-21 12:32:59 +03:00
|
|
|
let mut width = 0.0;
|
2018-01-16 11:24:49 +03:00
|
|
|
for i in 32..128 {
|
|
|
|
let glyph_pos = FT_Get_Char_Index(self.face, i);
|
2019-12-15 00:16:57 +03:00
|
|
|
if glyph_pos == 0 {
|
|
|
|
continue;
|
|
|
|
}
|
2018-01-17 10:02:32 +03:00
|
|
|
let res = FT_Load_Glyph(self.face, glyph_pos, FT_LOAD_COLOR as i32);
|
2018-09-20 03:52:34 +03:00
|
|
|
if succeeded(res) {
|
2018-01-16 11:24:49 +03:00
|
|
|
let glyph = &(*(*self.face).glyph);
|
2018-01-21 12:32:59 +03:00
|
|
|
if glyph.metrics.horiAdvance as f64 > width {
|
|
|
|
width = glyph.metrics.horiAdvance as f64;
|
|
|
|
}
|
2018-01-16 11:24:49 +03:00
|
|
|
}
|
|
|
|
}
|
2018-01-21 12:32:59 +03:00
|
|
|
(width / 64.0, height)
|
2018-01-16 11:24:49 +03:00
|
|
|
}
|
|
|
|
}
|
2018-01-15 10:34:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Library {
|
|
|
|
lib: FT_Library,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Library {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {
|
|
|
|
FT_Done_FreeType(self.lib);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Library {
|
|
|
|
pub fn new() -> Result<Library, Error> {
|
|
|
|
let mut lib = ptr::null_mut();
|
|
|
|
let res = unsafe { FT_Init_FreeType(&mut lib as *mut _) };
|
2019-11-02 19:13:51 +03:00
|
|
|
let lib = ft_result(res, lib).context("FT_Init_FreeType")?;
|
2019-12-10 00:42:07 +03:00
|
|
|
let mut lib = Library { lib };
|
|
|
|
|
|
|
|
// Some systems don't support this mode, so if it fails, we don't
|
|
|
|
// care to abort the rest of what we're doing
|
|
|
|
lib.set_lcd_filter(FT_LcdFilter::FT_LCD_FILTER_DEFAULT).ok();
|
|
|
|
|
|
|
|
Ok(lib)
|
2018-01-15 10:34:59 +03:00
|
|
|
}
|
|
|
|
|
2019-12-09 23:12:54 +03:00
|
|
|
pub fn face_from_locator(&self, handle: &FontDataHandle) -> Fallible<Face> {
|
|
|
|
match handle {
|
|
|
|
FontDataHandle::OnDisk { path, index } => {
|
|
|
|
self.new_face(path.to_str().unwrap(), *index as _)
|
|
|
|
}
|
|
|
|
FontDataHandle::Memory { data, index } => self.new_face_from_slice(&data, *index as _),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-19 09:02:31 +03:00
|
|
|
#[allow(dead_code)]
|
2018-01-17 10:02:32 +03:00
|
|
|
pub fn new_face<P>(&self, path: P, face_index: FT_Long) -> Result<Face, Error>
|
2018-01-15 10:34:59 +03:00
|
|
|
where
|
|
|
|
P: Into<Vec<u8>>,
|
|
|
|
{
|
|
|
|
let mut face = ptr::null_mut();
|
2018-01-16 04:32:31 +03:00
|
|
|
let path = CString::new(path.into())?;
|
2018-01-15 10:34:59 +03:00
|
|
|
|
2018-01-17 10:02:32 +03:00
|
|
|
let res = unsafe { FT_New_Face(self.lib, path.as_ptr(), face_index, &mut face as *mut _) };
|
2018-02-21 09:08:19 +03:00
|
|
|
Ok(Face {
|
2019-11-02 19:13:51 +03:00
|
|
|
face: ft_result(res, face).context("FT_New_Face")?,
|
2019-12-09 03:02:04 +03:00
|
|
|
_bytes: Vec::new(),
|
2018-02-21 09:08:19 +03:00
|
|
|
})
|
2018-01-15 10:34:59 +03:00
|
|
|
}
|
|
|
|
|
2019-03-25 21:45:52 +03:00
|
|
|
#[allow(dead_code)]
|
2019-02-18 23:56:41 +03:00
|
|
|
pub fn new_face_from_slice(&self, data: &[u8], face_index: FT_Long) -> Result<Face, Error> {
|
2019-12-09 03:02:04 +03:00
|
|
|
let data = data.to_vec();
|
2019-02-18 23:56:41 +03:00
|
|
|
let mut face = ptr::null_mut();
|
|
|
|
|
|
|
|
let res = unsafe {
|
|
|
|
FT_New_Memory_Face(
|
|
|
|
self.lib,
|
|
|
|
data.as_ptr(),
|
2019-02-19 00:13:36 +03:00
|
|
|
data.len() as _,
|
2019-02-18 23:56:41 +03:00
|
|
|
face_index,
|
|
|
|
&mut face as *mut _,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
Ok(Face {
|
2019-11-02 19:13:51 +03:00
|
|
|
face: ft_result(res, face)
|
|
|
|
.map_err(|e| e.context(format!("FT_New_Memory_Face for index {}", face_index)))?,
|
2019-12-09 03:02:04 +03:00
|
|
|
_bytes: data,
|
2019-02-18 23:56:41 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-01-17 10:02:32 +03:00
|
|
|
pub fn set_lcd_filter(&mut self, filter: FT_LcdFilter) -> Result<(), Error> {
|
|
|
|
unsafe { ft_result(FT_Library_SetLcdFilter(self.lib, filter), ()) }
|
2018-01-15 10:34:59 +03:00
|
|
|
}
|
|
|
|
}
|