1 // Copyright 2016 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 #ifndef UI_GFX_GEOMETRY_MOJO_GEOMETRY_STRUCT_TRAITS_H_ 6 #define UI_GFX_GEOMETRY_MOJO_GEOMETRY_STRUCT_TRAITS_H_ 7 8 #include "ui/gfx/geometry/insets.h" 9 #include "ui/gfx/geometry/insets_f.h" 10 #include "ui/gfx/geometry/mojo/geometry.mojom-shared.h" 11 #include "ui/gfx/geometry/point.h" 12 #include "ui/gfx/geometry/point_f.h" 13 #include "ui/gfx/geometry/rect.h" 14 #include "ui/gfx/geometry/rect_f.h" 15 #include "ui/gfx/geometry/size.h" 16 #include "ui/gfx/geometry/size_f.h" 17 #include "ui/gfx/geometry/vector2d.h" 18 #include "ui/gfx/geometry/vector2d_f.h" 19 20 namespace mojo { 21 22 template <> 23 struct StructTraits<gfx::mojom::InsetsDataView, gfx::Insets> { 24 static int top(const gfx::Insets& p) { return p.top(); } 25 static int left(const gfx::Insets& p) { return p.left(); } 26 static int bottom(const gfx::Insets& p) { return p.bottom(); } 27 static int right(const gfx::Insets& p) { return p.right(); } 28 static bool Read(gfx::mojom::InsetsDataView data, gfx::Insets* out) { 29 out->Set(data.top(), data.left(), data.bottom(), data.right()); 30 return true; 31 } 32 }; 33 34 template <> 35 struct StructTraits<gfx::mojom::InsetsFDataView, gfx::InsetsF> { 36 static float top(const gfx::InsetsF& p) { return p.top(); } 37 static float left(const gfx::InsetsF& p) { return p.left(); } 38 static float bottom(const gfx::InsetsF& p) { return p.bottom(); } 39 static float right(const gfx::InsetsF& p) { return p.right(); } 40 static bool Read(gfx::mojom::InsetsFDataView data, gfx::InsetsF* out) { 41 out->Set(data.top(), data.left(), data.bottom(), data.right()); 42 return true; 43 } 44 }; 45 46 template <> 47 struct StructTraits<gfx::mojom::PointDataView, gfx::Point> { 48 static int x(const gfx::Point& p) { return p.x(); } 49 static int y(const gfx::Point& p) { return p.y(); } 50 static bool Read(gfx::mojom::PointDataView data, gfx::Point* out) { 51 out->SetPoint(data.x(), data.y()); 52 return true; 53 } 54 }; 55 56 template <> 57 struct StructTraits<gfx::mojom::PointFDataView, gfx::PointF> { 58 static float x(const gfx::PointF& p) { return p.x(); } 59 static float y(const gfx::PointF& p) { return p.y(); } 60 static bool Read(gfx::mojom::PointFDataView data, gfx::PointF* out) { 61 out->SetPoint(data.x(), data.y()); 62 return true; 63 } 64 }; 65 66 template <> 67 struct StructTraits<gfx::mojom::RectDataView, gfx::Rect> { 68 static int x(const gfx::Rect& p) { return p.x(); } 69 static int y(const gfx::Rect& p) { return p.y(); } 70 static int width(const gfx::Rect& p) { return p.width(); } 71 static int height(const gfx::Rect& p) { return p.height(); } 72 static bool Read(gfx::mojom::RectDataView data, gfx::Rect* out) { 73 if (data.width() < 0 || data.height() < 0) 74 return false; 75 76 out->SetRect(data.x(), data.y(), data.width(), data.height()); 77 return true; 78 } 79 }; 80 81 template <> 82 struct StructTraits<gfx::mojom::RectFDataView, gfx::RectF> { 83 static float x(const gfx::RectF& p) { return p.x(); } 84 static float y(const gfx::RectF& p) { return p.y(); } 85 static float width(const gfx::RectF& p) { return p.width(); } 86 static float height(const gfx::RectF& p) { return p.height(); } 87 static bool Read(gfx::mojom::RectFDataView data, gfx::RectF* out) { 88 if (data.width() < 0 || data.height() < 0) 89 return false; 90 91 out->SetRect(data.x(), data.y(), data.width(), data.height()); 92 return true; 93 } 94 }; 95 96 template <> 97 struct StructTraits<gfx::mojom::SizeDataView, gfx::Size> { 98 static int width(const gfx::Size& p) { return p.width(); } 99 static int height(const gfx::Size& p) { return p.height(); } 100 static bool Read(gfx::mojom::SizeDataView data, gfx::Size* out) { 101 if (data.width() < 0 || data.height() < 0) 102 return false; 103 104 out->SetSize(data.width(), data.height()); 105 return true; 106 } 107 }; 108 109 template <> 110 struct StructTraits<gfx::mojom::SizeFDataView, gfx::SizeF> { 111 static float width(const gfx::SizeF& p) { return p.width(); } 112 static float height(const gfx::SizeF& p) { return p.height(); } 113 static bool Read(gfx::mojom::SizeFDataView data, gfx::SizeF* out) { 114 if (data.width() < 0 || data.height() < 0) 115 return false; 116 117 out->SetSize(data.width(), data.height()); 118 return true; 119 } 120 }; 121 122 template <> 123 struct StructTraits<gfx::mojom::Vector2dDataView, gfx::Vector2d> { 124 static int x(const gfx::Vector2d& v) { return v.x(); } 125 static int y(const gfx::Vector2d& v) { return v.y(); } 126 static bool Read(gfx::mojom::Vector2dDataView data, gfx::Vector2d* out) { 127 out->set_x(data.x()); 128 out->set_y(data.y()); 129 return true; 130 } 131 }; 132 133 template <> 134 struct StructTraits<gfx::mojom::Vector2dFDataView, gfx::Vector2dF> { 135 static float x(const gfx::Vector2dF& v) { return v.x(); } 136 static float y(const gfx::Vector2dF& v) { return v.y(); } 137 static bool Read(gfx::mojom::Vector2dFDataView data, gfx::Vector2dF* out) { 138 out->set_x(data.x()); 139 out->set_y(data.y()); 140 return true; 141 } 142 }; 143 144 } // namespace mojo 145 146 #endif // UI_GFX_GEOMETRY_MOJO_GEOMETRY_STRUCT_TRAITS_H_ 147