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.
23 #include <text/font-wrapper.h>
24 #include <object/property-value-wrapper.h>
32 namespace // un named namespace
38 } //un named namespace
40 /***************************************
43 ****************************************/
44 Font FontApi::GetFont( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
46 v8::HandleScope handleScope( isolate );
48 v8::Local<v8::Object> object = args.This();
49 v8::Local<v8::External> field = v8::Local<v8::External>::Cast( object->GetInternalField(0) );
50 void* ptr = field->Value();
52 FontWrapper* wrapper = static_cast< FontWrapper *>(ptr);
53 return wrapper->GetFont();
55 Font FontApi::GetFont( v8::Isolate* isolate, v8::Local<v8::Value>& value )
57 v8::HandleScope handleScope( isolate );
58 v8::Local<v8::Object> object = value->ToObject();
59 v8::Local<v8::External> field = v8::Local<v8::External>::Cast( object->GetInternalField(0) );
60 void* ptr = field->Value();
62 FontWrapper* wrapper = static_cast< FontWrapper *>(ptr);
63 return wrapper->GetFont();
81 sizeType( FontParams::NO_SIZE_SET )
96 void ReadFontParameters( v8::Isolate* isolate,
97 v8::Local<v8::Value > options,
98 FontParams& fontParams )
100 // foont options is an optional parameter passed in which holds
102 // var fontOptions = {
105 // // one of the following
108 // capsHeight:xx // height of a capital letter above the baseline for a particular typeface.
111 v8::HandleScope handleScope( isolate );
112 if( !options->IsObject() )
114 DALI_SCRIPT_EXCEPTION( isolate, "bad parameter 0 ( font parameters)" );
117 v8::Local<v8::Object> obj = options->ToObject();
119 v8::Local<v8::Value> familyValue = obj->Get( v8::String::NewFromUtf8( isolate, "family" ) );
120 if( familyValue->IsString() )
122 fontParams.family = V8Utils::v8StringToStdString( familyValue );
125 v8::Local<v8::Value> styleValue = obj->Get( v8::String::NewFromUtf8( isolate, "style" ) );
126 if( styleValue->IsString() )
128 fontParams.style = V8Utils::v8StringToStdString( styleValue );
131 v8::Local<v8::Value> pixelSize = obj->Get( v8::String::NewFromUtf8( isolate, "pixelSize" ) );
132 v8::Local<v8::Value> pointSize = obj->Get( v8::String::NewFromUtf8( isolate, "pointSize" ) );
133 v8::Local<v8::Value> capsHeight = obj->Get( v8::String::NewFromUtf8( isolate, "capsHeight" ) );
135 if( pixelSize->IsUint32() )
137 fontParams.pixelSize.value = pixelSize->ToUint32()->Value();
138 fontParams.sizeType = FontParams::USE_PIXEL_SIZE;
140 else if( pointSize->IsUint32() )
142 fontParams.pointSize.value = pointSize->ToUint32()->Value();
143 fontParams.sizeType = FontParams::USE_POINT_SIZE;
145 else if( capsHeight->IsUint32() )
147 fontParams.capsSize.value = capsHeight->ToUint32()->Value();
148 fontParams.sizeType = FontParams::USE_CAP_SIZE;
153 Font FontApi::New( const v8::FunctionCallbackInfo< v8::Value >& args )
155 v8::Isolate* isolate = args.GetIsolate();
156 v8::HandleScope handleScope( isolate );
158 // if no parameters just create a default font
159 if ( args.Length() == 0)
165 ReadFontParameters( isolate, args[0], params );
167 FontParameters fontParams;
168 // construct a dali font parameters object
169 switch( params.sizeType)
171 case FontParams::USE_PIXEL_SIZE:
173 fontParams = FontParameters( params.family, params.style, params.pixelSize );
176 case FontParams::USE_POINT_SIZE:
178 fontParams = FontParameters( params.family, params.style, params.pointSize );
181 case FontParams::USE_CAP_SIZE:
183 fontParams = FontParameters( params.family, params.style, params.capsSize );
188 fontParams = FontParameters( params.family, params.style, PointSize(0.f));
192 return Font::New( fontParams );
197 void FontApi::GetFamilyForText( const v8::FunctionCallbackInfo< v8::Value >& args )
199 v8::Isolate* isolate = args.GetIsolate();
200 v8::HandleScope handleScope( isolate );
204 std::string text = V8Utils::GetStringParameter( PARAMETER_0, foundString, isolate, args );
207 DALI_SCRIPT_EXCEPTION( isolate, "bad parameter 0 ( text )" );
210 std::string family = Font::GetFamilyForText( text );
212 args.GetReturnValue().Set( v8::String::NewFromUtf8( isolate, family.c_str()) );
215 void FontApi::GetLineHeightFromCapsHeight( const v8::FunctionCallbackInfo< v8::Value >& args )
217 v8::Isolate* isolate = args.GetIsolate();
218 v8::HandleScope handleScope( isolate );
221 ReadFontParameters( isolate, args[0], params );
222 if( params.sizeType != FontParams::USE_CAP_SIZE )
224 DALI_SCRIPT_EXCEPTION( isolate, "caps height not found" );
228 PixelSize size = Font::GetLineHeightFromCapsHeight( params.family, params.style, params.capsSize);
229 args.GetReturnValue().Set( v8::Integer::New( isolate, size.value ) );
232 void FontApi::GetInstalledFonts( const v8::FunctionCallbackInfo< v8::Value >& args )
234 v8::Isolate* isolate = args.GetIsolate();
235 v8::HandleScope handleScope( isolate );
238 std::vector<std::string> fontList;
240 std::string mode = V8Utils::GetStringParameter( PARAMETER_0, found, isolate, args );
242 if( mode == "LIST_APPLICATION_FONTS" )
244 fontList = Font::GetInstalledFonts( Font::LIST_APPLICATION_FONTS );
246 else if( mode == "LIST_ALL_FONTS")
248 fontList = Font::GetInstalledFonts( Font::LIST_ALL_FONTS );
252 fontList = Font::GetInstalledFonts( Font::LIST_SYSTEM_FONTS );
254 // create a javascript array
255 v8::Local<v8::Array> array = v8::Array::New(isolate, fontList.size() );
256 for( std::size_t i = 0; i < fontList.size(); i++)
258 const char* fontName = fontList[i].c_str();
259 array->Set(v8::Integer::New(args.GetIsolate(), i), v8::String::NewFromUtf8( isolate,fontName));
262 args.GetReturnValue().Set( array );
265 void FontApi::MeasureTextWidth( const v8::FunctionCallbackInfo< v8::Value >& args )
267 v8::Isolate* isolate = args.GetIsolate();
268 v8::HandleScope handleScope( isolate );
269 Font font = GetFont( isolate, args );
271 //float MeasureTextWidth(const std::string& text, float textHeightPx) const;
274 std::string text = V8Utils::GetStringParameter( PARAMETER_0, found, isolate, args );
277 DALI_SCRIPT_EXCEPTION( isolate, "text not found" );
280 int height = V8Utils::GetIntegerParameter( PARAMETER_1, found, isolate, args, 0 );
283 DALI_SCRIPT_EXCEPTION( isolate, "missing text height" );
286 float width = font.MeasureTextWidth( text, height );
288 args.GetReturnValue().Set( v8::Integer::New( isolate, width ) );
291 void FontApi::MeasureTextHeight( const v8::FunctionCallbackInfo< v8::Value >& args )
293 v8::Isolate* isolate = args.GetIsolate();
294 v8::HandleScope handleScope( isolate );
295 Font font = GetFont( isolate, args );
297 //float MeasureTextHeight(const std::string& text, float textHeightPx) const;
300 std::string text = V8Utils::GetStringParameter( PARAMETER_0, found, isolate, args );
303 DALI_SCRIPT_EXCEPTION( isolate, "text not found" );
306 int width= V8Utils::GetIntegerParameter( PARAMETER_1, found, isolate, args, 0 );
309 DALI_SCRIPT_EXCEPTION( isolate, "missing text height" );
312 float height = font.MeasureTextHeight( text, width );
314 args.GetReturnValue().Set( v8::Integer::New( isolate, height ) );
316 void FontApi::MeasureText( const v8::FunctionCallbackInfo< v8::Value >& args )
318 v8::Isolate* isolate = args.GetIsolate();
319 v8::HandleScope handleScope( isolate );
320 Font font = GetFont( isolate, args );
323 std::string text = V8Utils::GetStringParameter( PARAMETER_0, found, isolate, args );
326 DALI_SCRIPT_EXCEPTION( isolate, "text not found" );
330 Vector3 vec3 = font.MeasureText( text );
331 Dali::Property::Value value( vec3 );
332 v8::Local <v8::Object> object = PropertyValueWrapper::WrapDaliProperty( isolate, value);
333 args.GetReturnValue().Set( object );
336 void FontApi::AllGlyphsSupported( const v8::FunctionCallbackInfo< v8::Value >& args )
338 v8::Isolate* isolate = args.GetIsolate();
339 v8::HandleScope handleScope( isolate );
340 Font font = GetFont( isolate, args );
343 std::string text = V8Utils::GetStringParameter( PARAMETER_0, found, isolate, args );
346 DALI_SCRIPT_EXCEPTION( isolate, "text not found" );
350 bool supported = font.AllGlyphsSupported( text );
351 args.GetReturnValue().Set( v8::Boolean::New( isolate, supported) );
355 // This one function is use to create a property map, instead of the many individual property
356 // getters used by the C++ Dali API.
365 // underlineThickness:
366 // underlinePosition:
367 // isDefaultSystemFont:
368 // isDefaultSystemSize:
371 void FontApi::GetProperties( const v8::FunctionCallbackInfo< v8::Value >& args )
373 v8::Isolate* isolate = args.GetIsolate();
374 v8::HandleScope handleScope( isolate );
375 Font font = GetFont( isolate, args );
377 v8::Local<v8::Object> keyObject = v8::Object::New( isolate );
379 keyObject->Set( v8::String::NewFromUtf8( isolate, "family" ), v8::String::NewFromUtf8( isolate, font.GetName().c_str() ) );
380 keyObject->Set( v8::String::NewFromUtf8( isolate, "style" ), v8::String::NewFromUtf8( isolate, font.GetStyle().c_str() ) );
381 keyObject->Set( v8::String::NewFromUtf8( isolate, "pointSize" ), v8::Integer::New( isolate, font.GetPointSize() ) );
382 keyObject->Set( v8::String::NewFromUtf8( isolate, "pixelSize" ), v8::Integer::New( isolate, font.GetPixelSize() ) );
383 keyObject->Set( v8::String::NewFromUtf8( isolate, "lineHeight" ), v8::Integer::New( isolate, font.GetLineHeight() ) );
384 keyObject->Set( v8::String::NewFromUtf8( isolate, "ascender" ), v8::Integer::New( isolate, font.GetAscender() ) );
385 keyObject->Set( v8::String::NewFromUtf8( isolate, "underlineThickness" ), v8::Integer::New( isolate, font.GetUnderlineThickness() ) );
386 keyObject->Set( v8::String::NewFromUtf8( isolate, "underlinePosition" ), v8::Integer::New( isolate, font.GetUnderlinePosition()) );
387 keyObject->Set( v8::String::NewFromUtf8( isolate, "isDefaultSystemFont" ), v8::Boolean::New( isolate, font.IsDefaultSystemFont() ) );
388 keyObject->Set( v8::String::NewFromUtf8( isolate, "isDefaultSystemSize" ), v8::Boolean::New( isolate, font.IsDefaultSystemSize() ) );
390 args.GetReturnValue().Set( keyObject );
394 void FontApi::GetMetrics( const v8::FunctionCallbackInfo< v8::Value >& args )
396 v8::Isolate* isolate = args.GetIsolate();
397 v8::HandleScope handleScope( isolate );
398 Font font = GetFont( isolate, args );
401 std::string text = V8Utils::GetStringParameter( PARAMETER_0, found, isolate, args );
405 DALI_SCRIPT_EXCEPTION( isolate, "character not found" );
409 Text textObject( text );
411 Font::Metrics metric = font.GetMetrics( textObject[0] );
413 v8::Local<v8::Object> keyObject = v8::Object::New( isolate );
414 keyObject->Set( v8::String::NewFromUtf8( isolate, "advance" ), v8::Integer::New( isolate, metric.GetAdvance() ) );
415 keyObject->Set( v8::String::NewFromUtf8( isolate, "bearing" ), v8::Integer::New( isolate, metric.GetBearing() ) );
416 keyObject->Set( v8::String::NewFromUtf8( isolate, "width" ), v8::Integer::New( isolate, metric.GetWidth() ) );
417 keyObject->Set( v8::String::NewFromUtf8( isolate, "height" ), v8::Integer::New( isolate, metric.GetHeight() ) );
419 args.GetReturnValue().Set( keyObject );
424 void FontApi::PointsToPixels( const v8::FunctionCallbackInfo< v8::Value >& args )
426 v8::Isolate* isolate = args.GetIsolate();
427 v8::HandleScope handleScope( isolate );
430 int pointSize= V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 );
433 DALI_SCRIPT_EXCEPTION( isolate, "missing pointSize" );
436 args.GetReturnValue().Set( v8::Integer::New( isolate, Font::PointsToPixels(pointSize) ) );
439 void FontApi::PixelsToPoints( const v8::FunctionCallbackInfo< v8::Value >& args )
441 v8::Isolate* isolate = args.GetIsolate();
442 v8::HandleScope handleScope( isolate );
445 int pixelSize= V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 );
448 DALI_SCRIPT_EXCEPTION( isolate, "missing pixelSize" );
451 args.GetReturnValue().Set( v8::Integer::New( isolate, Font::PixelsToPoints(pixelSize) ) );
456 } // namespace V8Plugin