1 #ifndef __DALI_TOOLKIT_TEXT_VISUAL_MODEL_IMPL_H__
2 #define __DALI_TOOLKIT_TEXT_VISUAL_MODEL_IMPL_H__
5 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <dali/public-api/common/dali-vector.h>
23 #include <dali/public-api/common/intrusive-ptr.h>
24 #include <dali/public-api/math/vector4.h>
25 #include <dali/public-api/object/ref-object.h>
28 #include <dali-toolkit/internal/text/line-run.h>
43 typedef IntrusivePtr<VisualModel> VisualModelPtr;
46 * @brief A visual text model contains layout specific information.
49 * - A series of glyphs in visual order i.e. after the bidirectional reordering.
50 * - The position of each glyph within a 2D bounding box.
52 class VisualModel : public RefObject
57 * @brief Create a new instance of a VisualModel.
59 * @return A pointer to a new VisualModel.
61 static VisualModelPtr New();
66 * @brief Replaces any glyphs previously set.
68 * @note If the number of glyphs is zero, all buffers are cleared.
69 * @note If one pointer is NULL and the number of glyphs is not zero, the buffer is not touched.
71 * @param[in] glyphs An array of glyphs in the visual order.
72 * @param[in] characterIndices An array containing the first character in the logical model that each glyph relates to.
73 * @param[in] charactersPerGlyph An array containing the number of characters per glyph.
74 * @param[in] numberOfGlyphs The number of glyphs.
76 void SetGlyphs( const GlyphInfo* glyphs,
77 const CharacterIndex* characterIndices,
78 const Length* charactersPerGlyph,
79 Length numberOfGlyphs );
82 * @brief Creates the character to glyph conversion table.
84 * @pre The glyphs per character table needs to be created first.
86 * @param[in] numberOfCharacters The number of characters.
88 void CreateCharacterToGlyphTable( Length numberOfCharacters = 0u );
91 * @brief Creates an array containing the number of glyphs per character.
93 * @param[in] numberOfCharacters The number of characters.
95 void CreateGlyphsPerCharacterTable( Length numberOfCharacters = 0u );
98 * @brief Retrieves the number of glyphs.
100 * @return The number of glyphs.
102 Length GetNumberOfGlyphs() const;
105 * @brief Retrieves glyphs in the given buffer.
107 * The size of the @p glyphs buffer needs to be big enough to copy the @p numberOfGlyphs.
108 * @param[out] glyphs Pointer to a buffer where the glyphs are copied.
109 * @param[in] glyphIndex Index to the first glyph.
110 * @param[in] numberOfGlyphs Number of glyphs to be copied.
112 void GetGlyphs( GlyphInfo* glyphs,
113 GlyphIndex glyphIndex,
114 Length numberOfGlyphs ) const;
117 * @brief Retrieves a glyph.
119 * @param[in] glyphIndex Index to a glyph.
123 const GlyphInfo& GetGlyphInfo( GlyphIndex glyphIndex ) const;
126 * @brief Replaces glyphs.
128 * If the @p numberOfGlyphsToRemove is zero, this operation is like an insert.
129 * If the @p numberOfGlyphsToInsert is zero, this operation is like a remove.
131 * @param[in] glyphIndex Where to replace the glyphs.
132 * @param[in] numberOfGlyphsToRemove The number of glyphs to be removed.
133 * @param[in] glyphs Pointer to a buffer with the new glyphs.
134 * @param[in] numberOfCharacters Pointer to a buffer with the number of characters per glyph.
135 * @param[in] numberOfGlyphsToInsert The number of new glyphs in the buffer.
137 void ReplaceGlyphs( GlyphIndex glyphIndex,
138 Length numberOfGlyphsToRemove,
139 const GlyphInfo* const glyphs,
140 const Length* const numberOfCharacters,
141 Length numberOfGlyphsToInsert );
143 // Character <--> Glyph conversion
146 * @brief Retrieves the first character in the logical model which a glyph represents.
148 * @note After shaping several characters may be represented by the same glyph.
149 * Alternatively several glyphs may be required to display a character.
150 * @param[in] glyphIndex The glyph index.
151 * @return The character index.
153 CharacterIndex GetCharacterIndex( GlyphIndex glyphIndex ) const;
156 * @brief Query the number of characters the glyph represents.
158 * @param[in] glyphIndex The glyph index.
159 * @return The number of characters represented by the glyph.
161 Length GetCharactersPerGlyph( GlyphIndex glyphIndex ) const;
164 * @brief Retrieves the first glyph in the visual model which represents a given character.
166 * @note After shaping several characters may be represented by the same glyph.
167 * Alternatively several glyphs may be required to display a character.
168 * @param[in] characterIndex The character index.
169 * @return The glyph index.
171 GlyphIndex GetGlyphIndex( CharacterIndex characterIndex ) const;
174 * @brief Retrieves the whole or part of the character to glyph conversion map.
176 * The size of the buffer needs to be big enough to copy the @p numberOfCharacters.
178 * @param[out] characterToGlyphMap Pointer to a buffer where the conversion map is copied.
179 * @param[in] characterIndex Index to the first character.
180 * @param[in] numberOfCharacters The number of characters.
182 void GetCharacterToGlyphMap( GlyphIndex* characterToGlyphMap,
183 CharacterIndex characterIndex,
184 Length numberOfCharacters ) const;
187 * @brief Retrieves the whole or part of the glyph to character conversion map.
189 * The size of the buffer needs to be big enough to copy the @p numberOfGlyphs.
191 * @param[out] glyphToCharacter Pointer to a buffer where the conversion map is copied.
192 * @param[in] glyphIndex Index to the first glyph.
193 * @param[in] numberOfGlyphs The number of glyphs.
195 void GetGlyphToCharacterMap( CharacterIndex* glyphToCharacter,
196 GlyphIndex glyphIndex,
197 Length numberOfGlyphs ) const;
200 * @brief Retrieves for each glyph the number of characters the glyph represents.
202 * @param[out] charactersPerGlyph Pointer to a buffer where the number of characters for each glyph are copied.
203 * @param[in] glyphIndex Index to the first glyph.
204 * @param[in] numberOfGlyphs The number of glyphs.
206 void GetCharactersPerGlyphMap( Length* charactersPerGlyph,
207 GlyphIndex glyphIndex,
208 Length numberOfGlyphs ) const;
211 * @brief Retrieves for each character the number of glyphs the character is shaped.
213 * @param[out] glyphsPerCharacter Pointer to a buffer where the number of glyphs for each character are copied.
214 * @param[in] characterIndex Index to the first character.
215 * @param[in] numberOfCharacters The number of characters.
217 void GetGlyphsPerCharacterMap( Length* glyphsPerCharacter,
218 CharacterIndex characterIndex,
219 Length numberOfCharacters ) const;
221 // Position interface
224 * @brief Replaces any glyph positions previously set.
226 * @note If the number of glyphs is zero the position buffer is cleared.
228 * @param[in] glyphPositions An array of visual positions for each glyph.
229 * @param[in] numberOfGlyphs The number of positions.
231 void SetGlyphPositions( const Vector2* glyphPositions,
232 Length numberOfGlyphs );
235 * @brief Retrieves the number of glyph positions set.
237 * @note This may be less than the number of glyphs in the model.
238 * @return The number of glyphs.
240 Length GetNumberOfGlyphPositions() const;
243 * @brief Retrieves the glyph positions.
245 * @pre The size of the @p positions buffer needs to be big enough to copy the @p numberOfGlyphs positions.
246 * @param[out] glyphPositions Pointer to a buffer where the glyph positions are copied.
247 * @param[in] glyphIndex Index to the first glyph position.
248 * @param[in] numberOfGlyphs The number of positions to be copied.
250 void GetGlyphPositions( Vector2* glyphPositions,
251 GlyphIndex glyphIndex,
252 Length numberOfGlyphs ) const;
255 * @brief Retrieve the glyph's position of the given glyph.
257 * @param[in] glyphIndex Index to the glyph.
259 * @return The glyph's position.
261 const Vector2& GetGlyphPosition( GlyphIndex glyphIndex ) const;
264 * @brief Replaces glyph's positions.
266 * If the @p numberOfGlyphsToRemove is zero, this operation is like an insert.
267 * If the @p numberOfGlyphsToInsert is zero, this operation is like a remove.
269 * @param[in] glyphIndex Where to replace the glyph's positions.
270 * @param[in] numberOfGlyphsToRemove The number of glyph's positions to be removed.
271 * @param[in] positions Pointer to a buffer with the new glyph's positions.
272 * @param[in] numberOfGlyphsToInsert The number of new glyph's positions in the buffer.
274 void ReplaceGlyphPositions( GlyphIndex glyphIndex,
275 Length numberOfGlyphsToRemove,
276 const Vector2* const positions,
277 Length numberOfGlyphsToInsert );
282 * @brief Sets the lines.
284 * Replaces any lines previously set.
286 * Every line is an item run containing the index to the first glyph of the line and the number of glyphs.
288 * @note If the number of lines is zero or the pointer is NULL, the lines buffer is cleared.
290 * @param[in] lines Pointer to a buffer containing all the line runs.
291 * @param[in] numberOfLines The number of lines in the buffer.
293 void SetLines( const LineRun* const lines,
294 Length numberOfLines );
297 * @brief Retrieves the number of lines of the whole text.
299 * @return The number of lines.
301 Length GetNumberOfLines() const;
304 * @brief Retrieves lines.
306 * The size of the @p lines buffer needs to be big enough to copy the @p numberOfLines.
308 * @param[out] lines Pointer to a buffer where the lines are copied.
309 * @param[in] lineIndex Index to the first line.
310 * @param[in] numberOfLines Number of lines to be copied.
312 void GetLines( LineRun* lines,
314 Length numberOfLines ) const;
317 * @brief Retrieves the number of lines and the index to the first line where the given range of glyphs is laid out.
319 * @param[in] glyphIndex Index to the first glyph.
320 * @param[in] numberOfGlyphs The number of glyph.
321 * @param[out] firstLine Index to the line containing the glyph index.
322 * @param[out] numberOfLines The number of lines.
324 void GetNumberOfLines( GlyphIndex glyphIndex,
325 Length numberOfGlyphs,
326 LineIndex& firstLine,
327 Length& numberOfLines ) const;
329 * @brief Retrieves the lines where the given range of glyphs is laid out.
331 * The size of the @p lines buffer needs to be big enough to copy the @p numberOfLines.
333 * @param[out] lines Pointer to a buffer where the lines are copied.
334 * @param[in] glyphIndex Index to the first glyphs of the range.
335 * @param[in] numberOfGlyphs Number of glyphs in the range.
337 void GetLinesOfGlyphRange( LineRun* lines,
338 GlyphIndex glyphIndex,
339 Length numberOfGlyphs ) const;
342 * @brief Retrieves the line index where the glyph is laid-out.
344 * @param[in] glyphIndex The glyph's index.
346 * @return The line index.
348 LineIndex GetLineOfGlyph( GlyphIndex glyphIndex );
351 * @brief Retrieves the line index where the character is laid-out.
353 * @param[in] characterIndex The character's index.
355 * @return The line index.
357 LineIndex GetLineOfCharacter( CharacterIndex characterIndex );
360 * @brief Replaces lines for the given range of glyphs.
362 * If the @p numberOfGlyphsToRemove is zero, this operation is like an insert.
363 * If the @p numberOfGlyphsToInsert is zero, this operation is like a remove.
365 * @param[in] glyphIndex Index of the first glyph where to replace the line info.
366 * @param[in] numberOfGlyphsToRemove The number of glyphs to be the line info removed.
367 * @param[in] lines Pointer to a buffer with the lines.
368 * @param[in] numberOfGlyphsToInsert The number of characters to be the line info inserted.
370 void ReplaceLines( GlyphIndex glyphIndex,
371 Length numberOfGlyphsToRemove,
372 const LineRun* const lines,
373 Length numberOfGlyphsToInsert );
378 * @brief Sets the natural size.
380 * @param[in] size The text's natural size.
382 void SetNaturalSize( const Vector2& size );
385 * @brief Retrieves the natural size.
387 * @return The text's natural size.
389 const Vector2& GetNaturalSize() const;
392 * @brief Sets the text's actual size after it has been laid out.
394 * @param[in] size The text's size.
396 void SetActualSize( const Vector2& size );
399 * @brief Retrieves the text's actual size after it has been laid out.
401 * @return The text's size.
403 const Vector2& GetActualSize() const;
406 * @brief Set the text's color
408 * @param[in] textColor The text's color
410 void SetTextColor( const Vector4& textColor );
413 * @brief Retrieve the text's color
415 * @return The text's color
417 const Vector4& GetTextColor() const;
420 * @brief Sets the text's shadow offset.
422 * @param[in] shadowOffset The shadow offset, 0,0 indicates no shadow.
424 void SetShadowOffset( const Vector2& shadowOffset );
427 * @brief Retrieves the text's shadow offset.
429 * @return The text's shadow offset, 0,0 indicates no shadow.
431 const Vector2& GetShadowOffset() const;
434 * @brief Sets the text's shadow color.
436 * @param[in] shadowColor The shadow color.
438 void SetShadowColor( const Vector4& shadowColor );
441 * @brief Retrieves the text's shadow color.
443 * @return The text's shadow color.
445 const Vector4& GetShadowColor() const;
448 * @brief Sets the text's underline color.
450 * @param[in] color The text's underline color.
452 void SetUnderlineColor( const Vector4& color );
455 * @brief Retrieves the text's underline color.
457 * @return The text's underline color.
459 const Vector4& GetUnderlineColor() const;
462 * @brief Sets the text underline flag.
464 * @param[in] enabled true if underlined.
466 void SetUnderlineEnabled( bool enabled );
469 * @brief Returns whether the text is underlined or not.
471 * @return underline state.
473 bool IsUnderlineEnabled() const;
476 * @brief Clear the caches.
481 * @brief Set the override used for underline height, 0 indicates height will be come from font metrics
483 * @param[in] height The height in pixels of the underline
485 void SetUnderlineHeight( float height );
488 * @brief Retrieves the underline height override
490 * @return Returns the override height for an underline, 0 indicates that font metrics will determine the height
492 float GetUnderlineHeight() const;
497 * @brief A reference counted object may only be deleted by calling Unreference().
499 virtual ~VisualModel();
504 * @brief Private constructor.
509 VisualModel( const VisualModel& handle );
512 VisualModel& operator=( const VisualModel& handle );
516 Vector<GlyphInfo> mGlyphs; ///< For each glyph, the font's id, glyph's index within the font and glyph's metrics.
517 Vector<CharacterIndex> mGlyphsToCharacters; ///< For each glyph, the index of the first character.
518 Vector<GlyphIndex> mCharactersToGlyph; ///< For each character, the index of the first glyph.
519 Vector<Length> mCharactersPerGlyph; ///< For each glyph, the number of characters that form the glyph.
520 Vector<Length> mGlyphsPerCharacter; ///< For each character, the number of glyphs that are shaped.
521 Vector<Vector2> mGlyphPositions; ///< For each glyph, the position.
522 Vector<LineRun> mLines; ///< The laid out lines.
524 Vector4 mTextColor; ///< The text color
525 Vector4 mShadowColor; ///< Color of drop shadow
526 Vector4 mUnderlineColor; ///< Color of underline
527 Vector2 mShadowOffset; ///< Offset for drop shadow, 0 indicates no shadow
528 float mUnderlineHeight; ///< Fixed height for underline to override font metrics.
534 Size mNaturalSize; ///< Size of the text with no line wrapping.
535 Size mActualSize; ///< Size of the laid-out text considering the layout properties set.
537 // Caches to increase performance in some consecutive operations.
538 LineIndex mCachedLineIndex; ///< Used to increase performance in consecutive calls to GetLineOfGlyph() or GetLineOfCharacter() with consecutive glyphs or characters.
542 bool mUnderlineEnabled:1; ///< Underline enabled flag
543 bool mUnderlineColorSet:1; ///< Has the underline color been explicitly set?
549 } // namespace Toolkit
553 #endif // __DALI_TOOLKIT_TEXT_VISUAL_MODEL_IMPL_H__