Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/thirdparty/zxing-cpp/wrappers/wasm/BarcodeWriter.cpp @ 2:b50eed0cc0ef upstream
ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4.
The directory name has changed: no version number in the expanded directory now.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:43:07 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:1d09e1dec1d9 | 2:b50eed0cc0ef |
|---|---|
| 1 /* | |
| 2 * Copyright 2016 Nu-book Inc. | |
| 3 */ | |
| 4 // SPDX-License-Identifier: Apache-2.0 | |
| 5 | |
| 6 #include "BarcodeFormat.h" | |
| 7 #include "MultiFormatWriter.h" | |
| 8 #include "BitMatrix.h" | |
| 9 #include "CharacterSet.h" | |
| 10 | |
| 11 #include <string> | |
| 12 #include <memory> | |
| 13 #include <exception> | |
| 14 #include <emscripten/bind.h> | |
| 15 #include <emscripten/val.h> | |
| 16 | |
| 17 #define STB_IMAGE_WRITE_IMPLEMENTATION | |
| 18 #include <stb_image_write.h> | |
| 19 | |
| 20 class ImageData | |
| 21 { | |
| 22 public: | |
| 23 uint8_t* const buffer; | |
| 24 const int length; | |
| 25 | |
| 26 ImageData(uint8_t* buf, int len) : buffer(buf), length(len) {} | |
| 27 ~ImageData() { STBIW_FREE(buffer); } | |
| 28 }; | |
| 29 | |
| 30 class WriteResult | |
| 31 { | |
| 32 std::shared_ptr<ImageData> _image; | |
| 33 std::string _error; | |
| 34 | |
| 35 public: | |
| 36 WriteResult(const std::shared_ptr<ImageData>& image) : _image(image) {} | |
| 37 WriteResult(std::string error) : _error(std::move(error)) {} | |
| 38 | |
| 39 std::string error() const { return _error; } | |
| 40 | |
| 41 emscripten::val image() const | |
| 42 { | |
| 43 if (_image != nullptr) | |
| 44 return emscripten::val(emscripten::typed_memory_view(_image->length, _image->buffer)); | |
| 45 else | |
| 46 return emscripten::val::null(); | |
| 47 } | |
| 48 }; | |
| 49 | |
| 50 WriteResult generateBarcode(std::wstring text, std::string format, std::string encoding, int margin, int width, int height, int eccLevel) | |
| 51 { | |
| 52 using namespace ZXing; | |
| 53 try { | |
| 54 auto barcodeFormat = BarcodeFormatFromString(format); | |
| 55 if (barcodeFormat == BarcodeFormat::None) | |
| 56 return {"Unsupported format: " + format}; | |
| 57 | |
| 58 MultiFormatWriter writer(barcodeFormat); | |
| 59 if (margin >= 0) | |
| 60 writer.setMargin(margin); | |
| 61 | |
| 62 CharacterSet charset = CharacterSetFromString(encoding); | |
| 63 if (charset != CharacterSet::Unknown) | |
| 64 writer.setEncoding(charset); | |
| 65 | |
| 66 if (eccLevel >= 0 && eccLevel <= 8) | |
| 67 writer.setEccLevel(eccLevel); | |
| 68 | |
| 69 auto buffer = ToMatrix<uint8_t>(writer.encode(text, width, height)); | |
| 70 | |
| 71 int len; | |
| 72 uint8_t* bytes = stbi_write_png_to_mem(buffer.data(), 0, buffer.width(), buffer.height(), 1, &len); | |
| 73 if (bytes == nullptr) | |
| 74 return {"Unknown error"}; | |
| 75 | |
| 76 return {std::make_shared<ImageData>(bytes, len)}; | |
| 77 } catch (const std::exception& e) { | |
| 78 return {e.what()}; | |
| 79 } catch (...) { | |
| 80 return {"Unknown error"}; | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 EMSCRIPTEN_BINDINGS(BarcodeWriter) | |
| 85 { | |
| 86 using namespace emscripten; | |
| 87 | |
| 88 class_<WriteResult>("WriteResult") | |
| 89 .property("image", &WriteResult::image) | |
| 90 .property("error", &WriteResult::error) | |
| 91 ; | |
| 92 | |
| 93 function("generateBarcode", &generateBarcode); | |
| 94 } |
