def zoom_fit(screen, bounds, balanced=True):
    """What affine transform will zoom-fit the given items?
         screen: (w,h) of the viewing region
         bounds: (x,y,w,h) of the items to fit
         balance: Should the x and y scales match?
         returns: [translate x, translate y, scale x, scale y]
    """
    (sw, sh) = screen
    (gx, gy, gw, gh) = bounds
    x_scale = sw/gw
    y_scale = sh/gh
    if (balanced):
        x_scale = min(x_scale, y_scale)
        y_scale = x_scale
    return [-gx*x_scale, -gy*y_scale, x_scale, y_scale]