Merge "Layout Engine - Fix for multiline" into new_text
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / actors / text-actor-api.cpp
1 /*
2  * Copyright (c) 2015 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 "text-actor-api.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/text/text-actor-parameters.h>
23
24 // INTERNAL INCLUDES
25 #include <object/handle-wrapper.h>
26 #include <v8-utils.h>
27 #include <object/property-value-wrapper.h>
28 #include <text/font-api.h>
29
30
31 namespace Dali
32 {
33
34 namespace V8Plugin
35 {
36
37 namespace //unnamed name space
38 {
39
40 struct TextActorParametersInternal
41 {
42   TextActorParametersInternal()
43   : fontDetection( true )
44   {
45   }
46   bool fontDetection;
47   Font font;
48 };
49
50
51 TextActor GetTextActor( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
52 {
53   HandleWrapper* handleWrapper = HandleWrapper::Unwrap( isolate, args.This() );
54   return TextActor::DownCast( handleWrapper->mHandle );
55 }
56
57
58 void GetTextOptions( v8::Isolate* isolate,
59                           v8::Local<v8::Value > options,
60                           TextActorParametersInternal& textParams )
61 {
62   //      fontDetection: true / false  ( default  true)
63   //      font: dali font object
64   v8::HandleScope handleScope( isolate );
65
66   if( options->IsObject() )
67   {
68     v8::Local<v8::Object> obj = options->ToObject();
69
70     v8::Local<v8::Value> fontDetect = obj->Get( v8::String::NewFromUtf8( isolate, "fontDetection" ) );
71     if( fontDetect->IsBoolean() )
72     {
73
74       textParams.fontDetection = fontDetect->ToBoolean()->Value();
75     }
76
77     v8::Local<v8::Value> fontValue = obj->Get( v8::String::NewFromUtf8( isolate, "font" ) );
78     if( fontValue->IsObject() )
79     {
80       textParams.font = FontApi::GetFont( isolate, fontValue );
81     }
82
83   }
84 }
85
86 }
87
88 /**
89  * @constructor
90  * @for TextActor
91  * @method TextActor
92  * @param {String} text
93  * @param {Object} [textOptions] data
94  * Options text options struct
95  * @param {Boolean} [textOptions.fontDetection]
96  * if true the fontDetection is used to make sure the text is displayed.
97  * E.g. if the current font used by the text-actor does not support certain characters
98  * it will find a new font that does. Default = true.
99  * @param {Object}  [textOptions.font]
100  * Dali font object
101  * @return {Object} TextActor
102  */
103 Actor TextActorApi::New( const v8::FunctionCallbackInfo<v8::Value>& args )
104 {
105   v8::Isolate* isolate = args.GetIsolate();
106   v8::HandleScope handleScope( isolate );
107
108   //
109   // TextActor( text, options (optional) )
110   //
111   // options =
112   // {
113   //    font:   font
114   //    fontDetection: true / false   ( default  true)
115   // }
116
117   // get the text (if passed in)
118
119   bool found( false );
120   std::string text = V8Utils::GetStringParameter( PARAMETER_0, found, isolate, args );
121
122   TextActorParametersInternal params;
123   TextActor actor;
124
125   GetTextOptions( isolate, args[1], params );
126
127   TextStyle style;
128
129   if( params.font )
130   {
131     style.SetFontName( params.font.GetName() );
132     style.SetFontStyle( params.font.GetStyle() );
133     style.SetFontPointSize( PointSize(params.font.GetPointSize()));
134
135   }
136   TextActorParameters textActorParameters( style, params.fontDetection? TextActorParameters::FONT_DETECTION_OFF:TextActorParameters::FONT_DETECTION_ON );
137
138   actor = TextActor::New( text, textActorParameters );
139
140   return actor;
141
142 }
143
144 /**
145  * Set text to the natural size of the text string.
146  *
147  * After this method the text actor always uses the natural size of the text
148  * when SetText is called unless SetSize is called to override the size.
149  *
150  * @for TextActor
151  * @method setToNaturalSize
152  */
153 void TextActorApi::SetToNaturalSize( const v8::FunctionCallbackInfo<v8::Value>& args )
154 {
155   v8::Isolate* isolate = args.GetIsolate();
156   v8::HandleScope handleScope( isolate );
157
158   TextActor textActor = GetTextActor( isolate, args );
159   textActor.SetToNaturalSize();
160 }
161
162 } // namespace V8Plugin
163
164 } // namespace Dali