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 // A LayoutOffset is an x-offset specified in leading pixels.
122 typedef CGFloat LayoutOffset
;
124 // Returns |point| with its x-value shifted |offset| pixels in the leading
125 // direction according to |direction|
126 CGPoint
CGPointLayoutOffsetUsingDirection(CGPoint point
,
128 base::i18n::TextDirection direction
);
130 // As above, using |direction| == RIGHT_TO_LEFT if UseRTLLayout(), LEFT_TO_RIGHT
132 CGPoint
CGPointLayoutOffset(CGPoint point
, LayoutOffset offset
);
134 // Returns |rect| with its x-origin shifted |offset| pixels in the leading
135 // direction according to |direction|
136 CGRect
CGRectLayoutOffsetUsingDirection(CGRect rect
,
138 base::i18n::TextDirection direction
);
140 // As above, using |direction| == RIGHT_TO_LEFT if UseRTLLayout(), LEFT_TO_RIGHT
142 CGRect
CGRectLayoutOffset(CGRect rect
, LayoutOffset offset
);
144 // Returns the leading offset of |rect| inside |boundingBox|, as a LayoutOffset.
145 LayoutOffset
CGRectGetLeadingLayoutOffsetInBoundingRect(CGRect rect
,
146 CGRect boundingRect
);
148 // Returns the trailing offset of |rect| inside |boundingBox|, as a
149 // LayoutOffset. Note that this will be the distance from the trailing edge of
150 // |rect| to the trailing edge of |boundingRect|.
151 LayoutOffset
CGRectGetTrailingLayoutOffsetInBoundingRect(CGRect rect
,
152 CGRect boundingRect
);
154 // Returns the leading content offset of |scrollView|.
155 LayoutOffset
LeadingContentOffsetForScrollView(UIScrollView
* scrollView
);
157 // Utilities for mapping UIKit geometric structures to RTL-independent geometry.
159 // Get leading and trailing edges of |rect|, assuming layout direction
161 CGFloat
CGRectGetLeadingEdgeUsingDirection(CGRect rect
,
162 base::i18n::TextDirection direction
);
163 CGFloat
CGRectGetTrailingEdgeUsingDirection(
165 base::i18n::TextDirection direction
);
167 // As above, with |direction| == LayoutDirection().
168 CGFloat
CGRectGetLeadingEdge(CGRect rect
);
169 CGFloat
CGRectGetTrailingEdge(CGRect rect
);
171 // Leading/trailing autoresizing masks. 'Leading' is 'Left' under iOS <= 8 or
172 // in an LTR language, 'Right' otherwise; 'Trailing' is the obverse.
173 UIViewAutoresizing
UIViewAutoresizingFlexibleLeadingMargin();
174 UIViewAutoresizing
UIViewAutoresizingFlexibleTrailingMargin();
176 // Text-direction aware UIEdgeInsets constructor; just like UIEdgeInsetsMake(),
177 // except |leading| and |trailing| map to left and right when |direction| is
178 // LEFT_TO_RIGHT, and are swapped for RIGHT_TO_LEFT.
179 UIEdgeInsets
UIEdgeInsetsMakeUsingDirection(
184 base::i18n::TextDirection direction
);
185 // As above, but uses LayoutDirection() for |direction|.
186 UIEdgeInsets
UIEdgeInsetsMakeDirected(CGFloat top
,
191 // Inverses of the above functions: return the leading/trailing inset for
192 // the current direction.
193 CGFloat
UIEdgeInsetsGetLeading(UIEdgeInsets insets
);
194 CGFloat
UIEdgeInsetsGetTrailing(UIEdgeInsets insets
);
196 // Autolayout utilities
198 // Returns the correct NSLayoutFormatOption for the current OS and build. This
199 // will return NSLayoutFormatDirectionLeadingToTrailing when a full RTL flip
200 // is correct, and NSLayoutFormatDirectionLeftToRight when layout should not
201 // change with text direction.
202 // Generally speaking this option should be applied to any whole-page layouts;
203 // smaller sections of views should be determined case by case.
204 NSLayoutFormatOptions
LayoutOptionForRTLSupport();
206 #endif // RTL_GEOMETRY_H_