units.py 917 B

123456789101112131415161718192021222324252627282930
  1. #!/bin/env python
  2. #Copyright ReportLab Europe Ltd. 2000-2017
  3. #see license.txt for license details
  4. #history https://hg.reportlab.com/hg-public/reportlab/log/tip/src/reportlab/lib/units.py
  5. __version__='3.3.0'
  6. __doc__='''Defines inch, cm, mm etc as multiples of a point
  7. You can now in user-friendly units by doing::
  8. from reportlab.lib.units import inch
  9. r = Rect(0, 0, 3 * inch, 6 * inch)
  10. '''
  11. inch = 72.0
  12. cm = inch / 2.54
  13. mm = cm * 0.1
  14. pica = 12.0
  15. def toLength(s):
  16. '''convert a string to a length'''
  17. try:
  18. if s[-2:]=='cm': return float(s[:-2])*cm
  19. if s[-2:]=='in': return float(s[:-2])*inch
  20. if s[-2:]=='pt': return float(s[:-2])
  21. if s[-1:]=='i': return float(s[:-1])*inch
  22. if s[-2:]=='mm': return float(s[:-2])*mm
  23. if s[-4:]=='pica': return float(s[:-4])*pica
  24. return float(s)
  25. except:
  26. raise ValueError("Can't convert '%s' to length" % s)