Initialize Minecraft text generator project with basic structure and CLI functionality

This commit is contained in:
2026-03-23 00:56:19 +01:00
commit 55d730d542
20 changed files with 900 additions and 0 deletions

91
crates/lib/src/engine.rs Normal file
View 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
View 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
View 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
}
}

View File

@@ -0,0 +1 @@
pub mod ttf_font;

View 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
View 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
View 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
View 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,
}