about summary refs log tree commit diff
path: root/src/x/text/font_list.rs
blob: 1ef34990e5edd0a7ea79601d126d74509b1da513 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use crate::error::{ Error, Result };
use crate::x::Display;
use crate::x::text::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()
  }
}