From cc0780b9cd74c936d620eca725bf5c5fa7f850e1 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Sat, 18 Apr 2020 10:45:01 +0100 Subject: [PATCH] implement font and text direction; call std and mock directly instead of importing --- src/game.rs | 59 ++++++++++++++++++++++++++++++++++++++++------------- src/lib.rs | 2 ++ src/mock.rs | 3 +++ src/text.rs | 27 ++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 14 deletions(-) create mode 100644 src/text.rs diff --git a/src/game.rs b/src/game.rs index bc9b173..b817c92 100644 --- a/src/game.rs +++ b/src/game.rs @@ -1,5 +1,17 @@ -use crate::{Avatar, Dialogue, Ending, Item, Palette, Room, Sprite, Tile, Variable, mock, optional_data_line}; -use std::fs; +use crate::{ + Avatar, + Dialogue, + Ending, + Font, + Item, + Palette, + Room, + Sprite, + TextDirection, + Tile, + Variable, + optional_data_line +}; #[derive(Debug, PartialEq)] pub struct Game { @@ -7,6 +19,9 @@ pub struct Game { pub major_version: u8, pub minor_version: u8, pub room_format: u8, // this is "0 = non-comma separated, 1 = comma separated" apparently + pub font: Font, + pub custom_font: Option, // used if font is Font::Custom + pub text_direction: TextDirection, pub palettes: Vec, pub rooms: Vec, pub tiles: Vec, @@ -74,6 +89,9 @@ impl From for Game { let mut major_version: u8 = 1; let mut minor_version: u8 = 0; let mut room_format: u8 = 1; + let mut font = Font::AsciiSmall; + let mut custom_font = None; + let mut text_direction = TextDirection::LeftToRight; let mut palettes: Vec = Vec::new(); let mut rooms: Vec = Vec::new(); let mut tiles: Vec = Vec::new(); @@ -91,6 +109,16 @@ impl From for Game { minor_version = parts.1; } else if segment.starts_with("! ROOM_FORMAT") { room_format = segment.replace("! ROOM_FORMAT ", "").parse().unwrap(); + } else if segment.starts_with("DEFAULT_FONT") { + let segment = segment.replace("DEFAULT_FONT ", ""); + + font = Font::from(&segment); + + if font == Font::Custom { + custom_font = Some(segment.to_string()); + } + } else if segment.trim() == "TEXT_DIRECTION RTL".to_string() { + text_direction = TextDirection::RightToLeft; } else if segment.starts_with("PAL") { palettes.push(Palette::from(segment)); } else if segment.starts_with("ROOM") { @@ -114,6 +142,9 @@ impl From for Game { major_version, minor_version, room_format, + font, + custom_font, + text_direction, palettes, rooms, tiles, @@ -231,34 +262,34 @@ fn test_game_from_string() { include_str!["test-resources/default.bitsy"].to_string() ); - let expected = mock::game_default(); + let expected = crate::mock::game_default(); assert_eq!(output, expected); } #[test] fn test_game_to_string() { - let output = mock::game_default().to_string(); + let output = crate::mock::game_default().to_string(); let expected = include_str!["test-resources/default.bitsy"].to_string(); assert_eq!(output, expected); } #[test] fn test_tile_ids() { - assert_eq!(mock::game_default().tile_ids(), vec![10]); + assert_eq!(crate::mock::game_default().tile_ids(), vec![10]); } #[test] fn test_new_tile_id() { // default tile has an id of 10 ("a"), so 0 is available - assert_eq!(mock::game_default().new_tile_id(), 0); + assert_eq!(crate::mock::game_default().new_tile_id(), 0); - let mut game = mock::game_default(); + let mut game = crate::mock::game_default(); let mut tiles : Vec = Vec::new(); for n in 0..9 { if n != 4 { - let mut new_tile = mock::tile_default(); + let mut new_tile = crate::mock::tile_default(); new_tile.id = n; tiles.push(new_tile); } @@ -270,7 +301,7 @@ fn test_new_tile_id() { // fill in the space created above, and test that tile IDs get sorted - let mut new_tile = mock::tile_default(); + let mut new_tile = crate::mock::tile_default(); new_tile.id = 4; game.tiles.push(new_tile); @@ -279,11 +310,11 @@ fn test_new_tile_id() { #[test] fn test_add_tile() { - let mut game = mock::game_default(); - let new_id = game.add_tile(mock::tile_default()); + let mut game = crate::mock::game_default(); + let new_id = game.add_tile(crate::mock::tile_default()); assert_eq!(new_id, 0); assert_eq!(game.tiles.len(), 2); - let new_id = game.add_tile(mock::tile_default()); + let new_id = game.add_tile(crate::mock::tile_default()); assert_eq!(new_id, 1); assert_eq!(game.tiles.len(), 3); } @@ -315,7 +346,7 @@ fn test_bitsy_omnibus() { let mut passes = 0; let mut skips = 0; - for file in fs::read_dir("src/test-resources/omnibus").unwrap() { + for file in std::fs::read_dir("src/test-resources/omnibus").unwrap() { let path = file.unwrap().path(); let nice_name = format!("{}", path.display()); @@ -327,7 +358,7 @@ fn test_bitsy_omnibus() { } println!("\nTesting: {}...", path.display()); - let game_data = fs::read_to_string(path).unwrap(); + let game_data = std::fs::read_to_string(path).unwrap(); let game = Game::from(game_data.clone()); assert_eq!(game.to_string().trim(), game_data.trim()); passes += 1; diff --git a/src/lib.rs b/src/lib.rs index fce01f9..30a3443 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ pub mod colour; pub mod dialogue; pub mod ending; pub mod exit; +pub mod text; pub mod game; pub mod palette; pub mod image; @@ -21,6 +22,7 @@ use colour::Colour; use dialogue::Dialogue; use ending::Ending; use exit::{Transition, Exit}; +use text::{Font, TextDirection}; use game::Game; use palette::Palette; use image::Image; diff --git a/src/mock.rs b/src/mock.rs index 496630f..3205ad6 100644 --- a/src/mock.rs +++ b/src/mock.rs @@ -191,6 +191,9 @@ pub fn game_default() -> Game { major_version: 6, minor_version: 5, room_format: 1, + font: Font::AsciiSmall, + custom_font: None, + text_direction: TextDirection::LeftToRight, palettes: vec![ Palette { id: 0, diff --git a/src/text.rs b/src/text.rs new file mode 100644 index 0000000..d93c608 --- /dev/null +++ b/src/text.rs @@ -0,0 +1,27 @@ +#[derive(Debug, Eq, PartialEq)] +pub enum Font { + AsciiSmall, // default - does not appear in game data + UnicodeEuropeanSmall, + UnicodeEuropeanLarge, + UnicodeAsian, + Arabic, + Custom, +} + +impl Font { + pub(crate) fn from(str: &str) -> Font { + match str { + "unicode_european_small" => Font::UnicodeEuropeanSmall, + "unicode_european_large" => Font::UnicodeEuropeanLarge, + "unicode_asian" => Font::UnicodeAsian, + "arabic" => Font::Arabic, + _ => Font::Custom, + } + } +} + +#[derive(Debug, Eq, PartialEq)] +pub enum TextDirection { + LeftToRight, // default + RightToLeft, +}