# Path: weasyprint/layout/min_max.py
# def handle_min_max_height(function):
#     """Decorate a function setting used height, handling {min,max}-height."""
#     @functools.wraps(function)
#     def wrapper(box, *args):
#         computed_margins = box.margin_top, box.margin_bottom
#         result = function(box, *args)
#         if box.height > box.max_height:
#             box.height = box.max_height
#             box.margin_top, box.margin_bottom = computed_margins
#             result = function(box, *args)
#         if box.height < box.min_height:
#             box.height = box.min_height
#             box.margin_top, box.margin_bottom = computed_margins
#             result = function(box, *args)
#         return result
#     wrapper.without_min_max = function
#     return wrapper
# 
# def handle_min_max_width(function):
#     """Decorate a function setting used width, handling {min,max}-width."""
#     @functools.wraps(function)
#     def wrapper(box, *args):
#         computed_margins = box.margin_left, box.margin_right
#         result = function(box, *args)
#         if box.width > box.max_width:
#             box.width = box.max_width
#             box.margin_left, box.margin_right = computed_margins
#             result = function(box, *args)
#         if box.width < box.min_width:
#             box.width = box.min_width
#             box.margin_left, box.margin_right = computed_margins
#             result = function(box, *args)
#         return result
#     wrapper.without_min_max = function
#     return wrapper
# 
# Path: weasyprint/layout/percent.py
# def percentage(value, refer_to):
#     """Return the percentage of the reference value, or the value unchanged.
# 
#     ``refer_to`` is the length for 100%. If ``refer_to`` is not a number, it
#     just replaces percentages.
# 
#     """
#     if value is None or value == 'auto':
#         return value
#     elif value.unit == 'px':
#         return value.value
#     else:
#         assert value.unit == '%'
#         return refer_to * value.value / 100


from .min_max import handle_min_max_height, handle_min_max_width
from .percent import percentage
    from .block import block_level_width
    from .block import block_level_width
    from .float import avoid_collisions
    from .block import block_level_width
    image = box.replacement
    intrinsic_width, intrinsic_height, intrinsic_ratio = (
        image.get_intrinsic_size(
            box.style['image_resolution'], box.style['font_size']))
    if None in (intrinsic_width, intrinsic_height):
        intrinsic_width, intrinsic_height = contain_constraint_image_sizing(
            box.width, box.height, intrinsic_ratio)

    if object_fit == 'fill':
        draw_width, draw_height = box.width, box.height
    else:
        if object_fit == 'contain' or object_fit == 'scale-down':
            draw_width, draw_height = contain_constraint_image_sizing(
                box.width, box.height, intrinsic_ratio)
        elif object_fit == 'cover':
            draw_width, draw_height = cover_constraint_image_sizing(
                box.width, box.height, intrinsic_ratio)
        else:
            assert object_fit == 'none', object_fit
            draw_width, draw_height = intrinsic_width, intrinsic_height

        if object_fit == 'scale-down':
            draw_width = min(draw_width, intrinsic_width)
            draw_height = min(draw_height, intrinsic_height)

    origin_x, position_x, origin_y, position_y = position[0]
    ref_x = box.width - draw_width
    ref_y = box.height - draw_height

    position_x = percentage(position_x, ref_x)

# Path: weasyprint/layout/min_max.py
# def handle_min_max_height(function):
#     """Decorate a function setting used height, handling {min,max}-height."""
#     @functools.wraps(function)
#     def wrapper(box, *args):
#         computed_margins = box.margin_top, box.margin_bottom
#         result = function(box, *args)
#         if box.height > box.max_height:
#             box.height = box.max_height
#             box.margin_top, box.margin_bottom = computed_margins
#             result = function(box, *args)
#         if box.height < box.min_height:
#             box.height = box.min_height
#             box.margin_top, box.margin_bottom = computed_margins
#             result = function(box, *args)
#         return result
#     wrapper.without_min_max = function
#     return wrapper
# 
# def handle_min_max_width(function):
#     """Decorate a function setting used width, handling {min,max}-width."""
#     @functools.wraps(function)
#     def wrapper(box, *args):
#         computed_margins = box.margin_left, box.margin_right
#         result = function(box, *args)
#         if box.width > box.max_width:
#             box.width = box.max_width
#             box.margin_left, box.margin_right = computed_margins
#             result = function(box, *args)
#         if box.width < box.min_width:
#             box.width = box.min_width
#             box.margin_left, box.margin_right = computed_margins
#             result = function(box, *args)
#         return result
#     wrapper.without_min_max = function
#     return wrapper
# 
# Path: weasyprint/layout/percent.py
# def percentage(value, refer_to):
#     """Return the percentage of the reference value, or the value unchanged.
# 
#     ``refer_to`` is the length for 100%. If ``refer_to`` is not a number, it
#     just replaces percentages.
# 
#     """
#     if value is None or value == 'auto':
#         return value
#     elif value.unit == 'px':
#         return value.value
#     else:
#         assert value.unit == '%'
#         return refer_to * value.value / 100


# Path: weasyprint/layout/replaced.py
from .min_max import handle_min_max_height, handle_min_max_width
from .percent import percentage
    from .block import block_level_width
    from .block import block_level_width
    from .float import avoid_collisions
    from .block import block_level_width

    image = box.replacement
    intrinsic_width, intrinsic_height, intrinsic_ratio = (
        image.get_intrinsic_size(
            box.style['image_resolution'], box.style['font_size']))
    if None in (intrinsic_width, intrinsic_height):
        intrinsic_width, intrinsic_height = contain_constraint_image_sizing(
            box.width, box.height, intrinsic_ratio)

    if object_fit == 'fill':
        draw_width, draw_height = box.width, box.height
    else:
        if object_fit == 'contain' or object_fit == 'scale-down':
            draw_width, draw_height = contain_constraint_image_sizing(
                box.width, box.height, intrinsic_ratio)
        elif object_fit == 'cover':
            draw_width, draw_height = cover_constraint_image_sizing(
                box.width, box.height, intrinsic_ratio)
        else:
            assert object_fit == 'none', object_fit
            draw_width, draw_height = intrinsic_width, intrinsic_height

        if object_fit == 'scale-down':
            draw_width = min(draw_width, intrinsic_width)
            draw_height = min(draw_height, intrinsic_height)

    origin_x, position_x, origin_y, position_y = position[0]
    ref_x = box.width - draw_width
    ref_y = box.height - draw_height

    position_x = percentage(position_x, ref_x)

    position_y = percentage(position_y, ref_y)