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