base: Change DCHECK_IS_ON to a macro DCHECK_IS_ON().
[chromium-blink-merge.git] / ui / gfx / image / image_family.h
blobc092b2a87ca521a4ff78678581fc332b3bfed8fa
1 // Copyright 2013 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.
5 #ifndef UI_GFX_IMAGE_IMAGE_FAMILY_H_
6 #define UI_GFX_IMAGE_IMAGE_FAMILY_H_
8 #include <iterator>
9 #include <map>
10 #include <utility>
12 #include "ui/gfx/gfx_export.h"
13 #include "ui/gfx/image/image.h"
15 namespace gfx {
16 class ImageSkia;
17 class Size;
19 // A collection of images at different sizes. The images should be different
20 // representations of the same basic concept (for example, an icon) at various
21 // sizes and (optionally) aspect ratios. A method is provided for finding the
22 // most appropriate image to fit in a given rectangle.
24 // NOTE: This is not appropriate for storing an image at a single logical pixel
25 // size, with high-DPI bitmap versions; use an Image or ImageSkia for that. Each
26 // image in an ImageFamily should have a different logical size (and may also
27 // include high-DPI representations).
28 class GFX_EXPORT ImageFamily {
29 private:
30 // An <aspect ratio, DIP width> pair.
31 // A 0x0 image has aspect ratio 1.0. 0xN and Nx0 images are treated as 0x0.
32 struct MapKey : std::pair<float, int> {
33 MapKey(float aspect, int width)
34 : std::pair<float, int>(aspect, width) {}
36 float aspect() const { return first; }
38 int width() const { return second; }
41 public:
42 // Type for iterating over all images in the family, in order.
43 // Dereferencing this iterator returns a gfx::Image.
44 class GFX_EXPORT const_iterator :
45 std::iterator<std::bidirectional_iterator_tag, const gfx::Image> {
46 public:
47 const_iterator();
49 const_iterator(const const_iterator& other);
51 ~const_iterator();
53 const_iterator& operator++() {
54 ++map_iterator_;
55 return *this;
58 const_iterator operator++(int /*unused*/) {
59 const_iterator result(*this);
60 ++(*this);
61 return result;
64 const_iterator& operator--() {
65 --map_iterator_;
66 return *this;
69 const_iterator operator--(int /*unused*/) {
70 const_iterator result(*this);
71 --(*this);
72 return result;
75 bool operator==(const const_iterator& other) const {
76 return map_iterator_ == other.map_iterator_;
79 bool operator!=(const const_iterator& other) const {
80 return map_iterator_ != other.map_iterator_;
83 const gfx::Image& operator*() const {
84 return map_iterator_->second;
87 const gfx::Image* operator->() const {
88 return &**this;
91 private:
92 friend class ImageFamily;
94 explicit const_iterator(
95 const std::map<MapKey, gfx::Image>::const_iterator& other);
97 std::map<MapKey, gfx::Image>::const_iterator map_iterator_;
100 ImageFamily();
101 ~ImageFamily();
103 // Gets an iterator to the first image.
104 const_iterator begin() const { return const_iterator(map_.begin()); }
105 // Gets an iterator to one after the last image.
106 const_iterator end() const { return const_iterator(map_.end()); }
108 // Determines whether the image family has no images in it.
109 bool empty() const { return map_.empty(); }
111 // Removes all images from the family.
112 void clear() { return map_.clear(); }
114 // Adds an image to the family. If another image is already present at the
115 // same size, it will be overwritten.
116 void Add(const gfx::Image& image);
118 // Adds an image to the family. If another image is already present at the
119 // same size, it will be overwritten.
120 void Add(const gfx::ImageSkia& image_skia);
122 // Gets the best image to use in a rectangle of |width|x|height|.
123 // Gets an image at the same aspect ratio as |width|:|height|, if possible, or
124 // if not, the closest aspect ratio. Among images of that aspect ratio,
125 // returns the smallest image with both its width and height bigger or equal
126 // to the requested size. If none exists, returns the largest image of that
127 // aspect ratio. If there are no images in the family, returns NULL.
128 const gfx::Image* GetBest(int width, int height) const;
130 // Gets the best image to use in a rectangle of |size|.
131 // Gets an image at the same aspect ratio as |size.width()|:|size.height()|,
132 // if possible, or if not, the closest aspect ratio. Among images of that
133 // aspect ratio, returns the smallest image with both its width and height
134 // bigger or equal to the requested size. If none exists, returns the largest
135 // image of that aspect ratio. If there are no images in the family, returns
136 // NULL.
137 const gfx::Image* GetBest(const gfx::Size& size) const;
139 private:
140 // Find the closest aspect ratio in the map to |desired_aspect|.
141 // Ties are broken by the thinner aspect.
142 // |map_| must not be empty. |desired_aspect| must be > 0.0.
143 float GetClosestAspect(float desired_aspect) const;
145 // Gets an image with aspect ratio |aspect|, at the best size for |width|.
146 // Returns the smallest image of aspect ratio |aspect| with its width bigger
147 // or equal to |width|. If none exists, returns the largest image of aspect
148 // ratio |aspect|. Behavior is undefined if there is not at least one image in
149 // |map_| of aspect ratio |aspect|.
150 const gfx::Image* GetWithExactAspect(float aspect, int width) const;
152 // Map from (aspect ratio, width) to image.
153 std::map<MapKey, gfx::Image> map_;
156 } // namespace gfx
158 #endif // UI_GFX_IMAGE_IMAGE_FAMILY_H_