1 // Copyright 2015 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.
5 #ifndef RTL_GEOMETRY_H_
6 #define RTL_GEOMETRY_H_
8 #include <CoreGraphics/CoreGraphics.h>
9 #import <UIKit/UIKit.h>
11 #include "base/i18n/rtl.h"
13 // Utilities for direction-independent layout calculations and related
16 // True if views should be laid out with full RTL mirroring.
19 // RIGHT_TO_LEFT if UseRTLLayout(), otherwise LEFT_TO_RIGHT.
20 base::i18n::TextDirection
LayoutDirection();
22 // A LayoutRectPosition contains the information needed to position a CGRect,
23 // optionally flipping across its bounding coordinate space's midpoint Y axis.
24 // |leading| is the distance from the leading edge at which the resulting rect
25 // should be laid out; in LTR this will be the x-origin, in RTL it will be used
26 // to compute the x-origin. |originY| is used to position the rect vertically.
27 struct LayoutRectPosition
{
32 // The null LayoutRectPosition, with |leading| and |originY| equal to 0.0.
33 extern const LayoutRectPosition LayoutRectPositionZero
;
35 // Returns a new LayoutRectPosition with the passed-in values.
36 LayoutRectPosition
LayoutRectPositionMake(CGFloat leading
, CGFloat originY
);
38 // Returns YES if |a|'s values are equal to those of |b|.
39 BOOL
LayoutRectPositionEqualToPosition(LayoutRectPosition a
,
40 LayoutRectPosition b
);
42 // Returns a new LayoutRectPosition created by aligning |position|'s values to
43 // the nearest pixel boundary.
44 LayoutRectPosition
AlignLayoutRectPositionToPixel(LayoutRectPosition position
);
46 // A LayoutRect contains the information needed to generate a CGRect that may or
47 // may not be flipped if positioned in RTL or LTR contexts. |boundingWidth| is
48 // the width of the bounding coordinate space in which the resulting rect will
49 // be used. |position| is used to describe the location of the resulting frame,
50 // and |size| is the size of resulting frame.
52 CGFloat boundingWidth
;
53 LayoutRectPosition position
;
57 // The null LayoutRect, with leading, boundingWidth and originY of 0.0, and
58 // a size of CGSizeZero.
59 extern const LayoutRect LayoutRectZero
;
61 // Returns a new LayoutRect; |height| and |width| are used to construct the
63 LayoutRect
LayoutRectMake(CGFloat leading
,
64 CGFloat boundingWidth
,
69 // Returns YES if |a|'s values are equal to those of |b|.
70 BOOL
LayoutRectEqualToRect(LayoutRect a
, LayoutRect b
);
72 // Given |layout|, returns the rect for that layout in text direction
74 CGRect
LayoutRectGetRectUsingDirection(LayoutRect layout
,
75 base::i18n::TextDirection direction
);
76 // As above, using |direction| == RIGHT_TO_LEFT if UseRTLLayout(), LEFT_TO_RIGHT
78 CGRect
LayoutRectGetRect(LayoutRect layout
);
80 // Utilities for getting CALayer positioning values from a layoutRect.
81 // Given |layout|, return the bounds rectangle of the generated rect -- that is,
82 // a rect with origin (0,0) and size equal to |layout|'s size.
83 CGRect
LayoutRectGetBoundsRect(LayoutRect layout
);
85 // Given |layout| and some anchor point |anchor| (defined in the way that
86 // CALayer's anchorPoint property is), return the CGPoint that defines the
87 // position of a rect in the context used by |layout|.
88 CGPoint
LayoutRectGetPositionForAnchorUsingDirection(
91 base::i18n::TextDirection direction
);
93 // As above, using |direction| == RIGHT_TO_LEFT if UseRTLLayout(), LEFT_TO_RIGHT
95 CGPoint
LayoutRectGetPositionForAnchor(LayoutRect layout
, CGPoint anchor
);
97 // Given |rect|, a rect, and |boundingRect|, a rect whose bounds are the
98 // context in which |rect|'s frame is interpreted, return the layout that
99 // defines |rect|, assuming |direction| is the direction |rect| was positioned
101 LayoutRect
LayoutRectForRectInBoundingRectUsingDirection(
104 base::i18n::TextDirection direction
);
106 // As above, using |direction| == RIGHT_TO_LEFT if UseRTLLayout(), LEFT_TO_RIGHT
108 LayoutRect
LayoutRectForRectInBoundingRect(CGRect rect
, CGRect boundingRect
);
110 // Given a layout |layout|, return the layout that defines the leading area up
112 LayoutRect
LayoutRectGetLeadingLayout(LayoutRect layout
);
114 // Given a layout |layout|, return the layout that defines the trailing area
116 LayoutRect
LayoutRectGetTrailingLayout(LayoutRect layout
);
118 // Return the trailing extent of |layout| (its leading plus its width).
119 CGFloat
LayoutRectGetTrailingEdge(LayoutRect layout
);
121 // Utilities for mapping UIKit geometric structures to RTL-independent geometry.
123 // Get leading and trailing edges of |rect|, assuming layout direction
125 CGFloat
CGRectGetLeadingEdgeUsingDirection(CGRect rect
,
126 base::i18n::TextDirection direction
);
127 CGFloat
CGRectGetTrailingEdgeUsingDirection(
129 base::i18n::TextDirection direction
);
131 // As above, with |direction| == LayoutDirection().
132 CGFloat
CGRectGetLeadingEdge(CGRect rect
);
133 CGFloat
CGRectGetTrailingEdge(CGRect rect
);
135 // Leading/trailing autoresizing masks. 'Leading' is 'Left' under iOS <= 8 or
136 // in an LTR language, 'Right' otherwise; 'Trailing' is the obverse.
137 UIViewAutoresizing
UIViewAutoresizingFlexibleLeadingMargin();
138 UIViewAutoresizing
UIViewAutoresizingFlexibleTrailingMargin();
140 // Text-direction aware UIEdgeInsets constructor; just like UIEdgeInsetsMake(),
141 // except |leading| and |trailing| map to left and right when |direction| is
142 // LEFT_TO_RIGHT, and are swapped for RIGHT_TO_LEFT.
143 UIEdgeInsets
UIEdgeInsetsMakeUsingDirection(
148 base::i18n::TextDirection direction
);
149 // As above, but uses LayoutDirection() for |direction|.
150 UIEdgeInsets
UIEdgeInsetsMakeDirected(CGFloat top
,
155 // Inverses of the above functions: return the leading/trailing inset for
156 // the current direction.
157 CGFloat
UIEdgeInsetsGetLeading(UIEdgeInsets insets
);
158 CGFloat
UIEdgeInsetsGetTrailing(UIEdgeInsets insets
);
160 // Autolayout utilities
162 // Returns the correct NSLayoutFormatOption for the current OS and build. This
163 // will return NSLayoutFormatDirectionLeadingToTrailing when a full RTL flip
164 // is correct, and NSLayoutFormatDirectionLeftToRight when layout should not
165 // change with text direction.
166 // Generally speaking this option should be applied to any whole-page layouts;
167 // smaller sections of views should be determined case by case.
168 NSLayoutFormatOptions
LayoutOptionForRTLSupport();
170 #endif // RTL_GEOMETRY_H_