Skip to content

Commit

Permalink
Implement %newimage -raise correctly
Browse files Browse the repository at this point in the history
Turns out mgp doesn't support -raise!  I'm amaze.
  • Loading branch information
mgedmin committed Dec 4, 2014
1 parent 146d5bc commit 73d4845
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
5 changes: 4 additions & 1 deletion SUPPORT.rst
Expand Up @@ -15,7 +15,6 @@ Supported:
- %right
- %cont
- %nodefault
- %newimage
- %default
- %page
- %vgap
Expand All @@ -32,6 +31,10 @@ Partially supported:
- %deffont (only one directive accepted, if multiple are present they'll be mishandled)
- %area (xoffset/yoffset not allowed)
- %tab (parsing is done, but it has no effet)
- %newimage (only -zoom, -raise [which isn't supported by mgp itself!])
(not supported bits include: -colors, -xysize, -xyzoom, -scrzoom, -xscrzoom, -yscrzoom, -xyscrzoom, -rotate)
(can't possibly support: -zoomonclk)
(not supported: applying fore/back colors to monochrome images)

Ignored:

Expand Down
11 changes: 5 additions & 6 deletions mgp2pdf.py
Expand Up @@ -200,9 +200,7 @@ def addImage(self, filename, zoom=100, raised_by=0):
XXX: shouldn't setArea() influence the image size?
``raised_by`` is a baseline adjustment, in points.
XXX: should it be in points?
``raised_by`` is a baseline adjustment, in percentage of image size.
"""
line = self.currentOrNewLine()
line.add(Image(filename, zoom, raised_by))
Expand Down Expand Up @@ -498,7 +496,7 @@ class Image(SimpleChunk):
def __init__(self, filename, zoom=100, raised_by=0):
self.filename = filename
self.zoom = zoom
self.raised_by = 0
self.raised_by = raised_by
self.image = ImageReader(filename)

def size(self, canvas, w, h):
Expand All @@ -509,8 +507,9 @@ def size(self, canvas, w, h):

def drawOn(self, canvas, x, y, w, h):
myw, myh = self.size(canvas, w, h)
raised_by = self.raised_by * myh / 100
try:
canvas.drawImage(self.filename, x, y - myh + self.raised_by, myw, myh,
canvas.drawImage(self.filename, x, y - myh + raised_by, myw, myh,
mask='auto')
except Exception:
log.debug("Exception in canvas.drawImage:", exc_info=True)
Expand Down Expand Up @@ -914,7 +913,7 @@ def _handleDirective_newimage(self, parts):
Supported flags include:
-zoom <percent>
-raise <amount>
-raise <percent>
"""
n = (len(parts) - 1) / 2
Expand Down
14 changes: 14 additions & 0 deletions tests.py
Expand Up @@ -263,6 +263,20 @@ def test_backslash_escaping(self):
"# This starts with a hash and has a \\\n")
# The test is incomplete: \xHH is not yet supported

@mock.patch('mgp2pdf.ImageReader')
def test_newimage(self, mock_ImageReader):
p = mgp2pdf.Presentation()
p._handleDirectives('%page')
p._handleDirectives('%newimage "cat.png"')
self.assertEqual(str(p),
"--- Slide 1 ---\n"
"[cat.png]\n")
p._handleDirectives('%page')
p._handleDirectives('%newimage -zoom 50 -raise 14 "dog.png"')
image = p.slides[-1].lines[-1].chunks[-1]
self.assertEqual(image.zoom, 50)
self.assertEqual(image.raised_by, 14)


@mock.patch('sys.stdout', StringIO())
@mock.patch('sys.stderr', StringIO())
Expand Down

0 comments on commit 73d4845

Please sign in to comment.