Refactor TextLabel to use text visual
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / visual-model-impl.h
index 9deaea7..6356f32 100644 (file)
@@ -2,7 +2,7 @@
 #define __DALI_TOOLKIT_TEXT_VISUAL_MODEL_IMPL_H__
 
 /*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 // EXTERNAL INCLUDES
 #include <dali/public-api/common/dali-vector.h>
 #include <dali/public-api/common/intrusive-ptr.h>
+#include <dali/public-api/math/vector2.h>
 #include <dali/public-api/math/vector4.h>
 #include <dali/public-api/object/ref-object.h>
 
 // INTERNAL INCLUDES
 #include <dali-toolkit/internal/text/line-run.h>
+#include <dali-toolkit/internal/text/color-run.h>
 
 namespace Dali
 {
 
-struct Vector2;
-
 namespace Toolkit
 {
 
 namespace Text
 {
 
-struct LineRun;
 class VisualModel;
 typedef IntrusivePtr<VisualModel> VisualModelPtr;
 
@@ -63,43 +62,28 @@ public:
   // Glyph interface.
 
   /**
-   * @brief Replaces any glyphs previously set.
-   *
-   * @note If the number of glyphs is zero, all buffers are cleared.
-   * @note If one pointer is NULL and the number of glyphs is not zero, the buffer is not touched.
-   *
-   * @param[in] glyphs An array of glyphs in the visual order.
-   * @param[in] characterIndices An array containing the first character in the logical model that each glyph relates to.
-   * @param[in] charactersPerGlyph An array containing the number of characters per glyph.
-   * @param[in] numberOfGlyphs The number of glyphs.
-   */
-  void SetGlyphs( const GlyphInfo* glyphs,
-                  const CharacterIndex* characterIndices,
-                  const Length* charactersPerGlyph,
-                  Length numberOfGlyphs );
-
-  /**
    * @brief Creates the character to glyph conversion table.
    *
    * @pre The glyphs per character table needs to be created first.
    *
+   * @param[in] startIndex The character from where the conversion table is created.
+   * @param[in] startGlyphIndex The glyph from where the conversion table is created.
    * @param[in] numberOfCharacters The number of characters.
    */
-  void CreateCharacterToGlyphTable( Length numberOfCharacters = 0u );
+  void CreateCharacterToGlyphTable( CharacterIndex startIndex,
+                                    GlyphIndex startGlyphIndex,
+                                    Length numberOfCharacters );
 
   /**
    * @brief Creates an array containing the number of glyphs per character.
    *
+   * @param[in] startIndex The character from where the table is created.
+   * @param[in] startGlyphIndex The glyph from where the conversion table is created.
    * @param[in] numberOfCharacters The number of characters.
    */
-  void CreateGlyphsPerCharacterTable( Length numberOfCharacters = 0u );
-
-  /**
-   * Retrieves the number of glyphs.
-   *
-   * @return The number of glyphs.
-   */
-  Length GetNumberOfGlyphs() const;
+  void CreateGlyphsPerCharacterTable( CharacterIndex startIndex,
+                                      GlyphIndex startGlyphIndex,
+                                      Length numberOfCharacters );
 
   /**
    * @brief Retrieves glyphs in the given buffer.
@@ -113,304 +97,195 @@ public:
                   GlyphIndex glyphIndex,
                   Length numberOfGlyphs ) const;
 
-  /**
-   * Retrieves a glyph.
-   *
-   * @param[in] glyphIndex Index to a glyph.
-   *
-   * @return A glyph.
-   */
-  const GlyphInfo& GetGlyphInfo( GlyphIndex glyphIndex ) const;
-
-  /**
-   * Replaces glyphs.
-   *
-   * If the @p numberOfGlyphsToRemove is zero, this operation is like an insert.
-   * If the @p numberOfGlyphsToInsert is zero, this operation is like a remove.
-   *
-   * @param[in] glyphIndex Where to replace the glyphs.
-   * @param[in] numberOfGlyphsToRemove The number of glyphs to be removed.
-   * @param[in] glyphs Pointer to a buffer with the new glyphs.
-   * @param[in] numberOfCharacters Pointer to a buffer with the number of characters per glyph.
-   * @param[in] numberOfGlyphsToInsert The number of new glyphs in the buffer.
-   */
-  void ReplaceGlyphs( GlyphIndex glyphIndex,
-                      Length numberOfGlyphsToRemove,
-                      const GlyphInfo* const glyphs,
-                      const Length* const numberOfCharacters,
-                      Length numberOfGlyphsToInsert );
-
-  // Character <--> Glyph conversion
+  // Position interface
 
   /**
-   * @brief Retrieves the first character in the logical model which a glyph represents.
+   * @brief Retrieves the glyph positions.
    *
-   * @note After shaping several characters may be represented by the same glyph.
-   * Alternatively several glyphs may be required to display a character.
-   * @param[in] glyphIndex The glyph index.
-   * @return The character index.
+   * @pre The size of the @p positions buffer needs to be big enough to copy the @p numberOfGlyphs positions.
+   * @param[out] glyphPositions Pointer to a buffer where the glyph positions are copied.
+   * @param[in] glyphIndex Index to the first glyph position.
+   * @param[in] numberOfGlyphs The number of positions to be copied.
    */
-  CharacterIndex GetCharacterIndex( GlyphIndex glyphIndex ) const;
+  void GetGlyphPositions( Vector2* glyphPositions,
+                          GlyphIndex glyphIndex,
+                          Length numberOfGlyphs ) const;
 
-  /**
-   * @brief Query the number of characters the glyph represents.
-   *
-   * @param[in] glyphIndex The glyph index.
-   * @return The number of characters represented by the glyph.
-   */
-  Length GetCharactersPerGlyph( GlyphIndex glyphIndex ) const;
+  // Line interface.
 
   /**
-   * Retrieves the first glyph in the visual model which represents a given character.
+   * @brief Retrieves the number of lines and the index to the first line where the given range of glyphs is laid out.
    *
-   * @note After shaping several characters may be represented by the same glyph.
-   * Alternatively several glyphs may be required to display a character.
-   * @param[in] characterIndex The character index.
-   * @return The glyph index.
+   * @param[in] glyphIndex Index to the first glyph.
+   * @param[in] numberOfGlyphs The number of glyph.
+   * @param[out] firstLine Index to the line containing the glyph index.
+   * @param[out] numberOfLines The number of lines.
    */
-  GlyphIndex GetGlyphIndex( CharacterIndex characterIndex ) const;
+  void GetNumberOfLines( GlyphIndex glyphIndex,
+                         Length numberOfGlyphs,
+                         LineIndex& firstLine,
+                         Length& numberOfLines ) const;
 
   /**
-   * Retrieves the whole or part of the character to glyph conversion map.
+   * @brief Retrieves the lines where the given range of glyphs is laid out.
    *
-   * The size of the buffer needs to be big enough to copy the @p numberOfCharacters.
+   * The size of the @p lines buffer needs to be big enough to copy the @p numberOfLines.
    *
-   * @param[out] characterToGlyphMap Pointer to a buffer where the conversion map is copied.
-   * @param[in] characterIndex Index to the first character.
-   * @param[in] numberOfCharacters The number of characters.
+   * @param[out] lines Pointer to a buffer where the lines are copied.
+   * @param[in] glyphIndex Index to the first glyphs of the range.
+   * @param[in] numberOfGlyphs Number of glyphs in the range.
    */
-  void GetCharacterToGlyphMap( GlyphIndex* characterToGlyphMap,
-                               CharacterIndex characterIndex,
-                               Length numberOfCharacters ) const;
+  void GetLinesOfGlyphRange( LineRun* lines,
+                             GlyphIndex glyphIndex,
+                             Length numberOfGlyphs ) const;
 
   /**
-   * Retrieves the whole or part of the glyph to character conversion map.
+   * @brief Retrieves the line index where the character is laid-out.
    *
-   * The size of the buffer needs to be big enough to copy the @p numberOfGlyphs.
+   * @param[in] characterIndex The character's index.
    *
-   * @param[out] glyphToCharacter Pointer to a buffer where the conversion map is copied.
-   * @param[in] glyphIndex Index to the first glyph.
-   * @param[in] numberOfGlyphs The number of glyphs.
+   * @return The line index.
    */
-  void GetGlyphToCharacterMap( CharacterIndex* glyphToCharacter,
-                               GlyphIndex glyphIndex,
-                               Length numberOfGlyphs ) const;
+  LineIndex GetLineOfCharacter( CharacterIndex characterIndex );
 
-  /**
-   * Retrieves for each glyph the number of characters the glyph represents.
-   *
-   * @param[out] charactersPerGlyph Pointer to a buffer where the number of characters for each glyph are copied.
-   * @param[in] glyphIndex Index to the first glyph.
-   * @param[in] numberOfGlyphs The number of glyphs.
-   */
-  void GetCharactersPerGlyphMap( Length* charactersPerGlyph,
-                                 GlyphIndex glyphIndex,
-                                 Length numberOfGlyphs ) const;
+  // Underline runs
 
   /**
-   * Retrieves for each character the number of glyphs the character is shaped.
+   * @brief Retrieves the underline runs.
    *
-   * @param[out] glyphsPerCharacter Pointer to a buffer where the number of glyphs for each character are copied.
-   * @param[in] characterIndex Index to the first character.
-   * @param[in] numberOfCharacters The number of characters.
+   * @param[out] underlineRuns Pointer to a buffer where the underline runs are copied.
+   * @param[in] index Index of the first underline run to be copied.
+   * @param[in] numberOfRuns Number of underline runs to be copied.
    */
-  void GetGlyphsPerCharacterMap( Length* glyphsPerCharacter,
-                                 CharacterIndex characterIndex,
-                                 Length numberOfCharacters ) const;
+  void GetUnderlineRuns( GlyphRun* underlineRuns,
+                         UnderlineRunIndex index,
+                         Length numberOfRuns ) const;
 
-  // Position interface
+  // Size interface
 
   /**
-   * @brief Replaces any glyph positions previously set.
-   *
-   * @note If the number of glyphs is zero the position buffer is cleared.
+   * @brief Sets the natural size.
    *
-   * @param[in] glyphPositions An array of visual positions for each glyph.
-   * @param[in] numberOfGlyphs The number of positions.
+   * @param[in] size The text's natural size.
    */
-  void SetGlyphPositions( const Vector2* glyphPositions,
-                          Length numberOfGlyphs );
+  void SetNaturalSize( const Vector2& size  );
 
   /**
-   * Retrieves the number of glyph positions set.
+   * @brief Retrieves the natural size.
    *
-   * @note This may be less than the number of glyphs in the model.
-   * @return The number of glyphs.
+   * @return The text's natural size.
    */
-  Length GetNumberOfGlyphPositions() const;
+  const Vector2& GetNaturalSize() const;
 
   /**
-   * @brief Retrieves the glyph positions.
+   * @brief Sets the text's layout size.
    *
-   * @pre The size of the @p positions buffer needs to be big enough to copy the @p numberOfGlyphs positions.
-   * @param[out] glyphPositions Pointer to a buffer where the glyph positions are copied.
-   * @param[in] glyphIndex Index to the first glyph position.
-   * @param[in] numberOfGlyphs The number of positions to be copied.
+   * @param[in] size The text's size.
    */
-  void GetGlyphPositions( Vector2* glyphPositions,
-                          GlyphIndex glyphIndex,
-                          Length numberOfGlyphs ) const;
+  void SetLayoutSize( const Vector2& size );
 
   /**
-   * Retrieve the glyph's position of the given glyph.
-   *
-   * @param[in] glyphIndex Index to the glyph.
+   * @brief Retrieves the text's layout size.
    *
-   * @return The glyph's position.
+   * @return The text's size.
    */
-  const Vector2& GetGlyphPosition( GlyphIndex glyphIndex ) const;
+  const Vector2& GetLayoutSize() const;
 
   /**
-   * Replaces glyph's positions.
+   * @brief Set the text's color
    *
-   * If the @p numberOfGlyphsToRemove is zero, this operation is like an insert.
-   * If the @p numberOfGlyphsToInsert is zero, this operation is like a remove.
-   *
-   * @param[in] glyphIndex Where to replace the glyph's positions.
-   * @param[in] numberOfGlyphsToRemove The number of glyph's positions to be removed.
-   * @param[in] positions Pointer to a buffer with the new glyph's positions.
-   * @param[in] numberOfGlyphsToInsert The number of new glyph's positions in the buffer.
+   * @param[in] textColor The text's color
    */
-  void ReplaceGlyphPositions( GlyphIndex glyphIndex,
-                              Length numberOfGlyphsToRemove,
-                              const Vector2* const positions,
-                              Length numberOfGlyphsToInsert );
-
-  // Line interface.
+  void SetTextColor( const Vector4& textColor );
 
   /**
-   * Sets the lines.
-   *
-   * Replaces any lines previously set.
-   *
-   * Every line is an item run containing the index to the first glyph of the line and the number of glyphs.
-   *
-   * @note If the number of lines is zero or the pointer is NULL, the lines buffer is cleared.
+   * @brief Retrieve the text's color
    *
-   * @param[in] lines Pointer to a buffer containing all the line runs.
-   * @param[in] numberOfLines The number of lines in the buffer.
+   * @return The text's color
    */
-  void SetLines( const LineRun* const lines,
-                 Length numberOfLines );
+  const Vector4& GetTextColor() const;
 
   /**
-   * Retrieves the number of lines of the whole text.
+   * @brief Sets the text's shadow offset.
    *
-   * @return The number of lines.
+   * @param[in] shadowOffset The shadow offset, 0,0 indicates no shadow.
    */
-  Length GetNumberOfLines() const;
+  void SetShadowOffset( const Vector2& shadowOffset );
 
   /**
-   * Retrieves lines.
-   *
-   * The size of the @p lines buffer needs to be big enough to copy the @p numberOfLines.
+   * @brief Retrieves the text's shadow offset.
    *
-   * @param[out] lines Pointer to a buffer where the lines are copied.
-   * @param[in] lineIndex Index to the first line.
-   * @param[in] numberOfLines Number of lines to be copied.
+   * @return The text's shadow offset, 0,0 indicates no shadow.
    */
-  void GetLines( LineRun* lines,
-                 LineIndex lineIndex,
-                 Length numberOfLines ) const;
+  const Vector2& GetShadowOffset() const;
 
   /**
-   * Retrieves the number of lines and the index to the first line where the given range of glyphs is laid out.
-   *
-   * @param[in] glyphIndex Index to the first glyph.
-   * @param[in] numberOfGlyphs The number of glyph.
-   * @param[out] firstLine Index to the line containing the glyph index.
-   * @param[out] numberOfLines The number of lines.
-   */
-  void GetNumberOfLines( GlyphIndex glyphIndex,
-                         Length numberOfGlyphs,
-                         LineIndex& firstLine,
-                         Length& numberOfLines ) const;
-  /**
-   * Retrieves the lines where the given range of glyphs is laid out.
-   *
-   * The size of the @p lines buffer needs to be big enough to copy the @p numberOfLines.
+   * @brief Sets the text's shadow color.
    *
-   * @param[out] lines Pointer to a buffer where the lines are copied.
-   * @param[in] glyphIndex Index to the first glyphs of the range.
-   * @param[in] numberOfGlyphs Number of glyphs in the range.
+   * @param[in] shadowColor The shadow color.
    */
-  void GetLinesOfGlyphRange( LineRun* lines,
-                             GlyphIndex glyphIndex,
-                             Length numberOfGlyphs ) const;
+  void SetShadowColor( const Vector4& shadowColor );
 
   /**
-   * Replaces lines for the given range of glyphs.
-   *
-   * If the @p numberOfGlyphsToRemove is zero, this operation is like an insert.
-   * If the @p numberOfGlyphsToInsert is zero, this operation is like a remove.
+   * @brief Retrieves the text's shadow color.
    *
-   * @param[in] glyphIndex Index of the first glyph where to replace the line info.
-   * @param[in] numberOfGlyphsToRemove The number of glyphs to be the line info removed.
-   * @param[in] lines Pointer to a buffer with the lines.
-   * @param[in] numberOfGlyphsToInsert The number of characters to be the line info inserted.
+   * @return The text's shadow color.
    */
-  void ReplaceLines( GlyphIndex glyphIndex,
-                     Length numberOfGlyphsToRemove,
-                     const LineRun* const lines,
-                     Length numberOfGlyphsToInsert );
-
-  // Size interface
+  const Vector4& GetShadowColor() const;
 
   /**
-   * Sets the natural size.
+   * @brief Sets the text's underline color.
    *
-   * @param[in] size The text's natural size.
+   * @param[in] color The text's underline color.
    */
-  void SetNaturalSize( const Vector2& size  );
+  void SetUnderlineColor( const Vector4& color );
 
   /**
-   * Retrieves the natural size.
+   * @brief Retrieves the text's underline color.
    *
-   * @return The text's natural size.
+   * @return The text's underline color.
    */
-  const Vector2& GetNaturalSize() const;
+  const Vector4& GetUnderlineColor() const;
 
   /**
-   * Sets the text's actual size after it has been laid out.
+   * @brief Sets the text underline flag.
    *
-   * @param[in] size The text's size.
+   * @param[in] enabled true if underlined.
    */
-  void SetActualSize( const Vector2& size );
+  void SetUnderlineEnabled( bool enabled );
 
   /**
-   * Retrieves the text's actual size after it has been laid out.
+   * @brief Returns whether the text is underlined or not.
    *
-   * @return The text's size.
+   * @return underline state.
    */
-  const Vector2& GetActualSize() const;
+  bool IsUnderlineEnabled() const;
 
   /**
-   * @brief Sets the text's shadow offset.
-   *
-   * @param[in] shadowOffset The shadow offset, 0,0 indicates no shadow.
+   * @brief Clear the caches.
    */
-  void SetShadowOffset( const Vector2& shadowOffset );
+  void ClearCaches();
 
   /**
-   * @brief Retrieves the text's shadow offset.
+   * @brief Set the override used for underline height, 0 indicates height will be come from font metrics
    *
-   * @return The text's shadow offset, 0,0 indicates no shadow.
+   * @param[in] height The height in pixels of the underline
    */
-  const Vector2& GetShadowOffset() const;
+  void SetUnderlineHeight( float height );
 
   /**
-   * @brief Sets the text's shadow color.
+   * @brief Retrieves the underline height override
    *
-   * @param[in] shadowColor The shadow color.
+   * @return Returns the override height for an underline, 0 indicates that font metrics will determine the height
    */
-  void SetShadowColor( const Vector4& shadowColor );
+  float GetUnderlineHeight() const;
 
   /**
-   * @brief Retrieves the text's shadow color.
+   * @brief Retrieves the number of underline runs.
    *
-   * @return The text's shadow color.
+   * @return The number of underline runs.
    */
-  const Vector4& GetShadowColor() const;
+  Length GetNumberOfUnderlineRuns() const;
 
 protected:
 
@@ -434,21 +309,36 @@ private:
 
 public:
 
-  Vector<GlyphInfo>      mGlyphs;             ///< For each glyph, the font's id, glyph's index within the font and glyph's metrics.
-  Vector<CharacterIndex> mGlyphsToCharacters; ///< For each glyph, the index of the first character.
-  Vector<GlyphIndex>     mCharactersToGlyph;  ///< For each character, the index of the first glyph.
-  Vector<Length>         mCharactersPerGlyph; ///< For each glyph, the number of characters that form the glyph.
-  Vector<Length>         mGlyphsPerCharacter; ///< For each character, the number of glyphs that are shaped.
-  Vector<Vector2>        mGlyphPositions;     ///< For each glyph, the position.
-  Vector<LineRun>        mLines;              ///< The laid out lines.
-
-  Vector2                mShadowOffset;       ///< Offset for drop shadow, 0.0 indicates no shadow
-  Vector4                mShadowColor;        ///< Color of drop shadow
+  Vector<GlyphInfo>      mGlyphs;               ///< For each glyph, the font's id, glyph's index within the font and glyph's metrics.
+  Vector<CharacterIndex> mGlyphsToCharacters;   ///< For each glyph, the index of the first character.
+  Vector<GlyphIndex>     mCharactersToGlyph;    ///< For each character, the index of the first glyph.
+  Vector<Length>         mCharactersPerGlyph;   ///< For each glyph, the number of characters that form the glyph.
+  Vector<Length>         mGlyphsPerCharacter;   ///< For each character, the number of glyphs that are shaped.
+  Vector<Vector2>        mGlyphPositions;       ///< For each glyph, the position.
+  Vector<LineRun>        mLines;                ///< The laid out lines.
+  Vector<GlyphRun>       mUnderlineRuns;        ///< Runs of glyphs that are underlined.
+  Vector<Vector4>        mColors;               ///< Colors of the glyphs.
+  Vector<ColorIndex>     mColorIndices;         ///< Indices to the vector of colors for each glyphs.
+
+  Vector4                mTextColor;            ///< The text color
+  Vector4                mShadowColor;          ///< Color of drop shadow
+  Vector4                mUnderlineColor;       ///< Color of underline
+  Size                   mControlSize;          ///< The size of the UI control.
+  Vector2                mShadowOffset;         ///< Offset for drop shadow, 0 indicates no shadow
+  float                  mUnderlineHeight;      ///< Fixed height for underline to override font metrics.
 
 private:
 
   Size                   mNaturalSize;        ///< Size of the text with no line wrapping.
-  Size                   mActualSize;         ///< Size of the laid-out text considering the layout properties set.
+  Size                   mLayoutSize;         ///< Size of the laid-out text considering the layout properties set.
+
+  // Caches to increase performance in some consecutive operations.
+  LineIndex mCachedLineIndex; ///< Used to increase performance in consecutive calls to GetLineOfGlyph() or GetLineOfCharacter() with consecutive glyphs or characters.
+
+public:
+
+  bool                   mUnderlineEnabled:1;   ///< Underline enabled flag
+  bool                   mUnderlineColorSet:1;  ///< Has the underline color been explicitly set?
 };
 
 } // namespace Text