aboutsummaryrefslogtreecommitdiff
path: root/font/src/lib.rs
diff options
context:
space:
mode:
authorJoe Wilm <joe@jwilm.com>2016-06-09 20:39:40 -0700
committerJoe Wilm <joe@jwilm.com>2016-06-14 07:39:06 -0700
commitbd8bd26c8bd6f9dfc988e222b57a71cf94902c4d (patch)
tree5e7211ac03e5481b8fb8b89f3b6c6a4246247f29 /font/src/lib.rs
parent2395066318fea516bc0efbc7aecfb90a0d2548da (diff)
downloadr-alacritty-bd8bd26c8bd6f9dfc988e222b57a71cf94902c4d.tar.gz
r-alacritty-bd8bd26c8bd6f9dfc988e222b57a71cf94902c4d.tar.bz2
r-alacritty-bd8bd26c8bd6f9dfc988e222b57a71cf94902c4d.zip
Add support for macOS
Alacritty now runs on macOS using CoreText for font rendering. The font rendering subsystems were moved into a separate crate called `font`. The font crate provides a unified (albeit limited) API which wraps CoreText on macOS and FreeType/FontConfig on other platforms. The unified API differed slightly from what the original Rasterizer for freetype implemented, and it was updated accordingly. The cell separation properties (sep_x and sep_y) are now premultiplied into the cell width and height. They were previously passed through as uniforms to the shaders; removing them prevents a lot of redundant work. `libc` has some differences between Linux and macOS. `__errno_location` is not available on macOS, and the `errno` crate was brought in to provide a cross-platform API for dealing with errno. Differences in `openpty` were handled by implementing a macOS specific version. It would be worth investigating a way to unify the implementations at some point. A type mismatch with TIOCSCTTY was resolved with a cast. Differences in libc::passwd struct fields were resolved by using std::mem::uninitialized instead of zeroing the struct ourselves. This has the benefit of being much cleaner. The thread setup had to be changed to support both macOS and Linux. macOS requires that events from the window be handled on the main thread. Failure to do so will prevent the glutin window from even showing up! For this reason, the renderer and parser were moved to their own thread, and the input is received on the main thread. This is essentially reverse the setup prior to this commit. Renderer initialization (and thus font cache initialization) had to be moved to the rendering thread as well since there's no way to make_context(null) with glx on Linux. Trying to just call make_context a second time on the rendering thread had resulted in a panic!.
Diffstat (limited to 'font/src/lib.rs')
-rw-r--r--font/src/lib.rs94
1 files changed, 94 insertions, 0 deletions
diff --git a/font/src/lib.rs b/font/src/lib.rs
new file mode 100644
index 00000000..ba632bee
--- /dev/null
+++ b/font/src/lib.rs
@@ -0,0 +1,94 @@
+//! Compatibility layer for different font engines
+//!
+//! This module is developed as part of Alacritty; Alacritty does not include Windows support
+//! as a goal at this time, and neither does this module.
+//!
+//! CoreText is used on Mac OS.
+//! FreeType is used on everything that's not Mac OS.
+
+#[cfg(not(target_os = "macos"))]
+extern crate fontconfig;
+#[cfg(not(target_os = "macos"))]
+extern crate freetype;
+#[cfg(not(target_os = "macos"))]
+extern crate libc;
+
+#[cfg(target_os = "macos")]
+extern crate core_text;
+#[cfg(target_os = "macos")]
+extern crate core_foundation;
+#[cfg(target_os = "macos")]
+extern crate core_foundation_sys;
+#[cfg(target_os = "macos")]
+extern crate core_graphics;
+
+extern crate euclid;
+
+use std::fmt;
+
+// If target isn't macos, reexport everything from ft
+#[cfg(not(target_os = "macos"))]
+mod ft;
+#[cfg(not(target_os = "macos"))]
+pub use ft::*;
+
+// If target is macos, reexport everything from darwin
+#[cfg(target_os = "macos")]
+mod darwin;
+#[cfg(target_os = "macos")]
+pub use darwin::*;
+
+#[derive(Clone, PartialEq, Eq, Hash)]
+pub struct FontDesc {
+ name: String,
+ style: String,
+}
+
+impl FontDesc {
+ pub fn new<S>(name: S, style: S) -> FontDesc
+ where S: Into<String>
+ {
+ FontDesc {
+ name: name.into(),
+ style: style.into()
+ }
+ }
+}
+
+pub struct RasterizedGlyph {
+ pub c: char,
+ pub width: i32,
+ pub height: i32,
+ pub top: i32,
+ pub left: i32,
+ pub buf: Vec<u8>,
+}
+
+struct BufDebugger<'a>(&'a [u8]);
+
+impl<'a> fmt::Debug for BufDebugger<'a> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ f.debug_struct("GlyphBuffer")
+ .field("len", &self.0.len())
+ .field("bytes", &self.0)
+ .finish()
+ }
+}
+
+impl fmt::Debug for RasterizedGlyph {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ f.debug_struct("RasterizedGlyph")
+ .field("c", &self.c)
+ .field("width", &self.width)
+ .field("height", &self.height)
+ .field("top", &self.top)
+ .field("left", &self.left)
+ .field("buf", &BufDebugger(&self.buf[..]))
+ .finish()
+ }
+}
+
+pub struct Metrics {
+ pub average_advance: f64,
+ pub line_height: f64,
+}