Removed some redundant methods from TextController & Moved some code to other files
[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) 2021 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 namespace Toolkit
29 {
30 namespace Text
31 {
32 struct CharacterHitTest
33 {
34   /**
35    * @brief Enumeration of the types of hit test.
36    */
37   enum Mode
38   {
39     TAP,   ///< Retrieves the first or last character of the line if the touch point is outside of the boundaries of the text.
40     SCROLL ///< Retrieves the character above or below to the touch point if it's outside of the boundaries of the text.
41   };
42 };
43
44 struct CursorInfo
45 {
46   CursorInfo()
47   : primaryPosition(),
48     secondaryPosition(),
49     lineOffset(0.f),
50     glyphOffset(0.f),
51     lineHeight(0.f),
52     primaryCursorHeight(0.f),
53     secondaryCursorHeight(0.f),
54     isSecondaryCursor(false)
55   {
56   }
57
58   ~CursorInfo()
59   {
60   }
61
62   Vector2 primaryPosition;       ///< The primary cursor's position (in text's coords).
63   Vector2 secondaryPosition;     ///< The secondary cursor's position (in text's coords).
64   float   lineOffset;            ///< The vertical offset where the line containing the cursor starts.
65   float   glyphOffset;           ///< The difference of line ascender and glyph ascender.
66   float   lineHeight;            ///< The height of the line where the cursor is placed.
67   float   primaryCursorHeight;   ///< The primary cursor's height.
68   float   secondaryCursorHeight; ///< The secondary cursor's height.
69   bool    isSecondaryCursor;     ///< Whether the secondary cursor is valid.
70 };
71
72 /**
73  * @brief Parameters passed to the GetCursorPosition() function.
74  */
75 struct GetCursorPositionParameters
76 {
77   VisualModelPtr  visualModel;  ///< The visual model.
78   LogicalModelPtr logicalModel; ///< The logical model.
79   MetricsPtr      metrics;      ///< A wrapper around FontClient used to get metrics.
80   CharacterIndex  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.
81   bool            isMultiline;  ///< Whether the text control is multi-line.
82 };
83
84 /**
85  * @brief Retrieves the closest line for a given touch point.
86  *
87  * It returns the first line if the touch point is above the text and the last line if the touch point is below.
88  *
89  * @param[in] visualModel The visual model.
90  * @param[in] visualY The touch point 'y' in text's coords.
91  * @param[out] matchedLine Whether the touch point actually hits a line.
92  *
93  * @return A line index.
94  */
95 LineIndex GetClosestLine(VisualModelPtr visualModel,
96                          float          visualY,
97                          bool&          matchedLine);
98
99 /**
100  * @brief Calculates the vertical line's offset for a given line.
101  *
102  * @pre @p lineIndex must be between 0 and the number of lines (both inclusive).
103  *
104  * @param[in] lines The laid-out lines.
105  * @param[in] lineIndex Index to the line.
106  *
107  * @return The vertical offset of the given line.
108  */
109 float CalculateLineOffset(const Vector<LineRun>& lines,
110                           LineIndex              lineIndex);
111
112 /**
113  * @brief Retrieves the cursor's logical position for a given touch point x,y
114  *
115  * There are two types of hit test: CharacterHitTest::TAP retrieves the first or
116  * last character of a line if the touch point is outside the boundaries of the
117  * text, CharacterHitTest::SCROLL retrieves the character above or below to the
118  * touch point if it's outside the boundaries of the text.
119  *
120  * @param[in] visualModel The visual model.
121  * @param[in] logicalModel The logical model.
122  * @param[in] metrics A wrapper around FontClient used to get metrics.
123  * @param[in] visualX The touch point 'x' in text's coords.
124  * @param[in] visualY The touch point 'y' in text's coords.
125  * @param[in] mode The type of hit test.
126  * @param[out] matchedCharacter Whether the touch point actually hits a character.
127  *
128  * @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.
129  */
130 CharacterIndex GetClosestCursorIndex(VisualModelPtr         visualModel,
131                                      LogicalModelPtr        logicalModel,
132                                      MetricsPtr             metrics,
133                                      float                  visualX,
134                                      float                  visualY,
135                                      CharacterHitTest::Mode mode,
136                                      bool&                  matchedCharacter);
137
138 /**
139  * @brief Calculates the cursor's position for a given character index in the logical order.
140  *
141  * It retrieves as well the line's height and the cursor's height and
142  * if there is a valid alternative cursor, its position and height.
143  *
144  * @param[in] parameters Parameters used to calculate the cursor's position.
145  * @param[out] cursorInfo The line's height, the cursor's height, the cursor's position and whether there is an alternative cursor.
146  */
147 void GetCursorPosition(GetCursorPositionParameters& parameters,
148                        CursorInfo&                  cursorInfo);
149
150 /**
151  * @brief Find the indices to the first and last characters of a word for the given touch point.
152  *
153  * @param[in] visualModel The visual model.
154  * @param[in] logicalModel The logical model.
155  * @param[in] metrics A wrapper around FontClient used to get metrics.
156  * @param[in] visualX The touch point 'x' in text's coords.
157  * @param[in] visualY The touch point 'y' in text's coords.
158  * @param[out] startIndex Index to the first character of the selected word.
159  * @param[out] endIndex Index to the last character of the selected word.
160  * @param[out] noTextHitIndex Index to the nearest character when there is no hit.
161  *
162  * @return @e true if the touch point hits a character.
163  */
164 bool FindSelectionIndices(VisualModelPtr  visualModel,
165                           LogicalModelPtr logicalModel,
166                           MetricsPtr      metrics,
167                           float           visualX,
168                           float           visualY,
169                           CharacterIndex& startIndex,
170                           CharacterIndex& endIndex,
171                           CharacterIndex& noTextHitIndex);
172
173 } // namespace Text
174
175 } // namespace Toolkit
176
177 } // namespace Dali
178
179 #endif // DALI_TOOLKIT_TEXT_CURSOR_HELPER_FUNCTIONS_H