Skip to content

Commit

Permalink
Fix font constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
d0rianb committed Feb 6, 2022
1 parent eae6a18 commit 69411d7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 33 deletions.
7 changes: 1 addition & 6 deletions TODO.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
- multiple windows
- multiple cursors
- formating (bold, italic, underline)
- cmd-Z
- cmd-r --> dynamic research
- find/replace
- preferences
- rework font
- write tests
- relative path in build
- write tests
45 changes: 22 additions & 23 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ use crate::editable::Editable;
pub const EDITOR_PADDING: f32 = 10.;
pub const EDITOR_OFFSET_TOP: f32 = 55.;

#[cfg(debug_assertions)]
fn get_working_dir() -> PathBuf { env::current_dir().unwrap() }

#[cfg(not(debug_assertions))]
fn get_working_dir() -> PathBuf {
let path_buf = env::current_exe().unwrap();
path_buf.parent().unwrap().to_path_buf()
}

pub fn get_file_path(filename: &str) -> String {
let mut wd = get_working_dir();
wd.push(filename);
let valid_file_path = wd.canonicalize().expect(&format!("Invalid path : {:?}", wd));
valid_file_path.into_os_string().to_str().unwrap().to_string()
}

pub struct Editor {
pub lines: Vec<Line>,
pub cursor: Cursor,
Expand All @@ -54,12 +70,11 @@ pub struct Editor {
impl Editor {
pub fn new(width: f32, height: f32, offset: Vector2<f32>, padding: f32) -> Self {
let font = Rc::new(RefCell::new(Font::new(
include_bytes!("../resources/font/CourierRegular.ttf"),
// "resources/font/Monaco.ttf",
width - offset.x - padding*2.,
height - offset.y - padding*2.,
&get_file_path("./resources/font/CourierRegular.ttf"),
width - offset.x - padding * 2.,
height - offset.y - padding * 2.,
)));
let system_font = Rc::new(RefCell::new(Font::new(include_bytes!("../resources/font/Roboto-Regular.ttf"), width, height)));
let system_font = Rc::new(RefCell::new(Font::new(&get_file_path("./resources/font/Roboto-Regular.ttf"), width, height)));
Self {
cursor: Cursor::new(0, 0, Rc::clone(&font)),
camera: Camera::new(width, height, offset, padding),
Expand Down Expand Up @@ -637,10 +652,10 @@ impl Editor {
if let Some(prefs) = &self.cached_prefs {
prefs[key].clone()
} else {
let prefs_path = self.get_file_path("./resources/prefs.yaml");
let prefs_path = get_file_path("./resources/prefs.yaml");
let prefs_str = fs::read_to_string(prefs_path).expect("Can't find the preference file");
let docs: Vec<Yaml> = YamlLoader::load_from_str(&prefs_str).expect("Invalid preferences");
let prefs = docs.get(0).unwrap();
let prefs: &Yaml = docs.get(0).unwrap();
self.cached_prefs = Some(prefs.clone());
prefs[key].clone()
}
Expand Down Expand Up @@ -688,22 +703,6 @@ impl Editor {
self.load_file(path);
}

#[cfg(debug_assertions)]
fn get_working_dir(&self) -> PathBuf { env::current_dir().unwrap() }

#[cfg(not(debug_assertions))]
fn get_working_dir(&self) -> PathBuf {
let path_buf = env::current_exe().unwrap();
path_buf.parent().unwrap().to_path_buf()
}

pub fn get_file_path(&self, filename: &str) -> String {
let mut wd = self.get_working_dir();
wd.push(filename);
let valid_file_path = wd.canonicalize().expect(&format!("Invalid path : {:?}", wd));
valid_file_path.into_os_string().to_str().unwrap().to_string()
}

fn print_dir(&mut self) {
let current_exe = env::current_exe();
let text = current_exe
Expand Down
11 changes: 7 additions & 4 deletions src/font.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fs;
use std::path::Path;
use std::rc::Rc;

use speedy2d::dimen::Vector2;
Expand All @@ -19,12 +21,13 @@ pub struct Font {
}

impl Font {
pub fn new(bytes: &[u8], editor_width: f32, editor_height: f32) -> Self {
let font_file_content = bytes;
let s2d_font = S2DFont::new(font_file_content).unwrap();
pub fn new(path: &str, editor_width: f32, editor_height: f32) -> Self {
let filename = Path::new(path).file_name().unwrap().to_str().unwrap();
let font_file_content = fs::read(path).unwrap();
let s2d_font = S2DFont::new(font_file_content.as_slice()).unwrap();
let font_layout = s2d_font.layout_text("a", 2.0 * DEFAULT_FONT_SIZE as f32, TextOptions::default());
Self {
name: "src".to_string(),
name: filename.to_string(),
size: DEFAULT_FONT_SIZE,
char_width: font_layout.width(),
char_height: font_layout.height(),
Expand Down

0 comments on commit 69411d7

Please sign in to comment.