Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions crates/panache-mcp/src/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,20 @@ pub enum Format {
#[serde(default, deny_unknown_fields)]
pub struct Style {
/// Main font ID from list_fonts. Bundled filenames remain valid. English letters and digits follow it by default.
#[schemars(transform = remove_schema_default)]
pub font: String,
pub paper: Paper,
/// RGB hex color, e.g. #302c30.
pub ink: String,
/// Pixels at the paper's native resolution (794 × 1123 for presets). Accepted range 16–72.
pub font_size: u8,
/// Homework paragraph layout. On a new photo sheet this also selects a handwriting font when font is omitted.
pub homework: bool,
/// Keep this value to reproduce handwriting variations.
#[schemars(transform = remove_seed_default)]
#[schemars(transform = remove_schema_default)]
pub seed: u32,
}
fn remove_seed_default(schema: &mut schemars::Schema) {
fn remove_schema_default(schema: &mut schemars::Schema) {
schema.remove("default");
}

Expand Down Expand Up @@ -97,6 +99,32 @@ pub struct Guide {
pub right: [u32; 2],
}

// Only creation distinguishes an omitted font from an explicitly selected default.
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
pub struct CreationStyle {
#[serde(default, deserialize_with = "explicit_font")]
font: Option<String>,
#[serde(flatten)]
settings: Style,
}
fn explicit_font<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Option<String>, D::Error> {
String::deserialize(d).map(Some)
}

impl CreationStyle {
pub fn resolve(&self, photo: bool, bundled: &[String]) -> Style {
let mut style = self.settings.clone();
if let Some(font) = &self.font {
style.font.clone_from(font);
} else if photo && style.homework && bundled.iter().any(|id| id == "李国夫手写体.ttf")
{
style.font = "李国夫手写体.ttf".into();
}
style
}
}

#[derive(Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct Save {
Expand All @@ -117,8 +145,11 @@ pub struct Save {
pub section: Option<u32>,
/// Set only on creation.
pub title: Option<String>,
/// Set only on creation. Stored with the draft.
pub style: Option<Style>,
/// For a handwritten assignment on a photo sheet, homework=true uses homework layout and, when bundled,
/// 李国夫手写体.ttf for an omitted font. Honor an explicitly requested font or formal style.
/// Preview a sample with letters and digits on the actual sheet. Set only on creation.
#[schemars(with = "Option<Style>")]
pub style: Option<CreationStyle>,
}
#[derive(Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
Expand Down
53 changes: 50 additions & 3 deletions crates/panache-mcp/src/save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rmcp::model::CallToolResult;
use serde_json::{Value, json};

pub(super) async fn run(server: &Panache, input: Save) -> Result<CallToolResult> {
let style = validate(&input)?;
let style = validate(&input, &server.0.fonts)?;
let state = server.0.clone();
let permit = state.prepare_slot.clone().acquire_owned().await?;
let saved = tokio::task::spawn_blocking(move || {
Expand All @@ -16,7 +16,7 @@ pub(super) async fn run(server: &Panache, input: Save) -> Result<CallToolResult>
Ok(saved_reply(saved))
}

fn validate(input: &Save) -> Result<Option<Style>> {
fn validate(input: &Save, bundled: &[String]) -> Result<Option<Style>> {
ensure!(
!input.markdown.trim().is_empty() && !input.markdown.contains('\0'),
"Supply nonempty Markdown without NUL characters"
Expand All @@ -41,7 +41,13 @@ fn validate(input: &Save) -> Result<Option<Style>> {
.is_none_or(|v| v.chars().count() <= 150),
"Title too long"
);
let style = input.style.clone().unwrap_or_default();
let photo = input.paper_path.is_some()
&& input.paper_mode.unwrap_or_default() == crate::PaperMode::Photo;
let style = input
.style
.as_ref()
.map(|s| s.resolve(photo, bundled))
.unwrap_or_default();
style.ink_rgba()?;
ensure!(
(16..=72).contains(&style.font_size),
Expand Down Expand Up @@ -121,3 +127,44 @@ fn saved_reply(mut saved: Value) -> CallToolResult {
}
reply(message, saved)
}

#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;

#[test]
fn photo_homework_font_is_a_creation_default() {
let bundled = vec!["李国夫手写体.ttf".to_owned(), "霞鹜文楷.ttf".to_owned()];
let font = |args: Value, fonts: &[String]| {
let mut args = args;
args["markdown"] = json!("试写");
validate(&serde_json::from_value(args).unwrap(), fonts)
.unwrap()
.unwrap()
.font
};
let homework = json!({"paper_path":"example.png","style":{"homework":true}});
assert_eq!(font(homework.clone(), &bundled), bundled[0]);
assert_eq!(font(homework.clone(), &bundled[1..]), bundled[1]);
for plain in [
json!({"style":{"homework":true}}),
json!({"paper_path":"example.png"}),
json!({"paper_path":"example.png","paper_mode":"flatten","style":{"homework":true}}),
json!({"paper_path":"example.png","style":{"homework":false}}),
] {
assert_eq!(font(plain, &bundled), bundled[1]);
}
let mut explicit = homework;
explicit["style"]["font"] = json!("霞鹜文楷.ttf");
assert_eq!(font(explicit.clone(), &bundled), bundled[1]);
explicit["style"]["font"] = json!("custom-font");
assert_eq!(font(explicit.clone(), &bundled), "custom-font");
explicit["markdown"] = json!("试写");
explicit["style"]["font"] = Value::Null;
assert!(serde_json::from_value::<Save>(explicit).is_err());
assert!(
serde_json::from_value::<Save>(json!({"markdown":"a","style":{"oops":true}})).is_err()
);
}
}