From c3498ef7e2c5f5f1dfafb07e50370c4f62f4d69e Mon Sep 17 00:00:00 2001 From: Akshay Date: Mon, 29 Mar 2021 12:13:33 +0530 Subject: fix: sdl panics on empty string drawing --- src/app.rs | 15 +++------------ src/utils.rs | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/app.rs b/src/app.rs index eef896d..ffa73dd 100644 --- a/src/app.rs +++ b/src/app.rs @@ -4,12 +4,12 @@ use crate::{ command::CommandBox, consts::{colors::*, FONT_PATH}, dither, - lisp::{error::LispError, eval, lex::Lexer, parse::Parser, prelude, EnvList}, + lisp::{eval, lex::Lexer, parse::Parser, prelude, EnvList}, message::Message, rect, symmetry::Symmetry, undo::{ModifyRecord, OpKind, PaintRecord, UndoStack}, - utils::{draw_text, is_copy_event, is_paste_event}, + utils::{draw_text, handle_error, is_copy_event, is_paste_event}, }; use std::{convert::From, fs::File, io::prelude::*, path::Path}; @@ -284,7 +284,7 @@ impl<'ctx, 'file> AppState<'ctx, 'file> { Ok(val) => self.message.set_info(format!("{}", val)), Err(eval_err) => self.message.set_error(format!("{}", eval_err)), }, - Err(err) => self.message = AppState::handle_error(err, &lisp_expr), + Err(err) => self.message = handle_error(err, &lisp_expr), } self.command_box.hist_append(); @@ -292,15 +292,6 @@ impl<'ctx, 'file> AppState<'ctx, 'file> { self.mode = Mode::Draw; } - fn handle_error(err: LispError, src: &str) -> Message { - let mut message = Message::new(); - match err { - LispError::Parse(p) => message.set_error(p.display(&src)), - eval_err => message.set_error(eval_err.to_string()), - } - message - } - fn draw_grid(&mut self) { let cs = self.zoom as u32; 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 @@ -use crate::consts::FONT_PATH; +use crate::{consts::FONT_PATH, lisp::error::LispError, message::Message}; use sdl2::{ keyboard::{Keycode, Mod}, pixels::Color, @@ -26,7 +26,10 @@ pub fn draw_text>( let mut font = ttf_context.load_font(FONT_PATH, 17).unwrap(); font.set_style(sdl2::ttf::FontStyle::NORMAL); font.set_hinting(sdl2::ttf::Hinting::Mono); - let surface = font.render(text.as_ref()).blended(color).unwrap(); + let surface = font + .render(if text.is_empty() { " " } else { text.as_ref() }) + .blended(color) + .unwrap(); let texture = texture_creator .create_texture_from_surface(&surface) .unwrap(); @@ -43,3 +46,12 @@ pub fn is_copy_event(keycode: Option, keymod: Mod) -> bool { pub fn is_paste_event(keycode: Option, keymod: Mod) -> bool { keycode == Some(Keycode::V) && (keymod == Mod::LCTRLMOD || keymod == Mod::RCTRLMOD) } + +pub fn handle_error(err: LispError, src: &str) -> Message { + let mut message = Message::new(); + match err { + LispError::Parse(p) => message.set_error(p.display(&src)), + eval_err => message.set_error(eval_err.to_string()), + } + message +} -- cgit v1.2.3