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/gfx/geometry/dip_util.h"
6
7 #include "ui/gfx/geometry/insets.h"
8 #include "ui/gfx/geometry/point.h"
9 #include "ui/gfx/geometry/point_conversions.h"
10 #include "ui/gfx/geometry/point_f.h"
11 #include "ui/gfx/geometry/rect.h"
12 #include "ui/gfx/geometry/rect_conversions.h"
13 #include "ui/gfx/geometry/size.h"
14 #include "ui/gfx/geometry/size_conversions.h"
15
16 namespace gfx {
17
ConvertInsetsToDIP(float scale_factor,const gfx::Insets & insets_in_pixel)18 Insets ConvertInsetsToDIP(float scale_factor,
19 const gfx::Insets& insets_in_pixel) {
20 if (scale_factor == 1.f)
21 return insets_in_pixel;
22 return insets_in_pixel.Scale(1.f / scale_factor);
23 }
24
ConvertPointToDIP(float scale_factor,const Point & point_in_pixel)25 Point ConvertPointToDIP(float scale_factor, const Point& point_in_pixel) {
26 if (scale_factor == 1.f)
27 return point_in_pixel;
28 return ScaleToFlooredPoint(point_in_pixel, 1.f / scale_factor);
29 }
30
ConvertPointToDIP(float scale_factor,const PointF & point_in_pixel)31 PointF ConvertPointToDIP(float scale_factor, const PointF& point_in_pixel) {
32 if (scale_factor == 1.f)
33 return point_in_pixel;
34 return ScalePoint(point_in_pixel, 1.f / scale_factor);
35 }
36
ConvertSizeToDIP(float scale_factor,const Size & size_in_pixel)37 Size ConvertSizeToDIP(float scale_factor, const Size& size_in_pixel) {
38 if (scale_factor == 1.f)
39 return size_in_pixel;
40 return ScaleToFlooredSize(size_in_pixel, 1.f / scale_factor);
41 }
42
ConvertRectToDIP(float scale_factor,const Rect & rect_in_pixel)43 Rect ConvertRectToDIP(float scale_factor, const Rect& rect_in_pixel) {
44 if (scale_factor == 1.f)
45 return rect_in_pixel;
46 return ToFlooredRectDeprecated(
47 ScaleRect(RectF(rect_in_pixel), 1.f / scale_factor));
48 }
49
ConvertInsetsToPixel(float scale_factor,const gfx::Insets & insets_in_dip)50 Insets ConvertInsetsToPixel(float scale_factor,
51 const gfx::Insets& insets_in_dip) {
52 if (scale_factor == 1.f)
53 return insets_in_dip;
54 return insets_in_dip.Scale(scale_factor);
55 }
56
ConvertPointToPixel(float scale_factor,const Point & point_in_dip)57 Point ConvertPointToPixel(float scale_factor, const Point& point_in_dip) {
58 if (scale_factor == 1.f)
59 return point_in_dip;
60 return ScaleToFlooredPoint(point_in_dip, scale_factor);
61 }
62
ConvertPointToPixel(float scale_factor,const PointF & point_in_dip)63 PointF ConvertPointToPixel(float scale_factor, const PointF& point_in_dip) {
64 if (scale_factor == 1.f)
65 return point_in_dip;
66 return ScalePoint(point_in_dip, scale_factor);
67 }
68
ConvertSizeToPixel(float scale_factor,const Size & size_in_dip)69 Size ConvertSizeToPixel(float scale_factor, const Size& size_in_dip) {
70 if (scale_factor == 1.f)
71 return size_in_dip;
72 return ScaleToFlooredSize(size_in_dip, scale_factor);
73 }
74
ConvertRectToPixel(float scale_factor,const Rect & rect_in_dip)75 Rect ConvertRectToPixel(float scale_factor, const Rect& rect_in_dip) {
76 // Use ToEnclosingRect() to ensure we paint all the possible pixels
77 // touched. ToEnclosingRect() floors the origin, and ceils the max
78 // coordinate. To do otherwise (such as flooring the size) potentially
79 // results in rounding down and not drawing all the pixels that are
80 // touched.
81 if (scale_factor == 1.f)
82 return rect_in_dip;
83 return ToEnclosingRect(
84 RectF(ScalePoint(gfx::PointF(rect_in_dip.origin()), scale_factor),
85 ScaleSize(gfx::SizeF(rect_in_dip.size()), scale_factor)));
86 }
87
88 } // namespace gfx
89