def aligned_maxdims(origin, dims, container_dims, alignment_x="left", alignment_y="top"):
  """ Determine the maximum dimensions of an object inside a container of fixed size. """
  x, y = origin
  w, h = dims
  cw, ch = container_dims

  # This needs to take into account the alignment (X)
  maxwidth = min(cw - x, w)
  if (alignment_x == "center"):
    maxwidth = min(2*x, 2*(cw - x))                 # Edges of the container
    maxwidth = min(maxwidth, w)                     # Specified width

  elif (alignment_x == "right"):
    maxwidth = min(x, w)

  # Likewise, we need to find the max height including the edges of the card.
  maxheight = min(ch - y, h)
  if (alignment_y == "center"):
    maxheight = min(2*y, 2*(ch - y))              # Edges of the container
    maxheight = min(maxheight, h)                 # Specified height

  elif (alignment_y == "bottom"):
    maxheight = min(y, h)

  return (maxwidth, maxheight)