| 30 | } |
| 31 | |
| 32 | Rect Rect::makeFittingSize(Size size, FittingSizeMode mode) const { |
| 33 | auto selfWidth = width(); |
| 34 | auto selfHeight = height(); |
| 35 | auto halfWidth = selfWidth / 2.0f; |
| 36 | auto halfHeight = selfHeight / 2.0f; |
| 37 | |
| 38 | switch (mode) { |
| 39 | case FittingSizeModeFill: |
| 40 | return *this; |
| 41 | case FittingSizeModeCenter: { |
| 42 | return Rect::makeXYWH( |
| 43 | left + halfWidth - size.width / 2.0f, top + halfHeight - size.height / 2.0f, size.width, size.height); |
| 44 | } |
| 45 | case FittingSizeModeCenterScaleFill: |
| 46 | case FittingSizeModeCenterScaleFit: { |
| 47 | auto wRatio = selfWidth / size.width; |
| 48 | auto hRatio = selfHeight / size.height; |
| 49 | auto ratio = mode == FittingSizeModeCenterScaleFill ? std::max(wRatio, hRatio) : std::min(wRatio, hRatio); |
| 50 | |
| 51 | auto resolvedTargetWidth = size.width * ratio; |
| 52 | auto resolvedTargetHeight = size.height * ratio; |
| 53 | |
| 54 | return Rect::makeXYWH(left + halfWidth - resolvedTargetWidth / 2.0f, |
| 55 | top + halfHeight - resolvedTargetHeight / 2.0f, |
| 56 | resolvedTargetWidth, |
| 57 | resolvedTargetHeight); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | bool Rect::intersects(const Rect& other) const { |
| 63 | // Use Skia's exact floating-point comparison for general intersection checks. |