Home | History | Annotate | Download | only in cursor
      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/base/cursor/cursor_util.h"
      6 
      7 #include "base/logging.h"
      8 #include "skia/ext/image_operations.h"
      9 #include "ui/gfx/point_conversions.h"
     10 #include "ui/gfx/size_conversions.h"
     11 #include "ui/gfx/skbitmap_operations.h"
     12 #include "ui/gfx/skia_util.h"
     13 
     14 namespace ui {
     15 
     16 void ScaleAndRotateCursorBitmapAndHotpoint(float scale,
     17                                            gfx::Display::Rotation rotation,
     18                                            SkBitmap* bitmap,
     19                                            gfx::Point* hotpoint) {
     20   switch (rotation) {
     21     case gfx::Display::ROTATE_0:
     22       break;
     23     case gfx::Display::ROTATE_90:
     24       hotpoint->SetPoint(bitmap->height() - hotpoint->y(), hotpoint->x());
     25       *bitmap = SkBitmapOperations::Rotate(
     26           *bitmap, SkBitmapOperations::ROTATION_90_CW);
     27       break;
     28     case gfx::Display::ROTATE_180:
     29       hotpoint->SetPoint(
     30           bitmap->width() - hotpoint->x(), bitmap->height() - hotpoint->y());
     31       *bitmap = SkBitmapOperations::Rotate(
     32           *bitmap, SkBitmapOperations::ROTATION_180_CW);
     33       break;
     34     case gfx::Display::ROTATE_270:
     35       hotpoint->SetPoint(hotpoint->y(), bitmap->width() - hotpoint->x());
     36       *bitmap = SkBitmapOperations::Rotate(
     37           *bitmap, SkBitmapOperations::ROTATION_270_CW);
     38       break;
     39   }
     40 
     41   if (scale < FLT_EPSILON) {
     42     NOTREACHED() << "Scale must be larger than 0.";
     43     scale = 1.0f;
     44   }
     45 
     46   if (scale == 1.0f)
     47     return;
     48 
     49   gfx::Size scaled_size = gfx::ToFlooredSize(
     50       gfx::ScaleSize(gfx::Size(bitmap->width(), bitmap->height()), scale));
     51 
     52   *bitmap = skia::ImageOperations::Resize(
     53       *bitmap,
     54       skia::ImageOperations::RESIZE_BETTER,
     55       scaled_size.width(),
     56       scaled_size.height());
     57   *hotpoint = gfx::ToFlooredPoint(gfx::ScalePoint(*hotpoint, scale));
     58 }
     59 
     60 }  // namespace ui
     61