src/reportlab/lib/units.py
author rptlab
Tue, 30 Apr 2013 14:28:14 +0100
branchpy33
changeset 3723 99aa837b6703
parent 3721 0c93dd8ff567
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:
41
75f8140d039b Initial Checkin
andy_robinson
parents:
diff changeset
     1
#!/bin/env python
3617
ae5744e97c42 reportlab: copyright date changes
robin
parents: 3029
diff changeset
     2
#Copyright ReportLab Europe Ltd. 2000-2012
494
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 41
diff changeset
     3
#see license.txt for license details
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 1683
diff changeset
     4
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/lib/units.py
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 1683
diff changeset
     5
__version__=''' $Id$ '''
3029
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
     6
__doc__='''Defines inch, cm, mm etc as multiples of a point
41
75f8140d039b Initial Checkin
andy_robinson
parents:
diff changeset
     7
3029
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
     8
You can now in user-friendly units by doing::
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
     9
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
    10
    from reportlab.lib.units import inch
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
    11
    r = Rect(0, 0, 3 * inch, 6 * inch)
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
    12
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
    13
'''
41
75f8140d039b Initial Checkin
andy_robinson
parents:
diff changeset
    14
inch = 72.0
75f8140d039b Initial Checkin
andy_robinson
parents:
diff changeset
    15
cm = inch / 2.54
1516
fd701770a440 Added picas/mm
rgbecker
parents: 895
diff changeset
    16
mm = cm * 0.1
fd701770a440 Added picas/mm
rgbecker
parents: 895
diff changeset
    17
pica = 12.0
41
75f8140d039b Initial Checkin
andy_robinson
parents:
diff changeset
    18
895
e43766aef23f Added toLength function
rgbecker
parents: 494
diff changeset
    19
def toLength(s):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1516
diff changeset
    20
    '''convert a string to  a length'''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1516
diff changeset
    21
    try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1516
diff changeset
    22
        if s[-2:]=='cm': return float(s[:-2])*cm
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1516
diff changeset
    23
        if s[-2:]=='in': return float(s[:-2])*inch
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1516
diff changeset
    24
        if s[-2:]=='pt': return float(s[:-2])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1516
diff changeset
    25
        if s[-1:]=='i': return float(s[:-1])*inch
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1516
diff changeset
    26
        if s[-2:]=='mm': return float(s[:-2])*mm
2455
b5bd3ca94a8f units.py: fix pica conversion thanks to Paul McNett
rgbecker
parents: 2332
diff changeset
    27
        if s[-4:]=='pica': return float(s[:-4])*pica
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1516
diff changeset
    28
        return float(s)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1516
diff changeset
    29
    except:
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
    30
        raise ValueError("Can't convert '%s' to length" % s)