diff options
| author | jacksonfu14 <jackson@fashionunited.com> | 2025-11-14 18:22:21 +0100 |
|---|---|---|
| committer | Jackson Moore <jacksonmoore@tuta.io> | 2025-11-14 18:22:21 +0100 |
| commit | 6aeb5a91c42446b5ea68ec81dd01079a74787c96 (patch) | |
| tree | 4de217802fc9701ca1a0dbb08685972885264ee8 /morphology-img | |
| parent | 30e48fe5083db3f83d68c830fb43c6a7a955a99c (diff) | |
feature: add morphology rendering module
Diffstat (limited to 'morphology-img')
| -rw-r--r-- | morphology-img/Cargo.toml | 13 | ||||
| -rw-r--r-- | morphology-img/src/error.rs | 19 | ||||
| -rw-r--r-- | morphology-img/src/file_gen.rs | 1 | ||||
| -rw-r--r-- | morphology-img/src/lib.rs | 9 | ||||
| -rw-r--r-- | morphology-img/src/pitch.rs | 43 | ||||
| -rw-r--r-- | morphology-img/src/render.rs | 1 | ||||
| -rw-r--r-- | morphology-img/src/template.rs | 1 | ||||
| -rw-r--r-- | morphology-img/static/elements.ily | 96 | ||||
| -rw-r--r-- | morphology-img/static/entry.ily | 24 | ||||
| -rw-r--r-- | morphology-img/static/format.ily | 45 | ||||
| -rw-r--r-- | morphology-img/static/template.ly | 12 | ||||
| -rw-r--r-- | morphology-img/tests/error.rs | 32 | ||||
| -rw-r--r-- | morphology-img/tests/pitch.rs | 85 |
13 files changed, 381 insertions, 0 deletions
diff --git a/morphology-img/Cargo.toml b/morphology-img/Cargo.toml new file mode 100644 index 0000000..07a47dd --- /dev/null +++ b/morphology-img/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "morphology-img" +edition = "2024" +version.workspace = true +authors.workspace = true + +[dependencies] +meta_affine = { path = "../morphology" } +thiserror = "2.0" +itertools = "0.13" + +[dev-dependencies] +tempfile = "3" diff --git a/morphology-img/src/error.rs b/morphology-img/src/error.rs new file mode 100644 index 0000000..cee092d --- /dev/null +++ b/morphology-img/src/error.rs @@ -0,0 +1,19 @@ +use thiserror::Error; + +#[derive(Error, Debug)] +pub enum RenderError { + #[error("LilyPond not found: {0}")] + LilyPondNotFound(String), + + #[error("LilyPond execution failed: {0}")] + LilyPondExecution(String), + + #[error("Template error: {0}")] + Template(String), + + #[error("File I/O error: {0}")] + FileIO(#[from] std::io::Error), + + #[error("Invalid sequence: {0}")] + InvalidSequence(String), +} diff --git a/morphology-img/src/file_gen.rs b/morphology-img/src/file_gen.rs new file mode 100644 index 0000000..c429719 --- /dev/null +++ b/morphology-img/src/file_gen.rs @@ -0,0 +1 @@ +// .ly file generation and temp directory management diff --git a/morphology-img/src/lib.rs b/morphology-img/src/lib.rs new file mode 100644 index 0000000..28dfc58 --- /dev/null +++ b/morphology-img/src/lib.rs @@ -0,0 +1,9 @@ +pub mod error; +pub mod pitch; +pub mod template; +pub mod file_gen; +pub mod render; + +// Re-export key types +pub use error::RenderError; +pub use pitch::{LilyPondPitch, LilyPondNote, Duration}; diff --git a/morphology-img/src/pitch.rs b/morphology-img/src/pitch.rs new file mode 100644 index 0000000..dd7d87e --- /dev/null +++ b/morphology-img/src/pitch.rs @@ -0,0 +1,43 @@ +/// Represents a LilyPond absolute pitch name (e.g., "c'", "bes,", "fis") +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct LilyPondPitch(String); + +impl LilyPondPitch { + pub fn new(s: impl Into<String>) -> Self { + Self(s.into()) + } + + pub fn as_str(&self) -> &str { + &self.0 + } +} + +/// Represents a complete LilyPond note with duration (e.g., "c'4") +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct LilyPondNote(String); + +impl LilyPondNote { + pub fn new(pitch: LilyPondPitch, duration: Duration) -> Self { + Self(format!("{}{}", pitch.as_str(), duration.as_str())) + } + + pub fn as_str(&self) -> &str { + &self.0 + } +} + +/// Represents a note duration in LilyPond (e.g., "4", "8", "2") +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Duration(u8); + +impl Duration { + pub const QUARTER: Duration = Duration(4); + + pub fn new(value: u8) -> Self { + Self(value) + } + + pub fn as_str(&self) -> String { + self.0.to_string() + } +} diff --git a/morphology-img/src/render.rs b/morphology-img/src/render.rs new file mode 100644 index 0000000..cd5b293 --- /dev/null +++ b/morphology-img/src/render.rs @@ -0,0 +1 @@ +// LilyPond CLI invocation for SVG generation diff --git a/morphology-img/src/template.rs b/morphology-img/src/template.rs new file mode 100644 index 0000000..afa091b --- /dev/null +++ b/morphology-img/src/template.rs @@ -0,0 +1 @@ +// Template handling and substitution diff --git a/morphology-img/static/elements.ily b/morphology-img/static/elements.ily new file mode 100644 index 0000000..59fef65 --- /dev/null +++ b/morphology-img/static/elements.ily @@ -0,0 +1,96 @@ +single = { + \stopStaff + \override StaffContainer.StaffSymbol.line-count = 0 + \override StaffContainer.StaffSymbol.line-positions = #'(-0.1 0 0.1) + \override StaffContainer.StaffSymbol.ledger-positions = #'(-2 2) + \startStaff +} + +skipUp = { + \stopStaff + \override StaffContainer.StaffSymbol.line-count = 0 + \override StaffContainer.StaffSymbol.line-positions = #'(-0.1 0 0.1 3) + \override StaffContainer.StaffSymbol.ledger-positions = #'(-2 020) + \startStaff +} + +skipDown = { + \stopStaff + \override StaffContainer.StaffSymbol.line-count = 0 + \override StaffContainer.StaffSymbol.line-positions = #'(-3 -0.1 0 0.1) + \override StaffContainer.StaffSymbol.ledger-positions = #'(-20 0 2) + \startStaff +} + +skipAround = { + \stopStaff + \override StaffContainer.StaffSymbol.line-count = 0 + \override StaffContainer.StaffSymbol.line-positions = #'(-3 -0.1 0 0.1 3) + \override StaffContainer.StaffSymbol.ledger-positions = #'() + \startStaff +} + +springUp = { + \stopStaff + \override StaffContainer.StaffSymbol.line-count = 0 + \override StaffContainer.StaffSymbol.line-positions = #'(-0.1 0 0.1 4) + \override StaffContainer.StaffSymbol.ledger-positions = #'(-2 6) + \startStaff +} + +springDown = { + \stopStaff + \override StaffContainer.StaffSymbol.line-count = 0 + \override StaffContainer.StaffSymbol.line-positions = #'(-4 -0.1 0 0.1) + \override StaffContainer.StaffSymbol.ledger-positions = #'(-6 2) + \startStaff +} + +springAround = { + \stopStaff + \override StaffContainer.StaffSymbol.line-count = 0 + \override StaffContainer.StaffSymbol.line-positions = #'(-4 -0.1 0 0.1 4) + \override StaffContainer.StaffSymbol.ledger-positions = #'(-6 2) + \startStaff +} + +climbDown = { + \stopStaff + \override StaffContainer.StaffSymbol.line-count = 0 + \override StaffContainer.StaffSymbol.line-positions = #'(-5 -0.1 0 0.1) + \override StaffContainer.StaffSymbol.ledger-positions = #'(-2 2) + \startStaff +} + +climbUp = { + \stopStaff + \override StaffContainer.StaffSymbol.line-count = 0 + \override StaffContainer.StaffSymbol.line-positions = #'(-0.1 0 0.1 5) + \override StaffContainer.StaffSymbol.ledger-positions = #'(-2 2) + \startStaff +} + +ladderUp = { + \stopStaff + \override StaffContainer.StaffSymbol.line-count = 0 + \override StaffContainer.StaffSymbol.line-positions = #'(-0.1 0 0.1 3 6) + \override StaffContainer.StaffSymbol.ledger-positions = #'() + \startStaff +} + +ladderDown = { + \stopStaff + \override StaffContainer.StaffSymbol.line-count = 0 + \override StaffContainer.StaffSymbol.line-positions = #'(-6 -3 -0.1 0 0.1) + \override StaffContainer.StaffSymbol.ledger-positions = #'() + \startStaff +} + +sp = { + \hideNotes b4 \unHideNotes +} + +back = { + \hideNotes b2 \unHideNotes +} + diff --git a/morphology-img/static/entry.ily b/morphology-img/static/entry.ily new file mode 100644 index 0000000..cf7b014 --- /dev/null +++ b/morphology-img/static/entry.ily @@ -0,0 +1,24 @@ +entry = + #(define-music-function + (morpheme) + (ly:music?) + #{ + \transpose c c' << + \new StaffContainer { + \override StaffContainer.Stem.transparent = ##t + \override StaffContainer.StaffSymbol.color = #white + \override StaffContainer.StaffSymbol.ledger-positions = #'(-10 -5 -3 3 5 10) + \override StaffContainer.StaffSymbol.line-count = 0 + \override StaffContainer.StaffSymbol.line-positions = #'(-5 -0.15 0 0.15 5) + \cadenzaOn + << + \new Staff { + \override NoteHead.color = #white + #morpheme + } + >> + \cadenzaOff + } + >> + #}) + diff --git a/morphology-img/static/format.ily b/morphology-img/static/format.ily new file mode 100644 index 0000000..346d3eb --- /dev/null +++ b/morphology-img/static/format.ily @@ -0,0 +1,45 @@ +\paper { + #(set-paper-size '(cons (* 24 mm) (* 24 mm))) + indent = 0 + ragged-bottom = ##f + ragged-last-bottom = ##f + line-width = 24 + left-margin = 0 + right-margin = 0 + bottom-margin = 0 + top-margin = 0 + paper-width = 24 +} + +\layout { + indent = 0 + left-margin = 0 + raggedright = ##f + \context { + \Score + \accepts StaffContainer + \denies Staff + } + \context { + \type Engraver_group + \consists Separating_line_group_engraver + \consists "Axis_group_engraver" + \accepts "Staff" + \defaultchild "Staff" + \consists "Parenthesis_engraver" + \name StaffContainer + } + \context { + \Staff + \remove Axis_group_engraver + \remove Separating_line_group_engraver + \remove Clef_engraver + \remove Time_signature_engraver + } +} + +\header { + tagline = ##f + composer = ##f +} + diff --git a/morphology-img/static/template.ly b/morphology-img/static/template.ly new file mode 100644 index 0000000..f0bb7b5 --- /dev/null +++ b/morphology-img/static/template.ly @@ -0,0 +1,12 @@ +\version "2.24.0" + +\include "format.ily" +\include "elements.ily" +\include "entry.ily" + +\score { + \entry { + %%% + } +} + diff --git a/morphology-img/tests/error.rs b/morphology-img/tests/error.rs new file mode 100644 index 0000000..3ce808b --- /dev/null +++ b/morphology-img/tests/error.rs @@ -0,0 +1,32 @@ +use morphology_img::RenderError; + +#[test] +fn test_error_display() { + let err = RenderError::LilyPondNotFound("test".to_string()); + assert_eq!(err.to_string(), "LilyPond not found: test"); +} + +#[test] +fn test_lilypond_execution_error() { + let err = RenderError::LilyPondExecution("stderr output".to_string()); + assert_eq!(err.to_string(), "LilyPond execution failed: stderr output"); +} + +#[test] +fn test_template_error() { + let err = RenderError::Template("missing placeholder".to_string()); + assert_eq!(err.to_string(), "Template error: missing placeholder"); +} + +#[test] +fn test_invalid_sequence_error() { + let err = RenderError::InvalidSequence("empty sequence".to_string()); + assert_eq!(err.to_string(), "Invalid sequence: empty sequence"); +} + +#[test] +fn test_io_error_conversion() { + let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found"); + let err: RenderError = io_err.into(); + assert!(matches!(err, RenderError::FileIO(_))); +} diff --git a/morphology-img/tests/pitch.rs b/morphology-img/tests/pitch.rs new file mode 100644 index 0000000..986cebc --- /dev/null +++ b/morphology-img/tests/pitch.rs @@ -0,0 +1,85 @@ +use morphology_img::pitch::{LilyPondPitch, LilyPondNote, Duration}; + +#[test] +fn test_lilypond_pitch_creation() { + let pitch = LilyPondPitch::new("c'"); + assert_eq!(pitch.as_str(), "c'"); +} + +#[test] +fn test_lilypond_pitch_from_string() { + let pitch = LilyPondPitch::new("bes,".to_string()); + assert_eq!(pitch.as_str(), "bes,"); +} + +#[test] +fn test_duration_quarter() { + let duration = Duration::QUARTER; + assert_eq!(duration.as_str(), "4"); +} + +#[test] +fn test_duration_new() { + let duration = Duration::new(8); + assert_eq!(duration.as_str(), "8"); +} + +#[test] +fn test_lilypond_note_creation() { + let pitch = LilyPondPitch::new("c'"); + let duration = Duration::QUARTER; + let note = LilyPondNote::new(pitch, duration); + assert_eq!(note.as_str(), "c'4"); +} + +#[test] +fn test_lilypond_note_with_different_duration() { + let pitch = LilyPondPitch::new("d"); + let duration = Duration::new(8); + let note = LilyPondNote::new(pitch, duration); + assert_eq!(note.as_str(), "d8"); +} + +#[test] +fn test_lilypond_note_with_flat() { + let pitch = LilyPondPitch::new("bes,"); + let duration = Duration::QUARTER; + let note = LilyPondNote::new(pitch, duration); + assert_eq!(note.as_str(), "bes,4"); +} + +#[test] +fn test_pitch_equality() { + let pitch1 = LilyPondPitch::new("c'"); + let pitch2 = LilyPondPitch::new("c'"); + let pitch3 = LilyPondPitch::new("d'"); + assert_eq!(pitch1, pitch2); + assert_ne!(pitch1, pitch3); +} + +#[test] +fn test_note_equality() { + let note1 = LilyPondNote::new( + LilyPondPitch::new("c'"), + Duration::QUARTER + ); + let note2 = LilyPondNote::new( + LilyPondPitch::new("c'"), + Duration::QUARTER + ); + let note3 = LilyPondNote::new( + LilyPondPitch::new("d'"), + Duration::QUARTER + ); + assert_eq!(note1, note2); + assert_ne!(note1, note3); +} + +#[test] +fn test_duration_equality() { + let dur1 = Duration::new(4); + let dur2 = Duration::QUARTER; + let dur3 = Duration::new(8); + assert_eq!(dur1, dur2); + assert_ne!(dur1, dur3); +} |