Skip to content
zachzurn edited this page Sep 29, 2024 · 6 revisions

Thermal contains two main crates:

Quick Start

To get started, it's easiest to use the .thermal file format.

  1. Create a file called hello_world.thermal

  2. Add these lines to the file:

'// Initialize
ESC "@"

'// Print Hello World
"Hello World"
  1. Run the code to render the image
    //A: Pick a destination for your image
    let dest = format!(
        "{}/{}/{}/{}",
        env!("CARGO_MANIFEST_DIR"),
        "resources",
        "out",
        "hello_world"
    );

    let source = format!(
        "{}/{}/{}/{}",
        env!("CARGO_MANIFEST_DIR"),
        "resources",
        "test",
        "hello_world.thermal"
    );

    //B: Parse the thermal file and convert it to binary
    let text = std::fs::read_to_string(source).unwrap();
    let bytes = parse_str(&text)

    //C: If you want to work with a binary file, skip B: and uncomment the next line
    // let bytes = std::fs::read(source).unwrap()

    //D: Create the image renderer, or use HtmlRenderer
    let mut image_renderer = ImageRenderer::new(out);

    //E: Create a context instance to hold the current graphic/style state
    let mut context = Context::new();

    // Create a clojure to tell the renderer to handle parsed commands
    let on_new_command = move |cmd: Command| {
        image_renderer.process_command(&mut context, &cmd);
    };

    //Create the parser
    let mut command_parser = thermal_parser::new_esc_pos_parser(Box::from(on_new_command));

    //Parse the bytes, which will render out the image 
    command_parser.parse_bytes(&bytes);

Thermal Parser

Handles parsing and emitting commands and contains the context that may change with each command.

Thermal Renderer

Uses the parser crate and allows for the creation of renderers.

  • HTML Renderer
  • Image Renderer
Clone this wiki locally