• Paul Berry's avatar
    Fix applyBoxFit's handling of fitWidth and fitHeight. (#117185) · 997d4361
    Paul Berry authored
    * Fix applyBoxFit's handling of fitWidth and fitHeight.
    
    Previously, in `fitWidth` mode, if the input size had a wider aspect
    ratio than the output size, `applyBoxFit` would make the source rect
    taller than the input size in order to match the aspect ratio of the
    destination rect.  Similarly, in `fitHeight` mode, if the input size
    had a taller aspect ratio than the output size, `applyBoxFit` would
    make the source rect wider than the input size in to match the aspect
    ratio of the destination rect.  This is in contrast to all the other
    modes, which never output a source rect that's larger than the input
    size.
    
    Most of the time this worked as intended (since attempting to blit
    pixels that are outside the source image has no effect), however it
    meant that if a user attempted to create a `BoxDecoration` that used
    both `fitWidth` and `repeatY` (e.g. in an attempt to tile a background
    image), the image would not actually appear to repeat, since the logic
    in `paintImage` for determining the proper tiling stride is based on
    the destination image size, meaning that the entire destination rect
    would be covered in a single tile.
    
    This change modifies `applyBoxFit` so that in `fitWidth` mode, if the
    input size has a wider aspect ratio than the output size, it uses
    formulas that are equivalent to `contain`, whereas if the input size
    has a taller aspect ratio than the output size, it uses formulas that
    are equivalent to `cover`.  And vice versa for `fitHeight` mode.  This
    produces source and destination rects that match the behaviour
    specified in https://api.flutter.dev/flutter/painting/BoxFit.html.
    
    * Apply suggestions from code review
    Co-authored-by: 's avatarMichael Goderbauer <goderbauer@google.com>
    997d4361
box_fit.dart 8.14 KB