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