e0a38747113801beda1ff990f7f1aba111be8ec2
[platform/core/uifw/dali-core.git] / dali / internal / event / text / glyph-status / glyph-status-container.h
1 #ifndef __DALI_INTERNAL_GLYPH_STATUS_CONTAINER_H__
2 #define __DALI_INTERNAL_GLYPH_STATUS_CONTAINER_H__
3
4 //
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 //
7 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 // INTERNAL INCLUDES
21
22 #include <dali/internal/event/text/glyph-status/glyph-status.h>
23 #include <dali/internal/common/text-array.h>
24
25 // EXTERNAL INCLUDES
26 #include <set>
27
28
29 namespace Dali
30 {
31
32 namespace Internal
33 {
34
35 /**
36  *
37  * Maintains a set of reference counted characters (glyph status objects).
38  * The glyph status objects are sorted by character code and font id.
39  *
40  * The class provides an API for inserting glyph status objects into
41  * the container and increasing / decreasing their reference count.
42  *
43  * Once a glyph status object reaches a ref count = 0, it remains in
44  * the container, but a pointer to it is added to the mDeadCharacters list.
45  * The dead character list is sorted by dead time.
46  *
47  * If new glyph objects are inserted when the container is full,
48  * dead characters (ref = 0) are replaced, starting with the oldest first.
49  *
50  * To see what is happening on the console, enable DEBUG_GLYPH_STATUS_CONTAINER in
51  * glyph-status-container-debug.h
52  *
53  */
54 class GlyphStatusContainer
55 {
56
57 public:
58
59   /**
60    * Constructor
61    * @param numberOfCharacters how many characters the container should hold
62    */
63   GlyphStatusContainer( unsigned int numberOfCharacters );
64
65   /**
66    * destructor, non-virtual not intended as a base class.
67    */
68   ~GlyphStatusContainer();
69
70   /**
71    * Increase the reference count of a character
72    * @param[in] character code
73    * @param[in] font id
74    */
75   void IncreaseRefCount( uint32_t charCode, FontId fontId);
76
77   /**
78    * @param[in] charCode character code
79    * @param[in] fontId font id
80    */
81   void DecreaseRefCount( uint32_t charCode, FontId fontId);
82
83   /**
84    * enum to represent the result of a character insertion.
85    */
86   enum InsertResult
87   {
88     INSERTED_OK,            ///< character was inserted in to an empty space
89     REPLACE_DEAD_CHARACTER, ///< character replaced a cached dead character.
90   };
91
92   /**
93    * Insert a new character in to the container
94    * @param[in] charCode character code
95    * @param[in] fontId font id
96    * @param[out] deadUniqueId the id of a dead character, if one was replaced
97    * @return insertion result
98    */
99   InsertResult InsertNewCharacter( uint32_t charCode, FontId fontId, unsigned int& deadUniqueId );
100
101   /**
102    * Find the glyph status object given a character code and font id
103    * @param[in] charCode character code
104    * @param[in] fontId font id
105    * @return glyph status object if it exists, NULL if it doesn't
106    */
107   const GlyphStatus* FindGlyphStatus( uint32_t charCode, FontId fontId) const;
108
109   /**
110    * Return a reference to a glyph status object given a character code and font id
111    * @param[in] charCode character code
112    * @param[in] fontId font id
113    * @return glyph status object
114    */
115   const GlyphStatus& GetStatus( uint32_t charCode, FontId fontId) const;
116
117   /**
118    * Status set typedef. Uses a custom sort function to sort by character code and font id
119    */
120   typedef std::set< GlyphStatus, GlyphStatus::Comparator > StatusSet;
121
122   /**
123    * Get the glyph status set
124    * @todo find a better solution than allowing direct access to the set
125    * @return the glyph status set
126    */
127   const StatusSet& GetStatusSet() const;
128
129   /**
130    * Check if all characters in a text array are marked as loaded
131    * @param[in] text the text array
132    * @param[in] fontId font id
133    * @return true if all characters are loaded false if not
134    */
135   bool IsTextLoaded( const TextArray& text, FontId fontId) const;
136
137   /**
138    * Given a text array, find how many character are loaded and
139    * whether it will fit in to the container.
140    * @param[in] text the text array
141    * @param[in] fontId font id
142    * @param[out] charsNotLoaded how many characters are not loaded
143    * @param[out] fitsInContainer whether the text fits in the container
144    */
145   void GetTextStatus( const TextArray& text,
146                       FontId fontId,
147                       unsigned int& charsNotLoaded,
148                       bool& fitsInContainer ) const;
149
150   /**
151    * Clone the contents of one container, into this container
152    * @param[in] clone the container to clone
153    */
154   void CloneContents( const GlyphStatusContainer& clone );
155
156   /**
157    * Get the list of dead characters
158    * @param[out] deadList to be filled with a list of dead characters
159    */
160   void GetDeadCharacters( std::vector< unsigned int >& deadList );
161
162   /**
163    * Clear dead characters.
164    */
165   void ClearDeadCharacters();
166
167   /**
168    * Check if the container is empty
169    * @return true if container is empty
170    */
171   bool Empty() const;
172
173   /**
174    * @return the container size
175    */
176   unsigned int GetSize() const;
177
178   /**
179    * Clear the container contents.
180    */
181   void ClearContents();
182
183 private:
184
185   /**
186    * Add a character to the dead character list
187    * @param[in] deadCharacter dead character
188    */
189   void AddDeadCharacter( const GlyphStatus* deadCharacter);
190
191   /**
192    * Remove a character from the dead character list.
193    * This happens if a characters reference count goes from 0 -> 1.
194    * @param[in] deadCharacter dead character
195    */
196   void RemoveDeadCharacter( const GlyphStatus* deadCharacter );
197
198   /**
199    * Remove the oldest dead character
200    * @return oldest dead character
201    */
202   const GlyphStatus* RemoveOldestDeadCharacter( );
203
204   /**
205    * Insert a character in to the lookup
206    * @param[in] charCode character code
207    * @param[in] fontId font id
208    */
209   void InsertCharacterIntoLookup(uint32_t charCode, FontId fontId );
210
211   /**
212    * Reset the dead time stamps of all glyph objects in the
213    * dead character list. This is called when mTimeStamp value reaches
214    * GlyphStatus::GetMaximumDeadTime()
215    */
216   void ResetTimeStamps();
217
218   /**
219    * Get the total available space in the container.
220    * This is empty space + space used by dead characters that can be replaced
221    * @return total space
222    */
223   unsigned int TotalAvailableSpace() const;
224
225   unsigned int mContainerSize;  ///< container size
226   unsigned int mEmptySpace;     ///< amount of space that is empty (has never been used)
227
228   /**
229    * Status pointer set typedef, sorted by custom function
230    */
231   typedef std::set< const GlyphStatus*, GlyphStatus::PointerComparator > StatusPointerSet;
232
233   StatusSet                   mCharacterLookup;       ///< set of glyph status objects sorted by font id and character code
234   StatusPointerSet            mDeadCharacters;        ///< set of characters with a ref count of zero, which are still cached.
235   unsigned int                mTimeStamp;             ///< current time stamp
236
237 };
238
239
240 } // namespace Internal
241
242 } // namespace Dali
243
244 #endif // __DALI_INTERNAL_GLYPH_STATUS_CONTAINER_H__