diff options
-rw-r--r-- | ui/gfx/paint_vector_icon.cc | 220 | ||||
-rw-r--r-- | ui/gfx/paint_vector_icon.h | 9 | ||||
-rw-r--r-- | ui/views/examples/examples.gyp | 1 | ||||
-rw-r--r-- | ui/views/examples/vector_example.cc | 36 |
4 files changed, 76 insertions, 190 deletions
diff --git a/ui/gfx/paint_vector_icon.cc b/ui/gfx/paint_vector_icon.cc index 0d178d8..fed63bdc 100644 --- a/ui/gfx/paint_vector_icon.cc +++ b/ui/gfx/paint_vector_icon.cc @@ -7,8 +7,6 @@ #include <map> #include "base/lazy_instance.h" -#include "base/strings/string_number_conversions.h" -#include "base/strings/string_split.h" #include "third_party/skia/include/core/SkPath.h" #include "ui/gfx/canvas.h" #include "ui/gfx/image/canvas_image_source.h" @@ -19,50 +17,84 @@ namespace gfx { namespace { -// Translates a string such as "MOVE_TO" into a command such as MOVE_TO. -CommandType CommandFromString(const std::string& source) { -#define RETURN_IF_IS(command) \ - if (source == #command) \ - return command; - - RETURN_IF_IS(MOVE_TO); - RETURN_IF_IS(R_MOVE_TO); - RETURN_IF_IS(R_LINE_TO); - RETURN_IF_IS(H_LINE_TO); - RETURN_IF_IS(R_H_LINE_TO); - RETURN_IF_IS(V_LINE_TO); - RETURN_IF_IS(R_V_LINE_TO); - RETURN_IF_IS(CUBIC_TO); - RETURN_IF_IS(R_CUBIC_TO); - RETURN_IF_IS(CIRCLE); - RETURN_IF_IS(CLOSE); - RETURN_IF_IS(END); -#undef RETURN_IF_IS - - NOTREACHED(); - return CLOSE; -} +class VectorIconSource : public CanvasImageSource { + public: + VectorIconSource(VectorIconId id, size_t dip_size, SkColor color) + : CanvasImageSource( + gfx::Size(static_cast<int>(dip_size), static_cast<int>(dip_size)), + false), + id_(id), + color_(color) {} + + ~VectorIconSource() override {} -std::vector<PathElement> PathFromSource(const std::string& source) { - std::vector<PathElement> path; - std::vector<std::string> pieces = base::SplitString( - source, "\n ,f", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); - for (const auto& piece : pieces) { - double value; - if (base::StringToDouble(piece, &value)) - path.push_back(PathElement(SkDoubleToScalar(value))); - else - path.push_back(PathElement(CommandFromString(piece))); + // CanvasImageSource: + void Draw(gfx::Canvas* canvas) override { + PaintVectorIcon(canvas, id_, size_.width(), color_); } - return path; -} -void PaintPath(Canvas* canvas, - const PathElement* path_elements, - size_t dip_size, - SkColor color) { - SkPath path; - path.setFillType(SkPath::kEvenOdd_FillType); + private: + const VectorIconId id_; + const SkColor color_; + + DISALLOW_COPY_AND_ASSIGN(VectorIconSource); +}; + +// This class caches vector icons (as ImageSkia) so they don't have to be drawn +// more than once. This also guarantees the backing data for the images returned +// by CreateVectorIcon will persist in memory until program termination. +class VectorIconCache { + public: + VectorIconCache() {} + ~VectorIconCache() {} + + ImageSkia GetOrCreateIcon(VectorIconId id, size_t dip_size, SkColor color) { + IconDescription description(id, dip_size, color); + auto iter = images_.find(description); + if (iter != images_.end()) + return iter->second; + + ImageSkia icon( + new VectorIconSource(id, dip_size, color), + gfx::Size(static_cast<int>(dip_size), static_cast<int>(dip_size))); + images_.insert(std::make_pair(description, icon)); + return icon; + } + + private: + struct IconDescription { + IconDescription(VectorIconId id, size_t dip_size, SkColor color) + : id(id), dip_size(dip_size), color(color) {} + + bool operator<(const IconDescription& other) const { + if (id != other.id) + return id < other.id; + if (dip_size != other.dip_size) + return dip_size < other.dip_size; + return color < other.color; + } + + VectorIconId id; + size_t dip_size; + SkColor color; + }; + + std::map<IconDescription, ImageSkia> images_; + + DISALLOW_COPY_AND_ASSIGN(VectorIconCache); +}; + +static base::LazyInstance<VectorIconCache> g_icon_cache = + LAZY_INSTANCE_INITIALIZER; + +} // namespace + +void PaintVectorIcon(Canvas* canvas, + VectorIconId id, + size_t dip_size, + SkColor color) { + DCHECK(VectorIconId::VECTOR_ICON_NONE != id); + const PathElement* path_elements = GetPathForVectorIcon(id); std::vector<SkPath> paths; paths.push_back(SkPath()); @@ -195,110 +227,8 @@ void PaintPath(Canvas* canvas, canvas->DrawPath(path, paint); } -class VectorIconSource : public CanvasImageSource { - public: - VectorIconSource(VectorIconId id, size_t dip_size, SkColor color) - : CanvasImageSource( - gfx::Size(static_cast<int>(dip_size), static_cast<int>(dip_size)), - false), - id_(id), - color_(color) {} - - VectorIconSource(const std::string& definition, - size_t dip_size, - SkColor color) - : CanvasImageSource( - gfx::Size(static_cast<int>(dip_size), static_cast<int>(dip_size)), - false), - id_(VectorIconId::VECTOR_ICON_NONE), - path_(PathFromSource(definition)), - color_(color) {} - - ~VectorIconSource() override {} - - // CanvasImageSource: - void Draw(gfx::Canvas* canvas) override { - if (path_.empty()) - PaintVectorIcon(canvas, id_, size_.width(), color_); - else - PaintPath(canvas, path_.data(), size_.width(), color_); - } - - private: - const VectorIconId id_; - const std::vector<PathElement> path_; - const SkColor color_; - - DISALLOW_COPY_AND_ASSIGN(VectorIconSource); -}; - -// This class caches vector icons (as ImageSkia) so they don't have to be drawn -// more than once. This also guarantees the backing data for the images returned -// by CreateVectorIcon will persist in memory until program termination. -class VectorIconCache { - public: - VectorIconCache() {} - ~VectorIconCache() {} - - ImageSkia GetOrCreateIcon(VectorIconId id, size_t dip_size, SkColor color) { - IconDescription description(id, dip_size, color); - auto iter = images_.find(description); - if (iter != images_.end()) - return iter->second; - - ImageSkia icon( - new VectorIconSource(id, dip_size, color), - gfx::Size(static_cast<int>(dip_size), static_cast<int>(dip_size))); - images_.insert(std::make_pair(description, icon)); - return icon; - } - - private: - struct IconDescription { - IconDescription(VectorIconId id, size_t dip_size, SkColor color) - : id(id), dip_size(dip_size), color(color) {} - - bool operator<(const IconDescription& other) const { - if (id != other.id) - return id < other.id; - if (dip_size != other.dip_size) - return dip_size < other.dip_size; - return color < other.color; - } - - VectorIconId id; - size_t dip_size; - SkColor color; - }; - - std::map<IconDescription, ImageSkia> images_; - - DISALLOW_COPY_AND_ASSIGN(VectorIconCache); -}; - -static base::LazyInstance<VectorIconCache> g_icon_cache = - LAZY_INSTANCE_INITIALIZER; - -} // namespace - -void PaintVectorIcon(Canvas* canvas, - VectorIconId id, - size_t dip_size, - SkColor color) { - DCHECK(VectorIconId::VECTOR_ICON_NONE != id); - PaintPath(canvas, GetPathForVectorIcon(id), dip_size, color); -} - ImageSkia CreateVectorIcon(VectorIconId id, size_t dip_size, SkColor color) { return g_icon_cache.Get().GetOrCreateIcon(id, dip_size, color); } -ImageSkia CreateVectorIconFromSource(const std::string& source, - size_t dip_size, - SkColor color) { - return ImageSkia( - new VectorIconSource(source, dip_size, color), - gfx::Size(static_cast<int>(dip_size), static_cast<int>(dip_size))); -} - } // namespace gfx diff --git a/ui/gfx/paint_vector_icon.h b/ui/gfx/paint_vector_icon.h index 51b2440..563d7cb 100644 --- a/ui/gfx/paint_vector_icon.h +++ b/ui/gfx/paint_vector_icon.h @@ -28,15 +28,6 @@ GFX_EXPORT ImageSkia CreateVectorIcon(VectorIconId id, size_t dip_size, SkColor color); -#if defined(GFX_VECTOR_ICONS_UNSAFE) || defined(GFX_IMPLEMENTATION) -// Takes a string of the format expected of .icon files and renders onto -// a canvas. This should only be used as a debugging aid and should never be -// used in production code. -GFX_EXPORT ImageSkia CreateVectorIconFromSource(const std::string& source, - size_t dip_size, - SkColor color); -#endif - } // namespace gfx #endif // UI_GFX_PAINT_VECTOR_ICON_H_ diff --git a/ui/views/examples/examples.gyp b/ui/views/examples/examples.gyp index cdf8120..56ef0b3 100644 --- a/ui/views/examples/examples.gyp +++ b/ui/views/examples/examples.gyp @@ -28,7 +28,6 @@ '../../..', ], 'defines': [ - 'GFX_VECTOR_ICONS_UNSAFE', 'VIEWS_EXAMPLES_IMPLEMENTATION', ], 'sources': [ diff --git a/ui/views/examples/vector_example.cc b/ui/views/examples/vector_example.cc index f563d50..4c72147 100644 --- a/ui/views/examples/vector_example.cc +++ b/ui/views/examples/vector_example.cc @@ -4,14 +4,10 @@ #include "ui/views/examples/vector_example.h" -#include "base/files/file_path.h" -#include "base/files/file_util.h" #include "base/strings/string_number_conversions.h" #include "base/strings/utf_string_conversions.h" #include "ui/gfx/paint_vector_icon.h" #include "ui/gfx/vector_icons_public2.h" -#include "ui/views/controls/button/blue_button.h" -#include "ui/views/controls/button/button.h" #include "ui/views/controls/image_view.h" #include "ui/views/controls/textfield/textfield.h" #include "ui/views/controls/textfield/textfield_controller.h" @@ -24,16 +20,12 @@ namespace examples { namespace { -class VectorIconGallery : public View, - public TextfieldController, - public ButtonListener { +class VectorIconGallery : public View, public TextfieldController { public: VectorIconGallery() : image_view_(new ImageView()), size_input_(new Textfield()), color_input_(new Textfield()), - file_chooser_(new Textfield()), - file_go_button_(new BlueButton(this, base::ASCIIToUTF16("Render"))), vector_id_(0), size_(32), color_(SK_ColorRED) { @@ -41,16 +33,6 @@ class VectorIconGallery : public View, AddChildView(color_input_); AddChildView(image_view_); - file_chooser_->set_placeholder_text( - base::ASCIIToUTF16("Or enter a file to read")); - View* file_container = new View(); - BoxLayout* file_box = new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10); - file_container->SetLayoutManager(file_box); - file_container->AddChildView(file_chooser_); - file_container->AddChildView(file_go_button_); - file_box->SetFlexForView(file_chooser_, 1); - AddChildView(file_container); - size_input_->set_placeholder_text(base::ASCIIToUTF16("Size in dip")); size_input_->set_controller(this); color_input_->set_placeholder_text(base::ASCIIToUTF16("Color (AARRGGBB)")); @@ -98,20 +80,6 @@ class VectorIconGallery : public View, } } - // ButtonListener - void ButtonPressed(Button* sender, const ui::Event& event) override { - DCHECK_EQ(file_go_button_, sender); - std::string contents; -#if defined(OS_POSIX) - base::FilePath path(base::UTF16ToUTF8(file_chooser_->text())); -#elif defined(OS_WIN) - base::FilePath path(file_chooser_->text()); -#endif - base::ReadFileToString(path, &contents); - image_view_->SetImage( - gfx::CreateVectorIconFromSource(contents, size_, color_)); - } - void UpdateImage() { image_view_->SetImage(gfx::CreateVectorIcon( static_cast<gfx::VectorIconId>(vector_id_), size_, color_)); @@ -121,8 +89,6 @@ class VectorIconGallery : public View, ImageView* image_view_; Textfield* size_input_; Textfield* color_input_; - Textfield* file_chooser_; - Button* file_go_button_; int vector_id_; size_t size_; |