2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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
8 // http://floralicense.org/license/
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.
19 #include <dali/public-api/text/text-style.h>
23 #include <dali/public-api/common/constants.h>
24 #include <dali/public-api/text/font.h>
29 const Degree TextStyle::DEFAULT_ITALICS_ANGLE( 20.0f );
30 const float TextStyle::DEFAULT_UNDERLINE_THICKNESS( 0.f );
31 const float TextStyle::DEFAULT_UNDERLINE_POSITION( 0.f );
32 const Vector4 TextStyle::DEFAULT_TEXT_COLOR( Vector4( 1.0f, 1.0f, 1.0f, 1.0f ) ); // cannot use Color::WHITE because it may or may not be initialized yet
33 const Vector4 TextStyle::DEFAULT_SHADOW_COLOR( Vector4( 0.0f, 0.0f, 0.0f, 1.0f ) ); // cannot use Color::BLACK because it may or may not be initialized yet
34 const Vector2 TextStyle::DEFAULT_SHADOW_OFFSET( 1.0f, 1.0f );
35 const float TextStyle::DEFAULT_SHADOW_SIZE( 0.0f );
36 const Vector4 TextStyle::DEFAULT_GLOW_COLOR( Vector4( 1.0f, 1.0f, 0.0f, 1.0f ) ); // cannot use Color::YELLOW because it may or may not be initialized yet
37 const float TextStyle::DEFAULT_GLOW_INTENSITY( 0.05f );
38 const float TextStyle::DEFAULT_SMOOTH_EDGE_DISTANCE_FIELD( 0.46f );
39 const Vector4 TextStyle::DEFAULT_OUTLINE_COLOR( Vector4( 0.0f, 0.0f, 0.0f, 1.0f ) ); // cannot use Color::BLACK because it may or may not be initialized yet
40 const Vector2 TextStyle::DEFAULT_OUTLINE_THICKNESS( 0.51f, 0.00f );
41 const Vector4 TextStyle::DEFAULT_GRADIENT_COLOR( 1.0f, 1.0f, 1.0f, 1.0f ); // cannot use Color::WHI|TE because it may or may not be initialized yet
42 const Vector2 TextStyle::DEFAULT_GRADIENT_START_POINT( 0.0f, 0.0f );
43 const Vector2 TextStyle::DEFAULT_GRADIENT_END_POINT( 0.0f, 0.0f );
45 const std::string DEFAULT_NAME;
47 struct TextStyle::Impl
52 mFontPointSize( 0.f ),
54 mTextColor( DEFAULT_TEXT_COLOR ),
60 mItalicsAngle( DEFAULT_ITALICS_ANGLE ),
61 mUnderlineThickness( DEFAULT_UNDERLINE_THICKNESS ),
62 mUnderlinePosition( DEFAULT_UNDERLINE_POSITION ),
63 mShadowColor( DEFAULT_SHADOW_COLOR ),
64 mShadowOffset( DEFAULT_SHADOW_OFFSET ),
65 mShadowSize( DEFAULT_SHADOW_SIZE ),
66 mGlowColor( DEFAULT_GLOW_COLOR ),
67 mGlowIntensity( DEFAULT_GLOW_INTENSITY ),
68 mSmoothEdge( DEFAULT_SMOOTH_EDGE_DISTANCE_FIELD ),
69 mOutlineColor( DEFAULT_OUTLINE_COLOR ),
70 mOutlineThickness( DEFAULT_OUTLINE_THICKNESS )
74 Impl( const std::string& fontName,
75 const std::string& fontStyle,
76 PointSize fontPointSize,
78 const Vector4& textColor,
85 float underlineThickness,
86 float underlinePosition,
87 const Vector4& shadowColor,
88 const Vector2& shadowOffset,
89 const float shadowSize,
90 const Vector4& glowColor,
93 const Vector4& outlineColor,
94 const Vector2& outlineThickness )
95 : mFontName( fontName ),
96 mFontStyle( fontStyle ),
97 mFontPointSize( fontPointSize ),
99 mTextColor( textColor ),
101 mUnderline( underline ),
105 mItalicsAngle( italicsAngle ),
106 mUnderlineThickness( underlineThickness ),
107 mUnderlinePosition( underlinePosition ),
108 mShadowColor( shadowColor ),
109 mShadowOffset( shadowOffset ),
110 mShadowSize( shadowSize ),
111 mGlowColor( glowColor ),
112 mGlowIntensity( glowIntensity ),
113 mSmoothEdge( smoothEdge ),
114 mOutlineColor( outlineColor ),
115 mOutlineThickness( outlineThickness )
119 std::string mFontName; ///< Font family name.
120 std::string mFontStyle; ///< Font style.
121 PointSize mFontPointSize; ///< Size of the font in points.
122 Weight mWeight; ///< Style weight.
123 Vector4 mTextColor; ///< Color of the text.
124 bool mItalics:1; ///< Whether the text is in italics or not.
125 bool mUnderline:1; ///< Whether the text is underlined or not.
126 bool mShadow:1; ///< Whether the text has a shadow or not.
127 bool mGlow:1; ///< Whether the text has a glow or not.
128 bool mOutline:1; ///< Whether the text has an outline or not.
129 Degree mItalicsAngle; ///< The italics angle.
130 float mUnderlineThickness; ///< The underline's thickness.
131 float mUnderlinePosition; ///< The underline's position.
132 Vector4 mShadowColor; ///< The color of the shadow.
133 Vector2 mShadowOffset; ///< The shadow offset in pixels.
134 float mShadowSize; ///< The shadow size in pixels
135 Vector4 mGlowColor; ///< The color of the glow.
136 float mGlowIntensity; ///< Determines the amount of glow around text.
137 float mSmoothEdge; ///< Specify the distance field value for the center of the text edge.
138 Vector4 mOutlineColor; ///< The color of the outline.
139 Vector2 mOutlineThickness; ///< The outline's thickness.
143 TextStyle::TextStyle()
148 TextStyle::TextStyle( const TextStyle& textStyle )
152 if ( textStyle.mImpl )
154 mImpl = new TextStyle::Impl( textStyle.mImpl->mFontName,
155 textStyle.mImpl->mFontStyle,
156 textStyle.mImpl->mFontPointSize,
157 textStyle.mImpl->mWeight,
158 textStyle.mImpl->mTextColor,
159 textStyle.mImpl->mItalics,
160 textStyle.mImpl->mUnderline,
161 textStyle.mImpl->mShadow,
162 textStyle.mImpl->mGlow,
163 textStyle.mImpl->mOutline,
164 textStyle.mImpl->mItalicsAngle,
165 textStyle.mImpl->mUnderlineThickness,
166 textStyle.mImpl->mUnderlinePosition,
167 textStyle.mImpl->mShadowColor,
168 textStyle.mImpl->mShadowOffset,
169 textStyle.mImpl->mShadowSize,
170 textStyle.mImpl->mGlowColor,
171 textStyle.mImpl->mGlowIntensity,
172 textStyle.mImpl->mSmoothEdge,
173 textStyle.mImpl->mOutlineColor,
174 textStyle.mImpl->mOutlineThickness );
177 TextStyle::~TextStyle()
182 TextStyle& TextStyle::operator=( const TextStyle& textStyle )
184 if( &textStyle != this )
186 // Is the source object current set to defaults?
187 if ( textStyle.mImpl == NULL )
189 // Yes, so delete our current implementation and set to defaults (ie/ no implementation)
195 CreateImplementationJustInTime();
196 mImpl->mFontName = textStyle.mImpl->mFontName;
197 mImpl->mFontStyle = textStyle.mImpl->mFontStyle;
198 mImpl->mFontPointSize = textStyle.mImpl->mFontPointSize;
199 mImpl->mWeight = textStyle.mImpl->mWeight;
200 mImpl->mTextColor = textStyle.mImpl->mTextColor;
201 mImpl->mItalics = textStyle.mImpl->mItalics;
202 mImpl->mUnderline = textStyle.mImpl->mUnderline;
203 mImpl->mShadow = textStyle.mImpl->mShadow;
204 mImpl->mGlow = textStyle.mImpl->mGlow;
205 mImpl->mOutline = textStyle.mImpl->mOutline;
206 mImpl->mItalicsAngle = textStyle.mImpl->mItalicsAngle;
207 mImpl->mUnderlineThickness = textStyle.mImpl->mUnderlineThickness;
208 mImpl->mUnderlinePosition = textStyle.mImpl->mUnderlinePosition;
209 mImpl->mShadowColor = textStyle.mImpl->mShadowColor;
210 mImpl->mShadowOffset = textStyle.mImpl->mShadowOffset;
211 mImpl->mShadowSize = textStyle.mImpl->mShadowSize;
212 mImpl->mGlowColor = textStyle.mImpl->mGlowColor;
213 mImpl->mGlowIntensity = textStyle.mImpl->mGlowIntensity;
214 mImpl->mSmoothEdge = textStyle.mImpl->mSmoothEdge;
215 mImpl->mOutlineColor = textStyle.mImpl->mOutlineColor;
216 mImpl->mOutlineThickness = textStyle.mImpl->mOutlineThickness;
222 bool TextStyle::operator==( const TextStyle& textStyle ) const
224 // If both Implementations are uninitialized then return equal
225 if ( mImpl == NULL && textStyle.mImpl == NULL )
229 // Otherwise if either one of the Implemetations are uninitialized then return not equal
230 else if ( mImpl == NULL || textStyle.mImpl == NULL )
234 return ( ( mImpl->mFontName == textStyle.mImpl->mFontName ) &&
235 ( mImpl->mFontStyle == textStyle.mImpl->mFontStyle ) &&
236 ( fabsf( mImpl->mFontPointSize - textStyle.mImpl->mFontPointSize ) < GetRangedEpsilon( mImpl->mFontPointSize, textStyle.mImpl->mFontPointSize ) ) &&
237 ( mImpl->mWeight == textStyle.mImpl->mWeight ) &&
238 ( mImpl->mTextColor == textStyle.mImpl->mTextColor ) &&
239 ( mImpl->mItalics == textStyle.mImpl->mItalics ) &&
240 ( mImpl->mUnderline == textStyle.mImpl->mUnderline ) &&
241 ( mImpl->mShadow == textStyle.mImpl->mShadow ) &&
242 ( mImpl->mGlow == textStyle.mImpl->mGlow ) &&
243 ( mImpl->mOutline == textStyle.mImpl->mOutline ) &&
244 ( mImpl->mItalicsAngle == textStyle.mImpl->mItalicsAngle ) &&
245 ( fabsf( mImpl->mUnderlineThickness - textStyle.mImpl->mUnderlineThickness ) < GetRangedEpsilon( mImpl->mUnderlineThickness, textStyle.mImpl->mUnderlineThickness ) ) &&
246 ( fabsf( mImpl->mUnderlinePosition - textStyle.mImpl->mUnderlinePosition ) < GetRangedEpsilon( mImpl->mUnderlinePosition, textStyle.mImpl->mUnderlinePosition ) ) &&
247 ( mImpl->mShadowColor == textStyle.mImpl->mShadowColor ) &&
248 ( mImpl->mShadowOffset == textStyle.mImpl->mShadowOffset ) &&
249 ( fabsf( mImpl->mShadowSize - textStyle.mImpl->mShadowSize ) < GetRangedEpsilon( mImpl->mShadowSize, textStyle.mImpl->mShadowSize ) ) &&
250 ( mImpl->mGlowColor == textStyle.mImpl->mGlowColor ) &&
251 ( fabsf( mImpl->mGlowIntensity - textStyle.mImpl->mGlowIntensity ) < GetRangedEpsilon( mImpl->mGlowIntensity, textStyle.mImpl->mGlowIntensity ) ) &&
252 ( fabsf( mImpl->mSmoothEdge - textStyle.mImpl->mSmoothEdge ) < GetRangedEpsilon( mImpl->mSmoothEdge, textStyle.mImpl->mSmoothEdge ) ) &&
253 ( mImpl->mOutlineColor == textStyle.mImpl->mOutlineColor ) &&
254 ( mImpl->mOutlineThickness == textStyle.mImpl->mOutlineThickness ) );
257 bool TextStyle::operator!=( const TextStyle& textStyle ) const
259 return !( *this == textStyle );
262 void TextStyle::Copy( const TextStyle& textStyle, const Mask mask )
264 // If we're attemping to copy ourselves then just return
265 if ( this == &textStyle )
270 // Check to see if we're copying a default style ?
271 if ( textStyle.mImpl == NULL )
273 // Yes, so if we're coping entirely then re-create a default style, else the mask resets attributes to defaults
274 if ( mImpl && mask == ALL )
285 mImpl->mFontName = DEFAULT_NAME;
289 mImpl->mFontStyle = DEFAULT_NAME;
293 mImpl->mFontPointSize = static_cast<PointSize>( 0.f );
297 mImpl->mWeight = REGULAR;
298 mImpl->mSmoothEdge = DEFAULT_SMOOTH_EDGE_DISTANCE_FIELD;
302 mImpl->mTextColor = DEFAULT_TEXT_COLOR;
306 mImpl->mItalics = false;
307 mImpl->mItalicsAngle = DEFAULT_ITALICS_ANGLE;
309 if( mask & UNDERLINE )
311 mImpl->mUnderline = false;
312 mImpl->mUnderlineThickness = DEFAULT_UNDERLINE_THICKNESS;
313 mImpl->mUnderlinePosition = DEFAULT_UNDERLINE_POSITION;
317 mImpl->mShadow = false;
318 mImpl->mShadowColor = DEFAULT_SHADOW_COLOR;
319 mImpl->mShadowOffset = DEFAULT_SHADOW_OFFSET;
320 mImpl->mShadowSize = DEFAULT_SHADOW_SIZE;
324 mImpl->mGlow = false;
325 mImpl->mGlowColor = DEFAULT_GLOW_COLOR;
326 mImpl->mGlowIntensity = DEFAULT_GLOW_INTENSITY;
328 if ( mask & OUTLINE )
330 mImpl->mOutline = false;
331 mImpl->mOutlineColor = DEFAULT_OUTLINE_COLOR;
332 mImpl->mOutlineThickness = DEFAULT_OUTLINE_THICKNESS;
339 // Source has an implementation and so the target also needs one
340 CreateImplementationJustInTime();
349 mImpl->mFontName = textStyle.mImpl->mFontName;
353 mImpl->mFontStyle = textStyle.mImpl->mFontStyle;
357 mImpl->mFontPointSize = textStyle.mImpl->mFontPointSize;
361 mImpl->mWeight = textStyle.mImpl->mWeight;
362 mImpl->mSmoothEdge = textStyle.mImpl->mSmoothEdge;
366 mImpl->mTextColor = textStyle.mImpl->mTextColor;
370 mImpl->mItalics = textStyle.mImpl->mItalics;
371 mImpl->mItalicsAngle = textStyle.mImpl->mItalicsAngle;
373 if( mask & UNDERLINE )
375 mImpl->mUnderline = textStyle.mImpl->mUnderline;
376 mImpl->mUnderlineThickness = textStyle.mImpl->mUnderlineThickness;
377 mImpl->mUnderlinePosition = textStyle.mImpl->mUnderlinePosition;
381 mImpl->mShadow = textStyle.mImpl->mShadow;
382 mImpl->mShadowColor = textStyle.mImpl->mShadowColor;
383 mImpl->mShadowOffset = textStyle.mImpl->mShadowOffset;
384 mImpl->mShadowSize = textStyle.mImpl->mShadowSize;
388 mImpl->mGlow = textStyle.mImpl->mGlow;
389 mImpl->mGlowColor = textStyle.mImpl->mGlowColor;
390 mImpl->mGlowIntensity = textStyle.mImpl->mGlowIntensity;
392 if ( mask & OUTLINE )
394 mImpl->mOutline = textStyle.mImpl->mOutline;
395 mImpl->mOutlineColor = textStyle.mImpl->mOutlineColor;
396 mImpl->mOutlineThickness = textStyle.mImpl->mOutlineThickness;
401 const std::string& TextStyle::GetFontName() const
409 return mImpl->mFontName;
413 void TextStyle::SetFontName( const std::string& fontName )
415 CreateImplementationJustInTime();
416 mImpl->mFontName = fontName;
419 const std::string& TextStyle::GetFontStyle() const
427 return mImpl->mFontStyle;
431 void TextStyle::SetFontStyle( const std::string& fontStyle )
433 CreateImplementationJustInTime();
434 mImpl->mFontStyle = fontStyle;
437 PointSize TextStyle::GetFontPointSize() const
441 return static_cast<PointSize>( 0.f );
445 return mImpl->mFontPointSize;
449 void TextStyle::SetFontPointSize( PointSize fontPointSize )
451 CreateImplementationJustInTime();
452 mImpl->mFontPointSize = fontPointSize;
455 TextStyle::Weight TextStyle::GetWeight() const
463 return mImpl->mWeight ;
467 void TextStyle::SetWeight( TextStyle::Weight weight )
469 CreateImplementationJustInTime();
470 mImpl->mWeight = weight;
473 const Vector4& TextStyle::GetTextColor() const
477 return DEFAULT_TEXT_COLOR;
481 return mImpl->mTextColor;
485 void TextStyle::SetTextColor( const Vector4& textColor )
487 CreateImplementationJustInTime();
488 mImpl->mTextColor = textColor;
491 bool TextStyle::GetItalics() const
497 return mImpl->mItalics;
500 void TextStyle::SetItalics( bool italics )
502 CreateImplementationJustInTime();
503 mImpl->mItalics = italics;
506 Degree TextStyle::GetItalicsAngle() const
510 return DEFAULT_ITALICS_ANGLE;
514 return mImpl->mItalicsAngle;
518 void TextStyle::SetItalicsAngle( Degree angle )
520 CreateImplementationJustInTime();
521 mImpl->mItalicsAngle = angle;
524 bool TextStyle::GetUnderline() const
532 return mImpl->mUnderline;
536 void TextStyle::SetUnderline( bool underline )
538 CreateImplementationJustInTime();
539 mImpl->mUnderline = underline;
542 float TextStyle::GetUnderlineThickness() const
546 return DEFAULT_UNDERLINE_THICKNESS;
550 return mImpl->mUnderlineThickness;
554 void TextStyle::SetUnderlineThickness( float thickness )
556 CreateImplementationJustInTime();
557 mImpl->mUnderlineThickness = thickness;
560 float TextStyle::GetUnderlinePosition() const
564 return DEFAULT_UNDERLINE_POSITION;
568 return mImpl->mUnderlinePosition;
572 void TextStyle::SetUnderlinePosition( float position )
574 CreateImplementationJustInTime();
575 mImpl->mUnderlinePosition = position;
578 bool TextStyle::GetShadow() const
586 return mImpl->mShadow;
590 const Vector4& TextStyle::GetShadowColor() const
594 return DEFAULT_SHADOW_COLOR;
596 return mImpl->mShadowColor;
599 const Vector2& TextStyle::GetShadowOffset() const
603 return DEFAULT_SHADOW_OFFSET;
605 return mImpl->mShadowOffset;
608 float TextStyle::GetShadowSize() const
612 return DEFAULT_SHADOW_SIZE;
616 return mImpl->mShadowSize;
620 void TextStyle::SetShadow( bool shadow, const Vector4& shadowColor, const Vector2& shadowOffset, const float shadowSize )
622 CreateImplementationJustInTime();
623 mImpl->mShadow = shadow;
624 mImpl->mShadowColor = shadowColor;
625 mImpl->mShadowOffset = shadowOffset;
626 mImpl->mShadowSize = shadowSize;
629 bool TextStyle::GetGlow() const
638 const Vector4& TextStyle::GetGlowColor() const
642 return DEFAULT_GLOW_COLOR;
646 return mImpl->mGlowColor;
650 float TextStyle::GetGlowIntensity() const
654 return DEFAULT_GLOW_INTENSITY;
658 return mImpl->mGlowIntensity;
662 void TextStyle::SetGlow( bool glow, const Vector4& glowColor, float glowIntensity )
664 CreateImplementationJustInTime();
666 mImpl->mGlowColor = glowColor;
667 mImpl->mGlowIntensity = glowIntensity;
670 float TextStyle::GetSmoothEdge() const
674 return DEFAULT_SMOOTH_EDGE_DISTANCE_FIELD;
678 return mImpl->mSmoothEdge;
682 void TextStyle::SetSmoothEdge( float smoothEdge )
684 CreateImplementationJustInTime();
685 mImpl->mSmoothEdge = smoothEdge;
688 bool TextStyle::GetOutline() const
696 return mImpl->mOutline;
700 const Vector4& TextStyle::GetOutlineColor() const
704 return DEFAULT_OUTLINE_COLOR;
708 return mImpl->mOutlineColor;
712 const Vector2& TextStyle::GetOutlineThickness() const
716 return DEFAULT_OUTLINE_THICKNESS;
720 return mImpl->mOutlineThickness;
724 void TextStyle::SetOutline( bool outline, const Vector4& outlineColor, const Vector2& outlineThickness )
726 CreateImplementationJustInTime();
727 mImpl->mOutline = outline;
728 mImpl->mOutlineColor = outlineColor;
729 mImpl->mOutlineThickness = outlineThickness;
732 void TextStyle::CreateImplementationJustInTime()
736 mImpl = new TextStyle::Impl();