-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4c2191e
commit a88bbb9
Showing
18 changed files
with
349 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
target/ | ||
|
||
# Korangar | ||
korangar/archive/data/font/* | ||
korangar/client/ | ||
korangar/data.grf | ||
korangar/rdata.grf | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
use std::io::{Cursor, Read}; | ||
use std::sync::Arc; | ||
|
||
use cosmic_text::fontdb::{Source, ID}; | ||
use cosmic_text::FontSystem; | ||
use flate2::bufread::GzDecoder; | ||
use hashbrown::HashMap; | ||
use image::{ImageFormat, ImageReader, RgbaImage}; | ||
#[cfg(feature = "debug")] | ||
use korangar_debug::logging::{print_debug, Colorize, Timer}; | ||
use korangar_util::FileLoader; | ||
|
||
use crate::loaders::font::font_map_descriptor::parse_glyphs; | ||
use crate::loaders::font::GlyphCoordinate; | ||
use crate::loaders::GameFileLoader; | ||
|
||
const FONT_FOLDER_PATH: &str = "data\\font"; | ||
|
||
pub(crate) struct FontFile { | ||
pub(crate) ids: Vec<ID>, | ||
pub(crate) font_map: RgbaImage, | ||
pub(crate) glyphs: Arc<HashMap<u16, GlyphCoordinate>>, | ||
} | ||
|
||
impl FontFile { | ||
pub(crate) fn new(name: &str, game_file_loader: &GameFileLoader, font_system: &mut FontSystem) -> Option<Self> { | ||
#[cfg(feature = "debug")] | ||
let timer = Timer::new_dynamic(format!("load font: {}", name.magenta())); | ||
|
||
let font_base_path = format!("{}\\{}", FONT_FOLDER_PATH, name); | ||
let ttf_file_path = format!("{}.ttf", font_base_path); | ||
let map_file_path = format!("{}.png", font_base_path); | ||
let map_description_file_path = format!("{}.csv.gz", font_base_path); | ||
|
||
let Ok(font_data) = game_file_loader.get(&ttf_file_path) else { | ||
#[cfg(feature = "debug")] | ||
print_debug!("[{}] failed to load font file '{}'", "error".red(), ttf_file_path.magenta()); | ||
return None; | ||
}; | ||
|
||
let ids = font_system.db_mut().load_font_source(Source::Binary(Arc::new(font_data))); | ||
|
||
let Ok(font_map_data) = game_file_loader.get(&map_file_path) else { | ||
#[cfg(feature = "debug")] | ||
print_debug!("[{}] failed to load font map file '{}'", "error".red(), map_file_path.magenta()); | ||
return None; | ||
}; | ||
|
||
let font_map_reader = ImageReader::with_format(Cursor::new(font_map_data), ImageFormat::Png); | ||
|
||
let Ok(font_map_decoder) = font_map_reader.decode() else { | ||
#[cfg(feature = "debug")] | ||
print_debug!("[{}] failed to decode font map '{}'", "error".red(), map_file_path.magenta()); | ||
return None; | ||
}; | ||
|
||
let font_map_rgba_image = font_map_decoder.into_rgba8(); | ||
let font_map_width = font_map_rgba_image.width(); | ||
let font_map_height = font_map_rgba_image.height(); | ||
|
||
let Ok(mut font_description_data) = game_file_loader.get(&map_description_file_path) else { | ||
#[cfg(feature = "debug")] | ||
print_debug!( | ||
"[{}] failed to load font map description file '{}'", | ||
"error".red(), | ||
map_description_file_path.magenta() | ||
); | ||
return None; | ||
}; | ||
|
||
let mut decoder = GzDecoder::new(&font_description_data[..]); | ||
let mut data = Vec::with_capacity(font_description_data.len() * 2); | ||
|
||
if let Err(_err) = decoder.read_to_end(&mut data) { | ||
#[cfg(feature = "debug")] | ||
print_debug!( | ||
"[{}] failed to decompress font map description file '{}': {:?}", | ||
"error".red(), | ||
map_description_file_path.magenta(), | ||
_err | ||
); | ||
return None; | ||
} | ||
|
||
font_description_data = data; | ||
|
||
let Ok(font_description_content) = String::from_utf8(font_description_data) else { | ||
#[cfg(feature = "debug")] | ||
print_debug!( | ||
"[{}] invalid UTF-8 text data found in font map description file '{}'", | ||
"error".red(), | ||
map_description_file_path.magenta() | ||
); | ||
return None; | ||
}; | ||
|
||
let glyphs = parse_glyphs(font_description_content, font_map_width, font_map_height); | ||
|
||
#[cfg(feature = "debug")] | ||
timer.stop(); | ||
|
||
Some(Self { | ||
ids: Vec::from_iter(ids), | ||
font_map: font_map_rgba_image, | ||
glyphs: Arc::new(glyphs), | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.