From 10a22a03177d4dc5dc79a8cf4017d0f0845db36a Mon Sep 17 00:00:00 2001 From: Marius Gedminas Date: Fri, 5 Dec 2014 16:42:10 +0200 Subject: [PATCH] WIP: fix %newimage Images are supposed to take a line of their own (unless you use %cont) and their positioning should be influenced by %default directives. This is a work in progress that triggers assertion errors when you use %again inside a %default directive. It also breaks my slide decks quite badly and doesn't actually fix image alignment anyway. --- mgp2pdf.py | 27 ++++++++++++++++------ samples/synthetic/images.mgp | 10 ++++++++ tests.py | 44 ++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 7 deletions(-) diff --git a/mgp2pdf.py b/mgp2pdf.py index 8af03c5..8409f88 100755 --- a/mgp2pdf.py +++ b/mgp2pdf.py @@ -202,7 +202,7 @@ def addImage(self, filename, zoom=100, raised_by=0): """ line = self.currentOrNewLine() line.add(Image(filename, zoom, raised_by)) - # XXX: self.closeCurrentLine()? + self.closeCurrentLine() def addMark(self): """Add a mark to the current line and return it. @@ -798,6 +798,7 @@ def _handleDirective_page(self, parts): self._lastlineno = 0 self._use_defaults = True self._continuing = False + self._applying_defaults = False self._directives_used_in_this_line = set() self.mark = None @@ -931,7 +932,12 @@ def _handleDirective_newimage(self, parts): else: raise MgpSyntaxError("newimage %s not handled yet" % k) filename = os.path.join(self.basedir, args[-1]) + if not self._continuing and not self._applying_defaults: + self._lastlineno += 1 + self._applyDefaults() self.slides[-1].addImage(filename, zoom, raised_by) + if self._applying_defaults: + self.slides[-1].reopenCurrentLine() def _handleDirective_mark(self, parts): """Handle %mark. @@ -970,7 +976,7 @@ def _handleDirective_again(self, parts): """ if not self.mark: raise MgpSyntaxError("%again without %mark") - self._handleText('') + ##self._handleText('') self.slides[-1].addAgain(self.mark) def _handleUnknownDirective(self, parts): @@ -1018,17 +1024,24 @@ def _parseArgs(self, parts, argspec): assert False, 'unknown argspec %r' % arg return tuple(results) + def _applyDefaults(self): + """Apply the default directives for this line""" + assert not self._applying_defaults + if self._use_defaults: + self._applying_defaults = True + for part in self.defaultDirectives.get(self._lastlineno, []): + word = self._splitArgs(part)[0] + if word not in self._directives_used_in_this_line: + self._handleDirective(part) + self._applying_defaults = False + def _handleText(self, line): """Handle a line of text that is not a comment or a directive.""" if self.inPreamble(): raise MgpSyntaxError('No text allowed in the preamble') if not self._continuing: self._lastlineno += 1 - if self._use_defaults: - for part in self.defaultDirectives.get(self._lastlineno, []): - word = self._splitArgs(part)[0] - if word not in self._directives_used_in_this_line: - self._handleDirective(part) + self._applyDefaults() line = line.rstrip('\n').replace(r'\#', '#').replace(r'\\', '\\') self.slides[-1].addText(line) self._continuing = False diff --git a/samples/synthetic/images.mgp b/samples/synthetic/images.mgp index edba6b4..c32204b 100644 --- a/samples/synthetic/images.mgp +++ b/samples/synthetic/images.mgp @@ -1,6 +1,16 @@ %default 1 fore "black", back "white", center %page +When do images cause line breaks? + +%newimage "povlogo.png" +%newimage "povlogo.png" +%newimage "povlogo.png" +text + +Always +%page + So, -raise doesn't appear to do anything %newimage "povlogo.png" diff --git a/tests.py b/tests.py index a8c1d6f..0cb23ca 100644 --- a/tests.py +++ b/tests.py @@ -308,6 +308,50 @@ def test_default(self): self.assertRaises(mgp2pdf.MgpSyntaxError, p._handleDirectives, '%default 1 left') + def test_application_of_defaults(self): + p = mgp2pdf.Presentation() + p.load(StringIO('\n'.join([ + '%default 1 left', + '%default 2 center', + '%default 3 right', + '%page', + 'a', + 'b', + '%cont', + 'b', + 'c', + ]))) + self.assertEqual(str(p), + "--- Slide 1 ---\n" + "a\n" + "bb\n" + "c\n") + self.assertEqual([line.alignment for line in p.slides[0].lines], + [mgp2pdf.Left, mgp2pdf.Center, mgp2pdf.Right]) + + @mock.patch('mgp2pdf.ImageReader') + def test_application_of_defaults_when_images_are_involved(self, mock_ImageReader): + mock_ImageReader().getSize.return_value = 100, 50 + p = mgp2pdf.Presentation() + p.load(StringIO('\n'.join([ + '%default 1 left', + '%default 2 center', + '%default 3 right', + '%page', + 'a', + '%newimage "b.png"', + '%cont', + 'b', + 'c', + ]))) + self.assertEqual(str(p), + "--- Slide 1 ---\n" + "a\n" + "[b.png]b\n" + "c\n") + self.assertEqual([line.alignment for line in p.slides[0].lines], + [mgp2pdf.Left, mgp2pdf.Center, mgp2pdf.Right]) + def test_tab(self): p = mgp2pdf.Presentation() p._handleDirectives('%tab 1')