mirror of
https://github.com/ONLYOFFICE/core.git
synced 2026-02-10 18:05:41 +08:00
Add new method for images inside zip
This commit is contained in:
@ -258,3 +258,56 @@ JSSmart<CJSValue> CZipEmbed::getImageType(JSSmart<CJSValue> typedArray)
|
||||
oBuffer.Free();
|
||||
return CJSContext::createInt(bIsImageFile ? oChecker.eFileType : 0);
|
||||
}
|
||||
|
||||
JSSmart<CJSValue> CZipEmbed::getImageBuffer(JSSmart<CJSValue> filePath)
|
||||
{
|
||||
if (!m_pFolder || !filePath->isString())
|
||||
return CJSContext::createNull();
|
||||
|
||||
std::wstring sFilePath = filePath->toStringW();
|
||||
IFolder::CBuffer* pBuffer;
|
||||
if (!m_pFolder->read(sFilePath, pBuffer))
|
||||
return CJSContext::createNull();
|
||||
|
||||
size_t nBufferSize = (size_t)pBuffer->Size;
|
||||
|
||||
CImageFileFormatChecker oChecker;
|
||||
bool bIsImageFile = oChecker.isImageFile(pBuffer->Buffer, (DWORD)pBuffer->Size);
|
||||
|
||||
if (!bIsImageFile)
|
||||
{
|
||||
RELEASEOBJECT(pBuffer);
|
||||
return CJSContext::createNull();
|
||||
}
|
||||
|
||||
if (oChecker.eFileType != _CXIMAGE_FORMAT_WMF && oChecker.eFileType != _CXIMAGE_FORMAT_EMF)
|
||||
{
|
||||
BYTE* pMemory = NSJSBase::NSAllocator::Alloc(nBufferSize);
|
||||
memcpy(pMemory, pBuffer->Buffer, nBufferSize);
|
||||
RELEASEOBJECT(pBuffer);
|
||||
|
||||
JSSmart<CJSObject> retObject = CJSContext::createObject();
|
||||
retObject->set("type", CJSContext::createInt(oChecker.eFileType));
|
||||
retObject->set("data", NSJSBase::CJSContext::createUint8Array(pMemory, (int)nBufferSize, false));
|
||||
return retObject->toValue();
|
||||
}
|
||||
|
||||
#ifndef GRAPHICS_DISABLE_METAFILE
|
||||
MetaFile::IMetaFile* pMetaFile = MetaFile::Create(NULL);
|
||||
pMetaFile->LoadFromBuffer(pBuffer->Buffer, (unsigned int)pBuffer->Size);
|
||||
std::wstring wsSvg = pMetaFile->ConvertToSvg();
|
||||
std::string sSvg = U_TO_UTF8(wsSvg);
|
||||
pMetaFile->Release();
|
||||
RELEASEOBJECT(pBuffer);
|
||||
|
||||
BYTE* pData = NSAllocator::Alloc(sSvg.length());
|
||||
memcpy(pData, sSvg.c_str(), sSvg.length());
|
||||
|
||||
JSSmart<CJSObject> retObject = CJSContext::createObject();
|
||||
retObject->set("type", CJSContext::createInt(24));
|
||||
retObject->set("data", NSJSBase::CJSContext::createUint8Array(pData, sSvg.length(), false));
|
||||
return retObject->toValue();
|
||||
#else
|
||||
return CJSContext::createNull();
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -36,6 +36,7 @@ public:
|
||||
JSSmart<CJSValue> encodeImageData(JSSmart<CJSValue> typedArray, JSSmart<CJSValue> w, JSSmart<CJSValue> h, JSSmart<CJSValue> stride, JSSmart<CJSValue> format, JSSmart<CJSValue> isRgba);
|
||||
JSSmart<CJSValue> encodeImage(JSSmart<CJSValue> typedArray, JSSmart<CJSValue> format);
|
||||
JSSmart<CJSValue> getImageType(JSSmart<CJSValue> typedArray);
|
||||
JSSmart<CJSValue> getImageBuffer(JSSmart<CJSValue> path);
|
||||
|
||||
DECLARE_EMBED_METHODS
|
||||
};
|
||||
|
||||
@ -17,6 +17,7 @@
|
||||
-(JSValue*) encodeImageData : (JSValue*)typedArray : (JSValue*)w : (JSValue*)h : (JSValue*)stride : (JSValue*)format : (JSValue*)isRgba;
|
||||
-(JSValue*) encodeImage : (JSValue*)typedArray : (JSValue*)format;
|
||||
-(JSValue*) getImageType : (JSValue*)typedArray;
|
||||
-(JSValue*) getImageBuffer : (JSValue*)path;
|
||||
@end
|
||||
|
||||
@interface CJSCZipEmbed : NSObject<IJSCZipEmbed, JSEmbedObjectProtocol>
|
||||
@ -41,6 +42,7 @@ FUNCTION_WRAPPER_JS_2(decodeImage, decodeImage)
|
||||
FUNCTION_WRAPPER_JS_6(encodeImageData, encodeImageData)
|
||||
FUNCTION_WRAPPER_JS_2(encodeImage, encodeImage)
|
||||
FUNCTION_WRAPPER_JS_1(getImageType, getImageType)
|
||||
FUNCTION_WRAPPER_JS_1(getImageBuffer, getImageBuffer)
|
||||
@end
|
||||
|
||||
class CZipEmbedAdapter : public CJSEmbedObjectAdapterJSC
|
||||
|
||||
@ -20,6 +20,7 @@ namespace NSZipEmbed
|
||||
FUNCTION_WRAPPER_V8_6(_encodeImageData, encodeImageData)
|
||||
FUNCTION_WRAPPER_V8_2(_encodeImage, encodeImage)
|
||||
FUNCTION_WRAPPER_V8_1(_getImageType, getImageType)
|
||||
FUNCTION_WRAPPER_V8_1(_getImageBuffer, getImageBuffer)
|
||||
|
||||
v8::Handle<v8::ObjectTemplate> CreateTemplate(v8::Isolate* isolate)
|
||||
{
|
||||
@ -39,6 +40,7 @@ namespace NSZipEmbed
|
||||
NSV8Objects::Template_Set(result, "encodeImageData", _encodeImageData);
|
||||
NSV8Objects::Template_Set(result, "encodeImage", _encodeImage);
|
||||
NSV8Objects::Template_Set(result, "getImageType", _getImageType);
|
||||
NSV8Objects::Template_Set(result, "getImageBuffer", _getImageBuffer);
|
||||
|
||||
return handle_scope.Escape(result);
|
||||
}
|
||||
|
||||
@ -531,6 +531,40 @@ ZLib.prototype.getImageAsSvg = function(path)
|
||||
|
||||
return string;
|
||||
};
|
||||
/**
|
||||
* Get image file raw data. this memory was copied and detach from archive.
|
||||
* @returns {Uint8Array}
|
||||
*/
|
||||
ZLib.prototype.getImageBuffer = function(path)
|
||||
{
|
||||
let result = {
|
||||
type : 0,
|
||||
data : null
|
||||
};
|
||||
result.type = this.getImageType(path);
|
||||
if (result.type != 10 &&
|
||||
result.type != 21)
|
||||
{
|
||||
let fileData = this.getFile(path);
|
||||
result.data = new Uint8Array(fileData.length);
|
||||
result.data.set(fileData);
|
||||
return result;
|
||||
}
|
||||
|
||||
result.type = 24;
|
||||
|
||||
let fileData = this.getFile(path);
|
||||
let encodedData = Module["_Raster_Encode"](this.files[path].p + 4, fileData.length, 24);
|
||||
let encodedSize = Module["_Raster_GetEncodedSize"](encodedData);
|
||||
let encodedBuffer = Module["_Raster_GetEncodedBuffer"](encodedData);
|
||||
|
||||
let fileDataEnc = new Uint8Array(Module["HEAP8"].buffer, encodedBuffer, encodedSize);
|
||||
result.data = new Uint8Array(fileDataEnc.length);
|
||||
result.data.set(fileDataEnc);
|
||||
|
||||
Module["_Raster_DestroyEncodedData"](encodedData);
|
||||
return result;
|
||||
};
|
||||
/**
|
||||
* Get image blob for browser
|
||||
* @returns {Blob}
|
||||
|
||||
Reference in New Issue
Block a user