2 * Copyright (c) 2019 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 * @file simple-text-renderer-example.cpp
20 * @brief Basic usage of Text Renderer utility.
24 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
25 #include <dali-toolkit/dali-toolkit.h>
26 #include <dali-toolkit/devel-api/text/text-utils-devel.h>
27 #include <devel-api/adaptor-framework/image-loading.h>
34 using namespace Dali::Toolkit;
39 const std::string IMAGE1 = DEMO_IMAGE_DIR "application-icon-1.png";
40 const std::string IMAGE2 = DEMO_IMAGE_DIR "application-icon-6.png";
42 #define MAKE_SHADER(A)#A
44 const std::string VERSION_3_ES = "#version 300 es\n";
46 const char* VERTEX_SHADER = MAKE_SHADER(
47 precision mediump float;
55 uniform mat4 uMvpMatrix;
59 vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);
60 vertexPosition.xyz *= uSize;
61 gl_Position = uMvpMatrix * vertexPosition;
67 const char* FRAGMENT_SHADER = MAKE_SHADER(
68 precision mediump float;
74 uniform sampler2D sAlbedo;
79 vec4 color = texture( sAlbedo, vUV );
80 FragColor = vec4( color.rgb, uColor.a * color.a );
84 Renderer CreateRenderer()
86 // Create the geometry.
89 Dali::Vector2 position;
90 Dali::Vector2 texCoord;
93 static const Vertex vertices[] = {{ Dali::Vector2( -0.5f, -0.5f ), Dali::Vector2( 0.0f, 0.0f ) },
94 { Dali::Vector2( 0.5f, -0.5f ), Dali::Vector2( 1.0f, 0.0f ) },
95 { Dali::Vector2( -0.5f, 0.5f ), Dali::Vector2( 0.0f, 1.0f ) },
96 { Dali::Vector2( 0.5f, 0.5f ), Dali::Vector2( 1.0f, 1.0f ) }};
98 Property::Map property;
99 property.Add("aPosition", Property::VECTOR2).Add("aTexCoord", Property::VECTOR2);
101 PropertyBuffer vertexBuffer = PropertyBuffer::New(property);
103 vertexBuffer.SetData(vertices, sizeof(vertices) / sizeof(Vertex));
105 Geometry geometry = Geometry::New();
106 geometry.AddVertexBuffer(vertexBuffer);
108 geometry.SetType(Geometry::TRIANGLE_STRIP);
111 Shader shader = Shader::New( VERSION_3_ES + VERTEX_SHADER, VERSION_3_ES + FRAGMENT_SHADER );
113 // Create the renderer
115 Renderer renderer = Renderer::New( geometry, shader );
120 TextureSet CreateTextureSet( const Dali::Toolkit::DevelText::RendererParameters& textParameters, const std::vector<std::string>& embeddedItems )
123 Dali::Vector<Dali::Toolkit::DevelText::EmbeddedItemInfo> embeddedItemLayout;
125 Devel::PixelBuffer pixelBuffer = Toolkit::DevelText::Render( textParameters, embeddedItemLayout );
128 const int dstWidth = static_cast<int>( pixelBuffer.GetWidth() );
129 const int dstHeight = static_cast<int>( pixelBuffer.GetHeight() );
131 unsigned int index = 0u;
132 for( const auto& itemLayout : embeddedItemLayout )
134 int width = static_cast<int>( itemLayout.size.width );
135 int height = static_cast<int>( itemLayout.size.height );
136 int x = static_cast<int>( itemLayout.position.x );
137 int y = static_cast<int>( itemLayout.position.y );
139 Dali::Devel::PixelBuffer itemPixelBuffer = Dali::LoadImageFromFile( embeddedItems[index++] );
140 itemPixelBuffer.Resize( width, height );
141 itemPixelBuffer.Rotate( itemLayout.angle );
143 width = static_cast<int>( itemPixelBuffer.GetWidth() );
144 height = static_cast<int>( itemPixelBuffer.GetHeight() );
146 Dali::Pixel::Format itemPixelFormat = itemPixelBuffer.GetPixelFormat();
148 // Check if the item is out of the buffer.
150 if( ( x + width < 0 ) ||
153 ( y - height > dstHeight ) )
155 // The embedded item is completely out of the buffer.
159 // Crop if it exceeds the boundaries of the destination buffer.
164 int newWidth = width;
165 int newHeight = height;
172 cropX = std::abs( x );
180 if( cropX + newWidth > dstWidth )
183 newWidth -= ( ( cropX + newWidth ) - dstWidth );
189 newHeight += layoutY;
190 cropY = std::abs(layoutY);
194 if( cropY + newHeight > dstHeight )
197 newHeight -= ( ( cropY + newHeight ) - dstHeight );
200 uint16_t uiCropX = static_cast<uint16_t>(cropX);
201 uint16_t uiCropY = static_cast<uint16_t>(cropY);
202 uint16_t uiNewWidth = static_cast<uint16_t>(newWidth);
203 uint16_t uiNewHeight = static_cast<uint16_t>(newHeight);
207 itemPixelBuffer.Crop( uiCropX, uiCropY, uiNewWidth, uiNewHeight );
210 // Blend the item pixel buffer with the text's color according its blending mode.
211 if( Dali::TextAbstraction::ColorBlendingMode::MULTIPLY == itemLayout.colorBlendingMode )
213 Dali::Devel::PixelBuffer buffer = Dali::Devel::PixelBuffer::New( uiNewWidth,
217 unsigned char* bufferPtr = buffer.GetBuffer();
218 const unsigned char* itemBufferPtr = itemPixelBuffer.GetBuffer();
219 const unsigned int bytesPerPixel = Dali::Pixel::GetBytesPerPixel(itemPixelFormat);
220 const unsigned int size = uiNewWidth * uiNewHeight * bytesPerPixel;
222 for (unsigned int i = 0u; i < size; i += bytesPerPixel)
224 *(bufferPtr + 0u) = static_cast<unsigned char>( static_cast<float>( *(itemBufferPtr + 0u) ) * textParameters.textColor.r );
225 *(bufferPtr + 1u) = static_cast<unsigned char>( static_cast<float>( *(itemBufferPtr + 1u) ) * textParameters.textColor.g );
226 *(bufferPtr + 2u) = static_cast<unsigned char>( static_cast<float>( *(itemBufferPtr + 2u) ) * textParameters.textColor.b );
227 *(bufferPtr + 3u) = static_cast<unsigned char>( static_cast<float>( *(itemBufferPtr + 3u) ) * textParameters.textColor.a );
229 itemBufferPtr += bytesPerPixel;
230 bufferPtr += bytesPerPixel;
233 itemPixelBuffer = buffer;
236 Dali::Toolkit::DevelText::UpdateBuffer(itemPixelBuffer, pixelBuffer, layoutX, layoutY, true);
239 PixelData pixelData = Devel::PixelBuffer::Convert( pixelBuffer );
241 Texture texture = Texture::New( TextureType::TEXTURE_2D,
242 pixelData.GetPixelFormat(),
243 pixelData.GetWidth(),
244 pixelData.GetHeight() );
245 texture.Upload(pixelData);
247 TextureSet textureSet = TextureSet::New();
248 textureSet.SetTexture( 0u, texture );
257 * @brief The main class of the demo.
259 class SimpleTextRendererExample : public ConnectionTracker
263 SimpleTextRendererExample( Application& application )
264 : mApplication( application )
266 // Connect to the Application's Init signal
267 mApplication.InitSignal().Connect( this, &SimpleTextRendererExample::Create );
270 ~SimpleTextRendererExample()
272 // Nothing to do here.
276 * One-time setup in response to Application InitSignal.
278 void Create( Application& application )
280 Stage stage = Stage::GetCurrent();
281 stage.SetBackgroundColor( Color::WHITE );
282 stage.SetBackgroundColor( Vector4( 0.04f, 0.345f, 0.392f, 1.0f ) );
284 stage.KeyEventSignal().Connect(this, &SimpleTextRendererExample::OnKeyEvent);
286 const std::string image1 = "<item 'width'=26 'height'=26 'url'='" + IMAGE1 + "'/>";
287 const std::string image2 = "<item 'width'=26 'height'=26/>";
289 Dali::Toolkit::DevelText::RendererParameters textParameters;
290 textParameters.text = "Hello " + image1 + " world " + image2 + " this " + image1 + " is " + image2 + " a " + image1 + " demo " + image2 + " of " + image1 + " circular " + image2 + " text " + image1 + " width " + image2 + " icons.";
291 textParameters.horizontalAlignment = "center";
292 textParameters.verticalAlignment = "center";
293 textParameters.circularAlignment = "center";
294 textParameters.fontFamily = "SamsungUI";
295 textParameters.fontWeight = "";
296 textParameters.fontWidth = "";
297 textParameters.fontSlant = "";
298 textParameters.layout = "circular";
299 textParameters.textColor = Color::BLACK;
300 textParameters.fontSize = 25.f;
301 textParameters.textWidth = 360u;
302 textParameters.textHeight = 360u;
303 textParameters.radius = 180u;
304 textParameters.beginAngle = 15.f;
305 textParameters.incrementAngle = 360.f;
306 textParameters.ellipsisEnabled = true;
307 textParameters.markupEnabled = true;
309 std::vector<std::string> embeddedItems = { IMAGE2, IMAGE2, IMAGE2, IMAGE2, IMAGE2 };
311 TextureSet textureSet = CreateTextureSet( textParameters, embeddedItems );
313 Renderer renderer = CreateRenderer();
314 renderer.SetTextures( textureSet );
316 Actor actor = Actor::New();
317 actor.SetAnchorPoint( AnchorPoint::CENTER );
318 actor.SetParentOrigin( ParentOrigin::CENTER );
319 actor.SetPosition( 0.f, 0.f);
320 actor.SetSize( 360.f, 360.f );
321 actor.SetColor( Color::WHITE );
323 actor.AddRenderer( renderer );
329 * Main key event handler
331 void OnKeyEvent(const KeyEvent& event)
333 if(event.state == KeyEvent::Down)
335 if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
344 Application& mApplication;
347 /** Entry point for Linux & Tizen applications */
348 int main( int argc, char **argv )
350 Application application = Application::New( &argc, &argv );
352 SimpleTextRendererExample test( application );
354 application.MainLoop();