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 );
104 * Get the Stretch Borders
106 * @method getStretchBorders
107 * @for NinePatchImage
108 * @return object containing x,y,w,h properties
110 void NinePatchImageApi::GetStretchBorders( const v8::FunctionCallbackInfo< v8::Value >& args )
112 v8::Isolate* isolate = args.GetIsolate();
113 v8::HandleScope handleScope( isolate );
115 NinePatchImage image = GetNinePatchImage( isolate, args );
117 v8::Local<v8::Object> rectObject = v8::Object::New( isolate );
119 Vector4 borders = image.GetStretchBorders();
122 rectObject->Set( v8::String::NewFromUtf8( isolate, "x" ), v8::Integer::New( isolate,borders.x ) );
123 rectObject->Set( v8::String::NewFromUtf8( isolate, "y" ), v8::Integer::New( isolate,borders.y ) );
124 rectObject->Set( v8::String::NewFromUtf8( isolate, "w" ), v8::Integer::New( isolate,borders.z ) );
125 rectObject->Set( v8::String::NewFromUtf8( isolate, "h" ), v8::Integer::New( isolate,borders.w ) );
127 args.GetReturnValue().Set( rectObject );
131 * Get the child rectangle
132 * @method getChildRectangle
133 * @for NinePatchImage
134 * @return {Object} position/size of of the child rectangle with x,y,w,h properties
137 void NinePatchImageApi::GetChildRectangle( const v8::FunctionCallbackInfo< v8::Value >& args )
139 v8::Isolate* isolate = args.GetIsolate();
140 v8::HandleScope handleScope( isolate );
142 NinePatchImage image = GetNinePatchImage( isolate, args );
144 v8::Local<v8::Object> rectObject = v8::Object::New( isolate );
145 Rect<int> childRect = image.GetChildRectangle();
147 rectObject->Set( v8::String::NewFromUtf8( isolate, "x" ), v8::Integer::New( isolate,childRect.x ) );
148 rectObject->Set( v8::String::NewFromUtf8( isolate, "y" ), v8::Integer::New( isolate,childRect.y ) );
149 rectObject->Set( v8::String::NewFromUtf8( isolate, "w" ), v8::Integer::New( isolate,childRect.width ) );
150 rectObject->Set( v8::String::NewFromUtf8( isolate, "h" ), v8::Integer::New( isolate,childRect.height ) );
152 args.GetReturnValue().Set( rectObject );
156 * Creates a buffer image from the bitmap with the 1 pixel border cropped off.
157 * This does not change the internal bitmap.
158 * @method createCroppedBufferImage
159 * @for NinePatchImage
161 void NinePatchImageApi::CreateCroppedBufferImage( const v8::FunctionCallbackInfo< v8::Value >& args )
163 v8::Isolate* isolate = args.GetIsolate();
164 v8::HandleScope handleScope( isolate );
166 Image image = GetNinePatchImage( isolate, args );
168 args.GetReturnValue().Set( v8::Integer::New( isolate, image.GetWidth() ) );
172 } // namespace V8Plugin