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 "nine-patch-image-api.h"
23 #include <image/image-wrapper.h>
33 * NinePatchImage represents an image resource that can be added to ImageActors.
34 * It contains a bitmap that is synchronously loaded from the file system that contains
35 * a 9 patch border - a 1 pixel border that describes the stretch borders and the child
38 * The class offers an API to read the stretch area and child area, but it does not
39 * remove the border from it's bitmap. An API can be used to obtain a BufferImage with
42 * Adding this image to an ImageActor using an Image handle will automatically convert
43 * to use the cropped BufferImage - if you don't retain a handle to this object, it will
44 * be automatically destroyed.
45 * @class NinePatchImage
46 * @extends ResourceImage
49 NinePatchImage NinePatchImageApi::GetNinePatchImage( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
51 v8::HandleScope handleScope( isolate );
53 v8::Local<v8::Object> object = args.This();
54 v8::Local<v8::External> field = v8::Local<v8::External>::Cast( object->GetInternalField(0) );
55 void* ptr = field->Value();
57 ImageWrapper* wrapper = static_cast< ImageWrapper *>(ptr);
58 return NinePatchImage ::DownCast( wrapper->GetImage() );
63 * Create a new nine patch image object.
66 * @method NinePatchImage
68 * @param {Object} options
69 * @param {String} options.url The URL of the nine patch image file to use.
70 * @return {Object} Image
72 Image NinePatchImageApi::New( const v8::FunctionCallbackInfo< v8::Value >& args )
74 v8::Isolate* isolate = args.GetIsolate();
75 v8::HandleScope handleScope( isolate );
78 v8::Local<v8::Value> options( args[0] );
80 if( !options->IsObject() )
82 DALI_SCRIPT_EXCEPTION( isolate, "Missing params");
83 return NinePatchImage();
86 v8::Local<v8::Object> optionsObject = options->ToObject();
88 v8::Local<v8::Value> urlValue = optionsObject->Get( v8::String::NewFromUtf8( isolate, "url" ) );
89 if( urlValue->IsString() )
91 url = V8Utils::v8StringToStdString( urlValue );
95 DALI_SCRIPT_EXCEPTION( isolate, "Missing url");
96 return NinePatchImage();
99 return NinePatchImage::New( url );
103 * Get the child rectangle
104 * @method getChildRectangle
105 * @for NinePatchImage
106 * @return {Object} position/size of of the child rectangle with x,y,w,h properties
109 void NinePatchImageApi::GetChildRectangle( const v8::FunctionCallbackInfo< v8::Value >& args )
111 v8::Isolate* isolate = args.GetIsolate();
112 v8::HandleScope handleScope( isolate );
114 NinePatchImage image = GetNinePatchImage( isolate, args );
116 v8::Local<v8::Object> rectObject = v8::Object::New( isolate );
117 Rect<int> childRect = image.GetChildRectangle();
119 rectObject->Set( v8::String::NewFromUtf8( isolate, "x" ), v8::Integer::New( isolate,childRect.x ) );
120 rectObject->Set( v8::String::NewFromUtf8( isolate, "y" ), v8::Integer::New( isolate,childRect.y ) );
121 rectObject->Set( v8::String::NewFromUtf8( isolate, "w" ), v8::Integer::New( isolate,childRect.width ) );
122 rectObject->Set( v8::String::NewFromUtf8( isolate, "h" ), v8::Integer::New( isolate,childRect.height ) );
124 args.GetReturnValue().Set( rectObject );
128 * Creates a buffer image from the bitmap with the 1 pixel border cropped off.
129 * This does not change the internal bitmap.
130 * @method createCroppedBufferImage
131 * @for NinePatchImage
133 void NinePatchImageApi::CreateCroppedBufferImage( const v8::FunctionCallbackInfo< v8::Value >& args )
135 v8::Isolate* isolate = args.GetIsolate();
136 v8::HandleScope handleScope( isolate );
138 Image image = GetNinePatchImage( isolate, args );
140 args.GetReturnValue().Set( v8::Integer::New( isolate, image.GetWidth() ) );
144 } // namespace V8Plugin