Skip to content

Commit

Permalink
Add new options: --a4, --letter
Browse files Browse the repository at this point in the history
  • Loading branch information
Josef-Friedrich committed Jan 27, 2024
1 parent 05e22fe commit 2812e1d
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 34 deletions.
6 changes: 4 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ CLI Usage
[--musical-symbol-font <font-face>]
[--musical-text-font <font-face>]
[--staff-space <dimension>]
[--page-size <width> <height> <width> <height>]
[--page-size <width> <height>] [--a4] [--letter]
[--margin <dimension>] [--header | --no-header]
[--footer | --no-footer]
[<path> ...]
Expand Down Expand Up @@ -682,8 +682,10 @@ CLI Usage
--staff-space <dimension>
Set the staff space or spatium. This is the vertical
distance between two lines of a music staff.
--page-size <width> <height> <width> <height>
--page-size <width> <height>
Set the page size.
--a4, --din-a4 Set the paper size to DIN A4 (210 by 297 mm).
--letter Set the paper size to Letter (8.5 by 11 in).
--margin <dimension> Set the top, right, bottom and left margins to the same
value.
--header, --no-header
Expand Down
5 changes: 4 additions & 1 deletion autocomplete.bash
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@



_shtab_musescore_manager_option_strings=('-h' '--help' '--print-completion' '-V' '--version' '-b' '--backup' '--bail' '-k' '--colorize' '-C' '--config-file' '-d' '--dry-run' '-m' '--mscore' '--diff' '-e' '--executable' '-v' '--verbose' '-E' '--export' '-c' '--clean-meta' '-D' '--delete-duplicates' '-i' '--distribute-fields' '-j' '--json' '-l' '--log' '-y' '--synchronize' '-S' '--set-field' '--metatag' '--metatag-meta' '--vbox' '--vbox-meta' '-x' '--extract' '--extract-lyrics' '-r' '--remap' '--remap-lyrics' '-F' '--fix' '--fix-lyrics' '--rename' '-f' '--format' '-A' '--alphanum' '-a' '--ascii' '-n' '--no-whitespace' '-K' '--skip-if-empty' '-t' '--target' '-L' '--list-files' '-g' '--glob' '--mscz' '--mscx' '-s' '--style' '--clean' '-Y' '--style-file' '--s3' '--styles-v3' '--s4' '--styles-v4' '--list-fonts' '--text-font' '--title-font' '--musical-symbol-font' '--musical-text-font' '--staff-space' '--page-size' '--margin' '--header' '--no-header' '--footer' '--no-footer')
_shtab_musescore_manager_option_strings=('-h' '--help' '--print-completion' '-V' '--version' '-b' '--backup' '--bail' '-k' '--colorize' '-C' '--config-file' '-d' '--dry-run' '-m' '--mscore' '--diff' '-e' '--executable' '-v' '--verbose' '-E' '--export' '-c' '--clean-meta' '-D' '--delete-duplicates' '-i' '--distribute-fields' '-j' '--json' '-l' '--log' '-y' '--synchronize' '-S' '--set-field' '--metatag' '--metatag-meta' '--vbox' '--vbox-meta' '-x' '--extract' '--extract-lyrics' '-r' '--remap' '--remap-lyrics' '-F' '--fix' '--fix-lyrics' '--rename' '-f' '--format' '-A' '--alphanum' '-a' '--ascii' '-n' '--no-whitespace' '-K' '--skip-if-empty' '-t' '--target' '-L' '--list-files' '-g' '--glob' '--mscz' '--mscx' '-s' '--style' '--clean' '-Y' '--style-file' '--s3' '--styles-v3' '--s4' '--styles-v4' '--list-fonts' '--text-font' '--title-font' '--musical-symbol-font' '--musical-text-font' '--staff-space' '--page-size' '--a4' '--din-a4' '--letter' '--margin' '--header' '--no-header' '--footer' '--no-footer')

_shtab_musescore_manager_pos_0_COMPGEN=_shtab_compgen_files
_shtab_musescore_manager__C_COMPGEN=_shtab_compgen_files
Expand Down Expand Up @@ -72,6 +72,9 @@ _shtab_musescore_manager___s4_nargs=0
_shtab_musescore_manager___styles_v4_nargs=0
_shtab_musescore_manager___list_fonts_nargs=0
_shtab_musescore_manager___page_size_nargs=2
_shtab_musescore_manager___a4_nargs=0
_shtab_musescore_manager___din_a4_nargs=0
_shtab_musescore_manager___letter_nargs=0
_shtab_musescore_manager___header_nargs=0
_shtab_musescore_manager___no_header_nargs=0
_shtab_musescore_manager___footer_nargs=0
Expand Down
2 changes: 1 addition & 1 deletion autocomplete.tcsh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


complete musescore-manager \
'c/--/(alphanum ascii backup bail clean clean-meta colorize config-file delete-duplicates diff distribute-fields dry-run executable export extract extract-lyrics fix fix-lyrics footer format glob header help json list-files list-fonts log margin metatag metatag-meta mscore mscx mscz musical-symbol-font musical-text-font no-footer no-header no-whitespace page-size print-completion remap remap-lyrics rename s3 s4 set-field skip-if-empty staff-space style style-file styles-v3 styles-v4 synchronize target text-font title-font vbox vbox-meta verbose version)/' \
'c/--/(a4 alphanum ascii backup bail clean clean-meta colorize config-file delete-duplicates diff din-a4 distribute-fields dry-run executable export extract extract-lyrics fix fix-lyrics footer format glob header help json letter list-files list-fonts log margin metatag metatag-meta mscore mscx mscz musical-symbol-font musical-text-font no-footer no-header no-whitespace page-size print-completion remap remap-lyrics rename s3 s4 set-field skip-if-empty staff-space style style-file styles-v3 styles-v4 synchronize target text-font title-font vbox vbox-meta verbose version)/' \
'c/-/(- A C D E F K L S V Y a b c d e f g h i j k l m n r s t v x y)/' \
'n/--print-completion/(bash zsh tcsh)/' \
'n/-C/f/' \
Expand Down
2 changes: 2 additions & 0 deletions autocomplete.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ _shtab_musescore_manager_options=(
"--musical-text-font[Set \“musicalTextFont\”.]:style_musical_text_font:"
"--staff-space[Set the staff space or spatium. This is the vertical distance between two lines of a music staff.]:style_staff_space:"
"--page-size[Set the page size.]:style_page_size:"
{--a4,--din-a4}"[Set the paper size to DIN A4 (210 by 297 mm).]"
"--letter[Set the paper size to Letter (8.5 by 11 in).]"
"--margin[Set the top, right, bottom and left margins to the same value.]:style_margin:"
{--header,--no-header}"[Show or hide the header]:style_show_header:"
{--footer,--no-footer}"[Show or hide the footer.]:style_show_footer:"
Expand Down
6 changes: 4 additions & 2 deletions docs/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Comande line interface
[--musical-symbol-font <font-face>]
[--musical-text-font <font-face>]
[--staff-space <dimension>]
[--page-size <width> <height> <width> <height>]
[--page-size <width> <height>] [--a4] [--letter]
[--margin <dimension>] [--header | --no-header]
[--footer | --no-footer]
[<path> ...]
Expand Down Expand Up @@ -380,8 +380,10 @@ Comande line interface
--staff-space <dimension>
Set the staff space or spatium. This is the vertical
distance between two lines of a music staff.
--page-size <width> <height> <width> <height>
--page-size <width> <height>
Set the page size.
--a4, --din-a4 Set the paper size to DIN A4 (210 by 297 mm).
--letter Set the paper size to Letter (8.5 by 11 in).
--margin <dimension> Set the top, right, bottom and left margins to the same
value.
--header, --no-header
Expand Down
40 changes: 26 additions & 14 deletions mscxyz/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,7 @@
from mscxyz.rename import rename
from mscxyz.score import Score
from mscxyz.settings import parse_args
from mscxyz.utils import Dimension


def _mm(value: str) -> float:
return Dimension(value).to("mm")


def _inch(value: str) -> float:
return Dimension(value).to("in")
from mscxyz.style import inch, mm


def _embed_fields(
Expand Down Expand Up @@ -573,7 +565,7 @@ def _split_lines(self, text: typing.Text, width: int) -> typing.List[str]:
group_style.add_argument(
"--staff-space",
dest="style_staff_space",
type=_mm,
type=mm,
metavar="<dimension>",
help="Set the staff space or spatium. This is the vertical distance between "
"two lines of a music staff.",
Expand All @@ -583,10 +575,25 @@ def _split_lines(self, text: typing.Text, width: int) -> typing.List[str]:
"--page-size",
dest="style_page_size",
nargs=2,
metavar="<width> <height>",
metavar=("<width>", "<height>"),
help="Set the page size.",
)

group_style.add_argument(
"--a4",
"--din-a4",
dest="style_page_size_a4",
action="store_true",
help="Set the paper size to DIN A4 (210 by 297 mm).",
)

group_style.add_argument(
"--letter",
dest="style_page_size_letter",
action="store_true",
help="Set the paper size to Letter (8.5 by 11 in).",
)

group_style.add_argument(
"--margin",
dest="style_margin",
Expand Down Expand Up @@ -711,11 +718,16 @@ def list_styles(version: int) -> None:
score.style.staff_space = args.style_staff_space

if args.style_page_size is not None:
score.style.page_width = _inch(args.style_page_size[0])
score.style.page_height = _inch(args.style_page_size[1])
score.style.set_page_size(*args.style_page_size)

if args.style_page_size_a4:
score.style.set_page_size_a4()

if args.style_page_size_letter:
score.style.set_page_size_letter()

if args.style_margin is not None:
score.style.margin = _inch(args.style_margin)
score.style.margin = inch(args.style_margin)

if args.style_show_header is not None:
score.style.show_header = args.style_show_header
Expand Down
28 changes: 14 additions & 14 deletions mscxyz/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ class Vbox:
.. code-block:: xml
<Staff id="1">
<Vbox>
<VBox>
<height>10</height>
<Text>
<style>Title</style>
Expand All @@ -410,7 +410,7 @@ class Vbox:
<style>Composer</style>
<text>Composer</text>
</Text>
</Vbox>
</VBox>
</Staff>
Expand All @@ -419,7 +419,7 @@ class Vbox:
.. code-block:: xml
<Staff id="1">
<Vbox>
<VBox>
<height>10</height>
<boxAutoSize>0</boxAutoSize>
<eid>4294967418</eid>
Expand All @@ -433,7 +433,7 @@ class Vbox:
<style>composer</style>
<text>Composer</text>
</Text>
</Vbox>
</VBox>
</Staff>
"""

Expand All @@ -455,9 +455,9 @@ def __init__(self, score: "Score") -> None:
self.xml_root = score.xml_root
xpath = '/museScore/Score/Staff[@id="1"]'

vbox = self.score.xml.xpath(xpath + "/Vbox")
vbox = self.score.xml.xpath(xpath + "/VBox")
if vbox is None:
vbox, _ = self.score.xml.create_sub_element("Vbox", "height", "10")
vbox, _ = self.score.xml.create_sub_element("VBox", "height", "10")

self.score.xml.xpath_safe(xpath).insert(0, vbox)
self.vbox = vbox
Expand Down Expand Up @@ -566,7 +566,7 @@ def title(self) -> str | None:
.. code-block:: xml
<Staff id="1">
<Vbox>
<VBox>
<height>10</height>
<boxAutoSize>0</boxAutoSize>
<eid>4294967418</eid>
Expand All @@ -575,7 +575,7 @@ def title(self) -> str | None:
<style>title</style>
<text>Mondscheinsonate</text>
</Text>
</Vbox>
</VBox>
</Staff>
"""
return self.__get_text("title")
Expand All @@ -592,7 +592,7 @@ def subtitle(self) -> str | None:
.. code-block:: xml
<Staff id="1">
<Vbox>
<VBox>
<height>10</height>
<boxAutoSize>0</boxAutoSize>
<eid>4294967418</eid>
Expand All @@ -601,7 +601,7 @@ def subtitle(self) -> str | None:
<style>subtitle</style>
<text>1. Satz</text>
</Text>
</Vbox>
</VBox>
</Staff>
"""
return self.__get_text("subtitle")
Expand All @@ -618,7 +618,7 @@ def composer(self) -> str | None:
.. code-block:: xml
<Staff id="1">
<Vbox>
<VBox>
<height>10</height>
<boxAutoSize>0</boxAutoSize>
<eid>4294967418</eid>
Expand All @@ -627,7 +627,7 @@ def composer(self) -> str | None:
<style>composer</style>
<text>Ludwig van Beethoven</text>
</Text>
</Vbox>
</VBox>
</Staff>
"""
return self.__get_text("composer")
Expand All @@ -644,7 +644,7 @@ def lyricist(self) -> str | None:
.. code-block:: xml
<Staff id="1">
<Vbox>
<VBox>
<height>10</height>
<boxAutoSize>0</boxAutoSize>
<eid>4294967418</eid>
Expand All @@ -653,7 +653,7 @@ def lyricist(self) -> str | None:
<style>lyricist</style>
<text>Johann Wolfgang von Goethe</text>
</Text>
</Vbox>
</VBox>
</Staff>
"""
return self.__get_text("lyricist")
Expand Down
2 changes: 2 additions & 0 deletions mscxyz/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class DefaultArguments:
style_musical_text_font: Optional[str] = None
style_staff_space: Optional[float] = None
style_page_size: Optional[tuple[str, str]] = None
style_page_size_a4: bool = False
style_page_size_letter: bool = False
style_margin: Optional[str] = None
style_show_header: Optional[bool] = None
style_show_footer: Optional[bool] = None
Expand Down
36 changes: 36 additions & 0 deletions mscxyz/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@
if typing.TYPE_CHECKING:
from mscxyz.score import Score

from mscxyz.utils import Dimension


def mm(value: str | float) -> float:
if not isinstance(value, str):
return value
return Dimension(value).to("mm")


def inch(value: str | float) -> float:
if not isinstance(value, str):
return value
return Dimension(value).to("in")


text_font_faces = (
"lyricsOddFontFace",
Expand Down Expand Up @@ -615,6 +629,28 @@ def page_height(self) -> float:
def page_height(self, value: float) -> None:
self.set("pageHeight", value)

def set_page_size(self, width: float | str, height: float | str) -> None:
"""
Set the page size in ``inch``.
:param width: The page width in ``inch`` as a float or a dimension string in ``mm`` or ``in``.
:param height: The page height in ``inch`` as a float or a dimension string in ``mm`` or ``in``.
"""
self.page_width = inch(width)
self.page_height = inch(height)

def set_page_size_a4(self) -> None:
"""
Set the page size to A4 (210mm x 297mm).
"""
self.set_page_size("210mm", "297mm")

def set_page_size_letter(self) -> None:
"""
Set the page size to Letter (8.5in x 11in).
"""
self.set_page_size("8.5in", "11in")

# page even/odd top/right/bottom/left margin # in CSS order ################

@property
Expand Down
11 changes: 11 additions & 0 deletions tests/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,17 @@ def test_option_page_size(self) -> None:
assert score.style.page_width == 5.9055
assert score.style.page_height == 3.9370

def test_option_page_size_a4(self) -> None:
score: Score = Cli("--a4").score()
# 8.27 in × 11.7 in ?
assert score.style.page_width == 8.2677
assert score.style.page_height == 11.6929

def test_option_page_size_letter(self) -> None:
score: Score = Cli("--letter").score()
assert score.style.page_width == 8.5
assert score.style.page_height == 11.0

def test_option_margin(self) -> None:
score = Cli("--margin", "30mm").score()
s = score.style
Expand Down

0 comments on commit 2812e1d

Please sign in to comment.