Merge "fix issue in negative line spacing with key arrow down" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / visual-model-impl.cpp
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/text/visual-model-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <memory.h>
23
24 namespace Dali
25 {
26 namespace Toolkit
27 {
28 namespace Text
29 {
30 VisualModelPtr VisualModel::New()
31 {
32   return VisualModelPtr(new VisualModel());
33 }
34
35 void VisualModel::CreateCharacterToGlyphTable(CharacterIndex startIndex,
36                                               GlyphIndex     startGlyphIndex,
37                                               Length         numberOfCharacters)
38 {
39   if(0u == numberOfCharacters)
40   {
41     // Nothing to do.
42     return;
43   }
44
45   DALI_ASSERT_DEBUG(mGlyphsPerCharacter.Count() != 0u);
46
47   // Get the total number of characters.
48   const Length totalNumberOfCharacters = (0u == mGlyphsToCharacters.Count()) ? 0u : *(mGlyphsToCharacters.End() - 1u) + *(mCharactersPerGlyph.End() - 1u); // Index to the first character + the number of characters that form the last glyph.
49
50   // Whether the current buffer is being updated or is set from scratch.
51   const bool updateCurrentBuffer = numberOfCharacters < totalNumberOfCharacters;
52
53   Vector<GlyphIndex> newCharactersToGlyph;
54   GlyphIndex*        charactersToGlyphBuffer = NULL;
55
56   // 1) Reserve some space for the glyph indices to avoid reallocations.
57   if(updateCurrentBuffer)
58   {
59     newCharactersToGlyph.Resize(numberOfCharacters);
60     charactersToGlyphBuffer = newCharactersToGlyph.Begin();
61   }
62   else
63   {
64     mCharactersToGlyph.Resize(numberOfCharacters);
65     charactersToGlyphBuffer = mCharactersToGlyph.Begin() + startIndex;
66   }
67
68   const Length* const glyphsPerCharacterBuffer = mGlyphsPerCharacter.Begin();
69
70   // 2) Traverse the glyphs and set the glyph indices per character.
71
72   // Index to the glyph.
73   GlyphIndex           glyphIndex                = startGlyphIndex;
74   CharacterIndex       characterIndex            = startIndex;
75   const CharacterIndex lastCharacterIndexPlusOne = startIndex + numberOfCharacters;
76   for(Vector<Length>::ConstIterator it    = mCharactersPerGlyph.Begin() + glyphIndex,
77                                     endIt = mCharactersPerGlyph.End();
78       (it != endIt) && (characterIndex < lastCharacterIndexPlusOne);
79       ++it)
80   {
81     const Length numberOfCharactersPerGlyph = *it;
82
83     Length numberOfGlyphs = 0u;
84     // Set the glyph indices.
85     for(Length index = 0u; index < numberOfCharactersPerGlyph; ++index, ++characterIndex)
86     {
87       *charactersToGlyphBuffer = glyphIndex;
88       numberOfGlyphs += *(glyphsPerCharacterBuffer + characterIndex);
89       ++charactersToGlyphBuffer;
90     }
91     glyphIndex += numberOfGlyphs;
92   }
93
94   // If the character to glyph buffer is updated, it needs to be inserted in the model.
95   if(updateCurrentBuffer)
96   {
97     // Update the indices.
98     const Length numberOfGlyphs = glyphIndex - startGlyphIndex;
99     for(Vector<Length>::Iterator it    = mCharactersToGlyph.Begin() + startIndex,
100                                  endIt = mCharactersToGlyph.End();
101         it != endIt;
102         ++it)
103     {
104       *it += numberOfGlyphs;
105     }
106
107     mCharactersToGlyph.Insert(mCharactersToGlyph.Begin() + startIndex,
108                               newCharactersToGlyph.Begin(),
109                               newCharactersToGlyph.End());
110   }
111 }
112
113 void VisualModel::CreateGlyphsPerCharacterTable(CharacterIndex startIndex,
114                                                 GlyphIndex     startGlyphIndex,
115                                                 Length         numberOfCharacters)
116 {
117   if(0u == numberOfCharacters)
118   {
119     // Nothing to do.
120     return;
121   }
122
123   // Get the total number of characters.
124   const Length totalNumberOfCharacters = (0u == mGlyphsToCharacters.Count()) ? 0u : *(mGlyphsToCharacters.End() - 1u) + *(mCharactersPerGlyph.End() - 1u); // Index to the first character + the number of characters that form the last glyph.
125
126   // Whether the current buffer is being updated or is set from scratch.
127   const bool updateCurrentBuffer = numberOfCharacters < totalNumberOfCharacters;
128
129   Vector<Length> newGlyphsPerCharacter;
130   Length*        glyphsPerCharacterBuffer = NULL;
131
132   // 1) Reserve some space for the glyphs per character to avoid reallocations.
133   if(updateCurrentBuffer)
134   {
135     newGlyphsPerCharacter.Resize(numberOfCharacters);
136     glyphsPerCharacterBuffer = newGlyphsPerCharacter.Begin();
137   }
138   else
139   {
140     mGlyphsPerCharacter.Resize(numberOfCharacters);
141     glyphsPerCharacterBuffer = mGlyphsPerCharacter.Begin() + startIndex;
142   }
143
144   // 2) Traverse the glyphs and set the number of glyphs per character.
145
146   Length traversedCharacters = 0;
147
148   // The number of 'characters per glyph' equal to zero.
149   Length zeroCharactersPerGlyph = 0u;
150
151   for(Vector<Length>::ConstIterator it    = mCharactersPerGlyph.Begin() + startGlyphIndex,
152                                     endIt = mCharactersPerGlyph.End();
153       (it != endIt) && (traversedCharacters < numberOfCharacters);
154       ++it)
155   {
156     const Length numberOfCharactersPerGlyph = *it;
157     traversedCharacters += numberOfCharactersPerGlyph;
158
159     // Set the glyphs per character.
160     if(0u == numberOfCharactersPerGlyph)
161     {
162       ++zeroCharactersPerGlyph;
163     }
164     else
165     {
166       const Length numberOfZeroGlyphsPerCharacter = (numberOfCharactersPerGlyph - 1u);
167       for(Length zeroIndex = 0u; zeroIndex < numberOfZeroGlyphsPerCharacter; ++zeroIndex)
168       {
169         *glyphsPerCharacterBuffer = 0u;
170
171         // Point to the next position in the buffer.
172         ++glyphsPerCharacterBuffer;
173       }
174
175       *glyphsPerCharacterBuffer = zeroCharactersPerGlyph + 1u;
176
177       zeroCharactersPerGlyph = 0u;
178
179       // Point to the next position in the buffer.
180       ++glyphsPerCharacterBuffer;
181     }
182   }
183
184   // If the glyphs per character buffer is updated, it needs to be inserted in the model.
185   if(updateCurrentBuffer)
186   {
187     mGlyphsPerCharacter.Insert(mGlyphsPerCharacter.Begin() + startIndex,
188                                newGlyphsPerCharacter.Begin(),
189                                newGlyphsPerCharacter.End());
190   }
191 }
192
193 void VisualModel::GetGlyphs(GlyphInfo* glyphs,
194                             GlyphIndex glyphIndex,
195                             Length     numberOfGlyphs) const
196 {
197   memcpy(glyphs, mGlyphs.Begin() + glyphIndex, numberOfGlyphs * sizeof(GlyphInfo));
198 }
199
200 void VisualModel::GetGlyphPositions(Vector2*   glyphPositions,
201                                     GlyphIndex glyphIndex,
202                                     Length     numberOfGlyphs) const
203 {
204   memcpy(glyphPositions, mGlyphPositions.Begin() + glyphIndex, numberOfGlyphs * sizeof(Vector2));
205 }
206
207 Length VisualModel::GetTotalNumberOfLines() const
208 {
209   return mLines.Size();
210 }
211
212 void VisualModel::GetNumberOfLines(GlyphIndex glyphIndex,
213                                    Length     numberOfGlyphs,
214                                    LineIndex& firstLine,
215                                    Length&    numberOfLines) const
216 {
217   // Initialize the number of lines and the first line.
218   firstLine           = 0u;
219   numberOfLines       = 0u;
220   bool firstLineFound = false;
221
222   const GlyphIndex lastGlyphIndex = glyphIndex + numberOfGlyphs;
223
224   // Traverse the lines and count those lines within the range of glyphs.
225   for(Vector<LineRun>::ConstIterator it    = mLines.Begin(),
226                                      endIt = mLines.End();
227       it != endIt;
228       ++it)
229   {
230     const LineRun& line = *it;
231
232     if((line.glyphRun.glyphIndex + line.glyphRun.numberOfGlyphs > glyphIndex) &&
233        (lastGlyphIndex > line.glyphRun.glyphIndex))
234     {
235       firstLineFound = true;
236       ++numberOfLines;
237     }
238     else if((line.glyphRunSecondHalf.glyphIndex + line.glyphRunSecondHalf.numberOfGlyphs > glyphIndex) &&
239             (lastGlyphIndex > line.glyphRunSecondHalf.glyphIndex))
240     {
241       firstLineFound = true;
242       ++numberOfLines;
243     }
244     else if(lastGlyphIndex <= line.glyphRun.glyphIndex)
245     {
246       // nothing else to do.
247       break;
248     }
249
250     if(!firstLineFound)
251     {
252       ++firstLine;
253     }
254   }
255 }
256
257 void VisualModel::GetLinesOfGlyphRange(LineRun*   lines,
258                                        GlyphIndex glyphIndex,
259                                        Length     numberOfGlyphs) const
260 {
261   LineIndex firstLine     = 0u;
262   Length    numberOfLines = 0u;
263
264   GetNumberOfLines(glyphIndex,
265                    numberOfGlyphs,
266                    firstLine,
267                    numberOfLines);
268
269   memcpy(lines, mLines.Begin() + firstLine, numberOfLines * sizeof(LineRun));
270 }
271
272 LineIndex VisualModel::GetLineOfCharacter(CharacterIndex characterIndex)
273 {
274   // 1) Check line is empty or not.
275   if(mLines.Empty())
276   {
277     return 0u;
278   }
279
280   // 2) Check in the cached line.
281   const LineRun& lineRun = *(mLines.Begin() + mCachedLineIndex);
282   if((lineRun.characterRun.characterIndex <= characterIndex) &&
283      (characterIndex < lineRun.characterRun.characterIndex + lineRun.characterRun.numberOfCharacters))
284   {
285     return mCachedLineIndex;
286   }
287
288   // 3) Is not in the cached line. Check in the other lines.
289   LineIndex index = characterIndex < lineRun.characterRun.characterIndex ? 0u : mCachedLineIndex + 1u;
290
291   for(Vector<LineRun>::ConstIterator it    = mLines.Begin() + index,
292                                      endIt = mLines.End();
293       it != endIt;
294       ++it, ++index)
295   {
296     const LineRun& lineRun = *it;
297
298     if(characterIndex < lineRun.characterRun.characterIndex + lineRun.characterRun.numberOfCharacters)
299     {
300       mCachedLineIndex = index;
301       break;
302     }
303   }
304
305   return index;
306 }
307
308 void VisualModel::GetUnderlineRuns(UnderlinedGlyphRun* underlineRuns,
309                                    UnderlineRunIndex   index,
310                                    Length              numberOfRuns) const
311 {
312   memcpy(underlineRuns,
313          mUnderlineRuns.Begin() + index,
314          numberOfRuns * sizeof(UnderlinedGlyphRun));
315 }
316
317 void VisualModel::SetNaturalSize(const Vector2& size)
318 {
319   mNaturalSize = size;
320 }
321
322 const Vector2& VisualModel::GetNaturalSize() const
323 {
324   return mNaturalSize;
325 }
326
327 void VisualModel::SetLayoutSize(const Vector2& size)
328 {
329   mLayoutSize = size;
330 }
331
332 const Vector2& VisualModel::GetLayoutSize() const
333 {
334   return mLayoutSize;
335 }
336
337 void VisualModel::SetTextColor(const Vector4& textColor)
338 {
339   mTextColor = textColor;
340
341   if(!mUnderlineColorSet)
342   {
343     mUnderlineColor = textColor;
344   }
345 }
346
347 void VisualModel::SetShadowOffset(const Vector2& shadowOffset)
348 {
349   mShadowOffset = shadowOffset;
350 }
351
352 void VisualModel::SetShadowColor(const Vector4& shadowColor)
353 {
354   mShadowColor = shadowColor;
355 }
356
357 void VisualModel::SetShadowBlurRadius(const float& shadowBlurRadius)
358 {
359   mShadowBlurRadius = shadowBlurRadius;
360 }
361
362 void VisualModel::SetUnderlineColor(const Vector4& color)
363 {
364   mUnderlineColor    = color;
365   mUnderlineColorSet = true;
366 }
367
368 void VisualModel::SetOutlineColor(const Vector4& color)
369 {
370   mOutlineColor = color;
371 }
372
373 void VisualModel::SetUnderlineEnabled(bool enabled)
374 {
375   mUnderlineEnabled = enabled;
376 }
377
378 void VisualModel::SetUnderlineHeight(float height)
379 {
380   mUnderlineHeight = height;
381 }
382
383 void VisualModel::SetUnderlineType(Text::Underline::Type type)
384 {
385   mUnderlineType = type;
386 }
387
388 void VisualModel::SetDashedUnderlineWidth(float width)
389 {
390   mDashedUnderlineWidth = width;
391 }
392
393 void VisualModel::SetDashedUnderlineGap(float gap)
394 {
395   mDashedUnderlineGap = gap;
396 }
397
398 void VisualModel::SetOutlineWidth(uint16_t width)
399 {
400   mOutlineWidth = width;
401 }
402
403 void VisualModel::SetBackgroundColor(const Vector4& color)
404 {
405   mBackgroundColor = color;
406 }
407
408 void VisualModel::SetBackgroundEnabled(bool enabled)
409 {
410   mBackgroundEnabled = enabled;
411 }
412
413 void VisualModel::SetMarkupProcessorEnabled(bool enabled)
414 {
415   mMarkupProcessorEnabled = enabled;
416 }
417
418 void VisualModel::SetTextElideEnabled(bool enabled)
419 {
420   mTextElideEnabled = enabled;
421 }
422
423 void VisualModel::SetEllipsisPosition(Toolkit::DevelText::EllipsisPosition::Type ellipsisPosition)
424 {
425   mEllipsisPosition = ellipsisPosition;
426 }
427
428 void VisualModel::SetStartIndexOfElidedGlyphs(GlyphIndex startIndexOfElidedGlyphs)
429 {
430   mStartIndexOfElidedGlyphs = startIndexOfElidedGlyphs;
431 }
432
433 void VisualModel::SetEndIndexOfElidedGlyphs(GlyphIndex endIndexOfElidedGlyphs)
434 {
435   mEndIndexOfElidedGlyphs = endIndexOfElidedGlyphs;
436 }
437
438 void VisualModel::SetFirstMiddleIndexOfElidedGlyphs(GlyphIndex firstMiddleIndexOfElidedGlyphs)
439 {
440   mFirstMiddleIndexOfElidedGlyphs = firstMiddleIndexOfElidedGlyphs;
441 }
442
443 void VisualModel::SetSecondMiddleIndexOfElidedGlyphs(GlyphIndex secondMiddleIndexOfElidedGlyphs)
444 {
445   mSecondMiddleIndexOfElidedGlyphs = secondMiddleIndexOfElidedGlyphs;
446 }
447
448 void VisualModel::SetStrikethroughColor(const Vector4& color)
449 {
450   mStrikethroughColor = color;
451 }
452
453 void VisualModel::SetStrikethroughEnabled(bool enabled)
454 {
455   mStrikethroughEnabled = enabled;
456 }
457
458 void VisualModel::SetStrikethroughHeight(float height)
459 {
460   mStrikethroughHeight = height;
461 }
462
463 void VisualModel::SetCharacterSpacing(float characterSpacing)
464 {
465   mCharacterSpacing = characterSpacing;
466 }
467
468 const Vector4& VisualModel::GetTextColor() const
469 {
470   return mTextColor;
471 }
472
473 const Vector2& VisualModel::GetShadowOffset() const
474 {
475   return mShadowOffset;
476 }
477
478 const Vector4& VisualModel::GetShadowColor() const
479 {
480   return mShadowColor;
481 }
482
483 const float& VisualModel::GetShadowBlurRadius() const
484 {
485   return mShadowBlurRadius;
486 }
487
488 const Vector4& VisualModel::GetUnderlineColor() const
489 {
490   return mUnderlineColor;
491 }
492
493 const Vector4& VisualModel::GetOutlineColor() const
494 {
495   return mOutlineColor;
496 }
497
498 bool VisualModel::IsUnderlineEnabled() const
499 {
500   return mUnderlineEnabled;
501 }
502
503 float VisualModel::GetUnderlineHeight() const
504 {
505   return mUnderlineHeight;
506 }
507
508 Text::Underline::Type VisualModel::GetUnderlineType() const
509 {
510   return mUnderlineType;
511 }
512
513 float VisualModel::GetDashedUnderlineWidth() const
514 {
515   return mDashedUnderlineWidth;
516 }
517
518 float VisualModel::GetDashedUnderlineGap() const
519 {
520   return mDashedUnderlineGap;
521 }
522
523 uint16_t VisualModel::GetOutlineWidth() const
524 {
525   return mOutlineWidth;
526 }
527
528 const Vector4& VisualModel::GetBackgroundColor() const
529 {
530   return mBackgroundColor;
531 }
532
533 const float VisualModel::GetCharacterSpacing() const
534 {
535   return mCharacterSpacing;
536 }
537
538 bool VisualModel::IsBackgroundEnabled() const
539 {
540   return mBackgroundEnabled;
541 }
542
543 bool VisualModel::IsMarkupProcessorEnabled() const
544 {
545   return mMarkupProcessorEnabled;
546 }
547
548 bool VisualModel::IsTextElideEnabled() const
549 {
550   return mTextElideEnabled;
551 }
552
553 Toolkit::DevelText::EllipsisPosition::Type VisualModel::GetEllipsisPosition() const
554 {
555   return mEllipsisPosition;
556 }
557
558 GlyphIndex VisualModel::GetStartIndexOfElidedGlyphs() const
559 {
560   return mStartIndexOfElidedGlyphs;
561 }
562
563 GlyphIndex VisualModel::GetEndIndexOfElidedGlyphs() const
564 {
565   return mEndIndexOfElidedGlyphs;
566 }
567
568 GlyphIndex VisualModel::GetFirstMiddleIndexOfElidedGlyphs() const
569 {
570   return mFirstMiddleIndexOfElidedGlyphs;
571 }
572
573 GlyphIndex VisualModel::GetSecondMiddleIndexOfElidedGlyphs() const
574 {
575   return mSecondMiddleIndexOfElidedGlyphs;
576 }
577
578 Length VisualModel::GetNumberOfUnderlineRuns() const
579 {
580   return mUnderlineRuns.Count();
581 }
582
583 const Vector4& VisualModel::GetStrikethroughColor() const
584 {
585   return mStrikethroughColor;
586 }
587
588 bool VisualModel::IsStrikethroughEnabled() const
589 {
590   return mStrikethroughEnabled;
591 }
592
593 float VisualModel::GetStrikethroughHeight() const
594 {
595   return mStrikethroughHeight;
596 }
597
598 void VisualModel::GetStrikethroughRuns(StrikethroughGlyphRun* strikethroughRuns,
599                                        StrikethroughRunIndex  index,
600                                        Length                 numberOfRuns) const
601 {
602   memcpy(strikethroughRuns,
603          mStrikethroughRuns.Begin() + index,
604          numberOfRuns * sizeof(StrikethroughGlyphRun));
605 }
606
607 Length VisualModel::GetNumberOfStrikethroughRuns() const
608 {
609   return mStrikethroughRuns.Count();
610 }
611
612 Length VisualModel::GetNumberOfCharacterSpacingGlyphRuns() const
613 {
614   return mCharacterSpacingRuns.Count();
615 }
616
617 const Vector<CharacterSpacingGlyphRun>& VisualModel::GetCharacterSpacingGlyphRuns() const
618 {
619   return mCharacterSpacingRuns;
620 }
621
622 void VisualModel::ClearCaches()
623 {
624   mCachedLineIndex = 0u;
625 }
626
627 const Vector<CharacterIndex>& VisualModel::GetGlyphsToCharacters() const
628 {
629   return mGlyphsToCharacters;
630 }
631
632 VisualModel::~VisualModel()
633 {
634 }
635
636 VisualModel::VisualModel()
637 : mGlyphs(),
638   mGlyphsToCharacters(),
639   mCharactersToGlyph(),
640   mCharactersPerGlyph(),
641   mGlyphsPerCharacter(),
642   mGlyphPositions(),
643   mLines(),
644   mTextColor(Color::BLACK),
645   mShadowColor(Color::BLACK),
646   mUnderlineColor(Color::BLACK),
647   mOutlineColor(Color::WHITE),
648   mBackgroundColor(Color::TRANSPARENT),
649   mStrikethroughColor(Color::BLACK),
650   mControlSize(),
651   mShadowOffset(),
652   mUnderlineHeight(0.0f),
653   mStrikethroughHeight(0.0f),
654   mUnderlineType(Text::Underline::SOLID),
655   mDashedUnderlineWidth(2.0f),
656   mDashedUnderlineGap(1.0f),
657   mShadowBlurRadius(0.0f),
658   mOutlineWidth(0u),
659   mNaturalSize(),
660   mLayoutSize(),
661   mCachedLineIndex(0u),
662   mEllipsisPosition(DevelText::EllipsisPosition::END),
663   mStartIndexOfElidedGlyphs(0u),
664   mEndIndexOfElidedGlyphs(0u),
665   mFirstMiddleIndexOfElidedGlyphs(0u),
666   mSecondMiddleIndexOfElidedGlyphs(0u),
667   mTextElideEnabled(false),
668   mUnderlineEnabled(false),
669   mUnderlineColorSet(false),
670   mBackgroundEnabled(false),
671   mMarkupProcessorEnabled(false),
672   mStrikethroughEnabled(false),
673   mCharacterSpacing(0.0f)
674
675 {
676 }
677
678 } // namespace Text
679
680 } // namespace Toolkit
681
682 } // namespace Dali