fonts: add support for "variable" fonts

With this configuration:

```lua
local wezterm = require 'wezterm'

return {
  font_dirs = {"/Users/wez/Downloads/Inconsolata"},
  font = wezterm.font("Inconsolata"),
  font_locator = "ConfigDirsOnly"
}
```

wezterm is now able to see the 74 variations that are available
in the single inconsolata ttf.

Running `WEZTERM_LOG=wezterm_font=trace wezterm` will log the
variations.

refs: https://github.com/wez/wezterm/issues/655
This commit is contained in:
Wez Furlong committed 2021-04-07 17:45:38 -07:00
1 parent 38d6c45194
commit e200605b8f
8 files changed
+374 -52

No files matched your search

+1
View File
@@ -4,3 +4,4 @@
#include <freetype/tttables.h>
#include <freetype/ftmodapi.h>
#include <freetype/ftoutln.h>
#include <freetype/ftmm.h>
+274 -17
View File
@@ -1,4 +1,4 @@
/* automatically generated by rust-bindgen */
/* automatically generated by rust-bindgen 0.58.1 */
#![allow(non_snake_case)]
#![allow(non_camel_case_types)]
@@ -14,6 +14,18 @@ pub type FT_UInt64 = u64;
pub const FT_RENDER_POOL_SIZE: u32 = 16384;
pub const FT_MAX_MODULES: u32 = 32;
pub const FT_CHAR_BIT: u32 = 8;
pub const FT_USHORT_MAX: u32 = 65535;
pub const FT_INT_MAX: u32 = 2147483647;
pub const FT_INT_MIN: i32 = -2147483648;
pub const FT_UINT_MAX: u32 = 4294967295;
pub const FT_LONG_MIN: i64 = -9223372036854775808;
pub const FT_LONG_MAX: u64 = 9223372036854775807;
pub const FT_ULONG_MAX: i32 = -1;
pub const FT_SIZEOF_INT: u32 = 4;
pub const FT_SIZEOF_LONG: u32 = 8;
pub const FT_OUTLINE_CONTOURS_MAX: u32 = 32767;
pub const FT_OUTLINE_POINTS_MAX: u32 = 32767;
pub const FT_OUTLINE_NONE: u32 = 0;
pub const FT_OUTLINE_OWNER: u32 = 1;
pub const FT_OUTLINE_EVEN_ODD_FILL: u32 = 2;
@@ -21,6 +33,7 @@ pub const FT_OUTLINE_REVERSE_FILL: u32 = 4;
pub const FT_OUTLINE_IGNORE_DROPOUTS: u32 = 8;
pub const FT_OUTLINE_SMART_DROPOUTS: u32 = 16;
pub const FT_OUTLINE_INCLUDE_STUBS: u32 = 32;
pub const FT_OUTLINE_OVERLAP: u32 = 64;
pub const FT_OUTLINE_HIGH_PRECISION: u32 = 256;
pub const FT_OUTLINE_SINGLE_PASS: u32 = 512;
pub const FT_CURVE_TAG_ON: u32 = 1;
@@ -106,6 +119,8 @@ pub const FT_MODULE_DRIVER_NO_OUTLINES: u32 = 512;
pub const FT_MODULE_DRIVER_HAS_HINTER: u32 = 1024;
pub const FT_MODULE_DRIVER_HINTS_LIGHTLY: u32 = 2048;
pub const FT_DEBUG_HOOK_TRUETYPE: u32 = 0;
pub const FT_VAR_AXIS_FLAG_HIDDEN: u32 = 1;
pub type size_t = ::std::os::raw::c_ulong;
pub type FT_Fast = ::std::os::raw::c_int;
pub type FT_UFast = ::std::os::raw::c_uint;
pub type FT_Memory = *mut FT_MemoryRec_;
@@ -140,7 +155,6 @@ pub type FT_Stream = *mut FT_StreamRec_;
pub union FT_StreamDesc_ {
pub value: ::std::os::raw::c_long,
pub pointer: *mut ::std::os::raw::c_void,
_bindgen_union_align: u64,
}
pub type FT_StreamDesc = FT_StreamDesc_;
pub type FT_Stream_IoFunc = ::std::option::Option<
@@ -185,7 +199,7 @@ pub struct FT_BBox_ {
}
pub type FT_BBox = FT_BBox_;
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum FT_Pixel_Mode_ {
FT_PIXEL_MODE_NONE = 0,
FT_PIXEL_MODE_MONO = 1,
@@ -261,7 +275,7 @@ pub struct FT_Outline_Funcs_ {
}
pub type FT_Outline_Funcs = FT_Outline_Funcs_;
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum FT_Glyph_Format_ {
FT_GLYPH_FORMAT_NONE = 0,
FT_GLYPH_FORMAT_COMPOSITE = 1668246896,
@@ -377,7 +391,7 @@ pub type FT_F26Dot6 = ::std::os::raw::c_long;
pub type FT_Fixed = ::std::os::raw::c_long;
pub type FT_Error = ::std::os::raw::c_int;
pub type FT_Pointer = *mut ::std::os::raw::c_void;
pub type FT_Offset = usize;
pub type FT_Offset = size_t;
pub type FT_PtrDist = isize;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
@@ -453,7 +467,7 @@ pub const FT_Mod_Err_Winfonts: _bindgen_ty_1 = _bindgen_ty_1::FT_Mod_Err_Base;
pub const FT_Mod_Err_GXvalid: _bindgen_ty_1 = _bindgen_ty_1::FT_Mod_Err_Base;
pub const FT_Mod_Err_Max: _bindgen_ty_1 = _bindgen_ty_1::FT_Mod_Err_Max;
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum _bindgen_ty_1 {
FT_Mod_Err_Base = 0,
FT_Mod_Err_Max = 1,
@@ -568,7 +582,7 @@ pub const FT_Err_Corrupted_Font_Header: _bindgen_ty_2 = _bindgen_ty_2::FT_Err_Co
pub const FT_Err_Corrupted_Font_Glyphs: _bindgen_ty_2 = _bindgen_ty_2::FT_Err_Corrupted_Font_Glyphs;
pub const FT_Err_Max: _bindgen_ty_2 = _bindgen_ty_2::FT_Err_Max;
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum _bindgen_ty_2 {
FT_Err_Ok = 0,
FT_Err_Cannot_Open_Resource = 1,
@@ -738,7 +752,7 @@ impl FT_Encoding_ {
pub const FT_ENCODING_MS_JOHAB: FT_Encoding_ = FT_Encoding_::FT_ENCODING_JOHAB;
}
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum FT_Encoding_ {
FT_ENCODING_NONE = 0,
FT_ENCODING_MS_SYMBOL = 1937337698,
@@ -941,7 +955,7 @@ extern "C" {
pub fn FT_Select_Size(face: FT_Face, strike_index: FT_Int) -> FT_Error;
}
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum FT_Size_Request_Type_ {
FT_SIZE_REQUEST_TYPE_NOMINAL = 0,
FT_SIZE_REQUEST_TYPE_REAL_DIM = 1,
@@ -991,7 +1005,7 @@ extern "C" {
pub fn FT_Set_Transform(face: FT_Face, matrix: *mut FT_Matrix, delta: *mut FT_Vector);
}
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum FT_Render_Mode_ {
FT_RENDER_MODE_NORMAL = 0,
FT_RENDER_MODE_LIGHT = 1,
@@ -1005,7 +1019,7 @@ extern "C" {
pub fn FT_Render_Glyph(slot: FT_GlyphSlot, render_mode: FT_Render_Mode) -> FT_Error;
}
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum FT_Kerning_Mode_ {
FT_KERNING_DEFAULT = 0,
FT_KERNING_UNFITTED = 1,
@@ -1066,7 +1080,7 @@ extern "C" {
) -> FT_Error;
}
extern "C" {
pub fn FT_Get_Name_Index(face: FT_Face, glyph_name: *mut FT_String) -> FT_UInt;
pub fn FT_Get_Name_Index(face: FT_Face, glyph_name: *const FT_String) -> FT_UInt;
}
extern "C" {
pub fn FT_Get_SubGlyph_Info(
@@ -1158,7 +1172,7 @@ extern "C" {
pub fn FT_Face_SetUnpatentedHinting(face: FT_Face, value: FT_Bool) -> FT_Bool;
}
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum FT_LcdFilter_ {
FT_LCD_FILTER_NONE = 0,
FT_LCD_FILTER_DEFAULT = 1,
@@ -1182,7 +1196,7 @@ extern "C" {
pub fn FT_Library_SetLcdGeometry(library: FT_Library, sub: *mut FT_Vector) -> FT_Error;
}
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum FT_Sfnt_Tag_ {
FT_SFNT_HEAD = 0,
FT_SFNT_MAXP = 1,
@@ -1285,7 +1299,7 @@ extern "C" {
pub fn FT_Done_Library(library: FT_Library) -> FT_Error;
}
pub type FT_DebugHook_Func =
::std::option::Option<unsafe extern "C" fn(arg: *mut ::std::os::raw::c_void)>;
::std::option::Option<unsafe extern "C" fn(arg: *mut ::std::os::raw::c_void) -> FT_Error>;
extern "C" {
pub fn FT_Set_Debug_Hook(
library: FT_Library,
@@ -1297,7 +1311,7 @@ extern "C" {
pub fn FT_Add_Default_Modules(library: FT_Library);
}
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum FT_TrueTypeEngineType_ {
FT_TRUETYPE_ENGINE_TYPE_NONE = 0,
FT_TRUETYPE_ENGINE_TYPE_UNPATENTED = 1,
@@ -1375,7 +1389,7 @@ impl FT_Orientation_ {
FT_Orientation_::FT_ORIENTATION_POSTSCRIPT;
}
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum FT_Orientation_ {
FT_ORIENTATION_TRUETYPE = 0,
FT_ORIENTATION_POSTSCRIPT = 1,
@@ -1385,3 +1399,246 @@ pub use self::FT_Orientation_ as FT_Orientation;
extern "C" {
pub fn FT_Outline_Get_Orientation(outline: *mut FT_Outline) -> FT_Orientation;
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct PS_FontInfoRec_ {
pub version: *mut FT_String,
pub notice: *mut FT_String,
pub full_name: *mut FT_String,
pub family_name: *mut FT_String,
pub weight: *mut FT_String,
pub italic_angle: FT_Long,
pub is_fixed_pitch: FT_Bool,
pub underline_position: FT_Short,
pub underline_thickness: FT_UShort,
}
pub type PS_FontInfo = *mut PS_FontInfoRec_;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct PS_PrivateRec_ {
pub unique_id: FT_Int,
pub lenIV: FT_Int,
pub num_blue_values: FT_Byte,
pub num_other_blues: FT_Byte,
pub num_family_blues: FT_Byte,
pub num_family_other_blues: FT_Byte,
pub blue_values: [FT_Short; 14usize],
pub other_blues: [FT_Short; 10usize],
pub family_blues: [FT_Short; 14usize],
pub family_other_blues: [FT_Short; 10usize],
pub blue_scale: FT_Fixed,
pub blue_shift: FT_Int,
pub blue_fuzz: FT_Int,
pub standard_width: [FT_UShort; 1usize],
pub standard_height: [FT_UShort; 1usize],
pub num_snap_widths: FT_Byte,
pub num_snap_heights: FT_Byte,
pub force_bold: FT_Bool,
pub round_stem_up: FT_Bool,
pub snap_widths: [FT_Short; 13usize],
pub snap_heights: [FT_Short; 13usize],
pub expansion_factor: FT_Fixed,
pub language_group: FT_Long,
pub password: FT_Long,
pub min_feature: [FT_Short; 2usize],
}
pub type PS_Private = *mut PS_PrivateRec_;
extern "C" {
pub fn FT_Has_PS_Glyph_Names(face: FT_Face) -> FT_Int;
}
extern "C" {
pub fn FT_Get_PS_Font_Info(face: FT_Face, afont_info: PS_FontInfo) -> FT_Error;
}
extern "C" {
pub fn FT_Get_PS_Font_Private(face: FT_Face, afont_private: PS_Private) -> FT_Error;
}
impl PS_Dict_Keys_ {
pub const PS_DICT_MAX: PS_Dict_Keys_ = PS_Dict_Keys_::PS_DICT_ITALIC_ANGLE;
}
#[repr(u32)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum PS_Dict_Keys_ {
PS_DICT_FONT_TYPE = 0,
PS_DICT_FONT_MATRIX = 1,
PS_DICT_FONT_BBOX = 2,
PS_DICT_PAINT_TYPE = 3,
PS_DICT_FONT_NAME = 4,
PS_DICT_UNIQUE_ID = 5,
PS_DICT_NUM_CHAR_STRINGS = 6,
PS_DICT_CHAR_STRING_KEY = 7,
PS_DICT_CHAR_STRING = 8,
PS_DICT_ENCODING_TYPE = 9,
PS_DICT_ENCODING_ENTRY = 10,
PS_DICT_NUM_SUBRS = 11,
PS_DICT_SUBR = 12,
PS_DICT_STD_HW = 13,
PS_DICT_STD_VW = 14,
PS_DICT_NUM_BLUE_VALUES = 15,
PS_DICT_BLUE_VALUE = 16,
PS_DICT_BLUE_FUZZ = 17,
PS_DICT_NUM_OTHER_BLUES = 18,
PS_DICT_OTHER_BLUE = 19,
PS_DICT_NUM_FAMILY_BLUES = 20,
PS_DICT_FAMILY_BLUE = 21,
PS_DICT_NUM_FAMILY_OTHER_BLUES = 22,
PS_DICT_FAMILY_OTHER_BLUE = 23,
PS_DICT_BLUE_SCALE = 24,
PS_DICT_BLUE_SHIFT = 25,
PS_DICT_NUM_STEM_SNAP_H = 26,
PS_DICT_STEM_SNAP_H = 27,
PS_DICT_NUM_STEM_SNAP_V = 28,
PS_DICT_STEM_SNAP_V = 29,
PS_DICT_FORCE_BOLD = 30,
PS_DICT_RND_STEM_UP = 31,
PS_DICT_MIN_FEATURE = 32,
PS_DICT_LEN_IV = 33,
PS_DICT_PASSWORD = 34,
PS_DICT_LANGUAGE_GROUP = 35,
PS_DICT_VERSION = 36,
PS_DICT_NOTICE = 37,
PS_DICT_FULL_NAME = 38,
PS_DICT_FAMILY_NAME = 39,
PS_DICT_WEIGHT = 40,
PS_DICT_IS_FIXED_PITCH = 41,
PS_DICT_UNDERLINE_POSITION = 42,
PS_DICT_UNDERLINE_THICKNESS = 43,
PS_DICT_FS_TYPE = 44,
PS_DICT_ITALIC_ANGLE = 45,
}
pub use self::PS_Dict_Keys_ as PS_Dict_Keys;
extern "C" {
pub fn FT_Get_PS_Font_Value(
face: FT_Face,
key: PS_Dict_Keys,
idx: FT_UInt,
value: *mut ::std::os::raw::c_void,
value_len: FT_Long,
) -> FT_Long;
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct FT_MM_Axis_ {
pub name: *mut FT_String,
pub minimum: FT_Long,
pub maximum: FT_Long,
}
pub type FT_MM_Axis = FT_MM_Axis_;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct FT_Multi_Master_ {
pub num_axis: FT_UInt,
pub num_designs: FT_UInt,
pub axis: [FT_MM_Axis; 4usize],
}
pub type FT_Multi_Master = FT_Multi_Master_;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct FT_Var_Axis_ {
pub name: *mut FT_String,
pub minimum: FT_Fixed,
pub def: FT_Fixed,
pub maximum: FT_Fixed,
pub tag: FT_ULong,
pub strid: FT_UInt,
}
pub type FT_Var_Axis = FT_Var_Axis_;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct FT_Var_Named_Style_ {
pub coords: *mut FT_Fixed,
pub strid: FT_UInt,
pub psid: FT_UInt,
}
pub type FT_Var_Named_Style = FT_Var_Named_Style_;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct FT_MM_Var_ {
pub num_axis: FT_UInt,
pub num_designs: FT_UInt,
pub num_namedstyles: FT_UInt,
pub axis: *mut FT_Var_Axis,
pub namedstyle: *mut FT_Var_Named_Style,
}
pub type FT_MM_Var = FT_MM_Var_;
extern "C" {
pub fn FT_Get_Multi_Master(face: FT_Face, amaster: *mut FT_Multi_Master) -> FT_Error;
}
extern "C" {
pub fn FT_Get_MM_Var(face: FT_Face, amaster: *mut *mut FT_MM_Var) -> FT_Error;
}
extern "C" {
pub fn FT_Done_MM_Var(library: FT_Library, amaster: *mut FT_MM_Var) -> FT_Error;
}
extern "C" {
pub fn FT_Set_MM_Design_Coordinates(
face: FT_Face,
num_coords: FT_UInt,
coords: *mut FT_Long,
) -> FT_Error;
}
extern "C" {
pub fn FT_Set_Var_Design_Coordinates(
face: FT_Face,
num_coords: FT_UInt,
coords: *mut FT_Fixed,
) -> FT_Error;
}
extern "C" {
pub fn FT_Get_Var_Design_Coordinates(
face: FT_Face,
num_coords: FT_UInt,
coords: *mut FT_Fixed,
) -> FT_Error;
}
extern "C" {
pub fn FT_Set_MM_Blend_Coordinates(
face: FT_Face,
num_coords: FT_UInt,
coords: *mut FT_Fixed,
) -> FT_Error;
}
extern "C" {
pub fn FT_Get_MM_Blend_Coordinates(
face: FT_Face,
num_coords: FT_UInt,
coords: *mut FT_Fixed,
) -> FT_Error;
}
extern "C" {
pub fn FT_Set_Var_Blend_Coordinates(
face: FT_Face,
num_coords: FT_UInt,
coords: *mut FT_Fixed,
) -> FT_Error;
}
extern "C" {
pub fn FT_Get_Var_Blend_Coordinates(
face: FT_Face,
num_coords: FT_UInt,
coords: *mut FT_Fixed,
) -> FT_Error;
}
extern "C" {
pub fn FT_Set_MM_WeightVector(
face: FT_Face,
len: FT_UInt,
weightvector: *mut FT_Fixed,
) -> FT_Error;
}
extern "C" {
pub fn FT_Get_MM_WeightVector(
face: FT_Face,
len: *mut FT_UInt,
weightvector: *mut FT_Fixed,
) -> FT_Error;
}
extern "C" {
pub fn FT_Get_Var_Axis_Flags(
master: *mut FT_MM_Var,
axis_index: FT_UInt,
flags: *mut FT_UInt,
) -> FT_Error;
}
extern "C" {
pub fn FT_Set_Named_Instance(face: FT_Face, instance_index: FT_UInt) -> FT_Error;
}
+1 -1
View File
@@ -24,7 +24,7 @@ impl Entry {
use ttf_parser::Face;
let (data, index) = match &self.handle {
FontDataHandle::Memory { data, index, .. } => (data.clone(), *index),
FontDataHandle::OnDisk { path, index } => {
FontDataHandle::OnDisk { path, index, .. } => {
let data = std::fs::read(path)
.with_context(|| anyhow!("reading font data from {}", path.display()))?;
(Cow::Owned(data), *index)
+53 -6
View File
@@ -1,6 +1,7 @@
//! Higher level freetype bindings
use crate::locator::FontDataHandle;
use crate::parser::Names;
use anyhow::{anyhow, Context};
use config::{configuration, FreeTypeLoadTarget};
pub use freetype::*;
@@ -76,6 +77,7 @@ pub struct Face {
pub face: FT_Face,
_bytes: CowVecU8,
size: Option<FaceSize>,
lib: FT_Library,
}
impl Drop for Face {
@@ -128,6 +130,28 @@ impl Face {
}
}
pub fn variations(&self) -> anyhow::Result<Vec<Names>> {
let mut mm = std::ptr::null_mut();
unsafe {
ft_result(FT_Get_MM_Var(self.face, &mut mm), ()).context("FT_Get_MM_Var")?;
let mut res = vec![];
let num_styles = (*mm).num_namedstyles;
for i in 1..=num_styles {
FT_Set_Named_Instance(self.face, i);
res.push(Names::from_ft_face(&self));
}
FT_Done_MM_Var(self.lib, mm);
FT_Set_Named_Instance(self.face, 0);
log::debug!("Variations: {:#?}", res);
Ok(res)
}
}
/// 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, point_size: f64, dpi: u32) -> anyhow::Result<(f64, f64)> {
@@ -356,14 +380,34 @@ impl Library {
}
pub fn face_from_locator(&self, handle: &FontDataHandle) -> anyhow::Result<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.clone(), *index as _)
let (face, variation) = match handle {
FontDataHandle::OnDisk {
path,
index,
variation,
} => (
self.new_face(path.to_str().unwrap(), *index as _)?,
*variation,
),
FontDataHandle::Memory {
data,
index,
variation,
..
} => (
self.new_face_from_slice(data.clone(), *index as _)?,
*variation,
),
};
if variation != 0 {
unsafe {
ft_result(FT_Set_Named_Instance(face.face, variation), ())
.context("FT_Set_Named_Instance")?;
}
}
Ok(face)
}
pub fn new_face<P>(&self, path: P, face_index: FT_Long) -> anyhow::Result<Face>
@@ -392,6 +436,7 @@ impl Library {
})?,
_bytes: CowVecU8::Borrowed(b""),
size: None,
lib: self.lib,
});
}
}
@@ -425,6 +470,7 @@ impl Library {
})?,
_bytes: data,
size: None,
lib: self.lib,
})
}
@@ -445,6 +491,7 @@ impl Library {
.with_context(|| format!("FT_New_Memory_Face for index {}", face_index))?,
_bytes: data,
size: None,
lib: self.lib,
})
}
+4 -20
View File
@@ -9,9 +9,7 @@ use core_foundation::number::CFNumber;
use core_foundation::string::CFString;
use core_text::font::*;
use core_text::font_descriptor::*;
use std::borrow::Cow;
use std::collections::HashSet;
use ttf_parser::fonts_in_collection;
lazy_static::lazy_static! {
static ref FALLBACK: Vec<FontDataHandle> = build_fallback_list();
@@ -56,28 +54,14 @@ fn descriptor_from_attr(attr: &FontAttributes) -> anyhow::Result<CTFontDescripto
/// the descriptor is referencing.
fn handle_from_descriptor(descriptor: &CTFontDescriptor) -> Option<FontDataHandle> {
let path = descriptor.font_path()?;
let name = descriptor.display_name();
let family_name = descriptor.family_name();
let data = std::fs::read(&path).ok()?;
let size = fonts_in_collection(&data).unwrap_or(1);
let mut font_info = vec![];
crate::parser::parse_and_collect_font_info(&path, &mut font_info).ok()?;
let mut handle = FontDataHandle::Memory {
data: Cow::Owned(data),
name,
index: 0,
};
for index in 0..size {
if let FontDataHandle::Memory { index: idx, .. } = &mut handle {
*idx = index;
}
let parsed = crate::parser::ParsedFont::from_locator(&handle).ok()?;
let names = parsed.names();
for (names, _, locator) in font_info {
if names.full_name == family_name || names.family.as_ref() == Some(&family_name) {
// Switch to an OnDisk handle so that we don't hold
// all of the fallback fonts in memory
return Some(FontDataHandle::OnDisk { path, index });
return Some(locator);
}
}
+21 -4
View File
@@ -21,11 +21,13 @@ pub enum FontDataHandle {
OnDisk {
path: PathBuf,
index: u32,
variation: u32,
},
Memory {
name: String,
data: std::borrow::Cow<'static, [u8]>,
index: u32,
variation: u32,
},
}
@@ -53,24 +55,28 @@ impl PartialEq for FontDataHandle {
Self::OnDisk {
path: path_a,
index: index_a,
variation: variation_a,
},
Self::OnDisk {
path: path_b,
index: index_b,
variation: variation_b,
},
) => path_a == path_b && index_a == index_b,
) => path_a == path_b && index_a == index_b && variation_a == variation_b,
(
Self::Memory {
name: name_a,
index: index_a,
variation: variation_a,
..
},
Self::Memory {
name: name_b,
index: index_b,
variation: variation_b,
..
},
) => name_a == name_b && index_a == index_b,
) => name_a == name_b && index_a == index_b && variation_a == variation_b,
_ => false,
}
}
@@ -93,16 +99,27 @@ impl PartialOrd for FontDataHandle {
impl std::fmt::Debug for FontDataHandle {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
match self {
Self::OnDisk { path, index } => fmt
Self::OnDisk {
path,
index,
variation,
} => fmt
.debug_struct("OnDisk")
.field("path", &path)
.field("index", &index)
.field("variation", &variation)
.finish(),
Self::Memory { data, index, name } => fmt
Self::Memory {
data,
index,
name,
variation,
} => fmt
.debug_struct("Memory")
.field("name", &name)
.field("data_len", &data.len())
.field("index", &index)
.field("variation", &variation)
.finish(),
}
}
+19 -3
View File
@@ -26,7 +26,7 @@ pub struct Names {
}
impl Names {
fn from_ft_face(face: &crate::ftwrap::Face) -> Names {
pub fn from_ft_face(face: &crate::ftwrap::Face) -> Names {
let postscript_name = face.postscript_name();
let family = face.family_name();
let sub_family = face.style_name();
@@ -154,6 +154,7 @@ pub(crate) fn load_built_in_fonts(
data: Cow::Borrowed(data),
index: 0,
name: name.to_string(),
variation: 0,
},
));
}
@@ -178,11 +179,26 @@ pub(crate) fn parse_and_collect_font_info(
let locator = FontDataHandle::OnDisk {
path: path.to_path_buf(),
index,
variation: 0,
};
let face = lib.face_from_locator(&locator)?;
let names = Names::from_ft_face(&face);
font_info.push((names, path.to_path_buf(), locator));
if let Ok(variations) = face.variations() {
for (variation, names) in variations.into_iter().enumerate() {
font_info.push((
names,
path.to_path_buf(),
FontDataHandle::OnDisk {
path: path.to_path_buf(),
index,
variation: variation as u32 + 1,
},
));
}
} else {
let names = Names::from_ft_face(&face);
font_info.push((names, path.to_path_buf(), locator));
}
Ok(())
}
+1 -1
View File
@@ -61,7 +61,7 @@ impl ParsedFont {
pub fn from_locator(handle: &FontDataHandle) -> anyhow::Result<Self> {
let (data, index) = match handle {
FontDataHandle::Memory { data, index, .. } => (data.to_vec(), *index),
FontDataHandle::OnDisk { path, index } => {
FontDataHandle::OnDisk { path, index, .. } => {
let data = std::fs::read(path)?;
(data, *index)
}