aboutsummaryrefslogtreecommitdiff
path: root/font/src/lib.rs
diff options
context:
space:
mode:
authorJoe Wilm <joe@jwilm.com>2016-08-12 13:38:41 -0500
committerJoe Wilm <joe@jwilm.com>2016-08-12 19:30:14 -0500
commitb70394170c0fa9cd4e4be8150121a698a426a069 (patch)
tree41d84240a4e58df201fbd7f4f984c3814c7cb055 /font/src/lib.rs
parent874719580ce3e12f09d01a9a53c5e64205ef5b5d (diff)
downloadalacritty-b70394170c0fa9cd4e4be8150121a698a426a069.tar.gz
alacritty-b70394170c0fa9cd4e4be8150121a698a426a069.zip
Support bold/italic font rendering on macOS
This patch adds support for rendering italic fonts and bold fonts. The `font` crate has a couple of new paradigms to support this: font keys and glyph keys. `FontKey` is a lightweight (4 byte) identifier for a font loaded out of the rasterizer. This replaces `FontDesc` for rasterizing glyphs from a loaded font. `FontDesc` had the problem that it contained two strings, and the glyph cache needs to store a copy of the font key for every loaded glyph. `GlyphKey` is now passed to the glyph rasterization method instead of a simple `char`. `GlyphKey` contains information including font, size, and the character. The rasterizer APIs do not define what happens when loading the same font from a `FontDesc` more than once. It is assumed that the application will track the resulting `FontKey` instead of asking the font to be loaded multiple times.
Diffstat (limited to 'font/src/lib.rs')
-rw-r--r--font/src/lib.rs52
1 files changed, 51 insertions, 1 deletions
diff --git a/font/src/lib.rs b/font/src/lib.rs
index a42020d8..74563b1a 100644
--- a/font/src/lib.rs
+++ b/font/src/lib.rs
@@ -19,6 +19,7 @@
//!
//! CoreText is used on Mac OS.
//! FreeType is used on everything that's not Mac OS.
+#![feature(integer_atomics)]
#[cfg(not(target_os = "macos"))]
extern crate fontconfig;
@@ -38,6 +39,7 @@ extern crate euclid;
extern crate libc;
use std::fmt;
+use std::sync::atomic::{AtomicU32, ATOMIC_U32_INIT, Ordering};
// If target isn't macos, reexport everything from ft
#[cfg(not(target_os = "macos"))]
@@ -51,7 +53,7 @@ mod darwin;
#[cfg(target_os = "macos")]
pub use darwin::*;
-#[derive(Clone, PartialEq, Eq, Hash)]
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct FontDesc {
name: String,
style: String,
@@ -68,6 +70,54 @@ impl FontDesc {
}
}
+/// Identifier for a Font for use in maps/etc
+#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
+pub struct FontKey {
+ token: u32,
+}
+
+impl FontKey {
+ /// Get next font key for given size
+ ///
+ /// The generated key will be globally unique
+ pub fn next() -> FontKey {
+ static TOKEN: AtomicU32 = ATOMIC_U32_INIT;
+
+ FontKey {
+ token: TOKEN.fetch_add(1, Ordering::SeqCst),
+ }
+ }
+}
+
+#[derive(Debug, Clone, Hash, PartialEq, Eq)]
+pub struct GlyphKey {
+ pub c: char,
+ pub font_key: FontKey,
+ pub size: Size,
+}
+
+/// Font size stored as integer
+#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
+pub struct Size(i32);
+
+impl Size {
+ /// Scale factor between font "Size" type and point size
+ #[inline]
+ pub fn factor() -> f32 {
+ 2.0
+ }
+
+ /// Create a new `Size` from a f32 size in points
+ pub fn new(size: f32) -> Size {
+ Size((size * Size::factor()) as i32)
+ }
+
+ /// Get the f32 size in points
+ pub fn as_f32_pts(self) -> f32 {
+ self.0 as f32 / Size::factor()
+ }
+}
+
pub struct RasterizedGlyph {
pub c: char,
pub width: i32,