author | rptlab |
Tue, 30 Apr 2013 14:28:14 +0100 | |
branch | py33 |
changeset 3723 | 99aa837b6703 |
parent 3617 | ae5744e97c42 |
child 4252 | fe660f227cac |
permissions | -rw-r--r-- |
3617 | 1 |
#Copyright ReportLab Europe Ltd. 2000-2012 |
2638 | 2 |
#see license.txt for license details |
3 |
__version__=''' $Id$ ''' |
|
3029 | 4 |
__doc__='''Utility functions to position and resize boxes within boxes''' |
2638 | 5 |
|
2639
9ff3bbcf3282
reportlab: make anchor/preserveAspectRatio cooperate
rgbecker
parents:
2638
diff
changeset
|
6 |
def aspectRatioFix(preserve,anchor,x,y,width,height,imWidth,imHeight): |
3092 | 7 |
"""This function helps position an image within a box. |
8 |
||
9 |
It first normalizes for two cases: |
|
10 |
- if the width is None, it assumes imWidth |
|
11 |
- ditto for height |
|
3094 | 12 |
- if width or height is negative, it adjusts x or y and makes them positive |
3092 | 13 |
|
3094 | 14 |
Given |
15 |
(a) the enclosing box (defined by x,y,width,height where x,y is the \ |
|
16 |
lower left corner) which you wish to position the image in, and |
|
3092 | 17 |
(b) the image size (imWidth, imHeight), and |
3094 | 18 |
(c) the 'anchor point' as a point of the compass - n,s,e,w,ne,se etc \ |
3092 | 19 |
and c for centre, |
3094 | 20 |
|
3092 | 21 |
this should return the position at which the image should be drawn, |
22 |
as well as a scale factor indicating what scaling has happened. |
|
3094 | 23 |
|
3092 | 24 |
It returns the parameters which would be used to draw the image |
3094 | 25 |
without any adjustments: |
26 |
||
3092 | 27 |
x,y, width, height, scale |
28 |
||
29 |
used in canvas.drawImage and drawInlineImage |
|
30 |
""" |
|
2679 | 31 |
scale = 1.0 |
2638 | 32 |
if width is None: |
33 |
width = imWidth |
|
34 |
if height is None: |
|
35 |
height = imHeight |
|
2639
9ff3bbcf3282
reportlab: make anchor/preserveAspectRatio cooperate
rgbecker
parents:
2638
diff
changeset
|
36 |
if width<0: |
9ff3bbcf3282
reportlab: make anchor/preserveAspectRatio cooperate
rgbecker
parents:
2638
diff
changeset
|
37 |
width = -width |
9ff3bbcf3282
reportlab: make anchor/preserveAspectRatio cooperate
rgbecker
parents:
2638
diff
changeset
|
38 |
x -= width |
9ff3bbcf3282
reportlab: make anchor/preserveAspectRatio cooperate
rgbecker
parents:
2638
diff
changeset
|
39 |
if height<0: |
9ff3bbcf3282
reportlab: make anchor/preserveAspectRatio cooperate
rgbecker
parents:
2638
diff
changeset
|
40 |
height = -height |
9ff3bbcf3282
reportlab: make anchor/preserveAspectRatio cooperate
rgbecker
parents:
2638
diff
changeset
|
41 |
y -= height |
2638 | 42 |
if preserve: |
2639
9ff3bbcf3282
reportlab: make anchor/preserveAspectRatio cooperate
rgbecker
parents:
2638
diff
changeset
|
43 |
imWidth = abs(imWidth) |
9ff3bbcf3282
reportlab: make anchor/preserveAspectRatio cooperate
rgbecker
parents:
2638
diff
changeset
|
44 |
imHeight = abs(imHeight) |
2638 | 45 |
scale = min(width/float(imWidth),height/float(imHeight)) |
2639
9ff3bbcf3282
reportlab: make anchor/preserveAspectRatio cooperate
rgbecker
parents:
2638
diff
changeset
|
46 |
owidth = width |
9ff3bbcf3282
reportlab: make anchor/preserveAspectRatio cooperate
rgbecker
parents:
2638
diff
changeset
|
47 |
oheight = height |
2638 | 48 |
width = scale*imWidth-1e-8 |
49 |
height = scale*imHeight-1e-8 |
|
3092 | 50 |
if anchor not in ('nw','w','sw'): |
51 |
dx = owidth-width |
|
52 |
if anchor in ('n','c','s'): |
|
53 |
x += dx/2. |
|
2639
9ff3bbcf3282
reportlab: make anchor/preserveAspectRatio cooperate
rgbecker
parents:
2638
diff
changeset
|
54 |
else: |
9ff3bbcf3282
reportlab: make anchor/preserveAspectRatio cooperate
rgbecker
parents:
2638
diff
changeset
|
55 |
x += dx |
3092 | 56 |
if anchor not in ('sw','s','se'): |
57 |
dy = oheight-height |
|
58 |
if anchor in ('w','c','e'): |
|
59 |
y += dy/2. |
|
2639
9ff3bbcf3282
reportlab: make anchor/preserveAspectRatio cooperate
rgbecker
parents:
2638
diff
changeset
|
60 |
else: |
9ff3bbcf3282
reportlab: make anchor/preserveAspectRatio cooperate
rgbecker
parents:
2638
diff
changeset
|
61 |
y += dy |
2679 | 62 |
return x,y, width, height, scale |