Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting image sizes #218

Merged
merged 14 commits into from
May 10, 2016
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{image: (( content.argument|search_image ))}
{image: "(( content.filename|search_image ))" ((content.size|render_size))}
4 changes: 2 additions & 2 deletions patacrep/data/templates/songs/chordpro/latex/content_image
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(* block image *)
(* set image = content.argument|search_image|path2posix *)
(* set image = content.filename|search_image|path2posix *)
(* if image *)
\image{(( image ))}
\image[(( content.size|render_size ))]{(( image ))}
(*- endif *)
(*- endblock *)
1 change: 1 addition & 0 deletions patacrep/data/templates/styles/patacrep.sty
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{patacrep}[2014/06/17 Patacrep Package, version 1]

\RequirePackage[space]{grffile}
\RequirePackage{graphicx,xcolor} %
\RequirePackage{epstopdf} %
\RequirePackage{fancybox}
Expand Down
23 changes: 23 additions & 0 deletions patacrep/songs/chordpro/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ def _filters(self):
parent = super()._filters()
parent.update({
'lang2babel': self.lang2babel,
'render_size': self._render_size,
})
return parent

Expand All @@ -203,6 +204,13 @@ def lang2babel(self, lang):
self.errors.append(new_error)
return error.babel

@staticmethod
def _render_size(size):
items = []
for name, value, unit in size:
items.append(name + "=" + value + unit)
return ", ".join(items)

class Chordpro2ChordproSong(ChordproSong):
"""Render chordpro song to chordpro code"""

Expand All @@ -219,10 +227,25 @@ class Chordpro2ChordproSong(ChordproSong):
'\\': '\\\\',
}

def _filters(self):
parent = super()._filters()
parent.update({
'render_size': self._render_size,
})
return parent

def search_file(self, filename, extensions=None, *, datadirs=None):
# pylint: disable=unused-variable
return filename

@staticmethod
def _render_size(size):
items = []
for name, value, unit in size:
items.append(name + "=" + value + unit)
return " ".join(items)


SONG_RENDERERS = {
"tsg": {
'csg': Chordpro2LatexSong,
Expand Down
16 changes: 16 additions & 0 deletions patacrep/songs/chordpro/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,22 @@ def pretty_key(self):
def __str__(self):
return None

class Image(Directive):
"""An image

.. attribute:: filename
The filename of the image.
.. attribute:: size
An iterable of tuples ``(type, float, unit)``.
"""

def __init__(self, filename, size=None):
self.filename = filename
if size is None:
size = []
self.size = size
super().__init__("image", None)

class Tab(AST):
"""Tablature"""

Expand Down
74 changes: 73 additions & 1 deletion patacrep/songs/chordpro/syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import logging
import re
import shlex

import ply.yacc as yacc

Expand Down Expand Up @@ -124,10 +125,67 @@ def _parse_define(groups):
fingers=fingers,
)

def _iter_raw_image_size_arguments(self, arguments, *, lineno):
for item in arguments:
prefix, _, suffix = item.partition("=")
if prefix in ['width', 'height']:
match = re.compile(
r"^(?P<value>(\d*\.\d+|\d+))(?P<unit>cm|em|pt)$",
re.VERBOSE,
).match(suffix)
if match is not None:
yield (prefix, match.groupdict()['value'], match.groupdict()['unit'])
continue
elif prefix in ['scale']:
match = re.compile(
r"^(?P<value>(\d*\.\d+|\d+))$",
re.VERBOSE,
).match(suffix)
if match is not None:
yield (prefix, match.groupdict()['value'], "")
continue
else:
self.error(
line=lineno,
message="Image: Unknown argument name '{}'.".format(prefix),
)
continue
self.error(
line=lineno,
message="Image: Unsupported {} value: '{}'.".format(prefix, suffix),
)

def _iter_image_size_arguments(self, argument, *, lineno):
arguments = set()
length_names = frozenset(["width", "height"])
for name, value, unit in self._iter_raw_image_size_arguments(argument, lineno=lineno):
if name in arguments:
self.error(
line=lineno,
message="Image: Ignoring repeated argument: {}.".format(name),
)
continue
if (
name == "scale" and not length_names.isdisjoint(arguments)
) or (
name in length_names and "scale" in arguments
):
self.error(
line=lineno,
message=(
"Image: Ignoring '{}' argument: Cannot mix scale and "
"width or height argument."
).format(name),
)
continue
arguments.add(name)
yield name, value, unit

def p_directive(self, symbols):
"""directive : LBRACE KEYWORD directive_next RBRACE
| LBRACE SPACE KEYWORD directive_next RBRACE
"""
# pylint: disable=too-many-branches
if len(symbols) == 5:
keyword = symbols[2]
argument = symbols[3]
Expand Down Expand Up @@ -171,7 +229,21 @@ def p_directive(self, symbols):
symbols[0] = ast.Error()
return
self._directives.append(define)

elif keyword == "image":
splitted = shlex.split(argument)
if len(splitted) < 1:
self.error(
line=symbols.lexer.lineno,
message="Missing filename for image directive",
)
symbols[0] = ast.Error()
else:
symbols[0] = ast.Image(
splitted[0],
list(
self._iter_image_size_arguments(splitted[1:], lineno=symbols.lexer.lineno)
),
)
else:
directive = ast.Directive(keyword, argument)
if directive.inline:
Expand Down
8 changes: 4 additions & 4 deletions test/test_book/datadir.tex.control
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Chordpro}[


\lilypond{scores/datadir.ly}
\image{img/datadir.png}
\image[]{img/datadir.png}


\endsong
Expand All @@ -138,7 +138,7 @@ Chordpro}[


\lilypond{scores/datadir2.ly}
\image{img/datadir2.png}
\image[]{img/datadir2.png}


\endsong
Expand All @@ -165,7 +165,7 @@ Chordpro}[


\lilypond{@TEST_FOLDER@/datadir_datadir/songs/./relative.ly}
\image{@TEST_FOLDER@/datadir_datadir/songs/./relative.png}
\image[]{@TEST_FOLDER@/datadir_datadir/songs/./relative.png}


\endsong
Expand All @@ -192,7 +192,7 @@ Chordpro}[


\lilypond{@TEST_FOLDER@/datadir_datadir/songs/./subdir/subdir.ly}
\image{@TEST_FOLDER@/datadir_datadir/songs/./subdir/subdir.png}
\image[]{@TEST_FOLDER@/datadir_datadir/songs/./subdir/subdir.png}


\endsong
Expand Down
67 changes: 64 additions & 3 deletions test/test_book/syntax.tex.control
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@





%% Automatically generated document.
%% You may edit this file but all changes will be overwritten.
%% If you want to change this document, have a look at
Expand Down Expand Up @@ -91,6 +89,69 @@ guitar,
\addcontentsline{toc}{section}{\songlistname}

\begin{songs}{titleidx,authidx}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% songs/./images.csg

\selectlanguage{english}

\beginsong{}[
by={
},
]


\image[]{img/image.png}
\image[]{img/image with spaces.png}



\image[scale=2]{img/image.png}
\image[scale=.20]{img/image with spaces.png}
\image[scale=1.2]{img/image.png}



\image[width=2cm]{img/image.png}
\image[height=2cm]{img/image with spaces.png}
\image[width=2cm, height=1cm]{img/image.png}



\image[width=2em]{img/image.png}
\image[height=2em]{img/image with spaces.png}
\image[width=2em, height=1em]{img/image.png}



\image[width=50pt]{img/image.png}
\image[height=50pt]{img/image with spaces.png}
\image[width=50pt, height=100pt]{img/image.png}



\image[width=2.5cm]{img/image.png}
\image[height=2.5cm]{img/image with spaces.png}
\image[width=2.5cm, height=1.5cm]{img/image.png}



\image[width=3cm, height=10pt]{img/image.png}
\image[width=10pt, height=3cm]{img/image with spaces.png}



\image[]{img/image.png}
\image[]{img/image.png}
\image[]{img/image with spaces.png}
\image[]{img/image with spaces.png}
\image[]{img/image.png}
\image[width=2cm]{img/image.png}
\image[width=2cm]{img/image.png}
\image[width=2cm]{img/image.png}


\endsong

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% songs/./musicnote.csg

Expand All @@ -117,4 +178,4 @@ guitar,



\end{document}
\end{document}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/test_book/syntax_datadir/img/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions test/test_book/syntax_datadir/songs/images.csg
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{image: image.png}
{image: "image with spaces.png"}

{image: image.png scale=2 }
{image: "image with spaces.png" scale=.20}
{image: image.png scale=1.2}

{image: image.png width=2cm}
{image: "image with spaces.png" height=2cm}
{image: image.png width=2cm height=1cm}

{image: image.png width=2em}
{image: "image with spaces.png" height=2em}
{image: image.png width=2em height=1em}

{image: image.png width=50pt}
{image: "image with spaces.png" height=50pt}
{image: image.png width=50pt height=100pt}

{image: image.png width=2.5cm}
{image: "image with spaces.png" height=2.5cm}
{image: image.png width=2.5cm height=1.5cm}

{image: image.png width=3cm height=10pt}
{image: "image with spaces.png" width=10pt height=3cm}

{image: image.png width= height=}
{image: image.png error=foo}
{image: "image with spaces.png" not_a_size}
{image: "image with spaces.png" too many arguments}
{image: image.png width=2ex height=3km}
{image: image.png width=2cm width=3cm}
{image: image.png width=2cm scale=3cm}
{image: image.png width=2cm scale=3}
{image: }
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 44 additions & 0 deletions test/test_song/image.csg
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{lang: en}

{image: "image.png" }
{image: "image with spaces.png" }


{image: "image.png" scale=2}
{image: "image with spaces.png" scale=.20}
{image: "image.png" scale=1.2}


{image: "image.png" width=2cm}
{image: "image with spaces.png" height=2cm}
{image: "image.png" width=2cm height=1cm}


{image: "image.png" width=2em}
{image: "image with spaces.png" height=2em}
{image: "image.png" width=2em height=1em}


{image: "image.png" width=50pt}
{image: "image with spaces.png" height=50pt}
{image: "image.png" width=50pt height=100pt}


{image: "image.png" width=2.5cm}
{image: "image with spaces.png" height=2.5cm}
{image: "image.png" width=2.5cm height=1.5cm}


{image: "image.png" width=3cm height=10pt}
{image: "image with spaces.png" width=10pt height=3cm}


{image: "image.png" }
{image: "image.png" }
{image: "image with spaces.png" }
{image: "image with spaces.png" }
{image: "image.png" }
{image: "image.png" width=2cm}
{image: "image.png" width=2cm}
{image: "image.png" width=2cm}

Loading