fontloader/lib.rs
1//! Compatibility layer for different font engines.
2//!
3//! CoreText is used on Mac OS.
4//! FreeType is used on everything that's not Mac OS.
5//! Eventually, ClearType support will be available for windows.
6
7#![deny(clippy::all, clippy::if_not_else, clippy::enum_glob_use)]
8
9use std::fmt;
10use std::ops::{Add, Mul};
11use std::sync::atomic::{AtomicUsize, Ordering};
12
13// // If target isn't macos or windows, reexport everything from ft.
14// #[cfg(not(any(target_os = "macos", windows)))]
15// pub mod ft;
16// #[cfg(not(any(target_os = "macos", windows)))]
17// pub use ft::FreeTypeFontMap as FontLoader;
18//
19// #[cfg(windows)]
20// pub mod directwrite;
21// #[cfg(windows)]
22// pub use directwrite::DirectWriteLoader as FontLoader;
23//
24// // If target is macos, reexport everything from darwin.
25// #[cfg(target_os = "macos")]
26// mod darwin;
27// #[cfg(target_os = "macos")]
28// pub use darwin::*;
29//
30// #[derive(Debug, Clone, PartialEq, Eq, Hash)]
31// pub struct FontDesc {
32// name: String,
33// style: Style,
34// }
35//
36// #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
37// pub enum Slant {
38// Normal,
39// Italic,
40// Oblique,
41// }
42//
43// #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
44// pub enum Weight {
45// Normal,
46// Bold,
47// }
48//
49// /// Style of font.
50// #[derive(Debug, Clone, PartialEq, Eq, Hash)]
51// pub enum Style {
52// Specific(String),
53// Description { slant: Slant, weight: Weight },
54// }
55//
56// impl fmt::Display for Style {
57// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
58// match *self {
59// Style::Specific(ref s) => f.write_str(s),
60// Style::Description { slant, weight } => {
61// write!(f, "slant={:?}, weight={:?}", slant, weight)
62// },
63// }
64// }
65// }
66//
67// impl FontDesc {
68// pub fn new<S>(name: S, style: Style) -> FontDesc
69// where
70// S: Into<String>,
71// {
72// FontDesc { name: name.into(), style }
73// }
74// }
75//
76// impl fmt::Display for FontDesc {
77// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
78// write!(f, "{} - {}", self.name, self.style)
79// }
80// }
81//
82// /// Identifier for a Font for use in maps/etc.
83// #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
84// pub struct FontKey {
85// token: u32,
86// }
87//
88// impl FontKey {
89// /// Get next font key for given size.
90// ///
91// /// The generated key will be globally unique.
92// pub fn next() -> FontKey {
93// static TOKEN: AtomicUsize = AtomicUsize::new(0);
94//
95// FontKey { token: TOKEN.fetch_add(1, Ordering::SeqCst) as _ }
96// }
97// }
98//
99// #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
100// pub struct GlyphKey {
101// pub character: char,
102// pub font_key: FontKey,
103// pub size: Size,
104// }
105//
106// /// Font size stored as integer.
107// #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
108// pub struct Size(i16);
109//
110// impl Size {
111// /// Create a new `Size` from a f32 size in points.
112// pub fn new(size: f32) -> Size {
113// Size((size * Size::factor()) as i16)
114// }
115//
116// /// Scale factor between font "Size" type and point size.
117// #[inline]
118// pub fn factor() -> f32 {
119// 2.0
120// }
121//
122// /// Get the f32 size in points.
123// pub fn as_f32_pts(self) -> f32 {
124// f32::from(self.0) / Size::factor()
125// }
126// }
127//
128// impl<T: Into<Size>> Add<T> for Size {
129// type Output = Size;
130//
131// fn add(self, other: T) -> Size {
132// Size(self.0.saturating_add(other.into().0))
133// }
134// }
135//
136// impl<T: Into<Size>> Mul<T> for Size {
137// type Output = Size;
138//
139// fn mul(self, other: T) -> Size {
140// Size(self.0 * other.into().0)
141// }
142// }
143//
144// impl From<f32> for Size {
145// fn from(float: f32) -> Size {
146// Size::new(float)
147// }
148// }
149//
150// #[derive(Debug, Clone)]
151// pub struct RasterizedGlyph {
152// pub character: char,
153// pub width: i32,
154// pub height: i32,
155// pub top: i32,
156// pub left: i32,
157// pub advance: (i32, i32),
158// pub buffer: BitmapBuffer,
159// }
160//
161// #[derive(Clone, Debug)]
162// pub enum BitmapBuffer {
163// /// RGB alphamask.
164// Rgb(Vec<u8>),
165//
166// /// RGBA pixels with premultiplied alpha.
167// Rgba(Vec<u8>),
168// }
169//
170// impl Default for RasterizedGlyph {
171// fn default() -> RasterizedGlyph {
172// RasterizedGlyph {
173// character: ' ',
174// width: 0,
175// height: 0,
176// top: 0,
177// left: 0,
178// advance: (0, 0),
179// buffer: BitmapBuffer::Rgb(Vec::new()),
180// }
181// }
182// }
183//
184// #[derive(Debug, Copy, Clone)]
185// pub struct Metrics {
186// pub average_advance: f64,
187// pub line_height: f64,
188// pub descent: f32,
189// pub underline_position: f32,
190// pub underline_thickness: f32,
191// pub strikeout_position: f32,
192// pub strikeout_thickness: f32,
193// }
194//
195// /// Errors occuring when using the rasterizer.
196// #[derive(Debug, error::Error)]
197// pub enum Error {
198// /// Unable to find a font matching the description.
199// #[error("font {0:?} not found")]
200// FontNotFound(FontDesc),
201//
202// /// Unable to find metrics for a font face.
203// #[error("metrics not found")]
204// MetricsNotFound,
205//
206// /// The glyph could not be found in any font.
207// #[error("glyph for character {:?} not found", .0.character)]
208// MissingGlyph(RasterizedGlyph),
209//
210// /// Requested an operation with a FontKey that isn't known to the rasterizer.
211// #[error("invalid font key")]
212// UnknownFontKey,
213//
214// /// Error from platfrom's font system.
215// #[error("{0:?}")]
216// PlatformError(freetype::Error),
217// }
218//
219// pub trait FontMap {
220// /// Create a new Rasterizer.
221// fn new(device_pixel_ratio: f32, use_thin_strokes: bool) -> Result<Self, Error>
222// where
223// Self: Sized;
224//
225// /// Get `Metrics` for the given `FontKey`.
226// fn metrics(&self, _: FontKey, _: Size) -> Result<Metrics, Error>;
227//
228// /// Load the font described by `FontDesc` and `Size`.
229// fn load_font(&mut self, _: &FontDesc, _: Size) -> Result<FontKey, Error>;
230//
231// /// Rasterize the glyph described by `GlyphKey`..
232// fn glyph(&mut self, _: GlyphKey) -> Result<RasterizedGlyph, Error>;
233//
234// /// Update the Rasterizer's DPI factor.
235// fn set_device_pixel_ratio(&mut self, device_pixel_ratio: f32);
236//
237// /// Kerning between two characters.
238// fn kerning(&mut self, left: GlyphKey, right: GlyphKey) -> (f32, f32);
239// }