7. Creating custom musical symbols with markup

>>> import abjad

7.1. Building basic shapes with markup

>>> mallet_stick = abjad.Markup.draw_line(0, -6)
>>> mallet_head = abjad.Markup.filled_box((-1, 1), (-1, 1))
>>> square_mallet = abjad.Markup.combine([mallet_stick, mallet_head])
>>> print(format(square_mallet))
\markup {
    \combine
        \draw-line
            #'(0 . -6)
        \filled-box
            #'(-1 . 1)
            #'(-1 . 1)
            #0.0
    }
>>> show(square_mallet)
>>> mallet_stick = abjad.Markup.draw_line(0, -6)
>>> mallet_head = abjad.Markup.filled_box((-1, 1), (-1, 1), blot=1)
>>> square_mallet = abjad.Markup.combine([mallet_stick, mallet_head])
>>> print(format(square_mallet))
\markup {
    \combine
        \draw-line
            #'(0 . -6)
        \filled-box
            #'(-1 . 1)
            #'(-1 . 1)
            #1.0
    }
>>> show(square_mallet)
>>> mallet_stick = abjad.Markup.draw_line(0, -6)
>>> mallet_head = abjad.Markup.draw_circle(2, 0.5)
>>> round_mallet = abjad.Markup.combine([mallet_stick, mallet_head])
>>> print(format(round_mallet))
\markup {
    \combine
        \draw-line
            #'(0 . -6)
        \draw-circle
            #2
            #0.5
            ##f
    }
>>> show(round_mallet)
>>> mallet_stick = abjad.Markup.draw_line(0, -5).translate((0, -1))
>>> mallet_head = abjad.Markup.draw_circle(1, 0.25)
>>> round_mallet = abjad.Markup.combine([mallet_stick, mallet_head])
>>> print(format(round_mallet))
\markup {
    \combine
        \translate
            #'(0 . -1)
            \draw-line
                #'(0 . -5)
        \draw-circle
            #1
            #0.25
            ##f
    }
>>> show(round_mallet)
>>> boxed_mallet = round_mallet.pad_around(1).box()
>>> print(format(boxed_mallet))
\markup {
    \box
        \pad-around
            #1
            \combine
                \translate
                    #'(0 . -1)
                    \draw-line
                        #'(0 . -5)
                \draw-circle
                    #1
                    #0.25
                    ##f
    }
>>> show(boxed_mallet)
>>> boxed_mallets = abjad.Markup.concat([round_mallet, square_mallet])
>>> boxed_mallets = boxed_mallets.pad_around(1).box()
>>> print(format(boxed_mallets))
\markup {
    \box
        \pad-around
            #1
            \concat
                {
                    \combine
                        \translate
                            #'(0 . -1)
                            \draw-line
                                #'(0 . -5)
                        \draw-circle
                            #1
                            #0.25
                            ##f
                    \combine
                        \draw-line
                            #'(0 . -6)
                        \filled-box
                            #'(-1 . 1)
                            #'(-1 . 1)
                            #1.0
                }
    }
>>> show(boxed_mallets)
>>> spacing = abjad.Markup.hspace(1)
>>> boxed_mallets = abjad.Markup.concat([round_mallet, spacing, square_mallet])
>>> boxed_mallets = boxed_mallets.pad_around(1).box()
>>> print(format(boxed_mallets))
\markup {
    \box
        \pad-around
            #1
            \concat
                {
                    \combine
                        \translate
                            #'(0 . -1)
                            \draw-line
                                #'(0 . -5)
                        \draw-circle
                            #1
                            #0.25
                            ##f
                    \hspace
                        #1
                    \combine
                        \draw-line
                            #'(0 . -6)
                        \filled-box
                            #'(-1 . 1)
                            #'(-1 . 1)
                            #1.0
                }
    }
>>> show(boxed_mallets)

7.2. Fonts and alignment

>>> flat = abjad.Markup.flat()
>>> print(format(flat))
\markup {
    \flat
    }
>>> show(flat)
>>> flat = abjad.Markup.flat()
>>> a_flat = abjad.Markup.concat(['A', flat])
>>> print(format(a_flat))
\markup {
    \concat
        {
            A
            \flat
        }
    }
>>> show(a_flat)
>>> a = abjad.Markup('A').fontsize(3)
>>> flat = abjad.Markup.flat()
>>> a_flat = abjad.Markup.concat([a, flat])
>>> print(format(a_flat))
\markup {
    \concat
        {
            \fontsize
                #3
                A
            \flat
        }
    }
>>> show(a_flat)
>>> a = abjad.Markup('A').fontsize(3)
>>> flat = abjad.Markup.flat().vcenter()
>>> a_flat = abjad.Markup.concat([a, flat])
>>> print(format(a_flat))
\markup {
    \concat
        {
            \fontsize
                #3
                A
            \vcenter
                \flat
        }
    }
>>> show(a_flat)
>>> a = abjad.Markup('A').fontsize(3).vcenter()
>>> flat = abjad.Markup.flat().vcenter()
>>> a_flat = abjad.Markup.concat([a, flat])
>>> print(format(a_flat))
\markup {
    \concat
        {
            \vcenter
                \fontsize
                    #3
                    A
            \vcenter
                \flat
        }
    }
>>> show(a_flat)
>>> a = abjad.Markup('A').fontsize(3).override(('font-name', 'Arial')).vcenter()
>>> flat = abjad.Markup.flat().vcenter()
>>> a_flat = abjad.Markup.concat([a, flat])
>>> print(format(a_flat))
\markup {
    \concat
        {
            \vcenter
                \override
                    #'(font-name . "Arial")
                    \fontsize
                        #3
                        A
            \vcenter
                \flat
        }
    }
>>> show(a_flat)

7.3. Working directly with Postscript

>>> postscript = abjad.Postscript()
>>> postscript = postscript.newpath()
>>> postscript = postscript.moveto(0, 0)
>>> postscript = postscript.rlineto(0, 6)
>>> postscript = postscript.rlineto(10, -2)
>>> postscript = postscript.rlineto(0, -2)
>>> postscript = postscript.rlineto(-10, -2)
>>> postscript = postscript.closepath()
>>> postscript = postscript.stroke()
>>> outline = abjad.Markup.postscript(postscript)
>>> print(format(outline))
\markup {
    \postscript
        #"
        newpath
        0 0 moveto
        0 6 rlineto
        10 -2 rlineto
        0 -2 rlineto
        -10 -2 rlineto
        closepath
        stroke
        "
    }
>>> show(outline)
>>> show(outline.pad_around(1).box())
>>> outline = outline.with_dimensions((0, 10), (0, 6))
>>> show(outline.pad_around(1).box())
>>> text = abjad.Markup('Vib').italic().bold().fontsize(3)
>>> show(text)
>>> diagram = abjad.Markup.combine([text, outline])
>>> show(diagram)
>>> diagram = abjad.Markup.combine([text, outline.vcenter()])
>>> show(diagram)
>>> diagram = abjad.Markup.combine([text.vcenter(), outline])
>>> show(diagram)
>>> diagram = abjad.Markup.combine([text.vcenter(), outline.vcenter()])
>>> show(diagram)
>>> diagram = abjad.Markup.combine([text.vcenter().translate((1, 0)), outline.vcenter()])
>>> show(diagram)
>>> print(format(diagram))
\markup {
    \combine
        \translate
            #'(1 . 0)
            \vcenter
                \fontsize
                    #3
                    \bold
                        \italic
                            Vib
        \vcenter
            \with-dimensions
                #'(0 . 10)
                #'(0 . 6)
                \postscript
                    #"
                    newpath
                    0 0 moveto
                    0 6 rlineto
                    10 -2 rlineto
                    0 -2 rlineto
                    -10 -2 rlineto
                    closepath
                    stroke
                    "
    }

7.4. Aligning markup on score components

>>> staff = abjad.Staff(r"\time 2/4 c'2 d'2 e'2")
>>> for leaf in staff:
...     abjad.attach(diagram, leaf)
... 
>>> show(staff)
>>> diagram = abjad.Markup(diagram, Up)
>>> staff = abjad.Staff(r"\time 2/4 c'2 d'2 e'2")
>>> for leaf in staff:
...     abjad.attach(diagram, leaf)
... 
>>> show(staff)
>>> abjad.override(staff[1]).text_script.self_alignment_X = Center
>>> abjad.override(staff[2]).text_script.self_alignment_X = Right
>>> show(staff)
>>> for leaf in staff:
...     abjad.override(leaf).text_script.parent_alignment_X = Center
... 
>>> show(staff)