gd_engrave ========== .. rust:crate:: gd_engrave :synopsis: Native music engraving: a score becomes positioned marks, then self-contained SVG. Native music engraving: a score becomes positioned marks, then self-contained SVG. Fonts are explicit immutable tables; one reusable Engraver belongs to one thread. .. rubric:: Structs .. rust:struct:: pub struct BreakPoint A legal measure boundary; the first and last entries delimit the complete score. .. rubric:: Fields .. rust:field:: pub gap: usize Gap index at this boundary. .. rust:field:: pub measure: u32 Printed number of the following measure. .. rust:field:: pub penalty: i32 Preferred break penalty; a negative value rewards this boundary. .. rust:field:: pub forced: bool No candidate may cross this boundary. .. rust:field:: pub forbidden: bool No candidate may end here, except the final score boundary. .. rust:field:: pub prefix: f64 Clef, signatures and instrument-name width at a line starting here. .. rust:field:: pub indent: f64 Instrument-name indent before the staff lines begin. .. rust:field:: pub suffix: f64 Courtesy signatures before a line ending here. .. rust:struct:: pub struct Line One line in the chosen optimal breaking within the bounded candidate window. .. rubric:: Fields .. rust:field:: pub first: usize Starting breakpoint index. .. rust:field:: pub last: usize Ending breakpoint index. .. rust:field:: pub stretch: f64 Selected proportional stretch. .. rust:field:: pub width: f64 Actual width including prefatory and courtesy matter. .. rust:field:: pub overfull: bool Whether a minimum-clearance line exceeds its available width. .. rust:struct:: pub struct FaceData<'f> One borrowed OpenType face; a collection needs its explicit face index. .. rubric:: Fields .. rust:field:: pub data: &'f [u8] Original font or collection bytes. .. rust:field:: pub index: u32 Face index; zero is the common single-font case. .. rust:field:: pub source: Option<&'f dyn FaceSource> Optional lazy source; when present it supplies bytes instead of data. .. rubric:: Trait Implementations .. rust:impl:: impl fmt::Debug for FaceData<'_> :noindex: :for: FaceData<'_> :trait: fmt::Debug .. rust:method:: fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result :noindex: .. rust:struct:: pub struct FontOptions<'f> Borrowed text faces, in fallback order; fonts outlive the returned tables. .. rubric:: Fields .. rust:field:: pub regular: Vec> Regular text faces. .. rust:field:: pub bold: Vec> Bold text faces, falling back to regular when empty. .. rust:field:: pub italic: Vec> Italic text faces, falling back to regular when empty. .. rust:field:: pub bold_italic: Vec> Bold italic text faces, falling back to bold, italic, then regular when empty. .. rust:struct:: pub struct Fonts<'f> Explicit immutable font tables, safe to share between engraving threads. .. rubric:: Implementations .. rust:impl:: impl Fonts<'_> :noindex: :for: Fonts<'_> .. rust:method:: pub fn glyph(&self, name: &str) -> Result Finds a named SMuFL glyph. Fails if the vendored font lacks its outline or metadata. .. rust:method:: pub fn glyph_data(&self, glyph: Glyph) -> Option<&GlyphData> Borrows one loaded glyph's path, bounds and anchors, or None for a foreign id. .. rubric:: Trait Implementations .. rust:impl:: impl fmt::Debug for Fonts<'_> :noindex: :for: Fonts<'_> :trait: fmt::Debug .. rust:method:: fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result :noindex: .. rust:struct:: pub struct Glyph(pub u16) A Bravura glyph identifier, independent of its name in SMuFL metadata. .. rust:struct:: pub struct GlyphData One immutable music glyph, in staff spaces with upward font coordinates. .. rubric:: Fields .. rust:field:: pub name: String Stable SMuFL glyph name. .. rust:field:: pub outline: String SVG path in staff-space units, with upward y. .. rust:field:: pub bounds: Rect Ink bounds in the display list's downward-y coordinates. .. rust:field:: pub anchors: BTreeMap SMuFL anchors and cut-outs in upward-y font coordinates. .. rust:field:: pub advance: f64 Horizontal advance in staff spaces. .. rust:struct:: pub struct Bezier A cubic Bezier curve with its two control points. .. rubric:: Fields .. rust:field:: pub from: Point Start. .. rust:field:: pub first: Point First control point. .. rust:field:: pub second: Point Second control point. .. rust:field:: pub to: Point End. .. rubric:: Implementations .. rust:impl:: impl Bezier :noindex: :for: Bezier .. rust:method:: pub fn point(self, t: f64) -> Point Evaluates the polynomial at t; callers use t in the closed interval 0..=1. .. rust:struct:: pub struct Point A point in staff spaces during layout and in points in the public display list. .. rubric:: Fields .. rust:field:: pub x: f64 Horizontal coordinate, increasing to the right. .. rust:field:: pub y: f64 Vertical coordinate, increasing downwards. .. rust:struct:: pub struct Rect An axis-aligned rectangle in the same units as its associated marks. .. rubric:: Fields .. rust:field:: pub left: f64 Minimum horizontal coordinate. .. rust:field:: pub top: f64 Minimum vertical coordinate. .. rust:field:: pub right: f64 Maximum horizontal coordinate. .. rust:field:: pub bottom: f64 Maximum vertical coordinate. .. rubric:: Implementations .. rust:impl:: impl Rect :noindex: :for: Rect .. rust:method:: pub fn translated(self, x: f64, y: f64) -> Self Returns a translated rectangle, borrowing no state and performing no allocation. .. rust:method:: pub fn union(self, other: Self) -> Self Returns the smallest rectangle enclosing both inputs. .. rust:method:: pub fn overlaps(self, other: Self, tolerance: f64) -> bool Returns whether the interiors overlap beyond a nonnegative tolerance. .. rust:struct:: pub struct Engraver Reusable per-thread layout buffers. No process-global state or hidden font policy. .. rubric:: Implementations .. rust:impl:: impl Engraver :noindex: :for: Engraver .. rust:method:: pub fn engrave_score(&mut self, score: &Score, fonts: &Fonts<'_>, options: &mut Options<'_>) -> Result Borrows a score and immutable fonts and produces owned systems of geometry. Fails for invalid notation, dimensions, fonts, work bounds or memory refusal. .. rust:struct:: pub struct Limits Explicit bounds on layout work, output size and system count. .. rubric:: Fields .. rust:field:: pub events: usize Maximum input event count. .. rust:field:: pub measures_per_line: usize Maximum line-breaking candidate window in measures. .. rust:field:: pub systems: usize Maximum systems in one drawing. .. rust:field:: pub height: f64 Maximum height in points. .. rust:field:: pub work: usize Charged candidate gaps, comparisons and curve samples. .. rust:field:: pub marks: usize Maximum positioned marks across every system. .. rust:field:: pub svg_bytes: usize Maximum serialized SVG bytes, including outlines. .. rubric:: Trait Implementations .. rust:impl:: impl Default for Limits :noindex: :for: Limits :trait: Default .. rust:method:: fn default() -> Self :noindex: .. rust:struct:: pub struct Options<'a> Plain layout options; dimensions are points. The default staff is 18 points high. .. rubric:: Fields .. rust:field:: pub line_width: f64 Available system width. .. rust:field:: pub staff_size: f64 Height of a five-line staff. .. rust:field:: pub justify: Justify Last-line and single-line spacing policy. .. rust:field:: pub optical_adjustment: f64 Optical gap adjustment between opposite stem directions, in staff spaces. .. rust:field:: pub limits: Limits Finite work and size bounds. .. rust:field:: pub reserve: Option<&'a mut Reserve<'a>> Optional caller-owned buffer-growth ledger. .. rust:field:: pub scratch: crate::Scratch<'a> Scoped reservations for short-lived spacing and skyline buffers. .. rubric:: Trait Implementations .. rust:impl:: impl fmt::Debug for Options<'_> :noindex: :for: Options<'_> :trait: fmt::Debug .. rust:method:: fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result :noindex: .. rust:impl:: impl Default for Options<'_> :noindex: :for: Options<'_> :trait: Default .. rust:method:: fn default() -> Self :noindex: .. rust:struct:: pub struct Engraving A complete immutable drawing, independent of input syntax and borrowed font bytes. .. rubric:: Fields .. rust:field:: pub width: f64 Natural output width in points. .. rust:field:: pub height: f64 Complete output height in points. .. rust:field:: pub systems: Vec Systems in reading order. .. rust:field:: pub glyphs: BTreeMap Only music glyphs used by this drawing, with outlines for standalone export. .. rust:field:: pub summary: Summary Accessible musical facts. .. rust:field:: pub warnings: Vec Recoverable layout and missing-text-glyph diagnostics. .. rust:struct:: pub struct PositionedMark Source-independent grouping and ink bounds for one positioned instruction. .. rubric:: Fields .. rust:field:: pub mark: Mark Instruction in points relative to its system's top. .. rust:field:: pub staff: StaffId Owning staff. .. rust:field:: pub measure: u32 Printed measure number; zero for shared system furniture. .. rust:field:: pub bounds: Rect Conservative ink bounds used by audits and vertical placement. .. rust:struct:: pub struct Summary Facts used by an adapter to word an accessible description in the page's language. .. rubric:: Fields .. rust:field:: pub staves: u32 Number of staves. .. rust:field:: pub instruments: Vec Named instruments, in staff order. .. rust:field:: pub key: Option Initial key when supplied. .. rust:field:: pub meter: Option Initial meter when supplied. .. rust:field:: pub measures: (u32, u32) Inclusive first and last printed measure numbers. .. rust:field:: pub lyrics: bool Whether any verse has syllables. .. rust:struct:: pub struct System One system's positioned geometry and vertical extent. .. rubric:: Fields .. rust:field:: pub width: f64 Actual system ink width in points; zero uses the complete engraving width. .. rust:field:: pub top: f64 Top in points from the complete drawing's origin. .. rust:field:: pub height: f64 Height in points, including lyrics and outside marks. .. rust:field:: pub first_measure: u32 Printed first measure number. .. rust:field:: pub last_measure: u32 Printed last measure number. .. rust:field:: pub marks: Vec Geometry with staff and measure identities for SVG grouping. .. rust:struct:: pub struct Scratch<'a> Borrowed memory callbacks for transient layout buffers; empty by default. They are ``Fn``, not ``FnMut`` like :rust:type:`crate::Reserve `, because several reservations are live at once and each releases its own bytes when dropped. .. rubric:: Fields .. rust:field:: pub reserve: Option<&'a dyn Fn(usize) -> bool> Reserves bytes before allocation; false stops the layout with a limit error. .. rust:field:: pub release: Option<&'a dyn Fn(usize)> Returns successfully reserved bytes when the temporary buffer is freed. Without this callback the caller conservatively retains the charge. .. rubric:: Trait Implementations .. rust:impl:: impl fmt::Debug for Scratch<'_> :noindex: :for: Scratch<'_> :trait: fmt::Debug .. rust:method:: fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result :noindex: .. rust:struct:: pub struct Rod A clearance constraint spanning one or more adjacent gaps. .. rubric:: Fields .. rust:field:: pub first: usize First gap, inclusive. .. rust:field:: pub last: usize Last gap, exclusive. .. rust:field:: pub minimum: f64 Required sum of those gaps. .. rust:struct:: pub struct Solution The unique spring-energy minimizer after conservative spanning-rod conversion. .. rubric:: Fields .. rust:field:: pub gaps: Vec Nonnegative gap lengths satisfying every original rod. .. rust:field:: pub stretch: f64 Proportional spring stretch; zero on an overfull line. .. rust:field:: pub overfull: bool Whether the minimum width exceeds the requested width. .. rust:field:: pub minimum: f64 Sum of converted minimum clearances. .. rust:field:: pub natural: f64 Sum at stretch one, including bound rods. .. rust:struct:: pub struct Spring One gap's preferred length and non-negotiable collision clearance. .. rubric:: Fields .. rust:field:: pub ideal: f64 Positive preferred length in staff spaces. .. rust:field:: pub minimum: f64 Nonnegative minimum length in staff spaces. .. rust:struct:: pub struct SvgOptions<'a> Standalone SVG export policy. The default selects every system and black ink. .. rubric:: Fields .. rust:field:: pub systems: Option> Selected system indices, or all systems. .. rust:field:: pub title: &'a str Accessible title, escaped as XML text. .. rust:field:: pub description: &'a str Accessible musical description, escaped as XML text. .. rust:field:: pub adaptive: bool Add screen-only dark-theme ink rules; PDF and PNG use false. .. rust:field:: pub max_bytes: usize Maximum complete SVG bytes, excluding an existing caller buffer prefix. .. rubric:: Trait Implementations .. rust:impl:: impl Default for SvgOptions<'_> :noindex: :for: SvgOptions<'_> :trait: Default .. rust:method:: fn default() -> Self :noindex: .. rust:struct:: pub struct TextGlyph One outlined text glyph in points, relative to its run's baseline. .. rubric:: Fields .. rust:field:: pub outline: String SVG path with upward font y, already scaled to points. .. rust:field:: pub x: f64 Horizontal placement in points. .. rust:field:: pub y: f64 Downward vertical placement in points. .. rust:struct:: pub struct TextRun Shaped text with real ink geometry; export never depends on browser fonts. .. rubric:: Fields .. rust:field:: pub glyphs: Vec Glyph outlines in visual order. .. rust:field:: pub advance: f64 Shaped horizontal advance in points, including spaces. .. rust:field:: pub bounds: Rect Composite ink bounds relative to the run's baseline. .. rust:field:: pub ascent: f64 Face ascent in points. .. rust:field:: pub descent: f64 Positive face descent in points. .. rust:field:: pub word_space: f64 Largest word-space advance among the selected faces, in points. .. rust:field:: pub sources: Vec Opaque identities of the text font sources actually selected during shaping. .. rubric:: Enums .. rust:enum:: pub enum Justify Last-line and single-line justification policy. .. rubric:: Variants .. rust:variant:: Auto Leave a short final line at its natural width. .. rust:variant:: All Stretch every line to the available measure. .. rust:variant:: None Compress when needed but never stretch beyond natural width. .. rust:enum:: pub enum Mark One self-contained geometry instruction in points, using downward y coordinates. .. rubric:: Variants .. rust:variant:: Glyph { glyph: Glyph, x: f64, y: f64, size: f64, role: Role } A music-font glyph, scaled from staff spaces to points. .. rust:field:: glyph: Glyph Glyph definition in Engraving::glyphs. .. rust:field:: x: f64 Horizontal origin. .. rust:field:: y: f64 Vertical origin. .. rust:field:: size: f64 Points per font staff space. .. rust:field:: role: Role Semantic role. .. rust:variant:: Line { from: Point, to: Point, thickness: f64, role: Role } A straight, solid segment. .. rust:field:: from: Point First endpoint. .. rust:field:: to: Point Last endpoint. .. rust:field:: thickness: f64 Stroke width. .. rust:field:: role: Role Semantic role. .. rust:variant:: Polygon { points: [Point; 4], role: Role } A filled quadrilateral, usually a beam. .. rust:field:: points: [Point; 4] Vertices in perimeter order. .. rust:field:: role: Role Semantic role. .. rust:variant:: Curve { outer: Bezier, inner: Bezier, role: Role } A filled bow bounded by two cubic curves. .. rust:field:: outer: Bezier Outer boundary, from left to right. .. rust:field:: inner: Bezier Inner boundary, from left to right. .. rust:field:: role: Role Semantic role. .. rust:variant:: Text { run: TextRun, x: f64, y: f64, role: Role } Outlined text with its shaped advance and ink bounds. .. rust:field:: run: TextRun Positioned text outlines, relative to the baseline origin. .. rust:field:: x: f64 Horizontal baseline origin. .. rust:field:: y: f64 Vertical baseline origin. .. rust:field:: role: Role Semantic role. .. rubric:: Implementations .. rust:impl:: impl Mark :noindex: :for: Mark .. rust:method:: pub fn role(&self) -> Role Returns the role shared by every geometry variant. .. rust:enum:: pub enum Role A mark's semantic role, used by SVG classes and collision audits. .. rubric:: Variants .. rust:variant:: Staff Staff line. .. rust:variant:: Clef Clef. .. rust:variant:: Signature Key or time signature. .. rust:variant:: Notehead Pitched notehead. .. rust:variant:: Fret Fret number on a tablature string. .. rust:variant:: Ledger Ledger line through an outside note. .. rust:variant:: Stem Stem. .. rust:variant:: Flag Flag. .. rust:variant:: Beam Beam. .. rust:variant:: Accidental Accidental or cautionary parentheses. .. rust:variant:: Dot Augmentation dot. .. rust:variant:: Rest Rest or multi-measure rest bar. .. rust:variant:: Bar Bar line or repeat dots. .. rust:variant:: Tie Tie. .. rust:variant:: Slur Legato or phrasing slur. .. rust:variant:: Tuplet Tuplet number or bracket. .. rust:variant:: Articulation Articulation or fingering. .. rust:variant:: Fingering Fingering digit. .. rust:variant:: Tempo Tempo instruction. .. rust:variant:: Rehearsal Rehearsal label. .. rust:variant:: Dynamic Dynamic or hairpin. .. rust:variant:: Lyric Lyric syllable, hyphen or extender. .. rust:variant:: Instrument Instrument name or group delimiter. .. rust:variant:: Script Text script, tempo or rehearsal mark. .. rust:variant:: ChordName Chord symbol. .. rust:variant:: Volta Repeat ending bracket and label. .. rust:variant:: MeasureNumber Measure number. .. rubric:: Implementations .. rust:impl:: impl Role :noindex: :for: Role .. rust:method:: pub fn name(self) -> &'static str Returns a stable ASCII class name without allocating. .. rubric:: Traits .. rust:trait:: pub trait FaceSource: Sync A build-owned immutable font source, loaded only when its coverage is needed. .. rust:method:: fn data(&self) -> Result<&[u8], Error> Borrows original bytes for the lifetime of this source, or reports a load failure. .. rust:method:: fn covers(&self, _character: char) -> bool :provided: Tests discovered coverage before loading bytes. True may be provisional. .. rust:method:: fn id(&self) -> Option<&str> :provided: Borrows an optional opaque dependency identity for the adapter to observe. .. rubric:: Functions .. rust:function:: pub fn break_lines(springs: &[Spring], rods: &[Rod], breaks: &[BreakPoint], width: f64, justify: Justify, limits: &Limits) -> Result, Error> Borrows a spacing problem and returns its optimal bounded measure breaking. Fails for invalid boundaries, exhausted work or system limits, or contradictory breaks. .. rust:function:: pub fn load_fonts<'f>(options: &FontOptions<'f>) -> Result, Error> Loads unmodified embedded music data and borrows the supplied text faces. No global state or filesystem is consulted. Fails for malformed fonts or metadata. .. rust:function:: pub fn engrave_svg(score: &Score, fonts: &Fonts<'_>, options: &mut Options<'_>) -> Result One-call SVG engraving with fresh workspace buffers and default non-adaptive export. Borrows score, fonts and options; fails like Engraver::engrave_score or write_svg. .. rust:function:: pub fn solve_spacing(springs: &[Spring], rods: &[Rod], width: f64) -> Result Borrows gaps and rods and solves for a positive finite width. Fails for invalid dimensions, rod indices, memory refusal or the default work limit. .. rust:function:: pub fn write_svg(out: &mut String, engraving: &Engraving, options: &SvgOptions<'_>, reserve: Option<&mut Reserve<'_>>) -> Result<(), Error> Appends self-contained SVG to a caller's buffer, borrowing all inputs for this call. Fails on invalid systems or dimensions, allocation refusal or size limits. A failure restores the caller's original buffer length. .. rubric:: Type Aliases .. rust:type:: pub type Reserve<'a> = dyn FnMut(usize) -> bool + 'a A caller-owned memory ledger, consulted before each buffer grows.