Skip to content

Commit 7783ee3

Browse files
committed
fix spritesheets keeping -uhd prefixes in sprite names + fix .fnt not
working
1 parent e78c88d commit 7783ee3

File tree

3 files changed

+41
-22
lines changed

3 files changed

+41
-22
lines changed

src/package.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ fn create_resources(
141141

142142
// Create fonts
143143
for font in mod_info.resources.fonts.values() {
144-
let font_file = bmfont::get_font_bundles(font, &output_dir, &mut cache_bundle);
144+
let font_file = bmfont::get_font_bundles(
145+
font, &output_dir, &mut cache_bundle, mod_info
146+
);
145147
cache.add_font(font, font_file.cache_name(&working_dir));
146148
}
147149

src/util/bmfont.rs

+27-20
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::fs;
44
use std::io::Read;
55
use std::path::Path;
66
use std::path::PathBuf;
7-
use fontdue::Font;
87
use texture_packer::exporter::ImageExporter;
98
use texture_packer::TexturePacker;
109
use texture_packer::TexturePackerConfig;
@@ -14,6 +13,7 @@ use crate::{done, info, fatal, NiceUnwrap};
1413
use image::{Rgba, Rgb, RgbaImage, LumaA, EncodableLayout, Pixel, GenericImageView, DynamicImage, GrayAlphaImage};
1514
use signed_distance_field::prelude::*;
1615

16+
use super::mod_file::ModFileInfo;
1717
use super::spritesheet::downscale;
1818

1919
struct RenderedChar {
@@ -111,7 +111,12 @@ fn generate_char(font: &BitmapFont, metrics: fontdue::Metrics, data: Vec<u8>) ->
111111
))
112112
}
113113

114-
fn initialize_font_bundle(bundle: &FontBundle, font: &BitmapFont, factor: u32) -> PathBuf {
114+
fn initialize_font_bundle(
115+
bundle: &FontBundle,
116+
font: &BitmapFont,
117+
factor: u32,
118+
_mod_info: &ModFileInfo
119+
) -> PathBuf {
115120
// Get all characters from the charset format
116121
let chars: Vec<char> = font.charset
117122
.as_deref()
@@ -206,27 +211,24 @@ fn initialize_font_bundle(bundle: &FontBundle, font: &BitmapFont, factor: u32) -
206211
}
207212

208213
// Get all kerning pairs
209-
let mut all_kerning_pairs = vec!();
210-
for left in &rasterized_chars {
211-
for right in &rasterized_chars {
212-
if let Some(kern) = ttf_font.horizontal_kern(
214+
let all_kerning_pairs = rasterized_chars.iter().flat_map(
215+
|left| rasterized_chars.iter().filter_map(|right| {
216+
ttf_font.horizontal_kern(
213217
left.id, right.id, font.size as f32
214-
) {
215-
all_kerning_pairs.push(format!(
216-
"kerning first={} second={} amount={}",
217-
left.id, right.id, kern as i32
218-
));
219-
}
220-
}
221-
}
218+
).map(|kern| format!(
219+
"kerning first={} second={} amount={}",
220+
left.id, right.id, kern as i32
221+
))
222+
})
223+
).collect::<Vec<_>>();
222224

223225
// Create .fnt file
224226
let line_metrics = ttf_font.horizontal_line_metrics(font.size as f32).unwrap();
225227
let fnt_data = format!(
226-
"info face=\"{font_name} size={font_size} bold=0 italic=0 \
228+
"info face=\"{font_name}\" size={font_size} bold=0 italic=0 \
227229
charset=\"\" unicode=1 stretchH=100 smooth=1 aa=1 padding=0,0,0,0 spacing=1,1\n\
228230
common lineHeight={common_line_height} base={font_base} \
229-
scaleW={scale_w} scaleH={scale_h} pages=1 packed=0\n \
231+
scaleW={scale_w} scaleH={scale_h} pages=1 packed=0\n\
230232
page id=0 file=\"{sprite_file_name}.png\"\n\
231233
chars count={char_count}\n\
232234
{all_chars}\n\
@@ -304,7 +306,12 @@ fn extract_from_cache(path: &Path, working_dir: &Path, cache_bundle: &mut CacheB
304306
);
305307
}
306308

307-
pub fn get_font_bundles(font: &BitmapFont, working_dir: &Path, cache: &mut Option<CacheBundle>) -> FontBundles {
309+
pub fn get_font_bundles(
310+
font: &BitmapFont,
311+
working_dir: &Path,
312+
cache: &mut Option<CacheBundle>,
313+
mod_info: &ModFileInfo
314+
) -> FontBundles {
308315
info!("Fetching font {}", font.name.bright_yellow());
309316

310317
if let Some(cache_bundle) = cache {
@@ -332,13 +339,13 @@ pub fn get_font_bundles(font: &BitmapFont, working_dir: &Path, cache: &mut Optio
332339
// Create new font
333340

334341
info!("Creating normal font");
335-
initialize_font_bundle(&mut bundles.sd, font, 4);
342+
initialize_font_bundle(&mut bundles.sd, font, 4, mod_info);
336343

337344
info!("Creating HD font");
338-
initialize_font_bundle(&mut bundles.hd, font, 2);
345+
initialize_font_bundle(&mut bundles.hd, font, 2, mod_info);
339346

340347
info!("Creating UHD font");
341-
initialize_font_bundle(&mut bundles.uhd, font, 1);
348+
initialize_font_bundle(&mut bundles.uhd, font, 1, mod_info);
342349

343350
done!("Built font {}", font.name.bright_yellow());
344351
bundles

src/util/spritesheet.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,19 @@ fn initialize_spritesheet_bundle(
138138
sprites.iter().for_each(|x| texture_packer.pack_ref(&x.name, &x.image).unwrap());
139139
done!("Packed sprites");
140140

141+
let sprite_name_in_sheet = |name: &String| {
142+
// `mod.id/sprite.png`
143+
mod_info.id.to_owned() + "/" +
144+
name
145+
.strip_suffix("-uhd")
146+
.or(name.strip_suffix("-hd"))
147+
.unwrap_or(name) +
148+
".png"
149+
};
150+
141151
// Initialize the plist file
142152
let frame_info = texture_packer.get_frames().iter().map(|(name, frame)| {
143-
(mod_info.id.to_owned() + "/" + name + ".png", json!({
153+
(sprite_name_in_sheet(name), json!({
144154
"textureRotated": frame.rotated,
145155
"spriteSourceSize": format!("{{{}, {}}}", frame.source.w, frame.source.h),
146156
"spriteSize": format!("{{{}, {}}}", frame.frame.w, frame.frame.h),

0 commit comments

Comments
 (0)