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