Merge "DALi Version 1.2.47" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / cursor-helper-functions.h
1 #ifndef DALI_TOOLKIT_TEXT_CURSOR_HELPER_FUNCTIONS_H
2 #define DALI_TOOLKIT_TEXT_CURSOR_HELPER_FUNCTIONS_H
3
4 /*
5  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // INTERNAL INCLUDES
22 #include <dali-toolkit/internal/text/logical-model-impl.h>
23 #include <dali-toolkit/internal/text/metrics.h>
24 #include <dali-toolkit/internal/text/visual-model-impl.h>
25
26 namespace Dali
27 {
28
29 namespace Toolkit
30 {
31
32 namespace Text
33 {
34
35 struct CharacterHitTest
36 {
37   /**
38    * @brief Enumeration of the types of hit test.
39    */
40   enum Mode
41   {
42     TAP,   ///< Retrieves the first or last character of the line if the touch point is outside of the boundaries of the text.
43     SCROLL ///< Retrieves the character above or below to the touch point if it's outside of the boundaries of the text.
44   };
45 };
46
47 struct CursorInfo
48 {
49   CursorInfo()
50   : primaryPosition(),
51     secondaryPosition(),
52     lineOffset( 0.f ),
53     glyphOffset( 0.f ),
54     lineHeight( 0.f ),
55     primaryCursorHeight( 0.f ),
56     secondaryCursorHeight( 0.f ),
57     isSecondaryCursor( false )
58   {}
59
60   ~CursorInfo()
61   {}
62
63   Vector2 primaryPosition;       ///< The primary cursor's position (in text's coords).
64   Vector2 secondaryPosition;     ///< The secondary cursor's position (in text's coords).
65   float   lineOffset;            ///< The vertical offset where the line containing the cursor starts.
66   float   glyphOffset;           ///< The difference of line ascender and glyph ascender.
67   float   lineHeight;            ///< The height of the line where the cursor is placed.
68   float   primaryCursorHeight;   ///< The primary cursor's height.
69   float   secondaryCursorHeight; ///< The secondary cursor's height.
70   bool    isSecondaryCursor;     ///< Whether the secondary cursor is valid.
71 };
72
73 /**
74  * @brief Retrieves the closest line for a given touch point.
75  *
76  * It returns the first line if the touch point is above the text and the last line if the touch point is below.
77  *
78  * @param[in] visualModel The visual model.
79  * @param[in] visualY The touch point 'y' in text's coords.
80  * @param[out] matchedLine Whether the touch point actually hits a line.
81  *
82  * @return A line index.
83  */
84 LineIndex GetClosestLine( VisualModelPtr visualModel,
85                           float visualY,
86                           bool& matchedLine );
87
88 /**
89  * @brief Calculates the vertical line's offset for a given line.
90  *
91  * @pre @p lineIndex must be between 0 and the number of lines (both inclusive).
92  *
93  * @param[in] lines The laid-out lines.
94  * @param[in] lineIndex Index to the line.
95  *
96  * @return The vertical offset of the given line.
97  */
98 float CalculateLineOffset( const Vector<LineRun>& lines,
99                            LineIndex lineIndex );
100
101 /**
102  * @brief Retrieves the cursor's logical position for a given touch point x,y
103  *
104  * There are two types of hit test: CharacterHitTest::TAP retrieves the first or
105  * last character of a line if the touch point is outside the boundaries of the
106  * text, CharacterHitTest::SCROLL retrieves the character above or below to the
107  * touch point if it's outside the boundaries of the text.
108  *
109  * @param[in] visualModel The visual model.
110  * @param[in] logicalModel The logical model.
111  * @param[in] metrics A wrapper around FontClient used to get metrics.
112  * @param[in] visualX The touch point 'x' in text's coords.
113  * @param[in] visualY The touch point 'y' in text's coords.
114  * @param[in] mode The type of hit test.
115  * @param[out] matchedCharacter Whether the touch point actually hits a character.
116  *
117  * @return The logical cursor position (in characters). 0 is just before the first character, a value equal to the number of characters is just after the last character.
118  */
119 CharacterIndex GetClosestCursorIndex( VisualModelPtr visualModel,
120                                       LogicalModelPtr logicalModel,
121                                       MetricsPtr metrics,
122                                       float visualX,
123                                       float visualY,
124                                       CharacterHitTest::Mode mode,
125                                       bool& matchedCharacter );
126
127 /**
128  * @brief Calculates the cursor's position for a given character index in the logical order.
129  *
130  * It retrieves as well the line's height and the cursor's height and
131  * if there is a valid alternative cursor, its position and height.
132  *
133  * @param[in] visualModel The visual model.
134  * @param[in] logicalModel The logical model.
135  * @param[in] metrics A wrapper around FontClient used to get metrics.
136  * @param[in] logical The logical cursor position (in characters). 0 is just before the first character, a value equal to the number of characters is just after the last character.
137  * @param[out] cursorInfo The line's height, the cursor's height, the cursor's position and whether there is an alternative cursor.
138  */
139 void GetCursorPosition( VisualModelPtr visualModel,
140                         LogicalModelPtr logicalModel,
141                         MetricsPtr metrics,
142                         CharacterIndex logical,
143                         CursorInfo& cursorInfo );
144
145 /**
146  * @brief Find the indices to the first and last characters of a word for the given touch point.
147  *
148  * @param[in] visualModel The visual model.
149  * @param[in] logicalModel The logical model.
150  * @param[in] metrics A wrapper around FontClient used to get metrics.
151  * @param[in] visualX The touch point 'x' in text's coords.
152  * @param[in] visualY The touch point 'y' in text's coords.
153  * @param[out] startIndex Index to the first character of the selected word.
154  * @param[out] endIndex Index to the last character of the selected word.
155  * @param[out] noTextHitIndex Index to the nearest character when there is no hit.
156  *
157  * @return @e true if the touch point hits a character.
158  */
159 bool FindSelectionIndices( VisualModelPtr visualModel,
160                            LogicalModelPtr logicalModel,
161                            MetricsPtr metrics,
162                            float visualX,
163                            float visualY,
164                            CharacterIndex& startIndex,
165                            CharacterIndex& endIndex,
166                            CharacterIndex& noTextHitIndex );
167
168 } // namespace Text
169
170 } // namespace Toolkit
171
172 } // namespace Dali
173
174 #endif // DALI_TOOLKIT_TEXT_CURSOR_HELPER_FUNCTIONS_H