Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/thirdparty/zxing-cpp/wrappers/ios/Sources/Wrapper/Writer/ZXIBarcodeWriter.mm @ 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 // Copyright 2022 KURZ Digital Solutions GmbH | |
| 2 // | |
| 3 // SPDX-License-Identifier: Apache-2.0 | |
| 4 | |
| 5 #import <CoreGraphics/CoreGraphics.h> | |
| 6 #import "ZXIBarcodeWriter.h" | |
| 7 #import "ZXIWriterOptions.h" | |
| 8 #import "MultiFormatWriter.h" | |
| 9 #import "BitMatrix.h" | |
| 10 #import "BitMatrixIO.h" | |
| 11 #import "ZXIFormatHelper.h" | |
| 12 #import "ZXIErrors.h" | |
| 13 #import <iostream> | |
| 14 | |
| 15 using namespace ZXing; | |
| 16 | |
| 17 std::wstring NSStringToStringW(NSString* str) { | |
| 18 NSData* asData = [str dataUsingEncoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF32LE)]; | |
| 19 return std::wstring((wchar_t*)[asData bytes], [asData length] / | |
| 20 sizeof(wchar_t)); | |
| 21 } | |
| 22 | |
| 23 std::wstring NSDataToStringW(NSData *data) { | |
| 24 std::wstring s; | |
| 25 const unsigned char *bytes = (const unsigned char *) [data bytes]; | |
| 26 size_t len = [data length]; | |
| 27 for (int i = 0; i < len; ++i) { | |
| 28 s.push_back(bytes[i]); | |
| 29 } | |
| 30 return s; | |
| 31 } | |
| 32 | |
| 33 @implementation ZXIBarcodeWriter | |
| 34 | |
| 35 - (instancetype)init { | |
| 36 return [self initWithOptions: [[ZXIWriterOptions alloc] init]]; | |
| 37 } | |
| 38 | |
| 39 - (instancetype)initWithOptions:(ZXIWriterOptions*)options{ | |
| 40 self = [super init]; | |
| 41 self.options = options; | |
| 42 return self; | |
| 43 } | |
| 44 | |
| 45 -(CGImageRef)writeData:(NSData *)data | |
| 46 error:(NSError *__autoreleasing _Nullable *)error { | |
| 47 return [self encode: NSDataToStringW(data) | |
| 48 encoding: CharacterSet::BINARY | |
| 49 format: self.options.format | |
| 50 width: self.options.width | |
| 51 height: self.options.height | |
| 52 margin: self.options.margin | |
| 53 ecLevel: self.options.ecLevel | |
| 54 error: error]; | |
| 55 } | |
| 56 | |
| 57 -(CGImageRef)writeString:(NSString *)contents | |
| 58 error:(NSError *__autoreleasing _Nullable *)error { | |
| 59 return [self encode: NSStringToStringW(contents) | |
| 60 encoding: CharacterSet::UTF8 | |
| 61 format: self.options.format | |
| 62 width: self.options.width | |
| 63 height: self.options.height | |
| 64 margin: self.options.margin | |
| 65 ecLevel: self.options.ecLevel | |
| 66 error: error]; | |
| 67 } | |
| 68 | |
| 69 -(CGImageRef)encode:(std::wstring)content | |
| 70 encoding:(CharacterSet)encoding | |
| 71 format:(ZXIFormat)format | |
| 72 width:(int)width | |
| 73 height:(int)height | |
| 74 margin:(int)margin | |
| 75 ecLevel:(int)ecLevel | |
| 76 error:(NSError *__autoreleasing _Nullable *)error { | |
| 77 MultiFormatWriter writer { BarcodeFormatFromZXIFormat(format) }; | |
| 78 writer.setEncoding(encoding); | |
| 79 writer.setMargin(margin); | |
| 80 writer.setEccLevel(ecLevel); | |
| 81 // Catch exception for invalid formats | |
| 82 try { | |
| 83 BitMatrix bitMatrix = writer.encode(content, width, height); | |
| 84 return [self inflate:&bitMatrix]; | |
| 85 } catch(std::exception &e) { | |
| 86 SetNSError(error, ZXIWriterError, e.what()); | |
| 87 return nil; | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 -(CGImageRef)inflate:(BitMatrix *)bitMatrix { | |
| 92 int realWidth = bitMatrix->width(); | |
| 93 int realHeight = bitMatrix->height(); | |
| 94 | |
| 95 #ifdef DEBUG | |
| 96 std::cout << ToString(*bitMatrix, 'X', ' ', false, false); | |
| 97 #endif | |
| 98 | |
| 99 NSMutableData *resultAsNSData = [[NSMutableData alloc] initWithLength:realWidth * realHeight]; | |
| 100 size_t index = 0; | |
| 101 uint8_t *bytes = (uint8_t*)resultAsNSData.mutableBytes; | |
| 102 for (int y = 0; y < realHeight; ++y) { | |
| 103 for (int x = 0; x < realWidth; ++x) { | |
| 104 bytes[index] = bitMatrix->get(x, y) ? 0 : 255; | |
| 105 ++index; | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericGray); | |
| 110 | |
| 111 return CGImageCreate(realWidth, | |
| 112 realHeight, | |
| 113 8, | |
| 114 8, | |
| 115 realWidth, | |
| 116 colorSpace, | |
| 117 kCGBitmapByteOrderDefault, | |
| 118 CGDataProviderCreateWithCFData((CFDataRef)resultAsNSData), | |
| 119 NULL, | |
| 120 YES, | |
| 121 kCGRenderingIntentDefault); | |
| 122 } | |
| 123 | |
| 124 @end |
