/* Entry points for johnmcm.com's QuarkXPress, PageMaker and FreeHand viewers: * libqxp, libpagemaker, libfreehand and librevenge, compiled to WebAssembly * as a module of their own (dtp.wasm), beside the Publisher, CorelDRAW and * Visio module (binding.cpp), whose structure and drawing generator it shares. * * Based on the OpenImprenta libmspub-wasm spike * (https://github.com/RicardoNigrelli/libmspub-wasm, MPL-2.0). * * SPDX-License-Identifier: MPL-2.0 * * Every entry point returns a plain object { ok, ..., error } instead of * throwing, so the JS side never deals with WebAssembly.Exception. Traps * (out-of-bounds, abort, stack overflow) still surface as a JS RuntimeError; * the page then discards the instance. * * view = inputBuffer(n) Uint8Array over n bytes of wasm memory; fill it at once * isSupportedInput(fmt) fmt: 0 QuarkXPress (.qxd, .qxt), 1 PageMaker (.pmd, .p65, .pm6), * 2 FreeHand (.fh and .fh3-.fh11, .ft*) * r = parseInput(fmt) { ok, pages, json, blobs, error?, unsupported? } * blob(i) Uint8Array view of picture i; copy it out before the next call * release() frees the blobs and any pending input */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "JSONDrawingGenerator.h" using emscripten::val; extern "C" const unsigned char oi_icudata_subset[]; namespace { // Register the filtered ICU data (icu/make-icudata.sh) before anything uses // ICU. Without it only algorithmic converters (UTF-8/16) are available. struct IcuDataInit { UErrorCode status = U_ZERO_ERROR; IcuDataInit() { udata_setCommonData(oi_icudata_subset, &status); } } g_icuDataInit; enum Format { QXP = 0, PMD = 1, FH = 2 }; bool supported(int format, librevenge::RVNGInputStream *input) { switch (format) { case QXP: return libqxp::QXPDocument::isSupported(input); case PMD: return libpagemaker::PMDocument::isSupported(input); case FH: return libfreehand::FreeHandDocument::isSupported(input); default: return false; } } bool parse(int format, librevenge::RVNGInputStream *input, librevenge::RVNGDrawingInterface *painter) { switch (format) { case QXP: return libqxp::QXPDocument::parse(input, painter) == libqxp::QXPDocument::RESULT_OK; case PMD: return libpagemaker::PMDocument::parse(input, painter); case FH: return libfreehand::FreeHandDocument::parse(input, painter); default: return false; } } val fail(const std::string &msg) { val r = val::object(); r.set("ok", false); r.set("error", msg); return r; } // The module's memory is capped (-sMAXIMUM_MEMORY): a file that needs more, // or a damaged one that asks for a huge block, ends here with a clear flag. val outOfMemory() { val r = fail("not enough memory for this file"); r.set("memory", true); return r; } /* * Set when an allocation failed during the parse. The libraries catch * exceptions of every kind themselves and report only that parsing failed, * so std::bad_alloc rarely reaches outOfMemory(); this handler notes it * first, then fails the allocation as usual. */ bool g_outOfMemory = false; void noteOutOfMemory() { g_outOfMemory = true; throw std::bad_alloc(); } /** Starts a parse: the flag cleared and the handler in place. */ void watchMemory() { g_outOfMemory = false; std::set_new_handler(noteOutOfMemory); } struct PendingState { unsigned char *input = nullptr; size_t inputSize = 0; std::vector blobs; } g_state; void freeInput() { std::free(g_state.input); g_state.input = nullptr; g_state.inputSize = 0; } void release() { freeInput(); std::vector().swap(g_state.blobs); } val inputBuffer(unsigned size) { release(); if (size == 0) return val::null(); g_state.input = static_cast(std::malloc(size)); if (!g_state.input) return val::null(); g_state.inputSize = size; return val(emscripten::typed_memory_view(size, g_state.input)); } bool isSupportedInput(int format) { if (!g_state.input) return false; try { librevenge::RVNGStringStream input(g_state.input, static_cast(g_state.inputSize)); return supported(format, &input); } catch (...) { return false; } } val parseInput(int format) { if (!g_state.input) return fail("no input: call inputBuffer() first"); watchMemory(); try { std::unique_ptr input( new librevenge::RVNGStringStream(g_state.input, static_cast(g_state.inputSize))); freeInput(); // the stream keeps its own copy if (!supported(format, input.get())) { val r = fail("unsupported"); r.set("unsupported", true); return r; } input->seek(0, librevenge::RVNG_SEEK_SET); oi::JSONDrawingGenerator generator; bool parsed = parse(format, input.get(), &generator); if (!parsed && g_outOfMemory) { release(); return outOfMemory(); } input.reset(); // free the file before serializing std::string json = generator.result(); g_state.blobs = generator.takeBlobs(); val r = val::object(); r.set("ok", parsed); r.set("pages", generator.pageCount()); r.set("json", json); r.set("blobs", static_cast(g_state.blobs.size())); if (!parsed) r.set("error", std::string("parse() returned false")); return r; } catch (const std::bad_alloc &) { release(); return outOfMemory(); } catch (const std::exception &e) { release(); return fail(std::string("C++ exception: ") + e.what()); } catch (...) { release(); return fail("unknown C++ exception"); } } val blob(unsigned index) { if (index >= g_state.blobs.size()) return val::null(); const std::vector &d = g_state.blobs[index].data; return val(emscripten::typed_memory_view(d.size(), d.data())); } double heapSize() { return static_cast(emscripten_get_heap_size()); } } // anonymous namespace EMSCRIPTEN_BINDINGS(dtp) { emscripten::function("inputBuffer", &inputBuffer); emscripten::function("isSupportedInput", &isSupportedInput); emscripten::function("parseInput", &parseInput); emscripten::function("blob", &blob); emscripten::function("release", &release); emscripten::function("heapSize", &heapSize); }