There is a problem that ellipsis does not work properly when MIN_LINE_SIZE is set.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / glyph-metrics-helper.cpp
1
2 /*
3  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18
19 // FILE HEADER
20 #include <dali-toolkit/internal/text/glyph-metrics-helper.h>
21
22 namespace Dali
23 {
24
25 namespace Toolkit
26 {
27
28 namespace Text
29 {
30
31 Length GetNumberOfGlyphsOfGroup( GlyphIndex glyphIndex,
32                                  GlyphIndex lastGlyphPlusOne,
33                                  const Length* const charactersPerGlyphBuffer )
34 {
35   Length numberOfGLyphsInGroup = 1u;
36
37   for( GlyphIndex index = glyphIndex; index < lastGlyphPlusOne; ++index )
38   {
39     if( 0u == *( charactersPerGlyphBuffer + index ) )
40     {
41       ++numberOfGLyphsInGroup;
42     }
43     else
44     {
45       break;
46     }
47   }
48
49   return numberOfGLyphsInGroup;
50 }
51
52 void GetGlyphsMetrics( GlyphIndex glyphIndex,
53                        Length numberOfGlyphs,
54                        GlyphMetrics& glyphMetrics,
55                        const GlyphInfo* const glyphsBuffer,
56                        MetricsPtr& metrics )
57 {
58   const GlyphInfo& firstGlyph = *( glyphsBuffer + glyphIndex );
59
60   Text::FontMetrics fontMetrics;
61   if( 0u != firstGlyph.fontId )
62   {
63     metrics->GetFontMetrics( firstGlyph.fontId, fontMetrics );
64   }
65   else if( 0u != firstGlyph.index )
66   {
67     // It may be an embedded image.
68     fontMetrics.ascender = firstGlyph.height;
69     fontMetrics.descender = 0.f;
70     fontMetrics.height = fontMetrics.ascender;
71   }
72
73   const bool isItalicFont = metrics->HasItalicStyle( firstGlyph.fontId );
74
75   glyphMetrics.fontId = firstGlyph.fontId;
76   glyphMetrics.fontHeight = fontMetrics.height;
77   glyphMetrics.width = firstGlyph.width;
78   glyphMetrics.advance = firstGlyph.advance;
79   glyphMetrics.ascender = fontMetrics.ascender;
80   glyphMetrics.xBearing = firstGlyph.xBearing;
81
82   if( 1u < numberOfGlyphs )
83   {
84     float  maxWidthEdge = firstGlyph.xBearing + firstGlyph.width;
85
86     for( unsigned int i = 1u; i < numberOfGlyphs; ++i )
87     {
88       const GlyphInfo& glyphInfo = *( glyphsBuffer + glyphIndex + i );
89
90       // update the initial xBearing if smaller.
91       glyphMetrics.xBearing = std::min( glyphMetrics.xBearing, glyphMetrics.advance + glyphInfo.xBearing );
92
93       // update the max width edge if bigger.
94       const float currentMaxGlyphWidthEdge = glyphMetrics.advance + glyphInfo.xBearing + glyphInfo.width;
95       maxWidthEdge = std::max( maxWidthEdge, currentMaxGlyphWidthEdge );
96
97       glyphMetrics.advance += glyphInfo.advance;
98     }
99
100     glyphMetrics.width = maxWidthEdge - glyphMetrics.xBearing;
101   }
102
103   glyphMetrics.width += ( firstGlyph.isItalicRequired && !isItalicFont ) ? TextAbstraction::FontClient::DEFAULT_ITALIC_ANGLE * firstGlyph.height : 0.f;
104 }
105
106 } // namespace Text
107
108 } // namespace Toolkit
109
110 } // namespace Dali