about summary refs log tree commit diff
path: root/src/x
diff options
context:
space:
mode:
Diffstat (limited to 'src/x')
-rw-r--r--src/x/connection.rs6
-rw-r--r--src/x/display.rs5
-rw-r--r--src/x/font.rs33
-rw-r--r--src/x/font_list.rs61
-rw-r--r--src/x/text_line.rs57
5 files changed, 158 insertions, 4 deletions
diff --git a/src/x/connection.rs b/src/x/connection.rs
index c4e879f..f37f665 100644
--- a/src/x/connection.rs
+++ b/src/x/connection.rs
@@ -7,6 +7,12 @@ use xcb::x::{ Atom, InternAtom };
 
 pub struct Connection(xcb::Connection);
 
+impl PartialEq for Connection {
+  fn eq(&self, other: &Connection) -> bool {
+    self.0.get_raw_conn() == other.0.get_raw_conn()
+  }
+}
+
 impl Connection {
   pub fn connect() -> xcb::ConnResult<(Connection, i32)> {
     let (connection, screen_index)
diff --git a/src/x/display.rs b/src/x/display.rs
index 7aa7e2e..e950484 100644
--- a/src/x/display.rs
+++ b/src/x/display.rs
@@ -16,6 +16,11 @@ impl HasConnection for Arc<Display> {
   fn connection(&self) -> &Connection { &self.connection }
 }
 
+impl PartialEq for Display {
+  fn eq(&self, other: &Display) -> bool {
+    self.connection == other.connection
+  }
+}
 
 impl Display {
   pub fn new() -> xcb::Result<Display> {
diff --git a/src/x/font.rs b/src/x/font.rs
index 8d49e98..3bd641e 100644
--- a/src/x/font.rs
+++ b/src/x/font.rs
@@ -5,7 +5,9 @@ use std::cell::OnceCell;
 use std::ffi::CString;
 use std::sync::Arc;
 
-use x11::xft::{ XftDrawStringUtf8, XftTextExtentsUtf8, XftFontOpenName };
+use x11::xft::{
+  XftCharExists, XftDrawStringUtf8, XftTextExtentsUtf8, XftFontOpenName
+};
 use x11::xrender::XGlyphInfo;
 
 
@@ -22,6 +24,13 @@ pub struct TextSpan {
 }
 
 
+impl PartialEq for Font {
+  fn eq(&self, other: &Font) -> bool {
+    self.display == other.display
+    && self.font_state == other.font_state
+  }
+}
+
 impl Font {
   pub fn new(display: Arc<Display>, name: &str) -> xcb::Result<Arc<Font>> {
     let xlib_display = display.connection().get_xlib_display();
@@ -32,7 +41,10 @@ impl Font {
       XftFontOpenName(xlib_display, screen_index, name.as_ptr().cast())
     };
 
-    let font = Arc::new(Font { display, font_state, metrics: OnceCell::new() });
+    let font = Arc::new(Font {
+      display, font_state,
+      metrics: OnceCell::new(),
+    });
 
     let x_height = font.measure_character_heights("acemnorsuvwxz")?;
     let ascent = font.text_span("bdfhiklt")?.draw_size().height;
@@ -70,6 +82,18 @@ impl Font {
 
     Ok(max_height)
   }
+
+  pub fn check_char_exists(self: &Arc<Self>, codepoint: u32) -> xcb::Result<bool> {
+    let xlib_display = self.display.connection().get_xlib_display();
+
+    let result = unsafe {
+      XftCharExists(xlib_display,
+		    self.font_state,
+		    codepoint)
+    };
+
+    Ok(result != 0)
+  }
 }
 
 
@@ -122,7 +146,7 @@ impl TextSpan {
    * deeply saddening, but there is nothing to be done but deal with it.
    */
   pub fn draw(&self, context: &FontGraphicContext, color: &Color,
-	      location: Point<i32>)
+	      location: &Point<i32>)
     -> xcb::Result<()>
   {
     let xft_draw = context.get_xft_draw();
@@ -146,7 +170,7 @@ impl TextSpan {
   pub fn draw_offset(&self) -> Offset<i16> {
     Offset {
       x: self.extent.x,
-      y: self.extent.y
+      y: self.extent.y,
     }
   }
 
@@ -184,6 +208,7 @@ impl TextSpan {
    * pixel, which causes it to often be smaller than draw_size(). See the note
    * on draw() for some highly relevant philosophy.
    */
+  #[allow(unused)]
   pub fn logical_offset(&self) -> Offset<i16> {
     Offset {
       x: self.extent.xOff,
diff --git a/src/x/font_list.rs b/src/x/font_list.rs
new file mode 100644
index 0000000..d21d8c4
--- /dev/null
+++ b/src/x/font_list.rs
@@ -0,0 +1,61 @@
+use crate::error::{ Error, Result };
+use crate::x::{ Display, Font };
+
+use std::sync::Arc;
+
+
+pub struct FontList {
+  display: Arc<Display>,
+  fonts: Vec<Arc<Font>>,
+}
+
+impl FontList {
+  pub fn new(display: Arc<Display>) -> Result<FontList> {
+    let fonts = Vec::new();
+
+    Ok(FontList { display, fonts })
+  }
+
+  pub fn append(&mut self, name: &str) -> Result<()> {
+    let font = Font::new(Arc::clone(&self.display), name)?;
+
+    self.append_font(font);
+
+    Ok(())
+  }
+
+  pub fn append_font(&mut self, font: Arc<Font>) {
+    self.fonts.push(font);
+  }
+
+  /*   The primary font is the first one in the list. Ascribing meaning to this
+   * is up to the caller.
+   */
+  pub fn primary_font(&self) -> Result<Arc<Font>> {
+    if self.fonts.len() > 0 {
+      return Ok(Arc::clone(&self.fonts[0]));
+    }
+
+    Err(Error::custom("There are no fonts in the list."))
+  }
+
+  /*   This accepts a Unicode codepoint, and returns a font containing it. At
+   * least, that's the hope. The xft docs are thin on the topic of what
+   * encoding the codepoint is in, and it's entirely possible something weird
+   * happens in fonts for which ISO-10646 is not an explicitly provided
+   * encoding, or for which it's provided in an unusual way.
+   *
+   *   If the character isn't found in any font, this returns the primary font,
+   * on the assumption that's the one whose missing-glyph glyph we want.
+   */
+  pub fn find_codepoint(&self, codepoint: u32) -> Result<Arc<Font>> {
+    for font in &self.fonts {
+      if font.check_char_exists(codepoint)? {
+        return Ok(Arc::clone(&font));
+      }
+    }
+
+    self.primary_font()
+  }
+}
+
diff --git a/src/x/text_line.rs b/src/x/text_line.rs
new file mode 100644
index 0000000..579f96f
--- /dev/null
+++ b/src/x/text_line.rs
@@ -0,0 +1,57 @@
+use crate::error::Result;
+use crate::x::{ Font, FontList, TextSpan };
+
+use std::sync::{ Arc, Mutex };
+
+
+pub struct TextLine {
+  fonts: Arc<Mutex<FontList>>,
+  spans: Vec<TextSpan>,
+}
+
+
+impl TextLine {
+  pub fn new(fonts: Arc<Mutex<FontList>>) -> Result<TextLine> {
+    let spans = Vec::new();
+
+    Ok(TextLine { fonts, spans })
+  }
+
+  pub fn append_text(&mut self, text: &str) -> Result<()> {
+    let mut current_span_font: Option<Arc<Font>> = None;
+    let mut current_span_text = String::new();
+
+    for char in text.chars() {
+      let codepoint = char as u32;
+
+      let new_font = &self.fonts.lock()?.find_codepoint(codepoint)?;
+      if let Some(old_font) = &current_span_font {
+        if old_font != new_font {
+          let span = old_font.text_span(&current_span_text)?;
+          self.spans.push(span);
+
+          current_span_font = Some(Arc::clone(new_font));
+          current_span_text = String::new();
+        }
+      } else {
+        current_span_font = Some(Arc::clone(new_font));
+      }
+
+      current_span_text.push(char);
+    }
+
+    if let Some(font) = current_span_font
+       && !current_span_text.is_empty()
+    {
+      let span = font.text_span(&current_span_text)?;
+      self.spans.push(span);
+    }
+
+    Ok(())
+  }
+
+  pub fn spans(&self) -> impl Iterator<Item=&TextSpan> + DoubleEndedIterator {
+    self.spans.iter()
+  }
+}
+