#include "DrawingFileEmbed.h" #include "../drawingfile.h" #include "../../raster/BgraFrame.h" JSSmart WasmMemoryToJS(BYTE* pWasmData) { if (NULL == pWasmData) return CJSContext::createNull(); int nLen = 0; memcpy(&nLen, pWasmData, sizeof(int)); if (4 >= nLen) return CJSContext::createNull(); return NSJSBase::CJSContext::createUint8Array(pWasmData + 4, nLen - 4, true); } CDrawingFileEmbed::CDrawingFileEmbed() { m_pFile = NULL; } CDrawingFileEmbed::~CDrawingFileEmbed() { RELEASEOBJECT(m_pFile); } JSSmart CDrawingFileEmbed::OpenFile(JSSmart sFile, JSSmart sPassword) { bool bResult = m_pFile->OpenFile(sFile->toStringW(), sPassword->isString() ? sPassword->toStringW() : L""); return CJSContext::createBool(bResult); } JSSmart CDrawingFileEmbed::CloseFile() { m_pFile->CloseFile(); return CJSContext::createUndefined(); } JSSmart CDrawingFileEmbed::GetType() { return CJSContext::createInt(m_pFile->GetType()); } JSSmart CDrawingFileEmbed::GetErrorCode() { return CJSContext::createInt(m_pFile->GetErrorCode()); } JSSmart CDrawingFileEmbed::GetInfo() { return WasmMemoryToJS(m_pFile->GetInfo()); } JSSmart CDrawingFileEmbed::GetPixmap(JSSmart nPageIndex, JSSmart nRasterW, JSSmart nRasterH, JSSmart nBackgroundColor) { int nW = nRasterW->toInt32(); int nH = nRasterH->toInt32(); BYTE* pData = m_pFile->GetPixmap(nPageIndex->toInt32(), nW, nH, nBackgroundColor->toInt32()); if (NULL == pData) return CJSContext::createNull(); return NSJSBase::CJSContext::createUint8Array(pData, 4 * nW * nH, true); } JSSmart CDrawingFileEmbed::DestroyPixmap(JSSmart typedArray) { if (!typedArray->isTypedArray()) return NULL; CBgraFrame oFrame; oFrame.put_Data(typedArray->toTypedArray()->getData().Data); // free on destructor return NULL; } JSSmart CDrawingFileEmbed::GetFontBinary(JSSmart Id) { if (0 != m_pFile->GetType()) return NULL; std::wstring sName = Id->toStringW(); std::wstring sFile = m_pFile->GetFontBinaryNative(sName); if (sFile.empty()) return NULL; return CJSContext::createUint8Array(sFile); } JSSmart CDrawingFileEmbed::GetGlyphs(JSSmart nPageIndex) { return WasmMemoryToJS(m_pFile->GetGlyphs(nPageIndex->toInt32())); } JSSmart CDrawingFileEmbed::GetLinks(JSSmart nPageIndex) { return WasmMemoryToJS(m_pFile->GetLinks(nPageIndex->toInt32())); } JSSmart CDrawingFileEmbed::GetStructure() { return WasmMemoryToJS(m_pFile->GetStructure()); } JSSmart CDrawingFileEmbed::GetInteractiveFormsInfo() { return WasmMemoryToJS(m_pFile->GetInteractiveFormsInfo()); } JSSmart CDrawingFileEmbed::GetInteractiveFormsFonts(JSSmart nTypeFonts) { return WasmMemoryToJS(m_pFile->GetInteractiveFormsFonts(nTypeFonts->toInt32())); } JSSmart CDrawingFileEmbed::GetInteractiveFormsAP(JSSmart nRasterW, JSSmart nRasterH, JSSmart nBackgroundColor, JSSmart nPageIndex, JSSmart nWidget, JSSmart nView, JSSmart nButtonView) { return WasmMemoryToJS(m_pFile->GetInteractiveFormsAP(nRasterW->toInt32(), nRasterH->toInt32(), nBackgroundColor->toInt32(), nPageIndex->toInt32(), nWidget->toInt32(), nView->toInt32(), nButtonView->toInt32())); } JSSmart CDrawingFileEmbed::GetButtonIcons(JSSmart nBackgroundColor, JSSmart nPageIndex, JSSmart bBase64, JSSmart nButtonWidget, JSSmart nIconView) { return WasmMemoryToJS(m_pFile->GetButtonIcons(nBackgroundColor->toInt32(), nPageIndex->toInt32(), bBase64->toInt32(), nButtonWidget->toInt32(), nIconView->toInt32())); } JSSmart CDrawingFileEmbed::GetAnnotationsInfo(JSSmart nPageIndex) { return WasmMemoryToJS(m_pFile->GetAnnotationsInfo(nPageIndex->toInt32())); } JSSmart CDrawingFileEmbed::GetAnnotationsAP(JSSmart nRasterW, JSSmart nRasterH, JSSmart nBackgroundColor, JSSmart nPageIndex, JSSmart nAnnot, JSSmart nView) { return WasmMemoryToJS(m_pFile->GetAnnotationsAP(nRasterW->toInt32(), nRasterH->toInt32(), nBackgroundColor->toInt32(), nPageIndex->toInt32(), nAnnot->toInt32(), nView->toInt32())); } JSSmart CDrawingFileEmbed::DestroyTextInfo() { m_pFile->DestroyTextInfo(); return CJSContext::createUndefined(); } JSSmart CDrawingFileEmbed::IsNeedCMap() { return CJSContext::createBool(false); } JSSmart CDrawingFileEmbed::ScanPage(JSSmart nPageIndex, JSSmart mode) { return WasmMemoryToJS(m_pFile->ScanPage(nPageIndex->toInt32(), mode->toInt32())); } JSSmart CDrawingFileEmbed::GetImageBase64(JSSmart rId) { std::string* pData = (std::string*)m_pFile->GetImageBase64(rId->toInt32()); if (!pData) return CJSContext::createNull(); JSSmart ret = CJSContext::createString(*pData); *pData = ""; return ret; } JSSmart CDrawingFileEmbed::FreeWasmData(JSSmart typedArray) { if (!typedArray->isTypedArray()) return NULL; BYTE* data = typedArray->toTypedArray()->getData().Data; typedArray->toTypedArray()->Detach(); data -= 4; // sizeof int (length in NSWasm::Data) free(data); return NULL; } JSSmart CDrawingFileEmbed::SplitPages(JSSmart arrPageIndexes, JSSmart data) { JSSmart 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 res = WasmMemoryToJS(m_pFile->SplitPages(pPages, nCountPages, changes.Data, (LONG)changes.Len)); if (pPages) delete [] pPages; return res; } JSSmart CDrawingFileEmbed::MergePages(JSSmart data, JSSmart nMaxID, JSSmart sPrefixForm) { bool result = false; if (m_pFile) { JSSmart 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, true); if (buffer.IsExternalize) buffer.Free(); } return CJSContext::createBool(result); } JSSmart CDrawingFileEmbed::UnmergePages() { if (m_pFile) return CJSContext::createBool(m_pFile->UnmergePages()); return CJSContext::createBool(false); } JSSmart CDrawingFileEmbed::RedactPage(JSSmart nPageIndex, JSSmart arrRedactBox, JSSmart dataFiller) { bool result = false; if (m_pFile) { int pageIndex = nPageIndex->toInt32(); JSSmart arrBox = arrRedactBox->toArray(); int nCountBox = arrBox->getCount(); double* pBox = NULL; if (0 < nCountBox) pBox = new double[nCountBox]; for (int i = 0; i < nCountBox; i++) pBox[i] = arrBox->get(i)->toDouble(); JSSmart dataPtr = dataFiller->toTypedArray(); CJSDataBuffer buffer = dataPtr->getData(); result = m_pFile->RedactPage(pageIndex, pBox, nCountBox / 8, buffer.Data, (int)buffer.Len, true); if (pBox) delete[] pBox; if (buffer.IsExternalize) buffer.Free(); } return CJSContext::createBool(result); } JSSmart CDrawingFileEmbed::UndoRedact() { if (m_pFile) return CJSContext::createBool(m_pFile->UndoRedact()); return CJSContext::createBool(false); } bool EmbedDrawingFile(JSSmart& context, IOfficeDrawingFile* pFile) { CJSContext::Embed(false); JSSmart oNativeDrawingFile = CJSContext::createEmbedObject("CDrawingFileEmbed"); context->GetGlobal()->set("g_native_drawing_file", oNativeDrawingFile); CDrawingFile* pDrFile = new CDrawingFile(pFile->GetFonts()); pDrFile->SetInternalFile(pFile); ((CDrawingFileEmbed*)oNativeDrawingFile->getNative())->m_pFile = pDrFile; return true; }