src/reportlab/lib/boxstuff.py
author robin <robin@reportlab.com>
Tue, 07 Mar 2017 10:00:34 +0000
changeset 4330 617ffa6bbdc8
parent 4252 fe660f227cac
child 4398 ae13c67803cb
permissions -rw-r--r--
changes for release 3.4.0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4330
617ffa6bbdc8 changes for release 3.4.0
robin <robin@reportlab.com>
parents: 4252
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2017
2638
ee3e8ad6c3cb reportlab: added first stage boxstuff for images
rgbecker
parents:
diff changeset
     2
#see license.txt for license details
4252
fe660f227cac changes for release 3.3.0
robin
parents: 3617
diff changeset
     3
__version__='3.3.0'
3029
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
     4
__doc__='''Utility functions to position and resize boxes within boxes'''
2638
ee3e8ad6c3cb reportlab: added first stage boxstuff for images
rgbecker
parents:
diff changeset
     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
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 3029
diff changeset
     7
    """This function helps position an image within a box.
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 3029
diff changeset
     8
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 3029
diff changeset
     9
    It first normalizes for two cases:
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 3029
diff changeset
    10
    - if the width is None, it assumes imWidth
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 3029
diff changeset
    11
    - ditto for height
3094
5387517a8605 reportlab: fix rest comments in boxstuff.py
rptlab
parents: 3092
diff changeset
    12
    - if width or height is negative, it adjusts x or y and makes them positive
3092
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 3029
diff changeset
    13
3094
5387517a8605 reportlab: fix rest comments in boxstuff.py
rptlab
parents: 3092
diff changeset
    14
    Given
5387517a8605 reportlab: fix rest comments in boxstuff.py
rptlab
parents: 3092
diff changeset
    15
    (a) the enclosing box (defined by x,y,width,height where x,y is the \
5387517a8605 reportlab: fix rest comments in boxstuff.py
rptlab
parents: 3092
diff changeset
    16
        lower left corner) which you wish to position the image in, and
3092
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 3029
diff changeset
    17
    (b) the image size (imWidth, imHeight), and
3094
5387517a8605 reportlab: fix rest comments in boxstuff.py
rptlab
parents: 3092
diff changeset
    18
    (c) the 'anchor point' as a point of the compass - n,s,e,w,ne,se etc \
3092
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 3029
diff changeset
    19
        and c for centre,
3094
5387517a8605 reportlab: fix rest comments in boxstuff.py
rptlab
parents: 3092
diff changeset
    20
3092
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 3029
diff changeset
    21
    this should return the position at which the image should be drawn,
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 3029
diff changeset
    22
    as well as a scale factor indicating what scaling has happened.
3094
5387517a8605 reportlab: fix rest comments in boxstuff.py
rptlab
parents: 3092
diff changeset
    23
3092
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 3029
diff changeset
    24
    It returns the parameters which would be used to draw the image
3094
5387517a8605 reportlab: fix rest comments in boxstuff.py
rptlab
parents: 3092
diff changeset
    25
    without any adjustments:
5387517a8605 reportlab: fix rest comments in boxstuff.py
rptlab
parents: 3092
diff changeset
    26
3092
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 3029
diff changeset
    27
        x,y, width, height, scale
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 3029
diff changeset
    28
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 3029
diff changeset
    29
    used in canvas.drawImage and drawInlineImage
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 3029
diff changeset
    30
    """
2679
b8464568f5e2 tweaks to support PDF art boxes
andy
parents: 2639
diff changeset
    31
    scale = 1.0
2638
ee3e8ad6c3cb reportlab: added first stage boxstuff for images
rgbecker
parents:
diff changeset
    32
    if width is None:
ee3e8ad6c3cb reportlab: added first stage boxstuff for images
rgbecker
parents:
diff changeset
    33
        width = imWidth
ee3e8ad6c3cb reportlab: added first stage boxstuff for images
rgbecker
parents:
diff changeset
    34
    if height is None:
ee3e8ad6c3cb reportlab: added first stage boxstuff for images
rgbecker
parents:
diff changeset
    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
ee3e8ad6c3cb reportlab: added first stage boxstuff for images
rgbecker
parents:
diff changeset
    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
ee3e8ad6c3cb reportlab: added first stage boxstuff for images
rgbecker
parents:
diff changeset
    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
ee3e8ad6c3cb reportlab: added first stage boxstuff for images
rgbecker
parents:
diff changeset
    48
        width = scale*imWidth-1e-8
ee3e8ad6c3cb reportlab: added first stage boxstuff for images
rgbecker
parents:
diff changeset
    49
        height = scale*imHeight-1e-8
3092
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 3029
diff changeset
    50
        if anchor not in ('nw','w','sw'):
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 3029
diff changeset
    51
            dx = owidth-width
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 3029
diff changeset
    52
            if anchor in ('n','c','s'):
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 3029
diff changeset
    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
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 3029
diff changeset
    56
        if anchor not in ('sw','s','se'):
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 3029
diff changeset
    57
            dy = oheight-height
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 3029
diff changeset
    58
            if anchor in ('w','c','e'):
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 3029
diff changeset
    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
b8464568f5e2 tweaks to support PDF art boxes
andy
parents: 2639
diff changeset
    62
    return x,y, width, height, scale