OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/gfx/nine_image_painter.h" |
| 6 |
| 7 #include "ui/gfx/canvas.h" |
| 8 #include "ui/gfx/image/image_skia_operations.h" |
| 9 #include "ui/gfx/insets.h" |
| 10 #include "ui/gfx/rect.h" |
| 11 #include "ui/gfx/scoped_canvas.h" |
| 12 |
| 13 namespace gfx { |
| 14 |
| 15 NineImagePainter::NineImagePainter(const std::vector<ImageSkia>& images) { |
| 16 DCHECK_EQ(arraysize(images_), images.size()); |
| 17 for (size_t i = 0; i < arraysize(images_); ++i) |
| 18 images_[i] = images[i]; |
| 19 } |
| 20 |
| 21 NineImagePainter::NineImagePainter(const ImageSkia& image, |
| 22 const Insets& insets) { |
| 23 DCHECK_GE(image.width(), insets.width()); |
| 24 DCHECK_GE(image.height(), insets.height()); |
| 25 |
| 26 // Extract subsets of the original image to match the |images_| format. |
| 27 const int x[] = |
| 28 { 0, insets.left(), image.width() - insets.right(), image.width() }; |
| 29 const int y[] = |
| 30 { 0, insets.top(), image.height() - insets.bottom(), image.height() }; |
| 31 |
| 32 for (size_t j = 0; j < 3; ++j) { |
| 33 for (size_t i = 0; i < 3; ++i) { |
| 34 images_[i + j * 3] = ImageSkiaOperations::ExtractSubset(image, |
| 35 Rect(x[i], y[j], x[i + 1] - x[i], y[j + 1] - y[j])); |
| 36 } |
| 37 } |
| 38 } |
| 39 |
| 40 NineImagePainter::~NineImagePainter() { |
| 41 } |
| 42 |
| 43 bool NineImagePainter::IsEmpty() const { |
| 44 return images_[0].isNull(); |
| 45 } |
| 46 |
| 47 Size NineImagePainter::GetMinimumSize() const { |
| 48 return IsEmpty() ? Size() : Size( |
| 49 images_[0].width() + images_[1].width() + images_[2].width(), |
| 50 images_[0].height() + images_[3].height() + images_[6].height()); |
| 51 } |
| 52 |
| 53 void NineImagePainter::Paint(Canvas* canvas, const Rect& bounds) { |
| 54 if (IsEmpty()) |
| 55 return; |
| 56 |
| 57 ScopedCanvas scoped_canvas(canvas); |
| 58 canvas->Translate(bounds.OffsetFromOrigin()); |
| 59 |
| 60 // In case the corners and edges don't all have the same width/height, we draw |
| 61 // the center first, and extend it out in all directions to the edges of the |
| 62 // images with the smallest widths/heights. This way there will be no |
| 63 // unpainted areas, though some corners or edges might overlap the center. |
| 64 int w = bounds.width(); |
| 65 int i0w = images_[0].width(); |
| 66 int i2w = images_[2].width(); |
| 67 int i3w = images_[3].width(); |
| 68 int i5w = images_[5].width(); |
| 69 int i6w = images_[6].width(); |
| 70 int i8w = images_[8].width(); |
| 71 int i4x = std::min(std::min(i0w, i3w), i6w); |
| 72 int i4w = w - i4x - std::min(std::min(i2w, i5w), i8w); |
| 73 int h = bounds.height(); |
| 74 int i0h = images_[0].height(); |
| 75 int i1h = images_[1].height(); |
| 76 int i2h = images_[2].height(); |
| 77 int i6h = images_[6].height(); |
| 78 int i7h = images_[7].height(); |
| 79 int i8h = images_[8].height(); |
| 80 int i4y = std::min(std::min(i0h, i1h), i2h); |
| 81 int i4h = h - i4y - std::min(std::min(i6h, i7h), i8h); |
| 82 if (!images_[4].isNull()) |
| 83 Fill(canvas, images_[4], i4x, i4y, i4w, i4h); |
| 84 canvas->DrawImageInt(images_[0], 0, 0); |
| 85 Fill(canvas, images_[1], i0w, 0, w - i0w - i2w, i1h); |
| 86 canvas->DrawImageInt(images_[2], w - i2w, 0); |
| 87 Fill(canvas, images_[3], 0, i0h, i3w, h - i0h - i6h); |
| 88 Fill(canvas, images_[5], w - i5w, i2h, i5w, h - i2h - i8h); |
| 89 canvas->DrawImageInt(images_[6], 0, h - i6h); |
| 90 Fill(canvas, images_[7], i6w, h - i7h, w - i6w - i8w, i7h); |
| 91 canvas->DrawImageInt(images_[8], w - i8w, h - i8h); |
| 92 } |
| 93 |
| 94 // static |
| 95 void NineImagePainter::Fill(Canvas* c, |
| 96 const ImageSkia& i, |
| 97 int x, |
| 98 int y, |
| 99 int w, |
| 100 int h) { |
| 101 c->DrawImageInt(i, 0, 0, i.width(), i.height(), x, y, w, h, false); |
| 102 } |
| 103 |
| 104 } // namespace gfx |
OLD | NEW |