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.
18 #include "emscripten-callbacks.h"
22 #include <dali/public-api/dali-core.h>
23 #include "emscripten/emscripten.h"
24 #include "emscripten/bind.h"
25 #include "emscripten/val.h"
38 // Javascript callbacks
40 // Allows the adaptor to get a glyph image from the browser
41 emscripten::val JSGetGlyphImage(emscripten::val::null());
43 // Allows the adaptor to request an image from the browser
44 emscripten::val JSGetImage(emscripten::val::null());
46 // Allows the adaptor to get image meta data
47 emscripten::val JSGetImageMetaData(emscripten::val::null());
49 // Signals to the browser the end of rendering
50 emscripten::val JSRenderFinished(emscripten::val::null());
53 Integration::BitmapPtr GetGlyphImage( const std::string& fontFamily, const std::string& fontStyle, float fontSize, uint32_t character )
55 Integration::BitmapPtr ret;
57 // causes exception in browser if callback isnt set to a function;
58 emscripten::val val = JSGetGlyphImage(fontFamily,
63 std::vector<unsigned char> data = emscripten::vecFromJSArray<unsigned char>(val);
65 int step = fontSize * 4;
69 Integration::Bitmap* bitmap = Integration::Bitmap::New(Integration::Bitmap::BITMAP_2D_PACKED_PIXELS,
70 ResourcePolicy::OWNED_DISCARD); // dali manages buffer
74 Integration::Bitmap::PackedPixelsProfile* profile = bitmap->GetPackedPixelsProfile();
78 std::vector<unsigned char> *buffer = new std::vector<unsigned char>;
80 buffer->reserve( fontSize * fontSize );
83 for(int y = 0; y < fontSize; ++y)
85 for(int x = 0; x < step; x+=4)
87 buffer->push_back(data[ x + (y*step) +3]);
93 profile->AssignBuffer(Pixel::A8,
100 ret = Integration::BitmapPtr( bitmap );
104 printf("bitmap has no packedpixelsprofile\n");
109 printf("bitmap not created\n");
114 printf("Image data from javascript is empty\n");
121 Integration::BitmapPtr GetImage( const Dali::ImageDimensions& size,
122 const Dali::FittingMode::Type& scalingMode,
123 const Dali::SamplingMode::Type& samplingMode,
124 const bool orientationCorrection,
125 const std::string& filename )
127 Integration::BitmapPtr ret;
129 // causes exception in browser if callback isnt set to a function;
130 emscripten::val val = JSGetImage(filename);
132 emscripten::val array = val["array"];
133 int w = val["x"].as<int>();
134 int h = val["y"].as<int>();
136 std::vector<unsigned char> data = emscripten::vecFromJSArray<unsigned char>(array);
138 Integration::Bitmap* bitmap = Integration::Bitmap::New(Integration::Bitmap::BITMAP_2D_PACKED_PIXELS,
139 Dali::ResourcePolicy::OWNED_DISCARD); // dali manages buffer
143 Integration::Bitmap::PackedPixelsProfile* profile = bitmap->GetPackedPixelsProfile();
147 std::vector<unsigned char> *buffer = new std::vector<unsigned char>(data.begin(), data.end());
151 profile->AssignBuffer(Pixel::RGBA8888,
158 ret = Integration::BitmapPtr( bitmap );
162 printf("bitmap has no packedpixelsprofile\n");
167 printf("bitmap not created\n");
173 Dali::ImageDimensions LoadImageMetadata(const std::string filename,
174 Dali::ImageDimensions& size,
175 Dali::FittingMode::Type fittingMode,
176 Dali::SamplingMode::Type samplingMode,
177 bool orientationCorrection )
179 emscripten::val val = JSGetImageMetaData(filename);
185 return Dali::ImageDimensions();
188 void RenderFinished()
190 if (JSRenderFinished.typeof().as<std::string>() == "function")
192 emscripten::val val = JSRenderFinished();
197 }; // namespace Emscripten
198 }; // namespace Internal