void PythonModule::Initialize() { PythonModule::instance_ = this; Py_Initialize(); PyEval_InitThreads(); PyEval_SaveThread(); { PyLockGIL lock; PyObject* path = PySys_GetObject((char*) "path"); PyListInsertString(path, 0, UTF8ToSystem(host->GetApplication()->GetResourcesPath()).c_str()); #ifdef OS_WIN32 PyListInsertString(path, 0, FileUtils::Join( UTF8ToSystem(this->GetPath()).c_str(), "DLLs", NULL)); PyListInsertString(path, 0, FileUtils::Join( UTF8ToSystem(this->GetPath()).c_str(), "Lib", NULL)); PyListInsertString(path, 0, FileUtils::Join( UTF8ToSystem(this->GetPath()).c_str(), "Lib", "lib-tk", NULL)); #endif } host->script()->AddInterpreter(&interpreter, supportedScriptTypes); PythonUtils::InitializePythonKClasses(); host->AddModuleProvider(this); }
/*static*/ KValueRef Codec::ExtractZipAsync(const ValueList& args) { std::string zipFile = args.GetString(0); std::string directory = args.GetString(1); AutoPtr<AsyncJob> job = args.GetObject(2).cast<AsyncJob>(); KMethodRef callback = 0; if (args.size() > 3) { callback = args.GetMethod(3); } std::ifstream stream(UTF8ToSystem(zipFile).c_str(), std::ios::binary); Poco::Zip::Decompress decompressor(stream, directory); try { decompressor.decompressAllFiles(); } catch (std::exception& e) { Logger::Get("Codec")->Error("exception decompressing: %s", e.what()); throw ValueException::FromFormat("Exception during extraction: %s", e.what()); } stream.close(); if (!callback.isNull()) { ValueList args; args.push_back(Value::NewString(directory)); RunOnMainThread(callback, args, true); } return Value::Undefined; }
/*static*/ HBITMAP Win32UIBinding::LoadPNGAsBitmap(std::string& path, int sizeX, int sizeY) { std::string systemPath(UTF8ToSystem(path)); cairo_surface_t* pngSurface = cairo_image_surface_create_from_png(systemPath.c_str()); cairo_t* pngcr = cairo_create(pngSurface); if (cairo_status(pngcr) != CAIRO_STATUS_SUCCESS) return 0; BITMAPINFO bitmapInfo; memset(&bitmapInfo, 0, sizeof(bitmapInfo)); bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bitmapInfo.bmiHeader.biWidth = sizeX; bitmapInfo.bmiHeader.biHeight = -sizeY; // Bottom-up bitmapInfo.bmiHeader.biPlanes = 1; bitmapInfo.bmiHeader.biBitCount = 32; bitmapInfo.bmiHeader.biCompression = BI_RGB; bitmapInfo.bmiHeader.biSizeImage = 0; bitmapInfo.bmiHeader.biXPelsPerMeter = 1000; bitmapInfo.bmiHeader.biYPelsPerMeter = bitmapInfo.bmiHeader.biXPelsPerMeter; bitmapInfo.bmiHeader.biClrUsed = 0; bitmapInfo.bmiHeader.biClrImportant = 0; void* pixels = NULL; HDC hdc = ::GetDC(NULL); HBITMAP out = CreateDIBSection(hdc, &bitmapInfo, DIB_RGB_COLORS, &pixels, NULL, 0); ::ReleaseDC(NULL, hdc); BITMAP info; ::GetObjectW(out, sizeof(info), &info); cairo_surface_t* outSurface = cairo_image_surface_create_for_data( (unsigned char*) pixels, CAIRO_FORMAT_ARGB32, sizeX, sizeY, info.bmWidthBytes); cairo_surface_t* scaledSurface = ScaleCairoSurface(pngSurface, sizeX, sizeY); cairo_t *cr = cairo_create(outSurface); cairo_save(cr); cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR); cairo_paint(cr); cairo_restore(cr); cairo_set_source_surface(cr, scaledSurface, 0, 0); cairo_rectangle(cr, 0, 0, sizeX, sizeY); cairo_fill(cr); cairo_destroy(cr); cairo_surface_destroy(outSurface); cairo_surface_destroy(scaledSurface); cairo_destroy(pngcr); cairo_surface_destroy(pngSurface); return out; }
Module* PythonModule::CreateModule(std::string& path) { PyLockGIL lock; path = UTF8ToSystem(path); FILE* file = fopen(path.c_str(), "r"); PyRun_SimpleFileEx(file, path.c_str(), 1); Poco::Path p(path); std::string basename = p.getBaseName(); std::string name = basename.substr(0,basename.length()-python_suffix.length()+3); std::string moduledir = path.substr(0,path.length()-basename.length()-3); return new PythonModuleInstance(host, path, moduledir, name); }
Module* RubyModule::CreateModule(std::string& path) { path = UTF8ToSystem(path); rb_load_file(path.c_str()); ruby_exec(); // TODO: Do we need to call ruby_cleanup() here? Poco::Path p(path); std::string basename = p.getBaseName(); std::string name = basename.substr(0,basename.length()-ruby_suffix.length()+3); std::string moduledir = path.substr(0,path.length()-basename.length()-3); return new RubyModuleInstance(host, path, moduledir, name); }
void RubyModule::Initialize() { RubyModule::instance_ = this; ruby_init(); ruby_init_loadpath(); // Add the application directoy to the Ruby include path so // that includes work in a intuitive way for application developers. ruby_incpush(UTF8ToSystem(host->GetApplication()->GetResourcesPath()).c_str()); host->script()->AddInterpreter(&interpreter, supportedScriptTypes); host->AddModuleProvider(this); }
/*static*/ KValueRef Codec::CreateZipAsync(const ValueList& args) { std::string directory = args.GetString(0); std::string zipFile = args.GetString(1); AutoPtr<AsyncJob> job = args.GetObject(2).cast<AsyncJob>(); KMethodRef callback = 0; if (args.size() > 3) { callback = args.GetMethod(3); } Poco::Path path(directory); path.makeDirectory(); std::ofstream stream(UTF8ToSystem(zipFile).c_str(), std::ios::binary | std::ios::trunc); Poco::Zip::Compress compressor(stream, true); try { compressor.addRecursive(path); } catch (std::exception& e) { Logger::Get("Codec")->Error("exception compressing: %s", e.what()); throw ValueException::FromFormat("Exception during zip: %s", e.what()); } compressor.close(); stream.close(); if (!callback.isNull()) { ValueList args; args.push_back(Value::NewString(zipFile)); RunOnMainThread(callback, args, true); } return Value::Undefined; }