Initialize Minecraft text generator project with basic structure and CLI functionality
This commit is contained in:
22
crates/bin/Cargo.toml
Normal file
22
crates/bin/Cargo.toml
Normal file
@@ -0,0 +1,22 @@
|
||||
[package]
|
||||
name = "minecraft-text-generator"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
default-run = "minecraft-text-generator"
|
||||
|
||||
[profile.release]
|
||||
|
||||
strip = true
|
||||
lto = true
|
||||
codegen-units = 1
|
||||
opt-level = 3
|
||||
|
||||
|
||||
[dependencies]
|
||||
lib = { path = "../lib" }
|
||||
exporters = { path = "../exporters" }
|
||||
clap = { version = "4.6.0", features = ["derive"] }
|
||||
anyhow = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
tracing-subscriber = { workspace = true, features = ["env-filter"] }
|
||||
64
crates/bin/src/cli.rs
Normal file
64
crates/bin/src/cli.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
use std::{fs, path::PathBuf};
|
||||
|
||||
use clap::Parser;
|
||||
use exporters::McFunctionExporter;
|
||||
use lib::{StructureExporter, TextBuilder, TtfFont};
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(author, version, about, long_about = None)]
|
||||
struct Cli {
|
||||
/// The text you want to generate
|
||||
#[arg(short, long)]
|
||||
text: String,
|
||||
|
||||
/// Path to the .ttf font file
|
||||
#[arg(short, long)]
|
||||
font: PathBuf,
|
||||
|
||||
/// How many blocks deep the text should be
|
||||
#[arg(short, long, default_value_t = 1)]
|
||||
depth: u32,
|
||||
|
||||
/// The Minecraft block ID to use for the text body
|
||||
#[arg(short, long, default_value = "minecraft:quartz_block")]
|
||||
block: String,
|
||||
|
||||
/// Output file path (without extension)
|
||||
#[arg(short, long, default_value = "output")]
|
||||
out: PathBuf,
|
||||
|
||||
/// Height of the text in blocks
|
||||
#[arg(long, default_value_t = 16.0)]
|
||||
size: f32,
|
||||
}
|
||||
|
||||
pub fn run() -> anyhow::Result<()> {
|
||||
let cli = Cli::parse();
|
||||
|
||||
tracing::info!("loading font from: {:?}", cli.font);
|
||||
let font_bytes = fs::read(&cli.font)?;
|
||||
|
||||
let font = TtfFont::from_bytes(&font_bytes, cli.size)
|
||||
.map_err(|e| anyhow::anyhow!(e))?;
|
||||
|
||||
tracing::info!("generating voxel grid for text: '{}'", cli.text);
|
||||
let builder = TextBuilder::new(&font).with_depth(cli.depth);
|
||||
let grid = builder.generate(&cli.text);
|
||||
|
||||
tracing::info!(
|
||||
"grid generated: {}x{}x{}",
|
||||
grid.width, grid.height, grid.depth
|
||||
);
|
||||
|
||||
let exporter = McFunctionExporter::new(&cli.block, "minecraft:obsidian");
|
||||
|
||||
let output_bytes = exporter.export(&grid)?;
|
||||
|
||||
let mut out_path = cli.out.clone();
|
||||
out_path.set_extension(exporter.file_extension());
|
||||
|
||||
fs::write(&out_path, output_bytes)?;
|
||||
tracing::info!("saved to: {:?}", out_path);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
1
crates/bin/src/lib.rs
Normal file
1
crates/bin/src/lib.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod cli;
|
||||
10
crates/bin/src/main.rs
Normal file
10
crates/bin/src/main.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
fn main() {
|
||||
tracing_subscriber::fmt()
|
||||
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
|
||||
.init();
|
||||
|
||||
minecraft_text_generator::cli::run().unwrap_or_else(|e| {
|
||||
tracing::error!("{e}");
|
||||
std::process::exit(1);
|
||||
});
|
||||
}
|
||||
10
crates/exporters/Cargo.toml
Normal file
10
crates/exporters/Cargo.toml
Normal file
@@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "exporters"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
lib = { path = "../lib" }
|
||||
anyhow = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
3
crates/exporters/src/lib.rs
Normal file
3
crates/exporters/src/lib.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
mod mcfunction;
|
||||
|
||||
pub use mcfunction::McFunctionExporter;
|
||||
50
crates/exporters/src/mcfunction.rs
Normal file
50
crates/exporters/src/mcfunction.rs
Normal file
@@ -0,0 +1,50 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use lib::{StructureExporter, VoxelType};
|
||||
|
||||
pub struct McFunctionExporter {
|
||||
palette: HashMap<VoxelType, String>,
|
||||
}
|
||||
|
||||
impl McFunctionExporter {
|
||||
pub fn new(body_block: &str, outline_block: &str) -> Self {
|
||||
let mut palette = HashMap::new();
|
||||
|
||||
palette.insert(VoxelType::Body, body_block.to_string());
|
||||
palette.insert(VoxelType::Outline, outline_block.to_string());
|
||||
|
||||
Self { palette }
|
||||
}
|
||||
}
|
||||
|
||||
impl StructureExporter for McFunctionExporter {
|
||||
fn export(&self, grid: &lib::VoxelGrid) -> anyhow::Result<Vec<u8>> {
|
||||
let mut output = String::new();
|
||||
|
||||
output.push_str(&format!("# Generated by Minecraft Text Builder\n"));
|
||||
output.push_str(&format!(
|
||||
"# Dimensions: {}x{}x{}\n\n",
|
||||
grid.width, grid.height, grid.depth
|
||||
));
|
||||
|
||||
for z in 0..grid.depth {
|
||||
for y in 0..grid.height {
|
||||
for x in 0..grid.width {
|
||||
if let Some(voxel_type) = grid.get(x, y, z) {
|
||||
if let Some(block_id) = self.palette.get(&voxel_type) {
|
||||
// ~x ~y ~z generates blocks relative to where the command is executed
|
||||
let command = format!("setblock ~{} ~{} ~{} {}\n", x, y, z, block_id);
|
||||
output.push_str(&command);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(output.into_bytes())
|
||||
}
|
||||
|
||||
fn file_extension(&self) -> &'static str {
|
||||
"mcfunction"
|
||||
}
|
||||
}
|
||||
10
crates/lib/Cargo.toml
Normal file
10
crates/lib/Cargo.toml
Normal file
@@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "lib"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
ab_glyph = "0.2.32"
|
||||
anyhow = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
91
crates/lib/src/engine.rs
Normal file
91
crates/lib/src/engine.rs
Normal file
@@ -0,0 +1,91 @@
|
||||
use crate::{font::FontProvider, grid::VoxelGrid, models::VoxelType};
|
||||
|
||||
pub struct GenerationOptions {
|
||||
pub extrusion_depth: u32,
|
||||
pub generate_outline: bool,
|
||||
// Future options can be added here, such as:
|
||||
// shadow, scale, italics, etc.
|
||||
}
|
||||
|
||||
impl Default for GenerationOptions {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
extrusion_depth: 1,
|
||||
generate_outline: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TextBuilder<'a> {
|
||||
font: &'a dyn FontProvider,
|
||||
options: GenerationOptions,
|
||||
}
|
||||
|
||||
impl<'a> TextBuilder<'a> {
|
||||
pub fn new(font: &'a dyn FontProvider) -> Self {
|
||||
Self {
|
||||
font,
|
||||
options: GenerationOptions::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_depth(mut self, depth: u32) -> Self {
|
||||
self.options.extrusion_depth = depth.max(1);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_outline(mut self, generate: bool) -> Self {
|
||||
self.options.generate_outline = generate;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn generate(&self, text: &str) -> VoxelGrid {
|
||||
let mut total_width = 0;
|
||||
let mut max_height = 0;
|
||||
let mut glyphs_to_render = Vec::new();
|
||||
|
||||
for c in text.chars() {
|
||||
if let Some(glyph) = self.font.get_glyph(c) {
|
||||
total_width += glyph.width + self.font.letter_spacing();
|
||||
max_height = max_height.max(glyph.height);
|
||||
glyphs_to_render.push(glyph);
|
||||
}
|
||||
}
|
||||
|
||||
let padding = if self.options.generate_outline { 2 } else { 0 };
|
||||
|
||||
let mut grid = VoxelGrid::new(
|
||||
total_width + padding,
|
||||
max_height + padding,
|
||||
self.options.extrusion_depth,
|
||||
);
|
||||
|
||||
let mut current_x = if self.options.generate_outline { 1 } else { 0 };
|
||||
let base_y = if self.options.generate_outline { 1 } else { 0 };
|
||||
|
||||
for glyph in glyphs_to_render {
|
||||
for gy in 0..glyph.height {
|
||||
for gx in 0..glyph.width {
|
||||
let glyph_index = (gy * glyph.width + gx) as usize;
|
||||
|
||||
if glyph.data[glyph_index] {
|
||||
let world_x = current_x + gx;
|
||||
let world_y = base_y + (glyph.height - 1 - gy); // Flip vertically
|
||||
|
||||
for z in 0..self.options.extrusion_depth {
|
||||
if let Err(e) = grid.set(world_x, world_y, z, VoxelType::Body) {
|
||||
tracing::warn!("failed to set voxel: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
current_x += glyph.width + self.font.letter_spacing();
|
||||
}
|
||||
|
||||
// TODO: Add outline generation logic here if self.options.generate_outline is true.
|
||||
|
||||
grid
|
||||
}
|
||||
}
|
||||
13
crates/lib/src/error.rs
Normal file
13
crates/lib/src/error.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum VoxelError {
|
||||
#[error("coordinates ({x}, {y}, {z}) out of bounds")]
|
||||
OutOfBounds { x: u32, y: u32, z: u32 },
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum FontError {
|
||||
#[error("failed to parse TTF font data: invalid format")]
|
||||
InvalidTtf,
|
||||
}
|
||||
14
crates/lib/src/font.rs
Normal file
14
crates/lib/src/font.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
/// Represents a single 2D character mapped to a boolean grid.
|
||||
/// `true` means a block exists, `false` means empty space.
|
||||
pub struct Glyph {
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub data: Vec<bool>,
|
||||
}
|
||||
|
||||
pub trait FontProvider {
|
||||
fn get_glyph(&self, character: char) -> Option<Glyph>;
|
||||
fn letter_spacing(&self) -> u32 {
|
||||
1
|
||||
}
|
||||
}
|
||||
1
crates/lib/src/fonts/mod.rs
Normal file
1
crates/lib/src/fonts/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod ttf_font;
|
||||
82
crates/lib/src/fonts/ttf_font.rs
Normal file
82
crates/lib/src/fonts/ttf_font.rs
Normal file
@@ -0,0 +1,82 @@
|
||||
use ab_glyph::{Font, FontVec, PxScale, ScaleFont};
|
||||
|
||||
use crate::{error::FontError, font::FontProvider};
|
||||
|
||||
pub struct TtfFont {
|
||||
font: FontVec,
|
||||
/// The height of the text in Minecraft blocks (pixels)
|
||||
scale: PxScale,
|
||||
/// How "thick" a pixel needs to be to become a block (0.0 to 1.0)
|
||||
/// Lower threshold = thicker text, Higher threshold = thinner text
|
||||
threshold: f32,
|
||||
}
|
||||
|
||||
impl TtfFont {
|
||||
pub fn from_bytes(font_data: &[u8], block_height: f32) -> Result<Self, FontError> {
|
||||
let font = FontVec::try_from_vec(font_data.to_vec())
|
||||
.map_err(|_| FontError::InvalidTtf)?;
|
||||
|
||||
Ok(Self {
|
||||
font,
|
||||
scale: PxScale::from(block_height),
|
||||
threshold: 0.5, // Default threshold, can be adjusted
|
||||
})
|
||||
}
|
||||
|
||||
pub fn with_threshold(mut self, threshold: f32) -> Self {
|
||||
self.threshold = threshold.clamp(0.0, 0.99);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl FontProvider for TtfFont {
|
||||
fn get_glyph(&self, character: char) -> Option<crate::font::Glyph> {
|
||||
let scaled_font = self.font.as_scaled(self.scale);
|
||||
let glyph_id = scaled_font.glyph_id(character);
|
||||
|
||||
if glyph_id.0 == 0 {
|
||||
return None; // Character not found in font
|
||||
}
|
||||
|
||||
let q_glyph = glyph_id.with_scale_and_position(self.scale, ab_glyph::point(0.0, 0.0));
|
||||
|
||||
if let Some(outlined) = scaled_font.outline_glyph(q_glyph) {
|
||||
let bounds = outlined.px_bounds();
|
||||
|
||||
let width = bounds.width().ceil() as u32;
|
||||
let height = bounds.height().ceil() as u32;
|
||||
let mut data = vec![false; (width * height) as usize];
|
||||
|
||||
outlined.draw(|x, y, coverage| {
|
||||
if coverage >= self.threshold {
|
||||
if x < width && y < height {
|
||||
let idx = (y * width + x) as usize;
|
||||
data[idx] = true;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Some(crate::font::Glyph {
|
||||
width,
|
||||
height,
|
||||
data,
|
||||
})
|
||||
} else {
|
||||
if character.is_whitespace() {
|
||||
let width = scaled_font.h_advance(glyph_id).ceil() as u32;
|
||||
|
||||
Some(crate::font::Glyph {
|
||||
width,
|
||||
height: self.scale.y as u32,
|
||||
data: vec![false; (width * self.scale.y as u32) as usize],
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn letter_spacing(&self) -> u32 {
|
||||
1
|
||||
}
|
||||
}
|
||||
44
crates/lib/src/grid.rs
Normal file
44
crates/lib/src/grid.rs
Normal file
@@ -0,0 +1,44 @@
|
||||
use crate::{error::VoxelError, models::VoxelType};
|
||||
|
||||
pub struct VoxelGrid {
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub depth: u32,
|
||||
data: Vec<Option<VoxelType>>,
|
||||
}
|
||||
|
||||
impl VoxelGrid {
|
||||
pub fn new(width: u32, height: u32, depth: u32) -> Self {
|
||||
let capacity = (width * height * depth) as usize;
|
||||
|
||||
Self {
|
||||
width,
|
||||
height,
|
||||
depth,
|
||||
data: vec![None; capacity],
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn get_index(&self, x: u32, y: u32, z: u32) -> Option<usize> {
|
||||
if x >= self.width || y >= self.height || z >= self.depth {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some((x + self.width * (y + self.height * z)) as usize)
|
||||
}
|
||||
|
||||
pub fn set(&mut self, x: u32, y: u32, z: u32, voxel: VoxelType) -> Result<(), VoxelError> {
|
||||
match self.get_index(x, y, z) {
|
||||
Some(index) => {
|
||||
self.data[index] = Some(voxel);
|
||||
Ok(())
|
||||
}
|
||||
None => Err(VoxelError::OutOfBounds { x, y, z }),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get(&self, x: u32, y: u32, z: u32) -> Option<VoxelType> {
|
||||
self.get_index(x, y, z).and_then(|index| self.data[index])
|
||||
}
|
||||
}
|
||||
18
crates/lib/src/lib.rs
Normal file
18
crates/lib/src/lib.rs
Normal file
@@ -0,0 +1,18 @@
|
||||
mod engine;
|
||||
mod error;
|
||||
mod font;
|
||||
mod fonts;
|
||||
mod grid;
|
||||
mod models;
|
||||
|
||||
pub use engine::{GenerationOptions, TextBuilder};
|
||||
pub use error::{FontError, VoxelError};
|
||||
pub use font::FontProvider;
|
||||
pub use fonts::ttf_font::TtfFont;
|
||||
pub use grid::VoxelGrid;
|
||||
pub use models::VoxelType;
|
||||
|
||||
pub trait StructureExporter {
|
||||
fn export(&self, grid: &VoxelGrid) -> anyhow::Result<Vec<u8>>;
|
||||
fn file_extension(&self) -> &'static str;
|
||||
}
|
||||
13
crates/lib/src/models.rs
Normal file
13
crates/lib/src/models.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum VoxelType {
|
||||
Body,
|
||||
Outline,
|
||||
Shadow,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct Point3D {
|
||||
pub x: u32,
|
||||
pub y: u32,
|
||||
pub z: u32,
|
||||
}
|
||||
Reference in New Issue
Block a user