Add /typst

This commit is contained in:
Joscha 2024-05-13 12:16:19 +02:00
parent 1464d074bf
commit 9b6865ff50
7 changed files with 1636 additions and 8 deletions

View file

@ -6,6 +6,7 @@ mod image;
mod photo;
mod text;
mod tictactoe;
mod typst;
use showbits_common::widgets::{FontStuff, HasFontStuff};
use tokio::sync::mpsc;
@ -15,7 +16,7 @@ use crate::printer::Printer;
pub use self::{
calendar::CalendarDrawing, cells::CellsDrawing, chat_message::ChatMessageDrawing,
egg::EggDrawing, image::ImageDrawing, photo::PhotoDrawing, text::TextDrawing,
tictactoe::TicTacToeDrawing,
tictactoe::TicTacToeDrawing, typst::TypstDrawing,
};
#[derive(Default)]

View file

@ -0,0 +1,25 @@
use showbits_common::{color::WHITE, widgets::Typst, Node, Tree, WidgetExt};
use taffy::style_helpers::percent;
use crate::printer::Printer;
use super::{Context, Drawing};
pub struct TypstDrawing(pub String);
impl Drawing for TypstDrawing {
fn draw(&self, printer: &mut Printer, ctx: &mut Context) -> anyhow::Result<()> {
let mut tree = Tree::<Context>::new(WHITE);
let typst = Typst::new(self.0.clone()).node().register(&mut tree)?;
let root = Node::empty()
.with_size_width(percent(1.0))
.and_child(typst)
.register(&mut tree)?;
printer.print_tree(&mut tree, ctx, root)?;
printer.feed()?;
Ok(())
}
}

View file

@ -14,7 +14,7 @@ use tokio::{net::TcpListener, sync::mpsc};
use crate::drawer::{
CalendarDrawing, CellsDrawing, ChatMessageDrawing, Command, EggDrawing, ImageDrawing,
PhotoDrawing, TextDrawing, TicTacToeDrawing,
PhotoDrawing, TextDrawing, TicTacToeDrawing, TypstDrawing,
};
use self::{r#static::get_static_file, statuscode::status_code};
@ -34,6 +34,7 @@ pub async fn run(tx: mpsc::Sender<Command>, addr: String) -> anyhow::Result<()>
.route("/photo", post(post_photo).fallback(get_static_file))
.route("/text", post(post_text))
.route("/tictactoe", post(post_tictactoe))
.route("/typst", post(post_typst))
.fallback(get(get_static_file))
.layer(DefaultBodyLimit::max(32 * 1024 * 1024)) // 32 MiB
.with_state(Server { tx });
@ -191,3 +192,17 @@ async fn post_text(server: State<Server>, request: Form<PostTextForm>) {
async fn post_tictactoe(server: State<Server>) {
let _ = server.tx.send(Command::draw(TicTacToeDrawing)).await;
}
// /typst
#[derive(Deserialize)]
struct PostTypstForm {
source: String,
}
async fn post_typst(server: State<Server>, request: Form<PostTypstForm>) {
let _ = server
.tx
.send(Command::draw(TypstDrawing(request.0.source)))
.await;
}