src/reportlab/lib/geomutils.py
author robin <robin@reportlab.com>
Tue, 07 Mar 2017 10:00:34 +0000
changeset 4330 617ffa6bbdc8
parent 4252 fe660f227cac
child 4407 7acf013b166e
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
3042
2ae512e69843 Implemented variable padding for platypus paragraphs.
jonas
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'
3042
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) ])