diff options
| author | metamuffin <metamuffin@disroot.org> | 2025-10-07 17:32:43 +0200 |
|---|---|---|
| committer | metamuffin <metamuffin@disroot.org> | 2025-10-07 17:32:43 +0200 |
| commit | 30bfe4dc801fdfd3ab8d7a5e36bba67eee70d33b (patch) | |
| tree | 59c6b9676e555f2264a1e8bd0f2b4f0fa7acd4e7 /server/book-export/src | |
| parent | f01b9bb2375e1dbaede262c6281dc3a3d068cbb1 (diff) | |
| download | hurrycurry-30bfe4dc801fdfd3ab8d7a5e36bba67eee70d33b.tar hurrycurry-30bfe4dc801fdfd3ab8d7a5e36bba67eee70d33b.tar.bz2 hurrycurry-30bfe4dc801fdfd3ab8d7a5e36bba67eee70d33b.tar.zst | |
Split book exporting to own crate; move locale tool to server dir
Diffstat (limited to 'server/book-export/src')
| -rw-r--r-- | server/book-export/src/book_html.css | 41 | ||||
| -rw-r--r-- | server/book-export/src/book_html.rs | 89 | ||||
| -rw-r--r-- | server/book-export/src/diagram_svg.rs | 152 | ||||
| -rw-r--r-- | server/book-export/src/main.rs | 60 |
4 files changed, 342 insertions, 0 deletions
diff --git a/server/book-export/src/book_html.css b/server/book-export/src/book_html.css new file mode 100644 index 00000000..9171aa5e --- /dev/null +++ b/server/book-export/src/book_html.css @@ -0,0 +1,41 @@ +/* + Hurry Curry! - a game about cooking + Copyright (C) 2025 Hurry Curry! Contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, version 3 of the License only. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. + +*/ +body { + margin: 0px; + background-color: rgb(74, 74, 74); + scroll-behavior: smooth; +} + +.pagegroup { + margin-left: auto; + margin-right: auto; + display: block; + width: 64em; +} + +.page { + display: inline-block; + background-color: antiquewhite; + box-sizing: border-box; + padding: 2em; + margin: 1em; + aspect-ratio: 0.707; + width: 30em; + height: auto; + overflow-y: auto; +} diff --git a/server/book-export/src/book_html.rs b/server/book-export/src/book_html.rs new file mode 100644 index 00000000..08a12e88 --- /dev/null +++ b/server/book-export/src/book_html.rs @@ -0,0 +1,89 @@ +/* + Hurry Curry! - a game about cooking + Copyright (C) 2025 Hurry Curry! Contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, version 3 of the License only. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. + +*/ + +use crate::diagram_svg::diagram_svg; +use hurrycurry_locale::{ + Locale, + message::{MessageDisplayExt, PLAIN}, +}; +use hurrycurry_protocol::{ + Gamedata, Message, + book::{Book, BookPage, Diagram}, +}; + +pub fn render_html_book(data: &Gamedata, book: &Book, locale: &Locale) -> String { + BookR { book, data, locale }.to_string() +} + +markup::define! { + BookR<'a>(data: &'a Gamedata, book: &'a Book, locale: &'a Locale) { + @markup::doctype() + html { + head { + style { @include_str!("book_html.css").replace(" ", "").replace("\n", "") } + title { "Recipe Book - Hurry Curry!" } + } + body { + @for (index, page) in book.pages.iter().enumerate() { + @PageR { data, locale, page, index } + } + } + } + } + + PageR<'a>(index: usize, data: &'a Gamedata, locale: &'a Locale, page: &'a BookPage) { + section.pagegroup[id=format!("page{index}")] { + @match page { + BookPage::Cover => { + div.page {} + div.page {} + } + BookPage::Recipe { title, description, diagram } => { + div.page { + h1 { @MessageR { data, locale, message: title } } + p { @MessageR { data, locale, message: description } } + } + div.page { + @DiagramR { data, diagram } + } + } + BookPage::Text { .. } => { + div.page {} + div.page {} + } + BookPage::Contents { title, table } => { + div.page { + h1 { @MessageR { data, locale, message: title } } + ol { @for (label, page) in table { + li { a[href=format!("#page{page}")] { @MessageR { data, locale, message: label } } } + }} + } + div.page {} + } + } + } + } + + MessageR<'a>(data: &'a Gamedata, locale: &'a Locale, message: &'a Message) { + @message.display_message(*locale, *data, &PLAIN) + } + + DiagramR<'a>(data: &'a Gamedata, diagram: &'a Diagram) { + @markup::raw(diagram_svg(data, diagram).unwrap()) + } +} diff --git a/server/book-export/src/diagram_svg.rs b/server/book-export/src/diagram_svg.rs new file mode 100644 index 00000000..d006ce78 --- /dev/null +++ b/server/book-export/src/diagram_svg.rs @@ -0,0 +1,152 @@ +/* + Hurry Curry! - a game about cooking + Copyright (C) 2025 Hurry Curry! Contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, version 3 of the License only. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. + +*/ + +use anyhow::Result; +use hurrycurry_protocol::{ + Gamedata, Message, + book::{Diagram, DiagramNode, NodeStyle}, + glam::Vec2, +}; +use std::fmt::Write; + +const SIZE: f32 = 64.; +const HSIZE: f32 = SIZE / 2.; + +pub fn diagram_svg(data: &Gamedata, diagram: &Diagram) -> Result<String> { + let mut out = String::new(); + + let (vb_min, vb_max) = diagram + .nodes + .iter() + .map(|n| (n.position, n.position)) + .reduce(|(xa, xb), (ya, yb)| (xa.min(ya), xb.max(yb))) + .unwrap(); + + writeln!( + out, + r#"<svg xmlns="http://www.w3.org/2000/svg" viewBox="{} {} {} {}" width="{}" height="{}">"#, + vb_min.x - HSIZE, + vb_min.y - HSIZE, + vb_max.x + HSIZE, + vb_max.y + HSIZE, + vb_max.x - vb_min.x + SIZE, + vb_max.y - vb_min.y + SIZE, + )?; + + for node in &diagram.nodes { + match node.label { + Message::Translation { .. } => {} + Message::Text(_) => {} + Message::Item(item_index) => { + image_node( + &mut out, + node, + format!("items/{}.png", data.item_name(item_index)), + )?; + } + Message::Tile(tile_index) => { + image_node( + &mut out, + node, + format!("tiles/{}.png", data.tile_name(tile_index)), + )?; + } + } + } + for edge in &diagram.edges { + let src_node = &diagram.nodes[edge.src]; + let dst_node = &diagram.nodes[edge.dst]; + let src = node_edge_connect_pos(src_node, dst_node); + let mut dst = node_edge_connect_pos(dst_node, src_node); + + let dir = (src - dst).normalize_or_zero(); + let tip0 = dst; + let tip1 = dst + (dir + dir.perp() * 0.5) * 10.; + let tip2 = dst + (dir + dir.perp() * -0.5) * 10.; + dst += dir * 5.; // prevent miter line cap from peeking out + + // line path + writeln!( + out, + r#"<path fill="none" stroke="black" stroke-width="2" d="M{} {} L{} {}" />"#, + src.x, src.y, dst.x, dst.y, + )?; + // tip path + writeln!( + out, + r#"<path fill="black" stroke="none" d="M{} {} L{} {} L{} {} Z" />"#, + tip0.x, tip0.y, tip1.x, tip1.y, tip2.x, tip2.y + )?; + } + + writeln!(out, "</svg>")?; + Ok(out) +} + +fn node_edge_connect_pos(src: &DiagramNode, dst: &DiagramNode) -> Vec2 { + let dir = (dst.position - src.position).normalize_or_zero(); + if matches!( + src.style, + NodeStyle::FinalProduct | NodeStyle::IntermediateProduct + ) { + src.position + dir * HSIZE // circle + } else { + src.position + dir / dir.y.abs() * HSIZE // square (only +Y and -Y sides) + } +} + +pub(crate) fn node_color(node: &DiagramNode) -> &'static str { + match node.style { + NodeStyle::FinalProduct => "#555", + NodeStyle::IntermediateProduct => "#333", + NodeStyle::ProcessActive => "#47c42b", + NodeStyle::ProcessPassive => "#c4a32b", + NodeStyle::ProcessInstant => "#5452d8", + } +} + +fn image_node(out: &mut String, node: &DiagramNode, path: String) -> Result<()> { + if matches!( + node.style, + NodeStyle::FinalProduct | NodeStyle::IntermediateProduct + ) { + writeln!( + out, + r#"<circle cx="{}" cy="{}" r="{}" stroke="none" fill="{}" />"#, + node.position.x, + node.position.y, + HSIZE, + node_color(node) + )?; + } else { + writeln!( + out, + r#"<rect x="{}" y="{}" width="{SIZE}" height="{SIZE}" stroke="none" fill="{}" />"#, + node.position.x - HSIZE, + node.position.y - HSIZE, + node_color(node) + )?; + } + writeln!( + out, + r#"<image href="{path}" x="{}" y="{}" width="{SIZE}" height="{SIZE}" />"#, + node.position.x - HSIZE, + node.position.y - HSIZE, + )?; + Ok(()) +} diff --git a/server/book-export/src/main.rs b/server/book-export/src/main.rs new file mode 100644 index 00000000..b57aa04f --- /dev/null +++ b/server/book-export/src/main.rs @@ -0,0 +1,60 @@ +/* + Hurry Curry! - a game about cooking + Copyright (C) 2025 Hurry Curry! Contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, version 3 of the License only. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. + +*/ +pub mod book_html; +pub mod diagram_svg; + +use crate::book_html::render_html_book; +use anyhow::Result; +use clap::{Parser, ValueEnum}; +use hurrycurry_data::{book::book, index::DataIndex}; +use hurrycurry_locale::FALLBACK_LOCALE; + +#[derive(Parser)] +struct Args { + #[arg(short, long)] + format: ExportFormat, + #[arg(short, long, default_value = "5star")] + map: String, +} + +#[derive(ValueEnum, Clone, Copy)] +enum ExportFormat { + Json, + Html, +} + +fn main() -> Result<()> { + env_logger::init_from_env("LOG"); + let args = Args::parse(); + + let mut index = DataIndex::default(); + index.reload()?; + let (data, serverdata) = index.generate(&args.map)?; + + let book = book(&data, &serverdata)?; + + match args.format { + ExportFormat::Json => { + println!("{}", serde_json::to_string(&book).unwrap()) + } + ExportFormat::Html => { + println!("{}", render_html_book(&data, &book, &FALLBACK_LOCALE)); + } + } + Ok(()) +} |