b31958165f86861cb15ec4f94ebc0c7a7da0256b
[platform/core/uifw/dali-core.git] / dali / public-api / images / glyph-image.cpp
1 /*
2  * Copyright (c) 2014 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/public-api/images/glyph-image.h>
20
21 // INTERNAL INCLUDES
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>
29
30 namespace Dali
31 {
32
33 GlyphImage::GlyphImage()
34 {
35 }
36
37 GlyphImage::GlyphImage( Internal::Image* internal )
38 : Image( internal )
39 {
40 }
41
42 GlyphImage::~GlyphImage()
43 {
44 }
45
46 GlyphImage::GlyphImage(const GlyphImage& handle)
47 : Image(handle)
48 {
49 }
50
51 GlyphImage& GlyphImage::operator=(const GlyphImage& rhs)
52 {
53   BaseHandle::operator=(rhs);
54   return *this;
55 }
56
57 GlyphImage GlyphImage::New( const Character& character )
58 {
59   // Retrieves the font family name for the given character.
60   const std::string fontFamilyName = Font::GetFamilyForText( character );
61
62   // Create a text style and set the font family name.
63   TextStyle style;
64   style.SetFontName( fontFamilyName );
65
66   return GlyphImage::New( character, style );
67 }
68
69 GlyphImage GlyphImage::New( const Character& character, const TextStyle& style )
70 {
71   Image image;
72
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() )
76   {
77     fontFamilyName = Font::GetFamilyForText( character );
78   }
79
80   // Creates a font with the parameters given in the text style.
81   const Font font = Font::New( FontParameters( fontFamilyName, style.GetFontStyle(), style.GetFontPointSize() ) );
82
83   if( GlyphImage::IsColorGlyph( character ) )
84   {
85     // Work around. Create images from .png files in the file system.
86
87     // Get the glyph metrics.
88     const Font::Metrics metrics = font.GetMetrics( character );
89
90     // Set the image size.
91     ImageAttributes attributes;
92     attributes.SetSize( metrics.GetWidth(), metrics.GetHeight() );
93
94     image = Image::New( DALI_EMOTICON_DIR + Internal::ThreadLocalStorage::Get().GetEmojiFactory().GetEmojiFileNameFromCharacter( character.GetImplementation().GetCharacter() ), attributes );
95   }
96   else
97   {
98     // Retrieves the alpha bitmap from the font.
99
100     Integration::PlatformAbstraction& platform = Internal::ThreadLocalStorage::Get().GetPlatformAbstraction();
101
102     Integration::BitmapPtr bitmapPtr = platform.GetGlyphImage( font.GetName(),
103                                                                font.GetStyle(),
104                                                                font.GetPointSize(),
105                                                                character.GetImplementation().GetCharacter() );
106
107     if( bitmapPtr )
108     {
109       // Create a color bitmap with the alpha bitmap retrieved from the platform and the color specified
110       // in the text style.
111       //
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 );
116
117       // Point the source bitmap buffer.
118       unsigned char* src = bitmapPtr->GetBuffer();
119
120       // Point the destination image buffer.
121       unsigned char* dst = bitmapImage.GetBuffer();
122
123       //Retrieve the color from the text style.
124       const Vector4& color = style.GetTextColor();
125
126       std::size_t srcOffset = 0;
127       std::size_t dstOffset = 0;
128       for( std::size_t y = 0; y < height; ++y )
129       {
130         for( std::size_t x = 0; x < width; ++x )
131         {
132           const float srcAlpha = static_cast<float>( src[ srcOffset ] );
133
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 );
138           ++srcOffset;
139           dstOffset += 4u;
140         }
141       }
142
143       bitmapImage.Update();
144
145       image = bitmapImage;
146     }
147   }
148
149   return GlyphImage( static_cast<Internal::Image*>( image.GetObjectPtr() ) );
150 }
151
152 GlyphImage GlyphImage::DownCast( BaseHandle handle )
153 {
154   return GlyphImage( dynamic_cast<Dali::Internal::Image*>( handle.GetObjectPtr() ) );
155 }
156
157 bool GlyphImage::IsColorGlyph( const Character& character )
158 {
159   return Internal::ThreadLocalStorage::Get().GetEmojiFactory().IsEmoji( character.GetImplementation().GetCharacter() );
160 }
161
162 } // namespace Dali