def best_fit(context, box_dim=(0, 0), svg_dim=(0, 0), start_coords=(0, 0)):
    """
    Scale context to best fit into box
    Credit: http://stackoverflow.com/a/5514344/216042
    Gravity to center
    """
    box_width = box_dim[0]
    box_height = box_dim[1]
    svg_width = svg_dim[0]
    svg_height = svg_dim[1]
    x_start, y_start = start_coords
    width_ratio = float(box_width) / svg_width
    height_ratio = float(box_height) / svg_height
    scale = min(height_ratio, width_ratio)
    x_margin = (box_width - svg_width * scale) / 2
    y_margin = (box_height - svg_height * scale) / 2
    context.translate(x_start + x_margin, y_start + y_margin)
    context.scale(scale, scale)
