about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/x.rs4
-rw-r--r--src/x/font.rs173
-rw-r--r--src/x/text_span.rs175
3 files changed, 190 insertions, 162 deletions
diff --git a/src/x.rs b/src/x.rs
index 0782649..7441dc3 100644
--- a/src/x.rs
+++ b/src/x.rs
@@ -3,11 +3,12 @@ pub use color::{ Color, DisplayColorExt };
 pub use context::{ GraphicContext, HasGraphicContext, GraphicContextExt };
 pub use connection::{ Connection, HasConnection, ConnectionExt };
 pub use display::{ Display, HasDisplay, DisplayExt };
-pub use font::{ Font, TextSpan };
+pub use font::Font;
 pub use font_list::FontList;
 pub use font_context::FontGraphicContext;
 pub use geometry::{ X_CLOCKWISE_RIGHT_ANGLE, SideExt, RectangleExt };
 pub use text_line::TextLine;
+pub use text_span::TextSpan;
 pub use window::{ Window, HasWindow, WindowExt };
 
 pub mod atoms;
@@ -20,5 +21,6 @@ pub mod font;
 pub mod font_list;
 pub mod geometry;
 pub mod text_line;
+pub mod text_span;
 pub mod window;
 
diff --git a/src/x/font.rs b/src/x/font.rs
index 2dbfb75..8b81fda 100644
--- a/src/x/font.rs
+++ b/src/x/font.rs
@@ -19,13 +19,6 @@ pub struct Font {
   metrics: OnceCell<(u16,u16)>,
 }
 
-pub struct TextSpan {
-  font: Arc<Font>,
-  text: CString,
-  extent: XGlyphInfo,
-}
-
-
 impl PartialEq for Font {
   fn eq(&self, other: &Font) -> bool {
     self.display == other.display
@@ -33,6 +26,14 @@ impl PartialEq for Font {
   }
 }
 
+impl HasConnection for Font {
+  fn connection(&self) -> &Connection { self.display.connection() }
+}
+
+impl HasDisplay for Font {
+  fn display(&self) -> &Arc<Display> { &self.display }
+}
+
 impl Font {
   pub fn new(display: Arc<Display>, name: &str) -> Result<Arc<Font>> {
     let xlib_display = display.connection().get_xlib_display();
@@ -57,6 +58,10 @@ impl Font {
     Ok(font)
   }
 
+  pub fn font_state(&self) -> *mut x11::xft::XftFont {
+    self.font_state
+  }
+
   pub fn ascent(self: &Arc<Self>) -> u16 {
     let (ascent, _) = self.metrics.get().unwrap();
 
@@ -98,157 +103,3 @@ impl Font {
   }
 }
 
-
-impl TextSpan {
-  pub fn new(font: Arc<Font>, text: &str) -> Result<Self> {
-    let xlib_display = font.display.connection().get_xlib_display();
-
-    let text = CString::new(text).unwrap();
-
-    let mut extent = XGlyphInfo {
-      width: 0,
-      height: 0,
-      x: 0,
-      y: 0,
-      xOff: 0,
-      yOff: 0,
-    };
-    unsafe {
-      XftTextExtentsUtf8(xlib_display,
-			 font.font_state,
-			 text.as_ptr().cast(),
-			 text.count_bytes() as i32,
-			 &mut extent)
-    };
-
-    Ok(TextSpan { font, text, extent })
-  }
-
-  /*   The coordinates given to draw() are those of the left-side baseline of
-   * the span's as-drawn pixel boundaries, in window coordinate space. Recall
-   * that window coordinate space puts the origin at the top-left.
-   *
-   *   As-drawn boundaries are about what pixels actually get marked on the
-   * screen, which include the descender and any marks that are outside the
-   * font's bounds as described by its metrics.
-   *
-   *   Though this can be counterintuitive to programmers, it is normal for
-   * fonts to draw outside their bounds. Font metrics are primarily intended
-   * to advise about graphic-design topics, which are socially defined and do
-   * not necessarily obey mathematical identities, even when such identities
-   * are offered as explanations of their meaning.
-   *
-   *   Instead, the mathematical meaning of these metrics serves as a first
-   * layer, a literal meaning which provides a reference point for additional
-   * layers of meaning to be added atop, in the same fashion as how the
-   * duration of a musical measure is not actually fixed during a live
-   * performance.
-   *
-   *   There are very good reasons that programmers find this state of affairs
-   * deeply saddening, but there is nothing to be done but deal with it.
-   */
-  pub fn draw(&self, context: &FontGraphicContext, color: &Color,
-	      location: &Point<i32>)
-    -> Result<()>
-  {
-    let xft_draw = context.get_xft_draw();
-
-    unsafe {
-      XftDrawStringUtf8(xft_draw,
-			color.as_xft_color(),
-			self.font.font_state,
-			location.x, location.y,
-                        self.text.as_ptr().cast(),
-			self.text.count_bytes() as i32)
-    };
-
-    Ok(())
-  }
-
-  /*   This returns the offset from the top-left of the span's logical parcel to the
-   * left-side baseline of its as-drawn pixel boundaries. See the note on draw() for
-   * more context on what these as-drawn boundaries mean and why.
-   */
-  pub fn draw_offset(&self) -> Offset<i16> {
-    Offset {
-      x: self.extent.x,
-      y: self.extent.y,
-    }
-  }
-
-  /*   This is the as-drawn pixel size, rounded up to the next whole pixel,
-   * so that it is safe to use as a bounding box. It is computed only with
-   * respect to characters present in the span, and only considers the marks
-   * they actually draw.
-   *
-   *   There are two important implications. First, due to rounding,
-   * pixel_size() is likely to be wider than logical_offset(). Second, trailing
-   * spaces are not included, since they do not draw any marks.
-   *
-   *   In the vertical dimension, this size is conceptually similar to the
-   * maximum ascent plus the maximum descent of any character present in the
-   * span. Since there is no way within xft to obtain the font's declared
-   * ascent or descent, this measurement may be the best way to approximate
-   * those measurements. Note that there is nothing that even approximates line
-   * height; just guess.
-   *
-   *   If you're thinking this all sounds highly inconvenient, you are correct.
-   * See the note on draw() for some highly relevant philosophy.
-   */
-  pub fn draw_size(&self) -> Size<u16> {
-    Size {
-      width: self.extent.width,
-      height: self.extent.height,
-    }
-  }
-
-  /*   This returns the offset from the left-side baseline of the span's
-   * logical parcel to the new left-side baseline that a span occurring
-   * immediately after it would start from.
-   *
-   *   This is rounded in some fashion, most likely to the nearest whole
-   * 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,
-      y: self.extent.yOff,
-    }
-  }
-
-  pub fn layout_and_draw(&self,
-                         open_parcel: &mut Parcel, location: &mut Point<i32>,
-                         alignment: Alignment, context: &FontGraphicContext,
-                         color: &Color)
-    -> Result<()>
-  {
-    match alignment {
-      Alignment::Left => {
-        location.x += self.draw_offset().x as i32;
-      }
-      Alignment::Right => {
-        location.x -= self.draw_size().width as i32;
-      }
-    }
-
-    self.draw(&context, color, &location)?;
-
-    match alignment {
-      Alignment::Left => {
-        location.x += self.draw_size().width as i32;
-
-        open_parcel.left += self.draw_size().width as u32;
-      }
-      Alignment::Right => {
-        location.x -= self.draw_offset().x as i32;
-
-        open_parcel.right -= self.draw_size().width as u32;
-      }
-    }
-
-    Ok(())
-  }
-}
-
diff --git a/src/x/text_span.rs b/src/x/text_span.rs
new file mode 100644
index 0000000..1f9f2ab
--- /dev/null
+++ b/src/x/text_span.rs
@@ -0,0 +1,175 @@
+use crate::error::Result;
+use crate::layout::Parcel;
+use crate::geometry::{ Alignment, Point, Offset, Size };
+use crate::x::*;
+
+use std::cell::OnceCell;
+use std::ffi::CString;
+use std::sync::Arc;
+
+use x11::xft::{
+  XftCharExists, XftDrawStringUtf8, XftTextExtentsUtf8, XftFontOpenName
+};
+use x11::xrender::XGlyphInfo;
+
+
+pub struct TextSpan {
+  font: Arc<Font>,
+  text: CString,
+  extent: XGlyphInfo,
+}
+
+
+impl TextSpan {
+  pub fn new(font: Arc<Font>, text: &str) -> Result<Self> {
+    let xlib_display = font.connection().get_xlib_display();
+
+    let text = CString::new(text).unwrap();
+
+    let mut extent = XGlyphInfo {
+      width: 0,
+      height: 0,
+      x: 0,
+      y: 0,
+      xOff: 0,
+      yOff: 0,
+    };
+    unsafe {
+      XftTextExtentsUtf8(xlib_display,
+			 font.font_state(),
+			 text.as_ptr().cast(),
+			 text.count_bytes() as i32,
+			 &mut extent)
+    };
+
+    Ok(TextSpan { font, text, extent })
+  }
+
+  /*   The coordinates given to draw() are those of the left-side baseline of
+   * the span's as-drawn pixel boundaries, in window coordinate space. Recall
+   * that window coordinate space puts the origin at the top-left.
+   *
+   *   As-drawn boundaries are about what pixels actually get marked on the
+   * screen, which include the descender and any marks that are outside the
+   * font's bounds as described by its metrics.
+   *
+   *   Though this can be counterintuitive to programmers, it is normal for
+   * fonts to draw outside their bounds. Font metrics are primarily intended
+   * to advise about graphic-design topics, which are socially defined and do
+   * not necessarily obey mathematical identities, even when such identities
+   * are offered as explanations of their meaning.
+   *
+   *   Instead, the mathematical meaning of these metrics serves as a first
+   * layer, a literal meaning which provides a reference point for additional
+   * layers of meaning to be added atop, in the same fashion as how the
+   * duration of a musical measure is not actually fixed during a live
+   * performance.
+   *
+   *   There are very good reasons that programmers find this state of affairs
+   * deeply saddening, but there is nothing to be done but deal with it.
+   */
+  pub fn draw(&self, context: &FontGraphicContext, color: &Color,
+	      location: &Point<i32>)
+    -> Result<()>
+  {
+    let xft_draw = context.get_xft_draw();
+
+    unsafe {
+      XftDrawStringUtf8(xft_draw,
+			color.as_xft_color(),
+			self.font.font_state(),
+			location.x, location.y,
+                        self.text.as_ptr().cast(),
+			self.text.count_bytes() as i32)
+    };
+
+    Ok(())
+  }
+
+  /*   This returns the offset from the top-left of the span's logical parcel to the
+   * left-side baseline of its as-drawn pixel boundaries. See the note on draw() for
+   * more context on what these as-drawn boundaries mean and why.
+   */
+  pub fn draw_offset(&self) -> Offset<i16> {
+    Offset {
+      x: self.extent.x,
+      y: self.extent.y,
+    }
+  }
+
+  /*   This is the as-drawn pixel size, rounded up to the next whole pixel,
+   * so that it is safe to use as a bounding box. It is computed only with
+   * respect to characters present in the span, and only considers the marks
+   * they actually draw.
+   *
+   *   There are two important implications. First, due to rounding,
+   * pixel_size() is likely to be wider than logical_offset(). Second, trailing
+   * spaces are not included, since they do not draw any marks.
+   *
+   *   In the vertical dimension, this size is conceptually similar to the
+   * maximum ascent plus the maximum descent of any character present in the
+   * span. Since there is no way within xft to obtain the font's declared
+   * ascent or descent, this measurement may be the best way to approximate
+   * those measurements. Note that there is nothing that even approximates line
+   * height; just guess.
+   *
+   *   If you're thinking this all sounds highly inconvenient, you are correct.
+   * See the note on draw() for some highly relevant philosophy.
+   */
+  pub fn draw_size(&self) -> Size<u16> {
+    Size {
+      width: self.extent.width,
+      height: self.extent.height,
+    }
+  }
+
+  /*   This returns the offset from the left-side baseline of the span's
+   * logical parcel to the new left-side baseline that a span occurring
+   * immediately after it would start from.
+   *
+   *   This is rounded in some fashion, most likely to the nearest whole
+   * 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,
+      y: self.extent.yOff,
+    }
+  }
+
+  pub fn layout_and_draw(&self,
+                         open_parcel: &mut Parcel, location: &mut Point<i32>,
+                         alignment: Alignment, context: &FontGraphicContext,
+                         color: &Color)
+    -> Result<()>
+  {
+    match alignment {
+      Alignment::Left => {
+        location.x += self.draw_offset().x as i32;
+      }
+      Alignment::Right => {
+        location.x -= self.draw_size().width as i32;
+      }
+    }
+
+    self.draw(&context, color, &location)?;
+
+    match alignment {
+      Alignment::Left => {
+        location.x += self.draw_size().width as i32;
+
+        open_parcel.left += self.draw_size().width as u32;
+      }
+      Alignment::Right => {
+        location.x -= self.draw_offset().x as i32;
+
+        open_parcel.right -= self.draw_size().width as u32;
+      }
+    }
+
+    Ok(())
+  }
+}
+