diff --git a/showbits-thermal-printer/src/documents.rs b/showbits-thermal-printer/src/documents.rs index 9d72262..8592c7a 100644 --- a/showbits-thermal-printer/src/documents.rs +++ b/showbits-thermal-printer/src/documents.rs @@ -1,9 +1,9 @@ use showbits_typst::Typst; -pub use self::{image::*, text::*}; +pub use self::image::*; mod image; -mod text; +pub mod text; fn typst_with_lib() -> Typst { Typst::new() diff --git a/showbits-thermal-printer/src/documents/text/main.typ b/showbits-thermal-printer/src/documents/text/main.typ index aed3c9f..1c6c3fc 100644 --- a/showbits-thermal-printer/src/documents/text/main.typ +++ b/showbits-thermal-printer/src/documents/text/main.typ @@ -3,11 +3,11 @@ #let data = json("data.json") -#if data.at("force_wrap", default: false) { +#if data.force_wrap { show regex("."): it => it + sym.zws data.text } else { data.text } -#if data.at("feed", default: false) { +#if data.feed { lib.feed } diff --git a/showbits-thermal-printer/src/documents/text/mod.rs b/showbits-thermal-printer/src/documents/text/mod.rs index cb47665..febe571 100644 --- a/showbits-thermal-printer/src/documents/text/mod.rs +++ b/showbits-thermal-printer/src/documents/text/mod.rs @@ -1,8 +1,13 @@ +use axum::{Form, extract::State}; use serde::{Deserialize, Serialize}; -use showbits_typst::Typst; + +use crate::{ + drawer::{Command, NewTypstDrawing}, + server::Server, +}; #[derive(Serialize, Deserialize)] -pub struct Text { +pub struct Data { pub text: String, #[serde(default)] pub force_wrap: bool, @@ -10,10 +15,13 @@ pub struct Text { pub feed: bool, } -impl From for Typst { - fn from(value: Text) -> Self { - super::typst_with_lib() - .with_json("/data.json", &value) - .with_main_file(include_str!("main.typ")) - } +pub async fn post(server: State, request: Form) { + let typst = super::typst_with_lib() + .with_json("/data.json", &request.0) + .with_main_file(include_str!("main.typ")); + + let _ = server + .tx + .send(Command::draw(NewTypstDrawing::new(typst))) + .await; } diff --git a/showbits-thermal-printer/src/drawer.rs b/showbits-thermal-printer/src/drawer.rs index 77e2909..bafc943 100644 --- a/showbits-thermal-printer/src/drawer.rs +++ b/showbits-thermal-printer/src/drawer.rs @@ -6,7 +6,6 @@ mod egg; mod image; mod new_typst; mod photo; -mod text; mod tictactoe; mod typst; @@ -18,8 +17,8 @@ use crate::persistent_printer::PersistentPrinter; pub use self::{ backlog::BacklogDrawing, calendar::CalendarDrawing, cells::CellsDrawing, chat_message::ChatMessageDrawing, egg::EggDrawing, image::ImageDrawing, - new_typst::NewTypstDrawing, photo::PhotoDrawing, text::TextDrawing, - tictactoe::TicTacToeDrawing, typst::TypstDrawing, + new_typst::NewTypstDrawing, photo::PhotoDrawing, tictactoe::TicTacToeDrawing, + typst::TypstDrawing, }; pub const FEED: f32 = 96.0; diff --git a/showbits-thermal-printer/src/drawer/text.rs b/showbits-thermal-printer/src/drawer/text.rs deleted file mode 100644 index 401fdc2..0000000 --- a/showbits-thermal-printer/src/drawer/text.rs +++ /dev/null @@ -1,29 +0,0 @@ -use showbits_common::{Node, Tree, WidgetExt, color::WHITE, widgets::Text}; -use taffy::style_helpers::percent; - -use crate::persistent_printer::PersistentPrinter; - -use super::{Context, Drawing}; - -pub struct TextDrawing(pub String); - -impl Drawing for TextDrawing { - fn draw(&self, printer: &mut PersistentPrinter, ctx: &mut Context) -> anyhow::Result<()> { - let mut tree = Tree::::new(WHITE); - - let text = Text::new() - .with_metrics(Text::default_metrics().scale(2.0)) - .and_plain(&self.0) - .widget(&mut ctx.font_stuff) - .node() - .register(&mut tree)?; - - let root = Node::empty() - .with_size_width(percent(1.0)) - .and_child(text) - .register(&mut tree)?; - - printer.print_tree(&mut tree, ctx, root)?; - Ok(()) - } -} diff --git a/showbits-thermal-printer/src/server.rs b/showbits-thermal-printer/src/server.rs index 4590281..9b8dda7 100644 --- a/showbits-thermal-printer/src/server.rs +++ b/showbits-thermal-printer/src/server.rs @@ -14,18 +14,18 @@ use showbits_common::widgets::DitherAlgorithm; use tokio::{net::TcpListener, sync::mpsc}; use crate::{ - documents::{Image, Text}, + documents::{self, Image}, drawer::{ CalendarDrawing, CellsDrawing, ChatMessageDrawing, Command, EggDrawing, ImageDrawing, - NewTypstDrawing, PhotoDrawing, TextDrawing, TicTacToeDrawing, TypstDrawing, + NewTypstDrawing, PhotoDrawing, TicTacToeDrawing, TypstDrawing, }, }; use self::{r#static::get_static_file, statuscode::status_code}; #[derive(Clone)] -struct Server { - tx: mpsc::Sender, +pub struct Server { + pub tx: mpsc::Sender, } pub async fn run(tx: mpsc::Sender, addr: String) -> anyhow::Result<()> { @@ -36,10 +36,12 @@ pub async fn run(tx: mpsc::Sender, addr: String) -> anyhow::Result<()> .route("/egg", post(post_egg).fallback(get_static_file)) .route("/image", post(post_image).fallback(get_static_file)) .route("/photo", post(post_photo).fallback(get_static_file)) - .route("/text", post(post_text)) + .route( + "/text", + post(documents::text::post).fallback(get_static_file), + ) .route("/tictactoe", post(post_tictactoe)) .route("/typst", post(post_typst).fallback(get_static_file)) - .route("/test", post(post_test).fallback(get_static_file)) .route("/test2", post(post_test2).fallback(get_static_file)) .fallback(get(get_static_file)) .layer(DefaultBodyLimit::max(32 * 1024 * 1024)) // 32 MiB @@ -194,20 +196,6 @@ async fn post_photo(server: State, mut multipart: Multipart) -> somehow: Ok(Redirect::to("photo").into_response()) } -// /text - -#[derive(Deserialize)] -struct PostTextForm { - text: String, -} - -async fn post_text(server: State, request: Form) { - let _ = server - .tx - .send(Command::draw(TextDrawing(request.0.text))) - .await; -} - // /tictactoe async fn post_tictactoe(server: State) { @@ -228,15 +216,6 @@ async fn post_typst(server: State, request: Form) { .await; } -// /test - -async fn post_test(server: State, request: Form) { - let _ = server - .tx - .send(Command::draw(NewTypstDrawing::new(request.0))) - .await; -} - // /test2 async fn post_test2(server: State, mut multipart: Multipart) -> somehow::Result {