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 * 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;
119 * @param[in] glyphIndex Index to a glyph.
123 const GlyphInfo& GetGlyphInfo( GlyphIndex glyphIndex ) const;
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 * 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 * 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 * 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 * 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 * 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 * 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 * 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 * 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 );
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 * Retrieves the number of lines of the whole text.
299 * @return The number of lines.
301 Length GetNumberOfLines() const;
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 * 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 * 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 * Replaces lines for the given range of glyphs.
344 * If the @p numberOfGlyphsToRemove is zero, this operation is like an insert.
345 * If the @p numberOfGlyphsToInsert is zero, this operation is like a remove.
347 * @param[in] glyphIndex Index of the first glyph where to replace the line info.
348 * @param[in] numberOfGlyphsToRemove The number of glyphs to be the line info removed.
349 * @param[in] lines Pointer to a buffer with the lines.
350 * @param[in] numberOfGlyphsToInsert The number of characters to be the line info inserted.
352 void ReplaceLines( GlyphIndex glyphIndex,
353 Length numberOfGlyphsToRemove,
354 const LineRun* const lines,
355 Length numberOfGlyphsToInsert );
360 * Sets the natural size.
362 * @param[in] size The text's natural size.
364 void SetNaturalSize( const Vector2& size );
367 * Retrieves the natural size.
369 * @return The text's natural size.
371 const Vector2& GetNaturalSize() const;
374 * Sets the text's actual size after it has been laid out.
376 * @param[in] size The text's size.
378 void SetActualSize( const Vector2& size );
381 * Retrieves the text's actual size after it has been laid out.
383 * @return The text's size.
385 const Vector2& GetActualSize() const;
388 * @brief Set the text's color
390 * @param[in] textColor The text's color
392 void SetTextColor( const Vector4& textColor );
395 * @brief Retrieve the text's color
397 * @return The text's color
399 const Vector4& GetTextColor() const;
402 * @brief Sets the text's shadow offset.
404 * @param[in] shadowOffset The shadow offset, 0,0 indicates no shadow.
406 void SetShadowOffset( const Vector2& shadowOffset );
409 * @brief Retrieves the text's shadow offset.
411 * @return The text's shadow offset, 0,0 indicates no shadow.
413 const Vector2& GetShadowOffset() const;
416 * @brief Sets the text's shadow color.
418 * @param[in] shadowColor The shadow color.
420 void SetShadowColor( const Vector4& shadowColor );
423 * @brief Retrieves the text's shadow color.
425 * @return The text's shadow color.
427 const Vector4& GetShadowColor() const;
430 * @brief Sets the text's underline color.
432 * @param[in] color The text's underline color.
434 void SetUnderlineColor( const Vector4& color );
437 * @brief Retrieves the text's underline color.
439 * @return The text's underline color.
441 const Vector4& GetUnderlineColor() const;
444 * @brief Sets the text underline flag.
446 * @param[in] enabled true if underlined.
448 void SetUnderlineEnabled( bool enabled );
451 * @brief Returns whether the text is underlined or not.
453 * @return underline state.
455 bool IsUnderlineEnabled() const;
460 * @brief A reference counted object may only be deleted by calling Unreference().
462 virtual ~VisualModel();
467 * @brief Private constructor.
472 VisualModel( const VisualModel& handle );
475 VisualModel& operator=( const VisualModel& handle );
479 Vector<GlyphInfo> mGlyphs; ///< For each glyph, the font's id, glyph's index within the font and glyph's metrics.
480 Vector<CharacterIndex> mGlyphsToCharacters; ///< For each glyph, the index of the first character.
481 Vector<GlyphIndex> mCharactersToGlyph; ///< For each character, the index of the first glyph.
482 Vector<Length> mCharactersPerGlyph; ///< For each glyph, the number of characters that form the glyph.
483 Vector<Length> mGlyphsPerCharacter; ///< For each character, the number of glyphs that are shaped.
484 Vector<Vector2> mGlyphPositions; ///< For each glyph, the position.
485 Vector<LineRun> mLines; ///< The laid out lines.
487 Vector4 mTextColor; ///< The text color
488 Vector2 mShadowOffset; ///< Offset for drop shadow, 0 indicates no shadow
489 Vector4 mShadowColor; ///< Color of drop shadow
490 Vector4 mUnderlineColor; ///< Color of underline
491 bool mUnderlineEnabled:1; ///< Underline enabled flag
492 bool mUnderlineColorSet:1; ///< Has the underline color been explicitly set?
496 Size mNaturalSize; ///< Size of the text with no line wrapping.
497 Size mActualSize; ///< Size of the laid-out text considering the layout properties set.
502 } // namespace Toolkit
506 #endif // __DALI_TOOLKIT_TEXT_VISUAL_MODEL_IMPL_H__