#include "DrawingFileEmbed.h" #include "../drawingfile.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; } 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; }