src/reportlab/lib/geomutils.py
author rptlab
Tue, 30 Apr 2013 14:28:14 +0100
branchpy33
changeset 3723 99aa837b6703
parent 3617 ae5744e97c42
child 4252 fe660f227cac
permissions -rw-r--r--
second stage of port to Python 3.3; working hello world
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3617
ae5744e97c42 reportlab: copyright date changes
robin
parents: 3042
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2012
3042
2ae512e69843 Implemented variable padding for platypus paragraphs.
jonas
parents:
diff changeset
     2
#see license.txt for license details
2ae512e69843 Implemented variable padding for platypus paragraphs.
jonas
parents:
diff changeset
     3
__version__=''' $Id$ '''
2ae512e69843 Implemented variable padding for platypus paragraphs.
jonas
parents:
diff changeset
     4
__doc__='''Utility functions for geometrical operations.'''
2ae512e69843 Implemented variable padding for platypus paragraphs.
jonas
parents:
diff changeset
     5
2ae512e69843 Implemented variable padding for platypus paragraphs.
jonas
parents:
diff changeset
     6
def normalizeTRBL(p):
2ae512e69843 Implemented variable padding for platypus paragraphs.
jonas
parents:
diff changeset
     7
    '''
2ae512e69843 Implemented variable padding for platypus paragraphs.
jonas
parents:
diff changeset
     8
    Useful for interpreting short descriptions of paddings, borders, margin, etc.
2ae512e69843 Implemented variable padding for platypus paragraphs.
jonas
parents:
diff changeset
     9
    Expects a single value or a tuple of length 2 to 4.
2ae512e69843 Implemented variable padding for platypus paragraphs.
jonas
parents:
diff changeset
    10
    Returns a tuple representing (clockwise) the value(s) applied to the 4 sides of a rectangle:
2ae512e69843 Implemented variable padding for platypus paragraphs.
jonas
parents:
diff changeset
    11
    If a single value is given, that value is applied to all four sides.
2ae512e69843 Implemented variable padding for platypus paragraphs.
jonas
parents:
diff changeset
    12
    If two or three values are given, the missing values are taken from the opposite side(s).
2ae512e69843 Implemented variable padding for platypus paragraphs.
jonas
parents:
diff changeset
    13
    If four values are given they are returned unchanged.
2ae512e69843 Implemented variable padding for platypus paragraphs.
jonas
parents:
diff changeset
    14
2ae512e69843 Implemented variable padding for platypus paragraphs.
jonas
parents:
diff changeset
    15
    >>> normalizeTRBL(1)
2ae512e69843 Implemented variable padding for platypus paragraphs.
jonas
parents:
diff changeset
    16
    (1, 1, 1, 1)
2ae512e69843 Implemented variable padding for platypus paragraphs.
jonas
parents:
diff changeset
    17
    >>> normalizeTRBL((1, 1.2))
2ae512e69843 Implemented variable padding for platypus paragraphs.
jonas
parents:
diff changeset
    18
    (1, 1.2, 1, 1.2)
2ae512e69843 Implemented variable padding for platypus paragraphs.
jonas
parents:
diff changeset
    19
    >>> normalizeTRBL((1, 1.2, 0))
2ae512e69843 Implemented variable padding for platypus paragraphs.
jonas
parents:
diff changeset
    20
    (1, 1.2, 0, 1.2)
2ae512e69843 Implemented variable padding for platypus paragraphs.
jonas
parents:
diff changeset
    21
    >>> normalizeTRBL((1, 1.2, 0, 8))
2ae512e69843 Implemented variable padding for platypus paragraphs.
jonas
parents:
diff changeset
    22
    (1, 1.2, 0, 8)
2ae512e69843 Implemented variable padding for platypus paragraphs.
jonas
parents:
diff changeset
    23
    '''
2ae512e69843 Implemented variable padding for platypus paragraphs.
jonas
parents:
diff changeset
    24
    if not isinstance(p, (tuple, list)):
2ae512e69843 Implemented variable padding for platypus paragraphs.
jonas
parents:
diff changeset
    25
        return (p,)*4
2ae512e69843 Implemented variable padding for platypus paragraphs.
jonas
parents:
diff changeset
    26
    l = len(p)
2ae512e69843 Implemented variable padding for platypus paragraphs.
jonas
parents:
diff changeset
    27
    if l < 2 or l > 4:
2ae512e69843 Implemented variable padding for platypus paragraphs.
jonas
parents:
diff changeset
    28
        raise ValueError('A padding must have between 2 and 4 values but got %d.' % l)
2ae512e69843 Implemented variable padding for platypus paragraphs.
jonas
parents:
diff changeset
    29
    return tuple(p) + tuple([ p[i-2] for i in range(l, 4) ])