RoundedRect.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. * This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "gfxRect.h"
  6. #include "mozilla/gfx/PathHelpers.h"
  7. namespace mozilla {
  8. /* A rounded rectangle abstraction.
  9. *
  10. * This can represent a rectangle with a different pair of radii on each corner.
  11. *
  12. * Note: CoreGraphics and Direct2D only support rounded rectangle with the same
  13. * radii on all corners. However, supporting CSS's border-radius requires the extra flexibility. */
  14. struct RoundedRect {
  15. typedef mozilla::gfx::RectCornerRadii RectCornerRadii;
  16. RoundedRect(gfxRect &aRect, RectCornerRadii &aCorners) : rect(aRect), corners(aCorners) { }
  17. void Deflate(gfxFloat aTopWidth, gfxFloat aBottomWidth, gfxFloat aLeftWidth, gfxFloat aRightWidth) {
  18. // deflate the internal rect
  19. rect.x += aLeftWidth;
  20. rect.y += aTopWidth;
  21. rect.width = std::max(0., rect.width - aLeftWidth - aRightWidth);
  22. rect.height = std::max(0., rect.height - aTopWidth - aBottomWidth);
  23. corners.radii[NS_CORNER_TOP_LEFT].width = std::max(0., corners.radii[NS_CORNER_TOP_LEFT].width - aLeftWidth);
  24. corners.radii[NS_CORNER_TOP_LEFT].height = std::max(0., corners.radii[NS_CORNER_TOP_LEFT].height - aTopWidth);
  25. corners.radii[NS_CORNER_TOP_RIGHT].width = std::max(0., corners.radii[NS_CORNER_TOP_RIGHT].width - aRightWidth);
  26. corners.radii[NS_CORNER_TOP_RIGHT].height = std::max(0., corners.radii[NS_CORNER_TOP_RIGHT].height - aTopWidth);
  27. corners.radii[NS_CORNER_BOTTOM_LEFT].width = std::max(0., corners.radii[NS_CORNER_BOTTOM_LEFT].width - aLeftWidth);
  28. corners.radii[NS_CORNER_BOTTOM_LEFT].height = std::max(0., corners.radii[NS_CORNER_BOTTOM_LEFT].height - aBottomWidth);
  29. corners.radii[NS_CORNER_BOTTOM_RIGHT].width = std::max(0., corners.radii[NS_CORNER_BOTTOM_RIGHT].width - aRightWidth);
  30. corners.radii[NS_CORNER_BOTTOM_RIGHT].height = std::max(0., corners.radii[NS_CORNER_BOTTOM_RIGHT].height - aBottomWidth);
  31. }
  32. gfxRect rect;
  33. RectCornerRadii corners;
  34. };
  35. } // namespace mozilla