2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <dali/public-api/text/text-style.h>
24 #include <dali/public-api/common/constants.h>
25 #include <dali/public-api/text/font.h>
30 const Degree TextStyle::DEFAULT_ITALICS_ANGLE( 20.0f );
31 const float TextStyle::DEFAULT_UNDERLINE_THICKNESS( 0.f );
32 const float TextStyle::DEFAULT_UNDERLINE_POSITION( 0.f );
33 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
34 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
35 const Vector2 TextStyle::DEFAULT_SHADOW_OFFSET( 1.0f, 1.0f );
36 const float TextStyle::DEFAULT_SHADOW_SIZE( 0.0f );
37 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
38 const float TextStyle::DEFAULT_GLOW_INTENSITY( 0.05f );
39 const float TextStyle::DEFAULT_SMOOTH_EDGE_DISTANCE_FIELD( 0.46f );
40 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
41 const Vector2 TextStyle::DEFAULT_OUTLINE_THICKNESS( 0.51f, 0.00f );
42 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
43 const Vector2 TextStyle::DEFAULT_GRADIENT_START_POINT( 0.0f, 0.0f );
44 const Vector2 TextStyle::DEFAULT_GRADIENT_END_POINT( 0.0f, 0.0f );
46 const std::string DEFAULT_NAME;
48 struct TextStyle::Impl
53 mFontPointSize( 0.f ),
55 mTextColor( DEFAULT_TEXT_COLOR ),
61 mItalicsAngle( DEFAULT_ITALICS_ANGLE ),
62 mUnderlineThickness( DEFAULT_UNDERLINE_THICKNESS ),
63 mUnderlinePosition( DEFAULT_UNDERLINE_POSITION ),
64 mShadowColor( DEFAULT_SHADOW_COLOR ),
65 mShadowOffset( DEFAULT_SHADOW_OFFSET ),
66 mShadowSize( DEFAULT_SHADOW_SIZE ),
67 mGlowColor( DEFAULT_GLOW_COLOR ),
68 mGlowIntensity( DEFAULT_GLOW_INTENSITY ),
69 mSmoothEdge( DEFAULT_SMOOTH_EDGE_DISTANCE_FIELD ),
70 mOutlineColor( DEFAULT_OUTLINE_COLOR ),
71 mOutlineThickness( DEFAULT_OUTLINE_THICKNESS )
75 Impl( const std::string& fontName,
76 const std::string& fontStyle,
77 PointSize fontPointSize,
79 const Vector4& textColor,
86 float underlineThickness,
87 float underlinePosition,
88 const Vector4& shadowColor,
89 const Vector2& shadowOffset,
90 const float shadowSize,
91 const Vector4& glowColor,
94 const Vector4& outlineColor,
95 const Vector2& outlineThickness )
96 : mFontName( fontName ),
97 mFontStyle( fontStyle ),
98 mFontPointSize( fontPointSize ),
100 mTextColor( textColor ),
102 mUnderline( underline ),
106 mItalicsAngle( italicsAngle ),
107 mUnderlineThickness( underlineThickness ),
108 mUnderlinePosition( underlinePosition ),
109 mShadowColor( shadowColor ),
110 mShadowOffset( shadowOffset ),
111 mShadowSize( shadowSize ),
112 mGlowColor( glowColor ),
113 mGlowIntensity( glowIntensity ),
114 mSmoothEdge( smoothEdge ),
115 mOutlineColor( outlineColor ),
116 mOutlineThickness( outlineThickness )
120 std::string mFontName; ///< Font family name.
121 std::string mFontStyle; ///< Font style.
122 PointSize mFontPointSize; ///< Size of the font in points.
123 Weight mWeight; ///< Style weight.
124 Vector4 mTextColor; ///< Color of the text.
125 bool mItalics:1; ///< Whether the text is in italics or not.
126 bool mUnderline:1; ///< Whether the text is underlined or not.
127 bool mShadow:1; ///< Whether the text has a shadow or not.
128 bool mGlow:1; ///< Whether the text has a glow or not.
129 bool mOutline:1; ///< Whether the text has an outline or not.
130 Degree mItalicsAngle; ///< The italics angle.
131 float mUnderlineThickness; ///< The underline's thickness.
132 float mUnderlinePosition; ///< The underline's position.
133 Vector4 mShadowColor; ///< The color of the shadow.
134 Vector2 mShadowOffset; ///< The shadow offset in pixels.
135 float mShadowSize; ///< The shadow size in pixels
136 Vector4 mGlowColor; ///< The color of the glow.
137 float mGlowIntensity; ///< Determines the amount of glow around text.
138 float mSmoothEdge; ///< Specify the distance field value for the center of the text edge.
139 Vector4 mOutlineColor; ///< The color of the outline.
140 Vector2 mOutlineThickness; ///< The outline's thickness.
144 TextStyle::TextStyle()
149 TextStyle::TextStyle( const TextStyle& textStyle )
153 if ( textStyle.mImpl )
155 mImpl = new TextStyle::Impl( textStyle.mImpl->mFontName,
156 textStyle.mImpl->mFontStyle,
157 textStyle.mImpl->mFontPointSize,
158 textStyle.mImpl->mWeight,
159 textStyle.mImpl->mTextColor,
160 textStyle.mImpl->mItalics,
161 textStyle.mImpl->mUnderline,
162 textStyle.mImpl->mShadow,
163 textStyle.mImpl->mGlow,
164 textStyle.mImpl->mOutline,
165 textStyle.mImpl->mItalicsAngle,
166 textStyle.mImpl->mUnderlineThickness,
167 textStyle.mImpl->mUnderlinePosition,
168 textStyle.mImpl->mShadowColor,
169 textStyle.mImpl->mShadowOffset,
170 textStyle.mImpl->mShadowSize,
171 textStyle.mImpl->mGlowColor,
172 textStyle.mImpl->mGlowIntensity,
173 textStyle.mImpl->mSmoothEdge,
174 textStyle.mImpl->mOutlineColor,
175 textStyle.mImpl->mOutlineThickness );
178 TextStyle::~TextStyle()
183 TextStyle& TextStyle::operator=( const TextStyle& textStyle )
185 if( &textStyle != this )
187 // Is the source object current set to defaults?
188 if ( textStyle.mImpl == NULL )
190 // Yes, so delete our current implementation and set to defaults (ie/ no implementation)
196 CreateImplementationJustInTime();
197 mImpl->mFontName = textStyle.mImpl->mFontName;
198 mImpl->mFontStyle = textStyle.mImpl->mFontStyle;
199 mImpl->mFontPointSize = textStyle.mImpl->mFontPointSize;
200 mImpl->mWeight = textStyle.mImpl->mWeight;
201 mImpl->mTextColor = textStyle.mImpl->mTextColor;
202 mImpl->mItalics = textStyle.mImpl->mItalics;
203 mImpl->mUnderline = textStyle.mImpl->mUnderline;
204 mImpl->mShadow = textStyle.mImpl->mShadow;
205 mImpl->mGlow = textStyle.mImpl->mGlow;
206 mImpl->mOutline = textStyle.mImpl->mOutline;
207 mImpl->mItalicsAngle = textStyle.mImpl->mItalicsAngle;
208 mImpl->mUnderlineThickness = textStyle.mImpl->mUnderlineThickness;
209 mImpl->mUnderlinePosition = textStyle.mImpl->mUnderlinePosition;
210 mImpl->mShadowColor = textStyle.mImpl->mShadowColor;
211 mImpl->mShadowOffset = textStyle.mImpl->mShadowOffset;
212 mImpl->mShadowSize = textStyle.mImpl->mShadowSize;
213 mImpl->mGlowColor = textStyle.mImpl->mGlowColor;
214 mImpl->mGlowIntensity = textStyle.mImpl->mGlowIntensity;
215 mImpl->mSmoothEdge = textStyle.mImpl->mSmoothEdge;
216 mImpl->mOutlineColor = textStyle.mImpl->mOutlineColor;
217 mImpl->mOutlineThickness = textStyle.mImpl->mOutlineThickness;
223 bool TextStyle::operator==( const TextStyle& textStyle ) const
225 // If both Implementations are uninitialized then return equal
226 if ( mImpl == NULL && textStyle.mImpl == NULL )
230 // Otherwise if either one of the Implemetations are uninitialized then return not equal
231 else if ( mImpl == NULL || textStyle.mImpl == NULL )
235 return ( ( mImpl->mFontName == textStyle.mImpl->mFontName ) &&
236 ( mImpl->mFontStyle == textStyle.mImpl->mFontStyle ) &&
237 ( fabsf( mImpl->mFontPointSize - textStyle.mImpl->mFontPointSize ) < GetRangedEpsilon( mImpl->mFontPointSize, textStyle.mImpl->mFontPointSize ) ) &&
238 ( mImpl->mWeight == textStyle.mImpl->mWeight ) &&
239 ( mImpl->mTextColor == textStyle.mImpl->mTextColor ) &&
240 ( mImpl->mItalics == textStyle.mImpl->mItalics ) &&
241 ( mImpl->mUnderline == textStyle.mImpl->mUnderline ) &&
242 ( mImpl->mShadow == textStyle.mImpl->mShadow ) &&
243 ( mImpl->mGlow == textStyle.mImpl->mGlow ) &&
244 ( mImpl->mOutline == textStyle.mImpl->mOutline ) &&
245 ( mImpl->mItalicsAngle == textStyle.mImpl->mItalicsAngle ) &&
246 ( fabsf( mImpl->mUnderlineThickness - textStyle.mImpl->mUnderlineThickness ) < GetRangedEpsilon( mImpl->mUnderlineThickness, textStyle.mImpl->mUnderlineThickness ) ) &&
247 ( fabsf( mImpl->mUnderlinePosition - textStyle.mImpl->mUnderlinePosition ) < GetRangedEpsilon( mImpl->mUnderlinePosition, textStyle.mImpl->mUnderlinePosition ) ) &&
248 ( mImpl->mShadowColor == textStyle.mImpl->mShadowColor ) &&
249 ( mImpl->mShadowOffset == textStyle.mImpl->mShadowOffset ) &&
250 ( fabsf( mImpl->mShadowSize - textStyle.mImpl->mShadowSize ) < GetRangedEpsilon( mImpl->mShadowSize, textStyle.mImpl->mShadowSize ) ) &&
251 ( mImpl->mGlowColor == textStyle.mImpl->mGlowColor ) &&
252 ( fabsf( mImpl->mGlowIntensity - textStyle.mImpl->mGlowIntensity ) < GetRangedEpsilon( mImpl->mGlowIntensity, textStyle.mImpl->mGlowIntensity ) ) &&
253 ( fabsf( mImpl->mSmoothEdge - textStyle.mImpl->mSmoothEdge ) < GetRangedEpsilon( mImpl->mSmoothEdge, textStyle.mImpl->mSmoothEdge ) ) &&
254 ( mImpl->mOutlineColor == textStyle.mImpl->mOutlineColor ) &&
255 ( mImpl->mOutlineThickness == textStyle.mImpl->mOutlineThickness ) );
258 bool TextStyle::operator!=( const TextStyle& textStyle ) const
260 return !( *this == textStyle );
263 void TextStyle::Copy( const TextStyle& textStyle, const Mask mask )
265 // If we're attemping to copy ourselves then just return
266 if ( this == &textStyle )
271 // Check to see if we're copying a default style ?
272 if ( textStyle.mImpl == NULL )
274 // Yes, so if we're coping entirely then re-create a default style, else the mask resets attributes to defaults
275 if ( mImpl && mask == ALL )
286 mImpl->mFontName = DEFAULT_NAME;
290 mImpl->mFontStyle = DEFAULT_NAME;
294 mImpl->mFontPointSize = static_cast<PointSize>( 0.f );
298 mImpl->mWeight = REGULAR;
299 mImpl->mSmoothEdge = DEFAULT_SMOOTH_EDGE_DISTANCE_FIELD;
303 mImpl->mTextColor = DEFAULT_TEXT_COLOR;
307 mImpl->mItalics = false;
308 mImpl->mItalicsAngle = DEFAULT_ITALICS_ANGLE;
310 if( mask & UNDERLINE )
312 mImpl->mUnderline = false;
313 mImpl->mUnderlineThickness = DEFAULT_UNDERLINE_THICKNESS;
314 mImpl->mUnderlinePosition = DEFAULT_UNDERLINE_POSITION;
318 mImpl->mShadow = false;
319 mImpl->mShadowColor = DEFAULT_SHADOW_COLOR;
320 mImpl->mShadowOffset = DEFAULT_SHADOW_OFFSET;
321 mImpl->mShadowSize = DEFAULT_SHADOW_SIZE;
325 mImpl->mGlow = false;
326 mImpl->mGlowColor = DEFAULT_GLOW_COLOR;
327 mImpl->mGlowIntensity = DEFAULT_GLOW_INTENSITY;
329 if ( mask & OUTLINE )
331 mImpl->mOutline = false;
332 mImpl->mOutlineColor = DEFAULT_OUTLINE_COLOR;
333 mImpl->mOutlineThickness = DEFAULT_OUTLINE_THICKNESS;
340 // Source has an implementation and so the target also needs one
341 CreateImplementationJustInTime();
350 mImpl->mFontName = textStyle.mImpl->mFontName;
354 mImpl->mFontStyle = textStyle.mImpl->mFontStyle;
358 mImpl->mFontPointSize = textStyle.mImpl->mFontPointSize;
362 mImpl->mWeight = textStyle.mImpl->mWeight;
363 mImpl->mSmoothEdge = textStyle.mImpl->mSmoothEdge;
367 mImpl->mTextColor = textStyle.mImpl->mTextColor;
371 mImpl->mItalics = textStyle.mImpl->mItalics;
372 mImpl->mItalicsAngle = textStyle.mImpl->mItalicsAngle;
374 if( mask & UNDERLINE )
376 mImpl->mUnderline = textStyle.mImpl->mUnderline;
377 mImpl->mUnderlineThickness = textStyle.mImpl->mUnderlineThickness;
378 mImpl->mUnderlinePosition = textStyle.mImpl->mUnderlinePosition;
382 mImpl->mShadow = textStyle.mImpl->mShadow;
383 mImpl->mShadowColor = textStyle.mImpl->mShadowColor;
384 mImpl->mShadowOffset = textStyle.mImpl->mShadowOffset;
385 mImpl->mShadowSize = textStyle.mImpl->mShadowSize;
389 mImpl->mGlow = textStyle.mImpl->mGlow;
390 mImpl->mGlowColor = textStyle.mImpl->mGlowColor;
391 mImpl->mGlowIntensity = textStyle.mImpl->mGlowIntensity;
393 if ( mask & OUTLINE )
395 mImpl->mOutline = textStyle.mImpl->mOutline;
396 mImpl->mOutlineColor = textStyle.mImpl->mOutlineColor;
397 mImpl->mOutlineThickness = textStyle.mImpl->mOutlineThickness;
402 const std::string& TextStyle::GetFontName() const
410 return mImpl->mFontName;
414 void TextStyle::SetFontName( const std::string& fontName )
416 CreateImplementationJustInTime();
417 mImpl->mFontName = fontName;
420 const std::string& TextStyle::GetFontStyle() const
428 return mImpl->mFontStyle;
432 void TextStyle::SetFontStyle( const std::string& fontStyle )
434 CreateImplementationJustInTime();
435 mImpl->mFontStyle = fontStyle;
438 PointSize TextStyle::GetFontPointSize() const
442 return static_cast<PointSize>( 0.f );
446 return mImpl->mFontPointSize;
450 void TextStyle::SetFontPointSize( PointSize fontPointSize )
452 CreateImplementationJustInTime();
453 mImpl->mFontPointSize = fontPointSize;
456 TextStyle::Weight TextStyle::GetWeight() const
464 return mImpl->mWeight ;
468 void TextStyle::SetWeight( TextStyle::Weight weight )
470 CreateImplementationJustInTime();
471 mImpl->mWeight = weight;
474 const Vector4& TextStyle::GetTextColor() const
478 return DEFAULT_TEXT_COLOR;
482 return mImpl->mTextColor;
486 void TextStyle::SetTextColor( const Vector4& textColor )
488 CreateImplementationJustInTime();
489 mImpl->mTextColor = textColor;
492 bool TextStyle::GetItalics() const
498 return mImpl->mItalics;
501 void TextStyle::SetItalics( bool italics )
503 CreateImplementationJustInTime();
504 mImpl->mItalics = italics;
507 Degree TextStyle::GetItalicsAngle() const
511 return DEFAULT_ITALICS_ANGLE;
515 return mImpl->mItalicsAngle;
519 void TextStyle::SetItalicsAngle( Degree angle )
521 CreateImplementationJustInTime();
522 mImpl->mItalicsAngle = angle;
525 bool TextStyle::GetUnderline() const
533 return mImpl->mUnderline;
537 void TextStyle::SetUnderline( bool underline )
539 CreateImplementationJustInTime();
540 mImpl->mUnderline = underline;
543 float TextStyle::GetUnderlineThickness() const
547 return DEFAULT_UNDERLINE_THICKNESS;
551 return mImpl->mUnderlineThickness;
555 void TextStyle::SetUnderlineThickness( float thickness )
557 CreateImplementationJustInTime();
558 mImpl->mUnderlineThickness = thickness;
561 float TextStyle::GetUnderlinePosition() const
565 return DEFAULT_UNDERLINE_POSITION;
569 return mImpl->mUnderlinePosition;
573 void TextStyle::SetUnderlinePosition( float position )
575 CreateImplementationJustInTime();
576 mImpl->mUnderlinePosition = position;
579 bool TextStyle::GetShadow() const
587 return mImpl->mShadow;
591 const Vector4& TextStyle::GetShadowColor() const
595 return DEFAULT_SHADOW_COLOR;
597 return mImpl->mShadowColor;
600 const Vector2& TextStyle::GetShadowOffset() const
604 return DEFAULT_SHADOW_OFFSET;
606 return mImpl->mShadowOffset;
609 float TextStyle::GetShadowSize() const
613 return DEFAULT_SHADOW_SIZE;
617 return mImpl->mShadowSize;
621 void TextStyle::SetShadow( bool shadow, const Vector4& shadowColor, const Vector2& shadowOffset, const float shadowSize )
623 CreateImplementationJustInTime();
624 mImpl->mShadow = shadow;
625 mImpl->mShadowColor = shadowColor;
626 mImpl->mShadowOffset = shadowOffset;
627 mImpl->mShadowSize = shadowSize;
630 bool TextStyle::GetGlow() const
639 const Vector4& TextStyle::GetGlowColor() const
643 return DEFAULT_GLOW_COLOR;
647 return mImpl->mGlowColor;
651 float TextStyle::GetGlowIntensity() const
655 return DEFAULT_GLOW_INTENSITY;
659 return mImpl->mGlowIntensity;
663 void TextStyle::SetGlow( bool glow, const Vector4& glowColor, float glowIntensity )
665 CreateImplementationJustInTime();
667 mImpl->mGlowColor = glowColor;
668 mImpl->mGlowIntensity = glowIntensity;
671 float TextStyle::GetSmoothEdge() const
675 return DEFAULT_SMOOTH_EDGE_DISTANCE_FIELD;
679 return mImpl->mSmoothEdge;
683 void TextStyle::SetSmoothEdge( float smoothEdge )
685 CreateImplementationJustInTime();
686 mImpl->mSmoothEdge = smoothEdge;
689 bool TextStyle::GetOutline() const
697 return mImpl->mOutline;
701 const Vector4& TextStyle::GetOutlineColor() const
705 return DEFAULT_OUTLINE_COLOR;
709 return mImpl->mOutlineColor;
713 const Vector2& TextStyle::GetOutlineThickness() const
717 return DEFAULT_OUTLINE_THICKNESS;
721 return mImpl->mOutlineThickness;
725 void TextStyle::SetOutline( bool outline, const Vector4& outlineColor, const Vector2& outlineThickness )
727 CreateImplementationJustInTime();
728 mImpl->mOutline = outline;
729 mImpl->mOutlineColor = outlineColor;
730 mImpl->mOutlineThickness = outlineThickness;
733 void TextStyle::CreateImplementationJustInTime()
737 mImpl = new TextStyle::Impl();