PushButton to use container Actor size not just ImageActors added to it
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / text / font-wrapper.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 "font-wrapper.h"
20
21 // INTERNAL INCLUDES
22 #include <v8-utils.h>
23 #include <dali-wrapper.h>
24 #include <text/font-api.h>
25 #include <shared/api-function.h>
26 #include <shared/object-template-helper.h>
27
28 namespace Dali
29 {
30
31 namespace V8Plugin
32 {
33
34 namespace // un-named name space
35 {
36
37 /**
38  * Contains a list of all functions that can be called on the font object
39  */
40 const ApiFunction FontFunctionTable[]=
41 {
42     /**************************************
43     * Font API (in order of font.h)
44     **************************************/
45     { "MeasureTextWidth",               FontApi::MeasureTextWidth },
46     { "MeasureTextHeight",              FontApi::MeasureTextHeight },
47     { "MeasureText",                    FontApi::MeasureText },
48     { "AllGlyphsSupported",             FontApi::AllGlyphsSupported },
49     { "GetProperties",                  FontApi::GetProperties }, // replace all getters
50     { "GetMetrics",                     FontApi::GetMetrics },
51 };
52
53 const unsigned int FontFunctionTableCount = sizeof(FontFunctionTable)/sizeof(FontFunctionTable[0]);
54
55 /**
56  * Contains a list of all functions that can be called
57  */
58 const ApiFunction StaticFontFunctionTable[]=
59 {
60     /**************************************
61     * Static font functions, called without a font object
62     **************************************/
63     { "GetFamilyForText",               FontApi::GetFamilyForText },
64     { "GetLineHeightFromCapsHeight",    FontApi::GetLineHeightFromCapsHeight },
65     { "GetInstalledFonts",              FontApi::GetInstalledFonts },
66     { "PointsToPixels",                 FontApi::PointsToPixels },
67     { "PixelsToPoints",                 FontApi::PixelsToPoints },
68 };
69
70 const unsigned int StaticFontFunctionTableCount = sizeof(StaticFontFunctionTable)/sizeof(FontFunctionTable[0]);
71
72 } //un-named space
73
74
75 FontWrapper::FontWrapper( const Dali::Font& font, GarbageCollectorInterface& gc )
76 : BaseWrappedObject( BaseWrappedObject::FONT , gc )
77 {
78     mFont = font;
79 }
80
81 v8::Handle<v8::Object> FontWrapper::WrapFont(v8::Isolate* isolate, const Dali::Font& font )
82 {
83   v8::EscapableHandleScope handleScope( isolate );
84   v8::Local<v8::ObjectTemplate> objectTemplate;
85
86   objectTemplate = MakeFontTemplate( isolate );
87
88   // create an instance of the template
89   v8::Local<v8::Object> localObject = objectTemplate->NewInstance();
90
91   // create the Font wrapper
92   FontWrapper* pointer =  new FontWrapper( font, Dali::V8Plugin::DaliWrapper::Get().GetDaliGarbageCollector() );
93
94   // assign the JavaScript object to the wrapper.
95   pointer->SetJavascriptObject( isolate, localObject );
96
97   printf("Created Font!\n");
98   return handleScope.Escape( localObject );
99 }
100
101
102 v8::Handle<v8::ObjectTemplate> FontWrapper::MakeFontTemplate( v8::Isolate* isolate )
103 {
104   v8::EscapableHandleScope handleScope( isolate );
105
106   v8::Local<v8::ObjectTemplate> objTemplate = v8::ObjectTemplate::New();
107
108   objTemplate->SetInternalFieldCount( BaseWrappedObject::FIELD_COUNT );
109
110   // add our function properties
111   ObjectTemplateHelper::InstallFunctions( isolate, objTemplate, FontFunctionTable, FontFunctionTableCount );
112
113   return handleScope.Escape( objTemplate );
114 }
115
116 void FontWrapper::NewFont( const v8::FunctionCallbackInfo< v8::Value >& args)
117 {
118   v8::Isolate* isolate = args.GetIsolate();
119   v8::HandleScope handleScope( isolate);
120
121   if(!args.IsConstructCall())
122   {
123     DALI_SCRIPT_EXCEPTION( isolate, "Font constructor called without 'new'");
124     return;
125   }
126
127   // attribs can be passed by value
128   Dali::Font font = FontApi::New( args );
129   if( !font )
130   {
131     DALI_SCRIPT_EXCEPTION( isolate, "bad font parameters\n");
132     return;
133   }
134
135   v8::Local<v8::Object> localObject = WrapFont( isolate, font );
136   args.GetReturnValue().Set( localObject );
137 }
138
139 Font FontWrapper::GetFont()
140 {
141   return mFont;
142 }
143
144 v8::Handle<v8::Object> FontWrapper::GetStaticFontObject(v8::Isolate* isolate)
145 {
146   v8::EscapableHandleScope handleScope( isolate );
147
148   v8::Local<v8::ObjectTemplate> objTemplate = v8::ObjectTemplate::New();
149
150    // add our functions properties
151   ObjectTemplateHelper::InstallFunctions( isolate, objTemplate, StaticFontFunctionTable, StaticFontFunctionTableCount );
152
153   v8::Local<v8::Object> localObject = objTemplate->NewInstance();
154
155   return handleScope.Escape( localObject );
156
157  }
158
159
160 } // namespace V8Plugin
161
162 } // namespace Dali