Remove useless iteration when debug mode
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / text-controller-background-actor.cpp
index dda514f..bc7c79b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2022 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.
 // INTERNAL INCLUDES
 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
 #include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
+#include <dali-toolkit/internal/text/cursor-helper-functions.h>
+#include <dali-toolkit/internal/text/rendering/styles/character-spacing-helper-functions.h>
 #include <dali-toolkit/internal/text/text-view.h>
 
 namespace Dali::Toolkit::Text
 {
-
 namespace
 {
 struct BackgroundVertex
@@ -44,7 +45,19 @@ struct BackgroundMesh
 };
 } // unnamed namespace
 
-Actor CreateControllerBackgroundActor(const View& textView, const VisualModelPtr& textVisualModel, Shader& textShaderBackground)
+Length CalculateBackgroundLineHeight(LineRun lineRun)
+{
+  Length height = lineRun.ascender + -(lineRun.descender);
+
+  if(lineRun.lineSpacing > 0)
+  {
+    height += lineRun.lineSpacing;
+  }
+
+  return height;
+}
+
+Actor CreateControllerBackgroundActor(const View& textView, const VisualModelPtr& textVisualModel, const LogicalModelPtr& textLogicalModel, Shader& textShaderBackground)
 {
   // NOTE: Currently we only support background color for left-to-right text.
 
@@ -63,10 +76,10 @@ Actor CreateControllerBackgroundActor(const View& textView, const VisualModelPtr
     const LineRun* lineRun         = textVisualModel->mLines.Begin();
     float          alignmentOffset = lineRun->alignmentOffset;
     numberOfGlyphs                 = textView.GetGlyphs(glyphs.Begin(),
-                                                     positions.Begin(),
-                                                     alignmentOffset,
-                                                     0u,
-                                                     numberOfGlyphs);
+                                        positions.Begin(),
+                                        alignmentOffset,
+                                        0u,
+                                        numberOfGlyphs);
 
     glyphs.Resize(numberOfGlyphs);
     positions.Resize(numberOfGlyphs);
@@ -86,13 +99,19 @@ Actor CreateControllerBackgroundActor(const View& textView, const VisualModelPtr
     const Vector4* const    backgroundColorsBuffer       = textView.GetBackgroundColors();
     const ColorIndex* const backgroundColorIndicesBuffer = textView.GetBackgroundColorIndices();
     const Vector4&          defaultBackgroundColor       = textVisualModel->IsBackgroundEnabled() ? textVisualModel->GetBackgroundColor() : Color::TRANSPARENT;
+    const float             modelCharacterSpacing        = textVisualModel->GetCharacterSpacing();
+    Vector<CharacterIndex>& glyphToCharacterMap          = textVisualModel->mGlyphsToCharacters;
+    const CharacterIndex*   glyphToCharacterMapBuffer    = glyphToCharacterMap.Begin();
+    float                   calculatedAdvance            = 0.f;
+
+    // Get the character-spacing runs.
+    const Vector<CharacterSpacingGlyphRun>& characterSpacingGlyphRuns = textVisualModel->GetCharacterSpacingGlyphRuns();
 
     Vector4   quad;
     uint32_t  numberOfQuads = 0u;
     Length    yLineOffset   = 0;
     Length    prevLineIndex = 0;
     LineIndex lineIndex;
-    Length    numberOfLines;
 
     for(uint32_t i = 0, glyphSize = glyphs.Size(); i < glyphSize; ++i)
     {
@@ -105,45 +124,53 @@ Actor CreateControllerBackgroundActor(const View& textView, const VisualModelPtr
       const bool       isDefaultBackgroundColor = (0u == backgroundColorIndex);
       const Vector4&   backgroundColor          = isDefaultBackgroundColor ? defaultBackgroundColor : *(backgroundColorsBuffer + backgroundColorIndex - 1u);
 
-      textVisualModel->GetNumberOfLines(i, 1, lineIndex, numberOfLines);
-      Length lineHeight = lineRun[lineIndex].ascender + -(lineRun[lineIndex].descender) + lineRun[lineIndex].lineSpacing;
+      lineIndex         = textVisualModel->GetLineOfGlyph(i);
+      Length lineHeight = CalculateBackgroundLineHeight(lineRun[lineIndex]);
 
       if(lineIndex != prevLineIndex)
       {
-        yLineOffset += lineHeight;
+        yLineOffset += CalculateBackgroundLineHeight(lineRun[prevLineIndex]);
+
+        if(lineRun[prevLineIndex].lineSpacing < 0)
+        {
+          yLineOffset += lineRun[prevLineIndex].lineSpacing;
+        }
       }
 
       // Only create quads for glyphs with a background color
       if(backgroundColor != Color::TRANSPARENT)
       {
+        const float characterSpacing = GetGlyphCharacterSpacing(i, characterSpacingGlyphRuns, modelCharacterSpacing);
+
         const Vector2 position = *(positionsBuffer + i);
+        calculatedAdvance      = GetCalculatedAdvance(*(textLogicalModel->mText.Begin() + (*(glyphToCharacterMapBuffer + i))), characterSpacing, glyph.advance);
 
         if(i == 0u && glyphSize == 1u) // Only one glyph in the whole text
         {
           quad.x = position.x;
           quad.y = yLineOffset;
-          quad.z = quad.x + std::max(glyph.advance, glyph.xBearing + glyph.width);
+          quad.z = quad.x + std::max(calculatedAdvance, glyph.xBearing + glyph.width);
           quad.w = lineHeight;
         }
         else if((lineIndex != prevLineIndex) || (i == 0u)) // The first glyph in the line
         {
           quad.x = position.x;
           quad.y = yLineOffset;
-          quad.z = quad.x - glyph.xBearing + glyph.advance;
+          quad.z = quad.x - glyph.xBearing + calculatedAdvance;
           quad.w = quad.y + lineHeight;
         }
         else if(i == glyphSize - 1u) // The last glyph in the whole text
         {
           quad.x = position.x - glyph.xBearing;
           quad.y = yLineOffset;
-          quad.z = quad.x + std::max(glyph.advance, glyph.xBearing + glyph.width);
+          quad.z = quad.x + std::max(calculatedAdvance, glyph.xBearing + glyph.width);
           quad.w = quad.y + lineHeight;
         }
         else // The glyph in the middle of the text
         {
           quad.x = position.x - glyph.xBearing;
           quad.y = yLineOffset;
-          quad.z = quad.x + glyph.advance;
+          quad.z = quad.x + calculatedAdvance;
           quad.w = quad.y + lineHeight;
         }