Skip to content

Commit

Permalink
Handle fonts differently and use fontconfig in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
trimental committed Aug 16, 2018
1 parent e853b17 commit 42828bf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ rusttype = "0.6.3"

[dev-dependencies]
smithay-client-toolkit = "0.2.6"
font-loader = "0.7.0"
12 changes: 9 additions & 3 deletions examples/basic_test/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
extern crate andrew;
extern crate font_loader;
extern crate smithay_client_toolkit as sctk;

use andrew::line;
Expand Down Expand Up @@ -152,15 +153,20 @@ fn redraw(
let mut text = text::Text::new(
(63, 69),
[0, 0, 0, 255],
"/usr/share/fonts/TTF/DejaVuSerif.ttf",
font_loader::system_fonts::get(
&font_loader::system_fonts::FontPropertyBuilder::new()
.monospace()
.build(),
).unwrap()
.0,
12.0,
2.0,
"hello world",
);
text.pos = (75 - (text.get_width() / 2), 69);
let text_box = rectangle::Rectangle {
pos: text.pos,
size: (text.get_width(), 12),
pos: (text.pos.0 - 3, text.pos.1),
size: (text.get_width() + 6, 12),
border: Some((1, [0, 0, 255, 255], rectangle::Sides::ALL, None)),
fill: None,
};
Expand Down
19 changes: 11 additions & 8 deletions src/text/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rusttype::{point, Font, Scale, VMetrics};
use rusttype::{point, Font, Scale, SharedBytes, VMetrics};
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;
Expand All @@ -14,22 +14,25 @@ pub struct Text<'a> {
pub v_metrics: VMetrics,
}

pub fn load_font_file<P: Into<PathBuf>>(path: P) -> Vec<u8> {
let mut data: Vec<u8> = Vec::new();
let mut file = File::open(path.into()).expect("Could not open font file");
file.read_to_end(&mut data)
.expect("Could not read font file");
data
}

impl<'a> Text<'a> {
pub fn new<P: Into<PathBuf>, T: Into<String>>(
pub fn new<P: Into<SharedBytes<'a>>, T: Into<String>>(
pos: (usize, usize),
color: [u8; 4],
font_path: P,
font_data: P,
height: f32,
width_scale: f32,
text: T,
) -> Text<'a> {
let text = text.into();
// Create font
let mut font_data: Vec<u8> = Vec::new();
let mut font_file = File::open(font_path.into()).expect("Could not open font file");
font_file
.read_to_end(&mut font_data)
.expect("Could not read font file");
let font = Font::from_bytes(font_data).expect("Error constructing Font");
// Create scale
let scale = Scale {
Expand Down

0 comments on commit 42828bf

Please sign in to comment.