docs: def _calculate(self):
          """Checks the dimensions of the sheet are valid and consistent.
  
          NB: this is called internally when needed; there should be no need for
          user code to call it.
  
          """
          # Check the dimensions are larger than zero.
          for dimension in ('_sheet_width', '_sheet_height', '_columns', '_rows', '_label_width', '_label_height'):
              if getattr(self, dimension) <= 0:
                  name = dimension.replace('_', ' ').strip().capitalize()
                  raise InvalidDimension("{0:s} must be greater than zero.".format(name))
  
          # Check margins / gaps are not smaller than zero if given.
          # At the same time, force the values to decimals.
          for margin in ('_left_margin', '_column_gap', '_right_margin', '_top_margin', '_row_gap', '_bottom_margin',
                         '_left_padding', '_right_padding', '_top_padding', '_bottom_padding'):
              val = getattr(self, margin)
              if val is not None:
                  if margin in self._autoset:
                      val = None
                  else:
                      val = Decimal(val)
                      if val < 0:
                          name = margin.replace('_', ' ').strip().capitalize()
                          raise InvalidDimension("{0:s} cannot be less than zero.".format(name))
                  setattr(self, margin, val)
              else:
                  self._autoset.add(margin)
  
          # Check the corner radius.
          if self._corner_radius < 0:
              raise InvalidDimension("Corner radius cannot be less than zero.")
          if self._corner_radius > (self._label_width / 2):
              raise InvalidDimension("Corner radius cannot be more than half the label width.")
          if self._corner_radius > (self._label_height / 2):
              raise InvalidDimension("Corner radius cannot be more than half the label height.")
  
          # If there is no padding, we don't need the padding radius.
          if (self._left_padding + self._right_padding + self._top_padding + self._bottom_padding) == 0:
              if self._padding_radius != 0:
                  raise InvalidDimension("Padding radius must be zero if there is no padding.")
          else:
              if (self._left_padding + self._right_padding) >= self._label_width:
                  raise InvalidDimension("Sum of horizontal padding must be less than the label width.")
              if (self._top_padding + self._bottom_padding) >= self._label_height:
                  raise InvalidDimension("Sum of vertical padding must be less than the label height.")
              if self._padding_radius < 0:
                  raise InvalidDimension("Padding radius cannot be less than zero.")
  
          # Calculate the amount of spare space.
          hspace = self._sheet_width - (self._label_width * self._columns)
          vspace = self._sheet_height - (self._label_height * self._rows)
  
          # Cannot fit.
          if hspace < 0:
              raise InvalidDimension("Labels are too wide to fit on the sheet.")
          if vspace < 0:
              raise InvalidDimension("Labels are too tall to fit on the sheet.")
  
          # Process the horizontal margins / gaps.
          hcount = 1 + self._columns
          if self._left_margin is not None:
              hspace -= self._left_margin
              if hspace < 0:
                  raise InvalidDimension("Left margin is too wide for the labels to fit on the sheet.")
              hcount -= 1
          if self._column_gap is not None:
              hspace -= ((self._columns - 1) * self._column_gap)
              if hspace < 0:
                  raise InvalidDimension("Column gap is too wide for the labels to fit on the sheet.")
              hcount -= (self._columns - 1)
          if self._right_margin is not None:
              hspace -= self._right_margin
              if hspace < 0.01 and hspace > -0.01:
                  self._right_margin += hspace
                  hspace = 0
              if hspace < 0:
                  raise InvalidDimension("Right margin is too wide for the labels to fit on the sheet.")
              hcount -= 1
  
          # Process the vertical margins / gaps.
          vcount = 1 + self._rows
          if self._top_margin is not None:
              vspace -= self._top_margin
              if vspace < 0:
                  raise InvalidDimension("Top margin is too tall for the labels to fit on the sheet.")
              vcount -= 1
          if self._row_gap is not None:
              vspace -= ((self._rows - 1) * self._row_gap)
              if vspace < 0:
                  raise InvalidDimension("Row gap is too tall for the labels to fit on the sheet.")
              vcount -= (self._rows - 1)
          if self._bottom_margin is not None:
              vspace -= self._bottom_margin
              if vspace < 0.01 and vspace > -0.01:
                  self._bottom_margin += vspace
                  vspace = 0
              if vspace < 0:
                  raise InvalidDimension("Bottom margin is too tall for the labels to fit on the sheet.")
              vcount -= 1
  
          # If all the margins are specified, they must use up all available space.
          if hcount == 0 and hspace != 0:
              raise InvalidDimension("Not all width used by manually specified margins/gaps; {}mm left.".format(hspace))
          if vcount == 0 and vspace != 0:
              raise InvalidDimension("Not all height used by manually specified margins/gaps; {}mm left.".format(vspace))
  
          # Split any extra horizontal space and allocate it.
          if hcount:
              auto_margin = hspace / hcount
              for margin in ('_left_margin', '_column_gap', '_right_margin'):
                  if getattr(self, margin) is None:
                      setattr(self, margin, auto_margin)
  
          # And allocate any extra vertical space.
          if vcount:
              auto_margin = vspace / vcount
              for margin in ('_top_margin', '_row_gap', '_bottom_margin'):
                  if getattr(self, margin) is None:
                      setattr(self, margin, auto_margin)