Realize pdf split/merge in native js wrapper

This commit is contained in:
Oleg Korshul
2025-05-15 16:04:29 +03:00
parent d70c26f5ce
commit e10aa90618
7 changed files with 121 additions and 30 deletions

View File

@ -166,6 +166,48 @@ JSSmart<CJSValue> CDrawingFileEmbed::FreeWasmData(JSSmart<CJSValue> typedArray)
return NULL;
}
JSSmart<CJSValue> CDrawingFileEmbed::SplitPages(JSSmart<CJSValue> arrPageIndexes, JSSmart<CJSValue> data)
{
JSSmart<CJSArray> arrPages = arrPageIndexes->toArray();
CJSDataBuffer changes;
if (data->isTypedArray())
changes = data->toTypedArray()->getData();
int nCountPages = arrPages->getCount();
int* pPages = NULL;
if (0 < nCountPages)
pPages = new int[nCountPages];
for (int i = 0; i < nCountPages; i++)
pPages[i] = arrPages->get(i)->toInt32();
JSSmart<CJSValue> res = WasmMemoryToJS(m_pFile->SplitPages(pPages, nCountPages, changes.Data, (LONG)changes.Len));
if (pPages)
delete [] pPages;
return res;
}
JSSmart<CJSValue> CDrawingFileEmbed::MergePages(JSSmart<CJSValue> data, JSSmart<CJSValue> nMaxID, JSSmart<CJSValue> sPrefixForm)
{
bool result = false;
if (m_pFile)
{
JSSmart<CJSTypedArray> dataPtr = data->toTypedArray();
int maxID = nMaxID->toInt32();
std::string prefix = sPrefixForm->toStringA();
CJSDataBuffer buffer = dataPtr->getData();
result = m_pFile->MergePages(buffer.Data, (LONG)buffer.Len, maxID, prefix);
if (buffer.IsExternalize)
buffer.Free();
}
return CJSContext::createBool(result);
}
JSSmart<CJSValue> CDrawingFileEmbed::UnmergePages()
{
if (m_pFile)
return CJSContext::createBool(m_pFile->UnmergePages());
return CJSContext::createBool(false);
}
bool EmbedDrawingFile(JSSmart<NSJSBase::CJSContext>& context, IOfficeDrawingFile* pFile)
{
CJSContext::Embed<CDrawingFileEmbed>(false);

View File

@ -51,6 +51,10 @@ public:
JSSmart<CJSValue> FreeWasmData(JSSmart<CJSValue> typedArray);
JSSmart<CJSValue> SplitPages(JSSmart<CJSValue> arrPageIndexes, JSSmart<CJSValue> data);
JSSmart<CJSValue> MergePages(JSSmart<CJSValue> data, JSSmart<CJSValue> nMaxID, JSSmart<CJSValue> sPrefixForm);
JSSmart<CJSValue> UnmergePages();
DECLARE_EMBED_METHODS
};

View File

@ -27,6 +27,9 @@
-(JSValue*) ScanPage : (JSValue*)nPageIndex : (JSValue*)mode;
-(JSValue*) GetImageBase64 : (JSValue*)rId;
-(JSValue*) FreeWasmData : (JSValue*)typedArray;
-(JSValue*) SplitPages : (JSValue*)arrPageIndexes : (JSValue*)data;
-(JSValue*) MergePages : (JSValue*)data : (JSValue*)nMaxID : (JSValue*)sPrefixForm;
-(JSValue*) UnmergePages;
@end
@interface CJSCDrawingFileEmbed : NSObject<IJSCDrawingFileEmbed, JSEmbedObjectProtocol>
@ -61,6 +64,9 @@ FUNCTION_WRAPPER_JS_0(IsNeedCMap, IsNeedCMap)
FUNCTION_WRAPPER_JS_2(ScanPage, ScanPage)
FUNCTION_WRAPPER_JS_1(GetImageBase64, GetImageBase64)
FUNCTION_WRAPPER_JS_1(FreeWasmData, FreeWasmData)
FUNCTION_WRAPPER_JS_2(SplitPages, SplitPages)
FUNCTION_WRAPPER_JS_3(MergePages, MergePages)
FUNCTION_WRAPPER_JS_0(UnmergePages, UnmergePages)
@end
class CDrawingFileEmbedAdapter : public CJSEmbedObjectAdapterJSC

View File

@ -30,6 +30,9 @@ namespace NSDrawingFileEmbed
FUNCTION_WRAPPER_V8_2(_ScanPage, ScanPage)
FUNCTION_WRAPPER_V8_1(_GetImageBase64, GetImageBase64)
FUNCTION_WRAPPER_V8_1(_FreeWasmData, FreeWasmData)
FUNCTION_WRAPPER_V8_2(_SplitPages, SplitPages)
FUNCTION_WRAPPER_V8_3(_MergePages, MergePages)
FUNCTION_WRAPPER_V8_0(_UnmergePages, UnmergePages)
v8::Handle<v8::ObjectTemplate> CreateTemplate(v8::Isolate* isolate)
{
@ -59,6 +62,9 @@ namespace NSDrawingFileEmbed
NSV8Objects::Template_Set(result, "ScanPage", _ScanPage);
NSV8Objects::Template_Set(result, "GetImageBase64", _GetImageBase64);
NSV8Objects::Template_Set(result, "FreeWasmData", _FreeWasmData);
NSV8Objects::Template_Set(result, "SplitPages", _SplitPages);
NSV8Objects::Template_Set(result, "MergePages", _MergePages);
NSV8Objects::Template_Set(result, "UnmergePages", _UnmergePages);
return handle_scope.Escape(result);
}

View File

@ -148,29 +148,9 @@ CFile.prototype["isNeedPassword"] = function()
CFile.prototype["SplitPages"] = function(arrPageIndex, arrayBufferChanges)
{
let ptr = this._SplitPages(arrPageIndex, arrayBufferChanges);
if (!ptr)
return null;
let lenArr = new Int32Array(Module["HEAP8"].buffer, ptr, 1);
if (!lenArr)
{
Module["_free"](ptr);
return null;
}
let len = lenArr[0];
if (len <= 4)
{
Module["_free"](ptr);
return null;
}
len -= 4;
let buffer = new Uint8Array(len);
buffer.set(new Uint8Array(Module["HEAP8"].buffer, ptr + 4, len));
Module["_free"](ptr);
return buffer;
let res = ptr.getMemory(true);
ptr.free();
return res;
};
CFile.prototype["MergePages"] = function(arrayBuffer, maxID, prefixForm)
{

View File

@ -42,6 +42,19 @@ CNativePointer.prototype.free = function()
g_native_drawing_file["FreeWasmData"](this.ptr);
this.ptr = null;
};
CNativePointer.prototype.getMemory = function(isCopy)
{
if (!this.ptr)
return null;
if (!isCopy)
return this.ptr;
let copyArray = new Uint8Array(this.ptr.length);
copyArray.set(this.ptr);
return copyArray;
};
CNativePointer.prototype.getReader = function()
{
if (!this.ptr)
@ -94,10 +107,33 @@ CFile.prototype._getError = function()
return g_native_drawing_file["GetErrorCode"]();
};
CFile.prototype._addPDF = function(buffer, password)
CFile.prototype._SplitPages = function(pages, changes)
{
return g_native_drawing_file["AddPDF"]();
}
let dataChanges = null;
if (changes)
dataChanges = (undefined !== changes.byteLength) ? new Uint8Array(changes) : changes;
g_module_pointer.ptr = g_native_drawing_file["SplitPages"](pages, dataChanges);
return g_module_pointer;
};
CFile.prototype._MergePages = function(buffer, maxID, prefixForm)
{
if (!buffer)
return false;
if (!maxID)
maxID = 0;
if (!prefixForm)
prefixForm = "";
let data = (undefined !== buffer.byteLength) ? new Uint8Array(buffer) : buffer;
return g_native_drawing_file["MergePages"](data, maxID, prefixForm);
};
CFile.prototype._UndoMergePages = function()
{
return g_native_drawing_file["UnmergePages"]();
};
// FONTS
CFile.prototype._isNeedCMap = function()

View File

@ -39,7 +39,7 @@ CWasmPointer.prototype.free = function()
Module["_free"](this.ptr);
this.ptr = 0;
};
CWasmPointer.prototype.getReader = function()
CWasmPointer.prototype.getMemory = function(isCopy)
{
if (!this.ptr)
return null;
@ -59,8 +59,23 @@ CWasmPointer.prototype.getReader = function()
}
len -= 4;
let buffer = new Uint8Array(Module["HEAP8"].buffer, this.ptr + 4, len);
return new CBinaryReader(buffer, 0, len);
let noCopyArray = new Uint8Array(Module["HEAP8"].buffer, this.ptr + 4, len);
if (!isCopy)
return noCopyArray;
let copyArray = new Uint8Array(len);
copyArray.set(noCopyArray);
return copyArray;
};
CWasmPointer.prototype.getReader = function()
{
let noCopyArray = this.getMemory(false);
if (!noCopyArray)
return null;
return new CBinaryReader(noCopyArray, 0, len);
};
var g_module_pointer = new CWasmPointer();
@ -139,7 +154,9 @@ CFile.prototype._SplitPages = function(memoryBuffer, arrayBufferChanges)
Module["_free"](pointer);
if (changesPtr)
Module["_free"](changesPtr);
return ptr;
g_module_pointer.ptr = ptr;
return g_module_pointer;
};
CFile.prototype._MergePages = function(buffer, maxID, prefixForm)