This source file includes following definitions.
- GetScaledAvatarHeightForWidth
- border_
- Draw
- GetSizedAvatarIconWithBorder
- GetAvatarIconForMenu
- GetAvatarIconForWebUI
- GetAvatarIconForTitleBar
#include "chrome/browser/profiles/profile_info_util.h"
#include "base/memory/scoped_ptr.h"
#include "third_party/skia/include/core/SkPaint.h"
#include "third_party/skia/include/core/SkPath.h"
#include "third_party/skia/include/core/SkScalar.h"
#include "third_party/skia/include/core/SkXfermode.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/image/canvas_image_source.h"
#include "ui/gfx/image/image_skia_operations.h"
namespace {
int GetScaledAvatarHeightForWidth(int width, const gfx::ImageSkia& avatar) {
int scaled_height = width *
((float) avatar.height() / (float) avatar.width()) + 0.5f;
return scaled_height;
}
class AvatarImageSource : public gfx::CanvasImageSource {
public:
enum AvatarPosition {
POSITION_CENTER,
POSITION_BOTTOM_CENTER,
};
enum AvatarBorder {
BORDER_NONE,
BORDER_NORMAL,
BORDER_ETCHED,
};
AvatarImageSource(gfx::ImageSkia avatar,
const gfx::Size& canvas_size,
int width,
AvatarPosition position,
AvatarBorder border);
virtual ~AvatarImageSource();
virtual void Draw(gfx::Canvas* canvas) OVERRIDE;
private:
gfx::ImageSkia avatar_;
const gfx::Size canvas_size_;
const int width_;
const int height_;
const AvatarPosition position_;
const AvatarBorder border_;
DISALLOW_COPY_AND_ASSIGN(AvatarImageSource);
};
AvatarImageSource::AvatarImageSource(gfx::ImageSkia avatar,
const gfx::Size& canvas_size,
int width,
AvatarPosition position,
AvatarBorder border)
: gfx::CanvasImageSource(canvas_size, false),
canvas_size_(canvas_size),
width_(width - profiles::kAvatarIconPadding),
height_(GetScaledAvatarHeightForWidth(width, avatar) -
profiles::kAvatarIconPadding),
position_(position),
border_(border) {
avatar_ = gfx::ImageSkiaOperations::CreateResizedImage(
avatar, skia::ImageOperations::RESIZE_BEST,
gfx::Size(width_, height_));
}
AvatarImageSource::~AvatarImageSource() {
}
void AvatarImageSource::Draw(gfx::Canvas* canvas) {
int x = (canvas_size_.width() - width_) / 2;
int y;
if (position_ == POSITION_CENTER) {
y = (canvas_size_.height() - height_) / 2;
} else {
y = canvas_size_.height() - height_ - 1;
}
canvas->DrawImageInt(avatar_, x, y);
int border_size = std::max(width_, height_);
x = (canvas_size_.width() - border_size) / 2;
y = (canvas_size_.height() - border_size) / 2;
if (border_ == BORDER_NORMAL) {
SkColor border_color = SkColorSetARGB(83, 0, 0, 0);
SkPath path;
path.addRect(SkFloatToScalar(x + 0.5f),
SkFloatToScalar(y + 0.5f),
SkFloatToScalar(x + border_size - 0.5f),
SkFloatToScalar(y + border_size - 0.5f));
SkPaint paint;
paint.setColor(border_color);
paint.setStyle(SkPaint::kStroke_Style);
paint.setStrokeWidth(SkIntToScalar(1));
canvas->DrawPath(path, paint);
} else if (border_ == BORDER_ETCHED) {
SkColor shadow_color = SkColorSetARGB(83, 0, 0, 0);
SkColor highlight_color = SkColorSetARGB(96, 255, 255, 255);
SkPaint paint;
paint.setStyle(SkPaint::kStroke_Style);
paint.setStrokeWidth(SkIntToScalar(1));
SkPath path;
path.moveTo(SkFloatToScalar(x + 0.5f), SkIntToScalar(y + height_));
path.rLineTo(SkIntToScalar(0), SkFloatToScalar(-height_ + 0.5f));
path.rLineTo(SkFloatToScalar(width_ - 0.5f), SkIntToScalar(0));
paint.setColor(shadow_color);
canvas->DrawPath(path, paint);
path.reset();
path.moveTo(SkIntToScalar(x + 1), SkFloatToScalar(y + height_ - 0.5f));
path.rLineTo(SkFloatToScalar(width_ - 1.5f), SkIntToScalar(0));
path.rLineTo(SkIntToScalar(0), SkFloatToScalar(-height_ + 1.5f));
paint.setColor(highlight_color);
canvas->DrawPath(path, paint);
}
}
}
namespace profiles {
const int kAvatarIconWidth = 38;
const int kAvatarIconHeight = 31;
const int kAvatarIconPadding = 2;
gfx::Image GetSizedAvatarIconWithBorder(const gfx::Image& image,
bool is_rectangle,
int width, int height) {
if (!is_rectangle)
return image;
gfx::Size size(width, height);
scoped_ptr<gfx::ImageSkiaSource> source(
new AvatarImageSource(
*image.ToImageSkia(),
size,
std::min(width, height),
AvatarImageSource::POSITION_CENTER,
AvatarImageSource::BORDER_NORMAL));
return gfx::Image(gfx::ImageSkia(source.release(), size));
}
gfx::Image GetAvatarIconForMenu(const gfx::Image& image,
bool is_rectangle) {
return GetSizedAvatarIconWithBorder(
image, is_rectangle, kAvatarIconWidth, kAvatarIconHeight);
}
gfx::Image GetAvatarIconForWebUI(const gfx::Image& image,
bool is_rectangle) {
if (!is_rectangle)
return image;
gfx::Size size(kAvatarIconWidth, kAvatarIconHeight);
scoped_ptr<gfx::ImageSkiaSource> source(
new AvatarImageSource(
*image.ToImageSkia(),
size,
std::min(kAvatarIconWidth, kAvatarIconHeight),
AvatarImageSource::POSITION_CENTER,
AvatarImageSource::BORDER_NONE));
return gfx::Image(gfx::ImageSkia(source.release(), size));
}
gfx::Image GetAvatarIconForTitleBar(const gfx::Image& image,
bool is_rectangle,
int dst_width,
int dst_height) {
if (!is_rectangle)
return image;
int size = std::min(std::min(kAvatarIconWidth, kAvatarIconHeight),
std::min(dst_width, dst_height));
gfx::Size dst_size(dst_width, dst_height);
scoped_ptr<gfx::ImageSkiaSource> source(
new AvatarImageSource(
*image.ToImageSkia(),
dst_size,
size,
AvatarImageSource::POSITION_BOTTOM_CENTER,
AvatarImageSource::BORDER_ETCHED));
return gfx::Image(gfx::ImageSkia(source.release(), dst_size));
}
}