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.
19 #include <dali/public-api/images/glyph-image.h>
22 #include <dali/public-api/images/bitmap-image.h>
23 #include <dali/public-api/images/resource-image.h>
24 #include <dali/public-api/text/text-style.h>
25 #include <dali/integration-api/platform-abstraction.h>
26 #include <dali/internal/event/common/thread-local-storage.h>
27 #include <dali/internal/event/images/image-impl.h>
28 #include <dali/internal/event/images/emoji-factory.h>
29 #include <dali/internal/event/text/character-impl.h>
34 GlyphImage::GlyphImage()
38 GlyphImage::GlyphImage( Internal::Image* internal )
43 GlyphImage::~GlyphImage()
47 GlyphImage::GlyphImage(const GlyphImage& handle)
52 GlyphImage& GlyphImage::operator=(const GlyphImage& rhs)
54 BaseHandle::operator=(rhs);
58 GlyphImage GlyphImage::New( const Character& character )
60 // Retrieves the font family name for the given character.
61 const std::string fontFamilyName = Font::GetFamilyForText( character );
63 // Create a text style and set the font family name.
65 style.SetFontName( fontFamilyName );
67 return GlyphImage::New( character, style );
70 GlyphImage GlyphImage::New( const Character& character, const TextStyle& style )
74 // Retrieves the font family name for the given character if is not defined in the text style.
75 std::string fontFamilyName = style.GetFontName();
76 if( fontFamilyName.empty() )
78 fontFamilyName = Font::GetFamilyForText( character );
81 // Creates a font with the parameters given in the text style.
82 const Font font = Font::New( FontParameters( fontFamilyName, style.GetFontStyle(), style.GetFontPointSize() ) );
84 if( GlyphImage::IsColorGlyph( character ) )
86 // Work around. Create images from .png files in the file system.
88 // Get the glyph metrics.
89 const Font::Metrics metrics = font.GetMetrics( character );
91 // Set the image size.
92 ImageAttributes attributes;
93 attributes.SetSize( metrics.GetWidth(), metrics.GetHeight() );
95 image = ResourceImage::New( DALI_EMOTICON_DIR + Internal::ThreadLocalStorage::Get().GetEmojiFactory().GetEmojiFileNameFromCharacter( character.GetImplementation().GetCharacter() ), attributes );
99 // Retrieves the alpha bitmap from the font.
101 Integration::PlatformAbstraction& platform = Internal::ThreadLocalStorage::Get().GetPlatformAbstraction();
103 Integration::BitmapPtr bitmapPtr = platform.GetGlyphImage( font.GetName(),
106 character.GetImplementation().GetCharacter() );
110 // Create a color bitmap with the alpha bitmap retrieved from the platform and the color specified
111 // in the text style.
113 // TODO: support more text decoration features: outline, glow, shadow, underline, ...
114 const std::size_t width = bitmapPtr->GetImageWidth();
115 const std::size_t height = bitmapPtr->GetImageHeight();
116 BitmapImage bitmapImage = BitmapImage::New( width, height, Pixel::RGBA8888 );
118 // Point the source bitmap buffer.
119 unsigned char* src = bitmapPtr->GetBuffer();
121 // Point the destination image buffer.
122 unsigned char* dst = bitmapImage.GetBuffer();
124 //Retrieve the color from the text style.
125 const Vector4& color = style.GetTextColor();
127 std::size_t srcOffset = 0;
128 std::size_t dstOffset = 0;
129 for( std::size_t y = 0; y < height; ++y )
131 for( std::size_t x = 0; x < width; ++x )
133 const float srcAlpha = static_cast<float>( src[ srcOffset ] );
135 dst[ dstOffset + 0 ] = static_cast<unsigned char>( srcAlpha * color.r );
136 dst[ dstOffset + 1 ] = static_cast<unsigned char>( srcAlpha * color.g );
137 dst[ dstOffset + 2 ] = static_cast<unsigned char>( srcAlpha * color.b );
138 dst[ dstOffset + 3 ] = static_cast<unsigned char>( srcAlpha * color.a );
144 bitmapImage.Update();
150 return GlyphImage( static_cast<Internal::Image*>( image.GetObjectPtr() ) );
153 GlyphImage GlyphImage::DownCast( BaseHandle handle )
155 return GlyphImage( dynamic_cast<Dali::Internal::Image*>( handle.GetObjectPtr() ) );
158 bool GlyphImage::IsColorGlyph( const Character& character )
160 return Internal::ThreadLocalStorage::Get().GetEmojiFactory().IsEmoji( character.GetImplementation().GetCharacter() );