Commit bcb1fa0f authored by Matthew Mourgos's avatar Matthew Mourgos Committed by Commit Bot

CrOS: Add padding to make standard icons square.

This change checks whether images to be standardized have a height equal
to their width, and if needed, adds padding to ensure the width and
height of the image are equal.

Bug: 1110496
Change-Id: I2879c0ad821074a30b7a983ae89541a7aade613d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2336636Reviewed-by: default avatarNancy Wang <nancylingwang@chromium.org>
Commit-Queue: Matthew Mourgos <mmourgos@chromium.org>
Cr-Commit-Position: refs/heads/master@{#794724}
parent d4a9f377
...@@ -21,6 +21,8 @@ constexpr float kInsideCircleDifferenceThreshold = 0.005f; ...@@ -21,6 +21,8 @@ constexpr float kInsideCircleDifferenceThreshold = 0.005f;
constexpr float kIconScaleToFit = 0.9f; constexpr float kIconScaleToFit = 0.9f;
constexpr float kBackgroundCirclePaddingRatio = 0.01f;
// Returns the bounding rect for the opaque part of the icon. // Returns the bounding rect for the opaque part of the icon.
gfx::Rect GetVisibleIconBounds(const SkBitmap& bitmap) { gfx::Rect GetVisibleIconBounds(const SkBitmap& bitmap) {
const SkPixmap pixmap = bitmap.pixmap(); const SkPixmap pixmap = bitmap.pixmap();
...@@ -251,18 +253,49 @@ bool CanVisibleIconFitInCircle(const gfx::ImageSkia& image) { ...@@ -251,18 +253,49 @@ bool CanVisibleIconFitInCircle(const gfx::ImageSkia& image) {
return can_icon_fit_in_circle; return can_icon_fit_in_circle;
} }
// Returns an image with equal width and height. If necessary, padding is
// added to ensure the width and height are equal.
gfx::ImageSkia StandardizeSize(const gfx::ImageSkia& image) {
gfx::ImageSkia final_image;
for (gfx::ImageSkiaRep rep : image.image_reps()) {
SkBitmap unscaled_bitmap(rep.GetBitmap());
int width = unscaled_bitmap.width();
int height = unscaled_bitmap.height();
if (width == height)
return image;
int longest_side = std::max(width, height);
SkBitmap final_bitmap;
final_bitmap.allocN32Pixels(longest_side, longest_side);
final_bitmap.eraseColor(SK_ColorTRANSPARENT);
SkCanvas canvas(final_bitmap);
canvas.drawBitmap(unscaled_bitmap, (longest_side - width) / 2,
(longest_side - height) / 2);
final_image.AddRepresentation(gfx::ImageSkiaRep(final_bitmap, rep.scale()));
}
return final_image;
}
} // namespace } // namespace
gfx::ImageSkia CreateStandardIconImage(const gfx::ImageSkia& image) { gfx::ImageSkia CreateStandardIconImage(const gfx::ImageSkia& image) {
gfx::ImageSkia final_image; gfx::ImageSkia final_image;
gfx::ImageSkia standard_size_image = app_list::StandardizeSize(image);
// If icon is already circle shaped, then return the original image. // If icon is already circle shaped, then return the original image.
if (IsIconCircleShaped(image)) if (IsIconCircleShaped(standard_size_image))
return image; return standard_size_image;
bool visible_icon_fits_in_circle = CanVisibleIconFitInCircle(image); bool visible_icon_fits_in_circle =
CanVisibleIconFitInCircle(standard_size_image);
for (gfx::ImageSkiaRep rep : image.image_reps()) { for (gfx::ImageSkiaRep rep : standard_size_image.image_reps()) {
SkBitmap unscaled_bitmap(rep.GetBitmap()); SkBitmap unscaled_bitmap(rep.GetBitmap());
int width = unscaled_bitmap.width(); int width = unscaled_bitmap.width();
int height = unscaled_bitmap.height(); int height = unscaled_bitmap.height();
...@@ -282,8 +315,10 @@ gfx::ImageSkia CreateStandardIconImage(const gfx::ImageSkia& image) { ...@@ -282,8 +315,10 @@ gfx::ImageSkia CreateStandardIconImage(const gfx::ImageSkia& image) {
float circle_diameter = width; float circle_diameter = width;
// Draw the background circle. // Draw the background circle.
canvas.drawCircle(SkPoint::Make((width - 1) / 2.0f, (height - 1) / 2.0f), canvas.drawCircle(
circle_diameter / 2.0f - 1, paint_background_circle); SkPoint::Make((width - 1) / 2.0f, (height - 1) / 2.0f),
circle_diameter / 2.0f - width * kBackgroundCirclePaddingRatio,
paint_background_circle);
gfx::Rect visible_icon_rect = GetVisibleIconBounds(unscaled_bitmap); gfx::Rect visible_icon_rect = GetVisibleIconBounds(unscaled_bitmap);
float visible_icon_diagonal = float visible_icon_diagonal =
......
...@@ -36,7 +36,7 @@ TEST_F(CreateStandardIconTest, SquareIconToStandardIcon) { ...@@ -36,7 +36,7 @@ TEST_F(CreateStandardIconTest, SquareIconToStandardIcon) {
paint_background_circle.setStyle(SkPaint::kFill_Style); paint_background_circle.setStyle(SkPaint::kFill_Style);
canvas.drawCircle( canvas.drawCircle(
SkPoint::Make((test_width - 1) / 2.0f, (test_width - 1) / 2.0f), SkPoint::Make((test_width - 1) / 2.0f, (test_width - 1) / 2.0f),
test_width / 2.0f - 1, paint_background_circle); test_width / 2.0f - test_width * 0.01f, paint_background_circle);
const SkBitmap scaled_bitmap = skia::ImageOperations::Resize( const SkBitmap scaled_bitmap = skia::ImageOperations::Resize(
square_icon_bitmap, skia::ImageOperations::RESIZE_BEST, 41, 41); square_icon_bitmap, skia::ImageOperations::RESIZE_BEST, 41, 41);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment