2 * Copyright (c) 2015 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 #include "font-wrapper.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>
34 namespace // un-named name space
38 * Contains a list of all functions that can be called on the font object
40 const ApiFunction FontFunctionTable[]=
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 },
53 const unsigned int FontFunctionTableCount = sizeof(FontFunctionTable)/sizeof(FontFunctionTable[0]);
56 * Contains a list of all functions that can be called
58 const ApiFunction StaticFontFunctionTable[]=
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 },
70 const unsigned int StaticFontFunctionTableCount = sizeof(StaticFontFunctionTable)/sizeof(FontFunctionTable[0]);
75 FontWrapper::FontWrapper( const Dali::Font& font, GarbageCollectorInterface& gc )
76 : BaseWrappedObject( BaseWrappedObject::FONT , gc )
81 v8::Handle<v8::Object> FontWrapper::WrapFont(v8::Isolate* isolate, const Dali::Font& font )
83 v8::EscapableHandleScope handleScope( isolate );
84 v8::Local<v8::ObjectTemplate> objectTemplate;
86 objectTemplate = MakeFontTemplate( isolate );
88 // create an instance of the template
89 v8::Local<v8::Object> localObject = objectTemplate->NewInstance();
91 // create the Font wrapper
92 FontWrapper* pointer = new FontWrapper( font, Dali::V8Plugin::DaliWrapper::Get().GetDaliGarbageCollector() );
94 // assign the JavaScript object to the wrapper.
95 pointer->SetJavascriptObject( isolate, localObject );
97 printf("Created Font!\n");
98 return handleScope.Escape( localObject );
102 v8::Handle<v8::ObjectTemplate> FontWrapper::MakeFontTemplate( v8::Isolate* isolate )
104 v8::EscapableHandleScope handleScope( isolate );
106 v8::Local<v8::ObjectTemplate> objTemplate = v8::ObjectTemplate::New();
108 objTemplate->SetInternalFieldCount( BaseWrappedObject::FIELD_COUNT );
110 // add our function properties
111 ObjectTemplateHelper::InstallFunctions( isolate, objTemplate, FontFunctionTable, FontFunctionTableCount );
113 return handleScope.Escape( objTemplate );
116 void FontWrapper::NewFont( const v8::FunctionCallbackInfo< v8::Value >& args)
118 v8::Isolate* isolate = args.GetIsolate();
119 v8::HandleScope handleScope( isolate);
121 if(!args.IsConstructCall())
123 DALI_SCRIPT_EXCEPTION( isolate, "Font constructor called without 'new'");
127 // attribs can be passed by value
128 Dali::Font font = FontApi::New( args );
131 DALI_SCRIPT_EXCEPTION( isolate, "bad font parameters\n");
135 v8::Local<v8::Object> localObject = WrapFont( isolate, font );
136 args.GetReturnValue().Set( localObject );
139 Font FontWrapper::GetFont()
144 v8::Handle<v8::Object> FontWrapper::GetStaticFontObject(v8::Isolate* isolate)
146 v8::EscapableHandleScope handleScope( isolate );
148 v8::Local<v8::ObjectTemplate> objTemplate = v8::ObjectTemplate::New();
150 // add our functions properties
151 ObjectTemplateHelper::InstallFunctions( isolate, objTemplate, StaticFontFunctionTable, StaticFontFunctionTableCount );
153 v8::Local<v8::Object> localObject = objTemplate->NewInstance();
155 return handleScope.Escape( localObject );
160 } // namespace V8Plugin