diff options
author | Akshay <[email protected]> | 2021-03-29 07:43:33 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2021-03-29 07:43:33 +0100 |
commit | c3498ef7e2c5f5f1dfafb07e50370c4f62f4d69e (patch) | |
tree | 5873be05fd9c503447942218d5e257adcba9568d | |
parent | dadbf350405967c0bf967eed6686b02c2cf6a048 (diff) |
fix: sdl panics on empty string drawing
-rw-r--r-- | src/app.rs | 15 | ||||
-rw-r--r-- | src/utils.rs | 16 |
2 files changed, 17 insertions, 14 deletions
@@ -4,12 +4,12 @@ use crate::{ | |||
4 | command::CommandBox, | 4 | command::CommandBox, |
5 | consts::{colors::*, FONT_PATH}, | 5 | consts::{colors::*, FONT_PATH}, |
6 | dither, | 6 | dither, |
7 | lisp::{error::LispError, eval, lex::Lexer, parse::Parser, prelude, EnvList}, | 7 | lisp::{eval, lex::Lexer, parse::Parser, prelude, EnvList}, |
8 | message::Message, | 8 | message::Message, |
9 | rect, | 9 | rect, |
10 | symmetry::Symmetry, | 10 | symmetry::Symmetry, |
11 | undo::{ModifyRecord, OpKind, PaintRecord, UndoStack}, | 11 | undo::{ModifyRecord, OpKind, PaintRecord, UndoStack}, |
12 | utils::{draw_text, is_copy_event, is_paste_event}, | 12 | utils::{draw_text, handle_error, is_copy_event, is_paste_event}, |
13 | }; | 13 | }; |
14 | 14 | ||
15 | use std::{convert::From, fs::File, io::prelude::*, path::Path}; | 15 | use std::{convert::From, fs::File, io::prelude::*, path::Path}; |
@@ -284,7 +284,7 @@ impl<'ctx, 'file> AppState<'ctx, 'file> { | |||
284 | Ok(val) => self.message.set_info(format!("{}", val)), | 284 | Ok(val) => self.message.set_info(format!("{}", val)), |
285 | Err(eval_err) => self.message.set_error(format!("{}", eval_err)), | 285 | Err(eval_err) => self.message.set_error(format!("{}", eval_err)), |
286 | }, | 286 | }, |
287 | Err(err) => self.message = AppState::handle_error(err, &lisp_expr), | 287 | Err(err) => self.message = handle_error(err, &lisp_expr), |
288 | } | 288 | } |
289 | self.command_box.hist_append(); | 289 | self.command_box.hist_append(); |
290 | 290 | ||
@@ -292,15 +292,6 @@ impl<'ctx, 'file> AppState<'ctx, 'file> { | |||
292 | self.mode = Mode::Draw; | 292 | self.mode = Mode::Draw; |
293 | } | 293 | } |
294 | 294 | ||
295 | fn handle_error(err: LispError, src: &str) -> Message { | ||
296 | let mut message = Message::new(); | ||
297 | match err { | ||
298 | LispError::Parse(p) => message.set_error(p.display(&src)), | ||
299 | eval_err => message.set_error(eval_err.to_string()), | ||
300 | } | ||
301 | message | ||
302 | } | ||
303 | |||
304 | fn draw_grid(&mut self) { | 295 | fn draw_grid(&mut self) { |
305 | let cs = self.zoom as u32; | 296 | let cs = self.zoom as u32; |
306 | let (width, height) = (self.width(), self.height()); | 297 | let (width, height) = (self.width(), self.height()); |
diff --git a/src/utils.rs b/src/utils.rs index 902f939..3e5ff55 100644 --- a/src/utils.rs +++ b/src/utils.rs | |||
@@ -1,4 +1,4 @@ | |||
1 | use crate::consts::FONT_PATH; | 1 | use crate::{consts::FONT_PATH, lisp::error::LispError, message::Message}; |
2 | use sdl2::{ | 2 | use sdl2::{ |
3 | keyboard::{Keycode, Mod}, | 3 | keyboard::{Keycode, Mod}, |
4 | pixels::Color, | 4 | pixels::Color, |
@@ -26,7 +26,10 @@ pub fn draw_text<S: AsRef<str>>( | |||
26 | let mut font = ttf_context.load_font(FONT_PATH, 17).unwrap(); | 26 | let mut font = ttf_context.load_font(FONT_PATH, 17).unwrap(); |
27 | font.set_style(sdl2::ttf::FontStyle::NORMAL); | 27 | font.set_style(sdl2::ttf::FontStyle::NORMAL); |
28 | font.set_hinting(sdl2::ttf::Hinting::Mono); | 28 | font.set_hinting(sdl2::ttf::Hinting::Mono); |
29 | let surface = font.render(text.as_ref()).blended(color).unwrap(); | 29 | let surface = font |
30 | .render(if text.is_empty() { " " } else { text.as_ref() }) | ||
31 | .blended(color) | ||
32 | .unwrap(); | ||
30 | let texture = texture_creator | 33 | let texture = texture_creator |
31 | .create_texture_from_surface(&surface) | 34 | .create_texture_from_surface(&surface) |
32 | .unwrap(); | 35 | .unwrap(); |
@@ -43,3 +46,12 @@ pub fn is_copy_event(keycode: Option<Keycode>, keymod: Mod) -> bool { | |||
43 | pub fn is_paste_event(keycode: Option<Keycode>, keymod: Mod) -> bool { | 46 | pub fn is_paste_event(keycode: Option<Keycode>, keymod: Mod) -> bool { |
44 | keycode == Some(Keycode::V) && (keymod == Mod::LCTRLMOD || keymod == Mod::RCTRLMOD) | 47 | keycode == Some(Keycode::V) && (keymod == Mod::LCTRLMOD || keymod == Mod::RCTRLMOD) |
45 | } | 48 | } |
49 | |||
50 | pub fn handle_error(err: LispError, src: &str) -> Message { | ||
51 | let mut message = Message::new(); | ||
52 | match err { | ||
53 | LispError::Parse(p) => message.set_error(p.display(&src)), | ||
54 | eval_err => message.set_error(eval_err.to_string()), | ||
55 | } | ||
56 | message | ||
57 | } | ||