Merge "Makes the LTR/RTL alignment of text follow the system language by default...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / logical-model-impl.h
1 #ifndef DALI_TOOLKIT_TEXT_LOGICAL_MODEL_IMPL_H
2 #define DALI_TOOLKIT_TEXT_LOGICAL_MODEL_IMPL_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 // EXTERNAL INCLUDES
22 #include <dali/public-api/common/dali-vector.h>
23 #include <dali/public-api/common/intrusive-ptr.h>
24 #include <dali/public-api/object/ref-object.h>
25
26 // INTERNAL INCLUDES
27 #include <dali-toolkit/internal/text/anchor.h>
28 #include <dali-toolkit/internal/text/bidirectional-line-info-run.h>
29 #include <dali-toolkit/internal/text/bidirectional-paragraph-info-run.h>
30 #include <dali-toolkit/internal/text/color-run.h>
31 #include <dali-toolkit/internal/text/embedded-item.h>
32 #include <dali-toolkit/internal/text/font-description-run.h>
33 #include <dali-toolkit/internal/text/font-run.h>
34 #include <dali-toolkit/internal/text/paragraph-run.h>
35 #include <dali-toolkit/internal/text/script-run.h>
36 #include <dali-toolkit/internal/text/underlined-character-run.h>
37
38 namespace Dali
39 {
40 namespace Toolkit
41 {
42 namespace Text
43 {
44 class LogicalModel;
45 typedef IntrusivePtr<LogicalModel> LogicalModelPtr;
46 struct InputStyle;
47
48 /**
49  * @brief A logical text model contains layout independent information.
50  *
51  * This includes:
52  * - A series of UTF-32 characters in logical order
53  */
54 class LogicalModel : public RefObject
55 {
56 public:
57   /**
58    * @brief Create a new instance of a LogicalModel.
59    *
60    * @return A pointer to a new LogicalModel.
61    */
62   static LogicalModelPtr New();
63
64   // Language support interface.
65
66   /**
67    * @brief Retrieves the script for the given character index.
68    *
69    * @param[in] characterIndex Index to the character.
70    *
71    * @return The character's script.
72    */
73   Script GetScript(CharacterIndex characterIndex) const;
74
75   // Bidirectional support interface.
76
77   /**
78    * @brief Retrieves the direction of a characters.
79    *
80    * See GetCharacterDirections().
81    *
82    * @param[in] characterIndex Index to a character.
83    *
84    * @return The character's direction.
85    */
86   CharacterDirection GetCharacterDirection(CharacterIndex characterIndex) const;
87
88   // Visual <--> Logical conversion tables.
89
90   /**
91    * @brief Retrieves the logical cursor index for the given visual cursor index.
92    *
93    * @pre The method FetchBidirectionalLineInfo() must be called before. If the result of FetchBidirectionalLineInfo() is false,
94    *      then the character is not in a bidirectional line and the result will be invalid.
95    *
96    * @param[in] visualCursorIndex The visual cursor index.
97    *
98    * @return The logical cursor index.
99    */
100   CharacterIndex GetLogicalCursorIndex(CharacterIndex visualCursorIndex);
101
102   /**
103    * @brief Retrieves the logical character index for the given visual character index.
104    *
105    * @pre The method FetchBidirectionalLineInfo() must be called before. If the result of FetchBidirectionalLineInfo() is false,
106    *      then the character is not in a bidirectional line and the result will be invalid.
107    *
108    * @param[in] visualCharacterIndex The visual character index.
109    *
110    * @return The logical character index.
111    */
112   CharacterIndex GetLogicalCharacterIndex(CharacterIndex visualCharacterIndex);
113
114   /**
115    * @brief Fetch the bidirectional line info for the given character.
116    *
117    * Call GetBidirectionalLineInfo() to retrieve the last fetched line.
118    *
119    * @param[in] characterIndex The character index.
120    *
121    * @return @e true if the given @e character is in a bidirectional line.
122    */
123   bool FetchBidirectionalLineInfo(CharacterIndex characterIndex);
124
125   /**
126    * @brief Retrieves the last fetched bidirectional line info.
127    *
128    * @return The index of the bidirectional line info.
129    */
130   BidirectionalLineRunIndex GetBidirectionalLineInfo() const;
131
132   // Text style.
133
134   /**
135    * @brief Updates the text's style runs with the added or removed text.
136    *
137    * @param[in] index The character's index.
138    * @param[in] numberOfCharacters The number of characters added or removed. If the value is negative the characters are removed.
139    */
140   void UpdateTextStyleRuns(CharacterIndex index, int numberOfCharacters);
141
142   /**
143    * @brief Retrieves the text's style for the given character index.
144    *
145    * @param[in] index The character index.
146    * @param[out] style The text's style in the given style.
147    */
148   void RetrieveStyle(CharacterIndex index, InputStyle& style);
149
150   /**
151    * @brief Clears the font description runs.
152    */
153   void ClearFontDescriptionRuns();
154
155   // Paragraphs
156
157   /**
158    * @brief Creates the paragraph info.
159    *
160    * @pre The line break info must be set.
161    *
162    * @param[in] startIndex The character from where the paragraph info is set.
163    * @param[in] numberOfCharacters The number of characters.
164    */
165   void CreateParagraphInfo(CharacterIndex startIndex,
166                            Length         numberOfCharacters);
167
168   /**
169    * @brief Find the paragraphs which contains the given characters.
170    *
171    * @param[in] index The first character's index of the run.
172    * @param[in] numberOfCharacters The number of characters of the run.
173    * @param[out] paragraphs Indices to the paragraphs which contain the characters.
174    */
175   void FindParagraphs(CharacterIndex             index,
176                       Length                     numberOfCharacters,
177                       Vector<ParagraphRunIndex>& paragraphs);
178
179   // Embedded images
180
181   /**
182    * @brief Clears the embedded images.
183    */
184   void ClearEmbeddedImages();
185
186   /**
187    * @brief Clears the anchors.
188    */
189   void ClearAnchors();
190
191 protected:
192   /**
193    * @brief A reference counted object may only be deleted by calling Unreference().
194    */
195   virtual ~LogicalModel();
196
197 private:
198   /**
199    * @brief Private constructor.
200    */
201   LogicalModel();
202
203   // Undefined
204   LogicalModel(const LogicalModel& handle);
205
206   // Undefined
207   LogicalModel& operator=(const LogicalModel& handle);
208
209 public:
210   Vector<Character>                     mText;
211   Vector<ScriptRun>                     mScriptRuns;
212   Vector<FontRun>                       mFontRuns;
213   Vector<ColorRun>                      mColorRuns;
214   Vector<ColorRun>                      mBackgroundColorRuns;
215   Vector<FontDescriptionRun>            mFontDescriptionRuns;
216   Vector<LineBreakInfo>                 mLineBreakInfo;
217   Vector<ParagraphRun>                  mParagraphInfo;
218   Vector<BidirectionalParagraphInfoRun> mBidirectionalParagraphInfo;
219   Vector<CharacterDirection>            mCharacterDirections; ///< For each character, whether is right to left. ( @e flase is left to right, @e true right to left ).
220   Vector<BidirectionalLineInfoRun>      mBidirectionalLineInfo;
221   Vector<EmbeddedItem>                  mEmbeddedItems;
222   Vector<Anchor>                        mAnchors;
223   Vector<UnderlinedCharacterRun>        mUnderlinedCharacterRuns; ///< The underlined character run from markup-processor
224
225   BidirectionalLineRunIndex mBidirectionalLineIndex; ///< The last fetched bidirectional line info.
226 };
227
228 } // namespace Text
229
230 } // namespace Toolkit
231
232 } // namespace Dali
233
234 #endif // DALI_TOOLKIT_TEXT_LOGICAL_MODEL_IMPL_H