use tape px width instead of device max

This commit is contained in:
Robin Appelman 2025-09-24 17:30:46 +02:00
commit f7910f967b
5 changed files with 20 additions and 14 deletions

4
Cargo.lock generated
View file

@ -1213,7 +1213,7 @@ dependencies = [
[[package]]
name = "ptouch-api"
version = "0.1.0"
version = "0.1.1"
dependencies = [
"axum",
"clap",
@ -1222,7 +1222,7 @@ dependencies = [
"main_error",
"ptouch-rs",
"serde",
"thiserror 1.0.69",
"thiserror 2.0.16",
"tokio",
"toml 0.9.6",
"tracing",

View file

@ -1,6 +1,6 @@
[package]
name = "ptouch-api"
version = "0.1.0"
version = "0.1.1"
edition = "2024"
[dependencies]
@ -11,7 +11,7 @@ image = { version = "0.25.8", features = ["png", "jpeg", "bmp"] }
axum = { version = "0.8.4", features = ["macros", "json"] }
listenfd = "1.0.2"
serde = { version = "1.0.225", features = ["derive"] }
thiserror = "1.0.69"
thiserror = "2.0.16"
toml = "0.9.6"
clap = { version = "4.5.47", features = ["derive"] }
tracing-subscriber = "0.3.20"

View file

@ -50,14 +50,18 @@ on the system access to the label printer.
### Status
The returned status is a json with the following fields
The returned status is a json with the following fields:
- `media_width`: width of the loaded tape in mm.
- `pixel_width`: the width of the printed image in pixels.
- `margin_width`: the left and right margin on the tape that can't be printed.
- `media_type`: material type of the loaded tape.
- `text_color`: text color for the loaded tape.
- `tape_color`: tape color for the loaded tape.
(note that the tape is assumed to be horizontal here, so "width" is the short
short direction)
## Supported printers
The following printers are currently supported

View file

@ -177,7 +177,7 @@ async fn status(State(state): State<App>) -> Result<Json<PStatus>, ApiError> {
let mut printer = state.printer.lock().await;
debug!("reloading printer status");
printer.reload_status().await?;
Ok(Json(PStatus::new(printer.status(), printer.ty())))
Ok(Json(PStatus::new(printer.status())))
}
async fn print(State(state): State<App>, bytes: Bytes) -> Result<(), ApiError> {
@ -186,12 +186,12 @@ async fn print(State(state): State<App>, bytes: Bytes) -> Result<(), ApiError> {
.unwrap()
.decode()?;
let printer = state.printer.lock().await;
let printer_info = printer.ty().info();
if image.height() != printer_info.max_px {
let width = image.width() * printer_info.max_px / image.height();
debug!(width, height = printer_info.max_px, "scaling image");
image = image.resize(width, printer_info.max_px, FilterType::CatmullRom);
let max_px = printer.status().media_width.info().px;
if image.height() != max_px {
let width = image.width() * max_px / image.height();
debug!(width, height = max_px, "scaling image");
image = image.resize(width, max_px, FilterType::CatmullRom);
}
info!(width = image.width(), height = image.height(), "printing");

View file

@ -1,4 +1,4 @@
use ptouch_rs::{MediaType, PrinterType, Status, TapeColor, TapeSize, TextColor};
use ptouch_rs::{MediaType, Status, TapeColor, TapeSize, TextColor};
use serde::Serialize;
#[derive(Debug, Serialize)]
@ -6,16 +6,18 @@ pub struct PStatus {
/// width in mm
media_width: f32,
pixel_width: u32,
margin_width: f32,
media_type: &'static str,
text_color: &'static str,
tape_color: &'static str,
}
impl PStatus {
pub fn new(status: Status, printer_type: PrinterType) -> Self {
pub fn new(status: Status) -> Self {
PStatus {
media_width: tape_size(&status.media_width),
pixel_width: printer_type.info().max_px,
pixel_width: status.media_width.info().px,
margin_width: status.media_width.info().margins,
media_type: media_type(&status.media_type),
text_color: text_color(&status.text_color),
tape_color: tape_color(&status.tape_color),