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/text/text-style.h>
24 #include <dali/integration-api/platform-abstraction.h>
25 #include <dali/internal/event/common/thread-local-storage.h>
26 #include <dali/internal/event/images/image-impl.h>
27 #include <dali/internal/event/images/emoji-factory.h>
28 #include <dali/internal/event/text/character-impl.h>
33 GlyphImage::GlyphImage()
37 GlyphImage::GlyphImage( Internal::Image* internal )
42 GlyphImage::~GlyphImage()
46 GlyphImage::GlyphImage(const GlyphImage& handle)
51 GlyphImage& GlyphImage::operator=(const GlyphImage& rhs)
53 BaseHandle::operator=(rhs);
57 GlyphImage GlyphImage::New( const Character& character )
59 // Retrieves the font family name for the given character.
60 const std::string fontFamilyName = Font::GetFamilyForText( character );
62 // Create a text style and set the font family name.
64 style.SetFontName( fontFamilyName );
66 return GlyphImage::New( character, style );
69 GlyphImage GlyphImage::New( const Character& character, const TextStyle& style )
73 // Retrieves the font family name for the given character if is not defined in the text style.
74 std::string fontFamilyName = style.GetFontName();
75 if( fontFamilyName.empty() )
77 fontFamilyName = Font::GetFamilyForText( character );
80 // Creates a font with the parameters given in the text style.
81 const Font font = Font::New( FontParameters( fontFamilyName, style.GetFontStyle(), style.GetFontPointSize() ) );
83 if( GlyphImage::IsColorGlyph( character ) )
85 // Work around. Create images from .png files in the file system.
87 // Get the glyph metrics.
88 const Font::Metrics metrics = font.GetMetrics( character );
90 // Set the image size.
91 ImageAttributes attributes;
92 attributes.SetSize( metrics.GetWidth(), metrics.GetHeight() );
94 image = Image::New( DALI_EMOTICON_DIR + Internal::ThreadLocalStorage::Get().GetEmojiFactory().GetEmojiFileNameFromCharacter( character.GetImplementation().GetCharacter() ), attributes );
98 // Retrieves the alpha bitmap from the font.
100 Integration::PlatformAbstraction& platform = Internal::ThreadLocalStorage::Get().GetPlatformAbstraction();
102 Integration::BitmapPtr bitmapPtr = platform.GetGlyphImage( font.GetName(),
105 character.GetImplementation().GetCharacter() );
109 // Create a color bitmap with the alpha bitmap retrieved from the platform and the color specified
110 // in the text style.
112 // TODO: support more text decoration features: outline, glow, shadow, underline, ...
113 const std::size_t width = bitmapPtr->GetImageWidth();
114 const std::size_t height = bitmapPtr->GetImageHeight();
115 BitmapImage bitmapImage = BitmapImage::New( width, height, Pixel::RGBA8888 );
117 // Point the source bitmap buffer.
118 unsigned char* src = bitmapPtr->GetBuffer();
120 // Point the destination image buffer.
121 unsigned char* dst = bitmapImage.GetBuffer();
123 //Retrieve the color from the text style.
124 const Vector4& color = style.GetTextColor();
126 std::size_t srcOffset = 0;
127 std::size_t dstOffset = 0;
128 for( std::size_t y = 0; y < height; ++y )
130 for( std::size_t x = 0; x < width; ++x )
132 const float srcAlpha = static_cast<float>( src[ srcOffset ] );
134 dst[ dstOffset + 0 ] = static_cast<unsigned char>( srcAlpha * color.r );
135 dst[ dstOffset + 1 ] = static_cast<unsigned char>( srcAlpha * color.g );
136 dst[ dstOffset + 2 ] = static_cast<unsigned char>( srcAlpha * color.b );
137 dst[ dstOffset + 3 ] = static_cast<unsigned char>( srcAlpha * color.a );
143 bitmapImage.Update();
149 return GlyphImage( static_cast<Internal::Image*>( image.GetObjectPtr() ) );
152 GlyphImage GlyphImage::DownCast( BaseHandle handle )
154 return GlyphImage( dynamic_cast<Dali::Internal::Image*>( handle.GetObjectPtr() ) );
157 bool GlyphImage::IsColorGlyph( const Character& character )
159 return Internal::ThreadLocalStorage::Get().GetEmojiFactory().IsEmoji( character.GetImplementation().GetCharacter() );