papersizes.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. """Helper to get paper sizes."""
  2. from typing import NamedTuple
  3. class Dimensions(NamedTuple):
  4. width: int
  5. height: int
  6. class PaperSize:
  7. """(width, height) of the paper in portrait mode in pixels at 72 ppi."""
  8. # Notes of how to calculate it:
  9. # 1. Get the size of the paper in millimeters
  10. # 2. Convert it to inches (25.4 millimeters is equal to 1 inch)
  11. # 3. Convert it to pixels at 72dpi (1 inch is equal to 72 pixels)
  12. # All Din-A paper sizes follow this pattern:
  13. # 2 x A(n - 1) = A(n)
  14. # So the height of the next bigger one is the width of the smaller one
  15. # The ratio is always approximately 1:2**0.5
  16. # Additionally, A0 is defined to have an area of 1 m**2
  17. # https://en.wikipedia.org/wiki/ISO_216
  18. # Be aware of rounding issues!
  19. A0 = Dimensions(2384, 3370) # 841mm x 1189mm
  20. A1 = Dimensions(1684, 2384)
  21. A2 = Dimensions(1191, 1684)
  22. A3 = Dimensions(842, 1191)
  23. A4 = Dimensions(
  24. 595, 842
  25. ) # Printer paper, documents - this is by far the most common
  26. A5 = Dimensions(420, 595) # Paperback books
  27. A6 = Dimensions(298, 420) # Postcards
  28. A7 = Dimensions(210, 298)
  29. A8 = Dimensions(147, 210)
  30. # Envelopes
  31. C4 = Dimensions(649, 918)
  32. _din_a = (
  33. PaperSize.A0,
  34. PaperSize.A1,
  35. PaperSize.A2,
  36. PaperSize.A3,
  37. PaperSize.A4,
  38. PaperSize.A5,
  39. PaperSize.A6,
  40. PaperSize.A7,
  41. PaperSize.A8,
  42. )