Turn Text into drawing

This commit is contained in:
Joscha 2024-03-30 12:32:52 +01:00
parent dddef6b3b5
commit 0bc2942b2b
3 changed files with 36 additions and 24 deletions

View file

@ -1,5 +1,6 @@
mod calendar;
mod cells;
mod text;
use image::{Luma, Pixel, RgbaImage};
use palette::{FromColor, IntoColor, LinLumaa};
@ -16,7 +17,7 @@ use tokio::sync::mpsc;
use crate::printer::Printer;
pub use self::{calendar::CalendarDrawing, cells::CellsDrawing};
pub use self::{calendar::CalendarDrawing, cells::CellsDrawing, text::TextDrawing};
#[derive(Default)]
pub struct Context {
@ -32,7 +33,6 @@ pub struct BoxedDrawing(Box<dyn Drawing + Send>);
pub enum Command {
Draw(BoxedDrawing),
Text(String),
Image { image: RgbaImage, bright: bool },
Photo { image: RgbaImage, title: String },
ChatMessage { username: String, content: String },
@ -78,7 +78,6 @@ impl Drawer {
match command {
Command::Draw(drawing) => drawing.0.draw(&mut self.printer, &mut self.ctx)?,
Command::Text(text) => self.on_text(text)?,
Command::Image { image, bright } => self.on_image(image, bright)?,
Command::Photo { image, title } => self.on_photo(image, title)?,
Command::ChatMessage { username, content } => {
@ -88,25 +87,6 @@ impl Drawer {
Ok(())
}
fn on_text(&mut self, text: String) -> anyhow::Result<()> {
let mut tree = Tree::<Context>::new(WHITE);
let text = Text::new()
.with_metrics(Text::default_metrics().scale(2.0))
.and_plain(text)
.widget(&mut self.ctx.font_stuff)
.node()
.register(&mut tree)?;
let root = Node::empty()
.with_size_width(percent(1.0))
.and_child(text)
.register(&mut tree)?;
self.printer.print_tree(&mut tree, &mut self.ctx, root)?;
Ok(())
}
fn on_image(&mut self, mut image: RgbaImage, bright: bool) -> anyhow::Result<()> {
let mut tree = Tree::<Context>::new(WHITE);

View file

@ -0,0 +1,29 @@
use showbits_common::{color::WHITE, widgets::Text, Node, Tree, WidgetExt};
use taffy::style_helpers::percent;
use crate::printer::Printer;
use super::{Context, Drawing};
pub struct TextDrawing(pub String);
impl Drawing for TextDrawing {
fn draw(&self, printer: &mut Printer, ctx: &mut Context) -> anyhow::Result<()> {
let mut tree = Tree::<Context>::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(())
}
}

View file

@ -12,7 +12,7 @@ use axum::{
use serde::Deserialize;
use tokio::{net::TcpListener, sync::mpsc};
use crate::drawer::{CalendarDrawing, CellsDrawing, Command};
use crate::drawer::{CalendarDrawing, CellsDrawing, Command, TextDrawing};
use self::{r#static::get_static_file, statuscode::status_code};
@ -44,7 +44,10 @@ struct PostTextForm {
}
async fn post_text(server: State<Server>, request: Form<PostTextForm>) {
let _ = server.tx.send(Command::Text(request.0.text)).await;
let _ = server
.tx
.send(Command::draw(TextDrawing(request.0.text)))
.await;
}
async fn post_image(server: State<Server>, mut multipart: Multipart) -> somehow::Result<Response> {