v5.0.0
    Preparing search index...

    Class Formatter

    Format implements the formatting and layout algorithms that are used to position notes in a voice. The algorithm can align multiple voices both within a stave, and across multiple staves.

    To do this, the formatter breaks up voices into a grid of rational-valued ticks, to which each note is assigned. Then, minimum widths are assigned to each tick based on the widths of the notes and modifiers in that tick. This establishes the smallest amount of space required for each tick.

    Finally, the formatter distributes the left over space proportionally to all the ticks, setting the x values of the notes in each tick.

    See tests/formatter_tests.ts for usage examples. The helper functions included here (FormatAndDraw, FormatAndDrawTab) also serve as useful usage examples.

    Index

    Constructors

    Properties

    contextGaps: { gaps: { x1: number; x2: number }[]; total: number }
    durationStats: Record<string, { count: number; mean: number; total: number }>
    formatterOptions: Required<FormatterOptions>
    hasMinTotalWidth: boolean
    justifyWidth: number
    lossHistory: number[]
    minTotalWidth: number
    modifierContexts: AlignmentModifierContexts[]
    totalCost: number
    totalShift: number
    voices: Voice[]
    DEBUG: boolean = false

    Methods

    • Find all the rests in each of the voices and align them vertically to neighboring notes.

      Parameters

      • voices: Voice[]
      • alignAllNotes: boolean

        If false, only align rests within beamed groups of notes. If true, align all rests.

      Returns void

    • Align rests in voices, justify the contexts, and position the notes so voices are aligned and ready to render onto the stave. This method mutates the x positions of all tickables in voices.

      Voices are full justified to fit in justifyWidth pixels.

      Set options.context to the rendering context. Set options.alignRests to true to enable rest vertical alignment.

      Parameters

      Returns this

    • Get minimum width required to render all voices. Either format or preCalculateMinTotalWidth must be called before this method.

      Returns number

    • Take all voices and create ModifierContexts out of them. This tells the formatters that the voices belong on a single stave.

      Parameters

      Returns this

    • This is the top-level call for all formatting logic completed after x and y values have been computed for the notes in the voices.

      Returns this

    • Estimate the width required to render 'voices'. This is done by:

      1. Sum the widths of all the tick contexts
      2. Estimate the padding. The latter is done by calculating the padding 3 different ways, and taking the greatest value:
      3. the padding required for unaligned notes in different voices
      4. the padding based on the stddev of the tickable widths
      5. the padding based on the stddev of the tickable durations.

      The last 2 quantities estimate a 'width entropy', where notes might need more room than the proportional formatting gives them. A measure of all same duration and width will need no extra padding, and all these quantities will be zero in that case.

      Note: joinVoices has to be called before calling preCalculateMinTotalWidth.

      Parameters

      • voices: Voice[]

        the voices that contain the notes

      Returns number

      the estimated width in pixels

    • This is the core formatter logic. Format voices and justify them to justifyWidth pixels. renderingContext is required to justify elements that can't retrieve widths without a canvas. This method sets the x positions of all the tickables/notes in the formatter.

      Parameters

      Returns number

    • Run a single iteration of rejustification. At a high level, this method calculates the overall "loss" (or cost) of this layout, and repositions tickcontexts in an attempt to reduce the cost. You can call this method multiple times until it finds and oscillates around a global minimum.

      Parameters

      • Optionaloptions: { alpha?: number }

        parameters for tuning, currently just "alpha".

      Returns number

    • Automatically set the vertical position of rests based on previous/next note positions.

      Parameters

      • tickables: Tickable[]

        an array of Tickables.

      • alignAllNotes: boolean

        If false, only align rests that are within a group of beamed notes.

      • OptionalalignTuplets: boolean

        If false, ignores tuplets.

      Returns void

    • Helper function to format and draw a single voice. Returns a bounding box for the notation.

      Parameters

      • ctx: RenderContext

        the rendering context

      • stave: Stave

        the stave to which to draw (Stave or TabStave)

      • notes: StemmableNote[]

        array of Note instances (Note, TextNote, TabNote, etc.)

      • Optionalparams: boolean | FormatParams

        one of below:

        • Setting autoBeam only (context, stave, notes, true) or (ctx, stave, notes, {autoBeam: true})
        • Setting alignRests a struct is needed (context, stave, notes, {alignRests: true})
        • Setting both a struct is needed (context, stave, notes, { autoBeam: true, alignRests: true})
        • autoBeam automatically generates beams for the notes.
        • alignRests aligns rests with nearby notes.

      Returns undefined | BoundingBox

    • Helper function to format and draw aligned tab and stave notes in two separate staves.

      Parameters

      • ctx: RenderContext

        the rendering context

      • tabstave: TabStave

        a TabStave instance on which to render TabNotes.

      • stave: Stave

        a Stave instance on which to render Notes.

      • tabnotes: TabNote[]

        array of Note instances for the tab stave (TabNote, BarNote, etc.)

      • notes: Tickable[]

        array of Note instances for the stave (Note, BarNote, etc.)

      • autoBeam: boolean

        automatically generate beams.

      • params: FormatParams

        a configuration object:

        • autoBeam automatically generates beams for the notes.
        • alignRests aligns rests with nearby notes.

      Returns void

    • Helper function to plot formatter debug info.

      Parameters

      • ctx: RenderContext
      • formatter: Formatter
      • xPos: number
      • y1: number
      • y2: number
      • Optionaloptions: { stavePadding: number }

      Returns void

    • Helper function to layout "notes" one after the other without regard for proportions. Useful for tests and debugging.

      Parameters

      • notes: Tickable[]
      • x: number = 0
      • __namedParameters: { paddingBetween?: number } = {}

      Returns void