Merge "Use RegisterUniqueProperty for some more renderers" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / rendering / styles / underline-helper-functions.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/rendering/styles/underline-helper-functions.h>
20
21 namespace Dali
22 {
23 namespace Toolkit
24 {
25 namespace Text
26 {
27 bool IsGlyphUnderlined(GlyphIndex                                 index,
28                        const Vector<UnderlinedGlyphRun>&          underlineRuns,
29                        Vector<UnderlinedGlyphRun>::ConstIterator& currentUnderlinedGlyphRunIt)
30 {
31   for(Vector<UnderlinedGlyphRun>::ConstIterator it    = underlineRuns.Begin(),
32                                                 endIt = underlineRuns.End();
33       it != endIt;
34       ++it)
35   {
36     const UnderlinedGlyphRun& run = *it;
37
38     if((run.glyphRun.glyphIndex <= index) && (index < run.glyphRun.glyphIndex + run.glyphRun.numberOfGlyphs))
39     {
40       currentUnderlinedGlyphRunIt = it;
41       return true;
42     }
43   }
44
45   return false;
46 }
47
48 float GetCurrentUnderlineHeight(const Vector<UnderlinedGlyphRun>&         underlineRuns,
49                                 Vector<UnderlinedGlyphRun>::ConstIterator currentUnderlinedGlyphRunIt,
50                                 const float                               underlineHeight)
51 {
52   if(currentUnderlinedGlyphRunIt == underlineRuns.End())
53   {
54     return underlineHeight;
55   }
56
57   const UnderlinedGlyphRun& underlinedGlyphRun = *currentUnderlinedGlyphRunIt;
58   return (underlinedGlyphRun.properties.heightDefined ? underlinedGlyphRun.properties.height : underlineHeight);
59 }
60
61 UnderlineStyleProperties GetCurrentUnderlineProperties(const bool&                               isGlyphUnderlined,
62                                                        const Vector<UnderlinedGlyphRun>&         underlineRuns,
63                                                        Vector<UnderlinedGlyphRun>::ConstIterator currentUnderlinedGlyphRunIt,
64                                                        const UnderlineStyleProperties&           commonUnderlineProperties)
65 {
66   return (isGlyphUnderlined && (currentUnderlinedGlyphRunIt != underlineRuns.End()))
67            ? currentUnderlinedGlyphRunIt->properties
68            : commonUnderlineProperties;
69 }
70
71 float FetchUnderlinePositionFromFontMetrics(const FontMetrics& fontMetrics)
72 {
73   //Helper method to fetch the underline metrics for the specified font glyph
74   const float descender         = ceil(fabsf(fontMetrics.descender));
75   float       underlinePosition = ceil(fabsf(fontMetrics.underlinePosition));
76
77   // Clamp the underline position at the font descender and check for ( as EFL describes it ) a broken font
78   if(underlinePosition > descender)
79   {
80     underlinePosition = descender;
81   }
82
83   if(fabsf(underlinePosition) < Math::MACHINE_EPSILON_1000)
84   {
85     // Move offset down by one ( EFL behavior )
86     underlinePosition = 1.0f;
87   }
88
89   return underlinePosition;
90 }
91
92 void CalcualteUnderlineHeight(const FontMetrics& fontMetrics, float& currentUnderlineHeight, float& maxUnderlineHeight)
93 {
94   //Helper method to fetch the underline metrics for the specified font glyph
95   //Height of underline represents the thickness of line.
96   if(fabsf(currentUnderlineHeight) < Math::MACHINE_EPSILON_1000)
97   {
98     currentUnderlineHeight = fontMetrics.underlineThickness;
99
100     // Ensure underline will be at least a pixel high
101     if(currentUnderlineHeight < 1.0f)
102     {
103       currentUnderlineHeight = 1.0f;
104     }
105     else
106     {
107       currentUnderlineHeight = ceil(currentUnderlineHeight);
108     }
109   }
110
111   // The underline height should be the max underline height of all glyphs of the line.
112   if(currentUnderlineHeight > maxUnderlineHeight)
113   {
114     maxUnderlineHeight = currentUnderlineHeight;
115   }
116 }
117
118 } // namespace Text
119
120 } // namespace Toolkit
121
122 } // namespace Dali