Skip to main content

zenthra_text/managed/
engine.rs

1use std::sync::{Arc, Mutex};
2use crate::renderer::TextRenderer;
3use crate::defaults::cosmic_font::CosmicFontProvider;
4use crate::defaults::swash_raster::SwashRasterizer;
5use crate::traits::FontProvider;
6
7use crate::types::options::TextOptions;
8use crate::types::color::Color;
9use crate::primitives::shaped_buffer::ShapedBuffer;
10
11
12/// The "Zero-Config" entry point for the Zentype engine.
13/// 
14/// This managed API handles the entire lifecycle of font shaping, 
15/// rasterization, and GPU atlas management internally.
16pub struct Zentype {
17    renderer: TextRenderer,
18}
19
20impl Zentype {
21    /// Creates a new Zentype instance with all default engines enabled.
22    pub fn new(device: std::sync::Arc<wgpu::Device>, queue: &wgpu::Queue, config: &wgpu::SurfaceConfiguration) -> Self {
23        // Initialize default engines
24        let shaper = Box::new(CosmicFontProvider::new());
25        
26        // Auto-load Symbols Nerd Font asynchronously if present in assets/fonts/
27        let nerd_font_path = std::path::Path::new("assets/fonts/SymbolsNerdFont-Regular.ttf");
28        if nerd_font_path.exists() {
29            let font_system = shaper.font_system();
30            let path = nerd_font_path.to_path_buf();
31            std::thread::spawn(move || {
32                match font_system.lock().unwrap().db_mut().load_font_file(&path) {
33                    Ok(_) => {}
34                    Err(e) => log::error!("Failed to auto-load Nerd Font: {:?}", e),
35                }
36            });
37        }
38
39        let font_system = shaper.font_system();
40        let rasterizer = Box::new(SwashRasterizer::new(font_system));
41        let atlas = crate::primitives::atlas::GlyphAtlas::new(&device, 2048);
42        
43        // Build the managed renderer
44        let renderer = TextRenderer::new(device, queue, config, shaper, rasterizer, atlas);
45
46        
47        Self { renderer }
48    }
49
50
51    /// Prepares text for drawing in the current frame at the specified position.
52    /// Returns the ShapedBuffer for interactivity.
53    pub fn draw(&mut self, queue: &wgpu::Queue, text: &str, pos: [f32; 2], options: &TextOptions) -> ShapedBuffer {
54        self.renderer.draw(queue, text, pos, options)
55    }
56
57    /// A convenience method for printing simple text labels.
58    pub fn print(&mut self, queue: &wgpu::Queue, text: &str, pos: [f32; 2], size: f32, color: Color) -> ShapedBuffer {
59        let options = TextOptions::new()
60            .font_size(size)
61            .color(color);
62        
63        self.renderer.draw(queue, text, pos, &options)
64    }
65
66    /// Draws a solid colored rectangle with optional clipping.
67    /// Perfect for cursors, underlines, or selection highlights.
68    pub fn draw_rect(&mut self, pos: [f32; 2], size: [f32; 2], color: Color, clip_rect: [f32; 4]) {
69        self.renderer.draw_rect(pos, size, color, clip_rect);
70    }
71
72    /// Finds the character index at the given screen-space coordinates for a specific buffer.
73    pub fn hit_test(&self, buffer: &ShapedBuffer, pos: [f32; 2], options: &TextOptions, mouse_pos: [f32; 2]) -> usize {
74        self.renderer.hit_test(buffer, pos, options, mouse_pos)
75    }
76
77    /// Returns the screen-space position for a given character index in a specific buffer.
78    pub fn position_at(&self, buffer: &ShapedBuffer, pos: [f32; 2], options: &TextOptions, index: usize) -> Option<[f32; 2]> {
79        self.renderer.position_at(buffer, pos, options, index)
80    }
81
82
83
84    /// Resizes the engine's projection to match the window dimensions.
85    pub fn resize(&mut self, queue: &wgpu::Queue, width: u32, height: u32) {
86        self.renderer.resize(queue, width, height);
87    }
88
89    /// Renders all accumulated text instances to the provided RenderPass.
90    pub fn render<'a>(&'a mut self, rpass: &mut wgpu::RenderPass<'a>) {
91        self.renderer.render(rpass);
92    }
93
94    /// Access the underlying renderer for advanced usage.
95    pub fn renderer_mut(&mut self) -> &mut TextRenderer {
96        &mut self.renderer
97    }
98
99    /// Access the font system used by the engine.
100    pub fn font_system(&self) -> Arc<Mutex<cosmic_text::FontSystem>> {
101        self.renderer.font_system()
102    }
103}